5289
|
1 ## Copyright (C) 2005 John W. Eaton |
|
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 -*- |
|
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}) |
|
21 ## Solve the nonlinear program |
6741
|
22 ## @iftex |
|
23 ## @tex |
|
24 ## $$ |
|
25 ## \min_x \phi (x) |
|
26 ## $$ |
|
27 ## @end tex |
|
28 ## @end iftex |
|
29 ## @ifnottex |
5289
|
30 ## |
|
31 ## @example |
|
32 ## min phi (x) |
|
33 ## x |
|
34 ## @end example |
|
35 ## |
6741
|
36 ## @end ifnottex |
|
37 ## subject to |
5289
|
38 ## @iftex |
|
39 ## @tex |
6741
|
40 ## $$ |
|
41 ## g(x) = 0 \qquad h(x) \geq 0 |
|
42 ## $$ |
5289
|
43 ## @end tex |
|
44 ## @end iftex |
6741
|
45 ## @ifnottex |
5289
|
46 ## |
|
47 ## @example |
|
48 ## g(x) = 0 |
|
49 ## h(x) >= 0 |
|
50 ## @end example |
6741
|
51 ## @end ifnottex |
5289
|
52 ## |
|
53 ## @noindent |
|
54 ## using a successive quadratic programming method. |
|
55 ## |
|
56 ## The first argument is the initial guess for the vector @var{x}. |
|
57 ## |
7001
|
58 ## The second argument is a function handle pointing to the objective |
5289
|
59 ## function. The objective function must be of the form |
|
60 ## |
|
61 ## @example |
|
62 ## y = phi (x) |
|
63 ## @end example |
|
64 ## |
|
65 ## @noindent |
|
66 ## in which @var{x} is a vector and @var{y} is a scalar. |
|
67 ## |
|
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 to compute 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. |
|
76 ## |
|
77 ## If supplied, the gradient function must be of the form |
|
78 ## |
|
79 ## @example |
|
80 ## g = gradient (x) |
|
81 ## @end example |
|
82 ## |
|
83 ## @noindent |
|
84 ## in which @var{x} is a vector and @var{g} is a vector. |
|
85 ## |
|
86 ## If supplied, the hessian function must be of the form |
|
87 ## |
|
88 ## @example |
|
89 ## h = hessian (x) |
|
90 ## @end example |
|
91 ## |
|
92 ## @noindent |
|
93 ## in which @var{x} is a vector and @var{h} is a matrix. |
|
94 ## |
|
95 ## The third and fourth arguments are function handles pointing to |
|
96 ## functions that compute the equality constraints and the inequality |
|
97 ## constraints, respectively. |
|
98 ## |
|
99 ## If your problem does not have equality (or inequality) constraints, |
|
100 ## you may pass an empty matrix for @var{cef} (or @var{cif}). |
|
101 ## |
|
102 ## If supplied, the equality and inequality constraint functions must be |
|
103 ## of the form |
|
104 ## |
|
105 ## @example |
|
106 ## r = f (x) |
|
107 ## @end example |
|
108 ## |
|
109 ## @noindent |
|
110 ## in which @var{x} is a vector and @var{r} is a vector. |
|
111 ## |
|
112 ## The third and fourth arguments may also be 2-element cell arrays of |
|
113 ## function handles. The first element should point to the constraint |
|
114 ## function and the second should point to a function that computes the |
|
115 ## gradient of the constraint function: |
|
116 ## |
6741
|
117 ## @iftex |
|
118 ## @tex |
|
119 ## $$ |
|
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 |
|
123 ## $$ |
|
124 ## @end tex |
|
125 ## @end iftex |
|
126 ## @ifnottex |
5289
|
127 ## @example |
|
128 ## [ d f(x) d f(x) d f(x) ] |
|
129 ## transpose ( [ ------ ----- ... ------ ] ) |
|
130 ## [ dx_1 dx_2 dx_N ] |
|
131 ## @end example |
6741
|
132 ## @end ifnottex |
5289
|
133 ## |
|
134 ## Here is an example of calling @code{sqp}: |
|
135 ## |
|
136 ## @example |
|
137 ## function r = g (x) |
|
138 ## r = [ sumsq(x)-10; x(2)*x(3)-5*x(4)*x(5); x(1)^3+x(2)^3+1]; |
|
139 ## endfunction |
|
140 ## |
|
141 ## function obj = phi (x) |
|
142 ## obj = exp(prod(x)) - 0.5*(x(1)^3+x(2)^3+1)^2; |
|
143 ## endfunction |
|
144 ## |
|
145 ## x0 = [-1.8; 1.7; 1.9; -0.8; -0.8]; |
|
146 ## |
|
147 ## [x, obj, info, iter, nf, lambda] = sqp (x0, @@phi, @@g, []) |
|
148 ## |
|
149 ## x = |
|
150 ## |
|
151 ## -1.71714 |
|
152 ## 1.59571 |
|
153 ## 1.82725 |
|
154 ## -0.76364 |
|
155 ## -0.76364 |
|
156 ## |
|
157 ## obj = 0.053950 |
|
158 ## info = 101 |
|
159 ## iter = 8 |
|
160 ## nf = 10 |
|
161 ## lambda = |
|
162 ## |
|
163 ## -0.0401627 |
|
164 ## 0.0379578 |
|
165 ## -0.0052227 |
|
166 ## @end example |
|
167 ## |
|
168 ## The value returned in @var{info} may be one of the following: |
|
169 ## @table @asis |
|
170 ## @item 101 |
|
171 ## The algorithm terminated because the norm of the last step was less |
|
172 ## than @code{tol * norm (x))} (the value of tol is currently fixed at |
|
173 ## @code{sqrt (eps)}---edit @file{sqp.m} to modify this value. |
|
174 ## @item 102 |
|
175 ## The BFGS update failed. |
|
176 ## @item 103 |
|
177 ## The maximum number of iterations was reached (the maximum number of |
|
178 ## allowed iterations is currently fixed at 100---edit @file{sqp.m} to |
|
179 ## increase this value). |
|
180 ## @end table |
5642
|
181 ## @seealso{qp} |
5289
|
182 ## @end deftypefn |
|
183 |
6768
|
184 function [x, obj, info, iter, nf, lambda] = sqp (x, objf, cef, cif, lb, ub, maxiter, tolerance) |
5289
|
185 |
6768
|
186 global __sqp_nfun__; |
5289
|
187 global __sqp_obj_fun__; |
|
188 global __sqp_ce_fun__; |
|
189 global __sqp_ci_fun__; |
6768
|
190 global __sqp_cif__; |
|
191 global __sqp_cifcn__; |
5289
|
192 |
6768
|
193 if (nargin >= 2 && nargin <= 8 && nargin != 5) |
5289
|
194 |
|
195 ## Choose an initial NxN symmetric positive definite Hessan |
|
196 ## approximation B. |
|
197 |
|
198 n = length (x); |
|
199 |
|
200 ## Evaluate objective function, constraints, and gradients at initial |
|
201 ## value of x. |
|
202 ## |
|
203 ## obj_fun |
|
204 ## obj_grad |
|
205 ## ce_fun -- equality constraint functions |
|
206 ## ci_fun -- inequality constraint functions |
|
207 ## A == [grad_{x_1} cx_fun, grad_{x_2} cx_fun, ..., grad_{x_n} cx_fun]^T |
|
208 |
|
209 obj_grd = @fd_obj_grd; |
|
210 have_hess = 0; |
|
211 if (iscell (objf)) |
|
212 if (length (objf) > 0) |
|
213 __sqp_obj_fun__ = obj_fun = objf{1}; |
|
214 if (length (objf) > 1) |
|
215 obj_grd = objf{2}; |
|
216 if (length (objf) > 2) |
|
217 obj_hess = objf{3}; |
|
218 have_hess = 1; |
|
219 endif |
|
220 endif |
|
221 else |
|
222 error ("sqp: invalid objective function"); |
|
223 endif |
|
224 else |
|
225 __sqp_obj_fun__ = obj_fun = objf; |
|
226 endif |
|
227 |
|
228 ce_fun = @empty_cf; |
|
229 ce_grd = @empty_jac; |
|
230 if (nargin > 2) |
|
231 ce_grd = @fd_ce_jac; |
|
232 if (iscell (cef)) |
|
233 if (length (cef) > 0) |
|
234 __sqp_ce_fun__ = ce_fun = cef{1}; |
|
235 if (length (cef) > 1) |
|
236 ce_grd = cef{2}; |
|
237 endif |
|
238 else |
|
239 error ("sqp: invalid equality constraint function"); |
|
240 endif |
|
241 elseif (! isempty (cef)) |
|
242 ce_fun = cef; |
|
243 endif |
|
244 endif |
|
245 __sqp_ce_fun__ = ce_fun; |
|
246 |
|
247 ci_fun = @empty_cf; |
|
248 ci_grd = @empty_jac; |
6768
|
249 |
5289
|
250 if (nargin > 3) |
6768
|
251 ## constraint function given by user with possibly gradient |
|
252 __sqp_cif__ = cif; |
|
253 ## constraint function given by user without gradient |
|
254 __sqp_cifcn__ = @empty_cf; |
|
255 if (iscell (__sqp_cif__)) |
|
256 if (length (__sqp_cif__) > 0) |
|
257 __sqp_cifcn__ = __sqp_cif__{1}; |
|
258 endif |
|
259 elseif (! isempty (__sqp_cif__)) |
|
260 __sqp_cifcn__ = __sqp_cif__; |
|
261 endif |
|
262 |
|
263 if (nargin < 5) |
|
264 ci_grd = @fd_ci_jac; |
|
265 if (iscell (cif)) |
|
266 if (length (cif) > 0) |
|
267 __sqp_ci_fun__ = ci_fun = cif{1}; |
|
268 if (length (cif) > 1) |
|
269 ci_grd = cif{2}; |
|
270 endif |
|
271 else |
|
272 error ("sqp: invalid equality constraint function"); |
5289
|
273 endif |
6768
|
274 elseif (! isempty (cif)) |
|
275 ci_fun = cif; |
|
276 endif |
|
277 else |
|
278 global __sqp_lb__; |
|
279 if (isvector (lb)) |
|
280 __sqp_lb__ = lb; |
|
281 elseif (isempty (lb)) |
|
282 __sqp_lb__ = -realmax; |
5289
|
283 else |
6768
|
284 error ("sqp: invalid lower bound"); |
5289
|
285 endif |
6768
|
286 |
|
287 global __sqp_ub__; |
|
288 if (isvector (ub)) |
|
289 __sqp_ub__ = ub; |
|
290 elseif (isempty (lb)) |
|
291 __sqp_ub__ = realmax; |
|
292 else |
|
293 error ("sqp: invalid upper bound"); |
|
294 endif |
|
295 |
|
296 if (lb > ub) |
|
297 error ("sqp: upper bound smaller than lower bound"); |
|
298 endif |
|
299 __sqp_ci_fun__ = ci_fun = @cf_ub_lb; |
|
300 ci_grd = @cigrad_ub_lb; |
|
301 endif |
|
302 __sqp_ci_fun__ = ci_fun; |
|
303 endif |
|
304 |
|
305 iter_max = 100; |
|
306 if (nargin > 6 && ! isempty (maxiter)) |
6921
|
307 if (isscalar (maxiter) && maxiter > 0 && round (maxiter) == maxiter) |
6768
|
308 iter_max = maxiter; |
|
309 else |
|
310 error ("sqp: invalid number of maximum iterations"); |
5289
|
311 endif |
|
312 endif |
|
313 |
6768
|
314 tol = sqrt (eps); |
|
315 if (nargin > 7 && ! isempty (tolerance)) |
|
316 if (isscalar (tolerance) && tolerance > 0) |
|
317 tol = tolerance; |
|
318 else |
|
319 error ("sqp: invalid value for tolerance"); |
|
320 endif |
|
321 endif |
5289
|
322 |
|
323 iter = 0; |
|
324 |
|
325 obj = feval (obj_fun, x); |
6768
|
326 __sqp_nfun__ = 1; |
5289
|
327 |
|
328 c = feval (obj_grd, x); |
|
329 |
6382
|
330 if (have_hess) |
|
331 B = feval (obj_hess, x); |
|
332 else |
|
333 B = eye (n, n); |
|
334 endif |
|
335 |
5289
|
336 ce = feval (ce_fun, x); |
|
337 F = feval (ce_grd, x); |
|
338 |
|
339 ci = feval (ci_fun, x); |
|
340 C = feval (ci_grd, x); |
|
341 |
|
342 A = [F; C]; |
|
343 |
|
344 ## Choose an initial lambda (x is provided by the caller). |
|
345 |
|
346 lambda = 100 * ones (rows (A), 1); |
|
347 |
|
348 qp_iter = 1; |
|
349 alpha = 1; |
|
350 |
|
351 ## report (); |
|
352 |
6768
|
353 ## report (iter, qp_iter, alpha, __sqp_nfun__, obj); |
5289
|
354 |
6527
|
355 info = 0; |
|
356 |
5289
|
357 while (++iter < iter_max) |
|
358 |
|
359 ## Check convergence. This is just a simple check on the first |
|
360 ## order necessary conditions. |
|
361 |
|
362 ## IDX is the indices of the active inequality constraints. |
|
363 |
|
364 nr_f = rows (F); |
|
365 |
|
366 lambda_e = lambda((1:nr_f)'); |
|
367 lambda_i = lambda((nr_f+1:end)'); |
|
368 |
|
369 con = [ce; ci]; |
|
370 |
|
371 t0 = norm (c - A' * lambda); |
|
372 t1 = norm (ce); |
|
373 t2 = all (ci >= 0); |
|
374 t3 = all (lambda_i >= 0); |
|
375 t4 = norm (lambda .* con); |
|
376 |
|
377 if (t2 && t3 && max ([t0; t1; t4]) < tol) |
|
378 break; |
|
379 endif |
|
380 |
|
381 ## Compute search direction p by solving QP. |
|
382 |
|
383 g = -ce; |
|
384 d = -ci; |
|
385 |
|
386 ## Discard inequality constraints that have -Inf bounds since those |
|
387 ## will never be active. |
|
388 idx = isinf (d) & d < 0; |
|
389 d(idx) = []; |
|
390 C(idx,:) = []; |
|
391 |
|
392 [p, obj_qp, INFO, lambda] = qp (x, B, c, F, g, [], [], d, C, |
|
393 Inf * ones (size (d))); |
|
394 |
|
395 info = INFO.info; |
|
396 |
|
397 ## Check QP solution and attempt to recover if it has failed. |
|
398 |
|
399 ## Choose mu such that p is a descent direction for the chosen |
|
400 ## merit function phi. |
|
401 |
|
402 [x_new, alpha, obj_new] = linesearch_L1 (x, p, obj_fun, obj_grd, |
|
403 ce_fun, ci_fun, lambda, obj); |
|
404 |
|
405 ## Evaluate objective function, constraints, and gradients at |
|
406 ## x_new. |
|
407 |
|
408 c_new = feval (obj_grd, x_new); |
|
409 |
|
410 ce_new = feval (ce_fun, x_new); |
|
411 F_new = feval (ce_grd, x_new); |
|
412 |
|
413 ci_new = feval (ci_fun, x_new); |
|
414 C_new = feval (ci_grd, x_new); |
|
415 |
|
416 A_new = [F_new; C_new]; |
|
417 |
|
418 ## Set |
|
419 ## |
|
420 ## s = alpha * p |
|
421 ## y = grad_x L (x_new, lambda) - grad_x L (x, lambda}) |
|
422 |
|
423 y = c_new - c; |
|
424 |
|
425 if (! isempty (A)) |
|
426 t = ((A_new - A)'*lambda); |
|
427 y -= t; |
|
428 endif |
|
429 |
|
430 delx = x_new - x; |
|
431 |
|
432 if (norm (delx) < tol * norm (x)) |
|
433 info = 101; |
|
434 break; |
|
435 endif |
|
436 |
|
437 if (have_hess) |
|
438 |
|
439 B = feval (obj_hess, x); |
|
440 |
|
441 else |
|
442 |
|
443 ## Update B using a quasi-Newton formula. |
|
444 |
|
445 delxt = delx'; |
|
446 |
|
447 ## Damped BFGS. Or maybe we would actually want to use the Hessian |
|
448 ## of the Lagrangian, computed directly. |
|
449 |
|
450 d1 = delxt*B*delx; |
|
451 |
|
452 t1 = 0.2 * d1; |
|
453 t2 = delxt*y; |
|
454 |
|
455 if (t2 < t1) |
|
456 theta = 0.8*d1/(d1 - t2); |
|
457 else |
|
458 theta = 1; |
|
459 endif |
|
460 |
|
461 r = theta*y + (1-theta)*B*delx; |
|
462 |
|
463 d2 = delxt*r; |
|
464 |
|
465 if (d1 == 0 || d2 == 0) |
|
466 info = 102; |
|
467 break; |
|
468 endif |
|
469 |
|
470 B = B - B*delx*delxt*B/d1 + r*r'/d2; |
|
471 |
|
472 endif |
|
473 |
|
474 x = x_new; |
|
475 |
|
476 obj = obj_new; |
|
477 |
|
478 c = c_new; |
|
479 |
|
480 ce = ce_new; |
|
481 F = F_new; |
|
482 |
|
483 ci = ci_new; |
|
484 C = C_new; |
|
485 |
|
486 A = A_new; |
|
487 |
6768
|
488 ## report (iter, qp_iter, alpha, __sqp_nfun__, obj); |
5289
|
489 |
|
490 endwhile |
|
491 |
|
492 if (iter >= iter_max) |
|
493 info = 103; |
|
494 endif |
|
495 |
6768
|
496 nf = __sqp_nfun__; |
5289
|
497 |
|
498 else |
|
499 |
6046
|
500 print_usage (); |
5289
|
501 |
|
502 endif |
|
503 |
|
504 ### endfunction |
|
505 |
|
506 |
|
507 function [merit, obj] = phi_L1 (obj, obj_fun, ce_fun, ci_fun, x, mu) |
|
508 |
6768
|
509 global __sqp_nfun__; |
5289
|
510 |
|
511 ce = feval (ce_fun, x); |
|
512 ci = feval (ci_fun, x); |
|
513 |
|
514 idx = ci < 0; |
|
515 |
|
516 con = [ce; ci(idx)]; |
|
517 |
|
518 if (isempty (obj)) |
|
519 obj = feval (obj_fun, x); |
6768
|
520 __sqp_nfun__++; |
5289
|
521 endif |
|
522 |
|
523 merit = obj; |
|
524 t = norm (con, 1) / mu; |
|
525 |
|
526 if (! isempty (t)) |
|
527 merit += t; |
|
528 endif |
|
529 |
|
530 ### endfunction |
|
531 |
|
532 |
|
533 function [x_new, alpha, obj] = linesearch_L1 (x, p, obj_fun, obj_grd, |
|
534 ce_fun, ci_fun, lambda, obj) |
|
535 |
|
536 ## Choose parameters |
|
537 ## |
|
538 ## eta in the range (0, 0.5) |
|
539 ## tau in the range (0, 1) |
|
540 |
|
541 eta = 0.25; |
|
542 tau = 0.5; |
|
543 |
|
544 delta_bar = sqrt (eps); |
|
545 |
|
546 if (isempty (lambda)) |
|
547 mu = 1 / delta_bar; |
|
548 else |
|
549 mu = 1 / (norm (lambda, Inf) + delta_bar); |
|
550 endif |
|
551 |
|
552 alpha = 1; |
|
553 |
|
554 c = feval (obj_grd, x); |
|
555 ce = feval (ce_fun, x); |
|
556 |
|
557 [phi_x_mu, obj] = phi_L1 (obj, obj_fun, ce_fun, ci_fun, x, mu); |
|
558 |
|
559 D_phi_x_mu = c' * p; |
|
560 d = feval (ci_fun, x); |
|
561 ## only those elements of d corresponding |
|
562 ## to violated constraints should be included. |
|
563 idx = d < 0; |
|
564 t = - norm ([ce; d(idx)], 1) / mu; |
|
565 if (! isempty (t)) |
|
566 D_phi_x_mu += t; |
|
567 endif |
|
568 |
|
569 while (1) |
|
570 [p1, obj] = phi_L1 ([], obj_fun, ce_fun, ci_fun, x+alpha*p, mu); |
|
571 p2 = phi_x_mu+eta*alpha*D_phi_x_mu; |
|
572 if (p1 > p2) |
|
573 ## Reset alpha = tau_alpha * alpha for some tau_alpha in the |
|
574 ## range (0, tau). |
|
575 tau_alpha = 0.9 * tau; ## ?? |
|
576 alpha = tau_alpha * alpha; |
|
577 else |
|
578 break; |
|
579 endif |
|
580 endwhile |
|
581 |
|
582 ## Set x_new = x + alpha * p; |
|
583 |
|
584 x_new = x + alpha * p; |
|
585 |
|
586 ### endfunction |
|
587 |
|
588 |
|
589 function report (iter, qp_iter, alpha, nfun, obj) |
|
590 |
|
591 if (nargin == 0) |
|
592 printf (" Itn ItQP Step Nfun Objective\n"); |
|
593 else |
|
594 printf ("%5d %4d %8.1g %5d %13.6e\n", iter, qp_iter, alpha, nfun, obj); |
|
595 endif |
|
596 |
|
597 ### endfunction |
|
598 |
|
599 |
|
600 function grd = fdgrd (f, x) |
|
601 |
|
602 if (! isempty (f)) |
|
603 y0 = feval (f, x); |
|
604 nx = length (x); |
|
605 grd = zeros (nx, 1); |
|
606 deltax = sqrt (eps); |
|
607 for i = 1:nx |
|
608 t = x(i); |
|
609 x(i) += deltax; |
|
610 grd(i) = (feval (f, x) - y0) / deltax; |
|
611 x(i) = t; |
|
612 endfor |
|
613 else |
|
614 grd = zeros (0, 1); |
|
615 endif |
|
616 |
|
617 ### endfunction |
|
618 |
|
619 |
|
620 function jac = fdjac (f, x) |
6768
|
621 nx = length (x); |
5289
|
622 if (! isempty (f)) |
|
623 y0 = feval (f, x); |
|
624 nf = length (y0); |
|
625 nx = length (x); |
|
626 jac = zeros (nf, nx); |
|
627 deltax = sqrt (eps); |
|
628 for i = 1:nx |
|
629 t = x(i); |
|
630 x(i) += deltax; |
|
631 jac(:,i) = (feval (f, x) - y0) / deltax; |
|
632 x(i) = t; |
|
633 endfor |
|
634 else |
|
635 jac = zeros (0, nx); |
|
636 endif |
|
637 |
|
638 ### endfunction |
|
639 |
|
640 |
|
641 function grd = fd_obj_grd (x) |
|
642 |
|
643 global __sqp_obj_fun__; |
|
644 |
|
645 grd = fdgrd (__sqp_obj_fun__, x); |
|
646 |
|
647 ### endfunction |
|
648 |
|
649 |
|
650 function res = empty_cf (x) |
|
651 |
|
652 res = zeros (0, 1); |
|
653 |
|
654 ### endfunction |
|
655 |
|
656 |
|
657 function res = empty_jac (x) |
|
658 |
|
659 res = zeros (0, length (x)); |
|
660 |
|
661 ### endfunction |
|
662 |
|
663 |
|
664 function jac = fd_ce_jac (x) |
|
665 |
|
666 global __sqp_ce_fun__; |
|
667 |
|
668 jac = fdjac (__sqp_ce_fun__, x); |
|
669 |
|
670 ### endfunction |
|
671 |
|
672 |
|
673 function jac = fd_ci_jac (x) |
|
674 |
6768
|
675 global __sqp_cifcn__; |
|
676 ## __sqp_cifcn__ = constraint function without gradients and lb or ub |
|
677 jac = fdjac (__sqp_cifcn__, x); |
|
678 |
|
679 ### endfunction |
|
680 |
|
681 function res = cf_ub_lb (x) |
5289
|
682 |
6768
|
683 ## combine constraint function with ub and lb |
|
684 global __sqp_cifcn__ __sqp_lb__ __sqp_ub__ |
|
685 |
|
686 res = [x-__sqp_lb__; __sqp_ub__-x]; |
|
687 |
|
688 if (! isempty (__sqp_cifcn__)) |
|
689 res = [feval(__sqp_cifcn__,x); x-__sqp_lb__; __sqp_ub__-x]; |
|
690 endif |
5289
|
691 |
|
692 ### endfunction |
6768
|
693 |
|
694 function res = cigrad_ub_lb (x) |
|
695 |
|
696 global __sqp_cif__ |
|
697 |
|
698 res = [eye(numel(x)); -eye(numel(x))]; |
|
699 |
|
700 cigradfcn = @fd_ci_jac; |
|
701 |
|
702 if (iscell (__sqp_cif__) && length (__sqp_cif__) > 1) |
|
703 cigradfcn = __sqp_cif__{2}; |
|
704 endif |
|
705 |
|
706 if (! isempty (cigradfcn)) |
|
707 res = [feval(cigradfcn,x); eye(numel(x)); -eye(numel(x))]; |
|
708 endif |
|
709 |
7001
|
710 ### endfunction |