简体   繁体   English

Optaplanner isEqual isNotEqual in python

[英]Optaplanner isEqual isNotEqual in python

I'm trying to schedule sports matches into timeslots and would like to not have empty timeslots during the day (so finish as early as possible).我正在尝试将体育比赛安排到时间段中,并且希望白天不要有空的时间段(所以尽早完成)。 I think isEqual and isNotEqual should help, but stuck in the syntax of the Python version.我认为 isEqual 和 isNotEqual 应该有所帮助,但停留在 Python 版本的语法中。 I think I'm close (relevant code below)我想我很接近(下面的相关代码)

in domain.py在域.py

@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})"

in constraints.py在约束.py

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))

This generates and expected error: TypeError: get_id() missing 1 required positional argument: 'self'这会产生预期错误:TypeError: get_id() missing 1 required positional argument: 'self'

But I can't work out the correct syntax - perhaps using lambda?但我无法找出正确的语法 - 也许使用 lambda?

You are close;你很近; this should work:这应该工作:

def fill_pitches_from_start(constraint_factory):
# A pitch should not be empty if possible
return constraint_factory \
    .from_(TimeslotClass).ifNotExists(MatchClass, Joiners.equal(lambda timeslot: timeslot.get_id(), lambda match: match.get_timeslot() ) )  \
    .join(TimeslotClass).ifExists(MatchClass, Joiners.equal(lambda timeslot: timeslot.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))

It can probably be improved using Joiners.lessThan , but that would require an update to optapy first so Joiners can work with any Python type.它可能可以使用Joiners.lessThan进行改进,但这需要先更新到optapy ,以便 Joiners 可以使用任何 Python 类型。 (Will update this answer when optapy is updated to support said feature). (将在更新optapy以支持所述功能时更新此答案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM