fibonacciguy
fibonacciguy

Reputation: 21

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:

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:

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.

So if my assignment matrix is as follows:

[[False, False], [True, False], [True, True]]

then applying any_fun on each row returns False, True, True respectively.

However, I can't seem to find a function that resembles 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...

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)];

Upvotes: 1

Views: 219

Answers (1)

fibonacciguy
fibonacciguy

Reputation: 21

I found the function I was looking for: exists. It should work as follows:

solve minimize sum(foo in foos) (
  exists(bar in bars) (assignment[foo, bar])
);

Upvotes: 1

Related Questions