-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.py
More file actions
70 lines (56 loc) · 2.31 KB
/
Agent.py
File metadata and controls
70 lines (56 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Practical for master course 'Reinforcement Learning',
Leiden University, The Netherlands
By Thomas Moerland
"""
import numpy as np
from Helper import softmax, argmax
import random
class BaseAgent:
def __init__(self, n_states, n_actions, learning_rate, gamma):
self.n_states = n_states
self.n_actions = n_actions
self.learning_rate = learning_rate
self.gamma = gamma
self.Q_sa = np.zeros((n_states,n_actions))
def select_action(self, s, policy='egreedy', epsilon=None, temp=None):
if policy == 'greedy':
# TO DO: Add own code
a = argmax(self.Q_sa[s])
elif policy == 'egreedy':
if epsilon is None:
raise KeyError("Provide an epsilon")
# TO DO: Add own code
"If the probability is lower than epsilon -> select a random action"
if np.random.random(1)[0]<epsilon:
a = np.random.choice(self.n_actions)
else:
"If the probability is greater then epsilon -> select the action with the highest Q-Value"
a = argmax(self.Q_sa[s])
elif policy == 'softmax':
if temp is None:
raise KeyError("Provide a temperature")
# TO DO: Add own code
p = softmax(self.Q_sa[s], temp)
a = int(np.random.choice(self.n_actions, p=p))
return a
def update(self):
raise NotImplementedError('For each agent you need to implement its specific back-up method') # Leave this and overwrite in subclasses in other files
def evaluate(self,eval_env,n_eval_episodes=30, max_episode_length=100):
returns = [] # list to store the reward per episode
for i in range(n_eval_episodes):
s = eval_env.reset()
R_ep = 0
for t in range(max_episode_length):
a = self.select_action(s, 'greedy')
s_prime, r, done = eval_env.step(a)
R_ep += r
if done:
break
else:
s = s_prime
returns.append(R_ep)
mean_return = np.mean(returns)
return mean_return