@Override public void run(HasPlansAndId<Plan,Person> person) { // leave removing of plans to the plans remover. Thsi was initially so that the master always only had one plan in memory // however, this doesn't have to be the case for all scenarios. // person.removePlan(person.getSelectedPlan()); Plan plan = plans.get(person.getId().toString()); person.addPlan(plan); person.setSelectedPlan(plan); }
private void removePlans(final HasPlansAndId<PL, AG> person, final int maxNumberOfPlans) { while (person.getPlans().size() > maxNumberOfPlans) { PL plan = this.removalPlanSelector.selectPlan(person); person.removePlan(plan); if (plan == person.getSelectedPlan()) { final PL newPlanToSelect = new RandomPlanSelector<PL, AG>().selectPlan(person) ; if ( newPlanToSelect == null ) { throw new IllegalStateException( "could not find a plan to select for person "+person ); } person.setSelectedPlan( newPlanToSelect ); } } }
/** * returns the already selected plan for this person * * One must be careful when using it, since the last executed plan might not be selected * since it might have been removed at the start of the new iteration * (because of the maxnumberofplans limit). balac aug '17 */ @Override public T selectPlan(HasPlansAndId<T, I> person) { return person.getSelectedPlan(); }
/** * Choose a random plan from the person and return it. * @return The newly selected plan for this person; <code>null</code> if the person has no plans. */ @Override public T selectPlan(final HasPlansAndId<T, I> person) { // this used to use person.getRandomPlan(), but I inlined the function here in order to get rid of the function of the data class. // kai, nov'13 if (person.getPlans().size() == 0) { return null; } int index = (int)(MatsimRandom.getRandom().nextDouble()*person.getPlans().size()); // yyyy As far as I can tell, this produces race conditions when running multi-threaded. I.e. when running the same // setup twice, this function may return different results per thread or per person. kai, jun'14 return person.getPlans().get(index); } }
@Override public Plan selectPlan(final HasPlansAndId<Plan, Person> person) { final Plan currentPlan = person.getSelectedPlan(); if (person.getPlans().size() <= 1 || currentPlan.getScore() == null) { return currentPlan;
@Override public void run(HasPlansAndId<Plan, Person> person) { counter.incCounter(); Assert.assertNull( "unexpected subpopulation", population.getPersonAttributes().getAttribute( person.getId().toString(), SUBPOP_ATT_NAME) ); }
person.setSelectedPlan(plan); plan = person.createCopyOfSelectedPlanAndMakeSelected();
@Override public void run(HasPlansAndId<Plan, Person> person) { delegate.run(person); slave.addPlansForPsim(person.getSelectedPlan()); //TODO: genometracking }
/** * selects the plan with the highest score from the person */ @Override public T selectPlan(final HasPlansAndId<T, I> person) { double maxScore = Double.NEGATIVE_INFINITY; T bestPlan = null; for (T plan : person.getPlans()) { Double score = plan.getScore(); if ((score != null) && (score.doubleValue() > maxScore) && !score.isNaN() ) { maxScore = plan.getScore().doubleValue(); bestPlan = plan; } } if (bestPlan == null && person.getPlans().size() > 0) { // it seems none of the plans has a real score... so just return the first one (if there is one) return person.getPlans().get(0); } return bestPlan; }
@Override public void run(HasPlansAndId<Plan, Person> person) { counter.incCounter(); Assert.assertEquals( "unexpected subpopulation", POP_NAME_1, population.getPersonAttributes().getAttribute( person.getId().toString(), SUBPOP_ATT_NAME) ); }
@Override public Plan selectPlan(HasPlansAndId<Plan, Person> person) { if (delegate == null) { delegate = (PlanSelector) ((GenericPlanStrategyImpl) controler.getInjector().getBinding(Key.get(PlanStrategy.class, Names.named(delegateName))).getProvider().get()).getPlanSelector(); } if (MatsimRandom.getLocalInstance().nextDouble() <= this.selectionFrequency) { Plan plan = (Plan) delegate.selectPlan(person); if (slave != null) slave.addPlansForPsim(plan); return plan; } else return person.getSelectedPlan(); }
/** * @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 public void run(HasPlansAndId<Plan, Person> person) { counter.incCounter(); Assert.assertEquals( "unexpected subpopulation", POP_NAME_2, population.getPersonAttributes().getAttribute( person.getId().toString(), SUBPOP_ATT_NAME) ); }
public T selectPlan(final HasPlansAndId<T, I> person) { T currentPlan = person.getSelectedPlan(); T otherPlan = new RandomPlanSelector<T, I>().selectPlan(person);
/** * Choose a random plan from the person and return it. * @return The newly selected plan for this person; <code>null</code> if the person has no plans. */ @Override public T selectPlan(final HasPlansAndId<T, I> person) { // following code copied from PersonImpl and then made runnable int cntUnscored = 0; for (T plan : person.getPlans()) { if (plan.getScore() == null) { cntUnscored++; } } if (cntUnscored > 0) { // select one of the unscored plans int idxUnscored = MatsimRandom.getRandom().nextInt(cntUnscored); cntUnscored = 0; for (T plan : person.getPlans()) { if (plan.getScore() == null) { if (cntUnscored == idxUnscored) { return plan; } cntUnscored++; } } } return null; } }
@Override public final Plan selectPlan(HasPlansAndId<Plan, Person> person) { // First check if there are any unscored plans Plan selectedPlan = new RandomUnscoredPlanSelector<Plan, Person>().selectPlan(person); if (selectedPlan != null) return selectedPlan; // Okay, no unscored plans... // Build the weights of all plans // - now calculate the weights Map<Plan,Double> wc = calcWeights(person.getPlans() ); double sumWeights = 0. ; for ( Double score : wc.values() ) { sumWeights += score ; } // choose a random number over interval [0,sumWeights[ double selnum = sumWeights*MatsimRandom.getRandom().nextDouble(); for (Plan plan : person.getPlans()) { selnum -= wc.get(plan); if (selnum <= 0.0) { return plan; } } // this case should never happen, except a person has no plans at all. return null; }
/** * 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; }
@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) ; }