OpenNN  2.2
Open Neural Networks Library
conditions_layer.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* C O N D I T I O N S L A Y E 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 "conditions_layer.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
25 
27 {
28  set();
29 
30  set_default();
31 }
32 
33 
34 // INPUTS-OUTPUTS NUMBER CONSTRUCTOR
35 
39 
40 ConditionsLayer::ConditionsLayer(const size_t& new_inputs_number, const size_t& new_conditions_neurons_number)
41 {
42  set(new_inputs_number, new_conditions_neurons_number);
43 
44  set_default();
45 }
46 
47 
48 // XML CONSTRUCTOR
49 
52 
53 ConditionsLayer::ConditionsLayer(const tinyxml2::XMLDocument& conditions_layer_document)
54 {
55  set_default();
56 
57  from_XML(conditions_layer_document);
58 }
59 
60 
61 // COPY CONSTRUCTOR
62 
65 
66 ConditionsLayer::ConditionsLayer(const ConditionsLayer& other_conditions_layer)
67 {
68  set(other_conditions_layer);
69 }
70 
71 
72 // DESTRUCTOR
73 
75 
77 {
78 }
79 
80 
81 // ASSIGNMENT OPERATOR
82 
86 
88 {
89  if(this != &other_conditions_layer)
90  {
91  external_inputs_number = other_conditions_layer.external_inputs_number;
93  conditions_method = other_conditions_layer.conditions_method;
94  external_input_values = other_conditions_layer.external_input_values;
95  output_values = other_conditions_layer.output_values;
96  display = other_conditions_layer.display;
97  }
98 
99  return(*this);
100 }
101 
102 
103 // EQUAL TO OPERATOR
104 
105 // bool operator == (const ConditionsLayer&) const method
106 
111 
112 bool ConditionsLayer::operator == (const ConditionsLayer& other_conditions_layer) const
113 {
114  if(external_inputs_number == other_conditions_layer.external_inputs_number
115  && conditions_neurons_number == other_conditions_layer.conditions_neurons_number
116  && conditions_method == other_conditions_layer.conditions_method
117  && external_input_values == other_conditions_layer.external_input_values
118  && output_values == other_conditions_layer.output_values
119  && display == other_conditions_layer.display)
120  {
121  return(true);
122  }
123  else
124  {
125  return(false);
126  }
127 }
128 
129 
130 // METHODS
131 
132 // const size_t& get_external_inputs_number(void) const method
133 
135 
137 {
138  return(external_inputs_number);
139 }
140 
141 
142 // const size_t& get_conditions_neurons_number(void) const method
143 
145 
147 {
149 }
150 
151 
152 // const ConditionsMethod& get_conditions_method(void) const method
153 
156 
158 {
159  return(conditions_method);
160 }
161 
162 
163 // std::string write_conditions_method(void) const method
164 
167 
169 {
170  if(conditions_method == OneCondition)
171  {
172  return("OneCondition");
173  }
174  else if(conditions_method == TwoConditions)
175  {
176  return("TwoConditions");
177  }
178  else
179  {
180  std::ostringstream buffer;
181 
182  buffer << "OpenNN Exception: ConditionsLayer class.\n"
183  << "std::string write_conditions_method(void) const method.\n"
184  << "Unknown conditions method.\n";
185 
186  throw std::logic_error(buffer.str());
187  }
188 }
189 
190 
191 // const Vector<double>& get_external_input_values(void) const method
192 
194 
196 {
197  return(external_input_values);
198 }
199 
200 
201 // const double& get_external_input_value(const size_t&) const method
202 
205 
206 const double& ConditionsLayer::get_external_input_value(const size_t& i) const
207 {
208  return(external_input_values[i]);
209 }
210 
211 
212 // const Matrix<double>& get_output_values(void) const method
213 
215 
217 {
218  return(output_values);
219 }
220 
221 
222 // const double& get_exteoutput_value(const size_t&, const size_t&) const method
223 
227 
228 const double& ConditionsLayer::get_output_value(const size_t& i, const size_t& j) const
229 {
230  return(output_values(i,j));
231 }
232 
233 
234 // const bool& get_display(void) const method
235 
238 
239 const bool& ConditionsLayer::get_display(void) const
240 {
241  return(display);
242 }
243 
244 
245 // void set(void) method
246 
249 
251 {
253 
255 
256  set_default();
257 }
258 
259 
260 // void set(const size_t&, const size_t&) method
261 
264 
265 void ConditionsLayer::set(const size_t& new_external_inputs_number, const size_t& new_conditions_neurons_number)
266 {
267  external_inputs_number = new_external_inputs_number;
268 
269  conditions_neurons_number = new_conditions_neurons_number;
270 
271  set_default();
272 }
273 
274 
275 // void set(const ConditionsLayer&) method
276 
279 
280 void ConditionsLayer::set(const ConditionsLayer& other_conditions_layer)
281 {
282  external_inputs_number = other_conditions_layer.external_inputs_number;
283 
284  conditions_neurons_number = other_conditions_layer.conditions_neurons_number;
285 
286  conditions_method = other_conditions_layer.conditions_method;
287 
288  external_input_values = other_conditions_layer.external_input_values;
289  output_values = other_conditions_layer.output_values;
290 
291  display = other_conditions_layer.display;
292 }
293 
294 
295 // void set_default(void) method
296 
298 
300 {
301  conditions_method = TwoConditions;
302 
304  {
306 
307  output_values.set();
308  }
309  else
310  {
311  external_input_values.set(2, 0.0);
312 
314  }
315 
316  display = true;
317 }
318 
319 
320 // void set_external_inputs_number(const size_t&) method
321 
324 
325 void ConditionsLayer::set_external_inputs_number(const size_t& new_external_inputs_number)
326 {
327  external_inputs_number = new_external_inputs_number;
328 }
329 
330 
331 // void set_conditions_neurons_number(const size_t&) method
332 
335 
336 void ConditionsLayer::set_conditions_neurons_number(const size_t& new_conditions_neurons_number)
337 {
338  conditions_neurons_number = new_conditions_neurons_number;
339 }
340 
341 
342 // void set_conditions_method(const ConditionsMethod&) method
343 
346 
348 {
349  conditions_method = new_conditions_method;
350 }
351 
352 
353 // void set_conditions_method(const std::string&) method
354 
357 
358 void ConditionsLayer::set_conditions_method(const std::string& new_conditions_method)
359 {
360  if(new_conditions_method == "OneCondition")
361  {
362  set_conditions_method(OneCondition);
363  }
364  else if(new_conditions_method == "TwoConditions")
365  {
366  set_conditions_method(TwoConditions);
367  }
368  else if(new_conditions_method == "UserConditionsMethod")
369  {
370  set_conditions_method(UserConditionsMethod);
371  }
372  else
373  {
374  std::ostringstream buffer;
375 
376  buffer << "OpenNN Exception: ConditionsLayer class.\n"
377  << "void set_conditions_method(const std::string&) method.\n"
378  << "Unknown conditions method: " << new_conditions_method << ".\n";
379 
380  throw std::logic_error(buffer.str());
381  }
382 }
383 
384 
385 // void set_external_input_values(const Vector<double>&) method
386 
389 
390 void ConditionsLayer::set_external_input_values(const Vector<double>& new_external_input_values)
391 {
392  external_input_values = new_external_input_values;
393 }
394 
395 
396 // void set_external_input_value(const size_t&, const double&) method
397 
401 
402 void ConditionsLayer::set_external_input_value(const size_t& i, const double& new_external_input_value)
403 {
404  external_input_values[i] = new_external_input_value;
405 }
406 
407 
408 // void set_output_values(const Matrix<double>&) method
409 
412 
414 {
415  output_values = new_output_values;
416 }
417 
418 
419 // void set_output_value(const size_t&, const double&) method
420 
425 
426 void ConditionsLayer::set_output_value(const size_t& i, const size_t& j, const double& new_output_value)
427 {
428  output_values(i,j) = new_output_value;
429 }
430 
431 
432 // void set_display(const bool&) method
433 
438 
439 void ConditionsLayer::set_display(const bool& new_display)
440 {
441  display = new_display;
442 }
443 
444 
445 // void initialize_random(void) method
446 
448 
450 {
451 
452 }
453 
454 
455 // void check(void) const method
456 
458 
459 void ConditionsLayer::check(void) const
460 {
461  std::ostringstream buffer;
462 
463  if(conditions_method == OneCondition || conditions_method == TwoConditions)
464  {
465  if(external_inputs_number != 1)
466  {
467  buffer << "OpenNN Exception: BoundingLayer class.\n"
468  << "void check(void) const method.\n"
469  << "Number of external inputs must be one.\n";
470 
471  throw std::logic_error(buffer.str());
472  }
473  }
474 
475  const size_t external_input_values_size = external_input_values.size();
476 
477  const size_t output_values_rows_number = output_values.get_rows_number();
478  const size_t output_values_columns_number = output_values.get_rows_number();
479 
480  if(output_values_rows_number != conditions_neurons_number)
481  {
482  buffer << "OpenNN Exception: BoundingLayer class.\n"
483  << "void check(void) const method.\n"
484  << "Number of rows in output values is not equal to number of conditions neurons.\n";
485 
486  throw std::logic_error(buffer.str());
487  }
488 
489  if(conditions_method == OneCondition)
490  {
491  if(external_input_values_size != 1)
492  {
493  buffer << "OpenNN Exception: BoundingLayer class.\n"
494  << "void check(void) const method.\n"
495  << "Size of input values is not 1.\n";
496 
497  throw std::logic_error(buffer.str());
498  }
499 
500  if(output_values_columns_number != 1)
501  {
502  buffer << "OpenNN Exception: BoundingLayer class.\n"
503  << "void check(void) const method.\n"
504  << "Number of columns in output values is not 1.\n";
505 
506  throw std::logic_error(buffer.str());
507  }
508  }
509  else if(conditions_method == OneCondition)
510  {
511  if(external_input_values_size != 2)
512  {
513  buffer << "OpenNN Exception: BoundingLayer class.\n"
514  << "void check(void) const method.\n"
515  << "Size of input values is not 2.\n";
516 
517  throw std::logic_error(buffer.str());
518  }
519 
520  if(output_values_columns_number != 2)
521  {
522  buffer << "OpenNN Exception: BoundingLayer class.\n"
523  << "void check(void) const method.\n"
524  << "Number of columns in output values is not 2.\n";
525 
526  throw std::logic_error(buffer.str());
527  }
528  }
529 }
530 
531 
532 // Vector<double> calculate_particular_solution(const Vector<double>&) const method
533 
536 
538 {
539  switch(conditions_method)
540  {
541  case OneCondition:
542  {
543  return(calculate_one_condition_particular_solution(external_inputs));
544  }
545  break;
546 
547  case TwoConditions:
548  {
549  return(calculate_two_conditions_particular_solution(external_inputs));
550  }
551  break;
552 
553  default:
554  {
555  std::ostringstream buffer;
556 
557  buffer << "OpenNN Exception: ScalingLayer class\n"
558  << "Vector<double> calculate_particular_solution(const Vector<double>&) const method.\n"
559  << "Unknown conditions method.\n";
560 
561  throw std::logic_error(buffer.str());
562  }
563  break;
564  }
565 }
566 
567 
568 // Matrix<double> calculate_particular_solution_Jacobian(const Vector<double>&) const method
569 
572 
574 {
575  switch(conditions_method)
576  {
577  case OneCondition:
578  {
580  }
581  break;
582 
583  case TwoConditions:
584  {
586  }
587  break;
588 
589  default:
590  {
591  std::ostringstream buffer;
592 
593  buffer << "OpenNN Exception: ScalingLayer class\n"
594  << "Matrix<double> calculate_particular_solution_Jacobian(const Vector<double>&) const method.\n"
595  << "Unknown conditions method.\n";
596 
597  throw std::logic_error(buffer.str());
598  }
599  break;
600  }
601 }
602 
603 
604 // Vector< Matrix<double> > calculate_particular_solution_Hessian_form(const Vector<double>&) const method
605 
608 
610 {
611  switch(conditions_method)
612  {
613  case OneCondition:
614  {
616  }
617  break;
618 
619  case TwoConditions:
620  {
622  }
623  break;
624 
625  default:
626  {
627  std::ostringstream buffer;
628 
629  buffer << "OpenNN Exception: ScalingLayer class\n"
630  << "Vector< Matrix<double> > calculate_particular_solution_Hessian_form(const Vector<double>&) const method.\n"
631  << "Unknown conditions method.\n";
632 
633  throw std::logic_error(buffer.str());
634  }
635  break;
636  }
637 }
638 
639 
640 // Vector<double> calculate_homogeneous_solution(const Vector<double>&) const method
641 
644 
646 {
647  switch(conditions_method)
648  {
649  case OneCondition:
650  {
651  return(calculate_one_condition_homogeneous_solution(external_inputs));
652  }
653  break;
654 
655  case TwoConditions:
656  {
657  return(calculate_two_conditions_homogeneous_solution(external_inputs));
658  }
659  break;
660 
661  default:
662  {
663  std::ostringstream buffer;
664 
665  buffer << "OpenNN Exception: ScalingLayer class\n"
666  << "Vector<double> calculate_homogeneous_solution(const Vector<double>&) const method.\n"
667  << "Unknown conditions method.\n";
668 
669  throw std::logic_error(buffer.str());
670  }
671  break;
672  }
673 }
674 
675 
676 // Matrix<double> calculate_homogeneous_solution_Jacobian(const Vector<double>&) const method
677 
680 
682 {
683  switch(conditions_method)
684  {
685  case OneCondition:
686  {
688  }
689  break;
690 
691  case TwoConditions:
692  {
694  }
695  break;
696 
697  default:
698  {
699  std::ostringstream buffer;
700 
701  buffer << "OpenNN Exception: ScalingLayer class\n"
702  << "Matrix<double> calculate_homogeneous_solution_Jacobian(const Vector<double>&) const method.\n"
703  << "Unknown conditions method.\n";
704 
705  throw std::logic_error(buffer.str());
706  }
707  break;
708  }
709 }
710 
711 
712 // Vector< Matrix<double> > calculate_homogeneous_solution_Hessian_form(const Vector<double>&) const method
713 
716 
718 {
719  switch(conditions_method)
720  {
721  case OneCondition:
722  {
724  }
725  break;
726 
727  case TwoConditions:
728  {
730  }
731  break;
732 
733  default:
734  {
735  std::ostringstream buffer;
736 
737  buffer << "OpenNN Exception: ScalingLayer class\n"
738  << "Matrix<double> calculate_homogeneous_solution_Hessian_form(const Vector<double>&) const method.\n"
739  << "Unknown conditions method.\n";
740 
741  throw std::logic_error(buffer.str());
742  }
743  break;
744  }
745 }
746 
747 
748 // Vector<double> calculate_outputs(const Vector<double>&, const Vector<double>&) const method
749 
753 
755 {
756  const Vector<double> particular_solution = calculate_particular_solution(external_inputs);
757  const Vector<double> homogeneous_solution = calculate_homogeneous_solution(external_inputs);
758 
759  return(particular_solution + homogeneous_solution*inputs);
760 }
761 
762 
763 // Matrix<double> calculate_Jacobian(const Vector<double>&, const Vector<double>&) const method
764 
769 
770 Matrix<double> ConditionsLayer::calculate_Jacobian(const Vector<double>& external_inputs, const Vector<double>& outputs, const Matrix<double>& Jacobian) const
771 {
772  const Vector<double> homogeneous_solution = calculate_homogeneous_solution(external_inputs);
773 
774  const Matrix<double> particular_solution_Jacobian = calculate_particular_solution_Jacobian(external_inputs);
775  const Matrix<double> homogeneous_solution_Jacobian = calculate_homogeneous_solution_Jacobian(external_inputs);
776 
777  return(particular_solution_Jacobian + homogeneous_solution_Jacobian*outputs + homogeneous_solution*Jacobian);
778 }
779 
780 
781 // Vector< Matrix<double> > calculate_Hessian_form(const Vector<double>&, const Vector<double>&) const method
782 
784 
786 {
788 
789  return(Hessian_form);
790 }
791 
792 
793 // Vector<double> calculate_one_condition_particular_solution(const Vector<double>&) const method
794 
797 
799 {
800  #ifndef NDEBUG
801 
802  check();
803 
804  #endif
805 
806  const Vector<double> particular_solution(conditions_neurons_number, external_inputs[0]);
807 
808  return(particular_solution);
809 }
810 
811 
812 // Matrix<double> calculate_one_condition_particular_solution_Jacobian(const Vector<double>&) const method
813 
815 
817 {
818  #ifndef NDEBUG
819 
820  check();
821 
822  #endif
823 
824  const Matrix<double> particular_solution_Jacobian(conditions_neurons_number, 1, 0.0);
825 
826  return(particular_solution_Jacobian);
827 }
828 
829 
830 // Vector< Matrix<double> > calculate_one_condition_particular_solution_Hessian_form(const Vector<double>&) const method
831 
834 
836 {
837  #ifndef NDEBUG
838 
839  check();
840 
841  #endif
842 
843  Vector< Matrix<double> > particular_solution_Hessian_form(conditions_neurons_number);
844 
845  return(particular_solution_Hessian_form);
846 }
847 
848 
849 // Vector<double> calculate_one_condition_homogeneous_solution(const Vector<double>&) const method
850 
853 
855 {
856  #ifndef NDEBUG
857 
858  check();
859 
860  #endif
861 
862  const Vector<double> homogeneous_solution(conditions_neurons_number, external_inputs[0]-external_input_values[0]);
863 
864  return(homogeneous_solution);
865 }
866 
867 
868 
869 // Matrix<double> calculate_one_condition_homogeneous_solution_Jacobian(const Vector<double>&) const method
870 
872 
874 {
875  #ifndef NDEBUG
876 
877  check();
878 
879  #endif
880 
881  const Matrix<double> homogeneous_solution_Jacobian(conditions_neurons_number, 1, 1.0);
882 
883  return(homogeneous_solution_Jacobian);
884 }
885 
886 
887 // Vector< Matrix<double> > calculate_one_condition_homogeneous_solution_Hessian_form(const Vector<double>&) const method
888 
891 
893 {
894  #ifndef NDEBUG
895 
896  check();
897 
898  #endif
899 
900  Vector< Matrix<double> > homogeneous_solution_Hessian_form(conditions_neurons_number);
901 
902  return(homogeneous_solution_Hessian_form);
903 }
904 
905 
906 // Vector<double> calculate_two_conditions_particular_solution(const Vector<double>&) const method
907 
910 
912 {
913  #ifndef NDEBUG
914 
915  check();
916 
917  #endif
918 
919  const double x = external_inputs[0];
920 
921  const double xa = external_input_values[0];
922  const double xb = external_input_values[1];
923 
924  #ifndef NDEBUG
925 
926  if(xb - xa < 1.0e-99)
927  {
928  std::ostringstream buffer;
929 
930  buffer << "OpenNN Exception: BoundingLayer class.\n"
931  << "Vector<double> calculate_two_conditions_particular_solution(const Vector<double>&) const method.\n"
932  << "Both input values are the same.\n";
933 
934  throw std::logic_error(buffer.str());
935  }
936 
937  #endif
938 
939  double ya;
940  double yb;
941 
942  Vector<double> particular_solutions(conditions_neurons_number);
943 
944  for(size_t i = 0; i < conditions_neurons_number; i++)
945  {
946  ya = output_values(i,0);
947  yb = output_values(i,1);
948 
949  particular_solutions[i] = ya + (yb-ya)*(x-xa)/(double)(xb-xa);
950  }
951 
952  return(particular_solutions);
953 }
954 
955 
956 // Vector<double> calculate_two_conditions_particular_solution_Jacobian(const Vector<double>&) const method
957 
960 
962 {
963  #ifndef NDEBUG
964 
965  check();
966 
967  #endif
968 
969  const double xa = external_input_values[0];
970  const double xb = external_input_values[1];
971 
972  #ifndef NDEBUG
973 
974  if(xb - xa < 1.0e-99)
975  {
976  std::ostringstream buffer;
977 
978  buffer << "OpenNN Exception: BoundingLayer class.\n"
979  << "Matrix<double> calculate_two_conditions_particular_solution_Jacobian(const Vector<double>&) const method.\n"
980  << "Both input values are the same.\n";
981 
982  throw std::logic_error(buffer.str());
983  }
984 
985  #endif
986 
987  const double ya = output_values(0,0);
988  const double yb = output_values(0,1);
989 
990  Matrix<double> particular_solution_Jacobian(1, 1, (yb-ya)/(xb-xa));
991 
992  return(particular_solution_Jacobian);
993 }
994 
995 
996 // Vector<double> calculate_two_conditions_particular_solution_Hessian_form(const Vector<double>&) const method
997 
1000 
1002 {
1003  #ifndef NDEBUG
1004 
1005  check();
1006 
1007  #endif
1008 
1009  Vector< Matrix<double> > particular_solution_Hessian_form(conditions_neurons_number);
1010 
1011  return(particular_solution_Hessian_form);
1012 }
1013 
1014 
1015 // Vector<double> calculate_two_conditions_homogeneous_solution(const Vector<double>&) const method
1016 
1019 
1021 {
1022  #ifndef NDEBUG
1023 
1024  check();
1025 
1026  #endif
1027 
1028  const double x = external_inputs[0];
1029 
1030  const double xa = external_input_values[0];
1031  const double xb = external_input_values[1];
1032 
1033  #ifndef NDEBUG
1034 
1035  if(xb - xa < 1.0e-99)
1036  {
1037  std::ostringstream buffer;
1038 
1039  buffer << "OpenNN Exception: BoundingLayer class.\n"
1040  << "Matrix<double> calculate_two_conditions_particular_solution_Jacobian(const Vector<double>&) const method.\n"
1041  << "Both input values are the same.\n";
1042 
1043  throw std::logic_error(buffer.str());
1044  }
1045 
1046  #endif
1047 
1048  const Vector<double> homogeneous_solutions(conditions_neurons_number, (x-xa)*(x-xb));
1049 
1050  return(homogeneous_solutions);
1051 }
1052 
1053 
1054 // Matrix<double> calculate_two_conditions_homogeneous_solution_Jacobian(const Vector<double>&) const method
1055 
1058 
1060 {
1061  #ifndef NDEBUG
1062 
1063  check();
1064 
1065  #endif
1066 
1067  const double x = external_inputs[0];
1068 
1069  const double xa = external_input_values[0];
1070  const double xb = external_input_values[1];
1071 
1072  #ifndef NDEBUG
1073 
1074  if(xb - xa < 1.0e-99)
1075  {
1076  std::ostringstream buffer;
1077 
1078  buffer << "OpenNN Exception: BoundingLayer class.\n"
1079  << "Matrix<double> calculate_two_conditions_particular_solution_Jacobian(const Vector<double>&) const method.\n"
1080  << "Both input values are the same.\n";
1081 
1082  throw std::logic_error(buffer.str());
1083  }
1084 
1085  #endif
1086 
1087  Matrix<double> homogeneous_solution_Jacobian(1, 1, (x-xa) + (x-xb));
1088 
1089  return(homogeneous_solution_Jacobian);
1090 }
1091 
1092 
1093 // Vector< Matrix<double> > calculate_two_conditions_homogeneous_solution_Hessian_form(const Vector<double>&) const method
1094 
1096 // @todo
1097 
1099 {
1100  #ifndef NDEBUG
1101 
1102  check();
1103 
1104  #endif
1105 
1106  Vector< Matrix<double> > homogeneous_solution_Hessian_form(conditions_neurons_number);
1107 
1108  return(homogeneous_solution_Hessian_form);
1109 }
1110 
1111 
1112 // std::string write_expression(const Vector<std::string>&, const Vector<std::string>&, const Vector<std::string>&) const method
1113 
1119 
1120 std::string ConditionsLayer::write_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& inputs_names, const Vector<std::string>& outputs_names) const
1121 {
1122  std::ostringstream buffer;
1123 
1124  Vector<std::string> particular_solutions_names(conditions_neurons_number);
1125  Vector<std::string> homogeneous_solutions_names(conditions_neurons_number);
1126 
1127  for(size_t i = 0; i < conditions_neurons_number; i++)
1128  {
1129  buffer.str("");
1130  buffer << "particular_solution_" << inputs_names[i];
1131  particular_solutions_names[i] = buffer.str();
1132 
1133  buffer.str("");
1134  buffer << "homogeneous_solution_" << inputs_names[i];
1135  homogeneous_solutions_names[i] = buffer.str();
1136 
1137  buffer.str("");
1138  }
1139 
1140  buffer << write_particular_solution_expression(external_inputs_names, particular_solutions_names)
1141  << write_homogeneous_solution_expression(external_inputs_names, particular_solutions_names);
1142 
1143 
1144  for(size_t i = 0; i < conditions_neurons_number; i++)
1145  {
1146  buffer << outputs_names[i] << "=\n";
1147  }
1148 // <<
1149 // (external_inputs_names, particular_solutions_names, particular_solutions_names, inputs_names) << "\n";
1150 
1151 // for(size_t i = 0; i < conditions_neurons_number; i++)
1152 // {
1153 // buffer << particular_solutions_names[i] << " = " << write_particular_solution_expression(inputs_names, particular_solutions_names) << "\n"
1154 // << homogeneous_solutions_names[i] << " = " << "\n";
1155 
1156 
1157 // buffer << outputs_names[i] << " = " << particular_solution_expression[i] << "+" << homogeneous_solution_expression[i] << "*" << outputs_expression[i] << ";\n";
1158 // }
1159 
1160  return(buffer.str());
1161 }
1162 
1163 
1164 // std::string write_particular_solution_expression(const Vector<std::string>&, const Vector<std::string>&) const method
1165 
1170 
1171 std::string ConditionsLayer::write_particular_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& particular_solutions_names) const
1172 {
1173  switch(conditions_method)
1174  {
1175  case OneCondition:
1176  {
1177  return(write_one_condition_particular_solution_expression(external_inputs_names, particular_solutions_names));
1178  }
1179  break;
1180 
1181  case TwoConditions:
1182  {
1183  return(write_two_conditions_particular_solution_expression(external_inputs_names, particular_solutions_names));
1184  }
1185  break;
1186 
1187  default:
1188  {
1189  std::ostringstream buffer;
1190 
1191  buffer << "OpenNN Exception: ScalingLayer class\n"
1192  << "std::string write_particular_solution_expression(const Vector<std::string>&, const Vector<std::string>&) const method.\n"
1193  << "Unknown conditions method.\n";
1194 
1195  throw std::logic_error(buffer.str());
1196  }
1197  break;
1198  }
1199 }
1200 
1201 
1202 // std::string write_homogeneous_solution_expression(const Vector<std::string>&, const Vector<std::string>&) const method
1203 
1208 
1209 std::string ConditionsLayer::write_homogeneous_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& homogeneous_solutions_names) const
1210 {
1211  switch(conditions_method)
1212  {
1213  case OneCondition:
1214  {
1215  return(write_one_condition_homogeneous_solution_expression(external_inputs_names, homogeneous_solutions_names));
1216  }
1217  break;
1218 
1219  case TwoConditions:
1220  {
1221  return(write_two_conditions_homogeneous_solution_expression(external_inputs_names, homogeneous_solutions_names));
1222  }
1223  break;
1224 
1225  default:
1226  {
1227  std::ostringstream buffer;
1228 
1229  buffer << "OpenNN Exception: ScalingLayer class\n"
1230  << "std::string write_homogeneous_solution_expression(const Vector<std::string>&, const Vector<std::string>&) const method.\n"
1231  << "Unknown conditions method.\n";
1232 
1233  throw std::logic_error(buffer.str());
1234  }
1235  break;
1236  }
1237 }
1238 
1239 
1240 // std::string write_one_condition_particular_solution_expression(const Vector<std::string>&, const Vector<std::string>&) const method
1241 
1245 
1246 std::string ConditionsLayer::write_one_condition_particular_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& particular_solutions_names) const
1247 {
1248  std::ostringstream buffer;
1249 
1250  for(size_t i = 0; i < conditions_neurons_number; i++)
1251  {
1252  buffer << particular_solutions_names[i] << " = " << external_inputs_names[i] << "\n";
1253  }
1254 
1255  return(buffer.str());
1256 }
1257 
1258 
1259 // std::string write_one_condition_homogeneous_solution_expression(const Vector<std::string>&, const Vector<std::string>&) const method
1260 
1264 
1265 std::string ConditionsLayer::write_one_condition_homogeneous_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& homogeneous_solutions_names) const
1266 {
1267  std::ostringstream buffer;
1268 
1269  for(size_t i = 0; i < conditions_neurons_number; i++)
1270  {
1271  buffer << homogeneous_solutions_names[i] << " = " << external_inputs_names[i] << "\n";
1272  }
1273 
1274  return(buffer.str());
1275 }
1276 
1277 
1278 // std::string write_two_conditions_particular_solution_expression(const Vector<std::string>&, const Vector<std::string>&) const method
1279 
1283 
1284 std::string ConditionsLayer::write_two_conditions_particular_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& particular_solutions_names) const
1285 {
1286  std::ostringstream buffer;
1287 
1288  for(size_t i = 0; i < conditions_neurons_number; i++)
1289  {
1290  buffer << particular_solutions_names[i] << " = " << external_inputs_names[i] << "\n";
1291  }
1292 
1293  return(buffer.str());
1294 }
1295 
1296 
1297 // std::string write_two_conditions_homogeneous_solution_expression(const Vector<std::string>&, const Vector<std::string>&) const method
1298 
1302 
1303 std::string ConditionsLayer::write_two_conditions_homogeneous_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& homogeneous_solutions_names) const
1304 {
1305  std::ostringstream buffer;
1306 
1307  for(size_t i = 0; i < conditions_neurons_number; i++)
1308  {
1309  buffer << homogeneous_solutions_names[i] << " = " << external_inputs_names[i] << "\n";
1310  }
1311 
1312  return(buffer.str());
1313 }
1314 
1315 
1316 // std::string write_output_expression(const Vector<std::string>&, const Vector<std::string>&, const Vector<std::string>&, const Vector<std::string>&) const method
1317 
1323 
1324 std::string ConditionsLayer::write_output_expression(const Vector<std::string>& particular_solutions_names, const Vector<std::string>& homogeneous_solutions_names, const Vector<std::string>& inputs_names, const Vector<std::string>& outputs_names) const
1325 {
1326  std::ostringstream buffer;
1327 
1328  for(size_t i = 0; i < conditions_neurons_number; i++)
1329  {
1330  buffer << outputs_names[i] << "=" << particular_solutions_names[i] << "+" << homogeneous_solutions_names[i] << "*" << inputs_names[i] << ";\n";
1331  }
1332 
1333  return(buffer.str());
1334 }
1335 
1336 
1337 // std::string to_string(void) const method
1338 
1340 
1341 std::string ConditionsLayer::to_string(void) const
1342 {
1343  std::ostringstream buffer;
1344 
1345  buffer << "Conditions layer\n"
1346  << "External inputs number: " << external_inputs_number << "\n"
1347  << "Conditions neurons number: " <<conditions_neurons_number << "\n"
1348  << "Conditions method: " << write_conditions_method() << "\n"
1349  << "Input values: " <<external_input_values << "\n"
1350  << "Output values: " << output_values
1351  << "Display: " << display << "\n";
1352 
1353  return(buffer.str());
1354 }
1355 
1356 
1357 // tinyxml2::XMLDocument* to_XML(void) const method
1358 
1360 
1361 tinyxml2::XMLDocument* ConditionsLayer::to_XML(void) const
1362 {
1363  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
1364 
1365  std::ostringstream buffer;
1366 
1367  tinyxml2::XMLElement* conditions_layer_element = document->NewElement("ConditionsLayer");
1368 
1369  document->InsertFirstChild(conditions_layer_element);
1370 
1371  // Inputs number
1372  {
1373  tinyxml2::XMLElement* element = document->NewElement("ExternalInputsNumber");
1374  conditions_layer_element->LinkEndChild(element);
1375 
1376  buffer.str("");
1377  buffer << external_inputs_number;
1378 
1379  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
1380  element->LinkEndChild(text);
1381  }
1382 
1383  // Conditions neurons number
1384  {
1385  tinyxml2::XMLElement* element = document->NewElement("ConditionsNeuronsNumber");
1386  conditions_layer_element->LinkEndChild(element);
1387 
1388  buffer.str("");
1389  buffer << conditions_neurons_number;
1390 
1391  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
1392  element->LinkEndChild(text);
1393  }
1394 
1395  // Conditions method
1396  {
1397  tinyxml2::XMLElement* element = document->NewElement("ConditionsMethod");
1398  conditions_layer_element->LinkEndChild(element);
1399 
1400  buffer.str("");
1401  buffer << write_conditions_method();
1402 
1403  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
1404  element->LinkEndChild(text);
1405  }
1406 
1407  // Input values
1408  {
1409  tinyxml2::XMLElement* element = document->NewElement("InputValues");
1410  conditions_layer_element->LinkEndChild(element);
1411 
1412  buffer.str("");
1413  buffer << external_input_values;
1414 
1415  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
1416  element->LinkEndChild(text);
1417  }
1418 
1419  // Output values
1420  {
1421  tinyxml2::XMLElement* element = document->NewElement("OutputValues");
1422  conditions_layer_element->LinkEndChild(element);
1423 
1424  buffer.str("");
1425  buffer << output_values.to_vector();
1426 
1427  tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
1428  element->LinkEndChild(text);
1429  }
1430 
1431  // Display
1432  {
1433  tinyxml2::XMLElement* display_element = document->NewElement("Display");
1434  conditions_layer_element->LinkEndChild(display_element);
1435 
1436  buffer.str("");
1437  buffer << display;
1438 
1439  tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
1440  display_element->LinkEndChild(display_text);
1441  }
1442 
1443  return(document);
1444 }
1445 
1446 
1447 // void from_XML(const tinyxml2::XMLDocument&) method
1448 
1452 
1453 void ConditionsLayer::from_XML(const tinyxml2::XMLDocument& document)
1454 {
1455  // Inputs number
1456  {
1457  const tinyxml2::XMLElement* element = document.FirstChildElement("ExternalInputsNumber");
1458 
1459  if(element)
1460  {
1461  const char* text = element->GetText();
1462 
1463  if(text)
1464  {
1465  try
1466  {
1467  set_external_inputs_number(atoi(text));
1468  }
1469  catch(const std::logic_error& e)
1470  {
1471  std::cout << e.what() << std::endl;
1472  }
1473  }
1474  }
1475  }
1476 
1477  // Inputs number
1478  {
1479  const tinyxml2::XMLElement* element = document.FirstChildElement("ConditionsNeuronsNumber");
1480 
1481  if(element)
1482  {
1483  const char* text = element->GetText();
1484 
1485  if(text)
1486  {
1487  try
1488  {
1489  set_conditions_neurons_number(atoi(text));
1490  }
1491  catch(const std::logic_error& e)
1492  {
1493  std::cout << e.what() << std::endl;
1494  }
1495  }
1496  }
1497  }
1498 
1499  // Conditions method
1500  {
1501  const tinyxml2::XMLElement* element = document.FirstChildElement("ConditionsMethod");
1502 
1503  if(element)
1504  {
1505  const char* text = element->GetText();
1506 
1507  if(text)
1508  {
1509  try
1510  {
1511  std::string new_conditions_method(text);
1512 
1513  set_conditions_method(new_conditions_method);
1514  }
1515  catch(const std::logic_error& e)
1516  {
1517  std::cout << e.what() << std::endl;
1518  }
1519  }
1520  }
1521  }
1522 
1523  // Input values
1524  {
1525  const tinyxml2::XMLElement* element = document.FirstChildElement("InputValues");
1526 
1527  if(element)
1528  {
1529  const char* text = element->GetText();
1530 
1531  if(text)
1532  {
1533  try
1534  {
1535  Vector<double> new_external_input_values;
1536  new_external_input_values.parse(text);
1537 
1538  set_external_input_values(new_external_input_values);
1539  }
1540  catch(const std::logic_error& e)
1541  {
1542  std::cout << e.what() << std::endl;
1543  }
1544  }
1545  }
1546  }
1547 
1548  // Output values
1549  {
1550  const tinyxml2::XMLElement* element = document.FirstChildElement("OutputValues");
1551 
1552  if(element)
1553  {
1554  const char* text = element->GetText();
1555 
1556  if(text)
1557  {
1558  try
1559  {
1560  Vector<double> new_output_values_vector;
1561  new_output_values_vector.parse(text);
1562 
1563  const Matrix<double> new_output_values = new_output_values_vector.to_matrix(conditions_neurons_number, 2);
1564 
1565  set_output_values(new_output_values);
1566  }
1567  catch(const std::logic_error& e)
1568  {
1569  std::cout << e.what() << std::endl;
1570  }
1571  }
1572  }
1573  }
1574 
1575  // Display
1576  {
1577  const tinyxml2::XMLElement* element = document.FirstChildElement("Display");
1578 
1579  if(element)
1580  {
1581  const char* text = element->GetText();
1582 
1583  if(text)
1584  {
1585  try
1586  {
1587  const std::string string(text);
1588 
1589  set_display(string != "0");
1590  }
1591  catch(const std::logic_error& e)
1592  {
1593  std::cout << e.what() << std::endl;
1594  }
1595  }
1596  }
1597  }
1598 }
1599 
1600 }
1601 
1602 // OpenNN: Open Neural Networks Library.
1603 // Copyright (c) 2005-2015 Roberto Lopez.
1604 //
1605 // This library is free software; you can redistribute it and/or
1606 // modify it under the terms of the GNU Lesser General Public
1607 // License as published by the Free Software Foundation; either
1608 // version 2.1 of the License, or any later version.
1609 //
1610 // This library is distributed in the hope that it will be useful,
1611 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1612 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1613 // Lesser General Public License for more details.
1614 
1615 // You should have received a copy of the GNU Lesser General Public
1616 // License along with this library; if not, write to the Free Software
1617 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
void parse(const std::string &)
Definition: vector.h:5217
void set_external_input_values(const Vector< double > &)
const size_t & get_external_inputs_number(void) const
Returns the number of external inputs.
Matrix< double > calculate_Jacobian(const Vector< double > &, const Vector< double > &, const Matrix< double > &) const
std::string write_two_conditions_particular_solution_expression(const Vector< std::string > &, const Vector< std::string > &) const
void set_default(void)
Sets the members of this conditions layer object to their default values.
const double & get_output_value(const size_t &, const size_t &) const
Vector< Matrix< double > > calculate_Hessian_form(const Vector< double > &, const Vector< double > &) const
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &, const Vector< std::string > &) const
Vector< double > calculate_outputs(const Vector< double > &, const Vector< double > &) const
void set_conditions_neurons_number(const size_t &)
Vector< double > calculate_two_conditions_particular_solution(const Vector< double > &) const
const size_t & get_conditions_neurons_number(void) const
Returns the number of conditions neurons.
void set(void)
Sets the size of a vector to zero.
Definition: vector.h:656
void set_external_inputs_number(const size_t &)
Vector< Matrix< double > > calculate_one_condition_homogeneous_solution_Hessian_form(const Vector< double > &) const
Vector< double > calculate_two_conditions_homogeneous_solution(const Vector< double > &) const
void set_output_values(const Matrix< double > &)
void set_display(const bool &)
bool operator==(const ConditionsLayer &) const
Vector< Matrix< double > > calculate_two_conditions_homogeneous_solution_Hessian_form(const Vector< double > &) const
Computes the homogeneous solution Hessian form for the two boundary conditions case.
const Vector< double > & get_external_input_values(void) const
Returns the input values for the conditions.
Vector< double > external_input_values
Independent values of the conditions.
virtual std::string write_particular_solution_expression(const Vector< std::string > &, const Vector< std::string > &) const
Vector< double > calculate_one_condition_homogeneous_solution(const Vector< double > &) const
void set_conditions_method(const ConditionsMethod &)
const ConditionsMethod & get_conditions_method(void) const
const bool & get_display(void) const
virtual ~ConditionsLayer(void)
Destructor.
virtual Matrix< double > calculate_homogeneous_solution_Jacobian(const Vector< double > &) const
Returns the homogeneous solution Jacobian for applying boundary conditions.
ConditionsLayer & operator=(const ConditionsLayer &)
std::string write_two_conditions_homogeneous_solution_expression(const Vector< std::string > &, const Vector< std::string > &) const
tinyxml2::XMLDocument * to_XML(void) const
Serializes the conditions layer object into a document of the TinyXML library.
std::string write_one_condition_particular_solution_expression(const Vector< std::string > &, const Vector< std::string > &) const
std::string write_output_expression(const Vector< std::string > &, const Vector< std::string > &, const Vector< std::string > &, const Vector< std::string > &) const
Matrix< double > calculate_one_condition_homogeneous_solution_Jacobian(const Vector< double > &) const
Computes the homogeneous solution Jacobian for the one boundary condition case.
std::string to_string(void) const
Returns a string representation of the current conditions layer object.
virtual Vector< double > calculate_homogeneous_solution(const Vector< double > &) const
Returns the homogeneous solution for applying boundary conditions.
Matrix< double > calculate_one_condition_particular_solution_Jacobian(const Vector< double > &) const
Computes the Jacobian of the particular solution function for the one boundary condition case...
void from_XML(const tinyxml2::XMLDocument &)
Matrix< T > to_matrix(const size_t &, const size_t &) const
Definition: vector.h:5338
void set(void)
This method set the numbers of rows and columns of the matrix to zero.
Definition: matrix.h:1101
const size_t & get_rows_number(void) const
Returns the number of rows in the matrix.
Definition: matrix.h:1079
Matrix< double > output_values
Dependent values of the conditions.
Vector< Matrix< double > > calculate_one_condition_particular_solution_Hessian_form(const Vector< double > &) const
void set_external_input_value(const size_t &, const double &)
virtual Vector< double > calculate_particular_solution(const Vector< double > &) const
Returns the particular solution for applying boundary conditions.
virtual std::string write_homogeneous_solution_expression(const Vector< std::string > &, const Vector< std::string > &) const
Vector< Matrix< double > > calculate_two_conditions_particular_solution_Hessian_form(const Vector< double > &) const
ConditionsMethod conditions_method
Conditions method (one or two conditions).
Matrix< double > calculate_two_conditions_homogeneous_solution_Jacobian(const Vector< double > &) const
ConditionsMethod
Enumeration of available methods for applying conditions.
void set_output_value(const size_t &, const size_t &, const double &)
std::string write_one_condition_homogeneous_solution_expression(const Vector< std::string > &, const Vector< std::string > &) const
std::string write_conditions_method(void) const
const double & get_external_input_value(const size_t &) const
void check(void) const
Checks that all the members of the conditions layer are correct before calculating the outputs from t...
Vector< T > to_vector(void) const
Definition: matrix.h:6642
Vector< double > calculate_one_condition_particular_solution(const Vector< double > &) const
virtual Vector< Matrix< double > > calculate_particular_solution_Hessian_form(const Vector< double > &) const
virtual Matrix< double > calculate_particular_solution_Jacobian(const Vector< double > &) const
Returns the particular solution Jacobian for applying boundary conditions.
virtual Vector< Matrix< double > > calculate_homogeneous_solution_Hessian_form(const Vector< double > &) const
Matrix< double > calculate_two_conditions_particular_solution_Jacobian(const Vector< double > &) const
bool display
Display messages to screen.
const Matrix< double > & get_output_values(void) const
Returns the output values for the conditions.