test_mdptoolbox.py 17.5 KB
Newer Older
Steven Cordwell's avatar
Steven Cordwell committed
1
# -*- coding: utf-8 -*-
2
3
4
5
6
7
8
9
"""The Python Markov Decision Process (MDP) Toolbox Test Suite
===========================================================

These unit tests are written for the nosetests framwork. You will need to have
nosetests installed, and then run from the command line.

    $ cd /path/to/pymdptoolbox
    $ nostests
Steven Cordwell's avatar
Steven Cordwell committed
10
11
12

"""

13
#from random import seed as randseed
Steven Cordwell's avatar
Steven Cordwell committed
14

15
from numpy import absolute, array, empty, eye, matrix, zeros
Steven Cordwell's avatar
Steven Cordwell committed
16
17
18
from numpy.random import rand
from scipy.sparse import eye as speye
from scipy.sparse import csr_matrix as sparse
Steven Cordwell's avatar
Steven Cordwell committed
19
#from scipy.stats.distributions import poisson
Steven Cordwell's avatar
Steven Cordwell committed
20

21
import mdp
Steven Cordwell's avatar
Steven Cordwell committed
22

Steven Cordwell's avatar
Steven Cordwell committed
23
24
STATES = 10
ACTIONS = 3
25
SMALLNUM = 10e-12
Steven Cordwell's avatar
Steven Cordwell committed
26

27
28
29
# Arrays
P = array([[[0.5, 0.5],[0.8, 0.2]],[[0, 1],[0.1, 0.9]]])
R = array([[5, 10], [-1, 2]])
30
31
32
Ps = empty(2, dtype=object)
Ps[0] = sparse([[0.5, 0.5],[0.8, 0.2]])
Ps[1] = sparse([[0, 1],[0.1, 0.9]])
33
34
35
Pf, Rf = mdp.exampleForest()
Pr, Rr = mdp.exampleRand(STATES, ACTIONS)
Prs, Rrs = mdp.exampleRand(STATES, ACTIONS, is_sparse=True)
36

Steven Cordwell's avatar
Steven Cordwell committed
37
38
39
40
41
42
43
44
# check: square, stochastic and non-negative ndarrays

def test_check_square_stochastic_nonnegative_array_1():
    P = zeros((ACTIONS, STATES, STATES))
    R = zeros((STATES, ACTIONS))
    for a in range(ACTIONS):
        P[a, :, :] = eye(STATES)
        R[:, a] = rand(STATES)
45
    assert (mdp.check(P, R) == None)
Steven Cordwell's avatar
Steven Cordwell committed
46
47
48
49
50
51

def test_check_square_stochastic_nonnegative_array_2():
    P = zeros((ACTIONS, STATES, STATES))
    R = rand(ACTIONS, STATES, STATES)
    for a in range(ACTIONS):
        P[a, :, :] = eye(STATES)
52
    assert (mdp.check(P, R) == None)
Steven Cordwell's avatar
Steven Cordwell committed
53
54
55
56

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

def test_check_P_square_stochastic_nonnegative_object_array():
57
    P = empty(ACTIONS, dtype=object)
Steven Cordwell's avatar
Steven Cordwell committed
58
59
60
    R = rand(STATES, ACTIONS)
    for a in range(ACTIONS):
        P[a] = eye(STATES)
61
    assert (mdp.check(P, R) == None)
Steven Cordwell's avatar
Steven Cordwell committed
62
63

def test_check_P_square_stochastic_nonnegative_object_matrix():
64
    P = empty(ACTIONS, dtype=object)
Steven Cordwell's avatar
Steven Cordwell committed
65
66
67
    R = rand(STATES, ACTIONS)
    for a in range(ACTIONS):
        P[a] = matrix(eye(STATES))
68
    assert (mdp.check(P, R) == None)
Steven Cordwell's avatar
Steven Cordwell committed
69
70

def test_check_P_square_stochastic_nonnegative_object_sparse():
71
    P = empty(ACTIONS, dtype=object)
Steven Cordwell's avatar
Steven Cordwell committed
72
73
74
    R = rand(STATES, ACTIONS)
    for a in range(ACTIONS):
        P[a] = speye(STATES, STATES).tocsr()
75
    assert (mdp.check(P, R) == None)
Steven Cordwell's avatar
Steven Cordwell committed
76

77
78
79
80
81
82
83
# check: P - square, stochastic and non-negative lists

def test_check_P_square_stochastic_nonnegative_list_array():
    P = []
    R = rand(STATES, ACTIONS)
    for a in xrange(ACTIONS):
        P.append(eye(STATES))
84
    assert (mdp.check(P, R) == None)
85

86
87
88
89
90
def test_check_P_square_stochastic_nonnegative_list_matrix():
    P = []
    R = rand(STATES, ACTIONS)
    for a in xrange(ACTIONS):
        P.append(matrix(eye(STATES)))
91
    assert (mdp.check(P, R) == None)
92
93
94
95
96
97

def test_check_P_square_stochastic_nonnegative_list_sparse():
    P = []
    R = rand(STATES, ACTIONS)
    for a in xrange(ACTIONS):
        P.append(speye(STATES, STATES).tocsr())
98
    assert (mdp.check(P, R) == None)
99

100
101
102
103
104
105
106
# check: P - square, stochastic and non-negative dicts

def test_check_P_square_stochastic_nonnegative_dict_array():
    P = {}
    R = rand(STATES, ACTIONS)
    for a in xrange(ACTIONS):
        P[a] = eye(STATES)
107
    assert (mdp.check(P, R) == None)
108
109

def test_check_P_square_stochastic_nonnegative_dict_matrix():
Steven Cordwell's avatar
Steven Cordwell committed
110
    P = {}
111
112
113
    R = rand(STATES, ACTIONS)
    for a in xrange(ACTIONS):
        P[a] = matrix(eye(STATES))
114
    assert (mdp.check(P, R) == None)
115
116

def test_check_P_square_stochastic_nonnegative_dict_sparse():
Steven Cordwell's avatar
Steven Cordwell committed
117
    P = {}
118
119
120
    R = rand(STATES, ACTIONS)
    for a in xrange(ACTIONS):
        P[a] = speye(STATES, STATES).tocsr()
121
    assert (mdp.check(P, R) == None)
122

Steven Cordwell's avatar
Steven Cordwell committed
123
124
125
126
127
128
129
# check: R - square stochastic and non-negative sparse

def test_check_R_square_stochastic_nonnegative_sparse():
    P = zeros((ACTIONS, STATES, STATES))
    R = sparse(rand(STATES, ACTIONS))
    for a in range(ACTIONS):
        P[a, :, :] = eye(STATES)
130
    assert (mdp.check(P, R) == None)
Steven Cordwell's avatar
Steven Cordwell committed
131
132
133
134
135

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

def test_check_R_square_stochastic_nonnegative_object_array():
    P = zeros((ACTIONS, STATES, STATES))
136
    R = empty(ACTIONS, dtype=object)
Steven Cordwell's avatar
Steven Cordwell committed
137
138
139
    for a in range(ACTIONS):
        P[a, :, :] = eye(STATES)
        R[a] = rand(STATES, STATES)
140
    assert (mdp.check(P, R) == None)
Steven Cordwell's avatar
Steven Cordwell committed
141
142
143

def test_check_R_square_stochastic_nonnegative_object_matrix():
    P = zeros((ACTIONS, STATES, STATES))
144
    R = empty(ACTIONS, dtype=object)
Steven Cordwell's avatar
Steven Cordwell committed
145
146
147
    for a in range(ACTIONS):
        P[a, :, :] = eye(STATES)
        R[a] = matrix(rand(STATES, STATES))
148
    assert (mdp.check(P, R) == None)
Steven Cordwell's avatar
Steven Cordwell committed
149
150
151

def test_check_R_square_stochastic_nonnegative_object_sparse():
    P = zeros((ACTIONS, STATES, STATES))
152
    R = empty(ACTIONS, dtype=object)
Steven Cordwell's avatar
Steven Cordwell committed
153
154
155
    for a in range(ACTIONS):
        P[a, :, :] = eye(STATES)
        R[a] = sparse(rand(STATES, STATES))
156
    assert (mdp.check(P, R) == None)
Steven Cordwell's avatar
Steven Cordwell committed
157
158
159
160
161
162
163

# 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()
164
    assert mdp.checkSquareStochastic(P) == None
Steven Cordwell's avatar
Steven Cordwell committed
165
166
167
168
169
170

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)
171
    assert mdp.checkSquareStochastic(P) == None
Steven Cordwell's avatar
Steven Cordwell committed
172
173
174
175
176
177

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)
178
    assert mdp.checkSquareStochastic(P) == None
Steven Cordwell's avatar
Steven Cordwell committed
179
180
181
182
183

# checkSquareStochastic: eye

def test_checkSquareStochastic_eye_array():
    P = eye(STATES)
184
    assert mdp.checkSquareStochastic(P) == None
Steven Cordwell's avatar
Steven Cordwell committed
185
186
187

def test_checkSquareStochastic_eye_matrix():
    P = matrix(eye(STATES))
188
    assert mdp.checkSquareStochastic(P) == None
Steven Cordwell's avatar
Steven Cordwell committed
189
190
191

def test_checkSquareStochastic_eye_sparse():
    P = speye(STATES, STATES).tocsr()
192
    assert mdp.checkSquareStochastic(P) == None
Steven Cordwell's avatar
Steven Cordwell committed
193

Steven Cordwell's avatar
Steven Cordwell committed
194
195
# exampleForest

196
197
def test_exampleForest_P_shape():
    assert (Pf == array([[[0.1, 0.9, 0.0],
Steven Cordwell's avatar
Steven Cordwell committed
198
199
200
201
202
                         [0.1, 0.0, 0.9],
                         [0.1, 0.0, 0.9]],
                        [[1, 0, 0],
                         [1, 0, 0],
                         [1, 0, 0]]])).all()
203
204
205

def test_exampleForest_R_shape():
    assert (Rf == array([[0, 0],
Steven Cordwell's avatar
Steven Cordwell committed
206
207
208
                        [0, 1],
                        [4, 2]])).all()

Steven Cordwell's avatar
Steven Cordwell committed
209
def test_exampleForest_check():
210
211
    P, R = mdp.exampleForest(10, 5, 3, 0.2)
    assert mdp.check(P, R) == None
Steven Cordwell's avatar
Steven Cordwell committed
212
213

# exampleRand
Steven Cordwell's avatar
Steven Cordwell committed
214

215
def test_exampleRand_dense_P_shape():
216
    assert (Pr.shape == (ACTIONS, STATES, STATES))
217
218

def test_exampleRand_dense_R_shape():
219
    assert (Rr.shape == (ACTIONS, STATES, STATES))
Steven Cordwell's avatar
Steven Cordwell committed
220

Steven Cordwell's avatar
Steven Cordwell committed
221
def test_exampleRand_dense_check():
222
    assert mdp.check(Pr, Rr) == None
Steven Cordwell's avatar
Steven Cordwell committed
223

224
def test_exampleRand_sparse_P_shape():
225
    assert (len(Prs) == ACTIONS)
226
227

def test_exampleRand_sparse_R_shape():
228
    assert (len(Rrs) == ACTIONS)
Steven Cordwell's avatar
Steven Cordwell committed
229

Steven Cordwell's avatar
Steven Cordwell committed
230
def test_exampleRand_sparse_check():
231
    assert mdp.check(Prs, Rrs) == None
Steven Cordwell's avatar
Steven Cordwell committed
232
233
234
235

# MDP

def test_MDP_P_R_1():
236
    P1 = []
237
238
    P1.append(array(matrix('0.5 0.5; 0.8 0.2')))
    P1.append(array(matrix('0 1; 0.1 0.9')))
239
240
    P1 = tuple(P1)
    R1 = []
241
242
    R1.append(array(matrix('5, -1')))
    R1.append(array(matrix('10, 2')))
243
    R1 = tuple(R1)
244
    a = mdp.MDP(P, R, 0.9, 0.01, 1)
245
246
    assert type(a.P) == type(P1)
    assert type(a.R) == type(R1)
Steven Cordwell's avatar
Steven Cordwell committed
247
248
    for kk in range(2):
        assert (a.P[kk] == P1[kk]).all()
249
        assert (a.R[kk] == R1[kk]).all()
Steven Cordwell's avatar
Steven Cordwell committed
250
251
252

def test_MDP_P_R_2():
    R = array([[[5, 10], [-1, 2]], [[1, 2], [3, 4]]])
253
    P1 = []
254
255
    P1.append(array(matrix('0.5 0.5; 0.8 0.2')))
    P1.append(array(matrix('0 1; 0.1 0.9')))
256
257
    P1 = tuple(P1)
    R1 = []
258
259
    R1.append(array(matrix('7.5, -0.4')))
    R1.append(array(matrix('2, 3.9')))
260
    R1 = tuple(R1)
261
    a = mdp.MDP(P, R, 0.9, 0.01, 1)
262
263
    assert type(a.P) == type(P1)
    assert type(a.R) == type(R1)
Steven Cordwell's avatar
Steven Cordwell committed
264
265
    for kk in range(2):
        assert (a.P[kk] == P1[kk]).all()
266
        assert (absolute(a.R[kk] - R1[kk]) < SMALLNUM).all()
Steven Cordwell's avatar
Steven Cordwell committed
267
268
269
270

def test_MDP_P_R_3():
    P = array([[[0.6116, 0.3884],[0, 1]],[[0.6674, 0.3326],[0, 1]]])
    R = array([[[-0.2433, 0.7073],[0, 0.1871]],[[-0.0069, 0.6433],[0, 0.2898]]])
271
    PR = []
272
273
    PR.append(array(matrix('0.12591304, 0.1871')))
    PR.append(array(matrix('0.20935652,0.2898')))
274
    PR = tuple(PR)
275
    a = mdp.MDP(P, R, 0.9, 0.01, 1)
276
277
    for kk in range(2):
        assert (absolute(a.R[kk] - PR[kk]) < SMALLNUM).all()
Steven Cordwell's avatar
Steven Cordwell committed
278

279
280
# LP

281
282
283
284
285
286
287
#def test_LP():
#    a = LP(P, R, 0.9)
#    v = matrix('42.4418604651163 36.0465116279070')
#    p = matrix('1 0')
#    a.iterate()
#    assert (array(a.policy) == p).all()
#    assert (absolute(array(a.V) - v) < SMALLNUM).all()
288

Steven Cordwell's avatar
Steven Cordwell committed
289
# PolicyIteration
Steven Cordwell's avatar
Steven Cordwell committed
290

291
def test_PolicyIteration_init_policy0():
292
    a = mdp.PolicyIteration(P, R, 0.9)
293
294
295
296
    p = matrix('1; 1')
    assert (a.policy == p).all()

def test_PolicyIteration_init_policy0_exampleForest():
297
    a = mdp.PolicyIteration(Pf, Rf, 0.9)
298
    p = matrix('0, 1, 0')
299
300
301
    assert (a.policy == p).all()

def test_PolicyIteration_computePpolicyPRpolicy_exampleForest():
302
    a = mdp.PolicyIteration(Pf, Rf, 0.9)
303
    P1 = matrix('0.1 0.9 0; 1 0 0; 0.1 0 0.9')
304
    R1 = matrix('0, 1, 4')
305
    Ppolicy, Rpolicy = a._computePpolicyPRpolicy()
306
307
308
309
    assert (absolute(Ppolicy - P1) < SMALLNUM).all()
    assert (absolute(Rpolicy - R1) < SMALLNUM).all()

def test_PolicyIteration_evalPolicyIterative_exampleForest():
310
311
312
    v0 = matrix('0, 0, 0')
    v1 = matrix('4.47504640074458, 5.02753258879703, 23.17234211944304')
    p = matrix('0, 1, 0')
313
    a = mdp.PolicyIteration(Pf, Rf, 0.9)
314
    assert (absolute(a.V - v0) < SMALLNUM).all()
315
    a._evalPolicyIterative()
316
    assert (absolute(a.V - v1) < SMALLNUM).all()
317
318
319
    assert (a.policy == p).all()

def test_PolicyIteration_evalPolicyIterative_bellmanOperator_exampleForest():
320
321
    v = matrix('4.47504640074458, 5.02753258879703, 23.17234211944304')
    p = matrix('0, 0, 0')
322
    a = mdp.PolicyIteration(Pf, Rf, 0.9)
323
    a._evalPolicyIterative()
324
    policy, value = a._bellmanOperator()
325
    assert (policy == p).all()
326
    assert (absolute(a.V - v) < SMALLNUM).all()
327
328

def test_PolicyIteration_iterative_exampleForest():
329
    a = mdp.PolicyIteration(Pf, Rf, 0.9, eval_type=1)
330
    v = matrix('26.2439058351861, 29.4839058351861, 33.4839058351861')
331
332
333
    p = matrix('0 0 0')
    itr = 2
    a.iterate()
334
    assert (absolute(array(a.V) - v) < SMALLNUM).all()
335
336
337
338
    assert (array(a.policy) == p).all()
    assert a.iter == itr

def test_PolicyIteration_evalPolicyMatrix_exampleForest():
339
    v_pol = matrix('4.47513812154696, 5.02762430939227, 23.17243384704857')
340
    a = mdp.PolicyIteration(Pf, Rf, 0.9)
341
    a._evalPolicyMatrix()
342
    assert (absolute(a.V - v_pol) < SMALLNUM).all()
343
344

def test_PolicyIteration_matrix_exampleForest():
345
    a = mdp.PolicyIteration(Pf, Rf, 0.9)
346
    v = matrix('26.2440000000000, 29.4840000000000, 33.4840000000000')
347
348
349
    p = matrix('0 0 0')
    itr = 2
    a.iterate()
350
    assert (absolute(array(a.V) - v) < SMALLNUM).all()
351
352
    assert (array(a.policy) == p).all()
    assert a.iter == itr
Steven Cordwell's avatar
Steven Cordwell committed
353

354
# QLearning
355
356

def test_QLearning():
357
    #randseed(0)
358
    a = mdp.QLearning(P, R, 0.9)
359
360
361
    #q = matrix('36.63245946346517 42.24434307022128; ' \
    #           '35.96582807367007 32.70456417451635')
    #v = matrix('42.24434307022128 35.96582807367007')
362
363
    p = matrix('1 0')
    a.iterate()
364
365
    #assert (absolute(a.Q - q) < SMALLNUM).all()
    #assert (absolute(array(a.V) - v) < SMALLNUM).all()
366
367
    assert (array(a.policy) == p).all()

368
def test_QLearning_exampleForest():
369
    a = mdp.QLearning(Pf, Rf, 0.9)
370
371
372
373
    #q = matrix('26.1841860892231 18.6273657021260; ' \
    #           '29.5880960371007 18.5901207622881; '\
    #           '33.3526406657418 25.2621054631519')
    #v = matrix('26.1841860892231 29.5880960371007 33.3526406657418')
Steven Cordwell's avatar
Steven Cordwell committed
374
375
    p = matrix('0 0 0')
    a.iterate()
376
377
    #assert (absolute(a.Q - q) < SMALLNUM).all()
    #assert (absolute(array(a.V) - v) < SMALLNUM).all()
Steven Cordwell's avatar
Steven Cordwell committed
378
    assert (array(a.policy) == p).all()
379
380
381

# RelativeValueIteration

382
def test_RelativeValueIteration_dense():
383
    a = mdp.RelativeValueIteration(P, R)
384
385
386
387
388
389
390
391
392
    p= matrix('1 0')
    ar = 3.88523524641183
    itr = 29
    a.iterate()
    assert (array(a.policy) == p).all()
    assert a.iter == itr
    assert absolute(a.average_reward - ar) < SMALLNUM

def test_RelativeValueIteration_sparse():
393
    a = mdp.RelativeValueIteration(Ps, R)
394
395
396
397
398
399
400
401
    p= matrix('1 0')
    ar = 3.88523524641183
    itr = 29
    a.iterate()
    assert (array(a.policy) == p).all()
    assert a.iter == itr
    assert absolute(a.average_reward - ar) < SMALLNUM

402
def test_RelativeValueIteration_exampleForest():
403
    a = mdp.RelativeValueIteration(Pf, Rf)
404
405
    itr = 4
    p = matrix('0 0 0')
406
407
    #v = matrix('-4.360000000000000 -0.760000000000000 3.240000000000000')
    ar = 2.43000000000000
408
409
410
    a.iterate()
    assert (array(a.policy) == p).all()
    assert a.iter == itr
411
412
    #assert (absolute(array(a.V) - v) < SMALLNUM).all()
    assert absolute(a.average_reward - ar) < SMALLNUM
413
414
415
416

# ValueIteration

def test_ValueIteration_boundIter():
417
    inst = mdp.ValueIteration(P, R, 0.9, 0.01)
418
419
420
    assert (inst.max_iter == 28)

def test_ValueIteration_iterate():
421
    inst = mdp.ValueIteration(P, R, 0.9, 0.01)
422
    inst.iterate()
423
424
    v = array((40.048625392716822,  33.65371175967546))
    assert (absolute(array(inst.V) - v) < SMALLNUM).all()
425
426
427
428
    assert (inst.policy == (1, 0))
    assert (inst.iter == 26)

def test_ValueIteration_exampleForest():
429
    a = mdp.ValueIteration(Pf, Rf, 0.96)
430
431
432
433
    a.iterate()
    assert (a.policy == array([0, 0, 0])).all()
    assert a.iter == 4

Steven Cordwell's avatar
Steven Cordwell committed
434
435
# ValueIterationGS

436
def test_ValueIterationGS_boundIter_exampleForest():
437
    a = mdp.ValueIterationGS(Pf, Rf, 0.9)
438
439
440
    itr = 39
    assert (a.max_iter == itr)

Steven Cordwell's avatar
Steven Cordwell committed
441
def test_ValueIterationGS_exampleForest():
442
    a = mdp.ValueIterationGS(Pf, Rf, 0.9)
Steven Cordwell's avatar
Steven Cordwell committed
443
    p = matrix('0 0 0')
444
    v = matrix('25.5833879767579 28.8306546355469 32.8306546355469')
Steven Cordwell's avatar
Steven Cordwell committed
445
446
447
448
    itr = 33
    a.iterate()
    assert (array(a.policy) == p).all()
    assert a.iter == itr
449
    assert (absolute(array(a.V) - v) < SMALLNUM).all()
450

Steven Cordwell's avatar
Steven Cordwell committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#def test_JacksCarRental():
#    S = 21 ** 2
#    A = 11
#    P = zeros((A, S, S))
#    R = zeros((A, S, S))
#    for a in range(A):
#        for s in range(21):
#            for s1 in range(21):
#                c1s = int(s / 21)
#                c2s = s - c1s * 21
#                c1s1 = int(s1 / 21)
#                c2s1 = s - c1s * 21
#                cs = c1s + c2s
#                cs1 = c1s1 + c2s1
#                netmove = 5 - a
#                if (s1 < s):
#                    pass
#                else:
#                    pass
#                P[a, s, s1] = 1
#                R[a, s, s1] = 10 * (cs - cs1) - 2 * abs(a)
#    
#    inst = PolicyIteration(P, R, 0.9)
#    inst.iterate()
#    #assert (inst.policy == )
#
#def test_JacksCarRental2():
#    pass
#
#def test_GamblersProblem():
#    inst = ValueIteration()
#    inst.iterate()
#    #assert (inst.policy == )
Steven Cordwell's avatar
Steven Cordwell committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502

# 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
503
    
Steven Cordwell's avatar
Steven Cordwell committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#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")