-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteepest_hill_climbing.py
52 lines (40 loc) · 1.52 KB
/
steepest_hill_climbing.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
import state as sta
import numpy as np
def steepest_hill_climbing(start_state):
current = start_state
visited_array = np.array([])
while not current.you_win():
visited_array = np.append(visited_array, current)
next_states = current.all_next_state_move()
if len(next_states) == 0:
break
best_next_state = min(next_states, key=sta.map_state.heuristic)
if best_next_state.heuristic() >= current.heuristic():
break
best_next_state.parent1 = current
current = best_next_state
if current.you_win():
print("you_win")
path = []
while current.parent1:
path.append(current.parent)
current = current.parent1
path.reverse()
return path, len(path), len(visited_array)
print(" not you_win")
return [], 0, visited_array
initial_state = sta. map_state(12)
initial_state.parent = "root"
initial_state.printer(10, 1, "red", "🔴", True, False)
initial_state.printer(1, 1, "red", "🟥", False, False)
# initial_state.printer(3, 4, "red", "🔴", True, False)
# initial_state.printer(3, 1, "red", "🟥", False, False)
# initial_state.printer(1, 8, "red", "🔴", True, False)
# initial_state.printer(8, 8, "red", "🟥", False, False)
initial_state.print_map()
# path, len(path), len(visited_array)
# path, path_win, path_len, visited_len
path, path_len, visited_len = steepest_hill_climbing(initial_state)
print("path: ", path)
print("path_len: ", path_len)
print("visited_len: ", visited_len)