API Reference
- class ilpy.Constraint
Bases:
object
- __init__()
- classmethod from_coefficients(coefficients=(), quadratic_coefficients=(), relation=Relation.LessEqual, value=0)
- Return type:
- 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 write2 * Variable('x') - Variable('y') >= 0
.Tip: you can use
ast.dump
to see the AST representation of an expression. Or, useprint(expr)
to see the string representation of an expression.- static _cast(obj)
Cast object into an Expression.
- Return type:
- as_constraint()
Create an ilpy.Constraint object from this expression.
- Return type:
- class ilpy.Objective(size=0)
Bases:
object
- __init__(size=0)
- classmethod from_coefficients(coefficients=(), quadratic_coefficients=(), constant=0, sense=Sense.Minimize)
- Return type:
- class ilpy.Preference(*values)
Bases:
IntEnum
Preference for a solver backend.
- Any = 1
- Gurobi = 3
- Scip = 2
- class ilpy.Solution(variable_values, objective_value, status, time, native_status=None)
Bases:
object
- __init__(variable_values, objective_value, status, time, native_status=None)
-
status:
SolverStatus
- 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)
- 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)
- 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:
- 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: