-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPePePePlayer.java
More file actions
532 lines (489 loc) · 16.6 KB
/
Copy pathPePePePlayer.java
File metadata and controls
532 lines (489 loc) · 16.6 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import java.util.ArrayList;
import java.util.List;
import org.ggp.base.apps.player.detail.DetailPanel;
import org.ggp.base.apps.player.detail.SimpleDetailPanel;
import org.ggp.base.player.gamer.event.GamerSelectedMoveEvent;
import org.ggp.base.player.gamer.exception.GamePreviewException;
import org.ggp.base.player.gamer.statemachine.StateMachineGamer;
import org.ggp.base.util.game.Game;
import org.ggp.base.util.gdl.grammar.GdlTerm;
import org.ggp.base.util.statemachine.MachineState;
import org.ggp.base.util.statemachine.Move;
import org.ggp.base.util.statemachine.StateMachine;
import org.ggp.base.util.statemachine.cache.CachedStateMachine;
import org.ggp.base.util.statemachine.exceptions.GoalDefinitionException;
import org.ggp.base.util.statemachine.exceptions.MoveDefinitionException;
import org.ggp.base.util.statemachine.exceptions.TransitionDefinitionException;
import org.ggp.base.util.statemachine.implementation.prover.ProverStateMachine;
public class PePePePlayer extends StateMachineGamer {
static long MEGABYTE_TO_BYTE = 1048576; // 1024*1024 = 1 Mio
static long MIN_MEMORY_AVAILABLE = MEGABYTE_TO_BYTE * 50; // 10 Mio
static long MAX_PROBES_COUNT = 10000; // 10 Mio
int myRoleIndex;
int mDepthLimit;
int mMyMaxMobility;
int mMaxEnemyMobility;
int mMaxDepthReached;
/*
* Time limit to stop searching tree and start returning chosen move
*/
long mAuthorizedTime;
boolean mInitialization;// Boolean true before game starts
boolean debugMode;
StoredState mCurrentState;// Save the current state of the game
Move mMySelectedMove;
ArrayList<Long> depthTimeList = new ArrayList<Long>();
/*
* Array containing the time of each probe, which were sent during the
* current turn, took to find a terminal state
*/
ArrayList<Long> probeTimeList = new ArrayList<Long>();
/*
* Array containing count of states explored by probes, during this turn, to
* find a terminal state.
*/
ArrayList<Integer> probeDepthList = new ArrayList<Integer>();
@Override
public StateMachine getInitialStateMachine()
{
return new CachedStateMachine(new ProverStateMachine());
}
public void logDebug(String log)
{
if (debugMode)
{
System.out.println(log);
}
}
@Override
public void stateMachineMetaGame(long timeout) throws TransitionDefinitionException, MoveDefinitionException,
GoalDefinitionException
{
System.out.println("\n\nNew game started!!!");
System.out.println("Match id : " + getMatch().getMatchId());
System.out.println("Play clock = " + getMatch().getPlayClock());
System.out.println("Start clock = " + getMatch().getStartClock());
System.out.println("Preview clock = " + getMatch().getPreviewClock() + "\n\n");
/* This will return Long.MAX_VALUE if there is no preset limit */
long maxMemory = Runtime.getRuntime().maxMemory();
/* Maximum amount of memory the JVM will attempt to use */
System.out.println("Maximum memory (Mo): "
+ (maxMemory == Long.MAX_VALUE ? "no limit" : (maxMemory / MEGABYTE_TO_BYTE)));
System.out.println("Total memory (Mo): "
+ (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / MEGABYTE_TO_BYTE);
System.out.println("Memory available (Mo): "
+ (maxMemory + Runtime.getRuntime().freeMemory() - Runtime.getRuntime().totalMemory())
/ MEGABYTE_TO_BYTE);
mInitialization = true;
myRoleIndex = getStateMachine().getRoleIndices().get(getRole());
mCurrentState = new StoredState(getCurrentState(), getStateMachine(), getRole(), null);
/*
// Première phase : estimation de la profondeur qu'on peut analyser en
// cour de partie
long startDepthCalculation = System.currentTimeMillis();
mAuthorizedTime = System.currentTimeMillis() + getMatch().getPlayClock() * 1000 - 300;
mDepthLimit = 0;
depthTimeList.clear();
getMiniMaxMove(mCurrentState);
long currentTime = System.currentTimeMillis();
while (currentTime < mAuthorizedTime)
{
depthTimeList.add(currentTime - startDepthCalculation);
System.out.println("AlphaBeta calcul time to depth " + mDepthLimit + " : " + depthTimeList.get(mDepthLimit)
+ "ms");
mDepthLimit++;
startDepthCalculation = currentTime;
getMiniMaxMove(mCurrentState);
currentTime = System.currentTimeMillis();
}
System.out.println("AlphaBeta calcul time to depth " + mDepthLimit + " exceded timeout");
mDepthLimit = 1;
*/
// Deuxième pahse : send as many probes as possible before game starts
mAuthorizedTime = timeout - 300;
sendProbes(mCurrentState,mAuthorizedTime);
debugMode = false;
mInitialization = false;
}
@Override
public Move stateMachineSelectMove(long timeout) throws TransitionDefinitionException, MoveDefinitionException,
GoalDefinitionException
{
mAuthorizedTime = timeout - 1000;
System.out.println("\n\ntime to think = " + (timeout - System.currentTimeMillis()));
// Update mCurrentState according to played moves
List<GdlTerm> gdlMoves = getMatch().getMostRecentMoves();
if (gdlMoves != null)
{
boolean enemyMovesFound = false;
GdlTerm myLastGdlMove = gdlMoves.get(myRoleIndex);
if (!mMySelectedMove.getContents().equals(myLastGdlMove))
{
System.out.println("My last selected move (" + mMySelectedMove.toString()
+ ") different from last played move (" + myLastGdlMove.toString() + ")");
System.out.println("My last selected move (" + mMySelectedMove.toString()
+ ") different from last played move (" + myLastGdlMove.toString() + ")");
System.out.println("My last selected move (" + mMySelectedMove.toString()
+ ") different from last played move (" + myLastGdlMove.toString() + ")");
}
for (MyMove myMove : mCurrentState.getMyLegalMoves())
{
// Looking for right MyMove
if (myMove.getMove().getContents().equals(myLastGdlMove))
{
for (EnemyMove enemyMove : myMove.getEnemyMoveList())
{
enemyMovesFound = true;
for (int i = 0; i < enemyMove.getJointMoves().size(); ++i)
{
// Looking for right EnemyMove
if (!enemyMove.getJointMoves().get(i).getContents().equals(gdlMoves.get(i)))
{
enemyMovesFound = false;
break;
}
}
if (enemyMovesFound)
{
mCurrentState = enemyMove.getNextState();
mCurrentState.deleteParent();
System.out.println("enemyMovefound : " + enemyMove.getJointMoves().toString());
break;
}
}
break;
}
}
}
List<Move> calculatedMoves = new ArrayList<>();
for (MyMove myMove : mCurrentState.getMyLegalMoves())
{
calculatedMoves.add(myMove.getMove());
}
System.out.println("possible moves calculated are : " + calculatedMoves.toString());
if(calculatedMoves.size() > 1)
{
mMySelectedMove = getMonteCarloMove(mCurrentState);
/*
sendProbes(mCurrentState, mAuthorizedTime - depthTimeList.get(mDepthLimit) - 500);
mMySelectedMove = getMiniMaxMove(mCurrentState);
*/
}
else
{
sendProbes(mCurrentState, mAuthorizedTime);
mMySelectedMove = calculatedMoves.get(0);
}
//mMySelectedMove = getMonteCarloMove(mCurrentState);
long timeLeft = mAuthorizedTime - System.currentTimeMillis();
System.out.println(timeLeft + " ms left before timeout");
/* This will return Long.MAX_VALUE if there is no preset limit */
long maxMemory = Runtime.getRuntime().maxMemory();
/* Maximum amount of memory the JVM will attempt to use */
System.out.println("Maximum memory (Mo): "
+ (maxMemory == Long.MAX_VALUE ? "no limit" : (maxMemory / MEGABYTE_TO_BYTE)));
System.out.println("Total memory (Mo): " + Runtime.getRuntime().totalMemory() / MEGABYTE_TO_BYTE);
System.out.println("Memory available (Mo): " + (maxMemory - Runtime.getRuntime().totalMemory())
/ MEGABYTE_TO_BYTE);
notifyObservers(new GamerSelectedMoveEvent(calculatedMoves, mMySelectedMove, timeLeft));
System.gc();
return mMySelectedMove;
}
public long getAvailableMemory()
{
long maxMemory = Runtime.getRuntime().maxMemory();
return maxMemory - Runtime.getRuntime().totalMemory();
}
public Move getMonteCarloMove(StoredState currentState) throws GoalDefinitionException,
TransitionDefinitionException, MoveDefinitionException
{
// mDepthLimit = 1;
// expandState(currentState, 0);
probeTimeList.clear();
probeDepthList.clear();
long totalProbeTime = 0;
long totalProbeDepth = 0;
int[] depth = new int[1];
while (!timeoutReached() && probeTimeList.size() < MAX_PROBES_COUNT)
{
depth[0] = 0;
long probeStartTime = System.currentTimeMillis();
if (getAvailableMemory() > MIN_MEMORY_AVAILABLE)
{
currentState.sendProbe(depth);
} else
{
System.out.println("Not enough memory to launch new probes. Sending simple probes");
monteCarloProbe(currentState, depth);
}
probeTimeList.add(System.currentTimeMillis() - probeStartTime);
probeDepthList.add(depth[0]);
totalProbeTime += probeTimeList.get(probeTimeList.size() - 1);
totalProbeDepth += depth[0];
}
if (probeTimeList.size() > 0)
{
long averageProbeTime = totalProbeTime / probeTimeList.size();
System.out.println(probeTimeList.size() + " probes sent in " + totalProbeTime + "ms (average="
+ averageProbeTime + "ms)");
System.out.println("Probes explored " + totalProbeDepth + " state (average=" + totalProbeDepth
/ probeDepthList.size() + ")");
} else
{
System.out.println("No probe sent");
}
int bestScore = -1;
Move selection = null;
for (MyMove move : currentState.getMyLegalMoves())
{
if (move.getWorstScore() > bestScore)
{
bestScore = move.getWorstScore();
selection = move.getMove();
}
}
System.out.println("getMonteCarloMove choosing " + selection.toString() + " with score " + bestScore);
return selection;
}
public Move getMiniMaxMove(StoredState currentState) throws MoveDefinitionException, GoalDefinitionException,
TransitionDefinitionException
{
Move selection = currentState.getMyLegalMoves().get(0).getMove();
System.out.println("getMiniMaxMove : " + currentState.getMyLegalMoves().size() + " moves possible");
int miniMaxScore = 0;
for (int i = 0; i < currentState.getMyLegalMoves().size(); ++i)
{
System.out.println("getMiniMaxMove : test move " + i + "/" + currentState.getMyLegalMoves().size() + ": "
+ currentState.getMyLegalMoves().get(i).getMove().toString());
int tmpMaxScore = getMinScore(currentState.getMyLegalMoves().get(i), 0, 100, 0);
System.out.println(" tmpMaxScore=" + tmpMaxScore);
if (tmpMaxScore > miniMaxScore)
{
miniMaxScore = tmpMaxScore;
selection = currentState.getMyLegalMoves().get(i).getMove();
}
if (timeoutReached())
{
if (mDepthLimit > 0)
{
mDepthLimit--;
}
System.out.println("Time out reached in getMiniMaxMove so mDepthLimit=" + mDepthLimit);
break;
}
}
System.out.println("Choosing " + selection.toString() + " with score " + miniMaxScore);
return selection;
}
public int getMinScore(MyMove myMove, int alpha, int beta, int depth) throws TransitionDefinitionException,
GoalDefinitionException, MoveDefinitionException
{
for (int i = 0; i < myMove.getEnemyMoveList().size(); ++i)
{
StoredState nextState = myMove.getEnemyMoveList().get(i).getNextState();
int tmpMaxScore = getMaxScore(nextState, alpha, beta, depth + 1);
if (tmpMaxScore < alpha)
{
return alpha;
} else if (tmpMaxScore < beta)
{
beta = tmpMaxScore;
}
if (timeoutReached())
{
break;
}
}
return beta;
}
public boolean timeoutReached()
{
if (System.currentTimeMillis() >= mAuthorizedTime)
{
return true;
}
return false;
}
public int getMaxScore(StoredState currentState, int alpha, int beta, int depth)
throws TransitionDefinitionException, GoalDefinitionException, MoveDefinitionException
{
if (getStateMachine().isTerminal(currentState.getState()))
{
int score = getStateMachine().getGoal(currentState.getState(), getRole());
return score;
}
if (depth > mDepthLimit || timeoutReached())
{
return monteCarloEvaluation(currentState);
}
for (int i = 0; i < currentState.getMyLegalMoves().size(); ++i)
{
int tmpMinScore = getMinScore(currentState.getMyLegalMoves().get(i), alpha, beta, depth);
if (tmpMinScore == 100)
{
return 100;
} else if (tmpMinScore > alpha)
{
alpha = tmpMinScore;
} else if (tmpMinScore > beta)
{
return beta;
}
if (timeoutReached())
{
break;
}
}
return alpha;
}
public int monteCarloEvaluation(StoredState currentState) throws MoveDefinitionException,
TransitionDefinitionException
{
int bestScore = -1;
for (MyMove move : currentState.getMyLegalMoves())
{
if (move.getWorstScore() > bestScore)
{
bestScore = move.getWorstScore();
System.out.println("monteCarloEvaluation analyse move with " + move.getVisitCount() + " probes visited");
}
}
return bestScore;
}
public int monteCarloProbe(StoredState currentState, int[] depth) throws GoalDefinitionException,
TransitionDefinitionException, MoveDefinitionException
{
int score = 0;
MachineState terminalState = getStateMachine().performDepthCharge(currentState.getState(), depth);
score = getStateMachine().getGoal(terminalState, getRole());
currentState.UpdateScore(score);
return score;
}
public int evaluateScore(MachineState currentState) throws MoveDefinitionException
{
double score;
try
{
score = getStateMachine().getGoal(currentState, getRole());
} catch (GoalDefinitionException e)
{
int legalMovesCount = getStateMachine().getLegalMoves(currentState, getRole()).size();
if (legalMovesCount > mMyMaxMobility)
{
mMyMaxMobility = legalMovesCount;
score = 100;
} else
{
score = (legalMovesCount * 100) / mMyMaxMobility;
}
}
if (score == 0)
{
score++;
}
return (int) score;
}
@Override
public void stateMachineStop()
{
int myScore;
try
{
myScore = getStateMachine().getGoal(getCurrentState(), getRole());
System.out.println("\n\nmatch finished! I got " + myScore + "\n\n");
} catch (GoalDefinitionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
cleanUpEveryThing();
}
@Override
public void stateMachineAbort()
{
cleanUpEveryThing();
System.out.println("\n\nmatch aborted!");
}
@Override
public void preview(Game g, long timeout) throws GamePreviewException
{
// TODO Auto-generated method stub
}
@Override
public String getName()
{
return getClass().getSimpleName();
}
@Override
public DetailPanel getDetailPanel()
{
return new SimpleDetailPanel();
}
private void cleanUpEveryThing()
{
cleanupAfterMatch();
mCurrentState = null;
mMySelectedMove = null;
probeDepthList.clear();
probeTimeList.clear();
depthTimeList.clear();
setMatch(null);
System.gc();
}
private void sendProbes(StoredState currentState, long timeLimit ) throws GoalDefinitionException, TransitionDefinitionException, MoveDefinitionException
{
probeTimeList.clear();
probeDepthList.clear();
long totalProbeTime = 0;
long totalProbeDepth = 0;
int[] depth = new int[1];
while (System.currentTimeMillis() < timeLimit && probeTimeList.size() < MAX_PROBES_COUNT)
{
depth[0] = 0;
long probeStartTime = System.currentTimeMillis();
if (getAvailableMemory() > MIN_MEMORY_AVAILABLE)
{
currentState.sendProbe(depth);
} else
{
System.out.println("Not enough memory to launch new probes. Sending simple probes");
monteCarloProbe(currentState, depth);
}
probeTimeList.add(System.currentTimeMillis() - probeStartTime);
probeDepthList.add(depth[0]);
totalProbeTime += probeTimeList.get(probeTimeList.size() - 1);
totalProbeDepth += depth[0];
}
if (probeTimeList.size() > 0)
{
long averageProbeTime = totalProbeTime / probeTimeList.size();
System.out.println(probeTimeList.size() + " probes sent in " + totalProbeTime + "ms (average="
+ averageProbeTime + "ms)");
System.out.println("Probes explored " + totalProbeDepth + " state (average=" + totalProbeDepth
/ probeDepthList.size() + ")");
} else
{
System.out.println("No probe sent");
}
}
private int expandState(StoredState rootState, int depth) throws MoveDefinitionException,
TransitionDefinitionException
{
int stateCount = 0;
for (int i = 0; i < rootState.getMyLegalMoves().size(); ++i)
{
for (int j = 0; j < rootState.getMyLegalMoves().get(i).getEnemyMoveList().size(); ++j)
{
StoredState state = rootState.getMyLegalMoves().get(i).getEnemyMoveList().get(j).getNextState();
if (depth == mDepthLimit)
{
++stateCount;
} else
{
stateCount += expandState(state, depth + 1);
}
}
}
return stateCount;
}
}