Mercurial > hg > octave-nkf
annotate scripts/optimization/qp.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | 693e22af08ae |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 Gabriele Pannocchia. |
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 -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}) |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
21 ## @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
|
22 ## @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
|
23 ## @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
|
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}, @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
|
25 ## @deftypefnx {Function File} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@dots{}, @var{options}) |
5289 | 26 ## Solve the quadratic program |
6741 | 27 ## @tex |
28 ## $$ | |
29 ## \min_x {1 \over 2} x^T H x + x^T q | |
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 0.5 x'*H*x + x'*q |
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 ## $$ |
45 ## Ax = b \qquad lb \leq x \leq ub \qquad A_{lb} \leq A_{in} \leq A_{ub} | |
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 ## A*x = b |
53 ## lb <= x <= ub | |
6741 | 54 ## 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
|
55 ## @end group |
5289 | 56 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
57 ## |
6741 | 58 ## @end ifnottex |
5289 | 59 ## @noindent |
60 ## using a null-space active-set method. | |
61 ## | |
62 ## Any bound (@var{A}, @var{b}, @var{lb}, @var{ub}, @var{A_lb}, | |
63 ## @var{A_ub}) may be set to the empty matrix (@code{[]}) if not | |
64 ## present. If the initial guess is feasible the algorithm is faster. | |
65 ## | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
66 ## @table @var |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
67 ## @item options |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
68 ## 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
|
69 ## 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
|
70 ## 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
|
71 ## 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
|
72 ## |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
73 ## @table @code |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
74 ## @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
|
75 ## 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
|
76 ## @end table |
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 ## |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
79 ## @table @var |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
80 ## @item info |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
81 ## Structure containing run-time information about the algorithm. The |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
82 ## following fields are defined: |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
83 ## |
5289 | 84 ## @table @code |
85 ## @item solveiter | |
86 ## The number of iterations required to find the solution. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
87 ## |
5289 | 88 ## @item info |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
89 ## An integer indicating the status of the solution. |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
90 ## |
5289 | 91 ## @table @asis |
92 ## @item 0 | |
93 ## The problem is feasible and convex. Global solution found. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
94 ## |
5289 | 95 ## @item 1 |
96 ## The problem is not convex. Local solution found. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
97 ## |
5289 | 98 ## @item 2 |
99 ## The problem is not convex and unbounded. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
100 ## |
5289 | 101 ## @item 3 |
102 ## Maximum number of iterations reached. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
103 ## |
5289 | 104 ## @item 6 |
105 ## The problem is infeasible. | |
106 ## @end table | |
107 ## @end table | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
108 ## @end table |
5289 | 109 ## @end deftypefn |
110 | |
10060
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
111 ## PKG_ADD: __all_opts__ ("qp"); |
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
112 |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
113 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
|
114 |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
115 nargs = nargin; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
116 |
10060
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
117 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
|
118 x = optimset ("MaxIter", 200); |
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
119 return; |
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
120 endif |
8f51a90eb8d1
implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents:
10059
diff
changeset
|
121 |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
122 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
|
123 options = varargin{end}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
124 nargs--; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
125 else |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
126 options = struct (); |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
127 endif |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
128 |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
129 if (nargs >= 3) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
130 q = varargin{1}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
131 else |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
132 q = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
133 endif |
5289 | 134 |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
135 if (nargs >= 5) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
136 A = varargin{2}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
137 b = varargin{3}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
138 else |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
139 A = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
140 b = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
141 endif |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
142 |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
143 if (nargs >= 7) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
144 lb = varargin{4}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
145 ub = varargin{5}; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
146 else |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
147 lb = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
148 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 == 10) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 else |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
156 A_lb = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
157 A_in = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
158 A_ub = []; |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
159 endif |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
160 |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
161 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
|
162 |
10064
17ce2a700a97
qp.m: Add missing semicolon.
Ben Abbott <bpabbott@mac.com>
parents:
10060
diff
changeset
|
163 maxit = optimget (options, "MaxIter", 200); |
5289 | 164 |
165 ## Checking the quadratic penalty | |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
166 if (! issquare (H)) |
5289 | 167 error ("qp: quadratic penalty matrix not square"); |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
168 elseif (! ishermitian (H)) |
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
169 ## warning ("qp: quadratic penalty matrix not hermitian"); |
5289 | 170 H = (H + H')/2; |
171 endif | |
9872
72d6e0de76c7
fix qp, condest and krylov
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
172 n = rows (H); |
5289 | 173 |
174 ## Checking the initial guess (if empty it is resized to the | |
175 ## right dimension and filled with 0) | |
176 if (isempty (x0)) | |
177 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
|
178 elseif (numel (x0) != n) |
5289 | 179 error ("qp: the initial guess has incorrect length"); |
180 endif | |
181 | |
182 ## Linear penalty. | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
183 if (isempty (q)) |
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
184 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
|
185 elseif (numel (q) != n) |
5289 | 186 error ("qp: the linear term has incorrect length"); |
187 endif | |
188 | |
189 ## 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
|
190 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
|
191 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
|
192 b = zeros (0, 1); |
5289 | 193 n_eq = 0; |
194 else | |
195 [n_eq, n1] = size (A); | |
196 if (n1 != n) | |
10549 | 197 error ("qp: equality constraint matrix has incorrect column dimension"); |
5289 | 198 endif |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
199 if (numel (b) != n_eq) |
10549 | 200 error ("qp: equality constraint matrix and vector have inconsistent dimension"); |
5289 | 201 endif |
202 endif | |
203 | |
204 ## Bound constraints | |
205 Ain = zeros (0, n); | |
206 bin = zeros (0, 1); | |
207 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
|
208 if (nargs > 5) |
5289 | 209 if (! isempty (lb)) |
10549 | 210 if (numel (lb) != n) |
211 error ("qp: lower bound has incorrect length"); | |
212 elseif (isempty (ub)) | |
213 Ain = [Ain; eye(n)]; | |
214 bin = [bin; lb]; | |
215 endif | |
5289 | 216 endif |
217 | |
218 if (! isempty (ub)) | |
10549 | 219 if (numel (ub) != n) |
220 error ("qp: upper bound has incorrect length"); | |
221 elseif (isempty (lb)) | |
222 Ain = [Ain; -eye(n)]; | |
223 bin = [bin; -ub]; | |
224 endif | |
5289 | 225 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
|
226 |
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 if (! isempty (lb) && ! isempty (ub)) |
10549 | 228 rtol = sqrt (eps); |
229 for i = 1:n | |
230 if (abs(lb (i) - ub(i)) < rtol*(1 + max (abs (lb(i) + ub(i))))) | |
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
|
231 ## These are actually an equality constraint |
10549 | 232 tmprow = zeros(1,n); |
233 tmprow(i) = 1; | |
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
|
234 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
|
235 b = [b; 0.5*(lb(i) + ub(i))]; |
10549 | 236 n_eq = n_eq + 1; |
237 else | |
238 tmprow = zeros(1,n); | |
239 tmprow(i) = 1; | |
240 Ain = [Ain; tmprow; -tmprow]; | |
241 bin = [bin; lb(i); -ub(i)]; | |
242 n_in = n_in + 2; | |
243 endif | |
244 endfor | |
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
|
245 endif |
5289 | 246 endif |
247 | |
248 ## Inequality constraints | |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
249 if (nargs > 7) |
5289 | 250 [dimA_in, n1] = size (A_in); |
251 if (n1 != n) | |
10549 | 252 error ("qp: inequality constraint matrix has incorrect column dimension"); |
5289 | 253 else |
10549 | 254 if (! isempty (A_lb)) |
255 if (numel (A_lb) != dimA_in) | |
256 error ("qp: inequality constraint matrix and lower bound vector inconsistent"); | |
257 elseif (isempty (A_ub)) | |
258 Ain = [Ain; A_in]; | |
259 bin = [bin; A_lb]; | |
260 endif | |
261 endif | |
262 if (! isempty (A_ub)) | |
263 if (numel (A_ub) != dimA_in) | |
264 error ("qp: inequality constraint matrix and upper bound vector inconsistent"); | |
265 elseif (isempty (A_lb)) | |
266 Ain = [Ain; -A_in]; | |
267 bin = [bin; -A_ub]; | |
268 endif | |
269 endif | |
270 | |
271 if (! isempty (A_lb) && ! isempty (A_ub)) | |
272 rtol = sqrt (eps); | |
273 for i = 1:dimA_in | |
274 if (abs (A_lb(i) - A_ub(i)) < rtol*(1 + max (abs (A_lb(i) + A_ub(i))))) | |
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
|
275 ## These are actually an equality constraint |
10549 | 276 tmprow = A_in(i,:); |
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
|
277 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
|
278 b = [b; 0.5*(A_lb(i) + A_ub(i))]; |
10549 | 279 n_eq = n_eq + 1; |
280 else | |
281 tmprow = A_in(i,:); | |
282 Ain = [Ain; tmprow; -tmprow]; | |
283 bin = [bin; A_lb(i); -A_ub(i)]; | |
284 n_in = n_in + 2; | |
285 endif | |
286 endfor | |
287 endif | |
5289 | 288 endif |
289 endif | |
290 | |
291 ## Now we should have the following QP: | |
292 ## | |
293 ## min_x 0.5*x'*H*x + x'*q | |
294 ## s.t. A*x = b | |
295 ## Ain*x >= bin | |
296 | |
297 ## Discard inequality constraints that have -Inf bounds since those | |
298 ## will never be active. | |
299 idx = isinf (bin) & bin < 0; | |
6523 | 300 |
6526 | 301 bin(idx) = []; |
302 Ain(idx,:) = []; | |
5289 | 303 |
10059
665ad34efeed
qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents:
9872
diff
changeset
|
304 n_in = numel (bin); |
5310 | 305 |
5289 | 306 ## Check if the initial guess is feasible. |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
307 if (isa (x0, "single") || isa (H, "single") || isa (q, "single") || isa (A, "single") |
10549 | 308 || isa (b, "single")) |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
309 rtol = sqrt (eps ("single")); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
310 else |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
311 rtol = sqrt (eps); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
312 endif |
5289 | 313 |
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
|
314 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
|
315 in_infeasible = (n_in > 0 && any (Ain*x0-bin < -rtol*(1+abs (bin)))); |
5289 | 316 |
317 info = 0; | |
318 if (eq_infeasible || in_infeasible) | |
319 ## The initial guess is not feasible. | |
320 ## First define xbar that is feasible with respect to the equality | |
321 ## constraints. | |
322 if (eq_infeasible) | |
10549 | 323 if (rank (A) < n_eq) |
324 error ("qp: equality constraint matrix must be full row rank") | |
325 endif | |
326 xbar = pinv (A) * b; | |
5289 | 327 else |
10549 | 328 xbar = x0; |
5289 | 329 endif |
330 | |
331 ## Check if xbar is feasible with respect to the inequality | |
332 ## constraints also. | |
333 if (n_in > 0) | |
10549 | 334 res = Ain * xbar - bin; |
335 if (any (res < -rtol * (1 + abs (bin)))) | |
336 ## xbar is not feasible with respect to the inequality | |
337 ## constraints. Compute a step in the null space of the | |
338 ## equality constraints, by solving a QP. If the slack is | |
339 ## small, we have a feasible initial guess. Otherwise, the | |
340 ## problem is infeasible. | |
341 if (n_eq > 0) | |
342 Z = null (A); | |
343 if (isempty (Z)) | |
344 ## The problem is infeasible because A is square and full | |
345 ## rank, but xbar is not feasible. | |
346 info = 6; | |
347 endif | |
348 endif | |
5289 | 349 |
10549 | 350 if (info != 6) |
5289 | 351 ## Solve an LP with additional slack variables to find |
10549 | 352 ## a feasible starting point. |
353 gamma = eye (n_in); | |
354 if (n_eq > 0) | |
355 Atmp = [Ain*Z, gamma]; | |
356 btmp = -res; | |
357 else | |
358 Atmp = [Ain, gamma]; | |
359 btmp = bin; | |
360 endif | |
361 ctmp = [zeros(n-n_eq, 1); ones(n_in, 1)]; | |
362 lb = [-Inf(n-n_eq,1); zeros(n_in,1)]; | |
363 ub = []; | |
364 ctype = repmat ("L", n_in, 1); | |
365 [P, dummy, status] = glpk (ctmp, Atmp, btmp, lb, ub, ctype); | |
366 if ((status == 180 || status == 181 || status == 151) | |
367 && all (abs (P(n-n_eq+1:end)) < rtol * (1 + norm (btmp)))) | |
368 ## We found a feasible starting point | |
369 if (n_eq > 0) | |
370 x0 = xbar + Z*P(1:n-n_eq); | |
371 else | |
372 x0 = P(1:n); | |
5289 | 373 endif |
10549 | 374 else |
375 ## The problem is infeasible | |
376 info = 6; | |
377 endif | |
378 endif | |
379 else | |
380 ## xbar is feasible. We use it a starting point. | |
381 x0 = xbar; | |
382 endif | |
5289 | 383 else |
10549 | 384 ## xbar is feasible. We use it a starting point. |
385 x0 = xbar; | |
5289 | 386 endif |
387 endif | |
388 | |
389 if (info == 0) | |
390 ## The initial (or computed) guess is feasible. | |
391 ## We call the solver. | |
392 [x, lambda, info, iter] = __qp__ (x0, H, q, A, b, Ain, bin, maxit); | |
393 else | |
394 iter = 0; | |
395 x = x0; | |
396 lambda = []; | |
397 endif | |
398 obj = 0.5 * x' * H * x + q' * x; | |
399 INFO.solveiter = iter; | |
400 INFO.info = info; | |
401 | |
402 else | |
6046 | 403 print_usage (); |
5289 | 404 endif |
405 | |
406 endfunction |