[Python] How to use score analysis with TimeFold in Python?

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I cloned the employee-scheduling from timefold-quickstarts\python\ and started to adapt it to my needs. When I add score analsysis like in the documentation I get the following error:

    Traceback (most recent call last):
    File "SolutionManager.java", line 114, in ai.timefold.solver.core.api.solver.SolutionManager.analyze
    java.lang.java.lang.ClassCastException: java.lang.ClassCastException: Cannot cast ai.timefold.jpyinterpreter.types.PythonLikeType to org.jpyinterpreter.user.employee_scheduling.domain.EmployeeSchedule

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
    File "SolutionManager.java", line 114, in ai.timefold.solver.core.api.solver.SolutionManager.analyze
    Exception: Java Exception

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
    File "<frozen runpy>", line 198, in _run_module_as_main
    File "<frozen runpy>", line 88, in _run_code
    File "D:\Timefold\timefold-quickstarts\python\employee-scheduling\.venv\Scripts\run-app.exe\__main__.py", line 4, in <module>
    File "D:\Timefold\timefold-quickstarts\python\employee-scheduling\src\employee_scheduling\__init__.py", line 3, in <module>
    from .rest_api import app
    File "D:\Timefold\timefold-quickstarts\python\employee-scheduling\src\employee_scheduling\rest_api.py", line 7, in <module>
    from .solver import solver_manager, solution_manager
    File "D:\Timefold\timefold-quickstarts\python\employee-scheduling\src\employee_scheduling\solver.py", line 22, in <module>
    score_analysis = solution_manager.analyze(EmployeeSchedule)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "D:\Timefold\timefold-quickstarts\python\employee-scheduling\.venv\Lib\site-packages\timefold\solver\_solution_manager.py", line 89, in analyze
    return ScoreAnalysis(self._delegate.analyze(convert_to_java_python_like_object(solution)))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    java.lang.java.lang.IllegalStateException: java.lang.IllegalStateException: The property (score) getterMethod (public ai.timefold.solver.core.api.score.buildin.hardsoftbigdecimal.HardSoftBigDecimalScore org.jpyinterpreter.user.employee_scheduling.domain.EmployeeSchedule.getScore()) on bean of class (class ai.timefold.jpyinterpreter.types.PythonLikeType) throws an exception.


    Pretty much the same occurs when using solution_manager.explain(EmployeeSchedule).

    Here is my domain.py:

    from timefold.solver import SolverStatus
    from timefold.solver.domain import *
    from timefold.solver.score import HardSoftDecimalScore
    from datetime import datetime, date
    from typing import Annotated
    from pydantic import Field
    from .json_serialization import *

    # problem facts
    class Employee(JsonDomainBase):
    name: Annotated[str, PlanningId]
    skills: Annotated[set[str], Field(default_factory=set)]
    integration: str
    max_nightshifts_in_a_row: str
    min_nightshifts_in_a_row: str
    unavailable_dates: Annotated[set[date], Field(default_factory=set)]
    undesired_dates: Annotated[set[date], Field(default_factory=set)]
    desired_dates: Annotated[set[date], Field(default_factory=set)]

    @planning_entity
    class Shift(JsonDomainBase):
    id: Annotated[str, PlanningId]
    start: datetime
    end: datetime
    #location: str
    required_skill: str
    shift_name: str
    shift_type: str
    employee: Annotated[Employee | None,
    PlanningVariable,
    Field(default=None)]

    @planning_solution
    class EmployeeSchedule(JsonDomainBase):
    employees: Annotated[list[Employee], ProblemFactCollectionProperty, ValueRangeProvider]
    shifts: Annotated[list[Shift], PlanningEntityCollectionProperty]
    score: Annotated[HardSoftDecimalScore | None,
    PlanningScore, ScoreSerializer, ScoreValidator, Field(default=None)]
    solver_status: Annotated[SolverStatus | None, Field(default=None)]


    and this is my solver.py

    from timefold.solver import SolverManager, SolverFactory, SolutionManager
    from timefold.solver.config import (SolverConfig, ScoreDirectorFactoryConfig,
    TerminationConfig, Duration)
    from .domain import *
    from .constraints import define_constraints


    solver_config = SolverConfig(
    solution_class=EmployeeSchedule,
    entity_class_list=[Shift],
    score_director_factory_config=ScoreDirectorFactoryConfig(
    constraint_provider_function=define_constraints
    ),
    termination_config=TerminationConfig(
    spent_limit=Duration(seconds=30)
    )
    )

    solver_manager = SolverManager.create(SolverFactory.create(solver_config))
    solution_manager = SolutionManager.create(solver_manager)
    score_analysis = solution_manager.analyze(EmployeeSchedule)
    print(score_analysis.summary)


    How do I use score anaysis/explaination in python or is not supported?

    Continue reading...

Compartilhe esta Página