-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLimitedValueUCTPlayer.java
217 lines (171 loc) · 6.9 KB
/
LimitedValueUCTPlayer.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
package codecup2022.player;
import codecup2022.data.Board;
import codecup2022.data.Move;
import codecup2022.data.RolloutBoard;
import codecup2022.movegenerator.MoveGenerator;
import codecup2022.stopcriterion.IterationCount;
import codecup2022.stopcriterion.StopCriterion;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class LimitedValueUCTPlayer extends Player {
private static final int MAX_ITERATIONS = 200_000;
private final MoveGenerator generator;
private final StopCriterion stop;
private final int ucbLimit;
private final double explorationWeight;
private final double valueWeight;
private final Random rand;
public LimitedValueUCTPlayer(MoveGenerator generator, StopCriterion stop, int ucbLimit, double explorationWeight, double valueWeight, Random rand) {
super(String.format("LimValUCT-%s-%s-%d-%.2f-%.2f", generator.name(), stop.name(), ucbLimit, explorationWeight, valueWeight));
this.generator = generator;
this.stop = stop;
this.ucbLimit = ucbLimit;
this.explorationWeight = explorationWeight;
this.valueWeight = valueWeight;
this.rand = rand;
}
private double[] sqrt = null;
private double[] sqrtLog = null;
@Override
protected int selectMove() {
stop.started();
if (sqrt == null) {
precomputeSquareRoots();
}
TreeNode root = new TreeNode(null, -1, 0, board, isBlue());
int majority = (stop instanceof IterationCount ? ((IterationCount) stop).getMaxIterations() / 2 : MAX_ITERATIONS / 2);
int maxExpandedTurn = board.getTurn();
for (int i = 0; i < MAX_ITERATIONS && !stop.shouldStop(); i++) {
TreeNode node = root;
TreeNode firstMoveNode = null;
Board b = new RolloutBoard(board);
// Traverse the tree
while (node.isFullyExpanded()) {
node = node.selectMoveNode();
b.applyMove(node.move);
if (firstMoveNode == null) {
firstMoveNode = node;
}
}
// Expand
if (!node.isLeaf()) {
node = node.expand(b);
}
maxExpandedTurn = Math.max(maxExpandedTurn, b.getTurn());
// Play the rest of the game randomly
int[] score = rollout(b);
// Update nodes along the path we followed
while (node != null) {
node.updateValue(score);
node = node.parent;
}
// No improvement possible?
if (firstMoveNode != null && firstMoveNode.visits >= majority) {
break;
}
}
TreeNode result = root.mostVisitedChild();
if (Player.SCORE) {
System.err.printf("%s (%.2f, %d) D=%d%n", Move.toString(result.move), result.value, result.visits, maxExpandedTurn);
}
return result.move;
}
private int[] rollout(Board b) {
int[] empty = b.emptySpaces();
Move.shuffle(empty, rand);
for (int i = 0; i < empty.length; i++) {
b.applyMove(Move.setTile(empty[i], rand.nextInt(3)));
}
return new int[]{b.getScore(true), b.getScore(false)};
}
private void precomputeSquareRoots() {
sqrt = new double[MAX_ITERATIONS + 1];
sqrtLog = new double[MAX_ITERATIONS + 1];
for (int i = 0; i <= MAX_ITERATIONS; i++) {
sqrt[i] = Math.sqrt(i);
sqrtLog[i] = Math.sqrt(explorationWeight * Math.log(i));
}
}
private class TreeNode {
final TreeNode parent;
final int move;
final int moveScoreDelta;
final boolean blueToMove;
final int[] possibleMoves;
final List<TreeNode> children = new ArrayList<>();
int unexpandedMoveCount;
int visits = 0;
double value = 0;
private TreeNode(final TreeNode parent, final int move, final int moveScoreDelta, final Board b, final boolean blueToMove) {
this.parent = parent;
this.move = move;
this.moveScoreDelta = moveScoreDelta;
this.blueToMove = blueToMove;
this.possibleMoves = generator.generateMoves(b);
this.unexpandedMoveCount = possibleMoves.length;
}
private boolean isLeaf() {
return possibleMoves.length == 0;
}
private boolean isFullyExpanded() {
return unexpandedMoveCount == 0 && possibleMoves.length > 0;
}
private TreeNode expand(final Board b) {
// Pick a random move
final int moveIndex = rand.nextInt(unexpandedMoveCount);
final int childMove = possibleMoves[moveIndex];
// Compact possibleMoves so that the first unexpandedMoveCount moves are still unexpanded
unexpandedMoveCount--;
possibleMoves[moveIndex] = possibleMoves[unexpandedMoveCount];
int scoreBefore = b.getScore(blueToMove);
b.applyMove(childMove);
int scoreDelta = b.getScore(blueToMove) - scoreBefore;
final TreeNode child = new TreeNode(this, childMove, scoreDelta, b, !blueToMove);
children.add(child);
return child;
}
private TreeNode selectMoveNode() {
if (visits < ucbLimit) {
return randomChild();
} else {
return bestUCBChild();
}
}
private TreeNode randomChild() {
return children.get(rand.nextInt(children.size()));
}
private TreeNode bestUCBChild() {
TreeNode best = null;
double bestValue = Double.NEGATIVE_INFINITY;
final double parentVisitFactor = sqrtLog[visits];
for (final TreeNode child : children) {
final double ucbValue = child.isLeaf() ? child.value : child.value + (valueWeight * child.moveScoreDelta + parentVisitFactor) / sqrt[child.visits];
if (ucbValue > bestValue) {
bestValue = ucbValue;
best = child;
}
}
return best;
}
private TreeNode mostVisitedChild() {
TreeNode mostVisited = null;
int mostVisits = -1;
for (final TreeNode child : children) {
if (child.visits > mostVisits) {
mostVisited = child;
mostVisits = child.visits;
}
}
return mostVisited;
}
private void updateValue(final int score[]) {
// This node's value is used by the parent to pick the best move
// Therefore we should pick the score for our parent's color
final int nodeScore = blueToMove ? score[1] : score[0];
value = value * visits + nodeScore;
visits++;
value /= visits;
}
}
}