问题:

在示例-CloudBalancing中,为什么此应用程序无法访问solver.getBestSolution()?该应用程序始终在solver.solve(unsolvedCloudBalance);上运行。

有什么建议?

这里的一些可能的解决方案是在solver.solve(unsolvedCloudBalance);处完成,然后获取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();
    }

}

最佳答案

我假设您没有编辑示例的规划求解配置XML文件。

如果查看XML文件,则正在加载Solver configuration from,它没有终止子句。基本上,您是在说规划求解要永远运行。

您可以编辑求解器配置XML文件,以使其在给定条件后终止。有关如何执行操作,请参见the documentation

如果要以编程方式进行操作,请查看JavaDoc:SolverConfig类的SolverConfig#setTerminationConfig methodTerminationConfig class。加载XML文件后,您将需要编辑SolverFactory的SolverConfig:请参见SolverFactory#getSolverConfig方法。

10-02 22:58