Limbo 3.5.4
Loading...
Searching...
No Matches
test_ILPColoring.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, int> >,
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
55{
56 mt19937 gen;
57 graph_type g;
58 int N = 40;
59 std::vector<vertex_descriptor> vertex_set;
60 std::vector< std::pair<vertex_descriptor, vertex_descriptor> > edge_set;
61 generate_random_graph(g, N, N * 2, gen,
62 std::back_inserter(vertex_set),
63 std::back_inserter(edge_set));
64 BOOST_AUTO(edge_weight_map, get(edge_weight, g));
65 unsigned int i = 0;
66 graph_traits<graph_type>::edge_iterator eit, eit_end;
67 for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit, ++i)
68 {
69#if 1
70 if (i%10 == 0) // generate stitch
71 edge_weight_map[*eit] = -1;
72 else // generate conflict
73#endif
74 edge_weight_map[*eit] = 1;
75 }
76
77 //test relaxed LP based coloring
79 lc.stitch_weight(0.1);
80 // THREE or FOUR
81 lc.color_num(limbo::algorithms::coloring::ILPColoring<graph_type>::THREE);
82 double cost = lc();
83 cout << "final cost = " << cost << endl;
84}
85
88void realGraph(string const& filename)
89{
90 ifstream in (filename.c_str());
91
92 // the graphviz reader in boost cannot specify vertex_index_t
93 // I have to create a temporary graph and then construct the real graph
94 typedef adjacency_list<vecS, vecS, undirectedS,
95 property<vertex_index_t, std::size_t, property<vertex_color_t, int, property<vertex_name_t, std::size_t> > >,
96 property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
97 property<graph_name_t, string> > tmp_graph_type;
98 tmp_graph_type tmpg;
99 dynamic_properties tmpdp;
100 tmpdp.property("node_id", get(vertex_name, tmpg));
101 tmpdp.property("label", get(vertex_name, tmpg));
102 tmpdp.property("weight", get(edge_weight, tmpg));
103 tmpdp.property("label", get(edge_weight, tmpg));
104 limboAssert(read_graphviz(in, tmpg, tmpdp, "node_id"));
105
106 // real graph
107 graph_type g (num_vertices(tmpg));
108 graph_traits<tmp_graph_type>::vertex_iterator vit, vit_end;
109 for (tie(vit, vit_end) = vertices(tmpg); vit != vit_end; ++vit)
110 {
111 size_t name = get(vertex_name, tmpg, *vit);
112 int color = get(vertex_color, tmpg, *vit);
113 put(vertex_color, g, (vertex_descriptor)name, color);
114 }
115
116 graph_traits<tmp_graph_type>::edge_iterator eit, eit_end;
117 for (tie(eit, eit_end) = edges(tmpg); eit != eit_end; ++eit)
118 {
119 size_t s_name = get(vertex_name, tmpg, source(*eit, tmpg));
120 size_t t_name = get(vertex_name, tmpg, target(*eit, tmpg));
121 pair<edge_descriptor, bool> pe = add_edge(s_name, t_name, g);
122 limboAssert(pe.second);
123 int weight = get(edge_weight, g, *eit);
124 put(edge_weight, g, pe.first, weight);
125 }
126
127#ifdef DEBUG_LPCOLORING
128 dynamic_properties dp;
129 dp.property("id", get(vertex_index, g));
130 dp.property("node_id", get(vertex_index, g));
131 dp.property("label", get(vertex_index, g));
132 dp.property("weight", get(edge_weight, g));
133 dp.property("label", get(edge_weight, g));
134 ofstream out ("graph_init.gv");
135 write_graphviz_dp(out, g, dp, string("id"));
136 out.close();
137 system("dot -Tpdf graph_init.gv -o graph_init.pdf");
138#endif
139
140 //test relaxed LP based coloring
142 lc.stitch_weight(0.1);
143 // THREE or FOUR
144 lc.color_num(limbo::algorithms::coloring::ILPColoring<graph_type>::THREE);
145 double cost = lc();
146 cout << "final cost = " << cost << endl;
147
148 in.close();
149}
150
156int main(int argc, char** argv)
157{
158 if (argc < 2)
159 {
160 randomGraph();
161 }
162 else realGraph(argv[1]);
163
164 return 0;
165}
assertion with message
#define limboAssert(condition)
custom assertion without message
Definition AssertMsg.h:36
coloring algorithm based on integer linear programming (ILP) with Gurobi as ILP solver.
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()
void randomGraph()
test 1: a random graph
void realGraph(string const &filename)