1 package org.matsim.core.replanning.choosers;
3 import com.google.inject.Binder;
4 import com.google.inject.Inject;
5 import com.google.inject.Singleton;
6 import com.google.inject.TypeLiteral;
7 import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
8 import it.unimi.dsi.fastutil.ints.IntSet;
9 import it.unimi.dsi.fastutil.objects.Object2IntMap;
10 import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
20 import java.util.HashMap;
33 private final Object2IntMap<String>
total =
new Object2IntOpenHashMap<>();
38 private final Object2IntMap<String>
seen =
new Object2IntOpenHashMap<>();
43 private final Object2IntMap<String>
innovated =
new Object2IntOpenHashMap<>();
48 private final Object2IntMap<String>
carryOver =
new Object2IntOpenHashMap<>();
53 private final Map<String, IntSet>
oneAhead =
new HashMap<>();
58 private final Map<String, IntSet>
twoAhead =
new HashMap<>();
65 total.mergeInt(key != null ? key :
"__none__", 1, Integer::sum);
68 for (String s : total.keySet()) {
69 oneAhead.put(s,
new IntOpenHashSet());
70 twoAhead.put(s,
new IntOpenHashSet());
80 public static void bind(Binder binder) {
83 }).in(Singleton.class);
109 double[] wInno =
new double[weights.size()];
110 double totalInno = 0;
112 double[] wSel =
new double[weights.size()];
115 for (
int i = 0; i < weights.size(); i++) {
117 wSel[i] = weights.getWeight(i);
120 wInno[i] = weights.getWeight(i);
121 totalInno += wInno[i];
128 if (subpopulation == null) {
129 subpopulation =
"__none__";
132 IntSet oneAhead = this.oneAhead.get(subpopulation);
133 IntSet twoAhead = this.twoAhead.get(subpopulation);
135 int id = person.
getId().index();
138 double expected = (seen.getInt(subpopulation) * totalInno) / (totalInno + totalSel);
139 double diff = expected - innovated.getInt(subpopulation);
141 seen.mergeInt(subpopulation, 1, Integer::sum);
143 if (rnd < totalInno) {
147 if (oneAhead.contains(
id)) {
148 carryOver.mergeInt(subpopulation, 1, Integer::sum);
153 innovated.mergeInt(subpopulation, 1, Integer::sum);
161 if (carryOver.getInt(subpopulation) > 0 && !oneAhead.contains(
id)) {
162 carryOver.mergeInt(subpopulation, -1, Integer::sum);
165 innovated.mergeInt(subpopulation, 1, Integer::sum);
172 if (carryOver.getInt(subpopulation) > 20 && diff > 50 && !twoAhead.contains(
id)) {
173 carryOver.mergeInt(subpopulation, -1, Integer::sum);
176 innovated.mergeInt(subpopulation, 1, Integer::sum);
185 IntSet oneAhead = this.oneAhead.get(subpopulation);
187 if (oneAhead.size() == total.getInt(subpopulation)) {
188 IntSet twoAhead = this.twoAhead.get(subpopulation);
191 oneAhead.addAll(twoAhead);
199 for (
int i = 0, max = w.length; i < max; i++) {
202 return weights.getStrategy(i);
AllowsConfiguration addOverridingModule(AbstractModule abstractModule)
static Random getRandom()
GenericPlanStrategy< PL, AG > chooseStrategy(HasPlansAndId< PL, AG > person, String subpopulation, ReplanningContext replanningContext, StrategyChooser.Weights< PL, AG > weights)
static void install(Controller controller)
final Object2IntMap< String > carryOver
final Map< String, IntSet > twoAhead
BalancedInnovationStrategyChooser(Population population)
static< P extends BasicPlan, R > boolean isOnlySelector(GenericPlanStrategy< P, R > planStrategy)
Map< Id< Person >,? extends Person > getPersons()
static String getSubpopulation(HasPlansAndId<?, ?> person)
void beforeReplanning(ReplanningContext replanningContext)
final Object2IntMap< String > total
GenericPlanStrategy< PL, AG > chooseStrategy(double total, double[] w, StrategyChooser.Weights< PL, AG > weights)
final Object2IntMap< String > innovated
final Map< String, IntSet > oneAhead
void advanceStep(String subpopulation)
static void bind(Binder binder)
final Object2IntMap< String > seen