forked from lightDRL/lightDRL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
84 lines (66 loc) · 3.75 KB
/
config.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import yaml
import sys
DATA_POOL = 'data_pool/'
cfg = []
""" For loader tuple"""
class YAMLPatch(yaml.SafeLoader):
def construct_python_tuple(self, node):
return tuple(self.construct_sequence(node))
YAMLPatch.add_constructor(u'tag:yaml.org,2002:python/tuple', YAMLPatch.construct_python_tuple)
def load_config(f_name = "config/default.yaml"):
global cfg
print("[I] Load {}".format(f_name))
""" Load File"""
with open(f_name, 'r') as stream:
try:
cfg = yaml.load(stream, Loader=YAMLPatch)
# print(yaml.dump(cfg))
except yaml.YAMLError as exc:
print(exc)
if len(sys.argv) >= 2 and sys.argv[1].endswith('.yaml'):
load_config(sys.argv[1])
else:
load_config()
def set_none_if_not_exist():
cfg['RL']['train_multi_steps'] = cfg['RL']['train_multi_steps'] if 'train_multi_steps' in cfg['RL'] else 1
cfg['RL']['add_data_steps'] = cfg['RL']['add_data_steps'] if 'add_data_steps' in cfg['RL'] else 1
cfg['RL']['reward_reverse'] = cfg['RL']['reward_reverse'] if 'reward_reverse' in cfg['RL'] else False
cfg['RL']['exploration'] = cfg['RL']['exploration'] if 'exploration' in cfg['RL'] else 0
cfg['RL']['exploration_action_noise'] = cfg['RL']['exploration_action_noise'] if 'exploration_action_noise' in cfg['RL'] else None
cfg['RL']['action_epsilon'] = cfg['RL']['action_epsilon'] if 'action_epsilon' in cfg['RL'] else None
cfg['RL']['action_epsilon_add'] = cfg['RL']['action_epsilon_add'] if 'action_epsilon_add' in cfg['RL'] else None
cfg['RL']['method'] = 'Qlearning' if cfg['RL']['method']=='Q-learning' else cfg['RL']['method'] # for python variable rule
cfg['RL']['reward_factor'] = 1.0 if not 'reward_factor' in cfg['RL'] else cfg['RL']['reward_factor']
cfg['RL']['action_noise'] = None if not 'action_noise' in cfg['RL'] else cfg['RL']['action_noise']
cfg['misc']['random_seed'] = None if 'random_seed' not in cfg['misc'] else cfg['misc']['random_seed']
cfg['misc']['render'] = False if not 'render' in cfg['misc'] else cfg['misc']['render']
cfg['misc']['render_after_ep'] = 0 if not 'render_after_ep' in cfg['misc'] else cfg['misc']['render_after_ep']
cfg['misc']['max_ep'] = 1000 if not 'max_ep' in cfg['misc'] else cfg['misc']['max_ep']
cfg['misc']['ep_max_step'] = sys.maxint if not 'ep_max_step' in cfg['misc'] else cfg['misc']['ep_max_step']
cfg['misc']['worker_nickname'] ='worker' if not 'worker_nickname' in cfg['misc'] else cfg['misc']['worker_nickname']
cfg['misc']['gym_monitor_path'] = None if 'gym_monitor_path' not in cfg['misc'] else cfg['misc']['gym_monitor_path']
cfg['misc']['gym_monitor_episode'] = 1 if 'gym_monitor_episode' not in cfg['misc'] else cfg['misc']['gym_monitor_episode']
cfg['misc']['gym_monitor_path_origin'] = cfg['misc']['gym_monitor_path']
cfg['misc']['gym_monitor_path'] = set_gym_monitor_path(cfg['misc']['gym_monitor_path'])
def set_gym_monitor_path(gym_monitor_path, i_project_name = None):
if gym_monitor_path==None:
return None
else:
import os
# from server.py
DATA_POOL = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data_pool')
# from gym_basic.py
project_name='gym-' + get_yaml_name() if i_project_name==None else i_project_name
monitor_path = os.path.join(DATA_POOL, project_name, gym_monitor_path)
# print('monitor_path= ', monitor_path)
# print('DATA_POOL=%s, project_name=%s')
return monitor_path
def get_yaml_name():
from os.path import basename, splitext
f_name = basename(sys.argv[1])
f_name = splitext(f_name)[0]
# print('f_name = ' , f_name)
return f_name
set_none_if_not_exist()
# print(cfg['RL']['action_shape'])
# print(type(cfg['RL']['action_shape']))