簡體   English   中英

Optaplanner 約束流:動態定義它是硬分數還是軟分數

[英]Optaplanner Constraint streams: Dynamically define if its a hard or a soft score

我正在使用 optaplanner 解決員工排班問題。 目前,我正在將用 DRL 編寫的規則重寫為約束流。 我有約束,用戶可以在其中定義,如果他想要規則作為硬約束或軟約束。 在 DRL then ,我可以訪問我的名冊 object ,這樣我可以確定是否必須添加hardConstraintMatchsoftConstraintMatch

如何使用約束流實現這一目標?

這里有一個例子:

 Constraint holiday(ConstraintFactory constraintFactory) { return constraintFactory.forEach(Shift.class).join( Absence.class, Joiners.equal(Shift::getEmployee, Absence::getEmployee), Joiners.greaterThanOrEqual(Shift::getDate, Absence::getStartDate), Joiners.lessThanOrEqual(Shift::getDate, Absence::getEndDate) ).penalize( "holiday", // Here I would need something like roster.getRules().get("holiday").getType() // to define dynamically if I have to add ONE_HARD or ONE_SOFT HardMediumSoftLongScore.ONE_HARD, (shift,absence) -> { Roster roster = shift.getRoster(); return roster.getRules().get("holiday").getPenaltyValue(); } ); }

有人知道如何實現這一目標嗎?

您要查找的內容稱為Constraint Configuration

在解決方案上引入@ConstraintConfiguration后,您的約束將如下所示:

 Constraint holiday(ConstraintFactory constraintFactory) { return constraintFactory.forEach(Shift.class).join(Absence.class, Joiners.equal(Shift::getEmployee, Absence::getEmployee), Joiners.greaterThanOrEqual(Shift::getDate, Absence::getStartDate), Joiners.lessThanOrEqual(Shift::getDate, Absence::getEndDate)).penalizeConfigurable( "holiday", (shift, absence) -> 1); }

為獲得最佳結果,請將匹配權重 ( 1 ) 更改為反映影響大小的值。 (例如,如果缺席長度被用作匹配權重,那么如果缺席時間越長,那么缺席時間就會越多。)

在我的 ConstraintConfiguration 中,我會在構造函數中執行類似的操作:

 public EmployeeRosterConstraintConfiguration(Roster roster) { Rule rule = roster.getRules().get("holiday"); if(rule == null) { holidayConflict = HardMediumSoftLongScore.ofSoft(0); } else if(rule.getType() == HARD_CONSTRAINT) { holidayConflict = HardMediumSoftLongScore.ofHard(rule.getPenaltyValue()); } else { holidayConflict = HardMediumSoftLongScore.ofSoft(rule.getPenaltyValue()); } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM