CS 312: Artificial Intelligence Laboratory: A Algorithm

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

​CS 312: Artificial Intelligence Laboratory

Hritik Kumar (170010013) Alok Divya (170010014)

A* Algorithm:

Domain Description:​ Water Jug Problem

You  are  given  an  ​m  litre  jug  and  an  ​n  litre  jug.  Both  the 

jugs  are  initially  empty.  The  jugs  don’t  have  markings  to 

allow  measuring  smaller  quantities.  You  have  to  use  the 

jugs to measure d
​ ​ litres of water where ​d​ is less than n
​ ​.  

(​X​,  ​Y​)  corresponds  to  a  state  where  X  refers  to  the 

amount  of  water  in  Jug1  and  Y  refers  to  the  amount  of 

water in Jug2. 

Initial state​ (xi, yi) to the​ final state​ (xf, yf), where (xi, yi) is​ (0, 0) ​which indicates both Jugs are 

initially empty and (xf, yf) indicates a state which 

could be (0, d) or​ (d, 0)​. 

The operations you can perform are​: 

1. Empty a Jug, (X, Y) -> (0, Y) (Empty Jug 1) 


2. Fill a Jug, (0, 0)->(X, 0) (Fill Jug 1) 
3. Pour water from one jug to the other until one 
of the jugs is either empty or full, (X, Y) -> (X-d, 
Y+d) 
 

​State-space:​ G
​ iven the capacity of the two jugs are X litre and Y litre then the 
possible number of states are (X+1)*(Y+1).
MOVEGEN and GOALTEST algorithm:
MOVEGEN is implemented with name MoveGen 
def​ ​MoveGen​(​next_item​):

​if​(next_item[​1​].x < capcity_x ​and


quadrants[capcity_x][next_item[​1​].y].visit == -​1​):
q.put((heuristic(capcity_x, next_item[​1​].y),
quadrants[capcity_x][next_item[​1​].y]))
quadrants[capcity_x][next_item[​1​].y].p_x = next_item[​1​].x
quadrants[capcity_x][next_item[​1​].y].p_y = next_item[​1​].y
quadrants[capcity_x][next_item[​1​].y].visit = ​1

​if​(next_item[​1​].y < capcity_y ​and


quadrants[next_item[​1​].x][capcity_y].visit == -​1​):
q.put((heuristic(next_item[​1​].x, capcity_y),
quadrants[next_item[​1​].x][capcity_y]))
quadrants[next_item[​1​].x][capcity_y].p_x = next_item[​1​].x
quadrants[next_item[​1​].x][capcity_y].p_y = next_item[​1​].y
quadrants[next_item[​1​].x][capcity_y].visit = ​1

​if​(next_item[​1​].x > ​0​ ​and​ quadrants[​0​][next_item[​1​].y].visit == -​1​):


q.put((heuristic(​0​, next_item[​1​].y), quadrants[​0​][next_item[​1​].y]))
quadrants[​0​][next_item[​1​].y].p_x = next_item[​1​].x
quadrants[​0​][next_item[​1​].y].p_y = next_item[​1​].y
quadrants[​0​][next_item[​1​].y].visit = ​1

​if​(next_item[​1​].y > ​0​ ​and​ quadrants[next_item[​1​].x][​0​].visit == -​1​):


q.put((heuristic(next_item[​1​].x, ​0​),quadrants[next_item[​1​].x][​0​]))
quadrants[next_item[​1​].x][​0​].p_x = next_item[​1​].x
quadrants[next_item[​1​].x][​0​].p_y = next_item[​1​].y
quadrants[next_item[​1​].x][​0​].visit = ​1

​if​(next_item[​1​].y > ​0​):


l = ​min​(next_item[​1​].x + next_item[​1​].y, capcity_x)
m = ​max​(​0​, next_item[​1​].x + next_item[​1​].y - capcity_x)
​if​(quadrants[​min​(next_item[​1​].x + next_item[​1​].y,
capcity_x)][​max​(​0​, next_item[​1​].x + next_item[​1​].y - capcity_x)].visit ==
-​1​):
q.put((heuristic(l, m),quadrants[l][m]))
quadrants[l][m].p_x = next_item[​1​].x
quadrants[l][m].p_y = next_item[​1​].y
quadrants[l][m].visit = ​1

​if​(next_item[​1​].x > ​0​):


l = ​max​(​0​, next_item[​1​].x + next_item[​1​].y - capcity_y)
m = ​min​(next_item[​1​].x + next_item[​1​].y , capcity_y)
​if​(quadrants[l][m].visit == -​1​):
q.put((heuristic(l, m),quadrants[l][m]))
quadrants[l][m].p_x = next_item[​1​].x
quadrants[l][m].p_y = next_item[​1​].y
quadrants[l][m].visit = ​1

Goal Test:
if​(next_item[​1​].x == target ​or​ next_item[​1​].y == target):
a = ​1
Return True

Heuristic functions considered:


We tried to create an analogy between the water jug problem with maze problem. Our
start state is (0,0) and the goal state is (d,0) with only limited transition allowed. We took
the euclidean distance as cost(g(n)).

Overestimating Heuristic function:


For overestimating we generate a random number between the capacity of x times
euclidean distance and the capacity of x plus 5 times the Euclidean distance.
Underestimating Heuristic function:
For this, we took the euclidean distance of the current state from the goal state.

Monotonic Heuristic:
For this, we took euclidean distance as we know that euclidean satisfies monotone
property.

A* algorithm analysis and observation:


We found that A* algorithm always finds the path if exists.

Capacity Capacity_ Goal h1(x) h1(x) h2(x) - h2(x) - node


_X y cost node explored cost explored

50 13 35 243.923 32 243.923 28

120 33 70 Not found 102 Not found 102

17 3 12 28.97 20 28.97 13

We observed that both the heuristic find the path with optimal cost but no. of
states explored varies significantly. The number of states explored is very less in
underestimating heuristic function than in overestimating heuristic function.

How to Run
“ python3 Water.py”

You might also like