19 package org.matsim.core.replanning;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.LinkedHashSet;
26 import java.util.List;
28 import java.util.TreeMap;
30 import org.apache.logging.log4j.LogManager;
31 import org.apache.logging.log4j.Logger;
59 final List<GenericPlanStrategy<T, I>> strategies =
new ArrayList<>();
60 final List<GenericPlanStrategy<T, I>> unmodifiableStrategies = Collections.unmodifiableList( strategies );
61 final List<Double> weights =
new ArrayList<>();
62 final List<Double> unmodifiableWeights = Collections.unmodifiableList(weights);
63 double totalWeights = 0.0;
64 final Map<Integer, Map<GenericPlanStrategy<T, I>, Double>> changeRequests =
new TreeMap<>();
66 @Override
public int size() {
67 return weights.size();
70 @Override
public double getWeight(
int idx) {
71 return weights.get(idx);
75 return strategies.get(idx);
78 @Override
public double getTotalWeights() {
118 final String subpopulation,
119 final double weight )
122 if ( weights.strategies.contains( strategy ) ) {
123 log.error(
"Strategy "+strategy+
" is already defined for subpopulation "+subpopulation );
124 log.error(
"This can lead to undefined behavior. Please only specify each strategy once" );
125 throw new IllegalStateException(
"Strategy "+strategy+
" is already defined for subpopulation "+subpopulation );
127 weights.strategies.add(strategy);
128 weights.weights.add(weight);
129 weights.totalWeights += weight;
140 final boolean removeStrategy(
142 final String subpopulation)
145 int idx = weights.strategies.indexOf(strategy);
147 weights.strategies.remove(idx);
148 double weight = weights.weights.remove(idx);
149 weights.totalWeights -= weight;
158 StrategyWeights<PL, AG> weights = weightsPerSubpopulation.get(subpop);
160 if ( weights == null ) {
161 weights =
new StrategyWeights<>();
162 weightsPerSubpopulation.put(subpop, weights);
174 final boolean changeWeightOfStrategy(
176 final String subpopulation,
177 final double newWeight) {
179 int idx = weights.strategies.indexOf(strategy);
181 double oldWeight = weights.weights.set(idx, newWeight);
182 weights.totalWeights += (newWeight - oldWeight);
183 LogManager.getLogger(this.getClass()).info( strategy.toString() +
": oldWeight=" + oldWeight +
" newWeight=" + newWeight );
196 @Override
public final void run(
200 handleChangeRequests(iteration);
201 run(persons, replanningContext );
217 strategy.
init(replanningContext);
224 if ((this.maxPlansPerAgent > 0) && (person.getPlans().size() > this.
maxPlansPerAgent)) {
236 if (strategy==null) {
237 throw new RuntimeException(
"No strategy found! Have you defined at least one replanning strategy per subpopulation? Current subpopulation = " + subpopName);
241 strategy.
run(person);
253 Collection<GenericPlanStrategy<PL, AG>> strategies =
new LinkedHashSet<>();
254 for (StrategyWeights<PL, AG> weights : weightsPerSubpopulation.values()) {
255 strategies.addAll(weights.strategies);
261 while (person.
getPlans().size() > maxNumberOfPlans) {
262 PL plan = this.removalPlanSelector.
selectPlan(person);
266 if ( newPlanToSelect == null ) {
267 throw new IllegalStateException(
"could not find a plan to select for person "+person );
278 final void handleChangeRequests(
final int iteration) {
279 for (
int ii = 0 ; ii <= iteration ; ii++ ) {
281 for ( Map.Entry<String, StrategyWeights<PL, AG>> wentry : weightsPerSubpopulation.entrySet() ) {
282 final String subpop = wentry.getKey();
283 final StrategyWeights<PL, AG> weights = wentry.getValue();
284 Map<GenericPlanStrategy<PL, AG>, Double> changes = weights.changeRequests.remove(ii);
285 if (changes != null) {
287 changeWeightOfStrategy( entry.getKey(), subpop, entry.getValue());
322 final String subpopulation,
323 final double newWeight ) {
325 Integer iter = iteration;
326 Map<GenericPlanStrategy<PL, AG>, Double> iterationRequests = weights.changeRequests.get(iter);
327 if (iterationRequests == null) {
328 iterationRequests =
new HashMap<>(3);
329 weights.changeRequests.put(iter, iterationRequests);
331 iterationRequests.put(strategy, newWeight);
332 LogManager.getLogger(this.getClass()).info(
"added change request: " 333 +
" iteration=" + iter +
" newWeight=" + newWeight +
" strategy=" + strategy.toString() );
364 LogManager.getLogger(this.getClass()).info(
"setting PlanSelectorForRemoval to " + planSelector.getClass() ) ;
365 this.removalPlanSelector = planSelector;
368 final int getMaxPlansPerAgent() {
372 @Override
public final List<GenericPlanStrategy<PL, AG>>
getStrategies( String subpopulation ) {
376 @Override
public final List<Double>
getWeights( String subpopulation ) {
StrategyWeights< PL, AG > getStrategyWeights(final String subpop)
abstract boolean removePlan(T p)
final void addChangeRequest(final int iteration, final GenericPlanStrategy< PL, AG > strategy, final String subpopulation, final double newWeight)
void removePlans(final HasPlansAndId< PL, AG > person, final int maxNumberOfPlans)
T selectPlan(HasPlansAndId< T, I > member)
GenericPlanStrategy< T, I > chooseStrategy(HasPlansAndId< T, I > person, final String subpopulation, ReplanningContext replanningContext, Weights< T, I > weights)
void run(final HasPlansAndId< T, I > person)
GenericStrategyManagerImpl()
Collection< GenericPlanStrategy< PL, AG > > distinctStrategies()
final List< GenericPlanStrategy< PL, AG > > getStrategies(String subpopulation)
GenericStrategyManagerImpl(StrategyChooser< PL, AG > strategyChooser)
static String getSubpopulation(HasPlansAndId<?, ?> person)
final StrategyChooser< PL, AG > strategyChooser
final void setMaxPlansPerAgent(final int maxPlansPerAgent)
void init(ReplanningContext replanningContext)
final void run(final Iterable<? extends HasPlansAndId< PL, AG >> persons, final int iteration, final ReplanningContext replanningContext)
final void addStrategy(final GenericPlanStrategy< PL, AG > strategy, final String subpopulation, final double weight)
abstract void setSelectedPlan(T selectedPlan)
final List< Double > getWeights(String subpopulation)
final Map< String, StrategyWeights< PL, AG > > weightsPerSubpopulation
PlanSelector< PL, AG > removalPlanSelector
default void beforeReplanning(ReplanningContext replanningContext)
final void setPlanSelectorForRemoval(final PlanSelector< PL, AG > planSelector)
abstract T getSelectedPlan()
abstract List<? extends T > getPlans()