-
Notifications
You must be signed in to change notification settings - Fork 2
/
loop.py
179 lines (165 loc) · 8.89 KB
/
loop.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import numpy as np
def get_visual_input(n, cameras, brain_obs):
'''
inputs:
n: agents number
cameras: camera number
brain_obs: observations of specified brain, include visual and vector observation.
output:
[vector_information, [visual_info0, visual_info1, visual_info2, ...]]
'''
ss = []
for j in range(n):
s = []
for k in range(cameras):
s.append(brain_obs.visual_observations[k][j])
ss.append(np.array(s))
return np.array(ss)
class Loop(object):
@staticmethod
def train(env, brain_names, models, begin_episode, save_frequency, reset_config, max_step, max_episode, sampler_manager, resampling_interval, policy_mode):
"""
Train loop. Execute until episode reaches its maximum or press 'ctrl+c' artificially.
Inputs:
env: Environment for interaction.
models: all models for this trianing task.
save_frequency: how often to save checkpoints.
reset_config: configuration to reset for Unity environment.
max_step: maximum number of steps for an episode.
sampler_manager: sampler configuration parameters for 'reset_config'.
resampling_interval: how often to resample parameters for env reset.
Variables:
brain_names: a list of brain names set in Unity.
state: store a list of states for each brain. each item contain a list of states for each agents that controlled by the same brain.
visual_state: store a list of visual state information for each brain.
action: store a list of actions for each brain.
dones_flag: store a list of 'done' for each brain. use for judge whether an episode is finished for every agents.
agents_num: use to record 'number' of agents for each brain.
rewards: use to record rewards of agents for each brain.
"""
brains_num = len(brain_names)
state = [0] * brains_num
visual_state = [0] * brains_num
action = [0] * brains_num
dones_flag = [0] * brains_num
agents_num = [0] * brains_num
rewards = [0] * brains_num
for episode in range(begin_episode, max_episode):
if episode % resampling_interval == 0:
reset_config.update(sampler_manager.sample_all())
obs = env.reset(config=reset_config, train_mode=True)
for i, brain_name in enumerate(brain_names):
agents_num[i] = len(obs[brain_name].agents)
dones_flag[i] = np.zeros(agents_num[i])
rewards[i] = np.zeros(agents_num[i])
step = 0
last_done_step = -1
while True:
step += 1
for i, brain_name in enumerate(brain_names):
state[i] = obs[brain_name].vector_observations
visual_state[i] = get_visual_input(agents_num[i], models[i].visual_sources, obs[brain_name])
action[i] = models[i].choose_action(s=state[i], visual_s=visual_state[i])
actions = {f'{brain_name}': action[i] for i, brain_name in enumerate(brain_names)}
obs = env.step(vector_action=actions)
for i, brain_name in enumerate(brain_names):
unfinished_index = np.where(dones_flag[i] == False)[0]
dones_flag[i] += obs[brain_name].local_done
next_state = obs[brain_name].vector_observations
next_visual_state = get_visual_input(agents_num[i], models[i].visual_sources, obs[brain_name])
models[i].store_data(
s=state[i],
visual_s=visual_state[i],
a=action[i],
r=np.array(obs[brain_name].rewards),
s_=next_state,
visual_s_=next_visual_state,
done=np.array(obs[brain_name].local_done)
)
rewards[i][unfinished_index] += np.array(obs[brain_name].rewards)[unfinished_index]
if all([all(dones_flag[i]) for i in range(brains_num)]):
if last_done_step == -1:
last_done_step = step
if policy_mode == 'off-policy':
break
if step >= max_step:
break
for i in range(brains_num):
models[i].learn(episode=episode, step=step)
models[i].writer_summary(
episode,
reward_mean=rewards[i].mean(),
reward_min=rewards[i].min(),
reward_max=rewards[i].max(),
step=last_done_step
)
print('-' * 40)
print(f'episode {episode:3d} | step {step:4d} | last_done_step {last_done_step:4d}')
if episode % save_frequency == 0:
for i in range(brains_num):
models[i].save_checkpoint(episode)
@staticmethod
def inference(env, brain_names, models, reset_config, sampler_manager, resampling_interval):
"""
inference mode. algorithm model will not be train, only used to show agents' behavior
"""
brains_num = len(brain_names)
state = [0] * brains_num
visual_state = [0] * brains_num
action = [0] * brains_num
agents_num = [0] * brains_num
while True:
if np.random.uniform() < 0.2: # the environment has probability below 0.2 to change its parameters while running in the inference mode.
reset_config.update(sampler_manager.sample_all())
obs = env.reset(config=reset_config, train_mode=False)
for i, brain_name in enumerate(brain_names):
agents_num[i] = len(obs[brain_name].agents)
while True:
for i, brain_name in enumerate(brain_names):
state[i] = obs[brain_name].vector_observations
visual_state[i] = get_visual_input(agents_num[i], models[i].visual_sources, obs[brain_name])
action[i] = models[i].choose_inference_action(s=state[i], visual_s=visual_state[i])
actions = {f'{brain_name}': action[i] for i, brain_name in enumerate(brain_names)}
obs = env.step(vector_action=actions)
@staticmethod
def no_op(env, brain_names, models, brains, steps, choose=False):
'''
Interact with the environment but do not perform actions. Prepopulate the ReplayBuffer.
Make sure steps is greater than n-step if using any n-step ReplayBuffer.
'''
assert isinstance(steps, int) and steps >= 0, 'no_op.steps must have type of int and larger than/equal 0'
brains_num = len(brain_names)
state = [0] * brains_num
visual_state = [0] * brains_num
agents_num = [0] * brains_num
action = [0] * brains_num
obs = env.reset(train_mode=False)
for i, brain_name in enumerate(brain_names):
# initialize actions to zeros
agents_num[i] = len(obs[brain_name].agents)
if brains[brain_name].vector_action_space_type == 'continuous':
action[i] = np.zeros((agents_num[i], brains[brain_name].vector_action_space_size[0]), dtype=np.int32)
else:
action[i] = np.zeros((agents_num[i], len(brains[brain_name].vector_action_space_size)), dtype=np.int32)
steps = steps // min(agents_num) + 1
for step in range(steps):
print(f'no op step {step}')
for i, brain_name in enumerate(brain_names):
state[i] = obs[brain_name].vector_observations
visual_state[i] = get_visual_input(agents_num[i], models[i].visual_sources, obs[brain_name])
if choose:
action[i] = models[i].choose_action(s=state[i], visual_s=visual_state[i])
actions = {f'{brain_name}': action[i] for i, brain_name in enumerate(brain_names)}
obs = env.step(vector_action=actions)
for i, brain_name in enumerate(brain_names):
next_state = obs[brain_name].vector_observations
next_visual_state = get_visual_input(agents_num[i], models[i].visual_sources, obs[brain_name])
models[i].no_op_store(
s=state[i],
visual_s=visual_state[i],
a=action[i],
r=np.array(obs[brain_name].rewards),
s_=next_state,
visual_s_=next_visual_state,
done=np.array(obs[brain_name].local_done)
)