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 #294 from matsim-vsp/kmt_2echelon4Diss
Browse files Browse the repository at this point in the history
Remove LSPCarrierPair from Schedulers
  • Loading branch information
kt86 authored Aug 16, 2024
2 parents b052bd9 + 37d94ef commit 4bae9b0
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 193 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
/*
*********************************************************************** *
* project: org.matsim.*
* *
* *********************************************************************** *
* *
* copyright : (C) 2022 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* ***********************************************************************
*********************************************************************** *
* project: org.matsim.*
* *
* *********************************************************************** *
* *
* copyright : (C) 2022 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* ***********************************************************************
*/

package org.matsim.freight.logistics.resourceImplementations;

import java.util.ArrayList;
import java.util.Objects;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.freight.carriers.Carrier;
import org.matsim.freight.carriers.CarrierService;
import org.matsim.freight.carriers.ScheduledTour;
import org.matsim.freight.carriers.Tour;
import org.matsim.freight.carriers.*;
import org.matsim.freight.carriers.Tour.Leg;
import org.matsim.freight.carriers.Tour.ServiceActivity;
import org.matsim.freight.carriers.Tour.TourElement;
import org.matsim.freight.logistics.*;
import org.matsim.freight.logistics.shipment.LspShipment;
Expand All @@ -44,9 +45,10 @@
*/
/*package-private*/ class CollectionCarrierScheduler extends LSPResourceScheduler {

Logger log = LogManager.getLogger(CollectionCarrierScheduler.class);

private Carrier carrier;
private CollectionCarrierResource resource;
private ArrayList<LSPCarrierPair> pairs;
private final Scenario scenario;

/**
Expand All @@ -56,13 +58,11 @@
* @param scenario the road pricing scheme
*/
CollectionCarrierScheduler(Scenario scenario) {
this.pairs = new ArrayList<>();
this.scenario = scenario;
}

@Override
public void initializeValues(LSPResource resource) {
this.pairs = new ArrayList<>();
if (resource.getClass() == CollectionCarrierResource.class) {
this.resource = (CollectionCarrierResource) resource;
this.carrier = this.resource.getCarrier();
Expand All @@ -82,15 +82,17 @@ public void scheduleResource() {
}

private CarrierService convertToCarrierService(LspShipment lspShipment) {
Id<CarrierService> serviceId =
Id.create(lspShipment.getId().toString(), CarrierService.class);
CarrierService.Builder builder =
CarrierService.Builder.newInstance(serviceId, lspShipment.getFrom());
builder.setServiceStartTimeWindow(lspShipment.getPickupTimeWindow());
builder.setCapacityDemand(lspShipment.getSize());
builder.setServiceDuration(lspShipment.getDeliveryServiceTime());
CarrierService carrierService = builder.build();
pairs.add(new LSPCarrierPair(lspShipment, carrierService));
Id<CarrierService> serviceId = Id.create(lspShipment.getId().toString(), CarrierService.class);
CarrierService carrierService = CarrierService.Builder.newInstance(serviceId, lspShipment.getFrom())
.setServiceStartTimeWindow(TimeWindow.newInstance(lspShipment.getPickupTimeWindow().getStart(), lspShipment.getPickupTimeWindow().getEnd()))
.setCapacityDemand(lspShipment.getSize())
.setServiceDuration(lspShipment.getDeliveryServiceTime())
.build();
//ensure that the ids of the lspShipment and the carrierService are the same. This is needed for updating the LSPShipmentPlan
if (! Objects.equals(lspShipment.getId().toString(), carrierService.getId().toString())) {
log.error("Id of LspShipment: {} and CarrierService: {} do not match", lspShipment.getId().toString(), carrierService.getId().toString(),
new IllegalStateException("Id of LspShipment and CarrierService do not match"));
}
return carrierService;
}

Expand All @@ -100,17 +102,13 @@ protected void updateShipments() {
for (ScheduledTour scheduledTour : carrier.getSelectedPlan().getScheduledTours()) {
Tour tour = scheduledTour.getTour();
for (TourElement element : tour.getTourElements()) {
if (element instanceof Tour.ServiceActivity serviceActivity) {
LSPCarrierPair carrierPair = new LSPCarrierPair(lspShipment, serviceActivity.getService());
for (LSPCarrierPair pair : pairs) {
if (pair.lspShipment == carrierPair.lspShipment
&& pair.carrierService.getId() == carrierPair.carrierService.getId()) {
addShipmentLoadElement(lspShipment, tour, serviceActivity);
addShipmentTransportElement(lspShipment, tour, serviceActivity);
addShipmentUnloadElement(lspShipment, tour);
addCollectionTourEndEventHandler(pair.carrierService, lspShipment, resource, tour);
addCollectionServiceEventHandler(pair.carrierService, lspShipment, resource);
}
if (element instanceof ServiceActivity serviceActivity) {
if (Objects.equals(lspShipment.getId().toString(), serviceActivity.getService().getId().toString())) {
addShipmentLoadElement(lspShipment, tour, serviceActivity);
addShipmentTransportElement(lspShipment, tour, serviceActivity);
addShipmentUnloadElement(lspShipment, tour);
addCollectionTourEndEventHandler(serviceActivity.getService(), lspShipment, resource, tour);
addCollectionServiceEventHandler(serviceActivity.getService(), lspShipment, resource);
}
}
}
Expand All @@ -119,10 +117,10 @@ protected void updateShipments() {
}

private void addShipmentLoadElement(
LspShipment lspShipment, Tour tour, Tour.ServiceActivity serviceActivity) {
LspShipment lspShipment, Tour tour, ServiceActivity serviceActivity) {

LspShipmentUtils.ScheduledShipmentLoadBuilder builder =
LspShipmentUtils.ScheduledShipmentLoadBuilder.newInstance();
LspShipmentUtils.ScheduledShipmentLoadBuilder.newInstance();
builder.setResourceId(resource.getId());

for (LogisticChainElement element : resource.getClientElements()) {
Expand All @@ -134,23 +132,23 @@ private void addShipmentLoadElement(
int serviceIndex = tour.getTourElements().indexOf(serviceActivity);
Leg legBeforeService = (Leg) tour.getTourElements().get(serviceIndex - 1);
double startTimeOfLoading =
legBeforeService.getExpectedDepartureTime() + legBeforeService.getExpectedTransportTime();
legBeforeService.getExpectedDepartureTime() + legBeforeService.getExpectedTransportTime();
builder.setStartTime(startTimeOfLoading);
builder.setEndTime(startTimeOfLoading + lspShipment.getDeliveryServiceTime());

LspShipmentPlanElement load = builder.build();
String idString =
load.getResourceId() + "" + load.getLogisticChainElement().getId() + load.getElementType();
load.getResourceId() + "" + load.getLogisticChainElement().getId() + load.getElementType();
Id<LspShipmentPlanElement> id = Id.create(idString, LspShipmentPlanElement.class);
LspShipmentUtils.getOrCreateShipmentPlan(super.lspPlan, lspShipment.getId())
.addPlanElement(id, load);
.addPlanElement(id, load);
}

private void addShipmentTransportElement(
LspShipment lspShipment, Tour tour, Tour.ServiceActivity serviceActivity) {
LspShipment lspShipment, Tour tour, Tour.ServiceActivity serviceActivity) {

LspShipmentUtils.ScheduledShipmentTransportBuilder builder =
LspShipmentUtils.ScheduledShipmentTransportBuilder.newInstance();
LspShipmentUtils.ScheduledShipmentTransportBuilder.newInstance();
builder.setResourceId(resource.getId());

for (LogisticChainElement element : resource.getClientElements()) {
Expand All @@ -172,39 +170,39 @@ private void addShipmentTransportElement(
builder.setCarrierService(serviceActivity.getService());
LspShipmentPlanElement transport = builder.build();
String idString =
transport.getResourceId()
+ ""
+ transport.getLogisticChainElement().getId()
+ transport.getElementType();
transport.getResourceId()
+ ""
+ transport.getLogisticChainElement().getId()
+ transport.getElementType();
Id<LspShipmentPlanElement> id = Id.create(idString, LspShipmentPlanElement.class);
LspShipmentUtils.getOrCreateShipmentPlan(super.lspPlan, lspShipment.getId())
.addPlanElement(id, transport);
.addPlanElement(id, transport);
}

private void addCollectionServiceEventHandler(
CarrierService carrierService, LspShipment lspShipment, LSPCarrierResource resource) {
CarrierService carrierService, LspShipment lspShipment, LSPCarrierResource resource) {

for (LogisticChainElement element : this.resource.getClientElements()) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(lspShipment)) {
CollectionServiceEndEventHandler endHandler =
new CollectionServiceEndEventHandler(
carrierService, lspShipment, element, resource);
new CollectionServiceEndEventHandler(
carrierService, lspShipment, element, resource);
lspShipment.addSimulationTracker(endHandler);
break;
}
}
}

private void addCollectionTourEndEventHandler(
CarrierService carrierService,
LspShipment lspShipment,
LSPCarrierResource resource,
Tour tour) {
CarrierService carrierService,
LspShipment lspShipment,
LSPCarrierResource resource,
Tour tour) {
for (LogisticChainElement element : this.resource.getClientElements()) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(lspShipment)) {
LSPTourEndEventHandler handler =
new LSPTourEndEventHandler(
lspShipment, carrierService, element, resource, tour);
new LSPTourEndEventHandler(
lspShipment, carrierService, element, resource, tour);
lspShipment.addSimulationTracker(handler);
break;
}
Expand All @@ -214,7 +212,7 @@ private void addCollectionTourEndEventHandler(
private void addShipmentUnloadElement(LspShipment lspShipment, Tour tour) {

LspShipmentUtils.ScheduledShipmentUnloadBuilder builder =
LspShipmentUtils.ScheduledShipmentUnloadBuilder.newInstance();
LspShipmentUtils.ScheduledShipmentUnloadBuilder.newInstance();
builder.setResourceId(resource.getId());
for (LogisticChainElement element : resource.getClientElements()) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(lspShipment)) {
Expand All @@ -228,13 +226,13 @@ private void addShipmentUnloadElement(LspShipment lspShipment, Tour tour) {

LspShipmentPlanElement unload = builder.build();
String idString =
unload.getResourceId()
+ ""
+ unload.getLogisticChainElement().getId()
+ unload.getElementType();
unload.getResourceId()
+ ""
+ unload.getLogisticChainElement().getId()
+ unload.getElementType();
Id<LspShipmentPlanElement> id = Id.create(idString, LspShipmentPlanElement.class);
LspShipmentUtils.getOrCreateShipmentPlan(super.lspPlan, lspShipment.getId())
.addPlanElement(id, unload);
.addPlanElement(id, unload);
}

private double getUnloadEndTime(Tour tour) {
Expand All @@ -247,5 +245,4 @@ private double getUnloadEndTime(Tour tour) {
return unloadEndTime;
}

private record LSPCarrierPair(LspShipment lspShipment, CarrierService carrierService) {}
}
Loading

0 comments on commit 4bae9b0

Please sign in to comment.