-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.c
769 lines (682 loc) · 23.9 KB
/
solver.c
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
// TODO <signal.h> catch Ctrl+C
#include <omp.h>
// No headers? Are you serious?!
#define MAIN
#include "state.c"
#include "dict.c"
#include "node.c"
#include "solver_common.c"
#include "utils.c"
// Where's the makefile? Oh, you gotta be kidding me.
// gcc -std=gnu99 -Wall -O3 solver.c -o solver -fopenmp; solver 4 3
void iterate(solution *sol, char *filename) {
size_t num_states = num_keys(sol->d);
int changed = 1;
while (changed) {
changed = 0;
for (size_t j = 0; j < sol->num_layers; j++) {
size_t key;
size_t i;
int tid;
int num_threads;
#pragma omp parallel private(i, key, tid, num_threads)
{
num_threads = omp_get_num_threads();
state s_;
state *s = &s_;
tid = omp_get_thread_num();
key = sol->d->min_key;
for (i = 0; i < num_states; i++) {
if (i % num_threads == tid) {
assert(from_key_s(sol, s, key, j));
node_value new_v = negamax_node(sol, s, key, j, 1);
assert(new_v.low >= sol->base_nodes[j][i].low);
assert(new_v.high <= sol->base_nodes[j][i].high);
changed = changed || !equal(sol->base_nodes[j][i], new_v);
sol->base_nodes[j][i] = new_v;
}
key = next_key(sol->d, key);
}
#pragma omp for
for (i = 0; i < sol->ko_ld->num_keys; i++) {
key = sol->ko_ld->keys[i];
assert(from_key_ko(sol, s, key, j));
node_value new_v = negamax_node(sol, s, key, j, 1);
assert(new_v.low >= sol->ko_nodes[j][i].low);
assert(new_v.high <= sol->ko_nodes[j][i].high);
changed = changed || !equal(sol->ko_nodes[j][i], new_v);
sol->ko_nodes[j][i] = new_v;
}
}
}
size_t base_layer;
size_t base_key = to_key_s(sol, sol->base_state, &base_layer);
printf("Saving...\n"); // TODO: Prevent data corruption by catching Ctrl+C.
FILE *f = fopen(filename, "wb");
save_solution(sol, f);
fclose(f);
print_node(negamax_node(sol, sol->base_state, base_key, base_layer, 0));
// Verify solution integrity. For debugging only. Leaks memory.
// char *buffer = file_to_buffer(filename);
// buffer = load_solution(sol, buffer, 0);
}
}
void endstate(solution *sol, state *s, node_value parent_v, int turn, int low_player) {
if (s->passes == 2 || target_dead(s)) {
if (turn) {
stones_t temp = s->player;
s->player = s->opponent;
s->opponent = temp;
}
return;
}
state child_;
state *child = &child_;
for (int j = 0; j < sol->si->num_moves; j++) {
*child = *s;
stones_t move = sol->si->moves[j];
int prisoners;
if (make_move(child, move, &prisoners)) {
size_t child_layer;
size_t child_key = to_key_s(sol, child, &child_layer);
node_value child_v = negamax_node(sol, child, child_key, child_layer, 0);
node_value child_v_p = add_prisoners(sol, child_v, prisoners);
int is_best_child = (-child_v_p.high == parent_v.low && child_v_p.high_distance + 1 == parent_v.low_distance);
is_best_child = low_player ? is_best_child : (-child_v_p.low == parent_v.high && child_v_p.low_distance + 1 == parent_v.high_distance);
if (is_best_child) {
*s = *child;
endstate(sol, s, child_v, !turn, !low_player);
return;
}
}
}
}
// Japanese leaf state calculation from capture data.
// We could store territory for the UI, it takes too much space.
void calculate_leaves(solution *sol) {
state s_;
state *s = &s_;
size_t key;
size_t zero_layer = abs(sol->base_state->ko_threats);
state new_s_;
state *new_s = &new_s_;
key = sol->d->min_key;
size_t num_states = num_keys(sol->d);
for (size_t i = 0; i < num_states; i++) {
assert(from_key_s(sol, s, key, zero_layer));
*new_s = *s;
node_value v = negamax_node(sol, s, key, zero_layer, 0);
node_value v_b = sol->base_nodes[zero_layer][i];
assert(equal(v, v_b));
endstate(sol, new_s, v, 0, 1);
// Use a flood of life so that partially dead nakade won't give extra points.
// Note while this won't mark dead groups as alive, it can treat living nakade stones as dead.
stones_t player_alive = flood(new_s->player, s->player);
stones_t opponent_alive = flood(new_s->opponent, s->opponent);
int score;
// First check if a target is not alive.
stones_t player_target = s->player & s->target;
stones_t opponent_target = s->opponent & s->target;
if (opponent_target & ~opponent_alive) {
// Make sure that both aren't dead.
assert(!(player_target & ~player_alive));
score = TARGET_SCORE;
}
else if (player_target & ~player_alive) {
score = -TARGET_SCORE;
}
else {
stones_t player_territory = 0;
stones_t opponent_territory = 0;
stones_t player_region_space = s->playing_area & ~player_alive;
stones_t opponent_region_space = s->playing_area & ~opponent_alive;
for (int j = 0; j < STATE_SIZE; j++) {
stones_t p = 1ULL << j;
stones_t region = flood(p, player_region_space);
player_region_space ^= region;
if (!(region & opponent_alive)) {
player_territory |= region;
}
region = flood(p, opponent_region_space);
opponent_region_space ^= region;
if (!(region & player_alive)) {
opponent_territory |= region;
}
}
// Subtract friendly stones on the board from territory.
player_territory &= ~s->player;
opponent_territory &= ~s->opponent;
score = popcount(player_territory) + popcount(player_territory & s->opponent) - popcount(opponent_territory) - popcount(opponent_territory & s->player);
}
sol->leaf_nodes[i] = score;
key = next_key(sol->d, key);
}
}
static const char* tsumego_name = NULL;
static int board_width = 0;
static int board_height = 0;
static int ko_threats = 0;
static int num_layers = -1;
static int string_is_number(const char* string) {
const size_t length = strlen(string);
size_t i;
if (!length) {
return 0;
}
for (i = 0; i < length; ++i) {
if (!isdigit(string[i])) {
return 0;
}
}
return 1;
}
static void parse_args(const int argc, char** argv) {
int i;
for (i = 0; i < argc; ++i) {
const char* argument = argv[i];
if (*argument != '-') {
if (string_is_number(argument)) {
if (!board_width) {
board_width = atoi(argument);
}
else if (!board_height) {
board_height = atoi(argument);
} else {
assert(0);
}
}
else if (!tsumego_name) {
tsumego_name = argument;
} else {
assert(0);
}
continue;
}
else if (argument[1] == 'k') {
ko_threats = atoi(argument + 2);
}
else if (argument[1] == 'l') {
num_layers = atoi(argument + 2);
} else {
assert(0);
}
}
if (board_width) {
assert(board_height);
assert(!tsumego_name);
}
else if (!tsumego_name) {
assert(0);
}
}
typedef struct tsumego_info {
const char* name;
state* state;
} tsumego_info;
void test();
void repair(int argc, char *argv[]);
void upgrade(int argc, char *argv[]);
int main(int argc, char *argv[]) {
#ifdef TEST
test();
return 0;
#endif
#ifdef REPAIR
repair(argc, argv);
return 0;
#endif
#ifdef UPGRADE
upgrade(argc, argv);
return 0;
#endif
int load_sol = 0;
int resume_sol = 0;
if (strcmp(argv[argc - 1], "load") == 0) {
load_sol = 1;
argc--;
}
if (strcmp(argv[argc - 1], "resume") == 0) {
resume_sol = 1;
argc--;
}
parse_args(argc - 1, argv + 1);
int width = board_width;
int height = board_height;
if (board_width >= 10) {
fprintf(stderr, "Width must be less than 10.\n");
exit(EXIT_FAILURE);
}
if (board_height >= 8) {
fprintf(stderr, "Height must be less than 8.\n");
exit(EXIT_FAILURE);
}
#include "tsumego.c"
state base_state_;
state *base_state = &base_state_;
char sol_name[64] = "unknown";
char temp_filename[128];
char filename[128];
if (board_width > 0) {
*base_state = (state) {rectangle(width, height), 0, 0, 0, 0};
sprintf(sol_name, "%dx%d", width, height);
}
else {
int i;
int found = 0;
for (i = 0; tsumego_infos[i].name; ++i) {
if (!strcmp(tsumego_name, tsumego_infos[i].name)) {
*base_state = *(tsumego_infos[i].state);
strcpy(sol_name, tsumego_name);
found = 1;
break;
}
}
if (!found) {
fprintf(stderr, "unknown tsumego: `%s'\n", tsumego_name);
exit(EXIT_FAILURE);
}
}
base_state->ko_threats = ko_threats;
sprintf(temp_filename, "%s_temp.dat", sol_name);
state_info si_;
state_info *si = &si_;
init_state(base_state, si);
if (si->color_symmetry) {
num_layers = 2 * abs(base_state->ko_threats) + 1;
}
else if (num_layers <= 0) {
num_layers = abs(base_state->ko_threats) + 1;
}
else {
assert(num_layers >= abs(base_state->ko_threats) + 1);
}
print_state(base_state);
for (int i = 0; i < si->num_external; i++) {
print_stones(si->externals[i]);
}
printf(
"width=%d height=%d c=%d v=%d h=%d d=%d\n",
si->width,
si->height,
si->color_symmetry,
si->mirror_v_symmetry,
si->mirror_h_symmetry,
si->mirror_d_symmetry
);
state s_;
state *s = &s_;
dict d_;
dict *d = &d_;
solution sol_;
solution *sol = &sol_;
sol->base_state = base_state;
sol->si = si;
sol->d = d;
sol->num_layers = num_layers;
size_t num_states;
// Re-used at frontend. TODO: Allocate a different pointer.
state child_;
state *child = &child_;
if (load_sol) {
goto frontend;
}
if (resume_sol) {
char *buffer = file_to_buffer(temp_filename);
buffer = load_solution(sol, buffer, 1);
num_states = num_keys(sol->d);
if (sol->leaf_rule == japanese_double_liberty) {
goto iterate_capture;
}
else {
goto iterate_japanese;
}
}
size_t k_size = key_size(sol->si);
if (!sol->si->color_symmetry) {
k_size *= 2;
}
init_dict(sol->d, k_size);
size_t total_legal = 0;
for (size_t k = 0; k < k_size; k++) {
if (!from_key_s(sol, s, k, 0)){
continue;
}
total_legal++;
size_t layer;
size_t key = to_key_s(sol, s, &layer);
assert(layer == 0);
add_key(sol->d, key);
}
finalize_dict(sol->d);
num_states = num_keys(sol->d);
printf("Total positions %zu\n", total_legal);
printf("Total unique positions %zu\n", num_states);
node_value **base_nodes = (node_value**) malloc(sol->num_layers * sizeof(node_value*));
for (size_t i = 0; i < sol->num_layers; i++) {
base_nodes[i] = (node_value*) malloc(num_states * sizeof(node_value));
}
value_t *leaf_nodes = (value_t*) malloc(num_states * sizeof(value_t));
lin_dict ko_ld_ = (lin_dict) {0, 0, 0, NULL};
lin_dict *ko_ld = &ko_ld_;
sol->base_nodes = base_nodes;
sol->leaf_nodes = leaf_nodes;
sol->ko_ld = ko_ld;
size_t child_key;
size_t key = sol->d->min_key;
for (size_t i = 0; i < num_states; i++) {
assert(from_key_s(sol, s, key, 0));
// size_t layer;
// assert(to_key_s(sol, s, &layer) == key);
sol->leaf_nodes[i] = 0;
for (size_t k = 0; k < sol->num_layers; k++) {
(sol->base_nodes[k])[i] = (node_value) {VALUE_MIN, VALUE_MAX, DISTANCE_MAX, DISTANCE_MAX};
}
for (int j = 0; j < STATE_SIZE; j++) {
*child = *s;
int prisoners;
if (make_move(child, 1ULL << j, &prisoners)) {
if (target_dead(child)) {
continue;
}
if (child->ko) {
size_t child_layer;
child_key = to_key_s(sol, child, &child_layer);
add_lin_key(sol->ko_ld, child_key);
}
}
}
key = next_key(sol->d, key);
}
finalize_lin_dict(sol->ko_ld);
node_value **ko_nodes = (node_value**) malloc(sol->num_layers * sizeof(node_value*));
sol->ko_nodes = ko_nodes;
for (size_t i = 0; i < sol->num_layers; i++) {
sol->ko_nodes[i] = (node_value*) malloc(sol->ko_ld->num_keys * sizeof(node_value));
}
printf("Unique positions with ko %zu\n", sol->ko_ld->num_keys);
for (size_t i = 0; i < sol->ko_ld->num_keys; i++) {
for (size_t k = 0; k < sol->num_layers; k++) {
sol->ko_nodes[k][i] = (node_value) {VALUE_MIN, VALUE_MAX, DISTANCE_MAX, DISTANCE_MAX};
}
}
#ifdef CHINESE
printf("Negamax with Chinese rules.\n");
sol->count_prisoners = 0;
sol->leaf_rule = chinese_liberty;
iterate(sol, temp_filename);
#endif
// NOTE: Capture rules may refuse to kill stones when the needed nakade sacrifices exceed triple the number of stones killed.
printf("Negamax with capture rules.\n");
sol->count_prisoners = 1;
sol->leaf_rule = japanese_double_liberty;
iterate_capture:
iterate(sol, temp_filename);
sprintf(filename, "%s_capture.dat", sol_name);
FILE *f = fopen(filename, "wb");
save_solution(sol, f);
fclose(f);
calculate_leaves(sol);
// Clear the rest of the tree.
for (size_t j = 0; j < sol->num_layers; j++) {
for (size_t i = 0; i < num_states; i++) {
sol->base_nodes[j][i] = (node_value) {VALUE_MIN, VALUE_MAX, DISTANCE_MAX, DISTANCE_MAX};
}
for (size_t i = 0; i < sol->ko_ld->num_keys; i++) {
sol->ko_nodes[j][i] = (node_value) {VALUE_MIN, VALUE_MAX, DISTANCE_MAX, DISTANCE_MAX};
}
}
printf("Negamax with Japanese rules.\n");
sol->count_prisoners = 1;
sol->leaf_rule = precalculated;
iterate_japanese:
iterate(sol, temp_filename);
sprintf(filename, "%s_japanese.dat", sol_name);
f = fopen(filename, "wb");
save_solution(sol, f);
fclose(f);
frontend:
if (load_sol) {
sprintf(filename, "%s_japanese.dat", sol_name);
char *buffer = file_to_buffer(filename);
buffer = load_solution(sol, buffer, 1);
}
*s = *sol->base_state;
char coord1;
int coord2;
int total_prisoners = 0;
int turn = 0;
while (1) {
size_t layer;
size_t key = to_key_s(sol, s, &layer);
node_value v = negamax_node(sol, s, key, layer, 0);
print_state(s);
if (turn) {
print_node((node_value) {total_prisoners - v.high, total_prisoners - v.low, v.high_distance, v.low_distance});
}
else {
print_node((node_value) {total_prisoners + v.low, total_prisoners + v.high, v.low_distance, v.high_distance});
}
if (target_dead(s) || s->passes >= 2) {
break;
}
for (int j = -1; j < STATE_SIZE; j++) {
*child = *s;
stones_t move;
if (j == -1){
move = 0;
}
else {
move = 1ULL << j;
}
char c1 = 'A' + (j % WIDTH);
char c2 = '0' + (j / WIDTH);
int prisoners;
if (make_move(child, move, &prisoners)) {
size_t child_layer;
size_t child_key = to_key_s(sol, child, &child_layer);
node_value child_v = negamax_node(sol, child, child_key, child_layer, 0);
if (sol->count_prisoners) {
if (child_v.low > VALUE_MIN && child_v.low < VALUE_MAX) {
child_v.low = child_v.low - prisoners;
}
if (child_v.high > VALUE_MIN && child_v.high < VALUE_MAX) {
child_v.high = child_v.high - prisoners;
}
}
if (move) {
printf("%c%c", c1, c2);
}
else {
printf("pass");
}
if (-child_v.high == v.low) {
printf("-");
if (child_v.high_distance + 1 == v.low_distance) {
printf("L");
}
else {
printf("l");
}
}
if (-child_v.high == v.low) {
printf("-");
if (child_v.low_distance + 1 == v.high_distance) {
printf("H");
}
else {
printf("h");
}
}
printf(" ");
}
}
printf("\n");
printf("Enter coordinates:\n");
assert(scanf("%c %d", &coord1, &coord2));
int c;
while ((c = getchar()) != '\n' && c != EOF);
coord1 = tolower(coord1) - 'a';
stones_t move;
if (coord1 < 0 || coord1 >= WIDTH) {
// printf("%d, %d\n", coord1, coord2);
move = 0;
}
else {
move = 1ULL << (coord1 + V_SHIFT * coord2);
}
int prisoners;
if (make_move(s, move, &prisoners)) {
if (turn) {
total_prisoners -= prisoners;
}
else {
total_prisoners += prisoners;
}
turn = !turn;
}
}
return 0;
}
void test() {
solution sol_;
solution *sol = &sol_;
char *buffer = file_to_buffer("cho589_capture.dat");
buffer = load_solution(sol, buffer, 1);
state s_;
state *s = &s_;
// sscanf_state("4337799298623 4335912108032 1879319553 0 1879048192 4335912108032 0 0 0", s);
sscanf_state("4337799298623 4335918405632 1879319581 0 1879048192 4335912108032 0 0 0", s);
print_state(s);
size_t layer;
size_t key = to_key_s(sol, s, &layer);
// node_value v = negamax_node(sol, s, key, layer, 0);
// print_node(v);
// node_value v_b = sol->base_nodes[layer][key_index(sol->d, key)];
// print_node(v_b);
// endstate(sol, s, v, 0, 1);
// print_state(s);
state new_s_;
state *new_s = &new_s_;
*new_s = *s;
node_value v = negamax_node(sol, s, key, 0, 0);
endstate(sol, new_s, v, 0, 1);
print_state(new_s);
// Use a flood of life so that partially dead nakade won't give extra points.
// Note while this won't mark dead groups as alive, it can treat living nakade stones as dead.
stones_t player_alive = flood(new_s->player, s->player);
stones_t opponent_alive = flood(new_s->opponent, s->opponent);
print_stones(player_alive);
print_stones(opponent_alive);
int score;
// First check if a target is not alive.
stones_t player_target = s->player & s->target;
stones_t opponent_target = s->opponent & s->target;
if (opponent_target & ~opponent_alive) {
assert(!(player_target & ~player_alive)); // Both shouldn't be dead.
score = TARGET_SCORE;
}
else if (player_target & ~player_alive) {
score = -TARGET_SCORE;
}
else {
stones_t player_territory = 0;
stones_t opponent_territory = 0;
stones_t player_region_space = s->playing_area & ~player_alive;
stones_t opponent_region_space = s->playing_area & ~opponent_alive;
for (int j = 0; j < STATE_SIZE; j++) {
stones_t p = 1ULL << j;
stones_t region = flood(p, player_region_space);
player_region_space ^= region;
if (!(region & opponent_alive)) {
player_territory |= region;
}
region = flood(p, opponent_region_space);
opponent_region_space ^= region;
if (!(region & player_alive)) {
opponent_territory |= region;
}
}
// Subtract friendly stones on the board from territory.
player_territory &= ~s->player;
opponent_territory &= ~s->opponent;
score = popcount(player_territory) + popcount(player_territory & s->opponent) - popcount(opponent_territory) - popcount(opponent_territory & s->player);
}
printf("%d\n", score);
}
void repair(int argc, char *argv[]) {
if (argc != 2) {
printf("Please enter a filename.\n");
return;
}
char capture_filename[128];
char japanese_filename[128];
char temp_filename[128];
sprintf(capture_filename, "%s_capture.dat", argv[1]);
sprintf(japanese_filename, "%s_japanese.dat", argv[1]);
sprintf(temp_filename, "%s_temp.dat", argv[1]);
solution sol_;
solution *sol = &sol_;
char *buffer = file_to_buffer(capture_filename);
buffer = load_solution(sol, buffer, 1);
print_state(sol->base_state);
calculate_leaves(sol);
solution solj_;
solution *solj = &solj_;
buffer = file_to_buffer(japanese_filename);
buffer = load_solution(solj, buffer, 1);
print_state(solj->base_state);
solj->leaf_nodes = sol->leaf_nodes;
FILE *f = fopen(temp_filename, "wb");
save_solution(solj, f);
fclose(f);
printf("Done!\n");
}
void upgrade(int argc, char *argv[]) {
parse_args(argc - 1, argv + 1);
solution sol_;
solution *sol = &sol_;
char *buffer = file_to_buffer(tsumego_name); // Not really a tsumego name. Needs extension too.
buffer = load_solution(sol, buffer, 1);
if (sol->si->color_symmetry) {
num_layers = 2 * abs(ko_threats) + 1;
}
else if (num_layers <= 0) {
num_layers = abs(ko_threats) + 1;
}
sol->base_nodes = (node_value**) realloc(sol->base_nodes, sol->num_layers * sizeof(node_value*));
sol->ko_nodes = (node_value**) realloc(sol->ko_nodes, sol->num_layers * sizeof(node_value*));
size_t zero_layer = abs(sol->base_state->ko_threats);
size_t new_zero_layer = abs(ko_threats);
sol->base_nodes[new_zero_layer] = sol->base_nodes[zero_layer];
sol->ko_nodes[new_zero_layer] = sol->ko_nodes[zero_layer];
sol->base_state->ko_threats = ko_threats;
sol->num_layers = num_layers;
size_t num_states = num_keys(sol->d);
for (size_t k = 0; k < sol->num_layers; k++) {
if (k == new_zero_layer) {
continue;
}
sol->base_nodes[k] = (node_value*) malloc(num_states * sizeof(node_value));
for (size_t i = 0; i < num_states; i++) {
(sol->base_nodes[k])[i] = (node_value) {VALUE_MIN, VALUE_MAX, DISTANCE_MAX, DISTANCE_MAX};
}
}
for (size_t k = 0; k < sol->num_layers; k++) {
if (k == new_zero_layer) {
continue;
}
sol->ko_nodes[k] = (node_value*) malloc(sol->ko_ld->num_keys * sizeof(node_value));
for (size_t i = 0; i < sol->ko_ld->num_keys; i++) {
sol->ko_nodes[k][i] = (node_value) {VALUE_MIN, VALUE_MAX, DISTANCE_MAX, DISTANCE_MAX};
}
}
FILE *f = fopen(tsumego_name, "wb");
save_solution(sol, f);
fclose(f);
printf("Done!\n");
}