Skip to content

A python high-level API to OR-Tools(CLP, CBC, SCIP and cp_model), PySCIPOpt and ... to solve LPs and MIPs.

License

Notifications You must be signed in to change notification settings

yuhaoli-95/py-mip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PY-MIP

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.

Requirements

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

Model and solve a linear program

For solving a MIP problem, you can use PY-MIP mainly in three steps:

  1. import Solver class and solver names
    from pymip.Config import CP_SAT_SOLVER, LP_SOLVER, SCIP_SOLVER
    from pymip.Solver import Solver
  2. Instantiate Solver class with a solver name, for example LP_SOLVER.
    solver = Solver(solver_name = LP_SOLVER)
  3. 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)
  1. 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 examples

Additional example scripts are available in the example of this GitHub.

About

A python high-level API to OR-Tools(CLP, CBC, SCIP and cp_model), PySCIPOpt and ... to solve LPs and MIPs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published