| 1 |
PuLP_LPARRAY: PuLP x NumPy, a match made in heaven |
| 2 |
====================================================== |
| 3 |
|
| 4 |
It's just PuLP under the hood: LpVariable, LpAffineExpression and LpConstraint do the heavy lifting. |
| 5 |
All the power of numpy for your linear variable sets: broadcasting, reshaping and indexing tricks galore. Never see a for or indexing variable ever again. |
| 6 |
Special support functions that allow efficient linearization of useful operations like min/max, abs, clip-to-binary, boolean operators, and more. Wide support for the axis keyword. |
| 7 |
|
| 8 |
site: https://github.com/qdbp/pulp-lparray |
| 9 |
|
| 10 |
example: |
| 11 |
|
| 12 |
Let's solve an unconstrained SuperSudoku puzzle. A SuperSudoku is a Sudoku with the additional requirement that all digits having box coordinates (x, y) be distinct for all (x, y):: |
| 13 |
|
| 14 |
from lparray import lparray |
| 15 |
|
| 16 |
# name R, C, r, c, n lb ub type |
| 17 |
X = lparray.create_anon("Board", (3, 3, 3, 3, 9), 0, 1, pp.LpBinary) |
| 18 |
prob = pp.LpProblem("SuperSudoku", pp.LpMinimize) |
| 19 |
(X.sum(axis=-1) == 1).constrain(prob, "OneDigitPerCell") |
| 20 |
(X.sum(axis=(1, 3)) == 1).constrain(prob, "MaxOnePerRow") |
| 21 |
(X.sum(axis=(0, 2)) == 1).constrain(prob, "MaxOnePerCol") |
| 22 |
(X.sum(axis=(2, 3)) == 1).constrain(prob, "MaxOnePerBox") |
| 23 |
(X.sum(axis=(0, 1)) == 1).constrain(prob, "MaxOnePerXY") |
| 24 |
prob.solve() |
| 25 |
board = X.values.argmax(axis=-1) |
| 26 |
print(board) |