-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvRunner.py
272 lines (200 loc) · 10.3 KB
/
EnvRunner.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
# ----------------------------------------------------------------------------
# PyAOgmaNeo
# Copyright(c) 2020-2021 Ogma Intelligent Systems Corp. All rights reserved.
#
# This copy of PyAOgmaNeo is licensed to you under the terms described
# in the PYAOGMANEO_LICENSE.md file included in this distribution.
# ----------------------------------------------------------------------------
# -*- coding: utf-8 -*-
import pyaogmaneo as neo
import numpy as np
import gym
import cv2
import os
from copy import copy
import time
def sigmoid(x):
return np.tanh(x) * 0.5 + 0.5
inputTypeNone = neo.none
inputTypePrediction = neo.prediction
inputTypeAction = neo.action
class EnvRunner:
def __init__(self, env, layerSizes=2 * [ (5, 5, 16) ], layerRadius=2, hiddenSize=(8, 8, 16), imageRadius=8, imageScale=1.0, obsResolution=32, actionResolution=16, rewardScale=1.0, terminalReward=0.0, infSensitivity=1.0, nThreads=8):
self.env = env
neo.setNumThreads(nThreads)
neo.setGlobalState(int(time.time()))
self.imEnc = None
self.imEncIndex = -1
self.inputSizes = []
self.inputLows = []
self.inputHighs = []
self.inputTypes = []
self.imageSizes = []
self.imgsPrev = []
self.actionIndices = []
self.rewardScale = rewardScale
self.terminalReward = terminalReward
self.infSensitivity = infSensitivity
if type(self.env.observation_space) is gym.spaces.Discrete:
self.inputSizes.append((1, 1, self.env.observation_space.n))
self.inputTypes.append(inputTypeNone)
self.inputLows.append([ 0.0 ])
self.inputHighs.append([ 0.0 ])
elif type(self.env.observation_space) is gym.spaces.Box:
if len(self.env.observation_space.shape) == 1 or len(self.env.observation_space.shape) == 0:
squareSize = int(np.ceil(np.sqrt(len(self.env.observation_space.low))))
self.inputSizes.append((squareSize, squareSize, obsResolution))
self.inputTypes.append(inputTypeNone)
lows = list(self.env.observation_space.low)
highs = list(self.env.observation_space.high)
# Detect large numbers/inf
for i in range(len(lows)):
if abs(lows[i]) > 100000 or abs(highs[i]) > 100000:
# Indicate inf by making low greater than high
lows[i] = 1.0
highs[i] = -1.0
self.inputLows.append(lows)
self.inputHighs.append(highs)
elif len(self.env.observation_space.shape) == 2:
scaledSize = ( int(self.env.observation_space.shape[0] * imageScale), int(self.env.observation_space.shape[1] * imageScale), 1 )
self.imageSizes.append(scaledSize)
elif len(self.env.observation_space.shape) == 3:
scaledSize = ( int(self.env.observation_space.shape[0] * imageScale), int(self.env.observation_space.shape[1] * imageScale), 3 )
self.imageSizes.append(scaledSize)
else:
raise Exception("Unsupported Box input: Dimensions too high " + str(self.env.observation_space.shape))
else:
raise Exception("Unsupported input type " + str(type(self.env.observation_space)))
if len(self.imageSizes) > 0:
vlds = []
for i in range(len(self.imageSizes)):
vld = neo.ImageEncoderVisibleLayerDesc((self.imageSizes[i][0], self.imageSizes[i][1], self.imageSizes[i][2]), imageRadius)
vlds.append(vld)
self.imgsPrev.append(np.zeros(self.imageSizes[i]))
self.imEnc = neo.ImageEncoder()
self.imEnc.initRandom(hiddenSize, vlds)
self.imEncIndex = len(self.inputSizes)
self.inputSizes.append(hiddenSize)
self.inputTypes.append(inputTypeNone)
self.inputLows.append([ 0.0 ])
self.inputHighs.append([ 1.0 ])
# Actions
if type(self.env.action_space) is gym.spaces.Discrete:
self.actionIndices.append(len(self.inputSizes))
self.inputSizes.append((1, 1, self.env.action_space.n))
self.inputTypes.append(inputTypeAction)
self.inputLows.append([ 0.0 ])
self.inputHighs.append([ 0.0 ])
elif type(self.env.action_space) is gym.spaces.Box:
if len(self.env.action_space.shape) < 3:
if len(self.env.action_space.shape) == 2:
self.actionIndices.append(len(self.inputSizes))
self.inputSizes.append((self.env.action_space.shape[0], self.env.action_space.shape[1], actionResolution))
self.inputTypes.append(inputTypeAction)
lows = list(self.env.action_space.low)
highs = list(self.env.action_space.high)
self.inputLows.append(lows)
self.inputHighs.append(highs)
else:
squareSize = int(np.ceil(np.sqrt(len(self.env.action_space.low))))
self.actionIndices.append(len(self.inputSizes))
self.inputSizes.append((squareSize, squareSize, actionResolution))
self.inputTypes.append(inputTypeAction)
lows = list(self.env.action_space.low)
highs = list(self.env.action_space.high)
self.inputLows.append(lows)
self.inputHighs.append(highs)
else:
raise Exception("Unsupported Box action: Dimensions too high " + str(self.env.action_space.shape))
else:
raise Exception("Unsupported action type " + str(type(self.env.action_space)))
lds = []
for i in range(len(layerSizes)):
ld = neo.LayerDesc(hiddenSize=layerSizes[i])
ld.eRadius = layerRadius
ld.dRadius = layerRadius
lds.append(ld)
self.h = neo.Hierarchy()
ioDescs = []
for i in range(len(self.inputSizes)):
ioDescs.append(neo.IODesc(self.inputSizes[i], self.inputTypes[i], layerRadius, layerRadius))
self.h.initRandom(ioDescs, lds)
self.actions = []
for i in range(len(self.actionIndices)):
index = self.actionIndices[i]
#self.h.setImportance(index, 0.01)
size = len(self.inputLows[index])
startAct = []
for _ in range(size):
startAct.append(np.random.randint(0, self.inputSizes[index][2]))
self.actions.append(startAct)
self.averageReward = -1.0
self.averageRewardDecay = 0.01
def _feedObservation(self, obs):
self.inputs = []
actionIndex = 0
for i in range(len(self.inputSizes)):
if self.inputTypes[i] == inputTypeAction:
self.inputs.append(self.actions[actionIndex])
actionIndex += 1
elif i == self.imEncIndex:
# Format image
img = cv2.resize(obs, (self.imageSizes[0][0], self.imageSizes[0][1]))
img = np.swapaxes(img, 0, 1)
#delta = img - self.imgsPrev[0]
self.imgsPrev[0] = copy(img)
# Encode image
self.imEnc.step([ img.ravel().tolist() ], True)
self.inputs.append(list(self.imEnc.getHiddenCIs()))
else:
indices = []
for j in range(len(self.inputLows[i])):
if self.inputLows[i][j] < self.inputHighs[i][j]:
# Rescale
indices.append(int((obs[j] - self.inputLows[i][j]) / (self.inputHighs[i][j] - self.inputLows[i][j]) * (self.inputSizes[i][2] - 1) + 0.5))
elif self.inputLows[i][j] > self.inputHighs[i][j]: # Inf
v = obs[j]
# Rescale
indices.append(int(sigmoid(v * self.infSensitivity) * (self.inputSizes[i][2] - 1) + 0.5))
else:
indices.append(int(obs[j]))
if len(indices) < self.inputSizes[i][0] * self.inputSizes[i][1]:
indices += ((self.inputSizes[i][0] * self.inputSizes[i][1]) - len(indices)) * [ int(0) ]
self.inputs.append(indices)
def act(self, epsilon=0.0, obsPreprocess=None):
feedActions = []
for i in range(len(self.actionIndices)):
index = self.actionIndices[i]
assert(self.inputTypes[index] == inputTypeAction)
if self.inputLows[index][0] < self.inputHighs[index][0]:
feedAction = []
# Explore
for j in range(len(self.inputLows[index])):
if np.random.rand() < epsilon:
self.actions[i][j] = np.random.randint(0, self.inputSizes[index][2])
if self.inputLows[index][j] < self.inputHighs[index][j]:
feedAction.append(self.actions[i][j] / float(self.inputSizes[index][2] - 1) * (self.inputHighs[index][j] - self.inputLows[index][j]) + self.inputLows[index][j])
else:
feedAction.append(self.actions[i][j])
feedActions.append(feedAction)
else:
if np.random.rand() < epsilon:
self.actions[i][0] = np.random.randint(0, self.inputSizes[index][2])
feedActions.append(int(self.actions[i][0]))
# Remove outer array if needed
if len(feedActions) == 1:
feedActions = feedActions[0]
obs, reward, done, _ = self.env.step(feedActions)
self.env.render()
if obsPreprocess is not None:
obs = obsPreprocess(obs)
self._feedObservation(obs)
r = reward * self.rewardScale + float(done) * self.terminalReward
self.averageReward += self.averageRewardDecay * (r - self.averageReward)
self.h.step(self.inputs, True, r)
# Retrieve actions
for i in range(len(self.actionIndices)):
index = self.actionIndices[i]
assert(self.inputTypes[index] == inputTypeAction)
self.actions[i] = list(self.h.getPredictionCIs(index))
return done, reward