forked from ennorehling/csmapfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscout.cpp
More file actions
355 lines (284 loc) · 7.59 KB
/
Copy pathscout.cpp
File metadata and controls
355 lines (284 loc) · 7.59 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
#ifdef WIN32
#define NOMINMAX
#include <windows.h>
#endif
#include <ctime>
#include <cmath>
#include <vector>
#include <ostream>
#include <sstream>
#include <set>
#include "datafile.h"
#include "scout.h"
// Phenotype class
class Phenotype
{
public:
enum {
NO, O, SO, SW, W, NW,
MAX_DIRECTION
};
enum { GENOM_SIZE = 12*6 };
enum { SHIP_RANGE = 12 };
Phenotype();
Phenotype(const Phenotype& parent1, const Phenotype& parent2);
const std::vector<char>& getGenom() const { return genom; }
double getFitness() const { return fitness; }
bool operator< (const Phenotype& rhs) const
{
return fitness > rhs.fitness;
}
void mutate();
void rate(bool(*isOcean)(int x, int y));
void random_rate();
private:
static const int mutation_probability = 5;
private:
std::vector<char> genom;
double fitness;
};
Phenotype::Phenotype() : genom(GENOM_SIZE)
{
srand((unsigned int)time(0));
for (size_t i = 0; i < genom.size(); i++)
{
genom[i] = rand() % Phenotype::MAX_DIRECTION;
}
}
Phenotype::Phenotype(const Phenotype& parent1, const Phenotype& parent2) : genom(GENOM_SIZE)
{
srand((unsigned int)time(0));
// generate new genom by recombination of parent1 and parent2
size_t crossover = rand() % Phenotype::GENOM_SIZE;
//size_t crossover2 = rand() % Phenotype::GENOM_SIZE;
//if (crossover > crossover2)
//std::swap(crossover, crossover2);
// copy first part of genom from parent1...
for (size_t i = 0; i < crossover; i++)
genom[i] = parent1.genom[i];
// ...and second part of genom from parent2...
for (size_t i = crossover; i < GENOM_SIZE; i++)
genom[i] = parent2.genom[i];
// ...and third part of genom from parent1 again
//for (size_t i = crossover2; i < GENOM_SIZE; i++)
// genom[i] = parent1.genom[i];
}
void Phenotype::mutate()
{
srand((unsigned int)time(0));
const double mutation = mutation_probability/100.0;
// mutate every gen by mutation_probability %
for (size_t i = 0; i < GENOM_SIZE; i++)
if (rand()/(double)RAND_MAX < mutation)
genom[i] = rand() % Phenotype::MAX_DIRECTION;
}
void Phenotype::random_rate()
{
srand((unsigned int)time(0));
fitness = floor(fitness) + rand() / (double)RAND_MAX;
}
void Phenotype::rate(bool(*isOcean)(int x, int y))
{
srand((unsigned int)time(0));
// NO, O, SO, SW, W, NW
const int offset_x[6] = { 0, +1, +1, 0, -1, -1 };
const int offset_y[6] = { +1, 0, -1, -1, 0, +1 };
std::set< std::pair<int,int> > visited;
double fit = 0;
for (int ship = 0; ship < GENOM_SIZE / SHIP_RANGE; ship++)
{
const size_t begin = ship * SHIP_RANGE;
const size_t end = (ship+1) * SHIP_RANGE;
char coast = SO;
int x = 3;
int y = -1;
for (size_t i = begin; i < end; i++)
{
char dir = genom[i]; // current sail direction
// check if ship leaves coast in correct direction
// break journey if it doesn't
if (coast != MAX_DIRECTION &&
dir != coast &&
dir != (coast+1) % MAX_DIRECTION &&
dir != (MAX_DIRECTION+coast-1) % MAX_DIRECTION)
{
//fit -= 2*(end - i);
break;
}
// check if ship leaves coast to an ocean region
if (coast != MAX_DIRECTION && !isOcean(x+offset_x[dir], y+offset_y[dir]))
{
//fit -= 2*(end - i);
break;
}
// ship leaves coast
coast = MAX_DIRECTION;
x += offset_x[dir];
y += offset_y[dir];
// update visited regions
visited.insert(std::make_pair(x, y));
for (int j = 0; j < 6; j++)
{
// put all explored ocean on a list
int nx = x+offset_x[j];
int ny = y+offset_y[j];
if (isOcean(nx,ny))
visited.insert(std::make_pair(nx, ny));
}
// put ship on coast if it hits on a land region
if (!isOcean(x,y))
{
coast = (dir + MAX_DIRECTION/2) % MAX_DIRECTION; // ship lies on coast where it came from
//fit -= end - i;
break;
}
}
}
// calc fitness bonus from visited regions
fitness = fit + visited.size();
}
// ScoutData class
class Scout::ScoutData
{
public:
ScoutData();
void rate_all(bool(*cond)(int x, int y));
void select_best(bool(*cond)(int x, int y));
void mutate_some(bool(*cond)(int x, int y));
void get_genom(std::ostream& out, size_t which) const;
void show(std::ostream& out) const;
private:
static const size_t population_count = 50;
static const size_t elite_count = 10;
static const size_t survivor_count = 0; // elite + survivor + clone_from_elite = population
private:
typedef std::list<Phenotype> generation_t;
generation_t generation;
bool rated;
};
Scout::ScoutData::ScoutData() : rated(false)
{
for (size_t i = 0; i < population_count; ++i)
generation.push_back(Phenotype());
}
void Scout::ScoutData::rate_all(bool(*cond)(int x, int y))
{
if (!rated)
{
rated = true;
generation_t::iterator itor = generation.begin();
generation_t::iterator end = generation.end();
for (; itor != end; ++itor)
itor->rate(cond);
}
/*
generation_t::iterator itor = generation.begin();
generation_t::iterator end = generation.end();
for (; itor != end; ++itor)
itor->random_rate();
*/
generation.sort();
}
void Scout::ScoutData::select_best(bool(*cond)(int x, int y))
{
srand((unsigned int)time(0));
while (generation.size() > elite_count + survivor_count)
generation.pop_back();
std::vector<Phenotype*> elite; // direct random access to elite Phenotypes
generation_t::iterator itor = generation.begin();
for (size_t i = 0; i < elite_count && itor != generation.end(); ++i, ++itor)
elite.push_back(&*itor);
// fill population with siblings from the elite until pop is up to population_count again
while (generation.size() < population_count)
{
size_t parent1 = rand() % elite_count;
size_t parent2 = rand() % elite_count;
while (parent1 == parent2)
parent2 = rand() % elite_count;
generation.push_back( Phenotype(*elite[parent1], *elite[parent2]) );
generation.back().rate(cond);
}
}
void Scout::ScoutData::mutate_some(bool(*cond)(int x, int y) )
{
generation_t::iterator itor = generation.begin();
generation_t::iterator end = generation.end();
//for (size_t i = 0; i < elite_count && itor != end; ++i, ++itor)
//{
// // don't mutate the elite
//}
++itor; // don't mutate best
for (; itor != end; ++itor)
{
itor->mutate();
itor->rate(cond);
}
}
void Scout::ScoutData::get_genom(std::ostream& out, size_t which) const
{
generation_t::const_iterator pheno = generation.begin();
for (; pheno != generation.end() && which != 0; ++pheno, --which)
{
// go to Phenotype number 'which'
}
if (pheno == generation.end())
{
out << "index out of bounds";
return;
}
const std::vector<char>& genom = pheno->getGenom();
const char* direction[] = { "NO", "O", "SO", "SW", "W", "NW" };
for (size_t i = 0; i < genom.size(); i++)
{
if (!(i % Phenotype::SHIP_RANGE) && i != 0)
out << "NACH ";
out << direction[genom[i]] << ' ';
}
}
void Scout::ScoutData::show(std::ostream& out) const
{
// show fitness of elite
generation_t::const_iterator itor = generation.begin();
generation_t::const_iterator end = generation.end();
for (size_t i = 0; i < elite_count && itor != end; ++i, ++itor)
{
out << " " << itor->getFitness();
}
}
// implementation of class Scout
Scout::Scout() : data(new ScoutData)
{
}
Scout::~Scout()
{
delete data;
}
void Scout::reset()
{
delete data;
data = 0;
data = new ScoutData;
}
void Scout::get(std::ostream& out, size_t which) const
{
data->get_genom(out, which);
}
void Scout::step(bool(*cond)(int x, int y))
{
data->rate_all(cond);
data->select_best(cond);
data->mutate_some(cond);
//std::ostringstream out;
//out << "Step.\n";
//OutputDebugString(out.str().c_str());
show();
}
void Scout::show()
{
std::ostringstream out;
data->show(out);
out << "\n";
#ifdef WIN32
OutputDebugString(out.str().c_str());
#endif
}