-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAbstractEnv.py
558 lines (438 loc) · 17.2 KB
/
AbstractEnv.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# -*- coding: utf-8 -*-
"""AbstractEnv to make the link between Gym and Sofa.
"""
__authors__ = ("PSC", "dmarchal", "emenager")
__version__ = "1.0.0"
__copyright__ = "(c) 2020, Robocath, CNRS, Inria"
__date__ = "Oct 7 2020"
import gym
from gym.utils import seeding
from typing import Optional
import numpy as np
import copy
import os
import splib3
from sofagym.viewer import Viewer
import importlib
import Sofa
import SofaRuntime
class AbstractEnv(gym.Env):
"""Use Sofa scene with a Gym interface.
Methods:
-------
__init__: classical __init__ method.
initialization: Initialization of all arguments.
seed: Initialization of the seed.
step: Realise a step in the environment.
async_step: Realise a step without blocking queue.
reset: Reset the environment and useful arguments.
render: Use viewer to see the environment.
_automatic_rendering: Automatically render the intermediate frames
while an action is still ongoing.
close: Terminate the simulation.
configure: Add element in the configuration.
_formataction.. : transforme the type of action to use server.
Arguments:
---------
config: Dictionary.
Contains the configuration of the environment.
Minimum:
- scene : the name of the simulation.
Note: define the name of the toolbox <scene>Toolbox and the
scene <scene>Scene in the directory ../<scene>.
- deterministic: whether or not the environment is deterministic.
- source,target: definition of the Sofa camera point of view.
- goalList : list of the goals to reach (position or index).
- start_node: the start node (position or index).
- scale_factor: int that define the number of step in simulation.
- timer_limit: int that define the maximum number of steps.
- timeout: int that define the timeout for the server/client requests.
- display_size: tuple of int that define the size of the Viewer
window.
- save_path: path to save the image of the simulation.
- render: wheter or not the viewer displays images.
0: no rendering.
1: render after simulation.
2: render all steps.
Warning: we can't change this value after initialization.
- save_data: wheter or not the data are saved.
- save_image: wheter or not the images are saved.
- planning: if realise planning or not.
- discrete: if the environment is discrete or not.
- timer_limit: the limit of the time.
- seed : the seed.
- start_from_history: list of actions that have to be carried
out before starting the training.
- python_version: the version of python.
- time_before_start: initialize the simulation with time_before_start steps.
- dt: float that define time step.
observation_space: spaces.Box
Define the size of the environment.
past_actions: list of int.
Keeps track of past actions. Allows you to retrieve past
configurations of the environment.
goalList: list
List of possible objectives to be achieved.
goal: list
Current objective.
num_envs: int
The number of environment.
np_random: np.random.RandomState()
Exposes a number of methods for generating random numbers
viewer: <class viewer>
Allows to manage the visual feedback of the simulation.
automatic_rendering_callback:
Callback function used in _automatic_rendering.
timer:
Number of steps already completed.
deterministic:
Whether the environment is deterministic or not.
timeout:
Number of times the queue is blocking. Allows to avoid blocking
situations.
Notes:
-----
It is necessary to define the specificity of the environment in a
subclass.
Usage:
-----
Use the reset method before launch the environment.
"""
def __init__(self, default_config, config=None, render_mode: Optional[str]=None, root=None):
"""
Classic initialization of a class in python.
Parameters:
----------
config: Dictionary or None, default = None
Customisable configuration element.
Returns:
---------
None.
"""
# Define a DEFAULT_CONFIG in sub-class.
self.config = copy.deepcopy(default_config)
self.config["dt"] = self.config.get('dt', 0.01)
if config is not None:
self.config.update(config)
self.scene = self.config['scene']
self._getState = importlib.import_module("sofagym.envs."+self.scene+"."+self.scene+"Toolbox").getState
self._getReward = importlib.import_module("sofagym.envs."+self.scene+"."+self.scene+"Toolbox").getReward
self._startCmd = importlib.import_module("sofagym.envs."+self.scene+"."+self.scene+"Toolbox").startCmd
self._getPos = importlib.import_module("sofagym.envs."+self.scene+"."+self.scene+"Toolbox").getPos
try:
self.create_scene = importlib.import_module("sofagym.envs."+self.scene+"." + self.scene + "Scene").createScene
except Exception as exc:
print("sofagym.envs."+self.scene+"." + self.scene + "Scene")
raise NotImplementedError("Importing your SOFA Scene Failed") from exc
self.viewer = None
self.render_mode = render_mode
self.past_actions = []
self.pos = []
self.past_pos = []
self.num_envs = 40
self.np_random = None
self.seed(self.config['seed'])
self.timer = 0
self.timeout = self.config["timeout"]
self.init_save_paths()
self.root = root
self.init_states = None
self.goal = None
self.nb_actions = self.config["nb_actions"]
self.dim_state = self.config["dim_state"]
def init_save_paths(self):
"""Create directories to save results and images.
Parameters:
----------
None.
Returns:
-------
None.
"""
if 'save_data' in self.config and self.config['save_data']:
save_path_results = self.config['save_path']+"/data"
os.makedirs(save_path_results, exist_ok=True)
else:
save_path_results = None
if 'save_image' in self.config and self.config['save_image']:
save_path_image = self.config['save_path']+"/img"
os.makedirs(save_path_image, exist_ok=True)
else:
save_path_image = None
self.configure({"save_path_image": save_path_image, "save_path_results": save_path_results})
def init_root(self):
self.init_simulation()
def initialize_states(self):
if self.config["randomize_states"]:
self.init_states = self.randomize_init_states()
self.config.update({'init_states': list(self.init_states)})
else:
self.init_states = self.config["init_states"]
def randomize_init_states(self):
"""Randomize initial states.
Returns:
-------
init_states: list
List of random initial states for the environment.
Note:
----
This method should be implemented according to needed random initialization.
"""
return self.config["init_states"]
def init_goal(self):
# Set a new random goal from the list
goalList = self.config["goalList"]
id_goal = self.np_random.choice(range(len(goalList)))
self.config.update({'goal_node': id_goal})
self.goal = goalList[id_goal]
self.config.update({'goalPos': self.goal})
def seed(self, seed=None):
"""
Computes the random generators of the environment.
Parameters:
----------
seed: int, 1D array or None, default = None
seed for the RandomState.
Returns:
---------
[seed]
"""
self.np_random, seed = seeding.np_random(seed)
return [seed]
def get_available_actions(self):
"""Gives the actions available in the environment.
Parameters:
----------
None.
Returns:
-------
list of the action available in the environment.
"""
return self.action_space
def _formataction(self, action):
"""Change the type of action to be in [list, float, int].
Parameters:
----------
action:
The action with no control on the type.
Returns:
-------
action: in [list, float, int]
The action with control on the type.
"""
if isinstance(action, np.ndarray):
action = action.tolist()
elif isinstance(action, np.int64):
action = int(action)
elif isinstance(action, np.float64):
action = float(action)
elif isinstance(action, tuple):
action = self._formatactionTuple(action)
elif isinstance(action, dict):
action = self._formatactionDict(action)
return action
def _formatactionTuple(self, action):
"""Change the type of tuple action to be in [list, float, int].
Parameters:
----------
action:
The action with no control on the type.
Returns:
-------
action:
The action with control on the type.
"""
return self._formataction(action[0]), self._formataction(action[1])
def _formatactionDict(self, action):
"""Change the type of tuple action to be in [list, float, int].
Parameters:
----------
action:
The action with no control on the type.
Returns:
-------
action:
The action with control on the type.
"""
for key in action.keys():
action[key] = self._formataction(action[key])
return action
def step(self, action):
"""Executes one action in the environment.
Apply action and execute scale_factor simulation steps of 0.01 s.
Parameters:
----------
action: int
Action applied in the environment.
Returns:
-------
obs(ObsType):
The new state of the agent.
reward(float):
The reward obtain after applying the action in the current state.
done(bool):
Whether the agent reaches the terminal state
info(dict):
additional information (not used here)
"""
# assert self.action_space.contains(action), "%r (%s) invalid" % (action, type(action))
action = self._formataction(action)
self.pos = self.step_simulation(action)
self.past_actions.append(action)
self.past_pos.append(self.pos)
obs = np.array(self._getState(self.root), dtype=np.float32)
done, reward = self._getReward(self.root)
# Avoid long explorations by using a timer.
self.timer += 1
if self.timer >= self.config["timer_limit"]:
# reward = -150
truncated = True
info = {} #(not use here)
return obs, reward, done, info
def reset(self):
"""Reset simulation.
Parameters:
----------
None.
Returns:
-------
obs, info
"""
self.viewer = None
splib3.animation.animate.manager = None
self.timer = 0
self.past_actions = []
self.pos = []
self.past_pos = []
Sofa.Simulation.reset(self.root)
self.root = None
self.init_simulation()
obs = np.array(self._getState(self.root), dtype=np.float32)
return obs
def render(self, mode):
"""See the current state of the environment.
Get the OpenGL Context to render an image (snapshot) of the simulation
state.
Parameters:
----------
mode: string, default = 'rgb_array'
Type of representation.
Returns:
-------
None.
"""
self.render_mode = mode
# Define the viewer at the first run of render.
if self.viewer is None:
display_size = self.config["display_size"] # Sim display
if 'zFar' in self.config:
zFar = self.config['zFar']
else:
zFar = 0
self.viewer = Viewer(self, self.root, display_size, zFar=zFar, save_path=self.config["save_path_image"])
# Use the viewer to display the environment.
return self.viewer.render()
def close(self):
"""Terminate simulation.
Close the viewer and the scene.
Parametres:
----------
None.
Returns:
-------
None.
"""
if self.viewer is not None:
self.viewer.close()
print("All clients are closed. Bye Bye.")
def configure(self, config):
"""Update the configuration.
Parameters:
----------
config: Dictionary.
Elements to be added in the configuration.
Returns:
-------
None.
"""
self.config.update(config)
def init_simulation(self, mode='simu_and_visu'):
"""Function to create scene and initialize all variables.
Parameters:
----------
config: Dictionary.
Configuration of the environment.
_startCmd: function
Initialize the command.
mode: string, default = 'simu_and_visu'
Init a scene with or without visu and computations.
In ['simu', 'visu', 'simu_and_visu']
Returns:
-------
root: <Sofa.Core>
The loaded and initialized scene.
"""
# Load the scene
self.root = Sofa.Core.Node("root")
SofaRuntime.importPlugin("Sofa.Component")
self.create_scene(self.root, self.config, mode=mode)
Sofa.Simulation.init(self.root)
# Realise action from history
if self.config['start_from_history'] is not None and self._startCmd is not None:
print(">> Start from history ...")
render = self.config['render']
self.config.update({'render': 0})
for action in self.config['start_from_history']:
self.step_simulation(action)
self.config.update({'render': render})
print(">> ... Done.")
if self.config["randomize_states"]:
self.root.StateInitializer.init_state(self.config["init_states"])
if 'time_before_start' in self.config:
print(">> Time before start:", self.config["time_before_start"], "steps. Initialization ...")
for _ in range(self.config["time_before_start"]):
Sofa.Simulation.animate(self.root, self.config["dt"])
print(">> ... Done.")
# Update Reward and GoalSetter
if self.config["goal"]:
self.root.GoalSetter.update(self.goal)
self.root.Reward.update(self.goal)
def step_simulation(self, action):
"""Realise one step in the simulation.
Apply action and execute 5 times scale_factor simulation steps of dt s.
Parameters:
----------
root: <Sofa.Core>
The scene.
config: Dictionary
The configuration of the environment.
action: int
The action to apply in the environment.
_startCmd: function
Initialize the command.
_getPos: function
Get the position of the object in the scene.
Returns:
--------
position(s): list
The positions of object(s) in the scene.
"""
if self.config["goal"]:
goal = self.config['goalPos']
self.root.GoalSetter.set_mo_pos(goal)
render = self.config['render']
surface_size = self.config['display_size']
# Create the command from action
self._startCmd(self.root, action, self.config["dt"]*(self.config["scale_factor"]-1))
pos = []
# Realise scale_factor simulation steps of 0.01 s
for _ in range(self.config["scale_factor"]):
Sofa.Simulation.animate(self.root, self.config["dt"])
#if render == 2:
# pos.append(self._getPos(self.root))
# if self.viewer is not None:
# self.viewer.render_simulation(self.root)
if render == 1:
pos.append(self._getPos(self.root))
return pos