Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_MISColoring.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, int> >,
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 void randomGraph()
54 {
55  mt19937 gen;
56  graph_type g;
57  int N = 40;
58  std::vector<vertex_descriptor> vertex_set;
59  std::vector< std::pair<vertex_descriptor, vertex_descriptor> > edge_set;
60  generate_random_graph(g, N, N * 2, gen,
61  std::back_inserter(vertex_set),
62  std::back_inserter(edge_set));
63  BOOST_AUTO(edge_weight_map, get(edge_weight, g));
64  unsigned int i = 0;
65  graph_traits<graph_type>::edge_iterator eit, eit_end;
66  for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit, ++i)
67  {
68  edge_weight_map[*eit] = 1;
69  }
70 
71  //test relaxed LP based coloring
73  lc.stitch_weight(0.1);
74  // THREE or FOUR
76  double cost = lc();
77  cout << "final cost = " << cost << endl;
78 }
79 
82 void realGraph(string const& filename)
83 {
84  ifstream in (filename.c_str());
85 
86  // the graphviz reader in boost cannot specify vertex_index_t
87  // I have to create a temporary graph and then construct the real graph
88  typedef adjacency_list<vecS, vecS, undirectedS,
89  property<vertex_index_t, std::size_t, property<vertex_color_t, int, property<vertex_name_t, std::size_t> > >,
90  property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
91  property<graph_name_t, string> > tmp_graph_type;
92  tmp_graph_type tmpg;
93  dynamic_properties tmpdp;
94  tmpdp.property("node_id", get(vertex_name, tmpg));
95  tmpdp.property("label", get(vertex_name, tmpg));
96  tmpdp.property("weight", get(edge_weight, tmpg));
97  tmpdp.property("label", get(edge_weight, tmpg));
98  assert(read_graphviz(in, tmpg, tmpdp, "node_id"));
99 
100  // real graph
101  graph_type g (num_vertices(tmpg));
102  graph_traits<tmp_graph_type>::vertex_iterator vit, vit_end;
103  for (tie(vit, vit_end) = vertices(tmpg); vit != vit_end; ++vit)
104  {
105  size_t name = get(vertex_name, tmpg, *vit);
106  int color = get(vertex_color, tmpg, *vit);
107  put(vertex_color, g, (vertex_descriptor)name, color);
108  }
109 
110  graph_traits<tmp_graph_type>::edge_iterator eit, eit_end;
111  for (tie(eit, eit_end) = edges(tmpg); eit != eit_end; ++eit)
112  {
113  size_t s_name = get(vertex_name, tmpg, source(*eit, tmpg));
114  size_t t_name = get(vertex_name, tmpg, target(*eit, tmpg));
115  pair<edge_descriptor, bool> pe = add_edge(s_name, t_name, g);
116  assert(pe.second);
117  int weight = get(edge_weight, g, *eit);
118  put(edge_weight, g, pe.first, weight);
119  }
120 
121 #ifdef DEBUG_MISCOLORING
122  dynamic_properties dp;
123  dp.property("id", get(vertex_index, g));
124  dp.property("node_id", get(vertex_index, g));
125  dp.property("label", get(vertex_index, g));
126  dp.property("weight", get(edge_weight, g));
127  dp.property("label", get(edge_weight, g));
128  ofstream out ("graph_init.gv");
129  write_graphviz_dp(out, g, dp, string("id"));
130  out.close();
131  system("dot -Tpdf graph_init.gv -o graph_init.pdf");
132 #endif
133 
134  //test relaxed LP based coloring
136  // stitch is not supported
137  // THREE or FOUR
139  double cost = lc();
140  cout << "final cost = " << cost << endl;
141 
142  in.close();
143 }
144 
150 int main(int argc, char** argv)
151 {
152  if (argc < 2)
153  {
154  randomGraph();
155  }
156  else realGraph(argv[1]);
157 
158  return 0;
159 }
int main(int argc, char **argv)
Boost.Geometry.
Definition: GdsObjects.h:724
virtual double stitch_weight() const
Definition: Coloring.h:181
void randomGraph()
test 1: a random graph
void realGraph(string const &filename)
virtual void color_num(ColorNumType cn)
Definition: Coloring.h:165