Skip to content

Commit

Permalink
update run script and category class
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Mar 30, 2024
1 parent c58fbe8 commit 026be77
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
58 changes: 58 additions & 0 deletions src/main/java/org/matsim/run/scoring/Category.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.matsim.run.scoring;

import org.matsim.core.config.ReflectiveConfigGroup;
import org.matsim.utils.objectattributes.attributable.Attributes;

import java.util.*;
import java.util.regex.Pattern;

/**
* Categorize values into groups.
Expand All @@ -21,21 +25,69 @@ public final class Category {
*/
private final Map<String, String> grouped;


/**
* Regular expressions for each category.
*/
private final Map<String, Pattern> regex;

/**
* Range categories.
*/
private final List<Range> ranges;

/**
* Create categories from config parameters.
*/
public static Map<String, Category> fromConfigParams(Collection<? extends ReflectiveConfigGroup> params) {

Map<String, Set<String>> categories = new HashMap<>();

// Collect all values
for (ReflectiveConfigGroup parameter : params) {
for (Map.Entry<String, String> kv : parameter.getParams().entrySet()) {
categories.computeIfAbsent(kv.getKey(), k -> new HashSet<>()).add(kv.getValue());
}
}

return categories.entrySet().stream()
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), new Category(e.getValue())), HashMap::putAll);
}

/**
* Match attributes from an object with parameters defined in config.
*/
public static boolean matchAttributesWithConfig(Attributes attr, ReflectiveConfigGroup config, Map<String, Category> categories) {

for (Map.Entry<String, String> e : config.getParams().entrySet()) {
// might be null if not defined
Object objValue = attr.getAttribute(e.getKey());
String category = categories.get(e.getKey()).categorize(objValue);

// compare as string
if (!Objects.toString(category).equals(e.getValue()))
return false;
}

return true;
}


public Category(Set<String> values) {
this.values = values;
this.grouped = new HashMap<>();
this.regex = new HashMap<>();
for (String v : values) {
if (v.contains(",")) {
String[] grouped = v.split(",");
for (String g : grouped) {
this.grouped.put(g, v);
}
}

if (v.startsWith("/") && v.endsWith("/")) {
this.regex.put(v, Pattern.compile(v.substring(1, v.length() - 1), Pattern.CASE_INSENSITIVE));
}
}

boolean range = this.values.stream().allMatch(v -> v.contains("-") || v.contains("+"));
Expand Down Expand Up @@ -85,6 +137,12 @@ public String categorize(Object value) {
return v;
else if (grouped.containsKey(v))
return grouped.get(v);
else {
for (Map.Entry<String, Pattern> kv : regex.entrySet()) {
if (kv.getValue().matcher(v).matches())
return kv.getKey();
}
}

try {
double d = Double.parseDouble(v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,10 @@ public IndividualPersonScoringParameters(Scenario scenario) {
this.scoring = ConfigUtils.addOrGetModule(scenario.getConfig(), AdvancedScoringConfigGroup.class);
this.transitConfig = scenario.getConfig().transit();
this.globalAvgIncome = computeAvgIncome(scenario.getPopulation());
this.categories = buildCategories(this.scoring);
this.categories = Category.fromConfigParams(this.scoring.getScoringParameters());
this.cache = new IdMap<>(Person.class, scenario.getPopulation().getPersons().size());
}

static Map<String, Category> buildCategories(AdvancedScoringConfigGroup scoring) {

Map<String, Set<String>> categories = new HashMap<>();

// Collect all values
for (AdvancedScoringConfigGroup.ScoringParameters parameter : scoring.getScoringParameters()) {
for (Map.Entry<String, String> kv : parameter.getParams().entrySet()) {
categories.computeIfAbsent(kv.getKey(), k -> new HashSet<>()).add(kv.getValue());
}
}

return categories.entrySet().stream()
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), new Category(e.getValue())), HashMap::putAll);
}

static DistanceGroup[] calcDistanceGroups(List<Integer> dists, DoubleList distUtils) {

// Nothing to do if no distance groups are defined.
Expand Down Expand Up @@ -223,7 +208,7 @@ public ScoringParameters getScoringParameters(Person person) {

for (AdvancedScoringConfigGroup.ScoringParameters parameter : scoring.getScoringParameters()) {

if (parameter.matchObject(person.getAttributes(), categories)) {
if (Category.matchAttributesWithConfig(person.getAttributes(), parameter, categories)) {
for (Map.Entry<String, AdvancedScoringConfigGroup.ModeParams> mode : parameter.getModeParams().entrySet()) {

DistanceGroupModeUtilityParameters.DeltaBuilder b =
Expand Down
2 changes: 1 addition & 1 deletion src/main/sh/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export RUN_NAME="$name"
echo "Starting run $name"
echo "$*"

sbatch --export=ALL --job-name matsim-"$name" job.sh
sbatch --parsable --export=ALL --job-name matsim-"$name" job.sh

0 comments on commit 026be77

Please sign in to comment.