-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtd.cpp
More file actions
136 lines (117 loc) · 3.72 KB
/
Copy pathtd.cpp
File metadata and controls
136 lines (117 loc) · 3.72 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
#include <stdlib.h>
#include <sys/time.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include "sim.hpp"
typedef std::pair<std::string, BigSim*> SimPair;
#define TIMER_CLEAR (tv1.tv_sec = tv1.tv_usec = tv2.tv_sec = tv2.tv_usec = 0)
#define TIMER_START gettimeofday(&tv1, (struct timezone*)0)
#define TIMER_ELAPSED ((tv2.tv_usec-tv1.tv_usec)+((tv2.tv_sec-tv1.tv_sec)*1000000))
#define TIMER_STOP gettimeofday(&tv2, (struct timezone*)0)
struct timeval tv1,tv2;
int main(int argc, char **argv)
{
srand(1);
std::vector<BigSim*> *pool = new std::vector<BigSim*>;
std::map<std::string,BigSim*> *poolMap = new std::map<std::string,BigSim*>;
std::vector<BigSim*> *winners = new std::vector<BigSim*>;
std::map<std::string,BigSim*> *winnersMap = new std::map<std::string,BigSim*>;
std::vector<BigSim*> *temp;
std::map<std::string,BigSim*> *tempMap;
int poolSize = 300;
int tourneySize = 8;
int tournaments = 200;
int iterations = 10;
int oldPoolSize, best, r;
BigSim* bestSim;
TIMER_CLEAR;
TIMER_START;
//create Sim pool
while(pool->size() < poolSize) {
BigSim* sim = new BigSim;
sim->populateMap();
if(poolMap->find(sim->getTowerListString())==poolMap->end()) {
while(sim->activeEntities()) sim->tick();
pool->push_back(sim);
poolMap->insert(SimPair(sim->getTowerListString(),sim));
}
else {
delete sim;
}
}
for(int iter=0;iter<iterations;++iter) {
//find average
double avg=0;
for(int i=0;i<pool->size();++i) {
avg+=pool->at(i)->entitiesAtGoal();
}
avg/=pool->size();
std::cout << avg << std::endl;
//remove some of the worst sims
winners->clear();
winnersMap->clear();
for(int i=0;i<tournaments;++i) {
best = 100;
bestSim = NULL;
for(int j=0;j<tourneySize;++j) {
do {
r = rand()%pool->size();
} while(winnersMap->find(pool->at(r)->getTowerListString())!=winnersMap->end());
if(bestSim == NULL || pool->at(r)->entitiesAtGoal() <= best) {
bestSim = pool->at(r);
best = pool->at(r)->entitiesAtGoal();
}
}
winners->push_back(bestSim);
winnersMap->insert(SimPair(bestSim->getTowerListString(),bestSim));
}
pool->clear();
poolMap->clear();
temp = pool;
tempMap = poolMap;
pool = winners;
poolMap = winnersMap;
winners = temp;
winnersMap = tempMap;
//mutate/crossover to refill pool
int oldPoolSize = pool->size();
double prevScore = 0;
while(pool->size() < poolSize) {
BigSim* sim = new BigSim;
if(rand()%10<1) {
int i = rand()%oldPoolSize;
sim->setTowersFromList(pool->at(i)->getTowerList(),
pool->at(i)->getMap()->getNumTowers()*3);
sim->mutateTower();
prevScore = pool->at(i)->entitiesAtGoal();
}
else {
int i = rand()%oldPoolSize;
int j = rand()%oldPoolSize;
sim->crossoverTowers(pool->at(i),pool->at(j));
prevScore = (pool->at(i)->entitiesAtGoal()+pool->at(j)->entitiesAtGoal())/2.;
}
while(sim->activeEntities()) sim->tick();
pool->push_back(sim);
poolMap->insert(SimPair(sim->getTowerListString(),sim));
}
}
//find & print best Sim
best = 100;
bestSim = NULL;
for(int i=0;i<poolSize;++i) {
if(pool->at(i)->entitiesAtGoal() < best) {
best = pool->at(i)->entitiesAtGoal();
bestSim = pool->at(i);
}
}
if(bestSim) {
std::cout << bestSim->getMap()->print();
std::cout << best << std::endl;
}
TIMER_STOP;
std::cout << std::setprecision(8) << TIMER_ELAPSED/1000000.0 << std::endl;
return 0;
}