-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathevaluator.py
50 lines (36 loc) · 1.57 KB
/
evaluator.py
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
import numpy as np
from util import *
class Evaluator(object):
def __init__(self, num_episodes, interval, max_episode_length=None):
self.num_episodes = num_episodes
self.max_episode_length = max_episode_length
self.interval = interval
self.results = np.array([]).reshape(num_episodes,0)
def __call__(self, env, policy, debug=False, visualize=False, save=True):
self.is_training = False
observation = None
result = []
for episode in range(self.num_episodes):
# reset at the start of episode
observation = env.reset()
episode_steps = 0
episode_reward = 0.
assert observation is not None
# start episode
done = False
while not done:
# basic operation, action ,reward, blablabla ...
action = policy(observation)
observation, reward, done, info = env.step(action)
if self.max_episode_length and episode_steps >= self.max_episode_length -1:
done = True
if visualize:
env.render(mode='human')
# update
episode_reward += reward
episode_steps += 1
if debug: prYellow('[Evaluate] #Episode{}: episode_reward:{}'.format(episode,episode_reward))
result.append(episode_reward)
result = np.array(result).reshape(-1,1)
self.results = np.hstack([self.results, result])
return np.mean(result)