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