-
Notifications
You must be signed in to change notification settings - Fork 0
/
CartStemContactToolbox.py
303 lines (225 loc) · 7.45 KB
/
CartStemContactToolbox.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
# -*- coding: utf-8 -*-
"""Toolbox: compute reward, create scene, ...
"""
__authors__ = ("emenager")
__contact__ = ("etienne.menager@ens-rennes.fr")
__version__ = "1.0.0"
__copyright__ = "(c) 2021, Inria"
__date__ = "Fab 3 2021"
import numpy as np
import Sofa
import Sofa.Core
import Sofa.Simulation
import SofaRuntime
from splib3.animation.animate import Animation
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.absolute())+"/../")
sys.path.insert(0, str(pathlib.Path(__file__).parent.absolute()))
SofaRuntime.importPlugin("Sofa.Component")
class rewardShaper(Sofa.Core.Controller):
"""Compute the reward.
Methods:
-------
__init__: Initialization of all arguments.
getReward: Compute the reward.
update: Initialize the value of cost.
Arguments:
---------
rootNode: <Sofa.Core>
The scene.
"""
def __init__(self, *args, **kwargs):
"""Initialization of all arguments.
Parameters:
----------
kwargs: Dictionary
Initialization of the arguments.
Returns:
-------
None.
"""
Sofa.Core.Controller.__init__(self, *args, **kwargs)
self.rootNode = None
if kwargs["rootNode"]:
self.rootNode = kwargs["rootNode"]
if kwargs["max_dist"]:
self.max_dist = kwargs["max_dist"]
else:
print(">> ERROR: give a max dist for the normalization of the reward.")
exit(1)
self.goal = np.array(self.rootNode.GoalSetter.goalPos)
self.sphere = self.rootNode.cartstem.Cart.MappedFrames.sphere_CollisionModel
def getReward(self):
"""Compute the reward.
Parameters:
----------
None.
Returns:
-------
The reward and the cost.
"""
current_sphere_pos = self._getSpherePos()
dist = float(abs(current_sphere_pos[0]-self.goal[0]))
r = -dist # /self.init_goal_dist
reward = max(-self.max_dist, r)/self.max_dist
# reward = -dist
return reward, dist
def update(self):
"""Update function.
This function is used as an initialization function.
Parameters:
----------
None.
Arguments:
---------
None.
"""
current_sphere_pos = self._getSpherePos()
self.init_goal_dist = float(abs(current_sphere_pos[0]-self.goal[0]))
def _getSpherePos(self):
pos = self.sphere.MechanicalObject.position.value[0, :3]
return pos
class goalSetter(Sofa.Core.Controller):
def __init__(self, *args, **kwargs):
Sofa.Core.Controller.__init__(self, *args, **kwargs)
self.goalPos = None
if 'goalPos' in kwargs:
self.goalPos = kwargs["goalPos"]
def update(self):
pass
def set_mo_pos(self, goal):
pass
class sceneModerator(Sofa.Core.Controller):
def __init__(self, *args, **kwargs):
Sofa.Core.Controller.__init__(self, *args, **kwargs)
self.cartstem=None
if kwargs["cartstem"]:
self.cartstem = kwargs["cartstem"]
self.contacts=None
if kwargs["contacts"]:
self.contacts = kwargs["contacts"]
def getPos(self):
return self.cartstem.getPos()
def setPos(self, pos):
self.cartstem.setPos(pos)
def getState(rootNode):
"""Compute the state of the environment/agent.
Parameters:
----------
rootNode: <Sofa.Core>
The scene.
Returns:
-------
State: list of float
The state of the environment/agent.
"""
contacts = rootNode.sceneModerator.contacts
cartstem = rootNode.sceneModerator.cartstem
factor = cartstem.max_move
posCart = cartstem.cart.RigidBaseMO.position.value.tolist()[0][0] # /factor
posTip = cartstem.cart.MappedFrames.sphere_CollisionModel.MechanicalObject.position.value.tolist()[0][0] # /factor
posContacts = [p for p in contacts.getPos()] # [p/factor for p in contacts.getPos()]
goal = rootNode.GoalSetter.goalPos[0] # /factor
state = [posCart, posTip] + posContacts + [goal]
return state
def getReward(rootNode):
r, dist = rootNode.Reward.getReward()
done = dist < 0.15
return done, r
def getPos(root):
position = root.sceneModerator.getPos()
return position
def setPos(root, pos):
root.sceneModerator.setPos(pos)
class applyAction(Sofa.Core.Controller):
def __init__(self, *args, **kwargs):
Sofa.Core.Controller.__init__(self, *args, **kwargs)
self.root = kwargs["root"]
if "cartstem" in kwargs:
print(">> Init cartstem...")
self.cartstem = kwargs["cartstem"]
self.cart = self.cartstem.cart
else:
print(">> ERROR: No cartstem ...")
exit(1)
self.max_incr = self.cartstem.max_v*self.cartstem.dt
self.set_max_move(self.cartstem.max_move)
print(">> Init done.")
def set_max_move(self, new_max_move):
self.max_move = new_max_move
self.a, self.b = self.max_move, 0
def _move(self, incr):
cartMO = self.cart.RigidBaseMO
pos = cartMO.position.value.tolist()
if abs(pos[0][0]+incr) < self.max_move:
pos[0][0] += incr
cartMO.position.value = np.array(pos)
def _normalizedAction_to_action(self, action):
return self.a*action + self.b
def compute_action(self, actions, nb_step):
position_goal = self._normalizedAction_to_action(actions[0])
current_position = self.cart.RigidBaseMO.position.value.tolist()[0][0]
incr = (position_goal - current_position)/nb_step
if abs(incr) > self.max_incr:
if incr >= 0:
incr = self.max_incr
else:
incr = -self.max_incr
return incr
def apply_action(self, incr):
self._move(incr)
def action_to_command(actions, root, nb_step):
"""Link between Gym action (int) and SOFA command (displacement of cables).
Parameters:
----------
action: int
The number of the action (Gym).
root:
The root of the scene.
Returns:
-------
The command.
"""
incr = root.applyAction.compute_action(actions, nb_step)
return incr
def startCmd(root, actions, duration):
"""Initialize the command from root and action.
Parameters:
----------
rootNode: <Sofa.Core>
The scene.
action: int
The action.
duration: float
Duration of the animation.
Returns:
------
None.
"""
incr = action_to_command(actions, root, duration/root.dt.value + 1)
startCmd_CartStem(root, incr, duration)
def startCmd_CartStem(rootNode, incr, duration):
"""Initialize the command.
Parameters:
----------
rootNode: <Sofa.Core>
The scene.
incr:
The elements of the commande.
duration: float
Duration of the animation.
Returns:
-------
None.
"""
# Definition of the elements of the animation
def executeAnimation(rootNode, incr, factor):
rootNode.applyAction.apply_action(incr)
# Add animation in the scene
rootNode.AnimationManager.addAnimation(
Animation(
onUpdate=executeAnimation,
params={"rootNode": rootNode,
"incr": incr},
duration=duration, mode="once"))