-
Notifications
You must be signed in to change notification settings - Fork 0
/
rl_glue.py
354 lines (276 loc) · 9.87 KB
/
rl_glue.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""
Glues together an experiment, agent, and environment.
"""
from abc import ABCMeta, abstractmethod
class RLGlue:
"""
Facilitates interaction between an agent and environment for reinforcement learning experiments.
The RL-Glue program mediates teh communication between the agent and environment programs in response to commands
from the experiment program. -Brian Tanner & Adam White
args:
env_obj: an object that implements BaseEnvironment
agent_obj: an object that implements BaseAgent
"""
def __init__(self, env_obj, agent_obj):
self._environment = env_obj # environment
self._agent = agent_obj # agent
# useful statistics
self._total_reward = None
self._num_steps = None
self._num_episodes = None
self._episode_reward = None
self._num_ep_steps = None
self._action = None
def total_reward(self):
"""
@return self._total_reward: float64
amount of reward accumulated in a single run
"""
return self._total_reward
def episode_reward(self):
"""
@return self._episode_reward: float64
amount of reward accumulated in an episode
"""
return self._episode_reward
def num_steps(self):
"""
@return self._num_steps: int
number of steps in a single run
"""
return self._num_steps
def num_episodes(self):
"""
@return self._num_episodes: int
number of episodes in a single run
"""
return self._num_episodes
def num_ep_steps(self):
"""
@return self._num_ep_steps: int
number of steps in the current episode
"""
return self._num_ep_steps
# repeat for each run
def rl_init(self, total_reward=0, num_steps=0, num_episodes=0):
"""
Reset experiment data.
Reset action.
Reset agent and environment.
"""
self._total_reward = total_reward # amount of reward accumulated in a single run
self._num_steps = num_steps # number of steps in a single run
self._num_episodes = num_episodes # number of episodes in a single run
self._episode_reward = 0 # amount of reward accumulated in an episode
self._num_ep_steps = 0 # number of steps in the current episode
self._action = None
self._agent.agent_init()
self._environment.env_init()
# repeat for each episode
def rl_start(self):
"""
Starts RLGlue experiment.
@return (state, self._action):
state: float64 numpy array with shape (state_dim,)
state of the environment
self._action: float64 numpy array with shape (action_dim,)
action selected by the agent
"""
self._episode_reward = 0 # reward accumulated in an episode
self._num_ep_steps = 0 # number of steps in the current episode
# self._num_steps = max(self._num_steps, 0) # number of steps in a run
state = self._environment.env_start()
self._action = self._agent.agent_start(state)
return state, self._action
def rl_step(self):
"""
Takes a step in a RLGlue experiment.
@return (reward, next_state, terminal, self._action):
reward: float64
reward received for taking action
next_state: float64 numpy array with shape (state_dim,)
state of the environment
terminal: boolean
true if the goal state has been reached after taking action; otherwise false
self._action: float64 numpy array with shape (action_dim,)
action selected by the agent
"""
reward, next_state, terminal = self._environment.env_step(self._action) # returns reward, next_state, done
self._num_ep_steps += 1
self._num_steps += 1
self._total_reward += reward
self._episode_reward += reward
if terminal:
self._action = self._agent.agent_end(reward, next_state, terminal)
self._num_episodes += 1
else:
self._action = self._agent.agent_step(reward, next_state, terminal)
return reward, next_state, terminal, self._action
# repeat for each episode
def rl_episode(self, max_steps_this_episode=0):
"""
Runs an episode in a RLGlue experiment.
@param max_steps_this_episode: int
the maximum number of steps that can be taken in the current episode (<=0 if no limit on number of steps)
@return terminal: boolean
true if the goal state has been reached after taking action; otherwise false
"""""
terminal = False
self.rl_start()
while not terminal and ((max_steps_this_episode <= 0) or (self._num_ep_steps < max_steps_this_episode)):
_, _, _, terminal = self.rl_step()
self._num_episodes += 1
return terminal
# CONVENIENCE FUNCTIONS BELOW
def rl_env_start(self):
"""
Useful when manually specifying agent actions (for debugging). Starts
RL-Glue environment.
Returns:
state observation
"""
self._num_ep_steps = 0
return self._environment.env_start()
def rl_env_step(self, action):
"""
Useful when manually specifying agent actions (for debugging).Takes a
step in the environment based on an action.
Args:
action: Action taken by agent.
Returns:
(float, state, Boolean): reward, state observation, boolean
indicating termination.
"""
reward, state, terminal = self._environment.env_step(action)
self._total_reward += reward
if terminal:
self._num_episodes += 1
else:
self._num_ep_steps += 1
self._num_steps += 1
return reward, state, terminal
def rl_agent_message(self, message):
"""
pass a message to the agent
Args:
message (str): the message to pass
returns:
str: the agent's response
"""
if message is None:
message_to_send = ""
else:
message_to_send = message
the_agent_response = self._agent.agent_message(message_to_send)
if the_agent_response is None:
the_agent_response = ""
return the_agent_response
def rl_env_message(self, message):
"""
pass a message to the environment
Args:
message (str): the message to pass
Returns:
the_env_response (str) : the environment's response
"""
if message is None:
message_to_send = ""
else:
message_to_send = message
the_env_response = self._environment.env_message(message_to_send)
if the_env_response is None:
return ""
return the_env_response
class BaseAgent:
"""
Defines the interface of an RLGlue Agent
ie. These methods must be defined in your own Agent classes
"""
__metaclass__ = ABCMeta
@abstractmethod
def __init__(self):
"""Declare agent variables."""
pass
@abstractmethod
def agent_init(self):
"""Initialize agent variables."""
@abstractmethod
def agent_start(self, state):
"""
The first method called when the experiment starts, called after
the environment starts.
Args:
state (state observation): The agent's current state
Returns:
The first action the agent takes.
"""
@abstractmethod
def agent_step(self, reward, next_state, terminal):
"""
A step taken by the agent.
Args:
reward (float): the reward received for taking the last action taken
next_state (state observation): the agent's current state
terminal (boolean): boolean indicating if the goal state has ben reached
Returns:
The action the agent is taking.
"""
@abstractmethod
def agent_end(self, reward, next_state, terminal):
"""
Run when the agent terminates.
Args:
reward (float): the reward received for entering the terminal state
next_state (state observation): the agent's current state
terminal (boolean): boolean indicating if the goal state has ben reached
"""
@abstractmethod
def agent_message(self, message):
"""
receive a message from rlglue
args:
message (str): the message passed
returns:
str : the agent's response to the message (optional)
"""
class BaseEnvironment:
"""
Defines the interface of an RLGlue environment
ie. These methods must be defined in your own environment classes
"""
__metaclass__ = ABCMeta
@abstractmethod
def __init__(self):
"""Declare environment variables."""
@abstractmethod
def env_init(self):
"""
Initialize environment variables.
"""
@abstractmethod
def env_start(self):
"""
The first method called when the experiment starts, called before the
agent starts.
Returns:
The first state observation from the environment.
"""
@abstractmethod
def env_step(self, action):
"""
A step taken by the environment.
Args:
action: The action taken by the agent
Returns:
(float, state, Boolean): a tuple of the reward, state observation,
and boolean indicating if it's terminal.
"""
@abstractmethod
def env_message(self, message):
"""
receive a message from RLGlue
Args:
message (str): the message passed
Returns:
str: the environment's response to the message (optional)
"""