Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
WIP: add method for calculation with toll. Some refactoring on the way
Browse files Browse the repository at this point in the history
  • Loading branch information
kt86 committed Jul 31, 2024
1 parent a84c4a3 commit 9ab7f0c
Showing 1 changed file with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,27 +43,69 @@ 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<VehicleRoutingProblemSolution> 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);
carrier.setSelectedPlan(plan);
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<CarrierPlan> scheduledPlans) {
double score = 0;
for (CarrierPlan scheduledPlan : scheduledPlans) {
Expand Down

0 comments on commit 9ab7f0c

Please sign in to comment.