-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata_generator.py
More file actions
135 lines (93 loc) · 5.38 KB
/
data_generator.py
File metadata and controls
135 lines (93 loc) · 5.38 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from abc import ABC
import numpy as np
import pickle
import random
class DataGenerator(ABC):
def __init__(self, mu=0, sigma=1, lb=-1., ub=1., p=20):
self._mu = mu
self._sigma = sigma
self._lb = lb
self._ub = ub
self.p = p
def get_context(self):
return NotImplementedError
def get_reward(self, context, action):
return NotImplementedError
def get_mean_reward(self, context, action):
return NotImplementedError
class DataGeneratorScena1(DataGenerator):
def __init__(self, mu=0, sigma=1, lb=-1., ub=1., p=20):
super(DataGeneratorScena1, self).__init__(mu, sigma, lb, ub, p)
def get_context(self):
return np.random.uniform(self._lb, self._ub, self.p)
def get_reward(self, context, action):
return (action < 0.35) * (1 + context[0]) + (action >= 0.35) * (action <= 0.65) * (context[0] - context[1]) + (action > 0.65) * (1 - context[1]) + np.random.normal(self._mu, self._sigma, 1)[0]
def get_mean_reward(self, context, action):
return (action < 0.35) * (1 + context[0]) + (action >= 0.35) * (action <= 0.65) * (context[0] - context[1]) + (action > 0.65) * (1 - context[1])
class DataGeneratorScena2(DataGenerator):
def __init__(self, mu=0, sigma=1, lb=-1., ub=1., p=20):
super(DataGeneratorScena2, self).__init__(mu, sigma, lb, ub, p)
def get_context(self):
return np.random.uniform(self._lb, self._ub, self.p)
def get_reward(self, context, action):
return (action < 0.25) + (action >= 0.25) * (action < 0.5) * (np.sin(2 * np.pi * context[0])) + (action >= 0.5) * (action < 0.75) * (0.5 - 8 * (context[0] - 0.75) ** 2) + 0.5 * (action >= 0.75) + np.random.normal(self._mu, self._sigma, 1)[0]
def get_mean_reward(self, context, action):
return (action < 0.25) + (action >= 0.25) * (action < 0.5) * (np.sin(2 * np.pi * context[0])) + (action >= 0.5) * (action < 0.75) * (0.5 - 8 * (context[0] - 0.75) ** 2) + 0.5 * (action >= 0.75)
class DataGeneratorScena3(DataGenerator):
def __init__(self, mu=0, sigma=1, lb=-1., ub=1., p=20):
super(DataGeneratorScena3, self).__init__(mu, sigma, lb, ub, p)
def get_context(self):
return np.random.uniform(self._lb, self._ub, self.p)
def get_reward(self, context, action):
return (action > 0.5) * 10 * np.log(context[0] + 2) * max(action ** 2 - 0.25, 0) + np.random.normal(self._mu, self._sigma, 1)[0]
def get_mean_reward(self, context, action):
return (action > 0.5) * 10 * np.log(context[0] + 2) * max(action ** 2 - 0.25, 0)
class DataGeneratorScena4(DataGenerator):
def __init__(self, mu=0, sigma=1, lb=-1., ub=1., p=20):
super(DataGeneratorScena4, self).__init__(mu, sigma, lb, ub, p)
def get_context(self):
return np.random.uniform(self._lb, self._ub, self.p)
def get_reward(self, context, action):
return (8 + 4 * context[0] - 2 * context[1] - 2 * context[2] - 10 * (1 + 0.5 * context[0] + 0.5 * context[1] - 2 * action) ** 2) / 5 + np.random.normal(self._mu, self._sigma, 1)[0]
def get_mean_reward(self, context, action):
return (8 + 4 * context[0] - 2 * context[1] - 2 * context[2] - 10 * (1 + 0.5 * context[0] + 0.5 * context[1] - 2 * action) ** 2) / 5
class RealDataGenerator(DataGenerator):
def __init__(self, file_name='real_envir.pickle'):
with open(file_name, 'rb') as handle:
envir= pickle.load(handle)
self.org_data = envir[0]
self.regr_mean = envir[1]
self.sigma = envir[2]
self.Q_nn = envir[3]
self.tau = envir[4]
def get_sample(self, policy):
idx = random.sample(range(len(self.org_data)), 1)[0]
context = self.org_data['xt'][idx]
if policy == 'behavior':
action = self.org_data['at'][idx]
else:
action = policy(context)
# mean of y
y_mean = self.regr_mean.predict(np.append(np.array(context),np.array(action)).reshape(1, len(context)+1))
# std of y
#res_mean = self.regr_res.predict(np.append(np.array(context),np.array(action)).reshape(1, len(context)+1))
return context, action, np.random.normal(y_mean[0], self.sigma, 1)[0]
def get_mean_reward(self, policy):
idx = random.sample(range(len(self.org_data)), 1)[0]
context = self.org_data['xt'][idx]
if policy == 'behavior':
action = self.org_data['at'][idx]
else:
action = policy(context)
# mean of y
y_mean = self.regr_mean.predict(np.append(np.array(context),np.array(action)).reshape(1, len(context)+1))
return y_mean[0]
class ToyExample(DataGenerator):
def __init__(self, mu=0, sigma=1, lb=0., ub=1., p=20):
super(ToyExample, self).__init__(mu, sigma, lb, ub, p)
def get_context(self):
return np.random.uniform(self._lb, self._ub, self.p)[0]
def get_reward(self, context, action):
return (action > 0.5) * 10 * np.log(context + 2) * max(action ** 2 - 0.25, 0) + np.random.normal(self._mu, self._sigma, 1)[0]
def get_mean_reward(self, context, action):
return (action > 0.5) * 10 * np.log(context + 2) * np.maximum(action ** 2 - 0.25, 0)