Search code examples
gecode

In Gecode, accessing home/space variables values from merit function


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?


Solution

  • 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:

    • The best way to test a variable against a single value is using the in method. This method returns true if the value is in the domain of the variable.
    • To check variables against each other you would generally use the min and max methods to compare their domains.
    • If the value is only relevant when it is assigned, then you would check that the cardinality (size of the domain) is 1, using the 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.