genetic_algorithm.h
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// G E N E T I C A L G O R I T H M C L A S S H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#ifndef GENETICALGORITHM_H
10#define GENETICALGORITHM_H
11
12// System includes
13
14#include <algorithm>
15#include <cmath>
16#include <ctime>
17#include <fstream>
18#include <functional>
19#include <iostream>
20#include <limits>
21#include <numeric>
22
23// OpenNN includes
24
25#include "training_strategy.h"
26#include "tensor_utilities.h"
27#include "inputs_selection.h"
28#include "config.h"
29
30namespace OpenNN
31{
34
42
44{
45
46public:
47
48 // Constructors
49
50 explicit GeneticAlgorithm();
51
53
54 // Destructor
55
56 virtual ~GeneticAlgorithm();
57
58 // Get methods
59
60 const Tensor<bool, 2>& get_population() const;
61
62 const Tensor<type, 1>& get_fitness() const;
63 const Tensor<bool, 1>& get_selection() const;
64
65 Index get_individuals_number() const;
66 Index get_genes_number() const;
67
68 const type& get_mutation_rate() const;
69
70 const Index& get_elitism_size() const;
71
72 // Set methods
73
74 void set_default();
75
76 void set_population(const Tensor<bool, 2>&);
77 void set_individuals_number(const Index&);
78
79 void set_training_errors(const Tensor<type, 1>&);
80 void set_selection_errors(const Tensor<type, 1>&);
81
82 void set_fitness(const Tensor<type, 1>&);
83
84 void set_mutation_rate(const type&);
85
86 void set_elitism_size(const Index&);
87
88 // GENETIC METHODS
89
90 // Population methods
91
93
95
97
98 // Selection methods
99
100 void perform_selection();
101
102 // Crossover methods
103
104 void perform_crossover();
105
106 // Mutation methods
107
108 void perform_mutation();
109
110 // Inputs selection methods
111
113
114 // Serialization methods
115
116 Tensor<string, 2> to_string_matrix() const;
117
118 void from_XML(const tinyxml2::XMLDocument&);
119
120 void write_XML(tinyxml2::XMLPrinter&) const;
121
122 void print() const;
123
124 void save(const string&) const;
125 void load(const string&);
126
127private:
128
129
131
132 Tensor<bool, 2> population;
133
135
136 Tensor<type, 1> fitness;
137
138 Tensor<bool, 1> selection;
139
141
142 Tensor<Tensor<type, 1>, 1> parameters;
143
144 Tensor<type, 1> training_errors;
145 Tensor<type, 1> selection_errors;
146
150
152
156
158};
159
160}
161
162#endif
Tensor< bool, 2 > population
Population matrix.
void set_individuals_number(const Index &)
void set_fitness(const Tensor< type, 1 > &)
void from_XML(const tinyxml2::XMLDocument &)
void set_default()
Sets the members of the genetic algorithm object to their default values.
void set_elitism_size(const Index &)
Tensor< Tensor< type, 1 >, 1 > parameters
Performance of population.
virtual ~GeneticAlgorithm()
Destructor.
Tensor< string, 2 > to_string_matrix() const
InputsSelectionResults perform_inputs_selection()
Select the inputs with best generalization properties using the genetic algorithm.
GeneticAlgorithm()
Default constructor.
const Index & get_elitism_size() const
Returns the size of the elite in the selection.
void perform_fitness_assignment()
Calculate the fitness with the errors depending on the fitness assignment method.
void perform_selection()
Selects for crossover some individuals from the population.
void perform_mutation()
Perform the mutation of the individuals generated in the crossover.
void save(const string &) const
Index get_individuals_number() const
Returns the size of the population.
void set_mutation_rate(const type &)
void write_XML(tinyxml2::XMLPrinter &) const
const type & get_mutation_rate() const
Returns the rate used in the mutation.
const Tensor< type, 1 > & get_fitness() const
Returns the fitness of the population.
const Tensor< bool, 2 > & get_population() const
Returns the population matrix.
Tensor< type, 1 > fitness
Fitness of population.
void set_population(const Tensor< bool, 2 > &)
void initialize_population()
Initialize the population depending on the intialization method.
void perform_crossover()
Perform the crossover depending on the crossover method.
This abstract class represents the concept of inputs selection algorithm for a ModelSelection[1].
This class represents the concept of training strategy for a neural network in OpenNN.
This structure contains the results from the inputs selection.