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

Bind CarrierModule in LSPModule, add CarrierAnalysis to some runs #306

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<artifactId>freight</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.matsim.contrib</groupId>
<artifactId>vsp</artifactId>
<version>${project.parent.version}</version>
</dependency>

<!--MATSim examples. Not transitive -->
<dependency>
Expand Down
67 changes: 3 additions & 64 deletions src/main/java/org/matsim/freight/logistics/LSPModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@

package org.matsim.freight.logistics;

import com.google.inject.Provides;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.events.Event;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.HasPlansAndId;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.events.BeforeMobsimEvent;
Expand All @@ -40,18 +36,13 @@
import org.matsim.core.replanning.GenericPlanStrategy;
import org.matsim.core.replanning.ReplanningContext;
import org.matsim.core.replanning.selectors.PlanSelector;
import org.matsim.core.scoring.ScoringFunction;
import org.matsim.freight.carriers.Carrier;
import org.matsim.freight.carriers.Carriers;
import org.matsim.freight.carriers.FreightCarriersConfigGroup;
import org.matsim.freight.carriers.controler.*;
import org.matsim.freight.logistics.analysis.LspScoreStatsModule;

public class LSPModule extends AbstractModule {
private static final Logger log = LogManager.getLogger(LSPModule.class);

// private final FreightCarriersConfigGroup carrierConfig = new FreightCarriersConfigGroup();

@Override
public void install() {
FreightCarriersConfigGroup freightConfig =
Expand All @@ -60,12 +51,7 @@ public void install() {
bind(LSPControlerListener.class).in(Singleton.class);
addControlerListenerBinding().to(LSPControlerListener.class);

bind(CarrierControlerListener.class).in(Singleton.class);
addControlerListenerBinding().to(CarrierControlerListener.class);

bind(CarrierAgentTracker.class).in(Singleton.class);
addEventHandlerBinding().to(CarrierAgentTracker.class);

install(new CarrierModule());
install(new LspScoreStatsModule());

// this switches on certain qsim components:
Expand Down Expand Up @@ -113,22 +99,15 @@ protected void configureQSim() {
});

// the scorers are necessary to run a zeroth iteration to the end:
bind(CarrierScoringFunctionFactory.class).to(CarrierScoringFactoryDummyImpl.class);
bind(LSPScorerFactory.class).to(LSPScoringFunctionFactoryDummyImpl.class);

// for iterations, one needs to replace the following with something meaningful. If nothing
// else, there are "empty implementations" that do nothing. kai, jul'22
bind(CarrierStrategyManager.class).toProvider(() -> null);
bind(LSPStrategyManager.class).toProvider(() -> null);

this.addControlerListenerBinding().to(DumpLSPPlans.class);
}

@Provides
Carriers provideCarriers(LSPControlerListener lspControlerListener) {
return lspControlerListener.getCarriersFromLSP();
}

private static class LSPScoringFunctionFactoryDummyImpl implements LSPScorerFactory {
@Override
public LSPScorer createScoringFunction() {
Expand All @@ -144,39 +123,6 @@ public void setEmbeddingContainer(LSP pointer) {}
}
}

private static class CarrierScoringFactoryDummyImpl implements CarrierScoringFunctionFactory {
@Override
public ScoringFunction createScoringFunction(Carrier carrier) {
return new ScoringFunction() {
@Override
public void handleActivity(Activity activity) {}

@Override
public void handleLeg(Leg leg) {}

@Override
public void agentStuck(double time) {}

@Override
public void addMoney(double amount) {}

@Override
public void addScore(double amount) {}

@Override
public void finish() {}

@Override
public double getScore() {
return Double.NEGATIVE_INFINITY;
}

@Override
public void handleEvent(Event event) {}
};
}
}

public static final class LSPStrategyManagerEmptyImpl implements LSPStrategyManager {

@Override
Expand All @@ -191,10 +137,7 @@ public void run(
int iteration,
ReplanningContext replanningContext) {
log.warn("Running iterations without a strategy may lead to unclear results."); // "run" is
// possible, but
// will not do
// anything. kai,
// jul'22
// possible, but will not do anything. kai, jul'22
}

@Override
Expand All @@ -203,11 +146,7 @@ public void setMaxPlansPerAgent(int maxPlansPerAgent) {
}

@Override
public void addChangeRequest(
int iteration,
GenericPlanStrategy<LSPPlan, LSP> strategy,
String subpopulation,
double newWeight) {
public void addChangeRequest(int iteration, GenericPlanStrategy<LSPPlan, LSP> strategy, String subpopulation, double newWeight) {
throw new RuntimeException("not implemented");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void handleEvent(Event event) {
case CarrierTourStartEvent carrierTourStartEvent -> handleEvent(carrierTourStartEvent);
case CarrierTourEndEvent carrierTourEndEvent -> handleEvent(carrierTourEndEvent);
case LinkEnterEvent linkEnterEvent -> handleEvent(linkEnterEvent);
case PersonMoneyEvent personMoneyEvent -> handleEvent(personMoneyEvent); //FIXME: Aus irgendwelchen Gründen kommen hier keine PersonMoneyEvents an...
case PersonMoneyEvent personMoneyEvent -> handleEvent(personMoneyEvent);
case VehicleEntersTrafficEvent vehicleEntersTrafficEvent -> d2v.handleEvent(vehicleEntersTrafficEvent);
case VehicleLeavesTrafficEvent vehicleLeavesTrafficEvent -> d2v.handleEvent(vehicleLeavesTrafficEvent);
default -> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

package org.matsim.freight.logistics.examples.multipleChains;

import java.io.IOException;
import java.util.*;

import org.apache.logging.log4j.LogManager;
Expand All @@ -62,6 +63,7 @@
import org.matsim.core.replanning.selectors.GenericWorstPlanForRemovalSelector;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.freight.carriers.*;
import org.matsim.freight.carriers.analysis.RunFreightAnalysisEventBased;
import org.matsim.freight.carriers.controler.CarrierControlerUtils;
import org.matsim.freight.carriers.controler.CarrierScoringFunctionFactory;
import org.matsim.freight.carriers.controler.CarrierStrategyManager;
Expand Down Expand Up @@ -179,6 +181,15 @@ public void install() {
.setVspDefaultsCheckingLevel(VspExperimentalConfigGroup.VspDefaultsCheckingLevel.warn);
controler.run();

//Carrier Analysis
final String outputPath = controler.getControlerIO().getOutputPath();
RunFreightAnalysisEventBased freightAnalysis = new RunFreightAnalysisEventBased(outputPath +"/", outputPath +"/Analysis/", config.global().getCoordinateSystem());
try {
freightAnalysis.runAnalysis();
} catch (IOException e) {
throw new RuntimeException(e);
}

log.info("Done.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package org.matsim.freight.logistics.examples.multipleChains;

import java.io.IOException;
import java.util.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -43,6 +44,7 @@
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.misc.Time;
import org.matsim.freight.carriers.*;
import org.matsim.freight.carriers.analysis.RunFreightAnalysisEventBased;
import org.matsim.freight.carriers.controler.CarrierControlerUtils;
import org.matsim.freight.carriers.controler.CarrierScoringFunctionFactory;
import org.matsim.freight.carriers.controler.CarrierStrategyManager;
Expand Down Expand Up @@ -162,7 +164,16 @@ public void install() {
.setVspDefaultsCheckingLevel(VspExperimentalConfigGroup.VspDefaultsCheckingLevel.warn);
controler.run();

log.info("Done.");
//Carrier Analysis
final String outputPath = controler.getControlerIO().getOutputPath();
RunFreightAnalysisEventBased freightAnalysis = new RunFreightAnalysisEventBased(outputPath +"/", outputPath +"/Analysis/", config.global().getCoordinateSystem());
try {
freightAnalysis.runAnalysis();
} catch (IOException e) {
throw new RuntimeException(e);
}

log.info("Done.");
}

/*
Expand Down