首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >巨蟒中的OptaPy isEqual isNotEqual

巨蟒中的OptaPy isEqual isNotEqual
EN

Stack Overflow用户
提问于 2021-12-06 22:21:11
回答 1查看 122关注 0票数 0

我试着把体育比赛安排成时隙,并希望白天不要有空的时隙(所以要尽早完成)。我认为isEqual和isNotEqual应该有帮助,但仍然停留在Python版本的语法上。我想我已经接近了(下面的相关代码)

在domain.py中

代码语言:javascript
复制
@problem_fact
class Timeslot:
    def __init__(self, id, match_date, date_str, start_time, end_time):
        self.id = id
        self.match_date = match_date
        self.date_str = date_str # java does not have date or datetime equivalent so str also needed
        self.start_time = start_time
        self.end_time = end_time

    @planning_id
    def get_id(self):
        return self.id

    def __str__(self):
        return (
                f"Timeslot("
                f"id={self.id}, "
                f"match_date={self.match_date}, "
                f"date_str={self.date_str}, "
                f"start_time={self.start_time}, "
                f"end_time={self.end_time})"

在constraints.py中

代码语言:javascript
复制
def fill_pitches_from_start(constraint_factory):
    # A pitch should not be empty if possible
    return constraint_factory \
        .from_(TimeslotClass).ifNotExists(MatchClass, Joiners.equal(Timeslot.get_id(), Match.get_timeslot() ) )  \
        .join(TimeslotClass).ifExists(MatchClass, Joiners.equal(Timeslot.get_id(), Match.get_timeslot())) \
        .filter(lambda slot1, slot2: datetime.combine(slot1.date_str, slot1.timeslot.start_time) < datetime.combine(slot2.date_str, slot2.start_time) ) \
        .penalize("Pitch Empty with Later pitches populated", HardSoftScore.ofSoft(10))

这会生成和预期的错误: TypeError: get_id()缺少一个必需的位置参数:'self‘

但我无法找到正确的语法-也许使用lambda?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-07 14:44:33

你已经接近了;这应该是可行的:

代码语言:javascript
复制
def fill_pitches_from_start(constraint_factory):
# A pitch should not be empty if possible
return constraint_factory \
    .forEach(TimeslotClass).ifNotExists(MatchClass, Joiners.equal(lambda timeslot: timeslot.get_id(), lambda match: match.get_timeslot() ) )  \
    .join(TimeslotClass).ifExists(MatchClass, Joiners.equal(lambda timeslot1, timeslot2: timeslot2.get_id(), lambda match: match.get_timeslot())) \
    .filter(lambda slot1, slot2: datetime.combine(slot1.date_str, slot1.timeslot.start_time) < datetime.combine(slot2.date_str, slot2.start_time) ) \
    .penalize("Pitch Empty with Later pitches populated", HardSoftScore.ofSoft(10))

使用Joiners.lessThan可能会对其进行改进,但这需要首先对optapy进行更新,这样Joiners就可以使用任何类型的Python。(将在更新optapy以支持上述功能时更新此答案)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70252673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档