SlideShare a Scribd company logo
1 of 7
Download to read offline
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
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()

More Related Content

Similar to Will upvote Please fix the following code and post your inputs and o.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 SchlawackFwdays
 
DjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicDjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicGraham Dumpleton
 
Djangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicDjangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicNew Relic
 
Pybelsberg — Constraint-based Programming in Python
Pybelsberg — Constraint-based Programming in PythonPybelsberg — Constraint-based Programming in Python
Pybelsberg — Constraint-based Programming in PythonChristoph Matthies
 
Implemeting queue in python.pptx
Implemeting queue in python.pptxImplemeting queue in python.pptx
Implemeting queue in python.pptxKennyDon5
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .pptsushil155005
 
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
 
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
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethodsdreampuf
 
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
 
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
 
Python Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit TestingPython Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit TestingPython Ireland
 
Python decorators (中文)
Python decorators (中文)Python decorators (中文)
Python decorators (中文)Yiwei Chen
 
The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196Mahmoud Samir Fayed
 

Similar to Will upvote Please fix the following code and post your inputs and o.pdf (20)

"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
 
DjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicDjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New Relic
 
Djangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New RelicDjangocon11: Monkeying around at New Relic
Djangocon11: Monkeying around at New Relic
 
Pybelsberg — Constraint-based Programming in Python
Pybelsberg — Constraint-based Programming in PythonPybelsberg — Constraint-based Programming in Python
Pybelsberg — Constraint-based Programming in Python
 
Implemeting queue in python.pptx
Implemeting queue in python.pptxImplemeting queue in python.pptx
Implemeting queue in python.pptx
 
ScalaBlitz
ScalaBlitzScalaBlitz
ScalaBlitz
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .ppt
 
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
 
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
 
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
 
Monadologie
MonadologieMonadologie
Monadologie
 
Corona sdk
Corona sdkCorona sdk
Corona sdk
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethods
 
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
 
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
 
Python Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit TestingPython Ireland Nov 2010 Talk: Unit Testing
Python Ireland Nov 2010 Talk: Unit Testing
 
Python decorators (中文)
Python decorators (中文)Python decorators (中文)
Python decorators (中文)
 
Python speleology
Python speleologyPython speleology
Python speleology
 
The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196
 

More from info335653

You�ve been hired by the local government to determine which employe.pdf
You�ve been hired by the local government to determine which employe.pdfYou�ve been hired by the local government to determine which employe.pdf
You�ve been hired by the local government to determine which employe.pdfinfo335653
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfinfo335653
 
You are the chief data scientist of the marketing company �VisualZ.�.pdf
You are the chief data scientist of the marketing company �VisualZ.�.pdfYou are the chief data scientist of the marketing company �VisualZ.�.pdf
You are the chief data scientist of the marketing company �VisualZ.�.pdfinfo335653
 
Write a program that displays a table of the Celsius temperatures 0 .pdf
Write a program that displays a table of the Celsius temperatures 0 .pdfWrite a program that displays a table of the Celsius temperatures 0 .pdf
Write a program that displays a table of the Celsius temperatures 0 .pdfinfo335653
 
Why is the release of GDP statistics less interesting to investors t.pdf
Why is the release of GDP statistics less interesting to investors t.pdfWhy is the release of GDP statistics less interesting to investors t.pdf
Why is the release of GDP statistics less interesting to investors t.pdfinfo335653
 
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdfWinds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdfinfo335653
 
With reference to the case study, define social media marketing and cr.pdf
With reference to the case study, define social media marketing and cr.pdfWith reference to the case study, define social media marketing and cr.pdf
With reference to the case study, define social media marketing and cr.pdfinfo335653
 
Which of the following statements is CORRECT a. The four most importa.pdf
Which of the following statements is CORRECT a. The four most importa.pdfWhich of the following statements is CORRECT a. The four most importa.pdf
Which of the following statements is CORRECT a. The four most importa.pdfinfo335653
 
We will discuss 1. IT systems� complexity IT systems have become unman.pdf
We will discuss 1. IT systems� complexity IT systems have become unman.pdfWe will discuss 1. IT systems� complexity IT systems have become unman.pdf
We will discuss 1. IT systems� complexity IT systems have become unman.pdfinfo335653
 
usingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdfusingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdfinfo335653
 
Using MISP VM ova file, import it on VirtualBox and respond to the q.pdf
Using MISP VM ova file, import it on VirtualBox and respond to the q.pdfUsing MISP VM ova file, import it on VirtualBox and respond to the q.pdf
Using MISP VM ova file, import it on VirtualBox and respond to the q.pdfinfo335653
 
True or false Public policy is not an important component of organi.pdf
True or false Public policy is not an important component of organi.pdfTrue or false Public policy is not an important component of organi.pdf
True or false Public policy is not an important component of organi.pdfinfo335653
 

More from info335653 (12)

You�ve been hired by the local government to determine which employe.pdf
You�ve been hired by the local government to determine which employe.pdfYou�ve been hired by the local government to determine which employe.pdf
You�ve been hired by the local government to determine which employe.pdf
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
 
You are the chief data scientist of the marketing company �VisualZ.�.pdf
You are the chief data scientist of the marketing company �VisualZ.�.pdfYou are the chief data scientist of the marketing company �VisualZ.�.pdf
You are the chief data scientist of the marketing company �VisualZ.�.pdf
 
Write a program that displays a table of the Celsius temperatures 0 .pdf
Write a program that displays a table of the Celsius temperatures 0 .pdfWrite a program that displays a table of the Celsius temperatures 0 .pdf
Write a program that displays a table of the Celsius temperatures 0 .pdf
 
Why is the release of GDP statistics less interesting to investors t.pdf
Why is the release of GDP statistics less interesting to investors t.pdfWhy is the release of GDP statistics less interesting to investors t.pdf
Why is the release of GDP statistics less interesting to investors t.pdf
 
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdfWinds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
 
With reference to the case study, define social media marketing and cr.pdf
With reference to the case study, define social media marketing and cr.pdfWith reference to the case study, define social media marketing and cr.pdf
With reference to the case study, define social media marketing and cr.pdf
 
Which of the following statements is CORRECT a. The four most importa.pdf
Which of the following statements is CORRECT a. The four most importa.pdfWhich of the following statements is CORRECT a. The four most importa.pdf
Which of the following statements is CORRECT a. The four most importa.pdf
 
We will discuss 1. IT systems� complexity IT systems have become unman.pdf
We will discuss 1. IT systems� complexity IT systems have become unman.pdfWe will discuss 1. IT systems� complexity IT systems have become unman.pdf
We will discuss 1. IT systems� complexity IT systems have become unman.pdf
 
usingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdfusingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdf
 
Using MISP VM ova file, import it on VirtualBox and respond to the q.pdf
Using MISP VM ova file, import it on VirtualBox and respond to the q.pdfUsing MISP VM ova file, import it on VirtualBox and respond to the q.pdf
Using MISP VM ova file, import it on VirtualBox and respond to the q.pdf
 
True or false Public policy is not an important component of organi.pdf
True or false Public policy is not an important component of organi.pdfTrue or false Public policy is not an important component of organi.pdf
True or false Public policy is not an important component of organi.pdf
 

Recently uploaded

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
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
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
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
 
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
 
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
 
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
 
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
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
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
 
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
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
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
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).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...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
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
 
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Ă...
 
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
 
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
 
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
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
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...
 
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...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
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
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

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:
  • 6. 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
  • 7. 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()