-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_agent.py
More file actions
55 lines (44 loc) · 1.32 KB
/
test_agent.py
File metadata and controls
55 lines (44 loc) · 1.32 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
import importlib.machinery
import importlib.util
import gym
import numpy as np
from evaluator import get_env, config
def load_agent(path):
# 动态加载 agent.py 并实例化 Agent
loader = importlib.machinery.SourceFileLoader('agent', path)
spec = importlib.util.spec_from_loader(loader.name, loader)
mod = importlib.util.module_from_spec(spec)
loader.exec_module(mod)
return mod.PolicyAgent()
def evaluate_agent(agent, env, seeds):
results = []
for seed in seeds:
obs = env.reset(seed=seed)
agent.reset()
total_reward = 0.0
while True:
action = agent.act(obs)
obs, reward, done, _ = env.step(action)
total_reward += reward
if done:
break
results.append(total_reward)
return results
def main():
# 1. load agent
agent = load_agent("./agent/agent.py")
agent.seed(config["seed"])
# 2. create one env,循环复用
env = get_env()
# 3. evaluate
results = evaluate_agent(agent, env, config["seeds"])
# 4. clean up
env.close()
agent.close()
# 5. 可选:打印或返回
mean_score = np.mean(results)
std_score = np.std(results)
print(f"Score: {mean_score:.4f} ± {std_score:.4f}")
return results
if __name__ == "__main__":
main()