在Gecode中,我使用评价函数在分支时选择变量。
为了计算变量 v 的价值,我需要访问一些其他变量值,但看起来在调用价值函数时,空间变量还没有被分配任何值:
Exception: IntVar::val: Attempt to access value of unassigned variable.
难道我做错了什么?有没有办法访问评价函数中的变量值?
在Gecode中,我使用评价函数在分支时选择变量。
为了计算变量 v 的价值,我需要访问一些其他变量值,但看起来在调用价值函数时,空间变量还没有被分配任何值:
Exception: IntVar::val: Attempt to access value of unassigned variable.
难道我做错了什么?有没有办法访问评价函数中的变量值?
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.