timefold-solver icon indicating copy to clipboard operation
timefold-solver copied to clipboard

Can't use value ranges from python

Open luxaritas opened this issue 11 months ago • 3 comments

Describe the bug An error is encountered when attempting to annotate a ValueRangeProvider as a CountableValueRange

Expected behavior No error is encountered

Actual behavior

java.lang.java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: The entityClass (class <domainobject>) has a @PlanningVariable annotated property (<property>) that refers to a @ValueRangeProvider annotated member (method $method$get_range on class <domainobject>) that does not return a Collection, an array or a ValueRange

To Reproduce Attach the following to a planning entity and attempt to run the solver

val: Annotated[int, PlanningVariable(value_range_provider_refs=['val'])] = 0
def get_range(self) -> Annotated[CountableValueRange, ValueRangeProvider(id='val')]:
        return ValueRangeFactory.create_int_value_range(0, 10)

Environment

Timefold Solver Version or Git ref: 1.17.0

Output of java -version:

openjdk version "23" 2024-09-17
OpenJDK Runtime Environment (build 23)
OpenJDK 64-Bit Server VM (build 23, mixed mode, sharing)

Output of uname -a or ver:

Linux 6.6.63-1-MANJARO #1 SMP PREEMPT_DYNAMIC Sat Nov 23 02:15:34 UTC 2024 x86_64 GNU/Linux

luxaritas avatar Dec 24 '24 22:12 luxaritas

CountableValueRange is not meant to be used as an annotation. Instead, a value range provider may return instances of this type as a means of providing values, if a collection is not used. (Which is what you're doing.)

Value ranges should only be annotated with ValueRangeProvider - and if there is only one value range, it doesn't need to be annotated at all.

Please remove the annotation and let us know if the issue persists.

triceo avatar Dec 25 '24 06:12 triceo

I’m not using it as an annotation here - I’m using it as the return type. This matches how the docs say to specify the return type of a function using ValueRangeFactory. (Nb: Annotated[ValueRangeProvider] would mean that the return type of the function is ValueRangeProvider)

luxaritas avatar Dec 25 '24 07:12 luxaritas

OK, thanks for clarifying. In that case, this is probably a bug.

triceo avatar Dec 25 '24 08:12 triceo

As of today, Timefold halted any further development of the Python solver. Find out more here: https://github.com/TimefoldAI/timefold-solver/discussions/1698

Closing this issue.

triceo avatar Jul 11 '25 11:07 triceo