This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
209 lines (148 loc) · 6.55 KB
/
models.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import numpy as np
class ModelBase(object):
def __init__(self, action_fa=None, observation_fa=None):
self.observation_fa = observation_fa
self.action_fa = action_fa
@property
def n_observations(self):
return self.observation_fa.num_discrete
@property
def n_actions(self):
return self.action_fa.num_discrete
def action_value(self, observation):
"""Returns an array of values corresponding to possible actions in a state"""
raise NotImplementedError
def state_value(self, observation):
"""Returns the value for being in a particular state assuming the best action is taken"""
raise NotImplementedError
def state_action_value(self, observation, action):
"""Returns the expected value for performing an action in state"""
raise NotImplementedError
def export_values(self):
raise NotImplementedError
def import_values(self, values):
raise NotImplementedError
def reset(self):
raise NotImplementedError
def optimise(self, observation, action, value):
pass
def configure(self, action_fa, observation_fa):
raise NotImplementedError
def export(self):
raise NotImplementedError
def update(self, observation, action, value):
raise NotImplementedError
class DefaultModel(ModelBase):
"""Default Model where nothing happens"""
def __init__(self, action_fa=None, observation_fa=None):
ModelBase.__init__(self, action_fa, observation_fa)
if self.action_fa is not None and self.observation_fa is not None:
self.configure(self.action_fa, self.observation_fa)
def configure(self, action_fa, observation_fa):
self.action_fa = action_fa
self.observation_fa = observation_fa
def action_value(self, observation):
return [0] * self.n_actions
def state_value(self, observation):
return [0]
def state_action_value(self, observation, action):
return [0]
def export_values(self):
return [0]
def import_values(self, values):
pass
def reset(self):
pass
def update(self, observation, action, value):
pass
def export(self):
return {"Type": "Default"}
class WeightedLinearModel(ModelBase):
"""
Applies weighted linear function to an observation
"""
def __init__(self, action_fa=None, observation_fa=None, bias=True, normalise=False):
ModelBase.__init__(self, action_fa, observation_fa)
self.bias = bias
self.normalise = normalise
self.weights = None
self.bias_weight = None
if self.action_fa is not None and self.observation_fa is not None:
self.configure(self.action_fa, self.observation_fa)
def configure(self, action_fa, observation_fa):
self.action_fa = action_fa
self.observation_fa = observation_fa
self.reset()
# Returns the action-value array
def action_value(self, observation):
observation = self.observation_fa.convert(observation)
score = observation.dot(self.weights) + self.bias_weight
return score[0]
def state_value(self, observation):
return max(self.action_value(observation))
def state_action_value(self, observation, action):
if not isinstance(action, int):
raise TypeError("State Action Value current only accepts ints")
return self.action_value(observation)[action]
def export_values(self):
values = np.concatenate((self.weights, self.bias_weight)) if self.bias else self.weights
return values.flatten().copy()
def import_values(self, weights):
if len(weights) != self.n_observations * self.n_actions + (self.n_actions * int(self.bias)):
raise ValueError("Value count can't be inserted into model")
if self.normalise:
weights /= np.linalg.norm(weights)
self.weights = np.array(weights[:self.n_observations * self.n_actions].reshape(self.n_observations, self.n_actions))
if self.bias:
self.bias_weight = np.array(weights[self.n_observations * self.n_actions:].reshape(1, self.n_actions))
def reset(self):
self.weights = np.random.randn(self.n_observations * self.n_actions).reshape(self.n_observations, self.n_actions)
self.bias_weight = np.random.randn(self.n_actions).reshape(1, self.n_actions) if self.bias else np.zeros(self.n_actions).reshape(1, self.n_actions)
def export(self):
return {"Type": "Weighted Linear Model",
"Bias": self.bias,
"Normalise": self.normalise}
def update(self, observation, action, value):
pass
class TabularModel(ModelBase):
def __init__(self, action_fa=None, observation_fa=None, mean=0.0, std=1.0):
ModelBase.__init__(self, action_fa, observation_fa)
self.mean = mean
self.std = std
self.weights = None
self.keys = None
if self.action_fa is not None and self.observation_fa is not None:
self.configure(self.action_fa, self.observation_fa)
def configure(self, action_fa, observation_fa):
self.action_fa = action_fa
self.observation_fa = observation_fa
self.reset()
def state_value(self, observation):
observation = self.observation_fa.convert(observation)
return max(self.weights[observation])
def action_value(self, observation):
observation = self.observation_fa.convert(observation)
return self.weights[observation]
def state_action_value(self, observation, action):
return self.action_value(observation)[action]
def update(self, observation, action, value):
observation = self.observation_fa.convert(observation)
self.weights[observation][action] = value
def export_values(self):
values = []
for i in range(len(self.weights)):
values.extend(self.weights[i])
return np.array(values)
def import_values(self, values):
for i in range(len(values)/self.n_actions):
self.weights[i] = np.array(values[self.n_actions * i: self.n_actions * i + self.n_actions])
def reset(self):
if self.std == 0:
self.weights = np.full((self.observation_fa.n_total, self.action_fa.n_total), self.mean)
else:
self.weights = np.random.normal(self.mean, scale=self.std, size=(self.observation_fa.n_total, self.action_fa.n_total))
self.keys = None
def export(self):
return {"Type": "Tabular Model",
"Mean": self.mean,
"Std": self.std}