[英]How can I specify a function in Minizinc that returns True if a boolean array contains at least one True element, and False otherwise?
In a Minizinc model where I want to minimize a linear cost function, I can say the following:在我想要最小化线性成本 function 的 Minizinc model 中,我可以说以下内容:
set of int: foos = 1..8;
set of int: bars = 1..5;
array[foos] of float: cost;
array[foos, bars] of var bool: assignment;
solve minimize sum(foo in foos) (
sum(bar in bars) (cost[foo] * assignment[foo, bar])
);
What I want to do is to specify a nonlinear/discrete function like this:我想要做的是像这样指定一个非线性/离散的 function:
solve minimize sum(foo in foos) (
any_fun(bar in bars) (assignment[foo, bar])
);
where any_fun
returns True if one or more elements in array assignment[foo, :]
is True, like np.any(x)
in Numpy.如果数组
assignment[foo, :]
中的一个或多个元素为 True, any_fun
返回 True,如 Numpy 中的np.any(x)
。
So if my assignment
matrix is as follows:所以如果我的
assignment
矩阵如下:
[[False, False], [True, False], [True, True]]
then applying any_fun
on each row returns False
, True
, True
respectively.然后在每一行上应用
any_fun
分别返回False
、 True
、 True
。
However, I can't seem to find a function that resembles any_fun
.但是,我似乎找不到类似于 any_fun 的
any_fun
。 Is there a way to implement this?有没有办法实现这个?
I tried the following.我尝试了以下。 This seems to work for a 1D array but couldn't figure out how to do this for a 2D array.
这似乎适用于一维阵列,但无法弄清楚如何对二维阵列执行此操作。
Also, I'm hoping there's a builtin function (like sum
) that doesn't require me to specify something myself...另外,我希望有一个内置的 function (比如
sum
)不需要我自己指定一些东西......
set of int: foos = 1..10;
array[foos] of int: assignment;
var bool: any_fun = sum(i in foos) (assignment[i]) > 0;
solve minimize any_fun;
output [show(any_fun)];
I found the function I was looking for: exists
.我找到了我要找的 function:
exists
。 It should work as follows:它应该按如下方式工作:
solve minimize sum(foo in foos) (
exists(bar in bars) (assignment[foo, bar])
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.