Best combination of number of inputs to find the closest value to weight requirement
1 次查看(过去 30 天)
显示 更早的评论
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).
Thanks in advance.
4 个评论
Dyuman Joshi
2024-1-10
"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?
采纳的回答
Hassaan
2024-1-10
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 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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Mathematics and Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!