MATSIM
WorstPlanForRemovalSelector.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * WorstPlanSelector.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.replanning.selectors;
22 
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25 
29 
41 public class WorstPlanForRemovalSelector implements PlanSelector<Plan, Person> {
42 
43  private static final String UNDEFINED_TYPE = "undefined";
44 
45  @Override
47 
48  // hashmap that returns "Integer" count for given plans type:
49  Map<String, Integer> typeCounts = new ConcurrentHashMap<String, Integer>();
50 
51  // count how many plans per type an agent has:
52  for (Plan plan : person.getPlans()) {
53  String type = plan.getType();
54  if ( type==null ) {
55  type = UNDEFINED_TYPE ;
56  }
57  typeCounts.merge( type, 1, ( a, b ) -> a + b );
58  }
59 
60  Plan worst = null;
61  double worstScore = Double.POSITIVE_INFINITY;
62  for (Plan plan : person.getPlans()) {
63 
64  String type = plan.getType();
65  if ( type==null ) {
66  type = UNDEFINED_TYPE;
67  }
68  if ( typeCounts.get( type ) > 1) {
69  // (if we have more than one plan of the same type:)
70 
71  // if this plan has no score yet:
72  if (plan.getScore() == null || plan.getScore().isNaN() ) {
73  // say that the plan without score now is the "worst":
74  worst = plan;
75 
76  // make sure that this one remains the selected plan:
77  worstScore = Double.NEGATIVE_INFINITY;
78 
79  // otherwise do the usual logic to find the plan with the minimum score:
80  } else if ( plan.getScore() < worstScore) {
81  worst = plan;
82  worstScore = plan.getScore();
83  }
84  }
85  // (otherwise we just keep "worst=null")
86 
87  }
88 
89  if (worst == null) {
90  // there is exactly one plan, or we have of each plan-type exactly one.
91  // select the one with worst score globally, or the first one with score=null
92  for (Plan plan : person.getPlans()) {
93  if (plan.getScore() == null || plan.getScore().isNaN() ) {
94  return plan;
95  }
96  if ( plan.getScore() < worstScore) {
97  worst = plan;
98  worstScore = plan.getScore();
99  }
100  }
101  }
102  return worst;
103  }
104 
105 }
abstract List<? extends T > getPlans()