我刚从Minizinc开始,我会对此有所帮助。
如何编写以下约束:
我想购买最多4个团体优惠,最多花100美元,我可以购买 每组只有一个项目。最大化质量。
int: items = 10;
set of int: GROUPS = 0..itms;
set of int: PRODUCTS = 1..7;
set of int:BUYS = 1..4;
int : max_spent = 100;
array[GROUPS] of set of int : package = array1d(GROUPS,[{},{1,2,3},{4,7},{3,6},{1,4,5},{3,4,7},{1,2,5},{4,6},{3,7},{3,7,5},{2,3}]);
array[GROUPS] of int: package_price = array1d(GROUPS,[0,5,5,25,10,12,20,40,55,52,10]);
array[GROUPS] of int: package_quality = array1d(GROUPS,[0,7,2,7,2,3,5,4,9,6,5]);
所需的输出应该是:
{3,7} {4,6} {1,2,5} {}
quality = 10;
price = 97;
---更新---
到目前为止,我试过了:
var int : will_buy;
will_buy = sum(i in BUYS)(package_price[i]);
constraint will_buy <= max_spent;
var int : quality;
quality = sum(i in GROUPS)(package_quality[i]);
array[GROUPS] of var BUYS: index;
include "element.mzn";
constraint forall(t in GROUPS)
(
element(index[t], package_price, package_price[t])
/\
element(index[t], package_quality, package_quality[t])
);
:/
答案 0 :(得分:2)
你的问题是众所周知的背包问题的一个变种。 MiniZinc tutorial,第3.6章给出了这个问题的解决方案。了解背包模型的MiniZinc模型应该为您提供解决此问题的指南。
或者,您可能希望查看global packing constraints,这些可能会使建模更容易。