random_graph_generator.cpp 10.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 fading, random_attribute_generator* connector_generator, random_attribute_generator* terminal_generator){
44
45
46
47
  Graph g (this->_template_edge_attributes, this->_template_node_attributes);
  while(!random_graph_generator::grow_random_acyclic_in_steps(g, this->_number_of_nodes/steps, this->_number_of_edges/steps, steps, fading, this->_edge_generator, this->_node_generator).first){
    std::cout << "generation failed; retrying!" << std::endl;
  }
48
49
50
51
52
  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;
  auto [source, target] = g.tip_fringes(*connector_generator, *terminal_generator);
53
54
55
56

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

57
  return {std::pair{source, target}, std::move(core_network), std::move(g)};
58
59
60
}


Jonas Seidel's avatar
Jonas Seidel committed
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
96
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;
}

97
98
99
100
101
102
103
104
105
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
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
140
141
142
143
144
145
146
147
148
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
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}};

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
  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_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_fading, 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_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;


  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);
    }
  }
  for(int curr_step = 0; curr_step < number_of_steps; ++curr_step){

    Node* n1;
    Node* n2;
jonasseidel's avatar
jonasseidel committed
246
    size_t edges_to_be_added = curr_step == number_of_steps-1 ? number_of_edges_per_step/(step_fading+1) : number_of_edges_per_step;
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
    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()){
        std::cout << "you might want to increase fading or decrease the number of generated edges" << std::endl;
        goto failed;
      };


      std::uniform_int_distribution<int> fading_dist (0, std::min(step_fading, number_of_steps-1-curr_step));
      try{
        rand_streams[curr_step] >> n1;
        rand_streams[curr_step + fading_dist(engine)] >> 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}};


Jonas Seidel's avatar
Jonas Seidel committed
278
279
280
281
282
283
284
285
286
287
288
289
  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,{{},{}}};
}