-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_dynamic.py
More file actions
491 lines (407 loc) · 15.5 KB
/
benchmark_dynamic.py
File metadata and controls
491 lines (407 loc) · 15.5 KB
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
#!/usr/bin/env python3
"""
Dynamic Scenarios: Moving Targets and Obstacles
Tests lightlike observer performance in dynamic environments:
1. Moving target (tracking)
2. Moving obstacle (avoidance)
3. Both moving (complex dynamics)
4. Oscillating target (periodic motion)
5. Evasive obstacle (adversarial)
These scenarios are likely to create oscillations where lightlike
observer should provide significant benefit.
"""
import sys
import numpy as np
sys.path.insert(0, "/home/user/Eigen-Geometric-Control") # noqa: E402
import pandas as pd # noqa: E402
from src import ( # noqa: E402
compute_change_stability,
compute_ds2,
compute_gradient,
detect_oscillation,
forward_kinematics,
lightlike_damping_factor,
)
def run_dynamic_baseline(
theta_init=(-1.4, 1.2),
target_trajectory=None,
obstacle_trajectory=None,
n_ticks=200,
eta=0.12,
Go=4.0,
lam=0.02,
L1=0.9,
L2=0.9,
):
"""Run baseline without lightlike observer in dynamic environment"""
theta1, theta2 = theta_init
rows = []
for t in range(n_ticks):
# Get current target and obstacle positions
target = np.array(target_trajectory(t))
obstacle_center = np.array(obstacle_trajectory(t))
obstacle_radius = 0.25
# Compute current state
x, y = forward_kinematics(theta1, theta2, L1, L2)
ds2_total, components = compute_ds2(
theta1, theta2, target, obstacle_center, obstacle_radius, Go, lam, L1, L2
)
grad, grad_norm = compute_gradient(
theta1, theta2, target, obstacle_center, obstacle_radius, Go, lam, L1, L2
)
# Standard gradient update (no damping)
delta = -eta * grad
theta1_new = theta1 + delta[0]
theta2_new = theta2 + delta[1]
# Compute change/stability metrics
C, S, ds2_CS = compute_change_stability(delta, 1e-3)
# Record state
rows.append(
{
"tick": t,
"theta1_rad": theta1,
"theta2_rad": theta2,
"x": x,
"y": y,
"target_x": target[0],
"target_y": target[1],
"obs_x": obstacle_center[0],
"obs_y": obstacle_center[1],
"ds2_total": ds2_total,
"grad_norm": grad_norm,
"d_obs": components["d_obs"],
}
)
# Update for next iteration
theta1, theta2 = theta1_new, theta2_new
return pd.DataFrame(rows)
def run_dynamic_lightlike(
theta_init=(-1.4, 1.2),
target_trajectory=None,
obstacle_trajectory=None,
n_ticks=200,
eta=0.12,
Go=4.0,
lam=0.02,
L1=0.9,
L2=0.9,
window=5,
threshold=0.98,
damping_scale=0.7,
):
"""Run WITH lightlike observer in dynamic environment"""
theta1, theta2 = theta_init
state_history = []
rows = []
for t in range(n_ticks):
# Get current target and obstacle positions
target = np.array(target_trajectory(t))
obstacle_center = np.array(obstacle_trajectory(t))
obstacle_radius = 0.25
# Compute current state
x, y = forward_kinematics(theta1, theta2, L1, L2)
state = np.array([theta1, theta2])
state_history.append(state)
ds2_total, components = compute_ds2(
theta1, theta2, target, obstacle_center, obstacle_radius, Go, lam, L1, L2
)
grad, grad_norm = compute_gradient(
theta1, theta2, target, obstacle_center, obstacle_radius, Go, lam, L1, L2
)
# LIGHTLIKE OBSERVER
oscillating = False
damping = 0.0
osc_strength = 0.0
if len(state_history) >= window:
oscillating, osc_strength = detect_oscillation(
state_history, window=window, threshold=threshold
)
if oscillating:
damping = lightlike_damping_factor(osc_strength) * damping_scale
# Apply damped gradient update
delta = -(1.0 - damping) * eta * grad
theta1_new = theta1 + delta[0]
theta2_new = theta2 + delta[1]
# Compute change/stability metrics
C, S, ds2_CS = compute_change_stability(delta, 1e-3)
# Record state
rows.append(
{
"tick": t,
"theta1_rad": theta1,
"theta2_rad": theta2,
"x": x,
"y": y,
"target_x": target[0],
"target_y": target[1],
"obs_x": obstacle_center[0],
"obs_y": obstacle_center[1],
"ds2_total": ds2_total,
"grad_norm": grad_norm,
"d_obs": components["d_obs"],
"oscillating": oscillating,
"osc_strength": osc_strength,
"damping": damping,
}
)
# Update for next iteration
theta1, theta2 = theta1_new, theta2_new
return pd.DataFrame(rows)
def analyze_tracking(df, scenario_name):
"""Analyze tracking performance"""
# Tracking error
tracking_error = np.sqrt((df["x"] - df["target_x"]) ** 2 + (df["y"] - df["target_y"]) ** 2)
# Oscillation detection (gradient reversals)
grad_norms = df["grad_norm"].values
oscillations = 0
for i in range(10, len(grad_norms) - 1):
if grad_norms[i] > grad_norms[i - 1] * 1.3: # Gradient spike
oscillations += 1
# Collision risk (how close to obstacle)
min_clearance = df["d_obs"].min()
near_collisions = (df["d_obs"] < 0.30).sum() # Within 5cm of collision
metrics = {
"scenario": scenario_name,
"mean_tracking_error_mm": tracking_error.mean() * 1000,
"max_tracking_error_mm": tracking_error.max() * 1000,
"final_tracking_error_mm": tracking_error.iloc[-1] * 1000,
"tracking_variance": tracking_error.std() * 1000,
"oscillations": oscillations,
"min_clearance_m": min_clearance,
"near_collisions": near_collisions,
}
if "damping" in df.columns:
metrics["lightlike_activations"] = (df["damping"] > 0).sum()
metrics["max_damping"] = df["damping"].max()
return metrics
def main(): # noqa: C901
print("=" * 80)
print("DYNAMIC SCENARIOS: Moving Targets and Obstacles".center(80))
print("=" * 80)
print()
print("Testing lightlike observer in dynamic environments where")
print("oscillations are likely due to target/obstacle motion.")
print()
results = []
# Scenario 1: Moving target (linear motion)
print("Scenario 1: Linear moving target (tracking task)...")
def target_linear(t):
return [1.0 + 0.002 * t, 0.3 + 0.001 * t]
def obstacle_static(t):
return [0.6, 0.1]
df_base_1 = run_dynamic_baseline(
target_trajectory=target_linear, obstacle_trajectory=obstacle_static, n_ticks=150
)
df_light_1 = run_dynamic_lightlike(
target_trajectory=target_linear, obstacle_trajectory=obstacle_static, n_ticks=150
)
results.append(
{
"baseline": analyze_tracking(df_base_1, "Moving Target - Baseline"),
"lightlike": analyze_tracking(df_light_1, "Moving Target - Lightlike"),
}
)
# Scenario 2: Oscillating target (periodic motion)
print("Scenario 2: Oscillating target (tracking periodic motion)...")
def target_oscillate(t):
return [1.2 + 0.1 * np.sin(t * 0.3), 0.3 + 0.08 * np.cos(t * 0.3)]
df_base_2 = run_dynamic_baseline(
target_trajectory=target_oscillate, obstacle_trajectory=obstacle_static, n_ticks=150
)
df_light_2 = run_dynamic_lightlike(
target_trajectory=target_oscillate, obstacle_trajectory=obstacle_static, n_ticks=150
)
results.append(
{
"baseline": analyze_tracking(df_base_2, "Oscillating Target - Baseline"),
"lightlike": analyze_tracking(df_light_2, "Oscillating Target - Lightlike"),
}
)
# Scenario 3: Moving obstacle (avoidance)
print("Scenario 3: Moving obstacle (dynamic avoidance)...")
def target_fixed(t):
return [1.2, 0.3]
def obstacle_moving(t):
return [0.5 + 0.003 * t, 0.15 + 0.001 * t]
df_base_3 = run_dynamic_baseline(
target_trajectory=target_fixed, obstacle_trajectory=obstacle_moving, n_ticks=150
)
df_light_3 = run_dynamic_lightlike(
target_trajectory=target_fixed, obstacle_trajectory=obstacle_moving, n_ticks=150
)
results.append(
{
"baseline": analyze_tracking(df_base_3, "Moving Obstacle - Baseline"),
"lightlike": analyze_tracking(df_light_3, "Moving Obstacle - Lightlike"),
}
)
# Scenario 4: Both moving (complex dynamics)
print("Scenario 4: Both target and obstacle moving (complex dynamics)...")
df_base_4 = run_dynamic_baseline(
target_trajectory=target_linear, obstacle_trajectory=obstacle_moving, n_ticks=150
)
df_light_4 = run_dynamic_lightlike(
target_trajectory=target_linear, obstacle_trajectory=obstacle_moving, n_ticks=150
)
results.append(
{
"baseline": analyze_tracking(df_base_4, "Both Moving - Baseline"),
"lightlike": analyze_tracking(df_light_4, "Both Moving - Lightlike"),
}
)
# Scenario 5: Evasive obstacle (approaches end-effector)
print("Scenario 5: Evasive obstacle (adversarial scenario)...")
def obstacle_evasive(t):
# Obstacle tries to block path to target
return [0.8 + 0.002 * t, 0.2 + 0.002 * t]
df_base_5 = run_dynamic_baseline(
target_trajectory=target_fixed,
obstacle_trajectory=obstacle_evasive,
n_ticks=150,
Go=6.0, # Stronger repulsion
)
df_light_5 = run_dynamic_lightlike(
target_trajectory=target_fixed, obstacle_trajectory=obstacle_evasive, n_ticks=150, Go=6.0
)
results.append(
{
"baseline": analyze_tracking(df_base_5, "Evasive Obstacle - Baseline"),
"lightlike": analyze_tracking(df_light_5, "Evasive Obstacle - Lightlike"),
}
)
# Display results
print()
print("=" * 80)
print("RESULTS: Dynamic Tracking Performance".center(80))
print("=" * 80)
print()
print("Mean Tracking Error (mm):")
print("-" * 80)
for r in results:
base = r["baseline"]
light = r["lightlike"]
improvement = (
(base["mean_tracking_error_mm"] - light["mean_tracking_error_mm"])
/ base["mean_tracking_error_mm"]
* 100
)
arrow = "↓" if improvement > 0 else "↑"
scenario = base["scenario"].replace(" - Baseline", "")
print(
f"{scenario:<30} {base['mean_tracking_error_mm']:>7.1f}mm → {light['mean_tracking_error_mm']:>7.1f}mm "
f"{arrow} {abs(improvement):>5.1f}%"
)
print()
print("Tracking Stability (std deviation in mm):")
print("-" * 80)
for r in results:
base = r["baseline"]
light = r["lightlike"]
improvement = (
(base["tracking_variance"] - light["tracking_variance"])
/ base["tracking_variance"]
* 100
)
arrow = "↓" if improvement > 0 else "↑"
scenario = base["scenario"].replace(" - Baseline", "")
print(
f"{scenario:<30} {base['tracking_variance']:>7.1f}mm → {light['tracking_variance']:>7.1f}mm "
f"{arrow} {abs(improvement):>5.1f}%"
)
print()
print("Oscillation Events (gradient reversals):")
print("-" * 80)
for r in results:
base = r["baseline"]
light = r["lightlike"]
reduction = base["oscillations"] - light["oscillations"]
scenario = base["scenario"].replace(" - Baseline", "")
print(
f"{scenario:<30} {base['oscillations']:>4} → {light['oscillations']:>4} events "
f"({reduction:+d})"
)
print()
print("Collision Safety (near-collision events):")
print("-" * 80)
for r in results:
base = r["baseline"]
light = r["lightlike"]
reduction = base["near_collisions"] - light["near_collisions"]
scenario = base["scenario"].replace(" - Baseline", "")
print(
f"{scenario:<30} {base['near_collisions']:>4} → {light['near_collisions']:>4} events "
f"({reduction:+d})"
)
print()
print("=" * 80)
print("CONCLUSION".center(80))
print("=" * 80)
print()
# Calculate overall improvements
total_tracking_improvement = sum(
(r["baseline"]["mean_tracking_error_mm"] - r["lightlike"]["mean_tracking_error_mm"])
/ r["baseline"]["mean_tracking_error_mm"]
* 100
for r in results
) / len(results)
total_stability_improvement = sum(
(r["baseline"]["tracking_variance"] - r["lightlike"]["tracking_variance"])
/ r["baseline"]["tracking_variance"]
* 100
for r in results
) / len(results)
total_oscillation_reduction = sum(
r["baseline"]["oscillations"] - r["lightlike"]["oscillations"] for r in results
)
print(f"Average tracking accuracy improvement: {total_tracking_improvement:+.1f}%")
print(f"Average stability improvement: {total_stability_improvement:+.1f}%")
print(f"Total oscillation reduction: {total_oscillation_reduction:+d} events")
print()
if total_tracking_improvement > 2.0 or total_oscillation_reduction > 10:
print("★ SIGNIFICANT IMPROVEMENT DETECTED ★")
print()
print("The lightlike observer provides substantial benefits for dynamic scenarios:")
print(f" • Tracking accuracy: {total_tracking_improvement:+.1f}% better")
print(f" • Motion stability: {total_stability_improvement:+.1f}% better")
print(f" • Oscillations reduced by {total_oscillation_reduction} events")
print()
print("Dynamic environments create oscillations that the lightlike observer")
print("effectively dampens, leading to smoother tracking and better accuracy.")
print()
print("This validates the integration - moving targets/obstacles are exactly")
print("the scenarios where the lightlike observer excels!")
else:
print("MODERATE IMPROVEMENT")
print()
print("The lightlike observer provides some benefit, but not dramatic.")
print("Benefits are most pronounced in scenarios with:")
print(" • Periodic target motion (oscillating)")
print(" • Adversarial dynamics (evasive obstacles)")
print(" • High-speed tracking requirements")
print()
print("=" * 80)
# Best scenario analysis
best_idx = max(
range(len(results)),
key=lambda i: (
results[i]["baseline"]["mean_tracking_error_mm"]
- results[i]["lightlike"]["mean_tracking_error_mm"]
),
)
best = results[best_idx]
print()
print(f"BEST PERFORMANCE: {best['baseline']['scenario'].replace(' - Baseline', '')}")
improvement = (
(best["baseline"]["mean_tracking_error_mm"] - best["lightlike"]["mean_tracking_error_mm"])
/ best["baseline"]["mean_tracking_error_mm"]
* 100
)
print(f" Tracking improvement: {improvement:.1f}%")
print(
f" Oscillations reduced: {best['baseline']['oscillations'] - best['lightlike']['oscillations']} events"
)
if "lightlike_activations" in best["lightlike"]:
print(f" Observer activations: {best['lightlike']['lightlike_activations']}")
print(f" Max damping: {best['lightlike']['max_damping']:.3f}")
if __name__ == "__main__":
main()