h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

commit 6f338b97a222dee630c9b13896cf443254667dfb
parent 3135b159bb08bc5bf3e39e0c3ab865935447cc16
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Sat,  5 Oct 2024 18:05:47 +0200

Added distribution check to tool

Diffstat:
Mtools/000_gendata/gendata.c | 10+++++-----
Mtools/expected_distributions.h | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtools/nissy_extra.h | 5+++++
Mtools/tool.h | 1+
4 files changed, 97 insertions(+), 5 deletions(-)

diff --git a/tools/000_gendata/gendata.c b/tools/000_gendata/gendata.c @@ -18,11 +18,11 @@ run(void) { goto gendata_run_finish; default: nissy_datainfo(buf, write_stdout); - printf("\n"); - printf("Succesfully generated %" PRId64 " bytes. " - "See above for details on the tables.\n", size); - - writetable(buf, size, filename); + if (check_distribution(solver, buf)) { + printf("\n"); + printf("Generated %" PRId64 " bytes.\n", size); + writetable(buf, size, filename); + } break; } diff --git a/tools/expected_distributions.h b/tools/expected_distributions.h @@ -1,3 +1,16 @@ +uint64_t expected_cocsep[21] = { + [0] = 1, + [1] = 6, + [2] = 63, + [3] = 468, + [4] = 3068, + [5] = 15438, + [6] = 53814, + [7] = 71352, + [8] = 8784, + [9] = 96 +}; + uint64_t expected_h48[12][9][21] = { [0] = { [2] = { @@ -31,3 +44,76 @@ uint64_t expected_h48[12][9][21] = { }, }, }; + +static bool +distribution_equal(const uint64_t *expected, const uint64_t *actual, int n) +{ + bool equal; + int i; + + for (i = 0, equal = true; i <= n; i++) { + if (expected[i] != actual[i]) { + equal = false; + printf("Wrong value for %d: expected %" PRIu64 + ", actual %" PRIu64 "\n", + i, expected[i], actual[i]); + } + } + + return equal; +} + +static bool +check_cocsep(const void *data) +{ + tableinfo_t info; + + readtableinfo(data, &info); + return distribution_equal( + expected_cocsep, info.distribution, info.maxvalue); +} + +static bool +unknown_h48(uint8_t h, uint8_t k) +{ + if (k != 2 && k != 4) + return true; + + if (k == 4 && h != 0) + return true; + + return k == 2 && h > 1; +} + +STATIC bool +check_distribution(const char *solver, const void *data) +{ + tableinfo_t info = {0}; + + if (!strcmp(solver, "h48")) { + readtableinfo(data, &info); + if (!distribution_equal( + expected_cocsep, info.distribution, info.maxvalue)) { + printf("ERROR! cocsep distribution is incorrect\n"); + return false; + } + printf("cocsep distribution is correct\n"); + + readtableinfo_n(data, 2, &info); + if (unknown_h48(info.h48h, info.bits)) + goto check_distribution_unknown; + + if (!distribution_equal(expected_h48[info.h48h][info.bits], + info.distribution, info.maxvalue)) { + printf("ERROR! h48 distribution is incorrect\n"); + return false; + } + + printf("h48 distribution is correct\n"); + return true; + } + +check_distribution_unknown: + printf("Distribution unknown, not checked\n"); + return true; +} diff --git a/tools/nissy_extra.h b/tools/nissy_extra.h @@ -3,5 +3,10 @@ This header file exposes certain functions that are meant to be used for testing purposes only. */ +#define STATIC static +#define LOG printf + +#include "../src/solvers/tables.h" + size_t gendata_h48_derive(uint8_t, const void *, void *); int parse_h48_options(const char *, uint8_t *, uint8_t *, uint8_t *); diff --git a/tools/tool.h b/tools/tool.h @@ -4,6 +4,7 @@ #include <inttypes.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "../src/nissy.h" #include "nissy_extra.h"