@Override public PlanStrategy get() { return new PlanStrategyImpl(new ExpBetaPlanSelector(config)); }
/** * @return the probability that this expBetaPlanSelector will select this plan for this person. */ public static <T extends BasicPlan, I> double getSelectionProbability(ExpBetaPlanSelector<T, I> expBetaPlanSelector, HasPlansAndId<T, ?> person, final T plan) { Map<T, Double> weights = expBetaPlanSelector.calcWeights(person); double thisWeight = weights.get(plan); double sumWeights = 0.0; for (Double weight : weights.values()) { sumWeights += weight; } return (thisWeight / sumWeights); }
@Override public Plan selectPlan(HasPlansAndId<Plan, Person> person) { List<cadyts.demand.Plan<T>> plans = new ArrayList<cadyts.demand.Plan<T>>() ; Vector choiceProbs = new Vector( person.getPlans().size() ) ; int pos = 0 ; for ( Plan plan : person.getPlans() ) { plans.add( cContext.getPlansTranslator().getCadytsPlan(plan) ) ; choiceProbs.add(pos, ExpBetaPlanSelector.getSelectionProbability(new ExpBetaPlanSelector(beta), plan.getPerson(), plan) ); // I guess these are supposed to be the prior probabilities. If so, than the above shoudl be correct (albeit a bit expensive). // I would have expected that these are internally ignored when brute force is switched on, but they are not. // I think that what it does is to use them when plans are equal according to cadyts. kai, dec'13 pos++ ; } int idx = cContext.getCalibrator().selectPlan(plans, choiceProbs) ; // cContext.getCalibrator().addToDemand(cadytsPlan) ; // I _think_ that's done inside calibrator.selectPlan. kai, dec'13 return person.getPlans().get(idx) ; }
plan5.setScore(100.0); ExpBetaPlanSelector<Plan, Person> selector = new ExpBetaPlanSelector<Plan, Person>(this.config.planCalcScore()); int cnt1 = 0; int cnt2 = 0; Plan plan = selector.selectPlan(person); if (plan == plan1) cnt1++; if (plan == plan2) cnt2++;
/** * Builds the weights of all plans. * * @return a map containing the weights of all plans */ Map<T, Double> calcWeights(final HasPlansAndId<T, ?> person) { // - first find the max. score of all plans of this person double maxScore = Double.NEGATIVE_INFINITY; for (T plan1 : person.getPlans()) { if ( (plan1.getScore() != null) && plan1.getScore().isNaN() ) { Logger.getLogger(this.getClass()).error("encountering getScore().isNaN(). This class is not well behaved in this situation. Continuing anyway ...") ; } if ((plan1.getScore() != null) && (plan1.getScore() > maxScore)) { maxScore = plan1.getScore(); } } Map<T, Double> weights = new LinkedHashMap<T, Double>(person.getPlans().size()); for (T plan : person.getPlans()) { weights.put(plan, this.calcPlanWeight(plan, maxScore)); // see note in calcPlanWeight! } return weights; }
public void testGetSelectionProbability() { /* * the expected results were computed with R. The standard output of double precision numbers in R has 7 digits. */ final double EPSILON_R = 1e-7; this.config.planCalcScore().setBrainExpBeta(2.0); Person person = PopulationUtils.getFactory().createPerson(Id.create(1, Person.class)); Plan plan1 = PersonUtils.createAndAddPlan(person, false); plan1.setScore(180.0); Plan plan2 = PersonUtils.createAndAddPlan(person, false); plan2.setScore(180.1); Plan plan3 = PersonUtils.createAndAddPlan(person, false); plan3.setScore(180.5); Plan plan4 = PersonUtils.createAndAddPlan(person, false); plan4.setScore(169.9); ExpBetaPlanSelector testee = new ExpBetaPlanSelector(this.config.planCalcScore()); assertEquals(0.2024421, ExpBetaPlanSelector.getSelectionProbability(testee, person, plan1), EPSILON_R); assertEquals(0.2472634, ExpBetaPlanSelector.getSelectionProbability(testee, person, plan2), EPSILON_R); assertEquals(0.5502947, ExpBetaPlanSelector.getSelectionProbability(testee, person, plan3), EPSILON_R); assertEquals(6.208075e-10, ExpBetaPlanSelector.getSelectionProbability(testee, person, plan4), EPSILON_R); }
ExpBetaPlanSelector<Plan, Person> selector = new ExpBetaPlanSelector<Plan, Person>(this.config.planCalcScore()); int cnt1 = 0; int cnt2 = 0; Plan plan = selector.selectPlan(person); if (plan == plan1) cnt1++; if (plan == plan2) cnt2++;
@Override public ExpBetaPlanSelector<Plan, Person> get() { return new ExpBetaPlanSelector<>( - config.getBrainExpBeta()); } }
/** * @return a random plan from the person, random but according to its weight. */ @Override public T selectPlan(final HasPlansAndId<T, I> person) { // get the weights of all plans Map<T, Double> weights = this.calcWeights(person); double sumWeights = 0.0; for (Double weight : weights.values()) { sumWeights += weight; } // choose a random number over interval [0, sumWeights[ double selnum = sumWeights * MatsimRandom.getRandom().nextDouble(); for (T plan : person.getPlans()) { selnum -= weights.get(plan); if (selnum <= 0.0) { return plan; } } // hmm, no plan returned... either the person has no plans, or the plan(s) have no score. if (person.getPlans().size() > 0) { return person.getPlans().get(0); } // this case should never happen, except a person has no plans at all. return null; }
@Override protected ExpBetaPlanSelector<Plan, Person> getPlanSelector() { return new ExpBetaPlanSelector<Plan, Person>(this.config.planCalcScore()); }
public LocationChoicePlanStrategy(Scenario scenario, Provider<TripRouter> tripRouterProvider) { this.tripRouterProvider = tripRouterProvider; if ( DestinationChoiceConfigGroup.Algotype.bestResponse==((DestinationChoiceConfigGroup)scenario.getConfig().getModule("locationchoice")).getAlgorithm() ) { throw new RuntimeException("best response location choice not supported as part of LocationChoicePlanStrategy. " + "Use BestReplyLocationChoicePlanStrategy instead, but be aware that as of now some Java coding is necessary to do that. kai, feb'13") ; } DestinationChoiceConfigGroup dccg = (DestinationChoiceConfigGroup) scenario.getConfig().getModule(DestinationChoiceConfigGroup.GROUP_NAME); String planSelector = dccg.getPlanSelector(); if (planSelector.equals("BestScore")) { delegate = new PlanStrategyImpl(new BestPlanSelector()); } else if (planSelector.equals("ChangeExpBeta")) { delegate = new PlanStrategyImpl(new ExpBetaPlanChanger(scenario.getConfig().planCalcScore().getBrainExpBeta())); } else if (planSelector.equals("SelectRandom")) { delegate = new PlanStrategyImpl(new RandomPlanSelector()); } else { delegate = new PlanStrategyImpl(new ExpBetaPlanSelector(scenario.getConfig().planCalcScore())); } delegate.addStrategyModule(new DestinationChoice(this.tripRouterProvider, scenario)); delegate.addStrategyModule(new ReRoute(scenario, tripRouterProvider)); }
@Override public void init(ReplanningContext replanningContext) { /* * Somehow this is ugly. Should be initialized in the constructor. But I do not know, how to initialize the lc scenario elements * such that they are already available at the time of constructing this object. ah feb'13 */ DestinationChoiceContext lcContext = (DestinationChoiceContext) scenario.getScenarioElement(DestinationChoiceContext.ELEMENT_NAME); Config config = lcContext.getScenario().getConfig(); DestinationChoiceConfigGroup dccg = (DestinationChoiceConfigGroup) config.getModule(DestinationChoiceConfigGroup.GROUP_NAME); MaxDCScoreWrapper maxDcScoreWrapper = (MaxDCScoreWrapper)scenario.getScenarioElement(MaxDCScoreWrapper.ELEMENT_NAME); if ( !DestinationChoiceConfigGroup.Algotype.bestResponse.equals(dccg.getAlgorithm())) { throw new RuntimeException("wrong class for selected location choice algorithm type; aborting ...") ; } String planSelector = dccg.getPlanSelector(); if (planSelector.equals("BestScore")) { delegate = new PlanStrategyImpl(new BestPlanSelector<Plan, Person>()); } else if (planSelector.equals("ChangeExpBeta")) { delegate = new PlanStrategyImpl(new ExpBetaPlanChanger(config.planCalcScore().getBrainExpBeta())); } else if (planSelector.equals("SelectRandom")) { delegate = new PlanStrategyImpl(new RandomPlanSelector()); } else { delegate = new PlanStrategyImpl(new ExpBetaPlanSelector(config.planCalcScore())); } delegate.addStrategyModule(new TripsToLegsModule(tripRouterProvider, config.global())); delegate.addStrategyModule(new BestReplyDestinationChoice(tripRouterProvider, lcContext, maxDcScoreWrapper.getPersonsMaxDCScoreUnscaled(), scoringFunctionFactory, travelTimes, travelDisutilities)); delegate.addStrategyModule(new ReRoute(lcContext.getScenario(), tripRouterProvider)); delegate.init(replanningContext); }