How to optimize Cartesian product - cartesian-product

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:
Product 1 2 3
Vendor 1 10 20 40
Vendor 2 20 10 40
--------------------------
needed cnt 100 100 25
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:
1000+2000+1000+0 (shipping) = or for the same sum
2000+1000+1000+0 at V2, or splitted
1000+0+0+200 = 1200 at V1 plus
0+1000+1000+0 = 2000 at V2,
which sums up to 3200 and could be found by your method.
But you could split the purchase of product 3 this way:
1000+0+500+0 = 1500 at V1 plus
0+1000+500+0 = 1500 at V2
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
f(c11, p11) + f(c22, p12) + f(c13, p13) = c1 => dc1
f(c21, p21) + f(c22, p22) + f(c23, p23) = c2 => dc2
...
f(c31, p31) + f(c32, p32) + f(c13, p33) = c3 => dc3
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).

Related

Elasticsearch - Sum by quantity and sort by lowest price

I have a requirement in Elasticsearch which I'm not able to implement at the moment. The use case is as follows; we have certain products uploaded in elastic (1 million + items) and each item has a quantity, a price and a lead time (for delivery).
Now I basically want to get the top matches (based on a product description search) where tot sum of all quantities = 1000 (example) sorted by the lowest price.
A similar but other query would be to get the top 1000 items with the lowest lead time.
Any recommendation on how to implement this and what the most performant way of doing this is?
Assume we have the following records:
Product 1 | Quantity 200 | price 4USD | lead time 2 days
Product 2 | Quantity 150 | price 3USD | lead time 5 days
Product 3 | Quantity 275 | price 5 USD | lead time 14
Now I want to get all products for a maximum of quantity of 200 with the cheapest items first. That would give me something like:
Product 2
Product 1
And then it would also give me some aggregates like the average delivery time for these 2 items is 3.5 days and total value is 650USD (150 x 3USD + 50 x 4 USD)
Thanks,
Bram

Amazon Quicksight - how to calculate Percentage of Total UNIQUE values?

How to calculate percent of count of UNIQUE values?
E.g. I have a dataset with people who can pick multiple symptoms (i.e each person can have 0 to 10 values).
person 1 - symptom A, B
person 2 - symptom B, C, D
person 3 - no symptoms
person 4 - symptom A
etc.
E.g. if total UNIQUE count of people is 4 and 2 of them have picked symptom A, then I'd like to see:
A = 2/4 = 50%
Currently QuickSight is able to calculate shares based on total count of people (not unique count) as one person can have multiple symptoms, so A is 2/6 = 33% (not what I need).
As much as I've tried, QuickSight doesn't enable that??

deciding discounts on set (which can be multiple) of products

I tried finding this question on SO over here but I dont find it relevant.
Let's say
1. Offer1 : buy 3 or more pencil and get 10% discount
2. Offer2 : buy 2 scale and 1 sharpener and get 20% discount on each set
so, a cart with pencil ($10) : 4 , scale ($20): 5, eraser($5): 1 and sharpener($10): 2 will look like:
Pencil : 4 Qty ==> $36 ($40 with 10% discount)
Scale: 2 Qty + Sharpener : 1 Qty ==> $40 ( $50 with 20% discount)
Scale: 2 Qty + Sharpener : 1 Qty ==> $40 ( $50 with 20% discount)
Eraser: 1 Qty ==> $5
Cart Total : $ 121 ( 36 + 40 + 40 + 5)
How to go ahead for implementing such algorithm ? Please guide me on this.
It should be a comment, but I don't have enough reputation points..so posting this way..
You can try below -
At the starting you have all the items and their corresponding prices.
Now create a method like calculatePrice_set1 which takes no of pencils and per pencil cost as parameters. Calculate total cost, if no of pencil more than 3 then substract discount and return the amount.
create another method calculatePrice_set2 which takes no of scale and sharpener and their corresponding cost. Now find how many set of 2 scales you have. Suppose you have passed 6 scales and 4 sharpeners as parameter. Then you have 3 set of 2 scales and 4 sharpeners. From this you can find how many eligible set for Offer2 you can make, which is 3 for this case. Calculate the total price for the set accordingly, apply discount and add left over pieces which couldn't be part of any set.
Then you can add return amount from step 1+ step 2+ (no. of eraser*price)

Algorithm: Fill different baskets

Let's assume I have 3 different baskets with a fixed capacity
And n-products which provide different value for each basket -- you can only pick whole products
Each product should be limited to a max amount (i.e. you can maximal pick product A 5 times)
Every product adds at least 0 or more value to all baskets and come in all kinds of variations
Now I want a list with all possible combinations of products fitting in the baskets ordered by accuracy (like basket 1 is 5% more full would be 5% less accurate)
Edit: Example
Basket A capacity 100
Basket B capacity 80
Basket C capacity 30
fake products
Product 1 (A: 5, B: 10, C: 1)
Product 2 (A: 20 B: 0, C: 0)
There might be hundreds more products
Best fit with max 5 each would be
5 times Product 1
4 times Product 2
Result
A: 105
B: 50
C: 5
Accuracy: (qty_used / max_qty) * 100 = (160 / 210) * 100 = 76.190%
Next would be another combination with less accuracy
Any pointing in the right direction is highly appreciated Thanks
Edit:
instead of above method, accuracy should be as error and the list should be in ascending order of error.
Error(Basket x) = (|max_qty(x) - qty_used(x)| / max_qty(x)) * 100
and the overall error should be the weighted average of the errors of all baskets.
Total Error = [Σ (Error(x) * max_qty(x))] / [Σ (max_qty(x))]

How to find the closest set of numbers from a given one?

I am trying to find a proper algorithm for the following task:
I have an amount of resources (actual available quantity). This amount increases constantly at a given rate (increase/min). The goal is to buy all available products from all given options a,...,n (here: option_A, option_B, Option_C).
Now, depending on the rising resources, which product can be bought earlier (here: option_A4, option_B3, option_C3)?
Actual available quantity
Resource A 142
Resource B 56
Resource C 383
Resource D 335
Increase/min
Resource A 2
Resource B 263
Resource C 482
Resource D 301
Option_A ResA ResB ResC ResD bought
Product 1 00032 00066 00058 00008 *
Product 2 00292 00395 00407 00024 *
Product 3 01752 03555 02033 00073 *
Product 4 03505 31999 12200 00294
Product 5 07009 63998 85401 02938
Option_B ResA ResB ResC ResD bought
Product 1 00008 00048 00006 00034 *
Product 2 00049 00240 00012 00134 *
Product 3 00098 01438 00083 00806
Product 4 00491 04314 00499 06451
Product 5 03929 08628 04985 12901
Option_C ResA ResB ResC ResD bought
Product 1 00022 00011 00024 00078 *
Product 2 00111 00106 00122 00699 *
Product 3 00334 00211 00610 04892
Product 4 00669 01477 01831 39137
Product 5 06020 04432 16482 78275
I don't know if there is an algorithm for solving this kind of tasks already out there, but my approaches would be:
Approach A
1. Sum of digits of the actual available quantity
2. Sum of digits of every product
3. Compare the sum of the actual available quantity with each sum of products
4. Identify the product with the less distance
This would be easy, but it pictures only the actual situation without involvement of the constantly rising resources.
Approach B
1. Calculate how long it would take, to reach every single resource depending of the actual amount of resources plus the increasing rate.
E.g. for Option_A, Product 1, ResA:
needed: 3505
available: 142
increase: 2/min
required: 3363 (3505-142)
time after reaching requirements: 1681,5min (3363/2)
2. Repeat for ResB,ResC,ResD and sum the amount of time
3. Repeat 1+2 for every product
4. Choose the product with the shortest amount of time
What do you think?
Looks like you're building a script for resource management game like C&C Tiberium Alliances, haha
My answer would be: your second approach is the way, with a few alterations.
On your second step, you don't sum the amount of time, instead you pick the maximum of them. This is because all the resources are increasing concurrently, right?
See this example:
Res A Res B Res C Res D
Current 142 56 383 335
Increment 2 263 482 301
Product 4 3505 31999 12200 294
Required 3353 31943 11817 0
Time 1676.5 121.5 24.5 0 mins
So you need 1676.5 mins until Res A is enough to buy Product 4 (of Option A), 121.5 mins for Res B, 24.5 min for Res C, and none for Res D as it's already enough.
The time you need to actually be able to buy Product 4 will be 1676.5 mins (i.e., the maximum)
Then you repeat that for every product not bought yet, then sort with increasing time left.
Hope this helps!

Resources