-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticAlgorithm.h
More file actions
44 lines (31 loc) · 1.07 KB
/
GeneticAlgorithm.h
File metadata and controls
44 lines (31 loc) · 1.07 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
#include "bits/stdc++.h"
using namespace std;
const int LowerBound = -10, UpperBound = 10;
const double crossOverProb = 0.7, mutationProb = 0.015;
template<class GeneType>
struct Chromosome {
vector<GeneType> chromosome;
GeneType fitness;
};
template<class GeneType>
class GeneticAlgorithm {
protected:
vector<Chromosome<GeneType>> population, offsprings;
vector<GeneType> fitnessCum;
int offspringIdx, numberOfGenes, populationSz, iterations , currIter;
void initialize();
virtual void generateInitialPopulation() = 0;
virtual GeneType calcFitnessFunction(vector<GeneType> &chromosome) = 0;
void calcPopulationFitness();
void calcFitnessCum();
int selection();
int selection(int prevSelected);
bool shouldPerformCrossOver();
void crossOver(int selectedChromosome1, int selectedChromosome2);
virtual void mutation(int idx) = 0;
virtual void generateOffsprings() = 0;
void replacement();
public:
GeneticAlgorithm(int populationSz, int numberOfGenes ,int iterations);
pair<vector<GeneType>, GeneType> run();
};