-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealClimbingAlgorithm.py
More file actions
50 lines (39 loc) · 1.35 KB
/
healClimbingAlgorithm.py
File metadata and controls
50 lines (39 loc) · 1.35 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
class healClimbing:
def __init__(self, problem):
self.problem = problem
self.path = []
self.visited = 0
self.extend = 0
self.bestAns = []
self.value = 0
def bestNeighbor(self,currentSol):
"""
:param currentSol : current state
:return: the best neighbor for current state
"""
maxValue = self.problem.value(currentSol)
bestNeighbor = []
for i in self.problem.neighbors(currentSol):
self.visited = self.visited + 1
if (self.problem.value(i) > maxValue) :
maxValue = self.problem.value(i)
bestNeighbor = i
# self.visited = self.visited + len(self.problem.neighbors(currentSol))
# print(bestNeighbor)
return bestNeighbor
def healClimbingAlgorithm(self, start):
"""
:param start: start state
:return: path to goal state
"""
path = []
currentState = start
while (True) :
self.extend = self.extend + 1
path.append(currentState)
if not self.bestNeighbor(currentState) :
self.value = self.problem.value(currentState)
self.bestAns = currentState[:]
return path
currentState = self.bestNeighbor(currentState)[:]
# return path