From a90dbbd4c55c3fc62fdce1e6cb525bcb89e6c92f Mon Sep 17 00:00:00 2001 From: rakow Date: Mon, 1 Apr 2024 23:23:18 +0200 Subject: [PATCH] fix issues with injecting --- .../SelectSingleTripModeStrategyProvider.java | 6 +++++- .../replanning/scheduled/AllBestPlansStrategy.java | 2 +- .../modechoice/search/AbstractCandidateGenerator.java | 8 +++++--- .../matsim/modechoice/search/BestChoiceGenerator.java | 10 +++++----- .../modechoice/search/SingleTripChoicesGenerator.java | 3 ++- .../matsim/modechoice/search/TopKChoicesGenerator.java | 3 ++- .../org/matsim/modechoice/scheduled/solver.xml | 5 ++--- .../org/matsim/modechoice/search/TopKMinMaxTest.java | 2 +- 8 files changed, 23 insertions(+), 16 deletions(-) diff --git a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/replanning/SelectSingleTripModeStrategyProvider.java b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/replanning/SelectSingleTripModeStrategyProvider.java index dd9cea36045..9bbd46ac93a 100644 --- a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/replanning/SelectSingleTripModeStrategyProvider.java +++ b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/replanning/SelectSingleTripModeStrategyProvider.java @@ -1,5 +1,7 @@ package org.matsim.modechoice.replanning; +import org.matsim.core.config.Config; +import org.matsim.core.config.ConfigUtils; import org.matsim.core.config.groups.GlobalConfigGroup; import org.matsim.core.replanning.PlanStrategy; import org.matsim.core.replanning.PlanStrategyImpl; @@ -34,7 +36,7 @@ public class SelectSingleTripModeStrategyProvider implements Provider generator; @Inject - private InformedModeChoiceConfigGroup config; + private Config config; @Inject private Provider selector; @@ -45,6 +47,8 @@ public class SelectSingleTripModeStrategyProvider implements Provider()); builder.addStrategyModule(new SelectSingleTripModeStrategy(globalConfigGroup, config.getModes(), generator, selector, pruner, config.isRequireDifferentModes())); diff --git a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/replanning/scheduled/AllBestPlansStrategy.java b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/replanning/scheduled/AllBestPlansStrategy.java index b61856b96dd..8d80f35c168 100644 --- a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/replanning/scheduled/AllBestPlansStrategy.java +++ b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/replanning/scheduled/AllBestPlansStrategy.java @@ -145,7 +145,7 @@ public void run(Plan plan) { Id personId = plan.getPerson().getId(); List schedule = plans.get(personId); - if (schedule != null) { + if (schedule != null && !schedule.isEmpty()) { PlanCandidate candidate = schedule.get(applyIdx); candidate.applyTo(plan, true); diff --git a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/AbstractCandidateGenerator.java b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/AbstractCandidateGenerator.java index fe1c92afee2..ff5a7935864 100644 --- a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/AbstractCandidateGenerator.java +++ b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/AbstractCandidateGenerator.java @@ -2,6 +2,8 @@ import com.google.inject.Inject; import com.google.inject.Provider; +import org.matsim.core.config.Config; +import org.matsim.core.config.ConfigUtils; import org.matsim.core.scoring.functions.ScoringParametersForPerson; import org.matsim.modechoice.*; import org.matsim.modechoice.constraints.TripConstraint; @@ -42,9 +44,9 @@ abstract class AbstractCandidateGenerator implements CandidateGenerator { protected final InformedModeChoiceConfigGroup config; protected final Set allModes; - protected AbstractCandidateGenerator(InformedModeChoiceConfigGroup config) { - this.config = config; - this.allModes = new HashSet<>(config.getModes()); + protected AbstractCandidateGenerator(Config config) { + this.config = ConfigUtils.addOrGetModule(config, InformedModeChoiceConfigGroup.class); + this.allModes = new HashSet<>(this.config.getModes()); } @SuppressWarnings("unchecked") diff --git a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/BestChoiceGenerator.java b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/BestChoiceGenerator.java index c7f2ded08ea..1ba6e7b6034 100644 --- a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/BestChoiceGenerator.java +++ b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/BestChoiceGenerator.java @@ -1,14 +1,14 @@ package org.matsim.modechoice.search; import com.google.inject.Inject; -import org.matsim.api.core.v01.population.Plan; -import org.matsim.modechoice.InformedModeChoiceConfigGroup; -import org.matsim.modechoice.ModeOptions; +import org.matsim.core.config.Config; import org.matsim.modechoice.PlanCandidate; import org.matsim.modechoice.PlanModel; import javax.annotation.Nullable; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; /** * Generates the best known choices. @@ -20,7 +20,7 @@ public class BestChoiceGenerator extends TopKChoicesGenerator { // and a dedicated implementation might be more efficient @Inject - BestChoiceGenerator(InformedModeChoiceConfigGroup config) { + BestChoiceGenerator(Config config) { super(config); } diff --git a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/SingleTripChoicesGenerator.java b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/SingleTripChoicesGenerator.java index 5ba80a11740..4b2a6e4cad8 100644 --- a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/SingleTripChoicesGenerator.java +++ b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/SingleTripChoicesGenerator.java @@ -1,6 +1,7 @@ package org.matsim.modechoice.search; import org.apache.commons.lang3.ArrayUtils; +import org.matsim.core.config.Config; import org.matsim.modechoice.*; import javax.annotation.Nullable; @@ -14,7 +15,7 @@ public class SingleTripChoicesGenerator extends AbstractCandidateGenerator { @Inject - public SingleTripChoicesGenerator(InformedModeChoiceConfigGroup config) { + public SingleTripChoicesGenerator(Config config) { super(config); } diff --git a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/TopKChoicesGenerator.java b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/TopKChoicesGenerator.java index 27ed135b3b2..30981afc87d 100644 --- a/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/TopKChoicesGenerator.java +++ b/contribs/informed-mode-choice/src/main/java/org/matsim/modechoice/search/TopKChoicesGenerator.java @@ -5,6 +5,7 @@ import it.unimi.dsi.fastutil.objects.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.matsim.core.config.Config; import org.matsim.modechoice.*; import org.matsim.modechoice.estimators.FixedCostsEstimator; import org.matsim.modechoice.estimators.TripEstimator; @@ -31,7 +32,7 @@ public class TopKChoicesGenerator extends AbstractCandidateGenerator { @Inject - TopKChoicesGenerator(InformedModeChoiceConfigGroup config) { + TopKChoicesGenerator(Config config) { super(config); } diff --git a/contribs/informed-mode-choice/src/main/resources/org/matsim/modechoice/scheduled/solver.xml b/contribs/informed-mode-choice/src/main/resources/org/matsim/modechoice/scheduled/solver.xml index f33a11ee88e..16669b83372 100644 --- a/contribs/informed-mode-choice/src/main/resources/org/matsim/modechoice/scheduled/solver.xml +++ b/contribs/informed-mode-choice/src/main/resources/org/matsim/modechoice/scheduled/solver.xml @@ -17,13 +17,12 @@ - - 2 + 12 - 10000 + 5000 diff --git a/contribs/informed-mode-choice/src/test/java/org/matsim/modechoice/search/TopKMinMaxTest.java b/contribs/informed-mode-choice/src/test/java/org/matsim/modechoice/search/TopKMinMaxTest.java index 04cfe9860e2..2458d8fa933 100644 --- a/contribs/informed-mode-choice/src/test/java/org/matsim/modechoice/search/TopKMinMaxTest.java +++ b/contribs/informed-mode-choice/src/test/java/org/matsim/modechoice/search/TopKMinMaxTest.java @@ -70,7 +70,7 @@ public void setUp() throws Exception { InformedModeChoiceConfigGroup config = ConfigUtils.addOrGetModule(testModule.config, InformedModeChoiceConfigGroup.class); config.setModes(List.of(TransportMode.car, TransportMode.walk)); - generator = new TopKChoicesGenerator(config); + generator = new TopKChoicesGenerator(testModule.config); injector = Guice.createInjector(testModule);