### 25.1 Linear Programming

Octave can solve Linear Programming problems using the `glpk` function. That is, Octave can solve

    min C'*x

subject to the linear constraints A*x = b where x ≥ 0.

The `glpk` function also supports variations of this problem.

Function File: [xopt, fmin, errnum, extra] = glpk (c, A, b, lb, ub, ctype, vartype, sense, param)

  Solve a linear program using the GNU GLPK library. Given three arguments, `glpk` solves the following standard LP:

      min C'*x

  subject to

      A*x  = b
        x >= 0

  but may also solve problems of the form

      [ min | max ] C'*x

  subject to

      A*x [ "=" | "<=" | ">=" ] b
        x >= LB
        x <= UB

  Input arguments:

  c

    A column array containing the objective function coefficients.

  A

    A matrix containing the constraints coefficients.

  b

    A column array containing the right-hand side value for each constraint in the constraint matrix.

  lb

    An array containing the lower bound on each of the variables. If lb is not supplied, the default lower bound for the variables is zero.

  ub

    An array containing the upper bound on each of the variables. If ub is not supplied, the default upper bound is assumed to be infinite.

  ctype

    An array of characters containing the sense of each constraint in the constraint matrix. Each element of the array may be one of the following values

    `"F"`

      A free (unbounded) constraint (the constraint is ignored).

    `"U"`

      An inequality constraint with an upper bound (`A(i,:)*x <= b(i)`).

    `"S"`

      An equality constraint (`A(i,:)*x = b(i)`).

    `"L"`

      An inequality with a lower bound (`A(i,:)*x >= b(i)`).

    `"D"`

      An inequality constraint with both upper and lower bounds (`A(i,:)*x >= -b(i)`) and (`A(i,:)*x <= b(i)`).

  vartype

    A column array containing the types of the variables.

    `"C"`

      A continuous variable.

    `"I"`

      An integer variable.

  sense

    If sense is 1, the problem is a minimization. If sense is -1, the problem is a maximization. The default value is 1.

  param

    A structure containing the following parameters used to define the behavior of solver. Missing elements in the structure take on default values, so you only need to set the elements that you wish to change from the default.

    Integer parameters:

    `msglev (default: 1)`

      Level of messages output by solver routines:

      0 (`GLP_MSG_OFF`)

        No output.

      1 (`GLP_MSG_ERR`)

        Error and warning messages only.

      2 (`GLP_MSG_ON`)

        Normal output.

      3 (`GLP_MSG_ALL`)

        Full output (includes informational messages).

    `scale (default: 16)`

      Scaling option. The values can be combined with the bitwise OR operator and may be the following:

      1 (`GLP_SF_GM`)

        Geometric mean scaling.

      16 (`GLP_SF_EQ`)

        Equilibration scaling.

      32 (`GLP_SF_2N`)

        Round scale factors to power of two.

      64 (`GLP_SF_SKIP`)

        Skip if problem is well scaled.

      Alternatively, a value of 128 (`GLP_SF_AUTO`) may be also specified, in which case the routine chooses the scaling options automatically.

    `dual (default: 1)`

      Simplex method option:

      1 (`GLP_PRIMAL`)

        Use two-phase primal simplex.

      2 (`GLP_DUALP`)

        Use two-phase dual simplex, and if it fails, switch to the primal simplex.

      3 (`GLP_DUAL`)

        Use two-phase dual simplex.

    `price (default: 34)`

      Pricing option (for both primal and dual simplex):

      17 (`GLP_PT_STD`)

        Textbook pricing.

      34 (`GLP_PT_PSE`)

        Steepest edge pricing.

    `itlim (default: intmax)`

      Simplex iterations limit. It is decreased by one each time when one simplex iteration has been performed, and reaching zero value signals the solver to stop the search.

    `outfrq (default: 200)`

      Output frequency, in iterations. This parameter specifies how frequently the solver sends information about the solution to the standard output.

    `branch (default: 4)`

      Branching technique option (for MIP only):

      1 (`GLP_BR_FFV`)

        First fractional variable.

      2 (`GLP_BR_LFV`)

        Last fractional variable.

      3 (`GLP_BR_MFV`)

        Most fractional variable.

      4 (`GLP_BR_DTH`)

        Heuristic by Driebeck and Tomlin.

      5 (`GLP_BR_PCH`)

        Hybrid pseudocost heuristic.

    `btrack (default: 4)`

      Backtracking technique option (for MIP only):

      1 (`GLP_BT_DFS`)

        Depth first search.

      2 (`GLP_BT_BFS`)

        Breadth first search.

      3 (`GLP_BT_BLB`)

        Best local bound.

      4 (`GLP_BT_BPH`)

        Best projection heuristic.

    `presol (default: 1)`

      If this flag is set, the simplex solver uses the built-in LP presolver. Otherwise the LP presolver is not used.

    `lpsolver (default: 1)`

      Select which solver to use. If the problem is a MIP problem this flag will be ignored.

      1

        Revised simplex method.

      2

        Interior point method.

    `rtest (default: 34)`

      Ratio test technique:

      17 (`GLP_RT_STD`)

        Standard ("textbook").

      34 (`GLP_RT_HAR`)

        Harris’ two-pass ratio test.

    `tmlim (default: intmax)`

      Searching time limit, in milliseconds.

    `outdly (default: 0)`

      Output delay, in seconds. This parameter specifies how long the solver should delay sending information about the solution to the standard output.

    `save (default: 0)`

      If this parameter is nonzero, save a copy of the problem in CPLEX LP format to the file `"outpb.lp"`. There is currently no way to change the name of the output file.

    Real parameters:

    `tolbnd (default: 1e-7)`

      Relative tolerance used to check if the current basic solution is primal feasible. It is not recommended that you change this parameter unless you have a detailed understanding of its purpose.

    `toldj (default: 1e-7)`

      Absolute tolerance used to check if the current basic solution is dual feasible. It is not recommended that you change this parameter unless you have a detailed understanding of its purpose.

    `tolpiv (default: 1e-10)`

      Relative tolerance used to choose eligible pivotal elements of the simplex table. It is not recommended that you change this parameter unless you have a detailed understanding of its purpose.

    `objll (default: -DBL_MAX)`

      Lower limit of the objective function. If the objective function reaches this limit and continues decreasing, the solver stops the search. This parameter is used in the dual simplex method only.

    `objul (default: +DBL_MAX)`

      Upper limit of the objective function. If the objective function reaches this limit and continues increasing, the solver stops the search. This parameter is used in the dual simplex only.

    `tolint (default: 1e-5)`

      Relative tolerance used to check if the current basic solution is integer feasible. It is not recommended that you change this parameter unless you have a detailed understanding of its purpose.

    `tolobj (default: 1e-7)`

      Relative tolerance used to check if the value of the objective function is not better than in the best known integer feasible solution. It is not recommended that you change this parameter unless you have a detailed understanding of its purpose.

  Output values:

  xopt

    The optimizer (the value of the decision variables at the optimum).

  fopt

    The optimum value of the objective function.

  errnum

    Error code.

    0

      No error.

    1 (`GLP_EBADB`)

      Invalid basis.

    2 (`GLP_ESING`)

      Singular matrix.

    3 (`GLP_ECOND`)

      Ill-conditioned matrix.

    4 (`GLP_EBOUND`)

      Invalid bounds.

    5 (`GLP_EFAIL`)

      Solver failed.

    6 (`GLP_EOBJLL`)

      Objective function lower limit reached.

    7 (`GLP_EOBJUL`)

      Objective function upper limit reached.

    8 (`GLP_EITLIM`)

      Iterations limit exhausted.

    9 (`GLP_ETMLIM`)

      Time limit exhausted.

    10 (`GLP_ENOPFS`)

      No primal feasible solution.

    11 (`GLP_ENODFS`)

      No dual feasible solution.

    12 (`GLP_EROOT`)

      Root LP optimum not provided.

    13 (`GLP_ESTOP`)

      Search terminated by application.

    14 (`GLP_EMIPGAP`)

      Relative MIP gap tolerance reached.

    15 (`GLP_ENOFEAS`)

      No primal/dual feasible solution.

    16 (`GLP_ENOCVG`)

      No convergence.

    17 (`GLP_EINSTAB`)

      Numerical instability.

    18 (`GLP_EDATA`)

      Invalid data.

    19 (`GLP_ERANGE`)

      Result out of range.

  extra

    A data structure containing the following fields:

    `lambda`

      Dual variables.

    `redcosts`

      Reduced Costs.

    `time`

      Time (in seconds) used for solving LP/MIP problem.

    `status`

      Status of the optimization.

      1 (`GLP_UNDEF`)

        Solution status is undefined.

      2 (`GLP_FEAS`)

        Solution is feasible.

      3 (`GLP_INFEAS`)

        Solution is infeasible.

      4 (`GLP_NOFEAS`)

        Problem has no feasible solution.

      5 (`GLP_OPT`)

        Solution is optimal.

      6 (`GLP_UNBND`)

        Problem has no unbounded solution.

  Example:

      c = [10, 6, 4]';
      A = [ 1, 1, 1;
           10, 4, 5;
            2, 2, 6];
      b = [100, 600, 300]';
      lb = [0, 0, 0]';
      ub = [];
      ctype = "UUU";
      vartype = "CCC";
      s = -1;

      param.msglev = 1;
      param.itlim = 100;

      [xmin, fmin, status, extra] = ...
         glpk (c, A, b, lb, ub, ctype, vartype, s, param);