【发布时间】:2019-12-15 21:30:07
【问题描述】:
One of the recent Advent of code challenges 要求我解决最小量的输入材料,我可以使用这些材料来应用一组给定的反应并获得 1 个单位的输出材料。
例如,给定
10 ORE => 10 A
1 ORE => 1 B
7 A, 1 B => 1 C
7 A, 1 C => 1 D
7 A, 1 D => 1 E
7 A, 1 E => 1 FUEL
我们总共需要 31 块矿石来生产 1 种燃料(1 块用来生产单位 B,然后 30 块用来生产必需的 28 A)。
今年,我一直在努力拓展我的编程语言视野,因此我已经完成了 SML/NJ 的大部分挑战。这个似乎——似乎——很适合 Prolog,因为我对它知之甚少:逻辑编程、约束求解等。
I was able to put together a working version of this, with some help,我抽空阅读了一些关于 CHR 编程的教程。我想我基本上明白,下面,我们说如果我们知道 10 个a(1) 项目(这些叫什么?证明?),那么我们可以用ore(10) 替换它——这将扩展为 10 个单独的ore(1)来电。
%prolog
:- module(ore, [ fuel/1 ]).
:- use_module(library(chr)).
:- use_module(library(clpfd)).
% constraint names
% ore and fuel are guaranteed
:- chr_constraint
ore/1,
a/1,
b/1,
c/1,
d/1,
e/1,
fuel/1.
% problem constraints
a(1),a(1),a(1),a(1),a(1),a(1),a(1),a(1),a(1),a(1) <=> ore(10).
b(1) <=> ore(1).
c(1) <=> a(7), b(1).
d(1) <=> a(7), c(1).
e(1) <=> a(7), d(1).
fuel(1) <=> a(7), e(1).
% decompositions (one per element???)
a(X) <=> X #> 1, Y #= X-1 | a(Y), a(1).
b(X) <=> X #> 1, Y #= X-1 | b(Y), b(1).
c(X) <=> X #> 1, Y #= X-1 | a(Y), a(1).
d(X) <=> X #> 1, Y #= X-1 | d(Y), d(1).
e(X) <=> X #> 1, Y #= X-1 | e(Y), e(1).
ore(X) <=> X#> 1, Y #= X-1 | ore(Y), ore(1).
% aggregation (for convenience)
% always present
:- chr_constraint ore_add/1, total_ore/1.
total_ore(A), total_ore(Total) <=> NewTotal #= A + Total, total_ore(NewTotal).
ore(1) <=> total_ore(1).
如果能够写出以下内容,那就太好了:
a(10) <=> ore(10)
并以某种方式告诉 prolog,如果我“知道”,例如 a(8),我仍然可以替换 ore(10)(因为我需要 10 个矿石来制造这 8 个 a,而有些只是浪费了)。
我认为如果我能做到这一点,我就可以把这个输出
?- fuel(1).
ore:a(1),
ore:a(1),
ore:a(1),
ore:a(1),
ore:a(1),
ore:a(1),
ore:a(1),
ore:a(1),
ore:total_ore(21).
进入
?- fuel(1).
ore:total_ore(31).
如果我手动将,ore:a(2) 添加到燃料查询中,我会得到正确的总矿石。
总结一下,
- 如何表达我不能部分地运行反应,但我可以运行反应以获得比我需要的更多的约束?换句话说,如果我需要
a(8),就足以知道我需要a(10)?我认为,这也将允许我用a(10) <=> ore(10)之类的语言编写原始问题陈述,而不是荒谬的a(1),a(1)...。还是会? - 这会解决我的“搜索”问题,例如
fuel(1)将给我ore:total_ore(31)?
更新:我花了一些时间玩 (1) 来获得
%prolog
:- module(ore, [ fuel/1 ]).
:- use_module(library(chr)).
:- use_module(library(clpfd)).
% constraint names
% ore and fuel are guaranteed
:- chr_constraint
ore/1,
a/1,
b/1,
c/1,
d/1,
e/1,
fuel/1.
% problem constraints
a(X) <=> X #>= 1, X #=< 10 | ore(10).
b(1) <=> ore(1).
c(1) <=> a(7), b(1).
d(1) <=> a(7), c(1).
e(1) <=> a(7), d(1).
fuel(1) <=> a(7), e(1).
% decompositions (one per element???)
b(X) <=> X #> 1, Y #= X-1 | b(Y), b(1).
c(X) <=> X #> 1, Y #= X-1 | a(Y), a(1).
d(X) <=> X #> 1, Y #= X-1 | d(Y), d(1).
e(X) <=> X #> 1, Y #= X-1 | e(Y), e(1).
ore(X) <=> X#> 1, Y #= X-1 | ore(Y), ore(1).
% aggregation (for convenience)
% always present
:- chr_constraint ore_add/1, total_ore/1.
total_ore(A), total_ore(Total) <=> NewTotal #= A + Total, total_ore(NewTotal).
ore(1) <=> total_ore(1).
为我的查询生成了total_ore(41)。 我相信“剩菜”正在转化为矿石,在这种情况下,这不是我想要的(尽管它当然是指定的)。as 现在被删除得太早了——即,这是正确的,但不是最小的。如何喜欢整个反应?嗯……
【问题讨论】:
-
如果真的有必要,我可以把 3 分成它自己的问答;我认为这样看起来更干净,因为它们有点纠结。不过,1/2 确实属于一起。
-
"(这些叫什么?证明?)" 它们被称为约束。但我不确定约束处理规则是否适合这个问题。但它们实际上是“向前”链接的(使用多集状态,我还没有弄清楚如何考虑这一点),而查询“我需要多少东西才能获得 X 单位的燃料”似乎完全匹配反向链接 horn-clausy Prolog 引擎,可通过适当的回溯获得最小化。
-
这可能也是一个整数 CLP 问题,但我不知道该怎么做。
-
@DavidTonhofer 谢谢;我使用了约束,因为上一个问题就是这样开始的。我同意反向链;会欢迎对原始问题的回答
标签: prolog swi-prolog clpfd constraint-handling-rules