MATSIM
StrategyManager.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * StrategyManager.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007, 2009 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.replanning;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
34 
35 import jakarta.inject.Inject;
36 import jakarta.inject.Singleton;
37 import java.util.List;
38 import java.util.Map;
39 
46 @Singleton
47 public class StrategyManager implements MatsimManager {
48  // I understand the way in which this is integrated into the framwork as follows:
49  // * The PlansReplanning interface is the one that is bound as ReplanningListener, and it calls StrategyManager.
50  // * On the other hand, StrategyManager is also independently bound.
51  // * This has the consequence that users (possibly via the config) may configure StrategyManager, but it is effectively ignored if the bound
52  // implementation of ReplanningListener does not use it.
53  // * This is the design that I found. Not sure if I should change it, but I need something for Carrier and later for LSP.
54  // * A possible way out would be to unbind StrategyManager (for someone who implements a ReplanningListener that does not use
55  // StrategyManager). This is not immediately possible, but the internet gives possibilities. Could be added as a utility method, which would
56  // (I think) make sense together with the MATSim overall design which overrides defaults instead of asking the user to plug everything together.
57 
58  // Not sure if that would work: There is other code that tries to obtain StrategyManager via injection, and all of this would then fail.
59  // Not sure if one could unbind all of these.
60 
61  // kai, jun'22
62 
63  private static final Logger log = LogManager.getLogger(StrategyManager.class);
64 
66 
67  @Inject
68  StrategyManager(ReplanningConfigGroup replanningConfigGroup,
69  ControllerConfigGroup controllerConfigGroup, StrategyChooser<Plan, Person> strategyChooser,
70  Map<ReplanningConfigGroup.StrategySettings, PlanStrategy> planStrategies ) {
71 
72  this(strategyChooser);
73  setMaxPlansPerAgent(replanningConfigGroup.getMaxAgentPlanMemorySize());
74 
75  int globalInnovationDisableAfter = (int) ((controllerConfigGroup.getLastIteration() - controllerConfigGroup.getFirstIteration())
76  * replanningConfigGroup.getFractionOfIterationsToDisableInnovation() + controllerConfigGroup.getFirstIteration());
77  log.info("global innovation switch off after iteration: " + globalInnovationDisableAfter);
78 
79  for (Map.Entry<ReplanningConfigGroup.StrategySettings, PlanStrategy> entry : planStrategies.entrySet()) {
80  PlanStrategy strategy = entry.getValue();
81  ReplanningConfigGroup.StrategySettings settings = entry.getKey();
82  addStrategy(strategy, settings.getSubpopulation(), settings.getWeight());
83 
84  // now check if this modules should be disabled after some iterations
85  int maxIter = settings.getDisableAfter();
86  if ( maxIter > globalInnovationDisableAfter || maxIter==-1 ) {
87  if (!ReplanningUtils.isOnlySelector(strategy)) {
88  maxIter = globalInnovationDisableAfter ;
89  }
90  }
91 
92  if (maxIter >= 0) {
93  if (maxIter >= controllerConfigGroup.getFirstIteration()) {
94  addChangeRequest(maxIter + 1, strategy, settings.getSubpopulation(), 0.0);
95  } else {
96  /* The services starts at a later iteration than this change request is scheduled for.
97  * make the change right now. */
98  changeWeightOfStrategy(strategy, settings.getSubpopulation(), 0.0);
99  }
100  }
101  }
102  }
103 
104  public StrategyManager() {
105  this.delegate = new GenericStrategyManagerImpl<>();
106  }
107 
109  this.delegate = new GenericStrategyManagerImpl<>(strategyChooser);
110  }
111 
118  public final void addStrategy( final PlanStrategy strategy, final String subpopulation, final double weight) {
119  delegate.addStrategy(strategy, subpopulation, weight);
120  }
121 
130  public final boolean removeStrategy( final PlanStrategy strategy, final String subpopulation) {
131  return delegate.removeStrategy(strategy, subpopulation) ;
132  }
133 
140  public final boolean changeWeightOfStrategy( final GenericPlanStrategy<Plan, Person> strategy, final String subpopulation, final double newWeight) {
141  return delegate.changeWeightOfStrategy(strategy, subpopulation, newWeight) ;
142  }
143 
151  public final void run(final Population population, final int iteration, final ReplanningContext replanningContext) {
152  delegate.run( population.getPersons().values(), iteration, replanningContext );
153  }
154 
161  public GenericPlanStrategy<Plan, Person> chooseStrategy(final Person person, final String subpopulation, ReplanningContext replanningContext) {
162  return delegate.chooseStrategy(person, subpopulation, replanningContext );
163  }
164 
171  public final void setMaxPlansPerAgent(final int maxPlansPerAgent) {
172  delegate.setMaxPlansPerAgent(maxPlansPerAgent);
173  }
174 
175  public final int getMaxPlansPerAgent() {
176  return delegate.getMaxPlansPerAgent();
177  }
178 
184  public final void addChangeRequest(
185  final int iteration,
186  final PlanStrategy strategy,
187  final String subpopulation,
188  final double newWeight) {
189  delegate.addChangeRequest(iteration, strategy, subpopulation, newWeight);
190  }
191 
218  @Inject
219  public final void setPlanSelectorForRemoval(final PlanSelector<Plan, Person> planSelector) {
220  delegate.setPlanSelectorForRemoval(planSelector);
221  }
222 
223  public final List<GenericPlanStrategy<Plan, Person>> getStrategies(final String subpopulation) {
224  return delegate.getStrategies(subpopulation) ;
225  }
226 
230  public final List<Double> getWeights(final String subpopulation) {
231  return delegate.getWeights(subpopulation);
232  }
233 }
final void setMaxPlansPerAgent(final int maxPlansPerAgent)
final void run(final Population population, final int iteration, final ReplanningContext replanningContext)
final List< GenericPlanStrategy< Plan, Person > > getStrategies(final String subpopulation)
static< P extends BasicPlan, R > boolean isOnlySelector(GenericPlanStrategy< P, R > planStrategy)
Map< Id< Person >,? extends Person > getPersons()
final void addChangeRequest(final int iteration, final GenericPlanStrategy< PL, AG > strategy, final String subpopulation, final double newWeight)
final void addChangeRequest(final int iteration, final PlanStrategy strategy, final String subpopulation, final double newWeight)
final List< Double > getWeights(final String subpopulation)
final List< GenericPlanStrategy< PL, AG > > getStrategies(String subpopulation)
final boolean removeStrategy(final PlanStrategy strategy, final String subpopulation)
GenericPlanStrategy< Plan, Person > chooseStrategy(final Person person, final String subpopulation, ReplanningContext replanningContext)
final void setPlanSelectorForRemoval(final PlanSelector< Plan, Person > planSelector)
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)
final GenericStrategyManagerImpl< Plan, Person > delegate
final boolean changeWeightOfStrategy(final GenericPlanStrategy< Plan, Person > strategy, final String subpopulation, final double newWeight)
final void setPlanSelectorForRemoval(final PlanSelector< PL, AG > planSelector)
final void addStrategy(final PlanStrategy strategy, final String subpopulation, final double weight)