From 9ab7f0cfa83b37bfab50ff7953da3e1e70d76a27 Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Wed, 31 Jul 2024 12:44:17 +0200 Subject: [PATCH] WIP: add method for calculation with toll. Some refactoring on the way --- .../CarrierSchedulerUtils.java | 65 +++++++++++++++---- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/matsim/freight/logistics/resourceImplementations/CarrierSchedulerUtils.java b/src/main/java/org/matsim/freight/logistics/resourceImplementations/CarrierSchedulerUtils.java index 73e32368..6eb57410 100644 --- a/src/main/java/org/matsim/freight/logistics/resourceImplementations/CarrierSchedulerUtils.java +++ b/src/main/java/org/matsim/freight/logistics/resourceImplementations/CarrierSchedulerUtils.java @@ -5,16 +5,17 @@ import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem; import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution; import com.graphhopper.jsprit.core.util.Solutions; -import java.util.Collection; import java.util.List; import org.matsim.api.core.v01.Scenario; import org.matsim.api.core.v01.network.Network; import org.matsim.freight.carriers.Carrier; import org.matsim.freight.carriers.CarrierPlan; +import org.matsim.freight.carriers.CarriersUtils; import org.matsim.freight.carriers.jsprit.MatsimJspritFactory; import org.matsim.freight.carriers.jsprit.NetworkBasedTransportCosts; import org.matsim.freight.carriers.jsprit.NetworkRouter; +import org.matsim.freight.carriers.jsprit.VehicleTypeDependentRoadPricingCalculator; /** * This class contains some code fragments, that are used in the different *CarrierScheduler @@ -42,20 +43,23 @@ public class CarrierSchedulerUtils { * @return Carrier with the solution of the VehicleRoutingProblem and the routed plan. */ public static Carrier solveVrpWithJsprit(Carrier carrier, Network network) { - VehicleRoutingProblem.Builder vrpBuilder = - MatsimJspritFactory.createRoutingProblemBuilder(carrier, network); - NetworkBasedTransportCosts.Builder tpcostsBuilder = + NetworkBasedTransportCosts netbasedTransportCosts = NetworkBasedTransportCosts.Builder.newInstance( - network, ResourceImplementationUtils.getVehicleTypeCollection(carrier)); - NetworkBasedTransportCosts netbasedTransportCosts = tpcostsBuilder.build(); - vrpBuilder.setRoutingCost(netbasedTransportCosts); - VehicleRoutingProblem vrp = vrpBuilder.build(); + network, ResourceImplementationUtils.getVehicleTypeCollection(carrier)) + .build(); + + VehicleRoutingProblem vrp = + MatsimJspritFactory.createRoutingProblemBuilder(carrier, network) + .setRoutingCost(netbasedTransportCosts) + .build(); + + //Setting jspritIterations to use central infrastructure -> should go more up in the code + CarriersUtils.setJspritIterations(carrier, 1); VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(vrp); - algorithm.setMaxIterations(1); - Collection solutions = algorithm.searchSolutions(); + algorithm.setMaxIterations(CarriersUtils.getJspritIterations(carrier)); - VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions); + VehicleRoutingProblemSolution solution = Solutions.bestOf(algorithm.searchSolutions()); CarrierPlan plan = MatsimJspritFactory.createPlan(carrier, solution); NetworkRouter.routePlan(plan, netbasedTransportCosts); @@ -63,6 +67,45 @@ public static Carrier solveVrpWithJsprit(Carrier carrier, Network network) { return carrier; } + /** + * First try with tolls. + * Rest is the same as {@link #solveVrpWithJsprit(Carrier, Network)}. + * @param carrier Carrier for which the problem should be solved + * @param network the underlying network to create the network based transport costs + * @param roadPricingCalculator the road pricing calculator to calculate the tolls + * @return Carrier with the solution of the VehicleRoutingProblem and the routed plan. + */ + public static Carrier solveVrpWithJspritWithToll(Carrier carrier, Network network, VehicleTypeDependentRoadPricingCalculator roadPricingCalculator) { + if (roadPricingCalculator != null) { + NetworkBasedTransportCosts netbasedTransportCosts = + NetworkBasedTransportCosts.Builder.newInstance( + network, ResourceImplementationUtils.getVehicleTypeCollection(carrier)) + .setRoadPricingCalculator(roadPricingCalculator) + .build(); + + VehicleRoutingProblem vrp = + MatsimJspritFactory.createRoutingProblemBuilder(carrier, network) + .setRoutingCost(netbasedTransportCosts) + .build(); + + //Setting jspritIterations to use central infrastructure -> should go more up in the code + CarriersUtils.setJspritIterations(carrier, 1); + + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setMaxIterations(CarriersUtils.getJspritIterations(carrier)); + VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions()); + + CarrierPlan plan = MatsimJspritFactory.createPlan(carrier, solution); + NetworkRouter.routePlan(plan, netbasedTransportCosts); + carrier.setSelectedPlan(plan); + return carrier; + + } else { //no Toll -> goto previous implementation without toll + return solveVrpWithJsprit(carrier, network); + } + + } + public static Double sumUpScore(List scheduledPlans) { double score = 0; for (CarrierPlan scheduledPlan : scheduledPlans) {