Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust some carrier stuff #3497

Merged
merged 6 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
*
* @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
Loading