update
This commit is contained in:
@@ -16,11 +16,11 @@ import torch
|
||||
class FisrtVisitMC:
|
||||
''' On-Policy First-Visit MC Control
|
||||
'''
|
||||
def __init__(self,n_actions,cfg):
|
||||
self.n_actions = n_actions
|
||||
def __init__(self,action_dim,cfg):
|
||||
self.action_dim = action_dim
|
||||
self.epsilon = cfg.epsilon
|
||||
self.gamma = cfg.gamma
|
||||
self.Q = defaultdict(lambda: np.zeros(n_actions))
|
||||
self.Q = defaultdict(lambda: np.zeros(action_dim))
|
||||
self.returns_sum = defaultdict(float) # sum of returns
|
||||
self.returns_count = defaultdict(float)
|
||||
|
||||
@@ -28,11 +28,11 @@ class FisrtVisitMC:
|
||||
''' e-greed policy '''
|
||||
if state in self.Q.keys():
|
||||
best_action = np.argmax(self.Q[state])
|
||||
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)
|
||||
else:
|
||||
action = np.random.randint(0,self.n_actions)
|
||||
action = np.random.randint(0,self.action_dim)
|
||||
return action
|
||||
def update(self,one_ep_transition):
|
||||
# Find all (state, action) pairs we've visited in this one_ep_transition
|
||||
|
||||
@@ -79,8 +79,8 @@ def mc_train(cfg,env,agent):
|
||||
if __name__ == "__main__":
|
||||
mc_cfg = MCConfig()
|
||||
env = RacetrackEnv()
|
||||
n_actions=9
|
||||
agent = FisrtVisitMC(n_actions,mc_cfg)
|
||||
action_dim=9
|
||||
agent = FisrtVisitMC(action_dim,mc_cfg)
|
||||
rewards,ma_rewards= mc_train(mc_cfg,env,agent)
|
||||
save_results(rewards,ma_rewards,tag='train',path=RESULT_PATH)
|
||||
plot_rewards(rewards,ma_rewards,tag="train",algo = "On-Policy First-Visit MC Control",path=RESULT_PATH)
|
||||
|
||||
Reference in New Issue
Block a user