Commit b2101b2f authored by Steven Cordwell's avatar Steven Cordwell

[tests] Change example module tests

Add to and improve the quality of the tests for the example module. This
commit also fixes a small bug in the example module when dealing with
sparse masks to the example.rand function.
parent 81b4d6d5
......@@ -299,7 +299,12 @@ def rand(S, A, is_sparse=False, mask=None):
if n == 0:
m[randint(0, S)] = 1
n = 1
cols = where(m)[0] # m[s, :]
# find the columns of the vector that have non-zero elements
nz = m.nonzero()
if len(nz) == 1:
cols = nz[0]
else:
cols = nz[1]
vals = random(n)
vals = vals / vals.sum()
reward = 2*random(n) - ones(n)
......@@ -330,7 +335,6 @@ def rand(S, A, is_sparse=False, mask=None):
# Make sure that there is atleast one transition in each state
if m.sum() == 0:
m[randint(0, S)] = 1
n = 1
P[a][s] = m * random(S)
P[a][s] = P[a][s] / P[a][s].sum()
R[a][s] = (m * (2*random(S) - ones(S, dtype=int)))
......
......@@ -5,50 +5,183 @@ Created on Sat Aug 24 14:55:05 2013
@author: steve
"""
from nose.tools import assert_equal, assert_is_none, assert_true, \
assert_raises
import numpy as np
import scipy.sparse as sp
import mdptoolbox.example
from .utils import ACTIONS, STATES, P_forest, R_forest, P_rand, R_rand
from .utils import P_rand_sparse, R_rand_sparse
## example.forest
def test_example_forest_P_shape():
assert (P_forest == np.array([[[0.1, 0.9, 0.0],
def assert_spacing_equal(A, B):
return(assert_true((np.abs(A - B) <= np.spacing(1)).all()))
class TestExampleForest(object):
P = np.array(
[[[0.1, 0.9, 0.0],
[0.1, 0.0, 0.9],
[0.1, 0.0, 0.9]],
[[1, 0, 0],
[1, 0, 0],
[1, 0, 0]]])).all()
def test_example_forest_R_shape():
assert (R_forest == np.array([[0, 0],
[1, 0, 0]]])
R = np.array(
[[0, 0],
[0, 1],
[4, 2]])).all()
[4, 2]])
def test_dense_PR(self):
P, R = mdptoolbox.example.forest()
assert_equal(P.shape, self.P.shape)
assert_equal(R.shape, self.R.shape)
assert_spacing_equal(P, self.P)
assert_spacing_equal(R, self.R)
def test_example_forest_check():
def test_sparse_PR(self):
P, R = mdptoolbox.example.forest(is_sparse=True)
assert_equal(len(P), len(self.P))
for a in range(len(self.P)):
assert_equal(P[a].shape, self.P[a].shape)
assert_equal((P[a] != sp.csr_matrix(self.P[a])).nnz, 0)
assert_true((R == self.R).all())
assert_equal(R.shape, self.R.shape)
def test_example_forest_dense_check():
P, R = mdptoolbox.example.forest(10, 5, 3, 0.2)
assert mdptoolbox.util.check(P, R) == None
assert_is_none(mdptoolbox.util.check(P, R))
def test_example_forest_sparse_check():
P, R = mdptoolbox.example.forest(S=30, is_sparse=True)
assert_is_none(mdptoolbox.util.check(P, R))
def test_example_forest_S_raise():
assert_raises(AssertionError, mdptoolbox.example.forest, S=0)
def test_example_forest_r1_raise():
assert_raises(AssertionError, mdptoolbox.example.forest, r1=0)
def test_example_forest_r1_raise():
assert_raises(AssertionError, mdptoolbox.example.forest, r2=0)
def test_example_forest_p_low_raise():
assert_raises(AssertionError, mdptoolbox.example.forest, p=-1)
def test_example_forest_p_high_raise():
assert_raises(AssertionError, mdptoolbox.example.forest, p=1.1)
# exampleRand
## example.rand
def test_example_rand_dense_P_shape():
assert (P_rand.shape == (ACTIONS, STATES, STATES))
class TestExampleRand(object):
S = 3
A = 2
P = np.array(
[[[0.28109922699468015, 0.4285572503528079, 0.2903435226525119],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0]],
[[0.4656280088928742, 0.21500769329533384, 0.31936429781179193],
[0.0, 0.0, 1.0],
[0.19806726878845474, 0.8019327312115453, 0.0]]])
R = np.array(
[[[0.7835460015641595, 0.9273255210020586, -0.2331169623484446],
[0.0, -0.7192984391747097, 0.0],
[0.0, -0.7881847856244157, -0.0]],
[[-0.22702203774827612, 0.8051969510588093, -0.10010002017754482],
[-0.0, -0.0, 0.14039354083575928],
[-0.06737845428738742, -0.5111488159967945, -0.0]]])
def test_example_rand_dense_R_shape():
assert (R_rand.shape == (ACTIONS, STATES, STATES))
def test_dense_PR(self):
np.random.seed(0)
P, R = mdptoolbox.example.rand(self.S, self.A)
assert_equal(P.shape, self.P.shape)
assert_equal(R.shape, self.R.shape)
assert_spacing_equal(P, self.P)
assert_spacing_equal(R, self.R)
def test_example_rand_dense_check():
assert mdptoolbox.util.check(P_rand, R_rand) == None
def test_sparse_PR(self):
P, R = mdptoolbox.example.rand(self.S, self.A, is_sparse=True)
for a in range(self.A):
assert_equal(P[a].shape, self.P[a].shape)
assert_equal(R[a].shape, self.R[a].shape)
def test_example_rand_sparse_P_shape():
assert (len(P_rand_sparse) == ACTIONS)
for a in range(ACTIONS):
assert (P_rand_sparse[a].shape == (STATES, STATES))
def test_dense_PR_check(self):
np.random.seed(0)
P, R = mdptoolbox.example.rand(self.S, self.A)
assert_is_none(mdptoolbox.util.check(P, R))
def test_example_rand_sparse_R_shape():
assert (len(R_rand_sparse) == ACTIONS)
for a in range(ACTIONS):
assert (R_rand_sparse[a].shape == (STATES, STATES))
def test_sparse_PR_check(self):
np.random.seed(0)
P, R = mdptoolbox.example.rand(self.S, self.A, is_sparse=True)
assert_is_none(mdptoolbox.util.check(P, R))
def test_S_raise(self):
assert_raises(AssertionError, mdptoolbox.example.rand, S=0, A=self.A)
def test_A_raise(self):
assert_raises(AssertionError, mdptoolbox.example.rand, S=self.S, A=0)
def test_mask_raise_1(self):
mask = np.random.randint(2, size=(3, 6, 9))
assert_raises(AssertionError, mdptoolbox.example.rand, S=self.S,
A=self.A, mask=mask)
def test_mask_raise_2(self):
assert_raises(TypeError, mdptoolbox.example.rand, S=self.S, A=self.A,
mask=True)
def test_mask_dense_1(self):
mask = np.array(
[[1, 0, 0],
[0, 1, 1],
[1, 0, 1]])
P, R = mdptoolbox.example.rand(S=self.S, A=self.A, mask=mask)
assert_is_none(mdptoolbox.util.check(P, R))
P, R = mdptoolbox.example.rand(S=self.S, A=self.A, mask=mask,
is_sparse=True)
assert_is_none(mdptoolbox.util.check(P, R))
def test_mask_dense_2(self):
mask = np.array(
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
P, R = mdptoolbox.example.rand(S=self.S, A=self.A, mask=mask)
assert_is_none(mdptoolbox.util.check(P, R))
P, R = mdptoolbox.example.rand(S=self.S, A=self.A, mask=mask,
is_sparse=True)
assert_is_none(mdptoolbox.util.check(P, R))
def test_mask_dense_3(self):
mask = np.array(
[[[1, 1, 0],
[0, 1, 1],
[1, 0, 1]],
[[0, 0, 1],
[0, 1, 0],
[1, 0, 0]]])
P, R = mdptoolbox.example.rand(S=self.S, A=self.A, mask=mask)
assert_is_none(mdptoolbox.util.check(P, R))
P, R = mdptoolbox.example.rand(S=self.S, A=self.A, mask=mask,
is_sparse=True)
assert_is_none(mdptoolbox.util.check(P, R))
def test_mask_sparse_1(self):
mask = sp.csr_matrix(
[[1, 0, 0],
[0, 1, 1],
[1, 0, 1]])
P, R = mdptoolbox.example.rand(S=self.S, A=self.A, mask=mask)
assert_is_none(mdptoolbox.util.check(P, R))
P, R = mdptoolbox.example.rand(S=self.S, A=self.A, mask=mask,
is_sparse=True)
assert_is_none(mdptoolbox.util.check(P, R))
def test_example_rand_sparse_check():
assert mdptoolbox.util.check(P_rand_sparse, R_rand_sparse) == None
def test_mask_sparse_2(self):
mask = np.array(
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
P, R = mdptoolbox.example.rand(S=self.S, A=self.A, mask=mask)
assert_is_none(mdptoolbox.util.check(P, R))
P, R = mdptoolbox.example.rand(S=self.S, A=self.A, mask=mask,
is_sparse=True)
assert_is_none(mdptoolbox.util.check(P, R))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment