Mercurial > hg > octave-nkf
annotate scripts/optimization/qp.m @ 10060:8f51a90eb8d1
implement default opts query and register opts for qp
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 05 Jan 2010 07:28:42 +0100 |
parents | 665ad34efeed |
children | 17ce2a700a97 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007, 2008, |
2 ## 2009 Gabriele Pannocchia. | |
5289 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5289 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5289 | 19 |
20 ## -*- texinfo -*- | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
21 ## @deftypefn {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q}) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
23 ## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q}, @var{A}, @var{b}) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
24 ## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q}, @var{A}, @var{b}, @var{lb}, @var{ub}) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
25 ## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q}, @var{A}, @var{b}, @var{lb}, @var{ub}, @var{A_lb}, @var{A_in}, @var{A_ub}) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
26 ## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@dots{}, @var{options}) |
5289 | 27 ## Solve the quadratic program |
6741 | 28 ## @tex |
29 ## $$ | |
30 ## \min_x {1 \over 2} x^T H x + x^T q | |
31 ## $$ | |
32 ## @end tex | |
33 ## @ifnottex | |
5289 | 34 ## |
35 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
36 ## @group |
5289 | 37 ## min 0.5 x'*H*x + x'*q |
38 ## x | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
39 ## @end group |
5289 | 40 ## @end example |
41 ## | |
6741 | 42 ## @end ifnottex |
43 ## subject to | |
5289 | 44 ## @tex |
6741 | 45 ## $$ |
46 ## Ax = b \qquad lb \leq x \leq ub \qquad A_{lb} \leq A_{in} \leq A_{ub} | |
47 ## $$ | |
5289 | 48 ## @end tex |
6741 | 49 ## @ifnottex |
5289 | 50 ## |
51 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
52 ## @group |
5289 | 53 ## A*x = b |
54 ## lb <= x <= ub | |
6741 | 55 ## A_lb <= A_in*x <= A_ub |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
56 ## @end group |
5289 | 57 ## @end example |
6741 | 58 ## @end ifnottex |
5289 | 59 ## |
60 ## @noindent | |
61 ## using a null-space active-set method. | |
62 ## | |
63 ## Any bound (@var{A}, @var{b}, @var{lb}, @var{ub}, @var{A_lb}, | |
64 ## @var{A_ub}) may be set to the empty matrix (@code{[]}) if not | |
65 ## present. If the initial guess is feasible the algorithm is faster. | |
66 ## | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
67 ## @table @var |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
68 ## @item options |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
69 ## An optional structure containing the following |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
70 ## parameter(s) used to define the behavior of the solver. Missing elements |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
71 ## in the structure take on default values, so you only need to set the |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
72 ## elements that you wish to change from the default. |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
73 ## |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
74 ## @table @code |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
75 ## @item MaxIter (default: 200) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
76 ## Maximum number of iterations. |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
77 ## @end table |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
78 ## @end table |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
79 ## |
5289 | 80 ## The value @var{info} is a structure with the following fields: |
81 ## @table @code | |
82 ## @item solveiter | |
83 ## The number of iterations required to find the solution. | |
84 ## @item info | |
85 ## An integer indicating the status of the solution, as follows: | |
86 ## @table @asis | |
87 ## @item 0 | |
88 ## The problem is feasible and convex. Global solution found. | |
89 ## @item 1 | |
90 ## The problem is not convex. Local solution found. | |
91 ## @item 2 | |
92 ## The problem is not convex and unbounded. | |
93 ## @item 3 | |
94 ## Maximum number of iterations reached. | |
95 ## @item 6 | |
96 ## The problem is infeasible. | |
97 ## @end table | |
98 ## @end table | |
99 ## @end deftypefn | |
100 | |
10060
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
101 ## PKG_ADD: __all_opts__ ("qp"); |
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
102 |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
103 function [x, obj, INFO, lambda] = qp (x0, H, varargin) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
104 |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
105 nargs = nargin; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
106 |
10060
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
107 if (nargin == 1 && ischar (x0) && strcmp (x0, 'defaults')) |
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
108 x = optimset ("MaxIter", 200); |
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
109 return; |
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
110 endif |
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
111 |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
112 if (nargs > 2 && isstruct (varargin{end})) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
113 options = varargin{end}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
114 nargs--; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
115 else |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
116 options = struct (); |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
117 endif |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
118 |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
119 if (nargs >= 3) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
120 q = varargin{1}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
121 else |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
122 q = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
123 endif |
5289 | 124 |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
125 if (nargs >= 5) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
126 A = varargin{2}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
127 b = varargin{3}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
128 else |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
129 A = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
130 b = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
131 endif |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
132 |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
133 if (nargs >= 7) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
134 lb = varargin{4}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
135 ub = varargin{5}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
136 else |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
137 lb = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
138 ub = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
139 endif |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
140 |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
141 if (nargs == 10) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
142 A_lb = varargin{6}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
143 A_in = varargin{7}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
144 A_ub = varargin{8}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
145 else |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
146 A_lb = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
147 A_in = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
148 A_ub = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
149 endif |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
150 |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
151 if (nargs == 2 || nargs == 3 || nargs == 5 || nargs == 7 || nargs == 10) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
152 |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
153 maxit = optimget (options, "MaxIter", 200) |
5289 | 154 |
155 ## Checking the quadratic penalty | |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
156 if (! issquare (H)) |
5289 | 157 error ("qp: quadratic penalty matrix not square"); |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
158 elseif (! ishermitian (H)) |
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
159 ## warning ("qp: quadratic penalty matrix not hermitian"); |
5289 | 160 H = (H + H')/2; |
161 endif | |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
162 n = rows (H); |
5289 | 163 |
164 ## Checking the initial guess (if empty it is resized to the | |
165 ## right dimension and filled with 0) | |
166 if (isempty (x0)) | |
167 x0 = zeros (n, 1); | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
168 elseif (numel (x0) != n) |
5289 | 169 error ("qp: the initial guess has incorrect length"); |
170 endif | |
171 | |
172 ## Linear penalty. | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
173 if (isempty (q)) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
174 q = zeros (n, 1); |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
175 elseif (numel (q) != n) |
5289 | 176 error ("qp: the linear term has incorrect length"); |
177 endif | |
178 | |
179 ## Equality constraint matrices | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
180 if (isempty (A) || isempty (b)) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
181 A = zeros (0, n); |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
182 b = zeros (0, 1); |
5289 | 183 n_eq = 0; |
184 else | |
185 [n_eq, n1] = size (A); | |
186 if (n1 != n) | |
187 error ("qp: equality constraint matrix has incorrect column dimension"); | |
188 endif | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
189 if (numel (b) != n_eq) |
5289 | 190 error ("qp: equality constraint matrix and vector have inconsistent dimension"); |
191 endif | |
192 endif | |
193 | |
194 ## Bound constraints | |
195 Ain = zeros (0, n); | |
196 bin = zeros (0, 1); | |
197 n_in = 0; | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
198 if (nargs > 5) |
5289 | 199 if (! isempty (lb)) |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
200 if (numel (lb) != n) |
5289 | 201 error ("qp: lower bound has incorrect length"); |
8280
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
202 elseif (isempty (ub)) |
5289 | 203 Ain = [Ain; eye(n)]; |
204 bin = [bin; lb]; | |
205 endif | |
206 endif | |
207 | |
208 if (! isempty (ub)) | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
209 if (numel (ub) != n) |
5289 | 210 error ("qp: upper bound has incorrect length"); |
8280
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
211 elseif (isempty (lb)) |
5289 | 212 Ain = [Ain; -eye(n)]; |
213 bin = [bin; -ub]; | |
214 endif | |
215 endif | |
8280
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
216 |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
217 if (! isempty (lb) && ! isempty (ub)) |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
218 rtol = sqrt (eps); |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
219 for i = 1:n |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
220 if (abs(lb (i) - ub(i)) < rtol*(1 + max (abs (lb(i) + ub(i))))) |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
221 ## These are actually an equality constraint |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
222 tmprow = zeros(1,n); |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
223 tmprow(i) = 1; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
224 A = [A;tmprow]; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
225 b = [b; 0.5*(lb(i) + ub(i))]; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
226 n_eq = n_eq + 1; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
227 else |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
228 tmprow = zeros(1,n); |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
229 tmprow(i) = 1; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
230 Ain = [Ain; tmprow; -tmprow]; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
231 bin = [bin; lb(i); -ub(i)]; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
232 n_in = n_in + 2; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
233 endif |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
234 endfor |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
235 endif |
5289 | 236 endif |
237 | |
238 ## Inequality constraints | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
239 if (nargs > 7) |
5289 | 240 [dimA_in, n1] = size (A_in); |
241 if (n1 != n) | |
242 error ("qp: inequality constraint matrix has incorrect column dimension"); | |
243 else | |
244 if (! isempty (A_lb)) | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
245 if (numel (A_lb) != dimA_in) |
5289 | 246 error ("qp: inequality constraint matrix and lower bound vector inconsistent"); |
8280
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
247 elseif (isempty (A_ub)) |
5289 | 248 Ain = [Ain; A_in]; |
249 bin = [bin; A_lb]; | |
250 endif | |
251 endif | |
252 if (! isempty (A_ub)) | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
253 if (numel (A_ub) != dimA_in) |
5289 | 254 error ("qp: inequality constraint matrix and upper bound vector inconsistent"); |
8280
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
255 elseif (isempty (A_lb)) |
5289 | 256 Ain = [Ain; -A_in]; |
257 bin = [bin; -A_ub]; | |
258 endif | |
259 endif | |
8280
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
260 |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
261 if (! isempty (A_lb) && ! isempty (A_ub)) |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
262 rtol = sqrt (eps); |
8507 | 263 for i = 1:dimA_in |
8280
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
264 if (abs (A_lb(i) - A_ub(i)) < rtol*(1 + max (abs (A_lb(i) + A_ub(i))))) |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
265 ## These are actually an equality constraint |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
266 tmprow = A_in(i,:); |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
267 A = [A;tmprow]; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
268 b = [b; 0.5*(A_lb(i) + A_ub(i))]; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
269 n_eq = n_eq + 1; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
270 else |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
271 tmprow = A_in(i,:); |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
272 Ain = [Ain; tmprow; -tmprow]; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
273 bin = [bin; A_lb(i); -A_ub(i)]; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
274 n_in = n_in + 2; |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
275 endif |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
276 endfor |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
277 endif |
5289 | 278 endif |
279 endif | |
280 | |
281 ## Now we should have the following QP: | |
282 ## | |
283 ## min_x 0.5*x'*H*x + x'*q | |
284 ## s.t. A*x = b | |
285 ## Ain*x >= bin | |
286 | |
287 ## Discard inequality constraints that have -Inf bounds since those | |
288 ## will never be active. | |
289 idx = isinf (bin) & bin < 0; | |
6523 | 290 |
6526 | 291 bin(idx) = []; |
292 Ain(idx,:) = []; | |
5289 | 293 |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
294 n_in = numel (bin); |
5310 | 295 |
5289 | 296 ## Check if the initial guess is feasible. |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
297 if (isa (x0, "single") || isa (H, "single") || isa (q, "single") || isa (A, "single") |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
298 || isa (b, "single")) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
299 rtol = sqrt (eps ("single")); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
300 else |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
301 rtol = sqrt (eps); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
302 endif |
5289 | 303 |
8280
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
304 eq_infeasible = (n_eq > 0 && norm (A*x0-b) > rtol*(1+abs (b))); |
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
305 in_infeasible = (n_in > 0 && any (Ain*x0-bin < -rtol*(1+abs (bin)))); |
5289 | 306 |
307 info = 0; | |
308 if (eq_infeasible || in_infeasible) | |
309 ## The initial guess is not feasible. | |
310 ## First define xbar that is feasible with respect to the equality | |
311 ## constraints. | |
312 if (eq_infeasible) | |
313 if (rank (A) < n_eq) | |
314 error ("qp: equality constraint matrix must be full row rank") | |
315 endif | |
316 xbar = pinv (A) * b; | |
317 else | |
318 xbar = x0; | |
319 endif | |
320 | |
321 ## Check if xbar is feasible with respect to the inequality | |
322 ## constraints also. | |
323 if (n_in > 0) | |
324 res = Ain * xbar - bin; | |
8280
5ee11a81688e
qp.m: convert b <= x <= b and b <= A*x <= b to equality constraints
Gabriele Pannocchia <g.pannocchia@ing.unipi.it>
parents:
7795
diff
changeset
|
325 if (any (res < -rtol * (1 + abs (bin)))) |
5289 | 326 ## xbar is not feasible with respect to the inequality |
327 ## constraints. Compute a step in the null space of the | |
328 ## equality constraints, by solving a QP. If the slack is | |
329 ## small, we have a feasible initial guess. Otherwise, the | |
330 ## problem is infeasible. | |
331 if (n_eq > 0) | |
332 Z = null (A); | |
333 if (isempty (Z)) | |
334 ## The problem is infeasible because A is square and full | |
335 ## rank, but xbar is not feasible. | |
336 info = 6; | |
337 endif | |
338 endif | |
339 | |
340 if (info != 6) | |
341 ## Solve an LP with additional slack variables to find | |
342 ## a feasible starting point. | |
343 gamma = eye (n_in); | |
344 if (n_eq > 0) | |
345 Atmp = [Ain*Z, gamma]; | |
346 btmp = -res; | |
347 else | |
348 Atmp = [Ain, gamma]; | |
349 btmp = bin; | |
350 endif | |
351 ctmp = [zeros(n-n_eq, 1); ones(n_in, 1)]; | |
352 lb = [-Inf*ones(n-n_eq,1); zeros(n_in,1)]; | |
353 ub = []; | |
354 ctype = repmat ("L", n_in, 1); | |
355 [P, dummy, status] = glpk (ctmp, Atmp, btmp, lb, ub, ctype); | |
356 if ((status == 180 || status == 181 || status == 151) | |
357 && all (abs (P(n-n_eq+1:end)) < rtol * (1 + norm (btmp)))) | |
358 ## We found a feasible starting point | |
359 if (n_eq > 0) | |
5306 | 360 x0 = xbar + Z*P(1:n-n_eq); |
5289 | 361 else |
362 x0 = P(1:n); | |
363 endif | |
364 else | |
365 ## The problem is infeasible | |
366 info = 6; | |
367 endif | |
368 endif | |
369 else | |
370 ## xbar is feasible. We use it a starting point. | |
371 x0 = xbar; | |
372 endif | |
373 else | |
374 ## xbar is feasible. We use it a starting point. | |
375 x0 = xbar; | |
376 endif | |
377 endif | |
378 | |
379 if (info == 0) | |
380 ## The initial (or computed) guess is feasible. | |
381 ## We call the solver. | |
382 [x, lambda, info, iter] = __qp__ (x0, H, q, A, b, Ain, bin, maxit); | |
383 else | |
384 iter = 0; | |
385 x = x0; | |
386 lambda = []; | |
387 endif | |
388 obj = 0.5 * x' * H * x + q' * x; | |
389 INFO.solveiter = iter; | |
390 INFO.info = info; | |
391 | |
392 else | |
6046 | 393 print_usage (); |
5289 | 394 endif |
395 | |
396 endfunction |