简体   繁体   English

如何使用Prolog CLP FD进行路径限制?

[英]How to make path restrictions using Prolog CLP FD?

I'm trying to use restrictions programming through Prolog CLP FD to solve a puzzle that was proposed. 我正试图通过Prolog CLP FD使用限制编程来解决提出的难题。 This puzzle consists in the next simple rules: 这个难题包含下一个简单的规则:

阴阳拼图描述

Now, in my code, I already cover the restrictions for the 2x2 grid AND that one piece MUST be connected to AT LEAST one of the same color. 现在,在我的代码中,我已经涵盖了2x2网格的限制,并且一个必须连接到相同颜色的至少一个。

The problem is that I cannot find a way to build the restriction that says that one piece MUST have a PATH (be connected) to all the other pieces of the same color, without passing through pieces of the opposite color, so I'm getting this kind of outputs: 问题是,我找不到一种方法来建立限制,说明一件必须有一个PATH(连接)到相同颜色的所有其他部分,而不通过相反颜色的部分,所以我得到这种输出:

0 0 0 0
0 1 0 1
0 1 0 1
0 1 0 0

0 0 0 0
0 1 0 1
0 1 0 1
0 1 0 1

where the 1s are not all connected to each other. 其中1s并非全部相互连接。

How can I write this kind of graph restrictions in CLP FD? 如何在CLP FD中编写这种图形限制?

EDIT: I am using SICStus Prolog. 编辑:我正在使用SICStus Prolog。

To reword your situation so that we can more clearly think about it: 改写你的情况,以便我们可以更清楚地思考它:

  1. you can already generate answers 你已经可以生成答案了
  2. but the answers are currently too general . 答案目前过于笼统

To make your program more specific , you must find a condition that is currently violated in one of the answers you generate, but which must hold in every solution, and then express this condition with constraints. 为了使您的程序更具体 ,您必须在您生成的一个答案中找到当前违反的条件,但必须在每个解决方案中保留这些条件,然后使用约束表达此条件。

For example, consider again you case: 例如,再考虑一下你的情况:

0 0 0 0
0 1 0 1
0 1 0 1
0 1 0 1

Which condition is violated here? 这里违反了哪些条件 Obviously, the 1 pieces are not along a path. 显然, 1件不是沿着路径。 But describing a full path with CLP(FD) is extremely tedious, and since this is apparently taken from an exam or homework question, the idea suggests itself that there is a simple local criterion to express the desired condition. 但是用CLP(FD)描述一条完整的路径是非常繁琐的,而且由于这显然是从考试或家庭作业问题中得出的,因此这个想法表明自己有一个简单的局部标准来表达所需的条件。

By "local", I mean that you only have to take into account a few neighbours instead of the whole board. 通过“本地”,我的意思是你只需要考虑几个邻居而不是整个董事会。

So, consider again the 1 pieces. 所以,再考虑1件。 Obviously, every 1 piece has a neighbour that is also 1 in this answer. 显然,每1件有一个邻居,在这个答案中也是 1。 What else? 还有什么? Does every 1 piece have 2 neighbours? 1件有2个邻居吗? No currently, not. 目前没有,不是。 Should every 1 piece have 2 neighbours that are also 1 ? 应该1件有2个邻居也是1吗? If not, how many exceptions are admissible? 如果没有,可以接受多少例外?

If you think along these conditions, you will definitely obtain a nice solution. 如果你考虑这些条件,你肯定会得到一个很好的解决方案。

One hint: Sometimes reified constraints are useful in such tasks. 一个提示:有时, 具体化的约束在这些任务中很有用。 This means that you can for example say: B #<==> (X #= Y) , and let B denote whether X #= Y holds. 这意味着您可以例如说: B #<==> (X #= Y) ,并且让B表示X #= Y 是否成立。 Note that you may not even need this in this case. 请注意,在这种情况下您甚至可能不需要这样做。

did either of you solve this problem at last? 你们俩最后解决了这个问题吗?

I'm very interested in this problem and want to see the code,if exists. 我对这个问题很感兴趣,想看看代码,如果存在的话。

I think circuit/1 cannot help and want to see the usage for solving this. 我认为circuit / 1无法帮助,并希望看到解决这个问题的用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM