Skip to content

Commit

Permalink
fix default args and add more tests for config update (#3129)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow authored Feb 22, 2024
1 parent 20cd7b9 commit 788b93f
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ else if (property.equalsIgnoreCase("false") || property.equals("0"))
public static void applyConfigUpdate(Config config, Path yaml) {

if (!Files.exists(yaml)) {
throw new IllegalArgumentException("Desired run config does not exist:" + yaml);
throw new IllegalArgumentException("Given config yaml does not exist: " + yaml);
}

ObjectMapper mapper = new ObjectMapper(new YAMLFactory()
Expand All @@ -139,8 +139,8 @@ public static void applyConfigUpdate(Config config, Path yaml) {
String configGroupName = aliases.resolveAlias(field.getKey(), emptyStack);
ConfigGroup group = config.getModules().get(configGroupName);
if (group == null) {
log.warn("Config group not found: {}", configGroupName);
continue;
group = new ConfigGroup(configGroupName);
config.addModule(group);
}

applyNodeToConfigGroup(field.getValue(), group);
Expand Down Expand Up @@ -168,7 +168,17 @@ private static void applyNodeToConfigGroup(JsonNode node, ConfigGroup group) {
List<? extends ConfigGroup> params = new ArrayList<>(group.getParameterSets(field.getKey()));

for (JsonNode item : field.getValue()) {
applyNodeAsParameterSet(field.getKey(), item, group, params);


// Special case of parameter sets that have only one entry
if (field.getValue().size() == 1 && params.size() == 1 && field.getValue().get(0).isObject()) {

applyNodeToConfigGroup(field.getValue().get(0), params.get(0));

} else {

applyNodeAsParameterSet(field.getKey(), item, group, params);
}
}
} else {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public static void runWithDefaults(Class<? extends MATSimApplication> clazz, Str
} else {
// Automatically add run command
String[] runArgs = ApplicationUtils.mergeArgs(new String[]{"run"}, defaultArgs);
args = ApplicationUtils.mergeArgs(defaultArgs, runArgs);
args = ApplicationUtils.mergeArgs(runArgs, args);
}

} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.ReflectiveConfigGroup;
import org.matsim.core.config.groups.ScoringConfigGroup;
import org.matsim.testcases.MatsimTestUtils;

import java.nio.file.Path;
Expand All @@ -22,6 +24,32 @@ public class ConfigYamlUpdateTest {
@RegisterExtension
private MatsimTestUtils utils = new MatsimTestUtils();

@Test
void params() {

Path input = Path.of(utils.getClassInputDirectory());

Config config = ConfigUtils.loadConfig(input.resolve("config.xml").toString());

ApplicationUtils.applyConfigUpdate(
config, input.resolve("params.yml")
);

ScoringConfigGroup scoring = ConfigUtils.addOrGetModule(config, ScoringConfigGroup.class);

assertThat(scoring.getModes())
.hasSize(7);

assertThat(scoring.getPerforming_utils_hr())
.isEqualTo(6.88);

ScoringConfigGroup.ModeParams car = scoring.getModes().get(TransportMode.car);

assertThat(car.getConstant()).isEqualTo(-0.62);
assertThat(car.getMarginalUtilityOfTraveling()).isEqualTo(0);

}

@Test
void standard() {

Expand Down Expand Up @@ -74,6 +102,22 @@ void createParamSet() {
assertThat(next.getParams().get("extra")).isEqualTo("extra");
}

@Test
void createGroup() {
Config config = ConfigUtils.createConfig();
Path input = Path.of(utils.getClassInputDirectory());

ApplicationUtils.applyConfigUpdate(
config, input.resolve("multiLevel.yml")
);


TestConfigGroup test = ConfigUtils.addOrGetModule(config, TestConfigGroup.class);

assertThat(test.values).containsExactly(1, 2, 3);

}


@Test
void multiLevel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config SYSTEM "http://www.matsim.org/files/dtd/config_v2.dtd">
<config>
<module name="scoring">
<param name="fractionOfIterationsToStartScoreMSA" value="0.8"/>
<parameterset type="scoringParameters">
<param name="marginalUtilityOfMoney" value="1.0"/>
<param name="performing" value="6.88"/>

<parameterset type="modeParams">
<param name="constant" value="-0.6269580844054548"/>
<param name="dailyMonetaryConstant" value="-3.0"/>
<param name="dailyUtilityConstant" value="0"/>
<param name="marginalUtilityOfDistance_util_m" value="0.0"/>
<param name="marginalUtilityOfTraveling_util_hr" value="0.0"/>
<param name="monetaryDistanceRate" value="-1.49E-4"/>
<param name="mode" value="car"/>
</parameterset>
<parameterset type="modeParams">
<param name="constant" value="-1.3788242711735"/>
<param name="dailyMonetaryConstant" value="0.0"/>
<param name="dailyUtilityConstant" value="0.0"/>
<param name="marginalUtilityOfDistance_util_m" value="0.0"/>
<param name="marginalUtilityOfTraveling_util_hr" value="-6.88"/>
<param name="monetaryDistanceRate" value="-1.49E-4"/>
<param name="mode" value="ride"/>
</parameterset>
<parameterset type="modeParams">
<param name="constant" value="-0.24909103628514367"/>
<param name="dailyMonetaryConstant" value="-3.0"/>
<param name="dailyUtilityConstant" value="0.0"/>
<param name="marginalUtilityOfDistance_util_m" value="0.0"/>
<param name="marginalUtilityOfTraveling_util_hr" value="0.0"/>
<param name="monetaryDistanceRate" value="0.0"/>
<param name="mode" value="pt"/>
</parameterset>
<parameterset type="modeParams">
<param name="constant" value="-2.2321797854695955"/>
<param name="dailyMonetaryConstant" value="0.0"/>
<param name="dailyUtilityConstant" value="0.0"/>
<param name="marginalUtilityOfDistance_util_m" value="0.0"/>
<param name="marginalUtilityOfTraveling_util_hr" value="0.0"/>
<param name="monetaryDistanceRate" value="0.0"/>
<param name="mode" value="bike"/>
</parameterset>
<parameterset type="modeParams">
<param name="constant" value="0.0"/>
<param name="dailyMonetaryConstant" value="0.0"/>
<param name="dailyUtilityConstant" value="0.0"/>
<param name="marginalUtilityOfDistance_util_m" value="0.0"/>
<param name="marginalUtilityOfTraveling_util_hr" value="0.0"/>
<param name="monetaryDistanceRate" value="0.0"/>
<param name="mode" value="walk"/>
</parameterset>
<parameterset type="modeParams">
<param name="constant" value="0.0"/>
<param name="dailyMonetaryConstant" value="0.0"/>
<param name="dailyUtilityConstant" value="0.0"/>
<param name="marginalUtilityOfDistance_util_m" value="0.0"/>
<param name="marginalUtilityOfTraveling_util_hr" value="0.0"/>
<param name="monetaryDistanceRate" value="-4.0E-4"/>
<param name="mode" value="freight"/>
</parameterset>
<parameterset type="modeParams">
<param name="constant" value="0.0"/>
<param name="dailyMonetaryConstant" value="0.0"/>
<param name="dailyUtilityConstant" value="0.0"/>
<param name="marginalUtilityOfDistance_util_m" value="0.0"/>
<param name="marginalUtilityOfTraveling_util_hr" value="0.0"/>
<param name="monetaryDistanceRate" value="-4.0E-4"/>
<param name="mode" value="truck"/>
</parameterset>
</parameterset>
</module>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
scoring:
scoringParameters:
- modeParams:
- mode: walk
constant: 0.0
- mode: car
constant: -0.62
- mode: pt
constant: -0.25
- mode: bike
constant: -2.23
- mode: ride
constant: -1.37

0 comments on commit 788b93f

Please sign in to comment.