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

Skip to content

Tests: RL.py #450

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 2 commits into from
Apr 17, 2017
Merged
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
55 changes: 55 additions & 0 deletions tests/test_rl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest

from rl import *
from mdp import sequential_decision_environment


north = (0, 1)
south = (0,-1)
west = (-1, 0)
east = (1, 0)

policy = {
(0, 2): east, (1, 2): east, (2, 2): east, (3, 2): None,
(0, 1): north, (2, 1): north, (3, 1): None,
(0, 0): north, (1, 0): west, (2, 0): west, (3, 0): west,
}



def test_PassiveADPAgent():
agent = PassiveADPAgent(policy, sequential_decision_environment)
for i in range(75):
run_single_trial(agent,sequential_decision_environment)

# Agent does not always produce same results.
# Check if results are good enough.
assert agent.U[(0, 0)] > 0.15 # In reality around 0.3
assert agent.U[(0, 1)] > 0.15 # In reality around 0.4
assert agent.U[(1, 0)] > 0 # In reality around 0.2



def test_PassiveTDAgent():
agent = PassiveTDAgent(policy, sequential_decision_environment, alpha=lambda n: 60./(59+n))
for i in range(200):
run_single_trial(agent,sequential_decision_environment)

# Agent does not always produce same results.
# Check if results are good enough.
assert agent.U[(0, 0)] > 0.15 # In reality around 0.3
assert agent.U[(0, 1)] > 0.15 # In reality around 0.35
assert agent.U[(1, 0)] > 0.15 # In reality around 0.25


def test_QLearning():
q_agent = QLearningAgent(sequential_decision_environment, Ne=5, Rplus=2,
alpha=lambda n: 60./(59+n))

for i in range(200):
run_single_trial(q_agent,sequential_decision_environment)

# Agent does not always produce same results.
# Check if results are good enough.
assert q_agent.Q[((0, 1), (0, 1))] >= -0.5 # In reality around 0.1
assert q_agent.Q[((1, 0), (0, -1))] <= 0.5 # In reality around -0.1