OpenNN  2.2
Open Neural Networks Library
final_solutions_error.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* F I N A L S O L U T I O N S E R R O R 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 "final_solutions_error.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
26 
28  : PerformanceTerm()
29 {
31 
32  set_default();
33 }
34 
35 
36 // NEURAL NETWORK CONSTRUCTOR
37 
42 
44 : PerformanceTerm(new_neural_network_pointer)
45 {
47 
48  set_default();
49 }
50 
51 
52 // MATHEMATICAL MODEL CONSTRUCTOR
53 
58 
60 : PerformanceTerm(new_mathematical_model_pointer)
61 {
63 
64  set_default();
65 }
66 
67 
68 // NEURAL NETWORK AND MATHEMATICAL MODEL CONSTRUCTOR
69 
75 
76 FinalSolutionsError::FinalSolutionsError(NeuralNetwork* new_neural_network_pointer, MathematicalModel* new_mathematical_model_pointer)
77 : PerformanceTerm(new_neural_network_pointer, new_mathematical_model_pointer)
78 {
80 
81  set_default();
82 }
83 
84 
85 // XML CONSTRUCTOR
86 
91 
92 FinalSolutionsError::FinalSolutionsError(const tinyxml2::XMLDocument& final_state_error_document)
93  : PerformanceTerm(final_state_error_document)
94 {
96 
97  set_default();
98 
99  from_XML(final_state_error_document);
100 }
101 
102 
103 // COPY CONSTRUCTOR
104 
108 
110  : PerformanceTerm()
111 {
112 // set(other_final_solutions_error);
113 
114  neural_network_pointer = other_final_solutions_error.neural_network_pointer;
115 
116  data_set_pointer = other_final_solutions_error.data_set_pointer;
117 
118  mathematical_model_pointer = other_final_solutions_error.mathematical_model_pointer;
119 
120  if(other_final_solutions_error.numerical_differentiation_pointer)
121  {
123  }
124 
125  display = other_final_solutions_error.display;
126 
128 
129  target_final_solutions = other_final_solutions_error.target_final_solutions;
130 
131 }
132 
133 
134 // DESTRUCTOR
135 
137 
139 {
140 }
141 
142 
143 // ASSIGNMENT OPERATOR
144 
145 // FinalSolutionsError& operator = (const FinalSolutionsError&) method
146 
148 
150 {
151  if(this != &other_final_solutions_error)
152  {
153  *neural_network_pointer = *other_final_solutions_error.neural_network_pointer;
154  *data_set_pointer = *other_final_solutions_error.data_set_pointer;
155  *mathematical_model_pointer = *other_final_solutions_error.mathematical_model_pointer;
157  display = other_final_solutions_error.display;
158 
160  target_final_solutions = other_final_solutions_error.target_final_solutions;
161  }
162 
163  return(*this);
164 }
165 
166 
167 // EQUAL TO OPERATOR
168 
169 // bool operator == (const FinalSolutionsError&) const method
170 
173 
174 bool FinalSolutionsError::operator == (const FinalSolutionsError& other_final_solutions_error) const
175 {
176  if(*neural_network_pointer == *other_final_solutions_error.neural_network_pointer
177  && *mathematical_model_pointer == *other_final_solutions_error.mathematical_model_pointer
178  && *numerical_differentiation_pointer == *other_final_solutions_error.numerical_differentiation_pointer
179  && display == other_final_solutions_error.display
180  && final_solutions_errors_weights == other_final_solutions_error.final_solutions_errors_weights
181  && target_final_solutions == other_final_solutions_error.target_final_solutions)
182  {
183  return(true);
184  }
185  else
186  {
187  return(false);
188  }
189 }
190 
191 
192 // METHODS
193 
194 // const Vector<double>& get_final_solutions_errors_weights(void) const method
195 
197 
199 {
201 }
202 
203 
204 // const Vector<double>& get_target_final_solutions(void) const method
205 
207 
209 {
210  return(target_final_solutions);
211 }
212 
213 
214 // void set(void) method
215 
219 
221 {
222  neural_network_pointer = NULL;
223  data_set_pointer = NULL;
225 
227 
229 
230  set_default();
231 }
232 
233 
234 // void set(NeuralNetwork*) method
235 
237 
239 {
240 }
241 
242 
243 // void set(MathematicalModel*) method
244 
246 
248 {
249 }
250 
251 
252 // void set(NeuralNetwork*, MathematicalModel*) method
253 
255 
257 {
258 }
259 
260 
261 // void set(const FinalSolutionsError&) method
262 
263 // Sets this final solutions error object with the members from another object of the same class.
264 // @param other_final_solutions_error Final solutions error object to be copied.
265 
266 //void FinalSolutionsError::set(const FinalSolutionsError& other_final_solutions_error)
267 //{
268 // neural_network_pointer = other_final_solutions_error.neural_network_pointer;
269 
270 // data_set_pointer = other_final_solutions_error.data_set_pointer;
271 
272 // mathematical_model_pointer = other_final_solutions_error.mathematical_model_pointer;
273 
274 // if(other_final_solutions_error.numerical_differentiation_pointer)
275 // {
276 // numerical_differentiation_pointer = new NumericalDifferentiation(*other_final_solutions_error.numerical_differentiation_pointer);
277 // }
278 
279 // display = other_final_solutions_error.display;
280 
281 // final_solutions_errors_weights = other_final_solutions_error.final_solutions_errors_weights;
282 
283 // target_final_solutions = other_final_solutions_error.target_final_solutions;
284 //}
285 
286 
287 // void set_mathematical_model_pointer(MathematicalModel*) method
288 
291 
293 {
294  mathematical_model_pointer = new_mathematical_model_pointer;
295 
297  {
298  const size_t dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
299 
300  target_final_solutions.set(dependent_variables_number, 0.0);
301 
302  final_solutions_errors_weights.set(dependent_variables_number, 1.0);
303  }
304 }
305 
306 
307 // void set_final_solutions_errors_weights(const Vector<double>&) method
308 
311 
312 void FinalSolutionsError::set_final_solutions_errors_weights(const Vector<double>& new_final_solutions_errors_weights)
313 {
314  final_solutions_errors_weights = new_final_solutions_errors_weights;
315 }
316 
317 
318 // void set_final_solution_error_weight(const size_t&, const double&) method
319 
323 
324 void FinalSolutionsError::set_final_solution_error_weight(const size_t& i, const double& new_final_solution_error_weight)
325 {
326  final_solutions_errors_weights[i] = new_final_solution_error_weight;
327 }
328 
329 
330 // void set_target_final_solutions(const Vector<double>&) method
331 
334 
336 {
337  target_final_solutions = new_target_final_solutions;
338 }
339 
340 
341 // void set_target_final_solution(const size_t& const double&) method
342 
346 
347 void FinalSolutionsError::set_target_final_solution(const size_t& i, const double& new_target_final_solution)
348 {
349  target_final_solutions[i] = new_target_final_solution;
350 }
351 
352 
353 // void set_default(void)
354 
361 
363 {
365  {
366  const size_t dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
367 
368  target_final_solutions.set(dependent_variables_number, 0.0);
369 
370  final_solutions_errors_weights.set(dependent_variables_number, 1.0);
371  }
372  else
373  {
375 
377  }
378 
379  display = true;
380 }
381 
382 
383 // void check(void) const method
384 
388 
390 {
391  std::ostringstream buffer;
392 
393  // Neural network stuff
394 
396  {
397  buffer << "OpenNN Exception: FinalSolutionsError class.\n"
398  << "void check(void) const method.\n"
399  << "Pointer to neural network is NULL.\n";
400 
401  throw std::logic_error(buffer.str());
402  }
403 
404  const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
405 
406  if(!multilayer_perceptron_pointer)
407  {
408  buffer << "OpenNN Exception: FinalSolutionsError class.\n"
409  << "void check(void) const method.\n"
410  << "Pointer to multilayer perceptron is NULL.\n";
411 
412  throw std::logic_error(buffer.str());
413  }
414 
415  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
416  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
417 
418  if(inputs_number == 0)
419  {
420  buffer << "OpenNN Exception: FinalSolutionsError class.\n"
421  << "void check(void) const method.\n"
422  << "Number of inputs in multilayer perceptron object is zero.\n";
423 
424  throw std::logic_error(buffer.str());
425  }
426 
427  if(outputs_number == 0)
428  {
429  buffer << "OpenNN Exception: FinalSolutionsError class.\n"
430  << "void check(void) const method.\n"
431  << "Number of outputs in multilayer perceptron object is zero.\n";
432 
433  throw std::logic_error(buffer.str());
434  }
435 
436  // Mathematical model stuff
437 
439  {
440  buffer << "OpenNN Exception: FinalSolutionsError class.\n"
441  << "void check(void) const method.\n"
442  << "Pointer to mathematical model is NULL.\n";
443 
444  throw std::logic_error(buffer.str());
445  }
446 
447  // Final solutions error stuff
448 
449  const size_t dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
450 
451  const size_t target_final_solutions_size = target_final_solutions.size();
452 
453  if(target_final_solutions_size != dependent_variables_number)
454  {
455  std::ostringstream buffer;
456 
457  buffer << "OpenNN Exception: FinalSolutionsError class." << std::endl
458  << "double calculate_performance(void) const method." << std::endl
459  << "Size of target final solutions must be equal to number of dependent variables." << std::endl;
460 
461  throw std::logic_error(buffer.str());
462  }
463 
464 }
465 
466 
467 // Vector<double> calculate_performance(void) const method
468 
470 {
471  // Control sentence
472 
473  #ifndef NDEBUG
474 
475  check();
476 
477  #endif
478 
479  // Final state error stuff
480 
481  const size_t independent_variables_number = mathematical_model_pointer->get_independent_variables_number();
482  const size_t dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
483 
485 
486  const Vector<double> dependent_variables_final_solutions = final_solutions.take_out(independent_variables_number, dependent_variables_number);
487 
488  const Vector<double> final_solutions_errors = dependent_variables_final_solutions - target_final_solutions;
489 
490  return((final_solutions_errors_weights*final_solutions_errors*final_solutions_errors).calculate_sum());
491 }
492 
493 
494 // double calculate_performance(const Vector<double>&) const method
495 
497 {
498  return(0.0);
499 }
500 
501 
502 // std::string write_performance_term_type(void) const method
503 
505 
507 {
508  return("FINAL_SOLUTIONS_ERROR");
509 }
510 
511 
512 // std::string write_information(void) const method
513 
515 {
516  std::ostringstream buffer;
517 
518  const size_t independent_variables_number = mathematical_model_pointer->get_independent_variables_number();
519  const size_t dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
520 
522 
523  const Vector<double> dependent_variables_final_solutions = final_solutions.take_out(independent_variables_number, dependent_variables_number);
524 
525  const Vector<double> final_solutions_errors = dependent_variables_final_solutions - target_final_solutions;
526 
527  const double performance = (final_solutions_errors_weights*final_solutions_errors*final_solutions_errors).calculate_sum();
528 
529  buffer << "Final solutions error\n"
530  << "Target final solutions: " << target_final_solutions << "\n"
531  << "Final solutions: " << dependent_variables_final_solutions << "\n"
532  << "performance: " << performance << "\n";
533 
534  return(buffer.str());
535 }
536 
537 
538 // tinyxml2::XMLDocument* to_XML(void) method method
539 
541 
542 tinyxml2::XMLDocument* FinalSolutionsError::to_XML(void) const
543 {
544  std::ostringstream buffer;
545 
546  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
547 
548  // Final solutions error
549 
550  tinyxml2::XMLElement* final_solutions_error_element = document->NewElement("FinalSolutionsError");
551 
552  document->InsertFirstChild(final_solutions_error_element);
553 
554  // Numerical differentiation
555  {
557  {
558  tinyxml2::XMLElement* element = numerical_differentiation_pointer->to_XML()->FirstChildElement();
559 
560  if(element)
561  {
562  final_solutions_error_element->LinkEndChild(element);
563  }
564  }
565  }
566 
567  // Final solutions errors weights
568  {
569  tinyxml2::XMLElement* element = document->NewElement("FinalSolutionsErrorsWeights");
570  final_solutions_error_element->LinkEndChild(element);
571 
572  buffer.str("");
574 
575  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
576  element->LinkEndChild(text);
577  }
578 
579  // Target final solution
580  {
581  tinyxml2::XMLElement* element = document->NewElement("TargetFinalSolution");
582  final_solutions_error_element->LinkEndChild(element);
583 
584  buffer.str("");
585  buffer << target_final_solutions;
586 
587  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
588  element->LinkEndChild(text);
589  }
590 
591  // Display
592  {
593  tinyxml2::XMLElement* display_element = document->NewElement("Display");
594  final_solutions_error_element->LinkEndChild(display_element);
595 
596  buffer.str("");
597  buffer << display;
598 
599  tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
600  display_element->LinkEndChild(display_text);
601  }
602 
603  return(document);
604 }
605 
606 
607 // void from_XML(const tinyxml2::XMLDocument&) method
608 
611 
612 void FinalSolutionsError::from_XML(const tinyxml2::XMLDocument& document)
613 {
614  const tinyxml2::XMLElement* root_element = document.FirstChildElement("FinalSolutionsError");
615 
616  if(!root_element)
617  {
618  std::ostringstream buffer;
619 
620  buffer << "OpenNN Exception: FinalSolutionsError class.\n"
621  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
622  << "Final solutions error element is NULL.\n";
623 
624  throw std::logic_error(buffer.str());
625  }
626 
627  // Display
628  {
629  const tinyxml2::XMLElement* display_element = root_element->FirstChildElement("Display");
630 
631  if(display_element)
632  {
633  const std::string new_display_string = display_element->GetText();
634 
635  try
636  {
637  set_display(new_display_string != "0");
638  }
639  catch(const std::logic_error& e)
640  {
641  std::cout << e.what() << std::endl;
642  }
643  }
644  }
645 }
646 
647 
648 }
649 
650 
651 // OpenNN: Open Neural Networks Library.
652 // Copyright (c) 2005-2015 Roberto Lopez.
653 //
654 // This library is free software; you can redistribute it and/or
655 // modify it under the terms of the GNU Lesser General Public
656 // License as published by the Free Software Foundation; either
657 // version 2.1 of the License, or any later version.
658 //
659 // This library is distributed in the hope that it will be useful,
660 // but WITHOUT ANY WARRANTY; without even the implied warranty of
661 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
662 // Lesser General Public License for more details.
663 // You should have received a copy of the GNU Lesser General Public
664 // License along with this library; if not, write to the Free Software
665 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
void set_final_solution_error_weight(const size_t &, const double &)
const Vector< double > & get_target_final_solutions(void) const
Returns the desired final state of each dependent variable.
virtual ~FinalSolutionsError(void)
Destructor.
Vector< T > take_out(const size_t &, const size_t &) const
Definition: vector.h:4928
MathematicalModel * mathematical_model_pointer
Pointer to a mathematical model object.
double calculate_performance(void) const
Returns the performance value of the performance term.
size_t get_inputs_number(void) const
Returns the number of inputs to the multilayer perceptron.
void set(void)
Sets the size of a vector to zero.
Definition: vector.h:656
size_t get_outputs_number(void) const
Returns the number of outputs neurons in the multilayer perceptron.
const size_t & get_independent_variables_number(void) const
Returns the number of independent variables in the mathematical model.
Vector< double > final_solutions_errors_weights
void set_display(const bool &)
const size_t & get_dependent_variables_number(void) const
Returns the number of dependent variables in the mathematical model.
std::string write_performance_term_type(void) const
Returns a string with the name of the final solutions error performance type, "FINAL_SOLUTIONS_ERROR"...
const Vector< double > & get_final_solutions_errors_weights(void) const
Returns the weight values for each error in the final solutions.
MultilayerPerceptron * get_multilayer_perceptron_pointer(void) const
Returns a pointer to the multilayer perceptron composing this neural network.
virtual Vector< double > calculate_final_solutions(const NeuralNetwork &) const
void construct_numerical_differentiation(void)
This method constructs the numerical differentiation object which composes the performance term class...
void set_mathematical_model_pointer(MathematicalModel *)
NeuralNetwork * neural_network_pointer
Pointer to a multilayer perceptron object.
NumericalDifferentiation * numerical_differentiation_pointer
Numerical differentiation object.
void set_target_final_solutions(const Vector< double > &)
FinalSolutionsError & operator=(const FinalSolutionsError &)
Assignment operator.
void set_target_final_solution(const size_t &, const double &)
bool display
Display messages to screen.
void from_XML(const tinyxml2::XMLDocument &)
bool operator==(const FinalSolutionsError &) const
DataSet * data_set_pointer
Pointer to a data set object.
tinyxml2::XMLDocument * to_XML(void) const
Returns a representation of the sum squared error object, in XML format.
std::string write_information(void) const
tinyxml2::XMLDocument * to_XML(void) const
Serializes this numerical differentiation object into a XML document->
void set_final_solutions_errors_weights(const Vector< double > &)