简体   繁体   English

在Prolog中将立方体从位置A移动到位置B

[英]Move cubes from position A to position B in Prolog

I have the following problem: there are some towers build with cubes on a table. 我有以下问题:桌上有一些用立方体建造的塔。

       a 
       b d
       c e
------------------- <- table

Now I want to move the cubes to another situation, like this one: 现在,我想将多维数据集移至另一种情况,例如:

        c e
      a b d
------------------- 

The Prolog program should print the steps to get to this situation, for example: move cube a onto the table , and so on. Prolog程序应打印出解决此问题的步骤,例如: move cube a onto the table ,依此类推。 I have the first situation represented in Prolog: 我在Prolog中表示了第一种情况:

clean(t). % t is the table, you can always put things there
clean(X) :- \+ on(_,X). % X is the top element, if there is nothing above it

on(a,b). % a is on b
on(b,c). % b on c
on(d,e). % d on e
on(c,t). % c on the table
on(e,t). % and e on the table

Now my problem is to find an solution to make Prolog print the steps to the new situation. 现在,我的问题是找到一种解决方案,以使Prolog将步骤打印到新的情况。 My first problem is, how to tell Prolog how the new situation looks like. 我的第一个问题是,如何告诉Prolog新情况如何。 I tried it with some lists, but I didn´t succeed until now. 我尝试了一些列表,但是直到现在我都没有成功。

Does anyone have an idea how to solve this? 有谁知道如何解决这个问题?

If you encode the problem state by having facts of the on/2 predicate in your database, then you can only change that state by using assert and retract to change the database (and your predicates probably have to be declared dynamic as well). 如果您通过在数据库中包含on/2谓词的事实来对问题状态进行编码,则只能通过使用assertretract来更改数据库来更改该状态(并且您的谓词也可能必须声明为dynamic )。 This is unwieldy. 这太笨了。 A nicer solution is to pass around the problem state as a parameter to your solving predicates (and maybe hide that structure behind a wrapper predicate). 更好的解决方案是将问题状态作为参数传递给您的求解谓词(也许将结构隐藏在包装谓词后面)。 This works much better with the backtracking that you're probably going to need to implement the search for the solution. 这对于您可能需要实施搜索解决方案的回溯效果更好。

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

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