Tape-length search beats the layer search on the full adder
Five layers cost 95 characters; six layers cost 90. Both are in examples/ and
both pass the eight-combination check on the ROT-8 machine in
tests/synth_test.cc (which now takes a file argument).
sacrifice B, FLIP {S,T}
sacrifice A, FLIP {B}
sacrifice B, FLIP {A,Ci,S}
sacrifice Ci, FLIP {B,Co,T}
sacrifice S, SET1 {Ci,Co}
sacrifice T, FLIP {B,Ci,S,Co}
Cost 75 in the model, 90 on the tape once the inter-block hops and the two
output lines are counted. The goal was popped at 75 only after everything
below 75 had been expanded, so 75 is the minimum of this cost model.
Trading a layer for a shorter tape is not a contradiction: the layer search
cannot see that a small, cheap block can be worth more than one wide, expensive
one.
This commit is contained in:
1 parent
230cf92159
commit
548c239a85
3 files changed
+111
-3
No files matched your search
@@ -88,6 +88,28 @@ full_add(`r8veritas<6>`):**5 层,2038 万状态,7.7 秒,穷尽搜索
|
||||
|
||||
## 按纸带长度搜(`solve_cost`)
|
||||
|
||||
**结论先说**:full_add 上 5 层那版是 95 字符,代价搜索找到的 6 块版是 **90 字符**。两个都在
|
||||
`examples/` 里,都跑过那台 ROT-8 虚拟机,8 种输入组合全对:
|
||||
|
||||
| 版本 | 层数 | 模型代价 | 真实纸带 |
|
||||
|---|---|---|---|
|
||||
| 层数最优(`full_add.r8asm`) | 5 | 82 | 95 字符 |
|
||||
| 代价最优(`full_add_cost.r8asm`) | 6 | 75 | **90 字符** |
|
||||
|
||||
代价最优解:
|
||||
|
||||
```
|
||||
sacrifice B, FLIP {S,T}
|
||||
sacrifice A, FLIP {B}
|
||||
sacrifice B, FLIP {A,Ci,S}
|
||||
sacrifice Ci, FLIP {B,Co,T}
|
||||
sacrifice S, SET1 {Ci,Co}
|
||||
sacrifice T, FLIP {B,Ci,S,Co}
|
||||
```
|
||||
|
||||
层数最优不等于纸带最优:多一层反而少 5 个字符。Dijkstra 在弹出代价 75 之前已经把 < 75 的
|
||||
区域穷尽,所以 75 是模型内的最小值。
|
||||
|
||||
层数最少不等于纸带最短。`solve_cost` 用同一张状态图跑 Dijkstra,边权就是这个块汇编出来
|
||||
多少字符:
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
%include "asm/lib.r8asm"
|
||||
|
||||
; 按纸带长度搜索得到的最优解:6 个块,模型代价 75(5 层那版是 82)。
|
||||
; 这个文件不走 %synth,直接手写,用来实测真实长度并跑虚拟机。
|
||||
.var A 0
|
||||
.var B 1
|
||||
.var Ci 2
|
||||
.var S 3
|
||||
.var Co 4
|
||||
.var T 5
|
||||
|
||||
; sacrifice B, FLIP {S,T}
|
||||
.tp B
|
||||
biz
|
||||
.tp S
|
||||
flb
|
||||
.tp T
|
||||
flb
|
||||
.tp B
|
||||
flb
|
||||
rnz
|
||||
|
||||
; sacrifice A, FLIP {B}
|
||||
.tp A
|
||||
biz
|
||||
.tp B
|
||||
flb
|
||||
.tp A
|
||||
flb
|
||||
rnz
|
||||
|
||||
; sacrifice B, FLIP {A,Ci,S}
|
||||
.tp B
|
||||
biz
|
||||
.tp A
|
||||
flb
|
||||
.tp Ci
|
||||
flb
|
||||
.tp S
|
||||
flb
|
||||
.tp B
|
||||
flb
|
||||
rnz
|
||||
|
||||
; sacrifice Ci, FLIP {B,Co,T}
|
||||
.tp Ci
|
||||
biz
|
||||
.tp B
|
||||
flb
|
||||
.tp Co
|
||||
flb
|
||||
.tp T
|
||||
flb
|
||||
.tp Ci
|
||||
flb
|
||||
rnz
|
||||
|
||||
; sacrifice S, SET1 {Ci,Co}
|
||||
.tp S
|
||||
biz
|
||||
.tp Ci
|
||||
set_1
|
||||
.tp Co
|
||||
set_1
|
||||
.tp S
|
||||
flb
|
||||
rnz
|
||||
|
||||
; sacrifice T, FLIP {B,Ci,S,Co}
|
||||
.tp T
|
||||
biz
|
||||
.tp B
|
||||
flb
|
||||
.tp Ci
|
||||
flb
|
||||
.tp S
|
||||
flb
|
||||
.tp Co
|
||||
flb
|
||||
.tp T
|
||||
flb
|
||||
rnz
|
||||
|
||||
output S
|
||||
output Co
|
||||
+4
-3
@@ -71,10 +71,11 @@ struct machine {
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
int main(int argc, char **argv) {
|
||||
const char *file = argc > 1 ? argv[1] : "examples/full_add.r8asm";
|
||||
r8asm assembler;
|
||||
if (!assembler.import_file("examples/full_add.r8asm", 8)) {
|
||||
std::cerr << "could not assemble examples/full_add.r8asm\n";
|
||||
if (!assembler.import_file(file, 8)) {
|
||||
std::cerr << "could not assemble " << file << "\n";
|
||||
return 1;
|
||||
}
|
||||
const std::string tape = assembler.export_rot8();
|
||||
|
||||
Reference in new issue
Block a user