You are on page 1of 59

UNIT 1

-> AI APPLICATIONS

-> A*

-> IDA*

-> ALPHA BETA PRUNING

-> 3 MISSIONARIES AND 3


CANNIBALS STATE SPACE AND
PRODUCTION

-> WATER JUG PROBLEM

oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
oufastupdates.com
The Missionaries and Cannibals Problem:

The Missionaries and Cannibals Problem Statement

Three missionaries and three cannibals find the mselves on one side of a river. They have
would like to get to the other side. But the missionaries are not sure what else the cannibals
agreed to. So the missionaries managed the trip across the river in such a way that the
numbe r of missionaries on either side of the river is never less than the number of
cannibals who are on the same side. The only boar available holds only two at a time. How
can everyone get across the river without the missionaries risking being eaten?

Solution:-
The state for this problem can be defined as

{(i, j)/ i=0, 1, 2, 3, : j=0, 1, 2, 3}

where i represents the number missionaries in one side of a river . j represents the numbe r
of cannibals in the same side of river. The initial state is (3,3), that is three missionaries and
three cannibals one side of a river , (Bank 1) and ( 0,0) on another side of the river (bank 2)
. the goal state is to get (3,3) at bank 2 and (0,0) at bank 1.

To sole this proble m we will make the following assumptions:

1. Number of cannibals should lesser than the missionaries on either side.

2. Only one boat is available to travel.

3. Only one or maximum of two people can go in the boat at a time.

4. All the six have to cross the river from bank.

5. There is no restriction on the numbe r of trips that can be made to reach of the goal.

6. Both the missionaries and cannibals can row the boat.

oufastupdates.com
oufastupdates.com
This kind of problem is often solved by a graph search method. Represent the problem as a set of
states which are snapshots of the world and operators which transform one state into another
state are mapped to nodes of the graph and operators are the edges of the graph. The following
procedure shows the graph search algorithm in PROLOG, for Missionaries and Cannibals
Problem.

Path finding.

* Write a program to find path from one node to another.

* Must avoid cycles

* A template for the clause is

path(start, Finish, Visited, Path).


Start - the name of the starting node.
Finish - the name of the finishing node.
Visited - the list of nodes aready visited.
Path - the list of nodes on the path, including Start and Finish.

Program for finding a Path.

* The search for a path terminates when we have nowhere t0o go.

path {Node, Node,.., [Node]}

A path from start to Finish, starts with a node, X,connected to start followed by a path from X to
Finish.

path (Start, Finish, Visited, {Start [ Path]} :-


edge (Start,X),
not (member( X,Visited)),
path(x,Finish, {X [vISITED],Path)
member (X,[X I_ I).
member (X,I_ IYI):- member(X,Y).

Representing the state.

Now we returen to the peoblem or rrepresenting tyhe missionaries and cannibals problem;
*A State is one "snapshot" in time.

*For this problem the only infoemation we need to fully characterize the state is :
*the number of missionaries on the left bank,
*the number of cannibals on the left bank,
*the side the boat is on.

oufastupdates.com
All other information can be deduced from these thres items.

In PROLOG, the state can be representted by a 3-arity term, state (Missionaries,Cannibals,


State).

Representing the solution.

*The solution consists of a list of moves, e.g.


[move( I.I. right), move 2.0, lift)].
*We take this to mean that I missionary and i cannibal moved to the right bank, then 2
missionaries moved to hte left bank.
*Like the graph search problem, we must avoid returing to state we have visited before.
The visited list will have the form:
[Most Recent State I list of previousstates]

Overview of solution.

We follow a simple graph search procedure:

*Start from an initial state


*Find a neighbouring state
*Check that the now stae has not been visited before
*Find a path from the neighbour to the goal.

The search terminates when we have found the state:


state(0, 0, right).

PROLOG Code.

% mandc(CurrentState, Visited, Path)

mandc(start(0, 0, right), -,[]).


mande(Currentstate, visited,[Move I Rest O fMoves]):-
newstate (Current state, NestState, Move).
not (member(NestState,Visited)0,
make., move(Current state, NestState, Move),
mande((Nextstate I visited,[Move I Rest O fMoves])

make_move (state(M1,C1,left) state (M2,C2,right) move(M,C,right)) :-


M is M1 - M2.
C is C1 - C2.

make_move(state(M1,C1,right),state(M2,C2,right),move M,C,left)) :-
M is M2 -M1
C is C2 - C1.

oufastupdates.com
A move is characterized by the nummber of missionar5ies and the number of canniblls taken in
the boal at6 one time.

Since the boat can carry no more than two peopel at once, the only possible combinations are:
carry (2,0)
carry (1,0)
carry (1,1)
carry (0,1)
carry (0,2).
Where carry (M,C) means the boat will carry M, missionaries and C.cannibals on one trip.

Feasible Moves

*Once we hafe found a possible move, we have to confirm that it is feasible It is not feasible to
move more missionaries or more cannibals than that are present on one bank.
When the stae is stae(M1,C1,left) and we try carry (M,C)then
M ≤ M1 and C ≥ C1
must be true.

*When the state is state(M1, C1, left) and we try carry (M,C) then

M + M1 ≤ AND C + C1 ≤ 3

must be true.

Legal Moves.

* Once we have found a feasible move, we must check that it is legal, i.e no Missionaries
must be eaten.

legal(X,X).

legal(3,X).

legal(O,X).

*The only safe combinations are when there are equal numbe rs of Missionaries and
cannibals or all the Missionaries are on one side.

Generating the Next State.

oufastupdates.com
newstate(state(M1,C1,left), state(M2,C2,right)):-

carry(M,C),

M ≤ M1

C ≤ C1

M2 is M1 - M2,

C2 is C1 - C,

legal(M2,C2)

newstate(M1, C1, right), state(M2, C2, left)):-

carry(M,C),

M2 is M1 + M,

C2 is C1 + C,

M2 ≤ 3,

C2 ≤ 3,

legal(M2, C2)

oufastupdates.com
WaterJugProblem

WaterJugProblem

Statement :- We are given 2 jugs, a 4 liter one and a 3- liter one. Neither has any measuring
markers on it. There is a pump that can be used to fill the jugs with water. How can we get
exactly 2 liters of water in to the 4-liter jugs?
Solution:-
The state space for this problem can be defined as

{ ( i ,j ) i = 0,1,2,3,4 j = 0,1,2,3}

‘i’ represents the number of liters of water in the 4-liter jug and ‘j’ represents the number of liters
of water in the 3-liter jug. The initial state is ( 0,0) that is no water on each jug. The goal state is
to get ( 2,n) for any value of ‘n’.

To solve this we have to make some assumptions not mentioned in the problem. They are

1. We can fill a jug from the pump.

2. we can pour water out of a jug to the ground.

3. We can pour water from one jug to another.

4. There is no measuring device available.

The various operators (Production Rules) that are available to solve this problem may be stated
as given in the following figure .

oufastupdates.com
oufastupdates.com
oufastupdates.com
Constraint Satisfaction Problem1.

CONSTRAINT SATISFACTION:-
Many problems in AI can be considered as proble ms of constraint satisfaction, in which the
goal state satisfies a given set of constraint. constraint satisfaction problems can be solved
by using any of the search strategies. The general form of the constraint satisfaction
procedure is as follows:

Until a complete solution is found or until all paths have led to lead ends, do

1. select an unexpanded node of the search graph.

2. Apply the constraint inference rules to the selected node to generate all possible new
constraints.

3. If the set of constraints contains a contradiction, then report that this path is a dead end.

4. If the set of constraints describes a complete solution then report success.

5. If neither a constraint nor a complete solution has been found then apply the rules to
generate new partial solutions. Insert these partial solutions into the search graph.

Example: consider the crypt arithmetic proble ms.

SEND
+ MORE
----------
MONEY
----------

Assign decimal digit to each of the letters in such a way that the answer to the proble m is
correct to the same letter occurs more than once , it must be assign the same digit each time
. no two different letters may be assigned the same digit. Consider the crypt arithmetic
problem.

SEND
+ MORE
-----------
MONEY
-----------

CONSTRAINTS:-

1. no two digit can be assigned to same letter.

oufastupdates.com
2. only single digit number can be assign to a letter.

1. no two letters can be assigned same digit.

2. Assumption can be made at various levels such that they do not contradict each other.

3. The problem can be decomposed into secured constraints. A constraint satisfaction


approach may be used.

4. Any of search techniques may be used.

5. Backtracking may be performed as applicable us applied search techniques.

6. Rule of arithmetic may be followed.

Initial state of proble m.


D=?
E=?
Y=?
N=?
R=?
O=?
S=?
M=?
C1=?
C2=?
C1 ,C 2, C3 stands for the carry variables respectively.

Goal State: the digits to the letters must be assigned in such a manner so that the sum is
satisfied.

Solution Process:

We are following the depth-first method to solve the problem.

1. initial guess m=1 because the sum of two single digits can generate at most a carry '1'.

2. When n=1 o=0 or 1 because the largest single digit numbe r added to m=1 can generate
the sum of either 0 or 1 depend on the carry received from the carry sum. By t his we
conclude that o=0 because m is already 1 hence we cannot assign same digit another
letter(rule no.)

3. We have m=1 and o=0 to get o=0 we have s=8 or 9, again depending on the carry
received from the earlier sum.

oufastupdates.com
The same process can be repeated further. The proble m has to be composed into various
constraints. And each constraints is to be satisfied by guessing the possible digits that the
letters can be assume d that the initial guess has been already made . rest of the process is
being shown in the form of a tree, using depth-first search for the clear understandability
of the solution process.

D>6(Controduction)

oufastupdates.com
oufastupdates.com
Breadth First Search Procedure.

What do you know about BFS.?

Breadth First Search (BFS): This is also a brute force search procedure like DFS.
We are searching progresses level by level. Unlike DFS which goes deep into the tree. An
operator employed to generate all possible children of a node. BFS being a brute force search
generates all the nodes for identifying the goal.

oufastupdates.com
ALGORITHM:

Step 1. Put the initial node on a list “START”

Step 2. If START is empty or goal terminate the search.

Step 3. Remove the first node from the Start and call this node “a”

Step 4. If a =”GOAL” terminate search with success

Step 5. Else if node “a” has successors generate all of them and add them at the tail of

“START”

Step 6. Go to step 2.

Advantages:

1. BFS will not get trapped exploring a blind alley.

2. If there is a solution then BFS is guaranteed to find it.

3. The amount of time needed to generate all the nodes is considerable because of the time
complexity.

4. Memory constraint is also a major problem because of the space complexity.

5. The searching process remembers all unwanted nodes, which are not practical use for the
search process.

oufastupdates.com
What do you know about DFS.?

Depth first search: This is a very simple type of brute force searching techniques. The
search begins by expanding the initial node i.e. by using an operator generate all successors
of the initial node and test them.

This procedure finds whether the goal can be reached or not but the path it has to follow
has not been mentioned. Diving downward into a tree as quickly as possible performs Dfs
searches.

Algorithm:

Step1: Put the initial node on a list START.

Step2: If START is empty or START = GOAL terminates search.

Step3: Remove the first node from START. Call this node a.

Step4: If (a= GOAL) terminates search with success.

Step5: Else if node a has successors, generate all of them and add the m at the beginning

Of START.

Step6: Go to Step 2.

The major draw back of the DFS is the determination of the depth citric with the search
has to proceed this depth is called cut of depth.

The value of cutoff depth is essential because the search will go on and on. If the cutoff
depth is smaller solution may not be found. And if cutoff depth is large time complexity will
be more.

Advantages: DFS requires less me mory since only the nodes on the current path are stored.

By chance DFS may find a solution with out examining much of the search space at all.

oufastupdates.com

You might also like