-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_framework.py
279 lines (231 loc) · 8.67 KB
/
model_framework.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Model Generator
# generate the regressor model based on a Kronecker Product of different function
# each function is s one dimensional function of task variables
import numpy as np
import math
import pickle
import scipy
from scipy.special import comb
class Basis:
def __init__(self, n, var_name):
self.n = n
self.var_name = var_name
#Need to implement with other subclasses
def evaluate(self, x):
pass
#Need to implement the derivative of this also
def evaluate_derivative(self,x):
pass
def evaluate_conditional(self, x, apply_derivative):
if(apply_derivative == True):
return self.evaluate_derivative(x)
else:
return self.evaluate(x)
class Polynomial_Basis(Basis):
def __init__(self, n, var_name):
Basis.__init__(self, n, var_name)
#self.size = n
# New version: start from x to the power of 1; n >= 1
if n == 0:
self.size = 1
else:
self.size = n
# evaluate the model at the given x value
def evaluate(self, x):
#result = [x**i for i in range(0, self.n)]
# New version: start from x to the power of 1; n >= 1
if self.n == 0:
result = [1]
else:
result = [x**i for i in range(1, self.n + 1)]
##########################################################
return np.array(result)
# evaluate the derivative of the model at the given x value
def evaluate_derivative(self, x):
#if self.n == 1:
# result = [0]
#elif self.n > 1:
# result = [0]
# result += [i * x**(i-1) for i in range(1, self.n)]
# New version: start from x to the power of 1; n >= 1
if self.n == 0:
result = [0]
else:
result = [i * x**(i-1) for i in range(1, self.n + 1)]
##########################################################
return np.array(result)
class Fourier_Basis(Basis):
def __init__(self, n, var_name):
Basis.__init__(self, n, var_name)
self.size = 2*n-1
#This function will evaluate the model at the given x value
def evaluate(self, x):
result = [1]
result += [np.cos(2*np.pi*i*x) for i in range(1, self.n)]
result += [np.sin(2*np.pi*i*x) for i in range(1, self.n)]
return np.array(result)
#This function will evaluate the derivative of the model at the given x value
def evaluate_derivative(self, x):
result = [0]
result += [-2*np.pi*i*np.sin(2*np.pi*i*x) for i in range(1, self.n)]
result += [2*np.pi*i*np.cos(2*np.pi*i*x) for i in range(1, self.n)]
return np.array(result)
class Berstein_Basis(Basis):
def __init__(self, n, var_name):
Basis.__init__(self, n, var_name)
self.size = n + 1
#This function will evaluate the model at the given x value
def evaluate(self, x):
result = [comb(self.n, i) * x**i * (1-x)**(self.n-i) for i in range(0, self.n + 1)]
return np.array(result)
#This function will evaluate the derivative of the model at the given x value
def evaluate_derivative(self, x):
if self.n >= 2:
result = [-self.n * (1-x)**(self.n-1)]
result += [comb(self.n, i) * (i * x**(i-1) * (1-x)**(self.n-i) - x**i * (self.n-i) * (1-x)**(self.n-i-1)) for i in range(1, self.n)]
result += [self.n * x**(self.n-1)]
elif self.n == 1:
result = [-1, 1]
elif self.n == 0:
result = [0]
return np.array(result)
#Model Object:
# list of basis objects
# string description
# model_size
class Kronecker_Model:
def __init__(self, *funcs):
self.funcs = funcs
#Calculate the size of the parameter array
#Additionally, pre-allocate arrays for kronecker products intermediaries
# to speed up results
self.alocation_buff = []
size = 1
for func in funcs:
#Since we multiply left to right, the total size will be on the left
#and the size for the new row will be on the right
#print((str(size), str(func.size)))
self.alocation_buff.append(np.zeros((size, func.size)))
size = size * func.size
self.size = size
self.num_states = len(funcs)
#Evaluate the models at the function inputs that are received
#The function inputs are expected in the same order as they where defined
#Alternatively, you can also input a dictionary with the var_name as the key and the
# value you want to evaluate the function as the value
def evaluate(self, *function_inputs, partial_derivative = None):
#Crop so that you are only using the number of states and not the gait fingerprint
states = function_inputs[:self.num_states]
#Verify that you have the correct input
if(len(states) != len(self.funcs)):
err_string = 'Wrong amount of inputs. Received:' + str(len(states)) + ', expected:' + str(len(self.funcs))
raise ValueError(err_string)
#if(isinstance(states,dict) == False and isinstance(states,list) == False):
# raise TypeError("Only Lists and Dicts are supported, you used:" + str(type(states)))
#There are two behaviours: one for list and one for dictionary
#List expects the same order that you received it in
#Dictionary has key values for the function var names
result = np.array([1])
#Assume that you get a list which means that everything is in order
for values in zip(states, self.funcs, self.alocation_buff):
curr_val, curr_func, curr_buf = values
#If you get a dictionary, then get the correct input for the function
if(isinstance(states, dict) == True):
#Get the value from the var_name in the dictionary
curr_val = states[curr_func.var_name]
#Verify if we want to take the partial derivative of this function
#if(partial_derivative is not None and curr_func.var_name in partial_derivative):
if(partial_derivative is not None and curr_func.var_name == partial_derivative):
apply_derivative = True
else:
apply_derivative = False
#Since there isnt an implementation for doing kron in one shot, do it one by one
result = fast_kronecker(result, curr_func.evaluate_conditional(curr_val, apply_derivative), curr_buf)
return result
#Evaluate model
def model_prediction(model, psi, *input_list, partial_derivative = None):
result = [model.evaluate(*function_inputs, partial_derivative = partial_derivative) @ psi for function_inputs in zip(*input_list)]
return np.array(result)
##LOOK HERE
##There is a big mess with how the measurement model is storing the gait fingerprint coefficients
##They should really just be part of the state vector, the AXIS should be stored internally since that is
## fixed
class Measurement_Model():
def __init__(self, *models):
self.models = models
def evaluate_h_func(self, Psi, *states):
h = np.zeros((np.shape(Psi)[0], 1))
k = 0
for model in self.models:
h[k] = model.evaluate(*states) @ Psi[k].T #Psi[k, :].T
#print("f_reg", model.evaluate(*states))
k = k + 1
return h
def evaluate_dh_func(self, Psi, *states):
H = np.zeros((np.shape(Psi)[0], np.size(states)))
k = 0
for model in self.models:
j = 0
for func in model.funcs:
#print(func.var_name)
Reg = model.evaluate(*states, partial_derivative = func.var_name)
H[k, j] = Reg @ Psi[k].T #Psi[k, :].T
#print("f_derivative_reg", Reg)
j = j + 1
k = k + 1
return H
#Calculate the least squares based on the data
def least_squares(model, output, *data):
#Get data size
rows = data[0].shape[0]
columns = model.size
# Regressor Matrix
R = np.zeros((rows, columns))
counter = 0
for row in zip(*data):
R[counter, :] = model.evaluate(*row)
counter = counter + 1
# Only make it a np array if it isnt
if isinstance(output,(np.ndarray)):
output = np.array(output)
# linear least square solution
psi = np.linalg.solve(R.T @ R, R.T @ output)
return psi
#Save the model so that you can use them later
def model_saver(model, filename):
with open('Basis_model/' + filename, 'wb') as file:
pickle.dump(model, file)
#Load the model from a file
def model_loader(filename):
with open('Basis_model/' + filename, 'rb') as file:
return pickle.load(file)
# Speed up implementation of the kronecker product
# use outer products if a buffer is provided. This saves the time it takes
# to allocate every intermediate result
def fast_kronecker(a, b, buff=None):
#If you pass the buffer is the fast implementation
#139 secs with 1 parameter fit
if(buff is not None):
return np.outer(a, b, buff).ravel().copy()
#Else use the default implementation
#276.738 secs with 1 param
else:
return np.kron(a, b)
def model_test():
phase_model = Fourier_Basis(1, 'phase')
phase_dot_model = Polynomial_Basis(2, 'phase_dot')
step_length_model = Berstein_Basis(0,'step_length')
ramp_model = Berstein_Basis(0, 'ramp')
model_t = Kronecker_Model(phase_model, phase_dot_model, step_length_model, ramp_model)
m_model = Measurement_Model(model_t)
Psi = [np.ones((1, 8))]
z=m_model.evaluate_h_func(Psi, 2, 6, 7, 5)
dz=m_model.evaluate_h_func(Psi, 2, 6+1, 7, 5)-z
dx=np.array([[0], [1], [0], [0]])
H=m_model.evaluate_dh_func(Psi, 2, 6, 7, 5)
print("f = ", z)
print("H= ", H)
print("df", dz)
print("H dx", H @ dx)
if __name__ == '__main__':
model_test()