PaGMO  1.1.5
topology/base.h
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 #ifndef PAGMO_TOPOLOGY_BASE_H
26 #define PAGMO_TOPOLOGY_BASE_H
27 
28 #include <boost/graph/adjacency_list.hpp>
29 #include <boost/shared_ptr.hpp>
30 #include <iostream>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 #include "../config.h"
36 #include "../serialization.h"
37 
38 namespace pagmo {
39 
41 
44 namespace topology {
45 
47 class base;
48 
50 typedef boost::shared_ptr<base> base_ptr;
51 
53 
75 class __PAGMO_VISIBLE base
76 {
77  protected:
78  // Structure defining the edge property, containing the probability of migration.
79  struct edge_properties {
80  double migr_probability; // migration probability for the edge representation
81  friend class boost::serialization::access;
82  template <class Archive>
83  void serialize(Archive &ar, const unsigned int)
84  {
85  ar & migr_probability;
86  }
87  };
88 
89  // The underlying graph type for the representation of the topology and shortcut typedefs for graph-related types.
90  // The graph is bidirectional, since we want to distinguish the migration direction for both ways.
91  // We use std::vector for VertexList and OutEdgeList, as we rarely update the structure and access the vertices by index.
92  // We use std::list for EdgeList since we mostly iterate over the global edge list.
93  typedef boost::adjacency_list<
94  boost::vecS, // std::vector for list of adjacent vertices (OutEdgeList)
95  boost::vecS, // std::vector for the list of vertices (VertexList)
96  boost::bidirectionalS, // we require bi-directional edges for topology (Directed)
97  boost::no_property, // no vertex properties (VertexProperties)
98  edge_properties, // edge property stores migration probability (EdgeProperties)
99  boost::no_property, // no graph properties (GraphProperties)
100  boost::listS // std::list for of the graph's edge list (EdgeList)
101  > graph_type;
102 
104  typedef boost::graph_traits<graph_type>::vertex_iterator v_iterator;
106  typedef boost::graph_traits<graph_type>::edge_iterator e_iterator;
108  typedef boost::graph_traits<graph_type>::adjacency_iterator a_iterator;
110  typedef graph_type::inv_adjacency_iterator ia_iterator;
112  typedef boost::graph_traits<graph_type>::vertex_descriptor v_descriptor;
114  typedef boost::graph_traits<graph_type>::edge_descriptor e_descriptor;
115  public:
117  typedef graph_type::vertices_size_type vertices_size_type;
119  typedef graph_type::edges_size_type edges_size_type;
120  base();
121  base(const base &);
122  base &operator=(const base &);
124 
133  virtual base_ptr clone() const = 0;
134  virtual ~base();
135  virtual std::string get_name() const;
136  std::string human_readable_terse() const;
137  std::string human_readable() const;
140  vertices_size_type get_number_of_vertices() const;
141  edges_size_type get_number_of_edges() const;
142  void push_back();
143  double get_average_shortest_path_length() const;
144  double get_clustering_coefficient() const;
145  std::vector<double> get_degree_distribution() const;
146  bool are_adjacent(const vertices_size_type &, const vertices_size_type &) const;
147  bool are_inv_adjacent(const vertices_size_type &,const vertices_size_type &) const;
148  std::vector<vertices_size_type> get_v_adjacent_vertices(const vertices_size_type &) const;
149  std::vector<vertices_size_type> get_v_inv_adjacent_vertices(const vertices_size_type &) const;
150  edges_size_type get_num_adjacent_vertices(const vertices_size_type &) const;
151  edges_size_type get_num_inv_adjacent_vertices(const vertices_size_type &) const;
152  void set_weight(double);
153  void set_weight(const vertices_size_type &, double);
154  void set_weight(const vertices_size_type &, const vertices_size_type &, double);
155  double get_weight(const vertices_size_type &, const vertices_size_type &) const;
157  protected:
160  void add_vertex();
161  std::pair<a_iterator,a_iterator> get_adjacent_vertices(const vertices_size_type &) const;
162  std::pair<ia_iterator,ia_iterator> get_inv_adjacent_vertices(const vertices_size_type &) const;
163  void add_edge(const vertices_size_type &, const vertices_size_type &);
164  void remove_edge(const vertices_size_type &, const vertices_size_type &);
165  void remove_all_edges();
166  std::pair<v_iterator,v_iterator> get_vertices() const;
168 
174  virtual void connect(const vertices_size_type &idx) = 0;
176  virtual std::string human_readable_extra() const;
177  private:
178  void check_vertex_index(const vertices_size_type &) const;
179  void set_weight(const e_descriptor &, double);
180  double get_weight(const e_descriptor &) const;
181  friend class boost::serialization::access;
182  template <class Archive>
183  void serialize(Archive &ar, const unsigned int)
184  {
185  ar & m_graph;
186  }
187  private:
188  graph_type m_graph;
189 };
190 
191 std::ostream __PAGMO_VISIBLE_FUNC &operator<<(std::ostream &, const base &);
192 
193 }}
194 
195 BOOST_SERIALIZATION_ASSUME_ABSTRACT(pagmo::topology::base)
196 
197 #endif
Root PaGMO namespace.
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base topology.
Definition: topology/base.h:47
Base topology class.
Definition: topology/base.h:75
graph_type::edges_size_type edges_size_type
Edges size type.
boost::graph_traits< graph_type >::vertex_iterator v_iterator
Iterator over the vertices.
boost::graph_traits< graph_type >::edge_iterator e_iterator
Iterator over the edges.
graph_type::vertices_size_type vertices_size_type
Vertices size type.
boost::graph_traits< graph_type >::edge_descriptor e_descriptor
Edge descriptor.
boost::graph_traits< graph_type >::adjacency_iterator a_iterator
Iterator over adjacent vertices.
graph_type::inv_adjacency_iterator ia_iterator
Iterator over inversely adjacent vertices.
std::ostream & operator<<(std::ostream &s, const base &t)
Overload stream insertion operator for topology::base.
boost::graph_traits< graph_type >::vertex_descriptor v_descriptor
Vertex descriptor.