Mercurial > hg > octave-lyh
annotate scripts/optimization/sqp.m @ 11290:646063a22a35
sqp.m: Use correct stopping tolerance in documentation.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 22 Nov 2010 19:07:48 -0800 |
parents | a44f979a35ce |
children | 2726132f77f6 |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2005, 2006, 2007, 2008, 2009 John W. Eaton |
5289 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5289 | 9 ## |
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. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5289 | 18 |
19 ## -*- texinfo -*- | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {[@var{x}, @var{obj}, @var{info}, @var{iter}, @var{nf}, @var{lambda}] =} sqp (@var{x}, @var{phi}) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefnx {Function File} {[@dots{}] =} sqp (@var{x}, @var{phi}, @var{g}) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
22 ## @deftypefnx {Function File} {[@dots{}] =} sqp (@var{x}, @var{phi}, @var{g}, @var{h}) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
23 ## @deftypefnx {Function File} {[@dots{}] =} sqp (@var{x}, @var{phi}, @var{g}, @var{h}, @var{lb}, @var{ub}) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
24 ## @deftypefnx {Function File} {[@dots{}] =} sqp (@var{x}, @var{phi}, @var{g}, @var{h}, @var{lb}, @var{ub}, @var{maxiter}) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
25 ## @deftypefnx {Function File} {[@dots{}] =} sqp (@var{x}, @var{phi}, @var{g}, @var{h}, @var{lb}, @var{ub}, @var{maxiter}, @var{tolerance}) |
5289 | 26 ## Solve the nonlinear program |
6741 | 27 ## @tex |
28 ## $$ | |
29 ## \min_x \phi (x) | |
30 ## $$ | |
31 ## @end tex | |
32 ## @ifnottex | |
5289 | 33 ## |
34 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
35 ## @group |
5289 | 36 ## min phi (x) |
37 ## x | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
38 ## @end group |
5289 | 39 ## @end example |
40 ## | |
6741 | 41 ## @end ifnottex |
42 ## subject to | |
5289 | 43 ## @tex |
6741 | 44 ## $$ |
8167
17352ccd860e
describe additional arguments in sqp() documentation string
Ivan Sutoris <ivan.sutoris@gmail.com>
parents:
8047
diff
changeset
|
45 ## g(x) = 0 \qquad h(x) \geq 0 \qquad lb \leq x \leq ub |
6741 | 46 ## $$ |
5289 | 47 ## @end tex |
6741 | 48 ## @ifnottex |
5289 | 49 ## |
50 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
51 ## @group |
5289 | 52 ## g(x) = 0 |
53 ## h(x) >= 0 | |
8167
17352ccd860e
describe additional arguments in sqp() documentation string
Ivan Sutoris <ivan.sutoris@gmail.com>
parents:
8047
diff
changeset
|
54 ## lb <= x <= ub |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
55 ## @end group |
5289 | 56 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10678
diff
changeset
|
57 ## |
6741 | 58 ## @end ifnottex |
5289 | 59 ## @noindent |
60 ## using a successive quadratic programming method. | |
61 ## | |
62 ## The first argument is the initial guess for the vector @var{x}. | |
63 ## | |
7001 | 64 ## The second argument is a function handle pointing to the objective |
5289 | 65 ## function. The objective function must be of the form |
66 ## | |
67 ## @example | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
68 ## @var{y} = phi (@var{x}) |
5289 | 69 ## @end example |
70 ## | |
71 ## @noindent | |
72 ## in which @var{x} is a vector and @var{y} is a scalar. | |
73 ## | |
74 ## The second argument may also be a 2- or 3-element cell array of | |
75 ## function handles. The first element should point to the objective | |
76 ## function, the second should point to a function that computes the | |
77 ## gradient of the objective function, and the third should point to a | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
78 ## function that computes the Hessian of the objective function. If the |
5289 | 79 ## gradient function is not supplied, the gradient is computed by finite |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
80 ## differences. If the Hessian function is not supplied, a BFGS update |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
81 ## formula is used to approximate the Hessian. |
5289 | 82 ## |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
83 ## When supplied, the gradient function must be of the form |
5289 | 84 ## |
85 ## @example | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
86 ## @var{g} = gradient (@var{x}) |
5289 | 87 ## @end example |
88 ## | |
89 ## @noindent | |
90 ## in which @var{x} is a vector and @var{g} is a vector. | |
91 ## | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
92 ## When supplied, the Hessian function must be of the form |
5289 | 93 ## |
94 ## @example | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
95 ## @var{h} = hessian (@var{x}) |
5289 | 96 ## @end example |
97 ## | |
98 ## @noindent | |
99 ## in which @var{x} is a vector and @var{h} is a matrix. | |
100 ## | |
101 ## The third and fourth arguments are function handles pointing to | |
102 ## functions that compute the equality constraints and the inequality | |
103 ## constraints, respectively. | |
104 ## | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
105 ## If the problem does not have equality (or inequality) constraints, |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
106 ## then use an empty matrix ([]) for @var{cef} (or @var{cif}). |
5289 | 107 ## |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
108 ## When supplied, the equality and inequality constraint functions must be |
5289 | 109 ## of the form |
110 ## | |
111 ## @example | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
112 ## @var{r} = f (@var{x}) |
5289 | 113 ## @end example |
114 ## | |
115 ## @noindent | |
116 ## in which @var{x} is a vector and @var{r} is a vector. | |
117 ## | |
118 ## The third and fourth arguments may also be 2-element cell arrays of | |
119 ## function handles. The first element should point to the constraint | |
120 ## function and the second should point to a function that computes the | |
121 ## gradient of the constraint function: | |
6741 | 122 ## @tex |
123 ## $$ | |
124 ## \Bigg( {\partial f(x) \over \partial x_1}, | |
125 ## {\partial f(x) \over \partial x_2}, \ldots, | |
126 ## {\partial f(x) \over \partial x_N} \Bigg)^T | |
127 ## $$ | |
128 ## @end tex | |
129 ## @ifnottex | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10678
diff
changeset
|
130 ## |
5289 | 131 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
132 ## @group |
5289 | 133 ## [ d f(x) d f(x) d f(x) ] |
134 ## transpose ( [ ------ ----- ... ------ ] ) | |
135 ## [ dx_1 dx_2 dx_N ] | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
136 ## @end group |
5289 | 137 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10678
diff
changeset
|
138 ## |
6741 | 139 ## @end ifnottex |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
140 ## The fifth and sixth arguments contain lower and upper bounds |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
141 ## on @var{x}. These must be consistent with the equality and inequality |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
142 ## constraints @var{g} and @var{h}. If the arguments are vectors then |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
143 ## @var{x}(i) is bound by @var{lb}(i) and @var{ub}(i). A bound can also |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
144 ## be a scalar in which case all elements of @var{x} will share the same |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
145 ## bound. If only one bound (lb, ub) is specified then the other will |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
146 ## default to (-@var{realmax}, +@var{realmax}). |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
147 ## |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
148 ## The seventh argument specifies the maximum number of iterations. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
149 ## The default value is 100. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
150 ## |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
151 ## The eighth argument specifies the tolerance for the stopping criteria. |
11290
646063a22a35
sqp.m: Use correct stopping tolerance in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11120
diff
changeset
|
152 ## The default value is @code{sqrt(eps)}. |
8167
17352ccd860e
describe additional arguments in sqp() documentation string
Ivan Sutoris <ivan.sutoris@gmail.com>
parents:
8047
diff
changeset
|
153 ## |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
154 ## The value returned in @var{info} may be one of the following: |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10678
diff
changeset
|
155 ## |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
156 ## @table @asis |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
157 ## @item 101 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
158 ## The algorithm terminated normally. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
159 ## Either all constraints meet the requested tolerance, or the stepsize, |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
160 ## @tex |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
161 ## $\Delta x,$ |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
162 ## @end tex |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
163 ## @ifnottex |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
164 ## delta @var{x}, |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
165 ## @end ifnottex |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
166 ## is less than @code{tol * norm (x)}. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10678
diff
changeset
|
167 ## |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
168 ## @item 102 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
169 ## The BFGS update failed. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10678
diff
changeset
|
170 ## |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
171 ## @item 103 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
172 ## The maximum number of iterations was reached. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
173 ## @end table |
8167
17352ccd860e
describe additional arguments in sqp() documentation string
Ivan Sutoris <ivan.sutoris@gmail.com>
parents:
8047
diff
changeset
|
174 ## |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
175 ## An example of calling @code{sqp}: |
5289 | 176 ## |
177 ## @example | |
7031 | 178 ## function r = g (x) |
179 ## r = [ sumsq(x)-10; | |
180 ## x(2)*x(3)-5*x(4)*x(5); | |
181 ## x(1)^3+x(2)^3+1 ]; | |
182 ## endfunction | |
183 ## | |
184 ## function obj = phi (x) | |
185 ## obj = exp(prod(x)) - 0.5*(x(1)^3+x(2)^3+1)^2; | |
186 ## endfunction | |
5289 | 187 ## |
7031 | 188 ## x0 = [-1.8; 1.7; 1.9; -0.8; -0.8]; |
189 ## | |
190 ## [x, obj, info, iter, nf, lambda] = sqp (x0, @@phi, @@g, []) | |
5289 | 191 ## |
7031 | 192 ## x = |
193 ## | |
194 ## -1.71714 | |
195 ## 1.59571 | |
196 ## 1.82725 | |
197 ## -0.76364 | |
198 ## -0.76364 | |
5289 | 199 ## |
7031 | 200 ## obj = 0.053950 |
201 ## info = 101 | |
202 ## iter = 8 | |
203 ## nf = 10 | |
204 ## lambda = | |
205 ## | |
206 ## -0.0401627 | |
207 ## 0.0379578 | |
208 ## -0.0052227 | |
5289 | 209 ## @end example |
210 ## | |
5642 | 211 ## @seealso{qp} |
5289 | 212 ## @end deftypefn |
213 | |
6768 | 214 function [x, obj, info, iter, nf, lambda] = sqp (x, objf, cef, cif, lb, ub, maxiter, tolerance) |
5289 | 215 |
6768 | 216 global __sqp_nfun__; |
5289 | 217 global __sqp_obj_fun__; |
218 global __sqp_ce_fun__; | |
219 global __sqp_ci_fun__; | |
6768 | 220 global __sqp_cif__; |
221 global __sqp_cifcn__; | |
5289 | 222 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
223 if (nargin < 2 || nargin > 8 || nargin == 5) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
224 print_usage (); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
225 endif |
5289 | 226 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
227 if (!isvector (x)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
228 error ("sqp: X must be a vector"); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
229 endif |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
230 if (rows (x) == 1) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
231 x = x'; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
232 endif |
5289 | 233 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
234 obj_grd = @fd_obj_grd; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
235 have_hess = 0; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
236 if (iscell (objf)) |
11120
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
237 switch (numel (objf)) |
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
238 case 1 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
239 obj_fun = objf{1}; |
11120
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
240 case 2 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
241 obj_fun = objf{1}; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
242 obj_grd = objf{2}; |
11120
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
243 case 3 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
244 obj_fun = objf{1}; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
245 obj_grd = objf{2}; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
246 obj_hess = objf{3}; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
247 have_hess = 1; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
248 otherwise |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
249 error ("sqp: invalid objective function specification"); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
250 endswitch |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
251 else |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
252 obj_fun = objf; # No cell array, only obj_fun set |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
253 endif |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
254 __sqp_obj_fun__ = obj_fun; |
5289 | 255 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
256 ce_fun = @empty_cf; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
257 ce_grd = @empty_jac; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
258 if (nargin > 2) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
259 ce_grd = @fd_ce_jac; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
260 if (iscell (cef)) |
11120
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
261 switch (numel (cef)) |
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
262 case 1 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
263 ce_fun = cef{1}; |
11120
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
264 case 2 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
265 ce_fun = cef{1}; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
266 ce_grd = cef{2}; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
267 otherwise |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
268 error ("sqp: invalid equality constraint function specification"); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
269 endswitch |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
270 elseif (! isempty (cef)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
271 ce_fun = cef; # No cell array, only constraint equality function set |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
272 endif |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
273 endif |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
274 __sqp_ce_fun__ = ce_fun; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
275 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
276 ci_fun = @empty_cf; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
277 ci_grd = @empty_jac; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
278 if (nargin > 3) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
279 ## constraint function given by user with possible gradient |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
280 __sqp_cif__ = cif; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
281 ## constraint function given by user without gradient |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
282 __sqp_cifcn__ = @empty_cf; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
283 if (iscell (cif)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
284 if (length (cif) > 0) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
285 __sqp_cifcn__ = cif{1}; |
5289 | 286 endif |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
287 elseif (! isempty (cif)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
288 __sqp_cifcn__ = cif; |
5289 | 289 endif |
290 | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
291 if (nargin < 5 || (nargin > 5 && isempty (lb) && isempty (ub))) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
292 ## constraint inequality function only without any bounds |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
293 ci_grd = @fd_ci_jac; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
294 if (iscell (cif)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
295 switch length (cif) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
296 case {1} |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
297 ci_fun = cif{1}; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
298 case {2} |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
299 ci_fun = cif{1}; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
300 ci_grd = cif{2}; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
301 otherwise |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
302 error ("sqp: invalid inequality constraint function specification"); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
303 endswitch |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
304 elseif (! isempty (cif)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
305 ci_fun = cif; # No cell array, only constraint inequality function set |
5289 | 306 endif |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
307 else |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
308 ## constraint inequality function with bounds present |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
309 global __sqp_lb__; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
310 if (isvector (lb)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
311 __sqp_lb__ = lb(:); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
312 elseif (isempty (lb)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
313 if (isa (x, "single")) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
314 __sqp_lb__ = -realmax ("single"); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
315 else |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
316 __sqp_lb__ = -realmax; |
10549 | 317 endif |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
318 else |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
319 error ("sqp: invalid lower bound"); |
6768 | 320 endif |
321 | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
322 global __sqp_ub__; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
323 if (isvector (ub)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
324 __sqp_ub__ = ub(:); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
325 elseif (isempty (ub)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
326 if (isa (x, "single")) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
327 __sqp_ub__ = realmax ("single"); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
328 else |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
329 __sqp_ub__ = realmax; |
10549 | 330 endif |
6768 | 331 else |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
332 error ("sqp: invalid upper bound"); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
333 endif |
6768 | 334 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
335 if (__sqp_lb__ > __sqp_ub__) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
336 error ("sqp: upper bound smaller than lower bound"); |
6768 | 337 endif |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
338 ci_fun = @cf_ub_lb; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
339 ci_grd = @cigrad_ub_lb; |
6768 | 340 endif |
5289 | 341 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
342 __sqp_ci_fun__ = ci_fun; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
343 endif # if (nargin > 3) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
344 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
345 iter_max = 100; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
346 if (nargin > 6 && ! isempty (maxiter)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
347 if (isscalar (maxiter) && maxiter > 0 && fix (maxiter) == maxiter) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
348 iter_max = maxiter; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
349 else |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
350 error ("sqp: invalid number of maximum iterations"); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
351 endif |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
352 endif |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
353 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
354 tol = sqrt (eps); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
355 if (nargin > 7 && ! isempty (tolerance)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
356 if (isscalar (tolerance) && tolerance > 0) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
357 tol = tolerance; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
358 else |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
359 error ("sqp: invalid value for tolerance"); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
360 endif |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
361 endif |
5289 | 362 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
363 ## Evaluate objective function, constraints, and gradients at initial |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
364 ## value of x. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
365 ## |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
366 ## obj_fun -- objective function |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
367 ## obj_grad -- objective gradient |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
368 ## ce_fun -- equality constraint functions |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
369 ## ci_fun -- inequality constraint functions |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
370 ## A == [grad_{x_1} cx_fun, grad_{x_2} cx_fun, ..., grad_{x_n} cx_fun]^T |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
371 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
372 obj = feval (obj_fun, x); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
373 __sqp_nfun__ = 1; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
374 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
375 c = feval (obj_grd, x); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
376 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
377 ## Choose an initial NxN symmetric positive definite Hessan |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
378 ## approximation B. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
379 n = length (x); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
380 if (have_hess) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
381 B = feval (obj_hess, x); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
382 else |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
383 B = eye (n, n); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
384 endif |
5289 | 385 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
386 ce = feval (ce_fun, x); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
387 F = feval (ce_grd, x); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
388 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
389 ci = feval (ci_fun, x); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
390 C = feval (ci_grd, x); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
391 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
392 A = [F; C]; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
393 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
394 ## Choose an initial lambda (x is provided by the caller). |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
395 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
396 lambda = 100 * ones (rows (A), 1); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
397 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
398 qp_iter = 1; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
399 alpha = 1; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
400 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
401 # report (); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
402 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
403 # report (iter, qp_iter, alpha, __sqp_nfun__, obj); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
404 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
405 info = 0; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
406 iter = 0; |
5289 | 407 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
408 while (++iter < iter_max) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
409 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
410 ## Check convergence. This is just a simple check on the first |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
411 ## order necessary conditions. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
412 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
413 ## idx is the indices of the active inequality constraints. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
414 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
415 nr_f = rows (F); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
416 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
417 lambda_e = lambda((1:nr_f)'); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
418 lambda_i = lambda((nr_f+1:end)'); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
419 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
420 con = [ce; ci]; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
421 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
422 t0 = norm (c - A' * lambda); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
423 t1 = norm (ce); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
424 t2 = all (ci >= 0); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
425 t3 = all (lambda_i >= 0); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
426 t4 = norm (lambda .* con); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
427 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
428 if (t2 && t3 && max ([t0; t1; t4]) < tol) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
429 info = 101; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
430 break; |
6382 | 431 endif |
432 | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
433 ## Compute search direction p by solving QP. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
434 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
435 g = -ce; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
436 d = -ci; |
5289 | 437 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
438 ## Discard inequality constraints that have -Inf bounds since those |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
439 ## will never be active. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
440 idx = isinf (d) & d < 0; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
441 d(idx) = []; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
442 C(idx,:) = []; |
5289 | 443 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
444 [p, obj_qp, INFO, lambda] = qp (x, B, c, F, g, [], [], d, C, |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
445 Inf (size (d))); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
446 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
447 info = INFO.info; |
5289 | 448 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
449 ## Check QP solution and attempt to recover if it has failed. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
450 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
451 ## Choose mu such that p is a descent direction for the chosen |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
452 ## merit function phi. |
5289 | 453 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
454 [x_new, alpha, obj_new] = linesearch_L1 (x, p, obj_fun, obj_grd, |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
455 ce_fun, ci_fun, lambda, obj); |
5289 | 456 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
457 ## Evaluate objective function, constraints, and gradients at |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
458 ## x_new. |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
459 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
460 c_new = feval (obj_grd, x_new); |
5289 | 461 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
462 ce_new = feval (ce_fun, x_new); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
463 F_new = feval (ce_grd, x_new); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
464 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
465 ci_new = feval (ci_fun, x_new); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
466 C_new = feval (ci_grd, x_new); |
5289 | 467 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
468 A_new = [F_new; C_new]; |
5289 | 469 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
470 ## Set |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
471 ## |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
472 ## s = alpha * p |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
473 ## y = grad_x L (x_new, lambda) - grad_x L (x, lambda}) |
6527 | 474 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
475 y = c_new - c; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
476 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
477 if (! isempty (A)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
478 t = ((A_new - A)'*lambda); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
479 y -= t; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
480 endif |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
481 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
482 delx = x_new - x; |
5289 | 483 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
484 if (norm (delx) < tol * norm (x)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
485 info = 101; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
486 break; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
487 endif |
5289 | 488 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
489 if (have_hess) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
490 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
491 B = feval (obj_hess, x); |
5289 | 492 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
493 else |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
494 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
495 ## Update B using a quasi-Newton formula. |
5289 | 496 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
497 delxt = delx'; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
498 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
499 ## Damped BFGS. Or maybe we would actually want to use the Hessian |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
500 ## of the Lagrangian, computed directly. |
5289 | 501 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
502 d1 = delxt*B*delx; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
503 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
504 t1 = 0.2 * d1; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
505 t2 = delxt*y; |
5289 | 506 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
507 if (t2 < t1) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
508 theta = 0.8*d1/(d1 - t2); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
509 else |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
510 theta = 1; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
511 endif |
5289 | 512 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
513 r = theta*y + (1-theta)*B*delx; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
514 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
515 d2 = delxt*r; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
516 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
517 if (d1 == 0 || d2 == 0) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
518 info = 102; |
10549 | 519 break; |
5289 | 520 endif |
521 | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
522 B = B - B*delx*delxt*B/d1 + r*r'/d2; |
5289 | 523 |
524 endif | |
525 | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
526 x = x_new; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
527 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
528 obj = obj_new; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
529 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
530 c = c_new; |
5289 | 531 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
532 ce = ce_new; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
533 F = F_new; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
534 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
535 ci = ci_new; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
536 C = C_new; |
5289 | 537 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
538 A = A_new; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
539 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
540 # report (iter, qp_iter, alpha, __sqp_nfun__, obj); |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
541 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
542 endwhile |
5289 | 543 |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
544 if (iter >= iter_max) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
545 info = 103; |
5289 | 546 endif |
547 | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
548 nf = __sqp_nfun__; |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
549 |
7399 | 550 endfunction |
5289 | 551 |
552 | |
553 function [merit, obj] = phi_L1 (obj, obj_fun, ce_fun, ci_fun, x, mu) | |
554 | |
6768 | 555 global __sqp_nfun__; |
5289 | 556 |
557 ce = feval (ce_fun, x); | |
558 ci = feval (ci_fun, x); | |
559 | |
560 idx = ci < 0; | |
561 | |
562 con = [ce; ci(idx)]; | |
563 | |
564 if (isempty (obj)) | |
565 obj = feval (obj_fun, x); | |
6768 | 566 __sqp_nfun__++; |
5289 | 567 endif |
568 | |
569 merit = obj; | |
570 t = norm (con, 1) / mu; | |
571 | |
572 if (! isempty (t)) | |
573 merit += t; | |
574 endif | |
575 | |
7399 | 576 endfunction |
5289 | 577 |
578 | |
579 function [x_new, alpha, obj] = linesearch_L1 (x, p, obj_fun, obj_grd, | |
10549 | 580 ce_fun, ci_fun, lambda, obj) |
5289 | 581 |
582 ## Choose parameters | |
583 ## | |
584 ## eta in the range (0, 0.5) | |
585 ## tau in the range (0, 1) | |
586 | |
587 eta = 0.25; | |
588 tau = 0.5; | |
589 | |
590 delta_bar = sqrt (eps); | |
591 | |
592 if (isempty (lambda)) | |
593 mu = 1 / delta_bar; | |
594 else | |
595 mu = 1 / (norm (lambda, Inf) + delta_bar); | |
596 endif | |
597 | |
598 alpha = 1; | |
599 | |
600 c = feval (obj_grd, x); | |
601 ce = feval (ce_fun, x); | |
602 | |
603 [phi_x_mu, obj] = phi_L1 (obj, obj_fun, ce_fun, ci_fun, x, mu); | |
604 | |
605 D_phi_x_mu = c' * p; | |
606 d = feval (ci_fun, x); | |
607 ## only those elements of d corresponding | |
608 ## to violated constraints should be included. | |
609 idx = d < 0; | |
610 t = - norm ([ce; d(idx)], 1) / mu; | |
611 if (! isempty (t)) | |
612 D_phi_x_mu += t; | |
613 endif | |
614 | |
615 while (1) | |
616 [p1, obj] = phi_L1 ([], obj_fun, ce_fun, ci_fun, x+alpha*p, mu); | |
617 p2 = phi_x_mu+eta*alpha*D_phi_x_mu; | |
618 if (p1 > p2) | |
619 ## Reset alpha = tau_alpha * alpha for some tau_alpha in the | |
620 ## range (0, tau). | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
621 tau_alpha = 0.9 * tau; # ?? |
5289 | 622 alpha = tau_alpha * alpha; |
623 else | |
624 break; | |
625 endif | |
626 endwhile | |
627 | |
628 x_new = x + alpha * p; | |
629 | |
7399 | 630 endfunction |
5289 | 631 |
632 | |
633 function report (iter, qp_iter, alpha, nfun, obj) | |
634 | |
635 if (nargin == 0) | |
636 printf (" Itn ItQP Step Nfun Objective\n"); | |
637 else | |
638 printf ("%5d %4d %8.1g %5d %13.6e\n", iter, qp_iter, alpha, nfun, obj); | |
639 endif | |
640 | |
7399 | 641 endfunction |
5289 | 642 |
643 | |
644 function grd = fdgrd (f, x) | |
645 | |
646 if (! isempty (f)) | |
647 y0 = feval (f, x); | |
648 nx = length (x); | |
649 grd = zeros (nx, 1); | |
650 deltax = sqrt (eps); | |
651 for i = 1:nx | |
652 t = x(i); | |
653 x(i) += deltax; | |
654 grd(i) = (feval (f, x) - y0) / deltax; | |
655 x(i) = t; | |
656 endfor | |
657 else | |
658 grd = zeros (0, 1); | |
659 endif | |
660 | |
7399 | 661 endfunction |
5289 | 662 |
663 | |
664 function jac = fdjac (f, x) | |
6768 | 665 nx = length (x); |
5289 | 666 if (! isempty (f)) |
667 y0 = feval (f, x); | |
668 nf = length (y0); | |
669 nx = length (x); | |
670 jac = zeros (nf, nx); | |
671 deltax = sqrt (eps); | |
672 for i = 1:nx | |
673 t = x(i); | |
674 x(i) += deltax; | |
675 jac(:,i) = (feval (f, x) - y0) / deltax; | |
676 x(i) = t; | |
677 endfor | |
678 else | |
679 jac = zeros (0, nx); | |
680 endif | |
681 | |
7399 | 682 endfunction |
5289 | 683 |
684 | |
685 function grd = fd_obj_grd (x) | |
686 | |
687 global __sqp_obj_fun__; | |
688 | |
689 grd = fdgrd (__sqp_obj_fun__, x); | |
690 | |
7399 | 691 endfunction |
5289 | 692 |
693 | |
694 function res = empty_cf (x) | |
695 | |
696 res = zeros (0, 1); | |
697 | |
7399 | 698 endfunction |
5289 | 699 |
700 | |
701 function res = empty_jac (x) | |
702 | |
703 res = zeros (0, length (x)); | |
704 | |
7399 | 705 endfunction |
5289 | 706 |
707 | |
708 function jac = fd_ce_jac (x) | |
709 | |
710 global __sqp_ce_fun__; | |
711 | |
712 jac = fdjac (__sqp_ce_fun__, x); | |
713 | |
7399 | 714 endfunction |
5289 | 715 |
716 | |
717 function jac = fd_ci_jac (x) | |
718 | |
6768 | 719 global __sqp_cifcn__; |
720 ## __sqp_cifcn__ = constraint function without gradients and lb or ub | |
721 jac = fdjac (__sqp_cifcn__, x); | |
722 | |
7399 | 723 endfunction |
6768 | 724 |
7017 | 725 |
6768 | 726 function res = cf_ub_lb (x) |
5289 | 727 |
6768 | 728 ## combine constraint function with ub and lb |
729 global __sqp_cifcn__ __sqp_lb__ __sqp_ub__ | |
730 | |
731 res = [x-__sqp_lb__; __sqp_ub__-x]; | |
732 | |
733 if (! isempty (__sqp_cifcn__)) | |
734 res = [feval(__sqp_cifcn__,x); x-__sqp_lb__; __sqp_ub__-x]; | |
735 endif | |
5289 | 736 |
7399 | 737 endfunction |
6768 | 738 |
7017 | 739 |
6768 | 740 function res = cigrad_ub_lb (x) |
741 | |
742 global __sqp_cif__ | |
743 | |
744 res = [eye(numel(x)); -eye(numel(x))]; | |
745 | |
746 cigradfcn = @fd_ci_jac; | |
747 | |
748 if (iscell (__sqp_cif__) && length (__sqp_cif__) > 1) | |
749 cigradfcn = __sqp_cif__{2}; | |
750 endif | |
10549 | 751 |
6768 | 752 if (! isempty (cigradfcn)) |
753 res = [feval(cigradfcn,x); eye(numel(x)); -eye(numel(x))]; | |
754 endif | |
755 | |
7399 | 756 endfunction |
7361 | 757 |
7371 | 758 %!function r = g (x) |
759 %! r = [sumsq(x)-10; | |
760 %! x(2)*x(3)-5*x(4)*x(5); | |
761 %! x(1)^3+x(2)^3+1 ]; | |
7361 | 762 %! |
7371 | 763 %!function obj = phi (x) |
764 %! obj = exp(prod(x)) - 0.5*(x(1)^3+x(2)^3+1)^2; | |
7361 | 765 %! |
766 %!test | |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
767 %! |
7361 | 768 %! x0 = [-1.8; 1.7; 1.9; -0.8; -0.8]; |
769 %! | |
7381 | 770 %! [x, obj, info, iter, nf, lambda] = sqp (x0, @phi, @g, []); |
7361 | 771 %! |
772 %! x_opt = [-1.717143501952599; | |
773 %! 1.595709610928535; | |
774 %! 1.827245880097156; | |
775 %! -0.763643103133572; | |
776 %! -0.763643068453300]; | |
777 %! | |
7371 | 778 %! obj_opt = 0.0539498477702739; |
7361 | 779 %! |
8047
d54f113aa983
Increase test script tolerances.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
7795
diff
changeset
|
780 %! assert (all (abs (x-x_opt) < 5*sqrt (eps)) && abs (obj-obj_opt) < sqrt (eps)); |
10678
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
781 |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
782 %% Test input validation |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
783 %!error sqp () |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
784 %!error sqp (1) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
785 %!error sqp (1,2,3,4,5,6,7,8,9) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
786 %!error sqp (1,2,3,4,5) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
787 %!error sqp (ones(2,2)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
788 %!error sqp (1,cell(4,1)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
789 %!error sqp (1,cell(3,1),cell(3,1)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
790 %!error sqp (1,cell(3,1),cell(2,1),cell(3,1)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
791 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),ones(2,2),[]) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
792 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],ones(2,2)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
793 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),1,-1) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
794 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],[],ones(2,2)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
795 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],[],-1) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
796 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],[],1.5) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
797 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],[],[],ones(2,2)) |
35338deff753
Guarantee equivalent results if sqp called with or wihout bounds
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
798 %!error sqp (1,cell(3,1),cell(2,1),cell(2,1),[],[],[],-1) |