From bd80bfa1493e2731160afbea8bd29701bdc8b3f4 Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Fri, 30 Aug 2024 14:34:53 +0200 Subject: [PATCH] WIP commiting just to change the workspace.. --- .../DistributionCarrierScheduler.java | 70 +++++++++++------ .../DistributionServiceStartEventHandler.java | 75 +++++++++++-------- .../logistics/shipment/LspShipmentUtils.java | 6 ++ .../ScheduledLspShipmentTransport.java | 7 ++ 4 files changed, 104 insertions(+), 54 deletions(-) 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 c724e547..814cf83b 100644 --- a/src/main/java/org/matsim/freight/logistics/resourceImplementations/DistributionCarrierScheduler.java +++ b/src/main/java/org/matsim/freight/logistics/resourceImplementations/DistributionCarrierScheduler.java @@ -102,9 +102,9 @@ protected void scheduleResource() { scheduledPlans.add(auxiliaryCarrier.getSelectedPlan()); var vrpLogic = CarrierSchedulerUtils.getVrpLogic(carrier); switch (vrpLogic) { - case serviceBased -> { carrier.getServices().putAll(auxiliaryCarrier.getServices()); } - case shipmentBased -> { carrier.getShipments().putAll(auxiliaryCarrier.getShipments()); } - default -> throw new IllegalStateException("Unexpected value: " + vrpLogic); + case serviceBased -> { carrier.getServices().putAll(auxiliaryCarrier.getServices()); } + case shipmentBased -> { carrier.getShipments().putAll(auxiliaryCarrier.getShipments()); } + default -> throw new IllegalStateException("Unexpected value: " + vrpLogic); } cumulatedLoadingTime = 0; @@ -129,7 +129,7 @@ protected void scheduleResource() { //TODO: When using shipmentbased, only ONE Vrp should be created and solved. -> No need for the auxiliary carrier(s). KMT'Aug 24 //Then we can also just pass all the vehicles over :) //And need the TimeWindows for the Shipments... - } + } default -> throw new IllegalStateException("Unexpected value: " + CarrierSchedulerUtils.getVrpLogic(carrier)); } shipmentsInCurrentTour.clear(); @@ -197,13 +197,13 @@ private CarrierService convertToCarrierService(LspShipment lspShipment) { return carrierService; } -/** -* This method converts a LspShipment to a CarrierShipment. - * Please note: This method may get removed in the future, in case that the LSPShipment and the CarrierShipment are merged. KMT'Aug'24 + /** + * This method converts a LspShipment to a CarrierShipment. + * Please note: This method may get removed in the future, in case that the LSPShipment and the CarrierShipment are merged. KMT'Aug'24 - * @param lspShipment the LspShipment to convert - * @return a CarrierShipment -*/ + * @param lspShipment the LspShipment to convert + * @return a CarrierShipment + */ private CarrierShipment convertToCarrierShipment(LspShipment lspShipment) { Id serviceId = Id.create(lspShipment.getId().toString(), CarrierShipment.class); CarrierShipment carrierShipment = CarrierShipment.Builder.newInstance(serviceId, lspShipment.getFrom(), lspShipment.getTo(), lspShipment.getSize()) @@ -225,16 +225,38 @@ protected void updateShipments() { for (LspShipment lspShipment : lspShipmentsToSchedule) { for (ScheduledTour scheduledTour : carrier.getSelectedPlan().getScheduledTours()) { Tour tour = scheduledTour.getTour(); - for (TourElement element : tour.getTourElements()) { - if (element instanceof ServiceActivity serviceActivity) { - if (Objects.equals(lspShipment.getId().toString(), serviceActivity.getService().getId().toString())) { - addShipmentLoadElement(lspShipment, tour); - addShipmentTransportElement(lspShipment, tour, serviceActivity); - addShipmentUnloadElement(lspShipment, tour, serviceActivity); - addDistributionTourStartEventHandler(serviceActivity.getService(), lspShipment, resource, tour); - addDistributionServiceEventHandler(serviceActivity.getService(), lspShipment, resource); + + switch (CarrierSchedulerUtils.getVrpLogic(carrier)) { + case serviceBased -> { + for (TourElement element : tour.getTourElements()) { + if (element instanceof ServiceActivity serviceActivity) { + if (Objects.equals(lspShipment.getId().toString(), serviceActivity.getService().getId().toString())) { + addShipmentLoadElement(lspShipment, tour); + addShipmentTransportElement(lspShipment, tour, serviceActivity); + addShipmentUnloadElement(lspShipment, tour, serviceActivity); + addDistributionTourStartEventHandler(serviceActivity.getService(), lspShipment, resource, tour); + addDistributionServiceEventHandler(serviceActivity.getService(), lspShipment, resource); + } + } } } + case shipmentBased -> { + //TODO needs fixture + for (TourElement element : tour.getTourElements()) { + if (element instanceof Tour.Delivery deliveryActivity) { + if (Objects.equals(lspShipment.getId().toString(), deliveryActivity.getShipment().getId().toString())) { + addShipmentLoadElement(lspShipment, tour); + addShipmentTransportElement(lspShipment, tour, deliveryActivity); + addShipmentUnloadElement(lspShipment, tour, deliveryActivity); + addDistributionTourStartEventHandler(deliveryActivity.getShipment(), lspShipment, resource, tour); + addDistributionServiceEventHandler(deliveryActivity.getShipment(), lspShipment, resource); + } + } + } + } + default -> + throw new IllegalStateException( + "Unexpected value: " + CarrierSchedulerUtils.getVrpLogic(carrier)); } } } @@ -272,7 +294,7 @@ private void addShipmentLoadElement(LspShipment lspShipment, Tour tour) { } private void addShipmentTransportElement( - LspShipment lspShipment, Tour tour, ServiceActivity serviceActivity) { + LspShipment lspShipment, Tour tour, Tour.TourActivity tourActivity) { LspShipmentUtils.ScheduledShipmentTransportBuilder builder = LspShipmentUtils.ScheduledShipmentTransportBuilder.newInstance(); @@ -286,7 +308,7 @@ private void addShipmentTransportElement( int startIndex = tour.getTourElements().indexOf(tour.getTourElements().indexOf(tour.getStart())); final Leg legAfterStart = (Leg) tour.getTourElements().get(startIndex + 1); - final int serviceIndex = tour.getTourElements().indexOf(serviceActivity); + final int serviceIndex = tour.getTourElements().indexOf(tourActivity); final Leg legBeforeService = (Leg) tour.getTourElements().get(serviceIndex - 1); final double startTimeOfTransport = legAfterStart.getExpectedDepartureTime(); final double endTimeOfTransport = @@ -302,8 +324,8 @@ private void addShipmentTransportElement( builder.setEndTime(endTimeOfTransport); builder.setCarrierId(carrier.getId()); builder.setFromLinkId(tour.getStartLinkId()); - builder.setToLinkId(serviceActivity.getLocation()); - builder.setCarrierService(serviceActivity.getService()); + builder.setToLinkId(tourActivity.getLocation()); + builder.setCarrierService(tourActivity.getService()); LspShipmentPlanElement transport = builder.build(); String idString = transport.getResourceId() @@ -366,11 +388,11 @@ private Carrier createAuxiliaryCarrier(ArrayList shipmentsInCurrent switch (CarrierSchedulerUtils.getVrpLogic(carrier)) { case serviceBased -> { - for (LspShipment lspShipment : shipmentsInCurrentTour) { + for (LspShipment lspShipment : shipmentsInCurrentTour) { CarrierService carrierService = convertToCarrierService(lspShipment); auxiliaryCarrier.getServices().put(carrierService.getId(), carrierService); - } } + } case shipmentBased -> { for (LspShipment lspShipment : shipmentsInCurrentTour) { CarrierShipment carrierShipment = convertToCarrierShipment(lspShipment); diff --git a/src/main/java/org/matsim/freight/logistics/resourceImplementations/DistributionServiceStartEventHandler.java b/src/main/java/org/matsim/freight/logistics/resourceImplementations/DistributionServiceStartEventHandler.java index 80e04fc5..b885a5aa 100644 --- a/src/main/java/org/matsim/freight/logistics/resourceImplementations/DistributionServiceStartEventHandler.java +++ b/src/main/java/org/matsim/freight/logistics/resourceImplementations/DistributionServiceStartEventHandler.java @@ -1,22 +1,22 @@ /* - *********************************************************************** * - * 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; @@ -25,8 +25,11 @@ import org.matsim.core.controler.events.AfterMobsimEvent; import org.matsim.core.controler.listener.AfterMobsimListener; import org.matsim.freight.carriers.CarrierService; +import org.matsim.freight.carriers.CarrierShipment; import org.matsim.freight.carriers.events.CarrierServiceStartEvent; +import org.matsim.freight.carriers.events.CarrierShipmentDeliveryStartEvent; import org.matsim.freight.carriers.events.eventhandler.CarrierServiceStartEventHandler; +import org.matsim.freight.carriers.events.eventhandler.CarrierShipmentDeliveryStartEventHandler; import org.matsim.freight.logistics.LSPCarrierResource; import org.matsim.freight.logistics.LSPSimulationTracker; import org.matsim.freight.logistics.LogisticChainElement; @@ -36,20 +39,23 @@ import org.matsim.freight.logistics.shipment.LspShipmentUtils; /*package-private*/ class DistributionServiceStartEventHandler - implements AfterMobsimListener, + implements AfterMobsimListener, CarrierServiceStartEventHandler, + CarrierShipmentDeliveryStartEventHandler, LSPSimulationTracker { private final CarrierService carrierService; + private final CarrierShipment carrierShipment; private final LogisticChainElement logisticChainElement; private final LSPCarrierResource resource; private LspShipment lspShipment; DistributionServiceStartEventHandler( - CarrierService carrierService, - LspShipment lspShipment, - LogisticChainElement element, - LSPCarrierResource resource) { + CarrierService carrierService, + LspShipment lspShipment, + LogisticChainElement element, + LSPCarrierResource resource) { + this.carrierShipment = carrierShipment; this.carrierService = carrierService; this.lspShipment = lspShipment; this.logisticChainElement = element; @@ -65,7 +71,16 @@ public void reset(int iteration) { @Override public void handleEvent(CarrierServiceStartEvent event) { if (event.getServiceId() == carrierService.getId() - && event.getCarrierId() == resource.getCarrier().getId()) { + && event.getCarrierId() == resource.getCarrier().getId()) { + logTransport(event); + logUnload(event); + } + } + + @Override + public void handleEvent(CarrierShipmentDeliveryStartEvent event) { + if (event.getShipmentId() == this.carrierShipment.getId() + && event.getCarrierId() == resource.getCarrier().getId()) { logTransport(event); logUnload(event); } @@ -75,7 +90,7 @@ private void logTransport(CarrierServiceStartEvent event) { String idString = resource.getId() + "" + logisticChainElement.getId() + "TRANSPORT"; Id id = Id.create(idString, LspShipmentPlanElement.class); LspShipmentPlanElement abstractPlanElement = - lspShipment.getShipmentLog().getPlanElements().get(id); + lspShipment.getShipmentLog().getPlanElements().get(id); if (abstractPlanElement instanceof LspShipmentLeg transport) { transport.setEndTime(event.getTime()); } @@ -83,7 +98,7 @@ private void logTransport(CarrierServiceStartEvent event) { private void logUnload(CarrierServiceStartEvent event) { LspShipmentUtils.LoggedShipmentUnloadBuilder builder = - LspShipmentUtils.LoggedShipmentUnloadBuilder.newInstance(); + LspShipmentUtils.LoggedShipmentUnloadBuilder.newInstance(); builder.setCarrierId(event.getCarrierId()); builder.setLinkId(event.getLinkId()); builder.setLogisticChainElement(logisticChainElement); @@ -92,10 +107,10 @@ private void logUnload(CarrierServiceStartEvent event) { builder.setEndTime(event.getTime() + event.getServiceDuration()); LspShipmentPlanElement unload = builder.build(); String idString = - unload.getResourceId() - + "" - + unload.getLogisticChainElement().getId() - + unload.getElementType(); + unload.getResourceId() + + "" + + unload.getLogisticChainElement().getId() + + unload.getElementType(); Id unloadId = Id.create(idString, LspShipmentPlanElement.class); lspShipment.getShipmentLog().addPlanElement(unloadId, unload); } diff --git a/src/main/java/org/matsim/freight/logistics/shipment/LspShipmentUtils.java b/src/main/java/org/matsim/freight/logistics/shipment/LspShipmentUtils.java index 01efd96b..3b2c46e8 100644 --- a/src/main/java/org/matsim/freight/logistics/shipment/LspShipmentUtils.java +++ b/src/main/java/org/matsim/freight/logistics/shipment/LspShipmentUtils.java @@ -27,6 +27,7 @@ import org.matsim.api.core.v01.network.Link; import org.matsim.freight.carriers.Carrier; import org.matsim.freight.carriers.CarrierService; +import org.matsim.freight.carriers.CarrierShipment; import org.matsim.freight.carriers.TimeWindow; import org.matsim.freight.logistics.LSPPlan; import org.matsim.freight.logistics.LSPResource; @@ -420,6 +421,7 @@ public static final class ScheduledShipmentTransportBuilder { Id fromLinkId; Id toLinkId; CarrierService carrierService; + CarrierShipment carrierShipment; //TODO: Put CarrierShipment and CarrieTask behind one interface and use that here (CarrierTask...) private ScheduledShipmentTransportBuilder() {} @@ -459,6 +461,10 @@ public void setCarrierService(CarrierService carrierService) { this.carrierService = carrierService; } + public void setCarrierShipment(CarrierShipment carrierShipment) { + this.carrierShipment = carrierShipment; + } + public ScheduledLspShipmentTransport build() { return new ScheduledLspShipmentTransport(this); } diff --git a/src/main/java/org/matsim/freight/logistics/shipment/ScheduledLspShipmentTransport.java b/src/main/java/org/matsim/freight/logistics/shipment/ScheduledLspShipmentTransport.java index d4f69efa..71b084e2 100644 --- a/src/main/java/org/matsim/freight/logistics/shipment/ScheduledLspShipmentTransport.java +++ b/src/main/java/org/matsim/freight/logistics/shipment/ScheduledLspShipmentTransport.java @@ -24,6 +24,7 @@ import org.matsim.api.core.v01.network.Link; import org.matsim.freight.carriers.Carrier; import org.matsim.freight.carriers.CarrierService; +import org.matsim.freight.carriers.CarrierShipment; import org.matsim.freight.logistics.LSPResource; import org.matsim.freight.logistics.LogisticChainElement; @@ -37,6 +38,7 @@ final class ScheduledLspShipmentTransport implements LspShipmentLeg { private final Id fromLinkId; private final Id toLinkId; private final CarrierService carrierService; + private final CarrierShipment carrierShipment; //TODO: Put CarrierShipment and CarrieTask behind one interface and use that here (CarrierTask...) ScheduledLspShipmentTransport(LspShipmentUtils.ScheduledShipmentTransportBuilder builder) { this.startTime = builder.startTime; @@ -47,6 +49,7 @@ final class ScheduledLspShipmentTransport implements LspShipmentLeg { this.fromLinkId = builder.fromLinkId; this.toLinkId = builder.toLinkId; this.carrierService = builder.carrierService; + this.carrierShipment = builder.carrierShipment; } @Override @@ -103,4 +106,8 @@ public Id getFromLinkId() { public CarrierService getCarrierService() { return carrierService; } + + public CarrierShipment getCarrierShipment() { + return carrierShipment; + } }