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

Commit

Permalink
Merge branch 'main' into kmt_UseMATSimWorstPlanRemovalSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
kt86 authored Mar 25, 2024
2 parents 6b20881 + 54b0cc7 commit a99013a
Show file tree
Hide file tree
Showing 91 changed files with 627 additions and 537 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<parent>
<groupId>org.matsim</groupId>
<artifactId>matsim-all</artifactId>
<!-- <version>16.0-2024w04</version>-->
<version>16.0-PR3184</version>
<!-- <version>16.0-2024w13</version>-->
<!-- <version>16.0-SNAPSHOT</version>-->
<relativePath/>
</parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
/**
* Takes an {@link LSPShipment} and normally assigns it to something that belongs to an {@link LSP}.
* <br>
* After changes in fall 2023 (see master thesis of nrichter), the assingment is
* there to be done one time initially.
* <br>
* If there are several {@link LogisticChain}s, the {@link LSP} has to assign each {@link
* LSPShipment} to the suitable one. For this purpose, each LSPPlan contains a pluggable strategy
* that is contained in classes implementing the interface {@link ShipmentAssigner}. <br>
* If there are several {@link LogisticChain}s in a {@link LSPPlan}, the {@link LSP} has to assign each {@link
* LSPShipment} to the suitable {@link LogisticChain}. For this purpose, each {@link LSPPlan}
* (or only the LSP? - kmt'jan'24), contains a pluggable strategy
* that is contained in classes implementing the interface {@link InitialShipmentAssigner}. <br>
* <br>
* Discussion points:
*
* <ul>
* <li>yyyy Shipments are normally assigned to the selected plan only. I am not sure if this is
* what I would expect from the outside. kai, may'22
* </ul>
* During iterations, it can happen that the {@link LSPShipment} should be moved to another
* {@link LogisticChain} of the same {@link LSPPlan}. This is now (since fall 2023; see master
* thesis of nrichter) part of the (innovative) **Replanning** strategies.
*/
public interface ShipmentAssigner extends KnowsLSP {
public interface InitialShipmentAssigner {

void assignToPlan(LSPPlan lspPlan, LSPShipment shipment);
}
2 changes: 1 addition & 1 deletion src/main/java/org/matsim/freight/logistics/LSP.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* In the class library, the interface LSP has the following tasks: 1. Maintain one or several
* transport chains through which {@link LSPShipment}s are routed. 2. Assign {@link LSPShipment}s to
* the suitable transport chain. --> {@link ShipmentAssigner}. 3. Interact with the agents that
* the suitable transport chain. --> {@link InitialShipmentAssigner}. 3. Interact with the agents that
* embody the demand side of the freight transport market, if they are specified in the setting. 4.
* Coordinate carriers that are in charge of the physical transport.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.matsim.core.controler.events.*;
import org.matsim.core.controler.listener.*;
import org.matsim.core.events.handler.EventHandler;
import org.matsim.core.gbl.Gbl;
import org.matsim.freight.carriers.Carrier;
import org.matsim.freight.carriers.CarrierPlanWriter;
import org.matsim.freight.carriers.Carriers;
Expand All @@ -53,13 +54,17 @@ class LSPControlerListener
private final Scenario scenario;
private final List<EventHandler> registeredHandlers = new ArrayList<>();

private static int addListenerCnt = 0;
private static final int maxAddListenerCnt = 1;

@Inject private EventsManager eventsManager;
@Inject private MatsimServices matsimServices;
@Inject private LSPScorerFactory lspScoringFunctionFactory;
@Inject @Nullable private LSPStrategyManager strategyManager;
@Inject private OutputDirectoryHierarchy controlerIO;
@Inject private CarrierAgentTracker carrierAgentTracker;


@Inject
LSPControlerListener(Scenario scenario) {
this.scenario = scenario;
Expand Down Expand Up @@ -109,15 +114,20 @@ private void registerSimulationTrackers(HasSimulationTrackers<?> hasSimulationTr
hasSimulationTrackers.getSimulationTrackers()) {
// ... register them ...
if (!registeredHandlers.contains(simulationTracker)) {
log.warn("adding eventsHandler: " + simulationTracker);
log.info("adding eventsHandler: " + simulationTracker);
eventsManager.addHandler(simulationTracker);
registeredHandlers.add(simulationTracker);
matsimServices.addControlerListener(simulationTracker);
simulationTracker.setEventsManager(eventsManager);
} else {
} else if ( addListenerCnt < maxAddListenerCnt ){
log.warn("not adding eventsHandler since already added: " + simulationTracker);
addListenerCnt++;
if (addListenerCnt == maxAddListenerCnt) {
log.warn(Gbl.FUTURE_SUPPRESSED);
}
}
}

}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/matsim/freight/logistics/LSPImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static LSPPlan copyPlan(LSPPlan plan2copy) {
}

LSPPlan copiedPlan = LSPUtils.createLSPPlan();
copiedPlan.setAssigner(plan2copy.getAssigner());
copiedPlan.setInitialShipmentAssigner(plan2copy.getInitialShipmentAssigner());
copiedPlan.setLSP(plan2copy.getLSP());
copiedPlan.setScore(plan2copy.getScore());
copiedPlan.setType(plan2copy.getType());
Expand Down Expand Up @@ -152,7 +152,7 @@ public void assignShipmentToLSP(LSPShipment shipment) {
// lsp.getselectedPlan.getShipment...
shipments.add(shipment);
for (LSPPlan lspPlan : plans) {
lspPlan.getAssigner().assignToPlan(lspPlan, shipment);
lspPlan.getInitialShipmentAssigner().assignToPlan(lspPlan, shipment);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/matsim/freight/logistics/LSPPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* <li>As a {@link BasicPlan} it has a score, so it can be used for evolutionary learning. kai,
* may'22
* <li>An {@link LSPShipment} is added via lspPlan#getAssigner().assignToSolution(shipment). The
* {@link ShipmentAssigner} assigns it deterministically to a {@link LogisticChain}.
* {@link InitialShipmentAssigner} assigns it deterministically to a {@link LogisticChain}.
* </ul>
*/
public interface LSPPlan extends BasicPlan, KnowsLSP {
Expand All @@ -45,9 +45,9 @@ public interface LSPPlan extends BasicPlan, KnowsLSP {
* yy My intuition would be to replace lspPlan#getAssigner().assignToSolution( shipment ) by
* lspPlan.addShipment( shipment ). kai, may'22
*/
ShipmentAssigner getAssigner();
InitialShipmentAssigner getInitialShipmentAssigner();

LSPPlan setAssigner(ShipmentAssigner assigner);
LSPPlan setInitialShipmentAssigner(InitialShipmentAssigner assigner);

Collection<ShipmentPlan> getShipmentPlans();

Expand Down
14 changes: 3 additions & 11 deletions src/main/java/org/matsim/freight/logistics/LSPPlanImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class LSPPlanImpl implements LSPPlan {
private final Collection<ShipmentPlan> shipmentPlans;
private LSP lsp;
private Double score = null;
private ShipmentAssigner assigner;
private InitialShipmentAssigner assigner;
private String type = null;

public LSPPlanImpl() {
Expand All @@ -51,14 +51,13 @@ public Collection<LogisticChain> getLogisticChains() {
}

@Override
public ShipmentAssigner getAssigner() {
public InitialShipmentAssigner getInitialShipmentAssigner() {
return assigner;
}

@Override
public LSPPlan setAssigner(ShipmentAssigner assigner) {
public LSPPlan setInitialShipmentAssigner(InitialShipmentAssigner assigner) {
this.assigner = assigner;
this.assigner.setLSP(this.lsp);
return this;
}

Expand Down Expand Up @@ -101,13 +100,6 @@ public LSP getLSP() {
@Override
public void setLSP(LSP lsp) {
this.lsp = lsp;
if (assigner != null) {
this.assigner.setLSP(lsp);
// yy vom Design her wäre es vlt. einfacher und logischer, wenn der assigner einen backpointer
// auf den LSPPlan hätte. Dann
// müsste man nicht (wie hier) hedgen gegen unterschiedliche Initialisierungssequenzen. kai,
// may'22
}
for (LogisticChain solution : logisticChains) {
solution.setLSP(lsp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ private static LSP createInitialLSP(Network network) {
// The initial plan of the lsp is generated and the assigner and the solution from above are
// added
LSPPlan collectionPlan = LSPUtils.createLSPPlan();
ShipmentAssigner assigner =
InitialShipmentAssigner assigner =
ResourceImplementationUtils.createSingleLogisticChainShipmentAssigner();
collectionPlan.setAssigner(assigner);
collectionPlan.setInitialShipmentAssigner(assigner);
collectionPlan.addLogisticChain(collectionSolution);

// The exogenous list of Resoruces for the SolutuionScheduler is compiled and the Scheduler is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.matsim.freight.logistics.*;
import org.matsim.freight.logistics.resourceImplementations.ResourceImplementationUtils;
import org.matsim.freight.logistics.resourceImplementations.ResourceImplementationUtils.DistributionCarrierResourceBuilder;
import org.matsim.freight.logistics.resourceImplementations.transshipmentHub.TranshipmentHubUtils;
import org.matsim.freight.logistics.resourceImplementations.ResourceImplementationUtils.TransshipmentHubBuilder;
import org.matsim.freight.logistics.shipment.LSPShipment;
import org.matsim.freight.logistics.shipment.ShipmentPlanElement;
import org.matsim.freight.logistics.shipment.ShipmentUtils;
Expand Down Expand Up @@ -99,13 +99,13 @@ private static LSP createInitialLSP(Scenario scenario) {
// The first reloading adapter i.e. the Resource is created
Id<LSPResource> firstTransshipmentHubId = Id.create("TranshipmentHub1", LSPResource.class);
Id<Link> firstTransshipmentHub_LinkId = Id.createLinkId("(4 2) (4 3)");
TranshipmentHubUtils.TransshipmentHubBuilder firstTransshipmentHubBuilder =
TranshipmentHubUtils.TransshipmentHubBuilder.newInstance(
TransshipmentHubBuilder firstTransshipmentHubBuilder =
ResourceImplementationUtils.TransshipmentHubBuilder.newInstance(
firstTransshipmentHubId, firstTransshipmentHub_LinkId, scenario);

// The scheduler for the first reloading point is created
final LSPResourceScheduler firstHubScheduler =
TranshipmentHubUtils.TranshipmentHubSchedulerBuilder.newInstance()
ResourceImplementationUtils.TranshipmentHubSchedulerBuilder.newInstance()
.setCapacityNeedFixed(10)
.setCapacityNeedLinear(1)
.build();
Expand Down Expand Up @@ -169,13 +169,13 @@ private static LSP createInitialLSP(Scenario scenario) {

// The scheduler for the second reloading point is created
LSPResourceScheduler secondHubScheduler =
TranshipmentHubUtils.TranshipmentHubSchedulerBuilder.newInstance()
ResourceImplementationUtils.TranshipmentHubSchedulerBuilder.newInstance()
.setCapacityNeedFixed(10)
.setCapacityNeedLinear(1)
.build();

LSPResource secondTransshipmentHubResource =
TranshipmentHubUtils.TransshipmentHubBuilder.newInstance(
ResourceImplementationUtils.TransshipmentHubBuilder.newInstance(
secondTransshipmentHubId, secondTransshipmentHub_LinkId, scenario)
.setTransshipmentHubScheduler(secondHubScheduler)
.build();
Expand Down Expand Up @@ -256,9 +256,9 @@ private static LSP createInitialLSP(Scenario scenario) {
// The initial plan of the lsp is generated and the assigner and the solution from above are
// added
LSPPlan completePlan = LSPUtils.createLSPPlan();
ShipmentAssigner assigner =
InitialShipmentAssigner assigner =
ResourceImplementationUtils.createSingleLogisticChainShipmentAssigner();
completePlan.setAssigner(assigner);
completePlan.setInitialShipmentAssigner(assigner);
completePlan.addLogisticChain(completeSolution);

LSPUtils.LSPBuilder completeLSPBuilder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.matsim.freight.logistics.*;
import org.matsim.freight.logistics.example.lsp.lspReplanning.AssignmentStrategyFactory;
import org.matsim.freight.logistics.resourceImplementations.ResourceImplementationUtils;
import org.matsim.freight.logistics.resourceImplementations.transshipmentHub.TranshipmentHubUtils;
import org.matsim.freight.logistics.shipment.LSPShipment;
import org.matsim.freight.logistics.shipment.ShipmentUtils;
import org.matsim.vehicles.VehicleType;
Expand Down Expand Up @@ -214,14 +213,14 @@ private static LSP createInitialLSP(Scenario scenario, SolutionType solutionType
// The scheduler for the first reloading point is created --> this will be the depot in this
// use case
LSPResourceScheduler depotScheduler =
TranshipmentHubUtils.TranshipmentHubSchedulerBuilder.newInstance()
ResourceImplementationUtils.TranshipmentHubSchedulerBuilder.newInstance()
.setCapacityNeedFixed(10) // Time needed, fixed (for Scheduler)
.setCapacityNeedLinear(1) // additional time needed per shipmentSize (for Scheduler)
.build();

// The scheduler is added to the Resource and the Resource is created
LSPResource depotResource =
TranshipmentHubUtils.TransshipmentHubBuilder.newInstance(
ResourceImplementationUtils.TransshipmentHubBuilder.newInstance(
Id.create("Depot", LSPResource.class), depotLinkId, scenario)
.setTransshipmentHubScheduler(depotScheduler)
.build();
Expand Down Expand Up @@ -287,7 +286,7 @@ private static LSP createInitialLSP(Scenario scenario, SolutionType solutionType
log.info("The second reloading adapter (hub) i.e. the Resource is created");
// The scheduler for the second reloading point is created
LSPResourceScheduler hubScheduler =
TranshipmentHubUtils.TranshipmentHubSchedulerBuilder.newInstance()
ResourceImplementationUtils.TranshipmentHubSchedulerBuilder.newInstance()
.setCapacityNeedFixed(10)
.setCapacityNeedLinear(1)
.build();
Expand All @@ -296,7 +295,7 @@ private static LSP createInitialLSP(Scenario scenario, SolutionType solutionType
// The second reloading adapter i.e. the Resource is created
Id<LSPResource> secondTransshipmentHubId = Id.create("TranshipmentHub2", LSPResource.class);
LSPResource hubResource =
TranshipmentHubUtils.TransshipmentHubBuilder.newInstance(
ResourceImplementationUtils.TransshipmentHubBuilder.newInstance(
secondTransshipmentHubId, hubLinkId, scenario)
.setTransshipmentHubScheduler(hubScheduler)
.build();
Expand Down Expand Up @@ -510,7 +509,7 @@ private static LSPPlan createLSPPlan_direct(
"The initial plan of the lsp is generated and the assigner and the solution from above are added");

return LSPUtils.createLSPPlan()
.setAssigner(ResourceImplementationUtils.createSingleLogisticChainShipmentAssigner())
.setInitialShipmentAssigner(ResourceImplementationUtils.createSingleLogisticChainShipmentAssigner())
.addLogisticChain(completeSolutionDirect);
}

Expand Down Expand Up @@ -541,7 +540,7 @@ private static LSPPlan createLSPPlan_reloading(
"The initial plan of the lsp is generated and the assigner and the solution from above are added");

return LSPUtils.createLSPPlan()
.setAssigner(ResourceImplementationUtils.createSingleLogisticChainShipmentAssigner())
.setInitialShipmentAssigner(ResourceImplementationUtils.createSingleLogisticChainShipmentAssigner())
.addLogisticChain(completeSolutionWithReloading);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.matsim.freight.carriers.controler.CarrierStrategyManager;
import org.matsim.freight.logistics.*;
import org.matsim.freight.logistics.resourceImplementations.ResourceImplementationUtils;
import org.matsim.freight.logistics.resourceImplementations.transshipmentHub.TranshipmentHubUtils;
import org.matsim.freight.logistics.shipment.LSPShipment;
import org.matsim.freight.logistics.shipment.ShipmentUtils;
import org.matsim.vehicles.VehicleType;
Expand Down Expand Up @@ -274,12 +273,12 @@ private static LSP createLSP(Scenario scenario) {
.addLogisticChainElement(directCarrierElement)
.build();

final ShipmentAssigner singleSolutionShipmentAssigner =
final InitialShipmentAssigner singleSolutionShipmentAssigner =
ResourceImplementationUtils.createSingleLogisticChainShipmentAssigner();
lspPlan_direct =
LSPUtils.createLSPPlan()
.addLogisticChain(solution_direct)
.setAssigner(singleSolutionShipmentAssigner);
.setInitialShipmentAssigner(singleSolutionShipmentAssigner);
}

LSPPlan lspPlan_withHub;
Expand Down Expand Up @@ -311,14 +310,14 @@ private static LSP createLSP(Scenario scenario) {
// The scheduler for the first reloading point is created --> this will be the depot in this
// use case
LSPResourceScheduler hubScheduler =
TranshipmentHubUtils.TranshipmentHubSchedulerBuilder.newInstance()
ResourceImplementationUtils.TranshipmentHubSchedulerBuilder.newInstance()
.setCapacityNeedFixed(10) // Time needed, fixed (for Scheduler)
.setCapacityNeedLinear(1) // additional time needed per shipmentSize (for Scheduler)
.build();

// The scheduler is added to the Resource and the Resource is created
LSPResource hubResource =
TranshipmentHubUtils.TransshipmentHubBuilder.newInstance(
ResourceImplementationUtils.TransshipmentHubBuilder.newInstance(
Id.create("Hub", LSPResource.class), HUB_LINK_ID, scenario)
.setTransshipmentHubScheduler(hubScheduler)
.build();
Expand Down Expand Up @@ -378,7 +377,7 @@ private static LSP createLSP(Scenario scenario) {
lspPlan_withHub =
LSPUtils.createLSPPlan()
.addLogisticChain(solution_withHub)
.setAssigner(ResourceImplementationUtils.createSingleLogisticChainShipmentAssigner());
.setInitialShipmentAssigner(ResourceImplementationUtils.createSingleLogisticChainShipmentAssigner());
}

// Todo: Auch das ist wirr: Muss hier alle sommeln, damit man die dann im LSPBuilder dem
Expand Down
Loading

0 comments on commit a99013a

Please sign in to comment.