@@ -46,7 +46,7 @@ def path_cost(self, c, state1, action, state2):
46
46
and action. The default method costs 1 for every step in the path."""
47
47
return c + 1
48
48
49
- def value (self ):
49
+ def value (self , state ):
50
50
"""For optimization problems, each state has a value. Hill-climbing
51
51
and related algorithms try to maximize this value."""
52
52
abstract
@@ -247,10 +247,14 @@ def hill_climbing(problem):
247
247
stopping when no neighbor is better. [Fig. 4.11]"""
248
248
current = Node (problem .initial )
249
249
while True :
250
- neighbor = argmax (current .expand (problem ), Node .value )
251
- if neighbor .value () <= current .value ():
252
- return current .state
250
+ neighbors = current .expand (problem )
251
+ if not neighbors :
252
+ break
253
+ neighbor = argmax (neighbors , lambda node : problem .value (node .state ))
254
+ if problem .value (neighbor .state ) <= problem .value (current .state ):
255
+ break
253
256
current = neighbor
257
+ return current .state
254
258
255
259
def exp_schedule (k = 20 , lam = 0.005 , limit = 100 ):
256
260
"One possible schedule function for simulated annealing"
@@ -263,8 +267,11 @@ def simulated_annealing(problem, schedule=exp_schedule()):
263
267
T = schedule (t )
264
268
if T == 0 :
265
269
return current
266
- next = random .choice (current .expand (problem ))
267
- delta_e = next .path_cost - current .path_cost
270
+ neighbors = current .expand (problem )
271
+ if not neighbors :
272
+ return current
273
+ next = random .choice (neighbors )
274
+ delta_e = problem .value (next .state ) - problem .value (current .state )
268
275
if delta_e > 0 or probability (math .exp (delta_e / T )):
269
276
current = next
270
277
0 commit comments