OptaPlanner:如何使用链接的变量和影子变量来实现ProblemFactChange / Removed?

时间:2019-07-30 06:05:45

标签: optaplanner

我正在尝试在OptaPlanner中实施类似于TaskAssignment示例的计划。我想拥有一种能力,在求解器解决问题时可以删除任何任务/员工。我按照文档操作,并创建了一个类,该类使用重写的方法doChange()实现了ProblemFactChange接口。但是,在罢免员工时,我遇到了一个异常“实体(乍得)从未添加到此ScoreDirector中。”

我遵循CloudBalancing示例中的代码,其中删除了问题事实“计算机”,并将进程分配给了其余计算机。

@Override
    public void doChange(ScoreDirector<TaskAssigningSolution> scoreDirector) {

        //Get the existing workforce
        Employee existing = scoreDirector.lookUpWorkingObject(employeeToBeDeleted);
        TaskAssigningSolution workingSolution = scoreDirector.getWorkingSolution();

        if (existing == null) {
            //This is already deleted or is invalid
            return;
        }

        //Remove this workforce from all assignments in the working solution
        for (Task eachTask: workingSolution.getTaskList()) {
            if (eachTask.getEmployee() == existing) {
                scoreDirector.beforeVariableChanged(eachTask, "employee");
                eachTask.setEmployee(null);
                scoreDirector.afterVariableChanged(eachTask, "employee");
            }
        }

        // Shallow clone the employeeList so only workingSolution is affected, not bestSolution or guiSolution
        List<Employee> employeeList = new ArrayList<>(workingSolution.getEmployeeList());
        workingSolution.setEmployeeList(employeeList);

        // Remove the problem fact itself
        scoreDirector.beforeProblemFactRemoved(existing);
        employeeList.remove(existing);
        scoreDirector.afterProblemFactRemoved(existing);
        scoreDirector.triggerVariableListeners();
    }

当我简化域模型并删除链式变量和阴影变量时,此方法有效。我想知道是否需要在更改/删除问题事实或实体时修改上述代码以处理链接的/阴影变量。

0 个答案:

没有答案