简体   繁体   English

具有多重约束的背包(例如重量、体积等)

[英]Knapsack with multiple constraints(ex. weight, volume, etc.)

Ive successfully written algorithms in C++ to obtain optimal solutions for integer knapsacks, fractional knapsacks, and a mixed-type knapsack, all with or without bounds to how many of each item is allowed to be taken.我已经成功地在 C++ 中编写了算法,以获得 integer 背包、分数背包和混合型背包的最佳解决方案,所有这些都有或没有限制每个项目允许带多少。

These have only dealt with items with singular a constraint, which is weight.这些只处理具有奇异约束的项目,即重量。 If I wanted to solve a knapsack that could have 2+ constraints, would I pull from other code Ive already written or need to write a whole new algorithm?如果我想解决一个可能有 2 个以上约束的背包,我会从我已经编写的其他代码中提取还是需要编写一个全新的算法?

The integer and mixed type knapsacks were written solved dynamic programming, while the fractional used greedy. integer 和混合型背包被编写解决动态规划,而分数使用贪婪。

Ex.前任。 Given two integer items and one fractional item.给定两个 integer 项目和一个小数项目。

Respective values are (9, 8, 3)各自的值为 (9, 8, 3)

Respective weights are (2, 3, 1) max Weight =24各自的权重是 (2, 3, 1) max Weight =24

Respective volumes are (3, 2, 2) max Volume=23各自的卷是 (3, 2, 2) max Volume=23

Find the optimal solution and sum.找到最优解并求和。

I believe I've calculated this sum to be 77.5我相信我已经计算出这个总和是 77.5

As soon as you have multiple constraints you enter the realm of linear (if all your variables can be fractional) or mixed integer linear (if some need to be integer) programming, and your best bet is to use an MILP solver.一旦您有多个约束,您就可以输入 realm 线性(如果所有变量都可以是小数)或混合 integer 线性(如果某些需要是整数)编程,最好的选择是使用 MILP 求解器。 There are several commercial (cplex, gurobi, xpress) and open source solvers (clp/cbc, glpk).有几个商业(cplex、gurobi、xpress)和开源求解器(clp/cbc、glpk)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM