hot update PG

This commit is contained in:
johnjim0816
2022-08-25 21:00:53 +08:00
parent 4f4658503e
commit 80f20c73be
34 changed files with 1391 additions and 1695 deletions

View File

@@ -5,7 +5,7 @@ Author: John
Email: johnjim0816@gmail.com
Date: 2021-03-12 16:58:16
LastEditor: John
LastEditTime: 2022-08-04 22:22:16
LastEditTime: 2022-08-25 00:23:22
Discription:
Environment:
'''
@@ -14,45 +14,51 @@ from collections import defaultdict
import torch
import math
class Sarsa(object):
def __init__(self,
n_actions,cfg):
self.n_actions = n_actions
self.lr = cfg.lr
self.gamma = cfg.gamma
self.sample_count = 0
self.epsilon_start = cfg.epsilon_start
self.epsilon_end = cfg.epsilon_end
self.epsilon_decay = cfg.epsilon_decay
self.Q = defaultdict(lambda: np.zeros(n_actions)) # Q table
def sample(self, state):
def __init__(self,cfg):
self.n_actions = cfg['n_actions']
self.lr = cfg['lr']
self.gamma = cfg['gamma']
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.Q_table = defaultdict(lambda: np.zeros(self.n_actions)) # Q table
def sample_action(self, state):
''' another way to represent e-greedy policy
'''
self.sample_count += 1
self.epsilon = self.epsilon_end + (self.epsilon_start - self.epsilon_end) * \
math.exp(-1. * self.sample_count / self.epsilon_decay) # The probability to select a random action, is is log decayed
best_action = np.argmax(self.Q[state])
best_action = np.argmax(self.Q_table[state])
action_probs = np.ones(self.n_actions, dtype=float) * self.epsilon / self.n_actions
action_probs[best_action] += (1.0 - self.epsilon)
action = np.random.choice(np.arange(len(action_probs)), p=action_probs)
return action
def predict(self,state):
return np.argmax(self.Q[state])
def update(self, state, action, reward, next_state, next_action,done):
Q_predict = self.Q[state][action]
if done:
Q_target = reward # 终止状态
else:
Q_target = reward + self.gamma * self.Q[next_state][next_action] # 与Q learning不同Sarsa是拿下一步动作对应的Q值去更新
self.Q[state][action] += self.lr * (Q_target - Q_predict)
def save(self,path):
'''把 Q表格 的数据保存到文件中
def predict_action(self,state):
''' predict action while testing
'''
action = np.argmax(self.Q_table[state])
return action
def update(self, state, action, reward, next_state, next_action,done):
Q_predict = self.Q_table[state][action]
if done:
Q_target = reward # terminal state
else:
Q_target = reward + self.gamma * self.Q_table[next_state][next_action] # the only difference from Q learning
self.Q_table[state][action] += self.lr * (Q_target - Q_predict)
def save_model(self,path):
import dill
from pathlib import Path
# create path
Path(path).mkdir(parents=True, exist_ok=True)
torch.save(
obj=self.Q,
f=path+"sarsa_model.pkl",
obj=self.Q_table_table,
f=path+"checkpoint.pkl",
pickle_module=dill
)
def load(self, path):
'''从文件中读取数据到 Q表格
'''
print("Model saved!")
def load_model(self, path):
import dill
self.Q =torch.load(f=path+'sarsa_model.pkl',pickle_module=dill)
self.Q_table_table =torch.load(f=path+'checkpoint.pkl',pickle_module=dill)
print("Mode loaded!")