-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticAlgorithm.h
More file actions
135 lines (119 loc) · 4.82 KB
/
Copy pathGeneticAlgorithm.h
File metadata and controls
135 lines (119 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#pragma once
#include <random>
#include <vector>
#include "Individual.h"
#include "Problem.h"
/**
* Class representing a genetic algorithm.
*/
class GeneticAlgorithm {
public:
/**
* @struct Statistics
* @brief A structure to hold statistics about the evolution process of the Genetic Algorithm.
*
* This structure is used to collect and store various statistical measures such as the best,
* worst, and median results obtained during the evolution process. It also stores information
* about constraint violations and 'v' values for each evaluation point, for all penalty values.
*
* @var std::vector<double> Statistics::bestResults
* A vector storing the best fitness value found at each evaluation point.
*
* @var std::vector<double> Statistics::worstResults
* A vector storing the worst fitness value found at each evaluation point.
*
* @var std::vector<double> Statistics::medianResults
* A vector storing the median fitness value found at each evaluation point.
*
* @var std::array<int, 3> Statistics::constraintViolations
* A vector storing the number of constraint violations of the best individual at each
* evaluation point, for each penalty value.
*
* @var std::array<std::vector<double>> Statistics::vValues
* A vector storing the 'v' value of the best individual at each evaluation point, for
* each penalty value.
*/
struct Statistics {
std::vector<double> bestResults;
std::vector<double> worstResults;
std::vector<double> medianResults;
std::array<int, 3> constraintViolations = {0, 0, 0};
std::array<double, 3> vValues = {.0, .0, .0};
};
/**
* Constructor for the GeneticAlgorithm class.
* @param popSize The population size.
* @param chromosomeLength Length of the chromosome for each individual.
* @param mutationRate The mutation rate.
* @param crossoverRate The crossover rate.
* @param maxEvaluations The maximum number of evaluations.
* @param tournamentSize The tournament size.
* @param problem The optimization problem to solve.
* @param debug Whether to print debug information.
*/
GeneticAlgorithm(unsigned long popSize, int chromosomeLength, double mutationRate,
double crossoverRate, int maxEvaluations, int tournamentSize, Problem *problem,
bool debug);
/**
* Perform tournament selection.
* @param tournamentSize The size of the tournament.
* @return The winning individual.
*/
Individual tournamentSelection(int tournamentSize);
/**
* Perform crossover between two parents.
* @param parent1 The first parent.
* @param parent2 The second parent.
*/
void crossover(Individual &parent1, Individual &parent2);
/**
* Mutate an individual.
* @param ind The individual to mutate.
*/
void mutate(Individual &ind);
/**
* Evolve the population and gather statistical data.
*
* This method runs the evolution process on the population, applying genetic operations
* such as selection, crossover, and mutation. During this process, it gathers statistical
* data about the population at different evaluation points (2000D, 10000D, 20000D),
* including the best, worst, and median fitness values, the number of constraint violations,
* and the value of 'v' for the best individual.
*
* @return A Statistics struct containing the collected statistical data.
*/
Statistics evolve();
/**
* Get the best individual in the population.
* @return The best individual.
*/
Individual getBestIndividual() { return m_bestIndividual; }
/**
* Get the fitness of the best individual.
* @return The fitness of the best individual.
*/
double getBestFitness() { return m_bestIndividual.m_fitness; }
/**
* Get the fitness of the worst individual.
* @return The fitness of the worst individual.
*/
double getWorstFitness() { return m_population[m_popSize - 1].m_fitness; }
/**
* Get the fitness of the median individual.
* @return The fitness of the median individual.
*/
double getMedianFitness() { return m_population[m_popSize / 2].m_fitness; }
unsigned long m_popSize = 0;
int m_chromosomeLength = 0;
double m_mutationRate = .0;
double m_crossoverRate = .0;
const int m_maxEvaluations = 0;
int m_tournamentSize = 0;
Problem *m_problem;
bool m_debug = false;
int m_evaluationsCount = 0;
std::mt19937 gen; // Gerador Mersenne Twister
std::uniform_real_distribution<> dis; // Distribuição uniforme de [0, 1]
std::vector<Individual> m_population;
Individual m_bestIndividual;
};