SlideShare a Scribd company logo
1 of 6
Download to read offline
Fix this code so that it will run with proper answers, please dont use chatgpt and please test your
code before submitting:
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
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)
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
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]))
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:
f_val = manhattan(node.state.tiles, goal_state.tiles) + node.depth
node.val=f_val
frontier.push(node)
return None, V, N, d
def main():
global nodeid
nodeid = 0
inputs = []
for line in sys.stdin:
inputs += line.split()
array = [
[int(inputs[0]), int(inputs[1]), int(inputs[2])],
[int(inputs[3]), int(inputs[4]), int(inputs[5])],
[int(inputs[6]), int(inputs[7]), int(inputs[8])]
]
goal_state = State()
goal_state.tiles = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
s = State()
current = State()
current.tiles = array
s.tiles = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
print(array)
printMan = manhattan(array, goal_state.tiles)
print("Initial State:")
print(s)
print("Goal State:")
print(goal_state)
print("Manhattan Distance:", printMan)
for heuristic_type in range(2):
closedlist = Set()
nodeid = 0
d, N, V,_ = astar(current, goal_state, closedlist, heuristic_type)
print("nHeuristic", heuristic_type)
print("Nodes Visited/Expanded (V):", V)
print("Maximum Nodes Stored in Memory (N):", N)
print("Depth of Optimal
Solution
(d):", d)
if __name__ == "__main__":
main()
Current output results in a ton of unprecedented values for V, d, and N like 39,423

More Related Content

Similar to Fix this code so that it will run with proper answers, please dont u.pdf

Djangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicDjangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicNew Relic
 
밑바닥부터 시작하는 의료 AI
밑바닥부터 시작하는 의료 AI밑바닥부터 시작하는 의료 AI
밑바닥부터 시작하는 의료 AINAVER Engineering
 
Implemeting queue in python.pptx
Implemeting queue in python.pptxImplemeting queue in python.pptx
Implemeting queue in python.pptxKennyDon5
 
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
  goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf  goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdfANJALIENTERPRISES1
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
A Taste of Python - Devdays Toronto 2009
A Taste of Python - Devdays Toronto 2009A Taste of Python - Devdays Toronto 2009
A Taste of Python - Devdays Toronto 2009Jordan Baker
 
This assignment is about creating and using heaps. Implement a heap .docx
 This assignment is about creating and using heaps.  Implement a heap .docx This assignment is about creating and using heaps.  Implement a heap .docx
This assignment is about creating and using heaps. Implement a heap .docxKomlin1
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethodsdreampuf
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdfkesav24
 
Testing My Patience
Testing My PatienceTesting My Patience
Testing My PatienceAdam Lowry
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .pptsushil155005
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonTendayi Mawushe
 
Py Die R E A D M E
Py Die  R E A D M EPy Die  R E A D M E
Py Die R E A D M Ecfministries
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit TestDavid Xie
 

Similar to Fix this code so that it will run with proper answers, please dont u.pdf (20)

Djangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicDjangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New Relic
 
밑바닥부터 시작하는 의료 AI
밑바닥부터 시작하는 의료 AI밑바닥부터 시작하는 의료 AI
밑바닥부터 시작하는 의료 AI
 
ScalaBlitz
ScalaBlitzScalaBlitz
ScalaBlitz
 
Implemeting queue in python.pptx
Implemeting queue in python.pptxImplemeting queue in python.pptx
Implemeting queue in python.pptx
 
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
  goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf  goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
A Taste of Python - Devdays Toronto 2009
A Taste of Python - Devdays Toronto 2009A Taste of Python - Devdays Toronto 2009
A Taste of Python - Devdays Toronto 2009
 
Beyond Scala Lens
Beyond Scala LensBeyond Scala Lens
Beyond Scala Lens
 
This assignment is about creating and using heaps. Implement a heap .docx
 This assignment is about creating and using heaps.  Implement a heap .docx This assignment is about creating and using heaps.  Implement a heap .docx
This assignment is about creating and using heaps. Implement a heap .docx
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethods
 
Monadologie
MonadologieMonadologie
Monadologie
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
Testing My Patience
Testing My PatienceTesting My Patience
Testing My Patience
 
Unit test
Unit testUnit test
Unit test
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .ppt
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
 
Py Die R E A D M E
Py Die  R E A D M EPy Die  R E A D M E
Py Die R E A D M E
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
 

More from txkev

Critical Thinking 10-7 Dynamic Mobile AccessThere are so many o.pdf
Critical Thinking 10-7 Dynamic Mobile AccessThere are so many o.pdfCritical Thinking 10-7 Dynamic Mobile AccessThere are so many o.pdf
Critical Thinking 10-7 Dynamic Mobile AccessThere are so many o.pdftxkev
 
Crisis Response Instructions In Module 4, you learned that e.pdf
Crisis Response Instructions     In Module 4, you learned that e.pdfCrisis Response Instructions     In Module 4, you learned that e.pdf
Crisis Response Instructions In Module 4, you learned that e.pdftxkev
 
Create a Schedule D and schedule 1 for this information, and the cor.pdf
Create a Schedule D and schedule 1 for this information, and the cor.pdfCreate a Schedule D and schedule 1 for this information, and the cor.pdf
Create a Schedule D and schedule 1 for this information, and the cor.pdftxkev
 
Five areas that financial ratios concentrate on area) liquidity.pdf
Five areas that financial ratios concentrate on area) liquidity.pdfFive areas that financial ratios concentrate on area) liquidity.pdf
Five areas that financial ratios concentrate on area) liquidity.pdftxkev
 
double Skiplist code below#include iostream#include vector.pdf
double Skiplist code below#include iostream#include vector.pdfdouble Skiplist code below#include iostream#include vector.pdf
double Skiplist code below#include iostream#include vector.pdftxkev
 
Draw a class diagram for the following scenario The following are t.pdf
Draw a class diagram for the following scenario The following are t.pdfDraw a class diagram for the following scenario The following are t.pdf
Draw a class diagram for the following scenario The following are t.pdftxkev
 
Discuss the structure of intelligent agents in artificial intelligen.pdf
Discuss the structure of intelligent agents in artificial intelligen.pdfDiscuss the structure of intelligent agents in artificial intelligen.pdf
Discuss the structure of intelligent agents in artificial intelligen.pdftxkev
 
Consider the following game in which Sally can play T or B and John .pdf
Consider the following game in which Sally can play T or B and John .pdfConsider the following game in which Sally can play T or B and John .pdf
Consider the following game in which Sally can play T or B and John .pdftxkev
 
Computer NetworksConsider the following scenario involving a.pdf
Computer NetworksConsider the following scenario involving a.pdfComputer NetworksConsider the following scenario involving a.pdf
Computer NetworksConsider the following scenario involving a.pdftxkev
 
Character.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdfCharacter.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdftxkev
 
CNA STB 14 July 2022, STB said � visitor arrival to Singapore are ex.pdf
CNA STB 14 July 2022, STB said � visitor arrival to Singapore are ex.pdfCNA STB 14 July 2022, STB said � visitor arrival to Singapore are ex.pdf
CNA STB 14 July 2022, STB said � visitor arrival to Singapore are ex.pdftxkev
 
Directions# Caesar Cipher ImplementationYour task in this homew.pdf
Directions# Caesar Cipher ImplementationYour task in this homew.pdfDirections# Caesar Cipher ImplementationYour task in this homew.pdf
Directions# Caesar Cipher ImplementationYour task in this homew.pdftxkev
 
Describe the leadership of Captain Michael Davidson and second mate .pdf
Describe the leadership of Captain Michael Davidson and second mate .pdfDescribe the leadership of Captain Michael Davidson and second mate .pdf
Describe the leadership of Captain Michael Davidson and second mate .pdftxkev
 

More from txkev (13)

Critical Thinking 10-7 Dynamic Mobile AccessThere are so many o.pdf
Critical Thinking 10-7 Dynamic Mobile AccessThere are so many o.pdfCritical Thinking 10-7 Dynamic Mobile AccessThere are so many o.pdf
Critical Thinking 10-7 Dynamic Mobile AccessThere are so many o.pdf
 
Crisis Response Instructions In Module 4, you learned that e.pdf
Crisis Response Instructions     In Module 4, you learned that e.pdfCrisis Response Instructions     In Module 4, you learned that e.pdf
Crisis Response Instructions In Module 4, you learned that e.pdf
 
Create a Schedule D and schedule 1 for this information, and the cor.pdf
Create a Schedule D and schedule 1 for this information, and the cor.pdfCreate a Schedule D and schedule 1 for this information, and the cor.pdf
Create a Schedule D and schedule 1 for this information, and the cor.pdf
 
Five areas that financial ratios concentrate on area) liquidity.pdf
Five areas that financial ratios concentrate on area) liquidity.pdfFive areas that financial ratios concentrate on area) liquidity.pdf
Five areas that financial ratios concentrate on area) liquidity.pdf
 
double Skiplist code below#include iostream#include vector.pdf
double Skiplist code below#include iostream#include vector.pdfdouble Skiplist code below#include iostream#include vector.pdf
double Skiplist code below#include iostream#include vector.pdf
 
Draw a class diagram for the following scenario The following are t.pdf
Draw a class diagram for the following scenario The following are t.pdfDraw a class diagram for the following scenario The following are t.pdf
Draw a class diagram for the following scenario The following are t.pdf
 
Discuss the structure of intelligent agents in artificial intelligen.pdf
Discuss the structure of intelligent agents in artificial intelligen.pdfDiscuss the structure of intelligent agents in artificial intelligen.pdf
Discuss the structure of intelligent agents in artificial intelligen.pdf
 
Consider the following game in which Sally can play T or B and John .pdf
Consider the following game in which Sally can play T or B and John .pdfConsider the following game in which Sally can play T or B and John .pdf
Consider the following game in which Sally can play T or B and John .pdf
 
Computer NetworksConsider the following scenario involving a.pdf
Computer NetworksConsider the following scenario involving a.pdfComputer NetworksConsider the following scenario involving a.pdf
Computer NetworksConsider the following scenario involving a.pdf
 
Character.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdfCharacter.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdf
 
CNA STB 14 July 2022, STB said � visitor arrival to Singapore are ex.pdf
CNA STB 14 July 2022, STB said � visitor arrival to Singapore are ex.pdfCNA STB 14 July 2022, STB said � visitor arrival to Singapore are ex.pdf
CNA STB 14 July 2022, STB said � visitor arrival to Singapore are ex.pdf
 
Directions# Caesar Cipher ImplementationYour task in this homew.pdf
Directions# Caesar Cipher ImplementationYour task in this homew.pdfDirections# Caesar Cipher ImplementationYour task in this homew.pdf
Directions# Caesar Cipher ImplementationYour task in this homew.pdf
 
Describe the leadership of Captain Michael Davidson and second mate .pdf
Describe the leadership of Captain Michael Davidson and second mate .pdfDescribe the leadership of Captain Michael Davidson and second mate .pdf
Describe the leadership of Captain Michael Davidson and second mate .pdf
 

Recently uploaded

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

Fix this code so that it will run with proper answers, please dont u.pdf

  • 1. Fix this code so that it will run with proper answers, please dont use chatgpt and please test your code before submitting: 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 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
  • 2. 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) 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]:
  • 3. 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 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()
  • 4. 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])) 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()
  • 5. 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: f_val = manhattan(node.state.tiles, goal_state.tiles) + node.depth node.val=f_val frontier.push(node) return None, V, N, d def main(): global nodeid nodeid = 0 inputs = [] for line in sys.stdin: inputs += line.split() array = [ [int(inputs[0]), int(inputs[1]), int(inputs[2])], [int(inputs[3]), int(inputs[4]), int(inputs[5])], [int(inputs[6]), int(inputs[7]), int(inputs[8])] ] goal_state = State() goal_state.tiles = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] s = State() current = State() current.tiles = array s.tiles = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] print(array) printMan = manhattan(array, goal_state.tiles) print("Initial State:")
  • 6. print(s) print("Goal State:") print(goal_state) print("Manhattan Distance:", printMan) for heuristic_type in range(2): closedlist = Set() nodeid = 0 d, N, V,_ = astar(current, goal_state, closedlist, heuristic_type) print("nHeuristic", heuristic_type) print("Nodes Visited/Expanded (V):", V) print("Maximum Nodes Stored in Memory (N):", N) print("Depth of Optimal Solution (d):", d) if __name__ == "__main__": main() Current output results in a ton of unprecedented values for V, d, and N like 39,423