Skip to content

Commit

Permalink
fixed typos (aimacode#1118)
Browse files Browse the repository at this point in the history
* changed queue to set in AC3

Changed queue to set in AC3 (as in the pseudocode of the original algorithm) to reduce the number of consistency-check due to the redundancy of the same arcs in queue. For example, on the harder1 configuration of the Sudoku CSP the number consistency-check has been reduced from 40464 to 12562!

* re-added test commented by mistake

* added the mentioned AC4 algorithm for constraint propagation

AC3 algorithm has non-optimal worst case time-complexity O(cd^3 ), while AC4 algorithm runs in O(cd^2) worst case time

* added doctest in Sudoku for AC4 and and the possibility of choosing the constant propagation algorithm in mac inference

* removed useless doctest for AC4 in Sudoku because AC4's tests are already present in test_csp.py

* added map coloring SAT problems

* fixed typo errors and removed unnecessary brackets

* reformulated the map coloring problem

* Revert "reformulated the map coloring problem"

This reverts commit 20ab0e5.

* Revert "fixed typo errors and removed unnecessary brackets"

This reverts commit f743146.

* Revert "added map coloring SAT problems"

This reverts commit 9e0fa55.

* Revert "removed useless doctest for AC4 in Sudoku because AC4's tests are already present in test_csp.py"

This reverts commit b3cd24c.

* Revert "added doctest in Sudoku for AC4 and and the possibility of choosing the constant propagation algorithm in mac inference"

This reverts commit 6986247.

* Revert "added the mentioned AC4 algorithm for constraint propagation"

This reverts commit 03551fb.

* added map coloring SAT problem

* fixed build error

* Revert "added map coloring SAT problem"

This reverts commit 93af259.

* Revert "fixed build error"

This reverts commit 6641c2c.

* added map coloring SAT problem

* removed redundant parentheses

* added Viterbi algorithm

* added monkey & bananas planning problem

* simplified condition in search.py

* added tests for monkey & bananas planning problem

* removed monkey & bananas planning problem

* Revert "removed monkey & bananas planning problem"

This reverts commit 9d37ae0.

* Revert "added tests for monkey & bananas planning problem"

This reverts commit 24041e9.

* Revert "simplified condition in search.py"

This reverts commit 6d229ce.

* Revert "added monkey & bananas planning problem"

This reverts commit c74933a.

* defined the PlanningProblem as a specialization of a search.Problem & fixed typo errors

* fixed doctest in logic.py

* fixed doctest for cascade_distribution

* added ForwardPlanner and tests

* added __lt__ implementation for Expr

* added more tests

* renamed forward planner

* Revert "renamed forward planner"

This reverts commit c4139e5.

* renamed forward planner class & added doc

* added backward planner and tests

* fixed mdp4e.py doctests

* removed ignore_delete_lists_heuristic flag

* fixed heuristic for forward and backward planners

* added SATPlan and tests

* fixed ignore delete lists heuristic in forward and backward planners

* fixed backward planner and added tests

* updated doc

* added nary csp definition and examples

* added CSPlan and tests

* fixed CSPlan

* added book's cryptarithmetic puzzle example

* fixed typo errors in test_csp

* fixed aimacode#1111

* added sortedcontainers to yml and doc to CSPlan

* added tests for n-ary csp

* fixed utils.extend

* updated test_probability.py

* converted static methods to functions

* added AC3b and AC4 with heuristic and tests

* added conflict-driven clause learning sat solver

* added tests for cdcl and heuristics

* fixed probability.py

* fixed import

* fixed kakuro

* added Martelli and Montanari rule-based unification algorithm

* removed duplicate standardize_variables

* renamed variables known as built-in functions

* fixed typos in learning.py

* renamed some files and fixed typos

* fixed typos

* fixed typos

* fixed tests

* removed unify_mm

* remove unnecessary brackets

* fixed tests

* moved utility functions to utils.py
  • Loading branch information
dmeoli authored and antmarakis committed Sep 29, 2019
1 parent 255a160 commit 9fe0696
Show file tree
Hide file tree
Showing 50 changed files with 1,856 additions and 1,613 deletions.
63 changes: 41 additions & 22 deletions agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ def new_program(percept):
action = old_program(percept)
print('{} perceives {} and does {}'.format(agent, percept, action))
return action

agent.program = new_program
return agent


# ______________________________________________________________________________


Expand All @@ -130,6 +132,7 @@ def program(percept):
percepts.append(percept)
action = table.get(tuple(percepts))
return action

return program


Expand All @@ -146,26 +149,31 @@ def RandomAgentProgram(actions):
"""
return lambda percept: random.choice(actions)


# ______________________________________________________________________________


def SimpleReflexAgentProgram(rules, interpret_input):
"""This agent takes action based solely on the percept. [Figure 2.10]"""

def program(percept):
state = interpret_input(percept)
rule = rule_match(state, rules)
action = rule.action
return action

return program


def ModelBasedReflexAgentProgram(rules, update_state, model):
"""This agent takes action based on the percept and state. [Figure 2.12]"""

def program(percept):
program.state = update_state(program.state, program.action, percept, model)
rule = rule_match(program.state, rules)
action = rule.action
return action

program.state = program.action = None
return program

Expand All @@ -176,6 +184,7 @@ def rule_match(state, rules):
if rule.matches(state):
return rule


# ______________________________________________________________________________


Expand Down Expand Up @@ -205,8 +214,7 @@ def TableDrivenVacuumAgent():
((loc_B, 'Clean'), (loc_A, 'Dirty')): 'Suck',
((loc_B, 'Dirty'), (loc_B, 'Clean')): 'Left',
((loc_A, 'Dirty'), (loc_A, 'Clean'), (loc_B, 'Dirty')): 'Suck',
((loc_B, 'Dirty'), (loc_B, 'Clean'), (loc_A, 'Dirty')): 'Suck'
}
((loc_B, 'Dirty'), (loc_B, 'Clean'), (loc_A, 'Dirty')): 'Suck'}
return Agent(TableDrivenAgentProgram(table))


Expand All @@ -219,6 +227,7 @@ def ReflexVacuumAgent():
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
True
"""

def program(percept):
location, status = percept
if status == 'Dirty':
Expand All @@ -227,6 +236,7 @@ def program(percept):
return 'Right'
elif location == loc_B:
return 'Left'

return Agent(program)


Expand All @@ -253,8 +263,10 @@ def program(percept):
return 'Right'
elif location == loc_B:
return 'Left'

return Agent(program)


# ______________________________________________________________________________


Expand Down Expand Up @@ -392,22 +404,22 @@ def __add__(self, heading):
True
"""
if self.direction == self.R:
return{
return {
self.R: Direction(self.D),
self.L: Direction(self.U),
}.get(heading, None)
elif self.direction == self.L:
return{
return {
self.R: Direction(self.U),
self.L: Direction(self.D),
}.get(heading, None)
elif self.direction == self.U:
return{
return {
self.R: Direction(self.R),
self.L: Direction(self.L),
}.get(heading, None)
elif self.direction == self.D:
return{
return {
self.R: Direction(self.L),
self.L: Direction(self.R),
}.get(heading, None)
Expand Down Expand Up @@ -462,7 +474,7 @@ def things_near(self, location, radius=None):
radius2 = radius * radius
return [(thing, radius2 - distance_squared(location, thing.location))
for thing in self.things if distance_squared(
location, thing.location) <= radius2]
location, thing.location) <= radius2]

def percept(self, agent):
"""By default, agent perceives things within a default radius."""
Expand All @@ -476,11 +488,11 @@ def execute_action(self, agent, action):
agent.direction += Direction.L
elif action == 'Forward':
agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))
# elif action == 'Grab':
# things = [thing for thing in self.list_things_at(agent.location)
# if agent.can_grab(thing)]
# if things:
# agent.holding.append(things[0])
# elif action == 'Grab':
# things = [thing for thing in self.list_things_at(agent.location)
# if agent.can_grab(thing)]
# if things:
# agent.holding.append(things[0])
elif action == 'Release':
if agent.holding:
agent.holding.pop()
Expand All @@ -505,7 +517,7 @@ def move_to(self, thing, destination):
def add_thing(self, thing, location=(1, 1), exclude_duplicate_class_items=False):
"""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 self.is_inbounds(location):
if (exclude_duplicate_class_items and
any(isinstance(t, thing.__class__) for t in self.list_things_at(location))):
return
Expand All @@ -521,7 +533,7 @@ def random_location_inbounds(self, exclude=None):
location = (random.randint(self.x_start, self.x_end),
random.randint(self.y_start, self.y_end))
if exclude is not None:
while(location == exclude):
while location == exclude:
location = (random.randint(self.x_start, self.x_end),
random.randint(self.y_start, self.y_end))
return location
Expand All @@ -543,7 +555,7 @@ def add_walls(self):
for x in range(self.width):
self.add_thing(Wall(), (x, 0))
self.add_thing(Wall(), (x, self.height - 1))
for y in range(1, self.height-1):
for y in range(1, self.height - 1):
self.add_thing(Wall(), (0, y))
self.add_thing(Wall(), (self.width - 1, y))

Expand Down Expand Up @@ -574,6 +586,7 @@ class Obstacle(Thing):
class Wall(Obstacle):
pass


# ______________________________________________________________________________


Expand Down Expand Up @@ -682,6 +695,7 @@ def __init__(self, coordinates):
super().__init__()
self.coordinates = coordinates


# ______________________________________________________________________________
# Vacuum environment

Expand All @@ -691,7 +705,6 @@ class Dirt(Thing):


class VacuumEnvironment(XYEnvironment):

"""The environment of [Ex. 2.12]. Agent perceives dirty or clean,
and bump (into obstacle) or not; 2D discrete world of unknown size;
performance measure is 100 for each dirt cleaned, and -1 for
Expand All @@ -710,7 +723,7 @@ def percept(self, agent):
Unlike the TrivialVacuumEnvironment, location is NOT perceived."""
status = ('Dirty' if self.some_things_at(
agent.location, Dirt) else 'Clean')
bump = ('Bump' if agent.bump else'None')
bump = ('Bump' if agent.bump else 'None')
return (status, bump)

def execute_action(self, agent, action):
Expand All @@ -729,7 +742,6 @@ def execute_action(self, agent, action):


class TrivialVacuumEnvironment(Environment):

"""This environment has two locations, A and B. Each can be Dirty
or Clean. The agent perceives its location and the location's
status. This serves as an example of how to implement a simple
Expand Down Expand Up @@ -766,6 +778,7 @@ def default_location(self, thing):
"""Agents start in either location at random."""
return random.choice([loc_A, loc_B])


# ______________________________________________________________________________
# The Wumpus World

Expand All @@ -775,6 +788,7 @@ class Gold(Thing):
def __eq__(self, rhs):
"""All Gold are equal"""
return rhs.__class__ == Gold

pass


Expand Down Expand Up @@ -824,6 +838,7 @@ def can_grab(self, thing):

class WumpusEnvironment(XYEnvironment):
pit_probability = 0.2 # Probability to spawn a pit in a location. (From Chapter 7.2)

# Room should be 4x4 grid of rooms. The extra 2 for walls

def __init__(self, agent_program, width=6, height=6):
Expand Down Expand Up @@ -949,7 +964,7 @@ def execute_action(self, agent, action):
"""The arrow travels straight down the path the agent is facing"""
if agent.has_arrow:
arrow_travel = agent.direction.move_forward(agent.location)
while(self.is_inbounds(arrow_travel)):
while self.is_inbounds(arrow_travel):
wumpus = [thing for thing in self.list_things_at(arrow_travel)
if isinstance(thing, Wumpus)]
if len(wumpus):
Expand Down Expand Up @@ -979,12 +994,13 @@ def is_done(self):
print("Death by {} [-1000].".format(explorer[0].killed_by))
else:
print("Explorer climbed out {}."
.format(
"with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"))
.format(
"with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"))
return True


# TODO: Arrow needs to be implemented


# ______________________________________________________________________________


Expand Down Expand Up @@ -1016,13 +1032,16 @@ def test_agent(AgentFactory, steps, envs):
>>> result == 5
True
"""

def score(env):
agent = AgentFactory()
env.add_thing(agent)
env.run(steps)
return agent.performance

return mean(map(score, envs))


# _________________________________________________________________________


Expand Down
Loading

0 comments on commit 9fe0696

Please sign in to comment.