3

Issue:

In the example - CloudBalancing, why this application cannot reach to solver.getBestSolution()? The application is always running at solver.solve(unsolvedCloudBalance);.

Any suggestions?

Some possible solutions here are to finish at solver.solve(unsolvedCloudBalance);, and then, get the solver.getBestSolution()?

/*
 * Copyright 2012 JBoss Inc
 *
......
 */

......

public class CloudBalancingHelloWorld {

    public static void main(String[] args) {
        // Build the Solver
        SolverFactory solverFactory = SolverFactory.createFromXmlResource(
            "org/optaplanner/examples/cloudbalancing/solver/cloudBalancingSolverConfig.xml");
        Solver solver = solverFactory.buildSolver();

        // Load a problem with 2 computers and 6 processes
        CloudBalance unsolvedCloudBalance = new CloudBalancingGenerator().createCloudBalance(2, 6);

        // Solve the problem
        System.out.println("Test - solver - before");
        solver.solve(unsolvedCloudBalance);
        System.out.println("Test - solver - after");

        // Display the result
        CloudBalance solvedCloudBalance = (CloudBalance) solver.getBestSolution();
        System.out.println("\nSolved cloudBalance with 2 computers and 6 processes:\n"
            + toDisplayString(solvedCloudBalance));
    }

    public static String toDisplayString(CloudBalance cloudBalance) {
        StringBuilder displayString = new StringBuilder();
        for (CloudProcess process : cloudBalance.getProcessList()) {
            CloudComputer computer = process.getComputer();
            displayString.append("  ").append(process.getLabel()).append(" -> ")
                .append(computer == null ? null : computer.getLabel()).append("\n");
        }
        return displayString.toString();
    }

}
Roan
  • 1,200
  • 2
  • 19
  • 32
Michael
  • 55
  • 7
  • 1
    The commented out termination config is a known issue in the CloudBalancing example in 6.2.0.Final and will be fixed for 6.3.0.Beta1. – Geoffrey De Smet May 26 '15 at 06:45
  • Thans a lot, Geoffrey De Smet. The fix will help a lot for some new guys when just began to use OptaPlanner. That makes OptaPlanner much better. Thanks a lot. – Michael May 26 '15 at 07:40

2 Answers2

2

I'm assuming you didn't edit the example's Solver configuration XML file.

If you look at the XML file you're loading the Solver configuration from, it doesn't have a termination clause. Basically, you're telling the Solver to run forever.

You can edit the solver configuration XML file to let it terminate after a given condition. See the documentation on how to do this.

If you want to do it programatically, take a look at the JavaDoc: the SolverConfig#setTerminationConfig method of the SolverConfig class and the TerminationConfig class. You will need to edit the SolverConfig of the SolverFactory after loading the XML file: see the SolverFactory#getSolverConfig method.

Ondrej Skopek
  • 778
  • 10
  • 27
  • Thanks a lot, oskopek. For sure I accept your answer and for sure this answer is helpful. I am one new guy here on OptaPlanner. Thanks a lot. – Michael May 26 '15 at 07:36
2

Thanks a lot, oskopek, this issue was solved after following your suggestion.

Your suggestion is:

You can edit the solver configuration XML file to let it terminate after a given condition. See the documentation on how to do this.

What I did is:

Modify cloudBalancingSolverConfig.xml, use the termination as below. Then application exits after 120 seconds running.

<termination>
    <secondsSpentLimit>120</secondsSpentLimit>
</termination>

Below is the output of the application for 2 computers and 6 processes:

09:57:24.791 [main] INFO  o.d.c.k.b.impl.KieRepositoryImpl - KieModule was added: MemoryKieModule[releaseId=org.default:artifact:1.0.0-SNAPSHOT]
09:57:24.963 [main] DEBUG o.drools.core.impl.KnowledgeBaseImpl - Starting Engine in PHREAK mode
Test - solver - before
09:57:25.182 [main] INFO  o.o.core.impl.solver.DefaultSolver - Solving started: time spent (110), best score (uninitialized/0hard/0soft), environment mode (REPRODUCIBLE), random (JDK with seed 0).
09:57:25.213 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase -     CH step (0), time spent (141), score (0hard/-6000soft), selected move count (2), picked move (com.optaplanner.cloudbalancing.domain.CloudProcess@34abdee4 => com.optaplanner.cloudbalancing.domain.CloudComputer@1f130eaf).
09:57:25.228 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase -     CH step (1), time spent (156), score (0hard/-6000soft), selected move count (2), picked move (com.optaplanner.cloudbalancing.domain.CloudProcess@1494b84d => com.optaplanner.cloudbalancing.domain.CloudComputer@1f130eaf).
09:57:25.228 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase -     CH step (2), time spent (156), score (0hard/-6000soft), selected move count (2), picked move (com.optaplanner.cloudbalancing.domain.CloudProcess@655a5d9c => com.optaplanner.cloudbalancing.domain.CloudComputer@1f130eaf).
09:57:25.244 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase -     CH step (3), time spent (172), score (0hard/-6000soft), selected move count (2), picked move (com.optaplanner.cloudbalancing.domain.CloudProcess@1b5bc39d => com.optaplanner.cloudbalancing.domain.CloudComputer@1f130eaf).
09:57:25.244 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase -     CH step (4), time spent (172), score (0hard/-6000soft), selected move count (2), picked move (com.optaplanner.cloudbalancing.domain.CloudProcess@bc57b40 => com.optaplanner.cloudbalancing.domain.CloudComputer@1f130eaf).
09:57:25.260 [main] DEBUG o.o.c.i.c.DefaultConstructionHeuristicPhase -     CH step (5), time spent (188), score (0hard/-6000soft), selected move count (2), picked move (com.optaplanner.cloudbalancing.domain.CloudProcess@21362712 => com.optaplanner.cloudbalancing.domain.CloudComputer@1f130eaf).
09:57:25.260 [main] INFO  o.o.c.i.c.DefaultConstructionHeuristicPhase - Construction Heuristic phase (0) ended: step total (6), time spent (188), best score (0hard/-6000soft).
09:57:25.712 [main] DEBUG o.o.c.i.l.DefaultLocalSearchPhase -     LS step (0), time spent (640), score (0hard/-8000soft),     best score (0hard/-6000soft), accepted/selected move count (1000/1000), picked move ([com.optaplanner.cloudbalancing.domain.CloudProcess@2575f671, com.optaplanner.cloudbalancing.domain.CloudProcess@72bca894, com.optaplanner.cloudbalancing.domain.CloudProcess@503d56b5, com.optaplanner.cloudbalancing.domain.CloudProcess@433ffad1, com.optaplanner.cloudbalancing.domain.CloudProcess@1fc793c2, com.optaplanner.cloudbalancing.domain.CloudProcess@324dcd31] => com.optaplanner.cloudbalancing.domain.CloudComputer@758f4f03).
09:59:25.083 [main] DEBUG o.o.c.i.l.DefaultLocalSearchPhase -     LS step (1), time spent (120011), score (0hard/-6000soft),     best score (0hard/-6000soft), accepted/selected move count (0/10162968), picked move ([com.optaplanner.cloudbalancing.domain.CloudProcess@503d56b5, com.optaplanner.cloudbalancing.domain.CloudProcess@72bca894, com.optaplanner.cloudbalancing.domain.CloudProcess@324dcd31, com.optaplanner.cloudbalancing.domain.CloudProcess@433ffad1, com.optaplanner.cloudbalancing.domain.CloudProcess@2575f671, com.optaplanner.cloudbalancing.domain.CloudProcess@1fc793c2] => com.optaplanner.cloudbalancing.domain.CloudComputer@1f130eaf).
09:59:25.083 [main] INFO  o.o.c.i.l.DefaultLocalSearchPhase - Local Search phase (1) ended: step total (2), time spent (120011), best score (0hard/-6000soft).
09:59:25.083 [main] INFO  o.o.core.impl.solver.DefaultSolver - Solving ended: time spent (120011), best score (0hard/-6000soft), average calculate count per second (84692), environment mode (REPRODUCIBLE).
Test - solver - after

Solved cloudBalance with 2 computers and 6 processes:
Ondrej Skopek
  • 778
  • 10
  • 27
Michael
  • 55
  • 7