Skip to content

Commit

Permalink
towards systematic injection of opdyts/matsim objective functions
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnarF authored and GunnarF committed Nov 12, 2018
1 parent 4b55201 commit a75e3b0
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 125 deletions.
66 changes: 44 additions & 22 deletions stockholm/src/main/java/gunnar/ihop4/IHOP4ProductionRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.contrib.opdyts.buildingblocks.calibration.counting.AbsoluteLinkEntryCountDeviationObjectiveFunction;
import org.matsim.contrib.pseudosimulation.searchacceleration.Greedo;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting;
import org.matsim.core.controler.events.BeforeMobsimEvent;
import org.matsim.core.controler.listener.BeforeMobsimListener;
import org.matsim.core.scenario.ScenarioUtils;

import gunnar.ihop4.tollzonepassagedata.TollZoneMeasurementReader;

/**
*
* @author Gunnar Flötteröd
Expand Down Expand Up @@ -90,32 +96,48 @@ static void run(boolean useGreedo) {
final Controler controler = new Controler(scenario);
if (greedo != null) {
controler.addOverridingModule(greedo);
// greedo.meet(controler);
}

// Toll zone link flows.

// final LinkFlowTimeSeries tollZoneFlows = new LinkFlowTimeSeries(new
// TimeDiscretization(0, 3600, 30));
// tollZoneFlows.setLogFileName("tollZoneFlows.data");
// for (String linkName : new String[] { "101350_NW", "112157_SE", "9216_N",
// "13354_W", "18170_SE", "35424_NW",
// "44566_NE", "51930_W", "53064_SE", "68760_SW", "74188_NW", "79555_NE",
// "90466_SE", "122017_NW",
// "122017_SE", "22626_W", "77626_W", "90317_W", "92866_E", "110210_E",
// "2453_SW", "6114_S", "6114_N",
// "25292_NE", "28480_S", "34743_NE", "124791_W", "71617_SW", "71617_SE",
// "80449_N", "96508_NW",
// "96508_SE", "108353_SE", "113763_NW", "121908_N", "121908_S", "52416_NE",
// "125425_N" }) {
// tollZoneFlows.addObservedLink(linkName);
// }
// controler.addOverridingModule(new AbstractModule() {
// @Override
// public void install() {
// this.addEventHandlerBinding().toInstance(tollZoneFlows);
// }
// });
final TollZoneMeasurementReader measReader = new TollZoneMeasurementReader(config);
measReader.run();
for (AbstractModule module : measReader.getAllDayMeasurements().getModules()) {
controler.addOverridingModule(module);
}
for (AbstractModule module : measReader.getOnlyTollTimeMeasurements().getModules()) {
controler.addOverridingModule(module);
}

// Checking if this really works:

final int testIteration = 10;

BeforeMobsimListener testListener = new BeforeMobsimListener() {
@Override
public void notifyBeforeMobsim(BeforeMobsimEvent event) {
if (event.getIteration() == testIteration) {
for (AbsoluteLinkEntryCountDeviationObjectiveFunction objFct : measReader.getAllDayMeasurements().getObjectiveFunctions()) {
System.out.println();
System.out.println(objFct);
}
for (AbsoluteLinkEntryCountDeviationObjectiveFunction objFct : measReader.getOnlyTollTimeMeasurements().getObjectiveFunctions()) {
System.out.println();
System.out.println(objFct);
}
System.out.println();
System.out.println("TERMINATING IN ITERATION " + event.getIteration());
System.exit(0);
}
}
};

controler.addOverridingModule(new AbstractModule() {
@Override
public void install() {
this.addControlerListenerBinding().toInstance(testListener);
}
});

// ... and run.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abstract class AbstractTollZonePassageDataHandler extends AbstractTabularFileHan

@Override
public final void startDataRow(String[] args) {

this.time_s = null;
this.chargingPointStr = null;
this.linkStr = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,35 @@
* @author Gunnar Flötteröd
*
*/
public class PassageDataAnalyzer {
class PassageDataAnalyzer {

private final DynamicData<String> data;

private final double[] weightPerVehicleLengthClass;

public PassageDataAnalyzer(final TimeDiscretization timeDiscr, final double[] weightPerVehicleLengthClass) {
PassageDataAnalyzer(final TimeDiscretization timeDiscr, final double[] weightPerVehicleLengthClass) {
this.data = new DynamicData<>(timeDiscr);
this.weightPerVehicleLengthClass = weightPerVehicleLengthClass;
}

public void parse(final String file) {
void parse(final String file) {

final TabularFileParser parser = new TabularFileParser();
parser.setDelimiterTags(new String[] { "," });
parser.setOmitEmptyColumns(false);

final PassageDataHandler handler = new PassageDataHandler(this.data, this.weightPerVehicleLengthClass, false);
final PassageDataHandler handler = new PassageDataHandler(this.data, this.weightPerVehicleLengthClass);
try {
parser.parse(file, handler);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

DynamicData<String> getData() {
return this.data;
}

@Override
public String toString() {
final StringBuffer result = new StringBuffer();
Expand All @@ -80,19 +84,16 @@ public String toString() {
}
return result.toString();
}

public DynamicData<String> getData() {
return this.data;
}

public static void main(String[] args) throws IOException {

final TimeDiscretization timeDiscr = new TimeDiscretization(0, 3600, 24);
final TimeDiscretization timeDiscr = new TimeDiscretization(0, 3600 / 2, 24 * 2);

final List<String> days = Arrays.asList("2016-10-11", "2016-10-12", "2016-10-13", "2016-10-18", "2016-10-19",
"2016-10-20", "2016-10-25", "2016-10-26", "2016-10-27");

PassageDataAnalyzer dataAnalyzer = new PassageDataAnalyzer(timeDiscr, null);
PassageDataAnalyzer dataAnalyzer = new PassageDataAnalyzer(timeDiscr,
new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});

for (String day : days) {
for (String postfix : new String[] { "-01", "-02" }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,24 @@ class PassageDataHandler extends AbstractTollZonePassageDataHandler {

private final double[] weightPerMeterLengthClass;

private final boolean includeChargingPointId;

PassageDataHandler(final DynamicData<String> data, final double[] weightPerMeterLengthClass,
final boolean includeChargingPointId) {
PassageDataHandler(final DynamicData<String> data, final double[] weightPerMeterLengthClass) {
this.data = data;
this.weightPerMeterLengthClass = weightPerMeterLengthClass;
this.includeChargingPointId = includeChargingPointId;
}

@Override
protected void processFields() {

final String key = this.linkStr + (this.includeChargingPointId ? "(" + this.chargingPointStr + ")" : "");

if (this.containsRegisterData) {
if (this.registerDataFeasible) {
this.data.add(key, this.data.bin((int) this.time_s), 1.0);
this.data.add(super.linkStr, this.data.bin((int) this.time_s), 1.0);
}
} else { // contains no register data -> weight by length class
int lengthClass = this.vehicleLength_cm / 100;
if (lengthClass < this.weightPerMeterLengthClass.length) {
this.data.add(key, this.data.bin((int) this.time_s), this.weightPerMeterLengthClass[lengthClass]);
this.data.add(super.linkStr, this.data.bin((int) this.time_s),
this.weightPerMeterLengthClass[lengthClass]);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,26 @@
*/
class SizeAnalysisHandler extends AbstractTollZonePassageDataHandler {

private final int[] totalVehiclesPerMeterLengthClass;
private final int[] relevantIdentifiedVehiclesPerMeterLengthClass;
private final int[] identifiedVehiclesPerMeterLengthClass;

SizeAnalysisHandler(final int[] totalVehiclesPerMeterLengthClass, final int[] relevantVehiclesPerMeterLengthClass,
SizeAnalysisHandler(final int[] relevantIdentifiedVehiclesPerMeterLengthClass,
final int[] identifiedVehiclesPerMeterLengthClass) {
this.totalVehiclesPerMeterLengthClass = totalVehiclesPerMeterLengthClass;
this.relevantIdentifiedVehiclesPerMeterLengthClass = relevantVehiclesPerMeterLengthClass;
this.relevantIdentifiedVehiclesPerMeterLengthClass = relevantIdentifiedVehiclesPerMeterLengthClass;
this.identifiedVehiclesPerMeterLengthClass = identifiedVehiclesPerMeterLengthClass;
}

@Override
protected void processFields() {

final int lengthClass = this.vehicleLength_cm / 100;
if ((lengthClass < 0) || (lengthClass >= this.totalVehiclesPerMeterLengthClass.length)) {
final int lengthClass = super.vehicleLength_cm / 100;
if ((lengthClass < 0) || (lengthClass >= this.identifiedVehiclesPerMeterLengthClass.length)) {
return;
}
this.totalVehiclesPerMeterLengthClass[lengthClass]++;

if (this.containsRegisterData) {
if (super.containsRegisterData) {
this.identifiedVehiclesPerMeterLengthClass[lengthClass]++;
if (this.registerDataFeasible) {
if (super.registerDataFeasible) {
this.relevantIdentifiedVehiclesPerMeterLengthClass[lengthClass]++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,56 +32,51 @@
* @author Gunnar Flötteröd
*
*/
public class SizeAnalyzer {
class SizeAnalyzer {

private final int[] totalVehiclesPerMeterLengthClass;
private final int[] relevantIdentifiedVehiclesPerMeterLengthClass;
private final int[] identifiedVehiclesPerMeterLengthClass;

public SizeAnalyzer(final int maxLength_m) {
this.totalVehiclesPerMeterLengthClass = new int[maxLength_m];
SizeAnalyzer(final int maxLength_m) {
this.relevantIdentifiedVehiclesPerMeterLengthClass = new int[maxLength_m];
this.identifiedVehiclesPerMeterLengthClass = new int[maxLength_m];
}

public void parse(final String file) {
void parse(final String file) {

final TabularFileParser parser = new TabularFileParser();
parser.setDelimiterTags(new String[] { "," });
parser.setOmitEmptyColumns(false);

final SizeAnalysisHandler handler = new SizeAnalysisHandler(this.totalVehiclesPerMeterLengthClass,
this.relevantIdentifiedVehiclesPerMeterLengthClass, this.identifiedVehiclesPerMeterLengthClass);
final SizeAnalysisHandler handler = new SizeAnalysisHandler(this.relevantIdentifiedVehiclesPerMeterLengthClass,
this.identifiedVehiclesPerMeterLengthClass);
try {
parser.parse(file, handler);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public double[] getRelevantProbaPerMeterLengthClass() {
final double[] result = new double[this.totalVehiclesPerMeterLengthClass.length];
for (int i = 0; i < this.totalVehiclesPerMeterLengthClass.length; i++) {

double[] getRelevanceProbabilityPerMeterLengthClass() {
final double[] result = new double[this.identifiedVehiclesPerMeterLengthClass.length];
for (int i = 0; i < this.identifiedVehiclesPerMeterLengthClass.length; i++) {
final double relevantIdentified = this.relevantIdentifiedVehiclesPerMeterLengthClass[i];
final double identified = this.identifiedVehiclesPerMeterLengthClass[i];
if (identified > 0) {
result[i] = relevantIdentified / identified;
result[i] = relevantIdentified / identified;
}

}
return result;
}

@Override
public String toString() {
final double[] relevantProba = this.getRelevantProbaPerMeterLengthClass();
final StringBuffer result = new StringBuffer("length[m]\ttotal\tidentified\trelevantIdentified\tP(relevant)\n");
for (int i = 0; i < this.totalVehiclesPerMeterLengthClass.length; i++) {
final int total = this.totalVehiclesPerMeterLengthClass[i];
final double[] relevantProba = this.getRelevanceProbabilityPerMeterLengthClass();
final StringBuffer result = new StringBuffer("length[m]\tidentified\trelevantIdentified\tPr(relevant)\n");
for (int i = 0; i < this.identifiedVehiclesPerMeterLengthClass.length; i++) {
final int relevantIdentified = this.relevantIdentifiedVehiclesPerMeterLengthClass[i];
final int identified = this.identifiedVehiclesPerMeterLengthClass[i];
result.append(i + "\t" + total + "\t" + identified + "\t" + relevantIdentified + "\t"
+ (((double) relevantIdentified) / identified) + "\t" + relevantProba[i] + "\n");
result.append(i + "\t" + identified + "\t" + relevantIdentified + "\t" + relevantProba[i] + "\n");
}
return result.toString();
}
Expand Down
Loading

0 comments on commit a75e3b0

Please sign in to comment.