-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofApp.cpp
More file actions
119 lines (99 loc) · 3.23 KB
/
Copy pathofApp.cpp
File metadata and controls
119 lines (99 loc) · 3.23 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
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
fitness_plotter.setWindowSize(2000);
srand((unsigned)(time(0)));
// current generation
generation = 0;
// current average
average = 0;
vector<Individual> population;
found = false;
creator = false;
cout << "Enter mutation phrase here: ";
std::getline(std::cin, Genetics::TARGET);
}
//--------------------------------------------------------------
void ofApp::update(){
// create initial population
while (creator == false) {
for (int i = 0;i < POPULATION_SIZE;i++)
{
string gnome = Genetics::create_gnome();
population.push_back(Individual(gnome));
}
creator = true;
}
while (!found)
{
// sort the population in increasing order of fitness score
sort(population.begin(), population.end());
// if the individual having lowest fitness score ie.
// 0 then we know that we have reached to the target
// and break the loop
if (population[0].fitness <= 0)
{
found = true;
break;
}
// Otherwise generate new offsprings for new generation
vector<Individual> new_generation;
// Perform Elitism, that mean 10% of fittest population
// goes to the next generation
int s = ((10 * POPULATION_SIZE) / 100);
for (int i = 0;i<s;i++)
new_generation.push_back(population[i]);
// From 50% of fittest population, Individuals
// will mate to produce offspring
s = (90 * POPULATION_SIZE) / 100;
for (int i = 0;i<s;i++)
{
int len = population.size();
int r = Genetics::random_num(0, 50);
Individual parent1 = population[r];
r = Genetics::random_num(0, 50);
Individual parent2 = population[r];
Individual offspring = parent1.mate(parent2);
new_generation.push_back(offspring);
}
population = new_generation;
cout << "Generation: " << generation << "\t";
cout << "String: " << population[0].chromosome << "\t";
cout << "Fitness: " << population[0].fitness << "\n";
//Calculating averag fitness of every generation
for (int i = 0; i < population.size(); i++) {
average += population[i].fitness;
}
average = (average / POPULATION_SIZE);
//Plotting out values
fitness_plotter["Average Fitness"] << (average);
fitness_plotter["Maximum Fitness"] << population[0].fitness;
fitness_plotter["Minimum Fitness"] << population[POPULATION_SIZE-1].fitness;
generation++;
return;
}
for (int i = 0; i < population.size(); i++) {
average += population[i].fitness;
}
average = (average / POPULATION_SIZE);
cout << "Generation: " << generation << "\t";
cout << "String: " << population[0].chromosome << "\t";
cout << "Fitness: " << population[0].fitness << "\n";
fitness_plotter["Average Fitness"] << average;
fitness_plotter["Maximum Fitness"] << population[0].fitness;
fitness_plotter["Minimum Fitness"] << population[POPULATION_SIZE - 1].fitness;
return;
}
//--------------------------------------------------------------
void ofApp::draw(){
ofBackgroundGradient(ofColor(255,255,255), ofColor(255,255,255));
fitness_plotter.draw(0, 0, ofGetWidth(), ofGetHeight());
if (population[0].fitness == 0) {
return;
}
}
// overloaded < operator to compare 2 individuals
bool operator<(const Individual &ind1, const Individual &ind2)
{
return ind1.fitness < ind2.fitness;
}