You are on page 1of 10

AI & Logic Programming RA3803A05 CAP-451/Design Problem-1

Design Problem Title/No. 1 Course Code: CAP-451

Course Instructor: Mr. JASWINDER SINGH Course Tutor( if applicable):

Date of Allotment : Date of submission : 15-03-10

Student’s Roll number : RA3803A05 Section No.: A3803

Declaration:

I declare that this assignment is my individual work. I have not


copied from any other student’s work or from any other source except where
due acknowledgement is made explicitly in the text, nor has any part been
written for me by another person.

Student’s Sign:

Evaluator’s Comments:
___________________________________________________

Marks obtained: ____________ out of __________________

Page 1
AI & Logic Programming RA3803A05 CAP-451/Design Problem-1

CAP451
Design Problem 1
DOS:15-March-2010 Marks: 20

In “Farmer-Fox-Goose-Grain ” puzzle, a farmer wishes to cross a


river taking his fox, goose, ad grain with him.
He can use a boat which will accommodate only the farmer and one
possession.
Expectations:

i. If the fox is left alone with the goose, the goose will be eaten.
If the goose is left alone with the grain it will be eaten [3]

ii. How many journeys will be taken to cross river [3]

iii. Draw a state space search tree for this puzzle using left bank
and right bank to denote let and right banks respectively [4]

iv. For solving this problem, whether you will use Forward
reasoning or backward reasoning? Justify your answer [4]

v. Represent This Puzzle in Prolog Programming [6]

Page 2
AI & Logic Programming RA3803A05 CAP-451/Design Problem-1

SOLUTION
EXPECTATION-I
If the fox is left alone with the goose, the goose will be eaten. If the
goose is left alone with the grain it will be eaten [3]

Farmer+Goose+Fox+Grain Farmer+Goose Nobody

In first Journey Farmer takes Goose to other side. Above condition satisfied.

Fox+Grain Farmer Goose

In second journey the Farmer drop the Goose and come alone.

Fox Farmer+Grain Goose

In the third journey Farmer takes the grain to other side. Condition satisfied.

Fox Farmer+Goose Grain

In fourth journey Farmer come back with goose with condition satisfied.

Goose Farmer+Fox Grain

In fifth journey Farmer drops Goose on first side and takes the fox with him.

Goose Farmer Fox+Grain

In the sixth journey Farmer drops Fox on other side and come back.

Nobody Farmer+Goose Fox+Grain

In the seventh journey the Farmer finally takes the Goose and reach
successfully by all the three things with no loss.

In the above seven steps of Farmer’s journey from one side to other side of
the river neither fox and grain, goose nor goose, fox stay or go together.
Therefore the above condition gets satisfied.

Page 3
AI & Logic Programming RA3803A05 CAP-451/Design Problem-1

EXPECTATION-II
How many journeys will be taken to cross river [3]

The following moves will be taken by the farmer for successful journey:

i. In First Move the farmer will take Goose and leave the Goose on the
other end because Fox will not eat grain and also the Farmer will
satisfy the condition of number of persons which can be handled by
the boat.
ii. In Second Move the Farmer will return alone.
iii. In the third Move the farmer will take the grain and leave the grain
there but he will pick the Goose with him, so that the Goose will not
eat grain.
iv. In fourth Move the farmer will take the Goose back with him.
v. In the fifth move the farmer will drop the Goose their and pick the
Fox.
vi. In the sixth move the farmer will drop the Fox on the other side and
come back alone and also by doing this the Fox will not eat the Grain.
vii. In the seventh move the farmer will finally take the Goose on the
other side and make a successful journey.

EXPECTATION-III
Draw a state space search tree for this puzzle using left bank and
right bank to denote left and right banks respectively [4]

Now for a farmer to cross the river with no any kind of loss of his animals and
grain he should have some state which describe his position during the
problem. The farmer will have three states:
 Initial state
 Intermediate state
 Final or Goal state

Now in various states the representation will as follows:


Consider
Farmer as ‘FM’, Fox as ‘FX’, Goose as ‘GS’, Grain as ‘GR’
Because it will make it easier during the notation.

The states are:


Initial state
Initial (Fm,Fx,Gs,Gr)

Page 4
AI & Logic Programming RA3803A05 CAP-451/Design Problem-1

Now since we have two sides of the river so we should need some
notation symbols to describe them. So to resolve this problem either
takes some Boolean type values like true/false or take binary number
system digit to represent the two states i.e.
1  left bank of the river
0  right bank of the river
Intermediate states:
1. Move(Fm,Gs)
2. Drop(Gs)
3. Back(Fm)
4. Move(Fm,Gr)
5. Drop(Gr)
6. Back(Fm,Gs)
7. Move(Fm,Fx)
8. Drop(Fx)
9. Back(Fm) //Repeated
10. Move(Fm,Gs) //Repeated
11. Left(Fx,Gr)
12. Right(Gs)
13. Left(Fx)
14. Right(Gr)
15. Left(Gs)
16. Right(Fx,Gr)
Final state
Goal(Fm,Gs,Fs,Fx)

On the basis of the above data the following table will be drawn:
Sr. No. True/ Farmer Fox (Fx) Grain Goose
False (Fm) (Gr) (Gs)
1. Y 1 1 1 1
2. Y 1 1 1 0
3. Y 1 1 0 1
4. N 1 1 0 0
5. Y 1 0 1 1
6. Y 1 0 1 0
7. N 1 0 0 1
8. N 1 0 0 0
9. N 0 1 1 1
10. N 0 1 1 0
11. Y 0 1 0 1
12. 0
Y 0 1 0 0
13. 0
N 0 0 1 1
14. Y 0 0 1 0
15. Y 0 0 0 1
16. 0
Y 0 0 0 0

Page 5
AI & Logic Programming RA3803A05 CAP-451/Design Problem-1

STATE SPACE SEARCH DIAGRAM

Fa, Fo, Go, Gr


1, 1, 1, 1

Fa, Fo, Go, Gr Fa, Fo, Go, Gr Fa, Fo, Go, Gr


0, 0, 1, 1 0, 1, 0, 1 0, 1, 1, 0

Fa, Fo, Go, Gr


1, 1, 0, 1

Fa, Fo, Go, Gr Fa, Fo, Go, Gr


0, 1, 0, 0 0, 0, 0, 1

Fa, Fo, Go, Gr Fa, Fo, Gr, Go


1, 1, 1, 0 1, 0, 1, 0

Fa, Fo, Go, Gr


0, 0, 1, 0

Fa, Fo, Go, Gr


1, 0, 1, 0

Fa, Fo, Go, Gr


0, 0, 0, 0

Page 6
AI & Logic Programming RA3803A05 CAP-451/Design Problem-1

EXPECTATION-IV

For solving this problem, whether you will use Forward reasoning or
backward reasoning? Justify your answer [4]

Forward chaining:
Forward chaining is one of the two main methods of reasoning when using
inference rules (in artificial intelligence). The opposite of forward chaining is
backward chaining.
Forward chaining starts with the available data and uses inference rules to
extract more data (from an end user for example) until a goal is reached. An
inference engine using forward chaining searches the inference rules until it
finds one where the antecedent (If clause) is known to be true. When found it
can conclude, or infer, the consequent (Then clause), resulting in the addition
of new information to its data.
Inference engines will iterate through this process until a goal is reached.
For example, suppose that the goal is to conclude the color of a pet named
Fritz, given that he croaks and eats flies, and that the rule base contains the
following four rules:
1. If X croaks and eats flies - Then X is a frog
2. If X chirps and sings - Then X is a canary
3. If X is a frog - Then X is green
4. If X is a canary - Then X is yellow

Backward Chaining:
Backward chaining (or backward reasoning) is an inference method used in
automated theorem provers, proof assistants and other artificial intelligence
applications. It is one of the two most commonly used methods of reasoning
with inference rules and logical implications – the other is forward chaining.
Backward chaining is implemented in logic programming by SLD resolution.
Backward chaining starts with a list of goals (or a hypothesis) and works
backwards from the consequent to the antecedent to see if there is data
available that will support any of these consequents. An inference engine
using backward chaining would search the inference rules until it finds one
which has a consequent (Then clause) that matches a desired goal. If the
antecedent (If clause) of that rule is not known to be true, then it is added to
the list of goals (in order for one's goal to be confirmed one must also provide
data that confirms this new rule).
For example, suppose that the goal is to conclude the color of my pet Fritz,
given that he croaks and eats flies, and that the rule base contains the
following four rules:
1. If X croaks and eats flies – Then X is a frog
2. If X chirps and sings – Then X is a canary
3. If X is a frog – Then X is green
4. If X is a canary – Then X is yellow

Page 7
AI & Logic Programming RA3803A05 CAP-451/Design Problem-1

In the above problem we have forward and backward chaining in


the following steps:
Forward chaining steps:
 Step 1 we use forward reasoning because we are moving toward the
destination stage or goal stage from Initial Stage.
Initial(Fa,Gs)  final(1,1)
Because I consider the final stage as 0 and initial as 1.
Initial(0,0)  final(1,1)

Backward chaining steps:


 Step 4 we use backward chaining because we are moving toward the
initial stage from the goal stage.
Initial(1,1)  final(Fa,Gs)
Because I consider the final stage as 0 and initial as 1.
Initial(1,1)  final(0,0)

EXPECTATION-V
Represent This Puzzle in Prolog Programming [6]

puzzle(Start_State, Goal) :-
nl,
empty_stack(Empty_stack),
stack(Start_State, Empty_stack, Stack),
path(Start_State, Goal, Stack).

move(state(Farmer, Farmer, Goose, Grain), state(Crossing_Farmer,


Crossing_Farmer, Goose, Grain)) :-
cross(Farmer, Crossing_Farmer),
not(unsafe(state(Crossing_Farmer, Crossing_Farmer, Goose,
Grain))),
writelist(['Try farmer takes Fox -->', Crossing_Farmer,
Crossing_Farmer, Goose, Grain]).

move(state(Farmer, Fox, Farmer, Grain), state(Crossing_Farmer, Fox,


Crossing_Farmer, Grain)) :-
cross(Farmer, Crossing_Farmer),
not(unsafe(state(Crossing_Farmer, Fox, Crossing_Farmer, Grain))),
writelist(['Try farmer takes Goose -->', Crossing_Farmer, Fox,
Crossing_Farmer, Grain]).

move(state(Farmer, Fox, Goose, Farmer), state(Crossing_Farmer, Fox, Goose,


Crossing_Farmer)) :-

cross(Farmer, Crossing_Farmer),
not(unsafe(state(Crossing_Farmer, Fox, Goose, Crossing_Farmer))),

Page 8
AI & Logic Programming RA3803A05 CAP-451/Design Problem-1

writelist(['Try farmer takes Grain -->', Crossing_Farmer, Fox, Goose,


Crossing_Farmer]).

move(state(Farmer, Fox, Goose, Grain), state(Crossing_Farmer, Fox, Goose,


Grain)) :-
cross(Farmer, Crossing_Farmer),
not(unsafe(state(Crossing_Farmer, Fox, Goose, Grain))),
writelist(['Try farmer takes self -->', Crossing_Farmer, Fox, Goose,
Grain]).

move(state(Farmer, Fox, Goose, Grain), state(Farmer, Fox, Goose, Grain)) :-


writelist([' Backtrack from -->', Farmer, Fox, Goose, Grain]),
fail.

empty_stack([]).

stack(ElementA, ElementB, [ElementA|ElementB]).

path(Goal, Goal, Stack) :-


nl,
write('Solution Path is:'),
nl,
reverse_print_stack(Stack).

path(State, Goal, Stack) :-


move(State, NextState),
not(member_stack(NextState, Stack)),
stack(NextState, Stack, NewStack),
path(NextState, Goal, NewStack), !.

reverse_print_stack(Stack) :-
empty_stack(Stack).
reverse_print_stack(Stack) :-
stack(ElementA, ElementB, Stack),
reverse_print_stack(ElementB),
write(ElementA),

nl.

member_stack(State, Stack) :-
member(State, Stack).

unsafe(state(Farmer, Farmer_Fox_Goose, Farmer_Fox_Goose, Grain)) :-


cross(Farmer, Farmer_Fox_Goose).

Page 9
AI & Logic Programming RA3803A05 CAP-451/Design Problem-1

unsafe(state(Farmer, Fox, Farmer_Goose_Grain, Farmer_Goose_Grain)) :-


cross(Farmer, Farmer_Goose_Grain).

writelist([]) :- nl.
writelist([ElementA|ElementB]) :-
print(ElementA),

tab(1),
writelist(ElementB).

cross(s, n).
cross(n, s).

Another Solution:

change(e,w).
change(w,e).
move([X,X,Goose,Grain],Fox,[Y,Y,Goose,Grain]) :-
change(X,Y).
move([X,Fox,X,Grain],Goose,[Y,Fox,Y,Grain]) :-
change(X,Y).
move([X,Fox,Goose,X],Grain,[Y,Fox,Goose,Y]) :-
change(X,Y).
move([X,Fox,Goose,C],nothing,[Y,Fox,Goose,C]) :-
change(X,Y).
oneEq(X,X,_).
oneEq(X,_,X).

safe([Man,Fox,Goose,Grain]) :-
oneEq(Man,Goose,Fox),
oneEq(Man,Goose,Grain).

solution([e,e,e,e],[]).
solution(Config,[Move|Rest]) :-
move(Config,Move,NextConfig),
safe(NextConfig),
solution(NextConfig,Rest).

Page
10

You might also like