Commit 7038997d authored by Sebastian Hörl's avatar Sebastian Hörl
Browse files

Make configurable via parameter file

parent c4b4a015
......@@ -22,7 +22,7 @@ public class RunASTRA2018002 {
static public void main(String[] args) throws ConfigurationException {
CommandLine cmd = new CommandLine.Builder(args) //
.requireOptions("config-path", "model") //
.allowOptions("use-route-choice", "use-only-significant") //
.allowOptions("use-route-choice", "use-only-significant", "utility-parameters") //
.allowPrefixes(SwissDiscreteModeChoiceModule.COMMAND_LINE_PREFIX) //
.build();
......
package ch.ethz.matsim.projects.astra_2018_002.mode_choice;
import java.io.File;
import java.io.IOException;
import org.matsim.api.core.v01.network.Network;
import org.matsim.core.router.TripRouter;
import org.matsim.facilities.ActivityFacilities;
......@@ -9,6 +12,7 @@ import com.google.inject.Provides;
import com.google.inject.Singleton;
import ch.ethz.matsim.baseline_scenario.config.CommandLine;
import ch.ethz.matsim.baseline_scenario.config.CommandLine.ConfigurationException;
import ch.ethz.matsim.discrete_mode_choice.modules.AbstractDiscreteModeChoiceExtension;
import ch.ethz.matsim.discrete_mode_choice.modules.DiscreteModeChoiceModule;
import ch.ethz.matsim.projects.astra_2018_002.mode_choice.SwissUtilityParameters.UtilitySet;
......@@ -48,8 +52,14 @@ public class SwissDiscreteModeChoiceModule extends AbstractDiscreteModeChoiceExt
@Provides
@Singleton
public SwissUtilityParameters provideSwissUtilityParameters() {
public SwissUtilityParameters provideSwissUtilityParameters() throws IOException, ConfigurationException {
SwissUtilityParameters parameters = SwissUtilityParameters.build(utilitySet, useRouteChoice, onlySignificant);
if (cmd.hasOption("utility-parameters")) {
File path = new File(cmd.getOptionStrict("utility-parameters"));
SwissUtilityParameters.adaptFromFile(parameters, path);
}
SwissUtilityParameters.adaptFromCommandLine(parameters, cmd);
return parameters;
}
......
......@@ -46,7 +46,7 @@ public class SwissUtilityEstimator extends BaseUtilityEstimator {
if (personVariables.isMale) {
utility += socioParameters.betaMale;
}
return utility;
}
......
package ch.ethz.matsim.projects.astra_2018_002.mode_choice;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
......@@ -530,9 +537,10 @@ public class SwissUtilityParameters {
return parameters;
}
/**
* Custom: Keep interactions and sociodemographics, but remove constants for region, municipality type and purpose
* Custom: Keep interactions and sociodemographics, but remove constants for
* region, municipality type and purpose
*/
static private SwissUtilityParameters buildCustom(boolean useRouteChoice, boolean onlySignificant) {
SwissUtilityParameters parameters = new SwissUtilityParameters();
......@@ -558,16 +566,18 @@ public class SwissUtilityParameters {
parameters.walk.sociodemographics.betaIncome = 0.056 * s;
parameters.walk.sociodemographics.betaIncomeSquared = -0.004 * s;
/*parameters.walk.purpose.betaWork = -0.088 * s;
parameters.walk.purpose.betaEducation = 0.655 * s;
parameters.walk.purpose.betaShop = -0.355 * s;
parameters.walk.purpose.betaErrand = -1.281;
parameters.walk.region.betaRegion1 = 0.403;
parameters.walk.region.betaRegion3 = -0.239 * s;
parameters.walk.municipalityType.betaSubUrban = -0.170 * s;
parameters.walk.municipalityType.betaRural = -0.003 * s;*/
/*
* parameters.walk.purpose.betaWork = -0.088 * s;
* parameters.walk.purpose.betaEducation = 0.655 * s;
* parameters.walk.purpose.betaShop = -0.355 * s;
* parameters.walk.purpose.betaErrand = -1.281;
*
* parameters.walk.region.betaRegion1 = 0.403;
* parameters.walk.region.betaRegion3 = -0.239 * s;
*
* parameters.walk.municipalityType.betaSubUrban = -0.170 * s;
* parameters.walk.municipalityType.betaRural = -0.003 * s;
*/
// Bike
parameters.bike.alpha = -2.517;
......@@ -580,16 +590,18 @@ public class SwissUtilityParameters {
parameters.bike.sociodemographics.betaIncome = 0.218;
parameters.bike.sociodemographics.betaIncomeSquared = -0.009;
/*parameters.bike.purpose.betaWork = 0.405;
parameters.bike.purpose.betaEducation = 0.953;
parameters.bike.purpose.betaShop = -0.859;
parameters.bike.purpose.betaErrand = -0.795;
parameters.bike.region.betaRegion1 = 0.663;
parameters.bike.region.betaRegion3 = 0.667;
parameters.bike.municipalityType.betaSubUrban = -0.266 * s;
parameters.bike.municipalityType.betaRural = -0.201 * s;*/
/*
* parameters.bike.purpose.betaWork = 0.405;
* parameters.bike.purpose.betaEducation = 0.953;
* parameters.bike.purpose.betaShop = -0.859; parameters.bike.purpose.betaErrand
* = -0.795;
*
* parameters.bike.region.betaRegion1 = 0.663;
* parameters.bike.region.betaRegion3 = 0.667;
*
* parameters.bike.municipalityType.betaSubUrban = -0.266 * s;
* parameters.bike.municipalityType.betaRural = -0.201 * s;
*/
// Car
parameters.car.alpha = 0.0;
......@@ -633,16 +645,17 @@ public class SwissUtilityParameters {
parameters.pt.sociodemographics.betaIncome = -0.026 * s;
parameters.pt.sociodemographics.betaIncomeSquared = 0.005 * s;
/*parameters.pt.purpose.betaWork = 0.326;
parameters.pt.purpose.betaEducation = 0.911;
parameters.pt.purpose.betaShop = -0.408;
parameters.pt.purpose.betaErrand = -0.347 * s;
parameters.pt.region.betaRegion1 = 0.415;
parameters.pt.region.betaRegion3 = 0.107 * s;
parameters.pt.municipalityType.betaSubUrban = -0.178;
parameters.pt.municipalityType.betaRural = -0.213;*/
/*
* parameters.pt.purpose.betaWork = 0.326; parameters.pt.purpose.betaEducation =
* 0.911; parameters.pt.purpose.betaShop = -0.408;
* parameters.pt.purpose.betaErrand = -0.347 * s;
*
* parameters.pt.region.betaRegion1 = 0.415; parameters.pt.region.betaRegion3 =
* 0.107 * s;
*
* parameters.pt.municipalityType.betaSubUrban = -0.178;
* parameters.pt.municipalityType.betaRural = -0.213;
*/
return parameters;
}
......@@ -670,35 +683,72 @@ public class SwissUtilityParameters {
}
}
static public void adaptFromCommandLine(SwissUtilityParameters parameters, CommandLine cmd) {
for (String option : cmd.getAvailableOptions()) {
static public void adaptFromMap(SwissUtilityParameters parameters, Map<String, Double> values) {
for (Map.Entry<String, Double> entry : values.entrySet()) {
String option = entry.getKey();
double value = entry.getValue();
try {
if (option.startsWith(SwissDiscreteModeChoiceModule.COMMAND_LINE_PREFIX + ":")) {
String accessor = option.split(":")[1];
String[] parts = accessor.split("\\.");
Object activeObject = parameters;
for (int i = 0; i < parts.length - 1; i++) {
Field field = activeObject.getClass().getField(parts[i]);
activeObject = field.get(activeObject);
}
Field field = activeObject.getClass().getField(parts[parts.length - 1]);
if (field.getType().equals(double.class)) {
double value = Double.parseDouble(cmd.getOptionStrict(option));
logger.info(String.format("Set %s = %f", accessor, value));
field.set(activeObject, value);
} else {
throw new IllegalStateException(
String.format("Option %s does not refer to a double field", option));
}
String[] parts = option.split("\\.");
Object activeObject = parameters;
for (int i = 0; i < parts.length - 1; i++) {
Field field = activeObject.getClass().getField(parts[i]);
activeObject = field.get(activeObject);
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| ConfigurationException e) {
Field field = activeObject.getClass().getField(parts[parts.length - 1]);
if (field.getType().equals(double.class)) {
logger.info(String.format("Set %s = %f", option, value));
field.set(activeObject, value);
} else {
throw new IllegalStateException(
String.format("Option %s does not refer to a double field", option));
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException("Error while processing option " + option);
}
}
}
static public void adaptFromCommandLine(SwissUtilityParameters parameters, CommandLine cmd) {
Map<String, Double> values = new HashMap<>();
for (String option : cmd.getAvailableOptions()) {
if (option.startsWith(SwissDiscreteModeChoiceModule.COMMAND_LINE_PREFIX + ":")) {
try {
values.put(option.split(":")[1], Double.parseDouble(cmd.getOptionStrict(option)));
} catch (ConfigurationException e) {
// Should not happen
}
}
}
adaptFromMap(parameters, values);
}
static public void adaptFromFile(SwissUtilityParameters parameters, File path) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
Map<String, Double> values = new HashMap<>();
String line = null;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("=");
if (parts.length != 2) {
reader.close();
throw new RuntimeException("Error while parsing line: " + line);
} else {
String option = parts[0].trim();
String value = parts[1].trim();
values.put(option, Double.parseDouble(value));
}
}
reader.close();
adaptFromMap(parameters, values);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment