diff --git a/contribs/freight/src/main/java/org/matsim/freight/carriers/Carrier.java b/contribs/freight/src/main/java/org/matsim/freight/carriers/Carrier.java index e4363accdf8..8ee9e1812e5 100644 --- a/contribs/freight/src/main/java/org/matsim/freight/carriers/Carrier.java +++ b/contribs/freight/src/main/java/org/matsim/freight/carriers/Carrier.java @@ -76,7 +76,7 @@ public interface Carrier extends HasPlansAndId, Attributab /** * Sets a {@link CarrierPlan} as selected. * - *

If selectedPlan in not in plan-collection, it adds it. + *

The selected plan should be added to the list of plans before.

* * @param selectedPlan to be set */ diff --git a/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierImpl.java b/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierImpl.java index 5f6abfa909c..8dcc53adc75 100644 --- a/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierImpl.java +++ b/contribs/freight/src/main/java/org/matsim/freight/carriers/CarrierImpl.java @@ -83,9 +83,8 @@ public CarrierPlan getSelectedPlan() { /** * Adds a new CarrierPlan. - * Makes it selected, if no other selectedPlan exists. - * @param carrierPlan - * @return + * Makes it selected if no other selectedPlan exists. + * @param carrierPlan carrierPlan to be added */ @Override public boolean addPlan(CarrierPlan carrierPlan) { @@ -137,7 +136,10 @@ public boolean removePlan(CarrierPlan p) { } @Override - public void clearPlans() { this.plans.clear(); } + public void clearPlans() { + this.plans.clear(); + this.selectedPlan = null; + } @Override public Attributes getAttributes() { diff --git a/contribs/freight/src/main/java/org/matsim/freight/carriers/CarriersUtils.java b/contribs/freight/src/main/java/org/matsim/freight/carriers/CarriersUtils.java index 89842d7c2c5..7c8e7220e3e 100644 --- a/contribs/freight/src/main/java/org/matsim/freight/carriers/CarriersUtils.java +++ b/contribs/freight/src/main/java/org/matsim/freight/carriers/CarriersUtils.java @@ -266,6 +266,36 @@ public static void runJsprit(Scenario scenario, CarrierSelectionForSolution carr } } + /** + * Checks if the selected plan handles all jobs of a carrier. + * The check is done only by counting the number of activities in the selected plan and compare them with the number of services or shipments of the carrier. + * @param carrier the carrier + */ + public static boolean allJobsHandledBySelectedPlan(Carrier carrier) { + if (carrier.getSelectedPlan() == null) { + log.warn("Carrier {}: No selected plan available!", carrier.getId()); + return false; + } + int planedJobs; + int handledJobs; + if (!carrier.getServices().isEmpty()) { + planedJobs = carrier.getServices().size(); + handledJobs = carrier.getSelectedPlan().getScheduledTours().stream().mapToInt( + tour -> (int) tour.getTour().getTourElements().stream().filter(element -> element instanceof Tour.ServiceActivity).count()).sum(); + } else { + planedJobs = carrier.getShipments().size(); + handledJobs = carrier.getSelectedPlan().getScheduledTours().stream().mapToInt( + tour -> (int) tour.getTour().getTourElements().stream().filter( + element -> element instanceof Tour.ShipmentBasedActivity).count()).sum(); + handledJobs = handledJobs / 2; // Shipment has two activities + } + if (planedJobs != handledJobs) { + log.warn("Carrier {}: {} of {} jobs were not handled!", carrier.getId(), planedJobs - handledJobs, planedJobs); + return false; + } else { + return true; + } + } /** * Creates a new {@link Carriers} container only with {@link CarrierShipment}s @@ -686,7 +716,7 @@ else if (!carrier.getShipments().isEmpty()) startedVRPCounter.incrementAndGet(); log.info("started VRP solving for carrier number {} out of {} carriers. Thread id: {}. Priority: {}", startedVRPCounter.get(), taskCount, - Thread.currentThread().getId(), this.priority); + Thread.currentThread().threadId(), this.priority); VehicleRoutingProblem problem = MatsimJspritFactory.createRoutingProblemBuilder(carrier, scenario.getNetwork()) .setRoutingCost(netBasedCosts).build(); @@ -718,10 +748,12 @@ else if (!carrier.getShipments().isEmpty()) NetworkRouter.routePlan(newPlan, netBasedCosts); double timeForPlanningAndRouting = (System.currentTimeMillis() - start) / 1000; log.info("routing for carrier {} finished. Tour planning plus routing took {} seconds. Thread id: {}", carrier.getId(), - timeForPlanningAndRouting, Thread.currentThread().getId()); + timeForPlanningAndRouting, Thread.currentThread().threadId()); carrier.addPlan(newPlan); setJspritComputationTime(carrier, timeForPlanningAndRouting); + if (!allJobsHandledBySelectedPlan(carrier)) + log.warn("Not all jobs of carrier {} are handled by the selected plan.", carrier.getId()); } } diff --git a/contribs/freight/src/test/java/org/matsim/freight/carriers/jsprit/IntegrationIT.java b/contribs/freight/src/test/java/org/matsim/freight/carriers/jsprit/IntegrationIT.java index 11dd862686e..762a2efe08a 100644 --- a/contribs/freight/src/test/java/org/matsim/freight/carriers/jsprit/IntegrationIT.java +++ b/contribs/freight/src/test/java/org/matsim/freight/carriers/jsprit/IntegrationIT.java @@ -30,6 +30,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.Scenario; import org.matsim.api.core.v01.network.Network; import org.matsim.core.config.Config; @@ -85,6 +86,13 @@ void testJsprit() throws ExecutionException, InterruptedException { CarriersUtils.runJsprit(scenario, CarriersUtils.CarrierSelectionForSolution.solveForAllCarriersAndAddPLans); for (Carrier carrier : CarriersUtils.getCarriers(scenario).getCarriers().values()) { Assertions.assertEquals(2, carrier.getPlans().size(), "The number of plans is not as expected"); + // Test method if all jobs are handled + Assertions.assertTrue(CarriersUtils.allJobsHandledBySelectedPlan(carrier), "Not all jobs are handled"); + CarrierService newService = CarrierService.Builder.newInstance(Id.create( + "service" + carrier.getServices().size(), CarrierService.class), Id.createLinkId("100603")) + .setServiceDuration(10.).setServiceStartTimeWindow(TimeWindow.newInstance(0,86000)).build(); + carrier.getServices().put(newService.getId(), newService); + Assertions.assertFalse(CarriersUtils.allJobsHandledBySelectedPlan(carrier), "All jobs are handled although a new service was added"); } } diff --git a/contribs/freight/test/input/org/matsim/freight/carriers/jsprit/IntegrationIT/carrier.xml b/contribs/freight/test/input/org/matsim/freight/carriers/jsprit/IntegrationIT/carrier.xml index 3d59578d423..e14845dfaff 100644 --- a/contribs/freight/test/input/org/matsim/freight/carriers/jsprit/IntegrationIT/carrier.xml +++ b/contribs/freight/test/input/org/matsim/freight/carriers/jsprit/IntegrationIT/carrier.xml @@ -1,198 +1,209 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + + + + + + + + + + +