-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
173 lines (146 loc) · 6.28 KB
/
main.cpp
File metadata and controls
173 lines (146 loc) · 6.28 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <time.h>
#include <vector>
#include <string>
#include <stdlib.h>
#include "Items.h"
#include "Robby.h"
#include "World.h"
// Best to compile with c++11
//g++ -std=c++11 main.cpp Items.cpp Robby.cpp World.cpp -o Genetic
#define WORLDS_ROBBY_COMBINATIONS 100 // needs to be divisible by KEEP_TOP evenly
#define CANS_COUNT 50
#define LOOPS 500
#define KEEP_TOP 20
using namespace std;
void erase_world_robby_bottom(vector<World*>* world_array){
/* erasing the bottom 50% of the world array*/
int amt_keep = WORLDS_ROBBY_COMBINATIONS/5;
world_array->erase(world_array->begin()+amt_keep, world_array->end());
}
int main()
{
srand(time(0));
int can_num = CANS_COUNT;
vector<World*> world_array;
vector<World*>::iterator it = world_array.begin();
int position = 0;
vector<World*>::iterator iter = world_array.begin();
/*creating amount of world and robby combinations*/
for(int i=0; i<WORLDS_ROBBY_COMBINATIONS; i++){
world_array.push_back(new World(can_num));
}
/* iterating over all world robby combinations and creating fitness level
using robby_step method*/
for(it = world_array.begin(); it != world_array.end(); it++){
(*it)->robby_step();
}
/* Sorting world/robby combinations to have the highest first*/
int counter = WORLDS_ROBBY_COMBINATIONS;
while(counter){
for(it = world_array.begin(),(iter = world_array.begin())++; iter != world_array.end() || it != world_array.end(); it++, iter++){
// move over all of the items in the array and the one after it for comparison
if(iter == world_array.end()){
break;
}
if((*it)->robby_fitness() < (*iter)->robby_fitness()){
World* temp = (*it);
(*it) = (*iter);
(*iter) = temp;
}
}
counter--;
}
/* print world sample after*/
cout << endl << endl << "check world sample" << endl;
for(it = world_array.begin(); it != world_array.end(); it++){
cout << "fitness: " << (*it)->robby_fitness() << endl;
}
erase_world_robby_bottom(&world_array);
/* will loop over the 20 fittest items and will mate will a random 5 robbies*/
for(int i=0; i<WORLDS_ROBBY_COMBINATIONS/2; i++){
/* removing bottom 50%*/
erase_world_robby_bottom(&world_array);
/* will loop over the 20 fittest items and will mate will a random 5 robbies*/
for(int i=0; i<KEEP_TOP; i++){
// cout << "i value: " << i << endl;
for(int j=0; j<WORLDS_ROBBY_COMBINATIONS/KEEP_TOP; j++){
// mating with random robbies
int robby_num = 0;
do{
robby_num = rand()%KEEP_TOP; // selecting a mate from the top 20(0-19 in array)
}while(robby_num == i); // can't be itself
// robby at i and robby_num are different here
World* new_world_robby = new World(can_num);
world_array.push_back(new_world_robby); // adding one world/robby to the world_array
world_array.back()->mate(world_array.at(i), world_array.at(robby_num)); // mating new_world_robby(which is at the end) with robby at i and robby_num.
}
}
}
/* iterating over all world robby combinations and creating fitness level
using robby_step method*/
for(it = world_array.begin(); it != world_array.end(); it++){
(*it)->robby_step();
}
/* Sorting world/robby combinations to have the highest first*/
counter = WORLDS_ROBBY_COMBINATIONS;
while(counter){
for(it = world_array.begin(),(iter = world_array.begin())++; iter != world_array.end() || it != world_array.end(); it++, iter++){
// move over all of the items in the array and the one after it for comparison
if(iter == world_array.end()){
break;
}
if((*it)->robby_fitness() < (*iter)->robby_fitness()){
World* temp = (*it);
(*it) = (*iter);
(*iter) = temp;
}
}
counter--;
}
for(int i=0; i<LOOPS; i++){
/* removing bottom 50%*/
erase_world_robby_bottom(&world_array);
/* will loop over the 20 fittest items and will mate will a random 5 robbies*/
for(int i=0; i<KEEP_TOP; i++){
// cout << "i value: " << i << endl;
for(int j=0; j<WORLDS_ROBBY_COMBINATIONS/KEEP_TOP; j++){
// mating with random robbies
int robby_num = 0;
do{
robby_num = rand()%KEEP_TOP; // selecting a mate from the top 50(0-49 in array)
}while(robby_num == i); // can't be itself
// robby at i and robby_num are different here
World* new_world_robby = new World(can_num);
world_array.push_back(new_world_robby); // adding one world/robby to the world_array
world_array.back()->mate(world_array.at(i), world_array.at(robby_num)); // mating new_world_robby(which is at the end) with robby at i and robby_num.
}
}
/* iterating over all world robby combinations and creating fitness level
using robby_step method*/
for(it = world_array.begin(); it != world_array.end(); it++){
(*it)->robby_step();
}
/* Sorting world/robby combinations to have the highest first*/
counter = WORLDS_ROBBY_COMBINATIONS;
while(counter){
for(it = world_array.begin(),(iter = world_array.begin())++; iter != world_array.end() || it != world_array.end(); it++, iter++){
// move over all of the items in the array and the one after it for comparison
if(iter == world_array.end()){
break;
}
if((*it)->robby_fitness() < (*iter)->robby_fitness()){
World* temp = (*it);
(*it) = (*iter);
(*iter) = temp;
}
}
counter--;
}
}
cout << endl << endl << "final end check world sample" << endl;
for(it = world_array.begin(); it != world_array.end(); it++){
cout << " fitness: " << (*it)->robby_fitness() << endl;
}
return 0;
}