/
World.py
158 lines (134 loc) · 5.06 KB
/
World.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
149
150
151
152
153
154
155
156
157
158
import numpy as np
import matplotlib.pyplot as plt
class World:
def __init__(self):
self.nRows = 3
self.nCols = 4
self.stateObstacles = [5]
self.stateTerminals = [10, 11]
self.nStates = 12
self.nActions = 4
def _plot_world(self):
nStates = self.nStates
nRows = self.nRows
nCols = self.nCols
stateObstacles = self.stateObstacles
stateTerminals = self.stateTerminals
coord = [[0, 0], [nCols, 0], [nCols, nRows], [0, nRows], [0, 0]]
xs, ys = zip(*coord)
plt.plot(xs, ys, "black")
for i in stateObstacles:
(I, J) = np.unravel_index(i, shape=(nRows, nCols), order='F')
coord = [[J, nRows - I],
[J + 1, nRows - I],
[J + 1, nRows - I + 1],
[J, nRows - I + 1],
[J, nRows - I]]
xs, ys = zip(*coord)
plt.fill(xs, ys, "0.5")
plt.plot(xs, ys, "black")
for ind, i in enumerate(stateTerminals):
(I, J) = np.unravel_index(i, shape=(nRows, nCols), order='F')
coord = [[J, nRows - I],
[J + 1, nRows - I],
[J + 1, nRows - I + 1],
[J, nRows - I + 1],
[J, nRows - I]]
xs, ys = zip(*coord)
plt.fill(xs, ys, "0.8")
plt.plot(xs, ys, "black")
plt.plot(xs, ys, "black")
X, Y = np.meshgrid(range(nCols + 1), range(nRows + 1))
plt.plot(X, Y, 'k-')
plt.plot(X.transpose(), Y.transpose(), 'k-')
@staticmethod
def _truncate(n, decimals=0):
multiplier = 10 ** decimals
return int(n * multiplier) / multiplier
def plot(self):
"""
plot function
:return: None
"""
nStates = self.nStates
nRows = self.nRows
nCols = self.nCols
self._plot_world()
states = range(1, nStates + 1)
k = 0
for i in range(nCols):
for j in range(nRows, 0, -1):
plt.text(i + 0.5, j - 0.5, str(states[k]), fontsize=26, horizontalalignment='center', verticalalignment='center')
k += 1
plt.title('MDP gridworld', size=16)
plt.axis("equal")
plt.axis("off")
plt.show()
def plot_value(self, valueFunction):
nRows = self.nRows
nCols = self.nCols
stateObstacles = self.stateObstacles
self._plot_world()
k = 0
for i in range(nCols):
for j in range(nRows, 0, -1):
if k + 1 not in stateObstacles:
plt.text(i + 0.5, j - 0.5, str(self._truncate(valueFunction[k], 3)), fontsize=26, horizontalalignment='center', verticalalignment='center')
k += 1
plt.title('MDP gridworld', size=16)
plt.axis("equal")
plt.axis("off")
plt.show()
def plot_policy(self, policy):
nStates = self.nStates
nActions = self.nActions
nRows = self.nRows
nCols = self.nCols
stateObstacles = self.stateObstacles
stateTerminals = self.stateTerminals
policy = policy.reshape(nRows, nCols, order="F").reshape(-1, 1)
X, Y = np.meshgrid(range(nCols + 1), range(nRows + 1))
X1 = X[:-1, :-1]
Y1 = Y[:-1, :-1]
X2 = X1.reshape(-1, 1) + 0.5
Y2 = np.flip(Y1.reshape(-1, 1)) + 0.5
X2 = np.kron(np.ones((1, nActions)), X2)
Y2 = np.kron(np.ones((1, nActions)), Y2)
mat = np.cumsum(np.ones((nStates, nActions)), axis=1).astype("int64")
if policy.shape[1] == 1:
policy = (np.kron(np.ones((1, nActions)), policy) == mat)
index_no_policy = stateObstacles + stateTerminals
index_policy = [item - 1 for item in range(1, nStates + 1) if item not in index_no_policy]
mask = policy.astype("int64") * mat
mask = mask.reshape(nRows, nCols, nCols)
X3 = X2.reshape(nRows, nCols, nActions)
Y3 = Y2.reshape(nRows, nCols, nActions)
alpha = np.pi - np.pi / 2 * mask
self._plot_world()
for ii in index_policy:
ax = plt.gca()
j = int(ii / nRows)
i = (ii + 1 - j * nRows) % nCols - 1
index = np.where(mask[i, j] > 0)[0]
h = ax.quiver(X3[i, j, index], Y3[i, j, index], np.cos(alpha[i, j, index]), np.sin(alpha[i, j, index]), 0.3)
states = range(1, nStates + 1)
k = 0
for i in range(nCols):
for j in range(nRows, 0, -1):
plt.text(i + 0.25, j - 0.25, str(states[k]), fontsize=16, horizontalalignment='right', verticalalignment='bottom')
k += 1
plt.axis("equal")
plt.axis("off")
plt.show()
def get_nrows(self):
return self.nRows
def get_ncols(self):
return self.nCols
def get_stateobstacles(self):
return self.stateObstacles
def get_stateterminals(self):
return self.stateTerminals
def get_nstates(self):
return self.nStates
def get_nactions(self):
return self.nActions