gpt4 book ai didi

java - 如何在 Java 中运行 OptaPlanner CVRP

转载 作者:行者123 更新时间:2023-11-30 07:05:52 25 4
gpt4 key购买 nike

我想使用我自己的 xml 文件在 java 中的 optaplanner 中运行 CVRP 示例。我的问题是我不知道如何提供 xml 文件以及使用哪种方法。如果我实例化一个解决方案并提供 xml 配置。

如何告诉我的解决方案从哪里获取 UnSolved xml 文件?我更喜欢将 xml 文件作为参数。

下面的一些代码片段:

public static void run(){
SolverFactory<VehicleRoutingSolution> solverFactory = SolverFactory.createFromXmlResource("org/optaplanner/examples/vehiclerouting/solver/vehicleRoutingSolverConfig.xml");
Solver<VehicleRoutingSolution> solver = solverFactory.buildSolver();

VehicleRoutingSolution mySolution = new VehicleRoutingSolution();


mySolution.getCustomerList();
mySolution.getVehicleList();
mySolution.getDepotList();


//mySolution.setMyPlanningEntity(myPlanningEntity);

solver.solve(mySolution);

VehicleRoutingSolution bestSolution = (VehicleRoutingSolution)solver.getBestSolution();
System.out.println("Best solution: " + (bestSolution));
}

最佳答案

您可以创建一个 SolutionDao 对象,然后通过 XStream 使用它的 VRP 实现来加载“未解析”的 XML 文件。该格式需要与 OptaPlanner 用于 VRP XML 输入文件的格式相同。

public class TestApp {

public void solveFromFile(File vrpXmlInputFile) {
SolverFactory<VehicleRoutingSolution> solverFactory = SolverFactory.createFromXmlResource("org/optaplanner/examples/vehiclerouting/solver/vehicleRoutingSolverConfig.xml");
Solver<VehicleRoutingSolution> solver = solverFactory.buildSolver();

VehicleRoutingDao solutionDao = new VehicleRoutingDao();
VehicleRoutingSolution solution = solutionDao.readSolution(vrpXmlInputFile);

solver.solve(solution);

VehicleRoutingSolution bestSolution = solver.getBestSolution();
System.out.println("Best solution: " + (bestSolution));
}

public static void main(String[] args) {
if (args.length != 1) {
throw new IllegalArgumentException("Usage: TestApp [pathToVrpXmlInputFile]");
}

TestApp app = new TestApp();
app.solveFromFile(new File(args[0]));
}

}

确保正确设置工作目录(在我的例子中......./optaplanner/optaplanner-examples

关于java - 如何在 Java 中运行 OptaPlanner CVRP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com