2

Gecode中,我使用评价函数在分支时选择变量。

为了计算变量 v 的价值,我需要访问一些其他变量值,但看起来在调用价值函数时,空间变量还没有被分配任何值:

Exception: IntVar::val: Attempt to access value of unassigned variable.

难道我做错了什么?有没有办法访问评价函数中的变量值?

4

1 回答 1

4

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.

于 2017-11-25T13:36:06.353 回答