-
Notifications
You must be signed in to change notification settings - Fork 5
/
hillclimb.py
23 lines (20 loc) · 819 Bytes
/
hillclimb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from score import *
from utils.MatrixPrototypes import *
from random import random, shuffle
from utils import viz
from getSuccessors import getSuccessors, getSuccessorsHill
import math
def climbhill(matrix, solution, printFlag=True, maxAttempts=2000):
for tries in range(maxAttempts):
neighbors = getSuccessorsHill(solution, 1)
shuffle(neighbors)
initialScore = currentScore = score(matrix, solution)
if printFlag:
if tries % 1 == 0: print "partial score: ", currentScore, " tries: ", tries
for step in neighbors:
nextScore = score(matrix, step)
if nextScore > currentScore:
solution = step
currentScore = nextScore
if initialScore == currentScore: break
return solution, currentScore, tries