I'm working to figure out an algorithm to solve the problem below:
- There are w warehouses that store p different products with different quantities
- A customer places an order on n out of the p products
For example:
Product 1 | Product 2 | Product 3
Warehouse A | 1 | 2 | 1 |
Warehouse B | 2 | 2 | 1 |
Warehouse C | 2 | 1 | 5 |
Customer ordered: 2 X Product 1, 2 X Product 2, and 3 Product 3.
I searched and found this similar question:(Minimizing the number of warehouses for an order)
I followed the ILP solution:
import numpy as np
from ortools.linear_solver import pywraplp
# Some test data, replace with your own.
p = 50
w = 1000
n = np.random.randint(0, 10, p)
C = np.random.randint(0, 5, (w, p))
solver = pywraplp.Solver("model", pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
x = [solver.BoolVar(f"x[{i}]") for i in range(w)]
for j in range(p):
solver.Add(C[:, j] @ x >= n[j])
solver.Minimize(sum(x))
assert solver.Solve() is not None
print("Solution:")
print(f"assigned = {[i + 1 for i in range(len(x)) if x[i].solution_value()]}")
print(f" obj = {solver.Objective().Value()}")
print(f" time = {solver.WallTime() / 1000}s")
It is giving me the result of which warehouses are chosen to ship out the items. But I need solution that will tell each warehouse how many of what items to be shipped. I think it will involve solution of matrices.
Anyone can point me to the right direction of solving this? Much appreciated.