19eucs170-Vignesh.M-AI Lab-4
19eucs170-Vignesh.M-AI Lab-4
M
ROLLNO:19EUCS170
Objective:
Algorithm:
Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure and stops.
Step 3: Select the node from the OPEN list which has the smallest value of evaluation function
(g+h), if node n is goal node, then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed list. For each
successor n', check whether n' is already in the OPEN or CLOSED list, if not then compute
evaluation function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to the back
pointer which reflects the lowest g(n') value.
In this example, we will traverse the given graph using the A* algorithm. The heuristic value of all
states is given in the below table so we will calculate the f(n) of each state using the formula f(n)=
g(n) + h(n), where g(n) is the cost to reach any node from start state.
Iteration3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11), (S--> A-->B, 7), (S-->G, 10)}
Iteration 4 will give the final result, as S--->A--->C--->G it provides the optimal path with cost 6.
import random
import itertools
import collections
import time
class Node:
"""
"""
self.puzzle = puzzle
self.parent = parent
self.action = action
if (self.parent != None):
self.g = parent.g + 1
else:
self.g = 0
@property
def score(self):
@property
def state(self):
"""
return str(self)
@property
def path(self):
"""
"""
node, p = self, []
while node:
p.append(node)
node = node.parent
@property
def solved(self):
return self.puzzle.solved
@property
def actions(self):
return self.puzzle.actions
@property
def h(self):
""""h"""
@property
def f(self):
""""f"""
def __str__(self):
return str(self.puzzle)
class Solver:
"""
An '8-puzzle' solver
"""
self.start = start
def solve(self):
"""
"""
queue = collections.deque([Node(self.start)])
seen = set()
seen.add(queue[0].state)
while queue:
node = queue.popleft()
return node.path
queue.appendleft(child)
seen.add(child.state)
class Puzzle:
"""
e.g. [[1,2,3],[4,0,6],[7,5,8]]
"""
self.width = len(board[0])
self.board = board
@property
def solved(self):
"""
increasing order from left to right and the '0' tile is in the
"""
N = self.width * self.width
def actions(self):
"""
"""
moves = []
for i, j in itertools.product(range(self.width),
range(self.width)):
'L':(i, j+1),
'D':(i-1, j),
'U':(i+1, j)}
if r >= 0 and c >= 0 and r < self.width and c < self.width and \
self.board[r][c] == 0:
moves.append(move)
return moves
@property
def manhattan(self):
distance = 0
for j in range(3):
if self.board[i][j] != 0:
x, y = divmod(self.board[i][j]-1, 3)
return distance
def shuffle(self):
"""
Return a new puzzle that has been shuffled with 1000 random moves
"""
puzzle = self
for _ in range(1000):
puzzle = random.choice(puzzle.actions)[0]()
return puzzle
def copy(self):
"""
"""
board = []
return Puzzle(board)
"""
Return a new puzzle where 'at' and 'to' tiles have been swapped.
"""
copy = self.copy()
i, j = at
r, c = to
return copy
def pprint(self):
print(row)
print()
def __str__(self):
def __iter__(self):
# example of use
board = [[1,2,3],[4,5,0],[6,7,8]]
puzzle = Puzzle(board)
#puzzle = puzzle.shuffle()
s = Solver(puzzle)
tic = time.process_time()
p = s.solve()
toc = time.process_time()
steps = 0
print(node.action)
node.puzzle.pprint()
steps += 1
Output:
Result:
Coding 30
Documentation 10
Viva 10
Total 100