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 #293 from matsim-vsp/kmt_2echelon4Diss
Browse files Browse the repository at this point in the history
Get rid of ShipmentWithTime; Code cleanup in tests
  • Loading branch information
kt86 authored Aug 14, 2024
2 parents 0ab5c10 + d583037 commit b052bd9
Show file tree
Hide file tree
Showing 61 changed files with 634 additions and 737 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@
public abstract class LSPResourceScheduler {

protected LSPResource resource;
protected ArrayList<LspShipmentWithTime> lspShipmentsWithTime;
protected ArrayList<LspShipment> lspShipmentsToSchedule;

protected LSPPlan lspPlan;

public final void scheduleShipments(LSPPlan lspPlan, LSPResource resource, int bufferTime) {
this.lspPlan = lspPlan;
this.resource = resource;
this.lspShipmentsWithTime = new ArrayList<>();
this.lspShipmentsToSchedule = new ArrayList<>();
initializeValues(resource);
presortIncomingShipments();
scheduleResource();
updateShipments();
switchHandledShipments(bufferTime);
lspShipmentsWithTime.clear();
lspShipmentsToSchedule.clear();
}

/**
Expand All @@ -79,27 +79,25 @@ public final void scheduleShipments(LSPPlan lspPlan, LSPResource resource, int b
protected abstract void updateShipments();

private void presortIncomingShipments() {
this.lspShipmentsWithTime = new ArrayList<>();
this.lspShipmentsToSchedule = new ArrayList<>();
for (LogisticChainElement element : resource.getClientElements()) {
lspShipmentsWithTime.addAll(element.getIncomingShipments().getLspShipmentsWTime());
lspShipmentsToSchedule.addAll(element.getIncomingShipments().getLspShipmentsWTime());
}
lspShipmentsWithTime.sort(Comparator.comparingDouble(LspShipmentWithTime::getTime));
lspShipmentsToSchedule.sort(Comparator.comparingDouble(LspShipmentUtils::getTimeOfLspShipment));
}

private void switchHandledShipments(int bufferTime) {
for (LspShipmentWithTime lspShipmentWithTime : lspShipmentsWithTime) {
var shipmentPlan =
LspShipmentUtils.getOrCreateShipmentPlan(lspPlan, lspShipmentWithTime.getLspShipment().getId());
for (LspShipment lspShipmentWithTime : lspShipmentsToSchedule) {
var shipmentPlan = LspShipmentUtils.getOrCreateShipmentPlan(lspPlan, lspShipmentWithTime.getId());
double endOfTransportTime = shipmentPlan.getMostRecentEntry().getEndTime() + bufferTime;
LspShipmentWithTime outgoingTuple =
new LspShipmentWithTime(endOfTransportTime, lspShipmentWithTime.getLspShipment());
for (LogisticChainElement element : resource.getClientElements()) {
LspShipmentUtils.setTimeOfLspShipment(lspShipmentWithTime, endOfTransportTime);
for (LogisticChainElement element : resource.getClientElements()) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(lspShipmentWithTime)) {
element.getOutgoingShipments().getLspShipmentsWTime().add(outgoingTuple);
element.getIncomingShipments().getLspShipmentsWTime().remove(lspShipmentWithTime);
element.getOutgoingShipments().getLspShipmentsWTime().add(lspShipmentWithTime);
if (element.getNextElement() != null) {
element.getNextElement().getIncomingShipments().getLspShipmentsWTime().add(outgoingTuple);
element.getOutgoingShipments().getLspShipmentsWTime().remove(outgoingTuple);
element.getNextElement().getIncomingShipments().getLspShipmentsWTime().add(lspShipmentWithTime);
element.getOutgoingShipments().getLspShipmentsWTime().remove(lspShipmentWithTime);
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public interface WaitingShipments {

void addShipment(double time, LspShipment lspShipment);

Collection<LspShipmentWithTime> getSortedLspShipments();
Collection<LspShipment> getSortedLspShipments();

Collection<LspShipmentWithTime> getLspShipmentsWTime();
Collection<LspShipment> getLspShipmentsWTime();

void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,26 @@
import java.util.Comparator;
import java.util.List;
import org.matsim.freight.logistics.shipment.LspShipment;
import org.matsim.freight.logistics.shipment.LspShipmentUtils;

/* package-private */ class WaitingShipmentsImpl implements WaitingShipments {

private final List<LspShipmentWithTime> shipments;
private final List<LspShipment> shipments;

WaitingShipmentsImpl() {
this.shipments = new ArrayList<>();
}

@Override
public void addShipment(double time, LspShipment lspShipment) {
LspShipmentWithTime tuple = new LspShipmentWithTime(time, lspShipment);
this.shipments.add(tuple);
shipments.sort(Comparator.comparingDouble(LspShipmentWithTime::getTime));
LspShipmentUtils.setTimeOfLspShipment(lspShipment, time);
this.shipments.add(lspShipment);
shipments.sort(Comparator.comparingDouble(LspShipmentUtils::getTimeOfLspShipment));
}

@Override
public Collection<LspShipmentWithTime> getSortedLspShipments() {
shipments.sort(Comparator.comparingDouble(LspShipmentWithTime::getTime));
public Collection<LspShipment> getSortedLspShipments() {
shipments.sort(Comparator.comparingDouble(LspShipmentUtils::getTimeOfLspShipment));
return shipments;
}

Expand All @@ -52,7 +53,7 @@ public void clear() {
}

@Override
public Collection<LspShipmentWithTime> getLspShipmentsWTime() {
public Collection<LspShipment> getLspShipmentsWTime() {
return shipments;
}

Expand All @@ -62,8 +63,8 @@ public String toString() {
strb.append("WaitingShipmentsImpl{").append("No of Shipments= ").append(shipments.size());
if (!shipments.isEmpty()) {
strb.append("; ShipmentIds=");
for (LspShipmentWithTime shipment : getSortedLspShipments()) {
strb.append("[").append(shipment.getLspShipment().getId()).append("]");
for (LspShipment shipment : getSortedLspShipments()) {
strb.append("[").append(shipment.getId()).append("]");
}
}
strb.append('}');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,42 +74,42 @@ public void initializeValues(LSPResource resource) {

@Override
public void scheduleResource() {
for (LspShipmentWithTime tupleToBeAssigned : lspShipmentsWithTime) {
CarrierService carrierService = convertToCarrierService(tupleToBeAssigned);
for (LspShipment lspShipmentToBeAssigned : lspShipmentsToSchedule) {
CarrierService carrierService = convertToCarrierService(lspShipmentToBeAssigned);
carrier.getServices().put(carrierService.getId(), carrierService);
}
CarrierSchedulerUtils.solveVrpWithJsprit(carrier, scenario);
}

private CarrierService convertToCarrierService(LspShipmentWithTime tuple) {
private CarrierService convertToCarrierService(LspShipment lspShipment) {
Id<CarrierService> serviceId =
Id.create(tuple.getLspShipment().getId().toString(), CarrierService.class);
Id.create(lspShipment.getId().toString(), CarrierService.class);
CarrierService.Builder builder =
CarrierService.Builder.newInstance(serviceId, tuple.getLspShipment().getFrom());
builder.setServiceStartTimeWindow(tuple.getLspShipment().getPickupTimeWindow());
builder.setCapacityDemand(tuple.getLspShipment().getSize());
builder.setServiceDuration(tuple.getLspShipment().getDeliveryServiceTime());
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(tuple, carrierService));
pairs.add(new LSPCarrierPair(lspShipment, carrierService));
return carrierService;
}

@Override
protected void updateShipments() {
for (LspShipmentWithTime tuple : lspShipmentsWithTime) {
for (LspShipment lspShipment : lspShipmentsToSchedule) {
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(tuple, serviceActivity.getService());
LSPCarrierPair carrierPair = new LSPCarrierPair(lspShipment, serviceActivity.getService());
for (LSPCarrierPair pair : pairs) {
if (pair.tuple == carrierPair.tuple
if (pair.lspShipment == carrierPair.lspShipment
&& pair.carrierService.getId() == carrierPair.carrierService.getId()) {
addShipmentLoadElement(tuple, tour, serviceActivity);
addShipmentTransportElement(tuple, tour, serviceActivity);
addShipmentUnloadElement(tuple, tour, serviceActivity);
addCollectionTourEndEventHandler(pair.carrierService, tuple, resource, tour);
addCollectionServiceEventHandler(pair.carrierService, tuple, resource);
addShipmentLoadElement(lspShipment, tour, serviceActivity);
addShipmentTransportElement(lspShipment, tour, serviceActivity);
addShipmentUnloadElement(lspShipment, tour);
addCollectionTourEndEventHandler(pair.carrierService, lspShipment, resource, tour);
addCollectionServiceEventHandler(pair.carrierService, lspShipment, resource);
}
}
}
Expand All @@ -119,47 +119,52 @@ protected void updateShipments() {
}

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

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

for (LogisticChainElement element : resource.getClientElements()) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(tuple)) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(lspShipment)) {
builder.setLogisticChainElement(element);
}
}

int serviceIndex = tour.getTourElements().indexOf(serviceActivity);
Leg legBeforeService = (Leg) tour.getTourElements().get(serviceIndex - 1);
double startTimeOfLoading =
legBeforeService.getExpectedDepartureTime() + legBeforeService.getExpectedTransportTime();
builder.setStartTime(startTimeOfLoading);
builder.setEndTime(startTimeOfLoading + tuple.getLspShipment().getDeliveryServiceTime());
builder.setEndTime(startTimeOfLoading + lspShipment.getDeliveryServiceTime());

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

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

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

for (LogisticChainElement element : resource.getClientElements()) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(tuple)) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(lspShipment)) {
builder.setLogisticChainElement(element);
}
}

int serviceIndex = tour.getTourElements().indexOf(serviceActivity);
Leg legAfterService = (Leg) tour.getTourElements().get(serviceIndex + 1);
double startTimeOfTransport = legAfterService.getExpectedDepartureTime();
builder.setStartTime(startTimeOfTransport);
Leg lastLeg = (Leg) tour.getTourElements().getLast();
double endTimeOfTransport =
lastLeg.getExpectedDepartureTime() + lastLeg.getExpectedTransportTime();
double endTimeOfTransport = lastLeg.getExpectedDepartureTime() + lastLeg.getExpectedTransportTime();
builder.setEndTime(endTimeOfTransport);
builder.setCarrierId(carrier.getId());
builder.setFromLinkId(serviceActivity.getLocation());
Expand All @@ -172,46 +177,47 @@ private void addShipmentTransportElement(
+ transport.getLogisticChainElement().getId()
+ transport.getElementType();
Id<LspShipmentPlanElement> id = Id.create(idString, LspShipmentPlanElement.class);
LspShipmentUtils.getOrCreateShipmentPlan(super.lspPlan, tuple.getLspShipment().getId())
LspShipmentUtils.getOrCreateShipmentPlan(super.lspPlan, lspShipment.getId())
.addPlanElement(id, transport);
}

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

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

private void addCollectionTourEndEventHandler(
CarrierService carrierService,
LspShipmentWithTime tuple,
LspShipment lspShipment,
LSPCarrierResource resource,
Tour tour) {
for (LogisticChainElement element : this.resource.getClientElements()) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(tuple)) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(lspShipment)) {
LSPTourEndEventHandler handler =
new LSPTourEndEventHandler(
tuple.getLspShipment(), carrierService, element, resource, tour);
tuple.getLspShipment().addSimulationTracker(handler);
lspShipment, carrierService, element, resource, tour);
lspShipment.addSimulationTracker(handler);
break;
}
}
}

private void addShipmentUnloadElement(
LspShipmentWithTime tuple, Tour tour, Tour.ServiceActivity serviceActivity) {
private void addShipmentUnloadElement(LspShipment lspShipment, Tour tour) {

LspShipmentUtils.ScheduledShipmentUnloadBuilder builder =
LspShipmentUtils.ScheduledShipmentUnloadBuilder.newInstance();
builder.setResourceId(resource.getId());
for (LogisticChainElement element : resource.getClientElements()) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(tuple)) {
if (element.getIncomingShipments().getLspShipmentsWTime().contains(lspShipment)) {
builder.setLogisticsChainElement(element);
}
}
Expand All @@ -227,7 +233,7 @@ private void addShipmentUnloadElement(
+ unload.getLogisticChainElement().getId()
+ unload.getElementType();
Id<LspShipmentPlanElement> id = Id.create(idString, LspShipmentPlanElement.class);
LspShipmentUtils.getOrCreateShipmentPlan(super.lspPlan, tuple.getLspShipment().getId())
LspShipmentUtils.getOrCreateShipmentPlan(super.lspPlan, lspShipment.getId())
.addPlanElement(id, unload);
}

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

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

0 comments on commit b052bd9

Please sign in to comment.