-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHillClimbing.py
31 lines (28 loc) · 883 Bytes
/
HillClimbing.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
# A random-restart version of the Hill-Climbing search algorithm
# No sideways moves are allowed. If plateau is reacheed, restart is applied
from StateFormulation import *
def search():
restarts = 0
while True:
state = generate_random_state()
# Reset at each restart
transitions = [ state ]
while True:
curr_attacks = count_attacks(state)
if curr_attacks == 0:
# Goal reached
return state, transitions, restarts
# Generate next best state
move, next_attacks = get_next_best_move(state)
if next_attacks >= curr_attacks:
# At some local maxima or plateau
# Restart search
restarts += 1
break
# Move to the best successor state
in_col, to_row = move
state[in_col] = to_row
# Add this new state to set of transitions
transitions.append(state)
# Restart search with new random start
state = generate_random_state()