-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamics_script.py
725 lines (528 loc) · 20.2 KB
/
dynamics_script.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
from import_script import *
# ======================================================================
# Define dynamics functions
# ======================================================================
def g_nonlinear1(x):
return [x[0] - 0.05 * x[1], x[1] + 0.1 * math.sin(x[0])]
def g_3d_rot(x):
A = np.array([[0.9, -0.4, 0.0], [0.4, 0.5, 0.0], [0.0, 0.0, -0.05]])
result = A @ x
return result
def g_5d_mode0(xs):
result = [xs[0] + 0.35 + (0.1 * np.sin(xs[1])),
xs[1] + (0.15 * np.cos(xs[0])) + (0.05 * xs[2]),
(0.3 * xs[2]) + (0.4 * xs[3]),
(0.4 * xs[3]) + (0.05 * xs[4]),
(0.5 * xs[4])]
return result
def g_5d_mode1(xs):
result = [xs[0] - 0.35 + (0.1 * np.sin(xs[1])),
xs[1] + (0.15 * np.cos(xs[0])) + (0.05 * xs[2]),
(0.3 * xs[2]) + (0.4 * xs[3]),
(0.4 * xs[3]) + (0.05 * xs[4]),
(0.5 * xs[4])]
return result
def g_5d_mode2(xs):
result = [xs[0] + (0.15 * np.cos(xs[1])),
xs[1] + 0.35 + (0.1 * np.sin(xs[0])) + (0.05 * xs[2]),
(0.3 * xs[2]) + (0.4 * xs[3]),
(0.4 * xs[3]) + (0.05 * xs[4]),
(0.5 * xs[4])]
return result
def g_5d_mode3(xs):
result = [xs[0] + (0.15 * np.cos(xs[1])),
xs[1] - 0.35 + (0.1 * np.sin(xs[0])) + (0.05 * xs[2]),
(0.3 * xs[2]) + (0.4 * xs[3]),
(0.4 * xs[3]) + (0.05 * xs[4]),
(0.5 * xs[4])]
return result
def g_vector(x):
result = [np.sin(x[0] + x[1]),
np.cos(x[0] - x[1])]
return result
# ==================================================================================================================== #
# 2D linear
# ==================================================================================================================== #
def g_lin1(x):
A = np.array([[0.9, -0.4], [0.4, 0.5]])
result = A @ x
return result
def g_lin2(x):
A = np.array([[0.8, 0.5], [0, 0.5]])
result = A @ x
return result
def g_lin3(x):
A = np.array([[0.5, 0], [-0.5, 0.8]])
result = A @ x
return result
# ==================================================================================================================== #
# 3D nonlinear
# ==================================================================================================================== #
def g_2d_as_3d_mode0(x):
result = [x[0] + 0.5 + 0.2*np.sin(x[1]),
x[1] + 0.4*np.cos(x[0]),
0.75*x[2] + 0.1*np.cos(x[0])]
return result
def g_2d_as_3d_mode1(x):
result = [x[0] + -0.5 + 0.2*np.sin(x[1]),
x[1] + 0.4*np.cos(x[0]),
0.75*x[2] + 0.1*np.cos(x[0])]
return result
def g_2d_as_3d_mode2(x):
result = [x[0] + 0.4*np.cos(x[1]),
x[1] + 0.5 + 0.2*np.sin(x[0]),
0.75*x[2] + 0.1*np.cos(x[0])]
return result
def g_2d_as_3d_mode3(x):
result = [x[0] + 0.4*np.cos(x[1]),
x[1] + -0.5 + 0.2*np.sin(x[0]),
0.75*x[2] + 0.1*np.cos(x[0])]
return result
# ==================================================================================================================== #
# 2D nonlinear
# ==================================================================================================================== #
def g_2d_mode0(x):
result = [x[0] + 0.5 + 0.2*np.sin(x[1]),
x[1] + 0.4*np.cos(x[0])]
return result
def g_2d_mode1(x):
result = [x[0] + -0.5 + 0.2*np.sin(x[1]),
x[1] + 0.4*np.cos(x[0])]
return result
def g_2d_mode2(x):
result = [x[0] + 0.4*np.cos(x[1]),
x[1] + 0.5 + 0.2*np.sin(x[0])]
return result
def g_2d_mode3(x):
result = [x[0] + 0.4*np.cos(x[1]),
x[1] + -0.5 + 0.2*np.sin(x[0])]
return result
# ==================================================================================================================== #
# 3D dubins car
# ==================================================================================================================== #
dubins_ts = 0.1
def g_3d_mode1(xs, Ts=dubins_ts):
u = 10
omega = 5
phi = -0.3
result = [xs[0] + Ts * u * np.cos(xs[2]),
xs[1] + Ts * u * np.sin(xs[2]),
xs[2] + (phi - xs[2]) * Ts * omega]
return result
def g_3d_mode2(xs, Ts=dubins_ts):
u = 10
omega = 5
phi = -0.15
result = [xs[0] + Ts * u * np.cos(xs[2]),
xs[1] + Ts * u * np.sin(xs[2]),
xs[2] + (phi - xs[2]) * Ts * omega]
return result
def g_3d_mode3(xs, Ts=dubins_ts):
u = 10
omega = 5
phi = 0.
result = [xs[0] + Ts * u * np.cos(xs[2]),
xs[1] + Ts * u * np.sin(xs[2]),
xs[2] + (phi - xs[2]) * Ts * omega]
return result
def g_3d_mode4(xs, Ts=dubins_ts):
u = 10
omega = 5
phi = 0.3
result = [xs[0] + Ts * u * np.cos(xs[2]),
xs[1] + Ts * u * np.sin(xs[2]),
xs[2] + (phi - xs[2]) * Ts * omega]
return result
def g_3d_mode5(xs, Ts=dubins_ts):
u = 10
omega = 5
phi = 0.15
result = [xs[0] + Ts * u * np.cos(xs[2]),
xs[1] + Ts * u * np.sin(xs[2]),
xs[2] + (phi - xs[2]) * Ts * omega]
return result
def g_3d_mode6(xs, Ts=dubins_ts):
u = 10
omega = 5
phi = 0.45
result = [xs[0] + Ts * u * np.cos(xs[2]),
xs[1] + Ts * u * np.sin(xs[2]),
xs[2] + (phi - xs[2]) * Ts * omega]
return result
def g_3d_mode7(xs, Ts=dubins_ts):
u = 10
omega = 5
phi = -0.45
result = [xs[0] + Ts * u * np.cos(xs[2]),
xs[1] + Ts * u * np.sin(xs[2]),
xs[2] + (phi - xs[2]) * Ts * omega]
return result
# ==================================================================================================================== #
# 4D unicycle
# ==================================================================================================================== #
def g_unicycle_mode0(x):
v = 0.5
dt = 0.5
theta_dot = 0.0
k1 = [x[2],
x[3],
-v * np.sin(x[4]) * theta_dot,
v * np.cos(x[4]) * theta_dot,
theta_dot]
k2 = [x[2] + dt*k1[2]/2.0,
x[3] + dt*k1[3]/2.0,
-v * np.sin(x[4] + dt*k1[4]/2.0) * theta_dot,
v * np.cos(x[4] + dt*k1[4]/2.0) * theta_dot,
theta_dot]
k3 = [x[2] + dt*k2[2]/2.0,
x[3] + dt*k2[3]/2.0,
-v * np.sin(x[4] + dt*k2[4]/2.0) * theta_dot,
v * np.cos(x[4] + dt*k2[4]/2.0) * theta_dot,
theta_dot]
k4 = [x[2] + dt*k3[2],
x[3] + dt*k3[3],
-v * np.sin(x[4] + dt*k3[4]) * theta_dot,
v * np.cos(x[4] + dt*k3[4]) * theta_dot,
theta_dot]
result = [x[0] + 1.0/6.0 * (k1[0] + 2.0*k2[0] + 2.0*k3[0] + k4[0]) * dt,
x[1] + 1.0/6.0 * (k1[1] + 2.0*k2[1] + 2.0*k3[1] + k4[1]) * dt,
x[2] + 1.0/6.0 * (k1[2] + 2.0*k2[2] + 2.0*k3[2] + k4[2]) * dt,
x[3] + 1.0/6.0 * (k1[3] + 2.0*k2[3] + 2.0*k3[3] + k4[3]) * dt,
x[4] + 1.0/6.0 * (k1[4] + 2.0*k2[4] + 2.0*k3[4] + k4[4]) * dt]
return result
def g_unicycle_mode1(x):
v = 0.5
dt = 0.5
theta_dot = -0.25
k1 = [x[2],
x[3],
-v * np.sin(x[4]) * theta_dot,
v * np.cos(x[4]) * theta_dot,
theta_dot]
k2 = [x[2] + dt*k1[2]/2.0,
x[3] + dt*k1[3]/2.0,
-v * np.sin(x[4] + dt*k1[4]/2.0) * theta_dot,
v * np.cos(x[4] + dt*k1[4]/2.0) * theta_dot,
theta_dot]
k3 = [x[2] + dt*k2[2]/2.0,
x[3] + dt*k2[3]/2.0,
-v * np.sin(x[4] + dt*k2[4]/2.0) * theta_dot,
v * np.cos(x[4] + dt*k2[4]/2.0) * theta_dot,
theta_dot]
k4 = [x[2] + dt*k3[2],
x[3] + dt*k3[3],
-v * np.sin(x[4] + dt*k3[4]) * theta_dot,
v * np.cos(x[4] + dt*k3[4]) * theta_dot,
theta_dot]
result = [x[0] + 1.0/6.0 * (k1[0] + 2.0*k2[0] + 2.0*k3[0] + k4[0]) * dt,
x[1] + 1.0/6.0 * (k1[1] + 2.0*k2[1] + 2.0*k3[1] + k4[1]) * dt,
x[2] + 1.0/6.0 * (k1[2] + 2.0*k2[2] + 2.0*k3[2] + k4[2]) * dt,
x[3] + 1.0/6.0 * (k1[3] + 2.0*k2[3] + 2.0*k3[3] + k4[3]) * dt,
x[4] + 1.0/6.0 * (k1[4] + 2.0*k2[4] + 2.0*k3[4] + k4[4]) * dt]
return result
def g_unicycle_mode2(x):
v = 0.5
dt = 0.5
theta_dot = 0.25
k1 = [x[2],
x[3],
-v * np.sin(x[4]) * theta_dot,
v * np.cos(x[4]) * theta_dot,
theta_dot]
k2 = [x[2] + dt*k1[2]/2.0,
x[3] + dt*k1[3]/2.0,
-v * np.sin(x[4] + dt*k1[4]/2.0) * theta_dot,
v * np.cos(x[4] + dt*k1[4]/2.0) * theta_dot,
theta_dot]
k3 = [x[2] + dt*k2[2]/2.0,
x[3] + dt*k2[3]/2.0,
-v * np.sin(x[4] + dt*k2[4]/2.0) * theta_dot,
v * np.cos(x[4] + dt*k2[4]/2.0) * theta_dot,
theta_dot]
k4 = [x[2] + dt*k3[2],
x[3] + dt*k3[3],
-v * np.sin(x[4] + dt*k3[4]) * theta_dot,
v * np.cos(x[4] + dt*k3[4]) * theta_dot,
theta_dot]
result = [x[0] + 1.0/6.0 * (k1[0] + 2.0*k2[0] + 2.0*k3[0] + k4[0]) * dt,
x[1] + 1.0/6.0 * (k1[1] + 2.0*k2[1] + 2.0*k3[1] + k4[1]) * dt,
x[2] + 1.0/6.0 * (k1[2] + 2.0*k2[2] + 2.0*k3[2] + k4[2]) * dt,
x[3] + 1.0/6.0 * (k1[3] + 2.0*k2[3] + 2.0*k3[3] + k4[3]) * dt,
x[4] + 1.0/6.0 * (k1[4] + 2.0*k2[4] + 2.0*k3[4] + k4[4]) * dt]
return result
def g_unicycle_4d_mode0(x):
# states are x, y, theta, phi, phi being a steering angle and phi_dot being the rate
v = 1.2
L = 0.5
dt = 0.5
phi_dot = 0.0
k1 = [v * np.cos(x[2]),
v * np.sin(x[2]),
v/L * np.tan(x[3]),
phi_dot]
k2 = [v * np.cos(x[2] + dt*k1[2]/2.0),
v * np.sin(x[2] + dt*k1[2]/2.0),
v/L * np.tan(x[3] + dt*k1[3]/2.0),
phi_dot]
k3 = [v * np.cos(x[2] + dt * k2[2] / 2.0),
v * np.sin(x[2] + dt * k2[2] / 2.0),
v/L * np.tan(x[3] + dt * k2[3] / 2.0),
phi_dot]
k4 = [v * np.cos(x[2] + dt * k3[2]),
v * np.sin(x[2] + dt * k3[2]),
v/L * np.tan(x[3] + dt * k3[3]),
phi_dot]
result = [x[0] + 1.0/6.0 * (k1[0] + 2.0*k2[0] + 2.0*k3[0] + k4[0]) * dt,
x[1] + 1.0/6.0 * (k1[1] + 2.0*k2[1] + 2.0*k3[1] + k4[1]) * dt,
x[2] + 1.0/6.0 * (k1[2] + 2.0*k2[2] + 2.0*k3[2] + k4[2]) * dt,
x[3] + 1.0/6.0 * (k1[3] + 2.0*k2[3] + 2.0*k3[3] + k4[3]) * dt]
return result
def g_unicycle_4d_mode1(x):
v = 1.2
L = 0.5
dt = 0.5
phi_dot = -0.5
k1 = [v * np.cos(x[2]),
v * np.sin(x[2]),
v/L * np.tan(x[3]),
phi_dot]
k2 = [v * np.cos(x[2] + dt*k1[2]/2.0),
v * np.sin(x[2] + dt*k1[2]/2.0),
v/L * np.tan(x[3] + dt*k1[3]/2.0),
phi_dot]
k3 = [v * np.cos(x[2] + dt * k2[2] / 2.0),
v * np.sin(x[2] + dt * k2[2] / 2.0),
v/L * np.tan(x[3] + dt * k2[3] / 2.0),
phi_dot]
k4 = [v * np.cos(x[2] + dt * k3[2]),
v * np.sin(x[2] + dt * k3[2]),
v/L * np.tan(x[3] + dt * k3[3]),
phi_dot]
result = [x[0] + 1.0/6.0 * (k1[0] + 2.0*k2[0] + 2.0*k3[0] + k4[0]) * dt,
x[1] + 1.0/6.0 * (k1[1] + 2.0*k2[1] + 2.0*k3[1] + k4[1]) * dt,
x[2] + 1.0/6.0 * (k1[2] + 2.0*k2[2] + 2.0*k3[2] + k4[2]) * dt,
x[3] + 1.0/6.0 * (k1[3] + 2.0*k2[3] + 2.0*k3[3] + k4[3]) * dt]
return result
def g_unicycle_4d_mode2(x):
v = 1.2
L = 0.5
dt = 0.5
phi_dot = .5
k1 = [v * np.cos(x[2]),
v * np.sin(x[2]),
v/L * np.tan(x[3]),
phi_dot]
k2 = [v * np.cos(x[2] + dt*k1[2]/2.0),
v * np.sin(x[2] + dt*k1[2]/2.0),
v/L * np.tan(x[3] + dt*k1[3]/2.0),
phi_dot]
k3 = [v * np.cos(x[2] + dt * k2[2] / 2.0),
v * np.sin(x[2] + dt * k2[2] / 2.0),
v/L * np.tan(x[3] + dt * k2[3] / 2.0),
phi_dot]
k4 = [v * np.cos(x[2] + dt * k3[2]),
v * np.sin(x[2] + dt * k3[2]),
v/L * np.tan(x[3] + dt * k3[3]),
phi_dot]
result = [x[0] + 1.0/6.0 * (k1[0] + 2.0*k2[0] + 2.0*k3[0] + k4[0]) * dt,
x[1] + 1.0/6.0 * (k1[1] + 2.0*k2[1] + 2.0*k3[1] + k4[1]) * dt,
x[2] + 1.0/6.0 * (k1[2] + 2.0*k2[2] + 2.0*k3[2] + k4[2]) * dt,
x[3] + 1.0/6.0 * (k1[3] + 2.0*k2[3] + 2.0*k3[3] + k4[3]) * dt]
return result
def unicycle_left(xs):
x = xs[0]
y = xs[1]
x_dot = xs[2]
y_dot = xs[3]
dir_ = np.array([x_dot, y_dot])
v = np.linalg.norm(dir_, ord=2)
theta = math.atan2(y_dot, x_dot)
theta_des = math.pi / 4.5
des_theta = theta + theta_des
omega = 0.5
u2 = omega * v * np.cos(theta)
u1 = -u2 * np.tan(theta)
state = [x,
y,
v,
theta]
dt = 2.0
k1 = [v * np.cos(theta),
v * np.sin(theta),
u1 * np.cos(theta) + u2 * np.sin(theta),
(- np.sin(theta) * u2 + np.cos(theta) * u1) / v]
new_theta = theta + (dt / 2.) * k1[3]
u2 = omega * v * np.cos(new_theta)
u1 = -u2 * np.tan(new_theta)
k2 = [(v + (dt / 2.) * k1[2]) * np.cos(new_theta),
(v + (dt / 2.) * k1[2]) * np.sin(new_theta),
u1 * np.cos(new_theta) + u2 * np.sin(new_theta),
(- np.sin(new_theta) * u1 + np.cos(new_theta) * u2) / (v + (dt / 2.) * k1[2])]
new_theta = theta + (dt / 2.) * k2[3]
u2 = omega * v * np.cos(new_theta)
u1 = -u2 * np.tan(new_theta)
k3 = [(v + (dt / 2.) * k2[2]) * np.cos(new_theta),
(v + (dt / 2.) * k2[2]) * np.sin(new_theta),
u1 * np.cos(new_theta) + u2 * np.sin(new_theta),
(- np.sin(new_theta) * u1 + np.cos(new_theta) * u2) / (v + (dt / 2.) * k2[2])]
new_theta = theta + dt * k3[3]
u2 = omega * v * np.cos(new_theta)
u1 = -u2 * np.tan(new_theta)
k4 = [(v + dt * k3[2]) * np.cos(new_theta),
(v + dt * k3[2]) * np.sin(new_theta),
u1 * np.cos(new_theta) + u2 * np.sin(new_theta),
(- np.sin(new_theta) * u1 + np.cos(new_theta) * u2) / (v + dt * k3[2])]
new_x = [state[0] + 1.0 / 6.0 * (k1[0] + 2.0 * k2[0] + 2.0 * k3[0] + k4[0]) * dt,
state[1] + 1.0 / 6.0 * (k1[1] + 2.0 * k2[1] + 2.0 * k3[1] + k4[1]) * dt,
state[2] + 1.0 / 6.0 * (k1[2] + 2.0 * k2[2] + 2.0 * k3[2] + k4[2]) * dt,
state[3] + 1.0 / 6.0 * (k1[3] + 2.0 * k2[3] + 2.0 * k3[3] + k4[3]) * dt]
x_new = new_x[0]
y_new = new_x[1]
x_dot_new = new_x[2] * np.cos(new_x[3])
y_dot_new = new_x[2] * np.sin(new_x[3])
result = [x_new, y_new, x_dot_new, y_dot_new]
return result
def unicycle_right(xs):
x = xs[0]
y = xs[1]
x_dot = xs[2]
y_dot = xs[3]
dir_ = np.array([x_dot, y_dot])
v = np.linalg.norm(dir_, ord=2)
theta = math.atan2(y_dot, x_dot)
theta_des = -math.pi / 4.5
des_theta = theta + theta_des
omega = -0.5
u2 = omega * v * np.cos(theta)
u1 = -u2 * np.tan(theta)
state = [x,
y,
v,
theta]
dt = 2.0
k1 = [v * np.cos(theta),
v * np.sin(theta),
u1 * np.cos(theta) + u2 * np.sin(theta),
(- np.sin(theta) * u2 + np.cos(theta) * u1) / v]
new_theta = theta + (dt / 2.) * k1[3]
u2 = omega * v * np.cos(new_theta)
u1 = -u2 * np.tan(new_theta)
k2 = [(v + (dt / 2.) * k1[2]) * np.cos(new_theta),
(v + (dt / 2.) * k1[2]) * np.sin(new_theta),
u1 * np.cos(new_theta) + u2 * np.sin(new_theta),
(- np.sin(new_theta) * u1 + np.cos(new_theta) * u2) / (v + (dt / 2.) * k1[2])]
new_theta = theta + (dt / 2.) * k2[3]
u2 = omega * v * np.cos(new_theta)
u1 = -u2 * np.tan(new_theta)
k3 = [(v + (dt / 2.) * k2[2]) * np.cos(new_theta),
(v + (dt / 2.) * k2[2]) * np.sin(new_theta),
u1 * np.cos(new_theta) + u2 * np.sin(new_theta),
(- np.sin(new_theta) * u1 + np.cos(new_theta) * u2) / (v + (dt / 2.) * k2[2])]
new_theta = theta + dt * k3[3]
u2 = omega * v * np.cos(new_theta)
u1 = -u2 * np.tan(new_theta)
k4 = [(v + dt * k3[2]) * np.cos(new_theta),
(v + dt * k3[2]) * np.sin(new_theta),
u1 * np.cos(new_theta) + u2 * np.sin(new_theta),
(- np.sin(new_theta) * u1 + np.cos(new_theta) * u2) / (v + dt * k3[2])]
new_x = [state[0] + 1.0 / 6.0 * (k1[0] + 2.0 * k2[0] + 2.0 * k3[0] + k4[0]) * dt,
state[1] + 1.0 / 6.0 * (k1[1] + 2.0 * k2[1] + 2.0 * k3[1] + k4[1]) * dt,
state[2] + 1.0 / 6.0 * (k1[2] + 2.0 * k2[2] + 2.0 * k3[2] + k4[2]) * dt,
state[3] + 1.0 / 6.0 * (k1[3] + 2.0 * k2[3] + 2.0 * k3[3] + k4[3]) * dt]
x_new = new_x[0]
y_new = new_x[1]
x_dot_new = new_x[2] * np.cos(new_x[3])
y_dot_new = new_x[2] * np.sin(new_x[3])
result = [x_new, y_new, x_dot_new, y_dot_new]
return result
def unicycle_straight(xs):
x = xs[0]
y = xs[1]
x_dot = xs[2]
y_dot = xs[3]
dt = 2.0
x_new = x + dt*x_dot
y_new = y + dt * y_dot
x_dot_new = x_dot
y_dot_new = y_dot
result = [x_new, y_new, x_dot_new, y_dot_new]
return result
def unicycle_slow(xs):
x = xs[0]
y = xs[1]
x_dot = xs[2]
y_dot = xs[3]
dt = 2.0
x_dot_new = x_dot/2.0
y_dot_new = y_dot/2.0
x_new = x + dt * (x_dot + x_dot_new)/2.0
y_new = y + dt * (y_dot + y_dot_new)/2.0
result = [x_new, y_new, x_dot_new, y_dot_new]
return result
def unicycle_speed(xs):
x = xs[0]
y = xs[1]
x_dot = xs[2]
y_dot = xs[3]
dt = 2.0
x_dot_new = x_dot * 1.5
y_dot_new = y_dot * 1.5
x_new = x + dt * (x_dot + x_dot_new)/2.0
y_new = y + dt * (y_dot + y_dot_new)/2.0
result = [x_new, y_new, x_dot_new, y_dot_new]
return result
def f(x, g, known_fnc, process_dist=None):
if known_fnc is not None:
x_known = known_fnc(x)
output = np.add(x_known, g(x))
else:
output = g(x)
n_dims_out = len(output)
noise = [0] * n_dims_out
if process_dist is not None:
sig = process_dist["sig"]
noise = np.random.uniform(-sig, sig, size=(n_dims_out,))
return np.add(output, noise)
def known_part(x):
return x
def generate_training_data(unknown_fnc, domain, data_num, known_fnc=None, random_seed=11, n_dims_out=-1,
process_dist=None, measurement_dist=None):
# This function generates i.i.d. data points across the domain and adds noise from a specified distribution
f_sub = unknown_fnc
n_dims_in = len(domain)
if n_dims_out > 0:
n_dims_out = n_dims_out
else:
n_dims_out = n_dims_in
np.random.seed(random_seed)
keys = list(domain)
x_train = [np.random.uniform(domain[k][0], domain[k][1], data_num) for k in keys]
x_train = np.reshape(x_train, [n_dims_in, data_num])
y_train = [f_sub([x_train[idx_i][idx_j] for idx_i in range(n_dims_in)]) for idx_j in range(data_num)]
y_train = np.transpose(np.reshape(y_train, [data_num, n_dims_out]))
if process_dist is not None:
# this is essentially the same as measurement noise for this method of generating data
sig = process_dist["sig"]
dist = "uniform"
if "dist" in list(process_dist):
dist = process_dist["dist"]
if dist is "uniform":
noise = np.random.uniform(-sig, sig, size=(n_dims_out, data_num))
elif dist is "normal":
noise = np.random.normal(0., sig, size=(n_dims_out, data_num))
elif dist is "multi_norm":
noise = np.random.multivariate_normal(process_dist["mu"], np.diag(sig), data_num).transpose()
else:
error_print("Not an implemented noise distribution. Please check process distribution inputs.")
y_train += noise
# TODO, make measurement noise work properly and allow for known parts of the function
if measurement_dist is not None:
sig = measurement_dist["sig"]
noise = np.random.uniform(-sig, sig, size=(n_dims_out, data_num))
y_train += noise
if known_fnc is not None:
y_train -= np.transpose(
np.reshape([known_fnc([x_train[idx_i][idx_j] for idx_i in range(n_dims_in)]) for idx_j in range(data_num)],
[data_num, n_dims_out]))
assert np.size(x_train) == np.size(y_train)
return x_train, y_train