A.2 Example 2
The second example
solves the following 3-dimensional constrained optimization, printing the details of the
solver’s progress:
| (A.8) |
subject to
First, create a Matlab function to evaluate the objective function and its
gradients,
function [f, df, d2f] = f2(x) f = -x(1)*x(2) - x(2)*x(3); if nargout > 1 %% gradient is required df = -[x(2); x(1)+x(3); x(2)]; if nargout > 2 %% Hessian is required d2f = -[0 1 0; 1 0 1; 0 1 0]; %% actually not used since end %% 'hess_fcn' is provided end
one to evaluate the constraints, in this case inequalities only, and their gradients,
function [h, g, dh, dg] = gh2(x) h = [ 1 -1 1; 1 1 1] * x.^2 + [-2; -10]; dh = 2 * [x(1) x(1); -x(2) x(2); x(3) x(3)]; g = []; dg = [];
and another to evaluate the Hessian of the Lagrangian.
function Lxx = hess2(x, lam, cost_mult) if nargin < 3, cost_mult = 1; end %% allows to be used with 'fmincon' mu = lam.ineqnonlin; Lxx = cost_mult * [0 -1 0; -1 0 -1; 0 -1 0] + ... [2*[1 1]*mu 0 0; 0 2*[-1 1]*mu 0; 0 0 2*[1 1]*mu];
Then create a problem struct with handles to these functions, a starting value for and an
option to print the solver’s progress. Finally, pass this struct to mips to solve the problem
and print some of the return values to get the output below.
function example2 problem = struct( ... 'f_fcn', @(x)f2(x), ... 'gh_fcn', @(x)gh2(x), ... 'hess_fcn', @(x, lam, cost_mult)hess2(x, lam, cost_mult), ... 'x0', [1; 1; 0], ... 'opt', struct('verbose', 2) ... ); [x, f, exitflag, output, lambda] = mips(problem); fprintf('\nf = %g exitflag = %d\n', f, exitflag); fprintf('\nx = \n'); fprintf(' %g\n', x); fprintf('\nlambda.ineqnonlin =\n'); fprintf(' %g\n', lambda.ineqnonlin);
>> example2 MATPOWER Interior Point Solver -- MIPS, Version 1.3.1, 20-Jun-2019 (using built-in linear solver) it objective step size feascond gradcond compcond costcond ---- ------------ --------- ------------ ------------ ------------ ------------ 0 -1 0 1.5 5 0 1 -5.3250167 1.6875 0 0.894235 0.850653 2.16251 2 -7.4708991 0.97413 0.129183 0.00936418 0.117278 0.339269 3 -7.0553031 0.10406 0 0.00174933 0.0196518 0.0490616 4 -7.0686267 0.034574 0 0.00041301 0.0030084 0.00165402 5 -7.0706104 0.0065191 0 1.53531e-05 0.000337971 0.000245844 6 -7.0710134 0.00062152 0 1.22094e-07 3.41308e-05 4.99387e-05 7 -7.0710623 5.7217e-05 0 9.84879e-10 3.41587e-06 6.05875e-06 8 -7.0710673 5.6761e-06 0 9.73527e-12 3.41615e-07 6.15483e-07 Converged! f = -7.07107 exitflag = 1 x = 1.58114 2.23607 1.58114 lambda.ineqnonlin = 0 0.707107
More example problems for mips can be found in t_mips.m.