14 #ifndef HMS_DIRECTED_GRAPH_HPP_INCLUDED
15 #define HMS_DIRECTED_GRAPH_HPP_INCLUDED
52 const unsigned int nb_hidden_variables;
58 double vertices_density;
60 std::vector<std::string> pdf_hidden_variables_type;
61 std::vector< std::vector<double> > pdf_hidden_variables_param;
62 std::string prob_connection_type;
63 std::vector<double> prob_connection_param;
64 std::vector<double> min_hidden_variables;
65 std::vector<double> max_hidden_variables;
66 std::vector<double> avg_hidden_variables;
69 std::uniform_real_distribution<double> uniform_01;
72 const double Pi = 3.1415926535897932384626433832795;
77 double compute_distance_in_metric_space(std::vector<double> x1, std::vector<double> x2);
93 void create_edges(std::string _prob_connection_type, std::vector<double> _prob_connection_param, std::mt19937& _engine);
111 : nb_hidden_variables(2)
120 return nb_dimensions;
128 this->set_nb_vertices(_nb_vertices);
130 for(
unsigned int n(0), nn(this->get_nb_vertices()); n<nn ; ++n)
132 (*this)(n)->set_name(std::to_string(n));
135 geometry = _geometry;
137 vertices_density = _vertices_density;
139 if( geometry ==
"S1" )
142 double radius = this->get_nb_vertices() / (2.0 * Pi * vertices_density);
147 for(
unsigned int n(0), nn(this->get_nb_vertices()); n<nn ; ++n)
149 phi = 2 * Pi * uniform_01(_engine);
150 (*this)(n)->set_metric_space_position({radius,phi});
155 std::cout <<
"The geometry " << geometry <<
" is not supported." << std::endl;
258 if( _sequence_hidden_variables.size() != this->get_nb_vertices() )
260 std::cout <<
"The length of the sequence of hidden variables is not equal to the number of vertices." << std::endl;
264 pdf_hidden_variables_type[0] =
"pdf_custom_sequence";
265 pdf_hidden_variables_param[0] = {};
266 pdf_hidden_variables_type[1] =
"pdf_custom_sequence";
267 pdf_hidden_variables_param[1] = {};
269 for(
unsigned int n(0), nn(this->get_nb_vertices()); n<nn; ++n)
272 (*this)(n)->set_hidden_variables(_sequence_hidden_variables[n]);
275 survey_hidden_variables();
309 double gcl::hms_directed_graph::compute_distance_in_metric_space(std::vector<double> x1, std::vector<double> x2)
311 if( geometry ==
"S1" )
313 if(std::fabs(x1[0]-x2[0]) < 1e-8)
315 return x1[0] * ( Pi - std::fabs( Pi - std::fabs( x1[1] - x2[1] ) ) );
319 std::cout <<
"The two points do not lie on the same circle." << std::endl;
325 std::cout <<
"The geometry " << geometry <<
" is not supported." << std::endl;
335 min_hidden_variables.clear();
336 min_hidden_variables.resize(nb_hidden_variables,this->get_nb_vertices());
337 max_hidden_variables.clear();
338 max_hidden_variables.resize(nb_hidden_variables,0);
339 avg_hidden_variables.clear();
340 avg_hidden_variables.resize(nb_hidden_variables,0);
343 std::vector<double> tmp_hidden_variables;
344 for(
unsigned int v(0), vv(this->get_nb_vertices()); v<vv; ++v)
346 tmp_hidden_variables = (*this)(v)->get_hidden_variables();
348 for(
unsigned int t(0); t<nb_hidden_variables; ++t)
351 if(tmp_hidden_variables[t] < min_hidden_variables[t])
353 min_hidden_variables[t] = tmp_hidden_variables[t];
355 if(tmp_hidden_variables[t] > max_hidden_variables[t])
357 max_hidden_variables[t] = tmp_hidden_variables[t];
360 avg_hidden_variables[t] += tmp_hidden_variables[t];
364 for(
unsigned int t(0); t<nb_hidden_variables; ++t)
366 avg_hidden_variables[t] /= this->get_nb_vertices();
375 prob_connection_type = _prob_connection_type;
376 prob_connection_param = _prob_connection_param;
378 unsigned int tmp_nb_edges(0);
379 double tmp_dist, tmp_chi, tmp_r;
380 std::vector<double> x1, x2, k1, k2;
382 for(
unsigned int n1(0), nn(this->get_nb_vertices()); n1<nn ; ++n1)
385 x1 = (*this)(n1)->get_metric_space_position();
387 k1 = (*this)(n1)->get_hidden_variables();
389 for(
unsigned int n2(n1+1); n2<nn; ++n2)
392 x2 = (*this)(n2)->get_metric_space_position();
394 k2 = (*this)(n2)->get_hidden_variables();
396 tmp_dist = compute_distance_in_metric_space(x1,x2);
399 tmp_chi = tmp_dist / std::pow(prob_connection_param.back() * k1[id_out_degree] * k2[id_in_degree],1./nb_dimensions);
401 if( prob_connection_type ==
"prob_fermi-dirac" )
402 tmp_r = 1. / (std::pow(tmp_chi,prob_connection_param[0]) + 1);
404 if( uniform_01(_engine) < tmp_r )
412 tmp_chi = tmp_dist / std::pow(prob_connection_param.back() * k1[id_in_degree] * k2[id_out_degree],1./nb_dimensions);
414 if( prob_connection_type ==
"prob_fermi-dirac" )
415 tmp_r = 1. / (std::pow(tmp_chi,prob_connection_param[0]) + 1);
417 if( uniform_01(_engine) < tmp_r )
426 this->set_nb_edges(tmp_nb_edges);
428 this->survey_degrees_distribution();
436 std::ofstream graph_properties_file;
438 std::string graph_properties_filename = _name +
"_graph_properties.dat";
440 graph_properties_file.open(graph_properties_filename.c_str(), std::ios_base::out);
441 if( !graph_properties_file.is_open() )
443 std::cout <<
"Could not open file: " << _name <<
"." << std::endl;
447 time_t theTime = time(NULL);
448 struct tm *aTime = gmtime(&theTime);
449 int year = aTime->tm_year + 1900;
450 int month = aTime->tm_mon + 1;
451 int day = aTime->tm_mday;
452 int hours = aTime->tm_hour;
453 int minutes = aTime->tm_min;
455 graph_properties_file <<
"===================================================================================" << std::endl;
456 graph_properties_file <<
"General information " << std::endl;
457 graph_properties_file <<
"Type of graph: " << this->get_type_of_graph() << std::endl;
458 graph_properties_file <<
"Name: " << _name << std::endl;
459 graph_properties_file <<
"Computed on: " << year <<
"/";
461 graph_properties_file <<
"0";
462 graph_properties_file << month <<
"/";
464 graph_properties_file <<
"0";
465 graph_properties_file << day <<
" " << hours <<
":";
467 graph_properties_file <<
"0";
468 graph_properties_file << minutes <<
" UTC" << std::endl;
469 graph_properties_file <<
"===================================================================================" << std::endl;
470 graph_properties_file <<
"Parameters" << std::endl;
471 graph_properties_file <<
"Geometry: " << geometry << std::endl;
472 graph_properties_file <<
"Number of nodes: " << this->get_nb_vertices() << std::endl;
473 graph_properties_file <<
"Vertices density: " << vertices_density << std::endl;
502 if(prob_connection_type ==
"prob_fermi-dirac")
504 graph_properties_file <<
"Prob. of connection: fermi-dirac" << std::endl;
505 graph_properties_file <<
" - beta: " << prob_connection_param[0] << std::endl;
506 graph_properties_file <<
" - mu: " << prob_connection_param[1] << std::endl;
508 graph_properties_file <<
"===================================================================================" << std::endl;
509 graph_properties_file <<
"Hidden variables (including zero-degree vertices)" << std::endl;
510 graph_properties_file <<
"Related to the in-degree" << std::endl;
511 graph_properties_file <<
" - minimum: " << min_hidden_variables[id_in_degree] << std::endl;
512 graph_properties_file <<
" - maximum: " << max_hidden_variables[id_in_degree] << std::endl;
513 graph_properties_file <<
" - average: " << avg_hidden_variables[id_in_degree] << std::endl;
514 graph_properties_file <<
"Related to the out-degree" << std::endl;
515 graph_properties_file <<
" - minimum: " << min_hidden_variables[id_out_degree] << std::endl;
516 graph_properties_file <<
" - maximum: " << max_hidden_variables[id_out_degree] << std::endl;
517 graph_properties_file <<
" - average: " << avg_hidden_variables[id_out_degree] << std::endl;
518 graph_properties_file <<
"===================================================================================" << std::endl;
519 graph_properties_file <<
"Observables (excluding zero-degree vertices)" << std::endl;
520 graph_properties_file <<
"Number of nodes: " << this->get_nb_vertices()-this->get_nb_zerodegree_vertices() << std::endl;
521 graph_properties_file <<
"Number of edges: " << this->get_nb_edges() << std::endl;
522 graph_properties_file <<
" - reciprocals: " << this->get_nb_reciprocal_edges() << std::endl;
523 graph_properties_file <<
"In-degree" << std::endl;
524 graph_properties_file <<
" - minimum (excluding zero): " << this->get_min_in_degree() << std::endl;
525 graph_properties_file <<
" - maximum: " << this->get_max_in_degree() << std::endl;
526 graph_properties_file <<
" - average: " << this->get_avg_in_degree() << std::endl;
527 graph_properties_file <<
"Out-degree" << std::endl;
528 graph_properties_file <<
" - minimum (excluding zero): " << this->get_min_out_degree() << std::endl;
529 graph_properties_file <<
" - maximum: " << this->get_max_out_degree() << std::endl;
530 graph_properties_file <<
" - average: " << this->get_avg_out_degree() << std::endl;
532 graph_properties_file.close();
540 std::ofstream vertices_properties_file;
542 std::string vertices_properties_filename = _name +
"_vertices_properties.dat";
544 vertices_properties_file.open(vertices_properties_filename.c_str(), std::ios_base::out);
545 if( !vertices_properties_file.is_open() )
547 std::cout <<
"Could not open file: " << _name <<
". Aborting." << std::endl;
551 vertices_properties_file.precision(8);
552 for(
unsigned int n(0), nn(this->get_nb_vertices()); n<nn; ++n)
554 vertices_properties_file << std::fixed << (*this)(n)->get_name() <<
" ";
555 for(
unsigned int t(0), tt(nb_dimensions+1); t<tt; ++t)
556 vertices_properties_file << std::fixed << (*
this)(n)->get_metric_space_position()[t] <<
" ";
557 for(
unsigned int t(0), tt(nb_hidden_variables); t<tt; ++t)
558 vertices_properties_file << std::fixed << (*
this)(n)->get_hidden_variables()[t] <<
" ";
559 vertices_properties_file << std::fixed << (*this)(n)->get_out_degree() <<
" ";
560 vertices_properties_file << std::fixed << (*this)(n)->get_in_degree() <<
" ";
561 vertices_properties_file << std::fixed << (*this)(n)->get_nb_reciprocal_edges() << std::endl;
564 vertices_properties_file.close();
572 base_directed_graph_clear();
574 this->set_type_of_graph(
"HMS directed graph");
583 pdf_hidden_variables_type.clear();
584 pdf_hidden_variables_type.resize(nb_hidden_variables);
585 pdf_hidden_variables_param.clear();
586 pdf_hidden_variables_param.resize(nb_hidden_variables);
588 prob_connection_type =
"00";
589 prob_connection_param.clear();
591 vertices_density = 0;
593 min_hidden_variables.clear();
594 min_hidden_variables.resize(nb_hidden_variables);
595 max_hidden_variables.clear();
596 max_hidden_variables.resize(nb_hidden_variables);
597 avg_hidden_variables.clear();
598 avg_hidden_variables.resize(nb_hidden_variables);
603 #endif // HMS_DIRECTED_GRAPH_HPP_INCLUDED
Class of the attributes of a simple edge.
Definition: edge_attributes.hpp:24
void clear()
Reinitializes the graph.
Definition: hms_directed_graph.hpp:569
Virtual template base class for directed graph objects.
Definition: base_directed_graph.hpp:37
void survey_hidden_variables()
Computes the quantities related to the degrees distribution.
Definition: hms_directed_graph.hpp:332
void write_graph_properties(std::string _name)
Exports the graph's properties.
Definition: hms_directed_graph.hpp:433
Class for vertices in random directed graphs embedded in a hidden metric space.
void create_edges(std::string _prob_connection_type, std::vector< double > _prob_connection_param, std::mt19937 &_engine)
Creates the edges of the graph.
Definition: hms_directed_graph.hpp:372
Class for random directed graphs embedded in a hidden metric space.
Definition: hms_directed_graph.hpp:46
void set_hidden_variables(std::vector< std::vector< double > > _sequence_hidden_variables)
Sets the hidden variables of vertices.
Definition: hms_directed_graph.hpp:255
void set_metric_space_positions(unsigned int _nb_vertices, std::string _geometry, double _delta, std::mt19937 &_engine)
Sets the metric space position.
Definition: hms_directed_graph.hpp:125
hms_directed_graph()
Constructor.
Definition: hms_directed_graph.hpp:110
Random number generators and related functions for various distributions.
void write_vertices_properties(std::string _name)
Exports the vertices' properties.
Definition: hms_directed_graph.hpp:537
double get_nb_dimensions()
Returns the number of dimensions.
Definition: hms_directed_graph.hpp:118
Virtual base class for directed graphs.
~hms_directed_graph()
Destructor.
Definition: hms_directed_graph.hpp:82