In Gecode, I am using a merit function to select variables when branching.
In order to compute variable v's merit, I need to access some other variables values, but it looks like at the time the merit function is called, the space variables have not yet been asigned any values :
Exception: IntVar::val: Attempt to access value of unassigned variable.
Am I doing something wrong? Is there a way to access variables values in merit functions?
The problem is that while you are still searching a variable won't just have 1 value, its domain is still larger than 1. This means that there might still be different values that a variable can take. Until there is only one value left in its domain you are not allowed to use the val
method.
There are different solutions for this problem depending on how you want to use the value domain:
in
method. This method returns true
if the value is in the domain of the variable.min
and max
methods to compare their domains.size
method, before using the val
method.These are the most general cases, but there are countless ways to interact with variables. Be sure to check the IntVar documentation, where these and all other methods for the IntVar
class are described.