Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to solve the 4-3 Gallon Water Jug Problem

Posted By: Milind Mishra     Category: Artificial Intelligence     Views: 18570

Prolog program to solve the 4-3 Gallon Water Jug Problem

Code for Prolog program to solve the 4-3 Gallon Water Jug Problem in Artificial Intelligence

database
    visited_state(integer,integer)

predicates

    state(integer,integer)
    
clauses

    state(2,0).
    
    state(X,Y):- X < 4,
            not(visited_state(4,Y)),
            assert(visited_state(X,Y)),            
            write("Fill the 4-Gallon Jug: (",X,",",Y,") --> (", 4,",",Y,")\n"),
            state(4,Y).

    
    state(X,Y):- Y < 3,
            not(visited_state(X,3)),
            assert(visited_state(X,Y)),
            write("Fill the 3-Gallon Jug: (", X,",",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 = 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 = 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 <=4,
            Y > 0,
            NEW_X = 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<=3,
            X > 0,
            NEW_Y = 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).    


Output


+-----------------------------4-3 Water Jug Problem--------------------------+
¦Fill the 4-Gallon Jug: (0,0) --> (4,0)                                      ¦
¦Fill the 3-Gallon Jug: (4,0) --> (4,3)                                      ¦
¦Empty the 4-Gallon jug on ground: (4,3) --> (0,3)                           ¦
¦Pour all the water from 3-Gallon jug to 4-gallon: (0,3) --> (3,0)           ¦
¦Fill the 3-Gallon Jug: (3,0) --> (3,3)                                      ¦
¦Pour water from 3-Gallon jug to 4-gallon until it is full: (3,3) --> (4,2)  ¦
¦Empty the 4-Gallon jug on ground: (4,2) --> (0,2)                           ¦
¦Pour all the water from 3-Gallon jug to 4-gallon: (0,2) --> (2,0)           ¦
¦                                                                            ¦
¦Press the SPACE bar                                                         ¦
¦                                                                            ¦
¦                                                                            ¦
¦                                                                            ¦
+----------------------------------------------------------------------------+


  
Share: 


Didn't find what you were looking for? Find more on Prolog program to solve the 4-3 Gallon Water Jug Problem Or get search suggestion and latest updates.

Milind Mishra
Milind Mishra author of Prolog program to solve the 4-3 Gallon Water Jug Problem is from India.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!