-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneration.h
More file actions
411 lines (362 loc) · 11.8 KB
/
generation.h
File metadata and controls
411 lines (362 loc) · 11.8 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
4/19/2017
Class keeping track of an entire generation of animats in a vector
*/
#include "animat.h"
#include <vector> //to hold the animats in a generation
#include <algorithm> //to sort the genration by skill level, aka: score
#include <math.h> //creating statistics for generations
class generation
{
public:
//constructors
generation(); //default constructor
generation(int); //overloaded constructor, takes the number of animats in the generation
//methods
//setters
void setGen(int); //sets the generation number
void setPopulation(int); //sets the number of animats to create and hold
//getters
int getAnimatScore(int); //returns the animat at position "int" in the generation
int getGen(); //returns the generation number
int getPopulation(); //returns the number on animats in the generation
int getDead(); //returns number of dead
double getMean(); //returns mean of scores in generation
double getMedian(); //returns median of scores in generation
int getMode(); //returns mode of scores in generation
int getRange(); //returns the range of scores in generation
int getMinScore(); //returns minimum score
int getMaxScore(); //returns maximum score
//custom
void addAnimat(animat); //add another animat to the generation
void rankAnimats(bool); //re-order the list of animats based on skill level
generation reproduce(generation, bool); //reproduces animats, varies traits, adds mutations
void makeFirstGen(int); //creates 100 standard animats for first generation
void randomize(); //randomize scores
void dieOff(bool); //determines which animats die
void runSim(bool); //run simulation to test the animats' skills
void calcStats(); //calculats mean, median, mode, and range of generation's scores
void printStats(bool, bool, bool, bool, bool, bool, bool, bool); //prints population, death count, mean, median, mode, range, max, min with options to select specific
private:
//variables
vector <animat> animats; //holds roughly 100 animats
int gen; //holds the number of the generation for reference in the vector of generations
int population; //holds the number of animats to create and hold
int dead; //keeps track of dead animats
double mean; //mean of all scores in the generation
double median; //median of all scores in the generation
int mode; //mode of all scores in the generation
int range; //returns the range of scores in the generation
int minScore; //minimum score for a generation
int maxScore; //maximum score for a generation
};
//constructors
generation::generation() //default constructor
{
dead = 0;
}
generation::generation(int p) //overloaded constructor, takes the number of animats in the generation
{
population = p; //set population
animats.resize(p); //set size of the generation vector
dead = 0; //set number of dead animats to 0
}
//methods
//setters
void generation::setGen(int g) //sets the generation number
{
gen = g;
}
void generation::setPopulation(int p) //sets the number of animats to create and hold
{
population = p;
}
//getters
int generation::getAnimatScore(int a) //returns the animat at position "a" in the generation
{
return animats[a].getScore();
}
int generation::getGen() //returns the generation number
{
return gen;
}
int generation::getPopulation() //returns the number on animats in the generation
{
return population;
}
int generation::getDead() //return number of dead
{
return dead;
}
double generation::getMean() //returns mean of scores in generation
{
return mean;
}
double generation::getMedian() //returns median of scores in generation
{
return median;
}
int generation::getMode() //returns mode of scores in generation
{
return mode;
}
int generation::getRange() //returns the range of scores in generation
{
return range;
}
int generation::getMinScore() //reurns min score
{
return minScore;
}
int generation::getMaxScore() //returns max score
{
return maxScore;
}
//custom
//add another animat to the generation
void generation::addAnimat(animat)
{
}
//friend function to tell sort function what parameter to search by
bool sortByScore(const animat& lhs, const animat& rhs)
{
return (lhs.score > rhs.score); //sort highest to lowest score
}
//re-order the list of animats based on skill level
void generation::rankAnimats(bool p)
{
sort(animats.begin(), animats.end(), sortByScore); //sort the vector of animats based on their scores
///* //testing cout
if (p) //if user wants to print...
{
cout << "________________________________________________________" << endl;
cout << "Ranked" << endl;
for (int a = 0; a < animats.size(); a++) //test output
{
cout << a << " Score: " << animats[a].getScore() << endl;
}
}
//*/
}
//reproduces animats, varies traits, adds mutations
generation generation::reproduce(generation genB, bool p)
{
if (p) //if user wants to print...
{
cout << "________________________________________________________" << endl;
cout << "Reproduce" << endl;
}
genB.animats.resize(0);
genB.setPopulation((population - dead) * 2); //calc 2nd population
for (int a = 0; a < population; a++) //go through animat population...
{
if (animats[a].getAlive()) //if an animat is alive...
{
genB.animats.resize(genB.animats.size() + 2); //add two to the population of next gen
//create two new animats with same traits of predicessor
genB.animats[genB.animats.size() - 2] = animats[a]; //first offspring
genB.animats[genB.animats.size() - 1] = animats[a]; //second offspring
//genB.setPopulation(genB.getPopulation() + 2); //add two animats to the population size
//vary traits of offspring
genB.animats[genB.animats.size() - 2].reproduceVariation(); //first offspring
genB.animats[genB.animats.size() - 1].reproduceVariation(); //second offspring
//add mutations
//genB[genB.animats.size() - 2].mutation(); //first offspring
//genB[genB.animats.size() - 1].mutation(); //second offspring
if (p) //if user wants to print...
{
cout << genB.animats[genB.animats.size() - 2].gene() << endl; //first offspring test output
cout << genB.animats[genB.animats.size() - 1].gene() << endl; //second offspring test output
}
}
}
return genB;
}
//creates 100 standard animats for first generation
void generation::makeFirstGen(int p)
{
population = p; //set population
animats.resize(population);
/* Test creation of animats
for (int a = 0; a < population; a++)
{
cout << a << " Gene: " << animats[a].gene() << endl;
}
*/
}
//randomize scores //used for testing
void generation::randomize()
{
for (int a = 0; a < animats.size(); a++) //randomize the scores of the animats
{
animats[a].setScore(rand() % 20);
//cout << a << " Score: " << animats[a].getScore() << endl; //test output
}
}
//determines which animats die
void generation::dieOff(bool p)
{
//variables
int rNum, chance; //random number holder and chance the animat has to die
/* //old method
for (int a = 0; a < population; a++) //go through current generation...
{
chance = population - a; //higher ranking means less chance to die
rNum = rand() % chance;
if (rNum == 0)
{
animats[a].setAlive(0); //kill animat
dead++;
}
}
*/
///* //new method
for (int a = 0; a < population; a++) //go through current generation...
{
rNum = rand() % population;
for (int b = 0; b < a; b++) //check every number from 0 to their rank to see if they died
{
if (rNum == b) //if the range includes the animat's number, it dies off
{
animats[a].setAlive(0); //kill animat
dead++;
}
}
}
//*/
///* //test death function
if (p) //if user wants to print...
{
cout << "________________________________________________________" << endl;
cout << "Death Function" << endl;
for (int a = 0; a < population; a++)
{
if (animats[a].getAlive()) //if animat is alive
cout << a << " is alive" << endl;
else
cout << a << " is dead" << endl;
}
}
//*/
}
//run simulation to test the animats' skills
void generation::runSim(bool p)
{
//test 1d skills
if (p) //if user wants to print...
{
cout << "________________________________________________________" << endl;
cout << "Starting Scores" << endl;
}
for (int a = 0; a < population; a++)
{
animats[a].setScore(animats[a].test1D()); //set sthe animat's score to their score in testing
if (p) //if user wants to print...
{
cout << a << " Score: " << animats[a].getScore() << endl; //test output
}
}
}
//calculats mean, median, mode, and range of generation's scores
void generation::calcStats()
{
//variables
int total = 0; //holds total number for mean
int min = animats[population - 1].getScore(); //save the minimum score
int max = animats[0].getScore(); //set max score
vector <int> scoreCounts(max - min + 1, 0); //range to save the score, all set to 0
int finalMode = 0; //keep track of the mean
//calculate mean
for (int a = 0; a < population; a++) //go through all animats
{
total += animats[a].getScore(); //add score to total
}
mean = (double)total / population; //save mean
//calculate median
if (population % 2 == 0) //if population is even...
{
total = 0; //reset total
total += animats[ceil(population / 2)].getScore() + animats[ceil(population / 2) - 1].getScore(); //add totals of middle scores
median = (double)total / 2; //save median
}
else //if population is odd...
{
//use ceiling function
median = animats[ceil(population / 2)].getScore(); //save median
}
//calculate range
range = animats[0].getScore() - animats[population - 1].getScore(); //top score - bottom score is range
//calculate mode
for (int a = 0; a < population; a++) //go through population...
{
for (int b = min; b <= max; b++) //loop throught to check every possible score
{
if (animats[a].getScore() == b) //if the score is in the range
{
scoreCounts[(b - min)]++; //add to the count of the score - minimum score
}
}
}
for (int a = 0; a < scoreCounts.size(); a++) //go through score counts
{
if (scoreCounts[a] > finalMode) //if score is greater than the max, set max to the score
{
finalMode = scoreCounts[a]; //set new mode
mode = a + min; //set the mode
}
}
//set min and max
minScore = animats[population - 1].getScore(); //gets score from the bottom ranked animat
maxScore = animats[0].getScore(); //gets score from top ranked animat
}
//prints population, death count, mean, median, mode, range, max, min with options to select specific
/*
population
death count
mean
median
mode
range
max score
min score
*/
void generation::printStats(bool populationB, bool deathB, bool meanB, bool medianB, bool modeB, bool rangeB, bool maxB, bool minB)
{
if (populationB || deathB || meanB || medianB || modeB || rangeB || maxB || minB) //test if user wants anything printed...
{
cout << "________________________________________________________" << endl;
cout << "Stats" << endl;
}
if (populationB) //if user wants to print population
{
cout << "Population: " << population << endl;
}
if (deathB) //if user wants to print death count
{
cout << "Death Count: " << dead << endl;
}
if (meanB) //if user wants to print mean
{
cout << "Mean: " << mean << endl;
}
if (medianB) //if user wants to print median
{
cout << "Median: " << median << endl;
}
if (modeB) //if user wants to print mode
{
cout << "Mode: " << mode << endl;
}
if (rangeB) //if user wants to print range
{
cout << "Range: " << range << endl;
}
if (maxB) //if user wants to print max
{
cout << "Max: " << maxScore << endl;
}
if (minB) //if user wants to print min
{
cout << "Min: " << minScore << endl;
}
}