MATSIM
WorstPlanForRemovalSelectorWithConflicts.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.conflicts;
22 
23 import java.util.Collections;
24 import java.util.LinkedList;
25 
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.jboss.logging.Logger;
32 
33 import com.google.inject.Inject;
34 
45 public class WorstPlanForRemovalSelectorWithConflicts implements PlanSelector<Plan, Person> {
46  public static final String SELECTOR_NAME = "WorstPlanForRemovalSelectorWithConflicts";
47 
48  private final Logger logger = Logger.getLogger(WorstPlanForRemovalSelectorWithConflicts.class);
49 
51 
52  @Inject
54  this.conflictManager = conflictManager;
55  }
56 
57  @Override
59  LinkedList<Pair<Plan, Double>> sorter = new LinkedList<>();
60  int nonConflictingCount = 0;
61 
62  for (Plan plan : person.getPlans()) {
63  double score = plan.getScore() == null ? Double.NEGATIVE_INFINITY : plan.getScore();
64  sorter.add(Pair.of(plan, score));
65 
66  if (!conflictManager.isPotentiallyConflicting(plan)) {
67  nonConflictingCount++;
68  }
69  }
70 
71  if (nonConflictingCount == 0) {
72  logger.error(String.format("No non-conflicting plan found for agent %s", person.getId()));
73  }
74 
75  Collections.sort(sorter, (a, b) -> Double.compare(a.getRight(), b.getRight()));
76 
77  if (nonConflictingCount == 1 && !conflictManager.isPotentiallyConflicting(sorter.getFirst().getLeft())) {
78  // Remove the first from the removable candidates if it is the only
79  // non-conflicting one
80  sorter.removeFirst();
81  }
82 
83  if (sorter.size() > 0) {
84  return sorter.getFirst().getLeft();
85  }
86 
87  return null;
88  }
89 
90 }
abstract List<? extends T > getPlans()