| |
|
|
| import argparse |
| import os |
| import time |
| import json |
|
|
| from isaaclab.app import AppLauncher |
|
|
| from demo.solution import AlgSolution |
| solution = AlgSolution() |
|
|
| |
| |
| |
| parser = argparse.ArgumentParser(description="Play Atec Tasks (ENV only, no RL).") |
| parser.add_argument("--video", action="store_true", default=False, help="Record videos during play.") |
| parser.add_argument("--video_length", type=int, default=200, help="Length of the recorded video (in steps).") |
| parser.add_argument( |
| "--disable_fabric", action="store_true", default=False, help="Disable fabric and use USD I/O operations." |
| ) |
| parser.add_argument("--num_envs", type=int, default=1, help="Number of environments to simulate.") |
| parser.add_argument("--task", type=str, default=None, help="Name of the task.") |
| parser.add_argument("--real-time", action="store_true", default=False, help="Run in real-time, if possible.") |
| parser.add_argument( |
| "--debug", |
| action="store_true", |
| default=False, |
| help="Enable debug prints for per-step reward/time metrics.", |
| ) |
|
|
| |
| AppLauncher.add_app_launcher_args(parser) |
|
|
| args_cli = parser.parse_args() |
|
|
| |
| if args_cli.video: |
| args_cli.enable_cameras = True |
|
|
| |
| |
| |
| app_launcher = AppLauncher(args_cli) |
| simulation_app = app_launcher.app |
|
|
| |
| |
| |
| import gymnasium as gym |
| import torch |
|
|
| from isaaclab.envs import DirectMARLEnv, multi_agent_to_single_agent |
| from isaaclab.utils.dict import print_dict |
|
|
| import atec_rl_lab.tasks |
| from isaaclab_tasks.utils import parse_env_cfg |
| from rl_utils import camera_follow |
|
|
|
|
| def play() -> tuple[float, float]: |
| if args_cli.task is None: |
| raise ValueError("Please provide --task, e.g. --task ATEC-TaskA-G1") |
|
|
| is_task_e = isinstance(args_cli.task, str) and args_cli.task.startswith("ATEC-TaskE") |
| |
| |
| |
| env_cfg = parse_env_cfg( |
| args_cli.task, |
| device=args_cli.device, |
| num_envs=args_cli.num_envs, |
| use_fabric=not args_cli.disable_fabric |
| ) |
|
|
| env = gym.make(args_cli.task, cfg=env_cfg, render_mode="rgb_array" if args_cli.video else None) |
|
|
| |
| if isinstance(env.unwrapped, DirectMARLEnv): |
| env = multi_agent_to_single_agent(env) |
|
|
| |
| |
| |
| if args_cli.video: |
| |
| video_kwargs = { |
| "video_folder": os.path.abspath(os.path.join("logs", "videos", args_cli.task, "play")), |
| "step_trigger": lambda step: step == 0, |
| "video_length": args_cli.video_length, |
| "disable_logger": True, |
| } |
| print("[INFO] Recording videos during play.") |
| print_dict(video_kwargs, nesting=4) |
| env = gym.wrappers.RecordVideo(env, **video_kwargs) |
|
|
|
|
| |
| |
| |
| obs, _ = env.reset() |
|
|
| dt = env.unwrapped.step_dt if hasattr(env.unwrapped, "step_dt") else None |
| timestep = 0 |
|
|
| |
| |
| |
| total_episode_reward = 0.0 |
| total_elapsed_time = 0.0 |
| while simulation_app.is_running(): |
| with torch.inference_mode(): |
| start_time = time.time() |
|
|
| |
| resp = solution.predicts(obs, total_episode_reward) |
| giveup = resp["giveup"] |
| if giveup: |
| break |
| actions = resp["action"] |
| actions = torch.tensor(actions, dtype=torch.float32, device='cuda').view(1, -1) |
| obs, reward, terminated, truncated, info = env.step(actions) |
| if not is_task_e: |
| camera_follow(env) |
|
|
| sim_dt = info["Step_dt"] |
| if isinstance(reward, torch.Tensor): |
| total_episode_reward += reward.mean().item() / sim_dt |
| else: |
| total_episode_reward += float(reward) / sim_dt |
|
|
| if isinstance(info, dict) and "Elapsed_Time" in info: |
| elapsed = info["Elapsed_Time"] |
| total_elapsed_time = elapsed.item() if hasattr(elapsed, "item") else float(elapsed) |
| elif dt is not None: |
| total_elapsed_time += dt |
|
|
| if args_cli.debug: |
| print(f"total_episode_reward:{total_episode_reward: .2f}") |
| print(f"total_elapsed_time:{total_elapsed_time: .2f}") |
|
|
| done = (terminated.item() or truncated.item()) |
| if done: |
| break |
|
|
| timestep += 1 |
| |
| if args_cli.video and timestep >= args_cli.video_length: |
| break |
|
|
| |
| if args_cli.real_time and dt is not None: |
| sleep_time = dt - (time.time() - start_time) |
| if sleep_time > 0: |
| time.sleep(sleep_time) |
|
|
| env.close() |
|
|
| return total_episode_reward, total_elapsed_time |
|
|
|
|
| if __name__ == "__main__": |
| score, elapsed_time = play() |
| print(f"score: {score:.2f}, elapsed_time: {elapsed_time:.2f} seconds") |
|
|
| |
| print("Closing simulation app...") |
| simulation_app.close() |
|
|