How to achieve the control on job sequence?

public class MultipleTimeWindowExample {

static final int Male = 0;
static final int Female = 1;

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, 10);
    VehicleType vehicleType = vehicleTypeBuilder.build();

	/*
     * get a vehicle-builder and build a vehicle located at (10,10) with type "vehicleType"
	 */
    Builder vehicleBuilder = Builder.newInstance("vehicle");
    vehicleBuilder.setStartLocation(Location.newInstance(0, 0));
    vehicleBuilder.setType(vehicleType);
    VehicleImpl vehicle = vehicleBuilder.build();

	/*
     * build services at the required locations, each with a capacity-demand of 1.
	 */



    Service service1 = Service.Builder.newInstance("1").setPriority(5).addTimeWindow(TimeWindow.newInstance(50,55))
        .addSizeDimension(WEIGHT_INDEX, 1)
        .setLocation(Location.newInstance(10, -3)).build();

    Service service2 = Service.Builder.newInstance("2")
        .addSizeDimension(WEIGHT_INDEX, 1).setPriority(4).addTimeWindow(TimeWindow.newInstance(50,55))
        .setLocation(Location.newInstance(20, 0))
        .build();

    Service service3 = Service.Builder.newInstance("3").setPriority(3).addTimeWindow(TimeWindow.newInstance(50,55))
        .addSizeDimension(WEIGHT_INDEX, 1)
        .setLocation(Location.newInstance(35, 5))
        .build();

    Service service4 = Service.Builder.newInstance("4").setPriority(1).addTimeWindow(TimeWindow.newInstance(50,100))
        .addSizeDimension(WEIGHT_INDEX, 1)
        .setLocation(Location.newInstance(40, 4)).build();

    Service service5 = Service.Builder.newInstance("5").setPriority(6).addTimeWindow(TimeWindow.newInstance(50,55))
        .addSizeDimension(WEIGHT_INDEX, 1)
        .setLocation(Location.newInstance(20, -5)).build();


    VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
    vrpBuilder.addVehicle(vehicle);
    vrpBuilder.addJob(service1).addJob(service2).addJob(service3).addJob(service4).addJob(service5);
    vrpBuilder.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE);
   // vrpBuilder.setRoutingCost(new ManhattanCosts());
    VehicleRoutingProblem problem = vrpBuilder.build();

	/*
     * get the algorithm out-of-the-box.
	 */
    StateManager stateManager = new StateManager(problem);
    ConstraintManager constraintManager = new ConstraintManager(problem, stateManager);

    class FemaleFirst implements HardActivityConstraint {

        private StateManager stateManager;

        public FemaleFirst(StateManager stateManager) {
            this.stateManager = stateManager;
        }

        @Override
        public ConstraintsStatus fulfilled(JobInsertionContext jobInsertionContext, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double departureTimeAtPrevAct) {

                if(newAct instanceof JobActivity) {
                    if(prevAct instanceof JobActivity) {
                        if(((JobActivity) newAct).getJob().getPriority() < ((JobActivity) prevAct).getJob().getPriority()){
                            return ConstraintsStatus.NOT_FULFILLED;
                        }
                    }
                    if(nextAct instanceof JobActivity) {
                        if(((JobActivity) newAct).getJob().getPriority() > ((JobActivity) nextAct).getJob().getPriority()){
                            return ConstraintsStatus.NOT_FULFILLED;
                        }
                    }
                    return ConstraintsStatus.FULFILLED;
                }
            
            return ConstraintsStatus.FULFILLED;
        }
    }
    constraintManager.addConstraint(new FemaleFirst(stateManager), ConstraintManager.Priority.CRITICAL);
    VehicleRoutingAlgorithm algorithm = Jsprit.Builder.newInstance(problem).setStateAndConstraintManager(stateManager,constraintManager)
        .buildAlgorithm();

	/*
     * and search a solution
	 */
    Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();

	/*
     * get the best
	 */
    VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);

// new VrpXMLWriter(problem, solutions).write(“output/problem-with-solution.xml”);

    SolutionPrinter.print(problem, bestSolution, SolutionPrinter.Print.VERBOSE);
	/*
     * plot
	 */

   new GraphStreamViewer(problem, bestSolution).labelWith(GraphStreamViewer.Label.ID).setRenderDelay(500).display();
}

}

Sir @jie31best please review the code.

service3 is supposed to be served after service4, so changing the timewindow end time of service4 won’t help. you will need to either increase service3 timewindow end time or decrease service4 timewindow start time.

back to your request that lower priority job should not be served if a higher priority job is not served, you will need to add a constraint regarding this, as this is different from the constraint you have implemented.

1 Like

Sir, I added the constraint please can you review the code

sir what is logic for that?