I'm sorry for the title; frankly speaking, I don't even know whether my question is related to the Knapsack problem. Was reading some stuff about genetic algorithms and found this "Knapsack Problem".
I need someone to kick me in the right direction:
I am developing a python web application for a factory. So in a factory, they have something called an order. An order contains one or many products. There's a concept of mismatch, which is actually a negative number used to indicate how much less (in terms of quantity) a particular product will appear in an order.
Think of a matrix with the columns being products and the rows being orders. Just assume that all orders(rows) contain all products(columns). Again, there are 8 orders Orders 1 to Orders 8 and 5 products, Product 1 to Product 5.
Supposing, now I have a mismatch of 6 for Product 1. I need to split the number six equally amongst all 8 of the orders, randomly. So obviously 2 orders won't have mismatch quantities. Then I have a mismatch of 9 for Product 2. I split the mismatch quantity over 8 Orders as evenly as possible, randomly. This goes on for each Product. Now here comes the kicker, while I am in the process of splitting the mismatches randomly across all Orders, I need to ensure that the total mismatches per order (meaning for that row) are kept at a minimal. This means the total mismatches across an order needs to be the lowest possible number.
|-----|-----|-----|-----|-----|
| P1 | P2 | P3 | P4 | P5 |
-------------------------------
O1 | 2 | 1 | 1 | 0 | 2 | 6
-------------------------------
O2 | 1 | 2 | 1 | 1 | 1 | 6
-------------------------------
O3 | 2 | 2 | 1 | 0 | 1 | 6
-------------------------------
O4 | 1 | 2 | 0 | 1 | 1 | 5
-------------------------------
6 7 3 2 5
Do you get it? I need to code this in Python, and I have no idea where to start.
You almost have an integer linear programming problem, except that the objective function (the maximum of the row sums) you want to minimize is not linear. Look at scipy for solving optimization problems.
This thread, https://scicomp.stackexchange.com/questions/83/is-there-a-high-quality-nonlinear-programming-solver-for-python, may also help.
So the OP's problem has two requirements: randomly and evenly. It's a bit conflicting though, so I guess "truly random" is impossible.
Here is my try
Using OP's example, we have 4 orders and 5 products. Starting from the first product, we split the numbers randomly, so each product will at least have floor(6/4)=1 mismatches. Then we randomly put the remaining 2 mismatches to 2 products.
|-----|
| P1 |
-------
O1 | 2 | 2
-------
O2 | 1 | 1
-------
O3 | 2 | 2
-------
O4 | 1 | 1
-------
6
Next, we take care of the second product. Similarly, we split the numbers randomly first, so each product will at least have floor(7/4)=1 mismatches. Now for the remaining 3 mismatches, to make it as even as possible, we first assign them to O2 and O4, since last time they have 1 less mismatch than others. For the remaining 1 mismatches, we again randomly assign it to one of the product.
|-----|-----|
| P1 | P2 |
-------------
O1 | 2 | 1 | 3
-------------
O2 | 1 | 2 | 3
-------------
O3 | 2 | 2 | 4
-------------
O4 | 1 | 2 | 3
-------------
6 7
Repeat this process for all the products.
Using this way, you can guarantee that it's as even as possible (the biggest difference is 1), also you get some degree of random
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With