GLPK Backend

AUTHORS:

  • Nathann Cohen (2010-10): initial implementation
  • John Perry (2012-01): glp_simplex preprocessing
class sage.numerical.backends.glpk_backend.GLPKBackend

Bases: sage.numerical.backends.generic_backend.GenericBackend

x.__init__(...) initializes x; see help(type(x)) for signature

add_col(indices, coeffs)

Add a column.

INPUT:

  • indices (list of integers) – this list constains the indices of the constraints in which the variable’s coefficient is nonzero
  • coeffs (list of real values) – associates a coefficient to the variable in each of the constraints in which it appears. Namely, the ith entry of coeffs corresponds to the coefficient of the variable in the constraint represented by the ith entry in indices.

Note

indices and coeffs are expected to be of the same length.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.ncols()
0
sage: p.nrows()
0
sage: p.add_linear_constraints(5, 0, None)
sage: p.add_col(range(5), range(5))
sage: p.nrows()
5
add_linear_constraint(coefficients, lower_bound, upper_bound, name=None)

Add a linear constraint.

INPUT:

  • coefficients an iterable with (c,v) pairs where c is a variable index (integer) and v is a value (real value).
  • lower_bound - a lower bound, either a real value or None
  • upper_bound - an upper bound, either a real value or None
  • name - an optional name for this row (default: None)

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variables(5)
4
sage: p.add_linear_constraint( zip(range(5), range(5)), 2.0, 2.0)
sage: p.row(0)
([4, 3, 2, 1], [4.0, 3.0, 2.0, 1.0])
sage: p.row_bounds(0)
(2.0, 2.0)
sage: p.add_linear_constraint( zip(range(5), range(5)), 1.0, 1.0, name='foo')
sage: p.row_name(1)
'foo'
add_linear_constraints(number, lower_bound, upper_bound, names=None)

Add 'number linear constraints.

INPUT:

  • number (integer) – the number of constraints to add.
  • lower_bound - a lower bound, either a real value or None
  • upper_bound - an upper bound, either a real value or None
  • names - an optional list of names (default: None)

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variables(5)
4
sage: p.add_linear_constraints(5, None, 2)
sage: p.row(4)
([], [])
sage: p.row_bounds(4)
(None, 2.0)
sage: p.add_linear_constraints(2, None, 2, names=['foo','bar'])
add_variable(lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False, obj=0.0, name=None)

Add a variable.

This amounts to adding a new column to the matrix. By default, the variable is both positive, real and the coefficient in the objective function is 0.0.

INPUT:

  • lower_bound - the lower bound of the variable (default: 0)
  • upper_bound - the upper bound of the variable (default: None)
  • binary - True if the variable is binary (default: False).
  • continuous - True if the variable is binary (default: True).
  • integer - True if the variable is binary (default: False).
  • obj - (optional) coefficient of this variable in the objective function (default: 0.0)
  • name - an optional name for the newly added variable (default: None).

OUTPUT: The index of the newly created variable

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.ncols()
0
sage: p.add_variable()
0
sage: p.ncols()
1
sage: p.add_variable(binary=True)
1
sage: p.add_variable(lower_bound=-2.0, integer=True)
2
sage: p.add_variable(continuous=True, integer=True)
Traceback (most recent call last):
...
ValueError: ...            
sage: p.add_variable(name='x',obj=1.0)
3
sage: p.col_name(3)
'x'
sage: p.objective_coefficient(3)
1.0
add_variables(number, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False, obj=0.0, names=None)

Add number new variables.

This amounts to adding new columns to the matrix. By default, the variables are both positive, real and theor coefficient in the objective function is 0.0.

INPUT:

  • n - the number of new variables (must be > 0)
  • lower_bound - the lower bound of the variable (default: 0)
  • upper_bound - the upper bound of the variable (default: None)
  • binary - True if the variable is binary (default: False).
  • continuous - True if the variable is binary (default: True).
  • integer - True if the variable is binary (default: False).
  • obj - (optional) coefficient of all variables in the objective function (default: 0.0)
  • names - optional list of names (default: None)

OUTPUT: The index of the variable created last.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.ncols()
0
sage: p.add_variables(5)
4
sage: p.ncols()
5
sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b'])
6
col_bounds(index)

Return the bounds of a specific variable.

INPUT:

  • index (integer) – the variable’s id.

OUTPUT:

A pair (lower_bound, upper_bound). Each of them can be set to None if the variable is not bounded in the corresponding direction, and is a real value otherwise.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variable()
0
sage: p.col_bounds(0)
(0.0, None)
sage: p.variable_upper_bound(0, 5)
sage: p.col_bounds(0)
(0.0, 5.0)
col_name(index)

Return the index th col name

INPUT:

  • index (integer) – the col’s id

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variable(name='I am a variable')
0
sage: p.col_name(0)
'I am a variable'
copy()

Returns a copy of self.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = MixedIntegerLinearProgram(solver = "GLPK")
sage: b = p.new_variable()                         
sage: p.add_constraint(b[1] + b[2] <= 6)           
sage: p.set_objective(b[1] + b[2])                 
sage: copy(p).solve()                              
6.0
get_objective_value()

Returns the value of the objective function.

Note

Behaviour is undefined unless solve has been called before.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variables(2)
1
sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3)
sage: p.set_objective([2, 5])
sage: p.solve()
0
sage: p.get_objective_value()
7.5
sage: p.get_variable_value(0)
0.0
sage: p.get_variable_value(1)
1.5
get_variable_value(variable)

Returns the value of a variable given by the solver.

Note

Behaviour is undefined unless solve has been called before.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variables(2)
1
sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3)
sage: p.set_objective([2, 5])
sage: p.solve()
0
sage: p.get_objective_value()
7.5
sage: p.get_variable_value(0)
0.0
sage: p.get_variable_value(1)
1.5
is_maximization()

Test whether the problem is a maximization

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.is_maximization()
True
sage: p.set_sense(-1)
sage: p.is_maximization()
False
is_variable_binary(index)

Test whether the given variable is of binary type.

INPUT:

  • index (integer) – the variable’s id

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.ncols()
0
sage: p.add_variable()
0
sage: p.set_variable_type(0,0)
sage: p.is_variable_binary(0)
True
is_variable_continuous(index)

Test whether the given variable is of continuous/real type.

INPUT:

  • index (integer) – the variable’s id

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.ncols()
0
sage: p.add_variable()
0
sage: p.is_variable_continuous(0)
True
sage: p.set_variable_type(0,1)
sage: p.is_variable_continuous(0)
False
is_variable_integer(index)

Test whether the given variable is of integer type.

INPUT:

  • index (integer) – the variable’s id

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.ncols()
0
sage: p.add_variable()
0
sage: p.set_variable_type(0,1)
sage: p.is_variable_integer(0)
True
ncols()

Return the number of columns/variables.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.ncols()
0
sage: p.add_variables(2)
1
sage: p.ncols()
2
nrows()

Return the number of rows/constraints.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.nrows()
0
sage: p.add_linear_constraints(2, 2, None)
sage: p.nrows()
2
objective_coefficient(variable, coeff=None)

Set or get the coefficient of a variable in the objective function

INPUT:

  • variable (integer) – the variable’s id
  • coeff (double) – its coefficient or None for reading (default: None)

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variable()
0
sage: p.objective_coefficient(0)
0.0
sage: p.objective_coefficient(0,2)
sage: p.objective_coefficient(0)
2.0
problem_name(name=' NULL')

Return or define the problem’s name

INPUT:

  • name (char *) – the problem’s name. When set to NULL (default), the method returns the problem’s name.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.problem_name("There once was a french fry")
sage: print p.problem_name()
There once was a french fry
row(index)

Return a row

INPUT:

  • index (integer) – the constraint’s id.

OUTPUT:

A pair (indices, coeffs) where indices lists the entries whose coefficient is nonzero, and to which coeffs associates their coefficient on the model of the add_linear_constraint method.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variables(5)
4
sage: p.add_linear_constraint(zip(range(5), range(5)), 2, 2)
sage: p.row(0)
([4, 3, 2, 1], [4.0, 3.0, 2.0, 1.0])
sage: p.row_bounds(0)
(2.0, 2.0)
row_bounds(index)

Return the bounds of a specific constraint.

INPUT:

  • index (integer) – the constraint’s id.

OUTPUT:

A pair (lower_bound, upper_bound). Each of them can be set to None if the constraint is not bounded in the corresponding direction, and is a real value otherwise.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variables(5)
4
sage: p.add_linear_constraint(zip(range(5), range(5)), 2, 2)
sage: p.row(0)
([4, 3, 2, 1], [4.0, 3.0, 2.0, 1.0])
sage: p.row_bounds(0)
(2.0, 2.0)
row_name(index)

Return the index th row name

INPUT:

  • index (integer) – the row’s id

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_linear_constraints(1, 2, None, names=['Empty constraint 1'])
sage: p.row_name(0)
'Empty constraint 1'
set_objective(coeff)

Set the objective function.

INPUT:

  • coeff - a list of real values, whose ith element is the coefficient of the ith variable in the objective function.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variables(5)
4
sage: p.set_objective([1, 1, 2, 1, 3])
sage: map(lambda x :p.objective_coefficient(x), range(5))
[1.0, 1.0, 2.0, 1.0, 3.0]
set_sense(sense)

Set the direction (maximization/minimization).

INPUT:

  • sense (integer) :

    • +1 => Maximization
    • -1 => Minimization

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.is_maximization()
True
sage: p.set_sense(-1)
sage: p.is_maximization()
False
set_variable_type(variable, vtype)

Set the type of a variable

INPUT:

  • variable (integer) – the variable’s id

  • vtype (integer) :

    • 1 Integer
    • 0 Binary
    • -1 Real

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.ncols()
0
sage: p.add_variable()
0
sage: p.set_variable_type(0,1)
sage: p.is_variable_integer(0)
True
set_verbosity(level)

Set the verbosity level

INPUT:

  • level (integer) – From 0 (no verbosity) to 3.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.set_verbosity(2)
solve()

Solve the problem.

Note

This method raises MIPSolverException exceptions when the solution can not be computed for any reason (none exists, or the LP solver was not able to find it, etc...)

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_linear_constraints(5, 0, None)
sage: p.add_col(range(5), range(5))
sage: p.solve()
0
sage: p.objective_coefficient(0,1)
sage: p.solve()
Traceback (most recent call last):
...
MIPSolverException: ...

Warning

Sage uses GLPK’s glp_intopt to find solutions. This routine sometimes FAILS CATASTROPHICALLY when given a system it cannot solve. (Ticket #12309.) Here, “catastrophic” can mean either “infinite loop” or segmentation fault. Upstream considers this behavior “essentially innate” to their design, and suggests preprocessing it with glpk_simplex first. Thus, if you suspect that your system is infeasible, set the preprocessing option first.

EXAMPLE:

sage: lp = MixedIntegerLinearProgram(solver = "GLPK")
sage: lp.add_constraint( lp[1] +lp[2] -2.0 *lp[3] , max =-1.0)
sage: lp.add_constraint( lp[0] -4.0/3 *lp[1] +1.0/3 *lp[2] , max =-1.0/3)
sage: lp.add_constraint( lp[0] +0.5 *lp[1] -0.5 *lp[2] +0.25 *lp[3] , max =-0.25)
sage: lp.solve()
0.0
sage: lp.add_constraint( lp[0] +4.0 *lp[1] -lp[2] +lp[3] , max =-1.0)
sage: lp.solve()
Traceback (most recent call last):
...
RuntimeError: GLPK : Signal sent, try preprocessing option
sage: lp.solver_parameter("preprocessing", True)
sage: lp.solve()
Traceback (most recent call last):
...
MIPSolverException: 'GLPK : Simplex cannot find a feasible solution'
solver_parameter(name, value=None)

Return or define a solver parameter

INPUT:

  • name (string) – the parameter
  • value – the parameter’s value if it is to be defined, or None (default) to obtain its current value.

Note

The list of available parameters is available at sage.numerical.mip.MixedIntegerlinearProgram.solver_parameter()

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.solver_parameter("timelimit", 60)             
sage: p.solver_parameter("timelimit")                 
60.0
variable_lower_bound(index, value=False)

Return or define the lower bound on a variable

INPUT:

  • index (integer) – the variable’s id
  • value – real value, or None to mean that the variable has not lower bound. When set to False (default), the method returns the current value.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variable()
0
sage: p.col_bounds(0)
(0.0, None)
sage: p.variable_lower_bound(0, 5)
sage: p.col_bounds(0)
(5.0, None)
variable_upper_bound(index, value=False)

Return or define the upper bound on a variable

INPUT:

  • index (integer) – the variable’s id
  • value – real value, or None to mean that the variable has not upper bound. When set to False (default), the method returns the current value.

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variable()
0
sage: p.col_bounds(0)
(0.0, None)
sage: p.variable_upper_bound(0, 5)
sage: p.col_bounds(0)
(0.0, 5.0)
write_lp(filename)

Write the problem to a .lp file

INPUT:

  • filename (string)

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variables(2)
1
sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3)
sage: p.set_objective([2, 5])
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp")
write_mps(filename, modern)

Write the problem to a .mps file

INPUT:

  • filename (string)

EXAMPLE:

sage: from sage.numerical.backends.generic_backend import get_solver
sage: p = get_solver(solver = "GLPK")
sage: p.add_variables(2)
1
sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3)
sage: p.set_objective([2, 5])
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp")

Previous topic

Generic Backend for LP solvers

Next topic

Probability

This Page