2022-12-04

Python-MIP

What is Python-MIP

Python-MIP, short for Mixed Integer Programming, is an OSS Python library designed for solving complex optimization problems.

Underneath the Python interface, Python-MIP is powered by a C++ library called Cbc (Coin-or branch and cut), a highly optimized library known for its capabilities in solving mixed integer programming problems. The combination of Cbc's performance and Python's simplicity can tackle a variety of optimization tasks.

Python-MIP is not just limited to mixed integer programming. It is also capable of handling continuous linear programming problems. This versatility makes Python-MIP suitable for a wide range of use-cases, from operations research and logistics to machine learning and data science.

Key Features of Python-MIP

The Python-MIP library stands out due to several key features that make it an excellent tool for optimization tasks.

Mixed-Integer Linear Programming (MILP) and Continuous Linear Programming (LP)
Python-MIP is not limited to just one type of optimization problem. It can handle both MILP and LP problems, making it a versatile tool that can adapt to the requirements of a wide range of optimization tasks.

  • Ease of Use
    One of the key attractions of Python-MIP is its Pythonic interface. It provides a high-level, easy-to-use interface that is designed to be user-friendly. This lowers the barrier of entry for new users, making it easier for beginners to start using the library.

  • High Performance
    While Python-MIP provides a simple and user-friendly Python interface, under the hood it is powered by the Cbc library. This is a highly optimized C++ library designed specifically for solving mixed integer programming problems. By leveraging the power of Cbc, Python-MIP provides a high-performance tool that can handle large and complex problems efficiently.

  • Callback Functionality
    One of the more advanced features of Python-MIP is its callback functionality. This allows the users to inject custom logic into the solving process, which can be used to modify the behavior of the solver. This provides a great deal of flexibility, enabling users to customize the solving process to meet their specific needs.

Comparison to PuLP

While PuLP is another well-known Python library for optimization, there are several reasons why one might prefer Python-MIP. The primary advantage of Python-MIP is its performance. Due to its underlying Cbc solver, Python-MIP often outperforms PuLP, especially for larger and more complex problems. Additionally, Python-MIP's interface is considered more Pythonic and intuitive than PuLP's, making it easier to use for Python developers. Finally, Python-MIP's callback functionality provides a powerful tool for customization that is not available in PuLP.

Types of Optimization Problems Python-MIP Can Solve

Python-MIP is capable of solving a wide range of optimization problems, including:

  • Linear Programming (LP)
  • Mixed-Integer Linear Programming (MILP)

Case Study

We consider a case study of optimizing a logistics network for a large manufacturing company.

Problem Description

The company operates multiple production facilities and serves a vast network of retailers. The goal is to minimize the total cost of transportation from production facilities to retailers. The problem involves the following variables and constraints:

  • Production facilities, each with a certain production capacity
  • Retailers, each with a certain demand
  • Transportation costs from each facility to each retailer
  • Each retailer's demand must be satisfied
  • The total production at each facility cannot exceed its capacity

Problem Formulation

This problem can be formulated as a MILP problem. The objective function and constraints can be described as follows (where c_{ij} is the cost of shipping from facility i to retailer j, x_{ij} is the amount to ship from i to j, s_i is the supply at facility i, and d_j is the demand at retailer j):

Minimize:

\sum_i \sum_j c_{ij} * x_{ij}

Subject to:

\textrm{For all } i: \sum_j x_{ij} \leqq s_i \\ \textrm{For all } j: \sum_i x_{ij} \geqq d_j \\ \textrm{For all } i,j: x_{ij} \geqq 0 \\

Python-MIP Code

Let's now represent the problem and solve it using Python-MIP.

python
from mip import Model, xsum, MINIMIZE, CONTINUOUS

# Define the data
costs = [[2, 4, 5, 2], [3, 1, 3, 2], [2, 3, 2, 3]]  # costs from each facility to each retailer
supply = [1000, 2000, 1500]  # supply capacity at each facility
demand = [500, 900, 1800, 300]  # demand at each retailer

# Create a new model
m = Model("Transportation", sense=MINIMIZE)

# Create variables
x = [[m.add_var(var_type=CONTINUOUS) for j in range(len(demand))] for i in range(len(supply))]

# Set the objective function
m.objective = xsum(costs[i][j]*x[i][j] for i in range(len(supply)) for j in range(len(demand)))

# Add constraints
for i in range(len(supply)):
    m += xsum(x[i][j] for j in range(len(demand))) <= supply[i]

for j in range(len(demand)):
    m += xsum(x[i][j] for i in range(len(supply))) >= demand[j]

# Solve the problem
m.optimize()

# Print the solution
for i in range(len(supply)):
    for j in range(len(demand)):
        if x[i][j].x >= 0.99:
            print(f"Ship {x[i][j].x} units from facility {i} to retailer {j}")
Ship 500.0 units from facility 0 to retailer 0
Ship 300.0 units from facility 0 to retailer 3
Ship 900.0 units from facility 1 to retailer 1
Ship 300.0 units from facility 1 to retailer 2
Ship 1500.0 units from facility 2 to retailer 2

The script begins by defining the data, then it creates a model and defines the variables, objective function, and constraints. It finally calls the optimize function to solve the problem. The solution is then printed, indicating how to ship goods from each facility to each retailer in order to minimize transportation costs.

References

https://www.python-mip.com/

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!