1 ## Copyright (C) 2005-2012 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{x0}, @var{phi})
21 ## @deftypefnx {Function File} {[@dots{}] =} sqp (@var{x0}, @var{phi}, @var{g})
22 ## @deftypefnx {Function File} {[@dots{}] =} sqp (@var{x0}, @var{phi}, @var{g}, @var{h})
23 ## @deftypefnx {Function File} {[@dots{}] =} sqp (@var{x0}, @var{phi}, @var{g}, @var{h}, @var{lb}, @var{ub})
24 ## @deftypefnx {Function File} {[@dots{}] =} sqp (@var{x0}, @var{phi}, @var{g}, @var{h}, @var{lb}, @var{ub}, @var{maxiter})
25 ## @deftypefnx {Function File} {[@dots{}] =} sqp (@var{x0}, @var{phi}, @var{g}, @var{h}, @var{lb}, @var{ub}, @var{maxiter}, @var{tol})
26 ## Solve the nonlinear program
45 ## g(x) = 0 \qquad h(x) \geq 0 \qquad lb \leq x \leq ub
60 ## using a sequential quadratic programming method.
62 ## The first argument is the initial guess for the vector @var{x0}.
64 ## The second argument is a function handle pointing to the objective
65 ## function @var{phi}. The objective function must accept one vector
66 ## argument and return a scalar.
68 ## The second argument may also be a 2- or 3-element cell array of
69 ## function handles. The first element should point to the objective
70 ## function, the second should point to a function that computes the
71 ## gradient of the objective function, and the third should point to a
72 ## function that computes the Hessian of the objective function. If the
73 ## gradient function is not supplied, the gradient is computed by finite
74 ## differences. If the Hessian function is not supplied, a BFGS update
75 ## formula is used to approximate the Hessian.
77 ## When supplied, the gradient function @code{@var{phi}@{2@}} must accept
78 ## one vector argument and return a vector. When supplied, the Hessian
79 ## function @code{@var{phi}@{3@}} must accept one vector argument and
82 ## The third and fourth arguments @var{g} and @var{h} are function
83 ## handles pointing to functions that compute the equality constraints
84 ## and the inequality constraints, respectively. If the problem does
85 ## not have equality (or inequality) constraints, then use an empty
86 ## matrix ([]) for @var{g} (or @var{h}). When supplied, these equality
87 ## and inequality constraint functions must accept one vector argument
88 ## and return a vector.
90 ## The third and fourth arguments may also be 2-element cell arrays of
91 ## function handles. The first element should point to the constraint
92 ## function and the second should point to a function that computes the
93 ## gradient of the constraint function:
96 ## \Bigg( {\partial f(x) \over \partial x_1},
97 ## {\partial f(x) \over \partial x_2}, \ldots,
98 ## {\partial f(x) \over \partial x_N} \Bigg)^T
105 ## [ d f(x) d f(x) d f(x) ]
106 ## transpose ( [ ------ ----- ... ------ ] )
107 ## [ dx_1 dx_2 dx_N ]
112 ## The fifth and sixth arguments, @var{lb} and @var{ub}, contain lower
113 ## and upper bounds on @var{x}. These must be consistent with the
114 ## equality and inequality constraints @var{g} and @var{h}. If the
115 ## arguments are vectors then @var{x}(i) is bound by @var{lb}(i) and
116 ## @var{ub}(i). A bound can also be a scalar in which case all elements
117 ## of @var{x} will share the same bound. If only one bound (lb, ub) is
118 ## specified then the other will default to (-@var{realmax},
121 ## The seventh argument @var{maxiter} specifies the maximum number of
122 ## iterations. The default value is 100.
124 ## The eighth argument @var{tol} specifies the tolerance for the
125 ## stopping criteria. The default value is @code{sqrt(eps)}.
127 ## The value returned in @var{info} may be one of the following:
131 ## The algorithm terminated normally.
132 ## Either all constraints meet the requested tolerance, or the stepsize,
139 ## is less than @code{@var{tol} * norm (x)}.
142 ## The BFGS update failed.
145 ## The maximum number of iterations was reached.
148 ## An example of calling @code{sqp}:
151 ## function r = g (x)
152 ## r = [ sumsq(x)-10;
153 ## x(2)*x(3)-5*x(4)*x(5);
154 ## x(1)^3+x(2)^3+1 ];
157 ## function obj = phi (x)
158 ## obj = exp (prod (x)) - 0.5*(x(1)^3+x(2)^3+1)^2;
161 ## x0 = [-1.8; 1.7; 1.9; -0.8; -0.8];
163 ## [x, obj, info, iter, nf, lambda] = sqp (x0, @@phi, @@g, [])
187 function [x, obj, info, iter, nf, lambda] = sqp (x0, objf, cef, cif, lb, ub, maxiter, tolerance)
190 global __sqp_obj_fun__;
191 global __sqp_ce_fun__;
192 global __sqp_ci_fun__;
194 global __sqp_cifcn__;
196 if (nargin < 2 || nargin > 8 || nargin == 5)
201 error ("sqp: X0 must be a vector");
207 obj_grd = @fd_obj_grd;
210 switch (numel (objf))
222 error ("sqp: invalid objective function specification");
225 obj_fun = objf; # No cell array, only obj_fun set
227 __sqp_obj_fun__ = obj_fun;
241 error ("sqp: invalid equality constraint function specification");
243 elseif (! isempty (cef))
244 ce_fun = cef; # No cell array, only constraint equality function set
247 __sqp_ce_fun__ = ce_fun;
252 ## constraint function given by user with possible gradient
254 ## constraint function given by user without gradient
255 __sqp_cifcn__ = @empty_cf;
257 if (length (cif) > 0)
258 __sqp_cifcn__ = cif{1};
260 elseif (! isempty (cif))
264 if (nargin < 5 || (nargin > 5 && isempty (lb) && isempty (ub)))
265 ## constraint inequality function only without any bounds
275 error ("sqp: invalid inequality constraint function specification");
277 elseif (! isempty (cif))
278 ci_fun = cif; # No cell array, only constraint inequality function set
281 ## constraint inequality function with bounds present
283 lb_idx = ub_idx = true (size (x0));
284 ub_grad = - (lb_grad = eye (rows (x0)));
286 __sqp_lb__ = tmp_lb = lb(:);
287 lb_idx(:) = tmp_idx = (lb != -Inf);
288 __sqp_lb__ = __sqp_lb__(tmp_idx, 1);
289 lb_grad = lb_grad(lb_idx, :);
290 elseif (isempty (lb))
291 if (isa (x0, "single"))
292 __sqp_lb__ = tmp_lb = -realmax ("single");
294 __sqp_lb__ = tmp_lb = -realmax;
297 error ("sqp: invalid lower bound");
302 __sqp_ub__ = tmp_ub = ub(:);
303 ub_idx(:) = tmp_idx = (ub != Inf);
304 __sqp_ub__ = __sqp_ub__(tmp_idx, 1);
305 ub_grad = ub_grad(ub_idx, :);
306 elseif (isempty (ub))
307 if (isa (x0, "single"))
308 __sqp_ub__ = tmp_ub = realmax ("single");
310 __sqp_ub__ = tmp_ub = realmax;
313 error ("sqp: invalid upper bound");
316 if (any (tmp_lb > tmp_ub))
317 error ("sqp: upper bound smaller than lower bound");
319 bounds_grad = [lb_grad; ub_grad];
320 ci_fun = @ (x) cf_ub_lb (x, lb_idx, ub_idx);
321 ci_grd = @ (x) cigrad_ub_lb (x, bounds_grad);
324 __sqp_ci_fun__ = ci_fun;
325 endif # if (nargin > 3)
328 if (nargin > 6 && ! isempty (maxiter))
329 if (isscalar (maxiter) && maxiter > 0 && fix (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");
345 ## Initialize variables for search loop
346 ## Seed x with initial guess and evaluate objective function, constraints,
347 ## and gradients at initial value x0.
349 ## obj_fun -- objective function
350 ## obj_grad -- objective gradient
351 ## ce_fun -- equality constraint functions
352 ## ci_fun -- inequality constraint functions
353 ## A == [grad_{x_1} cx_fun, grad_{x_2} cx_fun, ..., grad_{x_n} cx_fun]^T
356 obj = feval (obj_fun, x0);
359 c = feval (obj_grd, x0);
361 ## Choose an initial NxN symmetric positive definite Hessian approximation B.
364 B = feval (obj_hess, x0);
369 ce = feval (ce_fun, x0);
370 F = feval (ce_grd, x0);
372 ci = feval (ci_fun, x0);
373 C = feval (ci_grd, x0);
377 ## Choose an initial lambda (x is provided by the caller).
378 lambda = 100 * ones (rows (A), 1);
385 # report (); # Called with no arguments to initialize reporting
386 # report (iter, qp_iter, alpha, __sqp_nfun__, obj);
388 while (++iter < iter_max)
390 ## Check convergence. This is just a simple check on the first
391 ## order necessary conditions.
394 lambda_e = lambda((1:nr_f)');
395 lambda_i = lambda((nr_f+1:end)');
399 t0 = norm (c - A' * lambda);
402 t3 = all (lambda_i >= 0);
403 t4 = norm (lambda .* con);
405 if (t2 && t3 && max ([t0; t1; t4]) < tol)
410 ## Compute search direction p by solving QP.
414 [p, obj_qp, INFO, lambda] = qp (x, B, c, F, g, [], [], d, C,
419 ## FIXME -- check QP solution and attempt to recover if it has
420 ## failed. For now, just warn about possible problems.
422 id = "Octave:SQP-QP-subproblem";
425 warning (id, "sqp: QP subproblem is non-convex and unbounded");
427 warning (id, "sqp: QP subproblem failed to converge in %d iterations",
430 warning (id, "sqp: QP subproblem is infeasible");
433 ## Choose mu such that p is a descent direction for the chosen
434 ## merit function phi.
435 [x_new, alpha, obj_new] = linesearch_L1 (x, p, obj_fun, obj_grd,
436 ce_fun, ci_fun, lambda, obj);
438 ## Evaluate objective function, constraints, and gradients at x_new.
439 c_new = feval (obj_grd, x_new);
441 ce_new = feval (ce_fun, x_new);
442 F_new = feval (ce_grd, x_new);
444 ci_new = feval (ci_fun, x_new);
445 C_new = feval (ci_grd, x_new);
447 A_new = [F_new; C_new];
452 ## y = grad_x L (x_new, lambda) - grad_x L (x, lambda})
457 t = ((A_new - A)'*lambda);
463 if (norm (delx) < tol * norm (x))
470 B = feval (obj_hess, x);
473 ## Update B using a quasi-Newton formula.
476 ## Damped BFGS. Or maybe we would actually want to use the Hessian
477 ## of the Lagrangian, computed directly?
484 theta = 0.8*d1/(d1 - t2);
489 r = theta*y + (1-theta)*B*delx;
493 if (d1 == 0 || d2 == 0)
498 B = B - B*delx*delxt*B/d1 + r*r'/d2;
516 # report (iter, qp_iter, alpha, __sqp_nfun__, obj);
520 if (iter >= iter_max)
529 function [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;
555 function [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 x_new = x + alpha * p;
609 function grd = fdgrd (f, x)
619 grd(i) = (feval (f, x) - y0) / deltax;
629 function jac = fdjac (f, x)
636 jac = zeros (nf, nx);
641 jac(:,i) = (feval (f, x) - y0) / deltax;
651 function grd = fd_obj_grd (x)
653 global __sqp_obj_fun__;
655 grd = fdgrd (__sqp_obj_fun__, x);
660 function res = empty_cf (x)
667 function res = empty_jac (x)
669 res = zeros (0, length (x));
674 function jac = fd_ce_jac (x)
676 global __sqp_ce_fun__;
678 jac = fdjac (__sqp_ce_fun__, x);
683 function jac = fd_ci_jac (x)
685 global __sqp_cifcn__;
686 ## __sqp_cifcn__ = constraint function without gradients and lb or ub
687 jac = fdjac (__sqp_cifcn__, x);
692 function res = cf_ub_lb (x, lbidx, ubidx)
694 ## combine constraint function with ub and lb
695 global __sqp_cifcn__ __sqp_lb__ __sqp_ub__
697 if (isempty (__sqp_cifcn__))
698 res = [x(lbidx,1)-__sqp_lb__; __sqp_ub__-x(ubidx,1)];
700 res = [feval(__sqp_cifcn__,x); \
701 x(lbidx,1)-__sqp_lb__; __sqp_ub__-x(ubidx,1)];
707 function res = cigrad_ub_lb (x, bgrad)
711 cigradfcn = @fd_ci_jac;
713 if (iscell (__sqp_cif__) && length (__sqp_cif__) > 1)
714 cigradfcn = __sqp_cif__{2};
717 if (isempty (cigradfcn))
720 res = [feval(cigradfcn,x); bgrad];
725 # Utility function used to debug sqp
726 function report (iter, qp_iter, alpha, nfun, obj)
729 printf (" Itn ItQP Step Nfun Objective\n");
731 printf ("%5d %4d %8.1g %5d %13.6e\n", iter, qp_iter, alpha, nfun, obj);
736 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
739 %!function r = __g (x)
741 %! x(2)*x(3)-5*x(4)*x(5);
742 %! x(1)^3+x(2)^3+1 ];
745 %!function obj = __phi (x)
746 %! obj = exp(prod(x)) - 0.5*(x(1)^3+x(2)^3+1)^2;
751 %! x0 = [-1.8; 1.7; 1.9; -0.8; -0.8];
753 %! [x, obj, info, iter, nf, lambda] = sqp (x0, @__phi, @__g, []);
755 %! x_opt = [-1.717143501952599;
756 %! 1.595709610928535;
757 %! 1.827245880097156;
758 %! -0.763643103133572;
759 %! -0.763643068453300];
761 %! obj_opt = 0.0539498477702739;
763 %! assert (all (abs (x-x_opt) < 5*sqrt (eps)) && abs (obj-obj_opt) < sqrt (eps));
765 %% Test input validation
768 %!error sqp (1,2,3,4,5,6,7,8,9)
769 %!error sqp (1,2,3,4,5)
770 %!error sqp (ones(2,2))
771 %!error sqp (1,cell(4,1))
772 %!error sqp (1,cell(3,1),cell(3,1))
773 %!error sqp (1,cell(3,1),cell(2,1),cell(3,1))
774 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),ones(2,2),[])
775 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],ones(2,2))
776 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),1,-1)
777 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],[],ones(2,2))
778 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],[],-1)
779 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],[],1.5)
780 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],[],[],ones(2,2))
781 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],[],[],-1)