-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFunctions_PIL.py
More file actions
1743 lines (1425 loc) · 60 KB
/
Copy pathMyFunctions_PIL.py
File metadata and controls
1743 lines (1425 loc) · 60 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
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import gymnasium as gym
from gymnasium import spaces
from typing import Dict, Any, Optional
import heyoka as hy
import time
import os
import torch
from stable_baselines3.common.monitor import Monitor
from stable_baselines3.common.utils import set_random_seed
def taylor_constant_thrust():
"""
Build the Taylor-adaptive integrator for a constant-thrust spacecraft dynamics model.
Author: Marcello Pareschi
Last update: 2026-03-24
This function defines the equations of motion of a spacecraft subject to
central gravitational attraction and a constant low-thrust acceleration.
The thrust magnitude and direction are treated as fixed parameters during
propagation. The resulting Taylor-adaptive integrator can therefore be used
to simulate trajectories under prescribed constant thrust conditions, which
is useful for preliminary analyses, guidance studies, and validation
purposes.
Returns
-------
ta : heyoka.taylor_adaptive
Taylor-adaptive integrator for the constant-thrust spacecraft dynamics.
"""
# Define the state variables:
# position (x, y, z), velocity (vx, vy, vz), and mass (m).
x, y, z, vx, vy, vz, m = hy.make_vars("x", "y", "z", "vx", "vy", "vz", "m")
# Compute ||r||^3 = (x^2 + y^2 + z^2)^(3/2),
# required for the central gravitational acceleration term.
r3 = (x**2 + y**2 + z**2) ** (1.5)
# The thrust direction could alternatively be parameterized through
# angular coordinates, as suggested in the commented formulation below.
# ux = hy.par[2] * hy.cos(hy.par[3]) * hy.cos(hy.par[4])
# uy = hy.par[2] * hy.sin(hy.par[3]) * hy.cos(hy.par[4])
# uz = hy.par[2] * hy.sin(hy.par[4])
# Define the dynamical system:
# - position derivatives are equal to velocity,
# - velocity derivatives include gravitational and thrust acceleration,
# - mass derivative accounts for propellant consumption.
syst = [
(x, vx),
(y, vy),
(z, vz),
(vx, -x / r3 + hy.par[0] * hy.par[2] * hy.par[3] / m),
(vy, -y / r3 + hy.par[0] * hy.par[2] * hy.par[4] / m),
(vz, -z / r3 + hy.par[0] * hy.par[2] * hy.par[5] / m),
(m, -hy.par[0] / hy.par[1] * hy.par[2])
]
# Set a strict numerical tolerance for high-accuracy propagation.
tol_propagation = 1e-16
# Define a default initial state for integrator construction.
# These values can be overwritten before actual propagation.
initial_state = [1.0] * 7
# Define a default initial time.
initial_time = 0.0
# Define default parameter values:
# these can be overwritten before actual propagation.
initial_pars = [0.0] * 6
# Build the Taylor-adaptive integrator for the constant-thrust system.
ta = hy.taylor_adaptive(
syst,
state=initial_state,
pars=initial_pars,
time=initial_time,
compact_mode=True,
tol=tol_propagation
)
# Return the integrator.
return ta
class LowThrustEnv(gym.Env):
"""
Reinforcement-learning environment for low-thrust trajectory optimization.
Author: Marcello Pareschi
Last update: 2026-03-24
This class implements a custom Gymnasium environment for the simulation
and optimization of low-thrust spacecraft transfers. The environment
propagates the spacecraft dynamics under thrust control, provides the
agent with state-time observations, and evaluates the control policy
through a reward function that combines fuel consumption, trajectory
tracking, and terminal accuracy. Additional stochastic perturbations can
be introduced on initial conditions, states, and controls in order to
improve robustness during training.
Parameters
----------
config : dict
Dictionary containing the full set of environment parameters, such as
initial and final states, scaling constants, reward weights, time
horizon, perturbation settings, and exploration options.
"""
def __init__(self, config: Optional[Dict[str, Any]]) -> None:
"""
Initialize the low-thrust environment.
This constructor loads the configuration dictionary, defines the
observation and action spaces, initializes the internal state, and
creates the Taylor integrator used to propagate the spacecraft
dynamics.
"""
# Initialize the parent Gym environment.
super(LowThrustEnv, self).__init__()
# Load all configuration entries as object attributes.
for key, item in config.items():
setattr(self, key, item)
# Define the time step size from the total time of flight
# and the number of integration steps.
self.dt = self.tof / self.N_steps
# Define the observation space:
# [position(3), velocity(3), mass(1), time(1)].
low = np.array(
[-self.rmax] * 3 + [-self.vmax] * 3 + [0.0] + [0.0],
dtype=np.float32
)
high = np.array(
[self.rmax] * 3 + [self.vmax] * 3 + [1.0] + [self.tof],
dtype=np.float32
)
self.observation_space = spaces.Box(low, high, dtype=np.float32)
# Define the action space:
# [normalized throttle, normalized azimuth, normalized elevation].
self.action_space = spaces.Box(
np.array([-1] * 3),
np.array([1] * 3),
dtype=np.float32
)
# Initialize internal counters and the initial physical state.
self.k = 0
self.time = 0.0
self.state = np.array([*self.r0, *self.v0, self.m0], dtype=np.float32)
self.prev_state = self.state.copy()
# Build the constant-thrust propagator and initialize its parameters.
self.ta = taylor_constant_thrust()
self.ta.pars[:2] = [self.c1, self.c2]
self.ta.state[:] = self.state.copy()
self.ta.time = 0.0
# Counter used to track the total number of training steps.
self.training_steps = 0
def eps_constraint(self):
"""
Return the current tolerance used for the terminal constraint.
The tolerance is selected from a predefined schedule according to the
current number of training steps. This allows the terminal constraint
to be progressively tightened during training.
Returns
-------
float
Current admissible tolerance for the terminal constraint.
"""
# Number of tolerance values available in the schedule.
n_values = len(self.eps_schedule)
# Number of training iterations assigned to each tolerance value.
n_steps_per_value = self.N_iters / n_values
# Select the appropriate tolerance value according to the current
# training-step counter.
for i in range(n_values):
if self.training_steps <= (i + 1) * n_steps_per_value:
return self.eps_schedule[i]
# If all schedule thresholds have been exceeded, return the last value.
return self.eps_schedule[n_values - 1]
def get_observation(self, s):
"""
Build the observation vector from the current state.
The observation contains the physical state and the current time.
In addition, the method updates the tracking error with respect to the
reference trajectory at the current time step.
Parameters
----------
s : ndarray
Current spacecraft state.
Returns
-------
ndarray
Observation vector composed of state and time.
"""
# Create a local copy of the input state.
state = s.copy()
# Extract the reference state corresponding to the current step.
state_ref = self.traj_ref[self.k, :]
# Store the tracking error with respect to the reference trajectory.
self.delta_ref = state - state_ref
# Return the observation as [state, time].
return np.array([*state, self.time], dtype=np.float32)
def get_control(self, action):
"""
Convert a normalized action vector into physical control variables.
The first action component is mapped to a throttle value in [0, 1],
while the remaining two components are interpreted as normalized
azimuth and elevation angles and converted into a Cartesian thrust
direction vector.
Parameters
----------
action : ndarray
Normalized action vector.
Returns
-------
ndarray
Physical control vector [throttle, direction_x, direction_y, direction_z].
"""
# Map the normalized throttle from [-1, 1] to [0, 1].
throttle = 0.5 * (action[0] + 1)
# Convert the normalized angular actions into physical angles.
Az = action[1] * np.pi
El = action[2] * (np.pi / 2)
# Convert azimuth and elevation into Cartesian direction components.
alpha = np.array([
np.cos(El) * np.cos(Az),
np.cos(El) * np.sin(Az),
np.sin(El)
])
# Return the full physical control vector.
return np.array([throttle, *alpha], dtype=np.float32)
def perturb_initial_conditions(self, s):
"""
Perturb the initial conditions for exploration purposes.
Gaussian noise is independently added to the position and velocity
components of the state.
Parameters
----------
s : ndarray
Initial state vector.
Returns
-------
ndarray
Perturbed initial state.
"""
# Copy the state to avoid modifying the original input.
s = s.copy()
# Add Gaussian perturbations to the initial position.
s[:3] += np.random.normal(0, self.sigma_ic_position, size=3)
# Add Gaussian perturbations to the initial velocity.
s[3:6] += np.random.normal(0, self.sigma_ic_velocity, size=3)
# Return the perturbed initial state.
return s
def perturb_control(self, c):
"""
Perturb the control input for exploration purposes.
The throttle is perturbed by additive Gaussian noise and clipped to
the admissible range [0, 1]. The thrust direction is perturbed by a
small random rotation and re-normalized. If a mandatory thrust-off
event is active, the throttle is set to zero within the specified
time window.
Parameters
----------
c : ndarray
Physical control vector.
Returns
-------
ndarray
Perturbed physical control vector.
"""
# Create a local copy of the control input.
control_unp = c.copy()
throttle_unp, alpha_unp = control_unp[0], control_unp[1:]
# Perturb the throttle and clip it to the admissible interval.
throttle = np.clip(
throttle_unp + np.random.normal(0, self.sigma_u_throttle),
0.0,
1.0
)
# Generate a small random rotation vector.
rot = np.random.normal(0, self.sigma_u_direction, size=3)
# Build a first-order rotation matrix approximation.
A = np.array([
[1, -rot[2], rot[1]],
[rot[2], 1, -rot[0]],
[-rot[1], rot[0], 1]
])
# Rotate and re-normalize the thrust direction.
alpha = A @ alpha_unp
alpha /= np.linalg.norm(alpha)
# If a mandatory thrust-off event is active, force zero throttle
# in the corresponding time window.
if self.mte_window is not None:
start, end = self.mte_window
if start <= self.k < end:
throttle = 0.0
# Return the perturbed control vector.
return np.array([throttle, *alpha], dtype=np.float32)
def perturb_state(self, s):
"""
Perturb the propagated state for exploration purposes.
Gaussian noise is independently added to the position and velocity
components of the state.
Parameters
----------
s : ndarray
Propagated state vector.
Returns
-------
ndarray
Perturbed state vector.
"""
# Copy the state to avoid modifying the original input.
s = s.copy()
# Add Gaussian perturbations to the position.
s[:3] += np.random.normal(0, self.sigma_s_position, size=3)
# Add Gaussian perturbations to the velocity.
s[3:6] += np.random.normal(0, self.sigma_s_velocity, size=3)
# Return the perturbed state.
return s
def propagation_step(self, state, control, time):
"""
Propagate the spacecraft dynamics for one time step.
Parameters
----------
state : ndarray
Current spacecraft state.
control : ndarray
Physical control vector.
time : float
Current propagation time.
Returns
-------
state : ndarray
Propagated spacecraft state at the next time step.
time : float
Updated propagation time.
"""
# Assign the control parameters to the propagator.
self.ta.pars[2:] = control
# Set the current propagation time.
self.ta.time = time
# Set the current state of the propagator.
self.ta.state[:] = state
# Propagate the dynamics until the next grid point.
self.ta.propagate_until(self.time_grid[self.k + 1])
# Increment the discrete step counter.
self.k += 1
# Store the propagated state.
self.state = self.ta.state.copy()
# Return the updated state and propagation time.
return self.state, float(self.ta.time)
def _terminated(self):
"""
Check whether the episode has reached its natural terminal step.
Returns
-------
bool
True if the maximum number of steps has been reached.
"""
return self.k >= self.N_steps
def _truncated(self, state):
"""
Check whether the episode must be truncated due to a constraint violation.
Parameters
----------
state : ndarray
Current spacecraft state.
Returns
-------
bool
True if the state violates radius, velocity, or mass constraints.
"""
return bool(
np.linalg.norm(state[:3]) <= self.rmin or
np.linalg.norm(state[:3]) >= self.rmax or
np.linalg.norm(state[3:6]) >= self.vmax or
state[-1] <= 0
)
def get_reward(self, observation, terminated, truncated):
"""
Compute the reward associated with the current transition.
The reward includes:
- a fuel-consumption penalty,
- a tracking penalty with respect to the reference trajectory,
- a terminal penalty based on final-state violation or truncation.
Parameters
----------
observation : ndarray
Current observation vector.
terminated : bool
Flag indicating whether the episode ended naturally.
truncated : bool
Flag indicating whether the episode was interrupted by a violation.
Returns
-------
reward : float
Scalar reward assigned to the transition.
done : bool
Boolean flag indicating whether the episode is over.
"""
# Compute the fuel-consumption penalty from the mass variation.
m_prev, m = self.prev_state[-1], self.state[-1]
reward = -self.w_fuel * (m_prev - m)
# Add the trajectory-tracking penalty.
reward -= self.w_tracking * np.linalg.norm(self.delta_ref)
# Determine whether the episode is over.
done = terminated or truncated
# Apply terminal penalties if the episode has ended.
if done:
# Relative terminal position violation.
self.r_viol = np.linalg.norm(self.state[:3] - self.rf) / np.linalg.norm(self.rf)
# Relative terminal velocity violation.
self.v_viol = np.linalg.norm(self.state[3:6] - self.vf) / np.linalg.norm(self.vf)
# Retrieve the current admissible tolerance.
eps = self.eps_constraint()
# Constraint violation beyond the current tolerance.
c_viol = max(0.0, max(self.r_viol, self.v_viol) - eps)
# Penalize truncation more severely, otherwise penalize terminal mismatch.
if truncated:
reward -= self.w_truncated
else:
reward -= self.w_terminated * c_viol
# Return the final reward and the done flag.
return float(reward), done
def get_info(self, control, done):
"""
Build the info dictionary used for logging and diagnostics.
Parameters
----------
control : ndarray
Control applied at the current step.
done : bool
Flag indicating whether the episode has ended.
Returns
-------
dict
Dictionary containing step-wise and terminal diagnostic data.
"""
# Build the step-wise logging dictionary.
info = {
"episode_step_data": {
"r": [self.prev_state[:3]],
"v": [self.prev_state[3:6]],
"m": [self.prev_state[-1]],
"t": [self.time],
"u": [control],
}
}
# If the episode is over, append terminal metrics and final state data.
if done:
info.update({
"custom_metrics": {
"r_viol": self.r_viol,
"v_viol": self.v_viol,
},
"episode_end_data": {
"rf": self.state[:3],
"vf": self.state[3:6],
"mf": self.state[-1],
}
})
# Return the full info dictionary.
return info
def reset(self, seed=None, options=None):
"""
Reset the environment at the beginning of a new episode.
The state is reinitialized, optional perturbations on the initial
conditions are applied, and a possible mandatory thrust-off event is
sampled if enabled.
Parameters
----------
seed : int, optional
Random seed passed to the parent environment reset.
options : dict, optional
Additional reset options, currently unused.
Returns
-------
obs : ndarray
Initial observation of the new episode.
info : dict
Initial diagnostic dictionary.
"""
# Reset the parent Gym environment.
super().reset(seed=seed)
# Reset the discrete time index and physical time.
self.k = 0
self.time = 0.0
# Restore the nominal initial state.
self.state = np.array([*self.r0, *self.v0, self.m0], dtype=np.float32)
# Optionally perturb the initial conditions.
if self.perturb_ics:
self.state = self.perturb_initial_conditions(self.state)
# By default, no mandatory thrust-off event is active.
self.mte_window = None
# Optionally sample a mandatory thrust-off window.
if self.mte and np.random.rand() < self.pr_mte:
duration = np.random.randint(self.n_mte_min, self.n_mte_max + 1)
last_possible_start = self.N_steps - duration - 1
if last_possible_start > 0:
start_step = np.random.randint(0, last_possible_start)
self.mte_window = (start_step, start_step + duration)
# Store the previous state for reward computation.
self.prev_state = self.state.copy()
# Build the initial observation and the initial info dictionary.
obs = self.get_observation(self.state)
info = self.get_info(np.zeros(3), done=False)
# Return the initial observation and info.
return obs, info
def step(self, action):
"""
Advance the environment by one step using the provided action.
The action is converted into a physical control, optionally perturbed,
then used to propagate the spacecraft dynamics. The new observation,
reward, termination flags, and diagnostic information are returned.
Parameters
----------
action : ndarray
Action selected by the agent.
Returns
-------
obs : ndarray
New observation after propagation.
reward : float
Reward associated with the transition.
terminated : bool
True if the episode ended because the horizon was reached.
truncated : bool
True if the episode ended because a constraint was violated.
info : dict
Additional diagnostic information.
"""
# Ensure that the action belongs to the admissible action space.
assert self.action_space.contains(action)
# Store the previous state before propagation.
self.prev_state = self.state.copy()
# Convert the normalized action into physical control variables.
control = self.get_control(action)
# Optionally perturb the control for exploration.
if self.perturb_controls:
control = self.perturb_control(control)
# Propagate the system dynamics by one step.
self.state, self.time = self.propagation_step(self.state, control, self.time)
# Copy the propagated state and optionally perturb it before observation.
s = self.state.copy()
if self.perturb_states:
s = self.perturb_state(s)
# Build the new observation.
obs = self.get_observation(s)
# Check natural termination and truncation conditions.
terminated = self._terminated()
truncated = self._truncated(self.state)
# Compute reward and global done flag.
reward, done = self.get_reward(obs, terminated, truncated)
# Build the diagnostic info dictionary.
info = self.get_info(control, done)
# Update the global training-step counter.
self.training_steps += 1
# Return the full Gymnasium step tuple.
return obs, reward, terminated, truncated, info
def run_pil_simulation(env, url, session):
"""
Run a single Processor-in-the-Loop (PIL) simulation episode.
Author: Marcello Pareschi
Last update: 2026-03-24
This function executes one complete PIL simulation episode by coupling a
local dynamical environment with an external inference server. At each
step, the current observation is sent to the remote device, which returns
the control action together with inference and hardware-monitoring data.
The environment is then advanced locally, and all relevant metrics are
collected throughout the episode. The function finally returns a structured
dictionary containing timing statistics, hardware usage, trajectory
history, control history, and terminal mission-performance indicators.
Parameters
----------
env : gym.Env
Environment used to simulate the spacecraft dynamics locally.
url : str
URL of the external inference endpoint.
session : requests.Session
Active HTTP session used to communicate with the external device.
Returns
-------
dict
Dictionary containing the collected episode data, including:
- inference times,
- network overheads,
- local physics-integration times,
- power consumption,
- GPU utilization,
- trajectory history,
- control history,
- terminal violations,
- final state.
"""
# Reset the environment and obtain the initial observation.
obs = env.reset()
# Initialize containers for timing and hardware metrics.
inf_times = [] # Remote inference times
net_overheads = [] # Communication overhead excluding inference
physics_times = [] # Local environment propagation times
pwr_cons = [] # Measured power consumption
gpu_utils = [] # Measured GPU utilization
# Initialize containers for the trajectory history.
t_history = [] # Time history
r_history = [] # Position history
v_history = [] # Velocity history
m_history = [] # Mass history
controls = [] # Applied control history
# Initialize terminal-violation variables.
r_viol = None
v_viol = None
# Initialize placeholders for the final state.
rf = None
vf = None
mf = None
# Episode loop.
done = False
while not done:
# --- PHASE 1: REMOTE INFERENCE ---
# Send the current observation to the external inference server
# and receive the predicted control action.
comm_start = time.perf_counter()
try:
# Convert the observation to raw bytes for efficient transmission.
obs_bytes = obs[0].astype(np.float32).tobytes()
# Send the observation to the external server.
response = session.post(
url,
data=obs_bytes,
headers={"Content-Type": "application/octet-stream"},
timeout=2
)
response.raise_for_status()
# Decode the server response.
res_data = response.json()
# Extract the control action and inference-related metrics.
action = res_data["action"]
jetson_inf = res_data["inference_time_ms"]
gpu_u = res_data.get("gpu_utilization_percent", 0)
pwr = res_data.get("power_core_mw", 0)
except Exception as e:
# Stop the episode if communication with the inference server fails.
print(f"Communication Error during episode: {e}")
break
# Compute the total round-trip communication time.
total_comm_ms = (time.perf_counter() - comm_start) * 1000
# Estimate the network overhead by subtracting the inference time
# reported by the remote device from the total communication time.
net_overhead = max(0, total_comm_ms - jetson_inf)
# --- PHASE 2: LOCAL PHYSICS PROPAGATION ---
# Advance the local environment using the action returned
# by the external inference server.
physics_start = time.perf_counter()
obs, reward, dones, infos = env.step([action])
physics_ms = (time.perf_counter() - physics_start) * 1000
# --- DATA COLLECTION ---
# Store timing and hardware-monitoring metrics for the current step.
inf_times.append(jetson_inf)
net_overheads.append(net_overhead)
physics_times.append(physics_ms)
pwr_cons.append(pwr)
gpu_utils.append(gpu_u)
# Extract the step-wise diagnostic data from the environment info.
info = infos[0]
step_data = info["episode_step_data"]
# Append the current step data to the history containers.
# Each quantity is extracted from a single-element list.
t_history.append(step_data["t"][0])
r_history.append(step_data["r"][0])
v_history.append(step_data["v"][0])
m_history.append(step_data["m"][0])
controls.append(step_data["u"][0])
# Update the termination flag.
done = dones[0]
# --- PHASE 3: TERMINAL METRICS ---
# If the episode has ended, extract the final state
# and the terminal constraint violations.
if done:
# Retrieve the final state reached at the end of the episode.
end_data = info["episode_end_data"]
# Append the terminal state to the trajectory history.
r_history.append(end_data["rf"])
v_history.append(end_data["vf"])
m_history.append(end_data["mf"])
# Extract the terminal position and velocity violations.
r_viol = info["custom_metrics"]["r_viol"]
v_viol = info["custom_metrics"]["v_viol"]
# Store the final state explicitly.
rf = end_data["rf"]
vf = end_data["vf"]
mf = end_data["mf"]
# Assemble all collected episode data into a structured dictionary.
ep_data = {
"inf_times": inf_times,
"net_overheads": net_overheads,
"physics_times": physics_times,
"pwr_cons": pwr_cons,
"gpu_utils": gpu_utils,
"steps": len(t_history),
"t_history": t_history,
"r_history": r_history,
"v_history": v_history,
"m_history": m_history,
"controls": controls,
"r_viol": r_viol,
"v_viol": v_viol,
"rf": rf,
"vf": vf,
"mf": mf
}
# Return the complete set of episode metrics.
return ep_data
def make_env(config: Dict[str, Any], rank: int, seed: int = 0) -> gym.Env:
"""
Create a callable environment factory for parallel low-thrust RL simulations.
Author: Marcello Pareschi
Last update: 2026-03-24
This function returns an initializer that builds a single instance of the
low-thrust environment and applies a worker-specific random seed. The
returned callable is especially useful when constructing vectorized
environments for parallel reinforcement-learning training or evaluation.
Parameters
----------
config : dict
Dictionary containing the configuration parameters required to
initialize the low-thrust environment.
rank : int
Identifier of the worker environment. It is used to generate a
distinct random seed for each parallel instance.
seed : int, optional
Base random seed shared across workers.
Returns
-------
callable
Environment-construction function to be used by vectorized or
parallel reinforcement-learning wrappers.
"""
def _init() -> gym.Env:
"""
Build and return one environment instance.
Returns
-------
gym.Env
Initialized low-thrust environment.
"""
# Create a new low-thrust environment using the provided configuration.
env = LowThrustEnv(config)
# Reset the environment with a worker-specific random seed.
env.reset(seed=seed + rank)
# Return the initialized environment instance.
return env
# Set the global random seed for reproducibility.
set_random_seed(seed)
# Return the environment factory function.
return _init
def run_pil_simulation_batch(env, url, session):
"""
Run a batch of Processor-in-the-Loop (PIL) simulation episodes in parallel.
Author: Marcello Pareschi
Last update: 2026-03-24
This function executes multiple PIL simulation episodes simultaneously by
coupling a vectorized local environment with an external inference server.
At each iteration, the full batch of observations is sent to the remote
device, which returns a batch of control actions together with aggregate
timing and hardware metrics. The local environments are then propagated in
parallel, and the trajectory history, timing data, and terminal metrics are
collected separately for each environment in the batch. The function
returns one structured dictionary per completed episode.
Parameters
----------
env : gym.Env
Vectorized environment containing multiple low-thrust simulation
instances.
url : str
URL of the external inference endpoint.
session : requests.Session
Active HTTP session used to communicate with the external device.
Returns
-------
list of dict
List containing one dictionary for each environment in the batch. Each
dictionary stores timing metrics, trajectory history, control history,
and terminal performance indicators.
"""
# Retrieve the number of parallel environments in the batch.
n_envs = env.num_envs
# Reset the vectorized environment and obtain the initial observations.
obs = env.reset()
# Initialize one buffer per metric and per environment.
# Each entry is a list that will store the corresponding time history
# for a single trajectory in the batch.
batch_metrics = {
"inf_times": [[] for _ in range(n_envs)],
"net_overheads": [[] for _ in range(n_envs)],
"physics_times": [[] for _ in range(n_envs)],
"pwr_cons": [[] for _ in range(n_envs)],
"t_hist": [[] for _ in range(n_envs)],
"r_hist": [[] for _ in range(n_envs)],
"v_hist": [[] for _ in range(n_envs)],
"m_hist": [[] for _ in range(n_envs)],
"controls": [[] for _ in range(n_envs)]
}
# Track which environments have already completed their episode.
dones = np.zeros(n_envs, dtype=bool)
# Placeholder for the final structured output of each environment.
episode_results = [None] * n_envs
# Continue until all environments in the batch have terminated.
while not np.all(dones):
# --- PHASE 1: REMOTE BATCH INFERENCE ---
# Send the full batch of observations to the external inference server.
comm_start = time.perf_counter()
try:
# Convert the full observation batch into raw bytes.
obs_bytes = obs.astype(np.float32).tobytes()
# Send the observation batch to the remote inference endpoint.
response = session.post(
url,
data=obs_bytes,
headers={"Content-Type": "application/octet-stream"},
timeout=5
)