minizinc - 从一组集合中选择和求和
问题描述
我是 Minizinc 的新手,需要一些帮助来解决以下问题:
假设我有以下几套
int: budget= 8;
enum paths = {c1,c2,c3,c4};
array[paths] of int: size=[20,50,90,10];
array[paths] of set of int:colors=[{1,2},{3,2},{4,1,3},{4,3}];
array[paths] of set of int:cost=[{1,2},{10,8},{4,4,4},{50,12}];
array[paths] of var set of int: x;
或者我可以通过以下方式制定它
int: budget= 8;
enum paths = {c1,c2,c3,c4};
array[paths] of int:size =[20,50,90,10];
enum colors = {w1,b2,y3,r4, h5};
array[colors] of int:cost=[2,10,50,12];
array[paths] of set of colors: s=[{w1,h5},{b2,y3,r4,},{w1,b2},{r4, h5}];
array [paths] of var set of colors: a;
所以,每条路径都有固定的大小,但我可以在颜色之间进行选择,每种颜色都有成本。因此,如果我允许为每条路径选择一种颜色,那么所有选定颜色的成本总和应该等于或低于预算。我知道如何从数组中选择或求和,而不是从一组集合中选择或求和。请问我能得到任何帮助吗?
解决方案
使用第二个公式,您可以对变量数组求和a
,并且由于它的元素是集合,您可以对这些集合进行求和,就像这样
选择颜色
constraint forall(i in paths)(card(a[i]) == 1);
对所选颜色求和
constraint sum(p in paths)( sum(c in a[p]) (cost[c]) ) < budget;
由于每组仅包含一种颜色,因此您基本上只是对整个数组求和。
编辑:
- 将 a 的定义更改为array [paths] of var colors: a;
- 确保从该路径的选项集中选择一个值
constraint forall(p in paths)(a[p] in s[p]);
- 对变量 a 的成本求和sum(p in paths)( cost[a[p]] ) <= budget;
修改后的代码如下所示。
int: budget = 20;
enum paths = {c1,c2,c3,c4};
array[paths] of int : size = [20,50,90,10];
enum colors = {w1,b2,y3,r4,h5};
array[colors] of int : cost = [2,10,50,12,4];
array[paths] of set of colors: s = [ {w1,h5}, {b2,y3,r4}, {w1,b2}, {r4,h5} ];
array [paths] of var colors: a;
constraint forall(p in paths)(a[p] in s[p]);
constraint sum(p in paths)( cost[a[p]] ) <= budget;
推荐阅读
- spring-boot - 从 Master 构建 Spring-boot 时出错 - 测试失败:阻塞读取超时 30000 MILLISECONDS
- activerecord - 可以在控制器操作中使用活动记录查询吗?
- excel - 有没有办法自动检查列中的重复项并在输入时删除/通知?
- json - 在 Coldfusion 中保存 JSON 响应
- python - 设置滚动条以使用树视图
- java - 如何检查片段中的firebase用户是否为空
- html - Bootstrap 4 - 如何创建一个从第二列开始的表?
- mongodb - 如何在 Ubuntu 18.04 仿生上安装 Mongo 3.4
- java - 如何以编程方式在 CardView 中创建对象(即:textView)?
- c# - 如何让玩家朝着它所面对的方向移动