pulp: Pulp classes
|
An LP Problem |
|
Thin wrapper over the Rust Variable. |
|
Thin wrapper around Rust AffineExpr. |
|
LP constraint backed by Rust for a row already stored in the model. |
The LpProblem Class
- class pulp.LpProblem(name: str = 'NoName', sense: int = 1)
Bases:
objectAn LP Problem
Creates an LP Problem
This function creates a new LP Problem with the specified associated parameters
- Parameters:
name – name of the problem used in the output .lp file
sense – of the LP problem objective. Either
LpMinimize(default) orLpMaximize.
- Returns:
An LP Problem
Three important attributes of the problem are:
- objective
The objective of the problem, an
LpAffineExpression
- constraints
A
listofconstraintsin model order (same order as rows in the Rust core).
Some of the more important methods:
- solve(solver: LpSolver | None = None, **kwargs: Any) int
Solve the given Lp problem.
This function changes the problem to make it suitable for solving then calls the solver.actualSolve() method to find the solution
- Parameters:
solver – Optional: the specific solver to be used, defaults to the default solver.
- Side Effects:
The attributes of the problem object are changed in
actualSolve()to reflect the Lp solution
- roundSolution(epsInt: float = 1e-05, eps: float = 1e-07) None
Rounds the lp variables
- Inputs:
none
- Side Effects:
The lp variables are rounded
- setObjective(obj: LpAffineExpression | LpVariable | int | float) None
Sets the objective function.
- Parameters:
obj – the objective function (LpAffineExpression, LpVariable, or numeric)
- Side Effects:
The objective function is set
- writeLP(filename: str, writeSOS: int | bool = 1, mip: bool = True, max_length: int = 100) list[LpVariable]
Write the given Lp problem to a .lp file.
This function writes the specifications (objective function, constraints, variables) of the defined Lp problem to a file.
- Parameters:
filename (str) – the name of the file to be created.
- Returns:
variables
- Side Effects:
The file is created
- writeMPS(filename: str, mpsSense: int = 0, rename: bool = False, mip: bool = True, with_objsense: bool = False) tuple[list[str], list[str], str, list[str]]
Writes an mps file from the problem information.
- Parameters:
filename – name of the file to write
mpsSense – 0 (use problem sense), 1 minimize, -1 maximize
rename – if True, normalized names (X0000000, C0000000) are used in the file
mip – include integer/binary markers
with_objsense – write OBJSENSE section
- Returns:
(variable_names, constraint_names, objective_name, pulp_names_in_column_order)
- toJson(filename: str, *args: Any, **kwargs: Any) None
Creates a json file from the LpProblem information
- Parameters:
filename (str) – filename to write json
args – additional arguments for json function
kwargs – additional keyword arguments for json function
- Returns:
None
- classmethod fromJson(filename: str) tuple[dict[str, LpVariable], LpProblem]
Creates a new LpProblem from a json file with information
- variables() list[LpVariable]
Problem variables from the Rust model, in id order (same order as
constraints()).
Variables and Expressions
- class pulp.LpVariable(_var: Variable)
Thin wrapper over the Rust Variable. Created only via LpProblem.add_variable() or add_variable_dicts/dict/matrix.
- classmethod fromDataclass(problem: LpProblem, mps: mpslp.MPSVariable)
Initializes a variable from a dataclass by adding it to the given problem.
- Parameters:
problem – the problem to add the variable to
mps – a
mpslp.MPSVariablewith the variable information
- Returns:
- classmethod fromDict(problem: LpProblem, data: dict[str, Any])
Initializes a variable from a dict by adding it to the given problem.
- Parameters:
problem – the problem to add the variable to
data – a dict with the variable information
- Returns:
- toDataclass() MPSVariable
Exports a variable into a dataclass with its relevant information
- Returns:
a
mpslp.MPSVariablewith the variable information- Return type:
:mpslp.MPSVariable
Example (variables are created from a problem):
>>> prob = LpProblem("example", LpMinimize)
>>> x = prob.add_variable('x', lowBound=0, cat='Continuous')
>>> y = prob.add_variable('y', upBound=5, cat='Integer')
gives \(x \in [0,\infty)\), \(y \in (-\infty, 5]\), an integer.
- class pulp.LpAffineExpression(_expr: AffineExpr)
Bases:
objectThin wrapper around Rust AffineExpr. A linear combination of LpVariables + constant. Optionally carries a constraint sense (for pending constraints created by <=, >=, ==).
- Use factory class methods to create instances:
LpAffineExpression.empty()
LpAffineExpression.from_variable(v)
LpAffineExpression.from_constant(c)
LpAffineExpression.from_dict({v: coeff, …})
LpAffineExpression.from_list([(v, coeff), …])
In brief, \(\textsf{LpAffineExpression([(x[i],a[i]) for i in I])} = \sum_{i \in I} a_i x_i\) where (note the order):
x[i]is anLpVariablea[i]is a numerical coefficient.
- pulp.lpSum(vector: Iterable[LpAffineExpression | LpVariable | int | float] | Iterable[tuple[LpVariable, float]] | int | float | LpVariable) LpAffineExpression
Calculate the sum of a list of linear expressions
- Parameters:
vector – A list of linear expressions
Constraints
- class pulp.LpConstraint(_constr: Constraint)
Bases:
objectLP constraint backed by Rust for a row already stored in the model.
Instances appear in
LpProblem.constraints()as a list in model order; they are not pending expressions and must not be combined with+/-on affine expressions. Usecopy()to obtain a pendingLpAffineExpression.- copy() LpAffineExpression
Return a pending LpAffineExpression copy with sense set.
Combinations and Permutations
- pulp.combination(iterable, r)
Return successive r-length combinations of elements in the iterable.
combinations(range(4), 3) –> (0,1,2), (0,1,3), (0,2,3), (1,2,3)
- pulp.allcombinations(orgset: list[Any], k: int) Iterator[tuple[Any, ...]]
returns all combinations of orgset with up to k items
- Parameters:
orgset – the list to be iterated
k – the maxcardinality of the subsets
- Returns:
an iterator of the subsets
example:
>>> c = allcombinations([1,2,3,4],2) >>> for s in c: ... print(s) (1,) (2,) (3,) (4,) (1, 2) (1, 3) (1, 4) (2, 3) (2, 4) (3, 4)
- pulp.permutation(iterable, r=None)
Return successive r-length permutations of elements in the iterable.
permutations(range(3), 2) –> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)
- pulp.allpermutations(orgset: list[Any], k: int) Iterator[tuple[Any, ...]]
returns all permutations of orgset with up to k items
- Parameters:
orgset – the list to be iterated
k – the maxcardinality of the subsets
- Returns:
an iterator of the subsets
example:
>>> c = allpermutations([1,2,3,4],2) >>> for s in c: ... print(s) (1,) (2,) (3,) (4,) (1, 2) (1, 3) (1, 4) (2, 1) (2, 3) (2, 4) (3, 1) (3, 2) (3, 4) (4, 1) (4, 2) (4, 3)