Limbo 3.5.4
Loading...
Searching...
No Matches
test_LPColoring.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>
18// do not include these two together
20//#include <limbo/algorithms/coloring/LPColoringOld.h>
21#include <boost/graph/erdos_renyi_generator.hpp>
22#include <boost/random/mersenne_twister.hpp>
23#include <boost/graph/random.hpp>
24#include <boost/graph/iteration_macros.hpp>
25
26#include <boost/version.hpp>
27#if BOOST_VERSION <= 14601
28#include <boost/graph/detail/is_same.hpp>
29#else
30#include <boost/type_traits/is_same.hpp>
31#endif
32
33using std::cout;
34using std::endl;
35using std::ifstream;
36using std::ofstream;
37using std::string;
38using std::pair;
39using namespace boost;
40
42// do not use setS, it does not compile for subgraph
43// do not use custom property tags, it does not compile for most utilities
44typedef adjacency_list<vecS, vecS, undirectedS,
45 property<vertex_index_t, std::size_t, property<vertex_color_t, int> >,
46 property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
47 property<graph_name_t, string> > graph_type;
48typedef subgraph<graph_type> subgraph_type;
49typedef property<vertex_index_t, std::size_t> VertexId;
50typedef property<edge_index_t, std::size_t> EdgeID;
51typedef graph_traits<graph_type>::vertex_descriptor vertex_descriptor;
52typedef graph_traits<graph_type>::edge_descriptor edge_descriptor;
53typedef property_map<graph_type, edge_weight_t>::type edge_weight_map_type;
54typedef property_map<graph_type, vertex_color_t>::type vertex_color_map_type;
56
59double simple_graph()
60{
61 graph_type g;
62 vertex_descriptor a = boost::add_vertex(g);
63 vertex_descriptor b = boost::add_vertex(g);
64 vertex_descriptor c = boost::add_vertex(g);
65 vertex_descriptor d = boost::add_vertex(g);
66 vertex_descriptor e = boost::add_vertex(g);
67 boost::add_edge(a, b, g);
68 boost::add_edge(a, c, g);
69 boost::add_edge(a, d, g);
70 boost::add_edge(b, c, g);
71 boost::add_edge(b, d, g);
72 boost::add_edge(c, d, g);
73 boost::add_edge(a, e, g);
74 boost::add_edge(c, e, g);
75 boost::add_edge(d, e, g);
76
77 BOOST_AUTO(edge_weight_map, get(edge_weight, g));
78 graph_traits<graph_type>::edge_iterator eit, eit_end;
79 for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit)
80 {
81 edge_weight_map[*eit] = 1;
82 }
83
84 //test relaxed LP based coloring
86 // THREE or FOUR
87 lc.color_num(limbo::algorithms::coloring::LPColoring<graph_type>::THREE);
88 double cost = lc();
89 printf("solved in %u LP iterations\n", lc.lp_iters());
90 return cost;
91}
92
95double random_graph()
96{
97 mt19937 gen;
98 graph_type g;
99 int N = 40;
100 std::vector<vertex_descriptor> vertex_set;
101 std::vector< std::pair<vertex_descriptor, vertex_descriptor> > edge_set;
102 generate_random_graph(g, N, N * 2, gen,
103 std::back_inserter(vertex_set),
104 std::back_inserter(edge_set));
105 BOOST_AUTO(edge_weight_map, get(edge_weight, g));
106 unsigned int i = 0;
107 graph_traits<graph_type>::edge_iterator eit, eit_end;
108 for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit, ++i)
109 edge_weight_map[*eit] = 1;
110
111 //test relaxed LP based coloring
113 // THREE or FOUR
114 lc.color_num(limbo::algorithms::coloring::LPColoring<graph_type>::FOUR);
115 double cost = lc();
116 printf("solved in %u LP iterations\n", lc.lp_iters());
117 return cost;
118}
119
123double real_graph(string const& filename)
124{
125 ifstream in (filename.c_str());
126
127 // the graphviz reader in boost cannot specify vertex_index_t
128 // I have to create a temporary graph and then construct the real graph
129 typedef adjacency_list<vecS, vecS, undirectedS,
130 property<vertex_index_t, std::size_t, property<vertex_color_t, int, property<vertex_name_t, std::size_t> > >,
131 property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
132 property<graph_name_t, string> > tmp_graph_type;
133 tmp_graph_type tmpg;
134 dynamic_properties tmpdp;
135 tmpdp.property("node_id", get(vertex_name, tmpg));
136 tmpdp.property("label", get(vertex_name, tmpg));
137 tmpdp.property("weight", get(edge_weight, tmpg));
138 tmpdp.property("label", get(edge_weight, tmpg));
139 limboAssert(read_graphviz(in, tmpg, tmpdp, "node_id"));
140
141 // real graph
142 graph_type g (num_vertices(tmpg));
143 graph_traits<tmp_graph_type>::vertex_iterator vit, vit_end;
144 for (tie(vit, vit_end) = vertices(tmpg); vit != vit_end; ++vit)
145 {
146 size_t name = get(vertex_name, tmpg, *vit);
147 int color = get(vertex_color, tmpg, *vit);
148 put(vertex_color, g, (vertex_descriptor)name, color);
149 }
150
151 graph_traits<tmp_graph_type>::edge_iterator eit, eit_end;
152 for (tie(eit, eit_end) = edges(tmpg); eit != eit_end; ++eit)
153 {
154 size_t s_name = get(vertex_name, tmpg, source(*eit, tmpg));
155 size_t t_name = get(vertex_name, tmpg, target(*eit, tmpg));
156 pair<edge_descriptor, bool> pe = add_edge(s_name, t_name, g);
157 limboAssert(pe.second);
158 int weight = get(edge_weight, g, *eit);
159 put(edge_weight, g, pe.first, weight);
160 }
161
162#ifdef DEBUG_LPCOLORING
163 dynamic_properties dp;
164 dp.property("id", get(vertex_index, g));
165 dp.property("node_id", get(vertex_index, g));
166 dp.property("label", get(vertex_index, g));
167 dp.property("weight", get(edge_weight, g));
168 dp.property("label", get(edge_weight, g));
169 ofstream out ("graph_init.gv");
170 write_graphviz_dp(out, g, dp, string("id"));
171 out.close();
172 system("dot -Tpdf graph_init.gv -o graph_init.pdf");
173#endif
174
175 //test relaxed LP based coloring
177 // THREE or FOUR
178 lc.color_num(limbo::algorithms::coloring::LPColoring<graph_type>::THREE);
179 double cost = lc();
180 printf("solved in %u LP iterations\n", lc.lp_iters());
181
182 in.close();
183
184 return cost;
185}
186
192int main(int argc, char** argv)
193{
194 double cost;
195 if (argc < 2)
196 {
197 cost = simple_graph();
198 //cost = random_graph();
199 }
200 else cost = real_graph(argv[1]);
201
202 cout << "cost = " << cost << endl;
203
204 return 0;
205}
assertion with message
#define limboAssert(condition)
custom assertion without message
Definition AssertMsg.h:36
return chromatic number of a graph
coloring a graph with saturation degree based heuristics
coloring algorithm based on iterative linear programming (LP) and rounding
virtual void color_num(ColorNumType cn)
Definition Coloring.h:168
Boost.Geometry.
Definition GdsObjects.h:740
int main()
double simple_graph()
double real_graph(string const &filename)
double random_graph()