Created
January 1, 2020 12:51
-
-
Save thunderInfy/46bb454f50c0cf79cc923ecfc872169e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Monte_Carlo_Control: | |
| #HELPFUL FUNCTIONS | |
| def evaluate_target_policy(self): | |
| env.reset() | |
| state = env.start() | |
| self.data.episode['S'].append(state) | |
| rew = -1 | |
| while rew!=None: | |
| action = agent.get_action(state,self.generate_target_policy_action) | |
| rew, state = env.step(state,action) | |
| self.data.rewards.append(sum(self.data.episode['R'][1:])) | |
| def plot_rewards(self): | |
| ax, fig = plt.subplots(figsize=(30,15)) | |
| x = np.arange(1,len(self.data.rewards)+1) | |
| plt.plot(x*10, self.data.rewards, linewidth=0.5, color = '#BB8FCE') | |
| plt.xlabel('Episode number', size = 20) | |
| plt.ylabel('Reward',size = 20) | |
| plt.title('Plot of Reward vs Episode Number',size=20) | |
| plt.xticks(size=20) | |
| plt.yticks(size=20) | |
| plt.savefig('RewardGraph.png') | |
| plt.close() | |
| def save_your_work(self): | |
| self.data.save_Q_vals() | |
| self.data.save_C_vals() | |
| self.data.save_π() | |
| self.data.save_rewards() | |
| def determine_probability_behaviour(self, state, action, possible_actions): | |
| best_action = self.data.π[tuple(state)] | |
| num_actions = len(possible_actions) | |
| if best_action in possible_actions: | |
| if action == best_action: | |
| prob = 1 - self.data.ε + self.data.ε/num_actions | |
| else: | |
| prob = self.data.ε/num_actions | |
| else: | |
| prob = 1/num_actions | |
| self.data.episode['probs'].append(prob) | |
| def generate_target_policy_action(self, state, possible_actions): | |
| ''' | |
| Returns target policy action, takes state and | |
| returns an action using this policy | |
| ''' | |
| if self.data.π[tuple(state)] in possible_actions: | |
| action = self.data.π[tuple(state)] | |
| else: | |
| action = np.random.choice(possible_actions) | |
| return action | |
| def generate_behavioural_policy_action(self, state, possible_actions): | |
| ''' | |
| Returns behavioural policy action | |
| which would be ε-greedy π policy, takes state and | |
| returns an action using this ε-greedy π policy | |
| ''' | |
| if np.random.rand() > self.data.ε and self.data.π[tuple(state)] in possible_actions: | |
| action = self.data.π[tuple(state)] | |
| else: | |
| action = np.random.choice(possible_actions) | |
| self.determine_probability_behaviour(state, action, possible_actions) | |
| return action | |
| #CONSTRUCTOR | |
| def __init__(self, data): | |
| ''' | |
| Initialize, for all s ∈ S, a ∈ A(s): | |
| data.Q(s, a) ← arbitrary (done in Data) | |
| data.C(s, a) ← 0 (done in Data) | |
| π(s) ← argmax_a Q(s,a) | |
| (with ties broken consistently) | |
| (some consistent approach needs to be followed)) | |
| ''' | |
| self.data = data | |
| for i in range(100): | |
| for j in range(100): | |
| if self.data.racetrack[i,j]!=-1: | |
| for k in range(5): | |
| for l in range(5): | |
| self.data.π[i,j,k,l] = np.argmax(self.data.Q_vals[i,j,k,l]) | |
| def control(self,env,agent): | |
| ''' | |
| Performs MC control using episode list [ S0 , A0 , R1, . . . , ST −1 , AT −1, RT , ST ] | |
| G ← 0 | |
| W ← 1 | |
| For t = T − 1, T − 2, . . . down to 0: | |
| G ← γ*G + R_t+1 | |
| C(St, At ) ← C(St,At ) + W | |
| Q(St, At ) ← Q(St,At) + (W/C(St,At))*[G − Q(St,At )] | |
| π(St) ← argmax_a Q(St,a) (with ties broken consistently) | |
| If At != π(St) then exit For loop | |
| W ← W * (1/b(At|St)) | |
| ''' | |
| env.reset() | |
| state = env.start() | |
| self.data.episode['S'].append(state) | |
| rew = -1 | |
| while rew!=None: | |
| action = agent.get_action(state,self.generate_behavioural_policy_action) | |
| rew, state = env.step(state,action) | |
| G = 0 | |
| W = 1 | |
| T = env.step_count | |
| for t in range(T-1,-1,-1): | |
| G = data.γ * G + self.data.episode['R'][t+1] | |
| S_t = tuple(self.data.episode['S'][t]) | |
| A_t = agent.map_to_1D(self.data.episode['A'][t]) | |
| S_list = list(S_t) | |
| S_list.append(A_t) | |
| SA = tuple(S_list) | |
| self.data.C_vals[SA] += W | |
| self.data.Q_vals[SA] += (W*(G-self.data.Q_vals[SA]))/(self.data.C_vals[SA]) | |
| self.data.π[S_t] = np.argmax(self.data.Q_vals[S_t]) | |
| if A_t!=self.data.π[S_t]: | |
| break | |
| W /= self.data.episode['probs'][t] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment