@woosh/meep-engine 2.138.13 → 2.138.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/bundle-worker-image-decoder.js +1 -1
- package/package.json +1 -1
- package/samples/terrain/from_image_2.js +2 -10
- package/src/engine/asset/loaders/image/ImageDecoderWorker.js +1 -1
- package/src/engine/asset/loaders/image/ImageRGBADataLoader.d.ts.map +1 -1
- package/src/engine/asset/loaders/image/ImageRGBADataLoader.js +6 -1
- package/src/engine/asset/loaders/image/png/PNGReader.d.ts +1 -99
- package/src/engine/asset/loaders/image/png/PNGReader.d.ts.map +1 -1
- package/src/engine/asset/loaders/image/png/PNGReader.js +31 -420
- package/src/engine/asset/loaders/image/png/chunk/png_chunk_decode_iTXt.d.ts +12 -0
- package/src/engine/asset/loaders/image/png/chunk/png_chunk_decode_iTXt.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/png/chunk/png_chunk_decode_iTXt.js +53 -0
- package/src/engine/asset/loaders/image/png/chunk/png_chunk_decode_zTXt.d.ts +10 -0
- package/src/engine/asset/loaders/image/png/chunk/png_chunk_decode_zTXt.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/png/chunk/png_chunk_decode_zTXt.js +42 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterAverage.d.ts +18 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterAverage.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterAverage.js +59 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterNone.d.ts +17 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterNone.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterNone.js +55 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterPaeth.d.ts +17 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterPaeth.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterPaeth.js +74 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterSub.d.ts +15 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterSub.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterSub.js +34 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterUp.d.ts +16 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterUp.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/png/filter/png_filter_unFilterUp.js +46 -0
- package/src/engine/asset/loaders/image/png/inflate.d.ts +7 -0
- package/src/engine/asset/loaders/image/png/inflate.d.ts.map +1 -0
- package/src/engine/asset/loaders/image/png/inflate.js +20 -0
- package/src/engine/ecs/terrain/ecs/Terrain.js +1 -1
- package/src/engine/ecs/terrain/ecs/splat/SplatMapping.d.ts.map +1 -1
- package/src/engine/ecs/terrain/ecs/splat/SplatMapping.js +32 -9
- package/src/engine/graphics/impostors/octahedral/prototypeBaker.js +202 -8
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderDepthV0.d.ts +10 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderDepthV0.d.ts.map +1 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderDepthV0.js +418 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderLitV0.d.ts +14 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderLitV0.d.ts.map +1 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderLitV0.js +757 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderNormalsV0.d.ts +13 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderNormalsV0.d.ts.map +1 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderNormalsV0.js +380 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderPerPixelV0.d.ts +6 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderPerPixelV0.d.ts.map +1 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderPerPixelV0.js +406 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderV0.d.ts.map +1 -1
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderV0.js +8 -1
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderViewportDepthV0.d.ts +14 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderViewportDepthV0.d.ts.map +1 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderViewportDepthV0.js +356 -0
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderWireframeV0.d.ts.map +1 -1
- package/src/engine/graphics/impostors/octahedral/shader/ImpostorShaderWireframeV0.js +4 -1
- package/src/engine/graphics/shaders/TerrainShader.js +2 -2
- package/src/engine/intelligence/mcts/MonteCarlo.d.ts +35 -4
- package/src/engine/intelligence/mcts/MonteCarlo.d.ts.map +1 -1
- package/src/engine/intelligence/mcts/MonteCarlo.js +101 -31
- package/src/engine/intelligence/mcts/StateNode.d.ts +47 -24
- package/src/engine/intelligence/mcts/StateNode.d.ts.map +1 -1
- package/src/engine/intelligence/mcts/StateNode.js +364 -316
|
@@ -1,316 +1,364 @@
|
|
|
1
|
-
import { assert } from "../../../core/assert.js";
|
|
2
|
-
import { MoveEdge } from "./MoveEdge.js";
|
|
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
|
-
node
|
|
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
|
-
const
|
|
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
|
-
|
|
1
|
+
import { assert } from "../../../core/assert.js";
|
|
2
|
+
import { MoveEdge } from "./MoveEdge.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Coarse classification of a node's terminal status, independent of payoff.
|
|
6
|
+
* The actual per-player payoff lives in {@link StateNode#outcome}.
|
|
7
|
+
* @enum {number}
|
|
8
|
+
*/
|
|
9
|
+
export const StateType = {
|
|
10
|
+
/** Game can still continue from this state. */
|
|
11
|
+
Undecided: 0,
|
|
12
|
+
/** Game ended at this state per the game's rules. Payoff in `outcome`. */
|
|
13
|
+
Terminal: 1,
|
|
14
|
+
/** Search hit `maxExplorationDepth` here; heuristic stands in for payoff. */
|
|
15
|
+
DepthCapped: 2,
|
|
16
|
+
/** Expansion produced no legal moves. Treated as terminal; payoff in `outcome`. */
|
|
17
|
+
NoMoves: 3
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Average outcome for `player` across all playouts that visited this child node.
|
|
22
|
+
* Falls back to heuristic when the node has not been rolled out yet.
|
|
23
|
+
* @param {MoveEdge} move
|
|
24
|
+
* @param {number} player
|
|
25
|
+
* @returns {number}
|
|
26
|
+
*/
|
|
27
|
+
function pickBestScore(move, player) {
|
|
28
|
+
assert.isNonNegativeInteger(player, 'player');
|
|
29
|
+
|
|
30
|
+
const node = move.target;
|
|
31
|
+
|
|
32
|
+
if (node.playouts === 0) {
|
|
33
|
+
return node.heuristicValue[player];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return node.totalScore[player] / node.playouts;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
let stack_pointer = 0;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @type {StateNode[]}
|
|
45
|
+
*/
|
|
46
|
+
const stack = [];
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @template State, Action
|
|
51
|
+
* @author Alex Goldring
|
|
52
|
+
* @copyright Company Named Limited (c) 2025
|
|
53
|
+
*/
|
|
54
|
+
export class StateNode {
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* How deep is the node in the tree
|
|
58
|
+
* @type {number}
|
|
59
|
+
*/
|
|
60
|
+
depth = 0;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Total number of explored playouts through this node.
|
|
64
|
+
* @type {number}
|
|
65
|
+
*/
|
|
66
|
+
playouts = 0;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Per-player heuristic estimate of the value of this state.
|
|
70
|
+
* For terminal nodes this mirrors {@link StateNode#outcome}.
|
|
71
|
+
* For internal nodes this is refined by maxN backup as children materialize.
|
|
72
|
+
* Index = player ID.
|
|
73
|
+
* @type {Float64Array | null}
|
|
74
|
+
*/
|
|
75
|
+
heuristicValue = null;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Per-player cumulative payoff aggregated by backpropagation.
|
|
79
|
+
* Average value for player `p` is `totalScore[p] / playouts` once `playouts > 0`.
|
|
80
|
+
* @type {Float64Array | null}
|
|
81
|
+
*/
|
|
82
|
+
totalScore = null;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Per-player payoff at this terminal node, cached at materialization.
|
|
86
|
+
* `null` for non-terminal nodes (uses {@link StateNode#heuristicValue} as a stand-in).
|
|
87
|
+
* @type {Float64Array | null}
|
|
88
|
+
*/
|
|
89
|
+
outcome = null;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Player ID of whoever chooses the next move from this state.
|
|
93
|
+
* `-1` for terminal nodes (no decision to make).
|
|
94
|
+
* @type {number}
|
|
95
|
+
*/
|
|
96
|
+
activePlayer = -1;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @type {null|StateNode}
|
|
100
|
+
*/
|
|
101
|
+
parent = null;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
*
|
|
105
|
+
* @type {null|MoveEdge[]}
|
|
106
|
+
*/
|
|
107
|
+
moves = null;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
*
|
|
111
|
+
* @type {StateType}
|
|
112
|
+
*/
|
|
113
|
+
type = StateType.Undecided;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Allocate the per-player buffers. Must be called before any backprop or selection.
|
|
117
|
+
* @param {number} numPlayers
|
|
118
|
+
*/
|
|
119
|
+
allocate(numPlayers) {
|
|
120
|
+
assert.isNonNegativeInteger(numPlayers, 'numPlayers');
|
|
121
|
+
|
|
122
|
+
this.heuristicValue = new Float64Array(numPlayers);
|
|
123
|
+
this.totalScore = new Float64Array(numPlayers);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
bubbleUpHeuristicScore() {
|
|
127
|
+
let r = this.parent;
|
|
128
|
+
|
|
129
|
+
while (r !== null) {
|
|
130
|
+
r.aggregateHeuristicScore();
|
|
131
|
+
|
|
132
|
+
r = r.parent;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* maxN backup: the active player picks the child that maximizes their own
|
|
138
|
+
* component; the chosen child's entire heuristic vector is copied here.
|
|
139
|
+
*
|
|
140
|
+
* This generalizes minimax across N players and asymmetric turn orders: the
|
|
141
|
+
* non-active components ride along, representing "what would happen to player Y
|
|
142
|
+
* if X plays the way X wants from this state".
|
|
143
|
+
*/
|
|
144
|
+
aggregateHeuristicScore() {
|
|
145
|
+
const moves = this.moves;
|
|
146
|
+
|
|
147
|
+
if (moves === null) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const p = this.activePlayer;
|
|
152
|
+
|
|
153
|
+
if (p < 0) {
|
|
154
|
+
// terminal node — no decision to make, heuristicValue already reflects outcome
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const own = this.heuristicValue;
|
|
159
|
+
let bestVec = own;
|
|
160
|
+
let bestVal = own[p];
|
|
161
|
+
|
|
162
|
+
const n = moves.length;
|
|
163
|
+
|
|
164
|
+
for (let i = 0; i < n; i++) {
|
|
165
|
+
/**
|
|
166
|
+
* @type {MoveEdge}
|
|
167
|
+
*/
|
|
168
|
+
const move = moves[i];
|
|
169
|
+
|
|
170
|
+
if (!move.isTargetMaterialized()) {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const childVec = move.target.heuristicValue;
|
|
175
|
+
const val = childVec[p];
|
|
176
|
+
|
|
177
|
+
if (val > bestVal) {
|
|
178
|
+
bestVal = val;
|
|
179
|
+
bestVec = childVec;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (bestVec !== own) {
|
|
184
|
+
own.set(bestVec);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @param state
|
|
190
|
+
* @param {function(State, source:StateNode):MoveEdge[]} computeValidMoves
|
|
191
|
+
* @param {function(State):number} computeActivePlayer
|
|
192
|
+
* @param {function(State):Float64Array} computeOutcome called when expansion produces no moves
|
|
193
|
+
* @returns {number} number of children
|
|
194
|
+
*/
|
|
195
|
+
expand(state, computeValidMoves, computeActivePlayer, computeOutcome) {
|
|
196
|
+
assert.defined(state, 'state');
|
|
197
|
+
assert.isFunction(computeValidMoves, 'computeValidMoves');
|
|
198
|
+
assert.isFunction(computeActivePlayer, 'computeActivePlayer');
|
|
199
|
+
assert.isFunction(computeOutcome, 'computeOutcome');
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
*
|
|
204
|
+
* @type {MoveEdge[]}
|
|
205
|
+
*/
|
|
206
|
+
const moves = computeValidMoves(state, this);
|
|
207
|
+
|
|
208
|
+
assert.notNull(moves, 'moves');
|
|
209
|
+
assert.defined(moves, 'moves');
|
|
210
|
+
assert.isArray(moves, 'moves');
|
|
211
|
+
|
|
212
|
+
const numMoves = moves.length;
|
|
213
|
+
|
|
214
|
+
this.moves = moves;
|
|
215
|
+
|
|
216
|
+
if (numMoves === 0) {
|
|
217
|
+
// No legal moves → terminal by exhaustion. Cache outcome so subsequent
|
|
218
|
+
// visits don't recompute, and seed heuristicValue with it.
|
|
219
|
+
// Copy into our own buffer so the user is free to reuse the source;
|
|
220
|
+
// outcome and heuristicValue share storage within the node (safe since
|
|
221
|
+
// terminal nodes are skipped by aggregateHeuristicScore).
|
|
222
|
+
this.type = StateType.NoMoves;
|
|
223
|
+
this.activePlayer = -1;
|
|
224
|
+
|
|
225
|
+
const out = computeOutcome(state);
|
|
226
|
+
|
|
227
|
+
assert.defined(out, 'out');
|
|
228
|
+
assert.isArrayLike(out, 'out');
|
|
229
|
+
|
|
230
|
+
this.heuristicValue.set(out);
|
|
231
|
+
this.outcome = this.heuristicValue;
|
|
232
|
+
} else {
|
|
233
|
+
|
|
234
|
+
this.activePlayer = computeActivePlayer(state);
|
|
235
|
+
|
|
236
|
+
assert.isNumber(this.activePlayer, 'activePlayer');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return numMoves;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Walk up from this node to the root, adding the per-player outcome to each
|
|
245
|
+
* ancestor's cumulative score and incrementing their visit count.
|
|
246
|
+
* @param {Float64Array} outcomeVector indexed by player ID
|
|
247
|
+
*/
|
|
248
|
+
backpropagate(outcomeVector) {
|
|
249
|
+
assert.defined(outcomeVector, 'outcomeVector');
|
|
250
|
+
assert.isArrayLike(outcomeVector, 'outcomeVector');
|
|
251
|
+
assert(outcomeVector.length > 0, 'outcomeVector must not be empty');
|
|
252
|
+
|
|
253
|
+
const len = outcomeVector.length;
|
|
254
|
+
let node = this;
|
|
255
|
+
|
|
256
|
+
do {
|
|
257
|
+
node.playouts += 1;
|
|
258
|
+
|
|
259
|
+
const score = node.totalScore;
|
|
260
|
+
for (let p = 0; p < len; p++) {
|
|
261
|
+
score[p] += outcomeVector[p];
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
node = node.parent;
|
|
265
|
+
|
|
266
|
+
} while (node !== null);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Whenever this is a terminal state or not (win/loss/tie/no-moves/depth-cap)
|
|
272
|
+
* @returns {boolean}
|
|
273
|
+
*/
|
|
274
|
+
isTerminal() {
|
|
275
|
+
return this.type !== StateType.Undecided;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
*
|
|
280
|
+
* @returns {boolean}
|
|
281
|
+
*/
|
|
282
|
+
isExpanded() {
|
|
283
|
+
return this.moves !== null;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Pick the best moves from this node's perspective — i.e. for whichever
|
|
288
|
+
* player owns the decision here.
|
|
289
|
+
* @returns {MoveEdge[]}
|
|
290
|
+
*/
|
|
291
|
+
pickBestMoves() {
|
|
292
|
+
const moves = this.moves;
|
|
293
|
+
|
|
294
|
+
if (moves === null) {
|
|
295
|
+
return [];
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const numMoves = moves.length;
|
|
299
|
+
|
|
300
|
+
if (numMoves === 0) {
|
|
301
|
+
return [];
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// For a decision node, score by the active player's component.
|
|
305
|
+
// For a terminal node (no active player), fall back to player 0 — caller
|
|
306
|
+
// shouldn't normally ask for best moves on a terminal but we don't crash.
|
|
307
|
+
const player = this.activePlayer >= 0 ? this.activePlayer : 0;
|
|
308
|
+
|
|
309
|
+
const firstMove = moves[0];
|
|
310
|
+
|
|
311
|
+
let result = [firstMove];
|
|
312
|
+
let bestScore = pickBestScore(firstMove, player);
|
|
313
|
+
|
|
314
|
+
for (let i = 1; i < numMoves; i++) {
|
|
315
|
+
|
|
316
|
+
const move = moves[i];
|
|
317
|
+
|
|
318
|
+
const score = pickBestScore(move, player);
|
|
319
|
+
|
|
320
|
+
if (score > bestScore) {
|
|
321
|
+
bestScore = score;
|
|
322
|
+
result = [move];
|
|
323
|
+
} else if (score === bestScore) {
|
|
324
|
+
result.push(move);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
*
|
|
333
|
+
* @param {function(StateNode)} visitor
|
|
334
|
+
*/
|
|
335
|
+
traverse(visitor) {
|
|
336
|
+
const stackOffset = stack_pointer;
|
|
337
|
+
|
|
338
|
+
stack[stack_pointer++] = this;
|
|
339
|
+
|
|
340
|
+
let n;
|
|
341
|
+
|
|
342
|
+
while (stack_pointer-- > stackOffset) {
|
|
343
|
+
|
|
344
|
+
n = stack[stack_pointer];
|
|
345
|
+
|
|
346
|
+
visitor(n);
|
|
347
|
+
|
|
348
|
+
if (n.isExpanded()) {
|
|
349
|
+
|
|
350
|
+
const moves = n.moves;
|
|
351
|
+
const numMoves = moves.length;
|
|
352
|
+
|
|
353
|
+
for (let i = 0; i < numMoves; i++) {
|
|
354
|
+
const moveEdge = moves[i];
|
|
355
|
+
|
|
356
|
+
if (moveEdge.isTargetMaterialized()) {
|
|
357
|
+
stack[stack_pointer++] = moveEdge.target;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
}
|
|
364
|
+
}
|