OpenNN  2.2
Open Neural Networks Library
evolutionary_algorithm.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* E V O L U T I O N A R Y A L G O R I T H M 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 "evolutionary_algorithm.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
26 
28 {
29  set_default();
30 }
31 
32 
33 // PERFORMANCE FUNCTIONAL CONSTRUCTOR
34 
39 
41 : TrainingAlgorithm(new_performance_functional_pointer)
42 {
43  set_default();
44 }
45 
46 
47 // XML CONSTRUCTOR
48 
53 
54 EvolutionaryAlgorithm::EvolutionaryAlgorithm(const tinyxml2::XMLDocument& evolutionary_algorithm_document)
55  : TrainingAlgorithm(evolutionary_algorithm_document)
56 {
57 }
58 
59 
60 // DESTRUCTOR
61 
63 
65 {
66 
67 }
68 
69 
70 // METHODS
71 
72 // const FitnessAssignmentMethod& get_fitness_assignment_method(void) const method
73 
75 
77 {
79 }
80 
81 
82 // std::string write_fitness_assignment_method(void) const method
83 
85 
87 {
89  {
90  case LinearRanking:
91  {
92  return("LinearRanking");
93  }
94  break;
95 
96  default:
97  {
98  std::ostringstream buffer;
99 
100  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
101  << "std::string write_fitness_assignment_method(void) const method.\n"
102  << "Unknown fitness assignment method.\n";
103 
104  throw std::logic_error(buffer.str());
105  }
106  break;
107  }
108 }
109 
110 
111 // const SelectionMethod& get_selection_method(void) const method
112 
114 
116 {
117  return(selection_method);
118 }
119 
120 
121 // std::string write_selection_method(void) const method
122 
124 
126 {
127  switch(selection_method)
128  {
129  case RouletteWheel:
130  {
131  return("RouletteWheel");
132  }
133  break;
134 
135  default:
136  {
137  std::ostringstream buffer;
138 
139  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
140  << "std::string write_selection_method(void) const method.\n"
141  << "Unknown selection method.\n";
142 
143  throw std::logic_error(buffer.str());
144  }
145  break;
146  }
147 }
148 
149 
150 // const RecombinationMethod& get_recombination_method(void) const method
151 
153 
155 {
156  return(recombination_method);
157 }
158 
159 
160 // std::string write_recombination_method(void) const method
161 
163 
165 {
166  switch(recombination_method)
167  {
168  case Line:
169  {
170  return("Line");
171  }
172  break;
173 
174  case Intermediate:
175  {
176  return("Intermediate");
177  }
178  break;
179 
180  default:
181  {
182  std::ostringstream buffer;
183 
184  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
185  << "std::string write_recombination_method(void) const method.\n"
186  << "Unknown recombination method.\n";
187 
188  throw std::logic_error(buffer.str());
189  }
190  break;
191  }
192 }
193 
194 
195 // const MutationMethod get_mutation_method(void) const method
196 
198 
200 {
201  return(mutation_method);
202 }
203 
204 
205 // std::string write_mutation_method(void) const method
206 
208 
210 {
211  switch(mutation_method)
212  {
213  case Normal:
214  {
215  return("Normal");
216  }
217  break;
218 
219  case Uniform:
220  {
221  return("Uniform");
222  }
223  break;
224 
225  default:
226  {
227  std::ostringstream buffer;
228 
229  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
230  << "std::string get_mutation_method_name(void) const method.\n"
231  << "Unknown mutation method.\n";
232 
233  throw std::logic_error(buffer.str());
234  }
235  break;
236  }
237 }
238 
239 
240 // size_t get_population_size(void) const method
241 
243 
245 {
246  return(population.get_rows_number());
247 }
248 
249 
250 // const Matrix<double>& get_population(void) const method
251 
253 
255 {
256  return(population);
257 }
258 
259 
260 // const Vector<double>& get_performance(void) const method
261 
263 
265 {
266  return(performance);
267 }
268 
269 
270 // const Vector<double>& get_fitness(void) const method
271 
273 
275 {
276  return(fitness);
277 }
278 
279 
280 // const Vector<bool>& get_selection(void) const method
281 
283 
285 {
286  return(selection);
287 }
288 
289 
290 // const double& get_warning_parameters_norm(void) const method
291 
294 
296 {
297  return(warning_parameters_norm);
298 }
299 
300 
301 // const double& get_error_parameters_norm(void) const method
302 
305 
307 {
308  return(error_parameters_norm);
309 }
310 
311 
312 // const double& get_best_performance_goal(void) const method
313 
316 
318 {
319  return(best_performance_goal);
320 }
321 
322 
323 // const size_t& get_maximum_generalization_performance_decreases(void) const method
324 
326 
328 {
330 }
331 
332 
333 // const double& get_maximum_time(void) const method
334 
336 
338 {
339  return(maximum_time);
340 }
341 
342 
343 // const bool& get_reserve_elapsed_time_history(void) const method
344 
346 
348 {
350 }
351 
352 
353 // const bool& get_reserve_generalization_performance_history(void) const method
354 
356 
358 {
360 }
361 
362 
363 // const bool& get_reserve_population_history(void) const method
364 
366 
368 {
370 }
371 
372 
373 // const bool& get_reserve_best_individual_history(void) const method
374 
376 
378 {
380 }
381 
382 
383 // const bool& get_reserve_mean_norm_history(void) const method
384 
386 
388 {
390 }
391 
392 
393 // const bool& get_reserve_standard_deviation_norm_history(void) const method
394 
397 
399 {
401 }
402 
403 
404 // const bool& get_reserve_best_norm_history(void) const method
405 
408 
410 {
412 }
413 
414 
415 // const bool& get_reserve_mean_performance_history(void) const method
416 
418 
420 {
422 }
423 
424 
425 // const bool& get_reserve_standard_deviation_performance_history(void) const method
426 
429 
431 {
433 }
434 
435 
436 // const bool& get_reserve_best_performance_history(void) const method
437 
439 
441 {
443 }
444 
445 
446 // void set(void) method
447 
450 
452 {
454 
455  set_default();
456 }
457 
458 
459 // void set(PerformanceFunctional*) method
460 
463 
464 void EvolutionaryAlgorithm::set(PerformanceFunctional* new_performance_functional_pointer)
465 {
466  performance_functional_pointer = new_performance_functional_pointer;
467 
468  set_default();
469 }
470 
471 
472 // void set_default(void) method
473 
514 
516 {
517  // Fitness assignment method
518 
519  fitness_assignment_method = LinearRanking;
520 
521  // Selection method
522 
523  selection_method = RouletteWheel;
524 
525  // Recombination method
526 
527  recombination_method = Intermediate;
528 
529  // Mutation method
530 
531  mutation_method = Normal;
532 
533  // Training parameters
534 
535  elitism_size = 2;
536 
537  selective_pressure = 1.5;
538 
539  recombination_size = 0.25;
540 
541  mutation_rate = 0.1;
542 
543  mutation_range = 0.1;
544 
545  // Stopping criteria
546 
547  mean_performance_goal = -1.0e99;
549  best_performance_goal = -1.0e99;
550 
551  maximum_time = 1.0e6;
552 
554 
555  // Training history
556 
558 
560 
564 
568 
570 
571  // User stuff
572 
573  display_period = 10;
574 }
575 
576 
577 // void set_population_size(size_t) method
578 
584 
585 void EvolutionaryAlgorithm::set_population_size(const size_t& new_population_size)
586 {
587  if(new_population_size == 0)
588  {
589  population.set();
590 
591  performance.set();
592 
593  fitness.set();
594 
595  selection.set();
596  }
597  else
598  {
599  // Control sentence (if debug)
600 
601  #ifndef NDEBUG
602 
603  check();
604 
605  #endif
606 
608 
609  // Control sentence (if debug)
610 
611  #ifndef NDEBUG
612 
613  if(!neural_network_pointer)
614  {
615  std::ostringstream buffer;
616 
617  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
618  << "void set_population_size(size_t) method.\n"
619  << "Neural network pointer is NULL.\n";
620 
621  throw std::logic_error(buffer.str());
622  }
623 
624  #endif
625 
626  const size_t parameters_number = neural_network_pointer->count_parameters_number();
627 
628  if(new_population_size < 4)
629  {
630  std::ostringstream buffer;
631 
632  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
633  << "void set_population_size(size_t) method.\n"
634  << "New population size must be equal or greater than 4.\n";
635 
636  throw std::logic_error(buffer.str());
637  }
638  else if(new_population_size%2 != 0)
639  {
640  std::ostringstream buffer;
641 
642  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
643  << "void set_population_size(size_t) method.\n"
644  << "New population size is not divisible by 2.\n";
645 
646  throw std::logic_error(buffer.str());
647  }
648  else
649  {
650  // Set population matrix
651 
652  population.set(new_population_size, parameters_number);
653 
655 
656  // Set performance vector
657 
658  performance.resize(new_population_size);
659 
660  // Set fitness vector
661 
662  fitness.resize(new_population_size);
663 
664  // Set selection vector
665 
666  selection.resize(new_population_size);
667  }
668  }
669 }
670 
671 
672 // void set_fitness_assignment_method(const std::string&) method
673 
680 
681 void EvolutionaryAlgorithm::set_fitness_assignment_method(const std::string& new_fitness_assignment_method_name)
682 {
683  if(new_fitness_assignment_method_name == "LinearRanking")
684  {
685  fitness_assignment_method = LinearRanking;
686  }
687  else
688  {
689  std::ostringstream buffer;
690 
691  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
692  << "void set_fitness_assignment_method(const std::string&) method.\n"
693  << "Unknown fitness assignment method: " << new_fitness_assignment_method_name << ".\n";
694 
695  throw std::logic_error(buffer.str());
696  }
697 }
698 
699 
700 // void set_selection_method(const std::string&) method
701 
708 
709 void EvolutionaryAlgorithm::set_selection_method(const std::string& new_selection_method_name)
710 {
711  if(new_selection_method_name == "RouletteWheel")
712  {
713  selection_method = RouletteWheel;
714  }
715  else
716  {
717  std::ostringstream buffer;
718 
719  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
720  << "void set_selection_method(const std::string&) method.\n"
721  << "Unknown selection method: " << new_selection_method_name << ".\n";
722 
723  throw std::logic_error(buffer.str());
724  }
725 }
726 
727 
728 // void set_recombination_method(const std::string&) method
729 
737 
738 void EvolutionaryAlgorithm::set_recombination_method(const std::string& new_recombination_method_name)
739 {
740  if(new_recombination_method_name == "Line")
741  {
742  recombination_method = Line;
743  }
744  else if(new_recombination_method_name == "Intermediate")
745  {
746  recombination_method = Intermediate;
747  }
748  else
749  {
750  std::ostringstream buffer;
751 
752  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
753  << "void set_recombination_method(const std::string&) method.\n"
754  << "Unknown recombination method: " << new_recombination_method_name << ".\n";
755 
756  throw std::logic_error(buffer.str());
757  }
758 }
759 
760 
761 // void set_mutation_method(const std::string&) method
762 
770 
771 void EvolutionaryAlgorithm::set_mutation_method(const std::string& new_mutation_method_name)
772 {
773  if(new_mutation_method_name == "Normal")
774  {
775  mutation_method = Normal;
776  }
777  else if(new_mutation_method_name == "Uniform")
778  {
779  mutation_method = Uniform;
780  }
781  else
782  {
783  std::ostringstream buffer;
784 
785  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
786  << "void set_mutation_method(const std::string&) method.\n"
787  << "Unknown mutationg method: " << new_mutation_method_name << ".\n";
788 
789  throw std::logic_error(buffer.str());
790  }
791 }
792 
793 
794 // void set_population(const Matrix<double>&) method
795 
799 
801 {
802  // Control sentence (if debug)
803 
804  #ifndef NDEBUG
805 
806  const size_t population_size = get_population_size();
807 
808  if(population_size == 0 && new_population.empty())
809  {
810  return;
811  }
812 
813  check();
814 
816 
817  const size_t parameters_number = neural_network_pointer->count_parameters_number();
818 
819  if(new_population.get_rows_number() != population_size)
820  {
821  std::ostringstream buffer;
822 
823  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
824  << "void set_population(const Matrix<double>&) method.\n"
825  << "New population size is not equal to population size.\n";
826 
827  throw std::logic_error(buffer.str());
828  }
829  else if(new_population.get_columns_number() != parameters_number)
830  {
831  std::ostringstream buffer;
832 
833  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
834  << "void set_population(const Matrix<double>&) method.\n"
835  << "New number of parameters is not equal to number of parameters.\n";
836 
837  throw std::logic_error(buffer.str());
838  }
839 
840  #endif
841 
842  // Set population
843 
844  population = new_population;
845 }
846 
847 
848 // void set_performance(const Vector<double>&) method
849 
853 
855 {
856  // Control sentence (if debug)
857 
858  #ifndef NDEBUG
859 
860  const size_t population_size = get_population_size();
861 
862  if(new_performance.size() != population_size)
863  {
864  std::ostringstream buffer;
865 
866  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
867  << "void set_performance(const Vector<double>&) method.\n"
868  << "Size is not equal to population size.\n";
869 
870  throw std::logic_error(buffer.str());
871  }
872 
873  #endif
874 
875  // Set performance
876 
877  performance = new_performance;
878 }
879 
880 
881 // void set_fitness(const Vector<double>&) method
882 
886 
888 {
889  // Control sentence (if debug)
890 
891  #ifndef NDEBUG
892 
893  const size_t population_size = get_population_size();
894 
895  if(new_fitness.size() != population_size)
896  {
897  std::ostringstream buffer;
898 
899  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
900  << "void set_fitness(Vector<double>) method.\n"
901  << "Size is not equal to population size.\n";
902 
903  throw std::logic_error(buffer.str());
904  }
905 
906  #endif
907 
908  // Set fitness
909 
910  fitness = new_fitness;
911 }
912 
913 
914 // void set_selection(const Vector<bool>&) method
915 
919 
921 {
922  // Control sentence (if debug)
923 
924  #ifndef NDEBUG
925 
926  const size_t population_size = get_population_size();
927 
928  if(new_selection.size() != population_size)
929  {
930  std::ostringstream buffer;
931 
932  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
933  << "void set_selection(Vector<double>) method.\n"
934  << "Size is not equal to population size.\n";
935 
936  throw std::logic_error(buffer.str());
937  }
938 
939  #endif
940 
941  // Set selection
942 
943  selection = new_selection;
944 }
945 
946 
947 // void set_reserve_population_history(bool) method
948 
952 
953 void EvolutionaryAlgorithm::set_reserve_population_history(const bool& new_reserve_population_history)
954 {
955  reserve_population_history = new_reserve_population_history;
956 }
957 
958 
959 // void set_reserve_best_individual_history(bool) method
960 
964 
965 void EvolutionaryAlgorithm::set_reserve_best_individual_history(const bool& new_reserve_best_individual_history)
966 {
967  reserve_best_individual_history = new_reserve_best_individual_history;
968 }
969 
970 
971 // void set_reserve_mean_norm_history(bool) method
972 
976 
977 void EvolutionaryAlgorithm::set_reserve_mean_norm_history(const bool& new_reserve_mean_norm_history)
978 {
979  reserve_mean_norm_history = new_reserve_mean_norm_history;
980 }
981 
982 
983 // void set_reserve_standard_deviation_norm_history(bool) method
984 
989 
991 set_reserve_standard_deviation_norm_history(const bool& new_reserve_standard_deviation_norm_history)
992 {
993  reserve_standard_deviation_norm_history = new_reserve_standard_deviation_norm_history;
994 }
995 
996 
997 // void set_reserve_best_norm_history(bool) method
998 
1002 
1003 void EvolutionaryAlgorithm::set_reserve_best_norm_history(const bool& new_reserve_best_norm_history)
1004 {
1005  reserve_best_norm_history = new_reserve_best_norm_history;
1006 }
1007 
1008 
1009 // void set_reserve_mean_performance_history(bool) method
1010 
1015 
1016 void EvolutionaryAlgorithm::set_reserve_mean_performance_history(const bool& new_reserve_mean_performance_history)
1017 {
1018  reserve_mean_performance_history = new_reserve_mean_performance_history;
1019 }
1020 
1021 
1022 // void set_reserve_standard_deviation_performance_history(bool) method
1023 
1028 
1030 ::set_reserve_standard_deviation_performance_history(const bool& new_reserve_standard_deviation_performance_history)
1031 {
1032  reserve_standard_deviation_performance_history = new_reserve_standard_deviation_performance_history;
1033 }
1034 
1035 
1036 // void set_reserve_best_performance_history(bool) method
1037 
1042 
1043 void EvolutionaryAlgorithm::set_reserve_best_performance_history(const bool& new_reserve_best_performance_history)
1044 {
1045  reserve_best_performance_history = new_reserve_best_performance_history;
1046 }
1047 
1048 
1049 // void set_reserve_all_training_history(bool) method
1050 
1055 
1056 void EvolutionaryAlgorithm::set_reserve_all_training_history(const bool& new_reserve_all_training_history)
1057 {
1058  // Multilayer perceptron
1059 
1060  reserve_population_history = new_reserve_all_training_history;
1061 
1062  reserve_best_individual_history = new_reserve_all_training_history;
1063 
1064  reserve_mean_norm_history = new_reserve_all_training_history;
1065  reserve_standard_deviation_norm_history = new_reserve_all_training_history;
1066  reserve_best_norm_history = new_reserve_all_training_history;
1067 
1068  // Objective functional
1069 
1070  reserve_mean_performance_history = new_reserve_all_training_history;
1071  reserve_standard_deviation_performance_history = new_reserve_all_training_history;
1072  reserve_best_performance_history = new_reserve_all_training_history;
1073 
1074  // Training algorithm
1075 
1076  reserve_elapsed_time_history = new_reserve_all_training_history;
1077 }
1078 
1079 
1080 // Vector<double> get_individual(const size_t&) const method
1081 
1085 
1087 {
1088  // Control sentence (if debug)
1089 
1090  #ifndef NDEBUG
1091 
1092  const size_t population_size = get_population_size();
1093 
1094  if(i >= population_size)
1095  {
1096  std::ostringstream buffer;
1097 
1098  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1099  << "Vector<double> get_individual(const size_t&) const method.\n"
1100  << "Index must be less than population size.\n";
1101 
1102  throw std::logic_error(buffer.str());
1103  }
1104 
1105  #endif
1106 
1107  // Get individual
1108 
1109  const Vector<double> individual = population.arrange_row(i);
1110 
1111  return(individual);
1112 }
1113 
1114 
1115 // set_individual(const size_t&, Vector<double>) method
1116 
1121 
1122 void EvolutionaryAlgorithm::set_individual(const size_t& i, const Vector<double>& individual)
1123 {
1124  // Control sentence (if debug)
1125 
1126  #ifndef NDEBUG
1127 
1128  const size_t size = individual.size();
1129 
1130  const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
1131 
1132  const size_t parameters_number = neural_network_pointer->count_parameters_number();
1133 
1134  const size_t population_size = get_population_size();
1135 
1136  if(i >= population_size)
1137  {
1138  std::ostringstream buffer;
1139 
1140  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1141  << "set_individual(const size_t&, Vector<double>) method.\n"
1142  << "Index must be less than population size.\n";
1143 
1144  throw std::logic_error(buffer.str());
1145  }
1146  else if(size != parameters_number)
1147  {
1148  std::ostringstream buffer;
1149 
1150  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1151  << "set_individual(const size_t&, Vector<double>) method.\n"
1152  << "Size must be equal to number of parameters.\n";
1153 
1154  throw std::logic_error(buffer.str());
1155  }
1156 
1157  #endif
1158 
1159  // Get individual
1160 
1161  population.set_row(i, individual);
1162 }
1163 
1164 
1165 // size_t calculate_best_individual_index(void) const method
1166 
1168 
1170 {
1171  return(fitness.calculate_maximal_index());
1172 }
1173 
1174 
1175 // void set_warning_parameters_norm(const double&) method
1176 
1180 
1181 void EvolutionaryAlgorithm::set_warning_parameters_norm(const double& new_warning_parameters_norm)
1182 {
1183  // Control sentence (if debug)
1184 
1185  #ifndef NDEBUG
1186 
1187  if(new_warning_parameters_norm < 0.0)
1188  {
1189  std::ostringstream buffer;
1190 
1191  buffer << "OpenNN Exception: TrainingAlgorithm class.\n"
1192  << "void set_warning_parameters_norm(const double&) method.\n"
1193  << "Warning parameters norm must be equal or greater than 0.\n";
1194 
1195  throw std::logic_error(buffer.str());
1196  }
1197 
1198  #endif
1199 
1200  // Set warning parameters norm
1201 
1202  warning_parameters_norm = new_warning_parameters_norm;
1203 }
1204 
1205 
1206 // void set_error_parameters_norm(const double&) method
1207 
1211 
1212 void EvolutionaryAlgorithm::set_error_parameters_norm(const double& new_error_parameters_norm)
1213 {
1214  // Control sentence (if debug)
1215 
1216  #ifndef NDEBUG
1217 
1218  if(new_error_parameters_norm < 0.0)
1219  {
1220  std::ostringstream buffer;
1221 
1222  buffer << "OpenNN Exception: TrainingAlgorithm class.\n"
1223  << "void set_error_parameters_norm(const double&) method.\n"
1224  << "Error parameters norm must be equal or greater than 0.\n";
1225 
1226  throw std::logic_error(buffer.str());
1227  }
1228 
1229  #endif
1230 
1231  // Set error parameters norm
1232 
1233  error_parameters_norm = new_error_parameters_norm;
1234 }
1235 
1236 
1237 // void set_best_performance_goal(const double&) method
1238 
1242 
1243 void EvolutionaryAlgorithm::set_best_performance_goal(const double& new_best_performance_goal)
1244 {
1245  best_performance_goal = new_best_performance_goal;
1246 }
1247 
1248 
1249 // void set_maximum_generalization_performance_decreases(const size_t&) method
1250 
1253 
1254 void EvolutionaryAlgorithm::set_maximum_generalization_performance_decreases(const size_t& new_maximum_generalization_performance_decreases)
1255 {
1256  maximum_generalization_performance_decreases = new_maximum_generalization_performance_decreases;
1257 }
1258 
1259 
1260 // void set_maximum_time(const double&) method
1261 
1264 
1265 void EvolutionaryAlgorithm::set_maximum_time(const double& new_maximum_time)
1266 {
1267  // Control sentence (if debug)
1268 
1269  #ifndef NDEBUG
1270 
1271  if(new_maximum_time < 0.0)
1272  {
1273  std::ostringstream buffer;
1274 
1275  buffer << "OpenNN Exception: TrainingAlgorithm class.\n"
1276  << "void set_maximum_time(const double&) method.\n"
1277  << "Maximum time must be equal or greater than 0.\n";
1278 
1279  throw std::logic_error(buffer.str());
1280  }
1281 
1282  #endif
1283 
1284  // Set maximum time
1285 
1286  maximum_time = new_maximum_time;
1287 }
1288 
1289 
1290 // void set_reserve_elapsed_time_history(const bool&) method
1291 
1295 
1296 void EvolutionaryAlgorithm::set_reserve_elapsed_time_history(const bool& new_reserve_elapsed_time_history)
1297 {
1298  reserve_elapsed_time_history = new_reserve_elapsed_time_history;
1299 }
1300 
1301 
1302 // void set_reserve_generalization_performance_history(const bool&) method
1303 
1307 
1308 void EvolutionaryAlgorithm::set_reserve_generalization_performance_history(const bool& new_reserve_generalization_performance_history)
1309 {
1310  reserve_generalization_performance_history = new_reserve_generalization_performance_history;
1311 }
1312 
1313 
1314 // void set_display_period(const size_t&) method
1315 
1319 
1320 void EvolutionaryAlgorithm::set_display_period(const size_t& new_display_period)
1321 {
1322  // Control sentence (if debug)
1323 
1324  #ifndef NDEBUG
1325 
1326  if(new_display_period <= 0)
1327  {
1328  std::ostringstream buffer;
1329 
1330  buffer << "OpenNN Exception: TrainingAlgorithm class.\n"
1331  << "void set_display_period(const size_t&) method.\n"
1332  << "First training rate must be greater than 0.\n";
1333 
1334  throw std::logic_error(buffer.str());
1335  }
1336 
1337  #endif
1338 
1339  display_period = new_display_period;
1340 }
1341 
1342 
1343 
1344 // Vector<double> calculate_population_norm(void) const method
1345 
1347 
1349 {
1350  const size_t population_size = get_population_size();
1351 
1352  Vector<double> population_norm(population_size);
1353 
1354  const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
1355 
1356  const size_t parameters_number = neural_network_pointer->count_parameters_number();
1357 
1358  Vector<double> individual(parameters_number);
1359 
1360  for(size_t i = 0; i < population_size; i++)
1361  {
1362  individual = get_individual(i);
1363 
1364  population_norm[i] = individual.calculate_norm();
1365  }
1366 
1367  return(population_norm);
1368 }
1369 
1370 
1371 // double calculate_mean_performance(void) const method
1372 
1374 
1376 {
1377  return(performance.calculate_mean());
1378 }
1379 
1380 
1381 // double calculate_standard_deviation_performance(void) const method
1382 
1384 
1386 {
1388 }
1389 
1390 
1391 // Training parameters
1392 
1393 // const bool& get_elitism_size(void) const method
1394 
1396 
1398 {
1399  return(elitism_size);
1400 }
1401 
1402 
1403 // const double& get_selective_pressure(void) const method
1404 
1406 
1408 {
1409  return(selective_pressure);
1410 }
1411 
1412 
1413 // const double& get_recombination_size(void) const method
1414 
1416 
1418 {
1419  return(recombination_size);
1420 }
1421 
1422 
1423 // const double& get_mutation_rate(void) const method
1424 
1426 
1428 {
1429  return(mutation_rate);
1430 }
1431 
1432 
1433 // const double& get_mutation_range(void) const method
1434 
1436 
1438 {
1439  return(mutation_range);
1440 }
1441 
1442 
1443 // const double& get_mean_performance_goal(void) const method
1444 
1446 
1448 {
1449  return(mean_performance_goal);
1450 }
1451 
1452 
1453 // const double& get_standard_deviation_performance_goal(void) const method
1454 
1456 
1458 {
1460 }
1461 
1462 
1463 // const size_t& get_maximum_generations_number(void) const method
1464 
1466 
1468 {
1470 }
1471 
1472 
1473 // void set_elitism_size(const size_t&) method
1474 
1478 
1479 void EvolutionaryAlgorithm::set_elitism_size(const size_t& new_elitism_size)
1480 {
1481  const size_t half_population_size = get_population_size()/2;
1482 
1483  if(new_elitism_size > half_population_size)
1484  {
1485 // buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1486 // << "void set_elitism_size(const size_t&) method.\n"
1487 // << "Eltism size (" << new_elitism_size << ") must be less or equal than half the population size (" << half_population_size << ").\n";
1488 //
1489 // throw std::logic_error(buffer.str());
1490 
1491  elitism_size = 0;
1492  }
1493 
1494  elitism_size = new_elitism_size;
1495 }
1496 
1497 
1498 // void set_selective_pressure(const double&) method
1499 
1503 
1504 void EvolutionaryAlgorithm::set_selective_pressure(const double& new_selective_pressure)
1505 {
1506  if(new_selective_pressure <= 0.0)
1507  {
1508  std::ostringstream buffer;
1509 
1510  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1511  << "void set_selective_pressure(const double&) method. "
1512  << "Selective pressure must be greater than 0.\n";
1513 
1514  throw std::logic_error(buffer.str());
1515  }
1516 
1517  // Set selective pressure
1518 
1519  selective_pressure = new_selective_pressure;
1520 }
1521 
1522 
1523 // void set_recombination_size(const double&) method
1524 
1529 
1530 void EvolutionaryAlgorithm::set_recombination_size(const double& new_recombination_size)
1531 {
1532  if(new_recombination_size < 0.0)
1533  {
1534  std::ostringstream buffer;
1535 
1536  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1537  << "void set_recombination_size(const double&) method.\n"
1538  << "Recombination size must be equal or greater than 0.\n";
1539 
1540  throw std::logic_error(buffer.str());
1541  }
1542 
1543  // Set recombination size
1544 
1545  recombination_size = new_recombination_size;
1546 }
1547 
1548 
1549 // void set_mutation_rate(const double&) method
1550 
1555 
1556 void EvolutionaryAlgorithm::set_mutation_rate(const double& new_mutation_rate)
1557 {
1558  // Control sentence
1559 
1560  if(new_mutation_rate < 0.0 || new_mutation_rate > 1.0)
1561  {
1562  std::ostringstream buffer;
1563 
1564  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1565  << "void set_mutation_rate(const double&) method.\n"
1566  << "Mutation rate must be a value between 0 and 1.\n";
1567 
1568  throw std::logic_error(buffer.str());
1569  }
1570 
1571  // Set mutation rate
1572 
1573  mutation_rate = new_mutation_rate;
1574 }
1575 
1576 
1577 // void set_mutation_range(const double&) method
1578 
1583 
1584 void EvolutionaryAlgorithm::set_mutation_range(const double& new_mutation_range)
1585 {
1586  // Control sentence
1587 
1588  if(new_mutation_range < 0.0)
1589  {
1590  std::ostringstream buffer;
1591 
1592  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1593  << "void set_mutation_range(const double&) method.\n"
1594  << "Mutation range must be equal or greater than 0.\n";
1595 
1596  throw std::logic_error(buffer.str());
1597  }
1598 
1599  // Set mutation range
1600 
1601  mutation_range = new_mutation_range;
1602 }
1603 
1604 
1605 // void set_maximum_generations_number(size_t) method
1606 
1610 
1611 void EvolutionaryAlgorithm::set_maximum_generations_number(const size_t& new_maximum_generations_number)
1612 {
1613  // Control sentence (if debug)
1614 
1615  #ifndef NDEBUG
1616 
1617  if(new_maximum_generations_number == 0)
1618  {
1619  std::ostringstream buffer;
1620 
1621  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1622  << "void set_maximum_generations_number(size_t) method.\n"
1623  << "Maximum number of generations must be greater than 0.\n";
1624 
1625  throw std::logic_error(buffer.str());
1626  }
1627 
1628  #endif
1629 
1630  // Set maximum number of generations
1631 
1632  maximum_generations_number = new_maximum_generations_number;
1633 }
1634 
1635 
1636 
1637 // void set_mean_performance_goal(const double&) method
1638 
1641 
1642 void EvolutionaryAlgorithm::set_mean_performance_goal(const double& new_mean_performance_goal)
1643 {
1644  mean_performance_goal = new_mean_performance_goal;
1645 }
1646 
1647 
1648 // void set_standard_deviation_performance_goal(const double&) method
1649 
1652 
1653 void EvolutionaryAlgorithm::set_standard_deviation_performance_goal(const double& new_standard_deviation_performance_goal)
1654 {
1655  // Control sentence (if debug)
1656 
1657  #ifndef NDEBUG
1658 
1659  if(new_standard_deviation_performance_goal < 0.0)
1660  {
1661  std::ostringstream buffer;
1662 
1663  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1664  << "void set_standard_deviation_performance_goal(const double&) method.\n"
1665  << "Standard deviation of performance goal must be equal or greater than 0.\n";
1666 
1667  throw std::logic_error(buffer.str());
1668  }
1669 
1670  #endif
1671 
1672  // Set standard deviation of performance goal
1673 
1674  standard_deviation_performance_goal = new_standard_deviation_performance_goal;
1675 
1676 }
1677 
1678 // void set_fitness_assignment_method(FitnessAssignmentMethod) method
1679 
1682 
1684 (const EvolutionaryAlgorithm::FitnessAssignmentMethod& new_fitness_assignment_method)
1685 {
1686  fitness_assignment_method = new_fitness_assignment_method;
1687 }
1688 
1689 
1690 // void set_selection_method(SelectionMethod) method
1691 
1695 
1698 {
1699  selection_method = new_selection_method;
1700 }
1701 
1702 
1703 // void set_recombination_method(RecombinationMethod) method
1704 
1708 
1711 {
1712  recombination_method = new_recombination_method;
1713 }
1714 
1715 
1716 // void set_mutation_method(MutationMethod) method
1717 
1721 
1723 {
1724  mutation_method = new_mutation_method;
1725 }
1726 
1727 
1728 // void initialize_population(const double&) method
1729 
1732 
1734 {
1735  population.initialize(new_value);
1736 }
1737 
1738 
1739 
1740 // void randomize_population_uniform(void) method
1741 
1744 
1746 {
1748 }
1749 
1750 
1751 // void randomize_population_uniform(const double&, const double&) method
1752 
1758 
1759 void EvolutionaryAlgorithm::randomize_population_uniform(const double& minimum, const double& maximum)
1760 {
1761  population.randomize_uniform(minimum, maximum);
1762 }
1763 
1764 
1765 // void randomize_population_uniform(Vector<double>, Vector<double>) method
1766 
1772 
1774 {
1775  const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
1776 
1777  const size_t parameters_number = neural_network_pointer->count_parameters_number();
1778 
1779  // Control sentence (if debug)
1780 
1781  #ifndef NDEBUG
1782 
1783  const size_t minimum_size = minimum.size();
1784  const size_t maximum_size = maximum.size();
1785 
1786  if(minimum_size != parameters_number || maximum_size != parameters_number)
1787  {
1788  std::ostringstream buffer;
1789 
1790  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1791  << "void randomize_population_uniform(Vector<double>, Vector<double>).\n"
1792  << "Minimum value and maximum value sizes must be equal to number of parameters.\n";
1793 
1794  throw std::logic_error(buffer.str());
1795  }
1796 
1797  #endif
1798 
1799  Vector<double> individual(parameters_number);
1800 
1801  const size_t population_size = get_population_size();
1802 
1803  for(size_t i = 0; i < population_size; i++)
1804  {
1805  individual.randomize_uniform(minimum, maximum);
1806 
1807  set_individual(i, individual);
1808  }
1809 }
1810 
1811 
1812 // void randomize_population_normal(void) method
1813 
1816 
1818 {
1820 }
1821 
1822 
1823 // void randomize_population_normal(const double&, const double&) method
1824 
1830 
1831 void EvolutionaryAlgorithm::randomize_population_normal(const double& mean, const double& standard_deviation)
1832 {
1833  population.randomize_normal(mean, standard_deviation);
1834 }
1835 
1836 
1837 // void randomize_population_normal(Vector<double>, Vector<double>) method
1838 
1844 
1846 randomize_population_normal(const Vector<double>& mean, const Vector<double>& standard_deviation)
1847 {
1848  const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
1849 
1850  const size_t parameters_number = neural_network_pointer->count_parameters_number();
1851 
1852  // Control sentence (if debug)
1853 
1854  #ifndef NDEBUG
1855 
1856  const size_t mean_size = mean.size();
1857  const size_t standard_deviation_size = standard_deviation.size();
1858 
1859  if(mean_size != parameters_number || standard_deviation_size != parameters_number)
1860  {
1861  std::ostringstream buffer;
1862 
1863  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1864  << "void randomize_population_normal(Vector<double>, Vector<double>).\n"
1865  << "Mean and standard deviation sizes must be equal to number of parameters.\n";
1866 
1867  throw std::logic_error(buffer.str());
1868  }
1869 
1870  #endif
1871 
1872  Vector<double> individual(parameters_number);
1873 
1874  const size_t population_size = get_population_size();
1875 
1876  for(size_t i = 0; i < population_size; i++)
1877  {
1878  individual.randomize_normal(mean, standard_deviation);
1879 
1880  set_individual(i, individual);
1881  }
1882 }
1883 
1884 
1885 // void perform_fitness_assignment(void) method
1886 
1888 
1890 {
1892  {
1893  case LinearRanking:
1894  {
1896  }
1897 
1898  break;
1899 
1900  default:
1901  {
1902  std::ostringstream buffer;
1903 
1904  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1905  << "void perform_fitness_assignment(void).\n"
1906  << "Unknown fitness assignment method.\n";
1907 
1908  throw std::logic_error(buffer.str());
1909  }
1910  break;
1911  }
1912 }
1913 
1914 
1915 // void perform_selection(void) method
1916 
1918 
1920 {
1921  switch(selection_method)
1922  {
1923  case RouletteWheel:
1924  {
1926  }
1927  break;
1928 
1929  default:
1930  {
1931  std::ostringstream buffer;
1932 
1933  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1934  << "void perform_selection(void).\n"
1935  << "Unknown selection method.\n";
1936 
1937  throw std::logic_error(buffer.str());
1938  }
1939  break;
1940  }
1941 
1942  // Control sentence (if debug)
1943 
1944  #ifndef NDEBUG
1945 
1946  const size_t selected_individuals_number = selection.count_occurrences(true);
1947 
1948  const size_t population_size = get_population_size();
1949 
1950  if(selected_individuals_number != population_size/2)
1951  {
1952  std::ostringstream buffer;
1953 
1954  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1955  << "void perform_selection(void).\n"
1956  << "Number of selected individuals is not equal to half of the population size.\n";
1957 
1958  throw std::logic_error(buffer.str());
1959  }
1960 
1961  #endif
1962 }
1963 
1964 
1965 // void perform_recombination(void) method
1966 
1968 
1970 {
1971  switch(recombination_method)
1972  {
1973  case Intermediate:
1974  {
1976  }
1977  break;
1978 
1979  case Line:
1980  {
1982  }
1983  break;
1984 
1985  default:
1986  {
1987  std::ostringstream buffer;
1988 
1989  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
1990  << "void perform_recombination(void).\n"
1991  << "Unknown recombination method.\n";
1992 
1993  throw std::logic_error(buffer.str());
1994  }
1995  break;
1996  }
1997 }
1998 
1999 
2000 // void perform_mutation(void) method
2001 
2003 
2005 {
2006  switch(mutation_method)
2007  {
2008  case Normal:
2009  {
2011  }
2012  break;
2013 
2014  case Uniform:
2015  {
2017  }
2018  break;
2019 
2020  default:
2021  {
2022  std::ostringstream buffer;
2023 
2024  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
2025  << "void perform_mutationg(void).\n"
2026  << "Unknown mutation method.\n";
2027 
2028  throw std::logic_error(buffer.str());
2029  }
2030  break;
2031  }
2032 }
2033 
2034 
2035 // void evolve_population(void) method
2036 
2038 
2040 {
2041  // Fitness assignment
2042 
2044 
2045  // Selection
2046 
2048 
2049  // Recombination
2050 
2052 
2053  // Mutation
2054 
2055  perform_mutation();
2056 }
2057 
2058 
2059 // void evaluate_population(void) method
2060 
2063 
2065 {
2066  // Control sentence (if debug)
2067 
2068  #ifndef NDEBUG
2069 
2070  if(performance_functional_pointer == NULL)
2071  {
2072  std::ostringstream buffer;
2073 
2074  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
2075  << "void evaluate_population(void).\n"
2076  << "Performance functional pointer is NULL.\n";
2077 
2078  throw std::logic_error(buffer.str());
2079  }
2080 
2081  #endif
2082 
2083  // Neural network
2084 
2085  const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
2086 
2087  // Control sentence (if debug)
2088 
2089  #ifndef NDEBUG
2090 
2091  if(!neural_network_pointer)
2092  {
2093  std::ostringstream buffer;
2094 
2095  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
2096  << "void evaluate_population(void).\n"
2097  << "Neural network pointer is NULL.\n";
2098 
2099  throw std::logic_error(buffer.str());
2100  }
2101 
2102  #endif
2103 
2104  const size_t parameters_number = neural_network_pointer->count_parameters_number();
2105 
2106  Vector<double> individual(parameters_number);
2107 
2108  // Evaluate performance functional for all individuals
2109 
2110  const size_t population_size = get_population_size();
2111 
2112  for(size_t i = 0; i < population_size; i++)
2113  {
2114  individual = get_individual(i);
2115 
2117 
2118  if(!(performance[i] > -1.0e99 && performance[i] < 1.0e99))
2119  {
2120  std::ostringstream buffer;
2121 
2122  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
2123  << "void evaluate_population(void) method.\n"
2124  << "Performance of individual " << i << " is not a real number.\n";
2125 
2126  throw std::logic_error(buffer.str());
2127  }
2128  }
2129 }
2130 
2131 
2132 // void perform_linear_ranking_fitness_assignment(void) method
2133 
2140 
2142 {
2143  // Sorted performance vector
2144 
2146 
2147  // Perform linear ranking fitness assignment
2148  // Cannot do that without loop because of different types of fitness and rank vectors
2149 
2150  const size_t population_size = get_population_size();
2151 
2152  for(size_t i = 0; i < population_size; i++)
2153  {
2154  fitness[i] = selective_pressure*rank[i];
2155  }
2156 }
2157 
2158 
2159 // void perform_roulette_wheel_selection(void) method
2160 
2164 
2166 {
2167  const size_t population_size = get_population_size();
2168 
2169  // Set selection vector to false
2170 
2171  selection.initialize(false);
2172 
2173  const Vector<size_t> elite_individuals = fitness.calculate_maximal_indices(elitism_size);
2174 
2175  for(size_t i = 0; i < elitism_size; i++)
2176  {
2177  const size_t elite_individual_index = elite_individuals[i];
2178 
2179  selection[elite_individual_index] = true;
2180  }
2181 
2182  const size_t selection_target = population_size/2 - elitism_size;
2183 
2184  if(selection_target <= 0)
2185  {
2186  return;
2187  }
2188 
2189  // Cumulative fitness vector
2190 
2191  const Vector<double> cumulative_fitness = fitness.calculate_cumulative();
2192 
2193  const double fitness_sum = fitness.calculate_sum();
2194 
2195  // Select individuals until the desired number of selections is obtained
2196 
2197  size_t selection_count = 0;
2198 
2199  double pointer;
2200 
2201  while(selection_count != selection_target)
2202  {
2203  // Random number between 0 and total cumulative fitness
2204 
2205  pointer = calculate_random_uniform(0.0, fitness_sum);
2206 
2207  // Perform selection
2208 
2209  if(pointer < cumulative_fitness[0])
2210  {
2211  if(!selection[0])
2212  {
2213  selection[0] = true;
2214  selection_count++;
2215  }
2216  }
2217  else
2218  {
2219  for(size_t i = 1; i < population_size; i++)
2220  {
2221  if(pointer < cumulative_fitness[i] && pointer >= cumulative_fitness[i-1])
2222  {
2223  if(!selection[i])
2224  {
2225  selection[i] = true;
2226  selection_count++;
2227  }
2228  }
2229  }
2230  }
2231  }
2232 
2233  // Control sentence (if debug)
2234 
2235  #ifndef NDEBUG
2236 
2237  if(selection.count_occurrences(true) != population_size/2)
2238  {
2239  std::ostringstream buffer;
2240 
2241  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
2242  << "void perform_roulette_wheel_selection(void) method.\n"
2243  << "Selection count (" << selection.count_occurrences(true) << ") is not equal to half population size (" << population_size/2 << ").\n";
2244 
2245  throw std::logic_error(buffer.str());
2246  }
2247 
2248  #endif
2249 }
2250 
2251 
2252 // void perform_intermediate_recombination(void) method
2253 
2258 
2260 {
2261  const size_t population_size = get_population_size();
2262 
2263  #ifndef NDEBUG
2264 
2265  if(selection.count_occurrences(true) != population_size/2)
2266  {
2267  std::ostringstream buffer;
2268 
2269  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
2270  << "void perform_intermediate_recombination(void) method.\n"
2271  << "Selection count (" << selection.count_occurrences(true) << ") is not equal to half population size (" << population_size/2 << ").\n";
2272 
2273  throw std::logic_error(buffer.str());
2274  }
2275 
2276  #endif
2277 
2278  const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
2279 
2280  const size_t parameters_number = neural_network_pointer->count_parameters_number();
2281 
2282  Matrix<double> new_population(population_size, parameters_number);
2283 
2284  Vector<double> parent_1(parameters_number);
2285  Vector<double> parent_2(parameters_number);
2286 
2287  Vector<double> offspring(parameters_number);
2288 
2289  Matrix<size_t> recombination(population_size, 2);
2290 
2291  // Start recombination
2292 
2293  size_t new_population_size_count = 0;
2294 
2295  bool parent_2_candidate;
2296 
2297  size_t parent_2_candidate_index;
2298 
2299  double scaling_factor;
2300 
2301  for(size_t i = 0; i < population_size; i++)
2302  {
2303  if(selection[i])
2304  {
2305  // Set parent 1
2306 
2307  parent_1 = get_individual(i);
2308 
2309  // Generate 2 offspring with parent 1
2310 
2311  for(size_t j = 0; j < 2; j++)
2312  {
2313  // Choose parent 2 at random among selected individuals
2314 
2315  parent_2_candidate = false;
2316 
2317  do{
2318  // Integer random number beteen 0 and population size
2319 
2320  parent_2_candidate_index = (size_t)calculate_random_uniform(0.0, (double)population_size);
2321 
2322  // Check if candidate for parent 2 is ok
2323 
2324  if(selection[parent_2_candidate_index] && parent_2_candidate_index != i)
2325  {
2326  parent_2_candidate = true;
2327 
2328  recombination(new_population_size_count,0) = i;
2329 
2330  recombination(new_population_size_count,1) = parent_2_candidate_index;
2331 
2332  parent_2 = get_individual(parent_2_candidate_index);
2333 
2334  // Perform inediate recombination between parent 1 and parent 2
2335 
2336  for(size_t j = 0; j < parameters_number; j++)
2337  {
2338  // Choose the scaling factor to be a random number between
2339  // -recombination_size and 1+recombination_size for each
2340  // variable anew.
2341 
2342  scaling_factor = calculate_random_uniform(-recombination_size, 1.0 + recombination_size);
2343 
2344  offspring[j] = scaling_factor*parent_1[j] + (1.0 - scaling_factor)*parent_2[j];
2345  }
2346 
2347  // Add offspring to new_population matrix
2348 
2349  new_population.set_row(new_population_size_count, offspring);
2350 
2351  new_population_size_count++;
2352  }
2353  }while(parent_2_candidate != true);
2354  }
2355  }
2356  }
2357 
2358  // Count number of new individuals control sentence
2359 
2360  #ifndef NDEBUG
2361 
2362  if(new_population_size_count != population_size)
2363  {
2364  std::ostringstream buffer;
2365 
2366  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
2367  << "void perform_intermediate_recombination(void) method.\n"
2368  << "Count new population size is not equal to population size.\n";
2369 
2370  throw std::logic_error(buffer.str());
2371  }
2372 
2373  #endif
2374 
2375  // Set new population
2376 
2377  population = new_population;
2378 }
2379 
2380 
2381 // void perform_line_recombination(void) method
2382 
2386 
2388 {
2389  const size_t population_size = get_population_size();
2390 
2391  #ifndef NDEBUG
2392 
2393  if(selection.count_occurrences(true) != population_size/2)
2394  {
2395  std::ostringstream buffer;
2396 
2397  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
2398  << "void perform_line_recombination(void) method.\n"
2399  << "Selection count (" << selection.count_occurrences(true) << ") is not equal to half population size (" << population_size/2 << ").\n";
2400 
2401  throw std::logic_error(buffer.str());
2402  }
2403 
2404  #endif
2405 
2406  const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
2407 
2408  const size_t parameters_number = neural_network_pointer->count_parameters_number();
2409 
2410  Matrix<double> new_population(population_size, parameters_number);
2411 
2412  Vector<double> parent_1(parameters_number);
2413  Vector<double> parent_2(parameters_number);
2414 
2415  Vector<double> offspring(parameters_number);
2416  Vector<double> parent_1_term(parameters_number);
2417  Vector<double> parent_2_term(parameters_number);
2418 
2419  Matrix<size_t> recombination(population_size, 2);
2420 
2421  // Start recombination
2422 
2423  size_t new_population_size_count = 0;
2424 
2425  bool parent_2_candidate;
2426 
2427  size_t parent_2_candidate_index;
2428 
2429  double scaling_factor;
2430 
2431  for(size_t i = 0; i < population_size; i++)
2432  {
2433  if(selection[i])
2434  {
2435  // Set parent 1
2436 
2437  parent_1 = get_individual(i);
2438 
2439  // Generate 2 offspring with parent 1
2440 
2441  for(size_t j = 0; j < 2; j++)
2442  {
2443  // Choose parent 2 at random among selected individuals
2444 
2445  parent_2_candidate = false;
2446 
2447  do
2448  {
2449  // Integer random number beteen 0 and population size
2450 
2451  parent_2_candidate_index = (size_t)calculate_random_uniform(0.0, (double)population_size);
2452 
2453  // Check if candidate for parent 2 is ok
2454 
2455  if(selection[parent_2_candidate_index] && parent_2_candidate_index != i)
2456  {
2457  parent_2_candidate = true;
2458 
2459  recombination(new_population_size_count,0) = i;
2460  recombination(new_population_size_count,1) = parent_2_candidate_index;
2461 
2462  parent_2 = get_individual(parent_2_candidate_index);
2463 
2464  // Perform inediate recombination between parent 1 and parent 2
2465 
2466  // Choose the scaling factor to be a random number between
2467  // -recombination_size and 1+recombination_size for all
2468  // variables.
2469 
2470  scaling_factor = calculate_random_uniform(-recombination_size , 1.0+recombination_size);
2471 
2472  parent_1_term = parent_1*scaling_factor;
2473  parent_2_term = parent_2*(1.0 - scaling_factor);
2474 
2475  offspring = parent_1_term + parent_2_term;
2476 
2477  // Add offspring to new_population matrix
2478 
2479  new_population.set_row(new_population_size_count, offspring);
2480 
2481  new_population_size_count++;
2482  }
2483  }while(!parent_2_candidate);
2484  }
2485  }
2486  }
2487 
2488  // Count new population size control sentence
2489 
2490  if(new_population_size_count != population_size)
2491  {
2492  std::ostringstream buffer;
2493 
2494  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
2495  << "void perform_line_recombination(void) method.\n"
2496  << "Count new population size is not equal to population size.\n";
2497 
2498  throw std::logic_error(buffer.str());
2499  }
2500 
2501  // Set new population
2502 
2503  population = new_population;
2504 }
2505 
2506 
2507 // void perform_normal_mutation(void) method
2508 
2511 
2513 {
2514  const size_t population_size = get_population_size();
2515 
2516  const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
2517 
2518  const size_t parameters_number = neural_network_pointer->count_parameters_number();
2519 
2520  Vector<double> individual(parameters_number);
2521 
2522  double pointer;
2523 
2524  for(size_t i = 0; i < population_size; i++)
2525  {
2526  individual = get_individual(i);
2527 
2528  for(size_t j = 0; j < parameters_number; j++)
2529  {
2530  // Random number between 0 and 1
2531 
2532  pointer = calculate_random_uniform(0.0, 1.0);
2533 
2534  if(pointer < mutation_rate)
2535  {
2536  individual[j] += calculate_random_normal(0.0, mutation_range);
2537  }
2538  }
2539 
2540  set_individual(i, individual);
2541  }
2542 }
2543 
2544 
2545 // void perform_uniform_mutation(void) method
2546 
2549 
2551 {
2552  const size_t population_size = get_population_size();
2553 
2554  const NeuralNetwork* neural_network_pointer = performance_functional_pointer->get_neural_network_pointer();
2555 
2556  const size_t parameters_number = neural_network_pointer->count_parameters_number();
2557 
2558  Vector<double> individual(parameters_number, 0.0);
2559 
2560  double pointer;
2561 
2562  for(size_t i = 0; i < population_size; i++)
2563  {
2564  individual = get_individual(i);
2565 
2566  for(size_t j = 0; j < parameters_number; j++)
2567  {
2568  // random number between 0 and 1
2569 
2570  pointer = calculate_random_uniform(0.0, 1.0);
2571 
2572  if(pointer < mutation_rate)
2573  {
2574  individual[j] += calculate_random_uniform(-mutation_range, mutation_range);
2575  }
2576  }
2577 
2578  set_individual(i, individual);
2579  }
2580 }
2581 
2582 
2583 // std::string to_string(void) const method
2584 
2586 
2588 {
2589  std::ostringstream buffer;
2590 
2591  // Population history
2592 
2593  if(!population_history.empty())
2594  {
2595  if(!population_history[0].empty())
2596  {
2597  buffer << "% Population history:\n"
2598  << population_history << "\n";
2599  }
2600  }
2601 
2602  // Best individual history
2603 
2604  if(!best_individual_history.empty())
2605  {
2606  if(!population_history[0].empty())
2607  {
2608  buffer << "% Best individual history:\n"
2609  << best_individual_history << "\n";
2610  }
2611  }
2612 
2613  // Mean norm history
2614 
2615  if(!mean_norm_history.empty())
2616  {
2617  buffer << "% Mean norm history:\n"
2618  << mean_norm_history << "\n";
2619  }
2620 
2621  // Standard deviation norm history
2622 
2623  if(!standard_deviation_norm_history.empty())
2624  {
2625  buffer << "% Standard deviation norm history:\n"
2626  << standard_deviation_norm_history << "\n";
2627  }
2628 
2629  // Best norm history
2630 
2631  if(!best_norm_history.empty())
2632  {
2633  buffer << "% Best norm history:\n"
2634  << best_norm_history << "\n";
2635  }
2636 
2637  // performance history
2638 
2639  if(!performance_history.empty())
2640  {
2641  buffer << "% performance history:\n"
2642  << performance_history << "\n";
2643  }
2644 
2645  // Mean performance history
2646 
2647  if(!mean_performance_history.empty())
2648  {
2649  buffer << "% Mean performance history:\n"
2650  << mean_performance_history << "\n";
2651  }
2652 
2653  // Standard deviation performance history
2654 
2656  {
2657  buffer << "% Standard deviation performance history:\n"
2659  }
2660 
2661  // Best performance history
2662 
2663  if(!best_performance_history.empty())
2664  {
2665  buffer << "% Best performance history:\n"
2666  << best_performance_history << "\n";
2667  }
2668 
2669  // Generalization performance history
2670 
2672  {
2673  buffer << "% Generalization performance history:\n"
2675  }
2676 
2677  // Elapsed time history
2678 
2679  if(!elapsed_time_history.empty())
2680  {
2681  buffer << "% Elapsed time history:\n"
2682  << elapsed_time_history << "\n";
2683  }
2684 
2685  return(buffer.str());
2686 }
2687 
2688 
2689 // void resize_training_history(const size_t&) method
2690 
2693 
2695 {
2696 // evolutionary_algorithm_pointer->set_reserve_population_history(false);
2697 // evolutionary_algorithm_pointer->set_reserve_best_individual_history(false);
2698 // evolutionary_algorithm_pointer->set_reserve_elapsed_time_history(false);
2699 
2700  if(evolutionary_algorithm_pointer->get_reserve_population_history())
2701  {
2702  population_history.resize(new_size);
2703  }
2704 
2705  if(evolutionary_algorithm_pointer->get_reserve_best_individual_history())
2706  {
2707  best_individual_history.resize(new_size);
2708  }
2709 
2710  if(evolutionary_algorithm_pointer->get_reserve_mean_norm_history())
2711  {
2712  mean_norm_history.resize(new_size);
2713  }
2714 
2715  if(evolutionary_algorithm_pointer->get_reserve_standard_deviation_norm_history())
2716  {
2717  standard_deviation_norm_history.resize(new_size);
2718  }
2719 
2720  if(evolutionary_algorithm_pointer->get_reserve_best_norm_history())
2721  {
2722  best_norm_history.resize(new_size);
2723  }
2724 
2725  if(evolutionary_algorithm_pointer->get_reserve_mean_performance_history())
2726  {
2727  mean_performance_history.resize(new_size);
2728  }
2729 
2730  if(evolutionary_algorithm_pointer->get_reserve_standard_deviation_performance_history())
2731  {
2732  standard_deviation_performance_history.resize(new_size);
2733  }
2734 // Bug?
2735  if(evolutionary_algorithm_pointer->get_reserve_best_performance_history())
2736  {
2737  best_performance_history.resize(new_size);
2738  }
2739 //
2740  if(evolutionary_algorithm_pointer->get_reserve_generalization_performance_history())
2741  {
2742  generalization_performance_history.resize(new_size);
2743  }
2744 
2745  if(evolutionary_algorithm_pointer->get_reserve_elapsed_time_history())
2746  {
2747  elapsed_time_history.resize(new_size);
2748  }
2749 }
2750 
2751 
2752 // Matrix<std::string> write_final_results(const size_t& precision) const method
2753 
2755 {
2756  std::ostringstream buffer;
2757 
2758  Vector<std::string> names;
2759  Vector<std::string> values;
2760 
2761  // Final mean norm of the population.
2762 
2763  names.push_back("Final mean norm");
2764 
2765  buffer.str("");
2766  buffer << std::setprecision(precision) << final_mean_norm;
2767 
2768  values.push_back(buffer.str());
2769 
2770  // Final standard deviation of the population norm.
2771 
2772  names.push_back("Final standard deviation norm");
2773 
2774  buffer.str("");
2775  buffer << std::setprecision(precision) << final_standard_deviation_norm;
2776 
2777  values.push_back(buffer.str());
2778 
2779  // Final norm of the best individual ever.
2780 
2781  names.push_back("Final best norm");
2782 
2783  buffer.str("");
2784  buffer << std::setprecision(precision) << final_best_norm;
2785 
2786  values.push_back(buffer.str());
2787 
2788  // Final mean population performance.
2789 
2790  names.push_back("Final mean performance");
2791 
2792  buffer.str("");
2793  buffer << std::setprecision(precision) << final_mean_performance;
2794 
2795  values.push_back(buffer.str());
2796 
2797  // Final standard deviation of the population performance.
2798 
2799  names.push_back("Final standard deviation performance");
2800 
2801  buffer.str("");
2802  buffer << std::setprecision(precision) << final_standard_deviation_performance;
2803 
2804  values.push_back(buffer.str());
2805 
2806  // Performance of the best individual ever
2807 
2808  names.push_back("Final best performance");
2809 
2810  buffer.str("");
2811  buffer << std::setprecision(precision) << final_best_performance;
2812 
2813  values.push_back(buffer.str());
2814 
2815  // Final generalization performance
2816 
2817  const PerformanceFunctional* performance_functional_pointer = evolutionary_algorithm_pointer->get_performance_functional_pointer();
2818 
2819  if(performance_functional_pointer->has_generalization())
2820  {
2821  names.push_back("Final generalization performance");
2822 
2823  buffer.str("");
2824  buffer << std::setprecision(precision) << final_generalization_performance;
2825 
2826  values.push_back(buffer.str());
2827  }
2828 
2829  // Generations number
2830 
2831  names.push_back("Generations number");
2832 
2833  buffer.str("");
2834  buffer << std::setprecision(precision) << generations_number;
2835 
2836  values.push_back(buffer.str());
2837 
2838  // Total elapsed time in the training process.
2839 
2840  names.push_back("Elapsed time");
2841 
2842  buffer.str("");
2843  buffer << std::setprecision(precision) << elapsed_time;
2844 
2845  values.push_back(buffer.str());
2846 
2847  // Matrix
2848 
2849  const size_t rows_number = names.size();
2850  const size_t columns_number = 2;
2851 
2852  Matrix<std::string> final_results(rows_number, columns_number);
2853 
2854  final_results.set_column(0, names);
2855  final_results.set_column(1, values);
2856 
2857  return(final_results);
2858 }
2859 
2860 
2861 // EvolutionaryAlgorithmResults* perform_training(void) method
2862 
2865 
2867 {
2868  #ifndef NDEBUG
2869 
2870  check();
2871 
2872  const size_t population_size = get_population_size();
2873 
2874  if(population_size == 0)
2875  {
2876  std::ostringstream buffer;
2877 
2878  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
2879  << "EvolutionaryAlgorithmResults* perform_training(void) method.\n"
2880  << "Population size is zero.\n";
2881 
2882  throw std::logic_error(buffer.str());
2883  }
2884 
2885  #endif
2886 
2887  if(display)
2888  {
2889  std::cout << "Training with the evolutionary algorithm...\n";
2890  }
2891 
2892  EvolutionaryAlgorithmResults* results_pointer = new EvolutionaryAlgorithmResults(this);
2893 
2895 
2896  size_t generalization_failures = 0;
2897 
2898  time_t beginning_time, current_time;
2899  time(&beginning_time);
2900  double elapsed_time;
2901 
2902  bool stop_training = false;
2903 
2904  // Performance functional
2905 
2906  Vector<double> population_norm;
2907 
2908  double mean_norm;
2909  double standard_deviation_norm;
2910 
2911  Vector<double> best_individual;
2912  size_t best_individual_index;
2913  double best_norm = 0.0;
2914 
2915  double best_performance_ever = 1.0e99;
2916 
2917  double best_generation_performance = 1.0e99;
2918 
2919  double generalization_performance = 0.0;
2920 // double old_generalization_performance = 0.0;
2921 
2922  // Neural network stuff
2923 
2925 
2926  // Main loop
2927 
2928  for(size_t generation = 0; generation <= maximum_generations_number; generation++)
2929  {
2930  // Population stuff
2931 
2933  {
2934  results_pointer->population_history[generation] = population;
2935  }
2936 
2937  population_norm = calculate_population_norm();
2938 
2939  // Mean norm
2940 
2941  mean_norm = population_norm.calculate_mean();
2942 
2944  {
2945  results_pointer->mean_norm_history[generation] = mean_norm;
2946  }
2947 
2948  // Standard deviation of norm
2949 
2950  standard_deviation_norm = population_norm.calculate_standard_deviation();
2951 
2953  {
2954  results_pointer->standard_deviation_norm_history[generation] = standard_deviation_norm;
2955  }
2956 
2957  // Population evaluation
2958 
2960 
2961  best_generation_performance = performance.calculate_minimum();
2962 
2963  if(best_generation_performance < best_performance_ever)
2964  {
2965  best_individual_index = performance.calculate_minimal_index();
2966 
2967  best_individual = get_individual(best_individual_index);
2968 
2969  neural_network_pointer->set_parameters(best_individual);
2970 
2971  best_norm = best_individual.calculate_norm();
2972 
2973  best_performance_ever = best_generation_performance;
2974 
2975  //old_generalization_performance = generalization_performance;
2976 
2978  }
2979 
2980  // Best individual
2981 
2983  {
2984  results_pointer->best_individual_history[generation] = best_individual;
2985  }
2986 
2987  // Best individual norm
2988 
2990  {
2991  results_pointer->best_norm_history[generation] = best_norm;
2992  }
2993 
2994  // Mean performance
2995 
2996  const double mean_performance = performance.calculate_mean();
2997 
2999  {
3000  results_pointer->mean_performance_history[generation] = mean_performance;
3001  }
3002 
3003  // Standard deviation of performance
3004 
3005  const double standard_deviation_performance = performance.calculate_standard_deviation();
3006 
3008  {
3009  results_pointer->standard_deviation_performance_history[generation] = standard_deviation_performance;
3010  }
3011 
3012  // Best performance
3013 
3015  {
3016  results_pointer->best_performance_history[generation] = best_performance_ever;
3017  }
3018 
3019  // Generalization performance
3020 
3022  {
3023  results_pointer->generalization_performance_history[generation] = generalization_performance;
3024  }
3025 
3026  // Elapsed time
3027 
3028  time(&current_time);
3029  elapsed_time = difftime(current_time, beginning_time);
3030 
3032  {
3033  results_pointer->elapsed_time_history[generation] = elapsed_time;
3034  }
3035 
3036  // Training history neural network
3037 
3039  {
3040  results_pointer->population_history[generation] = population;
3041  }
3042 
3044  {
3045  results_pointer->best_individual_history[generation] = best_individual;
3046  }
3047 
3049  {
3050  results_pointer->mean_norm_history[generation] = mean_norm;
3051  }
3052 
3054  {
3055  results_pointer->standard_deviation_norm_history[generation] = standard_deviation_norm;
3056  }
3057 
3059  {
3060  results_pointer->best_norm_history[generation] = best_norm;
3061  }
3062 
3063  // Training history training algorithm
3064 
3066  {
3067  results_pointer->mean_performance_history[generation] = mean_performance;
3068  }
3069 
3071  {
3072  results_pointer->standard_deviation_performance_history[generation] = standard_deviation_performance;
3073  }
3074 
3076  {
3077  results_pointer->best_performance_history[generation] = best_performance_ever;
3078  }
3079 
3081  {
3082  results_pointer->elapsed_time_history[generation] = elapsed_time;
3083  }
3084 
3085  // Stopping criteria
3086 
3087  if(best_performance_ever <= best_performance_goal)
3088  {
3089  if(display)
3090  {
3091  std::cout << "Generation " << generation << ": Performance goal reached.\n"
3093  }
3094 
3095  stop_training = true;
3096  }
3097 
3098  if(mean_performance <= mean_performance_goal)
3099  {
3100  if(display)
3101  {
3102  std::cout << "Generation " << generation << ": Mean performance goal reached.\n";
3103  }
3104 
3105  stop_training = true;
3106  }
3107 
3108  if(standard_deviation_performance <= standard_deviation_performance_goal)
3109  {
3110  if(display)
3111  {
3112  std::cout << "Generation " << generation << ": Standard deviation of performance goal reached.\n";
3113  }
3114 
3115  stop_training = true;
3116  }
3117 
3118  else if(generalization_failures > maximum_generalization_performance_decreases)
3119  {
3120  if(display)
3121  {
3122  std::cout << "Generation " << generation << ": Maximum generalization performance decreases reached.\n";
3123  std::cout << "Generalization performance decreases: "<< generalization_failures << std::endl;
3124  }
3125 
3126  stop_training = true;
3127  }
3128 
3129  else if(elapsed_time >= maximum_time)
3130  {
3131  if(display)
3132  {
3133  std::cout << "Generation " << generation << ": Maximum training time reached.\n";
3134  }
3135 
3136  stop_training = true;
3137  }
3138 
3139  else if(generation == maximum_generations_number)
3140  {
3141  if(display)
3142  {
3143  std::cout << "Generation " << generation << ": Maximum number of generations reached.\n";
3144  }
3145 
3146  stop_training = true;
3147  }
3148 
3149  if(generation != 0 && generation % save_period == 0)
3150  {
3151  neural_network_pointer->save(neural_network_file_name);
3152  }
3153 
3154  if(stop_training)
3155  {
3156  if(display)
3157  {
3158  std::cout << "Mean norm: " << mean_norm << "\n"
3159  << "Standard deviation of norm: " << standard_deviation_norm << "\n"
3160  << "Best norm: " << best_norm << "\n"
3161  << "Mean performance: " << mean_performance << "\n"
3162  << "Standard deviation of performance: " << standard_deviation_performance << "\n"
3163  << "Best performance: " << best_performance_ever << "\n"
3165  << "Elapsed time: " << elapsed_time << ";\n";
3166  }
3167 
3168  results_pointer->resize_training_history(1+generation);
3169 
3170  results_pointer->final_mean_norm = mean_norm;
3171  results_pointer->final_standard_deviation_norm = standard_deviation_norm;
3172  results_pointer->final_best_norm = best_norm;
3173  results_pointer->final_mean_performance = mean_performance;
3174  results_pointer->final_standard_deviation_performance = standard_deviation_performance;
3175  results_pointer->final_best_performance = best_performance_ever;
3176  results_pointer->final_generalization_performance = generalization_performance;
3177  results_pointer->elapsed_time = elapsed_time;
3178  results_pointer->generations_number = generation;
3179 
3180  break;
3181  }
3182  else if(display && generation % display_period == 0)
3183  {
3184  std::cout << "Generation " << generation << ";\n"
3185  << "Mean norm: " << mean_norm << "\n"
3186  << "Standard deviation of norm: " << standard_deviation_norm << "\n"
3187  << "Best norm: " << best_norm << "\n"
3188  << "Mean performance: " << mean_performance << "\n"
3189  << "Standard deviation of performance: " << standard_deviation_performance << "\n"
3190  << "Best performance: " << best_performance_ever << "\n"
3192  << "Elapsed time: " << elapsed_time << ";\n";
3193  }
3194 
3195  // Update stuff
3196 
3197  // old_generalization_performance = generalization_performance;
3198 
3199  selection.initialize(false);
3200 
3202 
3203  }
3204 
3205  return(results_pointer);
3206 }
3207 
3208 
3209 // std::string write_training_algorithm_type(void) const method
3210 
3212 {
3213  return("EVOLUTIONARY_ALGORITHM");
3214 }
3215 
3216 
3217 // Matrix<std::string> to_string_matrix(void) const method
3218 
3219 // the most representative
3220 
3222 {
3223  std::ostringstream buffer;
3224 
3225  Vector<std::string> labels;
3226  Vector<std::string> values;
3227 
3228  // Population size
3229 
3230  labels.push_back("Population size");
3231 
3232  buffer.str("");
3233  buffer << get_population_size();
3234 
3235  values.push_back(buffer.str());
3236 
3237  // Fitness assignment method
3238 
3239  labels.push_back("Fitness assignment method");
3240  values.push_back(write_fitness_assignment_method());
3241 
3242  // Selection method
3243 
3244  labels.push_back("Selection method");
3245  values.push_back(write_selection_method());
3246 
3247  // Recombination method
3248 
3249  labels.push_back("Recombination");
3250  values.push_back(write_recombination_method());
3251 
3252  // Mutation method
3253 
3254  labels.push_back("Mutation method");
3255  values.push_back(write_mutation_method());
3256 
3257  // Elitism size
3258 
3259  labels.push_back("Elitism size");
3260 
3261  buffer.str("");
3262  buffer << elitism_size;
3263 
3264  values.push_back(buffer.str());
3265 
3266  // Selective pressure
3267 
3268  labels.push_back("Selective pressure");
3269 
3270  buffer.str("");
3271  buffer << selective_pressure;
3272 
3273  values.push_back(buffer.str());
3274 
3275  // Recombination size
3276 
3277  labels.push_back("Recombination size");
3278 
3279  buffer.str("");
3280  buffer << recombination_size;
3281 
3282  values.push_back(buffer.str());
3283 
3284  // Mutation rate
3285 
3286  labels.push_back("Mutation rate");
3287 
3288  buffer.str("");
3289  buffer << mutation_rate;
3290 
3291  values.push_back(buffer.str());
3292 
3293  // Mutation range
3294 
3295  labels.push_back("Mutation range");
3296 
3297  buffer.str("");
3298  buffer << mutation_range;
3299 
3300  values.push_back(buffer.str());
3301 
3302  // Best performance goal
3303 
3304  labels.push_back("Best performance goal");
3305 
3306  buffer.str("");
3307  buffer << best_performance_goal;
3308 
3309  values.push_back(buffer.str());
3310 
3311  // Maximum generalization failures
3312 
3313  labels.push_back("Maximum generalization failures");
3314 
3315  buffer.str("");
3317 
3318  values.push_back(buffer.str());
3319 
3320  // Maximum generations number
3321 
3322  labels.push_back("Maximum generations number");
3323 
3324  buffer.str("");
3325  buffer << maximum_generations_number;
3326 
3327  values.push_back(buffer.str());
3328 
3329  // Maximum time
3330 
3331  labels.push_back("Maximum time");
3332 
3333  buffer.str("");
3334  buffer << maximum_time;
3335 
3336  values.push_back(buffer.str());
3337 
3338  // Reserve generalization performance history
3339 
3340  labels.push_back("Reserve generalization performance history");
3341 
3342  buffer.str("");
3344 
3345  values.push_back(buffer.str());
3346 
3347  // Reserve elapsed time history
3348 
3349  labels.push_back("Reserve elapsed time history");
3350 
3351  buffer.str("");
3352  buffer << reserve_elapsed_time_history;
3353 
3354  values.push_back(buffer.str());
3355 
3356  const size_t rows_number = labels.size();
3357  const size_t columns_number = 2;
3358 
3359  Matrix<std::string> string_matrix(rows_number, columns_number);
3360 
3361  string_matrix.set_column(0, labels);
3362  string_matrix.set_column(1, values);
3363 
3364  return(string_matrix);
3365 }
3366 
3367 
3368 // tinyxml2::XMLDocument* to_XML(void) const method
3369 
3412 
3413 tinyxml2::XMLDocument* EvolutionaryAlgorithm::to_XML(void) const
3414 {
3415  std::ostringstream buffer;
3416 
3417  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
3418 
3419  // Evolutionary algorithm
3420 
3421  tinyxml2::XMLElement* root_element = document->NewElement("EvolutionaryAlgorithm");
3422 
3423  document->InsertFirstChild(root_element);
3424 
3425  tinyxml2::XMLElement* element = NULL;
3426  tinyxml2::XMLText* text = NULL;
3427 
3428  // Population
3429 
3430  element = document->NewElement("Population");
3431  root_element->LinkEndChild(element);
3432 
3433  const std::string population_string = population.to_string();
3434 
3435  text = document->NewText(population_string.c_str());
3436  element->LinkEndChild(text);
3437 
3438  // Fitness assignment method
3439 
3440  element = document->NewElement("FitnessAssignmentMethod");
3441  root_element->LinkEndChild(element);
3442 
3443  text = document->NewText(write_fitness_assignment_method().c_str());
3444  element->LinkEndChild(text);
3445 
3446  // Selection method
3447 
3448  element = document->NewElement("SelectionMethod");
3449  root_element->LinkEndChild(element);
3450 
3451  text = document->NewText(write_selection_method().c_str());
3452  element->LinkEndChild(text);
3453 
3454  // Recombination method
3455 
3456  element = document->NewElement("RecombinationMethod");
3457  root_element->LinkEndChild(element);
3458 
3459  text = document->NewText(write_recombination_method().c_str());
3460  element->LinkEndChild(text);
3461 
3462  // Mutation method
3463 
3464  element = document->NewElement("MutationMethod");
3465  root_element->LinkEndChild(element);
3466 
3467  text = document->NewText(write_mutation_method().c_str());
3468  element->LinkEndChild(text);
3469 
3470  // Elitism
3471 
3472  element = document->NewElement("ElitismSize");
3473  root_element->LinkEndChild(element);
3474 
3475  buffer.str("");
3476  buffer << elitism_size;
3477 
3478  text = document->NewText(buffer.str().c_str());
3479  element->LinkEndChild(text);
3480 
3481  // Selective pressure.
3482 
3483  element = document->NewElement("SelectivePressure");
3484  root_element->LinkEndChild(element);
3485 
3486  buffer.str("");
3487  buffer << selective_pressure;
3488 
3489  text = document->NewText(buffer.str().c_str());
3490  element->LinkEndChild(text);
3491 
3492  // Recombination size.
3493 
3494  element = document->NewElement("RecombinationSize");
3495  root_element->LinkEndChild(element);
3496 
3497  buffer.str("");
3498  buffer << recombination_size;
3499 
3500  text = document->NewText(buffer.str().c_str());
3501  element->LinkEndChild(text);
3502 
3503  // Mutation rate.
3504 
3505  element = document->NewElement("MutationRate");
3506  root_element->LinkEndChild(element);
3507 
3508  buffer.str("");
3509  buffer << mutation_rate;
3510 
3511  text = document->NewText(buffer.str().c_str());
3512  element->LinkEndChild(text);
3513 
3514  // Mutation range
3515 
3516  element = document->NewElement("MutationRange");
3517  root_element->LinkEndChild(element);
3518 
3519  buffer.str("");
3520  buffer << mutation_range;
3521 
3522  text = document->NewText(buffer.str().c_str());
3523  element->LinkEndChild(text);
3524 
3525  // Mean performance goal
3526 
3527  element = document->NewElement("MeanPerformanceGoal");
3528  root_element->LinkEndChild(element);
3529 
3530  buffer.str("");
3531  buffer << mean_performance_goal;
3532 
3533  text = document->NewText(buffer.str().c_str());
3534  element->LinkEndChild(text);
3535 
3536  // Standard deviation performance goal
3537 
3538  element = document->NewElement("StandardDeviationPerformanceGoal");
3539  root_element->LinkEndChild(element);
3540 
3541  buffer.str("");
3543 
3544  text = document->NewText(buffer.str().c_str());
3545  element->LinkEndChild(text);
3546 
3547  // Best performance goal
3548 
3549  element = document->NewElement("BestPerformanceGoal");
3550  root_element->LinkEndChild(element);
3551 
3552  buffer.str("");
3553  buffer << best_performance_goal;
3554 
3555  text = document->NewText(buffer.str().c_str());
3556  element->LinkEndChild(text);
3557 
3558  // Maximum generalization performance decreases
3559 
3560  element = document->NewElement("MaximumGeneralizationPerformanceDecreases");
3561  root_element->LinkEndChild(element);
3562 
3563  buffer.str("");
3565 
3566  text = document->NewText(buffer.str().c_str());
3567  element->LinkEndChild(text);
3568 
3569  // Maximum generations number
3570 
3571  element = document->NewElement("MaximumGenerationsNumber");
3572  root_element->LinkEndChild(element);
3573 
3574  buffer.str("");
3575  buffer << maximum_generations_number;
3576 
3577  text = document->NewText(buffer.str().c_str());
3578  element->LinkEndChild(text);
3579 
3580  // Maximum time
3581 
3582  element = document->NewElement("MaximumTime");
3583  root_element->LinkEndChild(element);
3584 
3585  buffer.str("");
3586  buffer << maximum_time;
3587 
3588  text = document->NewText(buffer.str().c_str());
3589  element->LinkEndChild(text);
3590 
3591  // Reserve mean norm history
3592 
3593  element = document->NewElement("ReserveMeanNormHistory");
3594  root_element->LinkEndChild(element);
3595 
3596  buffer.str("");
3597  buffer << reserve_mean_norm_history;
3598 
3599  text = document->NewText(buffer.str().c_str());
3600  element->LinkEndChild(text);
3601 
3602  // Reserve standard deviation norm history
3603 
3604  element = document->NewElement("ReserveStandardDeviationNormHistory");
3605  root_element->LinkEndChild(element);
3606 
3607  buffer.str("");
3609 
3610  text = document->NewText(buffer.str().c_str());
3611  element->LinkEndChild(text);
3612 
3613  // Reserve best norm history
3614 
3615  element = document->NewElement("ReserveBestNormHistory");
3616  root_element->LinkEndChild(element);
3617 
3618  buffer.str("");
3619  buffer << reserve_best_norm_history;
3620 
3621  text = document->NewText(buffer.str().c_str());
3622  element->LinkEndChild(text);
3623 
3624  // Reserve mean performance history
3625 
3626  element = document->NewElement("ReserveMeanPerformanceHistory");
3627  root_element->LinkEndChild(element);
3628 
3629  buffer.str("");
3631 
3632  text = document->NewText(buffer.str().c_str());
3633  element->LinkEndChild(text);
3634 
3635  // Reserve standard deviation performance history
3636 
3637  element = document->NewElement("ReserveStandardDeviationPerformanceHistory");
3638  root_element->LinkEndChild(element);
3639 
3640  buffer.str("");
3642 
3643  text = document->NewText(buffer.str().c_str());
3644  element->LinkEndChild(text);
3645 
3646  // Reserve best performance history
3647 
3648  element = document->NewElement("ReserveBestPerformanceHistory");
3649  root_element->LinkEndChild(element);
3650 
3651  buffer.str("");
3653 
3654  text = document->NewText(buffer.str().c_str());
3655  element->LinkEndChild(text);
3656 
3657  // Reserve generalization performance history
3658 
3659  element = document->NewElement("ReserveGeneralizationPerformanceHistory");
3660  root_element->LinkEndChild(element);
3661 
3662  buffer.str("");
3664 
3665  text = document->NewText(buffer.str().c_str());
3666  element->LinkEndChild(text);
3667 
3668  return(document);
3669 }
3670 
3671 
3672 // void from_XML(const tinyxml2::XMLDocument&) method
3673 
3677 
3678 void EvolutionaryAlgorithm::from_XML(const tinyxml2::XMLDocument& document)
3679 {
3680  const tinyxml2::XMLElement* root_element = document.FirstChildElement("EvolutionaryAlgorithm");
3681 
3682  if(!root_element)
3683  {
3684  std::ostringstream buffer;
3685 
3686  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
3687  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
3688  << "Evolutionary algorithm element is NULL.\n";
3689 
3690  throw std::logic_error(buffer.str());
3691  }
3692 
3693  set_default();
3694 
3695  // Population
3696  {
3697  const tinyxml2::XMLElement* element = root_element->FirstChildElement("Population");
3698 
3699  if(element)
3700  {
3701  const char* population_text = element->GetText();
3702 
3703  if(population_text)
3704  {
3705  Matrix<double> new_population;
3706  new_population.parse(population_text);
3707 
3708  const size_t new_population_size = new_population.get_rows_number();
3709 
3710  set_population_size(new_population_size);
3711 
3712  set_population(new_population);
3713  }
3714  }
3715  }
3716 
3717 
3718  // Fitness assignment method
3719  {
3720  const tinyxml2::XMLElement* element = root_element->FirstChildElement("FitnessAssignmentMethod");
3721 
3722  if(element)
3723  {
3724  const std::string new_fitness_assignment_method = element->GetText();
3725 
3726  try
3727  {
3728  set_fitness_assignment_method(new_fitness_assignment_method);
3729  }
3730  catch(const std::logic_error& e)
3731  {
3732  std::cout << e.what() << std::endl;
3733  }
3734  }
3735  }
3736 
3737  // Selection method
3738  {
3739  const tinyxml2::XMLElement* element = root_element->FirstChildElement("SelectionMethod");
3740 
3741  if(element)
3742  {
3743  const std::string new_selection_method = element->GetText();
3744 
3745  try
3746  {
3747  set_selection_method(new_selection_method);
3748  }
3749  catch(const std::logic_error& e)
3750  {
3751  std::cout << e.what() << std::endl;
3752  }
3753  }
3754  }
3755 
3756  // Recombination method
3757  {
3758  const tinyxml2::XMLElement* element = root_element->FirstChildElement("RecombinationMethod");
3759 
3760  if(element)
3761  {
3762  const std::string new_recombination_method = element->GetText();
3763 
3764  try
3765  {
3766  set_recombination_method(new_recombination_method);
3767  }
3768  catch(const std::logic_error& e)
3769  {
3770  std::cout << e.what() << std::endl;
3771  }
3772  }
3773  }
3774 
3775  // Mutation method
3776  {
3777  const tinyxml2::XMLElement* element = root_element->FirstChildElement("MutationMethod");
3778 
3779  if(element)
3780  {
3781  const std::string new_mutation_method = element->GetText();
3782 
3783  try
3784  {
3785  set_mutation_method(new_mutation_method);
3786  }
3787  catch(const std::logic_error& e)
3788  {
3789  std::cout << e.what() << std::endl;
3790  }
3791  }
3792  }
3793 
3794  // Elitism size
3795  {
3796  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ElitismSize");
3797 
3798  if(element)
3799  {
3800  const size_t new_elitism_size = atoi(element->GetText());
3801 
3802  try
3803  {
3804  set_elitism_size(new_elitism_size);
3805  }
3806  catch(const std::logic_error& e)
3807  {
3808  std::cout << e.what() << std::endl;
3809  }
3810  }
3811  }
3812 
3813  // Selective pressure
3814  {
3815  const tinyxml2::XMLElement* element = root_element->FirstChildElement("SelectivePressure");
3816 
3817  if(element)
3818  {
3819  const double new_selective_pressure = atof(element->GetText());
3820 
3821  try
3822  {
3823  set_selective_pressure(new_selective_pressure);
3824  }
3825  catch(const std::logic_error& e)
3826  {
3827  std::cout << e.what() << std::endl;
3828  }
3829  }
3830  }
3831 
3832  // Recombination size
3833  {
3834  const tinyxml2::XMLElement* element = root_element->FirstChildElement("RecombinationSize");
3835 
3836  if(element)
3837  {
3838  const double new_recombination_size = atof(element->GetText());
3839 
3840  try
3841  {
3842  set_recombination_size(new_recombination_size);
3843  }
3844  catch(const std::logic_error& e)
3845  {
3846  std::cout << e.what() << std::endl;
3847  }
3848  }
3849  }
3850 
3851  // Mutation rate
3852  {
3853  const tinyxml2::XMLElement* element = root_element->FirstChildElement("MutationRate");
3854 
3855  if(element)
3856  {
3857  const double new_mutation_rate = atof(element->GetText());
3858 
3859  try
3860  {
3861  set_mutation_rate(new_mutation_rate);
3862  }
3863  catch(const std::logic_error& e)
3864  {
3865  std::cout << e.what() << std::endl;
3866  }
3867  }
3868  }
3869 
3870  // Mutation range
3871  {
3872  const tinyxml2::XMLElement* element = root_element->FirstChildElement("MutationRange");
3873 
3874  if(element)
3875  {
3876  const double new_mutation_range = atof(element->GetText());
3877 
3878  try
3879  {
3880  set_mutation_range(new_mutation_range);
3881  }
3882  catch(const std::logic_error& e)
3883  {
3884  std::cout << e.what() << std::endl;
3885  }
3886  }
3887  }
3888 
3889  // Mean performance goal
3890  {
3891  const tinyxml2::XMLElement* element = root_element->FirstChildElement("MeanPerformanceGoal");
3892 
3893  if(element)
3894  {
3895  const double new_mean_performance_goal = atof(element->GetText());
3896 
3897  try
3898  {
3899  set_mean_performance_goal(new_mean_performance_goal);
3900  }
3901  catch(const std::logic_error& e)
3902  {
3903  std::cout << e.what() << std::endl;
3904  }
3905  }
3906  }
3907 
3908  // Standard deviation performance goal
3909  {
3910  const tinyxml2::XMLElement* element = root_element->FirstChildElement("StandardDeviationPerformanceGoal");
3911 
3912  if(element)
3913  {
3914  const double new_standard_deviation_performance_goal = atof(element->GetText());
3915 
3916  try
3917  {
3918  set_standard_deviation_performance_goal(new_standard_deviation_performance_goal);
3919  }
3920  catch(const std::logic_error& e)
3921  {
3922  std::cout << e.what() << std::endl;
3923  }
3924  }
3925  }
3926 
3927  // Best performance goal
3928  {
3929  const tinyxml2::XMLElement* element = root_element->FirstChildElement("BestPerformanceGoal");
3930 
3931  if(element)
3932  {
3933  const double new_best_performance_goal = atof(element->GetText());
3934 
3935  try
3936  {
3937  set_best_performance_goal(new_best_performance_goal);
3938  }
3939  catch(const std::logic_error& e)
3940  {
3941  std::cout << e.what() << std::endl;
3942  }
3943  }
3944  }
3945 
3946  // Maximum generalization performance decreases
3947  {
3948  const tinyxml2::XMLElement* element = root_element->FirstChildElement("MaximumGeneralizationPerformanceDecreases");
3949 
3950  if(element)
3951  {
3952  const size_t new_maximum_generalization_performance_decreases = atoi(element->GetText());
3953 
3954  try
3955  {
3956  set_maximum_generalization_performance_decreases(new_maximum_generalization_performance_decreases);
3957  }
3958  catch(const std::logic_error& e)
3959  {
3960  std::cout << e.what() << std::endl;
3961  }
3962  }
3963  }
3964 
3965  // Maximum generations number
3966  {
3967  const tinyxml2::XMLElement* element = root_element->FirstChildElement("MaximumGenerationsNumber");
3968 
3969  if(element)
3970  {
3971  const size_t new_maximum_generations_number = atoi(element->GetText());
3972 
3973  try
3974  {
3975  set_maximum_generations_number(new_maximum_generations_number);
3976  }
3977  catch(const std::logic_error& e)
3978  {
3979  std::cout << e.what() << std::endl;
3980  }
3981  }
3982  }
3983 
3984  // Maximum time
3985  {
3986  const tinyxml2::XMLElement* element = root_element->FirstChildElement("MaximumTime");
3987 
3988  if(element)
3989  {
3990  const double new_maximum_time = atof(element->GetText());
3991 
3992  try
3993  {
3994  set_maximum_time(new_maximum_time);
3995  }
3996  catch(const std::logic_error& e)
3997  {
3998  std::cout << e.what() << std::endl;
3999  }
4000  }
4001  }
4002 
4003  // Reserve mean norm history
4004  {
4005  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ReserveMeanNormHistory");
4006 
4007  if(element)
4008  {
4009  const bool new_mean_norm_history = (atoi(element->GetText()) != 0);
4010  set_reserve_mean_norm_history(new_mean_norm_history);
4011  }
4012  }
4013 
4014  // Reserve standard deviation norm history
4015  {
4016  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ReserveStandardDeviationNormHistory");
4017 
4018  if(element)
4019  {
4020  const bool new_standard_deviation_norm_history = (atoi(element->GetText()) != 0);
4021  set_reserve_standard_deviation_norm_history(new_standard_deviation_norm_history);
4022  }
4023  }
4024 
4025  // Reserve best norm history
4026  {
4027  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ReserveBestNormHistory");
4028 
4029  if(element)
4030  {
4031  const bool new_best_norm_history = (atoi(element->GetText()) != 0);
4032  set_reserve_best_norm_history(new_best_norm_history);
4033  }
4034  }
4035 
4036  // Reserve mean performance history
4037  {
4038  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ReserveMeanPerformanceHistory");
4039 
4040  if(element)
4041  {
4042  const bool new_mean_performance_history = (atoi(element->GetText()) != 0);
4043  set_reserve_mean_performance_history(new_mean_performance_history);
4044  }
4045  }
4046 
4047  // Reserve standard deviation performance history
4048  {
4049  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ReserveStandardDeviationPerformanceHistory");
4050 
4051  if(element)
4052  {
4053  const bool new_standard_deviation_performance_history = (atoi(element->GetText()) != 0);
4054  set_reserve_standard_deviation_performance_history(new_standard_deviation_performance_history);
4055  }
4056  }
4057 
4058  // Reserve best performance history
4059  {
4060  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ReserveBestPerformanceHistory");
4061 
4062  if(element)
4063  {
4064  const bool new_best_performance_history = (atoi(element->GetText()) != 0);
4065  set_reserve_best_performance_history(new_best_performance_history);
4066  }
4067  }
4068 
4069  // Reserve generalization performance history
4070  {
4071  const tinyxml2::XMLElement* element = root_element->FirstChildElement("ReserveGeneralizationPerformanceHistory");
4072 
4073  if(element)
4074  {
4075  const bool new_generalization_performance_history = (atoi(element->GetText()) != 0);
4076  set_reserve_generalization_performance_history(new_generalization_performance_history);
4077  }
4078  }
4079 }
4080 
4081 
4082 // void initialize_random(void) method
4083 
4085 {
4086  // Fitness assingment method
4087 
4088  fitness_assignment_method = LinearRanking;
4089 
4090  // Selection method
4091 
4092  selection_method = RouletteWheel;
4093 
4094  // Recombination method
4095 
4096  switch(rand()%2)
4097  {
4098  case 0:
4099  {
4100  recombination_method = Line;
4101  }
4102  break;
4103 
4104  case 1:
4105  {
4106  recombination_method = Intermediate;
4107  }
4108  break;
4109 
4110  default:
4111  {
4112  std::ostringstream buffer;
4113 
4114  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
4115  << "void initialize_random(void) method.\n"
4116  << "Unknown recombination method.\n";
4117 
4118  throw std::logic_error(buffer.str());
4119  }
4120  break;
4121  }
4122 
4123  // Mutation method
4124 
4125  switch(rand()%2)
4126  {
4127  case 0:
4128  {
4129  mutation_method = Normal;
4130  }
4131  break;
4132 
4133  case 1:
4134  {
4135  mutation_method = Uniform;
4136  }
4137  break;
4138 
4139  default:
4140  {
4141  std::ostringstream buffer;
4142 
4143  buffer << "OpenNN Exception: EvolutionaryAlgorithm class.\n"
4144  << "void initialize_random(void) method.\n"
4145  << "Unknown mutation method.\n";
4146 
4147  throw std::logic_error(buffer.str());
4148  }
4149  break;
4150  }
4151 
4152  const size_t new_population_size = (size_t)calculate_random_uniform(1.0, 11.0)*4;
4153 
4154  set_population_size(new_population_size);
4155 }
4156 
4157 }
4158 
4159 
4160 // OpenNN: Open Neural Networks Library.
4161 // Copyright (c) 2005-2015 Roberto Lopez.
4162 //
4163 // This library is free software; you can redistribute it and/or
4164 // modify it under the terms of the GNU Lesser General Public
4165 // License as published by the Free Software Foundation; either
4166 // version 2.1 of the License, or any later version.
4167 //
4168 // This library is distributed in the hope that it will be useful,
4169 // but WITHOUT ANY WARRANTY; without even the implied warranty of
4170 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4171 // Lesser General Public License for more details.
4172 
4173 // You should have received a copy of the GNU Lesser General Public
4174 // License along with this library; if not, write to the Free Software
4175 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Vector< Matrix< double > > population_history
History of the population matrix over the generations.
const SelectionMethod & get_selection_method(void) const
Returns the selection method used for training.
void set_reserve_mean_performance_history(const bool &)
virtual double calculate_generalization_performance(void) const
double final_best_performance
Performance of the best individual ever.
const double & get_mutation_range(void) const
Returns the mutation range value.
void set_maximum_generations_number(const size_t &)
void set_reserve_generalization_performance_history(const bool &)
const double & get_mean_performance_goal(void) const
Returns the mean performance value of the population at which training will stop. ...
size_t count_parameters_number(void) const
void randomize_uniform(const double &=-1.0, const double &=1.0)
Definition: vector.h:781
const double & get_mutation_rate(void) const
Returns the mutation rate value.
void set_selection(const Vector< bool > &)
const size_t & get_maximum_generations_number(void) const
Returns the maximum number of generations to train.
const double & get_best_performance_goal(void) const
Vector< double > calculate_population_norm(void) const
Returns a vector containing the norm of each individual in the population.
const bool & get_reserve_population_history(void) const
Returns true if the population history vector of matrices is to be reserved, and false otherwise...
size_t get_population_size(void) const
Returns the number of individuals in the population.
void set_reserve_all_training_history(const bool &)
const double & get_standard_deviation_performance_goal(void) const
Returns the standard deviation of the performance at which training will stop.
std::string write_training_algorithm_type(void) const
This method writes a string with the type of training algoritm.
void initialize(const T &)
Definition: vector.h:753
const bool & get_reserve_standard_deviation_norm_history(void) const
void evolve_population(void)
Generates a new population matrix by applying fitness assignment, selection, recombination and mutati...
const double & get_error_parameters_norm(void) const
bool reserve_standard_deviation_norm_history
True if the standard deviation of norm history vector is to be reserved, false otherwise.
tinyxml2::XMLDocument * to_XML(void) const
Vector< double > get_individual(const size_t &) const
void set_reserve_mean_norm_history(const bool &)
const RecombinationMethod & get_recombination_method(void) const
Returns the recombination method used for training.
void set_fitness(const Vector< double > &)
void set_standard_deviation_performance_goal(const double &)
void set_reserve_elapsed_time_history(const bool &)
const Vector< double > & get_performance(void) const
Returns the actual performance value of all individuals in the population.
void set(void)
Sets the size of a vector to zero.
Definition: vector.h:656
bool display
Display messages to screen.
NeuralNetwork * get_neural_network_pointer(void) const
Returns a pointer to the neural network associated to the performance functional. ...
Vector< size_t > calculate_maximal_indices(const size_t &) const
Definition: vector.h:1700
double final_mean_performance
Final mean population performance.
Vector< double > standard_deviation_norm_history
History of the standard deviation of the individuals norm over the generations.
void set_reserve_best_individual_history(const bool &)
void set_performance(const Vector< double > &)
void perform_recombination(void)
Recombinates the selected individuals according to the recombination operator.
void set_selection_method(const SelectionMethod &)
std::string to_string(const std::string &=" ") const
Definition: matrix.h:6575
bool reserve_generalization_performance_history
True if the Generalization performance history vector is to be reserved, false otherwise.
double maximum_time
Maximum training time. It is used as a stopping criterion.
double final_standard_deviation_performance
Final standard deviation of the population performance.
const FitnessAssignmentMethod & get_fitness_assignment_method(void) const
Returns the fitness assignment method used for training.
EvolutionaryAlgorithmResults * perform_training(void)
Vector< double > mean_norm_history
History of the mean norm of the individuals over the generations.
const bool & get_reserve_best_individual_history(void) const
Returns true if the best individual history vector of vectors is to be reserved, and false otherwise...
bool reserve_elapsed_time_history
True if the elapsed time history vector is to be reserved, false otherwise.
Matrix< std::string > to_string_matrix(void) const
void set_reserve_population_history(const bool &)
Vector< bool > selection
Selected individuals in population.
T calculate_minimum(void) const
Returns the smallest element in the vector.
Definition: vector.h:1242
std::string write_mutation_method(void) const
Returns a string with the name of the method used for mutation.
Vector< T > calculate_cumulative(void) const
Definition: vector.h:1893
Vector< double > mean_performance_history
History of the mean performance of the individuals over the generations.
const double & get_warning_parameters_norm(void) const
void set_population(const Matrix< double > &)
size_t save_period
Number of iterations between the training saving progress.
std::string to_string(void) const
Returns a string representation of the current evolutionary algorithm resutls structure.
const MutationMethod & get_mutation_method(void) const
Returns the mutation method used for training.
const size_t & get_columns_number(void) const
Returns the number of columns in the matrix.
Definition: matrix.h:1090
Vector< double > best_norm_history
History of the norm of the best individual over the generations.
void perform_mutation(void)
Mutates the population matrix according to the mutation operator.
virtual std::string write_information(void)
Vector< Vector< double > > performance_history
History of the population performance over the generations.
void set_mutation_method(const MutationMethod &)
void set_warning_parameters_norm(const double &)
Vector< double > generalization_performance_history
History of the generalization performance of the best individual over each generations.
bool reserve_standard_deviation_performance_history
True if the standard deviation of performance history vector is to be reserved, false otherwise...
double calculate_norm(void) const
Returns the vector norm.
Definition: vector.h:2358
size_t calculate_maximal_index(void) const
Returns the index of the largest element in the vector.
Definition: vector.h:1645
bool reserve_best_norm_history
True if the best norm history vector is to be reserved, false otherwise.
virtual ~EvolutionaryAlgorithm(void)
Destructor.
void set_individual(const size_t &, const Vector< double > &)
const double & get_recombination_size(void) const
Returns the recombination size value.
double elapsed_time
Total elapsed time in the training process.
void parse(const std::string &)
Definition: matrix.h:6513
std::string write_recombination_method(void) const
Returns a string with the name of the method used for recombination.
std::string neural_network_file_name
Path where the neural network is saved.
Vector< size_t > calculate_greater_rank(void) const
Definition: vector.h:3360
SelectionMethod selection_method
Selection training operators enumeration.
double warning_parameters_norm
Value for the parameters norm at which a warning message is written to the screen.
const double & get_maximum_time(void) const
Returns the maximum training time.
double calculate_standard_deviation(void) const
Returns the standard deviation of the elements in the vector.
Definition: vector.h:2102
const bool & get_reserve_best_performance_history(void) const
Returns true if the best performance history vector is to be reserved, and false otherwise.
double final_best_norm
Final norm of the best individual ever.
void set_column(const size_t &, const Vector< T > &)
Definition: matrix.h:1774
void initialize(const T &)
Definition: matrix.h:2510
const bool & get_reserve_best_norm_history(void) const
void set_maximum_generalization_performance_decreases(const size_t &)
MutationMethod mutation_method
Mutation training operators enumeration.
void set_error_parameters_norm(const double &)
double final_standard_deviation_norm
Final standard deviation of the population norm.
const double & get_selective_pressure(void) const
Returns the selective pressure value.
size_t calculate_minimal_index(void) const
Returns the index of the smallest element in the vector.
Definition: vector.h:1620
double calculate_standard_deviation_performance(void) const
Returns the standard deviation value of the individuals performance.
void set_fitness_assignment_method(const FitnessAssignmentMethod &)
bool reserve_best_performance_history
True if the best performance history vector is to be reserved, false otherwise.
Matrix< std::string > write_final_results(const size_t &precision=3) const
Returns a default (empty) string matrix with the final results from training.
void randomize_normal(const double &=0.0, const double &=1.0)
Definition: vector.h:867
Vector< double > performance
Performance of population.
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
std::string write_fitness_assignment_method(void) const
Returns a string with the name of the method used for fitness assignment.
size_t maximum_generations_number
Maximum number of generations to perform_training.
const bool & get_reserve_standard_deviation_performance_history(void) const
bool reserve_mean_norm_history
True if the mean norm history vector is to be reserved, false otherwise.
Vector< double > standard_deviation_performance_history
History of the standard deviation of the population performance over the generations.
void set_reserve_standard_deviation_performance_history(const bool &)
void set_recombination_method(const RecombinationMethod &)
void from_XML(const tinyxml2::XMLDocument &)
void save(const std::string &) const
virtual void check(void) const
void randomize_uniform(const double &=-1.0, const double &=1.0)
Definition: matrix.h:2524
void perform_selection(void)
Selects for recombination some individuals from the population according to the selection operator...
FitnessAssignmentMethod
Enumeration of the available training operators for fitness assignment.
double calculate_mean_performance(void) const
Returns the mean value of the individuals performance.
size_t generations_number
Number of generations needed by the evolutionary algorithm.
Vector< double > best_performance_history
History of the performance of the best individual over each generations.
double best_performance_goal
Best goal value for the performance. It is used as a stopping criterion.
const bool & get_reserve_elapsed_time_history(void) const
Returns true if the elapsed time history vector is to be reserved, and false otherwise.
Vector< double > fitness
Fitness of population.
Matrix< double > population
Population matrix.
SelectionMethod
Enumeration of the available training operators for selection.
FitnessAssignmentMethod fitness_assignment_method
Fitness assignment training operators enumeration.
std::string write_selection_method(void) const
Returns a string with the name of the method used for selection.
size_t calculate_best_individual_index(void) const
Returns the index of the individual with greatest fitness.
bool reserve_mean_performance_history
True if the mean performance history vector is to be reserved, false otherwise.
const Vector< bool > & get_selection(void) const
Returns the actual selection value of all individuals in the population.
const bool & get_reserve_mean_norm_history(void) const
Returns true if the mean population norm history vector is to be reserved, and false otherwise...
const bool & get_reserve_generalization_performance_history(void) const
Returns true if the Generalization performance history vector is to be reserved, and false otherwise...
void set_reserve_best_performance_history(const bool &)
double calculate_mean(void) const
Returns the mean of the elements in the vector.
Definition: vector.h:2068
const bool & get_reserve_mean_performance_history(void) const
Returns true if the mean performance history vector is to be reserved, and false otherwise.
Vector< double > elapsed_time_history
History of the elapsed time over the generations.
PerformanceFunctional * performance_functional_pointer
Pointer to a performance functional for a multilayer perceptron object.
const size_t & get_maximum_generalization_performance_decreases(void) const
Returns the maximum number of generalization failures during the training process.
bool empty(void) const
Returns true if number of rows and columns is zero.
Definition: matrix.h:5908
RecombinationMethod recombination_method
Recombination training operators enumeration.
MutationMethod
Enumeration of the available training operators for mutation.
void set_reserve_standard_deviation_norm_history(const bool &)
T calculate_sum(void) const
Returns the sum of the elements in the vector.
Definition: vector.h:2005
double final_generalization_performance
Generalization performance after training.
void set_row(const size_t &, const Vector< T > &)
Definition: matrix.h:1691
size_t display_period
Number of iterations between the training showing progress.
double error_parameters_norm
Value for the parameters norm at which the training process is assumed to fail.
void set_mean_performance_goal(const double &)
const Matrix< double > & get_population(void) const
Returns the population matrix.
Vector< T > arrange_row(const size_t &) const
Definition: matrix.h:1505
RecombinationMethod
Enumeration of the available training operators for recombination.
const size_t & get_elitism_size(void) const
Returns the number of individuals which will always be selected for recombination.
Vector< Vector< double > > best_individual_history
History of the best individual parameters over the generations.
void randomize_normal(const double &=0.0, const double &=1.0)
Definition: matrix.h:2593
const Vector< double > & get_fitness(void) const
Returns the actual fitness value of all individuals in the population.
size_t count_occurrences(const T &) const
Returns the number of times that a certain value is contained in the vector.
Definition: vector.h:1094
void perform_fitness_assignment(void)
Assigns a fitness value to all the individuals in the population according to the finess assignment o...
void set_best_performance_goal(const double &)
void set_parameters(const Vector< double > &)