Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and
| 9.3 | FOL-FC-Ask | `fol_fc_ask` | [`logic.py`][logic] | Done | |
| 9.6 | FOL-BC-Ask | `fol_bc_ask` | [`logic.py`][logic] | Done | |
| 9.8 | Append | | | | |
| 10.1 | Air-Cargo-problem | `air_cargo` | [`planning.py`][planning] | Done | |
| 10.1 | Air-Cargo-problem | `air_cargo` | [`planning.py`][planning] | Done | Included |
| 10.2 | Spare-Tire-Problem | `spare_tire` | [`planning.py`][planning] | Done | |
| 10.3 | Three-Block-Tower | `three_block_tower` | [`planning.py`][planning] | Done | |
| 10.7 | Cake-Problem | `have_cake_and_eat_cake_too` | [`planning.py`][planning] | Done | |
Expand Down
12 changes: 6 additions & 6 deletions 12 agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def program(percept):
self.program = program

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

Expand Down Expand Up @@ -444,7 +444,7 @@ def move_to(self, thing, destination):
return thing.bump

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

def get_world(self, show_walls=True):
"""Returns the items in the world"""
"""Return the items in the world"""
result = []
x_start, y_start = (0, 0) if show_walls else (1, 1)

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

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

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

def in_danger(self, agent):
"""Checks if Explorer is in danger (Pit or Wumpus), if he is, kill him"""
"""Check if Explorer is in danger (Pit or Wumpus), if he is, kill him"""
for thing in self.list_things_at(agent.location):
if isinstance(thing, Pit) or (isinstance(thing, Wumpus) and thing.alive):
agent.alive = False
Expand Down
579 changes: 557 additions & 22 deletions 579 csp.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions 4 csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def topological_sort(X, root):


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

for n in neighbors[node]:
Expand Down Expand Up @@ -427,7 +427,7 @@ def MapColoringCSP(colors, neighbors):
different_values_constraint)


def parse_neighbors(neighbors, variables=[]):
def parse_neighbors(neighbors, variables=None):
"""Convert a string of the form 'X: Y Z; Y: Z' into a dict mapping
regions to neighbors. The syntax is a region name followed by a ':'
followed by zero or more region names, followed by ';', repeated for
Expand Down
53 changes: 23 additions & 30 deletions 53 knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
# ______________________________________________________________________________


def current_best_learning(examples, h, examples_so_far=[]):
def current_best_learning(examples, h, examples_so_far=None):
""" [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 Expand Up @@ -95,7 +96,7 @@ def generalizations(examples_so_far, h):


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


def all_hypotheses(examples):
"""Builds a list of all the possible hypotheses"""
"""Build a list of all the possible hypotheses"""
values = values_table(examples)
h_powerset = powerset(values.keys())
hypotheses = []
Expand All @@ -148,7 +149,7 @@ def all_hypotheses(examples):


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


def minimal_consistent_det(E, A):
"""Returns a minimal set of attributes which give consistent determination"""
"""Return a minimal set of attributes which give consistent determination"""
n = len(A)

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


def consistent_det(A, E):
"""Checks if the attributes(A) is consistent with the examples(E)"""
"""Check if the attributes(A) is consistent with the examples(E)"""
H = {}

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


class FOIL_container(FolKB):
"""Holds the kb and other necessary elements required by FOIL"""
"""Hold the kb and other necessary elements required by FOIL."""

def __init__(self, clauses=[]):
def __init__(self, clauses=None):
self.const_syms = set()
self.pred_syms = set()
FolKB.__init__(self, clauses)
Expand All @@ -251,7 +252,7 @@ def tell(self, sentence):
raise Exception("Not a definite clause: {}".format(sentence))

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

def new_clause(self, examples, target):
"""Finds a horn clause which satisfies part of the positive
"""Find a horn clause which satisfies part of the positive
examples but none of the negative examples.
The horn clause is specified as [consequent, list of antecedents]
Return value is the tuple (horn_clause, extended_positive_examples)"""
Return value is the tuple (horn_clause, extended_positive_examples)."""
clause = [target, []]
# [positive_examples, negative_examples]
extended_examples = examples
Expand All @@ -284,14 +285,14 @@ def new_clause(self, examples, target):
return (clause, extended_examples[0])

def extend_example(self, example, literal):
"""Generates extended examples which satisfy the literal"""
"""Generate extended examples which satisfy the literal."""
# find all substitutions that satisfy literal
for s in self.ask_generator(subst(example, literal)):
s.update(example)
yield s

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

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

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


def check_all_consistency(examples, h):
"""Check for the consistency of all examples under h"""
"""Check for the consistency of all examples under h."""
for e in examples:
if not is_consistent(e, h):
return False
Expand All @@ -355,7 +356,7 @@ def check_all_consistency(examples, h):


def check_negative_consistency(examples, h):
"""Check if the negative examples are consistent under h"""
"""Check if the negative examples are consistent under h."""
for e in examples:
if e['GOAL']:
continue
Expand All @@ -367,7 +368,7 @@ def check_negative_consistency(examples, h):


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


def guess_value(e, h):
"""Guess value of example e under hypothesis h"""
"""Guess value of example e under hypothesis h."""
for d in h:
if disjunction_value(e, d):
return True
Expand All @@ -394,16 +395,8 @@ def is_consistent(e, h):


def false_positive(e, h):
if e["GOAL"] == False:
if guess_value(e, h):
return True

return False
return guess_value(e, h) and not e["GOAL"]


def false_negative(e, h):
if e["GOAL"] == True:
if not guess_value(e, h):
return True

return False
return e["GOAL"] and not guess_value(e, h)
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.