-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffimax.js
More file actions
279 lines (229 loc) · 6.07 KB
/
Copy pathdiffimax.js
File metadata and controls
279 lines (229 loc) · 6.07 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
/**
* DiffiMax
*
* Minimax-based search with opponent-response analysis.
*
* Evaluations are from YOUR perspective:
* positive = good for you
* negative = good for opponent
*/
export const DEFAULT_CONFIG = {
responseScope: 2, // 2, 3, 4, 5, or "all"
reasonablePlayerDip: 2.0,
trapDipThreshold: 1.0,
minimumMeaningfulResponses: 2,
excludeChecks: true,
excludeCaptures: true
};
/**
* Run DiffiMax.
*
* @param {Object} root Search tree root
* @param {Object} options Configuration
* @returns {Object} Search result
*/
export function analyzeDiffiMax(root, options = {}) {
const config = {
...DEFAULT_CONFIG,
...options
};
const traps = [];
const evaluation = search(root, config, traps);
return {
evaluation,
traps
};
}
/**
* Recursively search the tree.
*
* The important distinction from ordinary minimax is that
* opponent nodes perform additional response analysis.
*/
function search(node, config, traps) {
if (!node.children || node.children.length === 0) {
return node.value ?? 0;
}
for (const child of node.children) {
child.value = search(child, config, traps);
}
if (node.player === node.yourPlayer) {
return chooseBestForYou(
node.children,
node.yourPlayer
).value;
}
return analyzeOpponentNode(node, config, traps);
}
/**
* Analyze an opponent decision.
*/
function analyzeOpponentNode(node, config, traps) {
const responses = getMeaningfulResponses(node, config);
if (responses.length < config.minimumMeaningfulResponses) {
return chooseBestForOpponent(
node.children,
node.yourPlayer
).value;
}
const ordered = [...responses].sort((a, b) => {
return opponentScore(a.value, node.yourPlayer)
- opponentScore(b.value, node.yourPlayer);
});
const best = ordered[0];
const considered = selectResponses(
ordered,
config.responseScope
);
const trap = findTrap(
best,
considered,
config
);
if (trap) {
const record = {
node,
bestResponse: best,
trapResponse: trap.response,
bestEvaluation: best.value,
trapEvaluation: trap.response.value,
evaluationDip: trap.dip,
responseCount: responses.length,
consideredResponses: considered,
responseScope: config.responseScope
};
traps.push(record);
/*
* DiffiMax follows the trap response for this branch.
*
* The overall search does not terminate here.
*/
return trap.response.value;
}
/*
* No trap at this node.
*
* Continue with ordinary minimax.
*/
return best.value;
}
/**
* Remove responses that should not participate in trap analysis.
*
* This prevents forced tactical moves from being treated as
* ordinary "mistakes".
*/
function getMeaningfulResponses(node, config) {
return node.children.filter(child => {
if (config.excludeChecks) {
if (node.isCheck || child.isCheck) {
return false;
}
}
if (config.excludeCaptures) {
if (node.isCapture || child.isCapture) {
return false;
}
if (child.isCapture) {
return false;
}
}
return true;
});
}
/**
* Select the requested response scope.
*/
function selectResponses(responses, scope) {
if (scope === "all") {
return [...responses];
}
const k = Number(scope);
if (![2, 3, 4, 5].includes(k)) {
throw new Error(
'responseScope must be 2, 3, 4, 5, or "all".'
);
}
return responses.slice(0, k);
}
/**
* Find a response that:
*
* 1. Is within the reasonable-player range.
* 2. Has at least the required trap dip.
*/
function findTrap(best, responses, config) {
const candidates = [];
for (const response of responses) {
if (response === best) {
continue;
}
const dip = response.value - best.value;
const isReasonable =
dip <= config.reasonablePlayerDip;
const isLargeEnough =
dip >= config.trapDipThreshold;
if (isReasonable && isLargeEnough) {
candidates.push({
response,
dip
});
}
}
if (candidates.length === 0) {
return null;
}
/*
* The largest meaningful dip is the strongest trap candidate.
*/
candidates.sort((a, b) => b.dip - a.dip);
return candidates[0];
}
/**
* Choose the best move for us.
*
* Evaluations are from our perspective.
*/
function chooseBestForYou(children, yourPlayer) {
return children.reduce((best, child) => {
if (yourPlayer === 1) {
return child.value > best.value
? child
: best;
}
return child.value < best.value
? child
: best;
});
}
/**
* Choose the best move for the opponent.
*/
function chooseBestForOpponent(children, yourPlayer) {
return children.reduce((best, child) => {
if (yourPlayer === 1) {
return child.value < best.value
? child
: best;
}
return child.value > best.value
? child
: best;
});
}
/**
* Score from the opponent's perspective.
*
* Our evaluations:
*
* + = good for us
* - = good for opponent
*
* Therefore the opponent wants the lowest value when
* we are White and the highest value when we are Black.
*/
function opponentScore(value, yourPlayer) {
return yourPlayer === 1
? value
: -value;
}