-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
471 lines (351 loc) · 11 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
'''
Implementation of a basic RL environment.
Rewards are all normal.
Transitions are multinomial.
author: [email protected]
'''
import numpy as np
#-------------------------------------------------------------------------------
class Environment(object):
'''General RL environment'''
def __init__(self):
pass
def reset(self):
pass
def advance(self, action):
'''
Moves one step in the environment.
Args:
action
Returns:
reward - double - reward
newState - int - new state
pContinue - 0/1 - flag for end of the episode
'''
return 0, 0, 0
#-------------------------------------------------------------------------------
class TabularMDP(Environment):
'''
Tabular MDP
R - dict by (s,a) - each R[s,a] = (meanReward, sdReward)
P - dict by (s,a) - each P[s,a] = transition vector size S
'''
def __init__(self, nState, nAction, epLen):
'''
Initialize a tabular episodic MDP
Args:
nState - int - number of states
nAction - int - number of actions
epLen - int - episode length
Returns:
Environment object
'''
self.nState = nState
self.nAction = nAction
self.epLen = epLen
self.timestep = 0
self.state = 0
# Now initialize R and P
self.R = {}
self.P = {}
for state in range(nState):
for action in range(nAction):
self.R[state, action] = (1, 1)
self.P[state, action] = np.ones(nState) / nState
def reset(self):
'''Reset the environment'''
self.timestep = 0
self.state = 0
def advance(self, action):
'''
Move one step in the environment
Args:
action - int - chosen action
Returns:
reward - double - reward
newState - int - new state
pContinue - 0/1 - flag for end of the episode
'''
if self.R[self.state, action][1] < 1e-9:
# Hack for no noise
reward = self.R[self.state, action][0]
else:
reward = np.random.normal(loc=self.R[self.state, action][0],
scale=self.R[self.state, action][1])
newState = np.random.choice(self.nState, p=self.P[self.state, action])
# Update the environment
self.state = newState
self.timestep += 1
if self.timestep == self.epLen:
pContinue = 0
self.reset()
else:
pContinue = 1
return reward, newState, pContinue
def compute_qVals(self):
'''
Compute the Q values for the environment
Args:
NULL - works on the TabularMDP
Returns:
qVals - qVals[state, timestep] is vector of Q values for each action
qMax - qMax[timestep] is the vector of optimal values at timestep
'''
qVals = {}
qMax = {}
qMax[self.epLen] = np.zeros(self.nState)
for i in range(self.epLen):
j = self.epLen - i - 1
qMax[j] = np.zeros(self.nState)
for s in range(self.nState):
qVals[s, j] = np.zeros(self.nAction)
for a in range(self.nAction):
qVals[s, j][a] = self.R[s, a][0] + np.dot(self.P[s, a], qMax[j + 1])
qMax[j][s] = np.max(qVals[s, j])
return qVals, qMax
#-------------------------------------------------------------------------------
# Benchmark environments
def make_riverSwim(epLen=20, nState=6):
'''
Makes the benchmark RiverSwim MDP.
Args:
NULL - works for default implementation
Returns:
riverSwim - Tabular MDP environment
'''
nAction = 2
R_true = {}
P_true = {}
for s in xrange(nState):
for a in xrange(nAction):
R_true[s, a] = (0, 0)
P_true[s, a] = np.zeros(nState)
# Rewards
R_true[0, 0] = (5. / 1000, 0)
R_true[nState - 1, 1] = (1, 0)
# Transitions
for s in xrange(nState):
P_true[s, 0][max(0, s-1)] = 1.
for s in xrange(1, nState - 1):
P_true[s, 1][min(nState - 1, s + 1)] = 0.35
P_true[s, 1][s] = 0.6
P_true[s, 1][max(0, s-1)] = 0.05
P_true[0, 1][0] = 0.4
P_true[0, 1][1] = 0.6
P_true[nState - 1, 1][nState - 1] = 0.6
P_true[nState - 1, 1][nState - 2] = 0.4
riverSwim = TabularMDP(nState, nAction, epLen)
riverSwim.R = R_true
riverSwim.P = P_true
riverSwim.reset()
return riverSwim
def make_deterministicChain(nState, epLen):
'''
Creates a deterministic chain MDP with two actions.
Args:
nState - int - number of states
epLen - int - episode length
Returns:
chainMDP - Tabular MDP environment
'''
nAction = 2
R_true = {}
P_true = {}
for s in range(nState):
for a in range(nAction):
R_true[s, a] = (0, 0)
P_true[s, a] = np.zeros(nState)
# Rewards
R_true[0, 0] = (0, 1)
R_true[nState - 1, 1] = (1, 1)
# Transitions
for s in range(nState):
P_true[s, 0][max(0, s-1)] = 1.
P_true[s, 1][min(nState - 1, s + 1)] = 1.
chainMDP = TabularMDP(nState, nAction, epLen)
chainMDP.R = R_true
chainMDP.P = P_true
chainMDP.reset()
return chainMDP
def make_stochasticChain(chainLen):
'''
Creates a difficult stochastic chain MDP with two actions.
Args:
chainLen - int - total number of states
Returns:
chainMDP - Tabular MDP environment
'''
nState = chainLen
epLen = chainLen
nAction = 2
pNoise = 1. / chainLen
R_true = {}
P_true = {}
for s in range(nState):
for a in range(nAction):
R_true[s, a] = (0, 0)
P_true[s, a] = np.zeros(nState)
# Rewards
R_true[0, 0] = (0, 1)
R_true[nState - 1, 1] = (1, 1)
# Transitions
for s in range(nState):
P_true[s, 0][max(0, s-1)] = 1.
P_true[s, 1][min(nState - 1, s + 1)] = 1. - pNoise
P_true[s, 1][max(0, s-1)] += pNoise
stochasticChain = TabularMDP(nState, nAction, epLen)
stochasticChain.R = R_true
stochasticChain.P = P_true
stochasticChain.reset()
return stochasticChain
def make_bootDQNChain(nState=6, epLen=15, nAction=2):
'''
Creates the chain from Bootstrapped DQN
Returns:
bootDQNChain - Tabular MDP environment
'''
R_true = {}
P_true = {}
for s in range(nState):
for a in range(nAction):
R_true[s, a] = (0, 0)
P_true[s, a] = np.zeros(nState)
# Rewards
R_true[0, 0] = (0.01, 1)
R_true[nState - 1, 1] = (1, 1)
# Transitions
for s in range(nState):
P_true[s, 0][max(0, s-1)] = 1.
P_true[s, 1][min(nState - 1, s + 1)] = 0.5
P_true[s, 1][max(0, s-1)] = 0.5
bootDQNChain = TabularMDP(nState, nAction, epLen)
bootDQNChain.R = R_true
bootDQNChain.P = P_true
bootDQNChain.reset()
return bootDQNChain
def make_hardBanditMDP(epLen, gap=0.01, nAction=2, pSuccess=0.5):
'''
Creates a difficult bandit-style MDP which is hard to distinguish.
Args:
epLen - int
gap - double - how much better is best arm
nAction - int - how many actions
Returns:
hardBanditMDP - Tabular MDP environment
'''
nState = 3
R_true = {}
P_true = {}
for a in range(nAction):
# Rewards are independent of action
R_true[0, a] = (0.5, 1)
R_true[1, a] = (1, 0)
R_true[2, a] = (0, 0)
# Transitions are like a bandit
P_true[0, a] = np.array([0, pSuccess, 1 - pSuccess])
P_true[1, a] = np.array([0, 1, 0])
P_true[2, a] = np.array([0, 0, 1])
# The first action is a better action though
P_true[0, 0] = np.array([0, pSuccess + gap, 1 - (pSuccess + gap)])
hardBanditMDP = TabularMDP(nState, nAction, epLen)
hardBanditMDP.R = R_true
hardBanditMDP.P = P_true
hardBanditMDP.reset()
return hardBanditMDP
def make_stateBanditMDP(stateMul, gap=0.1):
'''
Creates a bandit-style MDP which examines dependence on states.
Args:
epLen - int
gap - double - how much better is best arm
nAction - int - how many actions
Returns:
stateBanditMDP - Tabular MDP environment
'''
epLen = 2
nAction = 2
nState = 1 + 2 * stateMul
R_true = {}
P_true = {}
for a in range(nAction):
R_true[0, a] = (0, 0)
P_true[0, a] = np.zeros(nState)
for k in range(stateMul):
for i in range(2):
s = 1 + (2 * k) + i
P_true[s, a] = np.zeros(nState)
P_true[s, a][s] = 1
R_true[s, a] = (1-i, 0)
# Important piece is where the transitions go
P_true[0, 0] = np.ones(nState) / (nState - 1)
P_true[0, 0][0] = 0
# Rewarding states
inds = (np.arange(nState) % 2) > 0
P_true[0, 1][inds] = (0.5 + gap) / stateMul
P_true[0, 1][-inds] = (0.5 - gap) / stateMul
P_true[0, 1][0] = 0
stateBanditMDP = TabularMDP(nState, nAction, epLen)
stateBanditMDP.R = R_true
stateBanditMDP.P = P_true
stateBanditMDP.reset()
return stateBanditMDP
def make_confidenceMDP(stateMul, gap=0.1):
'''
Creates a bandit-style MDP which examines dependence on states.
Args:
epLen - int
gap - double - how much better is best arm
nAction - int - how many actions
Returns:
confidenceMDP - Tabular MDP environment
'''
epLen = 2
nAction = 1
nState = 1 + 2 * stateMul
R_true = {}
P_true = {}
for a in range(nAction):
R_true[0, a] = (0, 0)
P_true[0, a] = np.zeros(nState)
for k in range(stateMul):
for i in range(2):
s = 1 + (2 * k) + i
P_true[s, a] = np.zeros(nState)
P_true[s, a][s] = 1
R_true[s, a] = (1-i, 0)
# Important piece is where the transitions go
P_true[0, 0] = np.ones(nState) / (nState - 1)
P_true[0, 0][0] = 0
# Rewarding states
inds = (np.arange(nState) % 2) > 0
confidenceMDP = TabularMDP(nState, nAction, epLen)
confidenceMDP.R = R_true
confidenceMDP.P = P_true
confidenceMDP.reset()
return confidenceMDP
def make_HconfidenceMDP(epLen):
'''
Creates a H-dependence bandit confidence.
Args:
epLen - int
gap - double - how much better is best arm
nAction - int - how many actions
Returns:
hardBanditMDP - Tabular MDP environment
'''
nState = 3
R_true = {}
P_true = {}
# Rewards are independent of action
R_true[0, 0] = (0.5, 0)
R_true[1, 0] = (1, 0)
R_true[2, 0] = (0, 0)
# Transitions are like a bandit
P_true[0, 0] = np.array([0, 0.5, 0.5])
P_true[1, 0] = np.array([0, 1, 0])
P_true[2, 0] = np.array([0, 0, 1])
hardBanditMDP = TabularMDP(nState, 1, epLen)
hardBanditMDP.R = R_true
hardBanditMDP.P = P_true
hardBanditMDP.reset()
return hardBanditMDP