-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboltzman_q_policy.py
44 lines (34 loc) · 1003 Bytes
/
boltzman_q_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
from policy import *
from copy import deepcopy
##
## @brief Class for boltzamn Q policy.
##
class BoltzamnQPolicy(Policy):
##
## @brief Constructs the object.
##
## @param self The object
## @param tau The temperature
##
def __init__(self, tau = 1.0):
self.prec_qvalues = None
self.tau = tau
##
## @brief Gets the action.
##
## @param self The object
## @param observation The observation
## @param action_space The action space
## @param qvalues The qvalues
##
## @return The action selected
##
def getAction(self, observation, action_space, qvalues):
#All the qvalues are reduced to prevent the exponential from an overflow
normalized_q_values = qvalues - min(qvalues[0])
exp_q_values = np.exp(normalized_q_values / self.tau)
probs = exp_q_values / np.sum(exp_q_values)
probs = probs[0]
probs = probs / sum(probs)
a = np.random.choice(range(qvalues.shape[1]), p = probs)
return a