MATSIM
ProbabilityFilter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ProbabilityFilter.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2013 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.withinday.replanning.identifiers.filter;
22 
23 import java.util.Iterator;
24 import java.util.Random;
25 import java.util.Set;
26 
27 import org.matsim.api.core.v01.Id;
31 
32 public class ProbabilityFilter implements AgentFilter {
33 
34  private final Random random = MatsimRandom.getLocalInstance();
35  private final double replanningProbability;
36 
37  // use the factory
38  /*package*/ ProbabilityFilter(double replanningProbability) {
39  this.replanningProbability = replanningProbability;
40  }
41 
42  @Override
43  public void applyAgentFilter(Set<Id<Person>> set, double time) {
44  Iterator<Id<Person>> iter = set.iterator();
45 
46  while (iter.hasNext()) {
47  Id<Person> id = iter.next();
48 
49  if (!this.applyAgentFilter(id, time)) iter.remove();
50  }
51  }
52 
53  @Override
54  public boolean applyAgentFilter(Id<Person> id, double time) {
55 
56  /*
57  * This ensures that the filter's outcomes do not depend on the order
58  * in which agents are filtered. Otherwise agents stored in unsorted
59  * data structures will not produce deterministic outcomes!
60  */
61  random.setSeed(id.hashCode() + (long) time);
62 
63  /*
64  * Based on a random number it is decided whether an agent should
65  * do a replanning or not.
66  * number > replanningProbability: no replanning
67  */
68  double rand = random.nextDouble();
69  if (rand > replanningProbability) return false;
70  else return true;
71  }
72 }