aichallenge-py / test_mdp.py

# -*- coding: utf-8 -*-
#
#  test_mdp.py
#  aichallenge-py
#
#  Created by Lars Yencken on 2011-11-13.
#  Copyright 2011 Lars Yencken. All rights reserved.
#

"""
Test cases for solving MDPs.
"""

import unittest

import numpy as np

import mdp

class MDPPlaneTest(unittest.TestCase):
    def setUp(self):
        pass

    #def test_single_row(self):
        #reward = np.array([0.0,0.0,0.0,8.0])
        #reward.reshape((1, 4))
        #value = mdp.value_iteration(reward, gamma=0.5, eps=1e-5)
        #expected = np.array([1.0, 2.0, 4.0, 8.0])
        #expected.reshape((1, 4))
        #assert abs(value - expected).mean() < 1e-5

    def test_square(self):
        reward = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]])
        expected = np.array(
                [[ 0.3333, 0.6666, 0.3333],
                [ 0.6666, 1.3333, 0.6666],
                [ 0.3333, 0.6666, 0.3333]]
            )
        value = mdp.value_iteration(reward, gamma=0.5, eps=1e-3)
        assert abs(value - expected).mean() < 1e-3, str(value)

class MDPTorusTest(unittest.TestCase):
    def test_square(self):
        reward = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]])
        expected = np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]],
                dtype=np.float32) / 3.0
        value = mdp.value_iteration(reward, torus=True, gamma=0.5, eps=1e-5)
        assert abs(value - expected).mean() < 1e-5, str(value)

    def test_wraparound(self):
        reward = np.array([[0, 1, 0], [0, 0, 0], [0, 0, 0]])
        expected = np.array([[2, 4, 2], [1, 2, 1], [1, 2, 1]],
                dtype=np.float32) / 3.0
        value = mdp.value_iteration(reward, torus=True, gamma=0.5, eps=1e-5)
        assert abs(value - expected).mean() < 1e-5, str(value)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.