-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathenvironment.py
221 lines (179 loc) · 6.76 KB
/
environment.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
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
import numpy as np
class GridWorld:
"""
Gird environment with following stochastic property:
| Agent Action | Possible Actions | Probability |
| :----------: | :---------------: | :-----------: |
| UP | UP, RIGHT, LEFT | 0.8, 0.1, 0.1 |
| DOWN | DOWN, RIGHT, LEFT | 0.8, 0.1, 0.1 |
| LEFT | LEFT, UP, DOWN | 0.8, 0.1, 0.1 |
| RIGHT | RIGHT, UP, DOWN | 0.8, 0.1, 0.1 |
"""
POSSIBLE_ACTIONS = ['U', 'D', 'L', 'R']
def __init__(self, size, rewards, actions):
"""Initialize the GridWorl object
Parameters
----------
size : tuple (row,col)
rewards: dict {(row,col):int}
A dictionary with reward values for each state in the grid
actions: dict ({row,col}:list)
A dictionary that associates all possible actions for each state
"""
self.height, self.width = size
self.rewards = rewards
self.actions = actions
self.num_states = np.prod(size)
self.num_actions = len(GridWorld.POSSIBLE_ACTIONS)
def _limit_coordinates(self, state):
"""Limits the coordinates if/after collision with grid wall
Parameters
----------
state : tuple (row, col)
Returns
-------
state: tuple(row, col)
"""
i, j = state
if i < 0:
i = 0
elif i > self.height - 1:
i = self.height - 1
if j < 0:
j = 0
elif j > self.width - 1:
j = self.width - 1
return (i, j)
def _new_state_reward(self, action, state):
"""Returns the coordinates of a resultant state and its rewards
Parameters
----------
action: char
The character representing the action taken
state: tuple (row, col)
Returns
-------
state: tuple (row, col)
The new state reached by taking the action
reward: int
The reward for the state
"""
i, j = state
if action == 'U':
i, j = i - 1, j
elif action == 'D':
i, j = i + 1, j
elif action == 'R':
i, j = i, j + 1
elif action == 'L':
i, j = i, j - 1
# make sure the new state is not out of grid
new_state = self._limit_coordinates((i, j))
return new_state, self.rewards.get(new_state)
def transition(self, action, state, choose=False):
"""The stochastic transition model of the grid
Parameters
----------
action : char
The character representing the action taken
state : tuple (row, col)
The current state from where transition is occuring
choose : boolean
If True: environment takes stochastic action and returns resultant
state and reward
If False: environment returns a list of all possible actions with
corresponding reward and probabilties
Returns
-------
If choose == True
state : tuple (row, col)
reward: int
If choose == Flase
A list with following tuple:
prob: float
Probabilty with which environment selects the transition
state: tuple (row, col)
reward: int
"""
def stochastic_transition(possible_actions, prob):
if not choose:
# create and return a list of all possible actions
result = []
for i, a in enumerate(possible_actions):
coord, reward = self._new_state_reward(a, state)
result.append((prob[i], coord, reward))
return result
else:
# choose a random action with given probabilities
a = np.random.choice(possible_actions, 1, p=prob)
coord, reward = self._new_state_reward(a, state)
return coord, reward
if action == 'U':
return stochastic_transition(['U', 'R', 'L'], [0.8, 0.1, 0.1])
elif action == 'D':
return stochastic_transition(['D', 'R', 'L'], [0.8, 0.1, 0.1])
elif action == 'R':
return stochastic_transition(['R', 'U', 'D'], [0.8, 0.1, 0.1])
elif action == 'L':
return stochastic_transition(['L', 'U', 'D'], [0.8, 0.1, 0.1])
def grid():
"""Utility function, returns 4x4 GridWorld object with rewards and actions
"""
# dict with rewards for states of the grid
rewards = {
(0, 0): -1, (0, 1): -1, (0, 2): -1, (0, 3): -1,
(1, 0): -1, (1, 1): -1, (1, 2): -1, (1, 3): -1, # start state is 1x0
(2, 0): -1, (2, 1): -70, (2, 2): -1, (2, 3): -1, # bad state is 2x1
(3, 0): -1, (3, 1): -1, (3, 2): -1, (3, 3): 100 # goal state is 3x3
}
# dict with actions allowed for the grid states
actions = {
(0, 0): ['R', 'D'], (0, 1): ['R', 'L', 'D'],
(0, 2): ['R', 'L', 'D'], (0, 3): ['L', 'D'],
(1, 0): ['R', 'U', 'D'], (1, 1): ['R', 'L', 'U', 'D'],
(1, 2): ['R', 'L', 'U', 'D'], (1, 3): ['L', 'U', 'D'],
(2, 0): ['R', 'U', 'D'], (2, 1): ['R', 'L', 'U', 'D'],
(2, 2): ['R', 'L', 'U', 'D'], (2, 3): ['L', 'U', 'D'],
(3, 0): ['R', 'U'], (3, 1): ['R', 'L', 'U'],
(3, 2): ['R', 'L', 'U'], (3, 3): []
}
return GridWorld(size=(4, 4), rewards=rewards, actions=actions)
def print_grid(env, content_dict):
""" Utility function that prints the grid environment with given content
Parameters
---------
env: GridWorld
content_dict: dict {(row,col):object}
"""
grid = np.arange(env.num_states, dtype=object).reshape(
env.height, env.width)
for coord, content in content_dict.items():
grid[coord[0], coord[1]] = content
print(grid)
def play_game(env, start, end, policy):
"""Utility function that follows given policy from start to end and
returns a list of action, state and reward for each step
Parameters
----------
env : GridWorld
start : (row, col)
end : (row, col)
policy : list of optimal actions for each state
Policy list in the stats array returned by the algorithm
Returns
-------
steps: list of [action,state,reward]
"""
steps = []
state = start
while state != end:
# get 1D index from state tuple
state_idx = state[0] * env.height + state[1]
action = policy[state_idx]
# make a transition with choose=True
new_state, reward = env.transition(action, state, choose=True)
steps.append([action, list(state), reward])
state = new_state
# append the goal state for visualization
steps.append(['G', list(end), 0])
return steps