API Reference

class ilpy.Constraint

Bases: object

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

Constraint

get_coefficients()
Return type:

Mapping[int, float]

get_quadratic_coefficients()
Return type:

Mapping[tuple[int, int], float]

get_relation()
Return type:

Relation

get_value()
Return type:

float

is_violated(solution)
Return type:

bool

set_coefficient(i, value)
Return type:

None

set_quadratic_coefficient(i, j, value)
Return type:

None

set_relation(relation)
Return type:

None

set_value(value)
Return type:

None

class ilpy.Constraints

Bases: object

__init__()
add(constraint)
Return type:

None

add_all(constraints)
Return type:

None

clear()
Return type:

None

class ilpy.Expression

Bases: expr

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)

Create a linear objective from this expression.

Return type:

Objective

class ilpy.Objective(size=0)

Bases: object

__init__(size=0)
classmethod from_coefficients(coefficients=(), quadratic_coefficients=(), constant=0, sense=Sense.Minimize)
Return type:

Objective

get_coefficients()
Return type:

list[float]

get_constant()
Return type:

float

get_quadratic_coefficients()
Return type:

Mapping[tuple[int, int], float]

get_sense()
Return type:

Sense

resize(size)

Resize the objective function. New coefficients are set to 0.

Return type:

None

set_coefficient(i, value)
Return type:

None

set_constant(value)
Return type:

None

set_quadratic_coefficient(i, j, value)
Return type:

None

set_sense(sense)
Return type:

None

class ilpy.Preference(*values)

Bases: IntEnum

Preference for a solver backend.

Any = 1
Gurobi = 3
Scip = 2
class ilpy.Relation(*values)

Bases: IntEnum

Equal = 2
GreaterEqual = 3
LessEqual = 1
class ilpy.Sense(*values)

Bases: IntEnum

Maximize = 2
Minimize = 1
class ilpy.Solution(variable_values, objective_value, status, time, native_status=None)

Bases: object

__init__(variable_values, objective_value, status, time, native_status=None)
get_status()
Return type:

str

get_value()
Return type:

float

native_status: Any = None
objective_value: float
status: SolverStatus
time: float
variable_values: Sequence[float]
class ilpy.Solver(num_variables, default_variable_type, variable_types=None, preference=Preference.Any)

Bases: object

__init__(num_variables, default_variable_type, variable_types=None, preference=Preference.Any)
add_constraint(constraint)
Return type:

None

native_model()
Return type:

Any

set_constraints(constraints)
Return type:

None

set_event_callback(callback)
Return type:

None

set_num_threads(num_threads)
Return type:

None

set_objective(objective)
Return type:

None

set_optimality_gap(gap, absolute=False)
Return type:

None

set_timeout(timeout)
Return type:

None

set_verbose(verbose)
Return type:

None

solve()
Return type:

Solution

class ilpy.SolverBackend

Bases: ABC

__init__()
_abc_impl = <_abc._abc_data object>
abstractmethod add_constraint(constraint)
Return type:

None

emit_event_data(data)
Return type:

None

abstractmethod initialize(num_variables, default_variable_type, variable_types)
Return type:

None

abstractmethod native_model()
Return type:

Any

abstractmethod set_constraints(constraints)
Return type:

None

set_event_callback(callback)
Return type:

None

abstractmethod set_num_threads(num_threads)
Return type:

None

abstractmethod set_objective(objective)
Return type:

None

abstractmethod set_optimality_gap(gap, absolute)
Return type:

None

abstractmethod set_timeout(timeout)
Return type:

None

abstractmethod set_verbose(verbose)
Return type:

None

abstractmethod solve()
Return type:

Solution

class ilpy.SolverStatus(*values)

Bases: Enum

INFEASIBLE = 'infeasible'
INF_OR_UNBOUNDED = 'infeasible_or_unbounded'
NODELIMIT = 'node_limit'
NUMERIC = 'numeric_issue'
OPTIMAL = 'optimal'
OTHER = 'other'
SOLUTIONLIMIT = 'solution_limit'
SUBOPTIMAL = 'suboptimal'
TIMELIMIT = 'time_limit'
UNBOUNDED = 'unbounded'
UNKNOWN = 'unknown'
USERINTERRUPT = 'user_interrupt'
class ilpy.Variable(id, index=None)

Bases: Expression, Name

A variable.

id holds the index as a string (because 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(*values)

Bases: IntEnum

Binary = 3
Continuous = 1
Integer = 2
ilpy.solve(objective, constraints, sense=Sense.Minimize, variable_type=VariableType.Continuous, verbose=False, preference=Preference.Any, 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:

Solution

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

    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
    

Returns:

The solution to the problem.

Return type:

Solution