-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
185 lines (167 loc) · 10.5 KB
/
model.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
180
181
182
183
184
185
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Normal
import numpy as np
import gym
import make_env
import math
from buffer import replay_buffer
from net import policy_net, value_net
import os
import time
import copy
import argparse
from gym.spaces.discrete import Discrete
class maddpg(object):
def __init__(self, env_id, episode, learning_rate, gamma, capacity, batch_size, value_iter, policy_iter, rho, episode_len, render, train_freq, entropy_weight, start_count=10000, model_path=False):
self.env_id = env_id
self.env = make_env.make_env(self.env_id)
self.episode = episode
self.learning_rate = learning_rate
self.gamma = gamma
self.capacity = capacity
self.batch_size = batch_size
self.value_iter = value_iter
self.policy_iter = policy_iter
self.rho = rho
self.render = render
self.episode_len = episode_len
self.train_freq = train_freq
self.entropy_weight = entropy_weight
self.model_path = model_path
self.observation_dims = self.env.observation_space
self.action_dims = self.env.action_space
self.observation_total_dims = sum([self.env.observation_space[i].shape[0] for i in range(self.env.n)])
self.action_total_dims = sum([self.env.action_space[i].n if isinstance(self.env.action_space[i], Discrete) else sum(self.env.action_space[i].high) + self.env.action_space[i].shape for i in range(self.env.n)])
self.policy_nets = [policy_net(self.observation_dims[i].shape[0], self.action_dims[i].n if isinstance(self.env.action_space[i], Discrete) else sum(self.env.action_space[i].high) + self.env.action_space[i].shape) for i in range(self.env.n)]
self.target_policy_nets = [policy_net(self.observation_dims[i].shape[0], self.action_dims[i].n if isinstance(self.env.action_space[i], Discrete) else sum(self.env.action_space[i].high) + self.env.action_space[i].shape) for i in range(self.env.n)]
self.value_nets = [value_net(self.observation_total_dims, self.action_total_dims, 1) for i in range(self.env.n)]
self.target_value_nets = [value_net(self.observation_total_dims, self.action_total_dims, 1) for i in range(self.env.n)]
self.policy_optimizers = [torch.optim.Adam(policy_net.parameters(), lr=self.learning_rate) for policy_net in self.policy_nets]
self.value_optimizers = [torch.optim.Adam(value_net.parameters(), lr=self.learning_rate) for value_net in self.value_nets]
if self.model_path:
for i in range(self.env.n):
self.policy_nets[i] = torch.load('./models/{}/policy_model{}.pkl'.format(self.env_id, i))
self.value_nets[i] = torch.load('./models/{}/value_model{}.pkl'.format(self.env_id, i))
[target_policy_net.load_state_dict(policy_net.state_dict()) for target_policy_net, policy_net in zip(self.target_policy_nets, self.policy_nets)]
[target_value_net.load_state_dict(value_net.state_dict()) for target_value_net, value_net in zip(self.target_value_nets, self.value_nets)]
self.buffer = replay_buffer(self.capacity)
self.count = 0
self.train_count = 0
self.start_count = start_count
def soft_update(self):
for i in range(self.env.n):
for param, target_param in zip(self.value_nets[i].parameters(), self.target_value_nets[i].parameters()):
target_param.detach().copy_(self.rho * target_param.detach() + (1. - self.rho) * param.detach())
for param, target_param in zip(self.policy_nets[i].parameters(), self.target_policy_nets[i].parameters()):
target_param.detach().copy_(self.rho * target_param.detach() + (1. - self.rho) * param.detach())
def train(self):
for i in range(self.env.n):
observations, actions, rewards, next_observations, dones = self.buffer.sample(self.batch_size)
observations_stack = np.vstack([np.hstack(observations[b]) for b in range(len(observations))])
total_observations = torch.FloatTensor(observations_stack).view(len(observations), -1)
indiv_observations = [torch.FloatTensor(np.vstack([observations[b][n] for b in range(self.batch_size)])) for n in range(self.env.n)]
actions_stack = np.vstack([np.hstack(actions[b]) for b in range(len(actions))])
indiv_actions = [torch.FloatTensor(np.vstack([actions[b][n] for b in range(self.batch_size)])) for n in range(self.env.n)]
total_actions = torch.cat(indiv_actions, dim=1)
rewards = torch.FloatTensor(rewards)
indiv_rewards = [rewards[:, n] for n in range(self.env.n)]
next_observations_stack = np.vstack([np.hstack(next_observations[b]) for b in range(len(next_observations))])
total_next_observations = torch.FloatTensor(next_observations_stack).view(len(next_observations), -1)
indiv_next_observations = [torch.FloatTensor(np.vstack([next_observations[b][n] for b in range(self.batch_size)])) for n in range(self.env.n)]
dones = torch.FloatTensor(dones)
indiv_dones = [dones[:, n] for n in range(self.env.n)]
for _ in range(self.value_iter):
target_next_actions = torch.cat([self.target_policy_nets[n].forward(indiv_next_observations[n])[0] for n in range(self.env.n)], dim=1)
target_next_value = self.target_value_nets[i].forward(total_next_observations, target_next_actions)
q_target = indiv_rewards[i].unsqueeze(1) + self.gamma * (1 - indiv_dones[i].unsqueeze(1)) * target_next_value
q_target = q_target.detach()
q = self.value_nets[i].forward(total_observations, total_actions)
value_loss = (q - q_target).pow(2).mean()
self.value_optimizers[i].zero_grad()
value_loss.backward()
torch.nn.utils.clip_grad_norm_(self.policy_nets[i].parameters(), 0.5)
self.value_optimizers[i].step()
for _ in range(self.policy_iter):
prob, entropy, origin_output = self.policy_nets[i].forward(indiv_observations[i])
#action_idx = torch.LongTensor(actions)[:, i].unsqueeze(1)
#action_prob = prob.gather(dim=1, index=action_idx)
#policy_loss = -self.value_nets[i].forward(total_observations, total_actions).detach() * action_prob.log() - self.entropy_weight * entropy
new_action = copy.deepcopy(indiv_actions)
new_action[i] = prob
new_actions = torch.cat(new_action, dim=1)
policy_loss = - self.value_nets[i].forward(total_observations, new_actions) - self.entropy_weight * entropy
pse_loss = torch.mean(torch.pow(origin_output, 2))
policy_loss = policy_loss.mean()
total_loss = 1e-3 * pse_loss + policy_loss
self.policy_optimizers[i].zero_grad()
total_loss.backward()
torch.nn.utils.clip_grad_norm_(self.value_nets[i].parameters(), 0.5)
self.policy_optimizers[i].step()
self.soft_update()
#self.buffer.clear()
def run(self):
max_reward = -np.inf
save_flag = False
weight_reward = [None for i in range(self.env.n)]
for i in range(self.episode):
obs = self.env.reset()
total_reward = [0 for i in range(self.env.n)]
if self.render:
self.env.render()
while True:
actions = []
for n in range(self.env.n):
action = self.policy_nets[n].act(torch.FloatTensor(np.expand_dims(obs[n], 0)))
actions.append(action)
# * The action is real-value, not one-hot.
next_obs, reward, done, info = self.env.step(actions)
if self.render:
self.env.render()
self.buffer.store(obs, actions, reward, next_obs, done)
self.count += 1
total_reward = [total_reward[i] + reward[i] for i in range(self.env.n)]
obs = next_obs
if self.count % self.train_freq == 0 and len(self.buffer) > self.batch_size and self.count > self.start_count:
self.train_count += 1
self.train()
if any(done) or self.count % self.episode_len == 0:
for n in range(self.env.n):
if weight_reward[n] is not None:
weight_reward[n] = weight_reward[n] * 0.99 + total_reward[n] * 0.01
else:
weight_reward[n] = total_reward[n]
if max_reward < sum(weight_reward) and self.count > self.start_count:
max_reward = sum(weight_reward)
save_flag = True
if save_flag and self.count > self.start_count:
[torch.save(self.policy_nets[i], './models/{}/policy_model{}.pkl'.format(self.env_id, i)) for i in range(self.env.n)]
[torch.save(self.value_nets[i], './models/{}/value_model{}.pkl'.format(self.env_id, i)) for i in range(self.env.n)]
save_flag = False
print(('episode: {}\treward: '+'{:.2f}\t' * self.env.n).format(i + 1, *weight_reward))
break
def eval(self):
self.count = 0
for i in range(self.env.n):
self.policy_nets[i] = torch.load('./models/{}/policy_model{}.pkl'.format(self.env_id, i))
while True:
obs = self.env.reset()
total_reward = [0 for i in range(self.env.n)]
if self.render:
self.env.render()
while True:
time.sleep(0.05)
actions = []
for n in range(self.env.n):
action = self.policy_nets[n].act(torch.FloatTensor(np.expand_dims(obs[n], 0)))
actions.append(action)
next_obs, reward, done, info = self.env.step(actions)
if self.render:
self.env.render()
total_reward = [total_reward[i] + reward[i] for i in range(self.env.n)]
obs = next_obs
self.count += 1
if any(done) or self.count % self.episode_len == 0:
print('episode: {}\treward: {}'.format(i + 1, total_reward))
break