Limbo 3.5.4
Loading...
Searching...
No Matches
test_SDPColoring.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, double> >,
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
54double simple_graph()
55{
56 graph_type g;
57 vertex_descriptor a = boost::add_vertex(g);
58 vertex_descriptor b = boost::add_vertex(g);
59 vertex_descriptor c = boost::add_vertex(g);
60 vertex_descriptor d = boost::add_vertex(g);
61 vertex_descriptor e = boost::add_vertex(g);
62 boost::add_edge(a, b, g);
63 boost::add_edge(a, c, g);
64 boost::add_edge(a, d, g);
65 boost::add_edge(a, e, g);
66 boost::add_edge(b, c, g);
67 boost::add_edge(b, e, g);
68 boost::add_edge(c, d, g);
69 boost::add_edge(d, e, g);
70
71 BOOST_AUTO(edge_weight_map, get(edge_weight, g));
72 graph_traits<graph_type>::edge_iterator eit, eit_end;
73 for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit)
74 {
75 if (source(*eit, g) != a || target(*eit, g) != d)
76 edge_weight_map[*eit] = 1;
77 else
78 edge_weight_map[*eit] = -1;
79 }
80
81 //test relaxed LP based coloring
83 lc.stitch_weight(0.1);
84 // THREE or FOUR
85 lc.color_num(limbo::algorithms::coloring::SDPColoringCsdp<graph_type>::THREE);
86 return lc();
87}
88
90double random_graph()
91{
92 mt19937 gen;
93 graph_type g;
94 int N = 40;
95 std::vector<vertex_descriptor> vertex_set;
96 std::vector< std::pair<vertex_descriptor, vertex_descriptor> > edge_set;
97 generate_random_graph(g, N, N * 2, gen,
98 std::back_inserter(vertex_set),
99 std::back_inserter(edge_set));
100 BOOST_AUTO(edge_weight_map, get(edge_weight, g));
101 unsigned int i = 0;
102 graph_traits<graph_type>::edge_iterator eit, eit_end;
103 for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit, ++i)
104 {
105#if 1
106 if (i%10 == 0) // generate stitch
107 edge_weight_map[*eit] = -1;
108 else // generate conflict
109#endif
110 edge_weight_map[*eit] = 1;
111 }
112
113 //test relaxed LP based coloring
115 lc.stitch_weight(0.1);
116 // THREE or FOUR
117 lc.color_num(limbo::algorithms::coloring::SDPColoringCsdp<graph_type>::THREE);
118 return lc();
119}
120
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 std::cout << "read_graphviz\n";
140 limboAssert(read_graphviz(in, tmpg, tmpdp, "node_id"));
141
142 // real graph
143 graph_type g (num_vertices(tmpg));
144 graph_traits<tmp_graph_type>::vertex_iterator vit, vit_end;
145 for (tie(vit, vit_end) = vertices(tmpg); vit != vit_end; ++vit)
146 {
147 size_t name = get(vertex_name, tmpg, *vit);
148 int color = get(vertex_color, tmpg, *vit);
149 put(vertex_color, g, (vertex_descriptor)name, color);
150 }
151
152 graph_traits<tmp_graph_type>::edge_iterator eit, eit_end;
153 for (tie(eit, eit_end) = edges(tmpg); eit != eit_end; ++eit)
154 {
155 size_t s_name = get(vertex_name, tmpg, source(*eit, tmpg));
156 size_t t_name = get(vertex_name, tmpg, target(*eit, tmpg));
157 pair<edge_descriptor, bool> pe = add_edge(s_name, t_name, g);
158 limboAssert(pe.second);
159 int weight = get(edge_weight, g, *eit);
160 put(edge_weight, g, pe.first, weight);
161 }
162
163#ifdef DEBUG_SDPCOLORING
164 dynamic_properties dp;
165 dp.property("id", get(vertex_index, g));
166 dp.property("node_id", get(vertex_index, g));
167 dp.property("label", get(vertex_index, g));
168 dp.property("weight", get(edge_weight, g));
169 dp.property("label", get(edge_weight, g));
170 ofstream out ("graph_init.gv");
171 write_graphviz_dp(out, g, dp, string("id"));
172 out.close();
173 system("dot -Tpdf graph_init.gv -o graph_init.pdf");
174#endif
175
176 //test relaxed LP based coloring
178 lc.stitch_weight(0.1);
179 // THREE or FOUR
180 lc.color_num(limbo::algorithms::coloring::SDPColoringCsdp<graph_type>::THREE);
181 double cost = lc();
182
183 in.close();
184 return cost;
185}
186
191int main(int argc, char** argv)
192{
193 double cost;
194 if (argc < 2)
195 {
196 cost = simple_graph();
197 //cost = random_graph();
198 }
199 else cost = real_graph(argv[1]);
200
201 cout << "cost = " << cost << endl;
202
203 return 0;
204}
assertion with message
#define limboAssert(condition)
custom assertion without message
Definition AssertMsg.h:36
graph coloring algorithm based on semidefinite programming (SDP)
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()
double simple_graph()
test 1: a simple graph
double real_graph(string const &filename)
double random_graph()
test 2: a random graph