OpenNN  2.2
Open Neural Networks Library
mathematical_model.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* M A T H E M A T I C A L M O D E L C L A S S */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 // OpenNN includes
15 
16 #include "mathematical_model.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
25 
27 {
28  set_default();
29 }
30 
31 
32 // XML CONSTRUCTOR
33 
37 
38 MathematicalModel::MathematicalModel(const tinyxml2::XMLDocument& mathematical_model_document)
39 {
40  set_default();
41 
42  from_XML(mathematical_model_document);
43 }
44 
45 
46 // FILE CONSTRUCTOR
47 
51 
52 MathematicalModel::MathematicalModel(const std::string& file_name)
53 {
54  set_default();
55 
56  load(file_name);
57 }
58 
59 
60 // COPY CONSTRUCTOR
61 
65 
67 {
68  set(other_mathematical_model);
69 }
70 
71 
72 // DESTRUCTOR
73 
76 
78 {
79 }
80 
81 
82 // ASSIGNMENT OPERATOR
83 
87 
89 {
90  if(this != &other_mathematical_model)
91  {
93  dependent_variables_number = other_mathematical_model.dependent_variables_number;
94 
95  display = other_mathematical_model.display;
96  }
97 
98  return(*this);
99 }
100 
101 
102 // EQUAL TO OPERATOR
103 
108 
109 bool MathematicalModel::operator == (const MathematicalModel& other_mathematical_model) const
110 {
111  if(independent_variables_number == other_mathematical_model.independent_variables_number
112  && dependent_variables_number == other_mathematical_model.dependent_variables_number
113  && display == other_mathematical_model.display)
114  {
115  return(true);
116  }
117  else
118  {
119  return(false);
120  }
121 }
122 
123 
124 // METHODS
125 
126 // const size_t& get_independent_variables_number(void) const method
127 
129 
131 {
133 }
134 
135 
136 // size_t get_dependent_variables_number(void) const method
137 
139 
141 {
143 }
144 
145 
146 // size_t count_variables_number(void) const method
147 
150 
152 {
154 }
155 
156 
157 // const bool get_display(void) method
158 
161 
162 const bool& MathematicalModel::get_display(void) const
163 {
164  return(display);
165 }
166 
167 
168 // void set(const MathematicalModel&) method
169 
172 
173 void MathematicalModel::set(const MathematicalModel& other_mathematical_model)
174 {
176  dependent_variables_number = other_mathematical_model.dependent_variables_number;
177 
178  display = other_mathematical_model.display;
179 }
180 
181 
182 // void set_independent_variables_number(const size_t&) method
183 
186 
187 void MathematicalModel::set_independent_variables_number(const size_t& new_independent_variables_number)
188 {
189  independent_variables_number = new_independent_variables_number;
190 }
191 
192 
193 // void set_dependent_variables_number(const size_t&) method
194 
197 
198 void MathematicalModel::set_dependent_variables_number(const size_t& new_dependent_variables_number)
199 {
200  dependent_variables_number = new_dependent_variables_number;
201 }
202 
203 
204 // void set_default(void) method
205 
212 
214 {
217 
218  display = true;
219 }
220 
221 
222 // void set_display(const bool&) const method
223 
228 
229 void MathematicalModel::set_display(const bool& new_display)
230 {
231  display = new_display;
232 }
233 
234 
235 // Matrix<double> calculate_solutions(const NeuralNetwork&) const method
236 
240 
242 {
243  std::ostringstream buffer;
244 
245  buffer << "OpenNN Exception: MathematicalModel class.\n"
246  << "Matrix<double> calculate_solutions(const NeuralNetwork&) const method.\n"
247  << "This method has not been derived.\n";
248 
249  throw std::logic_error(buffer.str());
250 }
251 
252 
253 // Vector<double> calculate_final_solutions(const NeuralNetwork&) const method
254 
258 
260 {
261  std::ostringstream buffer;
262 
263  buffer << "OpenNN Exception: MathematicalModel class.\n"
264  << "Vector<double> calculate_final_solutions(const NeuralNetwork&) const method.\n"
265  << "This method has not been derived.\n";
266 
267  throw std::logic_error(buffer.str());
268 }
269 
270 
271 // Matrix<double> calculate_dependent_variables(const NeuralNetwork&, const Matrix<double>&) const method
272 
275 
277 {
278  std::ostringstream buffer;
279 
280  buffer << "OpenNN Exception: MathematicalModel class.\n"
281  << "Matrix<double> calculate_dependent_variables(const NeuralNetwork&, const Matrix<double>&) const method.\n"
282  << "This method has not been derived.\n";
283 
284  throw std::logic_error(buffer.str());
285 }
286 
287 
288 // std::string to_string(void) const method
289 
291 
292 std::string MathematicalModel::to_string(void) const
293 {
294  std::ostringstream buffer;
295 
296  buffer << "Mathematical model\n"
297  << "Independent variables number: " << independent_variables_number << "\n"
298  << "Dependent variables number: " << dependent_variables_number << "\n"
299  << "Display: " << display << "\n";
300 
301  return(buffer.str());
302 }
303 
304 
305 // void print(void) const method
306 
308 
309 void MathematicalModel::print(void) const
310 {
311  std::cout << to_string();
312 }
313 
314 
315 
316 // tinyxml2::XMLDocument* to_XML(void) const method
317 
320 
321 tinyxml2::XMLDocument* MathematicalModel::to_XML(void) const
322 {
323  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
324 
325  std::ostringstream buffer;
326 
327  tinyxml2::XMLElement* mathematical_model_element = document->NewElement("MathematicalModel");
328 
329  document->InsertFirstChild(mathematical_model_element);
330 
331  // Independent variables number
332  {
333  tinyxml2::XMLElement* independent_variables_number_element = document->NewElement("IndependentVariablesNumber");
334  mathematical_model_element->LinkEndChild(independent_variables_number_element);
335 
336  buffer.str("");
338 
339  tinyxml2::XMLText* independent_variables_number_text = document->NewText(buffer.str().c_str());
340  independent_variables_number_element->LinkEndChild(independent_variables_number_text);
341  }
342 
343  // Dependent variables number
344  {
345  tinyxml2::XMLElement* dependent_variables_number_element = document->NewElement("DependentVariablesNumber");
346  mathematical_model_element->LinkEndChild(dependent_variables_number_element);
347 
348  buffer.str("");
349  buffer << dependent_variables_number;
350 
351  tinyxml2::XMLText* dependent_variables_number_text = document->NewText(buffer.str().c_str());
352  dependent_variables_number_element->LinkEndChild(dependent_variables_number_text);
353  }
354 
355  // Display
356  {
357  tinyxml2::XMLElement* display_element = document->NewElement("Display");
358  mathematical_model_element->LinkEndChild(display_element);
359 
360  buffer.str("");
361  buffer << display;
362 
363  tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
364  display_element->LinkEndChild(display_text);
365  }
366 
367  return(document);
368 }
369 
370 
371 // void from_XML(const tinyxml2::XMLDocument&) method
372 
375 
376 void MathematicalModel::from_XML(const tinyxml2::XMLDocument& document)
377 {
378  const tinyxml2::XMLElement* root_element = document.FirstChildElement("MathematicalModel");
379 
380  std::ostringstream buffer;
381 
382  if(!root_element)
383  {
384  buffer << "OpenNN Exception: MathematicalModel class.\n"
385  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
386  << "Unkown root element.\n";
387 
388  throw std::logic_error(buffer.str());
389  }
390 
391  // Independent variables number
392  {
393  const tinyxml2::XMLElement* element = root_element->FirstChildElement("IndependentVariablesNumber");
394 
395  if(element)
396  {
397  const char* text = element->GetText();
398 
399  if(text)
400  {
401  try
402  {
404  }
405  catch(const std::logic_error& e)
406  {
407  std::cout << e.what() << std::endl;
408  }
409  }
410  }
411  }
412 
413  // Dependent variables number
414  {
415  const tinyxml2::XMLElement* element = root_element->FirstChildElement("DependentVariablesNumber");
416 
417  if(element)
418  {
419  const char* text = element->GetText();
420 
421  if(text)
422  {
423  try
424  {
425  set_dependent_variables_number(atoi(text));
426  }
427  catch(const std::logic_error& e)
428  {
429  std::cout << e.what() << std::endl;
430  }
431  }
432  }
433  }
434 
435  // Display
436  {
437  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Display");
438 
439  if(element)
440  {
441  const char* text = element->GetText();
442 
443  if(text)
444  {
445  try
446  {
447  const std::string string(text);
448 
449  set_display(string != "0");
450  }
451  catch(const std::logic_error& e)
452  {
453  std::cout << e.what() << std::endl;
454  }
455  }
456  }
457  }
458 }
459 
460 
461 // void save(const std::string&) const method
462 
465 
466 void MathematicalModel::save(const std::string& file_name) const
467 {
468  tinyxml2::XMLDocument* document = to_XML();
469 
470  document->SaveFile(file_name.c_str());
471 
472  delete document;
473 }
474 
475 
476 // void load(const std::string&) method
477 
480 
481 void MathematicalModel::load(const std::string& file_name)
482 {
483  std::ostringstream buffer;
484 
485  tinyxml2::XMLDocument document;
486 
487  if(document.LoadFile(file_name.c_str()))
488  {
489  buffer << "OpenNN Exception: MathematicalModel class.\n"
490  << "void load(const std::string&) method.\n"
491  << "Cannot load XML file " << file_name << ".\n";
492 
493  throw std::logic_error(buffer.str());
494  }
495 
496  from_XML(document);
497 }
498 
499 
500 // void save_data(const NeuralNetwork&, const std::string&) const method
501 
503 
504 void MathematicalModel::save_data(const NeuralNetwork&, const std::string&) const
505 {
506  std::ostringstream buffer;
507 
508  buffer << "OpenNN Exception: MathematicalModel class.\n"
509  << "void save_data(const NeuralNetwork&, const std::string&) const method.\n"
510  << "This method has not been derived.\n";
511 
512  throw std::logic_error(buffer.str());
513 }
514 
515 }
516 
517 
518 // OpenNN: Open Neural Networks Library.
519 // Copyright (c) 2005-2015 Roberto Lopez.
520 //
521 // This library is free software; you can redistribute it and/or
522 // modify it under the terms of the GNU Lesser General Public
523 // License as published by the Free Software Foundation; either
524 // version 2.1 of the License, or any later version.
525 //
526 // This library is distributed in the hope that it will be useful,
527 // but WITHOUT ANY WARRANTY; without even the implied warranty of
528 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
529 // Lesser General Public License for more details.
530 
531 // You should have received a copy of the GNU Lesser General Public
532 // License along with this library; if not, write to the Free Software
533 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
virtual Matrix< double > calculate_solutions(const NeuralNetwork &) const
void print(void) const
This method outputs to the console the string representation of the mathematical model.
virtual void from_XML(const tinyxml2::XMLDocument &)
const size_t & get_independent_variables_number(void) const
Returns the number of independent variables in the mathematical model.
void set(const MathematicalModel &)
virtual bool operator==(const MathematicalModel &) const
const size_t & get_dependent_variables_number(void) const
Returns the number of dependent variables in the mathematical model.
size_t count_variables_number(void) const
virtual tinyxml2::XMLDocument * to_XML(void) const
const bool & get_display(void) const
virtual Vector< double > calculate_final_solutions(const NeuralNetwork &) const
void save(const std::string &) const
virtual MathematicalModel & operator=(const MathematicalModel &)
virtual std::string to_string(void) const
Returns a string representation of the current mathematical model object.
virtual void save_data(const NeuralNetwork &, const std::string &) const
size_t dependent_variables_number
Number of dependent variables defining the mathematical model.
bool display
Flag for displaying warnings.
void set_dependent_variables_number(const size_t &)
virtual Matrix< double > calculate_dependent_variables(const NeuralNetwork &, const Matrix< double > &) const
size_t independent_variables_number
Number of independent variables defining the mathematical model.
void load(const std::string &)
void set_independent_variables_number(const size_t &)