changelog shortlog tags changeset files revisions annotate raw

scripts/optimization/glpk.m

changeset 10289: 4b124317dc38
parent:16f53d29049f
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (59 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2005, 2006, 2007, 2008, 2009 Nicolo' Giorgetti
2##
3## This file is part of Octave.
4##
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.
9##
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.
14##
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/>.
18
19## -*- texinfo -*-
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:
23##
24## @tex
25## $$
26## \min_x C^T x
27## $$
28## @end tex
29## @ifnottex
30## @example
31## min C'*x
32## @end example
33## @end ifnottex
34##
35## subject to
36##
37## @tex
38## $$
39## Ax = b \qquad x \geq 0
40## $$
41## @end tex
42## @ifnottex
43## @example
44## @group
45## A*x = b
46## x >= 0
47## @end group
48## @end example
49## @end ifnottex
50##
51## but may also solve problems of the form
52##
53## @tex
54## $$
55## [ \min_x | \max_x ] C^T x
56## $$
57## @end tex
58## @ifnottex
59## @example
60## [ min | max ] C'*x
61## @end example
62## @end ifnottex
63##
64## subject to
65##
66## @tex
67## $$
68## Ax [ = | \leq | \geq ] b \qquad LB \leq x \leq UB
69## $$
70## @end tex
71## @ifnottex
72## @example
73## @group
74## A*x [ "=" | "<=" | ">=" ] b
75## x >= LB
76## x <= UB
77## @end group
78## @end example
79## @end ifnottex
80##
81## Input arguments:
82##
83## @table @var
84## @item c
85## A column array containing the objective function coefficients.
86##
87## @item a
88## A matrix containing the constraints coefficients.
89##
90## @item b
91## A column array containing the right-hand side value for each constraint
92## in the constraint matrix.
93##
94## @item lb
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
97## zero.
98##
99## @item ub
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
102## infinite.
103##
104## @item ctype
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
107## following values
108## @table @code
109## @item "F"
110## A free (unbounded) constraint (the constraint is ignored).
111## @item "U"
112## An inequality constraint with an upper bound (@code{A(i,:)*x <= b(i)}).
113## @item "S"
114## An equality constraint (@code{A(i,:)*x = b(i)}).
115## @item "L"
116## An inequality with a lower bound (@code{A(i,:)*x >= b(i)}).
117## @item "D"
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)}).
120## @end table
121##
122## @item vartype
123## A column array containing the types of the variables.
124## @table @code
125## @item "C"
126## A continuous variable.
127## @item "I"
128## An integer variable.
129## @end table
130##
131## @item sense
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.
134##
135## @item param
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
139## from the default.
140##
141## Integer parameters:
142##
143## @table @code
144## @item msglev (@w{@code{LPX_K_MSGLEV}}, default: 1)
145## Level of messages output by solver routines:
146## @table @asis
147## @item 0
148## No output.
149## @item 1
150## Error messages only.
151## @item 2
152## Normal output .
153## @item 3
154## Full output (includes informational messages).
155## @end table
156##
157## @item scale (@w{@code{LPX_K_SCALE}}, default: 1)
158## Scaling option:
159## @table @asis
160## @item 0
161## No scaling.
162## @item 1
163## Equilibration scaling.
164## @item 2
165## Geometric mean scaling, then equilibration scaling.
166## @end table
167##
168## @item dual (@w{@code{LPX_K_DUAL}}, default: 0)
169## Dual simplex option:
170## @table @asis
171## @item 0
172## Do not use the dual simplex.
173## @item 1
174## If initial basic solution is dual feasible, use the dual simplex.
175## @end table
176##
177## @item price (@w{@code{LPX_K_PRICE}}, default: 1)
178## Pricing option (for both primal and dual simplex):
179## @table @asis
180## @item 0
181## Textbook pricing.
182## @item 1
183## Steepest edge pricing.
184## @end table
185##
186## @item round (@w{@code{LPX_K_ROUND}}, default: 0)
187## Solution rounding option:
188## @table @asis
189## @item 0
190## Report all primal and dual values "as is".
191## @item 1
192## Replace tiny primal and dual values by exact zero.
193## @end table
194##
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.
200##
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
204## standard output.
205##
206## @item branch (@w{@code{LPX_K_BRANCH}}, default: 2)
207## Branching heuristic option (for MIP only):
208## @table @asis
209## @item 0
210## Branch on the first variable.
211## @item 1
212## Branch on the last variable.
213## @item 2
214## Branch using a heuristic by Driebeck and Tomlin.
215## @end table
216##
217## @item btrack (@w{@code{LPX_K_BTRACK}}, default: 2)
218## Backtracking heuristic option (for MIP only):
219## @table @asis
220## @item 0
221## Depth first search.
222## @item 1
223## Breadth first search.
224## @item 2
225## Backtrack using the best projection heuristic.
226## @end table
227##
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.
231##
232## @item lpsolver (default: 1)
233## Select which solver to use. If the problem is a MIP problem this flag
234## will be ignored.
235## @table @asis
236## @item 1
237## Revised simplex method.
238## @item 2
239## Interior point method.
240## @end table
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.
245## @end table
246##
247## Real parameters:
248##
249## @table @code
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}}.
259##
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.
264##
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.
269##
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.
274##
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.
279##
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.
284##
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.
290##
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.
295##
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.
300##
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.
306## @end table
307## @end table
308##
309## Output values:
310##
311## @table @var
312## @item xopt
313## The optimizer (the value of the decision variables at the optimum).
314## @item fopt
315## The optimum value of the objective function.
316## @item status
317## Status of the optimization.
318##
319## Simplex Method:
320## @table @asis
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.
333## @end table
334## Interior Point Method:
335## @table @asis
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.
340## @end table
341## Mixed Integer Method:
342## @table @asis
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.
351## @end table
352## @noindent
353## If an error occurs, @var{status} will contain one of the following
354## codes:
355##
356## @table @asis
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).
379## @end table
380## @item extra
381## A data structure containing the following fields:
382## @table @code
383## @item lambda
384## Dual variables.
385## @item redcosts
386## Reduced Costs.
387## @item time
388## Time (in seconds) used for solving LP/MIP problem.
389## @item mem
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).
392## @end table
393## @end table
394##
395## Example:
396##
397## @example
398## @group
399## c = [10, 6, 4]';
400## a = [ 1, 1, 1;
401## 10, 4, 5;
402## 2, 2, 6];
403## b = [100, 600, 300]';
404## lb = [0, 0, 0]';
405## ub = [];
406## ctype = "UUU";
407## vartype = "CCC";
408## s = -1;
409##
410## param.msglev = 1;
411## param.itlim = 100;
412##
413## [xmin, fmin, status, extra] = ...
414## glpk (c, a, b, lb, ub, ctype, vartype, s, param);
415## @end group
416## @end example
417## @end deftypefn
418
419## Author: Nicolo' Giorgetti <giorgetti@dii.unisi.it>
420## Adapted-by: jwe
421
422function [xopt, fmin, status, extra] = glpk (c, a, b, lb, ub, ctype, vartype, sense, param)
423
424 ## If there is no input output the version and syntax
425 if (nargin < 3 || nargin > 9)
426 print_usage ();
427 return;
428 endif
429
430 if (all (size (c) > 1) || iscomplex (c) || ischar (c))
431 error ("C must be a real vector");
432 return;
433 endif
434 nx = length (c);
435 ## Force column vector.
436 c = c(:);
437
438 ## 2) Matrix constraint
439
440 if (isempty (a))
441 error ("A cannot be an empty matrix");
442 return;
443 endif
444 [nc, nxa] = size(a);
445 if (! isreal (a) || nxa != nx)
446 error ("A must be a real valued %d by %d matrix", nc, nx);
447 return;
448 endif
449
450 ## 3) RHS
451
452 if (isempty (b))
453 error ("B cannot be an empty vector");
454 return;
455 endif
456 if (! isreal (b) || length (b) != nc)
457 error ("B must be a real valued %d by 1 vector", nc);
458 return;
459 endif
460
461 ## 4) Vector with the lower bound of each variable
462
463 if (nargin > 3)
464 if (isempty (lb))
465 lb = zeros (nx, 1);
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);
468 return;
469 endif
470 else
471 lb = zeros (nx, 1);
472 endif
473
474 ## 5) Vector with the upper bound of each variable
475
476 if (nargin > 4)
477 if (isempty (ub))
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);
481 return;
482 endif
483 else
484 ub = repmat (Inf, nx, 1);
485 endif
486
487 ## 6) Sense of each constraint
488
489 if (nargin > 5)
490 if (isempty (ctype))
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);
494 return;
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");
498 return;
499 endif
500 else
501 ctype = repmat ("S", nc, 1);
502 endif
503
504 ## 7) Vector with the type of variables
505
506 if (nargin > 6)
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);
512 return;
513 elseif (! all (vartype == "C" | vartype == "I"))
514 error ("VARTYPE must contain only C or I");
515 return;
516 endif
517 else
518 ## As default we consider continuous vars
519 vartype = repmat ("C", nx, 1);
520 endif
521
522 ## 8) Sense of optimization
523
524 if (nargin > 7)
525 if (isempty (sense))
526 sense = 1;
527 elseif (ischar (sense) || all (size (sense) > 1) || ! isreal (sense))
528 error ("SENSE must be an integer value");
529 elseif (sense >= 0)
530 sense = 1;
531 else
532 sense = -1;
533 endif
534 else
535 sense = 1;
536 endif
537
538 ## 9) Parameters vector
539
540 if (nargin > 8)
541 if (! isstruct (param))
542 error ("PARAM must be a structure");
543 return;
544 endif
545 else
546 param = struct ();
547 endif
548
549 [xopt, fmin, status, extra] = ...
550 __glpk__ (c, a, b, lb, ub, ctype, vartype, sense, param);
551
552endfunction