MATSIM
ForceInnovationStrategyChooser.java
Go to the documentation of this file.
1 package org.matsim.core.replanning.choosers;
2 
9 
16 public class ForceInnovationStrategyChooser<PL extends BasicPlan, AG extends HasPlansAndId<? extends BasicPlan, AG>> implements StrategyChooser<PL, AG> {
17 
18  private final int iter;
19  private final boolean permute;
20 
28  public ForceInnovationStrategyChooser(int iter, Permute permute) {
29  this.iter = iter;
30  this.permute = permute == Permute.yes;
31  }
32 
36  static int hash(final int value) {
37  int x = value;
38 
39  x = ((x >>> 16) ^ x) * 0x119de1f3;
40  x = ((x >>> 16) ^ x) * 0x119de1f3;
41  x = (x >>> 16) ^ x;
42 
43  return x;
44  }
45 
46  @Override
47  public GenericPlanStrategy<PL, AG> chooseStrategy(HasPlansAndId<PL, AG> person, String subpopulation, ReplanningContext replanningContext, Weights<PL, AG> weights) {
48 
49  double[] w = new double[weights.size()];
50  double total = 0;
51 
52  // permutation term so the selection is different every nth iterations
53  int perm = (replanningContext.getIteration() / iter) % 16;
54 
55  for (int i = 0; i < weights.size(); i++) {
56 
57 
58  int term = (permute ? hash(person.getId().index()) >>> perm : person.getId().index()) % iter;
59  int mod = replanningContext.getIteration() % iter;
60 
61  // Set selectors weight to zero, so only innovate strategies remain
62  if ((term == mod || -term == mod) && ReplanningUtils.isOnlySelector(weights.getStrategy(i))) {
63  w[i] = 0;
64  } else
65  w[i] = weights.getWeight(i);
66 
67  total += w[i];
68  }
69 
70  double rnd = MatsimRandom.getRandom().nextDouble() * total;
71 
72  // If all weights are zero the first one is returned
73  double sum = 0.0;
74  for (int i = 0, max = weights.size(); i < max; i++) {
75  sum += w[i];
76  if (rnd <= sum) {
77  return weights.getStrategy(i);
78  }
79  }
80  return null;
81 
82  }
83 
84  public enum Permute {
85  yes,
86  no
87  }
88 
89 }
static< P extends BasicPlan, R > boolean isOnlySelector(GenericPlanStrategy< P, R > planStrategy)
GenericPlanStrategy< PL, AG > chooseStrategy(HasPlansAndId< PL, AG > person, String subpopulation, ReplanningContext replanningContext, Weights< PL, AG > weights)