-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpcCustomSolver.m
32 lines (31 loc) · 955 Bytes
/
mpcCustomSolver.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
function [x, status] = mpcCustomSolver(H, f, A, b, x0)
% "mpcCustomSolver" enables using "quadprog" from Optmization Toolbox
% as a custom QP solver with linear MPC controller for simulation.
%% Specify solver algorithm and options
options = optimoptions('quadprog','Algorithm','active-set','MaxIterations',30);
if coder.target('MATLAB')
options.Display = 'none';
end
%% Process solver inputs
% Use -A and -b in "quadprog" because MPC QP uses Ax>=b instead
A_custom = -A;
b_custom = -b;
% ensure Hessian is symmetric
H = (H+H')/2;
%% Call "quadprog"
[x, ~, exitflag, output] = quadprog(H, f, A_custom, b_custom, [], [], [], [], x0, options);
%% Converts exit flag to MPC "status"
switch exitflag
case 1
status = output.iterations;
case 0
status = 0;
case -2
status = -1;
otherwise
status = -2;
end
%% If "quadprog" fails to find a solution, set x to the initial guess
if status <= 0
x = x0;
end