random_graph_generator.cpp 12.9 KB
Newer Older
Jonas Seidel's avatar
Jonas Seidel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "random_graph_generator.h"

random_graph_generator::random_graph_generator(
  size_t number_of_edges, size_t number_of_nodes,
  random_attribute_generator edge_generator,
  random_attribute_generator node_generator,
  std::map<std::string, Attribute> default_values_edge_attributes,
  std::map<std::string, Attribute> default_values_node_attributes
)
  : _template_node_attributes(default_values_node_attributes), _template_edge_attributes(default_values_edge_attributes), _number_of_nodes(number_of_nodes), _number_of_edges(number_of_edges), _node_generator(node_generator), _edge_generator(edge_generator) {}

Graph random_graph_generator::next_acyclic(){
  Graph g (this->_template_edge_attributes, this->_template_node_attributes);
  while(!random_graph_generator::grow_random_acyclic(g, this->_number_of_nodes, this->_number_of_edges, this->_edge_generator, this->_node_generator).first){
    std::cout << "generation failed; retrying!" << std::endl;
  }
  return g;
}

Graph random_graph_generator::next(){
  Graph g (this->_template_edge_attributes, this->_template_node_attributes);
  while(!random_graph_generator::grow_random(g, this->_number_of_nodes, this->_number_of_edges, this->_edge_generator, this->_node_generator).first){
    std::cout << "generation failed; retrying!" << std::endl;
  }
  return g;
}

28
std::tuple<std::pair<Node*, Node*>, std::set<Edge*>, Graph> random_graph_generator::next_acyclic_2_tips(){
Jonas Seidel's avatar
Jonas Seidel committed
29
30
31
32
  Graph g (this->_template_edge_attributes, this->_template_node_attributes);
  while(!random_graph_generator::grow_random_acyclic(g, this->_number_of_nodes, this->_number_of_edges, this->_edge_generator, this->_node_generator).first){
    std::cout << "generation failed; retrying!" << std::endl;
  }
33
34
  std::set<Edge*> core_network = g.edges();

35
36
37
38
39
  auto [source, target] = g.tip_fringes(this->_edge_generator, this->_node_generator);

  source->add_attribute("Source", Attribute({fix, 1, Integral}) );
  target->add_attribute("Target", Attribute({fix, 1, Integral}) );

40
  return {std::pair{source, target}, std::move(core_network), std::move(g)};
Jonas Seidel's avatar
Jonas Seidel committed
41
42
}

43
std::tuple<std::pair<Node*, Node*>, std::set<Edge*>, Graph> random_graph_generator::next_acyclic_in_steps_2_tips(size_t steps, size_t fuzzing, bool only_tip_fringes, bool only_tip_extreme_layer, bool shelter_orphans, random_attribute_generator* connector_generator, random_attribute_generator* terminal_generator){
44
  Graph g (this->_template_edge_attributes, this->_template_node_attributes);
45
46
47

  std::tuple<bool, std::vector<std::set<Node*>>, std::pair<std::vector<Node*>, std::vector<Edge*>>> tpl;
  while( !std::get<0>(tpl = std::move(random_graph_generator::grow_random_acyclic_in_steps(g, this->_number_of_nodes/steps, this->_number_of_edges/steps, steps, fuzzing, this->_edge_generator, this->_node_generator))) ){
48
49
    std::cout << "generation failed; retrying!" << std::endl;
  }
50

51
52
53
54
  std::set<Edge*> core_network = g.edges();

  if(connector_generator == nullptr) connector_generator = &this->_edge_generator;
  if(terminal_generator == nullptr) terminal_generator = &this->_node_generator;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  Node* source;
  Node* target;
  if(only_tip_fringes){
    // connect those nodes to start that have no incoming edges / connect those nodes to target that have no outgoing edges

    if(only_tip_extreme_layer){
      // tips first and last layer

      auto [source_, target_] = g.tip_fringes(std::get<1>(tpl).front(), std::get<1>(tpl).back(), *connector_generator, *terminal_generator, shelter_orphans);
      source = source_;
      target = target_;
    }else{
      // tips all fringes

      auto [source_, target_] = g.tip_fringes(*connector_generator, *terminal_generator, shelter_orphans);
      source = source_;
      target = target_;
    }
  }else{
    // tips nodes regardless of if they have incoming or outgoing edges
    assert(only_tip_extreme_layer);
    // only those nodes of the first and last generated layers (tipping just everything doesn't make sense)

    Node* s = g.add_node(std::to_string(g.size().first), terminal_generator->next());
    for(Node* n : std::get<1>(tpl).front()){
      if(!shelter_orphans && n->incident().size() == 0) continue;
      std::stringstream name;
      name << s->description() << "_" << n->description();
      g.add_edge(s, n, name.str(), connector_generator->next());
    }
    Node* t = g.add_node(std::to_string(g.size().first), terminal_generator->next());
    for(Node* n : std::get<1>(tpl).back()){
      if(!shelter_orphans && n->incident().size() == 0) continue;
      std::stringstream name;
      name << n->description() << "_" << t->description();
      g.add_edge(n, t, name.str(), connector_generator->next());
    }

    source = s;
    target = t;
  }
96
97
98
99

  source->add_attribute("Source", Attribute({fix, 1, Integral}) );
  target->add_attribute("Target", Attribute({fix, 1, Integral}) );

100
  return {std::pair{source, target}, std::move(core_network), std::move(g)};
101
102
103
}


Jonas Seidel's avatar
Jonas Seidel committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
std::pair<bool, Attribute> random_graph_generator::node_template(const std::string& attr) const{
  auto search = this->_template_node_attributes.find(attr);
  if(search == this->_template_node_attributes.end()){
    return {false, {fix, 0}};
  }
  return {true, search->second};
}

std::pair<bool, Attribute> random_graph_generator::edge_template(const std::string& attr) const {
  auto search = this->_template_edge_attributes.find(attr);
  if(search == this->_template_edge_attributes.end()){
    return {false, {fix, 0}};
  }
  return {true, search->second};
}

Attribute random_graph_generator::edge_template_throwing(const std::string& attr){
  auto search = this->_template_edge_attributes.find(attr);
  if(search == this->_template_edge_attributes.end()){
    std::stringstream text;
    text << "\"" << attr << "\" is not defined by default for Edges generated by this Generator";
    throw std::range_error(text.str());
  }
  return search->second;
}

Attribute random_graph_generator::node_template_throwing(const std::string& attr){
  auto search = this->_template_node_attributes.find(attr);
  if(search == this->_template_node_attributes.end()){
    std::stringstream text;
    text << "\"" << attr << "\" is not defined by default for Nodes generated by this Generator";
    throw std::range_error(text.str());
  }
  return search->second;
}

140
141
142
143
144
145
146
147
148
random_attribute_generator& random_graph_generator::edge_generator(){
  return this->_edge_generator;
}

random_attribute_generator& random_graph_generator::node_generator(){
  return this->_node_generator;
}


Jonas Seidel's avatar
Jonas Seidel committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
std::pair<bool, std::pair<std::vector<Node*>, std::vector<Edge*>>> random_graph_generator::grow_random(Graph& g, size_t number_of_nodes, size_t number_of_edges, random_attribute_generator& edge_attribute_generator, random_attribute_generator& node_attribute_generator){
  std::vector<Node*> added_nodes;
  std::vector<Edge*> added_edges;
  added_nodes.reserve(number_of_nodes);
  added_edges.reserve(number_of_edges);

  for(size_t i = 0; i < number_of_nodes; ++i){
    std::stringstream name;
    name << g.size().first;
    added_nodes.push_back(g.add_node(name.str(), node_attribute_generator.next()));
  }
  random_set_element_generator<Node*> rand_stream (&g.nodes());

  Node* n1;
  Node* n2;
  for(size_t i = 0; i < number_of_edges; ++i){
    size_t attempt = 0;
    redo:
    if(attempt > g.nodes().size()*g.nodes().size()*g.nodes().size()){
      goto failed;
    };

    try{
      rand_stream >> n1;
      rand_stream >> n2;
    }catch (std::range_error& e){
      goto failed;
    }

    {
      auto existing_path = g.directed_admissible_st_path(n1, n2);
      if(existing_path.first && existing_path.second.number_of_edges() <= 1) {
        attempt++;
        goto redo;
      }
    }

    std::stringstream name;
    name << n1->description() << "_" << n2->description();
    added_edges.push_back(g.add_edge(n1, n2, name.str(), edge_attribute_generator.next()));
  }
  return {true,{added_nodes, added_edges}};

  failed:
  /*
    cleanup: restore state; due to failure: remove the already added components
  */
  for(Edge* e : added_edges){
    g.remove_edge(e);
  }
  for(Node* n : added_nodes){
    g.remove_node(n);
  }
  return {false,{{},{}}};
}

//TODO: add generate_random_edge and random node to node, edge and use additional parameters to generate random attributes for edges, nodes
std::pair<bool, std::pair<std::vector<Node*>, std::vector<Edge*>>> random_graph_generator::grow_random_acyclic(Graph& g, size_t number_of_nodes, size_t number_of_edges, random_attribute_generator& edge_attribute_generator, random_attribute_generator& node_attribute_generator){
  std::vector<Node*> added_nodes;
  std::vector<Edge*> added_edges;
  added_nodes.reserve(number_of_nodes);
  added_edges.reserve(number_of_edges);

  for(size_t i = 0; i < number_of_nodes; ++i){
    std::stringstream name;
    name << g.size().first;
    added_nodes.push_back(g.add_node(name.str(), node_attribute_generator.next()));
  }
  random_set_element_generator<Node*> rand_stream (&g.nodes());

  Node* n1;
  Node* n2;
  for(size_t i = 0; i < number_of_edges; ++i){
    size_t attempt = 0;
    redo:
    if(attempt > g.nodes().size()*g.nodes().size()*g.nodes().size()){
      goto failed;
    };

    try{
      rand_stream >> n1;
      rand_stream >> n2;
    }catch (std::range_error& e){
      goto failed;
    }

    if(n1 == n2 || g.directed_admissible_st_path(n2, n1).first || g.directed_admissible_st_path(n1, n2).second.number_of_edges() == 1) {
      attempt++;
      goto redo;
    }

    std::stringstream name;
    name << n1->description() << "_" << n2->description();
    added_edges.push_back(g.add_edge(n1, n2, name.str(), edge_attribute_generator.next()));
  }
  return {true,{added_nodes, added_edges}};

246
247
248
249
250
251
252
253
254
255
256
257
258
259
  failed:
  /*
    cleanup: restore state; due to failure: remove the already added components
  */
  for(Edge* e : added_edges){
    g.remove_edge(e);
  }
  for(Node* n : added_nodes){
    g.remove_node(n);
  }
  return {false,{{},{}}};
}

//TODO: add generate_random_edge and random node to node, edge and use additional parameters to generate random attributes for edges, nodes
260
std::tuple<bool, std::vector<std::set<Node*>>, std::pair<std::vector<Node*>, std::vector<Edge*>>> random_graph_generator::grow_random_acyclic_in_steps(Graph& g, size_t number_of_nodes_per_step, size_t number_of_edges_per_step, size_t number_of_steps, size_t step_fuzzing, random_attribute_generator& edge_attribute_generator, random_attribute_generator& node_attribute_generator){
261
262
263
264
265
266
267
268
  std::vector<Node*> added_nodes;
  std::vector<Edge*> added_edges;
  added_nodes.reserve(number_of_nodes_per_step);
  added_edges.reserve(number_of_edges_per_step);

  std::vector<std::set<Node*>> step_nodes;
  step_nodes.reserve(number_of_steps);
  std::vector<random_set_element_generator<Node*>> rand_streams;
269
  rand_streams.reserve(number_of_steps);
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286


  std::default_random_engine engine((std::random_device())());

  for(int curr_step = 0; curr_step < number_of_steps; ++curr_step){
    step_nodes.push_back(std::set<Node*>());
    rand_streams.push_back(random_set_element_generator<Node*>(&step_nodes.back())); // random_set_element_generator does not copy the set

    for(size_t i = 0; i < number_of_nodes_per_step; ++i){
      std::stringstream name;
      name << g.size().first;
      Node* new_node = g.add_node(name.str(), node_attribute_generator.next());
      added_nodes.push_back(new_node);
      step_nodes[curr_step].insert(new_node);
    }
  }

287
  for(int curr_step = 0; curr_step < number_of_steps; ++curr_step){
288
289
    Node* n1;
    Node* n2;
290
291
    size_t edges_to_be_added = (curr_step == number_of_steps-1) ? number_of_edges_per_step/(step_fuzzing+1) : number_of_edges_per_step;

292
293
294
295
    for(size_t i = 0; i < edges_to_be_added; ++i){ // the last step does not get any edges
      size_t attempt = 0;
      redo:
      if(attempt > g.nodes().size()*g.nodes().size()*g.nodes().size()){
296
        std::cout << "you might want to increase fuzzing or decrease the number of generated edges" << std::endl;
297
298
299
300
        goto failed;
      };


301
      std::uniform_int_distribution<int> fuzzing_dist (0, std::min(step_fuzzing, (number_of_steps-1)-curr_step));
302
303
      try{
        rand_streams[curr_step] >> n1;
304
        rand_streams[curr_step + fuzzing_dist(engine)] >> n2;
305
306
307
308
      }catch (std::range_error& e){
        goto failed;
      }

309
      //             ensure acyclic                                 ensure simple
310
311
312
313
314
315
316
317
318
319
      if(n1 == n2 || g.directed_admissible_st_path(n2, n1).first || g.directed_admissible_st_path(n1, n2).second.number_of_edges() == 1) {
        attempt++;
        goto redo;
      }

      std::stringstream name;
      name << n1->description() << "_" << n2->description();
      added_edges.push_back(g.add_edge(n1, n2, name.str(), edge_attribute_generator.next()));
    }
  }
320
  return {true,step_nodes,{added_nodes, added_edges}};
321
322


Jonas Seidel's avatar
Jonas Seidel committed
323
  failed:
324
325
  std::cerr << "graph generation failed; partial result given by:" << std::endl;
  std::cerr << g << std::endl;
Jonas Seidel's avatar
Jonas Seidel committed
326
327
328
329
330
331
332
333
334
  /*
    cleanup: restore state; due to failure: remove the already added components
  */
  for(Edge* e : added_edges){
    g.remove_edge(e);
  }
  for(Node* n : added_nodes){
    g.remove_node(n);
  }
335
  return {false,{},{{},{}}};
Jonas Seidel's avatar
Jonas Seidel committed
336
}