File size: 1,512 Bytes
78738de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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)