1 package org.opentrafficsim.core.math;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import org.djutils.exceptions.Throw;
8
9 import nl.tudelft.simulation.jstats.streams.StreamInterface;
10
11 /**
12 * Utility to draw from a collection.
13 * <p>
14 * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
16 * <p>
17 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 23 aug. 2018 <br>
18 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
21 */
22 public final class Draw
23 {
24
25 /** Constructor. */
26 private Draw()
27 {
28 // no instance
29 }
30
31 /**
32 * Returns a randomly drawn element using draw weights.
33 * @param map Map<E, ? extends Double>; map of elements and respective weights
34 * @param stream StreamInterface; random number stream
35 * @param <E> element type
36 * @return E; randomly drawn element
37 */
38 public static <E> E drawWeighted(final Map<E, ? extends Double> map, final StreamInterface stream)
39 {
40 Throw.whenNull(map, "Map may not be null.");
41 Throw.whenNull(stream, "Stream may not be null.");
42 Throw.when(map.isEmpty(), IllegalArgumentException.class, "Map may not be empty.");
43 double sumProb = 0.0;
44 for (E e : map.keySet())
45 {
46 double w = map.get(e);
47 Throw.when(w < 0.0, IllegalArgumentException.class, "Probabilities should be at least 0.0.");
48 sumProb += w;
49 }
50 Throw.when(sumProb == 0.0, IllegalArgumentException.class, "Probabilities should not add to 0.0.");
51
52 if (map.size() == 1)
53 {
54 return map.keySet().iterator().next();
55 }
56 double r = stream.nextDouble() * sumProb;
57 sumProb = 0.0;
58 E last = null;
59 for (E e : map.keySet())
60 {
61 double f = map.get(e);
62 if (sumProb <= r && r <= sumProb + f)
63 {
64 return e;
65 }
66 sumProb += f;
67 last = e;
68 }
69 return last; // rounding error
70 }
71
72 /**
73 * Returns a randomly drawn element using uniform weights.
74 * @param collection Collection<E>; collection of elements
75 * @param stream StreamInterface; random number stream
76 * @param <E> element type
77 * @return E; randomly drawn element
78 */
79 public static <E> E draw(final Collection<E> collection, final StreamInterface stream)
80 {
81 Throw.whenNull(collection, "Collection may not be null.");
82 Throw.whenNull(stream, "Stream may not be null.");
83 Throw.when(collection.isEmpty(), IllegalArgumentException.class, "Collection may not be empty.");
84 int n = (int) (collection.size() * stream.nextDouble());
85 Iterator<E> it = collection.iterator();
86 int m = 0;
87 while (m < n)
88 {
89 m++;
90 it.next();
91 }
92 return it.next();
93 }
94
95 }