143 KiB
143 KiB
In [1]:
import sys
from pathlib import Path
curr_path = str(Path().absolute())
parent_path = str(Path().absolute().parent)
sys.path.append(parent_path) # add current terminal path to sys.pathIn [2]:
import gym
import torch
import datetime
from common.utils import save_results, make_dir
from common.plot import plot_rewards
from DQN.agent import DQN
curr_time = datetime.datetime.now().strftime(
"%Y%m%d-%H%M%S") # obtain current timeIn [3]:
class DQNConfig:
def __init__(self):
self.algo = "DQN" # name of algo
self.env = 'CartPole-v0'
self.result_path = curr_path+"/outputs/" + self.env + \
'/'+curr_time+'/results/' # path to save results
self.model_path = curr_path+"/outputs/" + self.env + \
'/'+curr_time+'/models/' # path to save results
self.train_eps = 300 # max trainng episodes
self.eval_eps = 50 # number of episodes for evaluating
self.gamma = 0.95
self.epsilon_start = 0.90 # start epsilon of e-greedy policy
self.epsilon_end = 0.01
self.epsilon_decay = 500
self.lr = 0.0001 # learning rate
self.memory_capacity = 100000 # capacity of Replay Memory
self.batch_size = 64
self.target_update = 2 # update frequency of target net
self.device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu") # check gpu
self.hidden_dim = 256 # hidden size of netIn [4]:
def env_agent_config(cfg,seed=1):
env = gym.make(cfg.env)
env.seed(seed)
state_dim = env.observation_space.shape[0]
action_dim = env.action_space.n
agent = DQN(state_dim,action_dim,cfg)
return env,agentIn [5]:
def train(cfg, env, agent):
print('Start to train !')
print(f'Env:{cfg.env}, Algorithm:{cfg.algo}, Device:{cfg.device}')
rewards = []
ma_rewards = [] # moveing average reward
for i_ep in range(cfg.train_eps):
state = env.reset()
done = False
ep_reward = 0
while True:
action = agent.choose_action(state)
next_state, reward, done, _ = env.step(action)
ep_reward += reward
agent.memory.push(state, action, reward, next_state, done)
state = next_state
agent.update()
if done:
break
if i_ep % cfg.target_update == 0:
agent.target_net.load_state_dict(agent.policy_net.state_dict())
if (i_ep+1)%10 == 0:
print('Episode:{}/{}, Reward:{}'.format(i_ep+1, cfg.train_eps, ep_reward))
rewards.append(ep_reward)
# save ma rewards
if ma_rewards:
ma_rewards.append(0.9*ma_rewards[-1]+0.1*ep_reward)
else:
ma_rewards.append(ep_reward)
print('Complete training!')
return rewards, ma_rewardsIn [6]:
def eval(cfg,env,agent):
rewards = []
ma_rewards = [] # moving average rewards
for i_ep in range(cfg.eval_eps):
ep_reward = 0 # reward per episode
state = env.reset()
while True:
action = agent.predict(state)
next_state, reward, done, _ = env.step(action)
state = next_state
ep_reward += reward
if done:
break
rewards.append(ep_reward)
if ma_rewards:
ma_rewards.append(ma_rewards[-1]*0.9+ep_reward*0.1)
else:
ma_rewards.append(ep_reward)
if (i_ep+1)%10==0:
print(f"Episode:{i_ep+1}/{cfg.eval_eps}, reward:{ep_reward:.1f}")
return rewards,ma_rewardsIn [7]:
if __name__ == "__main__":
cfg = DQNConfig()
# train
env,agent = env_agent_config(cfg,seed=1)
rewards, ma_rewards = train(cfg, env, agent)
make_dir(cfg.result_path, cfg.model_path)
agent.save(path=cfg.model_path)
save_results(rewards, ma_rewards, tag='train', path=cfg.result_path)
plot_rewards(rewards, ma_rewards, tag="train",
algo=cfg.algo, path=cfg.result_path)
# eval
env,agent = env_agent_config(cfg,seed=10)
agent.load(path=cfg.model_path)
rewards,ma_rewards = eval(cfg,env,agent)
save_results(rewards,ma_rewards,tag='eval',path=cfg.result_path)
plot_rewards(rewards,ma_rewards,tag="eval",env=cfg.env,algo = cfg.algo,path=cfg.result_path)Start to train ! Env:CartPole-v0, Algorithm:DQN, Device:cuda Episode:10/300, Reward:13.0 Episode:20/300, Reward:14.0 Episode:30/300, Reward:14.0 Episode:40/300, Reward:12.0 Episode:50/300, Reward:125.0 Episode:60/300, Reward:98.0 Episode:70/300, Reward:200.0 Episode:80/300, Reward:160.0 Episode:90/300, Reward:200.0 Episode:100/300, Reward:200.0 Episode:110/300, Reward:200.0 Episode:120/300, Reward:198.0 Episode:130/300, Reward:200.0 Episode:140/300, Reward:200.0 Episode:150/300, Reward:200.0 Episode:160/300, Reward:200.0 Episode:170/300, Reward:200.0 Episode:180/300, Reward:200.0 Episode:190/300, Reward:200.0 Episode:200/300, Reward:200.0 Episode:210/300, Reward:200.0 Episode:220/300, Reward:200.0 Episode:230/300, Reward:188.0 Episode:240/300, Reward:200.0 Episode:250/300, Reward:200.0 Episode:260/300, Reward:193.0 Episode:270/300, Reward:200.0 Episode:280/300, Reward:200.0 Episode:290/300, Reward:200.0 Episode:300/300, Reward:200.0 Complete training! results saved!
Episode:10/50, reward:188.0 Episode:20/50, reward:200.0 Episode:30/50, reward:200.0 Episode:40/50, reward:200.0 Episode:50/50, reward:171.0 results saved!