Skip to content

Commit

Permalink
some optimizations in knowledge.py (aimacode#1034)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesagarsehgal authored and antmarakis committed Mar 3, 2019
1 parent 269786c commit 9f66fe6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
3 changes: 1 addition & 2 deletions knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
# ______________________________________________________________________________


def current_best_learning(examples, h, examples_so_far=None):
def current_best_learning(examples, h, examples_so_far=[]):
""" [Figure 19.2]
The hypothesis is a list of dictionaries, with each dictionary representing
a disjunction."""
if not examples:
return h

examples_so_far = examples_so_far or []
e = examples[0]
if is_consistent(e, h):
return current_best_learning(examples[1:], h, examples_so_far + [e])
Expand Down
2 changes: 1 addition & 1 deletion knowledge_current_best.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
"version": "3.6.7"
}
},
"nbformat": 4,
Expand Down
12 changes: 3 additions & 9 deletions tests/test_knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,21 @@ def test_current_best_learning():
examples = restaurant
hypothesis = [{'Alt': 'Yes'}]
h = current_best_learning(examples, hypothesis)
values = []
for e in examples:
values.append(guess_value(e, h))
values = [guess_value(e, h) for e in examples]

assert values == [True, False, True, True, False, True, False, True, False, False, False, True]

examples = animals_umbrellas
initial_h = [{'Species': 'Cat'}]
h = current_best_learning(examples, initial_h)
values = []
for e in examples:
values.append(guess_value(e, h))
values = [guess_value(e, h) for e in examples]

assert values == [True, True, True, False, False, False, True]

examples = party
initial_h = [{'Pizza': 'Yes'}]
h = current_best_learning(examples, initial_h)
values = []
for e in examples:
values.append(guess_value(e, h))
values = [guess_value(e, h) for e in examples]

assert values == [True, True, False]

Expand Down

0 comments on commit 9f66fe6

Please sign in to comment.