#include <genetic_algorithm.h>
Public Member Functions | |
GeneticAlgorithm () | |
Default constructor. More... | |
GeneticAlgorithm (TrainingStrategy *) | |
virtual | ~GeneticAlgorithm () |
Destructor. More... | |
const Tensor< bool, 2 > & | get_population () const |
Returns the population matrix. More... | |
const Tensor< type, 1 > & | get_fitness () const |
Returns the fitness of the population. More... | |
const Tensor< bool, 1 > & | get_selection () const |
Index | get_individuals_number () const |
Returns the size of the population. More... | |
Index | get_genes_number () const |
const type & | get_mutation_rate () const |
Returns the rate used in the mutation. More... | |
const Index & | get_elitism_size () const |
Returns the size of the elite in the selection. More... | |
void | set_default () |
Sets the members of the genetic algorithm object to their default values. More... | |
void | set_population (const Tensor< bool, 2 > &) |
void | set_individuals_number (const Index &) |
void | set_training_errors (const Tensor< type, 1 > &) |
void | set_selection_errors (const Tensor< type, 1 > &) |
void | set_fitness (const Tensor< type, 1 > &) |
void | set_mutation_rate (const type &) |
void | set_elitism_size (const Index &) |
void | initialize_population () |
Initialize the population depending on the intialization method. More... | |
void | evaluate_population () |
void | perform_fitness_assignment () |
Calculate the fitness with the errors depending on the fitness assignment method. More... | |
void | perform_selection () |
Selects for crossover some individuals from the population. More... | |
void | perform_crossover () |
Perform the crossover depending on the crossover method. More... | |
void | perform_mutation () |
Perform the mutation of the individuals generated in the crossover. More... | |
InputsSelectionResults | perform_inputs_selection () |
Select the inputs with best generalization properties using the genetic algorithm. More... | |
Tensor< string, 2 > | to_string_matrix () const |
void | from_XML (const tinyxml2::XMLDocument &) |
void | write_XML (tinyxml2::XMLPrinter &) const |
void | print () const |
void | save (const string &) const |
void | load (const string &) |
Public Member Functions inherited from InputsSelection | |
InputsSelection () | |
Default constructor. More... | |
InputsSelection (TrainingStrategy *) | |
virtual | ~InputsSelection () |
Destructor. More... | |
TrainingStrategy * | get_training_strategy_pointer () const |
Returns a pointer to the training strategy object. More... | |
bool | has_training_strategy () const |
Returns true if this inputs selection algorithm has a training strategy associated, and false otherwise. More... | |
const Index & | get_trials_number () const |
Returns the number of trials for each network architecture. More... | |
const bool & | get_display () const |
const type & | get_selection_error_goal () const |
Returns the goal for the selection error in the inputs selection algorithm. More... | |
const Index & | get_maximum_iterations_number () const |
Returns the maximum number of iterations in the inputs selection algorithm. More... | |
const type & | get_maximum_time () const |
Returns the maximum time in the inputs selection algorithm. More... | |
const type & | get_maximum_correlation () const |
Return the maximum correlation for the algorithm. More... | |
const type & | get_minimum_correlation () const |
Return the minimum correlation for the algorithm. More... | |
const type & | get_tolerance () const |
void | set (TrainingStrategy *) |
void | set_default () |
Sets the members of the inputs selection object to their default values. More... | |
void | set_trials_number (const Index &) |
void | set_display (const bool &) |
void | set_selection_error_goal (const type &) |
void | set_maximum_epochs_number (const Index &) |
void | set_maximum_time (const type &) |
void | set_maximum_correlation (const type &) |
void | set_minimum_correlation (const type &) |
string | write_stopping_condition (const TrainingResults &) const |
void | check () const |
Checks that the different pointers needed for performing the inputs selection are not nullptr. More... | |
Index | get_input_index (const Tensor< DataSet::VariableUse, 1 > &, const Index &) |
const string | write_time (const type &) const |
Writes the time from seconds in format HH:mm:ss. More... | |
Private Attributes | |
Tensor< bool, 2 > | population |
Population matrix. More... | |
Tensor< type, 1 > | fitness |
Fitness of population. More... | |
Tensor< bool, 1 > | selection |
Tensor< Tensor< type, 1 >, 1 > | parameters |
Performance of population. More... | |
Tensor< type, 1 > | training_errors |
Tensor< type, 1 > | selection_errors |
type | mutation_rate |
Index | elitism_size |
Additional Inherited Members | |
Public Types inherited from InputsSelection | |
enum class | StoppingCondition { MaximumTime , SelectionErrorGoal , MaximumInputs , MinimumInputs , MaximumEpochs , MaximumSelectionFailures , CorrelationGoal } |
Enumeration of all possibles condition of stop for the algorithms. More... | |
Protected Attributes inherited from InputsSelection | |
TrainingStrategy * | training_strategy_pointer = nullptr |
Pointer to a training strategy object. More... | |
Tensor< Index, 1 > | original_input_columns_indices |
Tensor< Index, 1 > | original_target_columns_indices |
Index | trials_number = 1 |
Number of trials for each neural network. More... | |
bool | display = true |
Display messages to screen. More... | |
type | selection_error_goal |
Goal value for the selection error. It is used as a stopping criterion. More... | |
Index | maximum_epochs_number |
Maximum number of epochs to perform_inputs_selection. It is used as a stopping criterion. More... | |
type | maximum_correlation |
Maximum value for the correlations. More... | |
type | minimum_correlation |
Minimum value for the correlations. More... | |
type | maximum_time |
Maximum selection algorithm time. It is used as a stopping criterion. More... | |
const Eigen::array< int, 1 > | rows_sum = {Eigen::array<int, 1>({1})} |
This concrete class represents a genetic algorithm, inspired by the process of natural selection[1] such as mutation, crossover and selection. This algorithm are commonly used in optimization and search problems. if the data_set has many inputs, but we do not know how they affect the target, then this algorithm provides the best possible combination of variables to optimize the problem.
[1] Neural Designer "Genetic Algorithms for Feature Selection." https://www.neuraldesigner.com/blog/genetic_algorithms_for_feature_selection
Definition at line 43 of file genetic_algorithm.h.
|
explicit |
Default constructor.
Definition at line 16 of file genetic_algorithm.cpp.
|
explicit |
Training strategy constructor.
new_training_strategy_pointer | Pointer to a training strategy object. |
Definition at line 26 of file genetic_algorithm.cpp.
|
virtual |
Destructor.
Definition at line 35 of file genetic_algorithm.cpp.
void evaluate_population | ( | ) |
Evaluate the population loss. Training all the neural networks in the population and calculate their fitness.
Definition at line 386 of file genetic_algorithm.cpp.
void from_XML | ( | const tinyxml2::XMLDocument & | document | ) |
Deserializes a TinyXML document into this genetic algorithm object.
document | TinyXML document containing the member data. |
Definition at line 1079 of file genetic_algorithm.cpp.
const Index & get_elitism_size | ( | ) | const |
Returns the size of the elite in the selection.
Definition at line 86 of file genetic_algorithm.cpp.
const Tensor< type, 1 > & get_fitness | ( | ) | const |
Returns the fitness of the population.
Definition at line 50 of file genetic_algorithm.cpp.
Index get_genes_number | ( | ) | const |
Definition at line 70 of file genetic_algorithm.cpp.
Index get_individuals_number | ( | ) | const |
Returns the size of the population.
Definition at line 64 of file genetic_algorithm.cpp.
const type & get_mutation_rate | ( | ) | const |
Returns the rate used in the mutation.
Definition at line 78 of file genetic_algorithm.cpp.
const Tensor< bool, 2 > & get_population | ( | ) | const |
Returns the population matrix.
Definition at line 42 of file genetic_algorithm.cpp.
const Tensor< bool, 1 > & get_selection | ( | ) | const |
Definition at line 56 of file genetic_algorithm.cpp.
void initialize_population | ( | ) |
Initialize the population depending on the intialization method.
Definition at line 338 of file genetic_algorithm.cpp.
void load | ( | const string & | file_name | ) |
Loads a genetic algorithm object from a XML-type file.
file_name | Name of genetic algorithm XML-type file. |
Definition at line 1293 of file genetic_algorithm.cpp.
void perform_crossover | ( | ) |
Perform the crossover depending on the crossover method.
Definition at line 594 of file genetic_algorithm.cpp.
void perform_fitness_assignment | ( | ) |
Calculate the fitness with the errors depending on the fitness assignment method.
Definition at line 489 of file genetic_algorithm.cpp.
|
virtual |
Select the inputs with best generalization properties using the genetic algorithm.
Implements InputsSelection.
Definition at line 775 of file genetic_algorithm.cpp.
void perform_mutation | ( | ) |
Perform the mutation of the individuals generated in the crossover.
Definition at line 742 of file genetic_algorithm.cpp.
void perform_selection | ( | ) |
Selects for crossover some individuals from the population.
Definition at line 504 of file genetic_algorithm.cpp.
void print | ( | ) | const |
Definition at line 1267 of file genetic_algorithm.cpp.
void save | ( | const string & | file_name | ) | const |
Saves to a XML-type file the members of the genetic algorithm object.
file_name | Name of genetic algorithm XML-type file. |
Definition at line 1278 of file genetic_algorithm.cpp.
void set_default | ( | ) |
Sets the members of the genetic algorithm object to their default values.
Definition at line 94 of file genetic_algorithm.cpp.
void set_elitism_size | ( | const Index & | new_elitism_size | ) |
Sets the number of individuals with the greatest fitness selected.
new_elitism_size | Size of the elitism. |
Definition at line 312 of file genetic_algorithm.cpp.
void set_fitness | ( | const Tensor< type, 1 > & | new_fitness | ) |
Sets a new fitness for the population.
new_fitness | New fitness values. |
Definition at line 208 of file genetic_algorithm.cpp.
void set_individuals_number | ( | const Index & | new_individuals_number | ) |
Sets a new population size. It must be greater than 4.
new_population_size | Size of the population. |
Definition at line 249 of file genetic_algorithm.cpp.
void set_mutation_rate | ( | const type & | new_mutation_rate | ) |
Sets a new rate used in the mutation. It is a number between 0 and 1.
new_mutation_rate | Rate used for the mutation. |
Definition at line 288 of file genetic_algorithm.cpp.
void set_population | ( | const Tensor< bool, 2 > & | new_population | ) |
Sets a new popualtion.
new_population | New population matrix. |
Definition at line 129 of file genetic_algorithm.cpp.
void set_selection_errors | ( | const Tensor< type, 1 > & | new_selection_errors | ) |
Definition at line 199 of file genetic_algorithm.cpp.
void set_training_errors | ( | const Tensor< type, 1 > & | new_training_errors | ) |
Definition at line 193 of file genetic_algorithm.cpp.
Tensor< string, 2 > to_string_matrix | ( | ) | const |
Writes as matrix of strings the most representative atributes.
Definition at line 946 of file genetic_algorithm.cpp.
void write_XML | ( | tinyxml2::XMLPrinter & | file_stream | ) | const |
Serializes the genetic algorithm object into a XML document of the TinyXML library without keep the DOM tree in memory. See the OpenNN manual for more information about the format of this document.
Definition at line 998 of file genetic_algorithm.cpp.
|
private |
Elitism size. It represents the number of individuals which will always be selected for recombination. This is a parameter of the selection operator.
Definition at line 157 of file genetic_algorithm.h.
|
private |
Fitness of population.
Definition at line 136 of file genetic_algorithm.h.
|
private |
Mutation rate. The mutation rate value must be between 0 and 1. This is a parameter of the mutation operator.
Definition at line 151 of file genetic_algorithm.h.
|
private |
Performance of population.
Definition at line 142 of file genetic_algorithm.h.
|
private |
Population matrix.
Definition at line 132 of file genetic_algorithm.h.
|
private |
Definition at line 138 of file genetic_algorithm.h.
|
private |
Definition at line 145 of file genetic_algorithm.h.
|
private |
Definition at line 144 of file genetic_algorithm.h.