Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_SDPColoring.cpp
Go to the documentation of this file.
1 
8 #include <iostream>
9 #include <fstream>
10 #include <string>
11 #include <boost/graph/graphviz.hpp>
12 #include <boost/graph/graph_utility.hpp>
13 #include <boost/graph/adjacency_list.hpp>
14 #include <boost/graph/undirected_graph.hpp>
16 #include <boost/graph/erdos_renyi_generator.hpp>
17 #include <boost/random/mersenne_twister.hpp>
18 #include <boost/graph/random.hpp>
19 #include <boost/graph/iteration_macros.hpp>
20 
21 #include <boost/version.hpp>
22 #if BOOST_VERSION <= 14601
23 #include <boost/graph/detail/is_same.hpp>
24 #else
25 #include <boost/type_traits/is_same.hpp>
26 #endif
27 
28 using std::cout;
29 using std::endl;
30 using std::ifstream;
31 using std::ofstream;
32 using std::string;
33 using std::pair;
34 using namespace boost;
35 
37 // do not use setS, it does not compile for subgraph
38 // do not use custom property tags, it does not compile for most utilities
39 typedef adjacency_list<vecS, vecS, undirectedS,
40  property<vertex_index_t, std::size_t, property<vertex_color_t, int> >,
41  property<edge_index_t, std::size_t, property<edge_weight_t, double> >,
42  property<graph_name_t, string> > graph_type;
43 typedef subgraph<graph_type> subgraph_type;
44 typedef property<vertex_index_t, std::size_t> VertexId;
45 typedef property<edge_index_t, std::size_t> EdgeID;
46 typedef graph_traits<graph_type>::vertex_descriptor vertex_descriptor;
47 typedef graph_traits<graph_type>::edge_descriptor edge_descriptor;
48 typedef property_map<graph_type, edge_weight_t>::type edge_weight_map_type;
49 typedef property_map<graph_type, vertex_color_t>::type vertex_color_map_type;
51 
53 double simple_graph()
54 {
55  graph_type g;
56  vertex_descriptor a = boost::add_vertex(g);
57  vertex_descriptor b = boost::add_vertex(g);
58  vertex_descriptor c = boost::add_vertex(g);
59  vertex_descriptor d = boost::add_vertex(g);
60  vertex_descriptor e = boost::add_vertex(g);
61  boost::add_edge(a, b, g);
62  boost::add_edge(a, c, g);
63  boost::add_edge(a, d, g);
64  boost::add_edge(a, e, g);
65  boost::add_edge(b, c, g);
66  boost::add_edge(b, e, g);
67  boost::add_edge(c, d, g);
68  boost::add_edge(d, e, g);
69 
70  BOOST_AUTO(edge_weight_map, get(edge_weight, g));
71  graph_traits<graph_type>::edge_iterator eit, eit_end;
72  for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit)
73  {
74  if (source(*eit, g) != a || target(*eit, g) != d)
75  edge_weight_map[*eit] = 1;
76  else
77  edge_weight_map[*eit] = -1;
78  }
79 
80  //test relaxed LP based coloring
82  lc.stitch_weight(0.1);
83  // THREE or FOUR
85  return lc();
86 }
87 
89 double random_graph()
90 {
91  mt19937 gen;
92  graph_type g;
93  int N = 40;
94  std::vector<vertex_descriptor> vertex_set;
95  std::vector< std::pair<vertex_descriptor, vertex_descriptor> > edge_set;
96  generate_random_graph(g, N, N * 2, gen,
97  std::back_inserter(vertex_set),
98  std::back_inserter(edge_set));
99  BOOST_AUTO(edge_weight_map, get(edge_weight, g));
100  unsigned int i = 0;
101  graph_traits<graph_type>::edge_iterator eit, eit_end;
102  for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit, ++i)
103  {
104 #if 1
105  if (i%10 == 0) // generate stitch
106  edge_weight_map[*eit] = -1;
107  else // generate conflict
108 #endif
109  edge_weight_map[*eit] = 1;
110  }
111 
112  //test relaxed LP based coloring
114  lc.stitch_weight(0.1);
115  // THREE or FOUR
117  return lc();
118 }
119 
122 double real_graph(string const& filename)
123 {
124  ifstream in (filename.c_str());
125 
126  // the graphviz reader in boost cannot specify vertex_index_t
127  // I have to create a temporary graph and then construct the real graph
128  typedef adjacency_list<vecS, vecS, undirectedS,
129  property<vertex_index_t, std::size_t, property<vertex_color_t, int, property<vertex_name_t, std::size_t> > >,
130  property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
131  property<graph_name_t, string> > tmp_graph_type;
132  tmp_graph_type tmpg;
133  dynamic_properties tmpdp;
134  tmpdp.property("node_id", get(vertex_name, tmpg));
135  tmpdp.property("label", get(vertex_name, tmpg));
136  tmpdp.property("weight", get(edge_weight, tmpg));
137  tmpdp.property("label", get(edge_weight, tmpg));
138  assert(read_graphviz(in, tmpg, tmpdp, "node_id"));
139 
140  // real graph
141  graph_type g (num_vertices(tmpg));
142  graph_traits<tmp_graph_type>::vertex_iterator vit, vit_end;
143  for (tie(vit, vit_end) = vertices(tmpg); vit != vit_end; ++vit)
144  {
145  size_t name = get(vertex_name, tmpg, *vit);
146  int color = get(vertex_color, tmpg, *vit);
147  put(vertex_color, g, (vertex_descriptor)name, color);
148  }
149 
150  graph_traits<tmp_graph_type>::edge_iterator eit, eit_end;
151  for (tie(eit, eit_end) = edges(tmpg); eit != eit_end; ++eit)
152  {
153  size_t s_name = get(vertex_name, tmpg, source(*eit, tmpg));
154  size_t t_name = get(vertex_name, tmpg, target(*eit, tmpg));
155  pair<edge_descriptor, bool> pe = add_edge(s_name, t_name, g);
156  assert(pe.second);
157  int weight = get(edge_weight, g, *eit);
158  put(edge_weight, g, pe.first, weight);
159  }
160 
161 #ifdef DEBUG_SDPCOLORING
162  dynamic_properties dp;
163  dp.property("id", get(vertex_index, g));
164  dp.property("node_id", get(vertex_index, g));
165  dp.property("label", get(vertex_index, g));
166  dp.property("weight", get(edge_weight, g));
167  dp.property("label", get(edge_weight, g));
168  ofstream out ("graph_init.gv");
169  write_graphviz_dp(out, g, dp, string("id"));
170  out.close();
171  system("dot -Tpdf graph_init.gv -o graph_init.pdf");
172 #endif
173 
174  //test relaxed LP based coloring
176  lc.stitch_weight(0.1);
177  // THREE or FOUR
179  double cost = lc();
180 
181  in.close();
182  return cost;
183 }
184 
189 int main(int argc, char** argv)
190 {
191  double cost;
192  if (argc < 2)
193  {
194  cost = simple_graph();
195  //cost = random_graph();
196  }
197  else cost = real_graph(argv[1]);
198 
199  cout << "cost = " << cost << endl;
200 
201  return 0;
202 }
int main(int argc, char **argv)
Boost.Geometry.
Definition: GdsObjects.h:724
double real_graph(string const &filename)
double simple_graph()
test 1: a simple graph
virtual double stitch_weight() const
Definition: Coloring.h:181
graph coloring algorithm based on semidefinite programming (SDP)
virtual void color_num(ColorNumType cn)
Definition: Coloring.h:165
double random_graph()
test 2: a random graph