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

Skip to content

Notebook updates #942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 2, 2018
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and
| 5.3 | Minimax-Decision | `minimax_decision` | [`games.py`][games] | Done | Included |
| 5.7 | Alpha-Beta-Search | `alphabeta_search` | [`games.py`][games] | Done | Included |
| 6 | CSP | `CSP` | [`csp.py`][csp] | Done | Included |
| 6.3 | AC-3 | `AC3` | [`csp.py`][csp] | Done | |
| 6.3 | AC-3 | `AC3` | [`csp.py`][csp] | Done | Included |
| 6.5 | Backtracking-Search | `backtracking_search` | [`csp.py`][csp] | Done | Included |
| 6.8 | Min-Conflicts | `min_conflicts` | [`csp.py`][csp] | Done | Included |
| 6.11 | Tree-CSP-Solver | `tree_csp_solver` | [`csp.py`][csp] | Done | Included |
Expand Down Expand Up @@ -118,7 +118,7 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and
| 11.8 | Angelic-Search | | | | |
| 11.10 | Doubles-tennis | `double_tennis_problem` | [`planning.py`][planning] | Done | Included |
| 13 | Discrete Probability Distribution | `ProbDist` | [`probability.py`][probability] | Done | Included |
| 13.1 | DT-Agent | `DTAgent` | [`probability.py`][probability] | | |
| 13.1 | DT-Agent | `DTAgent` | [`probability.py`][probability] | Done | Included |
| 14.9 | Enumeration-Ask | `enumeration_ask` | [`probability.py`][probability] | Done | Included |
| 14.11 | Elimination-Ask | `elimination_ask` | [`probability.py`][probability] | Done | Included |
| 14.13 | Prior-Sample | `prior_sample` | [`probability.py`][probability] | Done | Included |
Expand All @@ -133,7 +133,7 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and
| 17.7 | Policy-Iteration | `policy_iteration` | [`mdp.py`][mdp] | Done | Included |
| 17.9 | POMDP-Value-Iteration | `pomdp_value_iteration` | [`mdp.py`][mdp] | Done | Included |
| 18.5 | Decision-Tree-Learning | `DecisionTreeLearner` | [`learning.py`][learning] | Done | Included |
| 18.8 | Cross-Validation | `cross_validation` | [`learning.py`][learning] | | |
| 18.8 | Cross-Validation | `cross_validation` | [`learning.py`][learning]\* | | |
| 18.11 | Decision-List-Learning | `DecisionListLearner` | [`learning.py`][learning]\* | | |
| 18.24 | Back-Prop-Learning | `BackPropagationLearner` | [`learning.py`][learning] | Done | Included |
| 18.34 | AdaBoost | `AdaBoost` | [`learning.py`][learning] | Done | Included |
Expand Down
84 changes: 78 additions & 6 deletions agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,16 @@ def program(percept):


def RandomAgentProgram(actions):
"""An agent that chooses an action at random, ignoring all percepts."""
"""An agent that chooses an action at random, ignoring all percepts.
>>> list = ['Right', 'Left', 'Suck', 'NoOp']
>>> program = RandomAgentProgram(list)
>>> agent = Agent(program)
>>> environment = TrivialVacuumEnvironment()
>>> environment.add_thing(agent)
>>> environment.run()
>>> environment.status == {(1, 0): 'Clean' , (0, 0): 'Clean'}
True
"""
return lambda percept: random.choice(actions)

# ______________________________________________________________________________
Expand Down Expand Up @@ -171,7 +180,14 @@ def rule_match(state, rules):


def RandomVacuumAgent():
"""Randomly choose one of the actions from the vacuum environment."""
"""Randomly choose one of the actions from the vacuum environment.
>>> agent = RandomVacuumAgent()
>>> environment = TrivialVacuumEnvironment()
>>> environment.add_thing(agent)
>>> environment.run()
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
True
"""
return Agent(RandomAgentProgram(['Right', 'Left', 'Suck', 'NoOp']))


Expand All @@ -192,7 +208,14 @@ def TableDrivenVacuumAgent():


def ReflexVacuumAgent():
"""A reflex agent for the two-state vacuum environment. [Figure 2.8]"""
"""A reflex agent for the two-state vacuum environment. [Figure 2.8]
>>> agent = ReflexVacuumAgent()
>>> environment = TrivialVacuumEnvironment()
>>> environment.add_thing(agent)
>>> environment.run()
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
True
"""
def program(percept):
location, status = percept
if status == 'Dirty':
Expand All @@ -205,7 +228,14 @@ def program(percept):


def ModelBasedVacuumAgent():
"""An agent that keeps track of what locations are clean or dirty."""
"""An agent that keeps track of what locations are clean or dirty.
>>> agent = ModelBasedVacuumAgent()
>>> environment = TrivialVacuumEnvironment()
>>> environment.add_thing(agent)
>>> environment.run()
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
True
"""
model = {loc_A: None, loc_B: None}

def program(percept):
Expand Down Expand Up @@ -342,6 +372,22 @@ def __init__(self, direction):
self.direction = direction

def __add__(self, heading):
"""
>>> d = Direction('right')
>>> l1 = d.__add__(Direction.L)
>>> l2 = d.__add__(Direction.R)
>>> l1.direction
'up'
>>> l2.direction
'down'
>>> d = Direction('down')
>>> l1 = d.__add__('right')
>>> l2 = d.__add__('left')
>>> l1.direction == Direction.L
True
>>> l2.direction == Direction.R
True
"""
if self.direction == self.R:
return{
self.R: Direction(self.D),
Expand All @@ -364,6 +410,16 @@ def __add__(self, heading):
}.get(heading, None)

def move_forward(self, from_location):
"""
>>> d = Direction('up')
>>> l1 = d.move_forward((0, 0))
>>> l1
(0, -1)
>>> d = Direction(Direction.R)
>>> l1 = d.move_forward((0, 0))
>>> l1
(1, 0)
"""
x, y = from_location
if self.direction == self.R:
return (x + 1, y)
Expand Down Expand Up @@ -940,14 +996,30 @@ def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000):
"""See how well each of several agents do in n instances of an environment.
Pass in a factory (constructor) for environments, and several for agents.
Create n instances of the environment, and run each agent in copies of
each one for steps. Return a list of (agent, average-score) tuples."""
each one for steps. Return a list of (agent, average-score) tuples.
>>> environment = TrivialVacuumEnvironment
>>> agents = [ModelBasedVacuumAgent, ReflexVacuumAgent]
>>> result = compare_agents(environment, agents)
>>> performance_ModelBasedVacummAgent = result[0][1]
>>> performance_ReflexVacummAgent = result[1][1]
>>> performance_ReflexVacummAgent <= performance_ModelBasedVacummAgent
True
"""
envs = [EnvFactory() for i in range(n)]
return [(A, test_agent(A, steps, copy.deepcopy(envs)))
for A in AgentFactories]


def test_agent(AgentFactory, steps, envs):
"""Return the mean score of running an agent in each of the envs, for steps"""
"""Return the mean score of running an agent in each of the envs, for steps
>>> def constant_prog(percept):
... return percept
...
>>> agent = Agent(constant_prog)
>>> result = agent.program(5)
>>> result == 5
True
"""
def score(env):
agent = AgentFactory()
env.add_thing(agent)
Expand Down
Loading