Limbo 3.5.4
Loading...
Searching...
No Matches
test_MISColoring.cpp
Go to the documentation of this file.
1
7
8#include <iostream>
9#include <fstream>
10#include <string>
12#include <boost/graph/graphviz.hpp>
13#include <boost/graph/graph_utility.hpp>
14#include <boost/graph/adjacency_list.hpp>
15#include <boost/graph/undirected_graph.hpp>
17#include <boost/graph/erdos_renyi_generator.hpp>
18#include <boost/random/mersenne_twister.hpp>
19#include <boost/graph/random.hpp>
20#include <boost/graph/iteration_macros.hpp>
21
22#include <boost/version.hpp>
23#if BOOST_VERSION <= 14601
24#include <boost/graph/detail/is_same.hpp>
25#else
26#include <boost/type_traits/is_same.hpp>
27#endif
28
29using std::cout;
30using std::endl;
31using std::ifstream;
32using std::ofstream;
33using std::string;
34using std::pair;
35using namespace boost;
36
38// do not use setS, it does not compile for subgraph
39// do not use custom property tags, it does not compile for most utilities
40typedef adjacency_list<vecS, vecS, undirectedS,
41 property<vertex_index_t, std::size_t, property<vertex_color_t, int> >,
42 property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
43 property<graph_name_t, string> > graph_type;
44typedef subgraph<graph_type> subgraph_type;
45typedef property<vertex_index_t, std::size_t> VertexId;
46typedef property<edge_index_t, std::size_t> EdgeID;
47typedef graph_traits<graph_type>::vertex_descriptor vertex_descriptor;
48typedef graph_traits<graph_type>::edge_descriptor edge_descriptor;
49typedef property_map<graph_type, edge_weight_t>::type edge_weight_map_type;
50typedef property_map<graph_type, vertex_color_t>::type vertex_color_map_type;
52
55{
56 mt19937 gen;
57 graph_type g;
58 int N = 40;
59 std::vector<vertex_descriptor> vertex_set;
60 std::vector< std::pair<vertex_descriptor, vertex_descriptor> > edge_set;
61 generate_random_graph(g, N, N * 2, gen,
62 std::back_inserter(vertex_set),
63 std::back_inserter(edge_set));
64 BOOST_AUTO(edge_weight_map, get(edge_weight, g));
65 unsigned int i = 0;
66 graph_traits<graph_type>::edge_iterator eit, eit_end;
67 for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit, ++i)
68 {
69 edge_weight_map[*eit] = 1;
70 }
71
72 //test relaxed LP based coloring
74 lc.stitch_weight(0.1);
75 // THREE or FOUR
76 lc.color_num(limbo::algorithms::coloring::MISColoring<graph_type>::THREE);
77 double cost = lc();
78 cout << "final cost = " << cost << endl;
79}
80
83void realGraph(string const& filename)
84{
85 ifstream in (filename.c_str());
86
87 // the graphviz reader in boost cannot specify vertex_index_t
88 // I have to create a temporary graph and then construct the real graph
89 typedef adjacency_list<vecS, vecS, undirectedS,
90 property<vertex_index_t, std::size_t, property<vertex_color_t, int, property<vertex_name_t, std::size_t> > >,
91 property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
92 property<graph_name_t, string> > tmp_graph_type;
93 tmp_graph_type tmpg;
94 dynamic_properties tmpdp;
95 tmpdp.property("node_id", get(vertex_name, tmpg));
96 tmpdp.property("label", get(vertex_name, tmpg));
97 tmpdp.property("weight", get(edge_weight, tmpg));
98 tmpdp.property("label", get(edge_weight, tmpg));
99 limboAssert(read_graphviz(in, tmpg, tmpdp, "node_id"));
100
101 // real graph
102 graph_type g (num_vertices(tmpg));
103 graph_traits<tmp_graph_type>::vertex_iterator vit, vit_end;
104 for (tie(vit, vit_end) = vertices(tmpg); vit != vit_end; ++vit)
105 {
106 size_t name = get(vertex_name, tmpg, *vit);
107 int color = get(vertex_color, tmpg, *vit);
108 put(vertex_color, g, (vertex_descriptor)name, color);
109 }
110
111 graph_traits<tmp_graph_type>::edge_iterator eit, eit_end;
112 for (tie(eit, eit_end) = edges(tmpg); eit != eit_end; ++eit)
113 {
114 size_t s_name = get(vertex_name, tmpg, source(*eit, tmpg));
115 size_t t_name = get(vertex_name, tmpg, target(*eit, tmpg));
116 pair<edge_descriptor, bool> pe = add_edge(s_name, t_name, g);
117 limboAssert(pe.second);
118 int weight = get(edge_weight, g, *eit);
119 put(edge_weight, g, pe.first, weight);
120 }
121
122#ifdef DEBUG_MISCOLORING
123 dynamic_properties dp;
124 dp.property("id", get(vertex_index, g));
125 dp.property("node_id", get(vertex_index, g));
126 dp.property("label", get(vertex_index, g));
127 dp.property("weight", get(edge_weight, g));
128 dp.property("label", get(edge_weight, g));
129 ofstream out ("graph_init.gv");
130 write_graphviz_dp(out, g, dp, string("id"));
131 out.close();
132 system("dot -Tpdf graph_init.gv -o graph_init.pdf");
133#endif
134
135 //test relaxed LP based coloring
137 // stitch is not supported
138 // THREE or FOUR
139 lc.color_num(limbo::algorithms::coloring::MISColoring<graph_type>::FOUR);
140 double cost = lc();
141 cout << "final cost = " << cost << endl;
142
143 in.close();
144}
145
151int main(int argc, char** argv)
152{
153 if (argc < 2)
154 {
155 randomGraph();
156 }
157 else realGraph(argv[1]);
158
159 return 0;
160}
assertion with message
#define limboAssert(condition)
custom assertion without message
Definition AssertMsg.h:36
virtual double stitch_weight() const
Definition Coloring.h:184
virtual void color_num(ColorNumType cn)
Definition Coloring.h:168
Boost.Geometry.
Definition GdsObjects.h:740
int main()
void randomGraph()
test 1: a random graph
void realGraph(string const &filename)