annotate scripts/optimization/qp.m @ 33625:d213a148b3f1 default tip @

ensure exp. terminal widget has focus at startup * main-window.cc (main_window): call focus_command_window only if event loop is idle by using a single shot timer * main-window.h: make focus_command_window a public slot
author Torsten Lilge <ttl-octave@mailbox.org>
date Sun, 26 May 2024 02:29:44 +0200
parents c6ef7981b6f1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
3 ## Copyright (C) 2000-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26995
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
7 ##
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
8 ## This file is part of Octave.
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
13 ## (at your option) any later version.
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
14 ##
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
18 ## GNU General Public License for more details.
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
19 ##
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6848
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
25
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
27 ## @deftypefn {} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
28 ## @deftypefnx {} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
29 ## @deftypefnx {} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q}, @var{A}, @var{b})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
30 ## @deftypefnx {} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@var{x0}, @var{H}, @var{q}, @var{A}, @var{b}, @var{lb}, @var{ub})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
31 ## @deftypefnx {} {[@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})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
32 ## @deftypefnx {} {[@var{x}, @var{obj}, @var{info}, @var{lambda}] =} qp (@dots{}, @var{options})
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## Solve a quadratic program (QP).
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ##
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ## Solve the quadratic program defined by
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
36 ## @tex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
37 ## $$
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
38 ## \min_x {1 \over 2} x^T H x + x^T q
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
39 ## $$
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
40 ## @end tex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
41 ## @ifnottex
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
42 ##
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
43 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
44 ## @group
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
45 ## min 0.5 x'*H*x + x'*q
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
46 ## x
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
47 ## @end group
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
48 ## @end example
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
49 ##
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
50 ## @end ifnottex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
51 ## subject to
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
52 ## @tex
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
53 ## $$
24557
a3a263a26aab doc: Fix TeX documentation for qp and clarify size of inputs (bug #52829).
Rik <rik@octave.org>
parents: 23219
diff changeset
54 ## A x = b \qquad lb \leq x \leq ub \qquad A_{lb} \leq A_{in} x \leq A_{ub}
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
55 ## $$
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
56 ## @end tex
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
57 ## @ifnottex
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
58 ##
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
59 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
60 ## @group
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
61 ## A*x = b
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
62 ## lb <= x <= ub
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
63 ## 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
64 ## @end group
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
65 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
66 ##
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6526
diff changeset
67 ## @end ifnottex
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
68 ## @noindent
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
69 ## using a null-space active-set method.
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
70 ##
24557
a3a263a26aab doc: Fix TeX documentation for qp and clarify size of inputs (bug #52829).
Rik <rik@octave.org>
parents: 23219
diff changeset
71 ## Any bound (@var{A}, @var{b}, @var{lb}, @var{ub}, @var{A_in}, @var{A_lb},
a3a263a26aab doc: Fix TeX documentation for qp and clarify size of inputs (bug #52829).
Rik <rik@octave.org>
parents: 23219
diff changeset
72 ## @var{A_ub}) may be set to the empty matrix (@code{[]}) if not present. The
a3a263a26aab doc: Fix TeX documentation for qp and clarify size of inputs (bug #52829).
Rik <rik@octave.org>
parents: 23219
diff changeset
73 ## constraints @var{A} and @var{A_in} are matrices with each row representing
a3a263a26aab doc: Fix TeX documentation for qp and clarify size of inputs (bug #52829).
Rik <rik@octave.org>
parents: 23219
diff changeset
74 ## a single constraint. The other bounds are scalars or vectors depending on
a3a263a26aab doc: Fix TeX documentation for qp and clarify size of inputs (bug #52829).
Rik <rik@octave.org>
parents: 23219
diff changeset
75 ## the number of constraints. The algorithm is faster if the initial guess is
a3a263a26aab doc: Fix TeX documentation for qp and clarify size of inputs (bug #52829).
Rik <rik@octave.org>
parents: 23219
diff changeset
76 ## feasible.
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
77 ##
26334
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
78 ## @var{options} is a structure specifying additional parameters which
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
79 ## control the algorithm. Currently, @code{qp} recognizes these options:
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
80 ## @qcode{"MaxIter"}, @qcode{"TolX"}.
10059
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
81 ##
26334
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
82 ## @qcode{"MaxIter"} proscribes the maximum number of algorithm iterations
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
83 ## before optimization is halted. The default value is 200.
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
84 ## The value must be a positive integer.
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
85 ##
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
86 ## @qcode{"TolX"} specifies the termination tolerance for the unknown variables
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
87 ## @var{x}. The default is @code{sqrt (eps)} or approximately 1e-8.
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
88 ##
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
89 ## On return, @var{x} is the location of the minimum and @var{fval} contains
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
90 ## the value of the objective function at @var{x}.
10059
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
91 ##
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
92 ## @table @var
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
93 ## @item info
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
94 ## Structure containing run-time information about the algorithm. The
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
95 ## following fields are defined:
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
96 ##
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
97 ## @table @code
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
98 ## @item solveiter
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
99 ## 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
100 ##
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
101 ## @item info
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
102 ## An integer indicating the status of the solution.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
103 ##
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
104 ## @table @asis
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
105 ## @item 0
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
106 ## 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
107 ##
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
108 ## @item 1
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
109 ## The problem is not convex. Local solution found.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
110 ##
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
111 ## @item 2
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
112 ## The problem is not convex and unbounded.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
113 ##
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
114 ## @item 3
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
115 ## Maximum number of iterations reached.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
116 ##
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
117 ## @item 6
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
118 ## The problem is infeasible.
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
119 ## @end table
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
120 ## @end table
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
121 ## @end table
26334
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
122 ## @seealso{sqp}
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
123 ## @end deftypefn
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
124
13027
b9a89ca0fb75 prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents: 11589
diff changeset
125 ## PKG_ADD: ## Discard result to avoid polluting workspace with ans at startup.
b9a89ca0fb75 prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents: 11589
diff changeset
126 ## PKG_ADD: [~] = __all_opts__ ("qp");
10060
8f51a90eb8d1 implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents: 10059
diff changeset
127
10059
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
128 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
129
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
130 if (nargin == 1 && ischar (x0) && strcmp (x0, "defaults"))
26334
4bad0d5b97b3 qp.m: Update documentation and default solver options.
Rik <rik@octave.org>
parents: 26332
diff changeset
131 x = struct ("MaxIter", 200, "TolX", sqrt (eps));
10060
8f51a90eb8d1 implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents: 10059
diff changeset
132 return;
8f51a90eb8d1 implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents: 10059
diff changeset
133 endif
8f51a90eb8d1 implement default opts query and register opts for qp
Jaroslav Hajek <highegg@gmail.com>
parents: 10059
diff changeset
134
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
135 nargs = nargin;
10059
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
136 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
137 options = varargin{end};
20735
418ae0cb752f Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents: 20263
diff changeset
138 nargs -= 1;
10059
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
139 else
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
140 options = struct ();
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
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
143 if (nargs != 2 && nargs != 3 && nargs != 5 && nargs != 7 && nargs != 10)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
144 print_usage ();
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
145 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
146
10059
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
147 if (nargs >= 3)
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
148 q = varargin{1};
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
149 else
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
150 q = [];
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
151 endif
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
152
10059
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
153 if (nargs >= 5)
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
154 A = varargin{2};
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
155 b = varargin{3};
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
156 else
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
157 A = [];
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
158 b = [];
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 >= 7)
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
162 lb = varargin{4};
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
163 ub = varargin{5};
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
164 else
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
165 lb = [];
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
166 ub = [];
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
167 endif
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
168
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
169 if (nargs == 10)
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
170 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
171 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
172 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
173 else
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
174 A_lb = [];
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
175 A_in = [];
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
176 A_ub = [];
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
177 endif
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
178
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
179 maxit = optimget (options, "MaxIter", 200);
26332
d4211810202a qp and sqp: Non-fixed tolerance for qp (bug #53506).
Maor Shutman <maorus12@gmail.com>
parents: 26206
diff changeset
180 tol = optimget (options, "TolX", sqrt (eps));
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
181
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
182 ## Validate the quadratic penalty.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
183 if (! issquare (H))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
184 error ("qp: quadratic penalty matrix must be square");
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
185 elseif (! ishermitian (H))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
186 ## warning ("qp: quadratic penalty matrix not hermitian");
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
187 H = (H + H')/2;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
188 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
189 n = rows (H);
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
190
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
191 ## Validate the initial guess.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
192 ## If empty it is resized to the right dimension and filled with 0.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
193 if (isempty (x0))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
194 x0 = zeros (n, 1);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
195 else
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
196 if (! isvector (x0))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
197 error ("qp: the initial guess X0 must be a vector");
10059
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
198 elseif (numel (x0) != n)
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
199 error ("qp: the initial guess X0 has incorrect length");
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
200 endif
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
201 x0 = x0(:); # always use column vector.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
202 endif
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
203
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
204 ## Validate linear penalty.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
205 if (isempty (q))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
206 q = zeros (n, 1);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
207 else
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
208 if (! isvector (q))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
209 error ("qp: Q must be a vector");
10059
665ad34efeed qp.m: handle optimset options
Joshua Redstone <redstone@gmail.com>, John W. Eaton <jwe@octave.org>
parents: 9872
diff changeset
210 elseif (numel (q) != n)
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
211 error ("qp: Q has incorrect length");
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
212 endif
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
213 q = q(:); # always use column vector.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
214 endif
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
215
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
216 ## Validate equality constraint matrices.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
217 if (isempty (A) || isempty (b))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
218 A = zeros (0, n);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
219 b = zeros (0, 1);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
220 n_eq = 0;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
221 else
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
222 [n_eq, n1] = size (A);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
223 if (n1 != n)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
224 error ("qp: equality constraint matrix has incorrect column dimension");
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
225 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
226 if (numel (b) != n_eq)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
227 error ("qp: equality constraint matrix and vector have inconsistent dimensions");
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
228 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
229 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
230
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
231 ## Validate bound constraints.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
232 Ain = zeros (0, n);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
233 bin = zeros (0, 1);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
234 n_in = 0;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
235 if (nargs > 5)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
236 if (! isempty (lb))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
237 if (numel (lb) != n)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
238 error ("qp: lower bound LB has incorrect length");
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
239 elseif (isempty (ub))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
240 Ain = [Ain; eye(n)];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
241 bin = [bin; lb];
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
242 endif
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
243 endif
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
244
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
245 if (! isempty (ub))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
246 if (numel (ub) != n)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
247 error ("qp: upper bound UB has incorrect length");
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
248 elseif (isempty (lb))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
249 Ain = [Ain; -eye(n)];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
250 bin = [bin; -ub];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
251 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
252 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
253
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
254 if (! isempty (lb) && ! isempty (ub))
26332
d4211810202a qp and sqp: Non-fixed tolerance for qp (bug #53506).
Maor Shutman <maorus12@gmail.com>
parents: 26206
diff changeset
255 rtol = tol;
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
256 for i = 1:n
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
257 if (abs (lb (i) - ub(i)) < rtol*(1 + max (abs (lb(i) + ub(i)))))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
258 ## These are actually an equality constraint
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
259 tmprow = zeros (1,n);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
260 tmprow(i) = 1;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
261 A = [A;tmprow];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
262 b = [b; 0.5*(lb(i) + ub(i))];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
263 n_eq += 1;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
264 else
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
265 tmprow = zeros (1,n);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
266 tmprow(i) = 1;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
267 Ain = [Ain; tmprow; -tmprow];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
268 bin = [bin; lb(i); -ub(i)];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
269 n_in += 2;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
270 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
271 endfor
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
272 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
273 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
274
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
275 ## Validate inequality constraints.
28912
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 27923
diff changeset
276 if (nargs > 7 && isempty (A_in) && ! (isempty (A_lb) || isempty (A_ub)))
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 27923
diff changeset
277 warning ("qp: empty inequality constraint matrix but non-empty bound vectors");
26206
aaa23f9845dd qp.m: Allow empty inequality constraints (bug #38483).
Rik <rik@octave.org>
parents: 26138
diff changeset
278 endif
aaa23f9845dd qp.m: Allow empty inequality constraints (bug #38483).
Rik <rik@octave.org>
parents: 26138
diff changeset
279
aaa23f9845dd qp.m: Allow empty inequality constraints (bug #38483).
Rik <rik@octave.org>
parents: 26138
diff changeset
280 if (nargs > 7 && ! isempty (A_in))
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
281 [dimA_in, n1] = size (A_in);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
282 if (n1 != n)
26206
aaa23f9845dd qp.m: Allow empty inequality constraints (bug #38483).
Rik <rik@octave.org>
parents: 26138
diff changeset
283 error ("qp: inequality constraint matrix has incorrect column dimension, expected %i", n1);
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
284 else
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
285 if (! isempty (A_lb))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
286 if (numel (A_lb) != dimA_in)
26206
aaa23f9845dd qp.m: Allow empty inequality constraints (bug #38483).
Rik <rik@octave.org>
parents: 26138
diff changeset
287 error ("qp: inequality constraint matrix and lower bound vector are inconsistent, %i != %i", dimA_in, numel (A_lb));
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
288 elseif (isempty (A_ub))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
289 Ain = [Ain; A_in];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
290 bin = [bin; A_lb];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
291 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
292 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
293 if (! isempty (A_ub))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
294 if (numel (A_ub) != dimA_in)
26206
aaa23f9845dd qp.m: Allow empty inequality constraints (bug #38483).
Rik <rik@octave.org>
parents: 26138
diff changeset
295 error ("qp: inequality constraint matrix and upper bound vector are inconsistent, %i != %i", dimA_in, numel (A_ub));
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
296 elseif (isempty (A_lb))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
297 Ain = [Ain; -A_in];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
298 bin = [bin; -A_ub];
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
299 endif
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
300 endif
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
301
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
302 if (! isempty (A_lb) && ! isempty (A_ub))
26332
d4211810202a qp and sqp: Non-fixed tolerance for qp (bug #53506).
Maor Shutman <maorus12@gmail.com>
parents: 26206
diff changeset
303 rtol = tol;
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
304 for i = 1:dimA_in
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
305 if (abs (A_lb(i) - A_ub(i))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
306 < 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
307 ## These are actually an equality constraint
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
308 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
309 A = [A;tmprow];
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
310 b = [b; 0.5*(A_lb(i) + A_ub(i))];
20231
83792dd9bcc1 Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents: 20165
diff changeset
311 n_eq += 1;
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
312 else
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
313 tmprow = A_in(i,:);
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
314 Ain = [Ain; tmprow; -tmprow];
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
315 bin = [bin; A_lb(i); -A_ub(i)];
20231
83792dd9bcc1 Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents: 20165
diff changeset
316 n_in += 2;
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
317 endif
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
318 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
319 endif
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
320 endif
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
321 endif
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
322
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
323 ## Now we should have the following QP:
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
324 ##
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
325 ## min_x 0.5*x'*H*x + x'*q
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
326 ## s.t. A*x = b
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
327 ## Ain*x >= bin
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
328
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
329 ## Discard inequality constraints that have -Inf bounds since those
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
330 ## will never be active.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
331 idx = (bin == -Inf);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
332
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
333 bin(idx) = [];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
334 Ain(idx,:) = [];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
335
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
336 n_in = numel (bin);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
337
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
338 ## Check if the initial guess is feasible.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
339 if (isa (x0, "single") || isa (H, "single") || isa (q, "single")
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
340 || isa (A, "single") || isa (b, "single"))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
341 rtol = sqrt (eps ("single"));
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
342 else
26332
d4211810202a qp and sqp: Non-fixed tolerance for qp (bug #53506).
Maor Shutman <maorus12@gmail.com>
parents: 26206
diff changeset
343 rtol = tol;
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
344 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
345
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
346 eq_infeasible = (n_eq > 0 && norm (A*x0-b) > rtol*(1+abs (b)));
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
347 in_infeasible = (n_in > 0 && any (Ain*x0-bin < -rtol*(1+abs (bin))));
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
348
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
349 info = 0;
30591
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
350
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
351 if (isdefinite (H) != 1)
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
352 info = 2;
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
353 endif
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
354
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
355 if (info == 0 && (eq_infeasible || in_infeasible))
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
356 ## The initial guess is not feasible.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
357 ## First, define an xbar that is feasible with respect to the
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
358 ## equality constraints.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
359 if (eq_infeasible)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
360 if (rank (A) < n_eq)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
361 error ("qp: equality constraint matrix must be full row rank");
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
362 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
363 xbar = pinv (A) * b;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
364 else
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
365 xbar = x0;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
366 endif
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
367
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
368 ## Second, check that xbar is also feasible with respect to the
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
369 ## inequality constraints.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
370 if (n_in > 0)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
371 res = Ain * xbar - bin;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
372 if (any (res < -rtol * (1 + abs (bin))))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
373 ## xbar is not feasible with respect to the inequality constraints.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
374 ## Compute a step in the null space of the equality constraints,
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
375 ## by solving a QP. If the slack is small, we have a feasible initial
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
376 ## guess. Otherwise, the problem is infeasible.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
377 if (n_eq > 0)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
378 Z = null (A);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
379 if (isempty (Z))
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
380 ## The problem is infeasible because A is square and full rank,
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
381 ## but xbar is not feasible.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
382 info = 6;
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
383 endif
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
384 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
385
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
386 if (info != 6)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
387 ## Solve an LP with additional slack variables
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
388 ## to find a feasible starting point.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
389 gamma = eye (n_in);
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
390 if (n_eq > 0)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
391 Atmp = [Ain*Z, gamma];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
392 btmp = -res;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
393 else
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
394 Atmp = [Ain, gamma];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
395 btmp = bin;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
396 endif
23162
c7fb1f95842e qp.m: Fix regression with incorrect vector dimensions (bug #50067).
Rik <rik@octave.org>
parents: 23083
diff changeset
397 ctmp = [zeros(n-n_eq, 1); ones(n_in, 1)];
c7fb1f95842e qp.m: Fix regression with incorrect vector dimensions (bug #50067).
Rik <rik@octave.org>
parents: 23083
diff changeset
398 lb = [-Inf(n-n_eq,1); zeros(n_in,1)];
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
399 ub = [];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
400 ctype = repmat ("L", n_in, 1);
30316
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
401 [P, FMIN, status] = glpk (ctmp, Atmp, btmp, lb, ub, ctype);
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
402 ## FIXME: Test based only on rtol occasionally fails (Bug #38353).
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30316
diff changeset
403 ## This seems to be a problem in glpk in which return value XOPT(1)
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30316
diff changeset
404 ## is the same as FMIN. Workaround this by explicit test
30316
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
405 if (status != 0)
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
406 info = 6; # The problem is infeasible
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
407 else
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
408 if (all (abs (P(n-n_eq+2:end)) < rtol * (1 + norm (btmp)))
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
409 && (P(n-n_eq+1) < rtol * (1 + norm (btmp))
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
410 || P(n-n_eq+1) == FMIN))
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
411 ## We found a feasible starting point
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
412 if (n_eq > 0)
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
413 x0 = xbar + Z*P(1:n-n_eq);
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
414 else
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
415 x0 = P(1:n);
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
416 endif
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
417 else
30316
c08c73d96985 qp.m: Workaround apparent bug in glpk which causes infeasible solution error (bug #38353)
Rik <rik@octave.org>
parents: 29359
diff changeset
418 info = 6; # The problem is infeasible
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
419 endif
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
420 endif
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
421 endif
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
422 else
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
423 ## xbar is feasible. We use it a starting point.
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
424 x0 = xbar;
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
425 endif
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
426 else
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
427 ## xbar is feasible. We use it a starting point.
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
428 x0 = xbar;
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
429 endif
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
430 endif
26995
ab5bfee22d18 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 26377
diff changeset
431
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
432 if (info == 0)
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
433 ## The initial (or computed) guess is feasible. Call the solver.
26332
d4211810202a qp and sqp: Non-fixed tolerance for qp (bug #53506).
Maor Shutman <maorus12@gmail.com>
parents: 26206
diff changeset
434 [x, lambda, info, iter] = __qp__ (x0, H, q, A, b, Ain, bin, maxit, rtol);
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
435 else
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
436 iter = 0;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
437 x = x0;
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
438 lambda = [];
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
439 endif
32786
c6ef7981b6f1 replace uses of isargout with checks for nargout
John W. Eaton <jwe@octave.org>
parents: 32632
diff changeset
440 if (nargout > 1)
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
441 obj = 0.5 * x' * H * x + q' * x;
20263
5c088348fddb qp.m: Overhaul function (fixes bug #45324).
Rik <rik@octave.org>
parents: 20231
diff changeset
442 endif
32786
c6ef7981b6f1 replace uses of isargout with checks for nargout
John W. Eaton <jwe@octave.org>
parents: 32632
diff changeset
443 if (nargout > 2)
5289
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
444 INFO.solveiter = iter;
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
445 INFO.info = info;
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
446 endif
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
447
b98bf1d70a0a [project @ 2005-04-21 14:43:14 by jwe]
jwe
parents:
diff changeset
448 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 14868
diff changeset
449
17897
185038fe7a16 qp.m: Fix test on GLPK's return status (bug #40536)
Julien Bect <julien.bect@supelec.fr>
parents: 17744
diff changeset
450
22489
93ea313301f9 test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents: 22323
diff changeset
451 ## Test infeasible initial guess
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
452 %!testif HAVE_GLPK <*40536>
17897
185038fe7a16 qp.m: Fix test on GLPK's return status (bug #40536)
Julien Bect <julien.bect@supelec.fr>
parents: 17744
diff changeset
453 %!
185038fe7a16 qp.m: Fix test on GLPK's return status (bug #40536)
Julien Bect <julien.bect@supelec.fr>
parents: 17744
diff changeset
454 %! H = 1; q = 0; # objective: x -> 0.5 x^2
185038fe7a16 qp.m: Fix test on GLPK's return status (bug #40536)
Julien Bect <julien.bect@supelec.fr>
parents: 17744
diff changeset
455 %! A = 1; lb = 1; ub = +inf; # constraint: x >= 1
185038fe7a16 qp.m: Fix test on GLPK's return status (bug #40536)
Julien Bect <julien.bect@supelec.fr>
parents: 17744
diff changeset
456 %! x0 = 0; # infeasible initial guess
185038fe7a16 qp.m: Fix test on GLPK's return status (bug #40536)
Julien Bect <julien.bect@supelec.fr>
parents: 17744
diff changeset
457 %!
185038fe7a16 qp.m: Fix test on GLPK's return status (bug #40536)
Julien Bect <julien.bect@supelec.fr>
parents: 17744
diff changeset
458 %! [x, obj_qp, INFO, lambda] = qp (x0, H, q, [], [], [], [], lb, A, ub);
185038fe7a16 qp.m: Fix test on GLPK's return status (bug #40536)
Julien Bect <julien.bect@supelec.fr>
parents: 17744
diff changeset
459 %!
185038fe7a16 qp.m: Fix test on GLPK's return status (bug #40536)
Julien Bect <julien.bect@supelec.fr>
parents: 17744
diff changeset
460 %! assert (isstruct (INFO) && isfield (INFO, "info") && (INFO.info == 0));
185038fe7a16 qp.m: Fix test on GLPK's return status (bug #40536)
Julien Bect <julien.bect@supelec.fr>
parents: 17744
diff changeset
461 %! assert ([x obj_qp], [1.0 0.5], eps);
30591
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
462
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
463 %!test <*61762>
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
464 %! [x, obj, info] = qp ([], [21, 30, 39; 30, 45, 60; 39, 60, 81], [-40; -65; -90]);
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
465 %! assert (x, zeros (3, 1));
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
466 %! assert (obj, 0);
2f1fae9dd79d qp: check if argument H is positive definite, fixes bug #61762
Olaf Till <i7tiol@t-online.de>
parents: 30564
diff changeset
467 %! assert (info.info, 2);