TopologyGenerator.cpp
Go to the documentation of this file.
1 
18 #include "TopologyGenerator.hpp"
19 #include "../utility/LogHelper.hpp"
20 #include <random>
21 
22 namespace ISM {
23 
24  TopologyGenerator::TopologyGenerator(const std::map<std::string, ISM::ObjectRelations> allObjectRelationsPerPattern,
25  int maxNeighbourCount)
26  : mAllObjectRelationsPerPattern(allObjectRelationsPerPattern)
27  , mMaxNeighbourCount(maxNeighbourCount)
28  {
29  std::map<std::string, unsigned int> numObjectsPerPattern;
30 
31  for (std::map<std::string, ISM::ObjectRelations>::iterator it = mAllObjectRelationsPerPattern.begin();
33  ++it)
34  {
35  unsigned int numRelations = it->second.size();
36  unsigned int numObjects = ((std::sqrt(8 * numRelations + 1) - 1) / 2) + 1;
37 
38  mNumRelationsPerPattern[it->first] = numRelations;
39  numObjectsPerPattern[it->first] = numObjects;
40  }
41 
43  }
44 
45  std::vector<TopologyPtr> TopologyGenerator::generateNeighbours(const std::string &pattern, TopologyPtr from)
46  {
47  std::vector<std::vector<bool>> neighbours = calculateNeighbours(pattern, convertTopologyToBitvector(from, pattern));
48  std::vector<std::vector<bool>> filteredNeighours = filterBitvectors(neighbours, pattern);
49  std::vector<std::vector<bool>> selectedNeighbours = selectNeighbours(filteredNeighours);
50  std::vector<TopologyPtr> topologies = convertBitvectors(selectedNeighbours, pattern);
51  logNeighbourGeneration(topologies, from, filteredNeighours.size());
52  return topologies;
53  }
54 
55  std::vector<TopologyPtr> TopologyGenerator::generateStarTopologies(const std::string &pattern) {
56  std::vector<std::vector<bool>> starBitVectors;
57 
58  unsigned int numAllRelations = mNumRelationsPerPattern[pattern];
59  unsigned int numObjects = ((std::sqrt(8 * numAllRelations + 1) - 1) / 2) + 1;
60 
61  for (unsigned int i = 0; i < numObjects; ++i)
62  {
63  std::vector<bool> star(numAllRelations, 0);
64  unsigned int pos = 0;
65  for (unsigned int j = 0; j < i; ++j) {
66  star[pos + i - (j + 1)] = 1;
67  pos += numObjects - (j + 1);
68  }
69  for (unsigned int k = pos; k < pos + numObjects - 1 - i; ++k) {
70  star[k] = 1;
71  }
72  starBitVectors.push_back(star);
73  }
74 
75  return convertBitvectors(starBitVectors, pattern);
76  }
77 
79  {
80  unsigned int numRelations = mAllObjectRelationsPerPattern[pattern].size();
81  std::vector<bool> fullyMeshed(numRelations, true);
82 
83  return convertBitvectorToTopology(fullyMeshed, pattern);
84  }
85 
87  {
88  unsigned int numAllRelations = mAllObjectRelationsPerPattern[pattern].size();
89 
90  std::random_device rd;
91  std::mt19937 eng(rd());
92  std::uniform_int_distribution<unsigned int> dist(0, 1);
93 
94  std::vector<bool> relations(numAllRelations, 0);
95  do
96  {
97  for (unsigned int i = 0; i < numAllRelations; i++)
98  {
99  relations[i] = dist(eng);
100  }
101  }
102  while (!mConnectivityChecker->isConnected(relations, pattern));
103 
104  return convertBitvectorToTopology(relations, pattern);
105  }
106 
107 
108  std::vector<std::vector<bool>> TopologyGenerator::selectNeighbours(std::vector<std::vector<bool>>& from)
109  {
110  if (mMaxNeighbourCount < 0 || mMaxNeighbourCount >= (int) from.size())
111  return from;
112 
113  //Sort from by #relations.
114  struct compare {
115  bool operator() (const std::vector<bool> & first, const std::vector<bool> & second) {
116  return first.size() > second.size();
117  }
118  };
119 
120  std::sort(from.begin(), from.end(), compare());
121 
122  std::random_device rd;
123  std::mt19937 eng(rd());
124  std::normal_distribution<double> dist(0, from.size() / 2);
125 
126  std::vector<std::vector<bool>> selectedNeighbours;
127 
128  for (unsigned int i = 0; i < (unsigned int) mMaxNeighbourCount; ++i)
129  {
130  unsigned int randIndex;
131  do {
132  randIndex = std::abs(dist(eng));
133  } while (randIndex >= from.size());
134 
135  selectedNeighbours.push_back(from[randIndex]);
136  from.erase(from.begin() + randIndex);
137 
138  }
139 
140  return selectedNeighbours;
141  }
142 
143  TopologyPtr TopologyGenerator::convertBitvectorToTopology(const std::vector<bool> & bitvector,
144  const std::string& pattern)
145  {
146  ObjectRelations objectRelations;
147 
148  std::ostringstream oss;
149  std::vector<unsigned int> relationIndices;
150 
151  for (unsigned int i = 0; i < bitvector.size(); ++i)
152  {
153  if (bitvector[i])
154  {
155  relationIndices.push_back(i);
156  objectRelations[i] = mAllObjectRelationsPerPattern[pattern][i];
157  }
158  }
159 
160  oss << "[" << relationIndices[0];
161  for (unsigned int i = 1; i < relationIndices.size(); ++i)
162  {
163  oss << ", " << relationIndices[i];
164  }
165  oss << "]";
166 
167  TopologyPtr topology = TopologyPtr(new Topology());
168  topology->objectRelations = objectRelations;
169  topology->identifier = oss.str();
170  return topology;
171  }
172 
174  const std::string& pattern)
175  {
176  unsigned int numAllRelations = mNumRelationsPerPattern[pattern];
177  std::vector<bool> bitvector(numAllRelations, 0);
178  ObjectRelations objectRelations = topology->objectRelations;
179  for (ObjectRelations::iterator it = objectRelations.begin(); it != objectRelations.end(); ++it)
180  {
181  bitvector[it->first] = 1;
182  }
183 
184  return bitvector;
185  }
186 
187 
188  std::vector<std::vector<bool>> TopologyGenerator::filterBitvectors(std::vector<std::vector<bool>> bitvectors,
189  const std::string& pattern)
190  {
191  std::vector<std::vector<bool>> result;
192  for (unsigned int i = 0; i < bitvectors.size(); ++i)
193  {
194  if (mConnectivityChecker->isConnected(bitvectors[i], pattern))
195  {
196  result.push_back(bitvectors[i]);
197  }
198  }
199 
200  return result;
201  }
202 
203  std::vector<TopologyPtr> TopologyGenerator::convertBitvectors(std::vector<std::vector<bool>> bitvectors,
204  const std::string& pattern)
205  {
206  std::vector<TopologyPtr> result;
207  for (unsigned int i = 0; i < bitvectors.size(); ++i)
208  {
209  result.push_back(convertBitvectorToTopology(bitvectors[i], pattern));
210  }
211 
212  return result;
213  }
214 
215  void TopologyGenerator::logNeighbourGeneration(const std::vector<TopologyPtr> & neighbours,
216  TopologyPtr from, const unsigned int totalNumber)
217  {
218  std::ostringstream oss;
219 
220  if (neighbours.size() == 0)
221  {
222  oss << from->objectRelations << "does not have any neighbours"
223  << std::endl << std::endl;
224  } else {
225  oss << "Generated " << totalNumber << " neighbour topologies of topology\n\n" << from->objectRelations
226  << "and selected the following " << neighbours.size() << " topologies of those neighbours:\n\n";
227 
228  for (unsigned int i = 0; i < neighbours.size(); ++i)
229  {
230  oss << "\t- Topology " << neighbours[i]->identifier << std::endl;
231  }
232  }
233 
234  LogHelper::logMessage(oss.str(), LOG_INFO);
236  }
237 
238 }
std::vector< TopologyPtr > generateStarTopologies(const std::string &pattern)
std::vector< TopologyPtr > generateNeighbours(const std::string &pattern, TopologyPtr from)
virtual std::vector< std::vector< bool > > calculateNeighbours(const std::string &pattern, std::vector< bool > from)=0
TopologyPtr convertBitvectorToTopology(const std::vector< bool > &bitvector, const std::string &pattern)
static void logLine(LogLevel logLevel=LOG_INFO)
Definition: LogHelper.cpp:101
std::vector< std::vector< bool > > selectNeighbours(std::vector< std::vector< bool >> &neighbours)
boost::shared_ptr< ConnectivityChecker > ConnectivityCheckerPtr
std::vector< std::vector< bool > > filterBitvectors(std::vector< std::vector< bool >> bitvectors, const std::string &pattern)
std::map< std::string, unsigned int > mNumRelationsPerPattern
boost::shared_ptr< Topology > TopologyPtr
Definition: Topology.hpp:51
std::vector< bool > convertTopologyToBitvector(TopologyPtr topology, const std::string &pattern)
static void logMessage(const std::string &message, LogLevel logLevel=LOG_INFO, const char *logColor=LOG_COLOR_DEFAULT)
Definition: LogHelper.cpp:96
std::vector< TopologyPtr > convertBitvectors(std::vector< std::vector< bool >> bitvectors, const std::string &pattern)
std::map< std::string, ISM::ObjectRelations > mAllObjectRelationsPerPattern
std::map< unsigned int, ISM::ObjectRelationPtr, std::less< unsigned > > ObjectRelations
ConnectivityCheckerPtr mConnectivityChecker
TopologyPtr generateRandomTopology(const std::string &pattern)
TopologyGenerator(const std::map< std::string, ISM::ObjectRelations > allObjectRelationsPerPattern, int maxNeighbourCount)
void logNeighbourGeneration(const std::vector< TopologyPtr > &neighbours, TopologyPtr from, const unsigned int totalNumber)
TopologyPtr generateFullyMeshedTopology(const std::string &pattern)
this namespace contains all generally usable classes.


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:41