Will upvote Please fix the following code and post your inputs and o.pdf
1. Will upvote Please fix the following code and post your inputs and outputs please run your code
to make sure it works
import sys
import heapq
import copy
class PriorityQueue():
def __init__(self):
self.thisQueue = []
def push(self, thisNode):
heapq.heappush(self.thisQueue, (thisNode.val, -thisNode.id, thisNode))
def pop(self):
return heapq.heappop(self.thisQueue)[2]
def isEmpty(self):
return len(self.thisQueue) == 0
def length(self):
return len(self.thisQueue)
class Set():
def __init__(self):
self.thisSet = set()
def add(self, entry):
if entry is not None:
self.thisSet.add(entry.__hash__())
def length(self):
return len(self.thisSet)
def isMember(self, query):
return query.__hash__() in self.thisSet
2. class Node():
def __init__(self, val, startstate, endstate, depth):
global nodeid
self.id = nodeid
nodeid += 1
self.val = val
self.state = startstate
self.endstate = endstate
self.depth = depth
def __str__(self):
return 'Node: id=%d val=%d' % (self.id, self.val)
def expand(current_node, closedlist, goal_state):
closedlist.add(current_node.state)
successors = []
up_child = current_node.state.up()
down_child = current_node.state.down()
right_child = current_node.state.right()
left_child = current_node.state.left()
children = [left_child, right_child, up_child, down_child]
for child in children:
if child is not None and not closedlist.isMember(child):
s = Node(0, child, None, current_node.depth + 1)
s.parent_node = current_node
if child == left_child:
s.action = "left"
elif child == right_child:
s.action = "right"
elif child == up_child:
s.action = "up"
elif child == down_child:
s.action = "down"
successors.append(s)
3. return successors
def h0(currenttiles, stiles):
misplaced = 0
for i in range(len(stiles)):
for j in range(len(currenttiles)):
if stiles[i][j] != currenttiles[i][j]:
misplaced += 1
return misplaced
def manhattan(currenttiles, stiles):
total = 0
for i in range(len(stiles)):
for j in range(len(currenttiles)):
if stiles[i][j] != currenttiles[i][j]:
y = stiles[i][j]
a = i
b = j
for k in range(len(stiles)):
for l in range(len(stiles)):
if currenttiles[k][l] == y:
d = k
e = l
count = (abs(d - a) + abs(e - b))
total += count
return total
class State():
def __init__(self):
self.xpos = 0
self.ypos = 0
self.tiles = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
def left(self):
if self.ypos == 0:
return None
4. s = self.copy()
s.tiles[s.xpos][s.ypos] = s.tiles[s.xpos][s.ypos - 1]
s.ypos -= 1
s.tiles[s.xpos][s.ypos] = 0
return s
def right(self):
if self.ypos == 2:
return None
s = self.copy()
s.tiles[s.xpos][s.ypos] = s.tiles[s.xpos][s.ypos + 1]
s.ypos += 1
s.tiles[s.xpos][s.ypos] = 0
return s
def up(self):
if self.xpos == 0:
return None
s = self.copy()
s.tiles[s.xpos][s.ypos] = s.tiles[s.xpos - 1][s.ypos]
s.xpos -= 1
s.tiles[s.xpos][s.ypos] = 0
return s
def down(self):
if self.xpos == 2:
return None
s = self.copy()
s.tiles[s.xpos][s.ypos] = s.tiles[s.xpos + 1][s.ypos]
s.xpos += 1
s.tiles[s.xpos][s.ypos] = 0
return s
def __hash__(self):
return (tuple(self.tiles[0]), tuple(self.tiles[1]), tuple(self.tiles[2]))
5. def __str__(self):
return '%d %d %dn%d %d %dn%d %d %dn' % (
self.tiles[0][0], self.tiles[0][1], self.tiles[0][2],
self.tiles[1][0], self.tiles[1][1], self.tiles[1][2],
self.tiles[2][0], self.tiles[2][1], self.tiles[2][2])
def copy(self):
s = State()
s.xpos = self.xpos
s.ypos = self.ypos
s.tiles = [row[:] for row in self.tiles]
return s
def astar(initial_state, goal_state, closedlist, heuristic_type):
initial_node = Node(0, initial_state, goal_state, 0)
print(initial_node.val)
frontier = PriorityQueue()
frontier.push(initial_node)
V = 0
N = 0
d = 0
while not frontier.isEmpty():
if frontier.length() > N:
N = frontier.length()
current_node = frontier.pop()
V += 1
if current_node.state == goal_state:
d = current_node.depth # Found the solution
return current_node, V, N, d
expanded = expand(current_node, closedlist, goal_state)
for node in expanded:
if heuristic_type == 0:
f_val = h0(node.state.tiles, goal_state.tiles) + node.depth
elif heuristic_type == 1: