API Reference
Unified Python API for Integer Linear Programming (ILP) solvers.
ilpy provides a consistent interface that abstracts over different ILP
backends (currently Gurobi and SCIP), and lets you build problems either
imperatively (via Objective / Constraint) or as natural Python
expressions (via Variable and Expression).
Classes:
| Name | Description |
|---|---|
Constraint |
A linear (or quadratic) constraint of the form |
Constraints |
An ordered collection of |
Expression |
Base class for all expression nodes. |
Objective |
A linear (or quadratic) objective function to minimize or maximize. |
Preference |
Preference for a solver backend. |
Relation |
Relation used in a linear constraint. |
Sense |
Direction of an objective function. |
Solution |
The result of solving an optimization problem. |
Solver |
High-level wrapper around an ILP solver backend. |
SolverBackend |
Abstract base class implemented by each concrete solver backend. |
SolverStatus |
Normalized solver status across backends. |
Variable |
A variable. |
VariableType |
Type of a decision variable in an optimization problem. |
Functions:
| Name | Description |
|---|---|
solve |
Solve an objective subject to constraints. |
Constraint
A linear (or quadratic) constraint of the form Ax [<=|=|>=] b.
Methods:
| Name | Description |
|---|---|
__init__ |
Create an empty |
from_coefficients |
Build a |
get_coefficients |
Return the linear coefficients as a mapping from variable index to value. |
get_quadratic_coefficients |
Return the quadratic coefficients, keyed by variable-index pairs. |
get_relation |
Return the relation of this constraint. |
get_value |
Return the right-hand-side value of this constraint. |
is_violated |
Return True if |
set_coefficient |
Set the linear coefficient of variable |
set_quadratic_coefficient |
Set the quadratic coefficient for the term |
set_relation |
Set the relation ( |
set_value |
Set the right-hand-side value of this constraint. |
Source code in src/ilpy/_components.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
__init__()
Create an empty <= 0 constraint with no coefficients.
Source code in src/ilpy/_components.py
22 23 24 25 26 27 | |
from_coefficients(coefficients=(), quadratic_coefficients=(), relation=Relation.LessEqual, value=0)
classmethod
Build a Constraint from coefficients, a relation, and a value.
Source code in src/ilpy/_components.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
get_coefficients()
Return the linear coefficients as a mapping from variable index to value.
Source code in src/ilpy/_components.py
36 37 38 | |
get_quadratic_coefficients()
Return the quadratic coefficients, keyed by variable-index pairs.
Source code in src/ilpy/_components.py
50 51 52 | |
get_relation()
Return the relation of this constraint.
Source code in src/ilpy/_components.py
58 59 60 | |
get_value()
Return the right-hand-side value of this constraint.
Source code in src/ilpy/_components.py
66 67 68 | |
is_violated(solution)
Return True if solution violates this constraint.
Source code in src/ilpy/_components.py
70 71 72 73 74 75 76 77 78 79 | |
set_coefficient(i, value)
Set the linear coefficient of variable i.
Source code in src/ilpy/_components.py
29 30 31 32 33 34 | |
set_quadratic_coefficient(i, j, value)
Set the quadratic coefficient for the term x_i * x_j.
Source code in src/ilpy/_components.py
40 41 42 43 44 45 46 47 48 | |
set_relation(relation)
Set the relation (<=, =, >=) used by this constraint.
Source code in src/ilpy/_components.py
54 55 56 | |
set_value(value)
Set the right-hand-side value of this constraint.
Source code in src/ilpy/_components.py
62 63 64 | |
Constraints
An ordered collection of Constraint objects.
Methods:
| Name | Description |
|---|---|
__init__ |
Create an empty collection of constraints. |
add |
Append a |
add_all |
Append every constraint from another |
clear |
Remove all constraints from this collection. |
Source code in src/ilpy/_components.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
__init__()
Create an empty collection of constraints.
Source code in src/ilpy/_components.py
114 115 116 | |
add(constraint)
Append a Constraint (or an Expression convertible to one).
Source code in src/ilpy/_components.py
122 123 124 125 126 127 | |
add_all(constraints)
Append every constraint from another Constraints instance.
Source code in src/ilpy/_components.py
129 130 131 | |
clear()
Remove all constraints from this collection.
Source code in src/ilpy/_components.py
118 119 120 | |
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.
Methods:
| Name | Description |
|---|---|
__str__ |
Serialize this expression to string form. |
as_constraint |
Create an ilpy.Constraint object from this expression. |
as_objective |
Create a linear objective from this expression. |
Source code in src/ilpy/expressions.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
__str__()
Serialize this expression to string form.
Source code in src/ilpy/expressions.py
84 85 86 | |
as_constraint()
Create an ilpy.Constraint object from this expression.
Source code in src/ilpy/expressions.py
52 53 54 55 56 57 58 59 60 61 62 | |
as_objective(sense=Sense.Minimize)
Create a linear objective from this expression.
Source code in src/ilpy/expressions.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
Objective
A linear (or quadratic) objective function to minimize or maximize.
Methods:
| Name | Description |
|---|---|
__init__ |
Create an empty minimizing objective with |
from_coefficients |
Build an |
get_coefficients |
Return a copy of the linear coefficients. |
get_constant |
Return the constant term of the objective. |
get_quadratic_coefficients |
Return the quadratic coefficients, keyed by variable-index pairs. |
get_sense |
Return the sense of this objective. |
resize |
Resize the objective function. New coefficients are set to 0. |
set_coefficient |
Set the linear coefficient of variable |
set_constant |
Set the constant term added to the objective. |
set_quadratic_coefficient |
Set the quadratic coefficient for the term |
set_sense |
Set the sense ( |
Source code in src/ilpy/_components.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
__init__(size=0)
Create an empty minimizing objective with size zero coefficients.
Source code in src/ilpy/_components.py
143 144 145 146 147 148 | |
from_coefficients(coefficients=(), quadratic_coefficients=(), constant=0, sense=Sense.Minimize)
classmethod
Build an Objective from coefficients, a constant, and a sense.
Source code in src/ilpy/_components.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
get_coefficients()
Return a copy of the linear coefficients.
Source code in src/ilpy/_components.py
175 176 177 | |
get_constant()
Return the constant term of the objective.
Source code in src/ilpy/_components.py
154 155 156 | |
get_quadratic_coefficients()
Return the quadratic coefficients, keyed by variable-index pairs.
Source code in src/ilpy/_components.py
194 195 196 | |
get_sense()
Return the sense of this objective.
Source code in src/ilpy/_components.py
202 203 204 | |
resize(size)
Resize the objective function. New coefficients are set to 0.
Source code in src/ilpy/_components.py
158 159 160 161 162 163 164 165 166 | |
set_coefficient(i, value)
Set the linear coefficient of variable i, growing storage as needed.
Source code in src/ilpy/_components.py
168 169 170 171 172 173 | |
set_constant(value)
Set the constant term added to the objective.
Source code in src/ilpy/_components.py
150 151 152 | |
set_quadratic_coefficient(i, j, value)
Set the quadratic coefficient for the term x_i * x_j.
Source code in src/ilpy/_components.py
182 183 184 185 186 187 188 189 190 191 192 | |
set_sense(sense)
Set the sense (Sense.Minimize or Sense.Maximize).
Source code in src/ilpy/_components.py
198 199 200 | |
Preference
Bases: IntEnum
Preference for a solver backend.
A "full" Gurobi license means one that is not the size-limited license
bundled with the gurobipy pip wheel. See
https://support.gurobi.com/hc/en-us/articles/360051597492
Any: Use Gurobi if a full license is available, otherwise fall back to SCIP. The bundled size-limited license is treated as "no license" to avoid silent "Model too large" failures on problems with >2000 variables.Scip: Use SCIP. Raises ifpyscipoptis not installed.Gurobi: Use Gurobi; requires a full license. Raises otherwise. UseGurobiRestrictedif you only have the bundled pip license.GurobiRestricted: Use Gurobi with whatever license resolves (including the bundled size-limited pip license). Suitable for small problems (<2000 variables); larger ones will fail at solve time.
Source code in src/ilpy/solver_backends/__init__.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
Relation
Bases: IntEnum
Relation used in a linear constraint.
Attributes:
| Name | Type | Description |
|---|---|---|
Equal |
Left-hand side equals the right-hand side. |
|
GreaterEqual |
Left-hand side is greater than or equal to the right-hand side. |
|
LessEqual |
Left-hand side is less than or equal to the right-hand side. |
Source code in src/ilpy/_constants.py
26 27 28 29 30 31 32 33 34 | |
Equal = auto()
class-attribute
instance-attribute
Left-hand side equals the right-hand side.
GreaterEqual = auto()
class-attribute
instance-attribute
Left-hand side is greater than or equal to the right-hand side.
LessEqual = auto()
class-attribute
instance-attribute
Left-hand side is less than or equal to the right-hand side.
Sense
Bases: IntEnum
Direction of an objective function.
Attributes:
| Name | Type | Description |
|---|---|---|
Maximize |
Maximize the objective. |
|
Minimize |
Minimize the objective. |
Source code in src/ilpy/_constants.py
17 18 19 20 21 22 23 | |
Maximize = auto()
class-attribute
instance-attribute
Maximize the objective.
Minimize = auto()
class-attribute
instance-attribute
Minimize the objective.
Solution
dataclass
The result of solving an optimization problem.
Attributes:
| Name | Type | Description |
|---|---|---|
variable_values |
Sequence[float]
|
The values assigned to each variable by the solver. |
objective_value |
float
|
The value of the objective at the returned solution. |
status |
SolverStatus
|
Normalized status reported by the solver. |
time |
float
|
Wall-clock time (seconds) spent solving. |
native_status |
Any
|
The backend-specific status object, for callers who need more detail
than |
Methods:
| Name | Description |
|---|---|
get_status |
Return the solver status as a string (the enum member name). |
get_value |
Return the objective value at this solution. |
Source code in src/ilpy/_solver.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
get_status()
Return the solver status as a string (the enum member name).
Source code in src/ilpy/_solver.py
65 66 67 | |
get_value()
Return the objective value at this solution.
Source code in src/ilpy/_solver.py
55 56 57 | |
Solver
High-level wrapper around an ILP solver backend.
Methods:
| Name | Description |
|---|---|
__init__ |
Create a solver with |
add_constraint |
Add a single constraint (or an |
native_model |
Return the backend's native model object (e.g. a gurobipy Model). |
set_constraints |
Replace the current constraint set. |
set_event_callback |
Set (or clear) a callback invoked on backend progress events. |
set_num_threads |
Set the number of threads the backend may use. |
set_objective |
Set the objective, converting from an |
set_optimality_gap |
Set the optimality gap at which the solver may stop. |
set_timeout |
Set a wall-clock time limit (in seconds) for solving. |
set_verbose |
Enable or disable solver log output. |
solve |
Solve the problem and return a |
Source code in src/ilpy/_solver.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
__init__(num_variables, default_variable_type, variable_types=None, preference=Preference.Any)
Create a solver with num_variables decision variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_variables
|
int
|
The number of decision variables in the problem. |
required |
default_variable_type
|
VariableType
|
The type used for variables not listed in |
required |
variable_types
|
dict[int, VariableType]
|
Per-variable overrides for the default variable type. |
None
|
preference
|
Preference
|
Backend preference. |
Any
|
Source code in src/ilpy/_solver.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
add_constraint(constraint)
Add a single constraint (or an Expression convertible to one).
Source code in src/ilpy/_solver.py
108 109 110 111 112 | |
native_model()
Return the backend's native model object (e.g. a gurobipy Model).
Source code in src/ilpy/_solver.py
142 143 144 | |
set_constraints(constraints)
Replace the current constraint set.
Source code in src/ilpy/_solver.py
104 105 106 | |
set_event_callback(callback)
Set (or clear) a callback invoked on backend progress events.
Source code in src/ilpy/_solver.py
134 135 136 | |
set_num_threads(num_threads)
Set the number of threads the backend may use.
Source code in src/ilpy/_solver.py
126 127 128 | |
set_objective(objective)
Set the objective, converting from an Expression if needed.
Source code in src/ilpy/_solver.py
98 99 100 101 102 | |
set_optimality_gap(gap, absolute=False)
Set the optimality gap at which the solver may stop.
If absolute is True, gap is the absolute gap; otherwise it is
interpreted as a relative gap.
Source code in src/ilpy/_solver.py
118 119 120 121 122 123 124 | |
set_timeout(timeout)
Set a wall-clock time limit (in seconds) for solving.
Source code in src/ilpy/_solver.py
114 115 116 | |
set_verbose(verbose)
Enable or disable solver log output.
Source code in src/ilpy/_solver.py
130 131 132 | |
solve()
Solve the problem and return a Solution.
Source code in src/ilpy/_solver.py
138 139 140 | |
SolverBackend
Bases: ABC
Abstract base class implemented by each concrete solver backend.
Methods:
| Name | Description |
|---|---|
add_constraint |
Add a single constraint to the problem. |
emit_event_data |
Dispatch |
initialize |
Initialize the backend with decision variables and their types. |
native_model |
Return the underlying native model object for this backend. |
set_constraints |
Replace the backend's current constraint set. |
set_event_callback |
Set (or clear) a callback invoked on solver progress events. |
set_num_threads |
Set the number of threads the backend may use. |
set_objective |
Set the objective function for the problem. |
set_optimality_gap |
Set the optimality gap (absolute or relative) for early termination. |
set_timeout |
Set the wall-clock time limit (in seconds) for solving. |
set_verbose |
Enable or disable backend log output. |
solve |
Solve the problem and return a |
Source code in src/ilpy/solver_backends/_base.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
add_constraint(constraint)
abstractmethod
Add a single constraint to the problem.
Source code in src/ilpy/solver_backends/_base.py
47 48 49 | |
emit_event_data(data)
Dispatch data to the registered event callback (no-op if none).
Source code in src/ilpy/solver_backends/_base.py
25 26 27 28 | |
initialize(num_variables, default_variable_type, variable_types)
abstractmethod
Initialize the backend with decision variables and their types.
Source code in src/ilpy/solver_backends/_base.py
30 31 32 33 34 35 36 37 | |
native_model()
abstractmethod
Return the underlying native model object for this backend.
Source code in src/ilpy/solver_backends/_base.py
71 72 73 | |
set_constraints(constraints)
abstractmethod
Replace the backend's current constraint set.
Source code in src/ilpy/solver_backends/_base.py
43 44 45 | |
set_event_callback(callback)
Set (or clear) a callback invoked on solver progress events.
Source code in src/ilpy/solver_backends/_base.py
21 22 23 | |
set_num_threads(num_threads)
abstractmethod
Set the number of threads the backend may use.
Source code in src/ilpy/solver_backends/_base.py
59 60 61 | |
set_objective(objective)
abstractmethod
Set the objective function for the problem.
Source code in src/ilpy/solver_backends/_base.py
39 40 41 | |
set_optimality_gap(gap, absolute)
abstractmethod
Set the optimality gap (absolute or relative) for early termination.
Source code in src/ilpy/solver_backends/_base.py
55 56 57 | |
set_timeout(timeout)
abstractmethod
Set the wall-clock time limit (in seconds) for solving.
Source code in src/ilpy/solver_backends/_base.py
51 52 53 | |
set_verbose(verbose)
abstractmethod
Enable or disable backend log output.
Source code in src/ilpy/solver_backends/_base.py
63 64 65 | |
solve()
abstractmethod
Solve the problem and return a Solution.
Source code in src/ilpy/solver_backends/_base.py
67 68 69 | |
SolverStatus
Bases: Enum
Normalized solver status across backends.
Attributes:
| Name | Type | Description |
|---|---|---|
INFEASIBLE |
Model proven infeasible. |
|
INF_OR_UNBOUNDED |
Model infeasible or unbounded. |
|
NODELIMIT |
Node limit reached before proving optimality. |
|
NUMERIC |
Numerical issues encountered by the solver. |
|
OPTIMAL |
Optimal solution found. |
|
OTHER |
Status not otherwise classified. |
|
SOLUTIONLIMIT |
Solution limit reached. |
|
SUBOPTIMAL |
A feasible but not provably optimal solution was returned. |
|
TIMELIMIT |
Time limit reached before proving optimality. |
|
UNBOUNDED |
Model proven unbounded. |
|
UNKNOWN |
Model loaded but not optimized yet. |
|
USERINTERRUPT |
Optimization was interrupted by the user. |
Source code in src/ilpy/_constants.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
INFEASIBLE = 'infeasible'
class-attribute
instance-attribute
Model proven infeasible.
INF_OR_UNBOUNDED = 'infeasible_or_unbounded'
class-attribute
instance-attribute
Model infeasible or unbounded.
NODELIMIT = 'node_limit'
class-attribute
instance-attribute
Node limit reached before proving optimality.
NUMERIC = 'numeric_issue'
class-attribute
instance-attribute
Numerical issues encountered by the solver.
OPTIMAL = 'optimal'
class-attribute
instance-attribute
Optimal solution found.
OTHER = 'other'
class-attribute
instance-attribute
Status not otherwise classified.
SOLUTIONLIMIT = 'solution_limit'
class-attribute
instance-attribute
Solution limit reached.
SUBOPTIMAL = 'suboptimal'
class-attribute
instance-attribute
A feasible but not provably optimal solution was returned.
TIMELIMIT = 'time_limit'
class-attribute
instance-attribute
Time limit reached before proving optimality.
UNBOUNDED = 'unbounded'
class-attribute
instance-attribute
Model proven unbounded.
UNKNOWN = 'unknown'
class-attribute
instance-attribute
Model loaded but not optimized yet.
USERINTERRUPT = 'user_interrupt'
class-attribute
instance-attribute
Optimization was interrupted by the user.
Variable
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)
Source code in src/ilpy/expressions.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
VariableType
Bases: IntEnum
Type of a decision variable in an optimization problem.
Attributes:
| Name | Type | Description |
|---|---|---|
Binary |
A variable restricted to 0 or 1. |
|
Continuous |
A real-valued variable. |
|
Integer |
An integer-valued variable. |
Source code in src/ilpy/_constants.py
6 7 8 9 10 11 12 13 14 | |
Binary = auto()
class-attribute
instance-attribute
A variable restricted to 0 or 1.
Continuous = auto()
class-attribute
instance-attribute
A real-valued variable.
Integer = auto()
class-attribute
instance-attribute
An integer-valued variable.
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
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 |
required |
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 |
required |
sense
|
Sense | Literal['minimize', 'maximize']
|
The sense of the objective, either |
Minimize
|
variable_type
|
VariableType | Literal['continuous', 'binary', 'integer']
|
The type of the variables, either an |
Continuous
|
verbose
|
bool
|
Whether to print the solver output, by default |
False
|
preference
|
Preference | Literal['any', 'cplex', 'gurobi', 'scip']
|
Backend preference, either an |
Any
|
on_event
|
Callable[[EventData], None]
|
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 For example |
None
|
Returns:
| Type | Description |
|---|---|
Solution
|
The solution to the problem. |
Source code in src/ilpy/_functional.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |