我试着把体育比赛安排成时隙,并希望白天不要有空的时隙(所以要尽早完成)。我认为isEqual和isNotEqual应该有帮助,但仍然停留在Python版本的语法上。我想我已经接近了(下面的相关代码)
在domain.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})"在constraints.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))这会生成和预期的错误: TypeError: get_id()缺少一个必需的位置参数:'self‘
但我无法找到正确的语法-也许使用lambda?
发布于 2021-12-07 14:44:33
你已经接近了;这应该是可行的:
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以支持上述功能时更新此答案)。
https://stackoverflow.com/questions/70252673
复制相似问题