-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.py
48 lines (39 loc) · 1.17 KB
/
policy.py
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
import numpy as np
class EpsilonGreedyQPolicy(object):
"""Implement the epsilon greedy policy
Eps Greedy policy either:
- takes a random action with probability epsilon
- takes current best action with prob (1 - epsilon)
"""
def __init__(self, eps=.1):
self.eps = eps
def _set_agent(self, agent):
self.agent = agent
def select_action(self, q_values):
"""Return the selected action
# Arguments
q_values (np.ndarray): List of the estimations of Q for each action
# Returns
Selection action
"""
nb_actions = q_values.shape[0]
if np.random.uniform() < self.eps:
# take random action with probability eps
action = np.random.randint(0, nb_actions)
else:
action = np.argmax(q_values)
return action
@property
def metrics_names(self):
return []
@property
def metrics(self):
return []
def get_config(self):
"""Return configurations of EpsGreedyQPolicy
# Returns
Dict of config
"""
config = {}
config['eps'] = self.eps
return config