-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_mpc.m
45 lines (40 loc) · 858 Bytes
/
run_mpc.m
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
function [X,U] = run_mpc(spec,A,B,maxsteps,options)
arguments
spec Spec
A double
B double
maxsteps double
options.GoalThreshold double = 0.5
options.Verbose logical = false
end
opt = optimoptions("fmincon", ...
MaxIterations=300, ...
HessianApproximation="lbfgs", ...
Display="off" ...
);
environment = spec.environment;
x = environment.x0;
X = [x];
U = [];
for k=1:maxsteps
[U_opt,fval] = fmincon( ...
@(U)spec.objective(x,U),...
spec.U0,...
[],[],[],[],...
spec.Umin,spec.Umax,...
@(U)spec.nonlcon(x,U),...
opt ...
);
u = U_opt(:,1);
x = A*x+B*u;
X = [X x];
U = [U u];
distance_to_goal = norm(x);
if options.Verbose
disp([k,distance_to_goal])
end
if distance_to_goal<options.GoalThreshold
break
end
end
end