-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathExampleDUCT.java
362 lines (303 loc) · 10.1 KB
/
ExampleDUCT.java
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
package mcts;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import game.Game;
import main.collections.FastArrayList;
import other.AI;
import other.RankUtils;
import other.action.Action;
import other.context.Context;
import other.move.Move;
import utils.AIUtils;
/**
* A simple example implementation of Decoupled UCT, for simultaneous-move
* games. Note that this example is primarily intended to show how to build
* a search tree for simultaneous-move games in Ludii. This implementation
* is by no means intended to be an optimal (in terms of optimisations /
* computational efficiency) implementation of the algorithm.
*
* Only supports deterministic, simultaneous-move games.
*
* @author Dennis Soemers
*/
public class ExampleDUCT extends AI
{
//-------------------------------------------------------------------------
/** Our player index */
protected int player = -1;
//-------------------------------------------------------------------------
/**
* Constructor
*/
public ExampleDUCT()
{
this.friendlyName = "Example Decoupled UCT";
}
//-------------------------------------------------------------------------
@Override
public Move selectAction
(
final Game game,
final Context context,
final double maxSeconds,
final int maxIterations,
final int maxDepth
)
{
// Start out by creating a new root node (no tree reuse in this example)
final Node root = new Node(null, context);
// We'll respect any limitations on max seconds and max iterations (don't care about max depth)
final long stopTime = (maxSeconds > 0.0) ? System.currentTimeMillis() + (long) (maxSeconds * 1000L) : Long.MAX_VALUE;
final int maxIts = (maxIterations >= 0) ? maxIterations : Integer.MAX_VALUE;
int numIterations = 0;
// Our main loop through MCTS iterations
while
(
numIterations < maxIts && // Respect iteration limit
System.currentTimeMillis() < stopTime && // Respect time limit
!wantsInterrupt // Respect GUI user clicking the pause button
)
{
// Start in root node
Node current = root;
// Traverse tree
while (true)
{
if (current.context.trial().over())
{
// We've reached a terminal state
break;
}
current = select(current);
if (current.totalVisitCount == 0)
{
// We've expanded a new node, time for playout!
break;
}
}
Context contextEnd = current.context;
if (!contextEnd.trial().over())
{
// Run a playout if we don't already have a terminal game state in node
contextEnd = new Context(contextEnd);
game.playout
(
contextEnd,
null,
-1.0,
null,
0,
-1,
ThreadLocalRandom.current()
);
}
// This computes utilities for all players at the of the playout,
// which will all be values in [-1.0, 1.0]
final double[] utilities = RankUtils.utilities(contextEnd);
// Backpropagate utilities through the tree
while (current != null)
{
if (current.totalVisitCount > 0)
{
// This node was not newly expanded in this iteration
for (int p = 1; p <= game.players().count(); ++p)
{
if (current.visitCounts[p].length > 0)
{
current.visitCounts[p][current.lastSelectedMovesPerPlayer[p]] += 1;
current.scoreSums[p][current.lastSelectedMovesPerPlayer[p]] += utilities[p];
}
}
}
current.totalVisitCount += 1;
current = current.parent;
}
// Increment iteration count
++numIterations;
}
// Return the move we wish to play
return finalMoveSelection(root);
}
/**
* Selects child of the given "current" node according to UCB1 equation.
* This method also implements the "Expansion" phase of MCTS, and creates
* a new node if the given current node has unexpanded moves.
*
* @param current
* @return Selected node (if it has 0 visits, it will be a newly-expanded node).
*/
public static Node select(final Node current)
{
// Every player selects its move based on its own, decoupled statistics
final List<Action> playerMoves = new ArrayList<Action>();
final Game game = current.context.game();
final int numPlayers = game.players().count();
for (int p = 1; p <= numPlayers; ++p)
{
Move bestMove = null;
double bestValue = Double.NEGATIVE_INFINITY;
final double twoParentLog = 2.0 * Math.log(Math.max(1, current.totalVisitCount));
int numBestFound = 0;
final int numChildren = current.legalMovesPerPlayer.get(p).size();
for (int i = 0; i < numChildren; ++i)
{
final Move move = current.legalMovesPerPlayer.get(p).get(i);
final double exploit = (current.visitCounts[p][i] == 0) ? 1.0 : current.scoreSums[p][i] / current.visitCounts[p][i];
final double explore = Math.sqrt(twoParentLog / Math.max(1, current.visitCounts[p][i]));
final double ucb1Value = exploit + explore;
if (ucb1Value > bestValue)
{
bestValue = ucb1Value;
bestMove = move;
numBestFound = 1;
current.lastSelectedMovesPerPlayer[p] = i;
}
else if
(
ucb1Value == bestValue &&
ThreadLocalRandom.current().nextInt() % ++numBestFound == 0
)
{
// this case implements random tie-breaking
bestMove = move;
current.lastSelectedMovesPerPlayer[p] = i;
}
}
playerMoves.add(bestMove);
}
if (current.children.containsKey(playerMoves))
{
// We already have a node for this combination of moves
return current.children.get(playerMoves);
}
else
{
// We need to create a new node for this combination of moves
final Move combinedMove = new Move(playerMoves);
combinedMove.setMover(numPlayers + 1);
final Context context = new Context(current.context);
context.game().apply(context, combinedMove);
final Node newNode = new Node(current, context);
current.children.put(playerMoves, newNode);
return newNode;
}
}
/**
* Selects the move we wish to play as the one with the
* highest expected value.
*
* @param rootNode
* @return
*/
public Move finalMoveSelection(final Node rootNode)
{
Move bestMove = null;
double bestAvgScore = Double.NEGATIVE_INFINITY;
int numBestFound = 0;
final int numChildren = rootNode.legalMovesPerPlayer.get(player).size();
for (int i = 0; i < numChildren; ++i)
{
final Move move = rootNode.legalMovesPerPlayer.get(player).get(i);
final double sumScores = rootNode.scoreSums[player][i];
final int visitCount = rootNode.visitCounts[player][i];
final double avgScore = (visitCount == 0) ? -1.0 : sumScores / visitCount;
if (avgScore > bestAvgScore)
{
bestAvgScore = avgScore;
bestMove = move;
numBestFound = 1;
}
else if
(
avgScore == bestAvgScore &&
ThreadLocalRandom.current().nextInt() % ++numBestFound == 0
)
{
// this case implements random tie-breaking
bestMove = move;
}
}
return bestMove;
}
@Override
public void initAI(final Game game, final int playerID)
{
this.player = playerID;
}
@Override
public boolean supportsGame(final Game game)
{
// Don't allow stochastic games
if (game.isStochasticGame())
return false;
// Don't allow games which are NOT simultaneous-move games
if (game.isAlternatingMoveGame())
return false;
return true;
}
//-------------------------------------------------------------------------
/**
* Inner class for nodes used by example Decoupled UCT
*
* @author Dennis Soemers
*/
private static class Node
{
/** Our parent node */
private final Node parent;
/** This objects contains the game state for this node (this is why we don't support stochastic games) */
private final Context context;
/** Total visit count going through this node */
private int totalVisitCount = 0;
/** For every player, for every child move, a visit count */
private final int[][] visitCounts;
/** For every player, for every child move, a sum of backpropagated scores */
private final double[][] scoreSums;
/** Mapping from lists of actions (one per active player) to child nodes */
private final Map<List<Action>, Node> children = new HashMap<List<Action>, Node>();
/**
* For every player, the index of the legal move we selected for
* that player in this node in the last (current) MCTS iteration.
*/
private final int[] lastSelectedMovesPerPlayer;
/** For every player index, a list of legal moves in this node */
private final List<FastArrayList<Move>> legalMovesPerPlayer;
/**
* Constructor
*
* @param parent
* @param context
*/
public Node(final Node parent, final Context context)
{
this.parent = parent;
this.context = context;
final Game game = context.game();
final int numPlayers = game.players().count();
final FastArrayList<Move> allLegalMoves = game.moves(context).moves();
// For every active player in this state, compute their legal moves
legalMovesPerPlayer = new ArrayList<FastArrayList<Move>>(numPlayers + 1);
legalMovesPerPlayer.add(null);
for (int p = 1; p <= numPlayers; ++p)
{
legalMovesPerPlayer.add(AIUtils.extractMovesForMover(allLegalMoves, p));
}
// Prepare some arrays
visitCounts = new int[numPlayers + 1][];
for (int p = 1; p <= numPlayers; ++p)
{
visitCounts[p] = new int[legalMovesPerPlayer.get(p).size()];
}
scoreSums = new double[numPlayers + 1][];
for (int p = 1; p <= numPlayers; ++p)
{
scoreSums[p] = new double[legalMovesPerPlayer.get(p).size()];
}
lastSelectedMovesPerPlayer = new int[numPlayers + 1];
}
}
//-------------------------------------------------------------------------
}