Skip to content

Commit 34fcebc

Browse files
committed
update
1 parent 5085040 commit 34fcebc

31 files changed

+434
-137
lines changed

codes/DDPG/agent.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
@Date: 2020-06-09 20:25:52
77
@LastEditor: John
8-
LastEditTime: 2021-05-04 14:50:17
8+
LastEditTime: 2021-09-16 00:55:30
99
@Discription:
1010
@Environment: python 3.7.7
1111
'''
@@ -26,7 +26,7 @@ def __init__(self, state_dim, action_dim, cfg):
2626
self.target_critic = Critic(state_dim, action_dim, cfg.hidden_dim).to(cfg.device)
2727
self.target_actor = Actor(state_dim, action_dim, cfg.hidden_dim).to(cfg.device)
2828

29-
# copy parameters to target net
29+
# 复制参数到目标网络
3030
for target_param, param in zip(self.target_critic.parameters(), self.critic.parameters()):
3131
target_param.data.copy_(param.data)
3232
for target_param, param in zip(self.target_actor.parameters(), self.actor.parameters()):
@@ -37,7 +37,7 @@ def __init__(self, state_dim, action_dim, cfg):
3737
self.actor_optimizer = optim.Adam(self.actor.parameters(), lr=cfg.actor_lr)
3838
self.memory = ReplayBuffer(cfg.memory_capacity)
3939
self.batch_size = cfg.batch_size
40-
self.soft_tau = cfg.soft_tau
40+
self.soft_tau = cfg.soft_tau # 软更新参数
4141
self.gamma = cfg.gamma
4242

4343
def choose_action(self, state):
@@ -46,11 +46,11 @@ def choose_action(self, state):
4646
return action.detach().cpu().numpy()[0, 0]
4747

4848
def update(self):
49-
if len(self.memory) < self.batch_size:
49+
if len(self.memory) < self.batch_size: # 当 memory 中不满足一个批量时,不更新策略
5050
return
51-
state, action, reward, next_state, done = self.memory.sample(
52-
self.batch_size)
53-
# convert variables to Tensor
51+
# 从经验回放中(replay memory)中随机采样一个批量的转移(transition)
52+
state, action, reward, next_state, done = self.memory.sample(self.batch_size)
53+
# 转变为张量
5454
state = torch.FloatTensor(state).to(self.device)
5555
next_state = torch.FloatTensor(next_state).to(self.device)
5656
action = torch.FloatTensor(action).to(self.device)
@@ -70,10 +70,10 @@ def update(self):
7070
self.actor_optimizer.zero_grad()
7171
policy_loss.backward()
7272
self.actor_optimizer.step()
73-
7473
self.critic_optimizer.zero_grad()
7574
value_loss.backward()
7675
self.critic_optimizer.step()
76+
# 软更新
7777
for target_param, param in zip(self.target_critic.parameters(), self.critic.parameters()):
7878
target_param.data.copy_(
7979
target_param.data * (1.0 - self.soft_tau) +

codes/DDPG/env.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
@Date: 2020-06-10 15:28:30
77
@LastEditor: John
8-
LastEditTime: 2021-03-19 19:56:46
8+
LastEditTime: 2021-09-16 00:52:30
99
@Discription:
1010
@Environment: python 3.7.7
1111
'''
@@ -32,30 +32,27 @@ def reverse_action(self, action):
3232
return action
3333

3434
class OUNoise(object):
35-
'''Ornstein–Uhlenbeck
35+
'''Ornstein–Uhlenbeck噪声
3636
'''
3737
def __init__(self, action_space, mu=0.0, theta=0.15, max_sigma=0.3, min_sigma=0.3, decay_period=100000):
38-
self.mu = mu
39-
self.theta = theta
40-
self.sigma = max_sigma
38+
self.mu = mu # OU噪声的参数
39+
self.theta = theta # OU噪声的参数
40+
self.sigma = max_sigma # OU噪声的参数
4141
self.max_sigma = max_sigma
4242
self.min_sigma = min_sigma
4343
self.decay_period = decay_period
4444
self.action_dim = action_space.shape[0]
4545
self.low = action_space.low
4646
self.high = action_space.high
4747
self.reset()
48-
4948
def reset(self):
5049
self.obs = np.ones(self.action_dim) * self.mu
51-
5250
def evolve_obs(self):
5351
x = self.obs
5452
dx = self.theta * (self.mu - x) + self.sigma * np.random.randn(self.action_dim)
5553
self.obs = x + dx
5654
return self.obs
57-
5855
def get_action(self, action, t=0):
5956
ou_obs = self.evolve_obs()
60-
self.sigma = self.max_sigma - (self.max_sigma - self.min_sigma) * min(1.0, t / self.decay_period)
61-
return np.clip(action + ou_obs, self.low, self.high)
57+
self.sigma = self.max_sigma - (self.max_sigma - self.min_sigma) * min(1.0, t / self.decay_period) # sigma会逐渐衰减
58+
return np.clip(action + ou_obs, self.low, self.high) # 动作加上噪声后进行剪切
Binary file not shown.
Binary file not shown.
Binary file not shown.
66.5 KB
Loading
Binary file not shown.
Binary file not shown.
73.7 KB
Loading

codes/DDPG/task0_train.py

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
66
@Date: 2020-06-11 20:58:21
77
@LastEditor: John
8-
LastEditTime: 2021-05-04 14:49:45
8+
LastEditTime: 2021-09-16 01:31:33
99
@Discription:
1010
@Environment: python 3.7.7
1111
'''
1212
import sys,os
13-
curr_path = os.path.dirname(__file__)
14-
parent_path = os.path.dirname(curr_path)
15-
sys.path.append(parent_path) # add current terminal path to sys.path
13+
curr_path = os.path.dirname(os.path.abspath(__file__)) # 当前文件所在绝对路径
14+
parent_path = os.path.dirname(curr_path) # 父路径
15+
sys.path.append(parent_path) # 添加父路径到系统路径sys.path
1616

1717
import datetime
1818
import gym
@@ -21,49 +21,45 @@
2121
from DDPG.env import NormalizedActions, OUNoise
2222
from DDPG.agent import DDPG
2323
from common.utils import save_results,make_dir
24-
from common.plot import plot_rewards
25-
26-
curr_time = datetime.datetime.now().strftime(
27-
"%Y%m%d-%H%M%S") # obtain current time
24+
from common.plot import plot_rewards, plot_rewards_cn
2825

26+
curr_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") # 获取当前时间
2927

3028
class DDPGConfig:
3129
def __init__(self):
32-
self.algo = 'DDPG'
33-
self.env = 'Pendulum-v0' # env name
30+
self.algo = 'DDPG' # 算法名称
31+
self.env = 'Pendulum-v0' # 环境名称
3432
self.result_path = curr_path+"/outputs/" + self.env + \
35-
'/'+curr_time+'/results/' # path to save results
33+
'/'+curr_time+'/results/' # 保存结果的路径
3634
self.model_path = curr_path+"/outputs/" + self.env + \
37-
'/'+curr_time+'/models/' # path to save results
38-
self.gamma = 0.99
39-
self.critic_lr = 1e-3
40-
self.actor_lr = 1e-4
41-
self.memory_capacity = 10000
35+
'/'+curr_time+'/models/' # 保存模型的路径
36+
self.train_eps = 300 # 训练的回合数
37+
self.eval_eps = 50 # 测试的回合数
38+
self.gamma = 0.99 # 折扣因子
39+
self.critic_lr = 1e-3 # 评论家网络的学习率
40+
self.actor_lr = 1e-4 # 演员网络的学习率
41+
self.memory_capacity = 8000
4242
self.batch_size = 128
43-
self.train_eps = 300
44-
self.eval_eps = 50
45-
self.eval_steps = 200
46-
self.target_update = 4
47-
self.hidden_dim = 30
48-
self.soft_tau = 1e-2
49-
self.device = torch.device(
50-
"cuda" if torch.cuda.is_available() else "cpu")
43+
self.target_update = 2
44+
self.hidden_dim = 256
45+
self.soft_tau = 1e-2 # 软更新参数
46+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5147

5248
def env_agent_config(cfg,seed=1):
5349
env = NormalizedActions(gym.make(cfg.env))
54-
env.seed(seed)
50+
env.seed(seed) # 随机种子
5551
state_dim = env.observation_space.shape[0]
5652
action_dim = env.action_space.shape[0]
5753
agent = DDPG(state_dim,action_dim,cfg)
5854
return env,agent
5955

6056
def train(cfg, env, agent):
61-
print('Start to train ! ')
62-
print(f'Env:{cfg.env}, Algorithm:{cfg.algo}, Device:{cfg.device}')
63-
ou_noise = OUNoise(env.action_space) # action noise
64-
rewards = []
65-
ma_rewards = [] # moving average rewards
66-
for i_episode in range(cfg.train_eps):
57+
print('开始训练!')
58+
print(f'环境:{cfg.env},算法:{cfg.algo},设备:{cfg.device}')
59+
ou_noise = OUNoise(env.action_space) # 动作噪声
60+
rewards = [] # 记录奖励
61+
ma_rewards = [] # 记录滑动平均奖励
62+
for i_ep in range(cfg.train_eps):
6763
state = env.reset()
6864
ou_noise.reset()
6965
done = False
@@ -72,29 +68,29 @@ def train(cfg, env, agent):
7268
while not done:
7369
i_step += 1
7470
action = agent.choose_action(state)
75-
action = ou_noise.get_action(
76-
action, i_step) # 即paper中的random process
71+
action = ou_noise.get_action(action, i_step)
7772
next_state, reward, done, _ = env.step(action)
7873
ep_reward += reward
7974
agent.memory.push(state, action, reward, next_state, done)
8075
agent.update()
8176
state = next_state
82-
print('Episode:{}/{}, Reward:{}'.format(i_episode+1, cfg.train_eps, ep_reward))
77+
if (i_ep+1)%10 == 0:
78+
print('回合:{}/{},奖励:{:.2f}'.format(i_ep+1, cfg.train_eps, ep_reward))
8379
rewards.append(ep_reward)
8480
if ma_rewards:
8581
ma_rewards.append(0.9*ma_rewards[-1]+0.1*ep_reward)
8682
else:
8783
ma_rewards.append(ep_reward)
88-
print('Complete training!')
84+
print('完成训练!')
8985
return rewards, ma_rewards
9086

9187
def eval(cfg, env, agent):
92-
print('Start to Eval ! ')
93-
print(f'Env:{cfg.env}, Algorithm:{cfg.algo}, Device:{cfg.device}')
94-
rewards = []
95-
ma_rewards = [] # moving average rewards
96-
for i_episode in range(cfg.eval_eps):
97-
state = env.reset()
88+
print('开始测试!')
89+
print(f'环境:{cfg.env}, 算法:{cfg.algo}, 设备:{cfg.device}')
90+
rewards = [] # 记录奖励
91+
ma_rewards = [] # 记录滑动平均奖励
92+
for i_ep in range(cfg.eval_eps):
93+
state = env.reset()
9894
done = False
9995
ep_reward = 0
10096
i_step = 0
@@ -104,32 +100,29 @@ def eval(cfg, env, agent):
104100
next_state, reward, done, _ = env.step(action)
105101
ep_reward += reward
106102
state = next_state
107-
print('Episode:{}/{}, Reward:{}'.format(i_episode+1, cfg.train_eps, ep_reward))
103+
print('回合:{}/{}, 奖励:{}'.format(i_ep+1, cfg.train_eps, ep_reward))
108104
rewards.append(ep_reward)
109105
if ma_rewards:
110106
ma_rewards.append(0.9*ma_rewards[-1]+0.1*ep_reward)
111107
else:
112108
ma_rewards.append(ep_reward)
113-
print('Complete Eval!')
109+
print('完成测试!')
114110
return rewards, ma_rewards
115111

116112

117113
if __name__ == "__main__":
118114
cfg = DDPGConfig()
119-
120-
# train
115+
# 训练
121116
env,agent = env_agent_config(cfg,seed=1)
122117
rewards, ma_rewards = train(cfg, env, agent)
123118
make_dir(cfg.result_path, cfg.model_path)
124119
agent.save(path=cfg.model_path)
125120
save_results(rewards, ma_rewards, tag='train', path=cfg.result_path)
126-
plot_rewards(rewards, ma_rewards, tag="train",
127-
algo=cfg.algo, path=cfg.result_path)
128-
129-
# eval
121+
plot_rewards_cn(rewards, ma_rewards, tag="train", env = cfg.env, algo=cfg.algo, path=cfg.result_path)
122+
# 测试
130123
env,agent = env_agent_config(cfg,seed=10)
131124
agent.load(path=cfg.model_path)
132125
rewards,ma_rewards = eval(cfg,env,agent)
133-
save_results(rewards,ma_rewards,tag='eval',path=cfg.result_path)
134-
plot_rewards(rewards,ma_rewards,tag="eval",env=cfg.env,algo = cfg.algo,path=cfg.result_path)
126+
save_results(rewards,ma_rewards,tag = 'eval',path = cfg.result_path)
127+
plot_rewards_cn(rewards,ma_rewards,tag = "eval",env = cfg.env,algo = cfg.algo,path=cfg.result_path)
135128

0 commit comments

Comments
 (0)