1## Copyright (C) 2005, 2006, 2007, 2008, 2009 John W. Eaton
3## This file is part of Octave.
5## Octave is free software; you can redistribute it and/or modify it
6## under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
10## Octave is distributed in the hope that it will be useful, but
11## WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13## General Public License for more details.
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
20## @deftypefn {Function File} {[@var{x}, @var{obj}, @var{info}, @var{iter}, @var{nf}, @var{lambda}] =} sqp (@var{x}, @var{phi}, @var{g}, @var{h}, @var{lb}, @var{ub}, @var{maxiter}, @var{tolerance})
21## Solve the nonlinear program
40## g(x) = 0 \qquad h(x) \geq 0 \qquad lb \leq x \leq ub
55## using a successive quadratic programming method.
57## The first argument is the initial guess for the vector @var{x}.
59## The second argument is a function handle pointing to the objective
60## function. The objective function must be of the form
67## in which @var{x} is a vector and @var{y} is a scalar.
69## The second argument may also be a 2- or 3-element cell array of
70## function handles. The first element should point to the objective
71## function, the second should point to a function that computes the
72## gradient of the objective function, and the third should point to a
73## function to compute the hessian of the objective function. If the
74## gradient function is not supplied, the gradient is computed by finite
75## differences. If the hessian function is not supplied, a BFGS update
76## formula is used to approximate the hessian.
78## If supplied, the gradient function must be of the form
85## in which @var{x} is a vector and @var{g} is a vector.
87## If supplied, the hessian function must be of the form
94## in which @var{x} is a vector and @var{h} is a matrix.
96## The third and fourth arguments are function handles pointing to
97## functions that compute the equality constraints and the inequality
98## constraints, respectively.
100## If your problem does not have equality (or inequality) constraints,
101## you may pass an empty matrix for @var{cef} (or @var{cif}).
103## If supplied, the equality and inequality constraint functions must be
111## in which @var{x} is a vector and @var{r} is a vector.
113## The third and fourth arguments may also be 2-element cell arrays of
114## function handles. The first element should point to the constraint
115## function and the second should point to a function that computes the
116## gradient of the constraint function:
120## \Bigg( {\partial f(x) \over \partial x_1},
121## {\partial f(x) \over \partial x_2}, \ldots,
122## {\partial f(x) \over \partial x_N} \Bigg)^T
128## [ d f(x) d f(x) d f(x) ]
129## transpose ( [ ------ ----- ... ------ ] )
135## The fifth and sixth arguments are vectors containing lower and upper bounds
136## on @var{x}. These must be consistent with equality and inequality
137## constraints @var{g} and @var{h}. If the bounds are not specified, or are
138## empty, they are set to -@var{realmax} and @var{realmax} by default.
140## The seventh argument is max. number of iterations. If not specified,
141## the default value is 100.
143## The eighth argument is tolerance for stopping criteria. If not specified,
144## the default value is @var{eps}.
146## Here is an example of calling @code{sqp}:
151## x(2)*x(3)-5*x(4)*x(5);
155## function obj = phi (x)
156## obj = exp(prod(x)) - 0.5*(x(1)^3+x(2)^3+1)^2;
159## x0 = [-1.8; 1.7; 1.9; -0.8; -0.8];
161## [x, obj, info, iter, nf, lambda] = sqp (x0, @@phi, @@g, [])
182## The value returned in @var{info} may be one of the following:
185## The algorithm terminated because the norm of the last step was less
186## than @code{tol * norm (x))} (the value of tol is currently fixed at
187## @code{sqrt (eps)}---edit @file{sqp.m} to modify this value.
189## The BFGS update failed.
191## The maximum number of iterations was reached (the maximum number of
192## allowed iterations is currently fixed at 100---edit @file{sqp.m} to
193## increase this value).
198function [x, obj, info, iter, nf, lambda] = sqp (x, objf, cef, cif, lb, ub, maxiter, tolerance)
201 global __sqp_obj_fun__;
202 global __sqp_ce_fun__;
203 global __sqp_ci_fun__;
205 global __sqp_cifcn__;
207 if (nargin >= 2 && nargin <= 8 && nargin != 5)
209 ## Choose an initial NxN symmetric positive definite Hessan
214 ## Evaluate objective function, constraints, and gradients at initial
219 ## ce_fun -- equality constraint functions
220 ## ci_fun -- inequality constraint functions
221 ## A == [grad_{x_1} cx_fun, grad_{x_2} cx_fun, ..., grad_{x_n} cx_fun]^T
223 obj_grd = @fd_obj_grd;
226 if (length (objf) > 0)
227 __sqp_obj_fun__ = obj_fun = objf{1};
228 if (length (objf) > 1)
230 if (length (objf) > 2)
236 error ("sqp: invalid objective function");
239 __sqp_obj_fun__ = obj_fun = objf;
247 if (length (cef) > 0)
248 __sqp_ce_fun__ = ce_fun = cef{1};
249 if (length (cef) > 1)
253 error ("sqp: invalid equality constraint function");
255 elseif (! isempty (cef))
259 __sqp_ce_fun__ = ce_fun;
265 ## constraint function given by user with possibly gradient
267 ## constraint function given by user without gradient
268 __sqp_cifcn__ = @empty_cf;
269 if (iscell (__sqp_cif__))
270 if (length (__sqp_cif__) > 0)
271 __sqp_cifcn__ = __sqp_cif__{1};
273 elseif (! isempty (__sqp_cif__))
274 __sqp_cifcn__ = __sqp_cif__;
280 if (length (cif) > 0)
281 __sqp_ci_fun__ = ci_fun = cif{1};
282 if (length (cif) > 1)
286 error ("sqp: invalid equality constraint function");
288 elseif (! isempty (cif))
295 elseif (isempty (lb))
296 if (isa (x, "single"))
297 __sqp_lb__ = -realmax ("single");
299 __sqp_lb__ = -realmax;
302 error ("sqp: invalid lower bound");
308 elseif (isempty (lb))
309 if (isa (x, "single"))
310 __sqp_ub__ = realmax ("single");
312 __sqp_ub__ = realmax;
315 error ("sqp: invalid upper bound");
319 error ("sqp: upper bound smaller than lower bound");
321 __sqp_ci_fun__ = ci_fun = @cf_ub_lb;
322 ci_grd = @cigrad_ub_lb;
324 __sqp_ci_fun__ = ci_fun;
328 if (nargin > 6 && ! isempty (maxiter))
329 if (isscalar (maxiter) && maxiter > 0 && round (maxiter) == maxiter)
332 error ("sqp: invalid number of maximum iterations");
337 if (nargin > 7 && ! isempty (tolerance))
338 if (isscalar (tolerance) && tolerance > 0)
341 error ("sqp: invalid value for tolerance");
347 obj = feval (obj_fun, x);
350 c = feval (obj_grd, x);
353 B = feval (obj_hess, x);
358 ce = feval (ce_fun, x);
359 F = feval (ce_grd, x);
361 ci = feval (ci_fun, x);
362 C = feval (ci_grd, x);
366 ## Choose an initial lambda (x is provided by the caller).
368 lambda = 100 * ones (rows (A), 1);
375 ## report (iter, qp_iter, alpha, __sqp_nfun__, obj);
379 while (++iter < iter_max)
381 ## Check convergence. This is just a simple check on the first
382 ## order necessary conditions.
384 ## IDX is the indices of the active inequality constraints.
388 lambda_e = lambda((1:nr_f)');
389 lambda_i = lambda((nr_f+1:end)');
393 t0 = norm (c - A' * lambda);
396 t3 = all (lambda_i >= 0);
397 t4 = norm (lambda .* con);
399 if (t2 && t3 && max ([t0; t1; t4]) < tol)
403 ## Compute search direction p by solving QP.
408 ## Discard inequality constraints that have -Inf bounds since those
409 ## will never be active.
410 idx = isinf (d) & d < 0;
414 [p, obj_qp, INFO, lambda] = qp (x, B, c, F, g, [], [], d, C,
415 Inf * ones (size (d)));
419 ## Check QP solution and attempt to recover if it has failed.
421 ## Choose mu such that p is a descent direction for the chosen
422 ## merit function phi.
424 [x_new, alpha, obj_new] = linesearch_L1 (x, p, obj_fun, obj_grd,
425 ce_fun, ci_fun, lambda, obj);
427 ## Evaluate objective function, constraints, and gradients at
430 c_new = feval (obj_grd, x_new);
432 ce_new = feval (ce_fun, x_new);
433 F_new = feval (ce_grd, x_new);
435 ci_new = feval (ci_fun, x_new);
436 C_new = feval (ci_grd, x_new);
438 A_new = [F_new; C_new];
443 ## y = grad_x L (x_new, lambda) - grad_x L (x, lambda})
448 t = ((A_new - A)'*lambda);
454 if (norm (delx) < tol * norm (x))
461 B = feval (obj_hess, x);
465 ## Update B using a quasi-Newton formula.
469 ## Damped BFGS. Or maybe we would actually want to use the Hessian
470 ## of the Lagrangian, computed directly.
478 theta = 0.8*d1/(d1 - t2);
483 r = theta*y + (1-theta)*B*delx;
487 if (d1 == 0 || d2 == 0)
492 B = B - B*delx*delxt*B/d1 + r*r'/d2;
510 ## report (iter, qp_iter, alpha, __sqp_nfun__, obj);
514 if (iter >= iter_max)
529function [merit, obj] = phi_L1 (obj, obj_fun, ce_fun, ci_fun, x, mu)
533 ce = feval (ce_fun, x);
534 ci = feval (ci_fun, x);
541 obj = feval (obj_fun, x);
546 t = norm (con, 1) / mu;
555function [x_new, alpha, obj] = linesearch_L1 (x, p, obj_fun, obj_grd,
556 ce_fun, ci_fun, lambda, obj)
560 ## eta in the range (0, 0.5)
561 ## tau in the range (0, 1)
566 delta_bar = sqrt (eps);
568 if (isempty (lambda))
571 mu = 1 / (norm (lambda, Inf) + delta_bar);
576 c = feval (obj_grd, x);
577 ce = feval (ce_fun, x);
579 [phi_x_mu, obj] = phi_L1 (obj, obj_fun, ce_fun, ci_fun, x, mu);
582 d = feval (ci_fun, x);
583 ## only those elements of d corresponding
584 ## to violated constraints should be included.
586 t = - norm ([ce; d(idx)], 1) / mu;
592 [p1, obj] = phi_L1 ([], obj_fun, ce_fun, ci_fun, x+alpha*p, mu);
593 p2 = phi_x_mu+eta*alpha*D_phi_x_mu;
595 ## Reset alpha = tau_alpha * alpha for some tau_alpha in the
597 tau_alpha = 0.9 * tau; ## ??
598 alpha = tau_alpha * alpha;
604 ## Set x_new = x + alpha * p;
606 x_new = x + alpha * p;
611function report (iter, qp_iter, alpha, nfun, obj)
614 printf (" Itn ItQP Step Nfun Objective\n");
616 printf ("%5d %4d %8.1g %5d %13.6e\n", iter, qp_iter, alpha, nfun, obj);
622function grd = fdgrd (f, x)
632 grd(i) = (feval (f, x) - y0) / deltax;
642function jac = fdjac (f, x)
648 jac = zeros (nf, nx);
653 jac(:,i) = (feval (f, x) - y0) / deltax;
663function grd = fd_obj_grd (x)
665 global __sqp_obj_fun__;
667 grd = fdgrd (__sqp_obj_fun__, x);
672function res = empty_cf (x)
679function res = empty_jac (x)
681 res = zeros (0, length (x));
686function jac = fd_ce_jac (x)
688 global __sqp_ce_fun__;
690 jac = fdjac (__sqp_ce_fun__, x);
695function jac = fd_ci_jac (x)
697 global __sqp_cifcn__;
698 ## __sqp_cifcn__ = constraint function without gradients and lb or ub
699 jac = fdjac (__sqp_cifcn__, x);
704function res = cf_ub_lb (x)
706 ## combine constraint function with ub and lb
707 global __sqp_cifcn__ __sqp_lb__ __sqp_ub__
709 res = [x-__sqp_lb__; __sqp_ub__-x];
711 if (! isempty (__sqp_cifcn__))
712 res = [feval(__sqp_cifcn__,x); x-__sqp_lb__; __sqp_ub__-x];
718function res = cigrad_ub_lb (x)
722 res = [eye(numel(x)); -eye(numel(x))];
724 cigradfcn = @fd_ci_jac;
726 if (iscell (__sqp_cif__) && length (__sqp_cif__) > 1)
727 cigradfcn = __sqp_cif__{2};
730 if (! isempty (cigradfcn))
731 res = [feval(cigradfcn,x); eye(numel(x)); -eye(numel(x))];
738%! x(2)*x(3)-5*x(4)*x(5);
741%!function obj = phi (x)
742%! obj = exp(prod(x)) - 0.5*(x(1)^3+x(2)^3+1)^2;
745%! x0 = [-1.8; 1.7; 1.9; -0.8; -0.8];
747%! [x, obj, info, iter, nf, lambda] = sqp (x0, @phi, @g, []);
749%! x_opt = [-1.717143501952599;
752%! -0.763643103133572;
753%! -0.763643068453300];
755%! obj_opt = 0.0539498477702739;
757%! assert (all (abs (x-x_opt) < 5*sqrt (eps)) && abs (obj-obj_opt) < sqrt (eps));