-
Notifications
You must be signed in to change notification settings - Fork 34
/
multi_server.py
165 lines (152 loc) · 5.57 KB
/
multi_server.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
base = """
{
"config.py->GlobalConfig": {
"note": "Run1-Lr-Study", // 实验存储路径
"env_name": "dca_multiteam", // 环境(任务名称)
"env_path": "MISSION.dca_multiteam",
"draw_mode": "Img",
"num_threads": 32, // 环境并行数量
"report_reward_interval": 32,
"test_interval": 65536,
"test_epoch": 256,
"mt_parallel": true,
"device": "cpu", // 使用哪张显卡
"fold": "1", // 使用的进程数量 = 环境并行数量/fold
"n_parallel_frame": 50000000.0,
"max_n_episode": 40960.0,
"seed": 22334, // 随机数种子
"mt_act_order": "new_method",
"backup_files": [
"ALGORITHM/experimental_conc_mt_fuzzy5",
"MISSION/dca_multiteam"
]
},
"MISSION.dca_multiteam.collective_assault_parallel_run.py->ScenarioConfig": {
"N_TEAM": 2,
"N_AGENT_EACH_TEAM": [20, 20],
"introduce_terrain": true,
"terrain_parameters": [0.15, 0.2],
"size": "5",
"random_jam_prob": 0.05,
"MaxEpisodeStep": 150, // 时间限制, 胜利条件:尽量摧毁、存活
"render": false, // 高效渲染,只有0号线程环境会渲染
"RewardAsUnity": true,
"half_death_reward": true,
"TEAM_NAMES": [
"ALGORITHM.experimental_conc_mt_fuzzy5.foundation->ReinforceAlgorithmFoundation",
"TEMP.TEAM2.ALGORITHM.experimental_conc_mt_fuzzy5.foundation->ReinforceAlgorithmFoundation",
]
},
"ALGORITHM.experimental_conc_mt_fuzzy5.foundation.py->AlgorithmConfig": {
"train_traj_needed": 32,
"n_focus_on": 4,
"lr": 0.0003,
"ppo_epoch": 16,
"lr_descent": false,
"fuzzy_controller": true,
"fuzzy_controller_param": [2, 2, 3, 0, 2],
"fuzzy_controller_scale_param": [0.8117568492889404],
"use_policy_resonance": false,
"gamma": 0.99,
},
"TEMP.TEAM2.ALGORITHM.experimental_conc_mt_fuzzy5.foundation.py->AlgorithmConfig": {
"train_traj_needed": 32,
"n_focus_on": 4,
"lr": 0.0003,
"ppo_epoch": 16,
"lr_descent": false,
"use_policy_resonance": false,
"gamma": 0.99,
},
}
"""
import commentjson as json
import numpy as np
base_conf = json.loads(base)
n_run = 4
n_run_mode = [
{
"addr": "localhost:2266",
"usr": "hmp",
"pwd": "hmp"
},
]*n_run
assert len(n_run_mode)==n_run
sum_note = "test-stable"
conf_override = {
"config.py->GlobalConfig-->seed":
[
np.random.randint(0, 10000) for _ in range(n_run)
],
"config.py->GlobalConfig-->note":
[
"run1",
"run2",
"run3",
"run4",
],
########################################
"ALGORITHM.experimental_conc_mt_fuzzy5.foundation.py->AlgorithmConfig-->device_override":
[
"cuda:2",
"cuda:2",
"cuda:3",
"cuda:3",
],
"ALGORITHM.experimental_conc_mt_fuzzy5.foundation.py->AlgorithmConfig-->gpu_party_override":
[
"cuda2_party3", # 各子实验的party可以相同, 但每个实验的子队伍party建议设置为不同值
"cuda2_party3",
"cuda3_party3", # 各子实验的party可以相同, 但每个实验的子队伍party建议设置为不同值
"cuda3_party3",
],
########################################
"TEMP.TEAM2.ALGORITHM.experimental_conc_mt_fuzzy5.foundation.py->AlgorithmConfig-->device_override":
[
"cuda:3",
"cuda:3",
"cuda:2",
"cuda:2",
],
"TEMP.TEAM2.ALGORITHM.experimental_conc_mt_fuzzy5.foundation.py->AlgorithmConfig-->gpu_party_override":
[
"cuda3_party3",
"cuda3_party3",
"cuda2_party3",
"cuda2_party3",
],
}
def check_file_mod_time(base_conf):
import glob, time, os
fs = [k.split('->')[0].replace('.','/').replace('/py','.py') for k in base_conf]
fds = [os.path.dirname(f) for f in fs]; fs = []
for fd in fds:
fs.extend(glob.glob(fd+'/*.py'))
def file_mod_time_till_now(f):
filemt1= time.localtime(os.stat(f).st_mtime) #文件修改时间
t1=time.mktime(filemt1)
filemt2= time.localtime() #不带参数就是当前时间
t2=time.mktime(filemt2)
return (t2-t1)/60
minute_write = [file_mod_time_till_now(f) for f in fs]
i = np.argmin([file_mod_time_till_now(f) for f in fs])
print(f'latest file is {fs[i]}, last modified {int(minute_write[i])} minutes ago')
input('Confirm ?')
if __name__ == '__main__':
# copy the experiments
import shutil, os, argparse, time
parser = argparse.ArgumentParser('Run Experiments')
parser.add_argument('-d', '--debug', action='store_true', help='To debug?')
args = parser.parse_args()
check_file_mod_time(base_conf)
file = os.path.abspath(__file__)
os.chdir(os.path.dirname(file))
# copy the experiments
if not args.debug:
shutil.copyfile(file, os.path.join(os.path.dirname(file), 'batch_experiment_backup.py'))
shutil.copyfile(file, os.path.join(os.path.dirname(file),
f'private {time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())} batch_experiment_backup {sum_note}.py'))
# run experiments remotely
from UTIL.batch_exp import run_batch_exp
print('Execute in server:', n_run_mode[0])
run_batch_exp(sum_note, n_run, n_run_mode, base_conf, conf_override, file, debug=args.debug)