Mercurial > hg > octave-lyh
diff scripts/optimization/glpk.m @ 5237:652e8aa49fa7
[project @ 2005-03-23 21:28:45 by jwe]
author | jwe |
---|---|
date | Wed, 23 Mar 2005 21:28:46 +0000 |
parents | bdf892d3b024 |
children | a34c3c5c37cf |
line wrap: on
line diff
--- a/scripts/optimization/glpk.m +++ b/scripts/optimization/glpk.m @@ -19,19 +19,28 @@ ## GLPK - An Octave Interface for the GNU GLPK library ## -## This routine calls the glpk library to solve an LP/MIP problem. A typical -## LP problem has following structure: +## This routine calls the glpk library to solve an LP/MIP problem. By +## default, glpk solves the following standard LP: +## +## min C'*x +## +## subject to +## +## A*x = b +## x >= 0 ## -## [min|max] C'x -## s.t. -## Ax ["="|"<="|">="] b -## {x <= UB} -## {x >= LB} +## but may also solve problems of the form +## +## [min|max] C'x +## +## subject to +## +## Ax ["="|"<="|">="] b +## {x <= UB} +## {x >= LB} ## ## The calling syntax is: -## [XOPT,FOPT,STATUS,EXTRA]=glpk(SENSE,C,... -## A,B,CTYPE,LB,UB,... -## VARTYPE,PARAM,LPSOLVER,SAVE) +## [XOPT,FOPT,STATUS,EXTRA]=glpk(C,A,B,LB,UB,CTYPE,VARTYPE,SENSE,PARAM) ## ## For a quick reference to the syntax just type glpk at command prompt. ## @@ -40,11 +49,6 @@ ## ## --- INPUTS --- ## -## SENSE: indicates whether the problem is a minimization -## or maximization problem. -## SENSE = 1 minimize -## SENSE = -1 maximize. -## ## C: A column array containing the objective function ## coefficients. ## @@ -53,6 +57,12 @@ ## B: A column array containing the right-hand side value for ## each constraint in the constraint matrix. ## +## LB: An array of at least length numcols containing the lower +## bound on each of the variables. +## +## UB: An array of at least length numcols containing the upper +## bound on each of the variables. +## ## CTYPE: A column array containing the sense of each constraint ## in the constraint matrix. ## CTYPE(i) = 'F' Free (unbounded) variable @@ -62,30 +72,20 @@ ## CTYPE(i) = 'D' Double-bounded variable ## (This is case sensitive). ## -## LB: An array of at least length numcols containing the lower -## bound on each of the variables. -## -## UB: An array of at least length numcols containing the upper -## bound on each of the variables. -## ## VARTYPE: A column array containing the types of the variables. ## VARTYPE(i) = 'C' continuous variable ## VARTYPE(i) = 'I' Integer variable ## (This is case sensitive). ## +## SENSE: indicates whether the problem is a minimization +## or maximization problem. +## SENSE = 1 minimize +## SENSE = -1 maximize. +## ## PARAM: A structure containing some parameters used to define ## the behavior of solver. For more details type ## HELP GLPKPARAMS. ## -## LPSOLVER: Selects which solver using to solve LP problems. -## LPSOLVER=1 Revised Simplex Method -## LPSOLVER=2 Interior Point Method -## If the problem is a MIP problem this flag will be ignored. -## -## SAVE: Saves a copy of the problem if SAVE<>0. -## The file name can not be specified and defaults to "outpb.lp". -## The output file is CPLEX LP format. -## ## --- OUTPUTS --- ## ## XOPT: The optimizer. @@ -145,11 +145,11 @@ ## Author: Nicolo' Giorgetti <giorgetti@dii.unisi.it> ## Adapted-by: jwe -function [xopt, fmin, status, extra] = glpk (sense, c, a, b, ctype, lb, ub, vartype, param, lpsolver, savepb) +function [xopt, fmin, status, extra] = glpk (c, a, b, lb, ub, ctype, vartype, sense, param) ## If there is no input output the version and syntax - if (nargin < 4 || nargin > 11) - usage ("[xopt, fopt, status, extra] = glpk (sense, c, a, b, ctype, lb, ub, vartype, param, lpsolver, savepb"); + if (nargin < 3 || nargin > 9) + usage ("[xopt, fopt, status, extra] = glpk (c, a, b, lb, ub, ctype, vartype, sense, param)"); return; endif @@ -161,7 +161,7 @@ ## Force column vector. c = c(:); - ## 3) Matrix constraint + ## 2) Matrix constraint if (isempty (a)) error ("A cannot be an empty matrix"); @@ -173,7 +173,7 @@ return; endif - ## 4) RHS + ## 3) RHS if (isempty (b)) error ("B cannot be an empty vector"); @@ -184,39 +184,22 @@ return; endif - ## 5) Sense of each constraint + ## 4) Vector with the lower bound of each variable - if (nargin > 4) - if (isempty (ctype)) - ctype = repmat ("U", nc, 1); - elseif (! ischar (ctype) || all (size (ctype) > 1) || length (ctype) != nc) - error ("CTYPE must be a char valued %d by 1 column vector", nc); - return; - elseif (! all (ctype == "F" | ctype == "U" | ctype == "S" - | ctype == "L" | ctype == "D")) - error ("CTYPE must contain only F, U, S, L, or D"); - return; - endif - else - ctype = repmat ("U", nc, 1); - end - - ## 6) Vector with the lower bound of each variable - - if (nargin > 5) + if (nargin > 3) if (isempty (lb)) - lb = repmat (-Inf, nx, 1); + lb = zeros (0, nx, 1); elseif (! isreal (lb) || all (size (lb) > 1) || length (lb) != nx) error ("LB must be a real valued %d by 1 column vector", nx); return; endif else - lb = repmat (-Inf, nx, 1); + lb = zeros (nx, 1); end - ## 7) Vector with the upper bound of each variable + ## 5) Vector with the upper bound of each variable - if (nargin > 6) + if (nargin > 4) if (isempty (ub)) ub = repmat (Inf, nx, 1); elseif (! isreal (ub) || all (size (ub) > 1) || length (ub) != nx) @@ -227,9 +210,26 @@ ub = repmat (Inf, nx, 1); end - ## 8) Vector with the type of variables + ## 6) Sense of each constraint - if (nargin > 7) + if (nargin > 5) + if (isempty (ctype)) + ctype = repmat ("S", nc, 1); + elseif (! ischar (ctype) || all (size (ctype) > 1) || length (ctype) != nc) + error ("CTYPE must be a char valued %d by 1 column vector", nc); + return; + elseif (! all (ctype == "F" | ctype == "U" | ctype == "S" + | ctype == "L" | ctype == "D")) + error ("CTYPE must contain only F, U, S, L, or D"); + return; + endif + else + ctype = repmat ("S", nc, 1); + end + + ## 7) Vector with the type of variables + + if (nargin > 6) if isempty (vartype) vartype = repmat ("C", nx, 1); elseif (! ischar (vartype) || all (size (vartype) > 1) @@ -245,7 +245,7 @@ vartype = repmat ("C", nx, 1); endif - ## 9) Parameters vector + ## 8) Parameters vector if (nargin > 8) if (! isstruct (param)) @@ -256,29 +256,7 @@ param = struct (); endif - ## 10) Select solver method: simplex or interior point - - if (nargin > 9) - if (! (isreal (lpsolver) && isscalar (lpsolver))) - error ("LPSOLVER must be a real scalar value"); - return; - endif - else - lpsolver = 1; - endif - - ## 11) Save the problem - - if (nargin > 10) - if (! (isreal (savepb) && isscalar (lpsolver))) - error ("LPSOLVER must be a real scalar value"); - return; - endif - else - savepb = 0; - end - [xopt, fmin, status, extra] = ... - __glpk__ (sense, c, a, b, ctype, lb, ub, vartype, param, lpsolver, savepb); + __glpk__ (c, a, b, lb, ub, ctype, vartype, sense, param); endfunction