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

Commit

Permalink
WIP commiting just to change the workspace..
Browse files Browse the repository at this point in the history
  • Loading branch information
kt86 committed Aug 30, 2024
1 parent c95d985 commit bd80bfa
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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<CarrierShipment> serviceId = Id.create(lspShipment.getId().toString(), CarrierShipment.class);
CarrierShipment carrierShipment = CarrierShipment.Builder.newInstance(serviceId, lspShipment.getFrom(), lspShipment.getTo(), lspShipment.getSize())
Expand All @@ -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));
}
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -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 =
Expand All @@ -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()
Expand Down Expand Up @@ -366,11 +388,11 @@ private Carrier createAuxiliaryCarrier(ArrayList<LspShipment> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -36,20 +39,23 @@
import org.matsim.freight.logistics.shipment.LspShipmentUtils;

/*package-private*/ class DistributionServiceStartEventHandler
implements AfterMobsimListener,
implements AfterMobsimListener,
CarrierServiceStartEventHandler,
CarrierShipmentDeliveryStartEventHandler,
LSPSimulationTracker<LspShipment> {

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;
Expand All @@ -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);
}
Expand All @@ -75,15 +90,15 @@ private void logTransport(CarrierServiceStartEvent event) {
String idString = resource.getId() + "" + logisticChainElement.getId() + "TRANSPORT";
Id<LspShipmentPlanElement> 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());
}
}

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);
Expand All @@ -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<LspShipmentPlanElement> unloadId = Id.create(idString, LspShipmentPlanElement.class);
lspShipment.getShipmentLog().addPlanElement(unloadId, unload);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -420,6 +421,7 @@ public static final class ScheduledShipmentTransportBuilder {
Id<Link> fromLinkId;
Id<Link> toLinkId;
CarrierService carrierService;
CarrierShipment carrierShipment; //TODO: Put CarrierShipment and CarrieTask behind one interface and use that here (CarrierTask...)

private ScheduledShipmentTransportBuilder() {}

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,6 +38,7 @@ final class ScheduledLspShipmentTransport implements LspShipmentLeg {
private final Id<Link> fromLinkId;
private final Id<Link> 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;
Expand All @@ -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
Expand Down Expand Up @@ -103,4 +106,8 @@ public Id<Link> getFromLinkId() {
public CarrierService getCarrierService() {
return carrierService;
}

public CarrierShipment getCarrierShipment() {
return carrierShipment;
}
}

0 comments on commit bd80bfa

Please sign in to comment.