MATSIM
WeightedStrategyChooser.java
Go to the documentation of this file.
1 package org.matsim.core.replanning.choosers;
2 
8 
12 public class WeightedStrategyChooser<PL extends BasicPlan, AG extends HasPlansAndId<? extends BasicPlan, AG>> implements StrategyChooser<PL, AG> {
13 
14  @Override
15  public GenericPlanStrategy<PL, AG> chooseStrategy(HasPlansAndId<PL, AG> person, String subpopulation, ReplanningContext replanningContext, StrategyChooser.Weights<PL, AG> weights) {
16  double rnd = MatsimRandom.getRandom().nextDouble() * weights.getTotalWeights();
17 
18  double sum = 0.0;
19  for (int i = 0, max = weights.size(); i < max; i++) {
20  sum += weights.getWeight(i);
21  if (rnd <= sum) {
22  return weights.getStrategy(i);
23  }
24  }
25  return null;
26  }
27 
28 }
GenericPlanStrategy< PL, AG > chooseStrategy(HasPlansAndId< PL, AG > person, String subpopulation, ReplanningContext replanningContext, StrategyChooser.Weights< PL, AG > weights)