JaCoP:解决 0/1 背包问题
我一直在尝试自学如何使用 JaCoP 约束编程库,但在实现 0/1 背包问题时遇到了一些困难。我尝试了问题大小为 4 并定义了项目和变量,如下所示:
knapsack[0] = new KnapsackItem(quantity[0], 5, 7);
knapsack[1] = new KnapsackItem(quantity[1], 7, 9);
knapsack[2] = new KnapsackItem(quantity[2], 2, 3);
knapsack[3] = new KnapsackItem(quantity[3], 3, 3);
IntVar knapsackCapacity = new IntVar(store, "capacity", 0, 10);
IntVar knapsackProfit = new IntVar(store, "profit", 0, 22);
然后,我使用背包列表添加了背包约束:
Constraint con = new Knapsack(knapsack, knapsackCapacity, knapsackProfit); store.impose(con);
然后我按照教程中给出的方式搜索解决方案:
//search for a solution and print results
Search<IntVar> search = new DepthFirstSearch<IntVar>();
SelectChoicePoint<IntVar> select = new InputOrderSelect<IntVar>(store, quantity,
new IndomainMin<IntVar>());
boolean result = search.labeling(store, select);
if (result) {
System.out.println("Solution: "+quantity[0]+", "+quantity[1]+", "+quantity[2]+", "+quantity[3]);
} else {
System.out.println("*** No");
}
我得到的结果只是所有数量都为零,这满足了约束但没有优化任何内容。是否还有其他限制或我应该添加一些内容来尝试最大化每个项目的利润 * 数量?
谢谢
本
I've been trying to teach myself how to use the JaCoP constraint programming library but I'm having a bit of difficulty implementing an 0/1 knapsack problem. I've tried a problem size of 4 and defined the items and variables as follows:
knapsack[0] = new KnapsackItem(quantity[0], 5, 7);
knapsack[1] = new KnapsackItem(quantity[1], 7, 9);
knapsack[2] = new KnapsackItem(quantity[2], 2, 3);
knapsack[3] = new KnapsackItem(quantity[3], 3, 3);
IntVar knapsackCapacity = new IntVar(store, "capacity", 0, 10);
IntVar knapsackProfit = new IntVar(store, "profit", 0, 22);
I've then added a Knapsack constraint using the knapsack list:
Constraint con = new Knapsack(knapsack, knapsackCapacity, knapsackProfit);
store.impose(con);
And I have then searched for a solution in the way given in the tutorial:
//search for a solution and print results
Search<IntVar> search = new DepthFirstSearch<IntVar>();
SelectChoicePoint<IntVar> select = new InputOrderSelect<IntVar>(store, quantity,
new IndomainMin<IntVar>());
boolean result = search.labeling(store, select);
if (result) {
System.out.println("Solution: "+quantity[0]+", "+quantity[1]+", "+quantity[2]+", "+quantity[3]);
} else {
System.out.println("*** No");
}
The result I get is simply that all the quantities are zero, which satisfies the constraints but doesn't optimise anything. Is there another constraint or something I should add to try and maximise profit * quantity for each item?
Thank you
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有使用
Knapsack
约束,但一般为了优化(最小化),您使用以下内容(成本作为第三个参数):请注意,这最小化了成本,因此您必须将利润转换为“负成本”。示例
ExamplesJaCoP/KnapsackExample.java
(与ExamplesJaCoP/Example.java
结合)显示了原理。但是,该示例不使用KnapsackItem
,只是简单的IntVar
。I have not used the
Knapsack
constraint, but in general to optimize (minimize) you use the following (the cost as third argument):Note that this minimizes the cost, so you must convert the profit to a "negative cost". The example
ExamplesJaCoP/KnapsackExample.java
(combined withExamplesJaCoP/Example.java
) show the principle. However, the example don't useKnapsackItem
, just plainIntVar
.