OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
opennn::GeneticAlgorithm Class Referencefinal

Genetic-algorithm based input feature selection. More...

#include <genetic_algorithm.h>

Inheritance diagram for opennn::GeneticAlgorithm:
[legend]

Public Member Functions

 GeneticAlgorithm (TrainingStrategy *training_strategy=nullptr)
 Constructs the algorithm.
 
Index get_individuals_number () const
 Number of individuals in the current population.
 
Index get_genes_number () const
 Number of genes per chromosome (= number of original inputs).
 
void set_default ()
 Resets all hyperparameters to their default values.
 
Index get_minimum_inputs_number () const override
 Minimum number of selected inputs allowed in any individual.
 
Index get_maximum_inputs_number () const override
 Maximum number of selected inputs allowed in any individual.
 
void set_minimum_inputs_number (const Index new_minimum)
 Sets the minimum number of selected inputs.
 
void set_maximum_inputs_number (const Index)
 Sets the maximum number of selected inputs.
 
void set_individuals_number (const Index new_individuals_number=4)
 Sets the population size.
 
void set_initialization_method (string method)
 Sets the population initialization strategy.
 
void set_mutation_rate (const float rate)
 Sets the mutation rate.
 
void set_elitism_size (const Index size)
 Sets the elitism size.
 
InputsSelectionResults perform_input_selection () override
 Runs the genetic algorithm.
 
void from_JSON (const JsonDocument &) override
 Loads hyperparameters from a parsed JSON document.
 
void to_JSON (JsonWriter &) const override
 Writes hyperparameters to a streaming JSON writer.
 
- Public Member Functions inherited from opennn::InputsSelection
 InputsSelection (TrainingStrategy *training_strategy=nullptr)
 Constructs the selector.
 
virtual ~InputsSelection ()=default
 Virtual destructor.
 
const TrainingStrategyget_training_strategy () const
 Read-only access to the bound training strategy.
 
bool has_training_strategy () const
 Whether a training strategy has been bound.
 
bool get_display () const
 Whether progress should be printed to stdout.
 
void set (TrainingStrategy *new_training_strategy)
 Re-initializes the selector by setting its training strategy.
 
void set_trials_number (const Index new_trials_number)
 Sets the number of training trials per candidate (mean is used).
 
void set_display (bool new_display)
 Toggles per-iteration progress printing.
 
void set_validation_error_goal (const float new_validation_error_goal)
 Sets the validation error goal.
 
void set_maximum_epochs (const Index new_maximum_epochs)
 Sets the maximum training epochs per evaluation.
 
void set_maximum_validation_failures (const Index new_maximum_validation_failures)
 Sets the maximum number of consecutive validation-error increases tolerated.
 
void set_maximum_time (const float new_maximum_time)
 Sets the maximum wall-clock selection time.
 
void check () const
 Validates the bound configuration; throws if anything is missing.
 
string get_name () const
 Canonical name of the selector (set by subclasses).
 
void save (const filesystem::path &) const
 Saves the selector configuration to a file.
 
void load (const filesystem::path &)
 Loads the selector configuration from a file.
 
virtual void print () const
 Prints a human-readable summary of the selector to stdout.
 

Additional Inherited Members

- Public Types inherited from opennn::InputsSelection
enum class  StoppingCondition {
  MaximumTime , SelectionErrorGoal , MaximumInputs , MaximumEpochs ,
  MaximumSelectionFailures
}
 Reasons that can terminate an input-selection run. More...
 
- Protected Attributes inherited from opennn::InputsSelection
TrainingStrategytraining_strategy = nullptr
 Training strategy used to evaluate candidate subsets; not owned.
 
Index trials_number = 1
 Number of independent training runs averaged per candidate.
 
bool display = true
 Whether progress should be printed to stdout during selection.
 
float validation_error_goal
 Validation error goal; selection stops when reached.
 
Index maximum_epochs
 Maximum training epochs per evaluation.
 
Index maximum_validation_failures = 100
 Maximum consecutive validation-error increases tolerated.
 
float maximum_time
 Maximum wall-clock selection time in seconds.
 
string name
 Canonical name of the selector (set by subclasses).
 

Detailed Description

Genetic-algorithm based input feature selection.

Encodes each candidate input subset as a binary chromosome (one gene per input variable), evolves a population of chromosomes through selection, crossover and mutation, and returns the subset that achieved the lowest validation error.

Constructor & Destructor Documentation

◆ GeneticAlgorithm()

opennn::GeneticAlgorithm::GeneticAlgorithm ( TrainingStrategy * training_strategy = nullptr)

Constructs the algorithm.

Parameters
training_strategyTraining strategy used to evaluate candidates.

Member Function Documentation

◆ from_JSON()

void opennn::GeneticAlgorithm::from_JSON ( const JsonDocument & )
overridevirtual

Loads hyperparameters from a parsed JSON document.

Implements opennn::InputsSelection.

◆ get_genes_number()

Index opennn::GeneticAlgorithm::get_genes_number ( ) const
inline

Number of genes per chromosome (= number of original inputs).

◆ get_individuals_number()

Index opennn::GeneticAlgorithm::get_individuals_number ( ) const
inline

Number of individuals in the current population.

◆ get_maximum_inputs_number()

Index opennn::GeneticAlgorithm::get_maximum_inputs_number ( ) const
inlineoverridevirtual

Maximum number of selected inputs allowed in any individual.

Implements opennn::InputsSelection.

◆ get_minimum_inputs_number()

Index opennn::GeneticAlgorithm::get_minimum_inputs_number ( ) const
inlineoverridevirtual

Minimum number of selected inputs allowed in any individual.

Implements opennn::InputsSelection.

◆ perform_input_selection()

InputsSelectionResults opennn::GeneticAlgorithm::perform_input_selection ( )
overridevirtual

Runs the genetic algorithm.

Returns
Best-of-run input subset and supporting statistics.

Implements opennn::InputsSelection.

◆ set_default()

void opennn::GeneticAlgorithm::set_default ( )

Resets all hyperparameters to their default values.

◆ set_elitism_size()

void opennn::GeneticAlgorithm::set_elitism_size ( const Index size)
inline

Sets the elitism size.

Parameters
sizeNumber of best individuals carried unchanged to the next generation, clamped to [0, individuals_number].

◆ set_individuals_number()

void opennn::GeneticAlgorithm::set_individuals_number ( const Index new_individuals_number = 4)

Sets the population size.

Parameters
new_individuals_numberNumber of individuals to maintain.

◆ set_initialization_method()

void opennn::GeneticAlgorithm::set_initialization_method ( string method)
inline

Sets the population initialization strategy.

Parameters
methodStrategy name ("Random" or "Correlations").

◆ set_maximum_inputs_number()

void opennn::GeneticAlgorithm::set_maximum_inputs_number ( const Index )

Sets the maximum number of selected inputs.

Receives the new upper bound on the number of selected inputs.

◆ set_minimum_inputs_number()

void opennn::GeneticAlgorithm::set_minimum_inputs_number ( const Index new_minimum)
inline

Sets the minimum number of selected inputs.

Parameters
new_minimumNew lower bound on the number of selected inputs.

◆ set_mutation_rate()

void opennn::GeneticAlgorithm::set_mutation_rate ( const float rate)
inline

Sets the mutation rate.

Parameters
ratePer-gene flip probability, clamped to [0, 1].

◆ to_JSON()

void opennn::GeneticAlgorithm::to_JSON ( JsonWriter & ) const
overridevirtual

Writes hyperparameters to a streaming JSON writer.

Implements opennn::InputsSelection.