forked from gianluigigrandesso/cacto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
816 lines (598 loc) · 33.6 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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
import math
import mpmath
import random
import numpy as np
import tensorflow as tf
import pinocchio as pin
from utils import *
class Env:
def __init__(self, conf):
'''
:input conf : (Configuration file)
:param robot : (RobotWrapper instance)
:param simu : (RobotSimulator instance)
:param x_init_min : (float array) State lower bound initial configuration array
:param x_init_max : (float array) State upper bound initial configuration array
:param x_min : (float array) State lower bound vector
:param x_max : (float array) State upper bound vector
:param u_min : (float array) Action lower bound array
:param u_max : (float array) Action upper bound array
:param nb_state : (int) State size (robot state size + 1)
:param nb_action : (int) Action size (robot action size)
:param dt : (float) Timestep
:param end_effector_frame_id : (str) Name of EE-frame
# Cost function parameters
:param TARGET_STATE : (float array) Target position
:param cost_funct_param (float array) Cost function scale and offset factors
:param soft_max_param : (float array) Soft parameters array
:param obs_param : (float array) Obtacle parameters array
'''
self.conf = conf
self.nq = conf.nq
self.nv = conf.nv
self.nx = conf.nx
self.nu = conf.na
# Rename reward parameters
self.offset = self.conf.cost_funct_param[0]
self.scale = self.conf.cost_funct_param[1]
def reset(self):
''' Choose initial state uniformly at random '''
state = np.zeros(self.conf.nb_state)
time = random.uniform(self.conf.x_init_min[-1], self.conf.x_init_max[-1])
for i in range(self.conf.nb_state-1):
state[i] = random.uniform(self.conf.x_init_min[i], self.conf.x_init_max[i])
state[-1] = self.conf.dt*round(time/self.conf.dt)
return state
def check_ICS_feasible(self, state):
''' Check if ICS is not feasible '''
# check if ee is in the obstacles
p_ee = self.get_end_effector_position(state)
ellipse1 = ((p_ee[0] - self.conf.XC1)**2) / ((self.conf.A1 / 2)**2) + ((p_ee[1] - self.conf.YC1)**2) / ((self.conf.B1 / 2)**2)
ellipse2 = ((p_ee[0] - self.conf.XC2)**2) / ((self.conf.A2 / 2)**2) + ((p_ee[1] - self.conf.YC2)**2) / ((self.conf.B2 / 2)**2)
ellipse3 = ((p_ee[0] - self.conf.XC3)**2) / ((self.conf.A3 / 2)**2) + ((p_ee[1] - self.conf.YC3)**2) / ((self.conf.B3 / 2)**2)
feasible_flag = ellipse1 > 1 and ellipse2 > 1 and ellipse3 > 1
return feasible_flag
def step(self, weights, state, action):
''' Return next state and reward '''
# compute next state
state_next = self.simulate(state, action)
# compute reward
reward = self.reward(weights, state, action)
return (state_next, reward)
def simulate(self, state, action):
''' Simulate dynamics '''
state_next = np.zeros(self.nx+1)
# Simulate control action
self.conf.simu.simulate(np.copy(state[:-1]), action, self.conf.dt, 1) ### Explicit Euler ###
# Return next state
state_next[:self.nq], state_next[self.nq:self.nx] = np.copy(self.conf.simu.q), np.copy(self.conf.simu.v)
state_next[-1] = state[-1] + self.conf.dt
return state_next
def derivative(self, state, action):
''' Compute the derivative '''
# Create robot model in Pinocchio with q_init as initial configuration
q_init = state[:self.nq]
v_init = state[self.nq:self.nx]
# Dynamics gradient w.r.t control (1st order euler)
pin.computeABADerivatives(self.conf.robot.model, self.conf.robot.data, np.copy(q_init), np.copy(v_init), action)
Fu = np.zeros((self.nx+1, self.nu))
Fu[self.nv:-1, :] = self.conf.robot.data.Minv
Fu[:self.nx, :] *= self.conf.dt
if self.conf.NORMALIZE_INPUTS:
Fu[:-1] *= (1/self.conf.state_norm_arr[:-1,None])
return Fu
def augmented_derivative(self, state, action):
''' Partial derivatives of system dynamics w.r.t. x '''
q = state[:self.nq]
v = state[self.nq:self.nx]
# Compute Jacobians for continuous time dynamics
Fx = np.zeros((self.conf.nb_state-1,self.conf.nb_state-1))
Fu = np.zeros((self.conf.nb_state-1,self.conf.nb_action))
pin.computeABADerivatives(self.conf.robot.model, self.conf.robot.data, q, v, action)
Fx[:self.nv, :self.nv] = 0.0
Fx[:self.nv, self.nv:self.nx] = np.identity(self.nv)
Fx[self.nv:self.nx, :self.nv] = self.conf.robot.data.ddq_dq
Fx[self.nv:self.nx, self.nv:self.nx] = self.conf.robot.data.ddq_dv
Fu[self.nv:self.nx, :] = self.conf.robot.data.Minv
# Convert them to discrete time
Fx = np.identity(self.conf.nb_state-1) + self.conf.dt * Fx
Fu *= self.conf.dt
return Fx, Fu
def simulate_batch(self, state, action):
''' Simulate dynamics using tensors and compute its gradient w.r.t control. Batch-wise computation '''
state_next = np.array([self.simulate(s, a) for s, a in zip(state, action)])
return tf.convert_to_tensor(state_next, dtype=tf.float32)
def derivative_batch(self, state, action):
''' Simulate dynamics using tensors and compute its gradient w.r.t control. Batch-wise computation '''
Fu = np.array([self.derivative(s, a) for s, a in zip(state, action)])
return tf.convert_to_tensor(Fu, dtype=tf.float32)
def get_end_effector_position(self, state, recompute=True):
''' Compute end-effector position '''
q = state[:self.nq]
RF = self.conf.robot.model.getFrameId(self.conf.end_effector_frame_id)
H = self.conf.robot.framePlacement(q, RF, recompute)
p = H.translation
return p
def bound_control_cost(self, action):
u_cost = 0
for i in range(self.conf.nb_action):
u_cost += action[i]*action[i] + self.conf.w_b*(action[i]/self.conf.u_max[i])**10
return u_cost
class SingleIntegrator(Env):
'''
:param cost_function_parameters :
'''
metadata = {
"render_modes": [
"human", "rgb_array"
],
"render_fps": 4,
}
def __init__(self, conf):
self.conf = conf
super().__init__(conf)
# Rename reward parameters
self.offset = self.conf.cost_funct_param[0]
self.scale = self.conf.cost_funct_param[1]
self.alpha = self.conf.soft_max_param[0]
self.alpha2 = self.conf.soft_max_param[1]
self.XC1 = self.conf.obs_param[0]
self.YC1 = self.conf.obs_param[1]
self.XC2 = self.conf.obs_param[2]
self.YC2 = self.conf.obs_param[3]
self.XC3 = self.conf.obs_param[4]
self.YC3 = self.conf.obs_param[5]
self.A1 = self.conf.obs_param[6]
self.B1 = self.conf.obs_param[7]
self.A2 = self.conf.obs_param[8]
self.B2 = self.conf.obs_param[9]
self.A3 = self.conf.obs_param[10]
self.B3 = self.conf.obs_param[11]
self.TARGET_STATE = self.conf.TARGET_STATE
self.nx = conf.nx
self.nu = conf.na
def derivative(self, state, action):
''' Compute the derivative '''
# Dynamics gradient w.r.t control (1st order euler)
Fu = np.zeros((self.nx+1, self.nu))
Fu[0,0] = self.conf.dt
Fu[1,1] = self.conf.dt
if self.conf.NORMALIZE_INPUTS:
Fu[:-1] *= (1/self.conf.state_norm_arr[:-1,None])
return Fu
def augmented_derivative(self, state, action):
''' Partial derivatives of system dynamics w.r.t. x '''
# Compute Jacobians for discrete time dynamics
Fx = np.zeros((self.conf.nb_state-1,self.conf.nb_state-1))
Fu = np.zeros((self.conf.nb_state-1,self.conf.nb_action))
Fx[:self.conf.nb_state-1,:self.conf.nb_state-1] = np.array([[1, 0],
[0, 1]])
Fu[0,0] = self.conf.dt
Fu[1,1] = self.conf.dt
return Fx, Fu
def simulate(self, state, action):
''' Simulate dynamics '''
state_next = np.zeros(self.nx+1)
state_next[0] = state[0] + self.conf.dt*action[0]
state_next[1] = state[1] + self.conf.dt*action[1]
state_next[2] = state[2] + self.conf.dt
return state_next
def get_end_effector_position(self, state, recompute=True):
''' Compute end-effector position '''
p = np.zeros(3)
p[:2] = state[:2]
return p
def reward(self, weights, state, action=None):
''' Compute reward '''
# End-effector coordinates
x_ee, y_ee = self.get_end_effector_position(state)[:2]
# Penalties for the ellipses representing the obstacle
ell1_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC1)**2)/((self.A1/2)**2) + ((y_ee-self.YC1)**2)/((self.B1/2)**2) - 1.0)) + 1)/self.alpha
ell2_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC2)**2)/((self.A2/2)**2) + ((y_ee-self.YC2)**2)/((self.B2/2)**2) - 1.0)) + 1)/self.alpha
ell3_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC3)**2)/((self.A3/2)**2) + ((y_ee-self.YC3)**2)/((self.B3/2)**2) - 1.0)) + 1)/self.alpha
# Term pushing the agent to stay in the neighborhood of target
peak_rew = math.log(math.exp(self.alpha2*-(math.sqrt((x_ee-self.TARGET_STATE[0])**2 +0.1) - math.sqrt(0.1) - 0.1 + math.sqrt((y_ee-self.TARGET_STATE[1])**2 +0.1) - math.sqrt(0.1) - 0.1)) + 1)/self.alpha2
# Term pensalizing the control effort
if action is not None:
u_cost = self.bound_control_cost(action)
else:
u_cost = 0
dist_cost = (x_ee-self.TARGET_STATE[0])**2 + (y_ee-self.TARGET_STATE[1])**2
r = self.scale*(- weights[0]*dist_cost + weights[1]*peak_rew - weights[3]*ell1_cost - weights[4]*ell2_cost - weights[5]*ell3_cost - weights[6]*u_cost + self.offset)
return r
def reward_batch(self, weights, state, action):
''' Compute reward using tensors. Batch-wise computation '''
partial_reward = np.array([self.reward(w, s) for w, s in zip(weights, state)])
# Redefine action-related cost in tensorflow version
u_cost = tf.reduce_sum((action**2 + self.conf.w_b*(action/self.conf.u_max)**10),axis=1)
r = self.scale*(- weights[:,6]*u_cost) + tf.convert_to_tensor(partial_reward, dtype=tf.float32)
return tf.reshape(r, [r.shape[0], 1])
class DoubleIntegrator(Env):
'''
:param cost_function_parameters :
'''
metadata = {
"render_modes": [
"human", "rgb_array"
],
"render_fps": 4,
}
def __init__(self, conf):
self.conf = conf
super().__init__(conf)
# Rename reward parameters
self.offset = self.conf.cost_funct_param[0]
self.scale = self.conf.cost_funct_param[1]
self.alpha = self.conf.soft_max_param[0]
self.alpha2 = self.conf.soft_max_param[1]
self.XC1 = self.conf.obs_param[0]
self.YC1 = self.conf.obs_param[1]
self.XC2 = self.conf.obs_param[2]
self.YC2 = self.conf.obs_param[3]
self.XC3 = self.conf.obs_param[4]
self.YC3 = self.conf.obs_param[5]
self.A1 = self.conf.obs_param[6]
self.B1 = self.conf.obs_param[7]
self.A2 = self.conf.obs_param[8]
self.B2 = self.conf.obs_param[9]
self.A3 = self.conf.obs_param[10]
self.B3 = self.conf.obs_param[11]
self.TARGET_STATE = self.conf.TARGET_STATE
def reward(self, weights, state, action=None):
''' Compute reward '''
# End-effector coordinates
x_ee, y_ee = [self.get_end_effector_position(state)[i] for i in range(2)]
# Penalties for the ellipses representing the obstacle
ell1_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC1)**2)/((self.A1/2)**2) + ((y_ee-self.YC1)**2)/((self.B1/2)**2) - 1.0)) + 1)/self.alpha
ell2_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC2)**2)/((self.A2/2)**2) + ((y_ee-self.YC2)**2)/((self.B2/2)**2) - 1.0)) + 1)/self.alpha
ell3_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC3)**2)/((self.A3/2)**2) + ((y_ee-self.YC3)**2)/((self.B3/2)**2) - 1.0)) + 1)/self.alpha
# Term pushing the agent to stay in the neighborhood of target
peak_rew = np.math.log(math.exp(self.alpha2*-(math.sqrt((x_ee-self.TARGET_STATE[0])**2 +0.1) - math.sqrt(0.1) - 0.1 + math.sqrt((y_ee-self.TARGET_STATE[1])**2 +0.1) - math.sqrt(0.1) - 0.1)) + 1)/self.alpha2
if action is not None:
u_cost = self.bound_control_cost(action)
else:
u_cost = 0
dist_cost = (x_ee-self.TARGET_STATE[0])**2 + (y_ee-self.TARGET_STATE[1])**2
r = self.scale*(- weights[0]*dist_cost + weights[1]*peak_rew - weights[3]*ell1_cost - weights[4]*ell2_cost - weights[5]*ell3_cost - weights[6]*u_cost + self.offset)
return r
def reward_batch(self, weights, state, action):
''' Compute reward using tensors. Batch-wise computation '''
partial_reward = np.array([self.reward(w, s) for w, s in zip(weights, state)])
# Redefine action-related cost in tensorflow version
u_cost = tf.reduce_sum((action**2 + self.conf.w_b*(action/self.conf.u_max)**10),axis=1)
r = self.scale*(- weights[:,6]*u_cost) + tf.convert_to_tensor(partial_reward, dtype=tf.float32)
return tf.reshape(r, [r.shape[0], 1])
class Car(Env):
'''
:param cost_function_parameters :
'''
metadata = {
"render_modes": [
"human", "rgb_array"
],
"render_fps": 4,
}
def __init__(self, conf):
self.conf = conf
super().__init__(conf)
# Rename reward parameters
self.offset = self.conf.cost_funct_param[0]
self.scale = self.conf.cost_funct_param[1]
self.alpha = self.conf.soft_max_param[0]
self.alpha2 = self.conf.soft_max_param[1]
self.XC1 = self.conf.obs_param[0]
self.YC1 = self.conf.obs_param[1]
self.XC2 = self.conf.obs_param[2]
self.YC2 = self.conf.obs_param[3]
self.XC3 = self.conf.obs_param[4]
self.YC3 = self.conf.obs_param[5]
self.A1 = self.conf.obs_param[6]
self.B1 = self.conf.obs_param[7]
self.A2 = self.conf.obs_param[8]
self.B2 = self.conf.obs_param[9]
self.A3 = self.conf.obs_param[10]
self.B3 = self.conf.obs_param[11]
self.TARGET_STATE = self.conf.TARGET_STATE
self.nx = conf.nx
self.nu = conf.na
def derivative(self, state, action):
''' Compute the derivative '''
# Dynamics gradient w.r.t control (1st order euler)
Fu = np.zeros((self.nx+1, self.nu))
Fu[2,0] = self.conf.dt
Fu[4,1] = self.conf.dt
if self.conf.NORMALIZE_INPUTS:
Fu[:-1] *= (1/self.conf.state_norm_arr[:-1,None])
return Fu
def augmented_derivative(self, state, action):
''' Partial derivatives of system dynamics w.r.t. x '''
# Compute Jacobians for discrete time dynamics
Fx = np.zeros((self.conf.nb_state-1,self.conf.nb_state-1))
Fu = np.zeros((self.conf.nb_state-1,self.conf.nb_action))
Fx[:self.conf.nb_state-1,:self.conf.nb_state-1] = np.array([[1, 0, -self.conf.dt*state[3]*math.sin(state[2]) -self.conf.dt**2*state[4]*math.sin(state[2])/2, self.conf.dt*math.cos(state[2]), self.conf.dt**2*math.cos(state[2])/2],
[0, 1, self.conf.dt*state[3]*math.cos(state[2]) +self.conf.dt**2*state[4]*math.cos(state[2])/2, self.conf.dt*math.sin(state[2]), self.conf.dt**2*math.sin(state[2])/2],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, self.conf.dt],
[0, 0, 0, 0, 1]])
Fu[2,0] = self.conf.dt
Fu[4,1] = self.conf.dt
return Fx, Fu
def simulate(self, state, action):
''' Simulate dynamics '''
state_next = np.zeros(self.nx+1)
state_next[0] = state[0] + self.conf.dt*state[3]*tf.cos(state[2]) + self.conf.dt**2*state[4]*tf.cos(state[2])/2
state_next[1] = state[1] + self.conf.dt*state[3]*tf.sin(state[2]) + self.conf.dt**2*state[4]*tf.sin(state[2])/2
state_next[2] = state[2] + self.conf.dt*action[0]
state_next[3] = state[3] + self.conf.dt*state[4]
state_next[4] = state[4] + self.conf.dt*action[1]
state_next[5] = state[5] + self.conf.dt
return state_next
def get_end_effector_position(self, state, recompute=True):
''' Compute end-effector position '''
p = np.zeros(3)
p[:2] = state[:2]
return p
def reward(self, weights, state, action=None):
''' Compute reward '''
# End-effector coordinates
x_ee, y_ee = self.get_end_effector_position(state)[:2]
# Penalties for the ellipses representing the obstacle
ell1_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC1)**2)/((self.A1/2)**2) + ((y_ee-self.YC1)**2)/((self.B1/2)**2) - 1.0)) + 1)/self.alpha
ell2_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC2)**2)/((self.A2/2)**2) + ((y_ee-self.YC2)**2)/((self.B2/2)**2) - 1.0)) + 1)/self.alpha
ell3_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC3)**2)/((self.A3/2)**2) + ((y_ee-self.YC3)**2)/((self.B3/2)**2) - 1.0)) + 1)/self.alpha
# Term pushing the agent to stay in the neighborhood of target
peak_rew = math.log(math.exp(self.alpha2*-(math.sqrt((x_ee-self.TARGET_STATE[0])**2 +0.1) - math.sqrt(0.1) - 0.1 + math.sqrt((y_ee-self.TARGET_STATE[1])**2 +0.1) - math.sqrt(0.1) - 0.1)) + 1)/self.alpha2
# Term pensalizing the control effort
if action is not None:
u_cost = self.bound_control_cost(action)
else:
u_cost = 0
dist_cost = (x_ee-self.TARGET_STATE[0])**2 + (y_ee-self.TARGET_STATE[1])**2
r = self.scale*(- weights[0]*dist_cost + weights[1]*peak_rew - weights[3]*ell1_cost - weights[4]*ell2_cost - weights[5]*ell3_cost - weights[6]*u_cost + self.offset)
return r
def reward_batch(self, weights, state, action):
''' Compute reward using tensors. Batch-wise computation '''
partial_reward = np.array([self.reward(w, s) for w, s in zip(weights, state)])
# Redefine action-related cost in tensorflow version
u_cost = tf.reduce_sum((action**2 + self.conf.w_b*(action/self.conf.u_max)**10),axis=1)
r = self.scale*(- weights[:,6]*u_cost) + tf.convert_to_tensor(partial_reward, dtype=tf.float32)
return tf.reshape(r, [r.shape[0], 1])
class CarPark(Car):
'''
:param cost_function_parameters :
'''
metadata = {
"render_modes": [
"human", "rgb_array"
],
"render_fps": 4,
}
def __init__(self, conf):
self.conf = conf
super().__init__(conf)
# Rename reward parameters
self.offset = self.conf.cost_funct_param[0]
self.scale = self.conf.cost_funct_param[1]
self.alpha = self.conf.soft_max_param[0]
self.alpha2 = self.conf.soft_max_param[1]
self.XC1 = self.conf.obs_param[0]
self.YC1 = self.conf.obs_param[1]
self.XC2 = self.conf.obs_param[2]
self.YC2 = self.conf.obs_param[3]
self.XC3 = self.conf.obs_param[4]
self.YC3 = self.conf.obs_param[5]
self.A1 = self.conf.obs_param[6]
self.B1 = self.conf.obs_param[7]
self.A2 = self.conf.obs_param[8]
self.B2 = self.conf.obs_param[9]
self.A3 = self.conf.obs_param[10]
self.B3 = self.conf.obs_param[11]
self.TARGET_STATE = self.conf.TARGET_STATE
self.nx = conf.nx
self.nu = conf.na
def check_ICS_feasible(self, state):
''' Check if ICS is not feasible '''
# check if ee is in the obstacles
x_ee, y_ee = self.get_end_effector_position(state)[:2]
theta_ee = state[2]
for i in range(len(self.conf.check_points_BF)):
check_points_WF_i = np.array([x_ee, y_ee]) + np.array([[math.cos(theta_ee), -math.sin(theta_ee)], [math.sin(theta_ee), math.cos(theta_ee)]]).dot(self.conf.check_points_BF[i,:])
obs_1 = self.obs_cost_fun(check_points_WF_i[0],check_points_WF_i[1],self.XC1,self.YC1,self.A1,self.B1)
obs_2 = self.obs_cost_fun(check_points_WF_i[0],check_points_WF_i[1],self.XC2,self.YC2,self.A2,self.B2)
obs_3 = self.obs_cost_fun(check_points_WF_i[0],check_points_WF_i[1],self.XC3,self.YC3,self.A3,self.B3)
feasible_flag = obs_1 < 0.5 and obs_2 < 0.5 and obs_3 < 0.5
if feasible_flag == 0:
return feasible_flag
return feasible_flag
def derivative(self, state, action):
''' Compute the derivative '''
# Dynamics gradient w.r.t control (1st order euler)
Fu = np.zeros((self.nx+1, self.nu))
Fu[3,0] = self.conf.dt
Fu[4,1] = self.conf.dt/self.conf.tau_delta
if self.conf.NORMALIZE_INPUTS:
Fu[:-1] *= (1/self.conf.state_norm_arr[:-1,None])
return Fu
def augmented_derivative(self, state, action):
''' Partial derivatives of system dynamics w.r.t. x '''
# Compute Jacobians for discrete time dynamics
Fx = np.zeros((self.conf.nb_state-1,self.conf.nb_state-1))
Fu = np.zeros((self.conf.nb_state-1,self.conf.nb_action))
Fx[:self.conf.nb_state-1,:self.conf.nb_state-1] = np.array([[1, 0, -self.conf.dt*state[3]*math.sin(state[2]), self.conf.dt*math.cos(state[2]), 0],
[0, 1, self.conf.dt*state[3]*math.cos(state[2]), self.conf.dt*math.sin(state[2]), 0],
[0, 0, 1, self.conf.dt*math.tan(state[4])/self.conf.L_delta, self.conf.dt*state[3]*mpmath.sec(state[4])**2/self.conf.L_delta],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1]])
Fu[3,0] = self.conf.dt
Fu[4,1] = self.conf.dt/self.conf.tau_delta
return Fx, Fu
def simulate(self, state, action):
''' Simulate dynamics '''
state_next = np.zeros(self.nx+1)
state_next[0] = state[0] + self.conf.dt*state[3]*math.cos(state[2])
state_next[1] = state[1] + self.conf.dt*state[3]*math.sin(state[2])
state_next[2] = state[2] + self.conf.dt*state[3]*math.tan(state[4])/self.conf.L_delta
state_next[3] = state[3] + self.conf.dt*action[0]
state_next[4] = state[4] + self.conf.dt*action[1]/self.conf.tau_delta
state_next[5] = state[5] + self.conf.dt
return state_next
def get_end_effector_position(self, state, recompute=True):
''' Compute end-effector position '''
p = np.zeros(3)
p[:2] = state[:2] + np.array([[math.cos(state[2]), -math.sin(state[2])], [math.sin(state[2]), math.cos(state[2])]]).dot(np.array([self.conf.L_delta/2,0]))
return p
def obs_cost_fun(self,x,y,x_step,y_step,Wx,Wy,fv=1,k=50):
k = self.conf.k_db
term1 = 4 + 4 * (y - y_step + Wy/2)**2 * k**2
term2 = 4 + 4 * (y - y_step - Wy/2)**2 * k**2
term3 = 4 + 4 * (x - x_step + Wx/2)**2 * k**2
term4 = 4 + 4 * (x - x_step - Wx/2)**2 * k**2
obs_cost = (term1)**(-1/2) * fv * (-np.sqrt(term2) / 2 + (y - y_step - Wy/2) * k) * (term3)**(-1/2) * (term2)**(-1/2) * (np.sqrt(term1) / 2 + (y - y_step + Wy/2) * k) * (term4)**(-1/2) * (np.sqrt(term3) / 2 + (x - x_step + Wx/2) * k) * (-np.sqrt(term4) / 2 + (x - x_step - Wx/2) * k)
return obs_cost
def reward(self, weights, state, action=None):
''' Compute reward '''
# End-effector coordinates
x_ee, y_ee = self.get_end_effector_position(state)[:2]
theta_ee = state[2]
obs_cost = 0
check_points_WF = np.dot(np.array([[np.cos(theta_ee), -np.sin(theta_ee)], [np.sin(theta_ee), np.cos(theta_ee)]]), self.conf.check_points_BF.T).T + np.array([x_ee, y_ee])
obs_cost += np.sum(self.obs_cost_fun(check_points_WF[:, 0], check_points_WF[:, 1], self.XC1, self.YC1, self.A1, self.B1))
obs_cost += np.sum(self.obs_cost_fun(check_points_WF[:, 0], check_points_WF[:, 1], self.XC2, self.YC2, self.A2, self.B2))
obs_cost += np.sum(self.obs_cost_fun(check_points_WF[:, 0], check_points_WF[:, 1], self.XC3, self.YC3, self.A3, self.B3))
# Term pushing the agent to stay in the neighborhood of target
peak_rew = math.log(math.exp(self.alpha2*-(math.sqrt((x_ee-self.TARGET_STATE[0])**2 + 0.1) - math.sqrt(0.1) - 0.1 + math.sqrt((y_ee-self.TARGET_STATE[1])**2 + 0.1) - math.sqrt(0.1) - 0.1 )) + 1)/self.alpha2
# Term pensalizing the control effort
if action is not None:
u_cost = self.bound_control_cost(action)
else:
u_cost = 0
dist_cost = (x_ee-self.TARGET_STATE[0])**2 + (y_ee-self.TARGET_STATE[1])**2
r = self.scale*(- weights[0]*dist_cost + weights[1]*peak_rew - weights[2]*state[3]**2 - weights[3]*obs_cost - weights[6]*u_cost + self.offset)
return r
def reward_batch(self, weights, state, action):
''' Compute reward using tensors. Batch-wise computation '''
partial_reward = np.array([self.reward(w, s) for w, s in zip(weights, state)])
# Redefine action-related cost in tensorflow version
u_cost = tf.reduce_sum((action**2 + self.conf.w_b*(action/self.conf.u_max)**10),axis=1)
r = self.scale*(- weights[:,6]*u_cost) + tf.convert_to_tensor(partial_reward, dtype=tf.float32)
return tf.reshape(r, [r.shape[0], 1])
class Manipulator(Env):
'''
:param cost_function_parameters :
'''
metadata = {
"render_modes": [
"human", "rgb_array"
],
"render_fps": 4,
}
def __init__(self, conf):
self.conf = conf
super().__init__(conf)
# Rename reward parameters
self.offset = self.conf.cost_funct_param[0]
self.scale = self.conf.cost_funct_param[1]
self.alpha = self.conf.soft_max_param[0]
self.alpha2 = self.conf.soft_max_param[1]
self.XC1 = self.conf.obs_param[0]
self.YC1 = self.conf.obs_param[1]
self.XC2 = self.conf.obs_param[2]
self.YC2 = self.conf.obs_param[3]
self.XC3 = self.conf.obs_param[4]
self.YC3 = self.conf.obs_param[5]
self.A1 = self.conf.obs_param[6]
self.B1 = self.conf.obs_param[7]
self.A2 = self.conf.obs_param[8]
self.B2 = self.conf.obs_param[9]
self.A3 = self.conf.obs_param[10]
self.B3 = self.conf.obs_param[11]
self.TARGET_STATE = self.conf.TARGET_STATE
def reward(self, weights, state, action=None):
''' Compute reward '''
# End-effector coordinates
x_ee, y_ee = [self.get_end_effector_position(state)[i] for i in range(2)]
# Penalties for the ellipses representing the obstacle
ell1_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC1)**2)/((self.A1/2)**2) + ((y_ee-self.YC1)**2)/((self.B1/2)**2) - 1.0)) + 1)/self.alpha
ell2_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC2)**2)/((self.A2/2)**2) + ((y_ee-self.YC2)**2)/((self.B2/2)**2) - 1.0)) + 1)/self.alpha
ell3_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC3)**2)/((self.A3/2)**2) + ((y_ee-self.YC3)**2)/((self.B3/2)**2) - 1.0)) + 1)/self.alpha
# Term pushing the agent to stay in the neighborhood of target
peak_rew = math.log(math.exp(self.alpha2*-(math.sqrt((x_ee-self.TARGET_STATE[0])**2 +0.1) - math.sqrt(0.1) - 0.1 + math.sqrt((y_ee-self.TARGET_STATE[1])**2 +0.1) - math.sqrt(0.1) - 0.1)) + 1)/self.alpha2
# Term penalizing the FINAL joint velocity
if weights[2] != 0:
vel_cost = state[self.nq:self.nx].dot(state[self.nq:self.nx])
else:
vel_cost = 0
if action is not None:
u_cost = self.bound_control_cost(action)
else:
u_cost = 0
dist_cost = (x_ee-self.TARGET_STATE[0])**2 + (y_ee-self.TARGET_STATE[1])**2
r = self.scale*(- weights[0]*dist_cost + weights[1]*peak_rew - weights[2]*vel_cost - weights[3]*ell1_cost - weights[4]*ell2_cost - weights[5]*ell3_cost - weights[6]*u_cost + self.offset) #- weights[2]*vel_cost
return r
def reward_batch(self, weights, state, action):
''' Compute reward using tensors. Batch-wise computation '''
partial_reward = np.array([self.reward(w, s) for w, s in zip(weights, state)])
# Redefine action-related cost in tensorflow version
u_cost = tf.reduce_sum((action**2 + self.conf.w_b*(action/self.conf.u_max)**10),axis=1)
r = self.scale*(- weights[:,6]*u_cost) + tf.convert_to_tensor(partial_reward, dtype=tf.float32)
return tf.reshape(r, [r.shape[0], 1])
class UR5(Env):
metadata = {
"render_modes": [
"human", "rgb_array"
],
"render_fps": 4,
}
def __init__(self, conf):
self.conf = conf
super().__init__(conf)
# Rename reward parameters
self.offset = self.conf.cost_funct_param[0]
self.scale = self.conf.cost_funct_param[1]
self.alpha = self.conf.soft_max_param[0]
self.alpha2 = self.conf.soft_max_param[1]
self.XC1 = self.conf.obs_param[0]
self.YC1 = self.conf.obs_param[1]
self.ZC1 = self.conf.obs_param[2]
self.XC2 = self.conf.obs_param[3]
self.YC2 = self.conf.obs_param[4]
self.ZC2 = self.conf.obs_param[5]
self.XC3 = self.conf.obs_param[6]
self.YC3 = self.conf.obs_param[7]
self.ZC3 = self.conf.obs_param[8]
self.A1 = self.conf.obs_param[9]
self.B1 = self.conf.obs_param[10]
self.C1 = self.conf.obs_param[11]
self.A2 = self.conf.obs_param[12]
self.B2 = self.conf.obs_param[13]
self.C2 = self.conf.obs_param[14]
self.A3 = self.conf.obs_param[15]
self.B3 = self.conf.obs_param[16]
self.C3 = self.conf.obs_param[17]
self.TARGET_STATE = self.conf.TARGET_STATE
def reward(self, weights, state, action=None):
''' Compute reward '''
# End-effector coordinates
x_ee, y_ee, z_ee = self.get_end_effector_position(state)
# Penalties for the ellipses representing the obstacle
ell1_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC1)**2)/((self.A1/2)**2) + ((y_ee-self.YC1)**2)/((self.B1/2)**2) + ((z_ee-self.ZC1)**2)/((self.C1/2)**2) - 1.0)) + 1)/self.alpha
ell2_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC2)**2)/((self.A2/2)**2) + ((y_ee-self.YC2)**2)/((self.B2/2)**2) + ((z_ee-self.ZC2)**2)/((self.C2/2)**2) - 1.0)) + 1)/self.alpha
ell3_cost = math.log(math.exp(self.alpha*-(((x_ee-self.XC3)**2)/((self.A3/2)**2) + ((y_ee-self.YC3)**2)/((self.B3/2)**2) + ((z_ee-self.ZC3)**2)/((self.C3/2)**2) - 1.0)) + 1)/self.alpha
# Term pushing the agent to stay in the neighborhood of target
peak_rew = math.log(math.exp(self.alpha2*-(math.sqrt((x_ee-self.TARGET_STATE[0])**2 +0.1) - math.sqrt(0.1) - 0.1 + math.sqrt((y_ee-self.TARGET_STATE[1])**2 +0.1) - math.sqrt(0.1) - 0.1 + math.sqrt((z_ee-self.TARGET_STATE[2])**2 +0.1) - math.sqrt(0.1) - 0.1)) + 1)/self.alpha2
if action is not None:
u_cost = action.dot(action)
else:
u_cost = 0
# Term penalizing the FINAL joint velocity
vel_cost = state[self.nq:self.nx].dot(state[self.nq:self.nx])
dist_cost = (x_ee-self.TARGET_STATE[0])**2 + (y_ee-self.TARGET_STATE[1])**2 + (z_ee-self.TARGET_STATE[2])**2
r = self.scale*(- weights[0]*dist_cost + weights[1]*peak_rew - weights[2]*vel_cost - weights[3]*ell1_cost - weights[4]*ell2_cost - weights[5]*ell3_cost - weights[6]*u_cost + self.offset)
return r
def reward_batch(self, weights, state, action):
''' Compute reward using tensors. Batch-wise computation '''
partial_reward = np.array([self.reward(w, s) for w, s in zip(weights, state)])
# Redefine action-related cost in tensorflow version
u_cost = tf.reduce_sum((action**2 + self.conf.w_b*(action/self.conf.u_max)**10),axis=1)
r = self.scale*(- weights[:,6]*u_cost) + tf.convert_to_tensor(partial_reward, dtype=tf.float32)
return tf.reshape(r, [r.shape[0], 1])