OpenNN  2.2
Open Neural Networks Library
training_strategy.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* T R A I N I N G S T R A T E G Y 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 "training_strategy.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
26 
28  : performance_functional_pointer(NULL)
29  , random_search_pointer(NULL)
30  , evolutionary_algorithm_pointer(NULL)
31  , gradient_descent_pointer(NULL)
32  , conjugate_gradient_pointer(NULL)
33  , quasi_Newton_method_pointer(NULL)
34  , Levenberg_Marquardt_algorithm_pointer(NULL)
35  , Newton_method_pointer(NULL)
36 {
37  set_initialization_type(NO_INITIALIZATION);
38  set_main_type(QUASI_NEWTON_METHOD);
39  set_refinement_type(NO_REFINEMENT);
40 
41  set_default();
42 }
43 
44 
45 // PERFORMANCE FUNCTIONAL CONSTRUCTOR
46 
51 
52 TrainingStrategy::TrainingStrategy(PerformanceFunctional* new_performance_functional_pointer)
53  : performance_functional_pointer(new_performance_functional_pointer)
54  , random_search_pointer(NULL)
55  , evolutionary_algorithm_pointer(NULL)
56  , gradient_descent_pointer(NULL)
57  , conjugate_gradient_pointer(NULL)
58  , quasi_Newton_method_pointer(NULL)
59  , Levenberg_Marquardt_algorithm_pointer(NULL)
60  , Newton_method_pointer(NULL)
61 {
62  set_initialization_type(NO_INITIALIZATION);
63  set_main_type(QUASI_NEWTON_METHOD);
64  set_refinement_type(NO_REFINEMENT);
65 
66  set_default();
67 }
68 
69 
70 // XML CONSTRUCTOR
71 
76 
77 TrainingStrategy::TrainingStrategy(const tinyxml2::XMLDocument& document)
78  : performance_functional_pointer(NULL)
79  , random_search_pointer(NULL)
80  , evolutionary_algorithm_pointer(NULL)
81  , gradient_descent_pointer(NULL)
82  , conjugate_gradient_pointer(NULL)
83  , quasi_Newton_method_pointer(NULL)
84  , Levenberg_Marquardt_algorithm_pointer(NULL)
85  , Newton_method_pointer(NULL)
86 {
87  set_initialization_type(NO_INITIALIZATION);
88  set_main_type(QUASI_NEWTON_METHOD);
89  set_refinement_type(NO_REFINEMENT);
90 
91  set_default();
92 
93  from_XML(document);
94 }
95 
96 
97 // FILE CONSTRUCTOR
98 
103 
104 TrainingStrategy::TrainingStrategy(const std::string& file_name)
105  : performance_functional_pointer(NULL)
106  , random_search_pointer(NULL)
107  , evolutionary_algorithm_pointer(NULL)
108  , gradient_descent_pointer(NULL)
109  , conjugate_gradient_pointer(NULL)
110  , quasi_Newton_method_pointer(NULL)
111  , Levenberg_Marquardt_algorithm_pointer(NULL)
112  , Newton_method_pointer(NULL)
113 {
114  set_initialization_type(NO_INITIALIZATION);
115  set_main_type(QUASI_NEWTON_METHOD);
116  set_refinement_type(NO_REFINEMENT);
117 
118  set_default();
119 
120  load(file_name);
121 }
122 
123 
124 // DESTRUCTOR
125 
128 
130 {
131  delete random_search_pointer;
133 
138 
139  delete Newton_method_pointer;
140 }
141 
142 
143 // METHODS
144 
145 
146 // void check_performance_functional(void) const method
147 
149 
151 {
153  {
154  std::ostringstream buffer;
155 
156  buffer << "OpenNN Exception: TrainingStrategy class.\n"
157  << "void check_performance_functional(void) const.\n"
158  << "Pointer to performance functional is NULL.\n";
159 
160  throw std::logic_error(buffer.str());
161  }
162 }
163 
164 
165 // void check_training_algorithms(void) const method
166 
169 
171 {
172  if(initialization_type == NO_INITIALIZATION
173  && main_type == NO_MAIN
174  && refinement_type == NO_REFINEMENT)
175  {
176  std::ostringstream buffer;
177 
178  buffer << "OpenNN Exception: TrainingStrategy class.\n"
179  << "void check_training_algorithms(void) const method.\n"
180  << "None initialization, main or refinement terms are used.\n";
181 
182  throw std::logic_error(buffer.str());
183  }
184 }
185 
186 
187 // void initialize_random(void) method
188 
191 
193 {
194  // Initialization training algorithm
195 
196  switch(rand()%2)
197  {
198  case 0:
199  {
200  }
201  break;
202 
203  case 1:
204  {
205  }
206  break;
207 
208  default:
209  {
210  std::ostringstream buffer;
211 
212  buffer << "OpenNN Exception: TrainingStrategy class.\n"
213  << "void initialize_random(void) method.\n"
214  << "Unknown initialization training algorithm.\n";
215 
216  throw std::logic_error(buffer.str());
217  }
218  break;
219  }
220 
221  // Main training algorithm
222 
223  // Refinement training algorithm
224 
225 }
226 
227 
228 // PerformanceFunctional* get_performance_functional_pointer(void) const method
229 
231 
233 {
235  {
236  std::ostringstream buffer;
237 
238  buffer << "OpenNN Exception: TrainingStrategy class.\n"
239  << "PerformanceFunctional* get_performance_functional_pointer(void) const method.\n"
240  << "Performance functional pointer is NULL.\n";
241 
242  throw std::logic_error(buffer.str());
243  }
244 
246 }
247 
248 
249 // bool has_performance_functional(void) const method
250 
253 
255 {
257  {
258  return(true);
259  }
260  else
261  {
262  return(false);
263  }
264 }
265 
266 
267 // RandomSearch* get_random_search_pointer(void) const method
268 
271 
273 {
275  {
276  std::ostringstream buffer;
277 
278  buffer << "OpenNN Exception: TrainingStrategy class.\n"
279  << "RandomSearch* get_random_search_pointer(void) const method.\n"
280  << "Random search pointer is NULL.\n";
281 
282  throw std::logic_error(buffer.str());
283  }
284 
285  return(random_search_pointer);
286 }
287 
288 
289 // EvolutionaryAlgorithm* get_evolutionary_algorithm_pointer(void) const method
290 
293 
295 {
297  {
298  std::ostringstream buffer;
299 
300  buffer << "OpenNN Exception: TrainingStrategy class.\n"
301  << "EvolutionaryAlgorithm* get_evolutionary_algorithm_pointer(void) const method.\n"
302  << "Evolutionary algorithm pointer is NULL.\n";
303 
304  throw std::logic_error(buffer.str());
305  }
306 
308 }
309 
310 
311 // GradientDescent* get_gradient_descent_pointer(void) const method
312 
315 
317 {
319  {
320  std::ostringstream buffer;
321 
322  buffer << "OpenNN Exception: TrainingStrategy class.\n"
323  << "GradientDescent* get_gradient_descent_pointer(void) const method.\n"
324  << "Gradient descent pointer is NULL.\n";
325 
326  throw std::logic_error(buffer.str());
327  }
328 
329  return(gradient_descent_pointer);
330 }
331 
332 
333 // ConjugateGradient* get_conjugate_gradient_pointer(void) const method
334 
337 
339 {
341  {
342  std::ostringstream buffer;
343 
344  buffer << "OpenNN Exception: TrainingStrategy class.\n"
345  << "ConjugateGradient* get_conjugate_gradient_pointer(void) const method.\n"
346  << "Conjugate gradient pointer is NULL.\n";
347 
348  throw std::logic_error(buffer.str());
349  }
350 
352 }
353 
354 
355 // QuasiNewtonMethod* get_quasi_Newton_method_pointer(void) const method
356 
359 
361 {
363  {
364  std::ostringstream buffer;
365 
366  buffer << "OpenNN Exception: TrainingStrategy class.\n"
367  << "QuasiNetwtonMethod* get_quasi_Newton_method_pointer(void) const method.\n"
368  << "Quasi-Newton method pointer is NULL.\n";
369 
370  throw std::logic_error(buffer.str());
371  }
372 
374 }
375 
376 
377 // LevenbergMarquardtAlgorithm* get_Levenberg_Marquardt_algorithm_pointer(void) const method
378 
381 
383 {
385  {
386  std::ostringstream buffer;
387 
388  buffer << "OpenNN Exception: TrainingStrategy class.\n"
389  << "LevenbergMarquardtAlgorithm* get_Levenberg_Marquardt_algorithm_pointer(void) const method.\n"
390  << "Levenberg-Marquardt algorithm pointer is NULL.\n";
391 
392  throw std::logic_error(buffer.str());
393  }
394 
396 }
397 
398 
399 // NewtonMethod* get_Newton_method_pointer(void) const method
400 
403 
405 {
407  {
408  std::ostringstream buffer;
409 
410  buffer << "OpenNN Exception: TrainingStrategy class.\n"
411  << "NewtonMethod* get_Newton_method_pointer(void) const method.\n"
412  << "Newton method pointer is NULL.\n";
413 
414  throw std::logic_error(buffer.str());
415  }
416 
417  return(Newton_method_pointer);
418 }
419 
420 
421 // const TrainingAlgorithmType& get_initialization_type(void) const method
422 
424 
426 {
427  return(initialization_type);
428 }
429 
430 
431 // const MainType& get_main_type(void) const method
432 
434 
436 {
437  return(main_type);
438 }
439 
440 
441 // const RefinementType& get_refinement_type(void) const method
442 
444 
446 {
447  return(refinement_type);
448 }
449 
450 
451 // std::string TrainingStrategy::write_initialization_type(void) const
452 
454 
456 {
457  if(initialization_type == NO_INITIALIZATION)
458  {
459  return("NO_INITIALIZATION");
460  }
461  else if(initialization_type == RANDOM_SEARCH)
462  {
463  return("RANDOM_SEARCH");
464  }
465  else if(initialization_type == EVOLUTIONARY_ALGORITHM)
466  {
467  return("EVOLUTIONARY_ALGORITHM");
468  }
469  else if(initialization_type == USER_INITIALIZATION)
470  {
471  return("USER_INITIALIZATION");
472  }
473  else
474  {
475  std::ostringstream buffer;
476 
477  buffer << "OpenNN Exception: TrainingStrategy class.\n"
478  << "std::string write_initialization_type(void) const method.\n"
479  << "Unknown training algorithm type.\n";
480 
481  throw std::logic_error(buffer.str());
482  }
483 }
484 
485 
486 // std::string TrainingStrategy::write_main_type(void) const
487 
489 
490 std::string TrainingStrategy::write_main_type(void) const
491 {
492  if(main_type == NO_MAIN)
493  {
494  return("NO_MAIN");
495  }
496  else if(main_type == GRADIENT_DESCENT)
497  {
498  return("GRADIENT_DESCENT");
499  }
500  else if(main_type == CONJUGATE_GRADIENT)
501  {
502  return("CONJUGATE_GRADIENT");
503  }
504  else if(main_type == QUASI_NEWTON_METHOD)
505  {
506  return("QUASI_NEWTON_METHOD");
507  }
508  else if(main_type == LEVENBERG_MARQUARDT_ALGORITHM)
509  {
510  return("LEVENBERG_MARQUARDT_ALGORITHM");
511  }
512  else if(main_type == USER_MAIN)
513  {
514  return("USER_MAIN");
515  }
516  else
517  {
518  std::ostringstream buffer;
519 
520  buffer << "OpenNN Exception: TrainingStrategy class.\n"
521  << "std::string write_main_type(void) const method.\n"
522  << "Unknown main type.\n";
523 
524  throw std::logic_error(buffer.str());
525  }
526 }
527 
528 
529 // std::string TrainingStrategy::write_refinement_type(void) const
530 
532 
534 {
535  if(refinement_type == NO_REFINEMENT)
536  {
537  return("NO_REFINEMENT");
538  }
539  else if(refinement_type == NEWTON_METHOD)
540  {
541  return("NEWTON_METHOD");
542  }
543  else if(refinement_type == USER_REFINEMENT)
544  {
545  return("USER_REFINEMENT");
546  }
547  else
548  {
549  std::ostringstream buffer;
550 
551  buffer << "OpenNN Exception: TrainingStrategy class.\n"
552  << "std::string write_refinement_type(void) const method.\n"
553  << "Unknown refinement type.\n";
554 
555  throw std::logic_error(buffer.str());
556  }
557 }
558 
559 
560 // std::string TrainingStrategy::write_initialization_type_text(void) const
561 
563 
565 {
566  if(initialization_type == NO_INITIALIZATION)
567  {
568  return("none");
569  }
570  else if(initialization_type == RANDOM_SEARCH)
571  {
572  return("random search");
573  }
574  else if(initialization_type == EVOLUTIONARY_ALGORITHM)
575  {
576  return("evolutionary algorithm");
577  }
578  else if(initialization_type == USER_INITIALIZATION)
579  {
580  return("user defined");
581  }
582  else
583  {
584  std::ostringstream buffer;
585 
586  buffer << "OpenNN Exception: TrainingStrategy class.\n"
587  << "std::string write_initialization_type_text(void) const method.\n"
588  << "Unknown training algorithm type.\n";
589 
590  throw std::logic_error(buffer.str());
591  }
592 }
593 
594 
595 // std::string TrainingStrategy::write_main_type_text(void) const
596 
598 
600 {
601  if(main_type == NO_MAIN)
602  {
603  return("none");
604  }
605  else if(main_type == GRADIENT_DESCENT)
606  {
607  return("gradient descent");
608  }
609  else if(main_type == CONJUGATE_GRADIENT)
610  {
611  return("conjugate gradient");
612  }
613  else if(main_type == QUASI_NEWTON_METHOD)
614  {
615  return("quasi-Newton method");
616  }
617  else if(main_type == LEVENBERG_MARQUARDT_ALGORITHM)
618  {
619  return("Levenberg-Marquardt algorithm");
620  }
621  else if(main_type == USER_MAIN)
622  {
623  return("user defined");
624  }
625  else
626  {
627  std::ostringstream buffer;
628 
629  buffer << "OpenNN Exception: TrainingStrategy class.\n"
630  << "std::string write_main_type_text(void) const method.\n"
631  << "Unknown main type.\n";
632 
633  throw std::logic_error(buffer.str());
634  }
635 }
636 
637 
638 // std::string TrainingStrategy::write_refinement_type(void) const
639 
641 
643 {
644  if(refinement_type == NO_REFINEMENT)
645  {
646  return("none");
647  }
648  else if(refinement_type == NEWTON_METHOD)
649  {
650  return("Newton method");
651  }
652  else if(refinement_type == USER_REFINEMENT)
653  {
654  return("user defined");
655  }
656  else
657  {
658  std::ostringstream buffer;
659 
660  buffer << "OpenNN Exception: TrainingStrategy class.\n"
661  << "std::string write_refinement_type_text(void) const method.\n"
662  << "Unknown refinement type.\n";
663 
664  throw std::logic_error(buffer.str());
665  }
666 }
667 
668 
669 // const bool& get_display(void) const method
670 
673 
674 const bool& TrainingStrategy::get_display(void) const
675 {
676  return(display);
677 }
678 
679 
680 // void set(void) method
681 
685 
687 {
689 
690  set_initialization_type(NO_INITIALIZATION);
691  set_main_type(QUASI_NEWTON_METHOD);
692  set_refinement_type(NO_REFINEMENT);
693 
694  set_default();
695 }
696 
697 
698 // void set(PerformanceFunctional*) method
699 
704 
705 void TrainingStrategy::set(PerformanceFunctional* new_performance_functional_pointer)
706 {
707  performance_functional_pointer = new_performance_functional_pointer;
708 
709  set_initialization_type(NO_INITIALIZATION);
710  set_main_type(QUASI_NEWTON_METHOD);
711  set_refinement_type(NO_REFINEMENT);
712 
713  set_default();
714 }
715 
716 
717 // void set_initialization_type(const InitializationType&) method
718 
721 
723 {
725 
726  initialization_type = new_initialization_type;
727 
728  switch(initialization_type)
729  {
730  case NO_INITIALIZATION:
731  {
732  // do nothing
733  }
734  break;
735 
736  case RANDOM_SEARCH:
737  {
739  }
740  break;
741 
742  case EVOLUTIONARY_ALGORITHM:
743  {
745  }
746  break;
747 
748  case USER_INITIALIZATION:
749  {
750  // do nothing
751  }
752  break;
753 
754  default:
755  {
756  std::ostringstream buffer;
757 
758  buffer << "OpenNN Exception: TrainingStrategy class.\n"
759  << "void set_initialization_type(const InitializationType&) method.\n"
760  << "Unknown initialization type.\n";
761 
762  throw std::logic_error(buffer.str());
763  }
764  break;
765  }
766 }
767 
768 
769 // void set_main_type(const MainType&) method
770 
773 
774 void TrainingStrategy::set_main_type(const MainType& new_main_type)
775 {
776  destruct_main();
777 
778  main_type = new_main_type;
779 
780  switch(main_type)
781  {
782  case NO_MAIN:
783  {
784  // do nothing
785  }
786  break;
787 
788  case GRADIENT_DESCENT:
789  {
791  }
792  break;
793 
794  case CONJUGATE_GRADIENT:
795  {
797  }
798  break;
799 
800  case QUASI_NEWTON_METHOD:
801  {
803  }
804  break;
805 
806  case LEVENBERG_MARQUARDT_ALGORITHM:
807  {
809  }
810  break;
811 
812  case USER_MAIN:
813  {
814  // do nothing
815  }
816  break;
817 
818  default:
819  {
820  std::ostringstream buffer;
821 
822  buffer << "OpenNN Exception: TrainingStrategy class.\n"
823  << "void set_initialization_type(const MainType&) method.\n"
824  << "Unknown main type.\n";
825 
826  throw std::logic_error(buffer.str());
827  }
828  break;
829  }
830 }
831 
832 
833 // void set_refinement_type(const RefinementType&) method
834 
838 
840 {
842 
843  refinement_type = new_refinement_type;
844 
845  switch(refinement_type)
846  {
847  case NO_REFINEMENT:
848  {
849  // do nothing
850  }
851  break;
852 
853  case NEWTON_METHOD:
854  {
856  }
857  break;
858 
859  case USER_REFINEMENT:
860  {
861  // do nothing
862  }
863  break;
864 
865  default:
866  {
867  std::ostringstream buffer;
868 
869  buffer << "OpenNN Exception: TrainingStrategy class.\n"
870  << "void set_refinement_type(const RefinementType&) method.\n"
871  << "Unknown refinement type.\n";
872 
873  throw std::logic_error(buffer.str());
874  }
875  break;
876  }
877 }
878 
879 
880 // void set_initialization_type(const std::string&) method
881 
884 
885 void TrainingStrategy::set_initialization_type(const std::string& new_initialization_type)
886 {
887  if(new_initialization_type == "NO_INITIALIZATION")
888  {
889  set_initialization_type(NO_INITIALIZATION);
890  }
891  else if(new_initialization_type == "RANDOM_SEARCH")
892  {
893  set_initialization_type(RANDOM_SEARCH);
894  }
895  else if(new_initialization_type == "EVOLUTIONARY_ALGORITHM")
896  {
897  set_initialization_type(EVOLUTIONARY_ALGORITHM);
898  }
899  else if(new_initialization_type == "USER_INITIALIZATION")
900  {
901  set_initialization_type(USER_INITIALIZATION);
902  }
903  else
904  {
905  std::ostringstream buffer;
906 
907  buffer << "OpenNN Exception: TrainingStrategy class.\n"
908  << "void set_initialization_type(const std::string&) method.\n"
909  << "Unknown initialization type: " << new_initialization_type << ".\n";
910 
911  throw std::logic_error(buffer.str());
912  }
913 }
914 
915 
916 // void set_main_type(const std::string&) method
917 
920 
921 void TrainingStrategy::set_main_type(const std::string& new_main_type)
922 {
923  if(new_main_type == "NO_MAIN")
924  {
925  set_main_type(NO_MAIN);
926  }
927  else if(new_main_type == "GRADIENT_DESCENT")
928  {
929  set_main_type(GRADIENT_DESCENT);
930  }
931  else if(new_main_type == "CONJUGATE_GRADIENT")
932  {
933  set_main_type(CONJUGATE_GRADIENT);
934  }
935  else if(new_main_type == "QUASI_NEWTON_METHOD")
936  {
937  set_main_type(QUASI_NEWTON_METHOD);
938  }
939  else if(new_main_type == "LEVENBERG_MARQUARDT_ALGORITHM")
940  {
941  set_main_type(LEVENBERG_MARQUARDT_ALGORITHM);
942  }
943  else if(new_main_type == "USER_MAIN")
944  {
945  set_main_type(USER_MAIN);
946  }
947  else
948  {
949  std::ostringstream buffer;
950 
951  buffer << "OpenNN Exception: TrainingStrategy class.\n"
952  << "void set_main_type(const std::string&) method.\n"
953  << "Unknown main type: " << new_main_type << ".\n";
954 
955  throw std::logic_error(buffer.str());
956  }
957 }
958 
959 
960 // void set_refinement_type(const std::string&) method
961 
965 
966 void TrainingStrategy::set_refinement_type(const std::string& new_refinement_type)
967 {
968  if(new_refinement_type == "NO_REFINEMENT")
969  {
970  set_refinement_type(NO_REFINEMENT);
971  }
972  else if(new_refinement_type == "NEWTON_METHOD")
973  {
974  set_refinement_type(NEWTON_METHOD);
975  }
976  else if(new_refinement_type == "USER_REFINEMENT")
977  {
978  set_refinement_type(USER_REFINEMENT);
979  }
980  else
981  {
982  std::ostringstream buffer;
983 
984  buffer << "OpenNN Exception: TrainingStrategy class.\n"
985  << "void set_refinement_type(const std::string&) method.\n"
986  << "Unknown refinement type: " << new_refinement_type << ".\n";
987 
988  throw std::logic_error(buffer.str());
989  }
990 }
991 
992 
993 // void set_performance_functional_pointer(PerformanceFunctional*) method
994 
997 
999 {
1000  performance_functional_pointer = new_performance_functional_pointer;
1001 
1002  // Initialization
1003 
1004  switch(initialization_type)
1005  {
1006  case NO_INITIALIZATION:
1007  {
1008  // do nothing
1009  }
1010  break;
1011 
1012  case RANDOM_SEARCH:
1013  {
1014  random_search_pointer->set_performance_functional_pointer(new_performance_functional_pointer);
1015  }
1016  break;
1017 
1018  case EVOLUTIONARY_ALGORITHM:
1019  {
1020  evolutionary_algorithm_pointer->set_performance_functional_pointer(new_performance_functional_pointer);
1021  }
1022  break;
1023 
1024  case USER_INITIALIZATION:
1025  {
1026  // do nothing
1027  }
1028  break;
1029 
1030  default:
1031  {
1032  std::ostringstream buffer;
1033 
1034  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1035  << "void set_performance_functional_pointer(PerformanceFunctional*) method.\n"
1036  << "Unknown initialization type.\n";
1037 
1038  throw std::logic_error(buffer.str());
1039  }
1040  break;
1041  }
1042 
1043  // Main
1044 
1045  switch(main_type)
1046  {
1047  case NO_MAIN:
1048  {
1049  // do nothing
1050  }
1051  break;
1052 
1053  case GRADIENT_DESCENT:
1054  {
1055  gradient_descent_pointer->set_performance_functional_pointer(new_performance_functional_pointer);
1056  }
1057  break;
1058 
1059  case CONJUGATE_GRADIENT:
1060  {
1061  conjugate_gradient_pointer->set_performance_functional_pointer(new_performance_functional_pointer);
1062  }
1063  break;
1064 
1065  case QUASI_NEWTON_METHOD:
1066  {
1067  quasi_Newton_method_pointer->set_performance_functional_pointer(new_performance_functional_pointer);
1068  }
1069  break;
1070 
1071  case LEVENBERG_MARQUARDT_ALGORITHM:
1072  {
1074  }
1075  break;
1076 
1077  case USER_MAIN:
1078  {
1079  // do nothing
1080  }
1081  break;
1082 
1083  default:
1084  {
1085  std::ostringstream buffer;
1086 
1087  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1088  << "void set_performance_functional_pointer(PerformanceFunctional*) method.\n"
1089  << "Unknown main type.\n";
1090 
1091  throw std::logic_error(buffer.str());
1092  }
1093  break;
1094  }
1095 
1096  // Refinement
1097 
1098  switch(refinement_type)
1099  {
1100  case NO_REFINEMENT:
1101  {
1102  // do nothing
1103  }
1104  break;
1105 
1106  case NEWTON_METHOD:
1107  {
1108  Newton_method_pointer->set_performance_functional_pointer(new_performance_functional_pointer);
1109  }
1110  break;
1111 
1112  case USER_REFINEMENT:
1113  {
1114  // do nothing
1115  }
1116  break;
1117 
1118  default:
1119  {
1120  std::ostringstream buffer;
1121 
1122  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1123  << "void set_performance_functional_pointer(PerformanceFunctional) method.\n"
1124  << "Unknown refinement type.\n";
1125 
1126  throw std::logic_error(buffer.str());
1127  }
1128  break;
1129  }
1130 }
1131 
1132 
1133 // void set_display(const bool&) method
1134 
1139 
1140 void TrainingStrategy::set_display(const bool& new_display)
1141 {
1142  display = new_display;
1143 }
1144 
1145 
1146 // void set_default(void) method
1147 
1152 
1154 {
1155 
1156 
1157  display = true;
1158 }
1159 
1160 
1161 
1162 // void destruct_initialization(void) method
1163 
1165 
1167 {
1168  delete random_search_pointer;
1170 
1171  random_search_pointer = NULL;
1173 
1174  initialization_type = NO_INITIALIZATION;
1175 }
1176 
1177 
1178 // void destruct_main(void) method
1179 
1181 
1183 {
1184  delete gradient_descent_pointer;
1188 
1189  gradient_descent_pointer = NULL;
1193 
1194  main_type = NO_MAIN;
1195 }
1196 
1197 
1198 // void destruct_refinement(void) method
1199 
1201 
1203 {
1204  delete Newton_method_pointer;
1205 
1206  Newton_method_pointer = NULL;
1207 
1208  refinement_type = NO_REFINEMENT;
1209 }
1210 
1211 
1212 // Results perform_training(void) method
1213 
1218 
1220 {
1221  #ifndef NDEBUG
1222 
1224 
1226 
1227  #endif
1228 
1229  Results training_strategy_results;
1230 
1231  // Initialization
1232 
1233  switch(initialization_type)
1234  {
1235  case NO_INITIALIZATION:
1236  {
1237  // do nothing
1238  }
1239  break;
1240 
1241  case RANDOM_SEARCH:
1242  {
1243  training_strategy_results.random_search_results_pointer
1245  }
1246  break;
1247 
1248  case EVOLUTIONARY_ALGORITHM:
1249  {
1250  training_strategy_results.evolutionary_algorithm_results_pointer
1252  }
1253  break;
1254 
1255  case USER_INITIALIZATION:
1256  {
1257  // do nothing
1258  }
1259  break;
1260 
1261  default:
1262  {
1263  std::ostringstream buffer;
1264 
1265  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1266  << "Results perform_training(void) method.\n"
1267  << "Unknown initialization type.\n";
1268 
1269  throw std::logic_error(buffer.str());
1270  }
1271  break;
1272  }
1273 
1274  // Main
1275 
1276  switch(main_type)
1277  {
1278  case NO_MAIN:
1279  {
1280  // do nothing
1281  }
1282  break;
1283 
1284  case GRADIENT_DESCENT:
1285  {
1286  training_strategy_results.gradient_descent_results_pointer
1288 
1289  }
1290  break;
1291 
1292  case CONJUGATE_GRADIENT:
1293  {
1294  training_strategy_results.conjugate_gradient_results_pointer
1296  }
1297  break;
1298 
1299  case QUASI_NEWTON_METHOD:
1300  {
1301  training_strategy_results.quasi_Newton_method_results_pointer
1303  }
1304  break;
1305 
1306  case LEVENBERG_MARQUARDT_ALGORITHM:
1307  {
1308  training_strategy_results.Levenberg_Marquardt_algorithm_results_pointer
1310  }
1311  break;
1312 
1313  case USER_MAIN:
1314  {
1315  // do nothing
1316  }
1317  break;
1318 
1319  default:
1320  {
1321  std::ostringstream buffer;
1322 
1323  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1324  << "Results perform_training(void) method.\n"
1325  << "Unknown main type.\n";
1326 
1327  throw std::logic_error(buffer.str());
1328  }
1329  break;
1330  }
1331 
1332  // Refinement
1333 
1334  switch(refinement_type)
1335  {
1336  case NO_REFINEMENT:
1337  {
1338  // do nothing
1339  }
1340  break;
1341 
1342  case NEWTON_METHOD:
1343  {
1344  training_strategy_results.Newton_method_results_pointer
1346  }
1347  break;
1348 
1349  case USER_REFINEMENT:
1350  {
1351  // do nothing
1352  }
1353  break;
1354 
1355  default:
1356  {
1357  std::ostringstream buffer;
1358 
1359  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1360  << "Results perform_training(void) method.\n"
1361  << "Unknown refinement type.\n";
1362 
1363  throw std::logic_error(buffer.str());
1364  }
1365  break;
1366  }
1367 
1368  return(training_strategy_results);
1369 }
1370 
1371 
1372 // std::string to_string(void) const method
1373 
1375 
1376 std::string TrainingStrategy::to_string(void) const
1377 {
1378  std::ostringstream buffer;
1379 
1380  buffer << "Training strategy\n";
1381 
1382  // Initialization
1383 
1384  buffer << "Initialization type: " << write_initialization_type() << "\n";
1385 
1386  switch(initialization_type)
1387  {
1388  case NO_INITIALIZATION:
1389  {
1390  // do nothing
1391  }
1392  break;
1393 
1394  case RANDOM_SEARCH:
1395  {
1396  buffer << random_search_pointer->to_string();
1397 
1398  }
1399  break;
1400 
1401  case EVOLUTIONARY_ALGORITHM:
1402  {
1404  }
1405  break;
1406 
1407  case USER_INITIALIZATION:
1408  {
1409  // do nothing
1410  }
1411  break;
1412 
1413  default:
1414  {
1415  std::ostringstream buffer;
1416 
1417  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1418  << "std::string to_string(void) const method.\n"
1419  << "Unknown initialization type.\n";
1420 
1421  throw std::logic_error(buffer.str());
1422  }
1423  break;
1424  }
1425 
1426  // Main
1427 
1428  buffer << "Main type: " << write_main_type() << "\n";
1429 
1430  switch(main_type)
1431  {
1432  case NO_MAIN:
1433  {
1434  // do nothing
1435  }
1436  break;
1437 
1438  case GRADIENT_DESCENT:
1439  {
1440  buffer << gradient_descent_pointer->to_string();
1441 
1442  }
1443  break;
1444 
1445  case CONJUGATE_GRADIENT:
1446  {
1448  }
1449  break;
1450 
1451  case QUASI_NEWTON_METHOD:
1452  {
1454  }
1455  break;
1456 
1457  case LEVENBERG_MARQUARDT_ALGORITHM:
1458  {
1460  }
1461  break;
1462 
1463  case USER_MAIN:
1464  {
1465  // do nothing
1466  }
1467  break;
1468 
1469  default:
1470  {
1471  std::ostringstream buffer;
1472 
1473  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1474  << "std::string to_string(void) const method.\n"
1475  << "Unknown main type.\n";
1476 
1477  throw std::logic_error(buffer.str());
1478  }
1479  break;
1480  }
1481 
1482  // Refinement
1483 
1484  buffer << "Refinement type: " << write_refinement_type() << "\n";
1485 
1486  switch(refinement_type)
1487  {
1488  case NO_REFINEMENT:
1489  {
1490  // do nothing
1491  }
1492  break;
1493 
1494  case NEWTON_METHOD:
1495  {
1496  buffer << Newton_method_pointer->to_string();
1497  }
1498  break;
1499 
1500  case USER_REFINEMENT:
1501  {
1502  // do nothing
1503  }
1504  break;
1505 
1506  default:
1507  {
1508  std::ostringstream buffer;
1509 
1510  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1511  << "std::string to_string(void) const method.\n"
1512  << "Unknown refinement type.\n";
1513 
1514  throw std::logic_error(buffer.str());
1515  }
1516  break;
1517  }
1518 
1519  return(buffer.str());
1520 }
1521 
1522 
1523 // void print(void) const method
1524 
1526 
1527 void TrainingStrategy::print(void) const
1528 {
1529  std::cout << to_string();
1530 }
1531 
1532 
1533 // tinyxml2::XMLDocument* to_XML(void) const method
1534 
1537 
1538 tinyxml2::XMLDocument* TrainingStrategy::to_XML(void) const
1539 {
1540  std::ostringstream buffer;
1541 
1542  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
1543 
1544  // Training strategy
1545 
1546  tinyxml2::XMLElement* training_strategy_element = document->NewElement("TrainingStrategy");
1547 
1548  document->InsertFirstChild(training_strategy_element);
1549 
1550  tinyxml2::XMLElement* element = NULL;
1551  tinyxml2::XMLText* text = NULL;
1552 
1553  // Initialization
1554 
1555  switch(initialization_type)
1556  {
1557  case NO_INITIALIZATION:
1558  {
1559  tinyxml2::XMLElement* initialization_element = document->NewElement("Initialization");
1560  training_strategy_element->LinkEndChild(initialization_element);
1561 
1562  initialization_element->SetAttribute("Type", "NO_INITIALIZATION");
1563  }
1564  break;
1565 
1566  case RANDOM_SEARCH:
1567  {
1568  tinyxml2::XMLElement* initialization_element = document->NewElement("Initialization");
1569  training_strategy_element->LinkEndChild(initialization_element);
1570 
1571  initialization_element->SetAttribute("Type", "RANDOM_SEARCH");
1572 
1573  const tinyxml2::XMLDocument* random_search_document = random_search_pointer->to_XML();
1574 
1575  const tinyxml2::XMLElement* random_search_element = random_search_document->FirstChildElement("RandomSearch");
1576 
1577  DeepClone(initialization_element, random_search_element, document, NULL);
1578 
1579  delete random_search_document;
1580  }
1581  break;
1582 
1583  case EVOLUTIONARY_ALGORITHM:
1584  {
1585  tinyxml2::XMLElement* initialization_element = document->NewElement("Initialization");
1586  training_strategy_element->LinkEndChild(initialization_element);
1587 
1588  initialization_element->SetAttribute("Type", "EVOLUTIONARY_ALGORITHM");
1589 
1590  const tinyxml2::XMLDocument* evolutionary_algorithm_document = evolutionary_algorithm_pointer->to_XML();
1591 
1592  const tinyxml2::XMLElement* evolutionary_algorithm_element = evolutionary_algorithm_document->FirstChildElement("EvolutionaryAlgorithm");
1593 
1594  DeepClone(initialization_element, evolutionary_algorithm_element, document, NULL);
1595 
1596  delete evolutionary_algorithm_document;
1597  }
1598  break;
1599 
1600  case USER_INITIALIZATION:
1601  {
1602  // do nothing
1603  }
1604  break;
1605 
1606  default:
1607  {
1608  std::ostringstream buffer;
1609 
1610  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1611  << "tinyxml2::XMLDocument* to_XML(void) const method.\n"
1612  << "Unknown initialization type.\n";
1613 
1614  throw std::logic_error(buffer.str());
1615  }
1616  break;
1617  }
1618 
1619  // Main
1620 
1621  switch(main_type)
1622  {
1623  case NO_MAIN:
1624  {
1625  tinyxml2::XMLElement* main_element = document->NewElement("Main");
1626  training_strategy_element->LinkEndChild(main_element);
1627 
1628  main_element->SetAttribute("Type", "NO_MAIN");
1629  }
1630  break;
1631 
1632  case GRADIENT_DESCENT:
1633  {
1634  tinyxml2::XMLElement* main_element = document->NewElement("Main");
1635  training_strategy_element->LinkEndChild(main_element);
1636 
1637  main_element->SetAttribute("Type", "GRADIENT_DESCENT");
1638 
1639  const tinyxml2::XMLDocument* gradient_descent_document = gradient_descent_pointer->to_XML();
1640 
1641  const tinyxml2::XMLElement* gradient_descent_element = gradient_descent_document->FirstChildElement("GradientDescent");
1642 
1643  DeepClone(main_element, gradient_descent_element, document, NULL);
1644 
1645  delete gradient_descent_document;
1646  }
1647  break;
1648 
1649  case CONJUGATE_GRADIENT:
1650  {
1651  tinyxml2::XMLElement* main_element = document->NewElement("Main");
1652  training_strategy_element->LinkEndChild(main_element);
1653 
1654  main_element->SetAttribute("Type", "CONJUGATE_GRADIENT");
1655 
1656  const tinyxml2::XMLDocument* conjugate_gradient_document = conjugate_gradient_pointer->to_XML();
1657 
1658  const tinyxml2::XMLElement* conjugate_gradient_element = conjugate_gradient_document->FirstChildElement("ConjugateGradient");
1659 
1660  DeepClone(main_element, conjugate_gradient_element, document, NULL);
1661 
1662  delete conjugate_gradient_document;
1663  }
1664  break;
1665 
1666  case QUASI_NEWTON_METHOD:
1667  {
1668  tinyxml2::XMLElement* main_element = document->NewElement("Main");
1669  training_strategy_element->LinkEndChild(main_element);
1670 
1671  main_element->SetAttribute("Type", "QUASI_NEWTON_METHOD");
1672 
1673  const tinyxml2::XMLDocument* quasi_Newton_method_document = quasi_Newton_method_pointer->to_XML();
1674 
1675  const tinyxml2::XMLElement* quasi_Newton_method_element = quasi_Newton_method_document->FirstChildElement("QuasiNewtonMethod");
1676 
1677  DeepClone(main_element, quasi_Newton_method_element, document, NULL);
1678 
1679  delete quasi_Newton_method_document;
1680  }
1681  break;
1682 
1683  case LEVENBERG_MARQUARDT_ALGORITHM:
1684  {
1685  tinyxml2::XMLElement* main_element = document->NewElement("Main");
1686  training_strategy_element->LinkEndChild(main_element);
1687 
1688  main_element->SetAttribute("Type", "LEVENBERG_MARQUARDT_ALGORITHM");
1689 
1690  const tinyxml2::XMLDocument* Levenberg_Marquardt_algorithm_document = Levenberg_Marquardt_algorithm_pointer->to_XML();
1691 
1692  const tinyxml2::XMLElement* Levenberg_Marquardt_algorithm_element = Levenberg_Marquardt_algorithm_document->FirstChildElement("LevenbergMarquardtAlgorithm");
1693 
1694  DeepClone(main_element, Levenberg_Marquardt_algorithm_element, document, NULL);
1695 
1696  delete Levenberg_Marquardt_algorithm_document;
1697  }
1698  break;
1699 
1700  case USER_MAIN:
1701  {
1702  // do nothing
1703  }
1704  break;
1705 
1706  default:
1707  {
1708  std::ostringstream buffer;
1709 
1710  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1711  << "tinyxml2::XMLDocument* to_XML(void) const method.\n"
1712  << "Unknown main type.\n";
1713 
1714  throw std::logic_error(buffer.str());
1715  }
1716  break;
1717  }
1718 
1719  switch(refinement_type)
1720  {
1721  case NO_REFINEMENT:
1722  {
1723  // do nothing
1724  }
1725  break;
1726 
1727  case NEWTON_METHOD:
1728  {
1729  tinyxml2::XMLElement* refinement_element = document->NewElement("Refinement");
1730  training_strategy_element->LinkEndChild(refinement_element);
1731 
1732  refinement_element->SetAttribute("Type", "NEWTON_METHOD");
1733 
1734  const tinyxml2::XMLDocument* Newton_method_document = Newton_method_pointer->to_XML();
1735 
1736  const tinyxml2::XMLElement* Newton_method_element = Newton_method_document->FirstChildElement("NewtonMethod");
1737 
1738  DeepClone(refinement_element, Newton_method_element, document, NULL);
1739 
1740  delete Newton_method_document;
1741  }
1742  break;
1743 
1744  case USER_REFINEMENT:
1745  {
1746  // do nothing
1747  }
1748  break;
1749 
1750  default:
1751  {
1752  std::ostringstream buffer;
1753 
1754  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1755  << "tinyxml2::XMLDocument* to_XML(void) const method.\n"
1756  << "Unknown refinement type.\n";
1757 
1758  throw std::logic_error(buffer.str());
1759  }
1760  break;
1761  }
1762 
1763  // Display
1764  {
1765  element = document->NewElement("Display");
1766  training_strategy_element->LinkEndChild(element);
1767 
1768  buffer.str("");
1769  buffer << display;
1770 
1771  text = document->NewText(buffer.str().c_str());
1772  element->LinkEndChild(text);
1773  }
1774 
1775  return(document);
1776 }
1777 
1778 
1779 // void from_XML(const tinyxml2::XMLDocument&) method
1780 
1783 
1784 void TrainingStrategy::from_XML(const tinyxml2::XMLDocument& document)
1785 {
1786  const tinyxml2::XMLElement* root_element = document.FirstChildElement("TrainingStrategy");
1787 
1788  if(!root_element)
1789  {
1790  std::ostringstream buffer;
1791 
1792  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1793  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
1794  << "Training strategy element is NULL.\n";
1795 
1796  throw std::logic_error(buffer.str());
1797  }
1798 
1799  // Initialization
1800  {
1801  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Initialization");
1802 
1803  if(element)
1804  {
1805  const std::string new_initialization_type = element->Attribute("Type");
1806 
1807  set_initialization_type(new_initialization_type);
1808 
1809  switch(initialization_type)
1810  {
1811  case NO_INITIALIZATION:
1812  {
1813  // do nothing
1814  }
1815  break;
1816 
1817  case RANDOM_SEARCH:
1818  {
1819  tinyxml2::XMLDocument new_document;
1820 
1821  tinyxml2::XMLElement* element_clone = new_document.NewElement("RandomSearch");
1822  new_document.InsertFirstChild(element_clone);
1823 
1824  DeepClone(element_clone, element, &new_document, NULL);
1825 
1826  random_search_pointer->from_XML(new_document);
1827  }
1828  break;
1829 
1830  case EVOLUTIONARY_ALGORITHM:
1831  {
1832  tinyxml2::XMLDocument new_document;
1833 
1834  tinyxml2::XMLElement* element_clone = new_document.NewElement("EvolutionaryAlgorithm");
1835  new_document.InsertFirstChild(element_clone);
1836 
1837  DeepClone(element_clone, element, &new_document, NULL);
1838 
1840  }
1841  break;
1842 
1843  case USER_INITIALIZATION:
1844  {
1845  // do nothing
1846  }
1847  break;
1848 
1849  default:
1850  {
1851  std::ostringstream buffer;
1852 
1853  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1854  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
1855  << "Unknown initialization type.\n";
1856 
1857  throw std::logic_error(buffer.str());
1858  }
1859  break;
1860  }// end switch
1861  }
1862  }
1863 
1864  // Main
1865  {
1866  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Main");
1867 
1868  if(element)
1869  {
1870  const std::string new_main_type = element->Attribute("Type");
1871 
1872  set_main_type(new_main_type);
1873 
1874  switch(main_type)
1875  {
1876  case NO_MAIN:
1877  {
1878  // do nothing
1879  }
1880  break;
1881 
1882  case GRADIENT_DESCENT:
1883  {
1884  tinyxml2::XMLDocument new_document;
1885 
1886  tinyxml2::XMLElement* element_clone = new_document.NewElement("GradientDescent");
1887  new_document.InsertFirstChild(element_clone);
1888 
1889  DeepClone(element_clone, element, &new_document, NULL);
1890 
1891  gradient_descent_pointer->from_XML(new_document);
1892  }
1893  break;
1894 
1895  case CONJUGATE_GRADIENT:
1896  {
1897  tinyxml2::XMLDocument new_document;
1898 
1899  tinyxml2::XMLElement* element_clone = new_document.NewElement("ConjugateGradient");
1900  new_document.InsertFirstChild(element_clone);
1901 
1902  DeepClone(element_clone, element, &new_document, NULL);
1903 
1904  conjugate_gradient_pointer->from_XML(new_document);
1905  }
1906  break;
1907 
1908  case QUASI_NEWTON_METHOD:
1909  {
1910  tinyxml2::XMLDocument new_document;
1911 
1912  tinyxml2::XMLElement* element_clone = new_document.NewElement("QuasiNewtonMethod");
1913  new_document.InsertFirstChild(element_clone);
1914 
1915  DeepClone(element_clone, element, &new_document, NULL);
1916 
1917  quasi_Newton_method_pointer->from_XML(new_document);
1918  }
1919  break;
1920 
1921  case LEVENBERG_MARQUARDT_ALGORITHM:
1922  {
1923  tinyxml2::XMLDocument new_document;
1924 
1925  tinyxml2::XMLElement* element_clone = new_document.NewElement("LevenbergMarquardtAlgorithm");
1926  new_document.InsertFirstChild(element_clone);
1927 
1928  DeepClone(element_clone, element, &new_document, NULL);
1929 
1931  }
1932  break;
1933 
1934  case USER_MAIN:
1935  {
1936  // do nothing
1937  }
1938  break;
1939 
1940  default:
1941  {
1942  std::ostringstream buffer;
1943 
1944  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1945  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
1946  << "Unknown main type.\n";
1947 
1948  throw std::logic_error(buffer.str());
1949  }
1950  break;
1951  }
1952  }
1953  }
1954 
1955  // Refinement
1956  {
1957  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Refinement");
1958 
1959  if(element)
1960  {
1961  const std::string new_refinement_type = element->Attribute("Type");
1962 
1963  set_refinement_type(new_refinement_type);
1964 
1965  switch(refinement_type)
1966  {
1967  case NO_REFINEMENT:
1968  {
1969  // do nothing
1970  }
1971  break;
1972 
1973  case NEWTON_METHOD:
1974  {
1975  tinyxml2::XMLDocument new_document;
1976 
1977  tinyxml2::XMLElement* element_clone = new_document.NewElement("NewtonMethod");
1978  new_document.InsertFirstChild(element_clone);
1979 
1980  DeepClone(element_clone, element, &new_document, NULL);
1981 
1982  Newton_method_pointer->from_XML(new_document);
1983  }
1984  break;
1985 
1986  case USER_REFINEMENT:
1987  {
1988  // do nothing
1989  }
1990  break;
1991 
1992  default:
1993  {
1994  std::ostringstream buffer;
1995 
1996  buffer << "OpenNN Exception: TrainingStrategy class.\n"
1997  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
1998  << "Unknown refinement type.\n";
1999 
2000  throw std::logic_error(buffer.str());
2001  }
2002  break;
2003  }
2004  }
2005  }
2006 
2007  // Display
2008  {
2009  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Display");
2010 
2011  if(element)
2012  {
2013  const std::string new_display = element->GetText();
2014 
2015  try
2016  {
2017  set_display(new_display != "0");
2018  }
2019  catch(const std::logic_error& e)
2020  {
2021  std::cout << e.what() << std::endl;
2022  }
2023  }
2024  }
2025 
2026 }
2027 
2028 
2029 // void save(const std::string&) const method
2030 
2033 
2034 void TrainingStrategy::save(const std::string& file_name) const
2035 {
2036  tinyxml2::XMLDocument* document = to_XML();
2037 
2038  document->SaveFile(file_name.c_str());
2039 
2040  delete document;
2041 }
2042 
2043 
2044 // void load(const std::string&) method
2045 
2049 
2050 void TrainingStrategy::load(const std::string& file_name)
2051 {
2052  set_default();
2053 
2054  tinyxml2::XMLDocument document;
2055 
2056  if (document.LoadFile(file_name.c_str()))
2057  {
2058  std::ostringstream buffer;
2059 
2060  buffer << "OpenNN Exception: TrainingStrategy class.\n"
2061  << "void load(const std::string&) method.\n"
2062  << "Cannot load XML file " << file_name << ".\n";
2063 
2064  throw std::logic_error(buffer.str());
2065  }
2066 
2067  from_XML(document);
2068 }
2069 
2070 
2071 // Results constructor
2072 
2074 {
2076 
2078 
2080 
2082 
2084 
2086 
2088 }
2089 
2090 
2091 // Results destructor
2092 
2094 {
2095 // delete random_search_results_pointer;
2096 
2097 // delete evolutionary_algorithm_results_pointer;
2098 
2099 // delete gradient_descent_results_pointer;
2100 
2101 // delete conjugate_gradient_results_pointer;
2102 
2103 // delete quasi_Newton_method_results_pointer;
2104 
2105 // delete Levenberg_Marquardt_algorithm_results_pointer;
2106 
2107 // delete Newton_method_results_pointer;
2108 
2109 }
2110 
2111 
2112 // void Results::save(const std::string&) const method
2113 
2116 
2117 void TrainingStrategy::Results::save(const std::string& file_name) const
2118 {
2119  std::ofstream file(file_name.c_str());
2120 
2121  if(random_search_results_pointer)
2122  {
2123  file << random_search_results_pointer->to_string();
2124  }
2125 
2126  if(evolutionary_algorithm_results_pointer)
2127  {
2128  file << evolutionary_algorithm_results_pointer->to_string();
2129  }
2130 
2131  if(gradient_descent_results_pointer)
2132  {
2133  file << gradient_descent_results_pointer->to_string();
2134  }
2135 
2136  if(conjugate_gradient_results_pointer)
2137  {
2138  file << conjugate_gradient_results_pointer->to_string();
2139  }
2140 
2141  if(quasi_Newton_method_results_pointer)
2142  {
2143  file << quasi_Newton_method_results_pointer->to_string();
2144  }
2145 
2146  if(Levenberg_Marquardt_algorithm_results_pointer)
2147  {
2148  file << Levenberg_Marquardt_algorithm_results_pointer->to_string();
2149  }
2150 
2151  if(Newton_method_results_pointer)
2152  {
2153  file << Newton_method_results_pointer->to_string();
2154  }
2155 
2156  file.close();
2157 }
2158 
2159 }
2160 
2161 
2162 // OpenNN: Open Neural Networks Library.
2163 // Copyright (c) 2005-2015 Roberto Lopez.
2164 //
2165 // This library is free software; you can redistribute it and/or
2166 // modify it under the terms of the GNU Lesser General Public
2167 // License as published by the Free Software Foundation; either
2168 // version 2.1 of the License, or any later version.
2169 //
2170 // This library is distributed in the hope that it will be useful,
2171 // but WITHOUT ANY WARRANTY; without even the implied warranty of
2172 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2173 // Lesser General Public License for more details.
2174 
2175 // You should have received a copy of the GNU Lesser General Public
2176 // License along with this library; if not, write to the Free Software
2177 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
std::string write_main_type_text(void) const
Returns a string with the main type in text format.
void destruct_main(void)
This method deletes the main training algorithm object which composes this training strategy object...
GradientDescent::GradientDescentResults * gradient_descent_results_pointer
Pointer to a structure with the results from the gradient descent training algorithm.
std::string write_refinement_type_text(void) const
Returns a string with the refinement type in text format.
PerformanceFunctional * get_performance_functional_pointer(void) const
Returns a pointer to the performance functional object to which the training strategy is associated...
NewtonMethodResults * perform_training(void)
void set_display(const bool &)
tinyxml2::XMLDocument * to_XML(void) const
void from_XML(const tinyxml2::XMLDocument &)
void load(const std::string &)
virtual ~Results(void)
Destructor.
QuasiNewtonMethod * quasi_Newton_method_pointer
Pointer to a quasi-Newton method object to be used as a main training algorithm.
EvolutionaryAlgorithm * evolutionary_algorithm_pointer
Pointer to a evolutionary training object to be used for initialization in the training strategy...
tinyxml2::XMLDocument * to_XML(void) const
void from_XML(const tinyxml2::XMLDocument &)
void print(void) const
Prints to the screen the string representation of the training strategy object.
InitializationType initialization_type
Type of initialization training algorithm.
bool has_performance_functional(void) const
NewtonMethod * get_Newton_method_pointer(void) const
RandomSearch * get_random_search_pointer(void) const
void set_refinement_type(const RefinementType &)
const RefinementType & get_refinement_type(void) const
Returns the type of the refinement training algorithm composing this training strategy object...
void check_training_algorithms(void) const
QuasiNewtonMethod * get_quasi_Newton_method_pointer(void) const
LevenbergMarquardtAlgorithm::LevenbergMarquardtAlgorithmResults * Levenberg_Marquardt_algorithm_results_pointer
Pointer to a structure with the results from the Levenberg-Marquardt training algorithm.
EvolutionaryAlgorithmResults * perform_training(void)
NewtonMethod::NewtonMethodResults * Newton_method_results_pointer
Pointer to a structure with results from the Newton method training algorithm.
void from_XML(const tinyxml2::XMLDocument &)
void save(const std::string &) const
void set_performance_functional_pointer(PerformanceFunctional *)
ConjugateGradient::ConjugateGradientResults * conjugate_gradient_results_pointer
Pointer to a structure with the results from the conjugate gradient training algorithm.
NewtonMethod * Newton_method_pointer
Pointer to a Newton method object to be used for refinement in the training strategy.
void from_XML(const tinyxml2::XMLDocument &)
GradientDescentResults * perform_training(void)
std::string to_string(void) const
Returns a string representation of the training strategy.
std::string write_initialization_type(void) const
Returns a string with the type of the initialization training algorithm composing this training strat...
void set_performance_functional_pointer(PerformanceFunctional *)
void from_XML(const tinyxml2::XMLDocument &)
tinyxml2::XMLDocument * to_XML(void) const
GradientDescent * gradient_descent_pointer
Pointer to a gradient descent object to be used as a main training algorithm.
std::string write_refinement_type(void) const
Returns a string with the type of the refinement training algorithm composing this training strategy ...
ConjugateGradientResults * perform_training(void)
PerformanceFunctional * performance_functional_pointer
Pointer to an external performance functional object.
LevenbergMarquardtAlgorithm * get_Levenberg_Marquardt_algorithm_pointer(void) const
EvolutionaryAlgorithm::EvolutionaryAlgorithmResults * evolutionary_algorithm_results_pointer
Pointer to a structure with the results from the evolutionary training algorithm. ...
void save(const std::string &) const
void set_performance_functional_pointer(PerformanceFunctional *)
ConjugateGradient * get_conjugate_gradient_pointer(void) const
void set_initialization_type(const InitializationType &)
QuasiNewtonMethodResults * perform_training(void)
RefinementType refinement_type
Type of refinement training algorithm.
bool display
Display messages to screen.
QuasiNewtonMethod::QuasiNewtonMethodResults * quasi_Newton_method_results_pointer
Pointer to a structure with the results from the quasi-Newton method training algorithm.
tinyxml2::XMLDocument * to_XML(void) const
std::string write_initialization_type_text(void) const
Returns a string with the initialization type in text format.
void from_XML(const tinyxml2::XMLDocument &)
RandomSearchResults * perform_training(void)
std::string to_string(void) const
Returns a default string representation of a training algorithm.
void set_main_type(const MainType &)
void from_XML(const tinyxml2::XMLDocument &)
void destruct_initialization(void)
This method deletes the initialization training algorithm object which composes this training strateg...
const MainType & get_main_type(void) const
Returns the type of the main training algorithm composing this training strategy object.
void from_XML(const tinyxml2::XMLDocument &)
virtual void set_default(void)
void destruct_refinement(void)
This method deletes the refinement training algorithm object which composes this training strategy ob...
void set_performance_functional_pointer(PerformanceFunctional *)
Results(void)
Default constructor.
LevenbergMarquardtAlgorithmResults * perform_training(void)
virtual std::string to_string(void) const
Returns a default string representation of a training algorithm.
void set_performance_functional_pointer(PerformanceFunctional *)
MainType
Enumeration of all the available types of training algorithms.
void check_performance_functional(void) const
Throws an exception if the training strategy has not a performance functional associated.
ConjugateGradient * conjugate_gradient_pointer
Pointer to a conjugate gradient object to be used as a main training algorithm.
tinyxml2::XMLDocument * to_XML(void) const
GradientDescent * get_gradient_descent_pointer(void) const
RefinementType
Enumeration of all the available types of training algorithms.
RandomSearch * random_search_pointer
Pointer to a random search object to be used for initialization in the training strategy.
virtual void set_performance_functional_pointer(PerformanceFunctional *)
const bool & get_display(void) const
tinyxml2::XMLDocument * to_XML(void) const
tinyxml2::XMLDocument * to_XML(void) const
EvolutionaryAlgorithm * get_evolutionary_algorithm_pointer(void) const
const InitializationType & get_initialization_type(void) const
Returns the type of the initialization training algorithm composing this training strategy object...
LevenbergMarquardtAlgorithm * Levenberg_Marquardt_algorithm_pointer
Pointer to a Levenberg-Marquardt algorithm object to be used as a main training algorithm.
InitializationType
Enumeration of all the available types of training algorithms.
MainType main_type
Type of main training algorithm.
RandomSearch::RandomSearchResults * random_search_results_pointer
Pointer to a structure with the results from the random search training algorithm.
tinyxml2::XMLDocument * to_XML(void) const
std::string write_main_type(void) const
Returns a string with the type of the main training algorithm composing this training strategy object...