From 6047a78130b9429e0ec80d872961a214552498a2 Mon Sep 17 00:00:00 2001 From: rakow Date: Sun, 11 Feb 2024 11:08:34 +0100 Subject: [PATCH] fix performance in scoring param lookup --- .../DetailedPersonScoringParameters.java | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/matsim/run/scoring/DetailedPersonScoringParameters.java b/src/main/java/org/matsim/run/scoring/DetailedPersonScoringParameters.java index fe993601..c1a0726a 100644 --- a/src/main/java/org/matsim/run/scoring/DetailedPersonScoringParameters.java +++ b/src/main/java/org/matsim/run/scoring/DetailedPersonScoringParameters.java @@ -20,55 +20,51 @@ public class DetailedPersonScoringParameters implements ScoringParametersForPers /** * Cache instances of {@link ActivityUtilityParameters} for each subpopulation. */ - private final Map> activityParamsPerSubpopulation = new ConcurrentHashMap<>(); + private final Map cache = new ConcurrentHashMap<>(); - /** - * Cache instances of {@link ModeUtilityParameters} for each mode. - */ - private final Map modeParams = new TreeMap<>(); private final Scenario scenario; private final VspScoringConfigGroup vspScoring; + private final ScoringConfigGroup scoring; @Inject public DetailedPersonScoringParameters(Scenario scenario) { this.scenario = scenario; this.vspScoring = ConfigUtils.addOrGetModule(scenario.getConfig(), VspScoringConfigGroup.class); + this.scoring = scenario.getConfig().scoring(); } @Override public ScoringParameters getScoringParameters(Person person) { - ScoringConfigGroup scoring = scenario.getConfig().scoring(); String subpopulation = PopulationUtils.getSubpopulation(person); - ScoringConfigGroup.ScoringParameterSet scoringParameters = scoring.getScoringParameters(subpopulation); + return this.cache.computeIfAbsent(subpopulation, k -> { + + ScoringConfigGroup.ScoringParameterSet scoringParameters = scoring.getScoringParameters(k); - Map personParams = this.activityParamsPerSubpopulation.computeIfAbsent(subpopulation, k -> { Map activityParams = new TreeMap<>(); for (ScoringConfigGroup.ActivityParams params : scoringParameters.getActivityParams()) { ActivityUtilityParameters.Builder factory = new ActivityUtilityParameters.Builder(params); activityParams.put(params.getActivityType(), factory.build()); } - return activityParams; - }); - ScoringParameters.Builder builder = new ScoringParameters.Builder(scoring, scoringParameters, personParams, - scenario.getConfig().scenario()); + ScoringParameters.Builder builder = new ScoringParameters.Builder(scoring, + scoringParameters, activityParams, scenario.getConfig().scenario()); - // TODO: not configurable at the moment - if ("person".equals(subpopulation)) { + if ("person".equals(k)) { - for (Map.Entry e : vspScoring.getModeParams().entrySet()) { + for (Map.Entry e : vspScoring.getModeParams().entrySet()) { - ModeUtilityParameters params = builder.getModeParameters(e.getKey()); - DistanceGroupModeUtilityParameters p = modeParams.computeIfAbsent(e.getKey(), - k -> new DistanceGroupModeUtilityParameters(params, vspScoring.getDistGroups(), e.getValue())); + ModeUtilityParameters params = builder.getModeParameters(e.getKey()); + DistanceGroupModeUtilityParameters p = new DistanceGroupModeUtilityParameters(params, + vspScoring.getDistGroups(), e.getValue()); - builder.setModeParameters(e.getKey(), p); + builder.setModeParameters(e.getKey(), p); + } } - } - return builder.build(); + return builder.build(); + }); } }