#!/usr/bin/env python # coding=utf-8 ''' @Author: John @Email: johnjim0816@gmail.com @Date: 2020-06-12 00:50:49 @LastEditor: John LastEditTime: 2022-08-18 14:27:18 @Discription: @Environment: python 3.7.7 ''' '''off-policy ''' import torch import torch.nn as nn import torch.optim as optim import random import math import numpy as np class DQN: def __init__(self,n_actions,model,memory,cfg): self.n_actions = n_actions self.device = torch.device(cfg.device) self.gamma = cfg.gamma ## e-greedy parameters self.sample_count = 0 # sample count for epsilon decay self.epsilon = cfg.epsilon_start self.sample_count = 0 self.epsilon_start = cfg.epsilon_start self.epsilon_end = cfg.epsilon_end self.epsilon_decay = cfg.epsilon_decay self.batch_size = cfg.batch_size self.policy_net = model.to(self.device) self.target_net = model.to(self.device) ## copy parameters from policy net to target net for target_param, param in zip(self.target_net.parameters(),self.policy_net.parameters()): target_param.data.copy_(param.data) # self.target_net.load_state_dict(self.policy_net.state_dict()) # or use this to copy parameters self.optimizer = optim.Adam(self.policy_net.parameters(), lr=cfg.lr) self.memory = memory self.update_flag = False def sample_action(self, state): ''' sample action with e-greedy policy ''' self.sample_count += 1 # epsilon must decay(linear,exponential and etc.) for balancing exploration and exploitation self.epsilon = self.epsilon_end + (self.epsilon_start - self.epsilon_end) * \ math.exp(-1. * self.sample_count / self.epsilon_decay) if random.random() > self.epsilon: with torch.no_grad(): state = torch.tensor(state, device=self.device, dtype=torch.float32).unsqueeze(dim=0) q_values = self.policy_net(state) action = q_values.max(1)[1].item() # choose action corresponding to the maximum q value else: action = random.randrange(self.n_actions) return action def predict_action(self,state): ''' predict action ''' with torch.no_grad(): state = torch.tensor(state, device=self.device, dtype=torch.float32).unsqueeze(dim=0) q_values = self.policy_net(state) action = q_values.max(1)[1].item() # choose action corresponding to the maximum q value return action def update(self): if len(self.memory) < self.batch_size: # when transitions in memory donot meet a batch, not update return else: if not self.update_flag: print("begin to update!") self.update_flag = True # sample a batch of transitions from replay buffer state_batch, action_batch, reward_batch, next_state_batch, done_batch = self.memory.sample( self.batch_size) state_batch = torch.tensor(np.array(state_batch), device=self.device, dtype=torch.float) # shape(batchsize,n_states) action_batch = torch.tensor(action_batch, device=self.device).unsqueeze(1) # shape(batchsize,1) reward_batch = torch.tensor(reward_batch, device=self.device, dtype=torch.float).unsqueeze(1) # shape(batchsize) next_state_batch = torch.tensor(np.array(next_state_batch), device=self.device, dtype=torch.float) # shape(batchsize,n_states) done_batch = torch.tensor(np.float32(done_batch), device=self.device).unsqueeze(1) # shape(batchsize,1) # print(state_batch.shape,action_batch.shape,reward_batch.shape,next_state_batch.shape,done_batch.shape) # compute current Q(s_t,a), it is 'y_j' in pseucodes q_value_batch = self.policy_net(state_batch).gather(dim=1, index=action_batch) # shape(batchsize,1),requires_grad=True # print(q_values.requires_grad) # compute max(Q(s_t+1,A_t+1)) respects to actions A, next_max_q_value comes from another net and is just regarded as constant for q update formula below, thus should detach to requires_grad=False next_max_q_value_batch = self.target_net(next_state_batch).max(1)[0].detach().unsqueeze(1) # print(q_values.shape,next_q_values.shape) # compute expected q value, for terminal state, done_batch[0]=1, and expected_q_value=rewardcorrespondingly expected_q_value_batch = reward_batch + self.gamma * next_max_q_value_batch* (1-done_batch) # print(expected_q_value_batch.shape,expected_q_value_batch.requires_grad) loss = nn.MSELoss()(q_value_batch, expected_q_value_batch) # shape same to # backpropagation self.optimizer.zero_grad() loss.backward() # clip to avoid gradient explosion for param in self.policy_net.parameters(): param.grad.data.clamp_(-1, 1) self.optimizer.step() def save_model(self, path): from pathlib import Path # create path Path(path).mkdir(parents=True, exist_ok=True) torch.save(self.target_net.state_dict(), f"{path}/checkpoint.pt") def load_model(self, path): self.target_net.load_state_dict(torch.load(f"{path}/checkpoint.pt")) for target_param, param in zip(self.target_net.parameters(), self.policy_net.parameters()): param.data.copy_(target_param.data)