0

我在尝试解决我的解决方案时面临 NPE:

Exception in thread "main" java.lang.NullPointerException
at java.util.ArrayList.addAll(ArrayList.java:472)
at org.drools.planner.core.domain.variable.CompositePlanningValueRangeDescriptor.extractValues(CompositePlanningValueRangeDescriptor.java:46)
at org.drools.planner.core.domain.variable.PlanningVariableDescriptor.extractPlanningValues(PlanningVariableDescriptor.java:259)
at org.drools.planner.core.heuristic.selector.variable.PlanningValueSelector.initSelectedPlanningValueList(PlanningValueSelector.java:91)
at org.drools.planner.core.heuristic.selector.variable.PlanningValueSelector.phaseStarted(PlanningValueSelector.java:73)
at org.drools.planner.core.heuristic.selector.variable.PlanningValueWalker.phaseStarted(PlanningValueWalker.java:64)
at org.drools.planner.core.heuristic.selector.variable.PlanningVariableWalker.phaseStarted(PlanningVariableWalker.java:62)
at org.drools.planner.core.constructionheuristic.greedyFit.decider.DefaultGreedyDecider.phaseStarted(DefaultGreedyDecider.java:62)
at org.drools.planner.core.constructionheuristic.greedyFit.DefaultGreedyFitSolverPhase.phaseStarted(DefaultGreedyFitSolverPhase.java:112)
at org.drools.planner.core.constructionheuristic.greedyFit.DefaultGreedyFitSolverPhase.solve(DefaultGreedyFitSolverPhase.java:57)
at org.drools.planner.core.solver.DefaultSolver.runSolverPhases(DefaultSolver.java:190)
at org.drools.planner.core.solver.DefaultSolver.solve(DefaultSolver.java:155)
at de.haw.dsms.applicationcore.planning.BalancingApp.main(BalancingApp.java:47)

我使用以下注释对我的计划实体进行了注释,以从解决方案中的两个列表中收集值范围:

@PlanningEntity
public class ScheduleItem implements Cloneable{

private ChangeOfferEvent item;

@PlanningVariable() 
@ValueRanges({
        @ValueRange(type = ValueRangeType.FROM_SOLUTION_PROPERTY, solutionProperty = "offers"),
        @ValueRange(type = ValueRangeType.FROM_SOLUTION_PROPERTY, solutionProperty = "dummies")
})
public ChangeOfferEvent getItem() {
    return item;
}

public void setItem(ChangeOfferEvent item) {
    this.item = item;
}

public ScheduleItem() {
    this.item = null;
}
    ...

这是解决方案:

public class ProductionConsumptionBalancing implements Solution<HardAndSoftLongScore> {

/*
 * Problem facts
 */

// The grid entity offers
private List<ChangeOfferEvent> offers;
// Placeholder events to represent "not used schedule items"
private List<PlaceholderOfferEvent> dummies;
// The total energy consumption in the grid
// [Watt]
private TotalEnergyConsumption totalElectricityConsumption;
// The total energy production in the grid
// [Watt]
private TotalEnergyProduction totalElectricityProduction;

public List<ChangeOfferEvent> getOffers() {
    return offers;
}

public void setOffers(List<ChangeOfferEvent> offers) {
    this.offers = offers;
}

public List<PlaceholderOfferEvent> getDummies() {
    return dummies;
}

public void setDummies(List<PlaceholderOfferEvent> dummies) {
    this.dummies = dummies;
}

public TotalEnergyConsumption getTotalElectricityConsumption() {
    return totalElectricityConsumption;
}

public void setTotalElectricityConsumption(
        TotalEnergyConsumption totalElectricityConsumption) {
    this.totalElectricityConsumption = totalElectricityConsumption;
}

public TotalEnergyProduction getTotalElectricityProduction() {
    return totalElectricityProduction;
}

public void setTotalElectricityProduction(
        TotalEnergyProduction totalElectricityProduction) {
    this.totalElectricityProduction = totalElectricityProduction;
}

/*
 * Problem entities
 */
private List<ScheduleItem> schedule;    

@PlanningEntityCollectionProperty
public List<ScheduleItem> getSchedule() {
    return schedule;
}

public void setSchedule(List<ScheduleItem> schedule) {
    this.schedule = schedule;
}
    ...

奇怪的是,在调试过程中,我发现参数“planningEntity”为空,而不是解决方案中的值。

有没有人遇到同样的问题或知道如何解决这个问题?

谢谢和最好的问候!

PS:这似乎来自方法 initSelectedPlanningValueList:

 private void initSelectedPlanningValueList(AbstractSolverPhaseScope phaseScope) {
 90         if (planningVariableDescriptor.isPlanningValuesCacheable()) {
 91             Collection<?> planningValues =             planningVariableDescriptor.extractPlanningValues(
 92                     phaseScope.getWorkingSolution(), null);
 93             cachedPlanningValues = applySelectionOrder(planningValues);
 94         } else {
 95             cachedPlanningValues = null;
 96         }
 97     }

PSPS:问题解决了。出现问题是因为我忘记将克隆的虚拟属性链接到原始虚拟列表。所以克隆解决方案中的虚拟列表为空。

@Override
public Solution<HardAndSoftLongScore> cloneSolution() {

    ProductionConsumptionBalancing clone = new ProductionConsumptionBalancing();

    // Transfer consumption and production values
    clone.totalElectricityConsumption = this.totalElectricityConsumption;
    clone.totalElectricityProduction = this.totalElectricityProduction;

    // Shallow copy offer lists (shouldn't change)
    clone.offers = this.offers;

    // Shallow copy of dummy list
    clone.dummies = this.dummies;

    // Deep copy schedule
            ...
4

1 回答 1

0

从 6.0.0.Beta1 开始,OptaPlanner (= Drools Planner) 支持开箱即用的自动克隆。所以你不需要再实现这个cloneSolution()方法,因为规划器会自动计算出来。因为你不再需要实现该方法,所以你不能错误地实现它。

请注意,如果您真的想要,您仍然可以实现自定义克隆方法。

于 2013-05-08T12:10:22.963 回答