test_mdptoolbox.py 6.44 KB
Newer Older
Steven Cordwell's avatar
Steven Cordwell committed
1
2
3
4
5
6
7
# -*- coding: utf-8 -*-
"""
Created on Sun May 27 23:16:57 2012

@author: -
"""

Steven Cordwell's avatar
Steven Cordwell committed
8
9
10
from mdp import MDP
from numpy import array, eye, matrix, zeros
from numpy.random import rand
Steven Cordwell's avatar
Steven Cordwell committed
11
from scipy.sparse import eye as speye
Steven Cordwell's avatar
Steven Cordwell committed
12
from scipy.sparse import csr_matrix as sparse
Steven Cordwell's avatar
Steven Cordwell committed
13
14
15

inst = MDP()

Steven Cordwell's avatar
Steven Cordwell committed
16
17
STATES = 10
ACTIONS = 3
Steven Cordwell's avatar
Steven Cordwell committed
18

Steven Cordwell's avatar
Steven Cordwell committed
19
# check: square, stochastic and non-negative
Steven Cordwell's avatar
Steven Cordwell committed
20

Steven Cordwell's avatar
Steven Cordwell committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def test_check_square_stochastic_nonnegative_array():
    P = zeros((ACTIONS, STATES, STATES))
    R = zeros((STATES, ACTIONS))
    for a in range(ACTIONS):
        P[a, :, :] = eye(STATES)
        R[:, a] = rand(STATES)
    inst.check(P, R)

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

def test_check_square_stochastic_nonnegative_object_array():
    P = zeros((ACTIONS, ), dtype=object)
    R = zeros((STATES, ACTIONS))
    for a in range(ACTIONS):
        P[a] = eye(STATES)
        R[:, a] = rand(STATES)
    inst.check(P, R)

def test_check_square_stochastic_nonnegative_object_matrix():
    P = zeros((ACTIONS, ), dtype=object)
    R = zeros((STATES, ACTIONS))
    for a in range(ACTIONS):
        P[a] = matrix(eye(STATES))
        R[:, a] = rand(STATES)
    inst.check(P, R)

def test_check_square_stochastic_nonnegative_object_sparse():
    P = zeros((ACTIONS, ), dtype=object)
    R = zeros((STATES, ACTIONS))
    for a in range(ACTIONS):
        P[a] = speye(STATES, STATES).tocsr()
        R[:, a] = rand(STATES)
    inst.check(P, R)

# checkSquareStochastic: square, stochastic and non-negative

def test_checkSquareStochastic_square_stochastic_nonnegative_array():
    P = rand(STATES, STATES)
    for s in range(STATES):
        P[s, :] = P[s, :] / P[s, :].sum()
    assert inst.checkSquareStochastic(P) == None

def test_checkSquareStochastic_square_stochastic_nonnegative_matrix():
    P = rand(STATES, STATES)
    for s in range(STATES):
        P[s, :] = P[s, :] / P[s, :].sum()
    P = matrix(P)
Steven Cordwell's avatar
Steven Cordwell committed
68
69
    assert inst.checkSquareStochastic(P) == None

Steven Cordwell's avatar
Steven Cordwell committed
70
71
72
73
74
75
def test_checkSquareStochastic_square_stochastic_nonnegative_sparse():
    P = rand(STATES, STATES)
    for s in range(STATES):
        P[s, :] = P[s, :] / P[s, :].sum()
    P = sparse(P)
    assert inst.checkSquareStochastic(P) == None
Steven Cordwell's avatar
Steven Cordwell committed
76

Steven Cordwell's avatar
Steven Cordwell committed
77
# checkSquareStochastic: eye
Steven Cordwell's avatar
Steven Cordwell committed
78

Steven Cordwell's avatar
Steven Cordwell committed
79
80
81
def test_checkSquareStochastic_eye_array():
    P = eye(STATES)
    assert inst.checkSquareStochastic(P) == None
Steven Cordwell's avatar
Steven Cordwell committed
82

Steven Cordwell's avatar
Steven Cordwell committed
83
84
85
def test_checkSquareStochastic_eye_matrix():
    P = matrix(eye(STATES))
    assert inst.checkSquareStochastic(P) == None
Steven Cordwell's avatar
Steven Cordwell committed
86

Steven Cordwell's avatar
Steven Cordwell committed
87
88
89
def test_checkSquareStochastic_eye_sparse():
    P = speye(STATES, STATES).tocsr()
    assert inst.checkSquareStochastic(P) == None
Steven Cordwell's avatar
Steven Cordwell committed
90

Steven Cordwell's avatar
Steven Cordwell committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from mdp import exampleRand

def test_exampleRand_dense_shape():
    P, R = exampleRand(STATES, ACTIONS)
    assert (P.shape == (ACTIONS, STATES, STATES))
    assert (R.shape == (ACTIONS, STATES, STATES))

def test_exampleRand_dense_check():
    P, R = exampleRand(STATES, ACTIONS)
    inst.check(P, R)

def test_exampleRand_sparse_shape():
    P, R = exampleRand(STATES, ACTIONS, is_sparse=True)
    assert (P.shape == (ACTIONS, ))
    assert (R.shape == (ACTIONS, ))

def test_exampleRand_sparse_check():
    P, R = exampleRand(STATES, ACTIONS, is_sparse=True)
    inst.check(P, R)

from mdp import exampleForest

def test_exampleForest_shape():
    P, R = exampleForest()
    assert (P == 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()
    assert (R == array([[0, 0],
                        [0, 1],
                        [4, 2]])).all()

def test_exampleForest_check():
    P, R = exampleForest(10, 5, 3, 0.2)
    inst.check(P, R)

#inst = QLearning()

# checkSquareStochastic: not square, stochastic and non-negative

#@raises(ValueError(mdperr["mat_square"]))
#def test_checkSquareStochastic_notsquare_stochastic_nonnegative_array():
#    P = eye(STATES, STATES + 1)
#    inst.checkSquareStochastic(P)
#
#@raises(ValueError(mdperr["mat_square"]))
#def test_checkSquareStochastic_notsquare_stochastic_nonnegative_matrix():
#    P = matrix(eye(STATES, STATES + 1))
#    inst.checkSquareStochastic(P)
#
#@raises(ValueError(mdperr["mat_square"]))
#def test_checkSquareStochastic_notsquare_stochastic_nonnegative_sparse():
#    P = speye(STATES, STATES + 1).tocsr()
#    inst.checkSquareStochastic(P)

# checkSquareStochastic: square, not stochastic and non-negative
Steven Cordwell's avatar
Steven Cordwell committed
149
    
Steven Cordwell's avatar
Steven Cordwell committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#def test_checkSquareStochastic_square_notstochastic_nonnegative_array():
#    P = eye(STATES)
#    i = randint(STATES)
#    j = randint(STATES)
#    P[i, j] = P[i, j] + 1
#    try:
#        inst.checkSquareStochastic(P)
#    except ValueError(mdperr["mat_stoch"]):
#        pass
#
#def test_checkSquareStochastic_square_notstochastic_nonnegative_matrix():
#    P = matrix(eye(STATES))
#    i = randint(STATES)
#    j = randint(STATES)
#    P[i, j] = P[i, j] + 1
#    try:
#        inst.checkSquareStochastic(P)
#    except ValueError(mdperr["mat_stoch"]):
#        pass
#
#def test_checkSquareStochastic_square_notstochastic_nonnegative_sparse():
#    P = speye(STATES, STATES).tolil()
#    i = randint(STATES)
#    j = randint(STATES)
#    P[i, j] = P[i, j] + 1
#    P = P.tocsr()
#    try:
#        inst.checkSquareStochastic(P)
#    except ValueError(mdperr["mat_stoch"]):
#        pass

# checkSquareStochastic: square, stochastic and negative

#def test_checkSquareStochastic_square_stochastic_negative_array():
#    P = eye(STATES, STATES)
#    i = randint(STATES)
#    j = randint(STATES)
#    while j == i:
#        j = randint(STATES)
#    P[i, i] = -1
#    P[i, j] = 1
#    try:
#        inst.checkSquareStochastic(P)
#    except ValueError(mdperr["mat_nonneg"]):
#        pass
#
#def test_checkSquareStochastic_square_stochastic_negative_matrix():
#    P = matrix(eye(STATES, STATES))
#    i = randint(STATES)
#    j = randint(STATES)
#    while j == i:
#        j = randint(STATES)
#    P[i, i] = -1
#    P[i, j] = 1
#    try:
#        inst.checkSquareStochastic(P)
#    except ValueError(mdperr["mat_nonneg"]):
#        pass
#
#def test_checkSquareStochastic_square_stochastic_negative_sparse():
#    P = speye(STATES, STATES)
#    i = randint(STATES)
#    j = randint(STATES)
#    while j == i:
#        j = randint(STATES)
#    P[i, i] = -1
#    P[i, j] = 1
#    try:
#        inst.checkSquareStochastic(P)
#    except ValueError(mdperr["mat_nonneg"]):
#        pass

#def test_check_square_stochastic_array_Rtranspose():
#    P = array([eye(STATES), eye(STATES)])
#    R = array([ones(STATES), ones(STATES)])
#    assert inst.check(P, R) == (True, "R is wrong way")