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

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!

Related

Calculate shipping costs on the basis of product weight

my math is not so good, but can you guys help me with this
problem statement
Suppose I have 4 books with weights and prices.
Book1, 0.5KG
Book2, 0.8KG
Book3, 1KG
Book4, 0.3KG
I have a base price (shipping cost) based on weight, which is 30 Rs Per 0.5KG.
Now when I select "book 1", the shipping cost will be 30 Rs, but how can I get the shipping cost for book 2,book 3 and book 4?
it's not related to any programming or algorithm , anyways
if
30Rs -> 0.5KG
x -> 0.8Kg
then simply for Book2
x = (30Rs*0.8KG)/0.5KG = 48Rs
similarly for book3 and book4:
book3 = (30Rs*1KG)/0.5KG = 60Rs
book4 = (30Rs*0.3KG)/0.5KG = 18Rs
another way to solve it is if every 30RS corresponds to 0.5KG then by dividing each side by 5 then 6RS corresponds to 0.1KG.
Book2 is 0.8KG which is 8 times the value 0.1KG then it must cost 8 times the value 6RS so 8 * 6 = 48RS similarly for **Book3 and Book4 where
Book3 = 10 * 6 = 60RS
Book4 = 3 * 6 = 18RS
If the pricing is in brackets, that is the cost for 0 - 0.5kg is 30RS, and 0.5 - 1kg is 60RS, then you have to do as follows:
First find how many brackets you have:
weight / bracketSize
For your books, this will be:
Book1: 0.5/0.5 // 1
Book2: 0.8/0.5 // 1.6
Book3: 1/0.5 // 2
Book4, 0.3/0.5 // 0.6
Then, you need to round that value up to the nearest whole number. How you do this will depend on what language you're using, but it's often called Ceiling or ceil:
Book1: Ceiling(1) // 1
Book2: Ceiling(1.6) // 2
Book3: Ceiling(2) // 2
Book4, Ceiling(0.6) // 1
Then multiply by price to get your answer.
Book1: 1 * 30 // 30
Book2: 2 * 30 // 60
Book3: 2 * 30 // 60
Book4, 1 * 30 // 30
In one line:
result = Ceiling(weight / bracketSize) * pricePerBracket

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))]

Calculate the Ranks of Candidates based on Votes and Total Candidates

How can I claulate the rank of each candidate when I have the total candidates and votes secured by each?
I've managed the percentage part, but calculating the rank has me stuck.
I'll be using MySql in the end for this, but right now I only need the formula or method to calculate ranks.
Id be glad if you could help with just the formula. Just like the formula for interest is PTR/100.
Total Candidates
5
Total Votes
75
Votes
Name Marks Percentage Rank(What I'm trying to calculate)
A 25 33.34 1/5 ->Rank 1/5 has the most votes
B 20 26.67 2/5 ->And so on
C 10 13.34 4/5
D 5 6.67 5/5
E 15 20.00 3/5
There is a previous question on SO that addresses this, using MySQL and a ranking variable. There is some lovely stuff in the answers
MySQL rank function

How to optimize 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).

Resources