Dip  0.92.4
VRP_Boost.h
Go to the documentation of this file.
1 //===========================================================================//
2 // This file is part of the Decomp Solver Framework. //
3 // //
4 // Decomp is distributed under the Common Public License as part of the //
5 // COIN-OR repository (http://www.coin-or.org). //
6 // //
7 // Authors: Matthew Galati, SAS Institute Inc. (matthew.galati@sas.com) //
8 // Ted Ralphs, Lehigh University (ted@lehigh.edu) //
9 // Jiadong Wang, Lehigh University (jiw408@lehigh.edu) //
10 // //
11 // Copyright (C) 2002-2019, Lehigh University, Matthew Galati, and Ted Ralphs//
12 // All Rights Reserved. //
13 //===========================================================================//
14 
15 #ifndef VRP_BOOST_INCLUDED
16 #define VRP_BOOST_INCLUDED
17 
18 //---
19 //--- Interface class to Boost/Graph (an open source graph library).
20 //---
21 
22 //THINK design:
23 //design wise there should be a class that just wraps boost algos
24 //then one that carries boost data specific to your application
25 
26 // --------------------------------------------------------------------- //
27 #include <boost/graph/graphviz.hpp>
28 #include <boost/graph/graph_utility.hpp>
29 #include <boost/graph/adjacency_list.hpp>
30 #include <boost/graph/connected_components.hpp>
31 #include <boost/graph/kruskal_min_spanning_tree.hpp>
32 using namespace boost;
33 using namespace std;
34 
35 typedef property<edge_weight_t, double,
36  property<edge_index_t, int> > edge_prop;
37 typedef adjacency_list<vecS, vecS, undirectedS,
38  no_property, edge_prop> Graph;
39 
40 // --------------------------------------------------------------------- //
41 //---
42 //--- utility classes for writing to graphviz format (edges)
43 //--- TODO: convert double to closest fraction?
44 //---
45 template < class EdgeWeight >
47 public:
48  sg_label_writer(EdgeWeight e_) : weight(e_) {}
49  template <class VertexOrEdge>
50  void operator()(ostream & out, const VertexOrEdge & e) const {
51  if(weight[e] >= 0.9999999)
52  out << "[style=solid]";
53  else
54  out << "[label=\"" << weight[e] << "\", stype=dashed]";
55  }
56 private:
57  EdgeWeight weight;
58 };
59 template <class EdgeWeight >
62 }
63 
64 
65 // --------------------------------------------------------------------- //
66 //---
67 //--- utility classes for writing to graphviz format (vertices)
68 //--- TODO: convert double to closest fraction?
69 //---
71 public:
72  sg_label_writer_vertex(const int * vertex_wt)
73  : demand(vertex_wt) {}
74  template <class VertexOrEdge>
75  void operator()(ostream & out, const VertexOrEdge & v) const {
76  if(v == 0)
77  out << " [style=filled, fillcolor=black]";
78  else{
79  out << " [label=\"" << v << " : "
80  << demand[v] << "\" width=\"0.5\" height=\"0.5\"]";
81  }
82  }
83 private:
84  const int * demand;
85 };
87 sg_make_label_writer_vertex(const int * vertex_wt) {
88  return sg_label_writer_vertex(vertex_wt);
89 }
90 
91 // --------------------------------------------------------------------- //
92 class VRP_Boost {
93 public:
94  Graph m_sg; //for storage of support graph
95  Graph m_sg0; //for storage of support graph minus depot
96  double m_depotDegree; //degree at depot (since we can have single-customers)
97 
98 public:
100  m_sg (),
101  m_sg0 (),
102  m_depotDegree(0.0)
103  {}
105 
106 public:
108  void buildSubGraph(const int len,
109  const double * val,
110  const double tol = 1.0e-6){
111 
112  //---
113  //--- build the boost subgraph as the support graph for x=val
114  //--- at the same time, calculate the depot degree since we can
115  //--- have single-customer routes with x=2.0
116  //---
117  property_map<Graph, edge_weight_t>::type e_weight
118  = get(edge_weight, m_sg);
119  property_map<Graph, edge_index_t>::type e_index
120  = get(edge_index, m_sg);
121  graph_traits<Graph>::edge_descriptor ed; bool inserted;
122 
123  int c;
124  m_depotDegree = 0.0;
125  for (c = 0; c < len; ++c) {
126  if(val[c] <= tol)
127  continue;
128  pair<int,int> uv = UtilBothEndsU(c);
129 
130  if(uv.first == 0 || uv.second == 0){
131  m_depotDegree += val[c];
132  }
133 
134  tie(ed, inserted) = add_edge(uv.first, uv.second, m_sg);
135  e_weight[ed] = val[c];
136  e_index[ed] = c;
137  }
138  }
139 
140  /*
141  void buildCompleteGraphMinusVert(const int vert,
142  const int nVerts){
143 
144  property_map<Graph, edge_index_t>::type e_index
145  = get(edge_index, m_cgV);
146  graph_traits<Graph>::edge_descriptor ed; bool inserted;
147  int u, v;
148  int index = 0;
149  for(u = 1; u < nVerts; u++){
150  for(v = 0; v < u; v++){
151  if(u != vert && v != vert){
152  tie(ed, inserted) = add_edge(u, v, m_cgV);
153  e_index[ed] = index;
154  }
155  index++;
156  }
157  }
158  }
159  */
160 
161  inline void printGraph(const Graph & g) const {
162  print_graph(g);
163  }
164 
165  inline void printDotFile(const string & fileName,
166  const int * vertexWt,
167  const Graph & g) const {
168 
169  ofstream os;
170  UtilOpenFile(os, fileName);
171 
172  printf("PrintDotFile fileName = %s\n", fileName.c_str());
173 
174  map<string, string> graph_attr, vertex_attr, edge_attr;
175  graph_attr ["fontsize"] = "20";
176 
177  vertex_attr["fontsize"] = "20";
178  vertex_attr["shape"] = "circle";
179 
180  edge_attr ["style"] = "dotted";
181  edge_attr ["len"] = "1.7";
182  edge_attr ["fontsize"] = "20";
183 
184  //property_map<Graph, edge_weight_t>::type e_weight
185  // = get(edge_weight, g);
186 
187  write_graphviz(os, g,
188  sg_make_label_writer_vertex(vertexWt),
189  sg_make_label_writer(get(edge_weight, g)),
190  make_graph_attributes_writer(graph_attr,
191  vertex_attr,
192  edge_attr));
193  os.close();
194  }
195 
196  inline void copyGraph(const Graph & gFrom,
197  Graph & gTo){
198  gTo = gFrom;
199  }
200 
201  inline void clearSubGraph(){
202  m_sg.clear();
203  }
204 
205  inline void clearSubGraph(Graph & g){
206  g.clear();
207  }
208 
209  inline void clearVertex(const int vertex,
210  Graph & g){
211  clear_vertex(0, g);
212  }
213 
214  inline int findConnectedComponents(vector<int> & component){
215  return connected_components(m_sg, &component[0]);
216  }
217 
219  vector<int> & component){
220  return connected_components(g, &component[0]);
221  }
222 
223  inline int getDegree(const int nodeIndex){
224  return degree(nodeIndex, m_sg);
225  }
226 
227  inline double getDepotDegree(){
228  return m_depotDegree;
229  }
230 
231 };
232 
233 #endif
void printGraph(const Graph &g) const
Definition: VRP_Boost.h:161
sg_label_writer< EdgeWeight > sg_make_label_writer(EdgeWeight e)
Definition: VRP_Boost.h:60
void clearSubGraph()
Definition: VRP_Boost.h:201
adjacency_list< vecS, vecS, undirectedS, no_property, edge_prop > Graph
Definition: TSP_Boost.h:36
Definition: network.h:62
const int * demand
Definition: VRP_Boost.h:84
void buildSubGraph(const int len, const double *val, const double tol=1.0e-6)
Definition: VRP_Boost.h:108
pair< int, int > UtilBothEndsU(const int index)
EdgeWeight weight
Definition: VRP_Boost.h:57
void printDotFile(const string &fileName, const int *vertexWt, const Graph &g) const
Definition: VRP_Boost.h:165
VRP_Boost()
Definition: VRP_Boost.h:99
int findConnectedComponents(vector< int > &component)
Definition: VRP_Boost.h:214
void copyGraph(const Graph &gFrom, Graph &gTo)
Definition: VRP_Boost.h:196
void operator()(ostream &out, const VertexOrEdge &e) const
Definition: VRP_Boost.h:50
void UtilOpenFile(ifstream &fs, const char *fileName)
Definition: UtilMacros.h:526
sg_label_writer_vertex sg_make_label_writer_vertex(const int *vertex_wt)
Definition: VRP_Boost.h:87
double getDepotDegree()
Definition: VRP_Boost.h:227
Graph m_sg
Definition: VRP_Boost.h:94
sg_label_writer_vertex(const int *vertex_wt)
Definition: VRP_Boost.h:72
property< edge_weight_t, double, property< edge_index_t, int > > edge_prop
Definition: TSP_Boost.h:34
int findConnectedComponents(Graph &g, vector< int > &component)
Definition: VRP_Boost.h:218
double m_depotDegree
Definition: VRP_Boost.h:96
~VRP_Boost()
Definition: VRP_Boost.h:104
void clearSubGraph(Graph &g)
Definition: VRP_Boost.h:205
void clearVertex(const int vertex, Graph &g)
Definition: VRP_Boost.h:209
Graph m_sg0
Definition: VRP_Boost.h:95
sg_label_writer(EdgeWeight e_)
Definition: VRP_Boost.h:48
void operator()(ostream &out, const VertexOrEdge &v) const
Definition: VRP_Boost.h:75
int getDegree(const int nodeIndex)
Definition: VRP_Boost.h:223