1 ## Copyright (C) 2009 Luca Favatella <slackydeb@gmail.com>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn{Function File} {@var{x} =} linprog (@var{f}, @var{A}, @var{b})
18 ## @deftypefnx{Function File} {@var{x} =} linprog (@var{f}, @var{A}, @var{b}, @var{Aeq}, @var{beq})
19 ## @deftypefnx{Function File} {@var{x} =} linprog (@var{f}, @var{A}, @var{b}, @var{Aeq}, @var{beq}, @var{lb}, @var{ub})
20 ## @deftypefnx{Function File} {[@var{x}, @var{fval}] =} linprog (@dots{})
21 ## Solve a linear problem.
29 ## (both f and x are column vectors) subject to
39 ## If not specified, @var{Aeq} and @var{beq} default to empty matrices.
41 ## If not specified, the lower bound @var{lb} defaults to minus infinite
42 ## and the upper bound @var{ub} defaults to infinite.
47 function [x fval] = linprog (f, A, b,
51 if (((nargin != 3) && (nargin != 5) && (nargin != 7)) ||
59 if (isempty (A) && isempty (b))
61 b = zeros (rows (A), 1);
67 error ("f must be a column vector");
68 elseif (columns (A) != nr_f)
69 error ("columns (A) != rows (f)");
70 elseif (size (b) != [nr_A 1])
71 error ("size (b) != [(rows (A)) 1]");
76 Aeq = zeros (0, nr_f);
78 if (columns (Aeq) != nr_f)
79 error ("columns (Aeq) != rows (f)");
87 if (size (beq) != [nr_Aeq 1])
88 error ("size (beq) != [(rows (Aeq)) 1]");
95 if (size (lb) != [nr_f 1])
96 error ("size (lb) != [(rows (f)) 1]");
103 if (size (ub) != [nr_f 1])
104 error ("size (ub) != [(rows (f)) 1]");
109 ctype = [(repmat ("U", nr_A, 1));
110 (repmat ("S", nr_Aeq, 1))];
111 [x(1:nr_f, 1) fval(1, 1)] = glpk (f, [A; Aeq], [b; beq], lb, ub, ctype);
126 %! assert (linprog (f, A, b, Aeq, beq, lb, ub), x_exp);
128 %!shared f, A, b, lb, ub, x_exp, fval_exp
129 %! f = [21 25 31 34 23 19 32 36 27 25 19]';
131 %! A1 = [ 1 0 0 0 1 0 0 1 0 0 0;
132 %! 0 1 0 0 0 1 0 0 1 0 0;
133 %! 0 0 1 0 0 0 0 0 0 1 0;
134 %! 0 0 0 1 0 0 1 0 0 0 1];
135 %! A2 = [ 1 1 1 1 0 0 0 0 0 0 0;
136 %! 0 0 0 0 1 1 1 0 0 0 0;
137 %! 0 0 0 0 0 0 0 1 1 1 1];
140 %! b1 = [40; 50; 50; 70];
141 %! b2 = [100; 60; 50];
144 %! lb = zeros (rows (f), 1);
145 %! ub = Inf (rows (f), 1);
147 %! x_exp = [40 0 50 10 0 50 10 0 0 0 50]';
148 %! fval_exp = f' * x_exp;
153 %! [x_obs fval_obs] = linprog (f, A, b, Aeq, beq, lb, ub);
154 %! assert ([x_obs; fval_obs], [x_exp; fval_exp]);
157 %! Aeq = zeros (1, rows (f));
159 %! assert (linprog (f, A, b, Aeq, beq, lb, ub), x_exp);