A python wrapper for the module lp_solve, which provides notation based api.
- Notation management support.
- Offset management support.
- Auto flattening/reshaping.
- Multiple constraint addition methods support.
- Python 2.x/3.x support.
We strongly recommend you to install lpsolve
by conda
:
conda install -c conda-forge lpsolve55
Then add lpsolve_wrapper.py to your projects.
There are several examples. These display some functionality of the interface and can serve as an entry point for writing more complex code. The following steps are always required when using the interface:
- It is necessary to import lpsolve_wrapper in your code:
import lpsolve_wrapper as lw
- Create a solver instance and add two notations:
model = lw.Model(
notations={
'x': lw.notation(
lower_bound=0,
),
'y': lw.notation(
lower_bound=0,
)
})
- Access the methods in the
lpsolve_wrapper.py
file, e.g.:
model.add_constr(
coefs=[
lw.coef('x', 1),
lw.coef('y', 1),
],
right_value=75,
constr_type=lw.LEQ
)
objective, notation_list = model.lp_solve(
obj_func={
'x': 143,
'y': 60,
},
minimize=False
)
print('objective:', objective)
print('x:', notation_list['x'])
print('y:', notation_list['y'])
lpsolve_wrapper provides 3 methods to add a constraint:
- Add a constraint by assigning single values:
model.add_constr(
coefs=[
lw.coef(name='x', idx=[2, 3], value=1),
lw.coef(name='y', idx=[0], value=2),
],
right_value=75,
constr_type=lw.GEQ
)
- Add a constraint by coefficient mats of notations:
model.add_constr_mat(
coef_mats={
'x': [1, 1, 1],
'y': [[1, 2], [3, 4]]
},
right_value=1,
constr_type=lw.EQ
)
- Add a constraint by callbacks:
def tmp_func(y):
y[2][3] = 1
model.add_constr_callback(
callbacks={
'x': lambda x: x[0].fill(1),
'y': tmp_func
},
right_value=1,
constr_type=lw.EQ,
)
Use cases can be found in examples.
- | add_constr() | add_constr_mat() | add_constr_callback() |
---|---|---|---|
Example 0 | ✔ | ||
Example 1 | ✔ | ||
Example 2 | ✔ | ||
Example 3 | ✔ | ✔ | |
Example 4 | ✔ | ||
Example 5 | ✔ |