增加一个

时间:2014-03-06 02:07:00

标签: prolog

这是一个更大问题的一部分,所以我可能只是以错误的方式思考这个问题。但是这里有。

我有以下“过渡”规则:

//if in state(A,B,C), move to state C
trans( state(A, B, C), state(A, B, D is C + 1), added_one).

我正试图想出一种方法来向C添加1。

更多背景:我正在做一个“穿越”问题,I.E。让这些动物穿过一条河而不违反一套规则。 C是船上的动物数量,不能超过2(我有不同的规则来处理< = 2检查)。

目前,我设置它的方式似乎永远不会使用D.当我使用“跟踪”功能时,D始终是一个不关心的值。

我应该怎么做?谢谢。

编辑:代码。

initial( state(  3,  3,  1,  0,  0,  0, 0) ).
final(   state(  0,  0,  0,  3,  3,  1, 0) ).

//BCpp stands for boatcount++.
BCpp is BoatCount + 1,
trans(state( 2, LF, 1, RH, RF, 0 , BoatCount) , state(1, LF, 1, RH, RF, 0, BCpp), hen_in_left_boat).

BCpp is BoatCount + 1,
trans(state( 1, LF, 1, RH, RF, 0 , BoatCount) , state(0, LF, 1, RH, RF, 0, BCpp), hen_in_left_boat).

//and so on for a bunch of transitions.
//the code produces an error at the first line "BCpp is BoatCount + 1,"

1 个答案:

答案 0 :(得分:0)

根据@ mbratch的回答,通过猜测和检查来计算出来。

D is C + 1,
trans(state(A, B, C), state(A, B, D), added_one).

抛出错误,所以我将其切换为:

trans(state(A, B, C), state(A, B, D) ) :- D is C + 1.

它现在正常运行。好吧,无论如何这部分:(

相关问题