5290
|
1 /* |
|
2 |
|
3 Copyright (C) 2000, 2001, 2004, 2005 Gabriele Pannocchia |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
5290
|
21 |
|
22 */ |
|
23 |
5293
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
5290
|
28 #include <cfloat> |
|
29 |
5293
|
30 #include "dbleCHOL.h" |
|
31 #include "dbleSVD.h" |
|
32 #include "mx-m-dm.h" |
|
33 #include "EIG.h" |
5290
|
34 |
5293
|
35 #include "defun-dld.h" |
|
36 #include "error.h" |
|
37 #include "gripes.h" |
|
38 #include "oct-obj.h" |
|
39 #include "pr-output.h" |
|
40 #include "utils.h" |
5290
|
41 |
|
42 static Matrix |
5295
|
43 null (const Matrix& A, octave_idx_type& rank) |
5290
|
44 { |
|
45 Matrix retval; |
|
46 |
|
47 rank = 0; |
|
48 |
|
49 if (! A.is_empty ()) |
|
50 { |
|
51 SVD A_svd (A); |
|
52 |
|
53 DiagMatrix S = A_svd.singular_values (); |
|
54 |
|
55 ColumnVector s = S.diag (); |
|
56 |
|
57 Matrix V = A_svd.right_singular_matrix (); |
|
58 |
5295
|
59 octave_idx_type A_nr = A.rows (); |
|
60 octave_idx_type A_nc = A.cols (); |
5290
|
61 |
5295
|
62 octave_idx_type tmp = A_nr > A_nc ? A_nr : A_nc; |
5290
|
63 |
|
64 double tol = tmp * s(0) * DBL_EPSILON; |
|
65 |
5295
|
66 octave_idx_type n = s.length (); |
5290
|
67 |
5295
|
68 for (octave_idx_type i = 0; i < n; i++) |
5290
|
69 { |
|
70 if (s(i) > tol) |
|
71 rank++; |
|
72 } |
|
73 |
|
74 if (rank < A_nc) |
|
75 retval = V.extract (0, rank, A_nc-1, A_nc-1); |
|
76 else |
|
77 retval.resize (A_nc, 0); |
6431
|
78 |
|
79 for (octave_idx_type i = 0; i < retval.numel (); i++) |
|
80 if (std::abs (retval(i)) < DBL_EPSILON) |
|
81 retval(i) = 0; |
5290
|
82 } |
|
83 |
|
84 return retval; |
|
85 } |
|
86 |
|
87 static int |
|
88 qp (const Matrix& H, const ColumnVector& q, |
|
89 const Matrix& Aeq, const ColumnVector& beq, |
|
90 const Matrix& Ain, const ColumnVector& bin, |
|
91 int maxit, |
|
92 ColumnVector& x, ColumnVector& lambda, int& iter) |
|
93 { |
|
94 int info = 0; |
|
95 |
|
96 iter = 0; |
|
97 |
|
98 double rtol = sqrt (DBL_EPSILON); |
|
99 |
|
100 // Problem dimension. |
5295
|
101 octave_idx_type n = x.length (); |
5290
|
102 |
|
103 // Dimension of constraints. |
5295
|
104 octave_idx_type n_eq = beq.length (); |
|
105 octave_idx_type n_in = bin.length (); |
5290
|
106 |
|
107 // Filling the current active set. |
|
108 |
5295
|
109 octave_idx_type n_act = n_eq; |
5290
|
110 |
5295
|
111 octave_idx_type n_tot = n_eq + n_in; |
5290
|
112 |
|
113 // Equality constraints come first. We won't check the sign of the |
|
114 // Lagrange multiplier for those. |
|
115 |
|
116 Matrix Aact = Aeq; |
|
117 ColumnVector bact = beq; |
|
118 ColumnVector Wact; |
|
119 |
|
120 if (n_in > 0) |
|
121 { |
|
122 ColumnVector res = Ain*x - bin; |
|
123 |
5295
|
124 for (octave_idx_type i = 0; i < n_in; i++) |
5290
|
125 { |
6431
|
126 res(i) /= (1.0 + std::abs (bin(i))); |
5290
|
127 |
|
128 if (res(i) < rtol) |
|
129 { |
|
130 n_act++; |
|
131 Aact = Aact.stack (Ain.row (i)); |
|
132 bact.resize (n_act, bin(i)); |
6848
|
133 Wact.resize (n_act-n_eq, i); |
5290
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 // Computing the ??? |
|
139 |
|
140 EIG eigH (H); |
|
141 ColumnVector eigenvalH = real (eigH.eigenvalues ()); |
|
142 Matrix eigenvecH = real (eigH.eigenvectors ()); |
|
143 double minReal = eigenvalH.min (); |
5295
|
144 octave_idx_type indminR = 0; |
|
145 for (octave_idx_type i = 0; i < n; i++) |
5290
|
146 { |
|
147 if (minReal == eigenvalH(i)) |
|
148 { |
|
149 indminR = i; |
|
150 break; |
|
151 } |
|
152 } |
|
153 |
|
154 bool done = false; |
|
155 |
|
156 double alpha = 0.0; |
|
157 |
|
158 Matrix R; |
|
159 Matrix Y (n, 0, 0.0); |
|
160 |
|
161 ColumnVector g (n, 0.0); |
|
162 ColumnVector p (n, 0.0); |
|
163 |
|
164 ColumnVector lambda_tmp (n_in, 0.0); |
|
165 |
|
166 while (! done) |
|
167 { |
|
168 iter++; |
|
169 |
|
170 // Current Gradient |
|
171 // g = q + H * x; |
|
172 |
|
173 g = q + H * x; |
|
174 |
|
175 if (n_act == 0) |
|
176 { |
|
177 // There are no active constraints. |
|
178 |
|
179 if (minReal > 0.0) |
|
180 { |
|
181 // Inverting the Hessian. Using the Cholesky |
|
182 // factorization since the Hessian is positive |
|
183 // definite. |
|
184 |
|
185 CHOL cholH (H); |
|
186 |
|
187 R = cholH.chol_matrix (); |
|
188 |
5341
|
189 Matrix Hinv = chol2inv (R); |
5290
|
190 |
|
191 // Computing the unconstrained step. |
|
192 // p = -Hinv * g; |
|
193 |
|
194 p = -Hinv * g; |
|
195 |
|
196 info = 0; |
|
197 } |
|
198 else |
|
199 { |
|
200 // Finding the negative curvature of H. |
|
201 |
|
202 p = eigenvecH.column (indminR); |
|
203 |
|
204 // Following the negative curvature of H. |
|
205 |
|
206 if (p.transpose () * g > DBL_EPSILON) |
|
207 p = -p; |
|
208 |
|
209 info = 1; |
|
210 } |
|
211 |
|
212 // Multipliers are zero. |
|
213 lambda_tmp.fill (0.0); |
|
214 } |
|
215 else |
|
216 { |
|
217 // There are active constraints. |
|
218 |
|
219 // Computing the null space. |
|
220 |
5295
|
221 octave_idx_type rank; |
5290
|
222 |
|
223 Matrix Z = null (Aact, rank); |
|
224 |
5295
|
225 octave_idx_type dimZ = n - rank; |
5290
|
226 |
5775
|
227 // FIXME -- still remain to handle the case of |
5290
|
228 // non-full rank active set matrix. |
|
229 |
|
230 // Reduced Hessian |
|
231 Matrix Zt = Z.transpose (); |
|
232 Matrix rH = Zt * H * Z; |
|
233 |
5295
|
234 octave_idx_type pR = 0; |
5290
|
235 |
|
236 if (dimZ > 0) |
|
237 { |
|
238 // Computing the Cholesky factorization (pR = 0 means |
|
239 // that the reduced Hessian was positive definite). |
|
240 |
|
241 CHOL cholrH (rH, pR); |
|
242 Matrix tR = cholrH.chol_matrix (); |
|
243 if (pR == 0) |
|
244 R = tR; |
|
245 } |
|
246 |
|
247 if (pR == 0) |
|
248 { |
|
249 info = 0; |
|
250 |
|
251 // Computing the step pz. |
|
252 if (dimZ > 0) |
|
253 { |
|
254 // Using the Cholesky factorization to invert rH |
|
255 |
5341
|
256 Matrix rHinv = chol2inv (R); |
5290
|
257 |
|
258 ColumnVector pz = -rHinv * Zt * g; |
|
259 |
|
260 // Global step. |
|
261 p = Z * pz; |
|
262 } |
|
263 else |
|
264 { |
|
265 // Global step. |
|
266 p.fill (0.0); |
|
267 } |
|
268 } |
|
269 else |
|
270 { |
|
271 info = 1; |
|
272 |
|
273 // Searching for the most negative curvature. |
|
274 |
|
275 EIG eigrH (rH); |
|
276 ColumnVector eigenvalrH = real (eigrH.eigenvalues ()); |
|
277 Matrix eigenvecrH = real (eigrH.eigenvectors ()); |
|
278 double mRrH = eigenvalrH.min (); |
|
279 indminR = 0; |
5295
|
280 for (octave_idx_type i = 0; i < n; i++) |
5290
|
281 { |
|
282 if (mRrH == eigenvalH(i)) |
|
283 { |
|
284 indminR = i; |
|
285 break; |
|
286 } |
|
287 } |
|
288 |
|
289 ColumnVector eVrH = eigenvecrH.column (indminR); |
|
290 |
|
291 // Computing the step pz. |
|
292 p = Z * eVrH; |
|
293 |
|
294 if (p.transpose () * g > DBL_EPSILON) |
|
295 p = -p; |
|
296 } |
|
297 } |
|
298 |
|
299 // Checking the step-size. |
|
300 ColumnVector abs_p (n); |
5295
|
301 for (octave_idx_type i = 0; i < n; i++) |
6431
|
302 abs_p(i) = std::abs (p(i)); |
5290
|
303 double max_p = abs_p.max (); |
|
304 |
|
305 if (max_p < rtol) |
|
306 { |
|
307 // The step is null. Checking constraints. |
|
308 if (n_act - n_eq == 0) |
|
309 // Solution is found because no inequality |
|
310 // constraints are active. |
|
311 done = true; |
|
312 else |
|
313 { |
|
314 // Computing the multipliers only for the inequality |
|
315 // constraints that are active. We do NOT compute |
|
316 // multipliers for the equality constraints. |
6860
|
317 Y = Aact.pseudo_inverse (); |
5290
|
318 Matrix Yt = Y.transpose (); |
|
319 Yt = Yt.extract_n (n_eq, 0, n_act-n_eq, n); |
|
320 lambda_tmp = Yt * (g + H * p); |
|
321 |
|
322 // Checking the multipliers. We remove the most |
|
323 // negative from the set (if any). |
|
324 double min_lambda = lambda_tmp.min (); |
|
325 if (min_lambda >= 0) |
|
326 { |
|
327 // Solution is found. |
|
328 done = true; |
|
329 } |
|
330 else |
|
331 { |
5295
|
332 octave_idx_type which_eig = 0; |
|
333 for (octave_idx_type i = 0; i < n_act; i++) |
5290
|
334 { |
|
335 if (lambda_tmp(i) == min_lambda) |
|
336 { |
|
337 which_eig = i; |
|
338 break; |
|
339 } |
|
340 } |
|
341 |
|
342 // At least one multiplier is negative, we |
|
343 // remove it from the set. |
|
344 |
5295
|
345 for (octave_idx_type i = which_eig; i < n_act - n_eq; i++) |
5290
|
346 { |
|
347 Wact(i) = Wact(i+1); |
5295
|
348 for (octave_idx_type j = 0; j < n; j++) |
5290
|
349 Aact(n_eq+i,j) = Aact(n_eq+i+1,j); |
|
350 bact(n_eq+i) = bact(n_eq+i+1); |
|
351 } |
|
352 n_act--; |
|
353 |
|
354 // Resizing the active set. |
|
355 Wact.resize (n_act-n_eq); |
|
356 bact.resize (n_act); |
|
357 Aact.resize (n_act, n); |
|
358 } |
|
359 } |
|
360 } |
|
361 else |
|
362 { |
|
363 // The step is not null. |
|
364 if (n_act - n_eq == n_in) |
|
365 { |
|
366 // All inequality constraints were active. We can |
|
367 // add the whole step. |
|
368 x += p; |
|
369 } |
|
370 else |
|
371 { |
|
372 // Some constraints were not active. Checking if |
|
373 // there is a blocking constraint. |
|
374 alpha = 1.0; |
5295
|
375 octave_idx_type is_block = -1; |
5290
|
376 |
5295
|
377 for (octave_idx_type i = 0; i < n_in; i++) |
5290
|
378 { |
|
379 bool found = false; |
|
380 |
5295
|
381 for (octave_idx_type j = 0; j < n_act-n_eq; j++) |
5290
|
382 { |
6860
|
383 if (Wact(j) == i - n_eq) |
5290
|
384 { |
|
385 found = true; |
|
386 break; |
|
387 } |
|
388 } |
|
389 |
|
390 if (! found) |
|
391 { |
|
392 // The i-th constraint was not in the set. Is it a |
|
393 // blocking constraint? |
|
394 |
|
395 RowVector tmp_row = Ain.row (i); |
|
396 double tmp = tmp_row * p; |
|
397 double res = tmp_row * x; |
|
398 |
|
399 if (tmp < 0.0) |
|
400 { |
|
401 double alpha_tmp = (bin(i) - res) / tmp; |
|
402 |
|
403 if (alpha_tmp < alpha) |
|
404 { |
|
405 alpha = alpha_tmp; |
|
406 is_block = i; |
|
407 } |
|
408 } |
|
409 } |
|
410 } |
|
411 |
|
412 // In is_block there is the index of the blocking |
|
413 // constraint (if any). |
|
414 if (is_block >= 0) |
|
415 { |
|
416 // There is a blocking constraint (index in |
|
417 // is_block) which is added to the active set. |
|
418 n_act++; |
|
419 Aact = Aact.stack (Ain.row (is_block)); |
|
420 bact.resize (n_act, bin(is_block)); |
6848
|
421 Wact.resize (n_act-n_eq, is_block); |
5290
|
422 |
|
423 // Adding the reduced step |
|
424 x += alpha * p; |
|
425 } |
|
426 else |
|
427 { |
|
428 // There are no blocking constraints. Adding the |
|
429 // whole step. |
|
430 x += alpha * p; |
|
431 } |
|
432 } |
|
433 } |
|
434 |
|
435 if (iter == maxit) |
|
436 { |
|
437 done = true; |
|
438 // warning ("qp_main: maximum number of iteration reached"); |
|
439 info = 3; |
|
440 } |
|
441 } |
|
442 |
|
443 lambda_tmp = Y.transpose () * (g + H * p); |
|
444 |
|
445 // Reordering the Lagrange multipliers. |
|
446 |
|
447 lambda.resize (n_tot); |
|
448 lambda.fill (0.0); |
5295
|
449 for (octave_idx_type i = 0; i < n_eq; i++) |
5290
|
450 lambda(i) = lambda_tmp(i); |
|
451 |
5295
|
452 for (octave_idx_type i = n_eq; i < n_tot; i++) |
5290
|
453 { |
5295
|
454 for (octave_idx_type j = 0; j < n_act-n_eq; j++) |
5290
|
455 { |
|
456 if (Wact(j) == i) |
|
457 { |
|
458 lambda(i) = lambda_tmp(n_eq+j); |
|
459 break; |
|
460 } |
|
461 } |
|
462 } |
|
463 |
|
464 return info; |
|
465 } |
|
466 |
|
467 DEFUN_DLD (__qp__, args, , |
6945
|
468 "-*- texinfo -*-\n\ |
|
469 @deftypefn {Loadable Function} {[@var{x}, @var{lambda}, @var{info}, @var{iter}] =} __qp__ (@var{x0}, @var{H}, @var{q}, @var{Aeq}, @var{beq}, @var{Ain}, @var{bin}, @var{maxit})\n\ |
|
470 Undocumented internal function.\n\ |
|
471 @end deftypefn") |
5290
|
472 { |
|
473 octave_value_list retval; |
|
474 |
|
475 if (args.length () == 8) |
|
476 { |
|
477 const ColumnVector x0 (args(0) . vector_value ()); |
|
478 const Matrix H (args(1) . matrix_value ()); |
|
479 const ColumnVector q (args(2) . vector_value ()); |
|
480 const Matrix Aeq (args(3) . matrix_value ()); |
|
481 const ColumnVector beq (args(4) . vector_value ()); |
|
482 const Matrix Ain (args(5) . matrix_value ()); |
|
483 const ColumnVector bin (args(6) . vector_value ()); |
|
484 const int maxit (args(7) . int_value ()); |
|
485 |
|
486 if (! error_state) |
|
487 { |
|
488 int iter = 0; |
|
489 |
|
490 // Copying the initial guess in the working variable |
|
491 ColumnVector x = x0; |
|
492 |
|
493 // Reordering the Lagrange multipliers |
|
494 ColumnVector lambda; |
|
495 |
|
496 int info = qp (H, q, Aeq, beq, Ain, bin, maxit, x, lambda, iter); |
|
497 |
|
498 retval(3) = iter; |
|
499 retval(2) = info; |
|
500 retval(1) = lambda; |
|
501 retval(0) = x; |
|
502 } |
|
503 else |
|
504 error ("__qp__: invalid arguments"); |
|
505 } |
|
506 else |
5823
|
507 print_usage (); |
5290
|
508 |
|
509 return retval; |
|
510 } |