@Test public void traveledDistanceWithVehicle2ShouldBeCorrect() { Assert.assertEquals( 0d, stateManager.getActivityState( route.getActivities().get(0), vehicle2, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 15d, stateManager.getActivityState( route.getActivities().get(1), vehicle2, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 45d, stateManager.getActivityState( route.getActivities().get(2), vehicle2, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 80d, stateManager.getActivityState( route.getActivities().get(3), vehicle2, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 135d, stateManager.getActivityState( route.getActivities().get(4), vehicle2, traveledDistanceId, Double.class), 0.01); }
@Override public void visit(TourActivity activity) { // waiting time & toolate double waitAtAct = 0.; double tooLate = 0.; if (activityPolicy.equals(ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS)) { waitAtAct = Math.max( 0, activity.getTheoreticalEarliestOperationStartTime() - activity.getArrTime()); tooLate = Math.max(0, activity.getArrTime() - activity.getTheoreticalLatestOperationStartTime()); } sum_waiting_time += waitAtAct; sum_too_late += tooLate; // transport time double transportTime = activity.getArrTime() - prevActDeparture; sum_transport_time += transportTime; prevActDeparture = activity.getEndTime(); // service time sum_service_time += activityCosts.getActivityDuration( activity, activity.getArrTime(), route.getDriver(), route.getVehicle()); stateManager.putActivityState(activity, transport_time_id, sum_transport_time); }
@Test public void traveledDistanceShouldBeCorrect() { Assert.assertEquals( 20d, stateManager.getActivityState( route.getActivities().get(0), vehicle, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 35d, stateManager.getActivityState( route.getActivities().get(1), vehicle, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 65d, stateManager.getActivityState( route.getActivities().get(2), vehicle, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 100d, stateManager.getActivityState( route.getActivities().get(3), vehicle, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 155d, stateManager.getActivityState( route.getActivities().get(4), vehicle, traveledDistanceId, Double.class), 0.01); }
private Set<String> getInitialVehicleIds(VehicleRoutingProblem vehicleRoutingProblem) { Set<String> ids = new HashSet<String>(); for (VehicleRoute r : vehicleRoutingProblem.getInitialVehicleRoutes()) { ids.add(r.getVehicle().getId()); } return ids; }
@Test public void distanceToTravelShouldBeCorrectVehicle2() { double total = stateManager.getRouteState(route, vehicle2, traveledDistanceId, Double.class); Assert.assertEquals( 160d, total - stateManager.getActivityState( route.getActivities().get(0), vehicle2, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 145d, total - stateManager.getActivityState( route.getActivities().get(1), vehicle2, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 115d, total - stateManager.getActivityState( route.getActivities().get(2), vehicle2, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 80d, total - stateManager.getActivityState( route.getActivities().get(3), vehicle2, traveledDistanceId, Double.class), 0.01); Assert.assertEquals( 25d, total - stateManager.getActivityState( route.getActivities().get(4), vehicle2, traveledDistanceId, Double.class), 0.01); }
private double transportCost(TourActivity activity) { return transportCost.getTransportCost( prevAct.getLocation(), activity.getLocation(), prevActDeparture, route.getDriver(), route.getVehicle()); }
private double savings( VehicleRoute route, TourActivity actBefore, TourActivity actToEval, TourActivity act) { double savings = c(actBefore, actToEval, route.getVehicle()) + c(actToEval, act, route.getVehicle()) - c(actBefore, act, route.getVehicle()); return Math.max(0, savings + noiseMaker.makeNoise()); }
private Map<Job, VehicleRoute> map(Collection<VehicleRoute> vehicleRoutes) { Map<Job, VehicleRoute> map = new HashMap<Job, VehicleRoute>(vrp.getJobs().size()); for (VehicleRoute r : vehicleRoutes) { for (Job j : r.getTourActivities().getJobs()) { map.put(j, r); } } return map; }
@Test public void whenVehicleRouteHasPickupAndDeliveryAndPickup_latestStartTimeOfAct1MustBeCorrect() { assertEquals(30., route.getActivities().get(0).getTheoreticalLatestOperationStartTime(), 0.01); assertEquals( 10., stateManager.getActivityState( route.getActivities().get(0), InternalStates.LATEST_OPERATION_START_TIME, Double.class), 0.01); }
@Test public void distanceOfShipmentInRouteVehicle2() { double traveledDistanceBeforePickup = stateManager.getActivityState( route.getActivities().get(2), vehicle2, traveledDistanceId, Double.class); double traveledDistanceBeforeDelivery = stateManager.getActivityState( route.getActivities().get(4), vehicle2, traveledDistanceId, Double.class); Assert.assertEquals(90d, traveledDistanceBeforeDelivery - traveledDistanceBeforePickup, 0.01); }
@Override public void finish() { sum_transport_time += route.getEnd().getArrTime() - prevActDeparture; sum_too_late += Math.max( 0, route.getEnd().getArrTime() - route.getEnd().getTheoreticalLatestOperationStartTime()); stateManager.putRouteState(route, transport_time_id, sum_transport_time); stateManager.putRouteState(route, waiting_time_id, sum_waiting_time); stateManager.putRouteState(route, service_time_id, sum_service_time); stateManager.putRouteState(route, too_late_id, sum_too_late); }
@Override public void finish() { double distance = distanceCalculator.getDistance(prevAct.getLocation(), route.getEnd().getLocation()); sum_distance += distance; stateManager.putRouteState(route, distance_id, sum_distance); }
/** * @param route to get the capacity violation from (at end of the route) * @return violation, i.e. all dimensions and their corresponding violation. For example, if * vehicle has two capacity dimension with dimIndex=0 and dimIndex=1 and dimIndex=1 is * violated by 4 units then this method returns * [[dimIndex=0][dimValue=0][dimIndex=1][dimValue=4]] */ public Capacity getCapacityViolationAtEnd(VehicleRoute route) { if (route == null) throw new IllegalArgumentException("route is missing."); Capacity atEnd = getLoadAtEnd(route); return Capacity.max( Capacity.Builder.newInstance().build(), Capacity.subtract(atEnd, route.getVehicle().getType().getCapacityDimensions())); }
@Test public void whenAddingDeliverShipmentWithVehDiffStartEndLocs_constraintShouldWork() { Shipment shipment = Shipment.Builder.newInstance("s") .setPickupLocation(Location.newInstance(0, 1)) .setDeliveryLocation(Location.newInstance(4, 1)) .build(); VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v") .setStartLocation(Location.newInstance(0, 0)) .setEndLocation(Location.newInstance(0, 4)) .build(); final VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(shipment).addVehicle(vehicle).build(); VehicleRoute route = VehicleRoute.emptyRoute(); JobInsertionContext context = new JobInsertionContext(route, shipment, vehicle, null, 0); context.getAssociatedActivities().add(vrp.getActivities(shipment).get(0)); context.getAssociatedActivities().add(vrp.getActivities(shipment).get(1)); maxDistanceMap = new HashMap<>(); maxDistanceMap.put(vehicle, 10d); StateManager stateManager = new StateManager(vrp); MaxDistanceConstraint maxDistanceConstraint = new MaxDistanceConstraint( stateManager, traveledDistanceId, new TransportDistance() { @Override public double getDistance( Location from, Location to, double departureTime, Vehicle vehicle) { return vrp.getTransportCosts() .getTransportTime(from, to, departureTime, null, vehicle); } }, maxDistanceMap); Assert.assertTrue( maxDistanceConstraint .fulfilled( context, new Start(vehicle.getStartLocation(), 0, Double.MAX_VALUE), vrp.getActivities(shipment).get(0), new End(vehicle.getEndLocation(), 0, Double.MAX_VALUE), 0) .equals(HardActivityConstraint.ConstraintsStatus.FULFILLED)); ActivityContext pickupContext = new ActivityContext(); pickupContext.setArrivalTime(1); pickupContext.setEndTime(1); pickupContext.setInsertionIndex(0); context.setRelatedActivityContext(pickupContext); Assert.assertTrue( maxDistanceConstraint .fulfilled( context, vrp.getActivities(shipment).get(0), vrp.getActivities(shipment).get(1), new End(vehicle.getEndLocation(), 0, Double.MAX_VALUE), 1) .equals(HardActivityConstraint.ConstraintsStatus.FULFILLED)); }
@Test public void insertNewInVehicle2ShouldBeCorrect() { // current distance vehicle2: 160 allowed: 200 MaxDistanceConstraint maxDistanceConstraint = new MaxDistanceConstraint( stateManager, traveledDistanceId, new TransportDistance() { @Override public double getDistance( Location from, Location to, double departureTime, Vehicle vehicle) { return vrp.getTransportCosts() .getTransportTime(from, to, departureTime, null, vehicle); } }, maxDistanceMap); JobInsertionContext context = new JobInsertionContext(route, newDelivery, vehicle2, null, 0); Assert.assertTrue( maxDistanceConstraint .fulfilled(context, route.getStart(), newAct(), act(0), 0) .equals(HardActivityConstraint.ConstraintsStatus.FULFILLED)); // additional distance: 20+35-15=40 Assert.assertTrue( maxDistanceConstraint .fulfilled(context, act(0), newAct(), act(1), 0) .equals(HardActivityConstraint.ConstraintsStatus.FULFILLED)); // additional distance: 35+65-30=70 Assert.assertTrue( maxDistanceConstraint .fulfilled(context, act(1), newAct(), act(2), 0) .equals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED)); // additional distance: 65+100-35 Assert.assertTrue( maxDistanceConstraint .fulfilled(context, act(2), newAct(), act(3), 0) .equals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED)); // additional distance: 100+45-55 Assert.assertTrue( maxDistanceConstraint .fulfilled(context, act(3), newAct(), act(4), 0) .equals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED)); // additional distance: 45+20-25 Assert.assertTrue( maxDistanceConstraint .fulfilled(context, act(4), newAct(), route.getEnd(), 0) .equals(HardActivityConstraint.ConstraintsStatus.FULFILLED)); }
/** * @param route to get the capacity violation from (at activity of the route) * @return violation, i.e. all dimensions and their corresponding violation. For example, if * vehicle has two capacity dimension with dimIndex=0 and dimIndex=1 and dimIndex=1 is * violated by 4 units then this method returns * [[dimIndex=0][dimValue=0][dimIndex=1][dimValue=4]] */ public Capacity getCapacityViolationAfterActivity(TourActivity activity, VehicleRoute route) { if (route == null) throw new IllegalArgumentException("route is missing."); if (activity == null) throw new IllegalArgumentException("activity is missing."); Capacity afterAct = getLoadRightAfterActivity(activity, route); return Capacity.max( Capacity.Builder.newInstance().build(), Capacity.subtract(afterAct, route.getVehicle().getType().getCapacityDimensions())); }
@Test public void distanceOfPickupInRouteVehicle2() { double traveledDistanceBeforePickup = stateManager.getActivityState( route.getActivities().get(3), vehicle2, traveledDistanceId, Double.class); double total = stateManager.getRouteState(route, vehicle2, traveledDistanceId, Double.class); Assert.assertEquals(80d, total - traveledDistanceBeforePickup, 0.01); }
@Override public void begin(VehicleRoute route) { this.route = route; sum_waiting_time = 0.; sum_transport_time = 0.; sum_service_time = 0.; sum_too_late = 0.; prevActDeparture = route.getDepartureTime(); }
private Job getWorst(Collection<VehicleRoute> copied) { Job worst = null; double bestSavings = Double.MIN_VALUE; for (VehicleRoute route : copied) { if (route.isEmpty()) continue; Map<Job, Double> savingsMap = new HashMap<Job, Double>(); TourActivity actBefore = route.getStart(); TourActivity actToEval = null; for (TourActivity act : route.getActivities()) { if (actToEval == null) { actToEval = act; continue; } double savings = savings(route, actBefore, actToEval, act); Job job = ((TourActivity.JobActivity) actToEval).getJob(); if (!savingsMap.containsKey(job)) { savingsMap.put(job, savings); } else { double s = savingsMap.get(job); savingsMap.put(job, s + savings); } actBefore = actToEval; actToEval = act; } double savings = savings(route, actBefore, actToEval, route.getEnd()); Job job = ((TourActivity.JobActivity) actToEval).getJob(); if (!savingsMap.containsKey(job)) { savingsMap.put(job, savings); } else { double s = savingsMap.get(job); savingsMap.put(job, s + savings); } // getCounts best for (Job j : savingsMap.keySet()) { if (savingsMap.get(j) > bestSavings) { bestSavings = savingsMap.get(j); worst = j; } } } return worst; }
/* vehicle: 200.0 vehicle (max distance): 200.0 vehicle2: 160.0 vehicle2 (max distance): 180.0 */ @Test public void insertNewInVehicleShouldFail() { MaxDistanceConstraint maxDistanceConstraint = new MaxDistanceConstraint( stateManager, traveledDistanceId, new TransportDistance() { @Override public double getDistance( Location from, Location to, double departureTime, Vehicle vehicle) { return vrp.getTransportCosts() .getTransportTime(from, to, departureTime, null, vehicle); } }, maxDistanceMap); JobInsertionContext context = new JobInsertionContext(route, newDelivery, vehicle, null, 0); Assert.assertTrue( maxDistanceConstraint .fulfilled(context, route.getStart(), newAct(), act(0), 0) .equals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED)); Assert.assertTrue( maxDistanceConstraint .fulfilled(context, act(0), newAct(), act(1), 0) .equals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED)); Assert.assertTrue( maxDistanceConstraint .fulfilled(context, act(1), newAct(), act(2), 0) .equals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED)); Assert.assertTrue( maxDistanceConstraint .fulfilled(context, act(2), newAct(), act(3), 0) .equals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED)); Assert.assertTrue( maxDistanceConstraint .fulfilled(context, act(3), newAct(), act(4), 0) .equals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED)); Assert.assertTrue( maxDistanceConstraint .fulfilled(context, act(4), newAct(), route.getEnd(), 0) .equals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED)); }
@Override public void visit(TourActivity activity) { boolean violatedAtActivity = false; if (activity instanceof TourActivity.JobActivity) { Set<String> requiredForActivity = ((TourActivity.JobActivity) activity).getJob().getRequiredSkills().values(); for (String skill : requiredForActivity) { if (!route.getVehicle().getSkills().containsSkill(skill)) { violatedAtActivity = true; skillConstraintViolatedOnRoute = true; } } } stateManager.putActivityState(activity, skill_id, violatedAtActivity); }
@Test public void whenEndLocationIsSet_constraintShouldWork() { VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v") .setStartLocation(Location.newInstance(0, 0)) .setEndLocation(Location.newInstance(10, 0)) .build(); Pickup pickup = Pickup.Builder.newInstance("pickup").setLocation(Location.newInstance(10, 0)).build(); vrp = VehicleRoutingProblem.Builder.newInstance().addVehicle(vehicle).addJob(pickup).build(); route = VehicleRoute.emptyRoute(); maxDistanceMap = new HashMap<>(); maxDistanceMap.put(vehicle, 5d); MaxDistanceConstraint maxDistanceConstraint = new MaxDistanceConstraint( new StateManager(vrp), traveledDistanceId, new TransportDistance() { @Override public double getDistance( Location from, Location to, double departureTime, Vehicle vehicle) { return vrp.getTransportCosts() .getTransportTime(from, to, departureTime, null, vehicle); } }, maxDistanceMap); JobInsertionContext context = new JobInsertionContext(route, pickup, vehicle, null, 0); Assert.assertTrue( maxDistanceConstraint .fulfilled( context, new Start(vehicle.getStartLocation(), 0, Double.MAX_VALUE), vrp.getActivities(pickup).get(0), new End(vehicle.getEndLocation(), 0, Double.MAX_VALUE), 0) .equals(HardActivityConstraint.ConstraintsStatus.NOT_FULFILLED)); }
private TourActivity act(int i) { return route.getActivities().get(i); }
@Override public void begin(VehicleRoute route) { this.route = route; this.prevAct = route.getStart(); this.prevActDeparture = route.getDepartureTime(); }
/** * @param route to get the fixed costs from * @return fixed costs of route, i.e. fixed costs of employed vehicle on this route. */ public Double getFixedCosts(VehicleRoute route) { if (route == null) throw new IllegalArgumentException("route is missing."); return route.getVehicle().getType().getVehicleCostParams().fix; }
@Override public void finish() { stateManager.putRouteState(route, last_transport_distance_id, distance(route.getEnd())); stateManager.putRouteState(route, last_transport_time_id, transportTime(route.getEnd())); stateManager.putRouteState(route, last_transport_cost_id, transportCost(route.getEnd())); }
@Override public void begin(VehicleRoute route) { sum_distance = 0.; this.route = route; this.prevAct = route.getStart(); }
/** * @param route to get the total operation time from * @return operation time of this route, i.e. endTime - startTime of specified route */ public Double getOperationTime(VehicleRoute route) { if (route == null) throw new IllegalArgumentException("route is missing."); return route.getEnd().getArrTime() - route.getStart().getEndTime(); }
private void verifyThatRouteContainsAct(TourActivity activity, VehicleRoute route) { if (!route.getTourActivities().hasActivity(activity)) { throw new IllegalArgumentException( "specified route does not contain specified activity " + activity); } }