update
This commit is contained in:
@@ -14,17 +14,17 @@ from collections import defaultdict
|
||||
import torch
|
||||
class Sarsa(object):
|
||||
def __init__(self,
|
||||
n_actions,sarsa_cfg,):
|
||||
self.n_actions = n_actions # number of actions
|
||||
action_dim,sarsa_cfg,):
|
||||
self.action_dim = action_dim # number of actions
|
||||
self.lr = sarsa_cfg.lr # learning rate
|
||||
self.gamma = sarsa_cfg.gamma
|
||||
self.epsilon = sarsa_cfg.epsilon
|
||||
self.Q = defaultdict(lambda: np.zeros(n_actions))
|
||||
# self.Q = np.zeros((n_states, n_actions)) # Q表
|
||||
self.Q = defaultdict(lambda: np.zeros(action_dim))
|
||||
# self.Q = np.zeros((state_dim, action_dim)) # Q表
|
||||
def choose_action(self, state):
|
||||
best_action = np.argmax(self.Q[state])
|
||||
# action = best_action
|
||||
action_probs = np.ones(self.n_actions, dtype=float) * self.epsilon / self.n_actions
|
||||
action_probs = np.ones(self.action_dim, dtype=float) * self.epsilon / self.action_dim
|
||||
action_probs[best_action] += (1.0 - self.epsilon)
|
||||
action = np.random.choice(np.arange(len(action_probs)), p=action_probs)
|
||||
return action
|
||||
|
||||
@@ -70,8 +70,8 @@ def sarsa_train(cfg,env,agent):
|
||||
if __name__ == "__main__":
|
||||
sarsa_cfg = SarsaConfig()
|
||||
env = RacetrackEnv()
|
||||
n_actions=9
|
||||
agent = Sarsa(n_actions,sarsa_cfg)
|
||||
action_dim=9
|
||||
agent = Sarsa(action_dim,sarsa_cfg)
|
||||
rewards,ma_rewards = sarsa_train(sarsa_cfg,env,agent)
|
||||
agent.save(path=SAVED_MODEL_PATH)
|
||||
save_results(rewards,ma_rewards,tag='train',path=RESULT_PATH)
|
||||
|
||||
Reference in New Issue
Block a user