Commit 32fb5783 authored by Steven Cordwell's avatar Steven Cordwell

Fix some formatting mistakes

Make the code more compliant to PEP 8.
parent 507ba06b
......@@ -18,7 +18,7 @@ Available modules
How to use the documentation
----------------------------
Documentation is available both as docstrings provided with the code and
in html or pdf format from
in html or pdf format from
`The MDP toolbox homepage <http://www.somewhere.com>`_. The docstring
examples assume that the ``mdptoolbox`` package is imported like so::
......@@ -45,19 +45,19 @@ source code use ``mdp.ValueIteration??<ENTER>``.
Acknowledgments
---------------
This module is modified from the MDPtoolbox (c) 2009 INRA available at
This module is modified from the MDPtoolbox (c) 2009 INRA available at
http://www.inra.fr/mia/T/MDPtoolbox/.
"""
# Copyright (c) 2011-2013 Steven A. W. Cordwell
# Copyright (c) 2009 INRA
#
#
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
......@@ -66,7 +66,7 @@ http://www.inra.fr/mia/T/MDPtoolbox/.
# * Neither the name of the <ORGANIZATION> nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
......
......@@ -49,6 +49,7 @@ Available functions
import numpy as _np
import scipy.sparse as _sp
def forest(S=3, r1=4, r2=2, p=0.1, is_sparse=False):
"""Generate a MDP example based on a simple forest management scenario.
......@@ -187,6 +188,7 @@ def forest(S=3, r1=4, r2=2, p=0.1, is_sparse=False):
R[S - 1, 1] = r2
return(P, R)
def _randDense(states, actions, mask):
"""Generate random dense ``P`` and ``R``. See ``rand`` for details.
......@@ -204,7 +206,7 @@ def _randDense(states, actions, mask):
m[m <= r] = 0
m[m > r] = 1
elif mask.shape == (actions, states, states):
m = mask[action][state] # mask[action, state, :]
m = mask[action][state] # mask[action, state, :]
else:
m = mask[state]
# Make sure that there is atleast one transition in each state
......@@ -216,6 +218,7 @@ def _randDense(states, actions, mask):
_np.ones(states, dtype=int)))
return(P, R)
def _randSparse(states, actions, mask):
"""Generate random sparse ``P`` and ``R``. See ``rand`` for details.
......@@ -236,10 +239,10 @@ def _randSparse(states, actions, mask):
m[m <= 2/3.0] = 0
m[m > 2/3.0] = 1
elif mask.shape == (actions, states, states):
m = mask[action][state] # mask[action, state, :]
m = mask[action][state] # mask[action, state, :]
else:
m = mask[state]
n = int(m.sum()) # m[state, :]
n = int(m.sum()) # m[state, :]
if n == 0:
m[_np.random.randint(0, states)] = 1
n = 1
......@@ -261,6 +264,7 @@ def _randSparse(states, actions, mask):
R[action] = RR.tocsr()
return(P, R)
def rand(S, A, is_sparse=False, mask=None):
"""Generate a random Markov Decision Process.
......@@ -345,8 +349,9 @@ def rand(S, A, is_sparse=False, mask=None):
if mask is not None:
# the mask needs to be SxS or AxSxS
try:
assert mask.shape in ((S, S), (A, S, S)), "'mask' must have " \
"dimensions S×S or A×S×S."
assert mask.shape in ((S, S), (A, S, S)), (
"'mask' must have dimensions S×S or A×S×S."
)
except AttributeError:
raise TypeError("'mask' must be a numpy array or matrix.")
# generate the transition and reward matrices based on S, A and mask
......@@ -356,6 +361,7 @@ def rand(S, A, is_sparse=False, mask=None):
P, R = _randDense(S, A, mask)
return(P, R)
def small():
"""A very small Markov decision process.
......
This diff is collapsed.
......@@ -90,6 +90,7 @@ _MDPERR = {
"actions greater than 0. i.e. R.shape = (S, A) or (A, S, S)."
}
def _checkDimensionsListLike(arrays):
"""Check that each array in a list of arrays has the same size.
......@@ -102,6 +103,7 @@ def _checkDimensionsListLike(arrays):
raise _error.InvalidError(_MDPERR["obj_square"])
return dim1, dim2, dim3
def _checkRewardsListLike(reward, n_actions, n_states):
"""Check that a list-like reward input is valid.
......@@ -119,6 +121,7 @@ def _checkRewardsListLike(reward, n_actions, n_states):
raise _error.InvalidError(_MDPERR["R_shape"])
return dim1, dim2, dim3
def isSquare(matrix):
"""Check that ``matrix`` is square.
......@@ -139,6 +142,7 @@ def isSquare(matrix):
return True
return False
def isStochastic(matrix):
"""Check that ``matrix`` is row stochastic.
......@@ -155,6 +159,7 @@ def isStochastic(matrix):
absdiff = (_np.abs(matrix.sum(axis=1) - _np.ones(matrix.shape[0])))
return (absdiff.max() <= 10*_np.spacing(_np.float64(1)))
def isNonNegative(matrix):
"""Check that ``matrix`` is row non-negative.
......@@ -177,6 +182,7 @@ def isNonNegative(matrix):
return True
return False
def checkSquareStochastic(matrix):
"""Check if ``matrix`` is a square and row-stochastic.
......@@ -206,6 +212,7 @@ def checkSquareStochastic(matrix):
if not isNonNegative(matrix):
raise _error.NonNegativeError
def check(P, R):
"""Check if ``P`` and ``R`` define a valid Markov Decision Process (MDP).
......
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