在 CLPQ/R 中解决一个简单的几何难题 (Prolog)
Solving a simple geometric puzzle in CLPQ/R (Prolog)
考虑以下方块:
You are given three constraints:
- All rectangles (A, B, C, D and E) have the same area;
- Their geometric layout constitutes a square; and
- The height of A is 2.
现在,我知道这很容易手动解决,但我认为这将是一个很好的例子来展示 CLP(Q/R) 与 Prolog 的功能:
剧透警告:如果您想先自己解决这个难题,请不要继续阅读本文,因为有些限制会泄露解决方案。
无论如何,这是我用 CLP(Q/R):
定义的尝试(我认为包括 冗余 约束)
:- use_module(library(clpr)).
solve(Eh) :-
A = B, B = C, C = D, D = E,
{ A >= 1, B >= 1, C >= 1, D >= 1, E >= 1,
Aw >= 1, Bw >= 1, Cw >= 1, Dw >= 1, Ew >= 1 },
{ Ah = 2 },
{ A = Ah * Aw,
B = Bh * Bw,
C = Ch * Cw,
D = Dh * Dw,
E = Eh * Ew },
{ (Bw + Cw) = Aw,
Dw = Cw,
(Ah + Bh) = Eh,
(Ch + Dh) = Bh,
(Aw + Ew) = Eh },
minimize(Eh).
查询时:
?- solve(Eh).
false.
...让我很难过。约束求解器的一个很好的例子...有人愿意消除我的悲伤吗?
附录: 我使用 Mathematica 和 FindMinimum
函数来检查我的约束。它似乎在工作:
domain = a >= 1 && b >= 1 && c >= 1 && d >= 1 && e >= 1 && ah == 2.0 && a == b == c == d == e && aw >= 1 && bw >= 1 && cw >= 1 && dw >= 1 && ew >= 1
rectangles = (a == ah*aw && b == bh*bw && c == ch*cw && d == dh*dw && e == eh*ew)
FindMinimum[{eh,
domain && rectangles &&
((bw + cw ) == aw && dw == cw && (ah + bh) == eh && (ch + dh) == bh && (aw + ew) == eh)},
{a, b, c, d, e, ah, aw, bh, bw, ch, cw, dh, dw, eh, ew}]
答案:
{8., {a -> 12.8, b -> 12.8, c -> 12.8, d -> 12.8, e -> 12.8,
ah -> 2., aw -> 6.4, bh -> 6., bw -> 2.13333, ch -> 3.,
cw -> 4.26667, dh -> 3., dw -> 4.26667,
eh -> 8., ew -> 1.6}}
CLP 中有一个 old/new 条目,clpBNR。您可以在最新版本的 SWI-Prolog 中安装它。
我认为需要将方程组合成一个 {}。
?- pack_install(clpBNR).
:- use_module(library(clpBNR)).
solve_(Eh) :-
Vs = [A,B,C,D,E, Aw,Bw,Cw,Dw,Ew, Ah,Bh,Ch,Dh,Eh],
Vs::real(1,100),
{ Ah == 2,
A is Ah * Aw,
B is Bh * Bw,
C is Ch * Cw,
D is Dh * Dw,
E is Eh * Ew,
A == B,
B == C,
C == D,
D == E,
(Bw + Cw) == Aw,
Dw == Cw,
(Ah + Bh) == Eh,
(Ch + Dh) == Bh,
(Aw + Ew) == Eh
},
solve(Vs).
?- solve_(Eh).
::(Eh, ...( 8.000000)) .
考虑以下方块:
You are given three constraints:
- All rectangles (A, B, C, D and E) have the same area;
- Their geometric layout constitutes a square; and
- The height of A is 2.
现在,我知道这很容易手动解决,但我认为这将是一个很好的例子来展示 CLP(Q/R) 与 Prolog 的功能:
剧透警告:如果您想先自己解决这个难题,请不要继续阅读本文,因为有些限制会泄露解决方案。
无论如何,这是我用 CLP(Q/R):
定义的尝试(我认为包括 冗余 约束):- use_module(library(clpr)).
solve(Eh) :-
A = B, B = C, C = D, D = E,
{ A >= 1, B >= 1, C >= 1, D >= 1, E >= 1,
Aw >= 1, Bw >= 1, Cw >= 1, Dw >= 1, Ew >= 1 },
{ Ah = 2 },
{ A = Ah * Aw,
B = Bh * Bw,
C = Ch * Cw,
D = Dh * Dw,
E = Eh * Ew },
{ (Bw + Cw) = Aw,
Dw = Cw,
(Ah + Bh) = Eh,
(Ch + Dh) = Bh,
(Aw + Ew) = Eh },
minimize(Eh).
查询时:
?- solve(Eh).
false.
...让我很难过。约束求解器的一个很好的例子...有人愿意消除我的悲伤吗?
附录: 我使用 Mathematica 和 FindMinimum
函数来检查我的约束。它似乎在工作:
domain = a >= 1 && b >= 1 && c >= 1 && d >= 1 && e >= 1 && ah == 2.0 && a == b == c == d == e && aw >= 1 && bw >= 1 && cw >= 1 && dw >= 1 && ew >= 1
rectangles = (a == ah*aw && b == bh*bw && c == ch*cw && d == dh*dw && e == eh*ew)
FindMinimum[{eh,
domain && rectangles &&
((bw + cw ) == aw && dw == cw && (ah + bh) == eh && (ch + dh) == bh && (aw + ew) == eh)},
{a, b, c, d, e, ah, aw, bh, bw, ch, cw, dh, dw, eh, ew}]
答案:
{8., {a -> 12.8, b -> 12.8, c -> 12.8, d -> 12.8, e -> 12.8,
ah -> 2., aw -> 6.4, bh -> 6., bw -> 2.13333, ch -> 3.,
cw -> 4.26667, dh -> 3., dw -> 4.26667,
eh -> 8., ew -> 1.6}}
CLP 中有一个 old/new 条目,clpBNR。您可以在最新版本的 SWI-Prolog 中安装它。
我认为需要将方程组合成一个 {}。
?- pack_install(clpBNR).
:- use_module(library(clpBNR)).
solve_(Eh) :-
Vs = [A,B,C,D,E, Aw,Bw,Cw,Dw,Ew, Ah,Bh,Ch,Dh,Eh],
Vs::real(1,100),
{ Ah == 2,
A is Ah * Aw,
B is Bh * Bw,
C is Ch * Cw,
D is Dh * Dw,
E is Eh * Ew,
A == B,
B == C,
C == D,
D == E,
(Bw + Cw) == Aw,
Dw == Cw,
(Ah + Bh) == Eh,
(Ch + Dh) == Bh,
(Aw + Ew) == Eh
},
solve(Vs).
?- solve_(Eh).
::(Eh, ...( 8.000000)) .