forked from xuehy/pytorch-maddpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
129 lines (115 loc) · 4.27 KB
/
main.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
from madrl_environments.pursuit import MAWaterWorld_mod
from MADDPG import MADDPG
import numpy as np
import torch as th
import visdom
from params import scale_reward
# do not render the scene
e_render = False
food_reward = 10.
poison_reward = -1.
encounter_reward = 0.01
n_coop = 2
world = MAWaterWorld_mod(n_pursuers=2, n_evaders=50,
n_poison=50, obstacle_radius=0.04,
food_reward=food_reward,
poison_reward=poison_reward,
encounter_reward=encounter_reward,
n_coop=n_coop,
sensor_range=0.2, obstacle_loc=None, )
vis = visdom.Visdom(port=5274)
reward_record = []
np.random.seed(1234)
th.manual_seed(1234)
world.seed(1234)
n_agents = world.n_pursuers
n_states = 213
n_actions = 2
capacity = 1000000
batch_size = 1000
n_episode = 20000
max_steps = 1000
episodes_before_train = 100
win = None
param = None
maddpg = MADDPG(n_agents, n_states, n_actions, batch_size, capacity,
episodes_before_train)
FloatTensor = th.cuda.FloatTensor if maddpg.use_cuda else th.FloatTensor
for i_episode in range(n_episode):
obs = world.reset()
obs = np.stack(obs)
if isinstance(obs, np.ndarray):
obs = th.from_numpy(obs).float()
total_reward = 0.0
rr = np.zeros((n_agents,))
for t in range(max_steps):
# render every 100 episodes to speed up training
if i_episode % 100 == 0 and e_render:
world.render()
obs = obs.type(FloatTensor)
action = maddpg.select_action(obs).data.cpu()
obs_, reward, done, _ = world.step(action.numpy())
reward = th.FloatTensor(reward).type(FloatTensor)
obs_ = np.stack(obs_)
obs_ = th.from_numpy(obs_).float()
if t != max_steps - 1:
next_obs = obs_
else:
next_obs = None
total_reward += reward.sum()
rr += reward.cpu().numpy()
maddpg.memory.push(obs.data, action, next_obs, reward)
obs = next_obs
c_loss, a_loss = maddpg.update_policy()
maddpg.episode_done += 1
print('Episode: %d, reward = %f' % (i_episode, total_reward))
reward_record.append(total_reward)
if maddpg.episode_done == maddpg.episodes_before_train:
print('training now begins...')
print('MADDPG on WaterWorld\n' +
'scale_reward=%f\n' % scale_reward +
'agent=%d' % n_agents +
', coop=%d' % n_coop +
' \nlr=0.001, 0.0001, sensor_range=0.3\n' +
'food=%f, poison=%f, encounter=%f' % (
food_reward,
poison_reward,
encounter_reward))
if win is None:
win = vis.line(X=np.arange(i_episode, i_episode+1),
Y=np.array([
np.append(total_reward, rr)]),
opts=dict(
ylabel='Reward',
xlabel='Episode',
title='MADDPG on WaterWorld_mod\n' +
'agent=%d' % n_agents +
', coop=%d' % n_coop +
', sensor_range=0.2\n' +
'food=%f, poison=%f, encounter=%f' % (
food_reward,
poison_reward,
encounter_reward),
legend=['Total'] +
['Agent-%d' % i for i in range(n_agents)]))
else:
vis.line(X=np.array(
[np.array(i_episode).repeat(n_agents+1)]),
Y=np.array([np.append(total_reward,
rr)]),
win=win,
update='append')
if param is None:
param = vis.line(X=np.arange(i_episode, i_episode+1),
Y=np.array([maddpg.var[0]]),
opts=dict(
ylabel='Var',
xlabel='Episode',
title='MADDPG on WaterWorld: Exploration',
legend=['Variance']))
else:
vis.line(X=np.array([i_episode]),
Y=np.array([maddpg.var[0]]),
win=param,
update='append')
world.close()