尝试在Optaplanner中为分数添加值(使用Drools)

时间:2014-04-26 14:16:44

标签: java drools optaplanner

我有一个非常简单(可能太简单)的规则,我想在Drools中强制执行,以允许在Optaplanner中将值添加到我的硬分数中。 基本上,在我的解决方案类TaskAssignment中,我生成taskConflictList,每次发生冲突时都会添加taskConflictLog

public List<TaskConflict> calculateTaskConflictList(){
   List<TaskConflict> taskConflictList=new ArrayList<TaskConflict>();
   taskConflictLog=0;
    for(Task leftTask:taskList){
        for(Task rightTask:taskList){
            if(leftTask.solutionEquals(rightTask)==!true){
                if(leftTask.getAssignedDevAsString().equals(rightTask.getAssignedDevAsString())){
                if((rightTask.getAllottedStartTime()<=leftTask.getAllottedStartTime()) //long bit of code here....//){
                    taskConflictList.add(new TaskConflict(leftTask,rightTask));
                    taskConflictLog++;
                }
                }
            }
        }
    }
    return taskConflictList;       
   }

我想要做的就是将此taskConflictLog行为的否定作为Drools中的硬分数。我现在输入了这个:

rule "OnlyDoOneTaskAtTime"
when
    $TA:TaskAssignment($tCL:taskConflictLog)

then
    scoreHolder.addHardConstraintMatch(kcontext,-$tCL);

end

但我收到错误消息$tCL cannot be resolved to a variable

这感觉很容易,但出于某种原因,我无法理解它。这个

是不是一个简单的解决方案

1 个答案:

答案 0 :(得分:1)

在示例中,在数据集中查找以Parametrization结尾的类,例如在考试排班中:

public class InstitutionParametrization extends AbstractPersistable { // SINGLETON per Solution

    private int twoInARowPenalty;
    private int twoInADayPenalty;
    private int periodSpreadLength;
    private int periodSpreadPenalty;
    private int mixedDurationPenalty;
    private int frontLoadLargeTopicSize;
    private int frontLoadLastPeriodSize;
    private int frontLoadPenalty;

    ...
}

然后在DRL中:

rule "twoExamsInADay"
    when
        $institutionParametrization : InstitutionParametrization(twoInADayPenalty != 0)
        $topicConflict : TopicConflict($leftTopic : leftTopic, $rightTopic : rightTopic)
        $leftExam : Exam(topic == $leftTopic, $leftDayIndex : dayIndex, $leftPeriodIndex : periodIndex, period != null)
        $rightExam : Exam(topic == $rightTopic, dayIndex == $leftDayIndex,
            Math.abs($leftPeriodIndex - periodIndex) > 1)
    then
        scoreHolder.addSoftConstraintMatch(kcontext,
                $topicConflict.getStudentSize() * (- $institutionParametrization.getTwoInADayPenalty()));
end

// Exams which share students have to few periods between them
rule "periodSpread"
    when
        $institutionParametrization : InstitutionParametrization(periodSpreadPenalty != 0)
        ...
    then
        scoreHolder.addSoftConstraintMatch(kcontext,
                ... * (- $institutionParametrization.getPeriodSpreadPenalty()));
end