AI Outputs (4,5,6,7)
AI Outputs (4,5,6,7)
class PuzzleState:
def __init__(self, board, parent=None, action=None):
self.board = board
self.parent = parent
self.action = action
self.blank_position = self.find_blank_position()
def __hash__(self):
return hash(str(self.board))
def find_blank_position(self):
for i in range(3):
for j in range(3):
if self.board[i][j] == 0:
return (i, j)
def get_possible_moves(self):
moves = []
x, y = self.blank_position
if x > 0:
moves.append('up')
if x < 2:
moves.append('down')
if y > 0:
moves.append('left')
if y < 2:
moves.append('right')
return moves
def move(self, direction):
x, y = self.blank_position
if direction == 'up':
new_x, new_y = x - 1, y
elif direction == 'down':
new_x, new_y = x + 1, y
elif direction == 'left':
new_x, new_y = x, y - 1
elif direction == 'right':
new_x, new_y = x, y + 1
else:
return None
if current_state.is_goal_state(goal_state):
return get_solution_path(current_state)
visited.add(current_state)
possible_moves = current_state.get_possible_moves()
return None
def get_solution_path(final_state):
path = []
current_state = final_state
while current_state.parent is not None:
path.insert(0, current_state.action)
current_state = current_state.parent
return path
def print_puzzle_board(board):
for row in board:
print(" ".join(map(str, row)))
if __name__ == "__main__":
initial_board = [
[1, 2, 3],
[0, 4, 6],
[7, 5, 8]
]
goal_board = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 0]
]
initial_state = PuzzleState(initial_board)
goal_state = PuzzleState(goal_board)
if solution:
print("Solution steps:")
print_puzzle_board(initial_state.board) # Print the initial state
for step in solution:
print(f"Move {step}:")
initial_state = initial_state.move(step)
print_puzzle_board(initial_state.board)
print("Number of steps:", len(solution))
else:
print("No solution found.")
Output:
Solution steps:
123
046
758
Move right:
123
406
758
Move down:
123
456
708
Move right:
123
456
780
Number of steps: 3
Experiment No : 05
import heapq
# Perform A* Search
while open_set:
current_state = heapq.heappop(open_set)
closed_set.add(tuple(map(tuple, current_state.state)))
# No path found
return None
Intermediate State:
[1, 2, 3]
[4, 0, 5]
[7, 8, 6]
Intermediate State:
[1, 2, 3]
[4, 5, 0]
[7, 8, 6]
Goal State:
[1, 2, 3]
[4, 5, 6]
[7, 8, 0]
Solution found!
Experiment NO: 06
# Define the game state and the initial state
initial_state = [1, 4, 2, 6, -3, -5, 0, 7]
# Define the utility function (a simple sum of the elements for the MAX player)
def utility(state, player):
if player == 'MAX':
return sum(state)
elif player == 'MIN':
return -sum(state)
# Define the initial state and the goal state for the 8-puzzle
initial_state = [2, 8, 3, 1, 6, 4, 7, 0, 5]
goal_state = [1, 2, 3, 8, 0, 4, 7, 6, 5]
def fitness(state):
# Calculate the fitness of a state (number of misplaced tiles)
return sum(1 for i in range(9) if state[i] != goal_state[i])
def mutate(state):
# Apply mutation by swapping two random tiles
i, j = random.sample(range(9), 2)
state[i], state[j] = state[j], state[i]
def genetic_algorithm():
# Initialize the population with random states
population = [random.sample(range(9), 9) for _ in range(population_size)]
if __name__ == "__main__":
genetic_algorithm()
Output:
Generation 0: Best Solution - [1, 7, 0, 8, 2, 4, 3, 6, 5], Fitness - 4
Generation 1: Best Solution - [1, 7, 0, 8, 2, 4, 3, 6, 5], Fitness - 4
Generation 2: Best Solution - [1, 2, 2, 8, 0, 4, 2, 3, 5], Fitness - 3
Generation 3: Best Solution - [1, 2, 1, 8, 0, 4, 3, 6, 5], Fitness - 2
Generation 4: Best Solution - [1, 2, 3, 8, 0, 4, 2, 6, 5], Fitness - 1
Generation 5: Best Solution - [1, 2, 3, 8, 0, 4, 2, 6, 5], Fitness - 1
Generation 6: Best Solution - [1, 2, 3, 8, 0, 4, 2, 6, 5], Fitness - 1
Generation 7: Best Solution - [1, 2, 3, 8, 0, 4, 7, 6, 5], Fitness - 0
Solution found!
Solution: [1, 2, 3, 8, 0, 4, 7, 6, 5]
>