-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtdmpi.cpp
More file actions
191 lines (169 loc) · 5.64 KB
/
Copy pathtdmpi.cpp
File metadata and controls
191 lines (169 loc) · 5.64 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
#include <stdlib.h>
#include <sys/time.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <mpi.h>
#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)
{
MPI_Status status;
int numtasks, rank;
MPI_Init(&argc,&argv); // initalize MPI environment
MPI_Comm_size(MPI_COMM_WORLD,&numtasks); //get total number of processes
MPI_Comm_rank(MPI_COMM_WORLD,&rank); // get process identity number
srand(rank);
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
if(rank==0) {
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;
}
}
}
int buff[poolSize*numtasks][46]; //15 towers (x,y) + tower type + entitiesAtGoal
int local[poolSize/numtasks][46];
for(int iter=0;iter<iterations;++iter) {
if(rank==0) {
//find average
double avg=0;
int zeros = 0;
for(int i=0;i<pool->size();++i) {
avg+=pool->at(i)->entitiesAtGoal();
if(pool->at(i)->entitiesAtGoal()==0) {
}
}
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));
}
for(int i=0;i<tournaments;++i) {
winners->at(i)->encodeForMPI(buff[i]);
}
winners->clear();
winnersMap->clear();
for(int i=0;i<pool->size();++i) {
delete pool->at(i);
}
}
//scatter to all ranks
MPI_Scatter(buff,tournaments/numtasks*46,MPI_INT,
local,tournaments/numtasks*46,MPI_INT,
0,MPI_COMM_WORLD);
//rebuild Sims
pool->clear();
poolMap->clear();
for(int i=0;i<tournaments/numtasks;++i) {
BigSim* s = new BigSim;
s->setTowersFromList(local[i],15*3);
s->overrideEntitiesAtGoal(local[i][15*3]);
pool->push_back(s);
poolMap->insert(SimPair(s->getTowerListString(),s));
}
//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));
}
//gather from all ranks
for(int i=0;i<poolSize;++i) {
pool->at(i)->encodeForMPI(buff[i]);
}
for(int i=0;i<poolSize;++i) {
delete pool->at(i);
}
pool->clear();
poolMap->clear();
MPI_Gather(buff,poolSize*46,MPI_INT,buff,poolSize*46,MPI_INT,0,MPI_COMM_WORLD);
//rebuild Sims
if(rank==0) {
for(int i=0;i<poolSize*numtasks;++i) {
BigSim* s = new BigSim;
s->setTowersFromList(buff[i],15*3);
s->overrideEntitiesAtGoal(buff[i][15*3]);
pool->push_back(s);
poolMap->insert(SimPair(s->getTowerListString(),s));
}
}
}
TIMER_STOP;
if(rank==0) {
//find & print best Sim
best = 100;
bestSim = NULL;
for(int i=0;i<pool->size();++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;
}
std::cout << std::setprecision(8) << TIMER_ELAPSED/1000000.0 << std::endl;
}
MPI_Finalize();
return 0;
}