6
6
7
7
from utils import (
8
8
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
11
10
)
12
11
from grid import distance
13
12
@@ -419,7 +418,7 @@ def or_search(state, problem, path):
419
418
return [action , plan ]
420
419
421
420
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."""
423
422
plan = {}
424
423
for s in states :
425
424
plan [s ] = or_search (s , problem , path )
@@ -461,8 +460,8 @@ def __call__(self, percept):
461
460
if len (self .unbacktracked [s1 ]) == 0 :
462
461
self .a = None
463
462
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 )
466
465
for (s , b ) in self .result .keys ():
467
466
if self .result [(s , b )] == unbacktracked_pop :
468
467
self .a = b
@@ -546,7 +545,7 @@ def __call__(self, s1): # as of now s1 is a state rather than a percept
546
545
547
546
# an action b in problem.actions(s1) that minimizes costs
548
547
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 ))
550
549
551
550
self .s = s1
552
551
return self .a
@@ -573,17 +572,17 @@ def genetic_search(problem, fitness_fn, ngen=1000, pmut=0.1, n=20):
573
572
"""Call genetic_algorithm on the appropriate parts of a problem.
574
573
This requires the problem to have states that can mate and mutate,
575
574
plus a value method that scores states."""
576
-
575
+
577
576
# NOTE: This is not tested and might not work.
578
577
# TODO: Use this function to make Problems work with genetic_algorithm.
579
-
578
+
580
579
s = problem .initial_state
581
580
states = [problem .result (s , a ) for a in problem .actions (s )]
582
581
random .shuffle (states )
583
582
return genetic_algorithm (states [:n ], problem .value , ngen , pmut )
584
583
585
584
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
587
586
"""[Figure 4.8]"""
588
587
for i in range (ngen ):
589
588
new_population = []
@@ -954,7 +953,7 @@ def print_boggle(board):
954
953
print ()
955
954
956
955
957
- def boggle_neighbors (n2 , cache = {}): # noqa
956
+ def boggle_neighbors (n2 , cache = {}):
958
957
"""Return a list of lists, where the i-th element is the list of indexes
959
958
for the neighbors of square i."""
960
959
if cache .get (n2 ):
0 commit comments