1## Copyright (C) 2005, 2006, 2007, 2008, 2009 Nicolo' Giorgetti
3## This file is part of Octave.
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
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
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.
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
20## @deftypefn {Function File} {[@var{xopt}, @var{fmin}, @var{status}, @var{extra}] =} glpk (@var{c}, @var{a}, @var{b}, @var{lb}, @var{ub}, @var{ctype}, @var{vartype}, @var{sense}, @var{param})
21## Solve a linear program using the GNU GLPK library. Given three
22## arguments, @code{glpk} solves the following standard LP:
39## Ax = b \qquad x \geq 0
51## but may also solve problems of the form
55## [ \min_x | \max_x ] C^T x
68## Ax [ = | \leq | \geq ] b \qquad LB \leq x \leq UB
74## A*x [ "=" | "<=" | ">=" ] b
85## A column array containing the objective function coefficients.
88## A matrix containing the constraints coefficients.
91## A column array containing the right-hand side value for each constraint
92## in the constraint matrix.
95## An array containing the lower bound on each of the variables. If
96## @var{lb} is not supplied, the default lower bound for the variables is
100## An array containing the upper bound on each of the variables. If
101## @var{ub} is not supplied, the default upper bound is assumed to be
105## An array of characters containing the sense of each constraint in the
106## constraint matrix. Each element of the array may be one of the
110## A free (unbounded) constraint (the constraint is ignored).
112## An inequality constraint with an upper bound (@code{A(i,:)*x <= b(i)}).
114## An equality constraint (@code{A(i,:)*x = b(i)}).
116## An inequality with a lower bound (@code{A(i,:)*x >= b(i)}).
118## An inequality constraint with both upper and lower bounds
119## (@code{A(i,:)*x >= -b(i)} @emph{and} (@code{A(i,:)*x <= b(i)}).
123## A column array containing the types of the variables.
126## A continuous variable.
128## An integer variable.
132## If @var{sense} is 1, the problem is a minimization. If @var{sense} is
133## -1, the problem is a maximization. The default value is 1.
136## A structure containing the following parameters used to define the
137## behavior of solver. Missing elements in the structure take on default
138## values, so you only need to set the elements that you wish to change
141## Integer parameters:
144## @item msglev (@w{@code{LPX_K_MSGLEV}}, default: 1)
145## Level of messages output by solver routines:
150## Error messages only.
154## Full output (includes informational messages).
157## @item scale (@w{@code{LPX_K_SCALE}}, default: 1)
163## Equilibration scaling.
165## Geometric mean scaling, then equilibration scaling.
168## @item dual (@w{@code{LPX_K_DUAL}}, default: 0)
169## Dual simplex option:
172## Do not use the dual simplex.
174## If initial basic solution is dual feasible, use the dual simplex.
177## @item price (@w{@code{LPX_K_PRICE}}, default: 1)
178## Pricing option (for both primal and dual simplex):
183## Steepest edge pricing.
186## @item round (@w{@code{LPX_K_ROUND}}, default: 0)
187## Solution rounding option:
190## Report all primal and dual values "as is".
192## Replace tiny primal and dual values by exact zero.
195## @item itlim (@w{@code{LPX_K_ITLIM}}, default: -1)
196## Simplex iterations limit. If this value is positive, it is decreased by
197## one each time when one simplex iteration has been performed, and
198## reaching zero value signals the solver to stop the search. Negative
199## value means no iterations limit.
201## @item itcnt (@w{@code{LPX_K_OUTFRQ}}, default: 200)
202## Output frequency, in iterations. This parameter specifies how
203## frequently the solver sends information about the solution to the
206## @item branch (@w{@code{LPX_K_BRANCH}}, default: 2)
207## Branching heuristic option (for MIP only):
210## Branch on the first variable.
212## Branch on the last variable.
214## Branch using a heuristic by Driebeck and Tomlin.
217## @item btrack (@w{@code{LPX_K_BTRACK}}, default: 2)
218## Backtracking heuristic option (for MIP only):
221## Depth first search.
223## Breadth first search.
225## Backtrack using the best projection heuristic.
228## @item presol (@w{@code{LPX_K_PRESOL}}, default: 1)
229## If this flag is set, the routine lpx_simplex solves the problem using
230## the built-in LP presolver. Otherwise the LP presolver is not used.
232## @item lpsolver (default: 1)
233## Select which solver to use. If the problem is a MIP problem this flag
237## Revised simplex method.
239## Interior point method.
241## @item save (default: 0)
242## If this parameter is nonzero, save a copy of the problem in
243## CPLEX LP format to the file @file{"outpb.lp"}. There is currently no
244## way to change the name of the output file.
250## @item relax (@w{@code{LPX_K_RELAX}}, default: 0.07)
251## Relaxation parameter used in the ratio test. If it is zero, the textbook
252## ratio test is used. If it is non-zero (should be positive), Harris'
253## two-pass ratio test is used. In the latter case on the first pass of the
254## ratio test basic variables (in the case of primal simplex) or reduced
255## costs of non-basic variables (in the case of dual simplex) are allowed
256## to slightly violate their bounds, but not more than
257## @code{relax*tolbnd} or @code{relax*toldj (thus, @code{relax} is a
258## percentage of @code{tolbnd} or @code{toldj}}.
260## @item tolbnd (@w{@code{LPX_K_TOLBND}}, default: 10e-7)
261## Relative tolerance used to check if the current basic solution is primal
262## feasible. It is not recommended that you change this parameter unless you
263## have a detailed understanding of its purpose.
265## @item toldj (@w{@code{LPX_K_TOLDJ}}, default: 10e-7)
266## Absolute tolerance used to check if the current basic solution is dual
267## feasible. It is not recommended that you change this parameter unless you
268## have a detailed understanding of its purpose.
270## @item tolpiv (@w{@code{LPX_K_TOLPIV}}, default: 10e-9)
271## Relative tolerance used to choose eligible pivotal elements of the
272## simplex table. It is not recommended that you change this parameter unless you
273## have a detailed understanding of its purpose.
275## @item objll (@w{@code{LPX_K_OBJLL}}, default: -DBL_MAX)
276## Lower limit of the objective function. If on the phase II the objective
277## function reaches this limit and continues decreasing, the solver stops
278## the search. This parameter is used in the dual simplex method only.
280## @item objul (@w{@code{LPX_K_OBJUL}}, default: +DBL_MAX)
281## Upper limit of the objective function. If on the phase II the objective
282## function reaches this limit and continues increasing, the solver stops
283## the search. This parameter is used in the dual simplex only.
285## @item tmlim (@w{@code{LPX_K_TMLIM}}, default: -1.0)
286## Searching time limit, in seconds. If this value is positive, it is
287## decreased each time when one simplex iteration has been performed by the
288## amount of time spent for the iteration, and reaching zero value signals
289## the solver to stop the search. Negative value means no time limit.
291## @item outdly (@w{@code{LPX_K_OUTDLY}}, default: 0.0)
292## Output delay, in seconds. This parameter specifies how long the solver
293## should delay sending information about the solution to the standard
294## output. Non-positive value means no delay.
296## @item tolint (@w{@code{LPX_K_TOLINT}}, default: 10e-5)
297## Relative tolerance used to check if the current basic solution is integer
298## feasible. It is not recommended that you change this parameter unless
299## you have a detailed understanding of its purpose.
301## @item tolobj (@w{@code{LPX_K_TOLOBJ}}, default: 10e-7)
302## Relative tolerance used to check if the value of the objective function
303## is not better than in the best known integer feasible solution. It is
304## not recommended that you change this parameter unless you have a
305## detailed understanding of its purpose.
313## The optimizer (the value of the decision variables at the optimum).
315## The optimum value of the objective function.
317## Status of the optimization.
321## @item 180 (@w{@code{LPX_OPT}})
322## Solution is optimal.
323## @item 181 (@w{@code{LPX_FEAS}})
324## Solution is feasible.
325## @item 182 (@w{@code{LPX_INFEAS}})
326## Solution is infeasible.
327## @item 183 (@w{@code{LPX_NOFEAS}})
328## Problem has no feasible solution.
329## @item 184 (@w{@code{LPX_UNBND}})
330## Problem has no unbounded solution.
331## @item 185 (@w{@code{LPX_UNDEF}})
332## Solution status is undefined.
334## Interior Point Method:
336## @item 150 (@w{@code{LPX_T_UNDEF}})
337## The interior point method is undefined.
338## @item 151 (@w{@code{LPX_T_OPT}})
339## The interior point method is optimal.
341## Mixed Integer Method:
343## @item 170 (@w{@code{LPX_I_UNDEF}})
344## The status is undefined.
345## @item 171 (@w{@code{LPX_I_OPT}})
346## The solution is integer optimal.
347## @item 172 (@w{@code{LPX_I_FEAS}})
348## Solution integer feasible but its optimality has not been proven
349## @item 173 (@w{@code{LPX_I_NOFEAS}})
350## No integer feasible solution.
353## If an error occurs, @var{status} will contain one of the following
357## @item 204 (@w{@code{LPX_E_FAULT}})
358## Unable to start the search.
359## @item 205 (@w{@code{LPX_E_OBJLL}})
360## Objective function lower limit reached.
361## @item 206 (@w{@code{LPX_E_OBJUL}})
362## Objective function upper limit reached.
363## @item 207 (@w{@code{LPX_E_ITLIM}})
364## Iterations limit exhausted.
365## @item 208 (@w{@code{LPX_E_TMLIM}})
366## Time limit exhausted.
367## @item 209 (@w{@code{LPX_E_NOFEAS}})
368## No feasible solution.
369## @item 210 (@w{@code{LPX_E_INSTAB}})
370## Numerical instability.
371## @item 211 (@w{@code{LPX_E_SING}})
372## Problems with basis matrix.
373## @item 212 (@w{@code{LPX_E_NOCONV}})
374## No convergence (interior).
375## @item 213 (@w{@code{LPX_E_NOPFS}})
376## No primal feasible solution (LP presolver).
377## @item 214 (@w{@code{LPX_E_NODFS}})
378## No dual feasible solution (LP presolver).
381## A data structure containing the following fields:
388## Time (in seconds) used for solving LP/MIP problem.
390## Memory (in bytes) used for solving LP/MIP problem (this is not
391## available if the version of GLPK is 4.15 or later).
403## b = [100, 600, 300]';
413## [xmin, fmin, status, extra] = ...
414## glpk (c, a, b, lb, ub, ctype, vartype, s, param);
419## Author: Nicolo' Giorgetti <giorgetti@dii.unisi.it>
422function [xopt, fmin, status, extra] = glpk (c, a, b, lb, ub, ctype, vartype, sense, param)
424 ## If there is no input output the version and syntax
425 if (nargin < 3 || nargin > 9)
430 if (all (size (c) > 1) || iscomplex (c) || ischar (c))
431 error ("C must be a real vector");
435 ## Force column vector.
438 ## 2) Matrix constraint
441 error ("A cannot be an empty matrix");
445 if (! isreal (a) || nxa != nx)
446 error ("A must be a real valued %d by %d matrix", nc, nx);
453 error ("B cannot be an empty vector");
456 if (! isreal (b) || length (b) != nc)
457 error ("B must be a real valued %d by 1 vector", nc);
461 ## 4) Vector with the lower bound of each variable
466 elseif (! isreal (lb) || all (size (lb) > 1) || length (lb) != nx)
467 error ("LB must be a real valued %d by 1 column vector", nx);
474 ## 5) Vector with the upper bound of each variable
478 ub = repmat (Inf, nx, 1);
479 elseif (! isreal (ub) || all (size (ub) > 1) || length (ub) != nx)
480 error ("UB must be a real valued %d by 1 column vector", nx);
484 ub = repmat (Inf, nx, 1);
487 ## 6) Sense of each constraint
491 ctype = repmat ("S", nc, 1);
492 elseif (! ischar (ctype) || all (size (ctype) > 1) || length (ctype) != nc)
493 error ("CTYPE must be a char valued vector of length %d", nc);
495 elseif (! all (ctype == "F" | ctype == "U" | ctype == "S"
496 | ctype == "L" | ctype == "D"))
497 error ("CTYPE must contain only F, U, S, L, or D");
501 ctype = repmat ("S", nc, 1);
504 ## 7) Vector with the type of variables
507 if (isempty (vartype))
508 vartype = repmat ("C", nx, 1);
509 elseif (! ischar (vartype) || all (size (vartype) > 1)
510 || length (vartype) != nx)
511 error ("VARTYPE must be a char valued vector of length %d", nx);
513 elseif (! all (vartype == "C" | vartype == "I"))
514 error ("VARTYPE must contain only C or I");
518 ## As default we consider continuous vars
519 vartype = repmat ("C", nx, 1);
522 ## 8) Sense of optimization
527 elseif (ischar (sense) || all (size (sense) > 1) || ! isreal (sense))
528 error ("SENSE must be an integer value");
538 ## 9) Parameters vector
541 if (! isstruct (param))
542 error ("PARAM must be a structure");
549 [xopt, fmin, status, extra] = ...
550 __glpk__ (c, a, b, lb, ub, ctype, vartype, sense, param);