Skip to content

Commit 80c48c8

Browse files
goswami-rahulnorvig
authored andcommitted
fixed all instances of issue #833 (#843)
* test commit * agents.ipynb * agents.ipynb * Fixed all the instances of issue #833 * minor fix and cleared change in agents.ipynb
1 parent 14a704b commit 80c48c8

File tree

10 files changed

+110
-111
lines changed

10 files changed

+110
-111
lines changed

agents.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def program(percept):
9696
self.program = program
9797

9898
def can_grab(self, thing):
99-
"""Returns True if this agent can grab this thing.
99+
"""Return True if this agent can grab this thing.
100100
Override for appropriate subclasses of Agent and Thing."""
101101
return False
102102

@@ -444,7 +444,7 @@ def move_to(self, thing, destination):
444444
return thing.bump
445445

446446
def add_thing(self, thing, location=(1, 1), exclude_duplicate_class_items=False):
447-
"""Adds things to the world. If (exclude_duplicate_class_items) then the item won't be
447+
"""Add things to the world. If (exclude_duplicate_class_items) then the item won't be
448448
added if the location has at least one item of the same class."""
449449
if (self.is_inbounds(location)):
450450
if (exclude_duplicate_class_items and
@@ -809,7 +809,7 @@ def init_world(self, program):
809809
self.add_thing(Explorer(program), (1, 1), True)
810810

811811
def get_world(self, show_walls=True):
812-
"""Returns the items in the world"""
812+
"""Return the items in the world"""
813813
result = []
814814
x_start, y_start = (0, 0) if show_walls else (1, 1)
815815

@@ -826,7 +826,7 @@ def get_world(self, show_walls=True):
826826
return result
827827

828828
def percepts_from(self, agent, location, tclass=Thing):
829-
"""Returns percepts from a given location,
829+
"""Return percepts from a given location,
830830
and replaces some items with percepts from chapter 7."""
831831
thing_percepts = {
832832
Gold: Glitter(),
@@ -846,7 +846,7 @@ def percepts_from(self, agent, location, tclass=Thing):
846846
return result if len(result) else [None]
847847

848848
def percept(self, agent):
849-
"""Returns things in adjacent (not diagonal) cells of the agent.
849+
"""Return things in adjacent (not diagonal) cells of the agent.
850850
Result format: [Left, Right, Up, Down, Center / Current location]"""
851851
x, y = agent.location
852852
result = []
@@ -907,7 +907,7 @@ def execute_action(self, agent, action):
907907
agent.has_arrow = False
908908

909909
def in_danger(self, agent):
910-
"""Checks if Explorer is in danger (Pit or Wumpus), if he is, kill him"""
910+
"""Check if Explorer is in danger (Pit or Wumpus), if he is, kill him"""
911911
for thing in self.list_things_at(agent.location):
912912
if isinstance(thing, Pit) or (isinstance(thing, Wumpus) and thing.alive):
913913
agent.alive = False

csp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def topological_sort(X, root):
351351

352352

353353
def build_topological(node, parent, neighbors, visited, stack, parents):
354-
"""Builds the topological sort and the parents of each node in the graph"""
354+
"""Build the topological sort and the parents of each node in the graph."""
355355
visited[node] = True
356356

357357
for n in neighbors[node]:
@@ -427,7 +427,7 @@ def MapColoringCSP(colors, neighbors):
427427
different_values_constraint)
428428

429429

430-
def parse_neighbors(neighbors, variables=[]):
430+
def parse_neighbors(neighbors, variables=None):
431431
"""Convert a string of the form 'X: Y Z; Y: Z' into a dict mapping
432432
regions to neighbors. The syntax is a region name followed by a ':'
433433
followed by zero or more region names, followed by ';', repeated for

knowledge.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
# ______________________________________________________________________________
1212

1313

14-
def current_best_learning(examples, h, examples_so_far=[]):
14+
def current_best_learning(examples, h, examples_so_far=None):
1515
""" [Figure 19.2]
1616
The hypothesis is a list of dictionaries, with each dictionary representing
1717
a disjunction."""
1818
if not examples:
1919
return h
2020

21+
examples_so_far = examples_so_far or []
2122
e = examples[0]
2223
if is_consistent(e, h):
2324
return current_best_learning(examples[1:], h, examples_so_far + [e])
@@ -95,7 +96,7 @@ def generalizations(examples_so_far, h):
9596

9697

9798
def add_or(examples_so_far, h):
98-
"""Adds an OR operation to the hypothesis. The AND operations in the disjunction
99+
"""Add an OR operation to the hypothesis. The AND operations in the disjunction
99100
are generated by the last example (which is the problematic one)."""
100101
ors = []
101102
e = examples_so_far[-1]
@@ -135,7 +136,7 @@ def version_space_update(V, e):
135136

136137

137138
def all_hypotheses(examples):
138-
"""Builds a list of all the possible hypotheses"""
139+
"""Build a list of all the possible hypotheses"""
139140
values = values_table(examples)
140141
h_powerset = powerset(values.keys())
141142
hypotheses = []
@@ -148,7 +149,7 @@ def all_hypotheses(examples):
148149

149150

150151
def values_table(examples):
151-
"""Builds a table with all the possible values for each attribute.
152+
"""Build a table with all the possible values for each attribute.
152153
Returns a dictionary with keys the attribute names and values a list
153154
with the possible values for the corresponding attribute."""
154155
values = defaultdict(lambda: [])
@@ -210,7 +211,7 @@ def build_h_combinations(hypotheses):
210211

211212

212213
def minimal_consistent_det(E, A):
213-
"""Returns a minimal set of attributes which give consistent determination"""
214+
"""Return a minimal set of attributes which give consistent determination"""
214215
n = len(A)
215216

216217
for i in range(n + 1):
@@ -220,7 +221,7 @@ def minimal_consistent_det(E, A):
220221

221222

222223
def consistent_det(A, E):
223-
"""Checks if the attributes(A) is consistent with the examples(E)"""
224+
"""Check if the attributes(A) is consistent with the examples(E)"""
224225
H = {}
225226

226227
for e in E:
@@ -235,9 +236,9 @@ def consistent_det(A, E):
235236

236237

237238
class FOIL_container(FolKB):
238-
"""Holds the kb and other necessary elements required by FOIL"""
239+
"""Hold the kb and other necessary elements required by FOIL."""
239240

240-
def __init__(self, clauses=[]):
241+
def __init__(self, clauses=None):
241242
self.const_syms = set()
242243
self.pred_syms = set()
243244
FolKB.__init__(self, clauses)
@@ -251,7 +252,7 @@ def tell(self, sentence):
251252
raise Exception("Not a definite clause: {}".format(sentence))
252253

253254
def foil(self, examples, target):
254-
"""Learns a list of first-order horn clauses
255+
"""Learn a list of first-order horn clauses
255256
'examples' is a tuple: (positive_examples, negative_examples).
256257
positive_examples and negative_examples are both lists which contain substitutions."""
257258
clauses = []
@@ -268,10 +269,10 @@ def foil(self, examples, target):
268269
return clauses
269270

270271
def new_clause(self, examples, target):
271-
"""Finds a horn clause which satisfies part of the positive
272+
"""Find a horn clause which satisfies part of the positive
272273
examples but none of the negative examples.
273274
The horn clause is specified as [consequent, list of antecedents]
274-
Return value is the tuple (horn_clause, extended_positive_examples)"""
275+
Return value is the tuple (horn_clause, extended_positive_examples)."""
275276
clause = [target, []]
276277
# [positive_examples, negative_examples]
277278
extended_examples = examples
@@ -284,14 +285,14 @@ def new_clause(self, examples, target):
284285
return (clause, extended_examples[0])
285286

286287
def extend_example(self, example, literal):
287-
"""Generates extended examples which satisfy the literal"""
288+
"""Generate extended examples which satisfy the literal."""
288289
# find all substitutions that satisfy literal
289290
for s in self.ask_generator(subst(example, literal)):
290291
s.update(example)
291292
yield s
292293

293294
def new_literals(self, clause):
294-
"""Generates new literals based on known predicate symbols.
295+
"""Generate new literals based on known predicate symbols.
295296
Generated literal must share atleast one variable with clause"""
296297
share_vars = variables(clause[0])
297298
for l in clause[1]:
@@ -304,7 +305,7 @@ def new_literals(self, clause):
304305
yield Expr(pred, *[var for var in args])
305306

306307
def choose_literal(self, literals, examples):
307-
"""Chooses the best literal based on the information gain"""
308+
"""Choose the best literal based on the information gain."""
308309
def gain(l):
309310
pre_pos = len(examples[0])
310311
pre_neg = len(examples[1])
@@ -328,8 +329,8 @@ def represents(d):
328329
return max(literals, key=gain)
329330

330331
def update_examples(self, target, examples, extended_examples):
331-
"""Adds to the kb those examples what are represented in extended_examples
332-
List of omitted examples is returned"""
332+
"""Add to the kb those examples what are represented in extended_examples
333+
List of omitted examples is returned."""
333334
uncovered = []
334335
for example in examples:
335336
def represents(d):
@@ -346,7 +347,7 @@ def represents(d):
346347

347348

348349
def check_all_consistency(examples, h):
349-
"""Check for the consistency of all examples under h"""
350+
"""Check for the consistency of all examples under h."""
350351
for e in examples:
351352
if not is_consistent(e, h):
352353
return False
@@ -355,7 +356,7 @@ def check_all_consistency(examples, h):
355356

356357

357358
def check_negative_consistency(examples, h):
358-
"""Check if the negative examples are consistent under h"""
359+
"""Check if the negative examples are consistent under h."""
359360
for e in examples:
360361
if e['GOAL']:
361362
continue
@@ -367,7 +368,7 @@ def check_negative_consistency(examples, h):
367368

368369

369370
def disjunction_value(e, d):
370-
"""The value of example e under disjunction d"""
371+
"""The value of example e under disjunction d."""
371372
for k, v in d.items():
372373
if v[0] == '!':
373374
# v is a NOT expression
@@ -381,7 +382,7 @@ def disjunction_value(e, d):
381382

382383

383384
def guess_value(e, h):
384-
"""Guess value of example e under hypothesis h"""
385+
"""Guess value of example e under hypothesis h."""
385386
for d in h:
386387
if disjunction_value(e, d):
387388
return True
@@ -394,16 +395,8 @@ def is_consistent(e, h):
394395

395396

396397
def false_positive(e, h):
397-
if e["GOAL"] == False:
398-
if guess_value(e, h):
399-
return True
400-
401-
return False
398+
return guess_value(e, h) and not e["GOAL"]
402399

403400

404401
def false_negative(e, h):
405-
if e["GOAL"] == True:
406-
if not guess_value(e, h):
407-
return True
408-
409-
return False
402+
return e["GOAL"] and not guess_value(e, h)

logic.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -901,10 +901,11 @@ class FolKB(KB):
901901
False
902902
"""
903903

904-
def __init__(self, initial_clauses=[]):
904+
def __init__(self, initial_clauses=None):
905905
self.clauses = [] # inefficient: no indexing
906-
for clause in initial_clauses:
907-
self.tell(clause)
906+
if initial_clauses:
907+
for clause in initial_clauses:
908+
self.tell(clause)
908909

909910
def tell(self, sentence):
910911
if is_definite_clause(sentence):

nlp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def __repr__(self):
272272
class Chart:
273273

274274
"""Class for parsing sentences using a chart data structure.
275-
>>> chart = Chart(E0);
275+
>>> chart = Chart(E0)
276276
>>> len(chart.parses('the stench is in 2 2'))
277277
1
278278
"""

0 commit comments

Comments
 (0)