First version scanned ahead for the next nonempty bucket but stopped one max
edge weight out, so a sparse frontier ended the search early and claimed
"nothing under the limit" while the queue still had work. Scanning further is
also wrong: the ring reuses a slot every ring.size steps, so a far bucket sits
where a near one would be. Plain Dial now: step one cost unit at a time.
The growth table in the README was measured with the broken version and was
wildly optimistic. Real numbers from the fixed build, full adder with
adjacent cells: cost 58 -> 80M states, 66 -> 220M, 70 -> 280M, about 27 bytes
per state and 650k states/s, so cost 75 / 450M states / 12GB is roughly the
ceiling on this machine.
Validated solve_cost on the small problems where the answer is known by hand:
copy comes out at cost 17 (sacrifice 0 into {2}, then sacrifice 2 into {0,1}),
which is the two block program, and it replays ok.
785 lines
25 KiB
C++
785 lines
25 KiB
C++
#ifndef R8VERITAS
|
|
#define R8VERITAS
|
|
|
|
// R8Veritas -- shortest program search over the rot-8 "sacrifice block":
|
|
//
|
|
// .tp s
|
|
// biz
|
|
// .tp b ; once per b in the mask
|
|
// set_1 / flb ; OR / XOR
|
|
// .tp s
|
|
// flb
|
|
// rnz
|
|
//
|
|
// A state is the whole truth table of the working cell: for every input
|
|
// combination, what the cell holds. Written as bit planes -- plane b is bit
|
|
// b of the cell, over all inputs -- one block becomes a handful of 64-bit
|
|
// operations:
|
|
//
|
|
// cond = plane[s]; plane[s] = 0;
|
|
// for b in mask: plane[b] |= cond (set_1)
|
|
// plane[b] ^= cond (flb)
|
|
//
|
|
// Search strategy:
|
|
// * only the input bits that influence the answer survive (the old
|
|
// compress_stat_space), so a plane is a 2^K bit truth table instead of
|
|
// 2^CELL_SIZE, and the whole state is CELL_SIZE * 2^K bits
|
|
// * forward BFS keeps every reachable state of depth < d plus the block
|
|
// that produced it
|
|
// * the last block is not searched forward. A block clears its own
|
|
// plane, so the goal can only be its target if goal[s] == 0; for each
|
|
// such block every preimage of the goal is generated and looked up in
|
|
// the BFS table. A hit is the shortest program, and "nothing below d,
|
|
// something at d" is a proof.
|
|
//
|
|
// The backward step enumerates 2^K values of the condition, so it is only
|
|
// used while K <= 3; above that the last layer is expanded forward like the
|
|
// others (still a lot cheaper than the old implementation).
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <bit>
|
|
#include <bitset>
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <ostream>
|
|
#include <set>
|
|
#include <string>
|
|
#include <unordered_set>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
template <int CELL_SIZE> class r8veritas {
|
|
public:
|
|
using cell = uint8_t;
|
|
using stat = std::array<cell, (size_t(1) << CELL_SIZE)>;
|
|
using stat_ref = const stat *;
|
|
using truthtable = std::pair<stat_ref, stat_ref>;
|
|
using op = std::function<cell(cell)>;
|
|
|
|
// one biz block
|
|
struct instruction {
|
|
uint8_t sacrifice = 0; // plane whose bit is tested and cleared
|
|
uint8_t mask = 0; // bit b set: plane b is a destination
|
|
bool or_mode = false; // true: set_1 (|), false: flb (^)
|
|
};
|
|
|
|
struct options {
|
|
int max_depth = 5; // longest program to look for
|
|
size_t max_states = 80'000'000; // memory guard
|
|
bool verbose = false;
|
|
};
|
|
|
|
// Searching for the shortest *tape* instead of the fewest layers: the
|
|
// weight of a block is what it assembles to
|
|
// 1 (biz) + 1 (flb) + 1 (rnz) + travel + sum(op cost)
|
|
// with travel = 2 * span(cell addresses of the sacrifice and the mask)
|
|
// (the pointer walks out and comes back) and op cost 1 for flb, 4 for
|
|
// set_1. The inter-block hop from the previous sacrifice back to this one
|
|
// is not in the model -- the emitter charges it afterwards.
|
|
struct cost_options {
|
|
std::array<int, CELL_SIZE> addr{}; // cell address of each plane
|
|
int max_cost = 4096; // give up above this tape length
|
|
size_t max_states = 600'000'000;
|
|
bool verbose = false;
|
|
};
|
|
|
|
struct cost_result {
|
|
bool found = false;
|
|
bool proven = false; // nothing shorter than `cost` exists
|
|
std::vector<instruction> program;
|
|
int cost = 0;
|
|
size_t states = 0;
|
|
double seconds = 0;
|
|
std::string message;
|
|
};
|
|
|
|
struct result {
|
|
bool found = false;
|
|
// true when every program shorter than program.size() was ruled out
|
|
bool proven_optimal = false;
|
|
std::vector<instruction> program;
|
|
size_t states = 0; // states kept by the BFS
|
|
int depth_reached = 0; // deepest layer fully expanded
|
|
double seconds = 0;
|
|
std::string message;
|
|
};
|
|
|
|
std::array<std::string, CELL_SIZE> nicknames;
|
|
std::array<std::string, CELL_SIZE> tp_paths;
|
|
|
|
void initialize_nicknames() {
|
|
for (int i = 0; i < CELL_SIZE; i++)
|
|
nicknames[i] = "%" + std::to_string(i + 1);
|
|
}
|
|
|
|
void initialize_tp_paths() {
|
|
for (int i = 0; i < CELL_SIZE; i++)
|
|
tp_paths[i] = ".tp " + nicknames[i];
|
|
}
|
|
|
|
// ---------------------------------------------------------------- states
|
|
stat_ref generate_empty_stat() {
|
|
stat arr{};
|
|
arr.fill(0);
|
|
return keep(arr);
|
|
}
|
|
|
|
truthtable generate_truthtable(const op &operation) {
|
|
stat in{}, out{};
|
|
for (size_t i = 0; i < (size_t(1) << CELL_SIZE); i++) {
|
|
in[i] = cell(i);
|
|
out[i] = operation(cell(i));
|
|
}
|
|
return {keep(in), keep(out)};
|
|
}
|
|
|
|
// Build a truthtable straight from two tables (a synth block knows the
|
|
// target stat, it does not have a callable function).
|
|
truthtable make_truthtable(const stat &in, const stat &out) {
|
|
return {keep(in), keep(out)};
|
|
}
|
|
|
|
// Clears the input bits that never influence the answer, so the search
|
|
// starts from a canonical state.
|
|
truthtable compress_stat_space(const truthtable &raw) {
|
|
stat start = *raw.first;
|
|
for (int i = 0; i < CELL_SIZE; i++) {
|
|
bool influences = false;
|
|
for (size_t j = 0; j < (size_t(1) << CELL_SIZE); j++)
|
|
if ((*raw.second)[j] != (*raw.second)[j ^ (size_t(1) << i)])
|
|
influences = true;
|
|
if (!influences)
|
|
for (size_t j = 0; j < (size_t(1) << CELL_SIZE); j++)
|
|
start[j] = cell(start[j] & ~(1u << i));
|
|
}
|
|
return {keep(start), raw.second};
|
|
}
|
|
|
|
// Applies a program with the reference semantics (one state at a time) and
|
|
// tells whether it lands on the target.
|
|
bool run_program(const truthtable &raw, const std::vector<instruction> &p,
|
|
bool verbose = false) const {
|
|
stat cur = *raw.first;
|
|
for (const auto &i : p) {
|
|
for (size_t j = 0; j < cur.size(); j++)
|
|
if (cur[j] & (1u << i.sacrifice)) {
|
|
cur[j] = cell(cur[j] & ~(1u << i.sacrifice));
|
|
for (int b = 0; b < CELL_SIZE; b++)
|
|
if (i.mask & (1 << b))
|
|
cur[j] = i.or_mode ? cell(cur[j] | (1u << b))
|
|
: cell(cur[j] ^ (1u << b));
|
|
}
|
|
if (verbose) {
|
|
output_stat(cur);
|
|
std::cout << "\n";
|
|
}
|
|
}
|
|
return cur == *raw.second;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- output
|
|
void output_stat(const stat &s) const {
|
|
for (auto v : s)
|
|
std::cout << std::bitset<CELL_SIZE>(v).to_string() << " ";
|
|
}
|
|
|
|
void output_ins(const instruction &ins, std::ostream &os,
|
|
bool debug_output = true) const {
|
|
if (debug_output) {
|
|
os << "SACRIFICE: " << int(ins.sacrifice);
|
|
if (ins.mask) {
|
|
os << (ins.or_mode ? ", SET1: {" : ", FLIP: {");
|
|
bool first = true;
|
|
for (int b = 0; b < CELL_SIZE; b++)
|
|
if (ins.mask & (1 << b)) {
|
|
if (!first)
|
|
os << ',';
|
|
os << b;
|
|
first = false;
|
|
}
|
|
os << '}';
|
|
}
|
|
os << ';';
|
|
return;
|
|
}
|
|
os << tp_paths[ins.sacrifice] << "\n";
|
|
os << "biz\n";
|
|
for (int b = 0; b < CELL_SIZE; b++)
|
|
if (ins.mask & (1 << b)) {
|
|
os << '\t' << tp_paths[b] << "\n";
|
|
os << '\t' << (ins.or_mode ? "set_1" : "flb") << "\n";
|
|
}
|
|
os << '\t' << tp_paths[ins.sacrifice] << "\n";
|
|
os << "\tflb\n";
|
|
os << "rnz\n";
|
|
}
|
|
|
|
void print_program(const std::vector<instruction> &p, std::ostream &os,
|
|
bool debug_output = false) const {
|
|
for (const auto &i : p) {
|
|
output_ins(i, os, debug_output);
|
|
os << "\n";
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------- search
|
|
cost_result solve_cost(const truthtable &raw, const cost_options &opt) {
|
|
cost_result r;
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
auto finish = [&]() {
|
|
r.seconds =
|
|
std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
|
|
.count();
|
|
return r;
|
|
};
|
|
|
|
std::array<int, CELL_SIZE> bit_of{};
|
|
int k = 0;
|
|
for (int i = 0; i < CELL_SIZE; i++)
|
|
for (size_t j = 0; j < (size_t(1) << CELL_SIZE); j++)
|
|
if ((*raw.second)[j] != (*raw.second)[j ^ (size_t(1) << i)]) {
|
|
bit_of[k++] = i;
|
|
break;
|
|
}
|
|
if (k > 6) {
|
|
r.message = "more than 6 relevant inputs";
|
|
return finish();
|
|
}
|
|
std::vector<size_t> full(size_t(1) << k, 0);
|
|
for (size_t j = 0; j < full.size(); j++)
|
|
for (int b = 0; b < k; b++)
|
|
if (j & (size_t(1) << b))
|
|
full[j] |= size_t(1) << bit_of[b];
|
|
|
|
switch (k) {
|
|
case 0:
|
|
return solve_cost_with<0>(raw, full, opt);
|
|
case 1:
|
|
return solve_cost_with<1>(raw, full, opt);
|
|
case 2:
|
|
return solve_cost_with<2>(raw, full, opt);
|
|
case 3:
|
|
return solve_cost_with<3>(raw, full, opt);
|
|
case 4:
|
|
return solve_cost_with<4>(raw, full, opt);
|
|
case 5:
|
|
return solve_cost_with<5>(raw, full, opt);
|
|
default:
|
|
return solve_cost_with<6>(raw, full, opt);
|
|
}
|
|
}
|
|
|
|
result solve(const truthtable &raw, const options &opt = {}) {
|
|
result r;
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
auto finish = [&]() {
|
|
r.seconds =
|
|
std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
|
|
.count();
|
|
return r;
|
|
};
|
|
|
|
// which input bits matter, in compression order
|
|
std::array<int, CELL_SIZE> bit_of{};
|
|
int k = 0;
|
|
for (int i = 0; i < CELL_SIZE; i++) {
|
|
for (size_t j = 0; j < (size_t(1) << CELL_SIZE); j++)
|
|
if ((*raw.second)[j] != (*raw.second)[j ^ (size_t(1) << i)]) {
|
|
bit_of[k++] = i;
|
|
break;
|
|
}
|
|
}
|
|
if (k > 6) {
|
|
r.message = "the answer depends on more than 6 inputs; the packed state "
|
|
"does not fit and the search would be hopeless anyway";
|
|
return finish();
|
|
}
|
|
|
|
// compressed index j -> index in the full truth table
|
|
std::vector<size_t> full(size_t(1) << k, 0);
|
|
for (size_t j = 0; j < full.size(); j++)
|
|
for (int b = 0; b < k; b++)
|
|
if (j & (size_t(1) << b))
|
|
full[j] |= size_t(1) << bit_of[b];
|
|
|
|
switch (k) {
|
|
case 0:
|
|
return solve_with<0>(raw, full, opt, r, finish);
|
|
case 1:
|
|
return solve_with<1>(raw, full, opt, r, finish);
|
|
case 2:
|
|
return solve_with<2>(raw, full, opt, r, finish);
|
|
case 3:
|
|
return solve_with<3>(raw, full, opt, r, finish);
|
|
case 4:
|
|
return solve_with<4>(raw, full, opt, r, finish);
|
|
case 5:
|
|
return solve_with<5>(raw, full, opt, r, finish);
|
|
default:
|
|
return solve_with<6>(raw, full, opt, r, finish);
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::set<stat> pool;
|
|
|
|
stat_ref keep(const stat &s) { return &*pool.insert(s).first; }
|
|
|
|
// ---------------------------------------------------------------- engine
|
|
// K relevant inputs: every plane is a 2^K bit truth table, the whole state
|
|
// is CELL_SIZE * 2^K bits packed into KEYW words.
|
|
template <int K> struct engine {
|
|
static constexpr int M = 1 << K; // bits per plane
|
|
static constexpr int KEYW = (CELL_SIZE * M + 63) / 64;
|
|
static constexpr uint64_t PMASK =
|
|
(M >= 64) ? ~uint64_t(0) : ((uint64_t(1) << (M & 63)) - 1);
|
|
using key = std::array<uint64_t, KEYW>;
|
|
|
|
static uint64_t get(const key &k, int b) {
|
|
const int off = b * M;
|
|
return (k[off >> 6] >> (off & 63)) & PMASK;
|
|
}
|
|
static void put(key &k, int b, uint64_t v) {
|
|
const int off = b * M;
|
|
k[off >> 6] =
|
|
(k[off >> 6] & ~(PMASK << (off & 63))) | ((v & PMASK) << (off & 63));
|
|
}
|
|
static key zero() {
|
|
key k{};
|
|
return k;
|
|
}
|
|
static uint64_t cond(const key &k, const instruction &i) {
|
|
return get(k, i.sacrifice);
|
|
}
|
|
static key step(const key &k, const instruction &i) {
|
|
key n = k;
|
|
const uint64_t c = get(k, i.sacrifice);
|
|
put(n, i.sacrifice, 0);
|
|
for (int b = 0; b < CELL_SIZE; b++)
|
|
if (i.mask & (1 << b))
|
|
put(n, b, i.or_mode ? (get(k, b) | c) : (get(k, b) ^ c));
|
|
return n;
|
|
}
|
|
static uint64_t hash(const key &k) {
|
|
uint64_t h = 0xcbf29ce484222325ULL;
|
|
for (int i = 0; i < KEYW; i++) {
|
|
h ^= k[i];
|
|
h *= 0x100000001b3ULL;
|
|
h ^= h >> 29;
|
|
h *= 0xbf58476d1ce4e5b9ULL;
|
|
h ^= h >> 32;
|
|
}
|
|
return h;
|
|
}
|
|
|
|
struct slot {
|
|
key k{};
|
|
key parent{};
|
|
uint16_t ins = 0xFFFF; // 0xFFFF free, 0xFFFE start of a program
|
|
};
|
|
|
|
struct table {
|
|
std::vector<slot> v;
|
|
uint64_t mask = 0;
|
|
size_t used = 0;
|
|
static constexpr uint16_t FREE = 0xFFFF, ROOT = 0xFFFE;
|
|
|
|
static uint64_t capacity_for(size_t n) {
|
|
uint64_t c = 1 << 12;
|
|
while (c * 3 < n * 4 + 4)
|
|
c <<= 1;
|
|
return c;
|
|
}
|
|
void init(uint64_t cap) {
|
|
v.assign(cap, slot{});
|
|
mask = cap - 1;
|
|
used = 0;
|
|
}
|
|
void grow() {
|
|
std::vector<slot> old = std::move(v);
|
|
init(old.size() * 2);
|
|
for (const auto &s : old)
|
|
if (s.ins != FREE)
|
|
insert(s.k, s.parent, s.ins);
|
|
}
|
|
bool insert(const key &k, const key &parent, uint16_t ins) {
|
|
uint64_t j = hash(k) & mask;
|
|
while (v[j].ins != FREE) {
|
|
if (v[j].k == k)
|
|
return false;
|
|
j = (j + 1) & mask;
|
|
}
|
|
v[j].k = k;
|
|
v[j].parent = parent;
|
|
v[j].ins = ins;
|
|
if (++used * 4 > (mask + 1) * 3)
|
|
grow();
|
|
return true;
|
|
}
|
|
const slot *find(const key &k) const {
|
|
uint64_t j = hash(k) & mask;
|
|
while (v[j].ins != FREE) {
|
|
if (v[j].k == k)
|
|
return &v[j];
|
|
j = (j + 1) & mask;
|
|
}
|
|
return nullptr;
|
|
}
|
|
};
|
|
};
|
|
|
|
template <int K>
|
|
cost_result solve_cost_with(const truthtable &raw,
|
|
const std::vector<size_t> &full,
|
|
const cost_options &opt) {
|
|
using E = engine<K>;
|
|
using key = typename E::key;
|
|
cost_result r;
|
|
if (K <= 0 || K > 6) {
|
|
r.message = "K out of range";
|
|
return r;
|
|
}
|
|
|
|
// ---- what one block weighs on the tape
|
|
const std::vector<instruction> insns = build_instructions();
|
|
std::vector<uint16_t> w(insns.size());
|
|
int wmax = 0;
|
|
for (size_t i = 0; i < insns.size(); i++) {
|
|
const instruction &in = insns[i];
|
|
int lo = opt.addr[in.sacrifice], hi = lo, cnt = 0;
|
|
for (int b = 0; b < CELL_SIZE; b++)
|
|
if (in.mask & (1 << b)) {
|
|
cnt++;
|
|
lo = std::min(lo, opt.addr[b]);
|
|
hi = std::max(hi, opt.addr[b]);
|
|
}
|
|
// biz + flb + rnz, the walk out and back, then the ops themselves
|
|
w[i] = uint16_t(3 + 2 * (hi - lo) + cnt * (in.or_mode ? 4 : 1));
|
|
wmax = std::max<int>(wmax, w[i]);
|
|
}
|
|
|
|
// start = identity over the compressed inputs, goal = the target
|
|
key start = E::zero(), goal = E::zero();
|
|
for (size_t j = 0; j < full.size(); j++)
|
|
for (int b = 0; b < CELL_SIZE; b++) {
|
|
if (j & (size_t(1) << b))
|
|
E::put(start, b, E::get(start, b) | (uint64_t(1) << j));
|
|
if ((*raw.second)[full[j]] & (1u << b))
|
|
E::put(goal, b, E::get(goal, b) | (uint64_t(1) << j));
|
|
}
|
|
|
|
// ---- arena (stable ids) + open addressing id table
|
|
std::vector<key> keys;
|
|
std::vector<uint16_t> dist;
|
|
std::vector<uint32_t> par;
|
|
std::vector<uint16_t> how;
|
|
std::vector<int32_t> tab;
|
|
size_t tabmask = (1 << 18) - 1;
|
|
tab.assign(tabmask + 1, -1);
|
|
|
|
auto find_id = [&](const key &k) -> int32_t {
|
|
size_t j = size_t(E::hash(k)) & tabmask;
|
|
while (tab[j] >= 0) {
|
|
if (keys[size_t(tab[j])] == k)
|
|
return tab[j];
|
|
j = (j + 1) & tabmask;
|
|
}
|
|
return -1;
|
|
};
|
|
auto rehash = [&]() {
|
|
tab.assign((tabmask + 1) * 2, -1);
|
|
tabmask = tab.size() - 1;
|
|
for (size_t id = 0; id < keys.size(); id++) {
|
|
size_t j = size_t(E::hash(keys[id])) & tabmask;
|
|
while (tab[j] >= 0)
|
|
j = (j + 1) & tabmask;
|
|
tab[j] = int32_t(id);
|
|
}
|
|
};
|
|
auto add = [&](const key &k) -> uint32_t {
|
|
if ((keys.size() + 1) * 10 > (tabmask + 1) * 7)
|
|
rehash();
|
|
const uint32_t id = uint32_t(keys.size());
|
|
keys.push_back(k);
|
|
dist.push_back(0xFFFF);
|
|
par.push_back(0);
|
|
how.push_back(0);
|
|
size_t j = size_t(E::hash(k)) & tabmask;
|
|
while (tab[j] >= 0)
|
|
j = (j + 1) & tabmask;
|
|
tab[j] = int32_t(id);
|
|
return id;
|
|
};
|
|
|
|
const uint32_t start_id = add(start);
|
|
dist[start_id] = 0;
|
|
how[start_id] = 0xFFFE; // root
|
|
|
|
std::vector<std::vector<uint32_t>> ring(size_t(wmax) + 1);
|
|
auto push = [&](int d, uint32_t id) {
|
|
ring[size_t(d) % ring.size()].push_back(id);
|
|
};
|
|
push(0, start_id);
|
|
|
|
auto backtrack = [&](uint32_t id) {
|
|
std::vector<instruction> p;
|
|
while (how[id] != 0xFFFE) {
|
|
p.push_back(insns[how[id]]);
|
|
id = par[id];
|
|
}
|
|
std::reverse(p.begin(), p.end());
|
|
return p;
|
|
};
|
|
|
|
if (start == goal) {
|
|
r.found = r.proven = true;
|
|
return r;
|
|
}
|
|
|
|
int d = 0;
|
|
size_t pops_last = 0;
|
|
const int limit = opt.max_cost;
|
|
while (d <= limit) {
|
|
auto &bucket = ring[size_t(d) % ring.size()];
|
|
if (bucket.empty()) {
|
|
// plain Dial: step one unit at a time. Scanning ahead is not safe --
|
|
// the ring reuses slots every ring.size() steps, so a far bucket can
|
|
// sit where a near one would be.
|
|
d++;
|
|
continue;
|
|
}
|
|
const uint32_t id = bucket.back();
|
|
bucket.pop_back();
|
|
if (dist[id] != d)
|
|
continue; // stale
|
|
if (keys[id] == goal) {
|
|
r.program = backtrack(id);
|
|
r.cost = d;
|
|
r.found = r.proven = true;
|
|
r.states = keys.size();
|
|
return r;
|
|
}
|
|
if (d >= limit)
|
|
continue;
|
|
const key cur = keys[id];
|
|
for (size_t ii = 0; ii < insns.size(); ii++) {
|
|
if (E::get(cur, insns[ii].sacrifice) == 0)
|
|
continue;
|
|
const int nd = d + w[ii];
|
|
if (nd > limit)
|
|
continue;
|
|
const key nx = E::step(cur, insns[ii]);
|
|
const int32_t f = find_id(nx);
|
|
if (f < 0) {
|
|
const uint32_t nid = add(nx);
|
|
dist[nid] = uint16_t(nd);
|
|
par[nid] = id;
|
|
how[nid] = uint16_t(ii);
|
|
push(nd, nid);
|
|
} else if (nd < dist[size_t(f)]) {
|
|
dist[size_t(f)] = uint16_t(nd);
|
|
par[size_t(f)] = id;
|
|
how[size_t(f)] = uint16_t(ii);
|
|
push(nd, uint32_t(f));
|
|
}
|
|
}
|
|
if (opt.verbose && keys.size() / 20'000'000 != pops_last) {
|
|
pops_last = keys.size() / 20'000'000;
|
|
std::cerr << " cost " << d << ", " << keys.size() << " states\n";
|
|
}
|
|
if (keys.size() > opt.max_states) {
|
|
r.message = "state budget exhausted";
|
|
r.states = keys.size();
|
|
return r;
|
|
}
|
|
}
|
|
r.states = keys.size();
|
|
r.proven = true; // nothing at or below the limit
|
|
r.message = "no program with cost <= " + std::to_string(limit);
|
|
return r;
|
|
}
|
|
|
|
template <int K>
|
|
result solve_with(const truthtable &raw, const std::vector<size_t> &full,
|
|
const options &opt, result &r,
|
|
const std::function<result()> &finish) {
|
|
using E = engine<K>;
|
|
using table_t = typename E::table;
|
|
using key = typename E::key;
|
|
constexpr uint16_t ROOT = table_t::ROOT;
|
|
|
|
const std::vector<instruction> insns = build_instructions();
|
|
|
|
// start = identity over the compressed inputs, goal = the target
|
|
key start = E::zero(), goal = E::zero();
|
|
for (size_t j = 0; j < full.size(); j++)
|
|
for (int b = 0; b < CELL_SIZE; b++) {
|
|
if (j & (size_t(1) << b))
|
|
E::put(start, b, E::get(start, b) | (uint64_t(1) << j));
|
|
if ((*raw.second)[full[j]] & (1u << b))
|
|
E::put(goal, b, E::get(goal, b) | (uint64_t(1) << j));
|
|
}
|
|
|
|
table_t tab;
|
|
tab.init(table_t::capacity_for(1 << 16));
|
|
tab.insert(start, E::zero(), ROOT);
|
|
std::vector<key> frontier{start};
|
|
|
|
auto path_to = [&](const key &k, int extra) {
|
|
std::vector<instruction> p;
|
|
if (extra >= 0)
|
|
p.push_back(insns[size_t(extra)]);
|
|
key cur = k;
|
|
while (true) {
|
|
const auto *s = tab.find(cur);
|
|
if (s == nullptr || s->ins == ROOT)
|
|
break;
|
|
p.push_back(insns[s->ins]);
|
|
cur = s->parent;
|
|
}
|
|
std::reverse(p.begin(), p.end());
|
|
return p;
|
|
};
|
|
|
|
if (start == goal) {
|
|
r.found = r.proven_optimal = true;
|
|
return finish();
|
|
}
|
|
|
|
for (int d = 1; d <= opt.max_depth; d++) {
|
|
// ---- close the last block: enumerate the goal's preimages ----
|
|
// Only worth it while 2^K is small enough to walk every condition.
|
|
if (K <= 3) {
|
|
for (uint16_t ii = 0; ii < insns.size(); ii++) {
|
|
const instruction &i = insns[ii];
|
|
if (E::get(goal, i.sacrifice) != 0)
|
|
continue; // the block clears its own plane
|
|
for (uint64_t c = 1; c <= E::PMASK; c++) {
|
|
bool hit = false;
|
|
for_each_preimage<E>(
|
|
goal, i, c, [&](const key &p) {
|
|
if (tab.find(p) != nullptr) {
|
|
r.program = path_to(p, int(ii));
|
|
r.found = true;
|
|
r.proven_optimal = true;
|
|
hit = true;
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
if (hit)
|
|
return finish();
|
|
}
|
|
}
|
|
// the backward step has covered this length already
|
|
if (d == opt.max_depth)
|
|
break;
|
|
}
|
|
|
|
// ---- expand the BFS by one layer ----
|
|
std::vector<key> next;
|
|
next.reserve(frontier.size());
|
|
for (const auto &k : frontier) {
|
|
for (uint16_t ii = 0; ii < insns.size(); ii++) {
|
|
if (E::cond(k, insns[ii]) == 0)
|
|
continue; // nothing to sacrifice, the block would do nothing
|
|
key n = E::step(k, insns[ii]);
|
|
if (n == goal) {
|
|
r.program = path_to(k, int(ii));
|
|
r.found = true;
|
|
r.proven_optimal = true;
|
|
r.states = tab.used;
|
|
r.depth_reached = d;
|
|
return finish();
|
|
}
|
|
if (tab.insert(n, k, ii))
|
|
next.push_back(n);
|
|
}
|
|
if (tab.used > opt.max_states) {
|
|
r.message = "state budget exhausted: the search is not complete";
|
|
r.states = tab.used;
|
|
r.depth_reached = d;
|
|
return finish();
|
|
}
|
|
}
|
|
r.states = tab.used;
|
|
r.depth_reached = d;
|
|
if (opt.verbose)
|
|
std::cout << " depth " << d << ": " << next.size() << " new, "
|
|
<< tab.used << " kept\n";
|
|
frontier.swap(next);
|
|
}
|
|
r.proven_optimal = (r.message.empty());
|
|
if (r.message.empty())
|
|
r.message = "no program of length <= " + std::to_string(opt.max_depth);
|
|
return finish();
|
|
}
|
|
|
|
// Calls f on every preimage of `post` under `ins` with condition `c`.
|
|
// XOR destinations are forced, OR destinations are free on the bits of c
|
|
// they contain, so walk the destinations and branch where needed.
|
|
template <class E, class F>
|
|
static bool for_each_preimage(const typename E::key &post,
|
|
const instruction &ins, uint64_t c, F &&f) {
|
|
using key = typename E::key;
|
|
key pre = post;
|
|
E::put(pre, ins.sacrifice, c);
|
|
return preimage_walk<E>(post, ins, c, pre, 0, f);
|
|
}
|
|
|
|
template <class E, class F>
|
|
static bool preimage_walk(const typename E::key &post,
|
|
const instruction &ins, uint64_t c,
|
|
typename E::key pre, int b, F &f) {
|
|
using key = typename E::key;
|
|
for (; b < CELL_SIZE; b++)
|
|
if (ins.mask & (1 << b))
|
|
break;
|
|
if (b == CELL_SIZE)
|
|
return f(pre);
|
|
|
|
const uint64_t T = E::get(post, b);
|
|
if (!ins.or_mode) {
|
|
E::put(pre, b, T ^ c);
|
|
return preimage_walk<E>(post, ins, c, pre, b + 1, f);
|
|
}
|
|
if ((T & c) != c)
|
|
return false; // a destination bit can only be set, not cleared
|
|
const uint64_t fixed = T & ~c, free = T & c;
|
|
for (uint64_t sub = free;; sub = (sub - 1) & free) {
|
|
E::put(pre, b, fixed | sub);
|
|
if (preimage_walk<E>(post, ins, c, pre, b + 1, f))
|
|
return true;
|
|
if (sub == 0)
|
|
break;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- syntax
|
|
std::vector<instruction> build_instructions() const {
|
|
std::vector<instruction> out;
|
|
out.reserve(CELL_SIZE * (2 * (1 << (CELL_SIZE - 1)) + 1));
|
|
for (int s = 0; s < CELL_SIZE; s++) {
|
|
out.push_back({uint8_t(s), 0, false}); // pure sacrifice: clear plane s
|
|
for (int sub = 1; sub < (1 << (CELL_SIZE - 1)); sub++) {
|
|
uint8_t m = 0;
|
|
int bit = 0;
|
|
for (int b = 0; b < CELL_SIZE; b++)
|
|
if (b != s)
|
|
m |= uint8_t(((sub >> bit++) & 1) << b);
|
|
out.push_back({uint8_t(s), m, false});
|
|
out.push_back({uint8_t(s), m, true});
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
};
|
|
|
|
#endif
|