-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdual_solver.c
More file actions
77 lines (62 loc) · 2 KB
/
dual_solver.c
File metadata and controls
77 lines (62 loc) · 2 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
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "tinytsumego2/scoring.h"
#include "tinytsumego2/dual_solver.h"
#include "tinytsumego2/dual_reader.h"
#include "tsumego.c"
dual_graph solve(tsumego t, bool verbose) {
state root = t.state;
dual_graph dg = create_dual_graph(&root, COMPRESSED_KEYSPACE);
if (verbose) {
print_state(&root);
printf("Solution space size = %zu\n", dg.keyspace._.size);
}
while(iterate_dual_graph(&dg, verbose));
value root_value = get_dual_graph_value(&dg, &root, NONE);
if (verbose)
printf("Low = %f, high = %f\n", root_value.low, root_value.high);
if (root_value.low != t.low || root_value.high != t.high) {
fprintf(stderr, "%f, %f =! %f, %f\n", root_value.low, root_value.high, t.low, t.high);
}
assert(root_value.low == t.low);
assert(root_value.high == t.high);
root_value = get_dual_graph_value(&dg, &root, FORCING);
if (verbose)
printf("Root value (forcing) = (%f, %f)\n\n", root_value.low, root_value.high);
if (verbose) {
printf("Iterating area score\n");
while(area_iterate_dual_graph(&dg, true));
}
return dg;
}
int main(int argc, char *argv[]) {
int arg_count = argc;
if (arg_count <= 1) {
for (size_t i = 0; i < NUM_TSUMEGO; ++i) {
printf("%s\n", TSUMEGO_NAMES[i]);
dual_graph dg = solve(get_tsumego(TSUMEGO_NAMES[i]), false);
free_dual_graph(&dg);
}
return EXIT_SUCCESS;
} else {
dual_graph dg = solve(get_tsumego(argv[1]), true);
if (arg_count >= 3) {
size_t num_unique = 0;
frozen_hash_table fht = prepare_frozen_hash(&dg, &num_unique);
printf("%zu unique value quads in the graph\n", num_unique);
char *filename = argv[2];
printf("Saving result to %s\n", filename);
FILE *f = fopen(filename, "wb");
write_dual_graph(&dg, &fht, f);
fclose(f);
free(fht.bulk_map);
free(fht.tail_keys);
free(fht.tail_values);
}
free_dual_graph(&dg);
}
}