0% found this document useful (0 votes)
98 views3 pages

8 Puzzle Problem

This document describes the problem representation and search algorithm for solving sliding puzzle problems. It defines functions for printing puzzle states, checking if a state is the goal, determining the resulting state after a move, expanding a state to all possible next states, and performing a breadth-first search to solve the puzzle from the start to goal state. The code provides an example of representing a 3x3 sliding puzzle as a tuple, and performs a BFS search to solve it.

Uploaded by

hajra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views3 pages

8 Puzzle Problem

This document describes the problem representation and search algorithm for solving sliding puzzle problems. It defines functions for printing puzzle states, checking if a state is the goal, determining the resulting state after a move, expanding a state to all possible next states, and performing a breadth-first search to solve the puzzle from the start to goal state. The code provides an example of representing a 3x3 sliding puzzle as a tuple, and performs a BFS search to solve it.

Uploaded by

hajra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

# -*- coding: utf-8 -*-

"""
Created on Mon Oct 16 12:38:47 2017

@author: drkhan
"""

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 22 20:48:1 2017

@author: zia
"""

# This code demonstrates problem representation


# of the sliding puzzle example

# representing sliding puzzle states as a tuple


start = (1,2,3,4,0,5,6,7,8)
goal = (1,2,3,4,5,6,7,8,0)

# 0 1 2
# 3 4 5
# 6 7 8

#%%

# function to print states in three rows


# to represent a real puzzle state
def printstate(state):
print(state[0:3])
print(state[3:6])
print(state[6:9])

# test whether or not a state is the same as


# the goal state
def goaltest(state, goal):
if state == goal:
return True
else:
return False

# this functions implements the transition model


# given a state and action it returns the resultant
# state

def result(statein,action):

stateout = list(statein) # # make a local copy of statein

if action == 'Up':
idx = statein.index(0)
stateout[idx] = statein[idx-3]
stateout[idx-3] = 0

elif action == 'Down':


idx = statein.index(0)
stateout[idx] = statein[idx+3]
stateout[idx+3] = 0

elif action == 'Left':


idx = statein.index(0)
stateout[idx] = statein[idx-1]
stateout[idx-1] = 0

elif action == 'Right':


idx = statein.index(0)
stateout[idx] = statein[idx+1]
stateout[idx+1] = 0

return tuple(stateout)

# this function returns a list of states that


# can be reached from the given state.
def expand(state):
# an empty list to store possible actions from state
actions = []

# an empty list to store states reachable after applying the actions


successors = []

# find the index of the blank tile i.e. 0 in the puzzle


blank = state.index(0)

if blank in {3,4,5,6,7,8}: # action up is possible if blank tile is on one


of these locations
actions.append('Up') # append up in actions list
if blank in {0,1,2,3,4,5}: # locations from which down action is possible
actions.append('Down')
if blank in {1,2,4,5,7,8}:
actions.append('Left')
if blank in {0,1,3,4,6,7}:
actions.append('Right')
#print(actions)
for action in actions: # iterate over actions
#print(action)
# apply all possible actions one by one and store the resultant
# states in succesors list
successors.append(result(state,action))

return successors

# searching alogrithm

# this part of the code demonstrates breadth first search


# using the previouly coded problem formulation

#def search(start,goal):

if goaltest(start,goal):
# return
print('goal reached')
else:
frontier = []
explored = []
frontier.append(start)
while frontier:
# if len(frontier) >=300:
# break
current = frontier.pop(0)
print('current:',current)
print('frontier',len(frontier))
print('explored',len(explored))
if goaltest(current,goal):
print('gaoal reached')
break
explored.append(current)
children = expand(current)
for child in children:
if child not in frontier and child not in explored:
frontier.append(child)

You might also like