未初始化的计划值[错误]

时间:2012-02-15 03:59:31

标签: drools-planner

是否可以将具有未初始化计划价值的计划实体设置为解决方案?

如果有可能,那怎么样? :)

PLZ,帮帮我:)

当我使用非空计划变量执行我的代码时一切正常,但是当我执行而没有设置计划变量时,我有[Exception in thread "main" java.lang.NullPointerException]

2012-02-14 17:32:08,119 [main] INFO  Solver started: time spend (0), score (null), new best score (null), random seed (0). 
Exception in thread "main" java.lang.NullPointerException 
        at org.drools.planner.core.score.definition.HardAndSoftScoreDefinition.calculateTimeGradient(HardAndSoftScoreDefinition.java:73) 
        at org.drools.planner.core.score.definition.HardAndSoftScoreDefinition.calculateTimeGradient(HardAndSoftScoreDefinition.java:25) 
        at org.drools.planner.core.termination.ScoreAttainedTermination.calculateSolverTimeGradient(ScoreAttainedTermination.java:52) 
        at org.drools.planner.core.termination.OrCompositeTermination.calculateSolverTimeGradient(OrCompositeTermination.java:70) 
        at org.drools.planner.core.termination.OrCompositeTermination.calculateSolverTimeGradient(OrCompositeTermination.java:70) 
        at org.drools.planner.core.termination.PhaseToSolverTerminationBridge.calculatePhaseTimeGradient(PhaseToSolverTerminationBridge.java:49) 
        at org.drools.planner.core.localsearch.DefaultLocalSearchSolverPhase.solve(DefaultLocalSearchSolverPhase.java:55) 
        at org.drools.planner.core.solver.DefaultSolver.runSolverPhases(DefaultSolver.java:166) 
        at org.drools.planner.core.solver.DefaultSolver.solve(DefaultSolver.java:138) 
        at kz.bee.drools.planner.dating.DatingMain.start(DatingMain.java:58) 
        at kz.bee.drools.planner.dating.DatingMain.main(DatingMain.java:38) 

这是我的代码:

package kz.bee.drools.planner.dating;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.xml.DOMConfigurator;
import org.drools.planner.config.XmlSolverConfigurer;
import org.drools.planner.core.Solver;
import org.drools.planner.core.event.BestSolutionChangedEvent;
import org.drools.planner.core.event.SolverEventListener;
import org.drools.planner.core.solution.Solution;
import org.drools.planner.core.solver.DefaultSolverScope;


public class DatingMain {

public static final int PERSON_PER_GENDER = 10;

public static final int HOUR_COUNT = 5;

public static final int TABLE_COUNT = 5;

public static final String SOLVER_CONFIG
    = "/datingSolverConfig.xml";

private volatile Solver solver;

public DatingMain() {
    DOMConfigurator.configure(getClass().getResource("/log4j-test.xml"));
}

public static void main(String[] args) {
    DatingMain datingMain = new DatingMain();
    datingMain.init();
    datingMain.start();
}

private void init() {
    XmlSolverConfigurer configurer = new XmlSolverConfigurer();
    configurer.configure(SOLVER_CONFIG);
    solver = configurer.buildSolver();
    this.solver.addEventListener( new SolverEventListener() {
        public void bestSolutionChanged(BestSolutionChangedEvent event) {
            Dating dating = (Dating) solver.getBestSolution();

            print(dating);
        }

    });
    setPlanningProblem();
}

private void start() {
    System.out.println("Start solving ...");
    this.solver.solve();

    Dating dating = (Dating) solver.getBestSolution();


    System.out.println("Best solution:");

    print(dating);

    System.out.println("End solving.");
}

private void setPlanningProblem() {
    List<Table> tableList = new ArrayList<Table>();
    List<Hour> hourList = new ArrayList<Hour>();
    List<Man> manList = new ArrayList<Man>();
    List<Woman> womanList = new ArrayList<Woman>();
    List<Meeting> meetingList = new ArrayList<Meeting>();


    for( int i = 0; i < TABLE_COUNT; i++ ) {
        Table t = new Table();
        t.setId(Long.parseLong(""+i));
        t.setNumber(i+10);

        tableList.add(t);
    }

    for( int i = 0; i < HOUR_COUNT; i++ ) {
        Hour h = new Hour();
        h.setId(Long.parseLong(""+i));
        h.setValue(i+1);

        hourList.add(h);
    }


    for( int j = 0; j < PERSON_PER_GENDER; j++ ) {
        Man m = new Man();
        m.setId(Long.parseLong(""+j));
        m.setName("Mr. #"+(j+1));
        //m.setSuggestionList(new ArrayList<Long>());
        if( j == 0 ) {
            m.setSuggestionList(new ArrayList<Long>());
            m.getSuggestionList().add(7L);
        }
        else if( j == 1 ) {
            m.setSuggestionList(new ArrayList<Long>());
            m.getSuggestionList().add(4L);
            m.getSuggestionList().add(8L);
        }

        Woman w = new Woman();
        w.setId(Long.parseLong(""+j));
        w.setName("Ms. #"+(j+1));

        manList.add(m);
        womanList.add(w);
    }

// manList.add(null); // womanList.add(null);

    System.out.println("man & woman list size = " + manList.size() + " & " + womanList.size());

    for( int j = 0; j < hourList.size(); j++ ) {
        for( int i = 0; i < tableList.size(); i++ ) {
            Meeting m = new Meeting();
            m.setId(Long.parseLong(""+(j*hourList.size() + i)));
            m.setTable(tableList.get(i));
            m.setHour(hourList.get(j));

// m.setMan(null); // m.setWoman(null);                 //m.setMan(manList.get(i));                 //m.setWoman(womanList.get((i + j)%10));

            meetingList.add(m);
        }
    }

    Dating dating = new Dating();
    dating.setId(Long.parseLong("1"));
    dating.setTableList(tableList);
    dating.setHourList(hourList);
    dating.setManList(manList);
    dating.setWomanList(womanList);
    dating.setMeetingList(meetingList);

    this.solver.setPlanningProblem((Solution) dating);
}

private void print(Dating dating) {
    List<Meeting> meetingList = dating.getMeetingList();

    System.out.println("===============================");
    for(Meeting m : meetingList) {
        System.out.printf("# %3s ",m.getId());
        System.out.println(" t:" + m.getTable().getNumber() + " h:"+ m.getHour().getValue() + " m:"+m.getMan() + " w:" + m.getWoman());
    }
    System.out.println("Score (h/s): " + dating.getScore().getHardScore() + "/" + dating.getScore().getSoftScore() + " time: " + solver.getTimeMillisSpend() );

    System.out.println("===============================");
}
}

`

sovlerConfig-XML

<?xml version="1.0" encoding="UTF-8"?>
<solver>
    <solutionClass>kz.bee.drools.planner.dating.Dating</solutionClass>
    <planningEntityClass>kz.bee.drools.planner.dating.Meeting</planningEntityClass>
    <scoreDrl>/datingScoreRules.drl</scoreDrl>
    <scoreDefinition>
        <scoreDefinitionType>HARD_AND_SOFT</scoreDefinitionType>
    </scoreDefinition>
    <termination>
        <terminationCompositionStyle>OR</terminationCompositionStyle>
        <scoreAttained>0hard/1000soft</scoreAttained>
        <maximumSecondsSpend>30</maximumSecondsSpend>
    </termination>
    <localSearch>
        <selector>
            <selector>
                    <moveFactoryClass>kz.bee.drools.planner.dating.solver.move.factory.ManChangeMoveFactory</moveFactoryClass>
        </selector>
        <selector>
            <moveFactoryClass>kz.bee.drools.planner.dating.solver.move.factory.WomanChangeMoveFactory</moveFactoryClass>
        </selector>
    </selector>
    <acceptor>
        <completeSolutionTabuSize>1500</completeSolutionTabuSize>
    </acceptor>
    <forager>
        <!-- <pickEarlyType>NEVER</pickEarlyType> -->
    </forager>
</localSearch>

1 个答案:

答案 0 :(得分:2)

在您的solverConfig中,您在<constructionHeuristic>部分之前缺少<localSearch>部分(使用FIRST_FIT),有关详细信息或大部分示例,请参阅手册。

当发生这种情况时,我将提交JIRA以获得更好的异常消息。