-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.cpp
More file actions
387 lines (296 loc) · 10.7 KB
/
algorithm.cpp
File metadata and controls
387 lines (296 loc) · 10.7 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
////////////////////////////////////////////////////////////////////////////
// //
// Implementation file for algorithms that play chinese checkers. //
// //
// Author: Cédric Schoonen <cedric.schoonen1@gmail.com> //
// February 2020 //
// //
////////////////////////////////////////////////////////////////////////////
// Ideas for better algorithms
// o fitness term to keep pawns close to each others
// o for convex graphs/hexagram, fitness term to go away from the center
#ifndef ALGORITHM
#define ALGORITHM
#include <iostream>
#include <vector>
#include <math.h>
#include <random>
#include "Board.h"
using namespace std;
///////////////////////////// Declarations /////////////////////////////////
// Random number generator
random_device true_gen;
int seed = true_gen();
default_random_engine gen(seed);
uniform_real_distribution<double> dist01(0,1);
// class for typical move
class Move
{
public:
Move(int ivertexFrom, int ivertexTo)
: ivertexFrom_(ivertexFrom), ivertexTo_(ivertexTo) {;}
int ivertexFrom_;
int ivertexTo_;
double weight_;
};
// Algorithms (basic)
void randomMove(Board&, int&, int&);
void bestMove0MinSum(Board&, int&, int&);
void bestMove0MinFree(Board&, int&, int&);
// Algorithms (hamiltonian family)
void algorithmHamiltonian(Board &board, int &ipawnToMove, int &ivertexDestination);
double temperature = 0.1;
double hamiltonianTarget(Board&, Move);
double hamiltonian(Board &board, Move move)
{
return hamiltonianTarget(board, move);
}
// generic algorithm function used to redirect to other ones
void algorithm(Board &board, int &ipawnToMove, int &ivertexDestination)
{
//randomMove(board, ipawnToMove, ivertexDestination);
//bestMove0MinSum(board, ipawnToMove, ivertexDestination);
//bestMove0MinFree(board, ipawnToMove, ivertexDestination);
algorithmHamiltonian(board, ipawnToMove, ivertexDestination);
}
//////////////////////////// Implementations ///////////////////////////////
void randomMove(Board &board, int &ipawnToMove, int &ivertexDestination)
{
#ifdef DEBUG
cout << "--- randomMove algorithm ---" << endl;
#endif
vector<Pawn> pawns = board.getPawns();
vector<Move> moves;
// compute available moves
for (int ipawn=0; ipawn<pawns.size(); ipawn++)
{
if (pawns[ipawn].getTeam() != board.getPlayingTeam()) continue;
int ivertexFrom = board.getVertexFromPawn(ipawn);
for (int ivertexTo: board.availableMovesDirect(ivertexFrom))
moves.push_back(Move(ivertexFrom, ivertexTo));
for (int ivertexTo: board.availableMovesHopping(ivertexFrom))
moves.push_back(Move(ivertexFrom, ivertexTo));
}
// choose move
Move moveChosen = moves[int(moves.size()*dist01(gen))];
ipawnToMove = board.getPawnFromVertex(moveChosen.ivertexFrom_);
ivertexDestination = moveChosen.ivertexTo_;
}
// Fit function using the summed distances to the target vertices
double fitDistanceToTargets(Board &board, int ivertexFrom, int ivertexTo,
int team)
{
vector<Vertex> vertices = board.getVertices();
vector<int> targets = board.getTargetOfTeam(team);
int distance1 = 0;
for (int itarget : targets)
distance1 += board.distance(vertices[ivertexFrom], vertices[itarget]);
int distance2 = 0;
for (int itarget : targets)
distance2 += board.distance(vertices[ivertexTo], vertices[itarget]);
return distance1-distance2;
}
// Fit function using the distance to a free target vertex
// Tweaked to limit moves from a target vertex
double fitDistanceToFreeTarget(Board &board, int ivertexFrom, int ivertexTo,
int team)
{
vector<Vertex> vertices = board.getVertices();
vector<int> targets = board.getTargetOfTeam(team);
// find the free targets
vector<int> freeTargets;
for (int itarget : targets)
if (board.getPawnFromVertex(itarget)<0)
freeTargets.push_back(itarget);
// choose a free target randomly
// chose a occupied one if none are free (e.g. start of the game)
int itargetChosen;
if (freeTargets.size()>0)
itargetChosen = freeTargets[int(dist01(gen)*freeTargets.size())];
else
itargetChosen = targets[int(dist01(gen)*targets.size())];
// distances to free target
int distance1 = 0;
for (int itarget : targets)
distance1 += board.distance(vertices[ivertexFrom], vertices[itargetChosen]);
int distance2 = 0;
for (int itarget : targets)
distance2 += board.distance(vertices[ivertexTo], vertices[itargetChosen]);
// devaluate moves from a target vertex
for (int itarget : targets) if (itarget == ivertexFrom)
return 1.0/3*(distance1-distance2);
return distance1-distance2;
}
// Choose best move looking 0 steps ahead (immediate best move)
// this one tries to minimise the summed distances to the target vertices
void bestMove0MinSum(Board &board, int &ipawnToMove, int &ivertexDestination)
{
#ifdef DEBUG
cout << "--- bestMove0MinSum algorithm ---" << endl;
#endif
vector<Pawn> pawns = board.getPawns();
vector<Move> moves;
int pteam = board.getPlayingTeam();
// compute available moves
for (int ipawn=0; ipawn<pawns.size(); ipawn++)
{
if (pawns[ipawn].getTeam() != pteam) continue;
int ivertexFrom = board.getVertexFromPawn(ipawn);
for (int ivertexTo: board.availableMovesDirect(ivertexFrom))
moves.push_back(Move(ivertexFrom, ivertexTo));
for (int ivertexTo: board.availableMovesHopping(ivertexFrom))
moves.push_back(Move(ivertexFrom, ivertexTo));
}
// initialise best move
Move moveBest = moves[0];
double bestFit = fitDistanceToTargets(board, moveBest.ivertexFrom_,
moveBest.ivertexTo_, pteam);
for (Move move : moves)
{
double fit = fitDistanceToTargets(board, move.ivertexFrom_,
move.ivertexTo_, pteam);
#ifdef DEBUG
cout << "move from " << move.ivertexFrom_
<< " to " << move.ivertexTo_ << " : "
<< "fit = " << fit << endl;
#endif
if (fit > bestFit)
{
moveBest = move;
bestFit = fit;
}
// to introduce a bit of randomness
// TODO: uniform density because first moves in the list are
// less probable with current scheme, which leads to a
// struggle at the end and a bias towards players that
// have the favorable vertex order.
else if (fit == bestFit && dist01(gen)<0.5)
{
moveBest = move;
bestFit = fit;
}
}
// return best move
ipawnToMove = board.getPawnFromVertex(moveBest.ivertexFrom_);
ivertexDestination = moveBest.ivertexTo_;
}
// Choose best move looking 0 steps ahead (immediate best move)
// this one tries to minimise the distance to a free target vertex
void bestMove0MinFree(Board &board, int &ipawnToMove, int &ivertexDestination)
{
#ifdef DEBUG
cout << "--- bestMove0MinFree algorithm ---" << endl;
#endif
vector<Pawn> pawns = board.getPawns();
vector<Move> moves;
int pteam = board.getPlayingTeam();
// compute available moves
for (int ipawn=0; ipawn<pawns.size(); ipawn++)
{
if (pawns[ipawn].getTeam() != pteam) continue;
int ivertexFrom = board.getVertexFromPawn(ipawn);
for (int ivertexTo: board.availableMovesDirect(ivertexFrom))
moves.push_back(Move(ivertexFrom, ivertexTo));
for (int ivertexTo: board.availableMovesHopping(ivertexFrom))
moves.push_back(Move(ivertexFrom, ivertexTo));
}
// initialise best move
Move moveBest = moves[0];
double bestFit = fitDistanceToFreeTarget(board, moveBest.ivertexFrom_,
moveBest.ivertexTo_, pteam);
for (Move move : moves)
{
double fit = fitDistanceToFreeTarget(board, move.ivertexFrom_,
move.ivertexTo_, pteam);
#ifdef DEBUG
cout << "move from " << move.ivertexFrom_
<< " to " << move.ivertexTo_ << " : "
<< "fit = " << fit << endl;
#endif
if (fit > bestFit)
{
moveBest = move;
bestFit = fit;
}
// to introduce a bit of randomness
// TODO: uniform density because first moves in the list are
// less probable with current scheme, which leads to a
// struggle at the end and a bias towards players that
// have the favorable vertex order.
else if (fit == bestFit && dist01(gen)<0.5)
{
moveBest = move;
bestFit = fit;
}
}
// return best move
ipawnToMove = board.getPawnFromVertex(moveBest.ivertexFrom_);
ivertexDestination = moveBest.ivertexTo_;
}
//////////////////////////// Hamiltonian Family ////////////////////////////
double hamiltonianTarget(Board& board, Move move)
{
vector<Vertex> vertices = board.getVertices();
vector<int> targets = board.getBestTargets();
int pteam = board.getPlayingTeam();
// this is a method for convex graphs, check if it applies this one
assert(targets[pteam]>=0);
// distances to best target
int distance1 = board.distance(vertices[move.ivertexFrom_],
vertices[targets[pteam]]);
int distance2 = board.distance(vertices[move.ivertexTo_],
vertices[targets[pteam]]);
double energy = distance2-distance1;
return energy;
}
void algorithmHamiltonian(Board &board, int &ipawnToMove, int &ivertexDestination)
{
#ifdef DEBUG
cout << "--- generic hamiltonian algorithm ---" << endl;
#endif
vector<Pawn> pawns = board.getPawns();
vector<Move> moves;
int pteam = board.getPlayingTeam();
// compute available moves
for (int ipawn=0; ipawn<pawns.size(); ipawn++)
{
if (pawns[ipawn].getTeam() != pteam) continue;
int ivertexFrom = board.getVertexFromPawn(ipawn);
for (int ivertexTo: board.availableMovesDirect(ivertexFrom))
moves.push_back(Move(ivertexFrom, ivertexTo));
for (int ivertexTo: board.availableMovesHopping(ivertexFrom))
moves.push_back(Move(ivertexFrom, ivertexTo));
}
// compute weight of each move
double sumWeights = 0;
for (int i=0; i<moves.size(); i++)
{
double energy = hamiltonian(board, moves[i]);
moves[i].weight_ = exp(-energy/temperature);
sumWeights += moves[i].weight_;
#ifdef DEBUG
cout << "move from " << moves[i].ivertexFrom_
<< " to " << moves[i].ivertexTo_ << " : "
<< "energy = " << energy
<< " weight = " << moves[i].weight_ << endl;
#endif
}
// select move to perform
double ran = dist01(gen);
double cumulatedProba = 0;
for (Move move : moves)
{
cumulatedProba += move.weight_/sumWeights;
if (ran < cumulatedProba)
{
ipawnToMove = board.getPawnFromVertex(move.ivertexFrom_);
ivertexDestination = move.ivertexTo_;
#ifdef DEBUG
cout << "selected move from " << move.ivertexFrom_
<< " to " << move.ivertexTo_ << endl;
#endif
break;
}
}
}
#endif