-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealClimbingRandom.py
More file actions
49 lines (38 loc) · 1.28 KB
/
healClimbingRandom.py
File metadata and controls
49 lines (38 loc) · 1.28 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
import random
class healClimbingRandom:
def __init__(self, problem):
self.problem = problem
self.visited = 0
self.extend = 0
self.bestAns = []
self.value = 0
def RNeighbor(self, currentSol):
"""
:param currentSol : current state
:return: a random neighbor which is better than current state
"""
currValue = self.problem.value(currentSol)
bestNeighbors = []
for i in self.problem.neighbors(currentSol):
self.visited = self.visited + 1
if (self.problem.value(i) > currValue ):
bestNeighbors.append(i)
if not bestNeighbors :
return bestNeighbors
return random.choice(bestNeighbors)
def healClimbingRAlgorithm(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.RNeighbor(currentState):
self.bestAns = currentState[:]
self.value = self.problem.value(currentState)
return path
currentState = self.RNeighbor(currentState)
return path