Algorithm to calculate weighted distribution ratio - algorithm

Here's the problem I'm facing. I have some number of items. I then have a varying number of buckets with a weight (between 0 and 1) attached to them. I'm trying to calculate the percentage of the items that should go in each bucket.
For example, let's say I have 20 items and 3 buckets:
B1 - weight: 0.5
B2 - weight: 0.5
B3 - weight: 0.25
The percentage would then be:
B1 - 40% of the items = 8 items
B2 - 40% of the items = 8 items
B3 - 20% of the items = 4 items
The percentage should add to 100% so that all items will be distributed into buckets. In the example above, B1 and B2 should both have twice as many items as B3 since their weight is double that of B3; but, when all 3 buckets are put together, the actual percentage of items B1 gets is 40%.
Is there an algorithm already out there for this or do any of you have an idea of how to solve it?

I think you can just divide the weight of each bucket by the total weight of all items to find the percentage of items which each bucket should bear.
However, there is a slight issue should the number of items and bucket weights not divide evenly. For the sake of example, let's consider the following scenario:
B1 - weight: 0.15
B2 - weight: 0.15
B3 - weight: 0.70
And let us suppose that there are 23 items.
Then we can compute the number of items which should be allocated to each bucket by just multiplying the fraction of total weight against the total number of items:
B1 - weight: 0.15, 3.45 items
B2 - weight: 0.15, 3.45 items
B3 - weight: 0.70, 16.1 items
One algorithm which could deal with this fractional bucket problem would be to compute the number of items for each bucket, one at a time, and then shift the remainder to the next calculation. So, in this example, we would do this:
B1 - 3.45 items, keep 3, rollover 0.45
B2 - 3.45 items + 0.45 = 3.9 items, keep 3, rollover 0.9
B3 - 16.1 items + 0.9 = 17 items (whole number, and last bucket)

Sum the weights from all buckets, then divide each bucket's weight by that sum to derive the bucket's percentage of the total.

Related

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

Multiple Bucket Filling

I have two bucket with each having carrying capacity.
Bucket A: 100 kg
Bucket B: 110 kg
I have multiple weights:
Weight W1: 20 kg
Weight W2: 40 kg
Weight W3: 90 kg
Weight W4: 80 kg
What I want is to fill bucket with maximum weights
Bucket A <- W3
Bucket B <- W1, W4
If bucket is only one, it is like KnapSack problem.
For multiple bucket with different capacity, I am not able to solve it.
Find out each baskets free space if you put the highest weight you can possibly put to that bucket.
Put the weight to the bucket that has the maximum,
weight / SpaceLeftAfter
I'll post a code when I'm at a computer.

How can I distribute weights knowing what's inside the box

I have this problem and I think that I'm gonna need a mathematical solution.
I have some boxes. Of these, I only know their total weight and what is inside each one. I have to calculate each one's weight.
For example I have:
Total weight: 100
Number of boxes: 5
Number of items: 14
Stock:
Type1: 2 items
Type2: 1 items
Type3: 7 items
Type4: 4 items
Box #1:
Type1: 2 items
Type2: 1 items
Box #2:
Type4: 3 items
Box #3:
Type3: 3 items
Box #4:
Type3: 2 items
Type4: 1 items
Box #5:
Type3: 2 items
Each box can potentially have n types of items, so how can I distribute the total weight?
I cannot divide the total weight by the number of boxes because the result would be equal for all boxes and this is not a real case.
You have:
Four variables - the weight of each item type
One linear equation 2A + B + 7C + 4D = 100 - what you know about the total weight.
Some linear inequalities - you know that A, B, C and D are all positive.
There's an infinite number of possible solutions. For example A=B=C=2,D=20 or A=B=C=4,D=15 and everything in between.

Find the optimum number of non uniform bins

R - Problem: to find the optimum number of non-uniform bins to show a range of data points.
I have a bunch of data points (let us assume different prices of different mobiles). I need to categorize these mobile phones into some categories (based on the price). The bin size (in this example refers to the price range) need not be uniform (there might be lots of mobiles in the low price category and few in the long tail category).
Is there any efficient algorithm to find the optimum number of bins required and the number of data points (in this case mobile phones) which shall go into each category.
This is not a standard formula, but wanted to post as it seem to work well with data set i tested.
Find the average price of all the mobiles.
Ex: 5 mobiles with prices 10, 20, 40, 80, 200
Avg is 350/5 = 70
Subtract minimum price from average price: 70 - 10 = 60 -> name it N1
Subtract avg price from Max price: 200 - 70 = 130 -> name it N2
Find the ratio N2/N1 : 130/60: Roughly 2
This indicates that it is better to have 2 bins at the lower price range for every 1 bin at higher range.
So, for example take 2 bins below 70. Range 0 - 35(2 mobiles), 36 - 70(1 mobile)
1 bin above 70: Range 71 - 200(2 mobiles)
As you can see, number of bins and bin sizes are reasonably optimal.

How to scale down the values so they could fit inside the min and max values

I have 6 graph bars with the prices.
Each price number will represent its graphbar's height by respecting min and max heights.
What i want is that graph bar's height wouldn't go below or above the min and the max value.
So i have values of min = 55 and max = 110.
And price numbers are:
49
212
717
1081
93
By which mathematical algorithm I could achieve expected results ?
It's some sort of dynamic scalable bar graphs.
Modified
So the min and max values from the price list will be: 49(min price) => 55(min) and 1081 (max price) => 110(max)
The solution is simple:
Pick the smallest, and largest item and find the difference.
(largest_item - smallest_item) maps to (max-min).
Compute ratio = (max-min)/(largest_item-smallest_item)
final_value = min_value + ratio*(value-smallest_item)
As a mathematical function:
f(x,max,min,largest,smallest) = min + (max-min)/(largest-smallest)*(x-smallest)
where:
x : Input item's price
max: Maximum value (here, 110)
min: Minimum value (here, 55)
largest: Largest item in input (Here, 1081)
smallest: Smallest item in input (Here, 49)
One check, as #amit correctly points out: Ensure largest and smallest item are distinct.
So let x = 93. We have other 4 values with us.
f(x,max,min,largest,smallest) = min + (max-min)/(largest-smallest)*(x-smallest)
value = 55 + ((110-55)/(1081-49)) * (93-49)
value = 57.344961
Further,
f(93,110,55,1081,49) = 57.344961
f(49,110,55,1081,49) = 55
f(1081,110,55,1081,49) = 110
The function:
[(x - min ) / (max-min)*55] + 55
ensures the boundaries you are after - but you should also consider - what should the graph show? What do you want the reader to understand from it?
Why?
(x-min) / (max-min) gives a number in range [0,1] - 0 for min,
1 for max.
Multiplying it with 55 ensures a number in range [0,55].
Adding 55 ensures a number in range [55,110] - as expected.
(*) Note: for max = min - the above fails because of division with 0, take care for these cases manually.

Resources