bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
boost_serial_graph.h
Go to the documentation of this file.
1#ifndef BOOST_SERIAL_GRAPH_H
2#define BOOST_SERIAL_GRAPH_H
3
4#include "bitrl/bitrl_types.h"
6
7#include <boost/graph/graph_traits.hpp>
8#include <boost/graph/adjacency_list.hpp>
9#include <boost/graph/undirected_graph.hpp>
10
11#include <utility>
12#include <vector>
13#include <iterator>
14#include <stdexcept>
15
16namespace bitrl
17{
18namespace utils{
19
26template<typename VertexData, typename EdgeData>
28{
29
30public:
31
36
41
59
64
69
70private:
71
72 typedef boost::undirected_graph<vertex_t, edge_t> graph_type;
73 typedef typename boost::graph_traits<graph_type>::vertex_descriptor vertex_descriptor_t;
74 typedef typename boost::graph_traits<graph_type>::edge_descriptor edge_descriptor_t;
75
76public:
77
81 typedef typename graph_type::edge_iterator edge_iterator;
82
86 typedef typename graph_type::adjacency_iterator adjacency_iterator;
87
91 explicit BoostSerialGraph(uint_t nvs=0);
92
96 vertex_t& add_vertex(const VertexData& data);
97
102
106 const vertex_t& get_vertex(uint_t i)const;
107
112
118
124
129 const edge_t& get_edge(uint_t v1, uint_t v2)const;
130
136
140 std::pair<edge_iterator,edge_iterator> edges()const;
141
145 std::pair<adjacency_iterator, adjacency_iterator> get_vertex_neighbors(uint_t id)const;
146
150 std::pair<adjacency_iterator, adjacency_iterator> get_vertex_neighbors(const vertex_t& v)const;
151
156 std::vector<uint_t> get_vertex_neighbors_ids(uint_t id)const;
157
161 uint_t n_vertices()const{return g_.num_vertices();}
162
166 uint_t max_vertex_index()const noexcept{return g_.max_vertex_index();}
167
171 uint_t n_edges()const{return g_.num_edges();}
172
176 uint_t max_edge_index() const noexcept{return g_.max_edge_index();}
177
181 void clear(){g_.clear();}
182
183private:
184
188 graph_type g_;
189};
190
191template<typename VertexData,typename EdgeData>
196
197template<typename VertexData, typename EdgeData>
200
202 typedef typename BoostSerialGraph<VertexData,EdgeData>::vertex_descriptor_t vertex_descriptor_t;
203 uint_t idx = n_vertices();
204
205 //add a new vertex
206 vertex_descriptor_t a = boost::add_vertex(g_);
207 vertex_t& v = g_[a];
208 v.data = data;
209 v.id = idx;
210 return v;
211}
212
213template<typename VertexData,typename EdgeData>
216
217 //TODO what happens when v1 and v2 are not valid vertex indices?
218 //at the moment we throw an assertion
219 if(v1>=n_vertices() || v2 >=n_vertices())
220 throw std::logic_error("Invalid vertex index v1/v2: "+
221 std::to_string(v1)+
222 "/"+
223 std::to_string(v2)+
224 " not in [0,"+
225 std::to_string(n_vertices())+
226 ")");
227 typedef typename BoostSerialGraph<VertexData,EdgeData>::vertex_descriptor_t vertex_descriptor_t;
228 typedef typename BoostSerialGraph<VertexData,EdgeData>::edge_descriptor_t edge_descriptor_t;
230 edge_descriptor_t et;
231 bool condition;
232
233 // get the vertices that correspond to the indices
234 vertex_descriptor_t a = boost::vertex(v1, g_);
235 vertex_descriptor_t b = boost::vertex(v2, g_);
236 uint_t idx = n_edges();
237
238 // create an edge
239 boost::tie(et, condition) = boost::add_edge(a,b,g_);
240 edge_t& edge = g_[et];
241 edge.set_id(idx);
242 return edge;
243}
244
245
246template<typename VertexData,typename EdgeData>
249
250 if(i>=n_vertices()){
251 throw std::logic_error("Invalid vertex index. Index "+
252 std::to_string(i)+
253 " not in [0,"+
254 std::to_string(n_vertices())+
255 ")");
256 }
257
258 typedef typename BoostSerialGraph<VertexData,EdgeData>::vertex_descriptor_t vertex_descriptor_t;
259 vertex_descriptor_t a = boost::vertex(i,g_);
260 return g_[a];
261}
262
263template<typename VertexData,typename EdgeData>
269
270template<typename VertexData,typename EdgeData>
276
277template<typename VertexData,typename EdgeData>
286
287template<typename VertexData,typename EdgeData>
288std::vector<uint_t>
290
291 if(id >=n_vertices()){
292 throw std::logic_error("Invalid vertex index. Index "+
293 std::to_string(id)+
294 " not in [0,"+
295 std::to_string(n_vertices())+
296 ")");
297 }
298
299
300 auto vneighs = get_vertex_neighbors(id);
301 std::vector<uint_t> neighbors;
302 neighbors.reserve(std::distance(vneighs.first, vneighs.second));
303
304 auto start = vneighs.first;
305 auto end = vneighs.second;
306 while(start != end){
307 auto& vertex = get_vertex(start);
308 neighbors.push_back(vertex.id);
309 ++start;
310 }
311
312 neighbors;
313}
314
315
316template<typename VertexData,typename EdgeData>
319
320 //TODO what happens when v1 and v2 are not valid vertex indices?
321 //at the moment we throw an assertion
322 if(v1>=n_vertices() || v2 >=n_vertices())
323 throw std::logic_error("Invalid vertex index v1/v2. Indeces "+
324 std::to_string(v1)+
325 "/"+
326 std::to_string(v2)+
327 " not in [0,"+
328 std::to_string(n_vertices())+
329 ")");
330
331 typedef typename BoostSerialGraph<VertexData,EdgeData>::vertex_descriptor_t vertex_descriptor_t;
332 typedef typename BoostSerialGraph<VertexData,EdgeData>::edge_descriptor_t edge_descriptor_t;
333 vertex_descriptor_t a = boost::vertex(v1,g_);
334 vertex_descriptor_t b = boost::vertex(v2,g_);
335
336 std::pair<edge_descriptor_t,bool> rslt = boost::edge(a,b,g_);
337
338 return g_[rslt.first];
339}
340
341
342template<typename VertexData,typename EdgeData>
351
352template<typename VertexData,typename EdgeData>
353std::pair<typename BoostSerialGraph<VertexData,EdgeData>::adjacency_iterator,
356
357 if(i>=n_vertices()){
358 throw std::logic_error("Invalid vertex index. Index "+
359 std::to_string(i)+
360 " not in [0,"+
361 std::to_string(n_vertices())+
362 ")");
363 }
364
365 typedef typename BoostSerialGraph<VertexData,EdgeData>::vertex_descriptor_t vertex_descriptor_t;
366 vertex_descriptor_t a = boost::vertex(i,g_);
367 return boost::adjacent_vertices(a, g_);
368}
369
370template<typename VertexData,typename EdgeData>
371std::pair<typename BoostSerialGraph<VertexData,EdgeData>::adjacency_iterator,
374 return get_vertex_neighbors(v.id);
375}
376
377template<typename VertexData,typename EdgeData>
378std::pair<typename BoostSerialGraph<VertexData,EdgeData>::edge_iterator,
381 return boost::edges(g_);
382}
383
384
385template<typename VertexData,typename EdgeData>
387:
388data(data_),
389id(bitrl::consts::INVALID_ID)
390{}
391
392template<typename VertexData,typename EdgeData>
394:
395data(data_),
396id(bitrl::consts::INVALID_ID)
397{
398 data_.clear();
399}
400
401template<typename VertexData,typename EdgeData>
402BoostSerialGraph<VertexData,EdgeData>::SerialGraphNode::SerialGraphNode(const typename BoostSerialGraph<VertexData,EdgeData>::SerialGraphNode& o)
403:
404data(o.data),
405id(o.id)
406{}
407
408
409template<typename VertexData,typename EdgeData>
410typename BoostSerialGraph<VertexData,EdgeData>::SerialGraphNode&
412
413 if(this == &o)
414 return *this;
415
416 this->data = o.data;
417 this->id = o.id;
418 return *this;
419
420}
421
422/*template<typename VertexData,typename EdgeData>
423bool operator==(const typename boost_unidirected_serial_graph<VertexData,EdgeData>::vertex_t& v1,
424 const typename boost_unidirected_serial_graph<VertexData,EdgeData>::vertex_t& v2){
425
426 return (v2.id == v1.id);
427}
428
429template<typename VertexData,typename EdgeData>
430bool operator!=(const typename boost_unidirected_serial_graph<VertexData,EdgeData>::vertex_t& v1,
431 const typename boost_unidirected_serial_graph<VertexData,EdgeData>::vertex_t& v2){
432
433 return !(v2 == v1);
434}*/
435
436/*template<typename VertexData>
437inline
438bool operator==(const typename boost_unidirected_serial_graph<VertexData,void>::vertex_t& v1,
439 const typename boost_unidirected_serial_graph<VertexData,void>::vertex_t& v2){
440
441 return (v2.id == v1.id);
442}
443
444template<typename VertexData>
445inline
446bool operator!=(const typename boost_unidirected_serial_graph<VertexData,void>::vertex_t& v1,
447 const typename boost_unidirected_serial_graph<VertexData,void>::vertex_t& v2){
448
449 return !(v2 == v1);
450}*/
451
452
453}
454}
455#endif // BOOST_SERIAL_GRAPH_H
. Representation of a graph using adjacency lists. The underlying implementation uses Boost Graph lib...
Definition boost_serial_graph.h:28
GenericLine< vertex_t, EdgeData > edge_t
edge_t The edge type
Definition boost_serial_graph.h:68
const edge_t & get_edge(uint_t v1, uint_t v2) const
Access the i-th edge of the graph with endpoints the given vertices.
Definition boost_serial_graph.h:318
EdgeData edge_data_t
edge_data_t The type of the edge data
Definition boost_serial_graph.h:40
std::vector< uint_t > get_vertex_neighbors_ids(uint_t id) const
get_vertex_neighbors_ids Returns the ids of the vertices connectected with this vertex
Definition boost_serial_graph.h:289
uint_t n_vertices() const
Returns the number of vertices.
Definition boost_serial_graph.h:161
uint_t max_vertex_index() const noexcept
Returns the maximum vertex index.
Definition boost_serial_graph.h:166
std::pair< adjacency_iterator, adjacency_iterator > get_vertex_neighbors(const vertex_t &v) const
Returns the neighboring vertices for the given vertex id.
const vertex_t & get_vertex(uint_t i) const
Access the i-th vertex of the graph.
Definition boost_serial_graph.h:248
vertex_t & add_vertex(const VertexData &data)
Add a vertex to the graph by providing the data.
Definition boost_serial_graph.h:199
graph_type::edge_iterator edge_iterator
edge_iterator Edge iterator
Definition boost_serial_graph.h:81
edge_t & add_edge(uint_t v1, uint_t v2)
Add an edge formed by the two given vertices.
Definition boost_serial_graph.h:215
VertexData vertex_data_t
vertex_data_t The type of the vertex data
Definition boost_serial_graph.h:35
BoostSerialGraph(uint_t nvs=0)
Constructor.
Definition boost_serial_graph.h:192
uint_t n_edges() const
Returns the number of edges.
Definition boost_serial_graph.h:171
SerialGraphNode vertex_t
vertex_t The vertex type
Definition boost_serial_graph.h:63
void clear()
Clear the graph.
Definition boost_serial_graph.h:181
std::pair< edge_iterator, edge_iterator > edges() const
edges Access the edges of the tree
Definition boost_serial_graph.h:380
graph_type::adjacency_iterator adjacency_iterator
adjacency_iterator Adjacency iterator
Definition boost_serial_graph.h:86
uint_t max_edge_index() const noexcept
Returns the maximum edge index.
Definition boost_serial_graph.h:176
std::pair< adjacency_iterator, adjacency_iterator > get_vertex_neighbors(uint_t id) const
Returns the neighboring vertices for the given vertex id.
Definition boost_serial_graph.h:355
const uint_t INVALID_ID
Invalid id.
Definition bitrl_consts.h:21
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:25
Definition bitrl_consts.h:14
std::size_t uint_t
uint_t
Definition bitrl_types.h:43
Class that represents the Node of a graph.
Definition boost_serial_graph.h:46
vertex_data_t data
Definition boost_serial_graph.h:47
bool operator!=(const SerialGraphNode &o) const
Definition boost_serial_graph.h:57
uint_t id
Definition boost_serial_graph.h:48
bool operator==(const SerialGraphNode &o) const
Definition boost_serial_graph.h:56
SerialGraphNode & operator=(const SerialGraphNode &o)
Definition boost_serial_graph.h:411