0% found this document useful (0 votes)
12 views12 pages

Iai 16010422097 Exp6

Uploaded by

AARUSH SABOO
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)
12 views12 pages

Iai 16010422097 Exp6

Uploaded by

AARUSH SABOO
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/ 12

KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

Batch: A4 Experiment Number: 6

Roll Number: 16010422097

Aim of the Experiment: Prolog program on 8-Puzzle

Program/ Steps:

import heapq
import itertools

class PuzzleNode:
def __init__(self, state, cost, heuristic, parent=None,
action=None):
self.state = state
self.cost = cost
self.heuristic = heuristic
self.parent = parent
self.action = action

def __lt__(self, other):


return (self.cost + self.heuristic) < (other.cost +
other.heuristic)

def manhattan_distance(state, goal_state):


distance = 0
for num in range(0, 10):
if num in state:
x1, y1 = divmod(state.index(num), 3)
x2, y2 = divmod(goal_state.index(num), 3)
distance += abs(x1 - x2) + abs(y1 - y2)
return distance

def get_legal_moves(state):
empty_index = state.index(0)
x, y = divmod(empty_index, 3)
moves = []
if x > 0:
moves.append(empty_index - 3) # Up
if x < 2:
moves.append(empty_index + 3) # Down
if y > 0:
moves.append(empty_index - 1) # Left
if y < 2:

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

moves.append(empty_index + 1) # Right
return moves

def get_next_states(state):
legal_moves = get_legal_moves(state)
next_states = []
for move in legal_moves:
next_state = list(state)
empty_index = state.index(0)
next_state[empty_index] = state[move]
next_state[move] = 0
next_states.append(next_state)
return next_states

def solve_puzzle(initial_state, goal_state):


open_list = []
closed_list = set()
initial_node = PuzzleNode(initial_state, 0,
manhattan_distance(initial_state, goal_state))
heapq.heappush(open_list, initial_node)

while open_list:
current_node = heapq.heappop(open_list)
current_state = current_node.state

if current_state == goal_state:
# Reconstruct the path
path = []
while current_node:
path.append(current_node.state)
current_node = current_node.parent
path.reverse()
return path[1:], len(path) - 1 # Exclude initial
state and count number of moves

if tuple(current_state) in closed_list:
continue

closed_list.add(tuple(current_state))

for next_state in get_next_states(current_state):


if tuple(next_state) not in closed_list:
next_node = PuzzleNode(next_state,
current_node.cost + 1,
manhattan_distance(next_s
tate, goal_state), current_node)
heapq.heappush(open_list, next_node)

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

return "impossible"

# Example usage:
initial_state = [5, 0, 8, 2, 1, 6, 7, 4, 3] # Example initial
state
goal_state = [1, 2, 3, 4, 5, 6, 7, 8, 0] # Example goal state

result = solve_puzzle(initial_state, goal_state)


if result == "impossible":
print("The puzzle is unsolvable.")
else:
path, cost = result
print("Solution found in", cost, "moves.")
print("Path:")
for state in path:
print(state[:3])
print(state[3:6])
print(state[6:])
print()

Output/Result:

Solution found in 22 moves.

Path:

[5, 0, 8]

[2, 1, 6]

[7, 4, 3]

[5, 1, 8]

[2, 0, 6]

[7, 4, 3]

[5, 1, 8]

[2, 4, 6]

[7, 0, 3]

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

[5, 1, 8]

[2, 4, 6]

[7, 3, 0]

[5, 1, 8]

[2, 4, 0]

[7, 3, 6]

[5, 1, 0]

[2, 4, 8]

[7, 3, 6]

[5, 0, 1]

[2, 4, 8]

[7, 3, 6]

[0, 5, 1]

[2, 4, 8]

[7, 3, 6]

[2, 5, 1]

[0, 4, 8]

[7, 3, 6]

[2, 5, 1]

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

[4, 0, 8]

[7, 3, 6]

[2, 5, 1]

[4, 3, 8]

[7, 0, 6]

[2, 5, 1]

[4, 3, 8]

[7, 6, 0]

[2, 5, 1]

[4, 3, 0]

[7, 6, 8]

[2, 5, 1]

[4, 0, 3]

[7, 6, 8]

[2, 0, 1]

[4, 5, 3]

[7, 6, 8]

[0, 2, 1]

[4, 5, 3]

[7, 6, 8]

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

[4, 2, 1]

[0, 5, 3]

[7, 6, 8]

[4, 2, 1]

[7, 5, 3]

[0, 6, 8]

[4, 2, 1]

[7, 5, 3]

[6, 0, 8]

[4, 2, 1]

[7, 0, 3]

[6, 5, 8]

[4, 0, 1]

[7, 2, 3]

[6, 5, 8]

[0, 4, 1]

[7, 2, 3]

[6, 5, 8]

[1, 4, 0]

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

[7, 2, 3]

[6, 5, 8]

[1, 4, 3]

[7, 2, 0]

[6, 5, 8]

[1, 4, 3]

[7, 0, 2]

[6, 5, 8]

[1, 0, 3]

[7, 4, 2]

[6, 5, 8]

[1, 3, 0]

[7, 4, 2]

[6, 5, 8]

[1, 3, 2]

[7, 4, 0]

[6, 5, 8]

[1, 3, 2]

[7, 4, 8]

[6, 5, 0]

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

[1, 3, 2]

[7, 4, 8]

[6, 0, 5]

[1, 3, 2]

[7, 0, 8]

[6, 4, 5]

[1, 0, 2]

[7, 3, 8]

[6, 4, 5]

[1, 2, 0]

[7, 3, 8]

[6, 4, 5]

[1, 2, 8]

[7, 3, 0]

[6, 4, 5]

[1, 2, 8]

[7, 3, 5]

[6, 4, 0]

[1, 2, 8]

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

[7, 3, 5]

[6, 0, 4]

[1, 2, 8]

[7, 0, 5]

[6, 3, 4]

[1, 0, 8]

[7, 2, 5]

[6, 3, 4]

[1, 8, 0]

[7, 2, 5]

[6, 3, 4]

[1, 8, 5]

[7, 2, 0]

[6, 3, 4]

[1, 8, 5]

[7, 0, 2]

[6, 3, 4]

[1, 0, 5]

[7, 8, 2]

[6, 3, 4]

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

[0, 1, 5]

[7, 8, 2]

[6, 3, 4]

[7, 1, 5]

[0, 8, 2]

[6, 3, 4]

[7, 1, 5]

[6, 8, 2]

[0, 3, 4]

[7, 1, 5]

[6, 8, 2]

[3, 0, 4]

[7, 1, 5]

[6, 0, 2]

[3, 8, 4]

[7, 1, 5]

[6, 2, 0]

[3, 8, 4]

[7, 1, 0]

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

[6, 2, 5]

[3, 8, 4]

[7, 0, 1]

[6, 2, 5]

[3, 8, 4]

[7, 2, 1]

[6, 0, 5]

[3, 8, 4]

[7, 2, 1]

[6, 8, 5]

[3, 0, 4]

[7, 2, 1]

[6, 8, 5]

[0, 3, 4]

[7, 2, 1]

[0, 8, 5]

[6, 3, 4]

[0, 2, 1]

[7, 8, 5]

[6, 3, 4]

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/IT/SY/SEM IV/HO-IAI/2023-24

[2, 0, 1]

[7, 8, 5]

[6, 3, 4

Outcomes: Ability to formally state the problem and develop the appropriate proof for given a
logical deduction problem.

Conclusion (based on the Results and outcomes achieved):

The aim of writing a Prolog program for the 8-Puzzle problem is to demonstrate the application
of logic programming in solving a classic problem in artificial intelligence and puzzle-solving.
By implementing the program, we can explore how Prolog's logical inference engine can
efficiently search through the puzzle state space to find a solution. Through this exercise, we
gain insights into the use of logic-based approaches for problem-solving and expand our
understanding of both Prolog programming and the 8-Puzzle problem itself.

(A Constituent College of Somaiya Vidyavihar University)

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