Ticket #8250: bgl_in_edges.cpp

File bgl_in_edges.cpp, 4.3 KB (added by Borja Miñano <bminyano@…>, 10 years ago)

bgl monoprocessor in_edge code (working)

Line 
1// Copyright (C) 2007-2008 The Trustees of Indiana University.
2
3// Use, modification and distribution is subject to the Boost Software
4// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#include <boost/config.hpp>
8#include <boost/throw_exception.hpp>
9#include <boost/mpl/print.hpp>
10#include <boost/graph/adjacency_list.hpp>
11#include <boost/graph/iteration_macros.hpp>
12#include <boost/test/minimal.hpp>
13#include <boost/graph/graphviz.hpp>
14#include <boost/graph/graphml.hpp>
15#include <boost/graph/erdos_renyi_generator.hpp>
16#include <boost/graph/random.hpp>
17#include <boost/random/uniform_real.hpp>
18#include <boost/random/mersenne_twister.hpp>
19#include <boost/property_map/property_map.hpp>
20#include <string>
21#include <iostream>
22
23#include <boost/graph/iteration_macros.hpp>
24#include <boost/graph/rmat_graph_generator.hpp>
25#include <boost/random/linear_congruential.hpp>
26
27#include <boost/graph/metis.hpp>
28
29#ifdef BOOST_NO_EXCEPTIONS
30void
31boost::throw_exception(std::exception const& ex)
32{
33 std::cout << ex.what() << std::endl;
34 abort();
35}
36#endif
37
38using namespace boost;
39using namespace std;
40
41/// VertexProperties structure to be attached to each vertex
42struct VertexProperties {
43 VertexProperties() {
44 cash = 100;
45 goods = 10;
46 price = 0.1;
47 }
48
49
50 double cash;
51 double goods;
52 double price;
53
54};
55
56struct EdgeProperties {
57
58 EdgeProperties() {
59 cash = 0;
60 goods = 0;
61 index = 0;
62 }
63
64 EdgeProperties(size_t i, double c, double g) : index(i), cash(c), goods(g) { }
65
66public:
67 std::size_t index;
68 double cash;
69 double goods;
70
71
72
73};
74
75
76 double lambda = 0.2;
77 double mu = 0.3;
78 double tau = 10;
79 double eps = 0.01;
80
81typedef boost::adjacency_list<vecS,
82 vecS,
83 bidirectionalS, VertexProperties, EdgeProperties> Graph;
84
85
86typedef graph_traits<Graph>::vertex_descriptor Vertex;
87typedef graph_traits<Graph>::edge_descriptor Edge;
88
89
90
91template<typename GMap, typename PMap, typename Graph>
92class Remover
93{
94public:
95 Remover(const double min, const GMap& egoods, const PMap& price, const Graph& graph)
96 : m_min(min), m_egoods(egoods), m_price(price), m_graph(graph) {}
97
98 template<typename ED>
99 bool operator()(ED w) const {
100 return m_price[target(w, m_graph)] > tau * m_min || m_egoods[w] < eps;
101 }
102
103private:
104 const Graph& m_graph;
105 const GMap& m_egoods;
106 const PMap& m_price;
107 const double m_min;
108};
109
110template<typename Graph>
111class Remover_1
112{
113public:
114 Remover_1(const Graph& graph)
115 : m_graph(graph) {}
116
117 template<typename ED>
118 bool operator()(ED w) const {
119 return target(w, m_graph) == source(w, m_graph);
120 }
121
122private:
123 const Graph& m_graph;
124};
125
126
127int test_main(int argc, char** argv)
128{
129
130
131//Random scale-free
132typedef boost::unique_rmat_iterator<boost::minstd_rand, Graph> RMATGen;
133boost::minstd_rand gen;
134 Graph g(RMATGen(gen, 100, 1000, 0.57, 0.19, 0.19, 0.05),
135 RMATGen(), 100);
136
137
138 property_map<Graph, vertex_index_t>::type
139 index_map = get(vertex_index, g);
140
141
142 typedef property_map<Graph, double EdgeProperties::*>::type egoods_map;
143 typedef property_map<Graph, double EdgeProperties::*>::type ecash_map;
144 typedef property_map<Graph, double VertexProperties::*>::type price_map;
145
146 //Vertex iterator distributed property maps
147 typedef property_map<Graph, vertex_index_t>::const_type VertexIndexMap;
148 typedef iterator_property_map<std::vector<size_t>::iterator, VertexIndexMap>
149 d_index_map;
150
151 std::vector<size_t> index_S(num_vertices(g), std::numeric_limits<size_t>::max());
152 d_index_map d_index(index_S.begin(), get(vertex_index, g));
153
154
155
156 //Delete self-edges
157 Remover_1<Graph> r(g);
158 remove_edge_if(r, g);
159
160
161 boost::graph_traits<Graph>::vertex_iterator vit, vit_end;
162 boost::graph_traits<Graph>::in_edge_iterator it, it_end;
163 boost::graph_traits<Graph>::out_edge_iterator oit, oit_end;
164
165 for(boost::tie(vit,vit_end) = vertices(g); vit != vit_end; ++vit) {
166 cout << index_map[*vit] << " --> ";
167 for (boost::tie(oit,oit_end) = out_edges(*vit, g); oit != oit_end; ++oit)
168 cout << index_map[target(*oit, g)] << " ";
169 cout << endl;
170 }
171
172
173 for(boost::tie(vit,vit_end) = vertices(g); vit != vit_end; ++vit) {
174 cout << index_map[*vit] << " <-- ";
175 for (boost::tie(it,it_end) = in_edges(*vit, g); it != it_end; ++it)
176 cout << index_map[source(*it, g)] << " ";
177 cout << endl;
178 }
179
180
181 return 0;
182}