Skip to content

Commit

Permalink
renaming, code organization
Browse files Browse the repository at this point in the history
  • Loading branch information
kt86 committed Sep 24, 2024
1 parent 67f4aa5 commit 8d42359
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.graphhopper.jsprit.core.algorithm.VehicleRoutingAlgorithm;
import com.graphhopper.jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListeners;
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
import com.graphhopper.jsprit.core.problem.job.Shipment;
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import com.graphhopper.jsprit.core.util.Solutions;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -64,20 +65,14 @@ public class CarriersUtils {
private static final String ATTR_JSPRIT_SCORE = "jspritScore";
private static final String ATTR_JSPRIT_Time = "jspritComputationTime";

/**
* Enum to decide for which carriers a new solution should be created.
* This makes only a difference if the carriers already have a plan.
*/
public enum CarrierSelectionForSolution {

/**
* Overwrite all existing plans of the carriers and create new solutions.
*/
overwriteAllPlansAndCreateNewSolution,
/**
* Create new solutions only for carriers with no plans. Existing plans of other carriers are not changed.
*/
solutionOnlyForCarrierWithNoPlans,
/**
* Add new plans to existing plans of carriers. The new plans are set as selected plans.
*/
addNewPLansToExistingPlansOfCarrier
solveForAllCarriersAndOverrideExistingPlans, //Overwrite all existing plans of the carriers and create new solutions.
solveOnlyForCarrierWithoutPlans, //Create new solutions only for carriers with no plans. Existing plans of other carriers are not changed.
solveForAllCarriersAndAddPLans // Add new plans to existing plans of carriers. The new plans are set as selected plans.
}

public static Carrier createCarrier(Id<Carrier> id) {
Expand Down Expand Up @@ -191,6 +186,23 @@ public static void setJspritIterations(Carrier carrier, int jspritIterations) {
carrier.getAttributes().putAttribute(JSPRIT_ITERATIONS, jspritIterations);
}

/**
* Runs jsprit and so solves the VehicleRoutingProblem (VRP) for all {@link Carriers}, doing the following steps:
* - creating NetBasedCosts based on the network
* - building and solving the VRP for all carriers using jsprit
* - take the (best) solution, route and add it as {@link CarrierPlan} to the {@link Carrier}.
* <p>
*
* @param scenario the scenario
* @throws ExecutionException, InterruptedException
*/
public static void runJsprit(Scenario scenario) throws ExecutionException, InterruptedException {
CarrierSelectionForSolution usedCarriersSolutionType = CarrierSelectionForSolution.solveForAllCarriersAndOverrideExistingPlans;
log.warn("Running jsprit for all carriers with default solution type: {}", usedCarriersSolutionType);
log.warn("This will overwrite all existing plans of the carriers and create new solutions.");
runJsprit(scenario, usedCarriersSolutionType);
}

/**
* Runs jsprit and so solves the VehicleRoutingProblem (VRP) for all {@link Carriers}, doing the following steps:
* - creating NetBasedCosts based on the network
Expand All @@ -201,8 +213,7 @@ public static void setJspritIterations(Carrier carrier, int jspritIterations) {
* @param scenario the scenario
* @param carriersSolutionType the type of which carriers should be solved
*/
public static void runJsprit(Scenario scenario,
CarrierSelectionForSolution carriersSolutionType) throws ExecutionException, InterruptedException {
public static void runJsprit(Scenario scenario, CarrierSelectionForSolution carriersSolutionType) throws ExecutionException, InterruptedException {

// necessary to create FreightCarriersConfigGroup before submitting to ThreadPoolExecutor
ConfigUtils.addOrGetModule(scenario.getConfig(), FreightCarriersConfigGroup.class);
Expand All @@ -218,20 +229,17 @@ public static void runJsprit(Scenario scenario,
// This also selects the carriers for which a new solution should be created
for (Carrier carrier : carriers.getCarriers().values()) {
switch (carriersSolutionType) {
case overwriteAllPlansAndCreateNewSolution:
carrier.clearPlans();
break;
case solutionOnlyForCarrierWithNoPlans:
if (carrier.getSelectedPlan() != null) {
case solveForAllCarriersAndOverrideExistingPlans -> carrier.clearPlans();
case solveOnlyForCarrierWithoutPlans -> {
if (!carrier.getPlans().isEmpty()) {
continue;
}
break;
case addNewPLansToExistingPlansOfCarrier:
break;
}
case solveForAllCarriersAndAddPLans -> {}
default -> throw new IllegalStateException("Unexpected value: " + carriersSolutionType);
}
carrierActivityCounterMap.put(carrier.getId(), carrierActivityCounterMap.getOrDefault(carrier.getId(), 0) + carrier.getServices().size());
carrierActivityCounterMap.put(carrier.getId(),
carrierActivityCounterMap.getOrDefault(carrier.getId(), 0) + 2 * carrier.getShipments().size());
carrierActivityCounterMap.put(carrier.getId(), carrierActivityCounterMap.getOrDefault(carrier.getId(), 0) + 2 * carrier.getShipments().size());
}

AtomicInteger startedVRPCounter = new AtomicInteger(0);
Expand All @@ -258,23 +266,6 @@ public static void runJsprit(Scenario scenario,
}
}

/**
* Runs jsprit and so solves the VehicleRoutingProblem (VRP) for all {@link Carriers}, doing the following steps:
* - creating NetBasedCosts based on the network
* - building and solving the VRP for all carriers using jsprit
* - take the (best) solution, route and add it as {@link CarrierPlan} to the {@link Carrier}.
* <p>
*
* @param scenario the scenario
* @throws ExecutionException, InterruptedException
*/
public static void runJsprit(Scenario scenario) throws ExecutionException, InterruptedException {

CarrierSelectionForSolution usedCarriersSolutionType = CarrierSelectionForSolution.overwriteAllPlansAndCreateNewSolution;
log.warn("Running jsprit for all carriers with default solution type: {}", usedCarriersSolutionType);
log.warn("This will overwrite all existing plans of the carriers and create new solutions.");
runJsprit(scenario, usedCarriersSolutionType);
}

/**
* Creates a new {@link Carriers} container only with {@link CarrierShipment}s
Expand Down Expand Up @@ -426,7 +417,7 @@ private static void createShipmentsFromServices(Carrier carrierWS, Carrier carri
}

/**
* Adds a skill to the vehicle's {@link org.matsim.vehicles.VehicleType}.
* Adds a skill to the vehicle's {@link VehicleType}.
*
* @param vehicleType the vehicle type to change;
* @param skill the skill.
Expand Down Expand Up @@ -472,7 +463,7 @@ public static List<String> getSkills(VehicleType type) {
}

/**
* Adds a skill to the {@link com.graphhopper.jsprit.core.problem.job.Shipment}.
* Adds a skill to the {@link Shipment}.
*
* @param shipment the vehicle type to change;
* @param skill the skill.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void testJsprit() throws ExecutionException, InterruptedException {
for (Carrier carrier : CarriersUtils.getCarriers(scenario).getCarriers().values()) {
CarriersUtils.setJspritIterations(carrier, 20);
}
CarriersUtils.runJsprit(scenario, CarriersUtils.CarrierSelectionForSolution.addNewPLansToExistingPlansOfCarrier);
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");
}
Expand Down Expand Up @@ -112,7 +112,7 @@ void testJspritWithDefaultSolutionOption() throws ExecutionException, Interrupte
CarriersUtils.setJspritIterations(carrier, 1);
}

CarriersUtils.runJsprit(scenario, CarriersUtils.CarrierSelectionForSolution.overwriteAllPlansAndCreateNewSolution);
CarriersUtils.runJsprit(scenario, CarriersUtils.CarrierSelectionForSolution.solveForAllCarriersAndOverrideExistingPlans);
double scoreWithRunJsprit = 0;
for (Carrier carrier : CarriersUtils.getCarriers(scenario).getCarriers().values()) {
scoreWithRunJsprit = scoreWithRunJsprit + carrier.getSelectedPlan().getJspritScore();
Expand Down

0 comments on commit 8d42359

Please sign in to comment.