Multiple Bucket Filling - algorithm

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.

Related

How do you find the smallest difference in mapping elements of a Primary List to two Secondary Lists

I have 10 players on a team.
My team of players need to purchase a total of 10 bats and 10 balls.
They can purchase:
A bat and a ball
Two bats
Two balls
They cannot buy 3 bats, or 3 balls, or any other combination. Two items only.
The Seller has 10 different balls, and 10 different bats, all with different prices.
Once 1 bat is sold, then that would be removed from the list.
The Buyer can go into debt.
The Seller does not have any change (even after a purchase).
If the Buyer spends 100 on a ball worth 10, he does NOT get 90 back.
I do have access to how much money each player has, as well as the value of how much the Seller will sell each bat and ball for.
Buyer - Name, Ball Purchase Price, Bat Purchase Price
Alpha 10 15
Bravo 20 20
Charlie 30 30
Delta 40 40
Echo 50 50
Foxtrot 60 60
Golf 70 70
Hotel 80 80
India 90 95
Juliett 99 99
Seller - Ball Name, Ball Price
A 10
B 20
C 30
D 40
E 50
F 60
G 70
H 80
I 90
J 99
Seller - Bat Name, Bat Price
A 99
B 95
C 80
D 70
E 60
F 50
G 40
H 30
I 20
J 15
In this example Alpha should purchase Ball-A and Bat-J, and Juliett should purchase Ball-J and Bat-A.
I am trying find an optimized way of figuring out which player should be buying which items to save the most money as a team, or for the team to be the least in debt.
How do you find the smallest difference in mapping elements of a Primary List to two Secondary Lists?
In a more complex scenario, how do I find out which Buyer should purchase which items?
In other examples, the seller might have a very expensive store where the players might have to go into debt.
Searching this type of question was difficult, as I am trying to find the smallest differences between my primary list, and two secondary lists, where the primary list can compare the same element twice.

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

Algorithm to calculate weighted distribution ratio

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.

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.

Resources