I’m programming web application which has some tricky calculations and I would need some help with mathematical calculations.
What I'm trying to calculate:
I have number of items. Lets call items a1, a2, ..., an. Each item consists of three components (let's call them x, y and z). Components have a ratio which is percentage. Sum of that ratio is always 1 (or 100%).
There is individual amount of each item and each item has unique tolerance/range +- % (min - max).
Then I have target ratio (x, y and z). I need to figure out what amount of each item must be to find combination which forms target ratio. Items amount must be between their minimum and maximum values. Combined items forms overall x, y and z values with should match target x, y and z
Here is an example how I started to solve the problem:
x = 0.25, y = 0.50 and z = 0.25
Minimum amount: 100% - 30% = 70% -> 100 x 0.7 = 70
Maximum amount: 100% + 30% = 130% -> 100 x 1.3 = 130
(Here 70% and 130% are coming from tolerances and after that min and max amounts can be calculated
x = 0.15, y = 0.30 and z = 0.55
Minimum amount: 100% - 25% = 75% -> 250 x 0.75 = 187.5
Maximum amount: 100% + 25% = 125% -> 250 x 1.25 = 390.6
x = 0.40, y = 0.40 and z = 0.20
Minimum amount: 100% - 40% = 60% -> 180 x 0.6 = 108
Maximum amount: 100% + 40% = 140% -> 180 x 1.4= 252
I have a target ratio. In this example let the target ratio be:
x = 0.25, y = 0.40 and z = 0.35
I need to find what the amount of each items should be in order to form the target ratio and the items must be in their tolerance ranges (between min and max).
Because of each items has 3 components, without min/max amount the combination can be easily calculated (multiplier for each item) with 3 equations below by using linear equation. Item 1 = a1, item 2 = a2 and item 3 = a3:
0.25 a1 + 0.50 a2 + 0.25 a3 = 0.25 <- target x = 0.25
0.15 a1 + 0.30 a2 + 0.55 a3 = 0.40 <- target y = 0.40
0.40 a1 + 0.40 a2 + 0.20 a3 = 0.35 <- target z = 0.35
After calculating linear equation I get ratio of items: a1 = 0.375, a2 = 0.375 and a3 = 0.25 meaning that a1 = 37.5%, a2 = 37.5% and a3 = 25.0% of overall amount of items. Now x, y and z match with target ratio. At this point I haven't used the minimum or maximum values.
Below is the link to online calculator which I now used to calculate factors using linear equation for test purposes: (Also note that I used a, b and c for item/variable names instead of a1, a2 and a3 in online calculator). Link to online calculator
In this example there are 3 variables/items and 3 equations. Two problems: min/max is not included and there can be for example 10 items/variables in equation. Then the linear equation doesn't work anymore because there are much more items/variables than equations. I'm not sure how but these min and max amount's must be added to the same equation (and is it possible to form a matrix):
a1 >= 70 <- item 1 minimum amount
a1 <= 130 <- item 1 maximum amount
a2 >= 187.5 <- item 2 minimum amount
a2 <= 390.6 <- item 2 maximum amount
a3 >= 108 <- item 3 minimum amount
a3 <= 252 <- item 3 maximum amount
And this is my problem. I have no idea how to combine these together (inequalities and linear equations or am I going totally wrong direction?). I need to be able to get values for a1, a2, a3, …, an. And I need at least one suitable solution (combination) if there is any solution also when there are multiple or infinite amount of solutions.
If someone can help I would be very grateful! Thanks for your time!