Please write a python program for solving 8 puzzle problem using Iterative-Deepening-Search. please write in a manner that is easy to copy.
#Start
#8puzzle.py
import argparse
import timeit
class State:
depth = None
def __init__(self, state, parent, move, depth, cost, key):
self.state = state
self.parent = parent
self.move = move
self.depth = depth
self.cost = cost
self.key = key
if self.state:
self.map = ''.join(str(e) for e in self.state)
def __eq__(self, other):
return self.map == other.map
def __lt__(self, other):
return self.map < other.map
goal_state = [1, 2, 3, 4, 5, 6, 7, 8, 0]
goal_node = State
initial_state = list()
board_len = 0
board_side = 0
nodes_expanded = 0
max_search_depth = 0
max_frontier_size = 0
moves = list()
costs = set()
def dfs(start_state):
global max_frontier_size, goal_node, max_search_depth
explored, queue = set(), list([State(start_state, None, None, 0, 0, 0)])
while queue:
node = queue.pop()
explored.add(node.map)
if node.state == goal_state:
goal_node = node
return queue
neighbors = reversed(expand(node))
for neighbor in neighbors:
if neighbor.map not in explored:
queue.append(neighbor)
explored.add(neighbor.map)
if neighbor.depth > max_search_depth:
max_search_depth += 1
if len(queue) > max_frontier_size:
max_frontier_size = len(queue)
def expand(node):
global nodes_expanded
nodes_expanded += 1
neighbors = list()
neighbors.append(State(move(node.state, 1), node, 1, node.depth + 1, node.cost + 1, 0))
neighbors.append(State(move(node.state, 2), node, 2, node.depth + 1, node.cost + 1, 0))
neighbors.append(State(move(node.state, 3), node, 3, node.depth + 1, node.cost + 1, 0))
neighbors.append(State(move(node.state, 4), node, 4, node.depth + 1, node.cost + 1, 0))
nodes = [neighbor for neighbor in neighbors if neighbor.state]
return nodes
def move(state, position):
new_state = state[:]
index = new_state.index(0)
if position == 1: # Up
if index not in range(0, board_side):
temp = new_state[index - board_side]
new_state[index - board_side] = new_state[index]
new_state[index] = temp
return new_state
else:
return None
if position == 2: # Down
if index not in range(board_len - board_side, board_len):
temp = new_state[index + board_side]
new_state[index + board_side] = new_state[index]
new_state[index] = temp
return new_state
else:
return None
if position == 3: # Left
if index not in range(0, board_len, board_side):
temp = new_state[index - 1]
new_state[index - 1] = new_state[index]
new_state[index] = temp
return new_state
else:
return None
if position == 4: # Right
if index not in range(board_side - 1, board_len, board_side):
temp = new_state[index + 1]
new_state[index + 1] = new_state[index]
new_state[index] = temp
return new_state
else:
return None
def backtrace():
current_node = goal_node
while initial_state != current_node.state:
if current_node.move == 1:
movement = 'Up'
elif current_node.move == 2:
movement = 'Down'
elif current_node.move == 3:
movement = 'Left'
else:
movement = 'Right'
moves.insert(0, movement)
current_node = current_node.parent
return moves
def export(frontier, time):
global moves
moves = backtrace()
file = open('output.txt', 'w')
file.write("path_to_goal: " + str(moves))
file.write("\ncost_of_path: " + str(len(moves)))
file.write("\nnodes_expanded: " + str(nodes_expanded))
file.write("\nfringe_size: " + str(len(frontier)))
file.write("\nmax_fringe_size: " + str(max_frontier_size))
file.write("\nsearch_depth: " + str(goal_node.depth))
file.write("\nmax_search_depth: " + str(max_search_depth))
file.write("\nrunning_time: " + format(time, '.8f'))
file.close()
def read(configuration):
global board_len, board_side
data = configuration.split(",")
for element in data:
initial_state.append(int(element))
board_len = len(initial_state)
board_side = int(board_len ** 0.5)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('board')
args = parser.parse_args()
read(args.board)
start = timeit.default_timer()
frontier = dfs(initial_state)
stop = timeit.default_timer()
export(frontier, stop - start)
if __name__ == '__main__':
main()
#End
Please write a python program for solving 8 puzzle problem using Iterative-Deepening-Search. please write in a...
Exercise 1: (Greedy Search) Solve the following 8-puzzle problem using Greedy search algorithm as search strategy and h10 as heuristic hl(n): the number of misplaced tiles for the current node n 7 6
Please help with this python turtle problem using the functions provided. Python Turtle Write a program using the turtle module to draw the face of a clock that looks like the image below. You will need to use the turtle module for this exercise as well as the bgcolor), shape(), color), penup), right(), forward(), pendown(), and stamp() functions. The code below can be removed or changed as needed. import turtle t turtle.Turtle0 t.forward(75)
Write a program to solve the word puzzle problem. Explain your code.
Write a Python program that outputs all possible strings formed by using the characters c , a , t , d , o , and g exactly once. Please show screenshots and copy the code.
1) Write a Python program that prompts the user to enter the current month name and prints the season for that month. Hint: If the user enters March, the output should be "Spring"; if the user enters June, the output should be "Summer". 2 )Write a Python program using the recursive/loop structure to print out an equilateral triangle below (double spacing and one space between any two adjacent asterisks in the same row). * * * * *...
Selective Copy. Using Python program, write a program that walks through a folder tree and searches for files with a certain file extension (such as .pdf or .jpg). Copy these files from whatever location they are in to a new folder. Submit the code and a screenshot of the program running in Linux
In python. Thanks P5. Write a python program to solve the following polynomials. (Solving a polynomial means finding the roots of the polynomial) x2 3x 2 0 3x2 x 6-0 x3- 2x2 -x-2 0 Print the roots as x1-?, x2-?, x3-? Are the roots real or imaginary?
Using python, write code for problem. Please also put screenshot of your code. The following program will perform properly if the user enters 0 in response to the request for input. However, the program will crash if the user responds with "eight". Rewrite the program using a try/except statement so that it will handle both types of responses. See Fig. 6.1. while True: n = int (input ("Enter a nonzero integer: ")) if n! = 0: reciprocal = 1/n print...
The Knapsack Problem in Python Not using the exhaustive search method or the Dynamic Programming Method, find another method that accomplishes the task of the knapsack problem in python. PLEASE DON'T USE THE EXHAUSTIVE SEARCH METHOD OR THE DYNAMIC PROGRAMMING METHOD, I DON'T NEED THOSE. THANK YOU.