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

Skip to content

Commit c788734

Browse files
committed
Stripped trailing whitespace.
1 parent f7a329b commit c788734

File tree

12 files changed

+346
-346
lines changed

12 files changed

+346
-346
lines changed

agents.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Agent(Thing):
6565
"""An Agent is a subclass of Thing with one required slot,
6666
.program, which should hold a function that takes one argument, the
6767
percept, and returns an action. (What counts as a percept or action
68-
will depend on the specific environment in which the agent exists.)
68+
will depend on the specific environment in which the agent exists.)
6969
Note that 'program' is a slot, not a method. If it were a method,
7070
then the program could 'cheat' and look at aspects of the agent.
7171
It's not supposed to do that: the program can only look at the
@@ -87,7 +87,7 @@ def can_grab(self, thing):
8787
"""Returns True if this agent can grab this thing.
8888
Override for appropriate subclasses of Agent and Thing."""
8989
return False
90-
90+
9191
def TraceAgent(agent):
9292
"""Wrap the agent's program to print its input and output. This will let
9393
you see what the agent is doing in the environment."""
@@ -253,7 +253,7 @@ def list_things_at(self, location, tclass=Thing):
253253
"Return all things exactly at a given location."
254254
return [thing for thing in self.things
255255
if thing.location == location and isinstance(thing, tclass)]
256-
256+
257257
def some_things_at(self, location, tclass=Thing):
258258
"""Return true if at least one of the things at location
259259
is an instance of class tclass (or a subclass)."""
@@ -297,7 +297,7 @@ class XYEnvironment(Environment):
297297
def __init__(self, width=10, height=10):
298298
super(XYEnvironment, self).__init__()
299299
update(self, width=width, height=height, observers=[])
300-
300+
301301
def things_near(self, location, radius=None):
302302
"Return all things within radius of location."
303303
if radius is None: radius = self.perceptible_distance
@@ -343,7 +343,7 @@ def move_to(self, thing, destination):
343343
thing.location = destination
344344
for o in self.observers:
345345
o.thing_moved(thing)
346-
346+
347347
def add_thing(self, thing, location=(1, 1)):
348348
super(XYEnvironment, self).add_thing(thing, location)
349349
thing.holding = []
@@ -356,7 +356,7 @@ def delete_thing(self, thing):
356356
# Any more to do? Thing holding anything or being held?
357357
for obs in self.observers:
358358
obs.thing_deleted(thing)
359-
359+
360360
def add_walls(self):
361361
"Put walls around the entire perimeter of the grid."
362362
for x in range(self.width):
@@ -367,14 +367,14 @@ def add_walls(self):
367367
self.add_thing(Wall(), (self.width-1, y))
368368

369369
def add_observer(self, observer):
370-
"""Adds an observer to the list of observers.
370+
"""Adds an observer to the list of observers.
371371
An observer is typically an EnvGUI.
372-
372+
373373
Each observer is notified of changes in move_to and add_thing,
374374
by calling the observer's methods thing_moved(thing)
375375
and thing_added(thing, loc)."""
376376
self.observers.append(observer)
377-
377+
378378
def turn_heading(self, heading, inc):
379379
"Return the heading to the left (inc=+1) or right (inc=-1) of heading."
380380
return turn_heading(heading, inc)
@@ -388,11 +388,11 @@ class Wall(Obstacle):
388388
pass
389389

390390
#______________________________________________________________________________
391-
## Vacuum environment
391+
## Vacuum environment
392392

393393
class Dirt(Thing):
394394
pass
395-
395+
396396
class VacuumEnvironment(XYEnvironment):
397397
"""The environment of [Ex. 2.12]. Agent perceives dirty or clean,
398398
and bump (into obstacle) or not; 2D discrete world of unknown size;
@@ -440,9 +440,9 @@ def __init__(self):
440440
loc_B: random.choice(['Clean', 'Dirty'])}
441441

442442
def thing_classes(self):
443-
return [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent,
443+
return [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent,
444444
TableDrivenVacuumAgent, ModelBasedVacuumAgent]
445-
445+
446446
def percept(self, agent):
447447
"Returns the agent's location, and the location status (Dirty/Clean)."
448448
return (agent.location, self.status[agent.location])
@@ -485,16 +485,16 @@ def thing_classes(self):
485485

486486
## Needs a lot of work ...
487487

488-
488+
489489
#______________________________________________________________________________
490490

491491
def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000):
492492
"""See how well each of several agents do in n instances of an environment.
493493
Pass in a factory (constructor) for environments, and several for agents.
494-
Create n instances of the environment, and run each agent in copies of
494+
Create n instances of the environment, and run each agent in copies of
495495
each one for steps. Return a list of (agent, average-score) tuples."""
496496
envs = [EnvFactory() for i in range(n)]
497-
return [(A, test_agent(A, steps, copy.deepcopy(envs)))
497+
return [(A, test_agent(A, steps, copy.deepcopy(envs)))
498498
for A in AgentFactories]
499499

500500
def test_agent(AgentFactory, steps, envs):
@@ -527,7 +527,7 @@ def score(env):
527527
## give is a range of expected scores. If this test fails, it does
528528
## not necessarily mean something is wrong.
529529
>>> envs = [TrivialVacuumEnvironment() for i in range(100)]
530-
>>> def testv(A): return test_agent(A, 4, copy.deepcopy(envs))
530+
>>> def testv(A): return test_agent(A, 4, copy.deepcopy(envs))
531531
>>> 7 < testv(ModelBasedVacuumAgent) < 11
532532
True
533533
>>> 5 < testv(ReflexVacuumAgent) < 9
@@ -551,12 +551,12 @@ class EnvGUI(tk.Tk, object):
551551
def __init__(self, env, title = 'AIMA GUI', cellwidth=50, n=10):
552552

553553
# Initialize window
554-
554+
555555
super(EnvGUI, self).__init__()
556556
self.title(title)
557557

558558
# Create components
559-
559+
560560
canvas = EnvCanvas(self, env, cellwidth, n)
561561
toolbar = EnvToolbar(self, env, canvas)
562562
for w in [canvas, toolbar]:
@@ -569,14 +569,14 @@ def __init__(self, parent, env, canvas):
569569
super(EnvToolbar, self).__init__(parent, relief='raised', bd=2)
570570

571571
# Initialize instance variables
572-
572+
573573
self.env = env
574574
self.canvas = canvas
575575
self.running = False
576576
self.speed = 1.0
577577

578578
# Create buttons and other controls
579-
579+
580580
for txt, cmd in [('Step >', self.env.step),
581581
('Run >>', self.run),
582582
('Stop [ ]', self.stop),
@@ -608,7 +608,7 @@ def background_run(self):
608608
delay_sec = 1.0 / max(self.speed, 1.0) # avoid division by zero
609609
ms = int(1000.0 * delay_sec) # seconds to milliseconds
610610
self.after(ms, self.background_run)
611-
611+
612612
def list_things(self):
613613
print "Things in the environment:"
614614
for thing in self.env.things:
@@ -621,4 +621,4 @@ def list_agents(self):
621621

622622
def set_speed(self, speed):
623623
self.speed = float(speed)
624-
624+

csp.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, vars, domains, neighbors, constraints):
5353
update(self, vars=vars, domains=domains,
5454
neighbors=neighbors, constraints=constraints,
5555
initial=(), curr_domains=None, nassigns=0)
56-
56+
5757
def assign(self, var, val, assignment):
5858
"Add {var: val} to assignment; Discard the old value if any."
5959
assignment[var] = val
@@ -91,7 +91,7 @@ def actions(self, state):
9191
var = find_if(lambda v: v not in assignment, self.vars)
9292
return [(var, val) for val in self.domains[var]
9393
if self.nconflicts(var, val, assignment) == 0]
94-
94+
9595
def result(self, state, (var, val)):
9696
"Perform an action and return the new state."
9797
return state + ((var, val),)
@@ -137,7 +137,7 @@ def restore(self, removals):
137137
def infer_assignment(self):
138138
"Return the partial assignment implied by the current inferences."
139139
self.support_pruning()
140-
return dict((v, self.curr_domains[v][0])
140+
return dict((v, self.curr_domains[v][0])
141141
for v in self.vars if 1 == len(self.curr_domains[v]))
142142

143143
## This is for min_conflicts search
@@ -242,7 +242,7 @@ def backtrack(assignment):
242242
result = backtrack({})
243243
assert result is None or csp.goal_test(result)
244244
return result
245-
245+
246246
#______________________________________________________________________________
247247
# Constraint Propagation with AC-3
248248

@@ -292,7 +292,7 @@ def min_conflicts_value(csp, var, current):
292292
"""Return the value that will give var the least number of conflicts.
293293
If there is a tie, choose at random."""
294294
return argmin_random_tie(csp.domains[var],
295-
lambda val: csp.nconflicts(var, val, current))
295+
lambda val: csp.nconflicts(var, val, current))
296296

297297
#______________________________________________________________________________
298298
# Map-Coloring Problems
@@ -319,7 +319,7 @@ def MapColoringCSP(colors, neighbors):
319319
specified as a string of the form defined by parse_neighbors"""
320320

321321
if isinstance(neighbors, str):
322-
neighbors = parse_neighbors(neighbors)
322+
neighbors = parse_neighbors(neighbors)
323323
return CSP(neighbors.keys(), UniversalDict(colors), neighbors,
324324
different_values_constraint)
325325

@@ -345,7 +345,7 @@ def parse_neighbors(neighbors, vars=[]):
345345

346346
australia = MapColoringCSP(list('RGB'),
347347
'SA: WA NT Q NSW V; NT: WA Q; NSW: Q V; T: ')
348-
348+
349349
usa = MapColoringCSP(list('RGBY'),
350350
"""WA: OR ID; OR: ID NV CA; CA: NV AZ; NV: ID UT AZ; ID: MT WY UT;
351351
UT: WY CO AZ; MT: ND SD WY; WY: SD NE CO; CO: NE KA OK NM; NM: OK TX;
@@ -398,7 +398,7 @@ def __init__(self, n):
398398
UniversalDict(range(n)), queen_constraint)
399399
update(self, rows=[0]*n, ups=[0]*(2*n - 1), downs=[0]*(2*n - 1))
400400

401-
def nconflicts(self, var, val, assignment):
401+
def nconflicts(self, var, val, assignment):
402402
"""The number of conflicts, as recorded with each assignment.
403403
Count conflicts in row and in up, down diagonals. If there
404404
is a queen there, it can't conflict with itself, so subtract 3."""
@@ -422,7 +422,7 @@ def unassign(self, var, assignment):
422422
if var in assignment:
423423
self.record_conflict(assignment, var, assignment[var], -1)
424424
CSP.unassign(self, var, assignment)
425-
425+
426426
def record_conflict(self, assignment, var, val, delta):
427427
"Record conflicts caused by addition or deletion of a Queen."
428428
n = len(self.vars)
@@ -443,8 +443,8 @@ def display(self, assignment):
443443
for var in range(n):
444444
if assignment.get(var,'') == val: ch = '*'
445445
else: ch = ' '
446-
print str(self.nconflicts(var, val, assignment))+ch,
447-
print
446+
print str(self.nconflicts(var, val, assignment))+ch,
447+
print
448448

449449
#______________________________________________________________________________
450450
# Sudoku
@@ -568,7 +568,7 @@ def zebra_constraint(A, a, B, b, recurse=0):
568568
(A in Pets and B in Pets) or
569569
(A in Drinks and B in Drinks) or
570570
(A in Countries and B in Countries) or
571-
(A in Smokes and B in Smokes)): return not same
571+
(A in Smokes and B in Smokes)): return not same
572572
raise 'error'
573573
return CSP(vars, domains, neighbors, zebra_constraint)
574574

doctests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
python doctests.py *.py
66
python doctests.py -v *.py
77
8-
You can add more module-level tests with
8+
You can add more module-level tests with
99
__doc__ += "..."
10-
You can add stochastic tests with
10+
You can add stochastic tests with
1111
__doc__ += random_tests("...")
1212
"""
1313

games.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# from the third edition until this gets reviewed.)
66

77
from utils import *
8-
import random
8+
import random
99

1010
#______________________________________________________________________________
1111
# Minimax Search
@@ -39,7 +39,7 @@ def min_value(state):
3939

4040

4141
#______________________________________________________________________________
42-
42+
4343
def alphabeta_full_search(state, game):
4444
"""Search game to determine best action; use alpha-beta pruning.
4545
As in [Fig. 6.7], this version searches all the way to the leaves."""
@@ -157,7 +157,7 @@ def legal_moves(self, state):
157157
def make_move(self, move, state):
158158
"Return the state that results from making a move from a state."
159159
abstract
160-
160+
161161
def utility(self, state, player):
162162
"Return the value of this final state to player."
163163
abstract
@@ -198,7 +198,7 @@ class Fig62Game(Game):
198198
D=dict(d1='D1', d2='D2', d3='D3'))
199199
utils = Dict(B1=3, B2=12, B3=8, C1=2, C2=4, C3=6, D1=14, D2=5, D3=2)
200200
initial = 'A'
201-
201+
202202
def legal_moves(self, state):
203203
return [move for (move, next) in self.successors(state)]
204204

@@ -213,7 +213,7 @@ def utility(self, state, player):
213213
return self.utils[state]
214214
else:
215215
return -self.utils[state]
216-
216+
217217
def terminal_test(self, state):
218218
return state not in ('A', 'B', 'C', 'D')
219219

@@ -287,7 +287,7 @@ class ConnectFour(TicTacToe):
287287
"""A TicTacToe-like game in which you can only make a move on the bottom
288288
row, or in a square directly above an occupied square. Traditionally
289289
played on a 7x6 board and requiring 4 in a row."""
290-
290+
291291
def __init__(self, h=7, v=6, k=4):
292292
TicTacToe.__init__(self, h, v, k)
293293

0 commit comments

Comments
 (0)