-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_all.py
68 lines (57 loc) · 2.55 KB
/
test_all.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pathlib
import argparse
from os import listdir
from os.path import isfile, join
import logging
from stable_baselines.common.evaluation import evaluate_policy
from stable_baselines import PPO2
from rl.environments import *
from stable_baselines.common.vec_env import DummyVecEnv
import yaml, rl
"""
Usage of this tester:
python test_all.py -e [ENVIRONMENT_NAME] -s [SUBDIR] -r -d
e.g.
python test_all.py -e TestEnv -s Test123 -r -d
"""
#CHANGE LOGGING SETTINGS HERE: #INFO; showing all print statements #DEBUG: show extra info
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--environment', type=str, help='Name of the environment')
parser.add_argument('-s', '--subdir', type=str, help='Subdir to combine and analyze')
parser.add_argument('-r', '--render', action='store_true', help='Render the agents.')
parser.add_argument('-d','--deterministic', action='store_true', help='Deterministic or not')
args = parser.parse_args()
path = pathlib.Path().absolute()
specified_path = join(path, 'rl', 'trained_models', args.environment, args.subdir)
files_gen = (file for file in listdir(specified_path)
if isfile(join(specified_path, file)))
files = [file for file in files_gen]
# load config and variables needed
with open(join(specified_path, 'config.yml'), 'r') as c:
config = yaml.load(c)
print('\nLoaded config file from: {}\n'.format(specified_path))
model_config = config['models']['PPO2']
logs = []
for file in files:
try:
path = join(specified_path, file)
print(path)
#make env
# load environment with config variables
env_obj = getattr(rl.environments, args.environment)
env = env_obj(config)
env = DummyVecEnv([lambda: env])
#model = PPO2('MlpPolicy', env=env, tensorboard_log=specified_path, **model_config).load(path, env=env)
model = PPO2.load(path, env=env)
#evaluate
mean_reward, std_reward = evaluate_policy(model, model.get_env(), n_eval_episodes=10, render=args.render, deterministic=args.deterministic)
log = '{}: Mean Reward : {:5.3f}, Standard deviation Reward : {:5.3f}'.format(file, mean_reward, std_reward)
print(log)
logs.append(log)
except:
print('exception occurred')
with open(join(specified_path, 'evaluation_log.txt'), 'w') as f:
for item in logs:
f.write("%s\n" % item)