Code Generation for linprog Background - MATLAB & Simulink (original) (raw)
Main Content
What Is Code Generation?
Code generation is the conversion of MATLAB® code to C/C++ code using MATLAB Coder™. Code generation requires a MATLAB Coder license.
Typically, you use code generation to deploy code on hardware that is not running MATLAB.
For an example, see Generate Code for linprog. For examples of code generation in other optimization solvers, see Generate Code for fmincon, Generate Code for fsolve, Generate Code for quadprog, Generate Code for coneprog, or Generate Code for lsqcurvefit or lsqnonlin.
Code Generation Requirements
linprog
supports code generation using either the codegen (MATLAB Coder) function or the MATLAB Coder app. You must have a MATLAB Coder license to generate code.- The target hardware must support standard double-precision floating-point computations.
- Code generation targets do not use the same math kernel libraries as MATLAB solvers. Therefore, code generation solutions can vary from solver solutions, especially for poorly conditioned problems.
linprog
does not support the problem argument for code generation.
[x,fval] = linprog(problem) % Not supported- All
linprog
input matrices, such asA
,Aeq
,lb
, andub
, can be full or sparse. To use sparse inputs, you must enable dynamic memory allocation. SeeCode Generation Limitations (MATLAB Coder). Solvers have good performance with sparse inputs when the fraction of nonzero entries is small. - The
lb
andub
arguments must have the same number of entries as the linear objective function vectorf
, or must be empty[]
. - If your target hardware does not support infinite bounds, use optim.coder.infbound.
- For advanced code optimization involving embedded processors, you also need an Embedded Coder® license.
- You must include options for
linprog
and specify them usingoptimoptions. The options must include theAlgorithm
option set to"interior-point"
.
options = optimoptions("linprog",Algorithm="interior-point");
[x,fval,exitflag] = linprog(f,A,b,Aeq,beq,lb,ub,options); - Code generation supports these options:
Algorithm
— Must be"interior-point"
ConstraintTolerance
Display
— Specified as"off"
,"none"
, or"final"
for no display, or"iter"
for iterative displayMaxIterations
OptimalityTolerance
- Generated code has limited error checking for options. The recommended way to update an option is to use dot notation, not
optimoptions
.
opts = optimoptions("linprog",Algorithm="interior-point");
opts = optimoptions(opts,MaxIterations=1e4); % Not recommended
opts.MaxIterations = 1e4; % Recommended linprog
supports options passed to the solver, not only options created in the code.- If you specify an option that is not supported, the option is typically ignored during code generation. For reliable results, specify only supported options.
Generated Code Not Multithreaded
By default, generated code for use outside the MATLAB environment uses linear algebra libraries that are not multithreaded. Therefore, this code can run significantly slower than code in the MATLAB environment.
If your target hardware has multiple cores, you can achieve better performance by using custom multithreaded LAPACK and BLAS libraries. For details about incorporating these libraries in your generated code, see Speed Up Linear Algebra in Generated Standalone Code by Using LAPACK Calls (MATLAB Coder).
See Also
linprog | codegen (MATLAB Coder) | optimoptions | optim.coder.infbound