Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_ILPColoring.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 #if 1
69  if (i%10 == 0) // generate stitch
70  edge_weight_map[*eit] = -1;
71  else // generate conflict
72 #endif
73  edge_weight_map[*eit] = 1;
74  }
75 
76  //test relaxed LP based coloring
78  lc.stitch_weight(0.1);
79  // THREE or FOUR
81  double cost = lc();
82  cout << "final cost = " << cost << endl;
83 }
84 
87 void realGraph(string const& filename)
88 {
89  ifstream in (filename.c_str());
90 
91  // the graphviz reader in boost cannot specify vertex_index_t
92  // I have to create a temporary graph and then construct the real graph
93  typedef adjacency_list<vecS, vecS, undirectedS,
94  property<vertex_index_t, std::size_t, property<vertex_color_t, int, property<vertex_name_t, std::size_t> > >,
95  property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
96  property<graph_name_t, string> > tmp_graph_type;
97  tmp_graph_type tmpg;
98  dynamic_properties tmpdp;
99  tmpdp.property("node_id", get(vertex_name, tmpg));
100  tmpdp.property("label", get(vertex_name, tmpg));
101  tmpdp.property("weight", get(edge_weight, tmpg));
102  tmpdp.property("label", get(edge_weight, tmpg));
103  assert(read_graphviz(in, tmpg, tmpdp, "node_id"));
104 
105  // real graph
106  graph_type g (num_vertices(tmpg));
107  graph_traits<tmp_graph_type>::vertex_iterator vit, vit_end;
108  for (tie(vit, vit_end) = vertices(tmpg); vit != vit_end; ++vit)
109  {
110  size_t name = get(vertex_name, tmpg, *vit);
111  int color = get(vertex_color, tmpg, *vit);
112  put(vertex_color, g, (vertex_descriptor)name, color);
113  }
114 
115  graph_traits<tmp_graph_type>::edge_iterator eit, eit_end;
116  for (tie(eit, eit_end) = edges(tmpg); eit != eit_end; ++eit)
117  {
118  size_t s_name = get(vertex_name, tmpg, source(*eit, tmpg));
119  size_t t_name = get(vertex_name, tmpg, target(*eit, tmpg));
120  pair<edge_descriptor, bool> pe = add_edge(s_name, t_name, g);
121  assert(pe.second);
122  int weight = get(edge_weight, g, *eit);
123  put(edge_weight, g, pe.first, weight);
124  }
125 
126 #ifdef DEBUG_LPCOLORING
127  dynamic_properties dp;
128  dp.property("id", get(vertex_index, g));
129  dp.property("node_id", get(vertex_index, g));
130  dp.property("label", get(vertex_index, g));
131  dp.property("weight", get(edge_weight, g));
132  dp.property("label", get(edge_weight, g));
133  ofstream out ("graph_init.gv");
134  write_graphviz_dp(out, g, dp, string("id"));
135  out.close();
136  system("dot -Tpdf graph_init.gv -o graph_init.pdf");
137 #endif
138 
139  //test relaxed LP based coloring
141  lc.stitch_weight(0.1);
142  // THREE or FOUR
144  double cost = lc();
145  cout << "final cost = " << cost << endl;
146 
147  in.close();
148 }
149 
155 int main(int argc, char** argv)
156 {
157  if (argc < 2)
158  {
159  randomGraph();
160  }
161  else realGraph(argv[1]);
162 
163  return 0;
164 }
Boost.Geometry.
Definition: GdsObjects.h:724
void randomGraph()
test 1: a random graph
coloring algorithm based on integer linear programming (ILP) with Gurobi as ILP solver.
virtual double stitch_weight() const
Definition: Coloring.h:181
virtual void color_num(ColorNumType cn)
Definition: Coloring.h:165
int main(int argc, char **argv)
void realGraph(string const &filename)