# Best combination of number of inputs to find the closest value to weight requirement

1 view (last 30 days)
Anil on 10 Jan 2024
Edited: Anil on 10 Jan 2024
I have a vector unit_weight=[3.00;5.10;8.10;10.30;15.00] for weight of different metals. I would like to get the best combinations of numbers of metal to be used to achieve total weight requirement. Final metal weight must be bigger than or equal to required weight.
For example If weight required is 400 kg then the best combination is:
Metal_weight = 5 * 3.00+ 10 * 5.10+ 10 * 8.10 + 10 * 10.30 + 10 * 15.00 = 400 kg
table_weight =
5×3 table
quantity unit_weight total_weight
________ ___________ ____________
5 3 15
10 5.1 51
10 8.1 81
10 10.3 103
10 15 150
(I want to have the best combination of quantity)
Also, the best combination for my case is using only 1 type of metal and even number of its quantity.
40 * 10.3 = 412 kg (instead of 39 * 10.3 = 401.7 kg, even number of metal is better option).
Dyuman Joshi on 10 Jan 2024
"That's why, the best combination is the one that has the closest total weight to weight requirement (to reduce total weight), and more than weight requirement to meet regulations."
I understood this, as you mentioned this in the question description as well.
But you specified a particular combination for the above example. What makes that particular combination the "best"?
Do you have a logic/criteria, which needs to be followed to obtain the "best" combination?
Anil on 10 Jan 2024
Edited: Anil on 10 Jan 2024
Good question actually. "the best combination" depends on a lot of criterias like what we have in the storage, what regulations tell us to use, depending on the surface size, client's need and more.
I am looking for creating a code to guide us before project execution phase, so that the best possible scenario among different options can be discussed.
Also, it is easier to use 2 or 3 different metals not combination of 5-10 different materials because more drawings, more specifications and more work needed in this case. Seeing all possible 1 metal - 2 metals - 3 metals combination, where proposed metals are around same weight, allows me to decide which is the best option.
For example:
100 kg required
34pcs - 3 kg or 20pcs - 3 kg (since we only have 20 in the storage(this number will be in the code
+ as well) or better to cover %60 of the area with smaller metal)
20pcs - 5.1 kg or 8pcs - 5.1kg (the rest amount is covered by different size metal)
14pcs - 8.1 kg
10pcs - 10.3 kg

Hassaan on 10 Jan 2024
unit_weights = [3.0; 5.10; 8.1; 10.3; 15.0];
target_weight = 400;
best_combinations = []; % To store the best combinations
% Loop through each unit weight
for i = 1:length(unit_weights)
weight = unit_weights(i);
pieces = ceil(target_weight / weight); % Calculate the pieces needed
if mod(pieces, 2) ~= 0 % If pieces are not even, make it even
pieces = pieces + 1;
end
total_weight = pieces * weight;
if total_weight >= target_weight % If the total weight is equal or greater than target
best_combinations = [best_combinations; pieces, weight, total_weight];
end
end
% Finding the optimal solution
[~, idx] = min(best_combinations(:, 3)); % Find the index of the smallest weight that is over the target
best_solution = best_combinations(idx, :);
fprintf('The best combination is %d pieces of metal with unit weight %.2f kg, totaling %.2f kg.\n',...
best_solution(1), best_solution(2), best_solution(3));
The best combination is 134 pieces of metal with unit weight 3.00 kg, totaling 402.00 kg.
The algorithm does not account for the possibility that no single metal type can achieve the target weight while maintaining an even number of pieces. In such cases, you'd need to further refine the criteria or adjust the constraints. If a more complex or precise solution is needed, you may have to employ more advanced optimization techniques or algorithms.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
• Technical Services and Consulting
• Embedded Systems | Firmware Developement | Simulations
• Electrical and Electronics Engineering
Feel free to contact me.
Anil on 10 Jan 2024
Thank you for your promt reply and help. This code is working marvelously and I want to ask one more step.
I would like to improve this code to have the most suitable combination also regarding what we have in the storage. For example, the most suitable solution for 400kg weight requirement is "134 pieces of metal with unit weight 3.00 kg, totaling 402.00 kg" but if there are 100 pieces of 3kg in the storage, then rest part will be covered by different sized metals (100 *3kg and 20 * 5.1kg).
Also, depending on the project, instead of offering 134 pieces of small metal, it is better to use 40 pieces of 10.3 kg. Do you think is it good idea to implement this decision-making step to code or choose the optimum solution manually?