8 Puzzle Problem

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy