-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
129 lines (90 loc) · 3.12 KB
/
main.cpp
File metadata and controls
129 lines (90 loc) · 3.12 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
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <bits/stdc++.h>
#include "population.h"
#include "individual.h"
using namespace std;
int main(int argc, char* argv[])
{
// Format: ./program <targetfile.txt> <num_individuals> <selection_ratio> <mutation_rate>
if (argc < 5) {
cout << "Not enough arguments" << endl;
cout.flush();
return -1;
}
srand(time(NULL));
// ifstream file("target.txt");
ifstream file(argv[1]);
string target = "";
string line;
while (getline(file, line))
{
target += line;
target += '\n';
}
// file >> target;
transform(target.begin(), target.end(), target.begin(), ::tolower);
cout << target << endl;
cout.flush();
// initialize population with 10 individuals & calculate fitness
// int num_individuals = 200;
int num_individuals = stoi(argv[2]);
Population pop(target, target.size(), num_individuals);
// print all individuals
// cout << "pop_size_: " << pop.pop_size_ << endl;
// cout.flush();
// generate populations until maximum fitness achieved
bool sim_complete = false;
int generations = 0;
while (!sim_complete)
{
// cout << "---GENERATION " << generations << "---" << endl;
// cout.flush();
// select sub-population (parents) that survives (highest fitness = lowest distance)
// double selection_ratio = 0.10;
double selection_ratio = stod(argv[3]);
vector<Individual*> sub_population = pop.selectAlivePopulation(selection_ratio);
// Display progress
// cout << "Printing sub population picked" << endl;
// cout.flush();
// for (auto elem : sub_population)
// {
// elem->print();
// }
// cout.flush();
sub_population[0]->print();
cout.flush();
// crossover genes of selected parents and add to child_population list
pop.crossOverPopulation(sub_population, selection_ratio);
// mutate the children (0.08 rate) to explore newer solutions & evaluate fitness
double mutation_rate = stod(argv[4]);
for (auto& child : pop.child_population)
{
// child->mutateIndividual(0.05);
child->mutateIndividual(mutation_rate);
// child->print();
// cout << child->total_fitness_ << endl;
}
if (pop.checkComplete(pop.child_population))
{
sim_complete = true;
cout << "Num of generations: " << generations << " ";
cout.flush();
}
// select child survivors with highest fitness for the next generation
// add children to the population and clear out child_population list
for (auto& child : pop.child_population)
{
pop.population_list.push_back(child);
}
while (!pop.child_population.empty()) // remember auto would create a copy
{
pop.child_population.pop_back();
}
generations++;
}
return 0;
}