diff --git a/core/src/main/java/org/eqasim/core/simulation/EqasimConfigurator.java b/core/src/main/java/org/eqasim/core/simulation/EqasimConfigurator.java index 9183bbcba..1ecd57a2f 100644 --- a/core/src/main/java/org/eqasim/core/simulation/EqasimConfigurator.java +++ b/core/src/main/java/org/eqasim/core/simulation/EqasimConfigurator.java @@ -1,6 +1,13 @@ package org.eqasim.core.simulation; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import java.util.function.BiConsumer; import org.eqasim.core.components.EqasimComponentsModule; @@ -8,7 +15,6 @@ import org.eqasim.core.components.traffic.EqasimTrafficQSimModule; import org.eqasim.core.components.transit.EqasimTransitModule; import org.eqasim.core.components.transit.EqasimTransitQSimModule; -import org.eqasim.core.simulation.calibration.CalibrationConfigGroup; import org.eqasim.core.simulation.mode_choice.epsilon.EpsilonModule; import org.eqasim.core.simulation.termination.EqasimTerminationModule; import org.eqasim.core.simulation.termination.mode_share.ModeShareModule; @@ -49,8 +55,7 @@ public EqasimConfigurator() { configGroups.addAll(Arrays.asList( // new SwissRailRaptorConfigGroup(), // new EqasimConfigGroup(), // - new DiscreteModeChoiceConfigGroup(), // - new CalibrationConfigGroup() // + new DiscreteModeChoiceConfigGroup() // )); modules.addAll(Arrays.asList( // diff --git a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationConfigGroup.java b/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationConfigGroup.java deleted file mode 100644 index ffc2e1cd2..000000000 --- a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationConfigGroup.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.eqasim.core.simulation.calibration; - -import org.matsim.core.config.Config; -import org.matsim.core.config.ReflectiveConfigGroup; - -public class CalibrationConfigGroup extends ReflectiveConfigGroup { - static public final String GROUP_NAME = "eqasim:calibration"; - - static public final String REFERENCE_PATH = "referencePath"; - static public final String ENABLE = "enableOutput"; - static public final String HINT = "hint"; - - private String referencePath; - private boolean enable = false; - private String hint = "No hint given."; - - public CalibrationConfigGroup() { - super(GROUP_NAME); - } - - @StringGetter(REFERENCE_PATH) - public String getReferencePath() { - return referencePath; - } - - @StringSetter(REFERENCE_PATH) - public void setReferencePath(String referencePath) { - this.referencePath = referencePath; - } - - @StringGetter(ENABLE) - public boolean getEnable() { - return enable; - } - - @StringSetter(ENABLE) - public void setEnable(boolean enable) { - this.enable = enable; - } - - @StringGetter(HINT) - public String getHint() { - return hint; - } - - @StringSetter(HINT) - public void setHint(String hint) { - this.hint = hint; - } - - static public CalibrationConfigGroup get(Config config) { - if (!config.getModules().containsKey(GROUP_NAME)) { - config.addModule(new CalibrationConfigGroup()); - } - - return (CalibrationConfigGroup) config.getModules().get(GROUP_NAME); - } -} diff --git a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationData.java b/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationData.java deleted file mode 100644 index ac56f84bc..000000000 --- a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationData.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.eqasim.core.simulation.calibration; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public class CalibrationData { - public Map totalModeShare = new HashMap<>(); - - public List distanceBoundaries = new LinkedList<>(); - public List> modeShareByDistance = new LinkedList<>(); -} diff --git a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationDataProvider.java b/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationDataProvider.java deleted file mode 100644 index 3067acd11..000000000 --- a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationDataProvider.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.eqasim.core.simulation.calibration; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.eqasim.core.analysis.trips.TripItem; -import org.eqasim.core.analysis.trips.TripListener; - -public class CalibrationDataProvider { - private final TripListener tripListener; - private final CalibrationData reference; - - public CalibrationDataProvider(TripListener tripListener, CalibrationData reference) { - this.tripListener = tripListener; - this.reference = reference; - } - - public CalibrationData getData() { - Collection modes = reference.totalModeShare.keySet(); - TripCountTracker tracker = new TripCountTracker(reference.totalModeShare.keySet(), - reference.distanceBoundaries); - - for (TripItem trip : tripListener.getTripItems()) { - if (trip.precedingPurpose.equals("outside")) { - continue; - } - - if (trip.followingPurpose.equals("outside")) { - continue; - } - - if (trip.mode.equals("outside")) { - continue; - } - - if (trip.euclideanDistance == 0.0) { - continue; - } - - tracker.addObservation(trip.mode, trip.euclideanDistance); - } - - CalibrationData data = new CalibrationData(); - data.distanceBoundaries = reference.distanceBoundaries; - - double totalNumberOfTrips = tracker.getCounts().values().stream().flatMap(Collection::stream) - .mapToDouble(d -> d).sum(); - - for (String mode : modes) { - double modeNumberOfTrips = tracker.getCounts().get(mode).stream().mapToDouble(d -> d).sum(); - data.totalModeShare.put(mode, modeNumberOfTrips / totalNumberOfTrips); - } - - for (int i = 0; i < data.distanceBoundaries.size() + 1; i++) { - Map binDistribution = new HashMap<>(); - - final int _i = i; - double distanceNumberOfTrips = tracker.getCounts().values().stream().mapToDouble(b -> b.get(_i)).sum(); - - for (String mode : modes) { - binDistribution.put(mode, tracker.getCounts().get(mode).get(i) / distanceNumberOfTrips); - } - - data.modeShareByDistance.add(binDistribution); - } - - return data; - } -} diff --git a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationModule.java b/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationModule.java deleted file mode 100644 index 308efaea1..000000000 --- a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationModule.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.eqasim.core.simulation.calibration; - -import java.io.File; - -import org.matsim.core.controler.AbstractModule; - -public class CalibrationModule extends AbstractModule { - @Override - public void install() { - CalibrationConfigGroup config = CalibrationConfigGroup.get(getConfig()); - - if (config.getEnable()) { - if (!new File(config.getReferencePath()).exists()) { - throw new IllegalStateException("Reference path does not exist: " + config.getReferencePath()); - } - - addControlerListenerBinding().to(CalibrationOutputListener.class); - } - } -} diff --git a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationOutput.java b/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationOutput.java deleted file mode 100644 index 72ebb1f8b..000000000 --- a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationOutput.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.eqasim.core.simulation.calibration; - -public class CalibrationOutput { - public CalibrationData reference = new CalibrationData(); - public CalibrationData simulation = new CalibrationData(); - - public String hint = ""; -} diff --git a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationOutputListener.java b/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationOutputListener.java deleted file mode 100644 index 569d335e0..000000000 --- a/core/src/main/java/org/eqasim/core/simulation/calibration/CalibrationOutputListener.java +++ /dev/null @@ -1,144 +0,0 @@ -package org.eqasim.core.simulation.calibration; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.nio.file.Files; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.eqasim.core.analysis.trips.TripListener; -import org.eqasim.core.components.config.EqasimConfigGroup; -import org.matsim.core.config.groups.ControlerConfigGroup; -import org.matsim.core.controler.OutputDirectoryHierarchy; -import org.matsim.core.controler.events.IterationEndsEvent; -import org.matsim.core.controler.events.ShutdownEvent; -import org.matsim.core.controler.listener.IterationEndsListener; -import org.matsim.core.controler.listener.ShutdownListener; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.inject.Inject; -import com.google.inject.Singleton; - -@Singleton -public class CalibrationOutputListener implements IterationEndsListener, ShutdownListener { - private final Logger logger = LogManager.getLogger(CalibrationOutputListener.class); - - private static final String OUTPUT_FILE_NAME_JSON = "calibration.json"; - private static final String OUTPUT_FILE_NAME_HTML = "calibration.html"; - - private final CalibrationConfigGroup config; - - private final OutputDirectoryHierarchy outputDirectory; - private final int lastIteration; - - private final TripListener tripAnalysisListener; - private final int tripAnalysisInterval; - - private CalibrationData referenceData; - - @Inject - public CalibrationOutputListener(EqasimConfigGroup eqasimConfig, CalibrationConfigGroup config, - ControlerConfigGroup controllerConfig, OutputDirectoryHierarchy outputDirectory, - TripListener tripListener) { - this.outputDirectory = outputDirectory; - this.lastIteration = controllerConfig.getLastIteration(); - - this.tripAnalysisInterval = eqasimConfig.getAnalysisInterval(); - this.tripAnalysisListener = tripListener; - - this.config = config; - - if (eqasimConfig.getAnalysisInterval() < 1) { - logger.warn("To use calibration output, the tripAnalysisInterval must be > 0"); - } - } - - private CalibrationData getReference() { - try { - if (referenceData == null) { - File inputPath = new File(config.getReferencePath()); - referenceData = new ObjectMapper().readValue(inputPath, CalibrationData.class); - } - - return referenceData; - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - private String readTemplate() { - try { - BufferedReader reader = new BufferedReader( - new InputStreamReader(CalibrationOutputListener.class.getResourceAsStream("calibration.html"))); - - StringBuilder template = new StringBuilder(); - String line = null; - - while ((line = reader.readLine()) != null) { - template.append(line); - template.append("\n"); - } - - return template.toString(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public void notifyIterationEnds(IterationEndsEvent event) { - if (config.getEnable()) { - try { - if (tripAnalysisInterval > 0 && event.getIteration() % tripAnalysisInterval == 0) { - CalibrationData data = new CalibrationDataProvider(tripAnalysisListener, getReference()).getData(); - - CalibrationOutput output = new CalibrationOutput(); - output.reference = getReference(); - output.simulation = data; - output.hint = config.getHint(); - - // Write JSON - File outputFile = new File( - outputDirectory.getIterationFilename(event.getIteration(), OUTPUT_FILE_NAME_JSON)); - new ObjectMapper().writeValue(outputFile, output); - - // Write HTML - String template = readTemplate(); - String replacement = "var data = " + new ObjectMapper().writeValueAsString(output) + ";"; - template = template.replace("", replacement); - - outputFile = new File( - outputDirectory.getIterationFilename(event.getIteration(), OUTPUT_FILE_NAME_HTML)); - BufferedWriter writer = new BufferedWriter( - new OutputStreamWriter(new FileOutputStream(outputFile))); - writer.write(template); - writer.close(); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } - - @Override - public void notifyShutdown(ShutdownEvent event) { - if (config.getEnable()) { - try { - File iterationPath = new File( - outputDirectory.getIterationFilename(lastIteration, OUTPUT_FILE_NAME_JSON)); - File outputPath = new File(outputDirectory.getOutputFilename(OUTPUT_FILE_NAME_JSON)); - Files.copy(iterationPath.toPath(), outputPath.toPath()); - - iterationPath = new File(outputDirectory.getIterationFilename(lastIteration, OUTPUT_FILE_NAME_HTML)); - outputPath = new File(outputDirectory.getOutputFilename(OUTPUT_FILE_NAME_HTML)); - Files.copy(iterationPath.toPath(), outputPath.toPath()); - } catch (IOException e) { - } - } - } -} diff --git a/core/src/main/java/org/eqasim/core/simulation/calibration/TripCountTracker.java b/core/src/main/java/org/eqasim/core/simulation/calibration/TripCountTracker.java deleted file mode 100644 index bf80c211f..000000000 --- a/core/src/main/java/org/eqasim/core/simulation/calibration/TripCountTracker.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.eqasim.core.simulation.calibration; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class TripCountTracker { - private final List boundaries; - private final Map> counts = new HashMap<>(); - - public TripCountTracker(Collection modes, List boundaries) { - this.boundaries = boundaries; - - for (String mode : modes) { - counts.put(mode, new ArrayList<>(Collections.nCopies(boundaries.size() + 1, 0.0))); - } - } - - static public int getBin(double distance, List boundaries) { - int bin = 0; - - for (double boundary : boundaries) { - if (distance > boundary) { - bin++; - } - } - - return bin; - } - - public void addObservation(String mode, double distance) { - int bin = getBin(distance, boundaries); - - if (counts.containsKey(mode)) { - counts.get(mode).set(bin, counts.get(mode).get(bin) + 1); - } - } - - public Map> getCounts() { - return counts; - } -} diff --git a/core/src/main/resources/melun/config.xml b/core/src/main/resources/melun/config.xml index ea97f009c..6a9048784 100644 --- a/core/src/main/resources/melun/config.xml +++ b/core/src/main/resources/melun/config.xml @@ -233,11 +233,6 @@ - - - - - diff --git a/ile_de_france/src/main/resources/corsica/corsica_config.xml b/ile_de_france/src/main/resources/corsica/corsica_config.xml index a263b3bb2..d93df0cd0 100644 --- a/ile_de_france/src/main/resources/corsica/corsica_config.xml +++ b/ile_de_france/src/main/resources/corsica/corsica_config.xml @@ -233,11 +233,6 @@ - - - - -