Fixing the Routes in Initial Solution

Initially, there are two vehicle and four jobs and I generated an initial solution having two routes and I add one more job with an initial solution and run the algorithm again. Now what I want that Only one specific route(let’s suppose vehicle 1 route) must provide service to the added job and other routes must remain intact. Another problem is that all the jobs are not being served. job 4 is unassigned.

public class MultipleTimeWindowExample{
static class Windows implements HardRouteConstraint{
Windows( )
{}
@Override
public boolean fulfilled(JobInsertionContext iContext) {

        if(iContext.getNewVehicle().getId()!="1")
            if((iContext.getJob().getId()=="3" || iContext.getJob().getId()=="4")) {
            //    System.out.println("Constrainted " + iContext.getJob().getId() + "/" + iContext.getNewVehicle().getId());
                return false;
            }

        return true;
    } }
public static void main(String[] args) {

	/*
     * get a vehicle type-builder and build a type with the typeId "vehicleType" and one capacity dimension, i.e. weight, and capacity dimension value of 2
	 */
    final int WEIGHT_INDEX = 0;
    VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType")
        .addCapacityDimension(WEIGHT_INDEX, 50).setCostPerWaitingTime(1).setCostPerDistance(1);
    VehicleType vehicleType = vehicleTypeBuilder.build();


	/*
     * get a vehicle-builder and build a vehicle with type "vehicleType"
	 */
    Builder vehicleBuilder1 = VehicleImpl.Builder.newInstance("1");
    vehicleBuilder1.setStartLocation(loc(Coordinate.newInstance(7, 7))).setReturnToDepot(false);
    vehicleBuilder1.setType(vehicleType);
    VehicleImpl vehicle1 = vehicleBuilder1.build();

    Builder vehicleBuilder2 = VehicleImpl.Builder.newInstance("2");
    vehicleBuilder2.setStartLocation(loc(Coordinate.newInstance(7.3,6.5))).setReturnToDepot(false);
    vehicleBuilder2.setType(vehicleType);
    VehicleImpl vehicle2 = vehicleBuilder2.build();

	/*
     * build services at the required locations, each with a capacity-demand of 1.
	 */
    Service service1 = Service.Builder.newInstance("1")
      //  .addTimeWindow(1,2)
        .addSizeDimension(WEIGHT_INDEX, 1).setServiceTime(1).setLocation(Location.newInstance(7.3,6)).build();

    Service service2 = Service.Builder.newInstance("2")
        .setName("2")
       // .addTimeWindow(3,4)
        .addSizeDimension(WEIGHT_INDEX, 1)
        .setLocation(Location.newInstance(8,6)).setServiceTime(1).build();


    Service service5 = Service.Builder.newInstance("5")
        .setName("5")
       // .addTimeWindow(3,4)
        .addSizeDimension(WEIGHT_INDEX, 1)
        .setLocation(Location.newInstance(7.5,7)).setServiceTime(1).build();


    Service service6 = Service.Builder.newInstance("6")
        .setName("6")
       // .addTimeWindow(6,7)
        .addSizeDimension(WEIGHT_INDEX, 1)
        .setLocation(Location.newInstance(7.6,6.6)).setServiceTime(1).build();

/*
Service service7 = Service.Builder.newInstance(“2”)
.addTimeWindow(2,3)
.addSizeDimension(WEIGHT_INDEX, 1)
.setLocation(Location.newInstance(8,6)).setServiceTime(1).build();

    Service service8 = Service.Builder.newInstance("2")
        .addTimeWindow(2,3)
        .addSizeDimension(WEIGHT_INDEX, 1)
        .setLocation(Location.newInstance(8,6)).setServiceTime(1).build();

*/

    VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
    vrpBuilder.addVehicle(vehicle1).addVehicle(vehicle2);
    vrpBuilder.addJob(service1).addJob(service2).addJob(service5).addJob(service6);


    vrpBuilder.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE);
    vrpBuilder.setRoutingCost(new ManhattanCosts());
    VehicleRoutingProblem problem = vrpBuilder.build();



    // Building the algorithm for the problem

    VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(problem);



    //  and search a solution

    Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();
    VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);



	/*
	 * print nRoutes and totalCosts of bestSolution
	 */
    SolutionPrinter.print(bestSolution);

	/*
	 * plot problem without solution
	 */
    Plotter problemPlotter1 = new Plotter(problem);
    problemPlotter1.plotShipments(true);
    problemPlotter1.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_problem1.png", "en-route pickup and delivery1");

	/*
	 * plot problem with solution
	 */
    Plotter solutionPlotter1 = new Plotter(problem, Arrays.asList(bestSolution.getRoutes().iterator().next()));
    solutionPlotter1.plotShipments(true);
    solutionPlotter1.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_solution1.png", "en-route pickup and delivery1");

    new GraphStreamViewer(problem,bestSolution).labelWith(Label.ACTIVITY).setRenderDelay(100).setRenderShipments(true).display();
    // Iterating the all the solutions

    // System.out.println("Hello"+"\n");
 /*
    Iterator itr=solutions.iterator();
    while(itr.hasNext()){
        System.out.println(itr.next());
    }
    System.out.println("End"+"\n");

  */
    /*
       Adding a new service to the intial service
     */
    Service service3 = Service.Builder.newInstance("3")
        .setName("3")
      //  .addTimeWindow(5,6)
        .addSizeDimension(WEIGHT_INDEX, 1)
        .setLocation(Location.newInstance(7.6,7)).setServiceTime(1).build();


    Service service4 = Service.Builder.newInstance("4")
        .setName("4")
      //  .addTimeWindow(0,8)
        .addSizeDimension(WEIGHT_INDEX, 1)
        .setLocation(Location.newInstance(7.7,6)).setServiceTime(1).build();


    VehicleRoutingProblem.Builder tspBuilder = VehicleRoutingProblem.Builder.newInstance();
    tspBuilder.addAllVehicles(problem.getVehicles());
    tspBuilder.addAllJobs(vrpBuilder.getAddedJobs());
    tspBuilder.addJob(service3).addJob(service4);
    //tspBuilder.addInitialVehicleRoutes(bestSolution.getRoutes());
    tspBuilder.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE);
    tspBuilder.setRoutingCost(new ManhattanCosts());

    VehicleRoutingProblem tsp = tspBuilder.build();

     StateManager stateManager = new StateManager(tsp);
     ConstraintManager constraintManager = new ConstraintManager(problem, stateManager);
     constraintManager.addConstraint( new Windows(), ConstraintManager.Priority.CRITICAL);
   //setStateAndConstraintManager(stateManager,constraintManager).
    VehicleRoutingAlgorithm algorithm1 = Jsprit.Builder.newInstance(tsp).setStateAndConstraintManager(stateManager,constraintManager).buildAlgorithm();
    algorithm1.setMaxIterations(20000);
    algorithm1.addInitialSolution(bestSolution);


    Collection<VehicleRoutingProblemSolution> solutions1=algorithm1.searchSolutions();

     System.out.println("Hello"+"\n");

    Iterator itr=solutions1.iterator();
    while(itr.hasNext()){
        VehicleRoutingProblemSolution VR=(VehicleRoutingProblemSolution) itr.next();
        Plotter problemPlotter = new Plotter(tsp);
        problemPlotter.plotShipments(true);
        problemPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_problem.png", "en-route pickup and delivery");

	/*
	 * plot problem with solution
	 */
        Plotter solutionPlotter = new Plotter(tsp, Arrays.asList(VR.getRoutes().iterator().next()));
        solutionPlotter.plotShipments(true);
        solutionPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_solution.png", "en-route pickup and delivery");

        new GraphStreamViewer(tsp,VR).labelWith(Label.ACTIVITY).setRenderDelay(100).setRenderShipments(true).display();



        System.out.println(itr.next());
    }
    System.out.println("End"+"\n");

    VehicleRoutingProblemSolution tspSolution = Solutions.bestOf(algorithm1.searchSolutions());
	/*
     * get the best solution
	 */
    new VrpXMLWriter(tsp,algorithm1.searchSolutions()).write("output/shipment-problem-with-solution.xml");

	/*
	 * print nRoutes and totalCosts of bestSolution
	 */
    SolutionPrinter.print(tspSolution);
	/*
	 * plot problem without solution
	 */
    Plotter problemPlotter = new Plotter(tsp);
    problemPlotter.plotShipments(true);
    problemPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_problem.png", "en-route pickup and delivery");

	/*
	 * plot problem with solution
	 */
    Plotter solutionPlotter = new Plotter(tsp, Arrays.asList(tspSolution.getRoutes().iterator().next()));
    solutionPlotter.plotShipments(true);
    solutionPlotter.plot("output/enRoutePickupAndDeliveryWithMultipleLocationsExample_solution.png", "en-route pickup and delivery");

    new GraphStreamViewer(tsp,tspSolution).labelWith(Label.ACTIVITY).setRenderDelay(100).setRenderShipments(true).display();
	/*
     * plot
	 */

}

private static Location loc(Coordinate coordinate) {
    return Location.Builder.newInstance().setCoordinate(coordinate).build();
}}

This is the same question asked differently, so my suggestion is the same:

1 Like