Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ddf86f

Browse files
committed
Merge pull request aimacode#119 from lucasmoura/flake8_warnings
Fix flake8 warnings
2 parents 8b1b8a6 + 13e893f commit 7ddf86f

18 files changed

+434
-334
lines changed

agents.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
#
3636
# Speed control in GUI does not have any effect -- fix it.
3737

38-
from utils import *
38+
from utils import * # noqa
3939

4040
import random
4141
import copy
4242
import collections
4343

44-
#______________________________________________________________________________
44+
# ______________________________________________________________________________
4545

4646

4747
class Thing(object):
@@ -51,7 +51,8 @@ class Thing(object):
5151
.__name__ slot (used for output only)."""
5252

5353
def __repr__(self):
54-
return '<{}>'.format(getattr(self, '__name__', self.__class__.__name__))
54+
return '<{}>'.format(getattr(self, '__name__',
55+
self.__class__.__name__))
5556

5657
def is_alive(self):
5758
"Things that are 'alive' should return true."
@@ -108,7 +109,7 @@ def new_program(percept):
108109
agent.program = new_program
109110
return agent
110111

111-
#______________________________________________________________________________
112+
# ______________________________________________________________________________
112113

113114

114115
def TableDrivenAgentProgram(table):
@@ -129,7 +130,7 @@ def RandomAgentProgram(actions):
129130
"An agent that chooses an action at random, ignoring all percepts."
130131
return lambda percept: random.choice(actions)
131132

132-
#______________________________________________________________________________
133+
# ______________________________________________________________________________
133134

134135

135136
def SimpleReflexAgentProgram(rules, interpret_input):
@@ -159,7 +160,7 @@ def rule_match(state, rules):
159160
if rule.matches(state):
160161
return rule
161162

162-
#______________________________________________________________________________
163+
# ______________________________________________________________________________
163164

164165
loc_A, loc_B = (0, 0), (1, 0) # The two locations for the Vacuum world
165166

@@ -214,7 +215,7 @@ def program(location, status):
214215
return 'Left'
215216
return Agent(program)
216217

217-
#______________________________________________________________________________
218+
# ______________________________________________________________________________
218219

219220

220221
class Environment(object):
@@ -237,7 +238,10 @@ def thing_classes(self):
237238
return [] # List of classes that can go into environment
238239

239240
def percept(self, agent):
240-
"Return the percept that the agent sees at this point. (Implement this.)"
241+
'''
242+
Return the percept that the agent sees at this point.
243+
(Implement this.)
244+
'''
241245
raise NotImplementedError
242246

243247
def execute_action(self, agent, action):
@@ -305,7 +309,8 @@ def delete_thing(self, thing):
305309
except(ValueError, e):
306310
print(e)
307311
print(" in Environment delete_thing")
308-
print(" Thing to be removed: {} at {}" .format(thing, thing.location))
312+
print(" Thing to be removed: {} at {}" .format(thing,
313+
thing.location))
309314
print(" from list: {}" .format([(thing, thing.location)
310315
for thing in self.things]))
311316
if thing in self.agents:
@@ -419,7 +424,7 @@ class Obstacle(Thing):
419424
class Wall(Obstacle):
420425
pass
421426

422-
#______________________________________________________________________________
427+
# ______________________________________________________________________________
423428
# Vacuum environment
424429

425430

@@ -502,7 +507,7 @@ def default_location(self, thing):
502507
"Agents start in either location at random."
503508
return random.choice([loc_A, loc_B])
504509

505-
#______________________________________________________________________________
510+
# ______________________________________________________________________________
506511
# The Wumpus World
507512

508513

@@ -538,7 +543,7 @@ def thing_classes(self):
538543
# Needs a lot of work ...
539544

540545

541-
#______________________________________________________________________________
546+
# ______________________________________________________________________________
542547

543548
def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000):
544549
"""See how well each of several agents do in n instances of an environment.
@@ -559,7 +564,7 @@ def score(env):
559564
return agent.performance
560565
return mean(list(map(score, envs)))
561566

562-
#_________________________________________________________________________
567+
# _________________________________________________________________________
563568

564569
__doc__ += """
565570
>>> a = ReflexVacuumAgent()
@@ -590,4 +595,3 @@ def score(env):
590595
>>> 0.5 < testv(RandomVacuumAgent) < 3
591596
True
592597
"""
593-

csp.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""CSP (Constraint Satisfaction Problems) problems and solvers. (Chapter 6)."""
22

3-
from utils import *
3+
from utils import * # noqa
44
import search
55

66
from collections import defaultdict
77
from functools import reduce
88

9+
import itertools
10+
import re
11+
912

1013
class CSP(search.Problem):
1114

@@ -44,7 +47,8 @@ class CSP(search.Problem):
4447
display(a) Print a human-readable representation
4548
4649
>>> search.depth_first_graph_search(australia)
47-
<Node (('WA', 'B'), ('Q', 'B'), ('T', 'B'), ('V', 'B'), ('SA', 'G'), ('NT', 'R'), ('NSW', 'R'))>
50+
<Node (('WA', 'B'), ('Q', 'B'), ('T', 'B'), ('V', 'B'), ('SA', 'G'),
51+
('NT', 'R'), ('NSW', 'R'))>
4852
"""
4953

5054
def __init__(self, vars, domains, neighbors, constraints):
@@ -70,8 +74,8 @@ def nconflicts(self, var, val, assignment):
7074
"Return the number of conflicts var=val has with other variables."
7175
# Subclasses may implement this more efficiently
7276
def conflict(var2):
73-
return (var2 in assignment
74-
and not self.constraints(var, val, var2, assignment[var2]))
77+
return (var2 in assignment and
78+
not self.constraints(var, val, var2, assignment[var2]))
7579
return count_if(conflict, self.neighbors[var])
7680

7781
def display(self, assignment):
@@ -149,7 +153,7 @@ def conflicted_vars(self, current):
149153
return [var for var in self.vars
150154
if self.nconflicts(var, current[var], current) > 0]
151155

152-
#______________________________________________________________________________
156+
# ______________________________________________________________________________
153157
# Constraint Propagation with AC-3
154158

155159

@@ -180,7 +184,7 @@ def revise(csp, Xi, Xj, removals):
180184
revised = True
181185
return revised
182186

183-
#______________________________________________________________________________
187+
# ______________________________________________________________________________
184188
# CSP Backtracking Search
185189

186190
# Variable ordering
@@ -251,17 +255,21 @@ def backtracking_search(csp,
251255
"""[Fig. 6.5]
252256
>>> backtracking_search(australia) is not None
253257
True
254-
>>> backtracking_search(australia, select_unassigned_variable=mrv) is not None
258+
>>> backtracking_search(australia,
259+
>>> select_unassigned_variable=mrv) is not None
255260
True
256-
>>> backtracking_search(australia, order_domain_values=lcv) is not None
261+
>>> backtracking_search(australia,
262+
>>> order_domain_values=lcv) is not None
257263
True
258-
>>> backtracking_search(australia, select_unassigned_variable=mrv, order_domain_values=lcv) is not None
264+
>>> backtracking_search(australia, select_unassigned_variable=mrv,
265+
>>> order_domain_values=lcv) is not None
259266
True
260267
>>> backtracking_search(australia, inference=forward_checking) is not None
261268
True
262269
>>> backtracking_search(australia, inference=mac) is not None
263270
True
264-
>>> backtracking_search(usa, select_unassigned_variable=mrv, order_domain_values=lcv, inference=mac) is not None
271+
>>> backtracking_search(usa, select_unassigned_variable=mrv,
272+
>>> order_domain_values=lcv, inference=mac) is not None
265273
True
266274
"""
267275

@@ -285,7 +293,7 @@ def backtrack(assignment):
285293
assert result is None or csp.goal_test(result)
286294
return result
287295

288-
#______________________________________________________________________________
296+
# ______________________________________________________________________________
289297
# Min-conflicts hillclimbing search for CSPs
290298

291299

@@ -313,12 +321,11 @@ def min_conflicts_value(csp, var, current):
313321
return argmin_random_tie(csp.domains[var],
314322
lambda val: csp.nconflicts(var, val, current))
315323

316-
#______________________________________________________________________________
324+
# ______________________________________________________________________________
317325

318326

319327
def tree_csp_solver(csp):
320328
"[Fig. 6.11]"
321-
n = len(csp.vars)
322329
assignment = {}
323330
root = csp.vars[0]
324331
X, parent = topological_sort(csp.vars, root)
@@ -339,7 +346,7 @@ def topological_sort(xs, x):
339346
def make_arc_consistent(Xj, Xk, csp):
340347
unimplemented()
341348

342-
#______________________________________________________________________________
349+
# ______________________________________________________________________________
343350
# Map-Coloring Problems
344351

345352

@@ -417,7 +424,7 @@ def parse_neighbors(neighbors, vars=[]):
417424
PI; PA: LR RA; PC: PL CE LI AQ; PI: NH NO CA IF; PL: BR NB CE PC; RA:
418425
AU BO FC PA LR""")
419426

420-
#______________________________________________________________________________
427+
# ______________________________________________________________________________
421428
# n-Queens Problem
422429

423430

@@ -507,17 +514,14 @@ def display(self, assignment):
507514
print(str(self.nconflicts(var, val, assignment))+ch, end=' ')
508515
print()
509516

510-
#______________________________________________________________________________
517+
# ______________________________________________________________________________
511518
# Sudoku
512519

513-
import itertools
514-
import re
515-
516520

517521
def flatten(seqs): return sum(seqs, [])
518522

519-
easy1 = '..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..'
520-
harder1 = '4173698.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......'
523+
easy1 = '..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..' # noqa
524+
harder1 = '4173698.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......' # noqa
521525

522526
_R3 = list(range(3))
523527
_CELL = itertools.count().__next__
@@ -531,6 +535,7 @@ def flatten(seqs): return sum(seqs, [])
531535
for v in unit:
532536
_NEIGHBORS[v].update(unit - set([v]))
533537

538+
534539
class Sudoku(CSP):
535540

536541
"""A Sudoku problem.
@@ -564,7 +569,8 @@ class Sudoku(CSP):
564569
8 1 4 | 2 5 3 | 7 6 9
565570
6 9 5 | 4 1 7 | 3 8 2
566571
>>> h = Sudoku(harder1)
567-
>>> None != backtracking_search(h, select_unassigned_variable=mrv, inference=forward_checking)
572+
>>> None != backtracking_search(h, select_unassigned_variable=mrv,
573+
>>> inference=forward_checking)
568574
True
569575
"""
570576
R3 = _R3
@@ -596,8 +602,9 @@ def show_cell(cell): return str(assignment.get(cell, '.'))
596602
def abut(lines1, lines2): return list(
597603
map(' | '.join, list(zip(lines1, lines2))))
598604
print('\n------+-------+------\n'.join(
599-
'\n'.join(reduce(abut, list(map(show_box, brow)))) for brow in self.bgrid))
600-
#______________________________________________________________________________
605+
'\n'.join(reduce(
606+
abut, list(map(show_box, brow)))) for brow in self.bgrid))
607+
# ______________________________________________________________________________
601608
# The Zebra Puzzle
602609

603610

games.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Games, or Adversarial Search. (Chapter 5)
22
"""
33

4-
from utils import *
4+
from utils import * # noqa
55

66
import random
77

8-
#______________________________________________________________________________
8+
# ______________________________________________________________________________
99
# Minimax Search
1010

1111

@@ -35,7 +35,7 @@ def min_value(state):
3535
return argmax(game.actions(state),
3636
lambda a: min_value(game.result(state, a)))
3737

38-
#______________________________________________________________________________
38+
# ______________________________________________________________________________
3939

4040

4141
def alphabeta_full_search(state, game):
@@ -112,7 +112,8 @@ def min_value(state, alpha, beta, depth):
112112
# Body of alphabeta_search starts here:
113113
# The default test cuts off at depth d or at a terminal state
114114
cutoff_test = (cutoff_test or
115-
(lambda state, depth: depth > d or game.terminal_test(state)))
115+
(lambda state, depth: depth > d or
116+
game.terminal_test(state)))
116117
eval_fn = eval_fn or (lambda state: game.utility(state, player))
117118
best_score = -infinity
118119
best_action = None
@@ -123,7 +124,7 @@ def min_value(state, alpha, beta, depth):
123124
best_action = a
124125
return best_action
125126

126-
#______________________________________________________________________________
127+
# ______________________________________________________________________________
127128
# Players for Games
128129

129130

@@ -155,7 +156,7 @@ def play_game(game, *players):
155156
if game.terminal_test(state):
156157
return game.utility(state, game.to_move(game.initial))
157158

158-
#______________________________________________________________________________
159+
# ______________________________________________________________________________
159160
# Some Sample Games
160161

161162

0 commit comments

Comments
 (0)