如何在Java中运行OptaPlanner CVRP

时间:2016-10-19 12:55:14

标签: java optaplanner

我想使用自己的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));
}

1 个答案:

答案 0 :(得分:1)

您可以创建一个SolutionDao对象,然后使用它来使用XStream来加载&#34;未解决的&#34; 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