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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
use std::cmp;
use std::i32;
use analysis::{self, Evaluation as EvaluationTrait};
use impls::tak::Color;
use impls::tak::resolution::Resolution;
use impls::tak::state::State;
use impls::tak::state::metadata::{Bitmap, BitmapInterface, BOARD, EDGE, Metadata};
use state::State as StateTrait;
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Evaluation(pub i32);
impl Evaluation {
pub fn new(value: i32) -> Evaluation {
Evaluation(value)
}
}
prepare_evaluation_tuple!(Evaluation);
impl analysis::Evaluation for Evaluation {
fn null() -> Evaluation { Evaluation(0) }
fn shift(self, steps: i32) -> Evaluation { Evaluation(self.0 + steps) }
fn win() -> Evaluation { Evaluation(100_000) }
fn max() -> Evaluation { Evaluation(i32::MAX) }
fn is_win(&self) -> bool { self.0 >= 99_000 }
}
const END_GAME_FLATSTONE_THRESHOLD: [i32; 9] = [0, 0, 0, 5, 8, 10, 15, 20, 25];
struct Weights {
flatstone: (i32, i32),
standing_stone: i32,
capstone: i32,
hard_flat: (i32, i32, i32),
soft_flat: (i32, i32, i32),
threat: i32,
influence: (i32, i32, i32),
group: [i32; 8],
}
const WEIGHT: Weights = Weights {
flatstone: (400, 800),
standing_stone: 200,
capstone: 300,
hard_flat: (125, 125, 150),
soft_flat: (-75, -50, -25),
threat: 200,
influence: ( 20, 15, -5),
group: [0, 0, 100, 200, 400, 600, 0, 0],
};
#[derive(Clone)]
pub struct StaticEvaluator;
impl analysis::Evaluator for StaticEvaluator {
type State = State;
type Evaluation = Evaluation;
fn evaluate(&self, state: &State) -> Evaluation {
let next_color = if state.ply_count % 2 == 0 {
Color::White
} else {
Color::Black
};
match state.check_resolution() {
None => (),
Some(Resolution::Road(win_color)) |
Some(Resolution::Flat(win_color)) => {
if win_color == next_color {
return Evaluation::win().shift(-(state.ply_count as i32));
} else {
return Evaluation::lose().shift(state.ply_count as i32);
}
},
Some(Resolution::Draw) => return Evaluation::null(),
}
let mut p1_eval = 0;
let mut p2_eval = 0;
let m = &state.metadata;
let total_pieces = m.p1_pieces | m.p2_pieces;
let p1_flatstones = m.p1_pieces & !m.standing_stones & !m.capstones;
let p2_flatstones = m.p2_pieces & !m.standing_stones & !m.capstones;
let p1_standing_stones = m.p1_pieces & m.standing_stones;
let p2_standing_stones = m.p2_pieces & m.standing_stones;
let p1_capstones = m.p1_pieces & m.capstones;
let p2_capstones = m.p2_pieces & m.capstones;
let (p1_flatstone_weight, p2_flatstone_weight) = {
let flatstone_threshold = END_GAME_FLATSTONE_THRESHOLD[m.board_size];
let p1_position = cmp::min(state.p1_flatstones as i32, flatstone_threshold);
let p2_position = cmp::min(state.p2_flatstones as i32, flatstone_threshold);
(
WEIGHT.flatstone.0 * p1_position / flatstone_threshold +
WEIGHT.flatstone.1 * (flatstone_threshold - p1_position) / flatstone_threshold,
WEIGHT.flatstone.0 * p2_position / flatstone_threshold +
WEIGHT.flatstone.1 * (flatstone_threshold - p2_position) / flatstone_threshold,
)
};
p1_eval += evaluate_top_pieces(m.p1_flatstone_count as i32, p1_flatstone_weight, p1_standing_stones, p1_capstones);
p2_eval += evaluate_top_pieces(m.p2_flatstone_count as i32, p2_flatstone_weight, p2_standing_stones, p2_capstones);
{
let stacked_flatstones_eval = evaluate_stacked_flatstones(
m,
p1_flatstones,
p2_flatstones,
p1_standing_stones,
p2_standing_stones,
p1_capstones,
p2_capstones,
);
p1_eval += stacked_flatstones_eval.0;
p2_eval += stacked_flatstones_eval.1;
}
p1_eval += evaluate_road_groups(m, &m.p1_road_groups);
p2_eval += evaluate_road_groups(m, &m.p2_road_groups);
p1_eval += evaluate_threats(m, total_pieces, &m.p1_road_groups);
p2_eval += evaluate_threats(m, total_pieces, &m.p2_road_groups);
p1_eval += evaluate_influence(
m,
total_pieces,
m.p1_pieces,
&m.p1_flatstones,
p1_flatstones,
m.p2_pieces,
);
p2_eval += evaluate_influence(
m,
total_pieces,
m.p2_pieces,
&m.p2_flatstones,
p2_flatstones,
m.p1_pieces,
);
match next_color {
Color::White => Evaluation(p1_eval - p2_eval),
Color::Black => Evaluation(p2_eval - p1_eval),
}
}
}
fn evaluate_top_pieces(flatstone_count: i32, flatstone_weight: i32, standing_stones: Bitmap, capstones: Bitmap) -> i32 {
flatstone_count as i32 * flatstone_weight +
standing_stones.get_population() as i32 * WEIGHT.standing_stone +
capstones.get_population() as i32 * WEIGHT.capstone
}
fn evaluate_stacked_flatstones(
m: &Metadata,
p1_flatstones: Bitmap,
p2_flatstones: Bitmap,
p1_standing_stones: Bitmap,
p2_standing_stones: Bitmap,
p1_capstones: Bitmap,
p2_capstones: Bitmap,
) -> (i32, i32) {
let mut p1_flatstone_hard_flats = -(m.p1_flatstone_count as i32);
let mut p1_flatstone_soft_flats = 0;
let mut p1_standing_stone_hard_flats = 0;
let mut p1_standing_stone_soft_flats = 0;
let mut p1_capstone_hard_flats = 0;
let mut p1_capstone_soft_flats = 0;
for level in &m.p1_flatstones {
if *level != 0 {
p1_flatstone_hard_flats += (level & p1_flatstones).get_population() as i32;
p1_flatstone_soft_flats += (level & p2_flatstones).get_population() as i32;
p1_standing_stone_hard_flats += (level & p1_standing_stones).get_population() as i32;
p1_standing_stone_soft_flats += (level & p2_standing_stones).get_population() as i32;
p1_capstone_hard_flats += (level & p1_capstones).get_population() as i32;
p1_capstone_soft_flats += (level & p2_capstones).get_population() as i32;
}
}
let mut p2_flatstone_hard_flats = -(m.p2_flatstone_count as i32);
let mut p2_flatstone_soft_flats = 0;
let mut p2_standing_stone_hard_flats = 0;
let mut p2_standing_stone_soft_flats = 0;
let mut p2_capstone_hard_flats = 0;
let mut p2_capstone_soft_flats = 0;
for level in &m.p2_flatstones {
if *level != 0 {
p2_flatstone_hard_flats += (level & p2_flatstones).get_population() as i32;
p2_flatstone_soft_flats += (level & p1_flatstones).get_population() as i32;
p2_standing_stone_hard_flats += (level & p2_standing_stones).get_population() as i32;
p2_standing_stone_soft_flats += (level & p1_standing_stones).get_population() as i32;
p2_capstone_hard_flats += (level & p2_capstones).get_population() as i32;
p2_capstone_soft_flats += (level & p1_capstones).get_population() as i32;
}
}
let mut p1_eval = 0;
p1_eval += p1_flatstone_hard_flats * WEIGHT.hard_flat.0 + p2_flatstone_soft_flats * WEIGHT.soft_flat.0;
p1_eval += p1_standing_stone_hard_flats * WEIGHT.hard_flat.1 + p2_standing_stone_soft_flats * WEIGHT.soft_flat.1;
p1_eval += p1_capstone_hard_flats * WEIGHT.hard_flat.2 + p2_capstone_soft_flats * WEIGHT.soft_flat.2;
let mut p2_eval = 0;
p2_eval += p2_flatstone_hard_flats * WEIGHT.hard_flat.0 + p1_flatstone_soft_flats * WEIGHT.soft_flat.0;
p2_eval += p2_standing_stone_hard_flats * WEIGHT.hard_flat.1 + p1_standing_stone_soft_flats * WEIGHT.soft_flat.1;
p2_eval += p2_capstone_hard_flats * WEIGHT.hard_flat.2 + p1_capstone_soft_flats * WEIGHT.soft_flat.2;
(p1_eval, p2_eval)
}
fn evaluate_road_groups(m: &Metadata, groups: &[Bitmap]) -> i32 {
let mut eval = 0;
for group in groups {
let (width, height) = group.get_dimensions(m.board_size);
eval += WEIGHT.group[width] + WEIGHT.group[height];
}
eval
}
fn evaluate_threats(m: &Metadata, total_pieces: Bitmap, groups: &[Bitmap]) -> i32 {
let mut expanded_groups = vec![0; groups.len()];
let mut threats = 0;
let is_road = |group: Bitmap| {
use impls::tak::Direction::*;
if (group & EDGE[m.board_size][North as usize] != 0 &&
group & EDGE[m.board_size][South as usize] != 0) ||
(group & EDGE[m.board_size][West as usize] != 0 &&
group & EDGE[m.board_size][East as usize] != 0) {
return true;
}
false
};
for i in 0..groups.len() {
expanded_groups[i] = groups[i].grow(BOARD[m.board_size], m.board_size);
}
for l in 0..groups.len() {
for r in l..groups.len() {
if l != r {
let overlap = expanded_groups[l] & expanded_groups[r] & !total_pieces;
if overlap == 0 {
continue;
}
if is_road(groups[l] | groups[r] | overlap) {
threats += 1;
}
}
}
}
threats * WEIGHT.threat
}
fn evaluate_influence(
m: &Metadata,
total_pieces: Bitmap,
own_pieces: Bitmap,
own_stacks: &[Bitmap],
own_flatstones: Bitmap,
enemy_pieces: Bitmap,
) -> i32 {
fn add_bitmap(accumulator: &mut Vec<Bitmap>, mut bitmap: Bitmap) {
if accumulator.is_empty() {
accumulator.push(bitmap);
return;
}
let len = accumulator.len();
let carry = bitmap & accumulator[len - 1];
if carry != 0 {
accumulator[len - 1] ^= carry;
accumulator.push(carry);
bitmap ^= carry;
}
for level in (0..len - 1).rev() {
let carry = bitmap & accumulator[level];
accumulator[level] ^= carry;
accumulator[level + 1] |= carry;
bitmap ^= carry;
}
accumulator[0] |= bitmap;
}
let blocks = m.standing_stones | m.capstones;
let own_blocks = blocks & own_pieces;
let mut stack_levels = Vec::new();
add_bitmap(&mut stack_levels, own_blocks);
for &level in own_stacks {
add_bitmap(&mut stack_levels, level & own_pieces);
}
let add_influence = |influence: &mut Vec<Bitmap>, pieces: Bitmap, cast: usize| {
use impls::tak::Direction::*;
let mut cast_map = [pieces; 4];
for _ in 0..cast {
cast_map[North as usize] <<= m.board_size;
cast_map[East as usize] = (cast_map[East as usize] >> 1) & !EDGE[m.board_size][West as usize];
cast_map[South as usize] >>= m.board_size;
cast_map[West as usize] = (cast_map[West as usize] << 1) & !EDGE[m.board_size][East as usize];
add_bitmap(influence, cast_map[North as usize]);
add_bitmap(influence, cast_map[East as usize]);
add_bitmap(influence, cast_map[South as usize]);
add_bitmap(influence, cast_map[West as usize]);
cast_map[North as usize] &= !blocks;
cast_map[East as usize] &= !blocks;
cast_map[South as usize] &= !blocks;
cast_map[West as usize] &= !blocks;
}
};
let mut influence = Vec::new();
for (cast, &level) in stack_levels.iter().enumerate() {
add_influence(&mut influence, level, cast + 1);
}
{
let mut eval = 0;
for (level, map) in influence.iter().enumerate() {
eval += (map & own_flatstones).get_population() as i32 * (WEIGHT.influence.0 * (level as i32 + 1));
eval += (map & !total_pieces).get_population() as i32 * (WEIGHT.influence.1 * (level as i32 + 1));
eval += (map & enemy_pieces).get_population() as i32 * (WEIGHT.influence.2 >> level);
}
eval
}
}
#[cfg(test)]
mod test {
use std::cmp;
use test::{self, Bencher};
use analysis::Evaluator;
use impls::tak::*;
use super::{
END_GAME_FLATSTONE_THRESHOLD,
evaluate_influence,
evaluate_road_groups,
evaluate_stacked_flatstones,
evaluate_threats,
evaluate_top_pieces,
WEIGHT,
};
lazy_static! {
static ref STATE: State = State::from_tps("[TPS \"21,22221C,1,12212S,x/2121,2S,2,1S,2/x2,2,2,x/1,2111112C,2,x,21/x,1,21,x2 1 32\"]").unwrap();
}
#[bench]
fn bench_evaluate(b: &mut Bencher) {
b.iter(|| {
let evaluator = evaluator::StaticEvaluator;
evaluator.evaluate(test::black_box(&STATE))
});
}
#[bench]
fn bench_evaluate_top_pieces(b: &mut Bencher) {
let mut p1_eval = 0;
let mut p2_eval = 0;
let m = &STATE.metadata;
let p1_standing_stones = m.p1_pieces & m.standing_stones;
let p2_standing_stones = m.p2_pieces & m.standing_stones;
let p1_capstones = m.p1_pieces & m.capstones;
let p2_capstones = m.p2_pieces & m.capstones;
let (p1_flatstone_weight, p2_flatstone_weight) = {
let flatstone_threshold = END_GAME_FLATSTONE_THRESHOLD[m.board_size];
let p1_position = cmp::min(STATE.p1_flatstones as i32, flatstone_threshold);
let p2_position = cmp::min(STATE.p2_flatstones as i32, flatstone_threshold);
(
WEIGHT.flatstone.0 * p1_position / flatstone_threshold +
WEIGHT.flatstone.1 * (flatstone_threshold - p1_position) / flatstone_threshold,
WEIGHT.flatstone.0 * p2_position / flatstone_threshold +
WEIGHT.flatstone.1 * (flatstone_threshold - p2_position) / flatstone_threshold,
)
};
b.iter(|| {
p1_eval += test::black_box(evaluate_top_pieces(m.p1_flatstone_count as i32, p1_flatstone_weight, p1_standing_stones, p1_capstones));
p2_eval += test::black_box(evaluate_top_pieces(m.p2_flatstone_count as i32, p2_flatstone_weight, p2_standing_stones, p2_capstones));
});
}
#[bench]
fn bench_evaluate_stacked_flatstones(b: &mut Bencher) {
let mut p1_eval = 0;
let mut p2_eval = 0;
let m = &STATE.metadata;
let p1_flatstones = m.p1_pieces & !m.standing_stones & !m.capstones;
let p2_flatstones = m.p2_pieces & !m.standing_stones & !m.capstones;
let p1_standing_stones = m.p1_pieces & m.standing_stones;
let p2_standing_stones = m.p2_pieces & m.standing_stones;
let p1_capstones = m.p1_pieces & m.capstones;
let p2_capstones = m.p2_pieces & m.capstones;
b.iter(|| {
let stacked_flatstones_eval = test::black_box(evaluate_stacked_flatstones(
m,
p1_flatstones,
p2_flatstones,
p1_standing_stones,
p2_standing_stones,
p1_capstones,
p2_capstones,
));
p1_eval += stacked_flatstones_eval.0;
p2_eval += stacked_flatstones_eval.1;
});
}
#[bench]
fn bench_evaluate_road_groups(b: &mut Bencher) {
let mut p1_eval = 0;
let mut p2_eval = 0;
let m = &STATE.metadata;
b.iter(|| {
p1_eval += test::black_box(evaluate_road_groups(m, &m.p1_road_groups));
p2_eval += test::black_box(evaluate_road_groups(m, &m.p2_road_groups));
});
}
#[bench]
fn bench_evaluate_threats(b: &mut Bencher) {
let mut p1_eval = 0;
let mut p2_eval = 0;
let m = &STATE.metadata;
let total_pieces = m.p1_pieces | m.p2_pieces;
b.iter(|| {
p1_eval += test::black_box(evaluate_threats(m, total_pieces, &m.p1_road_groups));
p2_eval += test::black_box(evaluate_threats(m, total_pieces, &m.p2_road_groups));
});
}
#[bench]
fn bench_evaluate_influence(b: &mut Bencher) {
let mut p1_eval = 0;
let mut p2_eval = 0;
let m = &STATE.metadata;
let total_pieces = m.p1_pieces | m.p2_pieces;
let p1_flatstones = m.p1_pieces & !m.standing_stones & !m.capstones;
let p2_flatstones = m.p2_pieces & !m.standing_stones & !m.capstones;
b.iter(|| {
p1_eval += test::black_box(evaluate_influence(
m,
total_pieces,
m.p1_pieces,
&m.p1_flatstones,
p1_flatstones,
m.p2_pieces,
));
p2_eval += test::black_box(evaluate_influence(
m,
total_pieces,
m.p2_pieces,
&m.p2_flatstones,
p2_flatstones,
m.p1_pieces,
));
});
}
}