dpang commited on
Commit
545e2c8
·
verified ·
1 Parent(s): 87bf301

Update examples/random_agent.py

Browse files
Files changed (1) hide show
  1. examples/random_agent.py +82 -0
examples/random_agent.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # Copyright (c) Space Robotics Lab, SnT, University of Luxembourg, SpaceR
3
+ # RANS: arXiv:2310.07393 — OpenEnv training examples
4
+
5
+ """
6
+ Random Agent — Sanity Check
7
+ ============================
8
+ Verifies the RANS environment works end-to-end by running a random agent.
9
+ This is the first script to run after starting the server.
10
+
11
+ Requires a running RANS server:
12
+ uvicorn rans_env.server.app:app --host 0.0.0.0 --port 8000
13
+
14
+ Run this script:
15
+ python examples/random_agent.py
16
+ python examples/random_agent.py --task GoToPose --episodes 5
17
+ """
18
+
19
+ import argparse
20
+ import random
21
+ import sys
22
+ import time
23
+
24
+
25
+ def main() -> None:
26
+ parser = argparse.ArgumentParser(description="RANS random agent")
27
+ parser.add_argument("--url", default="http://localhost:8000")
28
+ parser.add_argument("--task", default="GoToPosition",
29
+ choices=["GoToPosition", "GoToPose",
30
+ "TrackLinearVelocity", "TrackLinearAngularVelocity"])
31
+ parser.add_argument("--episodes", type=int, default=3)
32
+ parser.add_argument("--max-steps", type=int, default=200)
33
+ args = parser.parse_args()
34
+
35
+ try:
36
+ from rans_env import RANSEnv, SpacecraftAction
37
+ except ImportError:
38
+ print("Install the RANS package first: pip install -e .")
39
+ sys.exit(1)
40
+
41
+ print(f"\nRANS Random Agent — task={args.task} server={args.url}")
42
+ print("=" * 60)
43
+
44
+ with RANSEnv(base_url=args.url).sync() as env:
45
+ for ep in range(1, args.episodes + 1):
46
+ result = env.reset()
47
+ obs = result.observation
48
+ n_thrusters = len(obs.thruster_masks)
49
+
50
+ print(f"\nEpisode {ep} | thrusters={n_thrusters} | task={obs.task}")
51
+ print(f" Initial state_obs: {[f'{v:.3f}' for v in obs.state_obs]}")
52
+
53
+ total_reward = 0.0
54
+ t0 = time.perf_counter()
55
+
56
+ for step in range(1, args.max_steps + 1):
57
+ # Random binary thruster activations
58
+ action = SpacecraftAction(
59
+ thrusters=[random.choice([0.0, 1.0]) for _ in range(n_thrusters)]
60
+ )
61
+ result = env.step(action)
62
+ total_reward += result.reward or 0.0
63
+
64
+ if result.done:
65
+ print(f" Step {step:4d} | reward={result.reward:.4f} "
66
+ f"| DONE ({result.info})")
67
+ break
68
+
69
+ if step % 50 == 0:
70
+ print(f" Step {step:4d} | reward={result.reward:.4f} "
71
+ f"| cumulative={total_reward:.3f}")
72
+
73
+ elapsed = time.perf_counter() - t0
74
+ fps = step / elapsed
75
+ print(f" Episode done | steps={step} total_reward={total_reward:.3f} "
76
+ f"| {fps:.0f} steps/s")
77
+
78
+ print("\nDone.")
79
+
80
+
81
+ if __name__ == "__main__":
82
+ main()