有问题的简单TimeWindowedVehicleRoutingSolution示例

时间:2018-12-11 11:08:06

标签: optaplanner

我目前正在尝试将Optaplanner库实现为简单的TimeWindowedVehicleRoutingSolution示例。我当前的代码试图尽可能简单,并使用了大多数默认的东西,例如vehicleRoutingSolverConfig。

public static void main(String[] args) {
    SolverFactory<TimeWindowedVehicleRoutingSolution> solverFactory = SolverFactory.createFromXmlResource("test/vehicleRoutingSolverConfig.xml");
    solverFactory.getSolverConfig();
    Solver<TimeWindowedVehicleRoutingSolution> solver = null;
    try{
          solver = solverFactory.buildSolver();
    }catch(Exception e){
        System.out.println(e.toString());
    }
    // from 12/07/2018 @ 12:00pm to 12/07/2018 @ 12:30pm
    TimeWindowedCustomer hans = new TimeWindowedCustomer();
    hans.setReadyTime(1544184000);
    hans.setDueTime(1544185800);
    // from 12/07/2018 @ 11:00pm to 12/07/2018 @ 11:30pm
    TimeWindowedCustomer detlef = new TimeWindowedCustomer();
    detlef.setReadyTime(1544180400);
    detlef.setDueTime(1544182200);

    RoadLocation hansRoad = new RoadLocation();
    RoadLocation detlefRoad = new RoadLocation();
    RoadLocation hubRoad = new RoadLocation();
    HashMap hansMap = new HashMap<RoadLocation, Double>();
    //10min
    hansMap.put(detlefRoad, 0.6);
    //15min
    hansMap.put(hubRoad, 0.9);
    hansRoad.setTravelDistanceMap(hansMap);

    HashMap detlefMap = new HashMap<RoadLocation, Double>();
    //10min
    detlefMap.put(hansRoad, 0.6);
    //20min
    detlefMap.put(hubRoad, 1.2);
    detlefRoad.setTravelDistanceMap(detlefMap);

    HashMap hubMap = new HashMap<RoadLocation, Double>();
    //15min
    hubMap.put(hansRoad, 0.9);
    //20min
    hubMap.put(detlefRoad, 1.2);
    hubRoad.setTravelDistanceMap(hubMap);

    TimeWindowedDepot hub = new TimeWindowedDepot();
    hub.setLocation(hubRoad);
    hans.setLocation(hansRoad);
    detlef.setLocation(detlefRoad);

    Vehicle vehicle = new Vehicle();
    vehicle.setDepot(hub);

    List<Customer> customers = new ArrayList<Customer>();
    customers.add(detlef);
    customers.add(hans);

    List<Depot> depots = new ArrayList<Depot>();
    depots.add(hub);

    List<Vehicle> vehicles = new ArrayList<Vehicle>();
    vehicles.add(vehicle);

    List<Location> locations = new ArrayList<Location>();
    locations.add(hansRoad);
    locations.add(detlefRoad);
    locations.add(hubRoad);

    hans.setId(1L);
    detlef.setId(2L);
    hub.setId(3L);
    vehicle.setId(4L);
    hansRoad.setId(5L);
    detlefRoad.setId(6L);
    hubRoad.setId(7L);


    TimeWindowedVehicleRoutingSolution problem = new TimeWindowedVehicleRoutingSolution();
    problem.setCustomerList(customers);
    problem.setDepotList(depots);
    problem.setVehicleList(vehicles);
    problem.setLocationList(locations);
    problem.setDistanceType(DistanceType.ROAD_DISTANCE);

    TimeWindowedVehicleRoutingSolution solution = solver.solve(problem);

    for(Customer c : solution.getCustomerList()) {
        TimeWindowedCustomer tc = (TimeWindowedCustomer) c;
        System.out.println(tc.getArrivalTime());
    }


}

我现在面临的问题是,时间窗口并不是真正的硬性规则,因此建议的路线在时间窗口之前到达。我认为我只是错过了驱动程序可能造成的中断,并且通过查看代码,我认为可能发生中断,只是我的配置没有。所以问题就是,如何打破我的榜样。

0 个答案:

没有答案