Skip to content

Commit

Permalink
Merge pull request #3497 from matsim-org/adjustSomeCarrierStuff
Browse files Browse the repository at this point in the history
Adjust some carrier stuff
  • Loading branch information
kt86 authored Sep 30, 2024
2 parents 1956693 + 4b24c27 commit 61e9eb3
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public interface Carrier extends HasPlansAndId<CarrierPlan, Carrier>, Attributab
/**
* Sets a {@link CarrierPlan} as selected.
*
* <p>If selectedPlan in not in plan-collection, it adds it.
* <p>The selected plan should be added to the list of plans before.</p>
*
* @param selectedPlan to be set
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}

Expand Down
Loading

0 comments on commit 61e9eb3

Please sign in to comment.