English | 简体中文
PY-MIP is a python-based collection of different MIP (Mixed-Integer Linear programs) FREE solvers for making full use of their own advantages by providing a uniform API. Many famous FREE solvers (such as OR-Tools) have had problems and a model defined by a FREE solver can be hard to be redefined by another solver because of their different API. So PY-MIP provides high-level API to model and solve MIP by invoking different FREE solvers including:
- OR-Tools
- PySCIPOpt
- …
You only need write one solver independent code.
The pros and cons of some famous FREE solvers are summarized here:
solver | pros | cons |
---|---|---|
ortools.linear_solver (LP) |
LP can export formula models that are easy to read and understand; Easy installation; |
LP doesn’t support modeling quadratic programming although SCIP solver can solve it. Lack of compute_IIS() function to find conflict constraints. |
ortools.sat (CP_SAT) |
CP_SAT supports computing IIS. Easy installion. |
It’s difficult for human to read and understand the exported formula model. CP_SAT doesn’t support modeling quadratic programming. |
PySCIPOpt | Quadratic programming. | Lack of detail including function and parameter detail in the interface. 😅 A working installation of the SCIP Optimization Suite is required. A commercial or evaluation license from ZIB is required. |
Commercial solvers (such as Gurobi or CPLEX) provide rich features including all of the above, but because of expensive license, they may be not suitable for small companies or individual developers.
ortools
pyscipopt(optional, a SCIP Optimization Suite needs to be installed)
PY-MIP provides 3 solvers to invoke:
solver name | solver core | scenario | requirement |
---|---|---|---|
LP_SOLVER |
SCIP_MIXED_INTEGER_PROGRAMMING of ortools.linear_solver |
Model problem; Export model detail; |
ortools |
CP_SAT_SOLVER |
ortools.sat.cp_model |
Find conflict constraints(compute IIS); | ortools |
SCIP_SOLVER |
pyscipopt.Model |
Solve quadratic programming; | pyscipopt, scip |
For solving a MIP problem, you can use PY-MIP mainly in three steps:
- import
Solver
class and solver namesfrom pymip.Config import CP_SAT_SOLVER, LP_SOLVER, SCIP_SOLVER from pymip.Solver import Solver
- Instantiate
Solver
class with a solver name, for exampleLP_SOLVER
.solver = Solver(solver_name = LP_SOLVER)
- Define decision variables, add constraint and set coefficient in objective function using
solver
. For example, trying to model and solve the following problem:
a = solver.new_bool_var("a")
b = solver.new_bool_var("b")
x = [solver.new_bool_var(f"x{i}") for i in range(10)]
sum_x = sum([item for item in x])
# add constraint
con_1 = 3 * a + b - 10 + sum_x == 0
con_2 = x[0] >= x[1]
solver.add_constraint(con_1, name = "constraint 1")
solver.add_constraint(con_2, name = "constraint 2")
# set coefficient of each variable in objective function
solver.set_obj(3, a)
for item_x in x:
solver.set_obj(2, item_x)
solver.set_obj(5, b)
- solve:
status = solver.solve()
# export model detial into ./model.txt
solver.export_model(file_path="./model.txt")
print(status)
print(solver.get_var_name(a), solver.get_var_value(a))
print(solver.get_var_name(b), solver.get_var_value(b))
[print(solver.get_var_name(x[i]), solver.get_var_value(x[i])) for i in range(len(x))]
print("objective value: ", solver.objective_value)
Here is the output of the program.
status: optimal
solution:
a = 1
b = 0
x0 = 0
x1 = 0
x2 = 0
x3 = 1
x4 = 1
x5 = 1
x6 = 1
x7 = 1
x8 = 1
x9 = 1
objective value: 16.999999999999996
Additional example scripts are available in the example of this GitHub.