Is there a better way to compute Cartesian product. Since Cartesian product is a special case that differs on each case. I think, I need to explain what I need to achieve and why I end up doing Cartesian product. Please help me if Cartesian product is the only solution for my problem. If so, how to improve the performance?
Background:
We are trying to help customers to buy products cheaper.
Let say customer ordered 5 products (prod1, prod2, prod3, prod4, prod5).
Each ordered product has been offered by different vendors.
Representation Format 1:
- Vendor 1 – offers prod1, prod2, prod4
- vendor 2 – offers prod1, prod5
- vendor 3 – offers prod1, prod2, prod5
- vendor 4 – offers prod1
- vendor 5 – offers prod2
- vendor 6 – offers prod3, prod4
In other words
Representation Format 2:
- Prod 1 – offered by vendor1, vendor2, vendor3, vendor4
- Prod 2 – offered by vendor5, vendor3, vendor1
- prod 3 – offered by vendor6
- prod 4 – offered by vendor1, vendor6
- prod 5 – offered by vendor3, vendor2
Now to choose the best vendor based on the price. We can sort the products by price and take the first one.
In that case we choose
- prod 1 from vendor 1
- prod 2 from vendor 5
- prod 3 from vendor 6
- prod 4 from vendor 1
- prod 5 from vendor 3
Complexity:
Since we chose 4 unique vendors, we need to pay 4 shipping prices.
Also each vendor has a minimum purchase order. If we don’t meet it, then we end up paying that charge as well.
In order to choose the best combination of products, we have to do Cartesian product of offered products to compute the total price.
total price computation algorithm:
foreach unique vendor
if (sum (product price offered by specific vendor * quantity) < minimum purchase order limit specified by specific vendor)
totalprice += sum (product price * quantity) + minimum purchase charge + shipping price
else
totalprice += sum (product price * quantity) + shipping price
end foreach
In our case
- {vendor1, vendor2, vendor3, vendor4}
- {vendor1, vendor3, vendor5}
- {vendor6}
- {vendor1, vendor6}
- {vendor2, vendor3}
4 * 3 * 1 * 2 * 2 = 48 combination needs to be computed to find the best combination.
- {vendor1,vendor1, vendor6, vendor1, vendor2} = totalprice1,
- {vendor1, vendor3, vendor6, vendor1, vendor2} = totalprice2,
-
-
*
- {vendor4, vendor5, vendor6, vendor6, vendor3} = totalprice48
Now sort the computed total price to find the best combination.
Actual problem:
If the customer orders more than 15 products, and assume, each product has been offered by 8 unique vendors, then we end up computing 8^15=35184372088832 combinations, which takes more than couple of hours. If the customer orders more than 20 products then it takes more than couple of days.
Is there a solution to approach this problem in a different angle?
Your problem can get even more complex. A simple example:
You need 100 El. of P1, 100 of P2, and 25 of P3.
P1 can be purchased for 1000 at V1, P2 for 1000 at V2, and P3 for 1000 at V1 or V3.
Now shipping would be free, if you purchase for 1500, but cost you 200 at each vendor else.
So if you order everything at V1, you would pay 4000:
which sums up to 3200 and could be found by your method.
But you could split the purchase of product 3 this way:
which only sums up to 3000 and would not be found by your method.
Afaik, there is established research in such topics, and the keywords are matrices and system of equations.
You can describe your problem as
where cij is the count of product j at vendor i and pij is the price of product j at vendor i, but f(c11,p11) is not just count*price, but a function of count and price, since there might be a quantity discount. The right side is the purchase total for vendor i.
This is without purchase discount, which has to be modeled on top. If the discount on shipping is only depending on the total costs, it can be modeled just from ci => dci.
You would try to minimize sum (dc1+dc2+…+dcm).