Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Production system for the Missionary cannibal problem

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

production system for the Missionary cannibal problem. Solve it using generalized production rule based program in prolog.

Code for Production system for the Missionary cannibal problem in Artificial Intelligence

domains
    leftX,leftY,rightX,rightY = integer
database
    go(integer,integer,integer,integer)
predicates
    travel(integer,integer,integer,integer)
clauses
    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 0,
                        LeftY = 0,
                        RightX = 3,
                        RightY = 3,
                        write("\nProblem is solved.").
    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 3,
                        LeftY = 3,
                        NewLeftX = 2,
                        NewLeftY = 2,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , NewLeftX, " Missionaries : ", NewLeftY),
                        Write(" Right : cannibals : " , RightX, " Missionaries : ", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 0,
                        RightY = 0,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 0,
                        NewRightY = 1,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , LeftX, " Missionaries : ", LeftY),
                        Write(" Right : cannibals : " , NewRightX, " Missionaries : ", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 2,
                        LeftY = 2,
                        NewLeftX = 3,
                        NewLeftY = 0,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , NewLeftX, " Missionaries : ", NewLeftY),
                        Write(" Right : cannibals : " , RightX, " Missionaries : ", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 0,
                        RightY = 1,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 0,
                        NewRightY = 2,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , LeftX, " Missionaries : ", LeftY),
                        Write(" Right : cannibals : " , NewRightX, " Missionaries : ", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 3,
                        LeftY = 0,
                        NewLeftX = 1,
                        NewLeftY = 1,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , NewLeftX, " Missionaries : ", NewLeftY),
                        Write(" Right : cannibals : " , RightX, " Missionaries : ", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 0,
                        RightY = 2,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 1,
                        NewRightY = 1,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , LeftX, " Missionaries : ", LeftY),
                        Write(" Right : cannibals : " , NewRightX, " Missionaries : ", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).


    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 1,
                        LeftY = 1,
                        NewLeftX = 0,
                        NewLeftY = 2,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , NewLeftX, " Missionaries : ", NewLeftY),
                        Write(" Right : cannibals : " , RightX, " Missionaries : ", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 1,
                        RightY = 1,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 3,
                        NewRightY = 0,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , LeftX, " Missionaries : ", LeftY),
                        Write(" Right : cannibals : " , NewRightX, " Missionaries : ", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).


    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 0,
                        LeftY = 2,
                        NewLeftX = 0,
                        NewLeftY = 1,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , NewLeftX, " Missionaries : ", NewLeftY),
                        Write(" Right : cannibals : " , RightX, " Missionaries : ", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).
    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 3,
                        RightY = 0,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 3,
                        NewRightY = 1,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , LeftX, " Missionaries : ", LeftY),
                        Write(" Right : cannibals : " , NewRightX, " Missionaries : ", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

    travel(LeftX,LeftY,RightX,RightY) :- 
                        LeftX = 0,
                        LeftY = 1,
                        NewLeftX = 0,
                        NewLeftY = 0,
                        NewRightX = RightX,
                        NewRightY = RightY,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , NewLeftX, " Missionaries : ", NewLeftY),
                        Write(" Right : cannibals : " , RightX, " Missionaries : ", RightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).
    travel(LeftX,LeftY,RightX,RightY) :- 
                        RightX = 3,
                        RightY = 1,
                        NewLeftX = LeftX,
                        NewLeftY = LeftY,
                        NewRightX = 3,
                        NewRightY = 3,
                        not(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        assert(go(NewLeftX,NewLeftY,NewRightX,NewRightY)),
                        Write("\nLeft : cannibals : " , LeftX, " Missionaries : ", LeftY),
                        Write(" Right : cannibals : " , NewRightX, " Missionaries : ", NewRightY),
                        travel(NewLeftX,NewLeftY,NewRightX,NewRightY).

goal
    makewindow(1,2,3,"Cannibals - missionaries problem",0,0,25,80),
    Write("\nLeft : cannibals : 3" ,  " Missionaries : 3" ),    
    Write(" Right : cannibals : 0" ,  " Missionaries : 0" ),
    travel(3,3,0,0).
  
Share: 


Didn't find what you were looking for? Find more on Production system for the Missionary cannibal problem Or get search suggestion and latest updates.

Milind Mishra
Milind Mishra author of Production system for the Missionary cannibal 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!