test_utils.py 6.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 24 14:52:17 2013

@author: steve
"""

import numpy as np
import scipy as sp

11
12
from nose.tools import assert_raises

13
14
import mdptoolbox

Steven Cordwell's avatar
Steven Cordwell committed
15
from .utils import ACTIONS, STATES
16
17
18
19
20
21
22

def test_check_square_stochastic_nonnegative_array_1():
    P = np.zeros((ACTIONS, STATES, STATES))
    R = np.zeros((STATES, ACTIONS))
    for a in range(ACTIONS):
        P[a, :, :] = np.eye(STATES)
        R[:, a] = np.random.rand(STATES)
23
    assert mdptoolbox.util.check(P, R) is None
24
25
26
27
28
29

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)
30
    assert mdptoolbox.util.check(P, R) is None
31
32
33
34
35
36
37
38

# check: P - square, stochastic and non-negative object np.arrays

def test_check_P_square_stochastic_nonnegative_object_array():
    P = np.empty(ACTIONS, dtype=object)
    R = np.random.rand(STATES, ACTIONS)
    for a in range(ACTIONS):
        P[a] = np.eye(STATES)
39
    assert mdptoolbox.util.check(P, R) is None
40
41
42
43
44
45

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))
46
    assert mdptoolbox.util.check(P, R) is None
47
48
49
50
51
52

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()
53
    assert mdptoolbox.util.check(P, R) is None
54
55
56
57
58
59

# check: P - square, stochastic and non-negative lists

def test_check_P_square_stochastic_nonnegative_list_array():
    P = []
    R = np.random.rand(STATES, ACTIONS)
Steven Cordwell's avatar
Steven Cordwell committed
60
    for a in range(ACTIONS):
61
        P.append(np.eye(STATES))
62
    assert mdptoolbox.util.check(P, R) is None
63
64
65
66

def test_check_P_square_stochastic_nonnegative_list_matrix():
    P = []
    R = np.random.rand(STATES, ACTIONS)
Steven Cordwell's avatar
Steven Cordwell committed
67
    for a in range(ACTIONS):
68
        P.append(np.matrix(np.eye(STATES)))
69
    assert mdptoolbox.util.check(P, R) is None
70
71
72
73

def test_check_P_square_stochastic_nonnegative_list_sparse():
    P = []
    R = np.random.rand(STATES, ACTIONS)
Steven Cordwell's avatar
Steven Cordwell committed
74
    for a in range(ACTIONS):
75
        P.append(sp.sparse.eye(STATES, STATES).tocsr())
76
    assert mdptoolbox.util.check(P, R) is None
77
78
79
80
81
82

# check: P - square, stochastic and non-negative dicts

def test_check_P_square_stochastic_nonnegative_dict_array():
    P = {}
    R = np.random.rand(STATES, ACTIONS)
Steven Cordwell's avatar
Steven Cordwell committed
83
    for a in range(ACTIONS):
84
        P[a] = np.eye(STATES)
85
    assert mdptoolbox.util.check(P, R) is None
86
87
88
89

def test_check_P_square_stochastic_nonnegative_dict_matrix():
    P = {}
    R = np.random.rand(STATES, ACTIONS)
Steven Cordwell's avatar
Steven Cordwell committed
90
    for a in range(ACTIONS):
91
        P[a] = np.matrix(np.eye(STATES))
92
    assert mdptoolbox.util.check(P, R) is None
93
94
95
96

def test_check_P_square_stochastic_nonnegative_dict_sparse():
    P = {}
    R = np.random.rand(STATES, ACTIONS)
Steven Cordwell's avatar
Steven Cordwell committed
97
    for a in range(ACTIONS):
98
        P[a] = sp.sparse.eye(STATES, STATES).tocsr()
99
    assert mdptoolbox.util.check(P, R) is None
100
101
102
103
104
105
106
107

# check: R - square stochastic and non-negative sparse

def test_check_R_square_stochastic_nonnegative_sparse():
    P = np.zeros((ACTIONS, STATES, STATES))
    R = sp.sparse.csr_matrix(np.random.rand(STATES, ACTIONS))
    for a in range(ACTIONS):
        P[a, :, :] = np.eye(STATES)
108
    assert mdptoolbox.util.check(P, R) is None
109
110
111
112
113
114
115
116
117

# check: R - square, stochastic and non-negative object np.arrays

def test_check_R_square_stochastic_nonnegative_object_array():
    P = np.zeros((ACTIONS, STATES, STATES))
    R = np.empty(ACTIONS, dtype=object)
    for a in range(ACTIONS):
        P[a, :, :] = np.eye(STATES)
        R[a] = np.random.rand(STATES, STATES)
118
    assert mdptoolbox.util.check(P, R) is None
119
120
121
122
123
124
125

def test_check_R_square_stochastic_nonnegative_object_matrix():
    P = np.zeros((ACTIONS, STATES, STATES))
    R = np.empty(ACTIONS, dtype=object)
    for a in range(ACTIONS):
        P[a, :, :] = np.eye(STATES)
        R[a] = np.matrix(np.random.rand(STATES, STATES))
126
    assert mdptoolbox.util.check(P, R) is None
127
128
129
130
131
132
133

def test_check_R_square_stochastic_nonnegative_object_sparse():
    P = np.zeros((ACTIONS, STATES, STATES))
    R = np.empty(ACTIONS, dtype=object)
    for a in range(ACTIONS):
        P[a, :, :] = np.eye(STATES)
        R[a] = sp.sparse.csr_matrix(np.random.rand(STATES, STATES))
134
    assert mdptoolbox.util.check(P, R) is None
135
136
137
138
139
140
141

# checkSquareStochastic: square, stochastic and non-negative

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()
142
    assert mdptoolbox.util.checkSquareStochastic(P) is None
143
144
145
146
147
148

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)
149
    assert mdptoolbox.util.checkSquareStochastic(P) is None
150
151
152
153
154
155

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)
156
    assert mdptoolbox.util.checkSquareStochastic(P) is None
157
158
159
160
161

# checkSquareStochastic: eye

def test_checkSquareStochastic_eye_array():
    P = np.eye(STATES)
162
    assert mdptoolbox.util.checkSquareStochastic(P) is None
163
164
165

def test_checkSquareStochastic_eye_matrix():
    P = np.matrix(np.eye(STATES))
166
    assert mdptoolbox.util.checkSquareStochastic(P) is None
167
168
169

def test_checkSquareStochastic_eye_sparse():
    P = sp.sparse.eye(STATES, STATES).tocsr()
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    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)