OpenNN  2.2
Open Neural Networks Library
evolutionary_algorithm.h
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 H E A D E R */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 #ifndef __EVOLUTIONARYALGORITHM_H__
15 #define __EVOLUTIONARYALGORITHM_H__
16 
17 // System includes
18 
19 #include <string>
20 #include <sstream>
21 #include <iostream>
22 #include <fstream>
23 #include <algorithm>
24 #include <functional>
25 #include <cmath>
26 #include <time.h>
27 
28 // OpenNN includes
29 
30 #include "training_algorithm.h"
31 #include "performance_functional.h"
32 
33 namespace OpenNN
34 {
35 
39 
41 {
42 
43 public:
44 
45  // ENUMERATIONS
46 
48 
49  enum FitnessAssignmentMethod{LinearRanking};
50 
52 
53  enum SelectionMethod{RouletteWheel};
54 
56 
57  enum RecombinationMethod{Line, Intermediate};
58 
60 
61  enum MutationMethod{Normal, Uniform};
62 
63 
64  // DEFAULT CONSTRUCTOR
65 
66  explicit EvolutionaryAlgorithm(void);
67 
68  // GENERAL CONSTRUCTOR
69 
71 
72  // XML CONSTRUCTOR
73 
74  explicit EvolutionaryAlgorithm(const tinyxml2::XMLDocument&);
75 
76 
77  // DESTRUCTOR
78 
79  virtual ~EvolutionaryAlgorithm(void);
80 
81  // STRUCTURES
82 
86 
88  {
90 
92  {
94  }
95 
97 
98  EvolutionaryAlgorithmResults(EvolutionaryAlgorithm* new_evolutionary_algorithm_pointer)
99  {
100  evolutionary_algorithm_pointer = new_evolutionary_algorithm_pointer;
101  }
102 
104 
106  {
107  }
108 
110 
112 
113  // Training history
114 
116 
118 
120 
122 
124 
126 
128 
130 
132 
134 
136 
138 
140 
142 
144 
146 
148 
150 
152 
154 
156 
158 
159  // Final values
160 
162 
164 
166 
168 
170 
172 
174 
176 
178 
180 
182 
184 
186 
188 
190 
191  double elapsed_time;
192 
194 
196 
197  void resize_training_history(const size_t&);
198  std::string to_string(void) const;
199  Matrix<std::string> write_final_results(const size_t& precision = 3) const;
200  };
201 
202 
203  // METHODS
204 
205  // Get methods
206 
207  // Training parameters
208 
209  const double& get_warning_parameters_norm(void) const;
210 
211  const double& get_error_parameters_norm(void) const;
212 
213  // Stopping criteria
214 
215  const double& get_best_performance_goal(void) const;
216  const size_t& get_maximum_generalization_performance_decreases(void) const;
217 
218  const size_t& get_maximum_generations_number(void) const;
219  const double& get_maximum_time(void) const;
220 
221  // Reserve training history
222 
223  const bool& get_reserve_generalization_performance_history(void) const;
224 
225  const bool& get_reserve_elapsed_time_history(void) const;
226 
227  // Population methods
228 
229  size_t get_population_size(void) const;
230 
231  const Matrix<double>& get_population(void) const;
232 
233  // Training operators
234 
236  std::string write_fitness_assignment_method(void) const;
237 
238  const SelectionMethod& get_selection_method(void) const;
239  std::string write_selection_method(void) const;
240 
242  std::string write_recombination_method(void) const;
243 
244  const MutationMethod& get_mutation_method(void) const;
245  std::string write_mutation_method(void) const;
246 
247  // Population values
248 
249  const Vector<double>& get_performance(void) const;
250  const Vector<double>& get_fitness(void) const;
251  const Vector<bool>& get_selection(void) const;
252 
253  const size_t& get_elitism_size(void) const;
254  const double& get_selective_pressure(void) const;
255 
256  const double& get_recombination_size(void) const;
257  const double& get_mutation_rate(void) const;
258  const double& get_mutation_range(void) const;
259  const double& get_mean_performance_goal(void) const;
260  const double& get_standard_deviation_performance_goal(void) const;
261 
262  const bool& get_reserve_population_history(void) const;
263  const bool& get_reserve_best_individual_history(void) const;
264  const bool& get_reserve_mean_norm_history(void) const;
265  const bool& get_reserve_standard_deviation_norm_history(void) const;
266  const bool& get_reserve_best_norm_history(void) const;
267 
268  const bool& get_reserve_mean_performance_history(void) const;
270  const bool& get_reserve_best_performance_history(void) const;
271 
272  // Set methods
273 
274  void set(void);
275  void set(PerformanceFunctional*);
276 
277  void set_default(void);
278 
280  void set_fitness_assignment_method(const std::string&);
281 
283  void set_selection_method(const std::string&);
284 
286  void set_recombination_method(const std::string&);
287 
288  void set_mutation_method(const MutationMethod&);
289  void set_mutation_method(const std::string&);
290 
291  void set_population_size(const size_t&);
292 
293  void set_population(const Matrix<double>&);
294 
295  void set_performance(const Vector<double>&);
296  void set_fitness(const Vector<double>&);
297  void set_selection(const Vector<bool>&);
298 
299  void set_elitism_size(const size_t&);
300  void set_selective_pressure(const double&);
301  void set_recombination_size(const double&);
302 
303  void set_mutation_rate(const double&);
304  void set_mutation_range(const double&);
305 
306  void set_maximum_generations_number(const size_t&);
307  void set_mean_performance_goal(const double&);
308  void set_standard_deviation_performance_goal(const double&);
309 
310  void set_reserve_population_history(const bool&);
311 
312  void set_reserve_best_individual_history(const bool&);
313 
314  void set_reserve_mean_norm_history(const bool&);
316  void set_reserve_best_norm_history(const bool&);
317 
318  void set_reserve_mean_performance_history(const bool&);
320  void set_reserve_best_performance_history(const bool&);
321 
322  void set_reserve_all_training_history(const bool&);
323 
324  // Training parameters
325 
326  void set_warning_parameters_norm(const double&);
327 
328  void set_error_parameters_norm(const double&);
329 
330  // Stopping criteria
331 
332  void set_best_performance_goal(const double&);
334 
335  void set_maximum_time(const double&);
336 
337  // Reserve training history
338 
340 
341  void set_reserve_elapsed_time_history(const bool&);
342 
343  // Utilities
344 
345  void set_display_period(const size_t&);
346 
347  // Population methods
348 
349  Vector<double> get_individual(const size_t&) const;
350  void set_individual(const size_t&, const Vector<double>&);
351 
352  size_t calculate_best_individual_index(void) const;
353 
354  double calculate_mean_performance(void) const;
355  double calculate_standard_deviation_performance(void) const;
356 
357  // Initialization methods
358 
359  void initialize_population(const double&);
360 
361  void randomize_population_uniform(void);
362  void randomize_population_uniform(const double&, const double&);
364 
365  void randomize_population_normal(void);
366  void randomize_population_normal(const double&, const double&);
368 
369  // Population norm methods
370 
372 
373  // Population performance methods
374 
375  void perform_fitness_assignment(void);
376  void perform_selection(void);
377  void perform_recombination(void);
378  void perform_mutation(void);
379 
380  void evolve_population(void);
381 
382  void evaluate_population(void);
383 
384  // Fitness assignment methods
385 
387 
388  // Selection methods
389 
391 
392  // Recombination methods
393 
395  void perform_line_recombination(void);
396 
397  // Mutation methods
398 
399  void perform_normal_mutation(void);
400  void perform_uniform_mutation(void);
401 
402  // Training methods
403 
405 
406  std::string write_training_algorithm_type(void) const;
407 
408  // Serialization methods
409 
411 
412  tinyxml2::XMLDocument* to_XML(void) const;
413  void from_XML(const tinyxml2::XMLDocument&);
414 
415  void initialize_random(void);
416 
417 private:
418 
419  // MEMBERS
420 
421  // Population stuff
422 
424 
426 
428 
430 
432 
434 
436 
438 
439  // Training operators
440 
442 
444 
446 
448 
450 
452 
454 
456 
460 
461  size_t elitism_size;
462 
466 
468 
472 
474 
478 
480 
484 
486 
488 
490 
492 
494 
495 
496  // STOPPING CRITERIA
497 
500 
502 
505 
507 
509 
511 
513 
515 
518 
520 
522 
523  double maximum_time;
524 
525  // Training history
526 
530 
532 
535 
537 
539 
541 
543 
545 
547 
549 
551 
553 
555 
557 
559 
561 
563 
565 
567 
569 };
570 
571 }
572 
573 #endif
574 
575 
576 // OpenNN: An Open Source Neural Networks C++ OpenNN.
577 // Copyright (c) 2005-2015 Roberto Lopez.
578 //
579 // This OpenNN is free software; you can redistribute it and/or
580 // modify it under the terms of the GNU Lesser General Public
581 // License as published by the Free Software Foundation; either
582 // version 2.1 of the License, or any later version.
583 //
584 // This OpenNN is distributed in the hope that it will be useful,
585 // but WITHOUT ANY WARRANTY; without even the implied warranty of
586 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
587 // Lesser General Public License for more details.
588 
589 // You should have received a copy of the GNU Lesser General Public
590 // License along with this OpenNN; if not, write to the Free Software
591 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
592 
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 &)
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. ...
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.
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.
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 &)
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.
EvolutionaryAlgorithm * evolutionary_algorithm_pointer
Pointer to the evolutionary algorithm object for which the training results are to be stored...
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.
std::string write_mutation_method(void) const
Returns a string with the name of the method used for mutation.
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 > &)
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.
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.
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...
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.
std::string write_recombination_method(void) const
Returns a string with the name of the method used for recombination.
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.
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.
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.
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.
Vector< double > performance
Performance of population.
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 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 &)
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.
const size_t & get_maximum_generalization_performance_decreases(void) const
Returns the maximum number of generalization failures during the training process.
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 &)
double final_generalization_performance
Generalization performance after training.
EvolutionaryAlgorithmResults(EvolutionaryAlgorithm *new_evolutionary_algorithm_pointer)
Evolutionary algorithm constructor.
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.
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.
const Vector< double > & get_fitness(void) const
Returns the actual fitness value of all individuals in the population.
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 &)