简体   繁体   English

如果 boolean 数组包含至少一个 True 元素,我如何在 Minizinc 中指定一个 function 返回 True,否则返回 False?

[英]How can I specify a function in Minizinc that returns True if a boolean array contains at least one True element, and False otherwise?

The problem问题

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分别返回FalseTrueTrue

However, I can't seem to find a function that resembles any_fun .但是,我似乎找不到类似于 any_fun 的any_fun Is there a way to implement this?有没有办法实现这个?

What I tried我试过的

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.

相关问题 我可以安全地假设将对if(true)和if(false)进行优化吗? - Can I safely assume that if(true) and if(false) will be optimized? 在C ++中有效使用布尔值true和false? - Efficient use of boolean true and false in C++? 如何将启发式算法纳入MiniZinc? - How can one incorporate heuristic algorithms in MiniZinc? 如何在MiniZinc的下游约束中使用数组元素的子集的中间和? - How can I calculate an intermediate sum of a subset of array elements to use in a downstream constraint in MiniZinc? 如何优化(真阳性)/(假阳性)的比率而不是准确性? - How to optimize the ratio of (True positive)/(False postive) instead of accuracy? python-使用true / false约束进行优化 - python - Optimization with true/false constraint 如何找到包含所有点或与y = x轴对称的点的最小矩形? - How can I find the least rect, which contains all points or the point symmetrical to the y=x axis? 如何在Minizinc中连接数组的所有字符串? - How to concatenate all strings of an array in Minizinc? if(var == true)比if(var!= false)快吗? - Is if(var == true) faster than if(var != false)? 哪种方法更好地返回真或假? - which approach is better to return true or false?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM