Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """Smoke test SinkOneBallEnv: agent random đánh N cú, in thống kê. | |
| Chạy từ gốc repo: | |
| python examples/random_agent.py | |
| Mong đợi: env chạy không lỗi; pot-rate của random thấp (vài %), | |
| reward shaping có phân bố quanh 0. Đây là baseline để so với PPO/SAC. | |
| """ | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) | |
| import numpy as np | |
| from poolcoach_rl.envs import SinkOneBallEnv | |
| def main(n_episodes: int = 50): | |
| env = SinkOneBallEnv(seed=42) | |
| rewards, potted, scratched, contacted = [], 0, 0, 0 | |
| for _ in range(n_episodes): | |
| env.reset() | |
| action = env.action_space.sample() | |
| _, reward, terminated, truncated, info = env.step(action) | |
| assert terminated and not truncated | |
| rewards.append(reward) | |
| potted += info.get("potted", False) | |
| scratched += info.get("scratch", False) | |
| contacted += info.get("contact", False) | |
| print(f"Episodes : {n_episodes}") | |
| print(f"Pot rate : {potted / n_episodes:.1%}") | |
| print(f"Contact rate : {contacted / n_episodes:.1%}") | |
| print(f"Scratch rate : {scratched / n_episodes:.1%}") | |
| print(f"Reward mean : {np.mean(rewards):+.4f}") | |
| print(f"Reward min/max: {np.min(rewards):+.4f} / {np.max(rewards):+.4f}") | |
| if __name__ == "__main__": | |
| # Tuy chon: python examples/random_agent.py [so_van] (mac dinh 50) | |
| n = int(sys.argv[1]) if len(sys.argv) > 1 else 50 | |
| main(n) | |