The entity was never added to this scoreDirector exception during custom cloning

旧街凉风 提交于 2019-12-08 06:33:59

问题


I'm trying to implement custom cloning in my solution, i followed the instructions as in the documentation, and i encountered a roadblock in the form of this exception : The entity was never added to this ScoreDirector. Maybe that specific instance is not in the return values of the PlanningSolution's entity members. I know that this is not true because before the custom cloning, this exception wasn't thrown.

My planningClone method is setup like this :

@Override
public Solution planningClone() {
    Solution clonedSolution = new Solution();
    clonedSolution.id = id;
    clonedSolution.code = code;
    clonedSolution.score = score;
    clonedSolution.field1 = field1;
    clonedSolution.field2 = field2;
    ...............
    clonedSolution.fieldN = fieldN;

    List<PlanningEntity1> clonedPlanningEntity1List= new ArrayList<PlanningEntity1>(planningEntity1List.size());
    List<PlanningEntity2> clonedPlanningEntity2List= new ArrayList<PlanningEntity2>(planningEntity1List.size());

    for (PlanningEntity1 planningEntity: planningEntity1List) {
        clonedPlanningEntity1List.add(planningEntity.clone());
    }
    for (PlanningEntity2 planningEntity: planningEntity2List) {
        clonedPlanningEntity1List.add(planningEntity.clone());
    }
    clonedSolution.planningEntity1List = clonedPlanningEntity1List;
    clonedSolution.planningEntity2List = clonedPlanningEntity2List;
    return clonedSolution;
{

The clone method for my planning entities is implemented through the Java interface Cloneable:

protected PlanningEntity clone() {
    try {
        return (PlanningEntity) super.clone();
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    return null;
}

Just to be sure, i checked every entity instance and their collections to make sure my cloning was working correctly, and it in fact is.

What step am i missing here?


回答1:


If there is one planning entity pointing to another planning entity from a different class or maybe pointing to a list, than the cloning process needs to take care of the references for those planning entities so they point to the cloned objects.This is something that the default cloning process is doing without a problem, and thus leaving the solution in a consistent state. It even updates the Lists of planning entity instances in the parent planning entities correctly (covered by the method "cloneCollectionsElementIfNeeded" from the class "FieldAccessingSolutionCloner" from the OptaPlanner core).

So for example if we have the next two planning entity classes

@PlanningEntity
public class ParentPlanningEntityClass{
    List<ChildPlanningEntityClass> childPlanningEntityClassList;
}

@PlanningEntity
public class ChildPlanningEntityClass{
    ParentPlanningEntityClass parentPlanningEntityClass;
}

The "parentPlanningEntityClass" variable needs to be set to point to the cloned object. When it comes to the list "childPlanningEntityClassList" it first needs to be created from scratch with "new ArrayList();" so that both the working and new best solution (the one that is currently getting cloned) don't point to the same list. At the end the newly created list needs to be filled with the cloned objects.



来源:https://stackoverflow.com/questions/44193111/the-entity-was-never-added-to-this-scoredirector-exception-during-custom-cloning

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!