Commit 299b4b4d authored by Steven Cordwell's avatar Steven Cordwell

[import] Add underscore before imported modules

Import modules with an underscore in front of their names. For example
import numpy as _np. This is to address the namespace getting to full.
parent b9d05b75
...@@ -43,9 +43,8 @@ rand ...@@ -43,9 +43,8 @@ rand
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
from numpy import diag, ones, where, zeros import numpy as _np
from numpy.random import randint, random import scipy.sparse as _sp
from scipy.sparse import coo_matrix, dok_matrix
def forest(S=3, r1=4, r2=2, p=0.1, is_sparse=False): 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. """Generate a MDP example based on a simple forest management scenario.
...@@ -165,22 +164,22 @@ def forest(S=3, r1=4, r2=2, p=0.1, is_sparse=False): ...@@ -165,22 +164,22 @@ def forest(S=3, r1=4, r2=2, p=0.1, is_sparse=False):
rows = list(range(S)) * 2 rows = list(range(S)) * 2
cols = [0] * S + list(range(1, S)) + [S - 1] cols = [0] * S + list(range(1, S)) + [S - 1]
vals = [p] * S + [1-p] * S vals = [p] * S + [1-p] * S
P.append(coo_matrix((vals, (rows, cols)), shape=(S,S)).tocsr()) P.append(_sp.coo_matrix((vals, (rows, cols)), shape=(S,S)).tocsr())
rows = list(range(S)) rows = list(range(S))
cols = [0] * S cols = [0] * S
vals = [1] * S vals = [1] * S
P.append(coo_matrix((vals, (rows, cols)), shape=(S,S)).tocsr()) P.append(_sp.coo_matrix((vals, (rows, cols)), shape=(S,S)).tocsr())
else: else:
P = zeros((2, S, S)) P = _np.zeros((2, S, S))
P[0, :, :] = (1 - p) * diag(ones(S - 1), 1) P[0, :, :] = (1 - p) * _np.diag(_np.ones(S - 1), 1)
P[0, :, 0] = p P[0, :, 0] = p
P[0, S - 1, S - 1] = (1 - p) P[0, S - 1, S - 1] = (1 - p)
P[1, :, :] = zeros((S, S)) P[1, :, :] = _np.zeros((S, S))
P[1, :, 0] = 1 P[1, :, 0] = 1
# Definition of Reward matrix # Definition of Reward matrix
R = zeros((S, 2)) R = _np.zeros((S, 2))
R[S - 1, 0] = r1 R[S - 1, 0] = r1
R[:, 1] = ones(S) R[:, 1] = _np.ones(S)
R[0, 1] = 0 R[0, 1] = 0
R[S - 1, 1] = r2 R[S - 1, 1] = r2
# we want to return the generated transition and reward matrices # we want to return the generated transition and reward matrices
...@@ -284,11 +283,11 @@ def rand(S, A, is_sparse=False, mask=None): ...@@ -284,11 +283,11 @@ def rand(S, A, is_sparse=False, mask=None):
# it may be more efficient to implement this by constructing lists # it may be more efficient to implement this by constructing lists
# of rows, columns and values then creating a coo_matrix, but this # of rows, columns and values then creating a coo_matrix, but this
# works for now # works for now
PP = dok_matrix((S, S)) PP = _sp.dok_matrix((S, S))
RR = dok_matrix((S, S)) RR = _sp.dok_matrix((S, S))
for s in range(S): for s in range(S):
if mask is None: if mask is None:
m = random(S) m = _np.random.random(S)
m[m <= 2/3.0] = 0 m[m <= 2/3.0] = 0
m[m > 2/3.0] = 1 m[m > 2/3.0] = 1
elif mask.shape == (A, S, S): elif mask.shape == (A, S, S):
...@@ -297,7 +296,7 @@ def rand(S, A, is_sparse=False, mask=None): ...@@ -297,7 +296,7 @@ def rand(S, A, is_sparse=False, mask=None):
m = mask[s] m = mask[s]
n = int(m.sum()) # m[s, :] n = int(m.sum()) # m[s, :]
if n == 0: if n == 0:
m[randint(0, S)] = 1 m[_np.random.randint(0, S)] = 1
n = 1 n = 1
# find the columns of the vector that have non-zero elements # find the columns of the vector that have non-zero elements
nz = m.nonzero() nz = m.nonzero()
...@@ -305,9 +304,9 @@ def rand(S, A, is_sparse=False, mask=None): ...@@ -305,9 +304,9 @@ def rand(S, A, is_sparse=False, mask=None):
cols = nz[0] cols = nz[0]
else: else:
cols = nz[1] cols = nz[1]
vals = random(n) vals = _np.random.random(n)
vals = vals / vals.sum() vals = vals / vals.sum()
reward = 2*random(n) - ones(n) reward = 2*_np.random.random(n) - _np.ones(n)
PP[s, cols] = vals PP[s, cols] = vals
RR[s, cols] = reward RR[s, cols] = reward
# PP.tocsr() takes the same amount of time as PP.tocoo().tocsr() # PP.tocsr() takes the same amount of time as PP.tocoo().tocsr()
...@@ -317,15 +316,15 @@ def rand(S, A, is_sparse=False, mask=None): ...@@ -317,15 +316,15 @@ def rand(S, A, is_sparse=False, mask=None):
R[a] = RR.tocsr() R[a] = RR.tocsr()
else: else:
# definition of transition matrix : square stochastic matrix # definition of transition matrix : square stochastic matrix
P = zeros((A, S, S)) P = _np.zeros((A, S, S))
# definition of reward matrix (values between -1 and +1) # definition of reward matrix (values between -1 and +1)
R = zeros((A, S, S)) R = _np.zeros((A, S, S))
for a in range(A): for a in range(A):
for s in range(S): for s in range(S):
# create our own random mask if there is no user supplied one # create our own random mask if there is no user supplied one
if mask is None: if mask is None:
m = random(S) m = _np.random.random(S)
r = random() r = _np.random.random()
m[m <= r] = 0 m[m <= r] = 0
m[m > r] = 1 m[m > r] = 1
elif mask.shape == (A, S, S): elif mask.shape == (A, S, S):
...@@ -334,9 +333,10 @@ def rand(S, A, is_sparse=False, mask=None): ...@@ -334,9 +333,10 @@ def rand(S, A, is_sparse=False, mask=None):
m = mask[s] m = mask[s]
# Make sure that there is atleast one transition in each state # Make sure that there is atleast one transition in each state
if m.sum() == 0: if m.sum() == 0:
m[randint(0, S)] = 1 m[_np.random.randint(0, S)] = 1
P[a][s] = m * random(S) P[a][s] = m * _np.random.random(S)
P[a][s] = P[a][s] / P[a][s].sum() P[a][s] = P[a][s] / P[a][s].sum()
R[a][s] = (m * (2*random(S) - ones(S, dtype=int))) R[a][s] = (m * (2 * _np.random.random(S) -
_np.ones(S, dtype=int)))
# we want to return the generated transition and reward matrices # we want to return the generated transition and reward matrices
return(P, R) return(P, R)
This diff is collapsed.
...@@ -46,9 +46,7 @@ getSpan ...@@ -46,9 +46,7 @@ getSpan
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
from numpy import absolute, ones import numpy as _np
SMALLNUM = 10e-12
# These need to be fixed so that we use classes derived from Error. # These need to be fixed so that we use classes derived from Error.
mdperr = { mdperr = {
...@@ -297,7 +295,7 @@ def checkSquareStochastic(Z): ...@@ -297,7 +295,7 @@ def checkSquareStochastic(Z):
raise InvalidMDPError(mdperr["mat_square"]) raise InvalidMDPError(mdperr["mat_square"])
# check that the matrix is square, and that each row sums to one # check that the matrix is square, and that each row sums to one
assert s1 == s2, mdperr["mat_square"] assert s1 == s2, mdperr["mat_square"]
assert (absolute(Z.sum(axis=1) - ones(s2))).max() < SMALLNUM, \ assert (_np.abs(Z.sum(axis=1) - _np.ones(s2))).max() <= _np.spacing(1), \
mdperr["mat_stoch"] mdperr["mat_stoch"]
# make sure that there are no values less than zero # make sure that there are no values less than zero
try: try:
......
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