Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infering index sets for array comprehensions #740

Closed
Ditta1337 opened this issue Oct 31, 2023 · 3 comments
Closed

Infering index sets for array comprehensions #740

Ditta1337 opened this issue Oct 31, 2023 · 3 comments
Labels
enhancement resolved Issue is resolved and the feature or fix will be part of next release

Comments

@Ditta1337
Copy link

I was wondering whether the MiniZinc could infer an index set for array comprehensions being assigned to well defined variables/parameters. Currently, it always assumes indexing starting from 1 and it requires manual reindexing.

Example:

array[0..N-1] of int: arr = [i | i in 0..N-1];

it throws an error:

MiniZinc: evaluation error: Index set mismatch. Declared index set of `arr' is [0..3], but is assigned to array with index set [1..4]. You may need to coerce the index sets using the array1d function.

Also when such assignment happens in constraint:

array[0..N-1] of var int: arr;
constraint arr = [i | i in 0..N-1];

it only throws:

MiniZinc: assertion failed: array index sets do not match

Why the error message is shorter and doesn't recommend coercing via the array1d function?

@Dekker1
Copy link
Member

Dekker1 commented Nov 1, 2023

We have discussed inferring index sets in the past, but decided against inferring index sets as it can lead to unexpected behaviour elsewhere in the language.

Instead we have added syntax to directly index your comprehension:

array[0..N-1] of int: arr = [i: i | i in 0..N-1];

or for a multidimensional array:

array[_,_,_] of int: arr = [(i,j,k): i*j*k | i,j,k in -5..5]

Regarding the difference in error message. The first message is from the compiler, whereas the second message is from the MiniZinc library (from the '=' predicate. Although it will remain an assertion, I can see what I can do to make sure it at least gives more information about the index sets

@zayenz
Copy link
Contributor

zayenz commented Nov 1, 2023

While I get that inferring index sets might be problematic in many cases and thus can't be done in general everywhere, it is a recurring annoyance.

The most common case for me when it turns up is when I have data that is best represented using (typically) 0-indexed arrays. Writing data-files with array1d calls is not very nice, and neither is having a data value that is just used, and then reshaped to another name in the model file.

Adding some way to solve some of these minor problems might be worth thinking about. For example, having an annotation for an array that specifies that this declaration will auto-coerce index sets could be one way.

@Dekker1
Copy link
Member

Dekker1 commented Nov 1, 2023

For data from data files there are currently already a few ways to mitigate the problem as well. First, JSON files will actually do the inference of index sets (since we currently have no other way to give an index set to JSON list data).

For DZN data, we currently do not do inference, but when you just need to offset an array literal you no longer have to use array1d calls. Instead you can use the new literal types and provide the index for the first member:

array[0..4] of int: x;
x = [0: 3, 2, 1, 4, 0];

@Dekker1 Dekker1 added enhancement resolved Issue is resolved and the feature or fix will be part of next release labels Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement resolved Issue is resolved and the feature or fix will be part of next release
Projects
None yet
Development

No branches or pull requests

3 participants