-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpc_called.jl
360 lines (302 loc) · 10.8 KB
/
mpc_called.jl
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import Pkg; Pkg.add("JuMP")
import Pkg; Pkg.add("OSQP")
using JuMP
#using Plots
using LinearAlgebra
#! Old model parameters
# #defining all parameters for model and probem
# A = [1 -6.66e-13 -2.03e-9 -4.14e-6;
# 9.83e-4 1 -4.09e-8 -8.32e-5;
# 4.83e-7 9.83e-4 1 -5.34e-4;
# 1.58e-10 4.83e-7 9.83e-4 .9994;
# ]
# B = [9.83e-4 4.83e-7 1.58e-10 3.89e-14]'
# B1 = B
# # fake opsin
# Binh = -B .* (1 .+ randn(4, 1)./5)
# B2 = [B Binh]
# C = [-.0096 .0135 .005 -.0095]
y2z(y) = exp(61.4*y - 5.468)
z2y(z) = (log(z) + 5.468)/61.4
t_step = 0.001 #1 milisecond
pred = 30 #prediction horizon
ctr = 6 #control horizon
sample = 200 #number of steps between sampling points for control
R = 1e-6*I # original R = 1e-5*I
T = 13000
Tpred = pred*sample
using OSQP
#function now designed not to do a control loop but only solve the optimization problem once
#assumes that reference has been "shifted" properly
#renamed to beta since new mpc function below takes variable parameters and is thus more flexible
function mpc_beta(x0, zref; nu=1, u_clamp=nothing, sample=250)
if nu == 1
B = B1
elseif nu == 2
B = B2
end
Tpred = pred*sample
if length(zref) < Tpred
zrefpad = cat(zref, fill(zref[end], Tpred - length(zref)), dims=1)
else
zrefpad = zref
end
#println(typeof(zrefpad))
neuron = Model(OSQP.Optimizer)
set_silent(neuron)
#Define state variables
@variables(neuron, begin
x[i=1:4, t=1:Tpred]
0 ≤ u[1:nu, 1:(ctr+1)] .<= 40
yD[i=1:1, t=1:Tpred]
# xD[i=1:4, t=1:Tpred], (start = 0)
end)
@expressions(
neuron,
begin
y, C*x
# x_error[t=1:Tpred], x[:, t] - xD[:, t]
# x_cost[t=1:Tpred], x_error[t]'*Q*x_error[t]
y_error[t=1:Tpred], y[:, t] - yD[:, t]
y_cost[t=1:Tpred], y_error'[t]*y_error[t]
# sampled_x_cost[t=1:pred], x_cost[t*sample]
sampled_y_cost[t=1:pred], y_cost[t*sample]
u_cost[t=1:ctr+1], u[t]'*R*u[t]
end
)
#fix first sample steps
@constraint(neuron, x[:, 2:(sample)] .== A*x[:, 1:sample-1] + B*u[:, 1])
#fix each sample period
for i in 1:(ctr-1)
@constraint(neuron, x[:, (sample*i+1):(sample*i+sample)] .== A*x[:, (sample*i):(sample*i+sample-1)] + B*u[:, (i+1)])
end
#fix rest of inputs
@constraint(neuron, x[:, (ctr*sample+1):(Tpred)] .== A*x[:, (ctr*sample):(Tpred-1)] + B*u[:, (ctr+1)])
yDall = z2y.(zrefpad)
# x_cost[t] returns a 1x1 matrix, which we need to index to get the value out
# J = @objective(neuron, Min, sum(sampled_x_cost[t] for t in 2:(pred)) + sum(u_cost[t] for t in 1:ctr+1))
J = @objective(neuron, Min, sum(sampled_y_cost[t] for t in 2:(pred)) + sum(u_cost[t] for t in 1:ctr+1))
# if nu == 2
# B = [B -B]
# end
x_current = x0
#for t in 1:steps - got rid of for loop since only optimizing once
fix.(x[:, 1], x_current; force=true)
if u_clamp != nothing
fix.(u[:, 1], u_clamp; force=true)
end
#set desired trajectory and optimize
fix.(yD[:], yDall[1:Tpred], force=true)
optimize!(neuron)
#return first input for use in experiment
optimal_u = value.(u[:, 1])
return optimal_u
end
###new version which will take in variable parameters
function mpc_v2(x0, zref; nu=1, u_clamp=nothing, sample=250, A=A, B=B, C=C)
# if nu == 1
# B = B1
# elseif nu == 2
# B = B2
# end
Tpred = pred*sample
if length(zref) < Tpred
zrefpad = cat(zref, fill(zref[end], Tpred - length(zref)), dims=1)
else
zrefpad = zref
end
#println(typeof(zrefpad))
neuron = Model(OSQP.Optimizer)
set_silent(neuron)
#Define state variables
@variables(neuron, begin
x[i=1:4, t=1:Tpred]
0 ≤ u[1:nu, 1:(ctr+1)] .<= 40
yD[i=1:1, t=1:Tpred]
# xD[i=1:4, t=1:Tpred], (start = 0)
end)
@expressions(
neuron,
begin
y, C*x
# x_error[t=1:Tpred], x[:, t] - xD[:, t]
# x_cost[t=1:Tpred], x_error[t]'*Q*x_error[t]
y_error[t=1:Tpred], y[:, t] - yD[:, t]
y_cost[t=1:Tpred], y_error'[t]*y_error[t]
# sampled_x_cost[t=1:pred], x_cost[t*sample]
sampled_y_cost[t=1:pred], y_cost[t*sample]
u_cost[t=1:ctr+1], u[t]'*R*u[t]
end
)
#fix first sample steps
@constraint(neuron, x[:, 2:(sample)] .== A*x[:, 1:sample-1] + B*u[:, 1])
#fix each sample period
for i in 1:(ctr-1)
@constraint(neuron, x[:, (sample*i+1):(sample*i+sample)] .== A*x[:, (sample*i):(sample*i+sample-1)] + B*u[:, (i+1)])
end
#fix rest of inputs
@constraint(neuron, x[:, (ctr*sample+1):(Tpred)] .== A*x[:, (ctr*sample):(Tpred-1)] + B*u[:, (ctr+1)])
yDall = z2y.(zrefpad)
# x_cost[t] returns a 1x1 matrix, which we need to index to get the value out
# J = @objective(neuron, Min, sum(sampled_x_cost[t] for t in 2:(pred)) + sum(u_cost[t] for t in 1:ctr+1))
J = @objective(neuron, Min, sum(sampled_y_cost[t] for t in 2:(pred)) + sum(u_cost[t] for t in 1:ctr+1))
# if nu == 2
# B = [B -B]
# end
x_current = x0
#for t in 1:steps - got rid of for loop since only optimizing once
fix.(x[:, 1], x_current; force=true)
if u_clamp != nothing
fix.(u[:, 1], u_clamp; force=true)
end
#set desired trajectory and optimize
fix.(yD[:], yDall[1:Tpred], force=true)
optimize!(neuron)
#return first input for use in experiment
optimal_u = value.(u[:, 1])
return optimal_u
end
#!Current version of mpc
#Called flex_mpc since can take variable parameters and switch between firing rate and lfp input
#1 stands for z mode and 2 is for y mode - did this because waned to avoid converting julia and python strings
function flex_mpc(x0, ref; nu=1, sample=250, A=A, B=B, C=C, ref_type=1)
# if nu == 1
# B = B1
# elseif nu == 2
# B = B2
# end
Tpred = pred*sample
# See if we were given a y or z reference
if ref_type == 1
if length(ref) < Tpred
zrefpad = cat(ref, fill(ref[end], Tpred - length(ref)), dims=1)
else
zrefpad = ref
end
yDall = z2y.(zrefpad)
elseif ref_type == 2
if length(ref) < Tpred
yrefpad = cat(ref, fill(ref[end], Tpred - length(ref)), dims=1)
else
yrefpad = ref
end
yDall = yrefpad
end
#println(typeof(zrefpad))
neuron = Model(OSQP.Optimizer)
set_silent(neuron)
#Define state variables
@variables(neuron, begin
x[i=1:4, t=1:Tpred]
0 ≤ u[1:nu, 1:(ctr+1)] .<= 70
yD[i=1:1, t=1:Tpred]
# xD[i=1:4, t=1:Tpred], (start = 0)
end)
@expressions(
neuron,
begin
y, C*x
# x_error[t=1:Tpred], x[:, t] - xD[:, t]
# x_cost[t=1:Tpred], x_error[t]'*Q*x_error[t]
y_error[t=1:Tpred], y[:, t] - yD[:, t]
y_cost[t=1:Tpred], y_error'[t]*y_error[t]
# sampled_x_cost[t=1:pred], x_cost[t*sample]
sampled_y_cost[t=1:pred], y_cost[t*sample]
u_cost[t=1:ctr+1], u[t]'*R*u[t]
end
)
#fix first sample steps
@constraint(neuron, x[:, 2:(sample)] .== A*x[:, 1:sample-1] + B*u[:, 1])
#fix each sample period
for i in 1:(ctr-1)
@constraint(neuron, x[:, (sample*i+1):(sample*i+sample)] .== A*x[:, (sample*i):(sample*i+sample-1)] + B*u[:, (i+1)])
end
#fix rest of inputs
@constraint(neuron, x[:, (ctr*sample+1):(Tpred)] .== A*x[:, (ctr*sample):(Tpred-1)] + B*u[:, (ctr+1)])
# yDall = z2y.(zrefpad)
# x_cost[t] returns a 1x1 matrix, which we need to index to get the value out
# J = @objective(neuron, Min, sum(sampled_x_cost[t] for t in 2:(pred)) + sum(u_cost[t] for t in 1:ctr+1))
J = @objective(neuron, Min, sum(sampled_y_cost[t] for t in 2:(pred)) + sum(u_cost[t] for t in 1:ctr+1))
# if nu == 2
# B = [B -B]
# end
x_current = x0
#for t in 1:steps - got rid of for loop since only optimizing once
fix.(x[:, 1], x_current; force=true)
#set desired trajectory and optimize
fix.(yD[:], yDall[1:Tpred], force=true)
optimize!(neuron)
#return first input for use in experiment
optimal_u = value.(u[:, 1])
return optimal_u
end
# ? Open loop mpc function
function open_loop_mpc(x0, ref; nu=1, sample=3, A=A, B=B, C=C, ref_type=1)
# if nu == 1
# B = B1
# elseif nu == 2
# B = B2
# end
print("\nref:", ref,"\n")
Tpred = length(ref)
print("\ngot length\n")
pred = length(ref)
ctr = Int(trunc(Int, length(ref)/sample)) #must be integer for indexing later
# See if we were given a y or z reference
if ref_type == 1
zrefpad = ref
yDall = z2y.(zrefpad)
elseif ref_type == 2
yDall = ref
end
#println(typeof(zrefpad))
neuron = Model(OSQP.Optimizer)
set_silent(neuron)
print("\ngot model\n")
#Define state variables
@variables(neuron, begin
x[i=1:4, t=1:Tpred]
0 ≤ u[1:nu, 1:(ctr)] .<= 70 #was ctr+1 ??? why
yD[i=1:1, t=1:Tpred]
# xD[i=1:4, t=1:Tpred], (start = 0)
end)
print("\ngot vars\n")
@expressions(
neuron,
begin
y, C*x
# x_error[t=1:Tpred], x[:, t] - xD[:, t]
# x_cost[t=1:Tpred], x_error[t]'*Q*x_error[t]
y_error[t=1:Tpred], y[:, t] - yD[:, t]
y_cost[t=1:Tpred], y_error'[t]*y_error[t]
# sampled_x_cost[t=1:pred], x_cost[t*sample]
#sampled_y_cost[t=1:pred], y_cost[t*sample]
u_cost[t=1:ctr], u[t]'*R*u[t] #was ctr + 1
end
)
print("\ngot expressions\n")
#fix first sample steps
@constraint(neuron, x[:, 2:(sample)] .== A*x[:, 1:sample-1] + B*u[:, 1])
#fix each sample period
for i in 1:(ctr-1)
@constraint(neuron, x[:, (sample*i+1):(sample*i+sample)] .== A*x[:, (sample*i):(sample*i+sample-1)] + B*u[:, (i+1)])
end
#fix rest of inputs
@constraint(neuron, x[:, (ctr*sample+1):(Tpred)] .== A*x[:, (ctr*sample):(Tpred-1)] + B*u[:, (ctr)]) #was ctr+1
# yDall = z2y.(zrefpad)
# x_cost[t] returns a 1x1 matrix, which we need to index to get the value out
# J = @objective(neuron, Min, sum(sampled_x_cost[t] for t in 2:(pred)) + sum(u_cost[t] for t in 1:ctr+1))
J = @objective(neuron, Min, sum(y_cost[t] for t in 2:(Tpred)) + sum(u_cost[t] for t in 1:ctr))
# if nu == 2
# B = [B -B]
# end
x_current = x0
#for t in 1:steps - got rid of for loop since only optimizing once
fix.(x[:, 1], x_current; force=true)
#set desired trajectory and optimize
fix.(yD[:], yDall[1:Tpred], force=true)
optimize!(neuron)
#return first input for use in experiment
optimal_us_vec = value.(u[:, 1:ctr])
return optimal_us_vec
end