random_graph_generator.cpp 6.49 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
96
97
98
99
100
101
102
103
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
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
#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;
}

std::pair<std::pair<Node*, Node*>,Graph> random_graph_generator::next_acyclic_2_tips(){
  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;
  }
  auto tips = g.tip_fringes(this->_edge_generator, this->_node_generator);
  return {tips, std::move(g)};
}

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;
}

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}};

  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,{{},{}}};
}