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