resourceList = new ArrayList<>();
+ for (LSPPlan lspPlan : lspPlans) {
+ for (LogisticChain logisticChain : lspPlan.getLogisticChains()) {
+ for (LogisticChainElement logisticChainElement : logisticChain.getLogisticChainElements()) {
+ resourceList.add(logisticChainElement.getResource());
+ }
+ }
+ }
+ return resourceList;
+ }
+}
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 bad24f1a..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,14 +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
@@ -21,21 +24,42 @@
* @author Kai Martins-Turner (kturner)
*/
public class CarrierSchedulerUtils {
- public static Carrier routeCarrier(Carrier carrier, Network network) {
- VehicleRoutingProblem.Builder vrpBuilder =
- MatsimJspritFactory.createRoutingProblemBuilder(carrier, network);
- NetworkBasedTransportCosts.Builder tpcostsBuilder =
- NetworkBasedTransportCosts.Builder.newInstance(
- network, ResourceImplementationUtils.getVehicleTypeCollection(carrier));
- NetworkBasedTransportCosts netbasedTransportCosts = tpcostsBuilder.build();
- vrpBuilder.setRoutingCost(netbasedTransportCosts);
- VehicleRoutingProblem vrp = vrpBuilder.build();
+
+ /**
+ * Creates a VehicleRoutingProblem from a carrier and a network and solves it with Jsprit.
+ *
+ * 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.
+ *
+ * @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???
+ *
Pro: saves computation times,
+ * Con: There is now update of the costs if the network (load) changes.
+ * --> do it at least per Carrier or generally or stay as it is? --> Discuss with KN
+ * @Todo: Make the number of jsprit-Iterations configurable
+ *
+ * @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.
+ */
+ 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(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);
@@ -43,6 +67,45 @@ public static Carrier routeCarrier(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) {
diff --git a/src/main/java/org/matsim/freight/logistics/resourceImplementations/CollectionCarrierScheduler.java b/src/main/java/org/matsim/freight/logistics/resourceImplementations/CollectionCarrierScheduler.java
index 2d880d20..b0103bf2 100644
--- a/src/main/java/org/matsim/freight/logistics/resourceImplementations/CollectionCarrierScheduler.java
+++ b/src/main/java/org/matsim/freight/logistics/resourceImplementations/CollectionCarrierScheduler.java
@@ -69,7 +69,7 @@ public void scheduleResource() {
CarrierService carrierService = convertToCarrierService(tupleToBeAssigned);
carrier.getServices().put(carrierService.getId(), carrierService);
}
- carrier = CarrierSchedulerUtils.routeCarrier(carrier, resource.getNetwork());
+ carrier = CarrierSchedulerUtils.solveVrpWithJsprit(carrier, resource.getNetwork());
}
private CarrierService convertToCarrierService(LspShipmentWithTime tuple) {
diff --git a/src/main/java/org/matsim/freight/logistics/resourceImplementations/DistributionCarrierScheduler.java b/src/main/java/org/matsim/freight/logistics/resourceImplementations/DistributionCarrierScheduler.java
index 51f13267..031495e7 100644
--- a/src/main/java/org/matsim/freight/logistics/resourceImplementations/DistributionCarrierScheduler.java
+++ b/src/main/java/org/matsim/freight/logistics/resourceImplementations/DistributionCarrierScheduler.java
@@ -88,7 +88,7 @@ protected void scheduleResource() {
> vehicleType.getCapacity().getOther().intValue()) {
load = 0;
Carrier auxiliaryCarrier =
- CarrierSchedulerUtils.routeCarrier(
+ CarrierSchedulerUtils.solveVrpWithJsprit(
createAuxiliaryCarrier(
shipmentsInCurrentTour, availabilityTimeOfLastShipment + cumulatedLoadingTime),
resource.getNetwork());
@@ -105,7 +105,7 @@ protected void scheduleResource() {
if (!shipmentsInCurrentTour.isEmpty()) {
Carrier auxiliaryCarrier =
- CarrierSchedulerUtils.routeCarrier(
+ CarrierSchedulerUtils.solveVrpWithJsprit(
createAuxiliaryCarrier(
shipmentsInCurrentTour, availabilityTimeOfLastShipment + cumulatedLoadingTime),
resource.getNetwork());