Prolog中实现水壶问题

Prolog中实现水壶问题

本文介绍了如何在SWI-Prolog中实现水壶问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SWI-Prolog中尝试了4加仑和3加仑的水壶问题,但它向我显示了操作员除外的错误.因此,我恳请您专家解决下面提到的水壶问题代码.

我尝试过的事情:

我已附上代码,请看一下.
visit_state(integer,integer).


状态(整数,整数).


状态(2,0).

state(X,Y):-X(,4,",,Y,")\ n),
状态(4,Y).


state(X,Y):-Y(,X,",,3,")\ n),
状态(X,3).

状态(X,Y):-X> 0,
not(visited_state(0,Y)),
断言(visited_state(X,Y)),
write(清空地面上的4加仑水罐:(",X,,",Y,)->;

(,0,",,Y,")\ n),
状态(0,Y).

状态(X,Y):-Y> 0,
not(visited_state(X,0)),
断言(visited_state(X,0)),
write(在地面上倒空3加仑的水罐:(",X,,",Y,)->;

(,X,",,0,")\ n),
状态(X,0).

状态(X,Y):-X + Y> == 4,
& 0,
NEW_Y是Y-(4-X),
not(visited_state(4,NEW_Y)),
断言(visited_state(X,Y)),
写(将3-加仑水罐中的水倒入4-加仑中,直到达到

full:(,X,",,Y,")-> (,4,",,NEW_Y,")\ n),
状态(4,NEW_Y).

状态(X,Y):-X + Y> == 3,
X> 0,
NEW_X是X-(3-Y),
not(visited_state(X,3)),
断言(visited_state(X,Y)),
写(将水从4加仑的水倒入3加仑的水,直到达到

full:(,X,",,Y,")-> (,NEW_X,",,3,")\ n),
状态(NEW_X,3).

状态(X,Y):-X + Y 0,
NEW_X是X + Y,
not(visited_state(NEW_X,0)),
断言(visited_state(X,Y)),
write(将所有水从3加仑水罐倒入4加仑水:

(,X,",,Y,")-> (,NEW_X,",,0,")\ n),
状态(NEW_X,0).

状态(X,Y):-X + Y 0,
NEW_Y是X + Y,
not(visited_state(0,NEW_Y)),
断言(visited_state(X,Y)),
写(将所有水从4加仑的水倒入3加仑的水:

(,X,",,Y,")-> (,0,",,NEW_Y,")\ n),
状态(0,NEW_Y).

状态(0,2):-不是(visited_state(2,0)),
断言(visited_state(0,2)),
write(将2加仑的水从3加仑的水罐倒入4加仑的水:(",

0,,",2,)->(",2,,",0,)\ n"),
状态(2,0).

状态(2,Y):-不是(visited_state(0,Y)),
断言(visited_state(2,Y)),
写(从地面上的4加仑水罐中倒空2加仑:(,

2,,",Y,)->((",0,,",Y,)\ n"),
状态(0,Y).

目标:-
makewindow(1,2,3,"4-3水壶问题",0,0,25,80),
状态(0,0).

I tried a water jug problem of 4 gallon and 3 gallon jug in SWI-Prolog but it is showing me a operator excepted error. So I kindly request you expert people to resolve the water jug problem code which i have mentioned below.

What I have tried:

I have attached a code please kindly have a look of it.
visited_state(integer,integer).


state(integer,integer).


state(2,0).

state(X,Y):- X (",4,",",Y,")\n"),
state(4,Y).


state(X,Y):- Y (",X,",",3,")\n"),
state(X,3).

state(X,Y):- X > 0,
not(visited_state(0,Y)),
assert(visited_state(X,Y)),
write("Empty the 4-Gallon jug on ground: (", X,",",Y,") -->

(", 0,",",Y,")\n"),
state(0,Y).

state(X,Y):- Y > 0,
not(visited_state(X,0)),
assert(visited_state(X,0)),
write("Empty the 3-Gallon jug on ground: (", X,",",Y,") -->

(", X,",",0,")\n"),
state(X,0).

state(X,Y):- X + Y >= 4,
Y > 0,
NEW_Y is Y - (4 - X),
not(visited_state(4,NEW_Y)),
assert(visited_state(X,Y)),
write("Pour water from 3-Gallon jug to 4-gallon until it is

full: (", X,",",Y,") --> (", 4,",",NEW_Y,")\n"),
state(4,NEW_Y).

state(X,Y):- X + Y >=3,
X > 0,
NEW_X is X - (3 - Y),
not(visited_state(X,3)),
assert(visited_state(X,Y)),
write("Pour water from 4-Gallon jug to 3-gallon until it is

full: (", X,",",Y,") --> (", NEW_X,",",3,")\n"),
state(NEW_X,3).

state(X,Y):- X + Y 0,
NEW_X is X + Y,
not(visited_state(NEW_X,0)),
assert(visited_state(X,Y)),
write("Pour all the water from 3-Gallon jug to 4-gallon:

(", X,",",Y,") --> (", NEW_X,",",0,")\n"),
state(NEW_X,0).

state(X,Y):- X+Y 0,
NEW_Y is X + Y,
not(visited_state(0,NEW_Y)),
assert(visited_state(X,Y)),
write("Pour all the water from 4-Gallon jug to 3-gallon:

(", X,",",Y,") --> (", 0,",",NEW_Y,")\n"),
state(0,NEW_Y).

state(0,2):- not(visited_state(2,0)),
assert(visited_state(0,2)),
write("Pour 2 gallons from 3-Gallon jug to 4-gallon: (",

0,",",2,") --> (", 2,",",0,")\n"),
state(2,0).

state(2,Y):- not(visited_state(0,Y)),
assert(visited_state(2,Y)),
write("Empty 2 gallons from 4-Gallon jug on the ground: (",

2,",",Y,") --> (", 0,",",Y,")\n"),
state(0,Y).

goal:-
makewindow(1,2,3,"4-3 Water Jug Problem",0,0,25,80),
state(0,0).

推荐答案


这篇关于如何在SWI-Prolog中实现水壶问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:55