Optaplanner:可以提前终止"返回最佳解决方案而不是最后计算的解决方案?

时间:2014-06-23 13:19:16

标签: optaplanner

对于我的项目,我想" solver.terminateEarly()"返回最佳解决方案的方法,而不是最后计算的解决方案。有什么办法吗?

以下是求解器类:

public class ProblemSolver implements SolverEventListener {

private static ProblemSolver instance = new ProblemSolver();
private volatile Solver solver; 
private ScoreDirector  guiScoreDirector;
private Problem unsolvedProblem;
public static final String SOLVER_CONFIG = "/com/abcdl/be/solver/ABCDLSolverConfig.xml";
private ProblemSolver(){
}

public static ProblemSolver getInstance(){
    return instance;
}

public void initializeSolver()
{
    System.out.println("------------------Start SOLVE--------------------");
    createSolver();
    System.out.println("------------------Solver Created-------------------");
    createScoreDirector(solver);
    System.out.println("------------------Create Planning Problem-------------------");
    createPlannningProblem();
    System.out.println("------------------Planning Problem created-------------------");
}

/**
 *   // Build the Solver
 * @return
 */
public void createSolver()
{
    try{
         SolverFactory solverFactory = new XmlSolverFactory(SOLVER_CONFIG);
            solver = solverFactory.buildSolver();
            solver.addEventListener(this);
        }
    catch (Exception e) {
         e.printStackTrace();

    }
}


public void createScoreDirector(Solver solver)
{
     ScoreDirectorFactory scoreDirectorFactory = solver.getScoreDirectorFactory();
     guiScoreDirector = scoreDirectorFactory.buildScoreDirector();
}


public void createPlannningProblem()
{
    unsolvedProblem = new Problem();

    Collection<Node> nodeList =  new ArrayList<Node>(Graph.getInstance().getNodes());
    nodeList.remove(Graph.getInstance().getNt()); // the root is removed from the list
    unsolvedProblem.setNodeList(nodeList);

    guiScoreDirector.setWorkingSolution(unsolvedProblem);
}

/**
 * Notifies the solver that it should stop at its earliest convenience.
 * @return true if successful
 */
public boolean terminateEarly()
{
    if (solver == null)
        return false;

    return solver.terminateEarly();
}

public void bestSolutionChanged(BestSolutionChangedEvent event) {
     if (solver.isEveryProblemFactChangeProcessed()) {
         // final is needed for thread visibility
         final Solution latestBestSolution = event.getNewBestSolution();
         // Migrate it to the event thread
         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 guiScoreDirector.setWorkingSolution(latestBestSolution);
                 Tab2.getInstance().getProgressPB().setString(""+latestBestSolution.getScore());
             }
         });
     }
 }

public Solver getSolver() {
    return solver;
}

public ScoreDirector getGuiScoreDirector() {
    return guiScoreDirector;
}

}

求解器任务:

public static class SolveTask extends SwingWorker<Solution, Void> {

    private Solution bestSolution = null;

    protected Solution doInBackground() throws Exception {
        final Solution planningProblem = ProblemSolver.getInstance().getGuiScoreDirector().getWorkingSolution();
        System.out.println("------------------SET Planning Problem-------------------");
        ProblemSolver.getInstance().getSolver().setPlanningProblem(planningProblem);
        System.out.println("------------------Solving the problem-------------------");
        ProblemSolver.getInstance().getSolver().solve(); // solve the problem using the constraint satisfaction solver
        bestSolution = (Problem) ProblemSolver.getInstance().getSolver().getBestSolution();

        return bestSolution;
    }

    protected void done()
    {
        Solution newSolution =  null;
        try {
            newSolution = get();
        } catch (CancellationException e) {
            JOptionPane.showMessageDialog(null, "\nSolving process was cancelled\n", "",
                      JOptionPane.WARNING_MESSAGE);
        }
        catch (Exception e) {
             e.printStackTrace();
             JOptionPane.showMessageDialog(null, "\nSolving process was terminated due to an unhandled exception\n", "ERROR",
                      JOptionPane.ERROR_MESSAGE);
        }
        if (newSolution != null) {
            ProblemSolver.getInstance().getGuiScoreDirector().setWorkingSolution(newSolution);
            Tab2.getInstance().getProgressPB().setString(""+ProblemSolver.getInstance().getGuiScoreDirector().calculateScore());
            Tab2.getInstance().setScore(""+ProblemSolver.getInstance().getGuiScoreDirector().calculateScore());
         }

         System.out.println("------------------END SOLVER--------------------");
    }
}

0 个答案:

没有答案