GeneticAlgorithm Class Reference

#include <genetic_algorithm.h>

Inheritance diagram for GeneticAlgorithm:
InputsSelection

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...
 
TrainingStrategyget_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
TrainingStrategytraining_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})}
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ GeneticAlgorithm() [1/2]

GeneticAlgorithm ( )
explicit

Default constructor.

Definition at line 16 of file genetic_algorithm.cpp.

◆ GeneticAlgorithm() [2/2]

GeneticAlgorithm ( TrainingStrategy new_training_strategy_pointer)
explicit

Training strategy constructor.

Parameters
new_training_strategy_pointerPointer to a training strategy object.

Definition at line 26 of file genetic_algorithm.cpp.

◆ ~GeneticAlgorithm()

~GeneticAlgorithm ( )
virtual

Destructor.

Definition at line 35 of file genetic_algorithm.cpp.

Member Function Documentation

◆ evaluate_population()

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.

◆ from_XML()

void from_XML ( const tinyxml2::XMLDocument document)

Deserializes a TinyXML document into this genetic algorithm object.

Parameters
documentTinyXML document containing the member data.

Definition at line 1079 of file genetic_algorithm.cpp.

◆ get_elitism_size()

const Index & get_elitism_size ( ) const

Returns the size of the elite in the selection.

Definition at line 86 of file genetic_algorithm.cpp.

◆ get_fitness()

const Tensor< type, 1 > & get_fitness ( ) const

Returns the fitness of the population.

Definition at line 50 of file genetic_algorithm.cpp.

◆ get_genes_number()

Index get_genes_number ( ) const

Definition at line 70 of file genetic_algorithm.cpp.

◆ get_individuals_number()

Index get_individuals_number ( ) const

Returns the size of the population.

Definition at line 64 of file genetic_algorithm.cpp.

◆ get_mutation_rate()

const type & get_mutation_rate ( ) const

Returns the rate used in the mutation.

Definition at line 78 of file genetic_algorithm.cpp.

◆ get_population()

const Tensor< bool, 2 > & get_population ( ) const

Returns the population matrix.

Definition at line 42 of file genetic_algorithm.cpp.

◆ get_selection()

const Tensor< bool, 1 > & get_selection ( ) const

Definition at line 56 of file genetic_algorithm.cpp.

◆ initialize_population()

void initialize_population ( )

Initialize the population depending on the intialization method.

Definition at line 338 of file genetic_algorithm.cpp.

◆ load()

void load ( const string &  file_name)

Loads a genetic algorithm object from a XML-type file.

Parameters
file_nameName of genetic algorithm XML-type file.

Definition at line 1293 of file genetic_algorithm.cpp.

◆ perform_crossover()

void perform_crossover ( )

Perform the crossover depending on the crossover method.

Definition at line 594 of file genetic_algorithm.cpp.

◆ perform_fitness_assignment()

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.

◆ perform_inputs_selection()

InputsSelectionResults perform_inputs_selection ( )
virtual

Select the inputs with best generalization properties using the genetic algorithm.

Implements InputsSelection.

Definition at line 775 of file genetic_algorithm.cpp.

◆ perform_mutation()

void perform_mutation ( )

Perform the mutation of the individuals generated in the crossover.

Definition at line 742 of file genetic_algorithm.cpp.

◆ perform_selection()

void perform_selection ( )

Selects for crossover some individuals from the population.

Definition at line 504 of file genetic_algorithm.cpp.

◆ print()

void print ( ) const

Definition at line 1267 of file genetic_algorithm.cpp.

◆ save()

void save ( const string &  file_name) const

Saves to a XML-type file the members of the genetic algorithm object.

Parameters
file_nameName of genetic algorithm XML-type file.

Definition at line 1278 of file genetic_algorithm.cpp.

◆ set_default()

void set_default ( )

Sets the members of the genetic algorithm object to their default values.

Definition at line 94 of file genetic_algorithm.cpp.

◆ set_elitism_size()

void set_elitism_size ( const Index &  new_elitism_size)

Sets the number of individuals with the greatest fitness selected.

Parameters
new_elitism_sizeSize of the elitism.

Definition at line 312 of file genetic_algorithm.cpp.

◆ set_fitness()

void set_fitness ( const Tensor< type, 1 > &  new_fitness)

Sets a new fitness for the population.

Parameters
new_fitnessNew fitness values.

Definition at line 208 of file genetic_algorithm.cpp.

◆ set_individuals_number()

void set_individuals_number ( const Index &  new_individuals_number)

Sets a new population size. It must be greater than 4.

Parameters
new_population_sizeSize of the population.

Definition at line 249 of file genetic_algorithm.cpp.

◆ set_mutation_rate()

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.

Parameters
new_mutation_rateRate used for the mutation.

Definition at line 288 of file genetic_algorithm.cpp.

◆ set_population()

void set_population ( const Tensor< bool, 2 > &  new_population)

Sets a new popualtion.

Parameters
new_populationNew population matrix.

Definition at line 129 of file genetic_algorithm.cpp.

◆ set_selection_errors()

void set_selection_errors ( const Tensor< type, 1 > &  new_selection_errors)

Definition at line 199 of file genetic_algorithm.cpp.

◆ set_training_errors()

void set_training_errors ( const Tensor< type, 1 > &  new_training_errors)

Definition at line 193 of file genetic_algorithm.cpp.

◆ to_string_matrix()

Tensor< string, 2 > to_string_matrix ( ) const

Writes as matrix of strings the most representative atributes.

Todo:
to many rows in string matrix.

Definition at line 946 of file genetic_algorithm.cpp.

◆ write_XML()

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.

Member Data Documentation

◆ elitism_size

Index elitism_size
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.

◆ fitness

Tensor<type, 1> fitness
private

Fitness of population.

Definition at line 136 of file genetic_algorithm.h.

◆ mutation_rate

type mutation_rate
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.

◆ parameters

Tensor<Tensor<type, 1>, 1> parameters
private

Performance of population.

Definition at line 142 of file genetic_algorithm.h.

◆ population

Tensor<bool, 2> population
private

Population matrix.

Definition at line 132 of file genetic_algorithm.h.

◆ selection

Tensor<bool, 1> selection
private

Definition at line 138 of file genetic_algorithm.h.

◆ selection_errors

Tensor<type, 1> selection_errors
private

Definition at line 145 of file genetic_algorithm.h.

◆ training_errors

Tensor<type, 1> training_errors
private

Definition at line 144 of file genetic_algorithm.h.


The documentation for this class was generated from the following files: