66
77from utils import (
88 is_in , argmin , argmax , argmax_random_tie , probability , weighted_sampler ,
9- weighted_sample_with_replacement , memoize , print_table , DataFile , Stack ,
10- FIFOQueue , PriorityQueue , name
9+ memoize , print_table , DataFile , Stack , FIFOQueue , PriorityQueue , name
1110)
1211from grid import distance
1312
@@ -419,7 +418,7 @@ def or_search(state, problem, path):
419418 return [action , plan ]
420419
421420 def and_search (states , problem , path ):
422- """Returns plan in form of dictionary where we take action plan[s] if we reach state s.""" # noqa
421+ """Returns plan in form of dictionary where we take action plan[s] if we reach state s."""
423422 plan = {}
424423 for s in states :
425424 plan [s ] = or_search (s , problem , path )
@@ -461,8 +460,8 @@ def __call__(self, percept):
461460 if len (self .unbacktracked [s1 ]) == 0 :
462461 self .a = None
463462 else :
464- # else a <- an action b such that result[s', b] = POP(unbacktracked[s']) # noqa
465- unbacktracked_pop = self .unbacktracked [s1 ].pop (0 ) # noqa
463+ # else a <- an action b such that result[s', b] = POP(unbacktracked[s'])
464+ unbacktracked_pop = self .unbacktracked [s1 ].pop (0 )
466465 for (s , b ) in self .result .keys ():
467466 if self .result [(s , b )] == unbacktracked_pop :
468467 self .a = b
@@ -546,7 +545,7 @@ def __call__(self, s1): # as of now s1 is a state rather than a percept
546545
547546 # an action b in problem.actions(s1) that minimizes costs
548547 self .a = argmin (self .problem .actions (s1 ),
549- key = lambda b :self .LRTA_cost (s1 , b , self .problem .output (s1 , b ), self .H ))
548+ key = lambda b : self .LRTA_cost (s1 , b , self .problem .output (s1 , b ), self .H ))
550549
551550 self .s = s1
552551 return self .a
@@ -573,17 +572,17 @@ def genetic_search(problem, fitness_fn, ngen=1000, pmut=0.1, n=20):
573572 """Call genetic_algorithm on the appropriate parts of a problem.
574573 This requires the problem to have states that can mate and mutate,
575574 plus a value method that scores states."""
576-
575+
577576 # NOTE: This is not tested and might not work.
578577 # TODO: Use this function to make Problems work with genetic_algorithm.
579-
578+
580579 s = problem .initial_state
581580 states = [problem .result (s , a ) for a in problem .actions (s )]
582581 random .shuffle (states )
583582 return genetic_algorithm (states [:n ], problem .value , ngen , pmut )
584583
585584
586- def genetic_algorithm (population , fitness_fn , gene_pool = ['0' , '1' ], f_thres = None , ngen = 1000 , pmut = 0.1 ):
585+ def genetic_algorithm (population , fitness_fn , gene_pool = ['0' , '1' ], f_thres = None , ngen = 1000 , pmut = 0.1 ): # noqa
587586 """[Figure 4.8]"""
588587 for i in range (ngen ):
589588 new_population = []
@@ -954,7 +953,7 @@ def print_boggle(board):
954953 print ()
955954
956955
957- def boggle_neighbors (n2 , cache = {}): # noqa
956+ def boggle_neighbors (n2 , cache = {}):
958957 """Return a list of lists, where the i-th element is the list of indexes
959958 for the neighbors of square i."""
960959 if cache .get (n2 ):
0 commit comments