How can I run a MPC simulation on command line using a Custom QP Solver?
1 view (last 30 days)
Show older comments
I have designed a mpc object (mpc1) for a control scenario (scenario1) using mpcDesigner.
Then, I have exported mpc1 to Workspace and I have generated a MATLAB script to reproduce current controller design (see Figure 1).

Now, I want to use a Custom QP Solver, so I run on the command line the following:
src = which('mpcCustomSolver.txt');
dest = fullfile(pwd,'mpcCustomSolver.m');
copyfile(src,dest,'f');
In this way, the Custom QP Solver to be used is 'quadprog'.
function [x, status] = mpcCustomSolver(H, f, A, b, x0)
% mpcCustomSolver allows user to specify a custom quadratic programming
% (QP) solver to solve the QP problem formulated by MPC controller. When
% the "mpcobj.Optimizer.CustomSolver" property is set true, instead of
% using the built-in QP solver, MPC controller will now use the customer QP
% solver defined in this function for simulations in MATLAB and Simulink.
%
% The MPC QP problem is defined as follows:
% Find an optimal solution, x, that minimizes the quadratic objective
% function, J = 0.5*x'*H*x + f'*x, subject to linear inequality
% constraints, A*x >= b.
%
% Inputs (provided by MPC controller at run-time):
% H: a n-by-n Hessian matrix, which is symmetric and positive definite.
% f: a n-by-1 column vector.
% A: a m-by-n matrix of inequality constraint coefficients.
% b: a m-by-1 vector of the right-hand side of inequality constraints.
% x0: a n-by-1 vector of the initial guess of the optimal solution.
%
% Outputs (fed back to MPC controller at run-time):
% x: must be a n-by-1 vector of optimal solution.
% status: must be an finite integer of:
% positive value: number of iterations used in computation
% 0: maximum number of iterations reached
% -1: QP is infeasible
% -2: Failed to find a solution due to other reasons
% Note that even if solver failed to find an optimal solution, "x" must be
% returned as a n-by-1 vector (i.e. set it to the initial guess x0)
%
% DO NOT CHANGE LINES ABOVE
% The following code is an example of how to implement the custom QP solver
% in this function. It requires Optimization Toolbox to run.
% Define QUADPROG options and turn off display of optimization results in
% Command window.
options = optimoptions('quadprog');
options.Display = 'none';
% By definition, constraints required by "quadprog" solver is defined as
% A*x <= b. However, in our MPC QP problem, the constraints are defined as
% A*x >= b. Therefore, we need to implement some conversion here:
A_custom = -A;
b_custom = -b;
% Compute the QP's optimal solution. Note that the default algorithm used
% by "quadprog" ('interior-point-convex') ignores x0. "x0" is used here as
% an input argument for illustration only.
H = (H+H')/2; % ensure Hessian is symmetric
[x, ~, Flag, Output] = quadprog(H, f, A_custom, b_custom, [], [], [], [], x0, options);
% Converts the "flag" output to "status" required by the MPC controller.
switch Flag
case 1
status = Output.iterations;
case 0
status = 0;
case -2
status = -1;
otherwise
status = -2;
end
% Always return a non-empty x of the correct size. When the solver fails,
% one convenient solution is to set x to the initial guess.
if status <= 0
x = x0;
end
As a custom QP solver is going to be used, I set
mpc1.Optimizer.CustomSolver=true
Finally, to reproduce the simulation with the Custom QP Solver I run the following in the command line:
%%specify simulation options
options = mpcsimopt();
options.MVSignal = mpc1_MVSignal;
options.RefLookAhead = 'on';
options.MDLookAhead = 'off';
options.Constraints = 'on';
options.OpenLoop = 'off';
%%run simulation
[y,t,u]=sim(mpc1, 151, mpc1_RefSignal, mpc1_MDSignal, options);
But I always get this error:
The number of rows and columns in H must equal the number of elements of f.
Error in quadprog (line 254)
error(message('optim:quadprog:MismatchObjCoefSize'));
Error in mpcCustomSolver (line 48)
[x, ~, Flag, Output] = quadprog(H, f, A_custom, b_custom, [], [], [], [], x0, options);
Error in mpc_solveQP (line 39)
Error in mpc_simPostprocess (line 12)
Error in sim (line 94)
As it is said in function [x, status] = mpcCustomSolver(H, f, A, b, x0) H,f are provided by MPC controller at run-time, so I can't guess what am I doing wrong.
% Inputs (provided by MPC controller at run-time):
% H: a n-by-n Hessian matrix, which is symmetric and positive definite.
% f: a n-by-1 column vector.
% A: a m-by-n matrix of inequality constraint coefficients.
% b: a m-by-1 vector of the right-hand side of inequality constraints.
% x0: a n-by-1 vector of the initial guess of the optimal solution.
Taking all these into account, every help is wellcome in order to solve the following questions :
1) Is the approach explained above correct? I mean, Can I follow this strategy in order to run the simulation using a Custom QP Solver?
2) In case that the approach explained above is not correct, How can I run the simulation on command line using a Custom QP Solver? Is there an alternative?
2) In case that the approach explained above is correct, How can I solve the error in quadprog ?
The number of rows and columns in H must equal the number of elements of f.
Error in quadprog (line 254)
error(message('optim:quadprog:MismatchObjCoefSize'));
0 Comments
Answers (0)
See Also
Categories
Find more on Controller Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!