Add a tape-length search: Dijkstra with the real block cost
Layers are not characters. solve_cost runs Dijkstra over the same state graph
with an edge weight of what the block actually assembles to:
w = 3 (biz + flb + rnz) + 2 * span(cell addresses) + sum(op)
op = 1 for flb, 4 for set_1
Dial buckets, pop in cost order, the first pop of the goal is the shortest
tape. Give it max_cost and it becomes branch and bound: anything past the
bound never enters the queue.
Exhausted regions on the full adder with adjacent cells:
max_cost 20 1,086 states 0.0s
max_cost 40 128,528 states 0.1s
max_cost 50 999,448 states 0.7s
max_cost 60 7,671,626 states 12.8s
max_cost 65 20,153,459 states 47.6s
About 2.6x states per 5 cost, about 32 bytes per state, so max_cost 80 is
~350M states / 11GB and 82 would be ~600M / 19GB. The block weight leaves out
the hop from the previous sacrifice (that would make the cost depend on
history and Dijkstra would stop working); the emitter adds it when it reports
the real length -- the five layer program is 82 in the model, 87 on the tape.
The first version of the bucket ring only looked one max-edge-weight ahead for
work, so it declared 'nothing under the limit' while the queue still had
states further out. That made the small limits look sound when they were not;
advancing to the next nonempty bucket fixed it.
This commit is contained in:
1 parent
bc9d873cfc
commit
a0790a66e2
4 files changed
+335
No files matched your search
@@ -19,3 +19,6 @@ target_link_libraries(r8forge r8forge_core)
|
||||
|
||||
add_executable(r8forge_synth_test tests/synth_test.cc)
|
||||
target_link_libraries(r8forge_synth_test r8forge_core)
|
||||
|
||||
add_executable(r8forge_cost_search tests/cost_search.cc)
|
||||
target_link_libraries(r8forge_cost_search r8forge_core)
|
||||
@@ -86,6 +86,36 @@ full_add(`r8veritas<6>`):**5 层,2038 万状态,7.7 秒,穷尽搜索
|
||||
| 参数 6 → 7 → 8 个 | 2,040 | 穷尽 540 万 / 4410 万状态 | 无 |
|
||||
| 条件位 ≠ 牺牲位 | 2,304 | 前向 3 层 317 万 + 反向 1,280 | 无 |
|
||||
|
||||
## 按纸带长度搜(`solve_cost`)
|
||||
|
||||
层数最少不等于纸带最短。`solve_cost` 用同一张状态图跑 Dijkstra,边权就是这个块汇编出来
|
||||
多少字符:
|
||||
|
||||
```
|
||||
w(block) = 3 (biz + flb + rnz) + 2 × span(cell 地址) + Σ op # flb=1, set_1=4
|
||||
```
|
||||
|
||||
桶队列(Dial),按代价递增弹出,第一次弹出目标态就是最短纸带。给一个 `max_cost` 上界就变成
|
||||
分支限界,超出上界的状态不入队。
|
||||
|
||||
穷尽得到的数据(6 个 cell 紧邻,full_add):
|
||||
|
||||
| max_cost | 状态数 | 用时 |
|
||||
|---|---|---|
|
||||
| 20 | 1,086 | 0.0 s |
|
||||
| 40 | 128,528 | 0.1 s |
|
||||
| 50 | 999,448 | 0.7 s |
|
||||
| 60 | 7,671,626 | 12.8 s |
|
||||
| 65 | 20,153,459 | 47.6 s |
|
||||
|
||||
每加 5 点代价状态数约 ×2.6,每状态约 32 字节(8 字节 key + 2 距离 + 4 父 + 2 指令 + 哈希表)。
|
||||
按这个斜率,`max_cost = 80` 约 3.5 亿状态 / 11 GB,`82` 约 6 亿 / 19 GB —— 后者超出机器
|
||||
内存,所以默认跑 80。
|
||||
|
||||
注意:模型里的 `w` 不含「上一块的牺牲位跳到这一块」的那几步(`|s_i − s_{i−1}|`),因为那让
|
||||
代价依赖历史、Dijkstra 就不能用了。发射器会把这部分加上再报真实长度,5 层程序是模型 82 /
|
||||
实际 87。
|
||||
|
||||
## 为什么合在一起
|
||||
|
||||
代价 = 汇编出来多少条指令,而这只有发射器知道:
|
||||
|
||||
@@ -74,6 +74,30 @@ public:
|
||||
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
|
||||
@@ -204,6 +228,52 @@ public:
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 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();
|
||||
@@ -363,6 +433,182 @@ private:
|
||||
};
|
||||
};
|
||||
|
||||
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 (true) {
|
||||
auto &bucket = ring[size_t(d) % ring.size()];
|
||||
if (bucket.empty()) {
|
||||
// Dijkstra pops in nondecreasing order, so when this bucket is dry the
|
||||
// next state lives in the next nonempty one -- which can be further
|
||||
// away than one edge weight.
|
||||
int next = d + 1;
|
||||
while (next <= limit && ring[size_t(next) % ring.size()].empty())
|
||||
next++;
|
||||
if (next > limit)
|
||||
break; // nothing left under the limit
|
||||
d = next;
|
||||
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,
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
// Shortest *tape* instead of fewest layers: Dijkstra over the same state
|
||||
// graph, where a block costs what it assembles to.
|
||||
#include "r8forge/r8veritas.hh"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
using V = r8veritas<6>;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const int limit = argc > 1 ? std::atoi(argv[1]) : 87;
|
||||
const bool spaced = argc > 2 ? std::atoi(argv[2]) != 0 : false;
|
||||
|
||||
V v;
|
||||
v.initialize_nicknames();
|
||||
v.initialize_tp_paths();
|
||||
auto raw = v.generate_truthtable([](V::cell inp) -> V::cell {
|
||||
unsigned a = inp & 1, b = (inp >> 1) & 1, c = (inp >> 2) & 1;
|
||||
unsigned s = a ^ b ^ c;
|
||||
unsigned co = (a & b) | (a & c) | (b & c);
|
||||
return V::cell((co << 4) | (s << 3) | (inp & 0b111));
|
||||
});
|
||||
auto compressed = v.compress_stat_space(raw);
|
||||
|
||||
V::cost_options opt;
|
||||
for (int i = 0; i < 6; i++)
|
||||
opt.addr[i] = spaced ? 2 * i : i; // 0,1,2.. or 0,2,4..
|
||||
opt.max_cost = limit;
|
||||
opt.verbose = true;
|
||||
if (argc > 3)
|
||||
opt.max_states = _strtoui64(argv[3], nullptr, 10);
|
||||
|
||||
const auto res = v.solve_cost(compressed, opt);
|
||||
std::cout << "limit " << limit << ", addresses ";
|
||||
for (int i = 0; i < 6; i++)
|
||||
std::cout << opt.addr[i] << (i == 5 ? "" : ",");
|
||||
std::cout << "\n";
|
||||
if (!res.found) {
|
||||
std::cout << "no program with cost <= " << limit << " " << res.states
|
||||
<< " states, " << res.seconds << "s " << res.message << "\n";
|
||||
return res.proven ? 0 : 2;
|
||||
}
|
||||
std::cout << "cost " << res.cost << ", " << res.program.size() << " blocks, "
|
||||
<< res.states << " states, " << res.seconds << "s\n";
|
||||
for (const auto &i : res.program) {
|
||||
std::cout << " sacrifice " << int(i.sacrifice) << " "
|
||||
<< (i.or_mode ? "SET1" : "FLIP") << " {";
|
||||
for (int b = 0; b < 6; b++)
|
||||
if (i.mask & (1 << b))
|
||||
std::cout << " " << b;
|
||||
std::cout << " }\n";
|
||||
}
|
||||
std::cout << "replay: "
|
||||
<< (v.run_program(compressed, res.program) ? "ok" : "MISMATCH")
|
||||
<< "\n";
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user