/** * Calculates the weight of a single plan. * * @return the weight of the plan */ protected double calcPlanWeight(final T plan, final double maxScore) { // NOTE: The deduction of "maxScore" from all scores is a numerical trick. It ensures that the values of exp(...) // are in some normal range, instead of close to numerical infinity. The latter leads to numerically instable // results (this is not fiction; we had that some time ago). kai, aug'12 if (plan.getScore() == null) { return Double.NaN; } double weight = Math.exp(this.beta * (plan.getScore() - maxScore)); if (weight < MIN_WEIGHT) weight = MIN_WEIGHT; return weight; }
/** * 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; }
/** * 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; }
if ((currentPlan.getScore() == null) || (otherPlan.getScore() == null)) { if ( currentPlan.getScore()!=null && otherPlan.getScore()==null ) { if ( scoreWrnFlag ) { log.error( "yyyyyy not switching to other plan although it needs to be explored. " if ( currentPlan.getScore().isNaN() ) { return otherPlan ; if ( otherPlan.getScore().isNaN() ) { return currentPlan ; double currentScore = currentPlan.getScore().doubleValue(); double otherScore = otherPlan.getScore().doubleValue();
/** * 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; } }