Timefold SolverFactory.create() - 无效函数:需要 2 个参数,但得到 1 个

问题描述 投票:0回答:1

我正在使用 Timefold Solver,并在尝试创建 SolverFactory 时遇到以下错误:

回溯(最近一次调用最后一次):文件 “G:\PycharmProject\Timefold-training ools\main.py”,第 62 行,位于 solver_factory = SolverFactory.create( 文件 "G:\PycharmProject\Timefold-training env\lib\site-packages imefold\solver_solver_factory.py", 第 52 行,在创建中 delegate = JavaSolverFactory.create(solver_config) # noqa ValueError: 无效函数: 期望 2 个参数,但得到 1

以下是发生错误的相关代码:

solver_factory = SolverFactory.create(
    SolverConfig(
        solution_class=DeviceSchedule,
        entity_class_list=[Device],
        score_director_factory_config=ScoreDirectorFactoryConfig(
            constraint_provider_function=define_constraints
        ),
        termination_config=TerminationConfig(
            spent_limit=Duration(seconds=60)
        )
    )
)

我有以下域类:

from dataclasses import dataclass, field
from typing import Annotated, Set, List
from timefold.solver.domain import PlanningId, planning_entity, planning_solution, PlanningVariable, \
    PlanningEntityCollectionProperty, ProblemFactCollectionProperty, ValueRangeProvider, PlanningScore
from timefold.solver.score import HardSoftScore

@dataclass
class Technician:
    id: Annotated[int, PlanningId]
    name: str
    rbh_do_zaplanowania: float
    rbh_przydzielone: float
    iums: Set[str]

    def has_ium(self, ium: str) -> bool:
        return ium in self.iums

@dataclass
class Device:
    index: Annotated[int, PlanningId]
    ium: str
    nazwa: str
    typ: str
    nr_fabryczny: str
    rbh_norma: float
    dni_w_om: int
    uzytkownik: str
    technician: Annotated[Technician | None, PlanningVariable(allows_unassigned=True)] = field(default=None)

@dataclass
@planning_solution
class DeviceSchedule:
    id: str
    technician_list: Annotated[List[Technician], ProblemFactCollectionProperty, ValueRangeProvider]
    device_list: Annotated[List[Device], PlanningEntityCollectionProperty]
    score: Annotated[HardSoftScore, PlanningScore] = field(default=None)

这是我的constraints.py 文件:

from timefold.solver.score import constraint_provider, HardSoftScore, ConstraintFactory

@constraint_provider
def define_constraints(constraint_factory: ConstraintFactory):
    return [
        technician_capacity_hard(constraint_factory),
        technician_skill_conflict(constraint_factory),
    ]

def technician_skill_conflict(constraint_factory: ConstraintFactory):
    return (
        constraint_factory.for_each(Device)
        .filter(lambda device: device.technician is not None and not device.technician.has_ium(device.ium))
        .penalize(HardSoftScore.ONE_HARD)
        .as_constraint("Technician skill conflict")
    )

def technician_capacity_hard(constraint_factory: ConstraintFactory):
    return (
        constraint_factory.for_each(Device)
        .group_by(lambda device: device.technician, sum(lambda device: device.rbh_norma))
        .filter(lambda technician, total_rbh_norma: total_rbh_norma > technician.rbh_do_zaplanowania)
        .penalize(HardSoftScore.ONE_HARD, lambda technician, total_rbh_norma: total_rbh_norma - technician.rbh_do_zaplanowania)
        .as_constraint("Technician capacity")
    )

我怀疑这个问题可能与我如何使用 SolverFactory.create() 或我的配置有关,但我不确定如何解决它。我已经查看了 Timefold Solver 文档,但无法查明此错误的原因。

以前有人遇到过这个问题吗?或者有人知道可能出了什么问题吗?

python optaplanner timefold
1个回答
0
投票

问题在

technician_capacity_hard
;您需要使用
ConstraintCollectors.sum
而不是
builtins.sum
。这段代码应该可以工作:

from timefold.solver.score import ConstraintCollectors

def technician_capacity_hard(constraint_factory: ConstraintFactory):
    return (
        constraint_factory.for_each(Device)
        .group_by(lambda device: device.technician, ConstraintCollectors.sum(lambda device: device.rbh_norma))
        .filter(lambda technician, total_rbh_norma: total_rbh_norma > technician.rbh_do_zaplanowania)
        .penalize(HardSoftScore.ONE_HARD, lambda technician, total_rbh_norma: total_rbh_norma - technician.rbh_do_zaplanowania)
        .as_constraint("Technician capacity")
    )
© www.soinside.com 2019 - 2024. All rights reserved.