OpenNN  2.2
Open Neural Networks Library
solutions_error.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* 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 "solutions_error.h"
17 
18 namespace OpenNN
19 {
20 
21 
22 // DEFAULT CONSTRUCTOR
23 
27 
29 {
31 
32  set_default();
33 }
34 
35 
36 // NEURAL NETWORK CONSTRUCTOR
37 
42 
43 SolutionsError::SolutionsError(NeuralNetwork* new_neural_network_pointer)
44 : PerformanceTerm(new_neural_network_pointer)
45 {
47 
48  set_default();
49 }
50 
51 
52 // MATHEMATICAL MODEL CONSTRUCTOR
53 
58 
59 SolutionsError::SolutionsError(MathematicalModel* new_mathematical_model_pointer)
60 : PerformanceTerm(new_mathematical_model_pointer)
61 {
63 
64  set_default();
65 }
66 
67 
68 // NEURAL NETWORK AND MATHEMATICAL MODEL CONSTRUCTOR
69 
75 
76 SolutionsError::SolutionsError(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 SolutionsError::SolutionsError(const tinyxml2::XMLDocument& solutions_error_document)
93  : PerformanceTerm(solutions_error_document)
94 {
95  set_default();
96 
97  from_XML(solutions_error_document);
98 }
99 
100 
101 // COPY CONSTRUCTOR
102 
106 
107 SolutionsError::SolutionsError(const SolutionsError& other_solutions_error)
108  : PerformanceTerm()
109 {
110 // set_default();
111 
112 // set(other_solutions_error);
113 
114  neural_network_pointer = other_solutions_error.neural_network_pointer;
115 
116  data_set_pointer = other_solutions_error.data_set_pointer;
117 
119 
120  if(other_solutions_error.numerical_differentiation_pointer)
121  {
123  }
124 
125  display = other_solutions_error.display;
126 
127  solutions_errors_weights = other_solutions_error.solutions_errors_weights;
128 
129 }
130 
131 
132 // DESTRUCTOR
133 
137 
139 {
140 }
141 
142 
143 // ASSIGNMENT OPERATOR
144 
145 // FinalSolutionsError& operator = (const FinalSolutionsError&) method
146 
150 
152 {
153  if(this != &other_solutions_error)
154  {
155  *neural_network_pointer = *other_solutions_error.neural_network_pointer;
156  *data_set_pointer = *other_solutions_error.data_set_pointer;
157  *mathematical_model_pointer = *other_solutions_error.mathematical_model_pointer;
159  display = other_solutions_error.display;
160 
161  solutions_error_method = other_solutions_error.solutions_error_method;
162  solutions_errors_weights = other_solutions_error.solutions_errors_weights;
163  }
164 
165  return(*this);
166 }
167 
168 
169 // EQUAL TO OPERATOR
170 
171 // bool operator == (const FinalSolutionsError&) const method
172 
175 
176 bool SolutionsError::operator == (const SolutionsError& other_solutions_error) const
177 {
178  if(*neural_network_pointer == *other_solutions_error.neural_network_pointer
179  && *mathematical_model_pointer == *other_solutions_error.mathematical_model_pointer
181  && display == other_solutions_error.display
182  && solutions_errors_weights == other_solutions_error.solutions_errors_weights)
183  {
184  return(true);
185  }
186  else
187  {
188  return(false);
189  }
190 }
191 
192 
193 // METHODS
194 
195 // const SolutionsErrorMethod& get_solutions_error_method(void) const method
196 
198 
200 {
201  return(solutions_error_method);
202 }
203 
204 
205 // string:std: write_solutions_error_method(void) const method
206 
208 
210 {
211  if(solutions_error_method == SolutionsErrorSum)
212  {
213  return("SolutionsErrorSum");
214  }
215  else if(solutions_error_method == SolutionsErrorIntegral)
216  {
217  return("SolutionsErrorIntegral");
218  }
219  else
220  {
221  std::ostringstream buffer;
222 
223  buffer << "OpenNN Exception: SolutionsError class.\n"
224  << "std::string write_solutions_error_method(void) const method.\n"
225  << "Unknown solutions error method.\n";
226 
227  throw std::logic_error(buffer.str());
228  }
229 }
230 
231 
232 // const Vector<double>& get_solutions_errors_weights(void) const method
233 
235 
237 {
238  return(solutions_errors_weights);
239 }
240 
241 
242 // const double& get_solution_error_weight(const size_t&) const method
243 
245 
246 const double& SolutionsError::get_solution_error_weight(const size_t& i) const
247 {
248  return(solutions_errors_weights[i]);
249 }
250 
251 
252 // void set(const SolutionsError&) method
253 
254 // Sets to this object the data from another object of the same class.
255 // @param other_solutions_error Solutions error object to be copied.
256 
257 //void SolutionsError::set(const SolutionsError& other_solutions_error)
258 //{
259 // neural_network_pointer = other_solutions_error.neural_network_pointer;
260 
261 // data_set_pointer = other_solutions_error.data_set_pointer;
262 
263 // mathematical_model_pointer = other_solutions_error.mathematical_model_pointer;
264 
265 // if(other_solutions_error.numerical_differentiation_pointer)
266 // {
267 // numerical_differentiation_pointer = new NumericalDifferentiation(*other_solutions_error.numerical_differentiation_pointer);
268 // }
269 
270 // display = other_solutions_error.display;
271 
272 // solutions_errors_weights = other_solutions_error.solutions_errors_weights;
273 //}
274 
275 
276 // void set_solutions_error_method(const SolutionsErrorMethod&) method
277 
280 
281 void SolutionsError::set_solutions_error_method(const SolutionsErrorMethod& new_solutions_error_method)
282 {
283  solutions_error_method = new_solutions_error_method;
284 }
285 
286 
287 // void set_solutions_error_method(const std::string&) method
288 
291 
292 void SolutionsError::set_solutions_error_method(const std::string& new_solutions_error_method)
293 {
294  if(new_solutions_error_method == "SolutionsErrorSum")
295  {
296  set_solutions_error_method(SolutionsErrorSum);
297  }
298  else if(new_solutions_error_method == "SolutionsErrorIntegral")
299  {
300  set_solutions_error_method(SolutionsErrorIntegral);
301  }
302  else
303  {
304  std::ostringstream buffer;
305 
306  buffer << "OpenNN Exception: SolutionsError class.\n"
307  << "void set_solutions_error_method(const std::string&) method.\n"
308  << "Unknown solutions error method: " << new_solutions_error_method << ".\n";
309 
310  throw std::logic_error(buffer.str());
311  }
312 }
313 
314 
315 // void set_solutions_errors_weights(const Vector<double>&) method
316 
319 
320 void SolutionsError::set_solutions_errors_weights(const Vector<double>& new_solutions_errors_weights)
321 {
322  solutions_errors_weights = new_solutions_errors_weights;
323 }
324 
325 
326 // void set_solution_error_weight(const size_t&, const double&) method
327 
331 
332 void SolutionsError::set_solution_error_weight(const size_t& i, const double& new_solution_error_weight)
333 {
334  solutions_errors_weights[i] = new_solution_error_weight;
335 }
336 
337 
338 // void set_default(void) method
339 
346 
348 {
349  solutions_error_method = SolutionsErrorSum;
350 
352  {
353  const size_t dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
354 
355  solutions_errors_weights.set(dependent_variables_number, 1.0);
356  }
357 
359  {
361  }
362 
364 
365  display = true;
366 }
367 
368 
369 // Matrix<double> calculate_target_dependent_variables(const Matrix<double>&) const method
370 
372 
374 {
375  #ifndef NDEBUG
376 
377  check();
378 
379  #endif
380 
381  const size_t& rows_number = independent_variables.get_rows_number();
382  const size_t& dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
383 
384  const Matrix<double> target_dependent_variables(rows_number, dependent_variables_number, 0.0);
385 
386  return(target_dependent_variables);
387 }
388 
389 
390 // void check(void) const method
391 
400 
401 void SolutionsError::check(void) const
402 {
403  std::ostringstream buffer;
404 
405  // Neural network stuff
406 
408  {
409  buffer << "OpenNN Exception: SolutionsError class.\n"
410  << "void check(void) const method.\n"
411  << "Pointer to neural network is NULL.\n";
412 
413  throw std::logic_error(buffer.str());
414  }
415 
416  const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
417 
418  if(!multilayer_perceptron_pointer)
419  {
420  buffer << "OpenNN Exception: SolutionsError class.\n"
421  << "void check(void) const method.\n"
422  << "Pointer to multilayer perceptron is NULL.\n";
423 
424  throw std::logic_error(buffer.str());
425  }
426 
427  const size_t inputs_number = multilayer_perceptron_pointer->get_inputs_number();
428  const size_t outputs_number = multilayer_perceptron_pointer->get_outputs_number();
429 
430  if(inputs_number == 0)
431  {
432  buffer << "OpenNN Exception: SolutionsError class.\n"
433  << "void check(void) const method.\n"
434  << "Number of inputs in multilayer perceptron object is zero.\n";
435 
436  throw std::logic_error(buffer.str());
437  }
438 
439  if(outputs_number == 0)
440  {
441  buffer << "OpenNN Exception: SolutionsError class.\n"
442  << "void check(void) const method.\n"
443  << "Number of outputs in multilayer perceptron object is zero.\n";
444 
445  throw std::logic_error(buffer.str());
446  }
447 
448  // Mathematical model stuff
449 
451  {
452  buffer << "OpenNN Exception: SolutionsError class.\n"
453  << "void check(void) const method.\n"
454  << "Pointer to mathematical model is NULL.\n";
455 
456  throw std::logic_error(buffer.str());
457  }
458 
459  const size_t& dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
460 
461  // Solutions error stuff
462 
463  const size_t solutions_errors_weights_size = solutions_errors_weights.size();
464 
465  if(solutions_errors_weights_size != dependent_variables_number)
466  {
467  buffer << "OpenNN Exception: SolutionsError class.\n"
468  << "void check(void) const method.\n"
469  << "Size of solutions errors weights (" << solutions_errors_weights_size << ") is not equal to number of dependent variables (" << dependent_variables_number << ").\n";
470 
471  throw std::logic_error(buffer.str());
472  }
473 }
474 
475 
476 // double calculate_solutions_error_sum(void) const method
477 
480 
482 {
483  // Control sentence
484 
485  #ifndef NDEBUG
486 
487  check();
488 
489  #endif
490 
491  const size_t independent_variables_number = mathematical_model_pointer->get_independent_variables_number();
492  const size_t dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
493 
494  const size_t variables_number = mathematical_model_pointer->count_variables_number();
495 
497 
498  const Vector<size_t> independent_variables_indices(0, 1, independent_variables_number-1);
499  const Vector<size_t> dependent_variables_indices(independent_variables_number, 1, variables_number-1);
500 
501  const Matrix<double> independent_variables = solution.arrange_submatrix_columns(independent_variables_indices);
502  const Matrix<double> dependent_variables = solution.arrange_submatrix_columns(dependent_variables_indices);
503 
504  const Matrix<double> target_dependent_variables = calculate_target_dependent_variables(independent_variables);
505 
506  double performance = 0.0;
507 
508  for(size_t i = 0; i < dependent_variables_number; i++)
509  {
510  if(solutions_errors_weights[i] != 0.0)
511  {
512  const Vector<double> dependent_variable = dependent_variables.arrange_column(i);
513  const Vector<double> target_dependent_variable = target_dependent_variables.arrange_column(i);
514 
515  performance += solutions_errors_weights[i]*(dependent_variable-target_dependent_variable).calculate_norm();
516  }
517  }
518 
519  const size_t rows_number = independent_variables.get_rows_number();
520 
521  return(performance/(double)rows_number);
522 }
523 
524 
525 // double calculate_solutions_error_integral(void) const method
526 
528 
530 {
531  return(0.0);
532 }
533 
534 
535 // double calculate_performance(void) const method
536 
538 
540 {
541  // Control sentence
542 
543  #ifndef NDEBUG
544 
545  check();
546 
547  #endif
548 
549  switch(solutions_error_method)
550  {
551  case SolutionsErrorSum:
552  {
554  }
555  break;
556 
557  case SolutionsErrorIntegral:
558  {
560  }
561  break;
562 
563  default:
564  {
565  std::ostringstream buffer;
566 
567  buffer << "OpenNN Exception: SolutionsError class\n"
568  << "double calculate_performance(void) const method.\n"
569  << "Unknown solutions error method.\n";
570 
571  throw std::logic_error(buffer.str());
572  }
573  break;
574  }
575 }
576 
577 
578 // double calculate_performance(const Vector<double>&) const method
579 
582 
584 {
585  // Control sentence
586 
587  #ifndef NDEBUG
588 
589  check();
590 
591  #endif
592 
593  NeuralNetwork neural_network_copy(*neural_network_pointer);
594 
595  neural_network_copy.set_parameters(parameters);
596 
597  SolutionsError solutions_error_copy(*this);
598 
599  solutions_error_copy.set_neural_network_pointer(&neural_network_copy);
601 
602  return(solutions_error_copy.calculate_performance());
603 }
604 
605 
606 // std::string write_performance_term_type(void) const method
607 
609 
611 {
612  return("SOLUTION_ERROR");
613 }
614 
615 
616 // std::string write_information(void) const method
617 
618 std::string SolutionsError::write_information(void) const
619 {
620  std::ostringstream buffer;
621 
622  buffer << "Solutions error: " << calculate_performance() << "\n";
623 
624  return(buffer.str());
625 }
626 
627 
628 // void print(void) const method
629 
631 
632 void SolutionsError::print(void) const
633 {
634  if(display)
635  {
636  std::cout << "Solutions error:\n"
637  << "Solutions error method: " << write_solutions_error_method() << "\n"
638  << "Solutions errors weights: " << solutions_errors_weights << std::endl;
639  }
640 }
641 
642 
643 // tinyxml2::XMLDocument* to_XML(void) method method
644 
646 
647 tinyxml2::XMLDocument* SolutionsError::to_XML(void) const
648 {
649  std::ostringstream buffer;
650 
651  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
652 
653  // Solutions error
654 
655  tinyxml2::XMLElement* root_element = document->NewElement("SolutionsError");
656 
657  document->InsertFirstChild(root_element);
658 
659  tinyxml2::XMLElement* element = NULL;
660  tinyxml2::XMLText* text = NULL;
661 
662  // Numerical differentiation
663 
665  {
666  element = numerical_differentiation_pointer->to_XML()->FirstChildElement();
667  root_element->LinkEndChild(element);
668  }
669 
670  // Numerical integration
671  {
672  element = numerical_integration.to_XML()->FirstChildElement();
673  root_element->LinkEndChild(element);
674  }
675 
676  // Solutions error method
677  {
678  element = document->NewElement("SolutionsErrorMethod");
679  root_element->LinkEndChild(element);
680 
681  text = document->NewText(write_solutions_error_method().c_str());
682  element->LinkEndChild(text);
683  }
684 
685  // Solutions error weights
686  {
687  element = document->NewElement("SolutionsErrorWeights");
688  root_element->LinkEndChild(element);
689 
690  buffer.str("");
691  buffer << solutions_errors_weights;
692 
693  text = document->NewText(buffer.str().c_str());
694  element->LinkEndChild(text);
695  }
696 
697  // Display
698  {
699  element = document->NewElement("Display");
700  root_element->LinkEndChild(element);
701 
702  buffer.str("");
703  buffer << display;
704 
705  text = document->NewText(buffer.str().c_str());
706  element->LinkEndChild(text);
707  }
708 
709  return(document);
710 }
711 
712 
713 // void from_XML(const tinyxml2::XMLDocument&) method
714 
717 
718 void SolutionsError::from_XML(const tinyxml2::XMLDocument& document)
719 {
720  // Display
721  {
722  const tinyxml2::XMLElement* display_element = document.FirstChildElement("Display");
723 
724  if(display_element)
725  {
726  std::string new_display_string = display_element->GetText();
727 
728  try
729  {
730  set_display(new_display_string != "0");
731  }
732  catch(const std::logic_error& e)
733  {
734  std::cout << e.what() << std::endl;
735  }
736  }
737  }
738 }
739 
740 
741 }
742 
743 
744 // OpenNN: Open Neural Networks Library.
745 // Copyright (c) 2005-2015 Roberto Lopez.
746 //
747 // This library is free software; you can redistribute it and/or
748 // modify it under the terms of the GNU Lesser General Public
749 // License as published by the Free Software Foundation; either
750 // version 2.1 of the License, or any later version.
751 //
752 // This library is distributed in the hope that it will be useful,
753 // but WITHOUT ANY WARRANTY; without even the implied warranty of
754 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
755 // Lesser General Public License for more details.
756 // You should have received a copy of the GNU Lesser General Public
757 // License along with this library; if not, write to the Free Software
758 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
virtual Matrix< double > calculate_solutions(const NeuralNetwork &) const
Vector< T > arrange_column(const size_t &) const
Definition: matrix.h:1580
std::string write_solutions_error_method(void) const
Returns a string with the name of the method for computing the solutions error.
const Vector< double > & get_solutions_errors_weights(void) const
Returns the weights for every single solution error, corresponding to a dependent variable...
tinyxml2::XMLDocument * to_XML(void) const
Returns a representation of the solutions error object, in XML format.
const SolutionsErrorMethod & get_solutions_error_method(void) const
Returns the method for computing the solutions error.
virtual Matrix< double > calculate_target_dependent_variables(const Matrix< double > &) const
Returns the default target solution matrix.
SolutionsErrorMethod solutions_error_method
Method used to compute the error between the mathematical model solutions and the target solutions...
MathematicalModel * mathematical_model_pointer
Pointer to a mathematical model object.
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
void set_solutions_error_method(const SolutionsErrorMethod &)
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.
SolutionsErrorMethod
Enumeration of the different methods to compute the errors between the mathematical model solutions a...
Vector< double > solutions_errors_weights
Weights for the different dependent variables errors.
double calculate_solutions_error_integral(void) const
void set_display(const bool &)
const size_t & get_dependent_variables_number(void) const
Returns the number of dependent variables in the mathematical model.
tinyxml2::XMLDocument * to_XML(void) const
Serializes this numerical integration object into a XML document.
Matrix< T > arrange_submatrix_columns(const Vector< size_t > &) const
Definition: matrix.h:1477
size_t count_variables_number(void) const
bool operator==(const SolutionsError &) const
std::string write_performance_term_type(void) const
Returns a string with the name of the solutions error performance type, "SOLUTIONS_ERROR".
const double & get_solution_error_weight(const size_t &) const
Returns the weight for a single solution error, corresponding to a dependent variable.
void check(void) const
virtual void set_neural_network_pointer(NeuralNetwork *)
std::string write_information(void) const
NumericalIntegration numerical_integration
Numerical integration object.
MultilayerPerceptron * get_multilayer_perceptron_pointer(void) const
Returns a pointer to the multilayer perceptron composing this neural network.
double calculate_solutions_error_sum(void) const
void construct_numerical_differentiation(void)
This method constructs the numerical differentiation object which composes the performance term class...
virtual void set_mathematical_model_pointer(MathematicalModel *)
Sets a new mathematical model on which the performance term is to be measured.
NeuralNetwork * neural_network_pointer
Pointer to a multilayer perceptron object.
NumericalDifferentiation * numerical_differentiation_pointer
Numerical differentiation object.
virtual double calculate_performance(void) const
Returns the objective value of a neural network according to the solutions error on a mathematical mo...
const size_t & get_rows_number(void) const
Returns the number of rows in the matrix.
Definition: matrix.h:1079
bool display
Display messages to screen.
void set_solution_error_weight(const size_t &, const double &)
DataSet * data_set_pointer
Pointer to a data set object.
virtual void print(void) const
Prints to the screen the string representation of this solutions error object.
SolutionsError & operator=(const SolutionsError &)
tinyxml2::XMLDocument * to_XML(void) const
Serializes this numerical differentiation object into a XML document->
void set_solutions_errors_weights(const Vector< double > &)
void set_parameters(const Vector< double > &)
void from_XML(const tinyxml2::XMLDocument &)