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

Commit

Permalink
Merge pull request #289 from matsim-vsp/kmt_2echelon4Diss
Browse files Browse the repository at this point in the history
Improve solveVrpWithJsprit
  • Loading branch information
kt86 authored Aug 12, 2024
2 parents 8405b77 + 2c658b5 commit 624417c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.graphhopper.jsprit.core.util.Solutions;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.roadpricing.RoadPricingScheme;
Expand All @@ -24,88 +26,72 @@
* @author Kai Martins-Turner (kturner)
*/
public class CarrierSchedulerUtils {
private static final Logger log = LogManager.getLogger(CarrierSchedulerUtils.class);

/**
* Creates a VehicleRoutingProblem from a carrier and a network and solves it with Jsprit.
* <p>
* This looks for me (KMT) similar to what is done in {@link org.matsim.freight.carriers.CarriersUtils#runJsprit(Scenario)}.
* So, maybe this can be more simplify.
* <p>
*
* @deprecated please inline; use #solveVrpWithJsprit(Carrier, Network, RoadPricingScheme) instead.
*
* @param carrier Carrier for which the problem should be solved
* @param network the underlying network to create the network based transport costs
* @return Carrier with the solution of the VehicleRoutingProblem and the routed plan.
* @Todo: include toll in the NetbasedCosts (if set), so it is also pat of the VRP
* @Todo: Find a way to reuse the netbasedCosts over the iterations(?) to avoid re-setting this up???
* <li> Pro: saves computation times,
* <li> Con: There is now update of the costs if the network (load) changes.
* <li> --> do it at least per Carrier or generally or stay as it is? --> Discuss with KN
* @Todo: Make the number of jsprit-Iterations configurable
*/
@Deprecated
public static Carrier solveVrpWithJsprit(Carrier carrier, Network network) {
NetworkBasedTransportCosts netbasedTransportCosts =
NetworkBasedTransportCosts.Builder.newInstance(
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(CarriersUtils.getJspritIterations(carrier));

VehicleRoutingProblemSolution solution = Solutions.bestOf(algorithm.searchSolutions());

CarrierPlan plan = MatsimJspritFactory.createPlan(carrier, solution);
NetworkRouter.routePlan(plan, netbasedTransportCosts);
carrier.setSelectedPlan(plan);
return carrier;
return solveVrpWithJsprit(carrier, network, null);
}

/**
* First try with tolls.
* Rest is the same as {@link #solveVrpWithJsprit(Carrier, Network)}.
* //TODO: Combine this method with the untolled version {@link #solveVrpWithJsprit(Carrier, Network)}.
* Creates a VehicleRoutingProblem from a carrier and a network and solves it with Jsprit.
* If a roadPricingScheme is given, the tolls are considered in the routing costs.
* <p>
* This looks for me (KMT) similar to what is done in {@link org.matsim.freight.carriers.CarriersUtils#runJsprit(Scenario)}.
* So, maybe this can be more simplify.
*
* @param carrier Carrier for which the problem should be solved
* @param network the underlying network to create the network based transport costs
* @param roadPricingScheme (MATSim's) road pricing scheme from the roadpricing contrib
* @param roadPricingScheme (MATSim's) road pricing scheme from the roadpricing contrib. If null, no tolls are considered.
* @return Carrier with the solution of the VehicleRoutingProblem and the routed plan.
*/
public static Carrier solveVrpWithJspritWithToll(Carrier carrier, Network network, RoadPricingScheme roadPricingScheme) {
public static Carrier solveVrpWithJsprit(
Carrier carrier, Network network, RoadPricingScheme roadPricingScheme) {
NetworkBasedTransportCosts netbasedTransportCosts;
if (roadPricingScheme != null) {
NetworkBasedTransportCosts netbasedTransportCosts =
NetworkBasedTransportCosts.Builder.newInstance(
network, ResourceImplementationUtils.getVehicleTypeCollection(carrier))
netbasedTransportCosts = NetworkBasedTransportCosts.Builder.newInstance(network, ResourceImplementationUtils.getVehicleTypeCollection(carrier))
.setRoadPricingScheme(roadPricingScheme)
.build();
} else {
log.debug("RoadPricingScheme is null. Tolls cannot be considered.");
netbasedTransportCosts = NetworkBasedTransportCosts.Builder.newInstance(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
//TODO: If not set, setze es auf 1.
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;
VehicleRoutingProblem vrp =
MatsimJspritFactory.createRoutingProblemBuilder(carrier, network)
.setRoutingCost(netbasedTransportCosts)
.build();

} else { //no Toll -> goto previous implementation without toll
return solveVrpWithJsprit(carrier, network);
//If jspritIterations are not set (get.... returns a negativ value), set it to 1
int jspritIterations;
if (CarriersUtils.getJspritIterations(carrier) >= 1) {
jspritIterations = CarriersUtils.getJspritIterations(carrier);
} else {
log.info("Jsprit iterations are not set (properly) for carrier {}. Set to 1.", carrier.getId());
jspritIterations = 1;
}

VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(jspritIterations);
VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions());

CarrierPlan plan = MatsimJspritFactory.createPlan(carrier, solution);
NetworkRouter.routePlan(plan, netbasedTransportCosts);
carrier.setSelectedPlan(plan);
return carrier;
}

public static Double sumUpScore(List<CarrierPlan> scheduledPlans) {
Expand All @@ -121,7 +107,7 @@ public static Double sumUpScore(List<CarrierPlan> scheduledPlans) {
/**
* Sum up the jsprit score of the given list of CarrierPlans.
* As a consequence this is not from the one and only jsprit run, but from all jsprit runs af the different auxiliary carriers.
* @param scheduledPlans
* @param scheduledPlans the scheduled plans with the jsprit results
* @return the summ of the scores coming from jsprit
*/
public static Double sumUpJspritScore(List<CarrierPlan> scheduledPlans) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void scheduleResource() {
CarrierService carrierService = convertToCarrierService(tupleToBeAssigned);
carrier.getServices().put(carrierService.getId(), carrierService);
}
carrier = CarrierSchedulerUtils.solveVrpWithJspritWithToll(carrier, resource.getNetwork(), rpscheme);
carrier = CarrierSchedulerUtils.solveVrpWithJsprit(carrier, resource.getNetwork(), rpscheme);
}

private CarrierService convertToCarrierService(LspShipmentWithTime tuple) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected void scheduleResource() {
> vehicleType.getCapacity().getOther().intValue()) {
load = 0;
Carrier auxiliaryCarrier =
CarrierSchedulerUtils.solveVrpWithJspritWithToll(
CarrierSchedulerUtils.solveVrpWithJsprit(
createAuxiliaryCarrier(
shipmentsInCurrentTour, availabilityTimeOfLastShipment + cumulatedLoadingTime),
resource.getNetwork(), rpscheme);
Expand All @@ -119,7 +119,7 @@ protected void scheduleResource() {

if (!shipmentsInCurrentTour.isEmpty()) {
Carrier auxiliaryCarrier =
CarrierSchedulerUtils.solveVrpWithJspritWithToll(
CarrierSchedulerUtils.solveVrpWithJsprit(
createAuxiliaryCarrier(
shipmentsInCurrentTour, availabilityTimeOfLastShipment + cumulatedLoadingTime),
resource.getNetwork(), rpscheme);
Expand Down

0 comments on commit 624417c

Please sign in to comment.