-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
209 lines (188 loc) · 6.41 KB
/
Copy pathmain.py
File metadata and controls
209 lines (188 loc) · 6.41 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from src.algorithms.base_search import BaseSearch
from src.core.game_state import GameState
from src.core.algorithm_manager import AlgorithmManager
from src.environment.maze_generator import MazeGenerator
from src.ui.button_manager import ButtonManager
from src.ui.game_window import GameWindow
from src.utilities.constants import (
SCREEN_WIDTH,
SCREEN_HEIGHT,
BUTTON_X,
BUTTON_Y,
BUTTON_WIDTH,
BUTTON_HEIGHT,
BUTTON_GAP,
MAZE_WIDTH,
MAZE_HEIGHT,
)
import src.environment.mazeSetup as mazeSetup
from src.core.frontier_exporter import FrontierExporter # Import FrontierExporter
from src.core.metrics_comparison import showDifferences_ExecutionTime
from src.utilities.constants import *
# Import Algorithm Functions
from src.algorithms.uninformed import bfs, dfs, ucs, ids
from src.algorithms.informed import greedy_bfs, astar
from src.algorithms.local import hill_climbing, simulated_annealing
from src.algorithms.genetic_algorithm import genetic_algorithm
from src.algorithms.q_learning_algorithm import q_learning
from src.core.metrics import Metrics
import threading
import numpy as np
def main():
# Initialize core components
maze_generator = MazeGenerator(MAZE_WIDTH, MAZE_HEIGHT)
game_state = GameState()
frontier_exporter = FrontierExporter() # Create FrontierExporter instance
algorithm_manager = AlgorithmManager(
game_state, frontier_exporter
) # Use FrontierExporter
# Initialize UI components
button_manager = ButtonManager(
BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT, BUTTON_GAP
)
game_window = GameWindow(
SCREEN_WIDTH, SCREEN_HEIGHT, button_manager, game_state, mazeSetup
)
# Function to solve maze with the algo and set ui and gamestate values
def solve_and_update(algorithm_name, algorithm_func, *args):
game_window.update_title(algorithm_name)
game_state.reset_path()
if algorithm_name == "IDS":
algorithm_manager.solve_algorithm(
algorithm_name, algorithm_func, game_state
)
else:
algorithm_manager.solve_algorithm(
algorithm_name, algorithm_func, *args)
# Function to create an instance of class and then pass the search to lambda function
def create_algorithm_and_solve(algorithm_class, algorithm_name, *args):
def solve():
instance: BaseSearch = algorithm_class(
game_state.maze, game_state.start_pos, game_state.goal_pos
)
if algorithm_name == "IDS":
solve_and_update(algorithm_name, instance.search, game_state)
else:
solve_and_update(algorithm_name, instance.search, *args)
return solve
def create_qlearning_and_solve(algorithm_class, algorithm_name, *args):
def solve():
instance: BaseSearch = algorithm_class(
game_state.maze, game_state.start_pos, game_state.goal_pos
)
solve_and_update(algorithm_name, instance.search, *args)
return solve
# Create buttons for each algorithm
button_manager.add_button(
"BFS", 0, action=create_algorithm_and_solve(bfs.BreadthFirstSearch, "BFS")
)
button_manager.add_button(
"DFS", 1, action=create_algorithm_and_solve(dfs.DepthFirstSearch, "DFS")
)
button_manager.add_button(
"UCS", 2, action=create_algorithm_and_solve(ucs.UniformCostSearch, "UCS")
)
L_BUTTON_WIDTH = BUTTON_WIDTH // 3
ids_button = button_manager.add_button(
"IDS",
3,
btn_width=L_BUTTON_WIDTH,
width_offset=L_BUTTON_WIDTH,
action=create_algorithm_and_solve(
ids.IterativeDeepeningSearch, "IDS", game_state
),
subtext=str(game_state.l),
)
# L button, that is next to IDS
button_manager.add_button(
"-",
3,
btn_width=L_BUTTON_WIDTH,
action=lambda: game_state.l_setter(game_state.l - 5)
or ids_button.set_subtext(game_state.l),
)
button_manager.add_button(
"+",
3,
btn_width=L_BUTTON_WIDTH,
width_offset=L_BUTTON_WIDTH * 2,
action=lambda: game_state.l_setter(game_state.l + 5)
or ids_button.set_subtext(game_state.l),
)
# Reset path and reset maze buttons
button_manager.add_button("Reset Path", 11, action=game_state.reset_path)
button_manager.add_button(
"Reset Maze", 12, action=lambda: game_state.reset_maze(maze_generator)
)
button_manager.add_button(
"Greedy BFS (Manhattan)",
4,
action=create_algorithm_and_solve(
greedy_bfs.GreedyBestFirstSearch,
"Greedy BFS (Manhattan)",
Metrics.manhattan,
),
)
button_manager.add_button(
"Greedy BFS (Euclidean)",
5,
action=create_algorithm_and_solve(
greedy_bfs.GreedyBestFirstSearch,
"Greedy BFS (Euclidean)",
Metrics.euclidean,
),
)
button_manager.add_button(
"A* (Manhattan)",
6,
action=create_algorithm_and_solve(
astar.AStarSearch, "A* (Manhattan)", Metrics.manhattan
),
)
button_manager.add_button(
"A* (Euclidean)",
7,
action=create_algorithm_and_solve(
astar.AStarSearch, "A* (Euclidean)", Metrics.euclidean
),
)
button_manager.add_button(
"Hill Climbing",
8,
action=create_algorithm_and_solve(
hill_climbing.HillClimbing, "Hill Climbing"),
)
button_manager.add_button(
"Simulated Annealing",
9,
action=create_algorithm_and_solve(
simulated_annealing.SimulatedAnnealing, "Simulated Annealing"
),
)
button_manager.add_button(
"Genetic Algorithm",
10,
action=lambda: solve_and_update(
"Genetic Algorithm", genetic_algorithm.genetic_algorithm
),
)
# Add the button for comparison.
button_manager.add_button(
"Compare Algos",
13,
action=lambda: showDifferences_ExecutionTime(
algorithm_manager.compare_algos),
)
button_manager.add_button(
"Q-Learning",
14,
action=create_qlearning_and_solve(
q_learning.QLearning, "Q-Learning"
)
)
# Setup initial game state
game_state.reset_maze(maze_generator)
# Run the game
game_window.run()
if __name__ == "__main__":
main()