From: <sla...@us...> - 2009-07-22 14:18:35
|
Revision: 6039 http://octave.svn.sourceforge.net/octave/?rev=6039&view=rev Author: slackydeb Date: 2009-07-22 14:18:32 +0000 (Wed, 22 Jul 2009) Log Message: ----------- Bump package version. linprog.m * API break: don't let nargin == 6 * API break: f, lb, ub and x must be column vectors * clean doc * add comments * improve the feeback given to users if arguments have wrong dimensions * improve tests readibility * test both output values (and not only the first one) * bump version Modified Paths: -------------- trunk/octave-forge/main/optim/DESCRIPTION trunk/octave-forge/main/optim/inst/linprog.m Modified: trunk/octave-forge/main/optim/DESCRIPTION =================================================================== --- trunk/octave-forge/main/optim/DESCRIPTION 2009-07-22 10:43:16 UTC (rev 6038) +++ trunk/octave-forge/main/optim/DESCRIPTION 2009-07-22 14:18:32 UTC (rev 6039) @@ -1,6 +1,6 @@ Name: Optim -Version: 1.0.6 -Date: 2009-05-18 +Version: 1.0.7 +Date: 2009-07-22 Author: Various Authors Maintainer: The Octave Community Title: Optimzation. Modified: trunk/octave-forge/main/optim/inst/linprog.m =================================================================== --- trunk/octave-forge/main/optim/inst/linprog.m 2009-07-22 10:43:16 UTC (rev 6038) +++ trunk/octave-forge/main/optim/inst/linprog.m 2009-07-22 14:18:32 UTC (rev 6039) @@ -16,92 +16,131 @@ ## -*- texinfo -*- ## @deftypefn{Function File} {@var{x} =} linprog (@var{f}, @var{A}, @var{b}) ## @deftypefnx{Function File} {@var{x} =} linprog (@var{f}, @var{A}, @var{b}, @var{Aeq}, @var{beq}) -## @deftypefnx{Function File} {@var{x} =} linprog (@var{f}, @var{A}, @var{b}, @var{Aeq}, @var{beq}, @var{LB}) -## @deftypefnx{Function File} {@var{x} =} linprog (@var{f}, @var{A}, @var{b}, @var{Aeq}, @var{beq}, @var{LB}, @var{UB}) +## @deftypefnx{Function File} {@var{x} =} linprog (@var{f}, @var{A}, @var{b}, @var{Aeq}, @var{beq}, @var{lb}, @var{ub}) ## @deftypefnx{Function File} {[@var{x}, @var{fval}] =} linprog (@dots{}) -## Solve a linear problem. @code{linprog} solves the following LP: +## Solve a linear problem. ## +## Finds +## ## @example -## min f'*x +## min (f' * x) ## @end example ## -## subject to +## (both f and x are column vectors) subject to ## ## @example ## @group -## A*x <= b -## Aeq*x = beq -## x >= LB -## x <= UB +## A * x <= b +## Aeq * x = beq +## lb <= x <= ub ## @end group ## @end example ## -## The default @var{Aeq} and @var{beq} are assumed to be empty matrices. +## If not specified, @var{Aeq} and @var{beq} default to empty matrices. ## -## The default lower bound @var{LB} is assumed to be minus infinite; the -## default upper bound @var{UB} is assumed to be infinite. +## If not specified, the lower bound @var{lb} defaults to minus infinite +## and the upper bound @var{ub} defaults to infinite. ## ## @seealso{glpk} ## @end deftypefn ## Author: Luca Favatella <sla...@gm...> -## Version: 0.3.1 +## Version: 0.4 # TODO: write a test using not null and # not zero Aeq and beq function [x fval] = linprog (f, A, b, Aeq = [], beq = [], - LB = [], UB = []) + lb = [], ub = []) - if ((nargin < 3) || (nargin == 4) || (nargin > 7) || - (nargout > 2) || - (! isvector (f))) + nr_f = rows(f); + nr_A = rows (A); + + if (((nargin != 3) && (nargin != 5) && (nargin != 7)) || + (nargout > 2)) print_usage (); + elseif (columns (f) != 1) + error ("f must be a column vector"); + elseif (columns (A) != nr_f) + error ("columns (A) != rows (f)"); + elseif (size (b) != [nr_A 1]) + error ("size (b) != [(rows (A)) 1]"); else - l_f = length(f); - nr_A = rows (A); - nr_Aeq = rows (Aeq); - + ## Sanitize Aeq if (isempty (Aeq)) - Aeq = zeros (0, l_f); + Aeq = zeros (0, nr_f); endif + if (columns (Aeq) != nr_f) + error ("columns (Aeq) != rows (f)"); + endif + + ## Sanitize beq if (isempty (beq)) beq = zeros (0, 1); endif + nr_Aeq = rows (Aeq); + if (size (beq) != [nr_Aeq 1]) + error ("size (beq) != [(rows (Aeq)) 1]"); + endif - if (isempty (LB)) - LB = - inf (1, l_f); + ## Sanitize lb + if (isempty (lb)) + LB = - Inf (nr_f, 1); endif - if (isempty (UB)) - UB = inf (1, l_f); + if (size (lb) != [nr_f 1]) + error ("size (lb) != [(rows (f)) 1]"); endif + ## Sanitize ub + if (isempty (ub)) + UB = Inf (nr_f, 1); + endif + if (size (ub) != [nr_f 1]) + error ("size (ub) != [(rows (f)) 1]"); + endif + + + ## Call glpk ctype = [(repmat ("U", nr_A, 1)); (repmat ("S", nr_Aeq, 1))]; - [x fval] = glpk (f(1:l_f), - [A(1:nr_A, 1:l_f); Aeq(1:nr_Aeq, 1:l_f)], - [b(1:nr_A, 1); beq(1:nr_Aeq, 1)], - LB(1:l_f), - UB(1:l_f), - ctype); + [x(1:nr_f, 1) fval(1, 1)] = glpk (f, [A; Aeq], [b; beq], lb, ub, ctype); endif endfunction -%!shared f, A, b, LB, expected -%! f = [21 25 31 34 23 19 32 36 27 25 19]; -%! A1 = [1 0 0 0 1 0 0 1 0 0 0; 0 1 0 0 0 1 0 0 1 0 0; 0 0 1 0 0 0 0 0 0 1 0; 0 0 0 1 0 0 1 0 0 0 1]; -%! A2 = [1 1 1 1 0 0 0 0 0 0 0; 0 0 0 0 1 1 1 0 0 0 0; 0 0 0 0 0 0 0 1 1 1 1]; -%! A = [-A1; A2]; +%!shared f, A, b, lb, ub, x_exp, fval_exp +%! f = [21 25 31 34 23 19 32 36 27 25 19]'; +%! +%! A1 = [ 1 0 0 0 1 0 0 1 0 0 0; +%! 0 1 0 0 0 1 0 0 1 0 0; +%! 0 0 1 0 0 0 0 0 0 1 0; +%! 0 0 0 1 0 0 1 0 0 0 1]; +%! A2 = [ 1 1 1 1 0 0 0 0 0 0 0; +%! 0 0 0 0 1 1 1 0 0 0 0; +%! 0 0 0 0 0 0 0 1 1 1 1]; +%! A = [-A1; A2]; +%! %! b1 = [40; 50; 50; 70]; %! b2 = [100; 60; 50]; -%! b = [-b1; b2]; -%! LB = zeros (1, 11); -%! expected = [40; 0; 50; 10; 0; 50; 10; 0; 0; 0; 50]; +%! b = [-b1; b2]; %! -%!assert (linprog (f, A, b, [], [], LB), expected); -%!assert (linprog (f, A, b, zeros (1, 11), 0, LB), expected); \ No newline at end of file +%! lb = zeros (rows (f), 1); +%! ub = Inf (rows (f), 1); +%! +%! x_exp = [40 0 50 10 0 50 10 0 0 0 50]'; +%! fval_exp = f' * x_exp; +%! +%!test +%! Aeq = []; +%! beq = []; +%! [x_obs fval_obs] = linprog (f, A, b, Aeq, beq, lb, ub); +%! assert ([x_obs; fval_obs], [x_exp; fval_exp]); +%! +%!test +%! Aeq = zeros (1, rows (f)); +%! beq = 0; +%! assert(linprog (f, A, b, Aeq, beq, lb, ub), x_exp); \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |