几何类比,为什么我得不到更多答案?(Geometry analogy, why don't I get more answer?)

编程入门 行业动态 更新时间:2024-10-08 05:28:07
几何类比,为什么我得不到更多答案?(Geometry analogy, why don't I get more answer?)

这是我的prolog代码:

figure(1, middle(circle, circle)). figure(2, top_left(circle, circle)). figure(3, bottom_right(circle, circle)). figure(4, middle(square, square)). figure(5, top_left(square, square)). figure(6, top_right(square, square)). figure(7, bottom_right(square, square)). figure(8, bottom_left(square, square)). relate(F1, F2, Relation) :- ( figure(F1, middle(X, Y)), figure(F2, middle(Y, X)), F1 \== F2 -> Relation = invert ; figure(F1, middle(X, X)), figure(F2, middle(Y, Y)), F1 \== F2 -> Relation = same_in_out ; figure(F1, top_left(X, X)), figure(F2, bottom_right(Y, Y)), F1 \== F2 -> Relation = opposite ; figure(F1, top_right(X, X)), figure(F2, bottom_left(Y, Y)), F1 \== F2 -> Relation = opposite ; relate(F2, F1, Relation) ). analogy((F1, F2), (F3, X)) :- relate(F1, F2, Relation), relate(F3, X, Relation).

代表这一套

这是一个简单的执行:

| ?- relate(2, X, Y). X = 3 Y = opposite; no | ?- relate(2, 7, X). X = opposite; no

我的问题是,为什么我没有X = 7,Y =相反,当我确实相关(2,X,Y)?

谢谢。

Here is my prolog code :

figure(1, middle(circle, circle)). figure(2, top_left(circle, circle)). figure(3, bottom_right(circle, circle)). figure(4, middle(square, square)). figure(5, top_left(square, square)). figure(6, top_right(square, square)). figure(7, bottom_right(square, square)). figure(8, bottom_left(square, square)). relate(F1, F2, Relation) :- ( figure(F1, middle(X, Y)), figure(F2, middle(Y, X)), F1 \== F2 -> Relation = invert ; figure(F1, middle(X, X)), figure(F2, middle(Y, Y)), F1 \== F2 -> Relation = same_in_out ; figure(F1, top_left(X, X)), figure(F2, bottom_right(Y, Y)), F1 \== F2 -> Relation = opposite ; figure(F1, top_right(X, X)), figure(F2, bottom_left(Y, Y)), F1 \== F2 -> Relation = opposite ; relate(F2, F1, Relation) ). analogy((F1, F2), (F3, X)) :- relate(F1, F2, Relation), relate(F3, X, Relation).

Which represent this set

And here is a simple execution :

| ?- relate(2, X, Y). X = 3 Y = opposite; no | ?- relate(2, 7, X). X = opposite; no

And my question is, why I don't have X = 7, Y = opposite, when I do relate(2, X, Y) ?

Thank you.

最满意答案

因为你使用if-then-else,而Prolog不会回溯这样的结构:

?- (member(X, [1,2,3]) -> Y = hello ; Y = goodbye). X = 1, Y = hello. ?-

If-then-else真正意味着有效的确定性计算。 如果你想要非确定性/回溯,你应该使用普通的连词,析取和事实列表来重写你的谓词:

relate(F1, F2, Relation) :- figure(F1, Fig1), figure(F2, Fig2), relate_(Fig1, Fig2, Relation), F1 \== F2. relate_(middle(X, Y), middle(Y, X), invert). % etc.

Because you use if-then-else, and Prolog won't backtrack out of such a construct:

?- (member(X, [1,2,3]) -> Y = hello ; Y = goodbye). X = 1, Y = hello. ?-

If-then-else is really meant for efficient deterministic computation. You should rewrite your predicate using ordinary conjunction, disjunction and listings of facts if you want non-determinism/backtracking:

relate(F1, F2, Relation) :- figure(F1, Fig1), figure(F2, Fig2), relate_(Fig1, Fig2, Relation), F1 \== F2. relate_(middle(X, Y), middle(Y, X), invert). % etc.

更多推荐

本文发布于:2023-07-19 15:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182562.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我得   几何   答案   更多   answer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!