Rules for 1-indexing and 0-indexing?

419 views
Skip to first unread message

guido...@gmail.com

unread,
Oct 21, 2016, 12:41:05 AM10/21/16
to mini...@googlegroups.com

Original post by user kiv (01/MAY/2015)

Hi, I need help understanding why and when some ranges in MiniZinc start from 0, and others from 1. For example, this is valid:

[code:1s1yneq3]array[1..3] of int: testarr = [0,1,2];[/code:1s1yneq3]

And this gives "Index set mismatch" error:

[code:1s1yneq3]array[0..2] of int: testarr = [0,1,2];[/code:1s1yneq3]

This is strange enough coming from a background of 0-indexed arrays in C, but then in Figure 17 of the tutorial we see that this is one with a 0-indexed array is valid:

[code:1s1yneq3]array[0..n-1] of var 0..n: s;[/code:1s1yneq3]

Can anyone resolve my confusion? Thanks in advance!
-----------------------------------------------------
This post was transfered to the google group when the phpbb based forum was shut down
-----------------------------------------------------

guido...@gmail.com

unread,
Oct 21, 2016, 12:41:10 AM10/21/16
to mini...@googlegroups.com

Original post by user hakank (01/MAY/2015)

MiniZinc is by default 1-based. However, you can change the base as you want, but when you then might have to use the arrayNd constructs, e.g. "array1d", "array2d" when initializing arrays.

Here's how you'd define and initialize the "testarr", i.e. using "array1d":

[code:29r1ufa3]array[0..2] of int: testarr = array1d(0..2, [0,1,2]);[/code:29r1ufa3]

Does this make sense?

guido...@gmail.com

unread,
Oct 21, 2016, 12:41:14 AM10/21/16
to mini...@googlegroups.com

Original post by user kiv (01/MAY/2015)

Thanks for the example, I understand now how I can make a 0-based array of parameters with arrayNd.

The arrayNd is only needed for array of parameters? For example it seems I can make a 0-based array of variables without requiring that construct:

[code:g2nvpqd2]array[0..2] of var 0..1: s;[/code:g2nvpqd2]

guido...@gmail.com

unread,
Oct 21, 2016, 12:41:19 AM10/21/16
to mini...@googlegroups.com

Original post by user guido.tack (01/MAY/2015)

Yes, that works because you don't assign another array to the newly created array. The arrayXd functions are only necessary when you have assignments between arrays with different index sets (or when the index set has a meaning for the constraint).

Cheers,
Guido
Reply all
Reply to author
Forward
0 new messages