JaCoP:解决 0/1 背包问题

发布于 2024-10-04 19:39:32 字数 1184 浏览 0 评论 0原文

我一直在尝试自学如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2024-10-11 19:39:32

我没有使用 Knapsack 约束,但一般为了优化(最小化),您使用以下内容(成本作为第三个参数):

search.labeling(store, select, cost);

请注意,这最小化了成本,因此您必须将利润转换为“负成本”。示例 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):

search.labeling(store, select, cost);

Note that this minimizes the cost, so you must convert the profit to a "negative cost". The example ExamplesJaCoP/KnapsackExample.java (combined with ExamplesJaCoP/Example.java) show the principle. However, the example don't useKnapsackItem, just plain IntVar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文