API Reference

class ilpy.Constraint

Bases: object

classmethod from_coefficients(coefficients=(), quadratic_coefficients=(), relation=<Relation.LessEqual: 0>, value=0.0)
Return type:

Constraint

get_coefficients()
get_quadratic_coefficients()
get_relation()
get_value()
is_violated(solution)
set_coefficient(i, value)
set_quadratic_coefficient(i, j, value)
set_relation(relation)
set_value(value)
class ilpy.Constraints

Bases: object

add(constraint)
add_all(constraints)
clear()
class ilpy.Expression

Bases: AST

Base class for all expression nodes.

Expressions allow ilpy to represent mathematical expressions in an intuitive syntax, and then convert to a native Constraint object.

This class provides all of the operators and methods needed to build expressions. For example, to create the expression 2 * x - y >= 0, you can write 2 * Variable('x') - Variable('y') >= 0.

Tip: you can use ast.dump to see the AST representation of an expression. Or, use print(expr) to see the string representation of an expression.

static _cast(obj)

Cast object into an Expression.

Return type:

Expression

as_constraint()

Create an ilpy.Constraint object from this expression.

Return type:

Constraint

as_objective(sense=<Sense.Minimize: 0>)

Create a linear objective from this expression.

Return type:

Objective

class ilpy.Objective

Bases: object

classmethod from_coefficients(coefficients=(), quadratic_coefficients=(), constant=0.0, sense=<Sense.Minimize: 0>)
Return type:

Objective

get_coefficients()
get_constant()
get_quadratic_coefficients()
get_sense()
resize(size)
set_coefficient(i, value)
set_constant(value)
set_quadratic_coefficient(i, j, value)
set_sense(sense)
class ilpy.Preference(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntFlag

Any = 0
Cplex = 1
Gurobi = 2
Scip = 3
_all_bits_ = 3
_boundary_ = 'keep'
_flag_mask_ = 3
_generate_next_value_(start, count, last_values)

Generate the next value when not given.

name: the name of the member start: the initial start value or None count: the number of existing members last_values: the last value assigned or None

_inverted_ = None
classmethod _iter_member_(value)

Extract all members from the value in definition order.

_singles_mask_ = 3
class ilpy.Relation(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntFlag

Equal = 1
GreaterEqual = 2
LessEqual = 0
_all_bits_ = 3
_boundary_ = 'keep'
_flag_mask_ = 3
_generate_next_value_(start, count, last_values)

Generate the next value when not given.

name: the name of the member start: the initial start value or None count: the number of existing members last_values: the last value assigned or None

_inverted_ = None
_singles_mask_ = 3
class ilpy.Sense(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntFlag

Maximize = 1
Minimize = 0
_all_bits_ = 1
_boundary_ = 'keep'
_flag_mask_ = 1
_generate_next_value_(start, count, last_values)

Generate the next value when not given.

name: the name of the member start: the initial start value or None count: the number of existing members last_values: the last value assigned or None

_inverted_ = None
_singles_mask_ = 1
class ilpy.Solution

Bases: object

get_status()
Return type:

str

get_value()
resize(size)
set_value(value)
class ilpy.Solver

Bases: object

add_constraint(constraint)
set_constraints(constraints)
set_event_callback(callback)
set_num_threads(num_threads)
set_objective(objective)
set_optimality_gap(gap, absolute=False)
set_timeout(timeout)
set_verbose(verbose)
solve()
class ilpy.Variable(id, index=None)

Bases: Expression, Name

A variable.

id holds the index as a string (becuase ast.Name requires a string).

The special attribute index is added here for the purpose of storing the index of a variable in a solver’s variable list: Variable('u', index=0)

__init__(id, index=None)
class ilpy.VariableType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntFlag

Binary = 2
Continuous = 0
Integer = 1
_all_bits_ = 3
_boundary_ = 'keep'
_flag_mask_ = 3
_generate_next_value_(start, count, last_values)

Generate the next value when not given.

name: the name of the member start: the initial start value or None count: the number of existing members last_values: the last value assigned or None

_inverted_ = None
_singles_mask_ = 3
ilpy.solve(objective, constraints, sense=<Sense.Minimize: 0>, variable_type=<VariableType.Continuous: 0>, verbose=False, preference=<Preference.Any: 0>, on_event=None)

Solve an objective subject to constraints.

This is a functional interface to the solver. It creates a solver instance and sets the objective and constraints, then solves the problem and returns the solution.

Return type:

list[float]

Parameters:
  • objective (Sequence[float] | Expression | Objective) – The objective to solve. If a sequence of floats is provided, it is interpreted as the coefficients of the objective. For example, the objective 2x + 3y would be provided as [2, 3]. Alternatively, an ilpy.Expression or ilpy.Objective can be provided.

  • constraints (Iterable[ConstraintTuple | Expression | Constraint]) – The constraints to satisfy. May be provided as a sequence of Expression or Constraint objects, or as a sequence of tuples of the form (coefficients, relation, value), where coefficients is a sequence of floats, relation is an ilpy.Relation or a string in {“<=”, “>=”, “=”}, and value is a float. For example, the constraint 2x + 3y <= 5 would be provided as ([2, 3], Relation.LessEqual, 5).

  • sense (Sense | Literal["minimize", "maximize"]) – The sense of the objective, either Sense.Minimize or Sense.Maximize. Alternatively, a string in {“minimize”, “maximize”} can be provided. By default, Sense.Minimize.

  • variable_type (VariableType | Literal["continuous", "binary", "integer"]) – The type of the variables, either an ilpy.VariableType, or a string in {“continuous”, “binary”, “integer”}. By default, VariableType.Continuous.

  • verbose (bool, optional) – Whether to print the solver output, by default False.

  • preference (Preference | Literal["any", "cplex", "gurobi", "scip"]) – Backend preference, either an ilpy.Preference or a string in {“any”, “cplex”, “gurobi”, “scip”}. By default, Preference.Any.

  • on_event (Callable[[Mapping], None], optional) –

    A callback function that is called when an event occurs, by default None. The callback function should accept a dict which will contain statics about the solving or presolving process. You can import ilpy.EventData from ilpy and use it to provide dict key hints in your IDE, but EventData is not available at runtime. See SCIP and Gurobi documentation for details what each value means.

    For example:

    import ilpy
    
    if TYPE_CHECKING:
        from ilpy import EventData
    
    def callback(data: EventData) -> None:
        # backend and event_type are guaranteed to be present
        # they will narrow down the available keys
        if data["backend"] == "gurobi":
            if data["event_type"] == "MIP":
                print(data["gap"])
    
    ilpy.solve(..., on_event=callback)
    

Returns:

The solution to the objective.

Return type:

list[float]