MATSIM
AbstractPlanSelector.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.* *
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2008 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 package org.matsim.core.replanning.selectors;
20 
21 import java.util.List;
22 import java.util.Map;
23 
28 
35 public abstract class AbstractPlanSelector implements PlanSelector<Plan, Person> {
36 
37  @Override
39  // First check if there are any unscored plans
40  Plan selectedPlan = new RandomUnscoredPlanSelector<Plan, Person>().selectPlan(person);
41  if (selectedPlan != null) return selectedPlan;
42  // Okay, no unscored plans...
43 
44  // Build the weights of all plans
45 
46  // - now calculate the weights
47  Map<Plan,Double> wc = calcWeights(person.getPlans() );
48  double sumWeights = 0. ;
49  for ( Double score : wc.values() ) {
50  sumWeights += score ;
51  }
52 
53  // choose a random number over interval [0,sumWeights[
54  double selnum = sumWeights*MatsimRandom.getRandom().nextDouble();
55  for (Plan plan : person.getPlans()) {
56  selnum -= wc.get(plan);
57  if (selnum <= 0.0) {
58  return plan;
59  }
60  }
61 
62  // this case should never happen, except a person has no plans at all.
63  return null;
64 
65  }
66 
67  abstract protected Map<Plan,Double> calcWeights( List<? extends Plan> plans ) ;
68 
69 }
abstract Map< Plan, Double > calcWeights(List<? extends Plan > plans)
final Plan selectPlan(HasPlansAndId< Plan, Person > person)
abstract List<? extends T > getPlans()