Commit 5af7d2a5 authored by Steven Cordwell's avatar Steven Cordwell

[tests] Add some tests for util module

parent 9cdd25bb
......@@ -8,6 +8,8 @@ Created on Sat Aug 24 14:52:17 2013
import numpy as np
import scipy as sp
from nose.tools import assert_raises
import mdptoolbox
from .utils import ACTIONS, STATES
......@@ -18,14 +20,14 @@ def test_check_square_stochastic_nonnegative_array_1():
for a in range(ACTIONS):
P[a, :, :] = np.eye(STATES)
R[:, a] = np.random.rand(STATES)
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
def test_check_square_stochastic_nonnegative_array_2():
P = np.zeros((ACTIONS, STATES, STATES))
R = np.random.rand(ACTIONS, STATES, STATES)
for a in range(ACTIONS):
P[a, :, :] = np.eye(STATES)
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
# check: P - square, stochastic and non-negative object np.arrays
......@@ -34,21 +36,21 @@ def test_check_P_square_stochastic_nonnegative_object_array():
R = np.random.rand(STATES, ACTIONS)
for a in range(ACTIONS):
P[a] = np.eye(STATES)
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
def test_check_P_square_stochastic_nonnegative_object_matrix():
P = np.empty(ACTIONS, dtype=object)
R = np.random.rand(STATES, ACTIONS)
for a in range(ACTIONS):
P[a] = np.matrix(np.eye(STATES))
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
def test_check_P_square_stochastic_nonnegative_object_sparse():
P = np.empty(ACTIONS, dtype=object)
R = np.random.rand(STATES, ACTIONS)
for a in range(ACTIONS):
P[a] = sp.sparse.eye(STATES, STATES).tocsr()
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
# check: P - square, stochastic and non-negative lists
......@@ -57,21 +59,21 @@ def test_check_P_square_stochastic_nonnegative_list_array():
R = np.random.rand(STATES, ACTIONS)
for a in range(ACTIONS):
P.append(np.eye(STATES))
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
def test_check_P_square_stochastic_nonnegative_list_matrix():
P = []
R = np.random.rand(STATES, ACTIONS)
for a in range(ACTIONS):
P.append(np.matrix(np.eye(STATES)))
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
def test_check_P_square_stochastic_nonnegative_list_sparse():
P = []
R = np.random.rand(STATES, ACTIONS)
for a in range(ACTIONS):
P.append(sp.sparse.eye(STATES, STATES).tocsr())
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
# check: P - square, stochastic and non-negative dicts
......@@ -80,21 +82,21 @@ def test_check_P_square_stochastic_nonnegative_dict_array():
R = np.random.rand(STATES, ACTIONS)
for a in range(ACTIONS):
P[a] = np.eye(STATES)
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
def test_check_P_square_stochastic_nonnegative_dict_matrix():
P = {}
R = np.random.rand(STATES, ACTIONS)
for a in range(ACTIONS):
P[a] = np.matrix(np.eye(STATES))
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
def test_check_P_square_stochastic_nonnegative_dict_sparse():
P = {}
R = np.random.rand(STATES, ACTIONS)
for a in range(ACTIONS):
P[a] = sp.sparse.eye(STATES, STATES).tocsr()
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
# check: R - square stochastic and non-negative sparse
......@@ -103,7 +105,7 @@ def test_check_R_square_stochastic_nonnegative_sparse():
R = sp.sparse.csr_matrix(np.random.rand(STATES, ACTIONS))
for a in range(ACTIONS):
P[a, :, :] = np.eye(STATES)
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
# check: R - square, stochastic and non-negative object np.arrays
......@@ -113,7 +115,7 @@ def test_check_R_square_stochastic_nonnegative_object_array():
for a in range(ACTIONS):
P[a, :, :] = np.eye(STATES)
R[a] = np.random.rand(STATES, STATES)
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
def test_check_R_square_stochastic_nonnegative_object_matrix():
P = np.zeros((ACTIONS, STATES, STATES))
......@@ -121,7 +123,7 @@ def test_check_R_square_stochastic_nonnegative_object_matrix():
for a in range(ACTIONS):
P[a, :, :] = np.eye(STATES)
R[a] = np.matrix(np.random.rand(STATES, STATES))
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
def test_check_R_square_stochastic_nonnegative_object_sparse():
P = np.zeros((ACTIONS, STATES, STATES))
......@@ -129,7 +131,7 @@ def test_check_R_square_stochastic_nonnegative_object_sparse():
for a in range(ACTIONS):
P[a, :, :] = np.eye(STATES)
R[a] = sp.sparse.csr_matrix(np.random.rand(STATES, STATES))
assert (mdptoolbox.util.check(P, R) == None)
assert mdptoolbox.util.check(P, R) is None
# checkSquareStochastic: square, stochastic and non-negative
......@@ -137,32 +139,54 @@ def test_checkSquareStochastic_square_stochastic_nonnegative_array():
P = np.random.rand(STATES, STATES)
for s in range(STATES):
P[s, :] = P[s, :] / P[s, :].sum()
assert mdptoolbox.util.checkSquareStochastic(P) == None
assert mdptoolbox.util.checkSquareStochastic(P) is None
def test_checkSquareStochastic_square_stochastic_nonnegative_matrix():
P = np.random.rand(STATES, STATES)
for s in range(STATES):
P[s, :] = P[s, :] / P[s, :].sum()
P = np.matrix(P)
assert mdptoolbox.util.checkSquareStochastic(P) == None
assert mdptoolbox.util.checkSquareStochastic(P) is None
def test_checkSquareStochastic_square_stochastic_nonnegative_sparse():
P = np.random.rand(STATES, STATES)
for s in range(STATES):
P[s, :] = P[s, :] / P[s, :].sum()
P = sp.sparse.csr_matrix(P)
assert mdptoolbox.util.checkSquareStochastic(P) == None
assert mdptoolbox.util.checkSquareStochastic(P) is None
# checkSquareStochastic: eye
def test_checkSquareStochastic_eye_array():
P = np.eye(STATES)
assert mdptoolbox.util.checkSquareStochastic(P) == None
assert mdptoolbox.util.checkSquareStochastic(P) is None
def test_checkSquareStochastic_eye_matrix():
P = np.matrix(np.eye(STATES))
assert mdptoolbox.util.checkSquareStochastic(P) == None
assert mdptoolbox.util.checkSquareStochastic(P) is None
def test_checkSquareStochastic_eye_sparse():
P = sp.sparse.eye(STATES, STATES).tocsr()
assert mdptoolbox.util.checkSquareStochastic(P) == None
assert mdptoolbox.util.checkSquareStochastic(P) is None
def test_check_vector_R():
R = np.random.rand(STATES)
P = [np.matrix(np.eye(STATES))] * 3
assert mdptoolbox.util.check(P, R) is None
# Exception tests
def test_check_P_shape_error_1():
P = np.eye(STATES)[:STATES - 1, :STATES]
assert_raises(mdptoolbox.util.InvalidMDPError, mdptoolbox.util.check,
P=P, R=np.random.rand(10, 3))
def test_check_P_shape_error_2():
P = (np.random.rand(9, 9), np.random.rand(9, 9), np.random.rand(9, 5))
assert_raises(mdptoolbox.util.InvalidMDPError, mdptoolbox.util.check,
P=P, R=np.random.rand(9))
def test_check_R_shape_error_1():
R = (np.random.rand(9, 9), np.random.rand(9, 9), np.random.rand(9, 5))
P = np.random.rand(3, 10, 10)
assert_raises(mdptoolbox.util.InvalidMDPError, mdptoolbox.util.check,
P=P, R=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