-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmazesolver.py
104 lines (96 loc) · 4.14 KB
/
mazesolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Install necessary libraries
!pip install matplotlib deap
import random
import numpy as np
import matplotlib.pyplot as plt
from deap import base, creator, tools, algorithms
# Define the maze
maze = [
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]
]
# Start and end points
start, end = (0, 0), (len(maze)-1, len(maze[0])-1)
# Genetic Algorithm setup
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
toolbox.register("attr_direction", random.choice, ['U', 'D', 'L', 'R'])
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_direction, n=100)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evaluate(individual):
x, y = start
for move in individual:
# Move up, down, left, or right while checking boundaries
if move == 'U': y = max(0, y - 1)
elif move == 'D': y = min(len(maze) - 1, y + 1)
elif move == 'L': x = max(0, x - 1)
elif move == 'R': x = min(len(maze[0]) - 1, x + 1)
# Check if the current position is the end goal
if (x, y) == end:
return (0,) # Perfect score since we reached the end
# Check if the current position is a wall
if maze[y][x] == 1:
break
# Return the Manhattan distance to the end point as the score
return (abs(end[0] - x) + abs(end[1] - y),)
def custom_mutate(individual, indpb=0.2):
directions = ['U', 'D', 'L', 'R']
for i in range(len(individual)):
if random.random() < indpb:
# Exclude the current direction to ensure mutation changes the gene
possible_directions = [d for d in directions if d != individual[i]]
individual[i] = random.choice(possible_directions)
return individual,
toolbox.register("evaluate", evaluate)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("mate", tools.cxUniform, indpb=0.5)
toolbox.register("mutate", custom_mutate, indpb=0.2)
# Function to visualize the maze and path
def plot_path(individual):
x, y = start
plt.plot(x, y, "go") # start point
for move in individual:
# Attempt the move
next_x, next_y = x, y
if move == 'U': next_y = max(0, y - 1)
elif move == 'D': next_y = min(len(maze) - 1, y + 1)
elif move == 'L': next_x = max(0, x - 1)
elif move == 'R': next_x = min(len(maze[0]) - 1, x + 1)
# Check for wall collision before plotting the move
if maze[next_y][next_x] == 1 or (next_x, next_y) == end: break
# No collision, so make the move and plot it
x, y = next_x, next_y
# plt.plot(x, y, "bo")
plt.plot(end[0], end[1], "ro") # end point
plt.imshow(maze, cmap="binary")
plt.show()
# Run the genetic algorithm
def run_ga(generations=2000, pop_size=50):
pop = toolbox.population(n=pop_size)
best_individuals = []
for gen in range(generations):
offspring = algorithms.varAnd(pop, toolbox, cxpb=0.5, mutpb=0.2)
fits = toolbox.map(toolbox.evaluate, offspring)
for fit, ind in zip(fits, offspring):
ind.fitness.values = fit
pop = toolbox.select(offspring, k=len(pop))
top_individual = tools.selBest(pop, k=1)[0]
best_individuals.append(top_individual)
if gen in [2, 10, 50, 100, 500] or gen == generations - 1:
print(f"Generation {gen}:")
plot_path(top_individual)
run_ga()