diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-08-27 17:44:17 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-08-27 17:44:17 +0200 |
| commit | b5efa2c7bfa259f1b9f5afa68349e2742427de80 (patch) | |
| tree | 106ddc90aa7aa92558415f89a8b39076ab508364 /tools/003_solve_small/solve_small.c | |
| parent | c558c7989eaa0921f7b29ee274b325f4f6d97f8c (diff) | |
| download | nissy-core-b5efa2c7bfa259f1b9f5afa68349e2742427de80.tar.gz nissy-core-b5efa2c7bfa259f1b9f5afa68349e2742427de80.zip | |
New tool for benchmarking small solver
Diffstat (limited to 'tools/003_solve_small/solve_small.c')
| -rw-r--r-- | tools/003_solve_small/solve_small.c | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/tools/003_solve_small/solve_small.c b/tools/003_solve_small/solve_small.c new file mode 100644 index 0000000..406e255 --- /dev/null +++ b/tools/003_solve_small/solve_small.c | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | #include <pthread.h> | ||
| 2 | #include <time.h> | ||
| 3 | #include "../timerun.h" | ||
| 4 | #include "../../src/nissy.h" | ||
| 5 | |||
| 6 | #define OPTIONS "0;4;20" | ||
| 7 | |||
| 8 | const char *filename = "tables/h48h0k4"; | ||
| 9 | char *buf; | ||
| 10 | char *scrambles[] = { | ||
| 11 | "R D' R2 D R U2 R' D' R U2 R D R'", /* 12 optimal */ | ||
| 12 | "RLUD RLUD RLUD", /* 12 optimal */ | ||
| 13 | NULL | ||
| 14 | }; | ||
| 15 | |||
| 16 | void run(void) { | ||
| 17 | int i; | ||
| 18 | int64_t n; | ||
| 19 | char sol[100], cube[22]; | ||
| 20 | |||
| 21 | printf("Solved the following scrambles:\n\n"); | ||
| 22 | for (i = 0; scrambles[i] != NULL; i++) { | ||
| 23 | printf("%s\n", scrambles[i]); | ||
| 24 | fprintf(stderr, "Solving scramble %s\n", scrambles[i]); | ||
| 25 | if (nissy_frommoves(scrambles[i], cube) == -1) { | ||
| 26 | fprintf(stderr, "Invalid scramble, " | ||
| 27 | "continuing with next scramble\n"); | ||
| 28 | continue; | ||
| 29 | } | ||
| 30 | n = nissy_solve( | ||
| 31 | cube, "h48", OPTIONS, "", 0, 20, 1, -1, buf, sol); | ||
| 32 | if (n == 0) | ||
| 33 | fprintf(stderr, "No solution found, " | ||
| 34 | "continuing with next scramble\n"); | ||
| 35 | } | ||
| 36 | printf("\n"); | ||
| 37 | } | ||
| 38 | |||
| 39 | int getdata(int64_t size) { | ||
| 40 | int64_t s; | ||
| 41 | FILE *f; | ||
| 42 | |||
| 43 | buf = malloc(size); | ||
| 44 | |||
| 45 | if ((f = fopen(filename, "rb")) == NULL) { | ||
| 46 | fprintf(stderr, "Table file not found, generating them." | ||
| 47 | " This can take a while.\n"); | ||
| 48 | s = nissy_gendata("h48", OPTIONS, buf); | ||
| 49 | if (s != size) { | ||
| 50 | fprintf(stderr, "Error generating table"); | ||
| 51 | if (s != -1) | ||
| 52 | fprintf(stderr, " (got %" PRId64 " bytes)", s); | ||
| 53 | fprintf(stderr, "\n"); | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | if ((f = fopen(filename, "wb")) == NULL) { | ||
| 57 | fprintf(stderr, "Could not write tables to file %s" | ||
| 58 | ", will be regenerated next time.\n", filename); | ||
| 59 | } else { | ||
| 60 | fwrite(buf, size, 1, f); | ||
| 61 | fclose(f); | ||
| 62 | } | ||
| 63 | } else { | ||
| 64 | fprintf(stderr, "Reading tables from file %s\n", filename); | ||
| 65 | fread(buf, size, 1, f); | ||
| 66 | fclose(f); | ||
| 67 | } | ||
| 68 | |||
| 69 | return 0; | ||
| 70 | } | ||
| 71 | |||
| 72 | int main(void) { | ||
| 73 | int64_t size; | ||
| 74 | |||
| 75 | srand(time(NULL)); | ||
| 76 | |||
| 77 | nissy_setlogger(log_stderr); | ||
| 78 | size = nissy_datasize("h48", OPTIONS); | ||
| 79 | if (size == -1) { | ||
| 80 | printf("h48 stats: error in datasize\n"); | ||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | if (getdata(size) != 0) { | ||
| 85 | printf("Error getting table, stopping\n"); | ||
| 86 | free(buf); | ||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | |||
| 90 | timerun(run, "small solver benchmark"); | ||
| 91 | |||
| 92 | free(buf); | ||
| 93 | return 0; | ||
| 94 | } | ||
