reset password
Author Message
sdo8
Posts: 54
Posted 00:02 Nov 08, 2019 |

Hi,

I was wondering if there is any way to try/catch values in minizinc. 
Currently, I am accessing array indices that are out of bounds in the following example application:

forall(x in 0..n) (
  array[x+1] == something[5] \/
  array[x-1] == something[4]
);

If the array looks something like this ... array = [ 1, 2, 3, 4, ... , n ],

When x is 0, the line array[x+1] has no errors. However, when x = n, the array[x+1] line will give you a warning below:

WARNING: undefined result becomes false in Boolean context (array access out of bounds)

The code will still run - but the result will simply become false (and prefer array[x-1] == something[4] instead).

On a larger scale, some of my options may be out of bounds, but I always have an option that will be valid.

This results in a ton of warnings. However, minizinc ends up suppressing all the warnings:

WARNING: Further warnings have been suppressed.

Is there any way to check if whatever we are trying to access actually exists before actually doing so?

rabbott
Posts: 1649
Posted 08:03 Nov 08, 2019 |

I don't think so. I've never seen anything like it, and the Handbook doesn't seem to mention anything like it.

You might do the checking yourself.

forall(x in 0..n) (
  (x+1 <= n -> array[x+1] == something[5]) \/
  (x-1 >= 0 -> array[x-1] == something[4])
);

But will this accomplish what you want? If x == 0 or x == n, the expression is True. What do you want to happen in those cases?

An alternative might be:

constraint forall(x in 1..n-1) (

               (array[x+1] == something[5] \/
                array[x-1] == something[4]));

 constraint array[1] == something[5];

 constraint array[n-1] == something[4];

 

Last edited by rabbott at 22:03 Nov 08, 2019.
kmarlis
Posts: 35
Posted 08:15 Nov 08, 2019 |

There's a couple of options you could try out. You could use trace(s, e) where s is the string to be printed before evaluating the expression e. You could also try assert(b, s) where b is a boolean and s is a string. If the boolean is false, the execution aborts and prints the s string. Neither of these will help force your model to check, but they might help you to understand what may be happening. They are also not the most intuitive to work with, and have a lot of frustrating limitations, so they might not be of much help.

 

Look again at what your forall constraint is saying.

When x is 0: 

array[0+1] == something[5] \ / array[-1] == something[4]

And when x is n: 

array[n+1] == something[5] \ / array[n-1] == something[4]

Do your indices match up with the size of your array? X being 0 might not give you issues because of the array[0 + 1] evaluation being true, meaning 0 - 1 wouldn't be evaluated. However x being n would try to evaluate array[n+1] first and give the error. 

kmarlis
Posts: 35
Posted 08:16 Nov 08, 2019 |

Oh I see I'm too slow!