0% found this document useful (0 votes)
124 views4 pages

AI DFS for 8-Puzzle Problem

The document describes implementing depth-first search (DFS) to solve the 8-puzzle problem. It defines classes for the puzzle state and problem, with methods to get successor states by moving the empty tile up, down, left or right. DFS is implemented to search the state space by recursively exploring successors until the goal is found or no more remain. The initial and goal states for an example 8-puzzle are given, a PuzzleProblem object is created, and DFS is used to solve it, printing the sequence of moves if a solution is found.

Uploaded by

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

AI DFS for 8-Puzzle Problem

The document describes implementing depth-first search (DFS) to solve the 8-puzzle problem. It defines classes for the puzzle state and problem, with methods to get successor states by moving the empty tile up, down, left or right. DFS is implemented to search the state space by recursively exploring successors until the goal is found or no more remain. The initial and goal states for an example 8-puzzle are given, a PuzzleProblem object is created, and DFS is used to solve it, printing the sequence of moves if a solution is found.

Uploaded by

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

PRACTICAL - 3

Aim :
Write a program to implement DFS (for 8 puzzle problem or Water jug Problem or any AI search
Problem).

8-Puzzle Problem:-

Code:
# Define the 8-puzzle problem state
class PuzzleState:
def __init__(self, state, parent, action, depth):
self.state = state
self.parent = parent
self.action = action
self.depth = depth

def __str__(self):
return str(self.state)

def __eq__(self, other):


return self.state == other.state

def __hash__(self):
return hash(str(self.state))

# Define the 8-puzzle problem


class PuzzleProblem:
def __init__(self, initial_state, goal_state):
self.initial_state = initial_state
self.goal_state = goal_state

def get_successors(self, state):


201310132060 Ms. Ritika Lohiya
successors = []
blank_pos = state.state.index(0)
if blank_pos not in [0, 1, 2]: # Move up
new_state = state.state[:]
new_state[blank_pos], new_state[blank_pos-3] = new_state[blank_pos-3],
new_state[blank_pos]
successors.append(PuzzleState(new_state, state, "Up", state.depth+1))
if blank_pos not in [6, 7, 8]: # Move down
new_state = state.state[:]
new_state[blank_pos], new_state[blank_pos+3] = new_state[blank_pos+3],
new_state[blank_pos]
successors.append(PuzzleState(new_state, state, "Down", state.depth+1))
if blank_pos not in [0, 3, 6]: # Move left
new_state = state.state[:]
new_state[blank_pos], new_state[blank_pos-1] = new_state[blank_pos-1],
new_state[blank_pos]
successors.append(PuzzleState(new_state, state, "Left", state.depth+1))
if blank_pos not in [2, 5, 8]: # Move right
new_state = state.state[:]
new_state[blank_pos], new_state[blank_pos+1] = new_state[blank_pos+1],
new_state[blank_pos]
successors.append(PuzzleState(new_state, state, "Right", state.depth+1))
return successors

def goal_test(self, state):


return state.state == self.goal_state

# Define the depth-first search algorithm


def dfs(problem):
explored = set()
stack = [problem.initial_state]
while stack:
state = stack.pop()
if problem.goal_test(state):
201310132060 Ms. Ritika Lohiya
return state
explored.add(state)
successors = problem.get_successors(state)
for successor in successors:
if successor not in explored and successor not in stack:
stack.append(successor)
return None

# Define the initial and goal states for the 8-puzzle problem
initial_state = [1, 0, 2,
7, 8, 3,
6, 5, 4]

goal_state = [1, 2, 3,
8, 0, 4,
7, 6, 5]

# Create a new PuzzleProblem object and solve it using DFS


problem = PuzzleProblem(PuzzleState(initial_state, None, None, 0), goal_state)
solution = dfs(problem)

# Print the solution


if solution is None:
print("No solution found.")
else:
path = []
while solution.parent is not None:
path.append(solution.action)
solution = solution.parent
path.reverse()
print("Moves:", path)

Output:-

201310132060 Ms. Ritika Lohiya


201310132060 Ms. Ritika Lohiya

You might also like