RepastHPC  2.3.1
WeightedRandomSelector.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * WeightedRandomSelector.h
36  *
37  * Created on: 12 June 2015
38  * Author: murphy
39  */
40 
41 #ifndef WEIGHTEDRANDOMSELECTOR_H_
42 #define WEIGHTEDRANDOMSELECTOR_H_
43 
44 #include <map>
45 
46 #include "Random.h"
47 
48 namespace repast {
49 
50 template<typename T>
52 
53 private:
54 
55  static bool dblComp (double lhs, double rhs) { return lhs > rhs; } // Note: we want these sorted in reverse order
56 
57  std::multimap<double, T*, bool(*)(double, double)>* scoresAndObjects;
58  std::set<T*>* contentSet;
59  double total;
60 
61 public:
63 
64  virtual ~WeightedRandomSelector();
65 
66  void set(T* instance, double score);
67 
68  double remove(T* instance);
69 
70  bool contains(T* instance);
71 
72  T* getRandomInstance();
73 
74  void clear();
75 
76  size_t size();
77 
78  void report();
79 
80 };
81 
82 template<typename T>
84  scoresAndObjects = new std::multimap<double, T*, bool(*)(double, double)>(dblComp);
85  contentSet = new std::set<T*>();
86  total = 0;
87 };
88 
89 template<typename T>
90 WeightedRandomSelector<T>::~WeightedRandomSelector(){
91  delete scoresAndObjects;
92  delete contentSet;
93 }
94 
95 template<typename T>
96 void WeightedRandomSelector<T>::set(T* instance, double score){
97  remove(instance);
98  if(score <= 0) return; // Setting to zero removes from set
99  scoresAndObjects->emplace(score, instance);
100  contentSet->emplace(instance);
101  total += score;
102 }
103 
104 template<typename T>
105 double WeightedRandomSelector<T>::remove(T* instance){
106  if(contentSet->find(instance) != contentSet->end()){
107  contentSet->erase(instance);
108  for(typename std::multimap<double, T*>::iterator iter = scoresAndObjects->begin(); iter != scoresAndObjects->end(); iter++){
109  if(iter->second == instance){
110  double score = iter->first;
111  total -= score;
112  scoresAndObjects->erase(iter);
113  return score;
114  }
115  }
116  }
117  return 0;
118 }
119 
120 template<typename T>
121 bool WeightedRandomSelector<T>::contains(T* instance){
122  return contentSet->find() != contentSet->end();
123 }
124 
125 template<typename T>
126 T* WeightedRandomSelector<T>::getRandomInstance(){
127  if(scoresAndObjects->size() == 0) return 0;
128  double val = Random::instance()->nextDouble() * total;
129  double sum = 0;
130  for(typename std::multimap<double, T*>::iterator iter = scoresAndObjects->begin(); iter != scoresAndObjects->end(); iter++){
131  sum += iter->first;
132  if(sum > val) return iter->second;
133  }
134  // Can't happen, but if it did, return the last entry
135  typename std::multimap<double, T*>::iterator iter = scoresAndObjects->end();
136  iter--;
137  return iter->second;
138 }
139 
140 template<typename T>
141 void WeightedRandomSelector<T>::clear(){
142  scoresAndObjects->clear();
143  contentSet->clear();
144  total = 0;
145 }
146 
147 template<typename T>
148 size_t WeightedRandomSelector<T>::size(){
149  return contentSet->size();
150 }
151 
152 template<typename T>
153 void WeightedRandomSelector<T>::report(){
154  for(typename std::multimap<double, T*>::iterator iter = scoresAndObjects->begin(); iter != scoresAndObjects->end(); iter++){
155  std::cout << " " << *(iter->second) << " == " << iter->first << std::endl;
156  }
157 }
158 
159 
160 }
161 
162 #endif /* AGENTID_H_ */
repast::Random::nextDouble
double nextDouble()
Gets the current seed.
Definition: Random.cpp:96
repast::Random::instance
static Random * instance()
Gets the singleton instance of this Random.
Definition: Random.cpp:80
repast::WeightedRandomSelector
Definition: WeightedRandomSelector.h:51