TopologyCreator.cpp
Go to the documentation of this file.
1 
19 
20 #include <random>
21 
22 namespace SceneModel {
23 
24 TopologyCreator::TopologyCreator(const std::vector<std::string>& pAllObjectTypes, unsigned int pMaxNeighbourCount, bool pRemoveRelations, bool pSwapRelations):
25  mAllObjectTypes(pAllObjectTypes), mMaxNeighbourCount(pMaxNeighbourCount), mRemoveRelations(pRemoveRelations), mSwapRelations(pSwapRelations)
26 {
28 }
29 
31 { }
32 
33 std::vector<boost::shared_ptr<Topology>> TopologyCreator::generateNeighbours(boost::shared_ptr<Topology> pFrom)
34 {
35  std::cout << "-----------------------------------------------------------" << std::endl;
36  std::cout << "Generating neighbours (c indicates connectedness): ";
37 
38  std::vector<bool> bitvector = convertTopologyToBitvector(pFrom);
39  std::vector<std::vector<bool>> neighbours = calculateNeighbours(bitvector);
40  std::vector<std::vector<bool>> selectedNeighbours;
41 
42  for (std::vector<bool> i: neighbours)
43  {
44  for (bool bit: i)
45  {
46  if (bit) std::cout << "1";
47  else std::cout << "0";
48  }
49  if (mConnectivityChecker->isConnected(i))
50  {
51  std::cout << "c";
52  selectedNeighbours.push_back(i);
53  }
54  std::cout << " ";
55  }
56  std::cout << std::endl;
57 
58  if (mMaxNeighbourCount < selectedNeighbours.size())
59  {
60  std::cout << "Found " << selectedNeighbours.size() << " neighbours, maximum is " << mMaxNeighbourCount << ". Selecting random neighbours." << std::endl;
61  selectedNeighbours = selectRandomNeighbours(selectedNeighbours);
62  if (selectedNeighbours.size() != mMaxNeighbourCount)
63  throw std::runtime_error("In TopologyCreator: number of randomly selected neighbours (" + boost::lexical_cast<std::string>(selectedNeighbours.size()) + ") was not equal to maximum.");
64  }
65  std::vector<boost::shared_ptr<Topology>> result;
66  std::cout << "Selected neighbours: ";
67  for (std::vector<bool> i: selectedNeighbours)
68  {
69  for (bool bit: i)
70  {
71  if (bit) std::cout << "1";
72  else std::cout << "0";
73  }
74  std::cout << " ";
75  result.push_back(convertBitvectorToTopology(i));
76  }
77  std::cout << std::endl;
78  std::cout << "-----------------------------------------------------------" << std::endl;
79 
80  return result;
81 }
82 
83 std::vector<boost::shared_ptr<Topology>> TopologyCreator::generateStarTopologies()
84 {
85  std::vector<boost::shared_ptr<Topology>> result;
86  // from lib_ism:
87  std::vector<std::vector<bool>> starBitVectors;
88  unsigned int numObjects = mAllObjectTypes.size();
89  unsigned int numAllRelations = (numObjects - 1) * numObjects / 2;
90 
91  for (unsigned int i = 0; i < numObjects; ++i)
92  {
93  std::vector<bool> star(numAllRelations, 0);
94  unsigned int pos = 0;
95  for (unsigned int j = 0; j < i; ++j) {
96  star[pos + i - (j + 1)] = 1;
97  pos += numObjects - (j + 1);
98  }
99  for (unsigned int k = pos; k < pos + numObjects - 1 - i; ++k) {
100  star[k] = 1;
101  }
102  starBitVectors.push_back(star);
103  }
104 
105  for (std::vector<bool> starBitVector: starBitVectors)
106  result.push_back(convertBitvectorToTopology(starBitVector));
107  return result;
108 }
109 
110 
112 {
113  unsigned int numObjects = mAllObjectTypes.size();
114  std::vector<bool> bitvector((numObjects - 1) * numObjects / 2, true); // contains all possible relations.
116  return result;
117 }
118 
120 {
121  unsigned int numAllObjects = mAllObjectTypes.size();
122  unsigned int numAllRelations = (numAllObjects - 1) * numAllObjects / 2;
123  // from lib_ism:
124  std::random_device rd;
125  std::mt19937 eng(rd());
126  std::uniform_int_distribution<unsigned int> dist(0, 1);
127 
128  std::vector<bool> relations(numAllRelations, 0);
129  do
130  {
131  for (unsigned int i = 0; i < numAllRelations; i++) relations[i] = dist(eng);
132  }
133  while (!mConnectivityChecker->isConnected(relations));
135  return result;
136 }
137 
138 std::vector<boost::shared_ptr<Topology>> TopologyCreator::generateAllConnectedTopologies()
139 {
140  std::vector<boost::shared_ptr<Topology>> result;
142  result.push_back(fullyMeshed);
143  std::cout << "Trying to reach all possible topologies from the fully meshed one." << std::endl;
144  std::vector<boost::shared_ptr<SceneModel::Topology>> neighboursToVisit;
145  neighboursToVisit.push_back(fullyMeshed);
146  std::vector<std::string> seenNeighbourIDs;
147  seenNeighbourIDs.push_back(fullyMeshed->mIdentifier);
148  unsigned int visitedCounter = 0;
149  while(!neighboursToVisit.empty())
150  {
151  boost::shared_ptr<SceneModel::Topology> from = neighboursToVisit[0];
152  neighboursToVisit.erase(neighboursToVisit.begin());
153 
154  std::cout << "Generating neighbours of topology " << from->mIdentifier << ":";
155  std::vector<boost::shared_ptr<SceneModel::Topology>> newNeighbours = generateNeighbours(from);
156  unsigned int uniqueCounter = 0;
157  for (boost::shared_ptr<SceneModel::Topology> newNeighbour: newNeighbours)
158  {
159  std::cout << " " << newNeighbour->mIdentifier;
160  if (std::find(seenNeighbourIDs.begin(), seenNeighbourIDs.end(), newNeighbour->mIdentifier) == seenNeighbourIDs.end()) // not yet visited
161  {
162  std::cout << "*"; // Mark unvisited.
163  neighboursToVisit.push_back(newNeighbour);
164  seenNeighbourIDs.push_back(newNeighbour->mIdentifier);
165  result.push_back(newNeighbour);
166  uniqueCounter++;
167  }
168  }
169  std::cout << std::endl << newNeighbours.size() << " neighbours found. " << uniqueCounter << " not yet visited." << std::endl;
170  std::cout << "===================================================" << std::endl;
171  visitedCounter++;
172  }
173  std::cout << "Done." << std::endl;
174  std::cout << "Visited " << visitedCounter << " topologies in total." << std::endl;
175  std::cout << "===================================================" << std::endl;
176 
177  return result;
178 }
179 
180 std::vector<std::vector<bool>> TopologyCreator::selectRandomNeighbours(std::vector<std::vector<bool>>& pNeighbours)
181 {
182  if (pNeighbours.size() <= mMaxNeighbourCount) return pNeighbours; // if amount of neighbours is already smaller than maximum amount: return neighbours
183  //from lib_ism: Sort from by #relations.
184  struct compare {
185  bool operator() (const std::vector<bool> & first, const std::vector<bool> & second) {
186  return first.size() > second.size();
187  }
188  };
189 
190  std::sort(pNeighbours.begin(), pNeighbours.end(), compare());
191 
192  std::random_device rd;
193  std::mt19937 eng(rd());
194  std::normal_distribution<double> dist(0, pNeighbours.size() / 2);
195 
196  std::vector<std::vector<bool>> selectedNeighbours;
197 
198  for (unsigned int i = 0; i < mMaxNeighbourCount; i++)
199  {
200  unsigned int randIndex;
201  do {
202  randIndex = std::abs(dist(eng));
203  } while (randIndex >= pNeighbours.size());
204 
205  selectedNeighbours.push_back(pNeighbours[randIndex]);
206  pNeighbours.erase(pNeighbours.begin() + randIndex);
207 
208  }
209 
210  return selectedNeighbours;
211 }
212 
214 {
215  unsigned int numObjectTypes = mAllObjectTypes.size();
216  std::vector<boost::shared_ptr<Relation>> relations;
217  unsigned int bitvectorindex = 0;
218  for (unsigned int i = 0; i < numObjectTypes - 1; i++)
219  {
220  std::string typeA = mAllObjectTypes[i];
221  for (unsigned int j = i + 1; j < numObjectTypes; j++)
222  {
223  std::string typeB = mAllObjectTypes[j];
224  if (pBitvector[bitvectorindex])
225  {
226  boost::shared_ptr<Relation> newRelation(new Relation(typeA, typeB));
227  relations.push_back(newRelation);
228  }
229  bitvectorindex++;
230  }
231  }
233  result->mRelations = relations;
234  // set result's identifier:
235  std::string identifier = "";
236  for (bool bit: pBitvector)
237  {
238  if (bit) identifier += "1";
239  else identifier += "0";
240  }
241  result->mIdentifier = identifier;
242  return result;
243 }
244 
246 {
247  unsigned int numObjectTypes = mAllObjectTypes.size();
248  unsigned int numAllRelations = (numObjectTypes - 1) * numObjectTypes / 2;
249  std::vector<bool> result = std::vector<bool>(numAllRelations, false);
250  std::map<std::string, unsigned int> indicesByTypes;
251  for (unsigned int i = 0; i < numObjectTypes; i++)
252  indicesByTypes[mAllObjectTypes[i]] = i;
253 
254  std::vector<boost::shared_ptr<Relation>> relations = pTopology->mRelations;
255  for (boost::shared_ptr<Relation> relation: relations)
256  {
257  std::string typeA = relation->getObjectTypeA();
258  std::string typeB = relation->getObjectTypeB();
259  unsigned int indexA = indicesByTypes[typeA];
260  unsigned int indexB = indicesByTypes[typeB];
261  unsigned int indexMin = std::min(indexA, indexB);
262  unsigned int indexMax = std::max(indexA, indexB);
263  unsigned int bitvectorindex = indexMin * numObjectTypes - indexMin * (indexMin + 1) / 2 + (indexMax - indexMin) - 1;
264  result[bitvectorindex] = true;
265  }
266  return result;
267 }
268 
269 std::vector<std::vector<bool>> TopologyCreator::calculateNeighbours(std::vector<bool> pFrom)
270 {
271  // from TopologyCreatorPaper in lib_ism:
272  std::vector<std::vector<bool>> neighbours;
273  std::vector<unsigned int> ones;
274  std::vector<unsigned int> zeros;
275 
276  for (unsigned int i = 0; i < pFrom.size(); ++i)
277  {
278  if (pFrom[i])
279  {
280  ones.push_back(i);
281  } else {
282  zeros.push_back(i);
283  }
284  }
285 
286  std::vector<bool> neighbour;
287  if (mRemoveRelations)
288  {
289  //Remove one relation
290  for (unsigned int i = 0; i < ones.size(); ++i)
291  {
292  neighbour = pFrom;
293  neighbour[ones[i]] = 0;
294  neighbours.push_back(neighbour);
295  }
296  }
297 
298  for (unsigned int i = 0; i < zeros.size(); ++i)
299  {
300  //Add one relation
301  neighbour = pFrom;
302  neighbour[zeros[i]] = 1;
303  neighbours.push_back(neighbour);
304 
305  if (mSwapRelations)
306  {
307  //Swap one relation
308  for (unsigned int j = 0; j < ones.size(); ++j)
309  {
310  neighbour = pFrom;
311  neighbour[zeros[i]] = 1;
312  neighbour[ones[j]] = 0;
313  neighbours.push_back(neighbour);
314  }
315  }
316  }
317 
318  return neighbours;
319 }
320 
321 }
boost::shared_ptr< Topology > generateFullyMeshedTopology()
boost::shared_ptr< Topology > generateRandomTopology()
boost::shared_ptr< Topology > convertBitvectorToTopology(const std::vector< bool > &pBitvector)
std::vector< boost::shared_ptr< Topology > > generateNeighbours(boost::shared_ptr< Topology > pFrom)
std::vector< std::vector< bool > > calculateNeighbours(std::vector< bool > pFrom)
std::vector< bool > convertTopologyToBitvector(boost::shared_ptr< Topology > pTopology)
std::vector< std::string > mAllObjectTypes
boost::shared_ptr< ConnectivityChecker > mConnectivityChecker
TopologyCreator(const std::vector< std::string > &pAllObjectTypes, unsigned int pMaxNeighbourCount, bool pRemoveRelations, bool pSwapRelations)
std::vector< boost::shared_ptr< Topology > > generateStarTopologies()
std::vector< std::vector< bool > > selectRandomNeighbours(std::vector< std::vector< bool >> &pNeighbours)
std::vector< boost::shared_ptr< Topology > > generateAllConnectedTopologies()


asr_relation_graph_generator
Author(s): Meißner Pascal
autogenerated on Fri Nov 15 2019 03:39:19