-
Notifications
You must be signed in to change notification settings - Fork 6
/
SimglucosePolicyFixedArea.py
87 lines (74 loc) · 3.31 KB
/
SimglucosePolicyFixedArea.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import autograd.numpy as np
from seldonian.RL.Agents.Policies.Policy import Policy
from seldonian.RL.Agents.Function_Approximators.Function_Approximator import (
Function_Approximator,
)
class SigmoidPolicyFixedArea(Policy):
def __init__(
self, bb_crmin, bb_crmax, bb_cfmin, bb_cfmax, cr_shrink_factor, cf_shrink_factor
):
"""
A policy used for the simglucose problem studied in this example: https://seldonian.cs.umass.edu/Tutorials/examples/diabetes/
:param bb_crmin: The bounding box minimum value in CR space.
:type bb_crmin: float
:param bb_crmax: The bounding box maximum value in CR space.
:type bb_crmax: float
:param bb_cfmin: The bounding box minimum value in CF space.
:type bb_cfmin: float
:param bb_cfmax: The bounding box maximum value in CF space.
:type bb_cfmax: float
:param cr_shrink_factor: How much to shrink the CR size by
:param cf_shrink_factor: How much to shrink the CF size by
"""
self.bb_crmin = bb_crmin
self.bb_crmax = bb_crmax
self.bb_cfmin = bb_cfmin
self.bb_cfmax = bb_cfmax
self.cr_shrink_factor = cr_shrink_factor
self.crwidth = (self.bb_crmax - self.bb_crmin) / cr_shrink_factor
self.new_crmin = self.bb_crmin + self.crwidth / 2
# center can range from crmin to new_crmax
self.new_cr_range = (self.bb_crmax - self.bb_crmin) - self.crwidth
self.cf_shrink_factor = cf_shrink_factor
self.cfwidth = (self.bb_cfmax - self.bb_cfmin) / cf_shrink_factor
self.new_cfmin = self.bb_cfmin + self.cfwidth / 2
# center can range from cfmin to new_cfmax
self.new_cf_range = (self.bb_cfmax - self.bb_cfmin) - self.cfwidth
self.FA = Function_Approximator() # needed to get and set self.FA.weights
# Initialize weights
self.FA.weights = np.array([-5.0, 5.0]) # each can range -inf to +inf
def set_new_params(self, new_params):
"""Set the parameters of the agent
:param new_params: array of weights
"""
self.FA.set_new_params(new_params)
def get_params(self):
"""Get the current parameters (weights) of the agent
:return: array of weights
"""
return self.FA.weights
def theta2crcf(self, theta):
"""Take theta and return cr_center,cf_center
Keeping in mind that they must be capped
so that when the fixed width of the box
is added, it doesn't exceed the original bounds.
"""
# if no shrinking then just use the entire original box
if self.cr_shrink_factor <= 1:
cr1 = self.bb_crmin
cr2 = self.bb_crmax
else:
cr_cen = self._sigmoid(theta[0]) * self.new_cr_range + self.new_crmin
cr1 = cr_cen - self.crwidth / 2
cr2 = cr1 + self.crwidth
# if no shrinking then just use the entire original box
if self.cf_shrink_factor <= 1:
cf1 = self.bb_cfmin
cf2 = self.bb_cfmax
else:
cf_cen = self._sigmoid(theta[1]) * self.new_cf_range + self.new_cfmin
cf1 = cf_cen - self.cfwidth / 2
cf2 = cf1 + self.cfwidth
return cr1, cr2, cf1, cf2
def _sigmoid(self, X, steepness=1.0):
return 1 / (1 + np.exp(-steepness * X))