-
Notifications
You must be signed in to change notification settings - Fork 72
/
agent.py
71 lines (58 loc) · 3 KB
/
agent.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
import torch as T
from networks import ActorNetwork, CriticNetwork
class Agent:
def __init__(self, actor_dims, critic_dims, n_actions, n_agents, agent_idx, chkpt_dir,
alpha=0.01, beta=0.01, fc1=64,
fc2=64, gamma=0.95, tau=0.01):
self.gamma = gamma
self.tau = tau
self.n_actions = n_actions
self.agent_name = 'agent_%s' % agent_idx
self.actor = ActorNetwork(alpha, actor_dims, fc1, fc2, n_actions,
chkpt_dir=chkpt_dir, name=self.agent_name+'_actor')
self.critic = CriticNetwork(beta, critic_dims,
fc1, fc2, n_agents, n_actions,
chkpt_dir=chkpt_dir, name=self.agent_name+'_critic')
self.target_actor = ActorNetwork(alpha, actor_dims, fc1, fc2, n_actions,
chkpt_dir=chkpt_dir,
name=self.agent_name+'_target_actor')
self.target_critic = CriticNetwork(beta, critic_dims,
fc1, fc2, n_agents, n_actions,
chkpt_dir=chkpt_dir,
name=self.agent_name+'_target_critic')
self.update_network_parameters(tau=1)
def choose_action(self, observation):
state = T.tensor([observation], dtype=T.float).to(self.actor.device)
actions = self.actor.forward(state)
noise = T.rand(self.n_actions).to(self.actor.device)
action = actions + noise
return action.detach().cpu().numpy()[0]
def update_network_parameters(self, tau=None):
if tau is None:
tau = self.tau
target_actor_params = self.target_actor.named_parameters()
actor_params = self.actor.named_parameters()
target_actor_state_dict = dict(target_actor_params)
actor_state_dict = dict(actor_params)
for name in actor_state_dict:
actor_state_dict[name] = tau*actor_state_dict[name].clone() + \
(1-tau)*target_actor_state_dict[name].clone()
self.target_actor.load_state_dict(actor_state_dict)
target_critic_params = self.target_critic.named_parameters()
critic_params = self.critic.named_parameters()
target_critic_state_dict = dict(target_critic_params)
critic_state_dict = dict(critic_params)
for name in critic_state_dict:
critic_state_dict[name] = tau*critic_state_dict[name].clone() + \
(1-tau)*target_critic_state_dict[name].clone()
self.target_critic.load_state_dict(critic_state_dict)
def save_models(self):
self.actor.save_checkpoint()
self.target_actor.save_checkpoint()
self.critic.save_checkpoint()
self.target_critic.save_checkpoint()
def load_models(self):
self.actor.load_checkpoint()
self.target_actor.load_checkpoint()
self.critic.load_checkpoint()
self.target_critic.load_checkpoint()