Dip  0.92.4
TSP_Concorde.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 TSP_CONCORDE_INCLUDED
16 #define TSP_CONCORDE_INCLUDED
17 
18 //---
19 //--- Interface class to Concorde (TSP solver).
20 //---
21 
22 // --------------------------------------------------------------------- //
23 extern "C"{
24 #include "TSP_ConcordeI.h"
25 }
26 
27 // --------------------------------------------------------------------- //
28 #include <vector>
29 using namespace std;
30 
31 // --------------------------------------------------------------------- //
32 #include "UtilMacros.h"
33 
34 // --------------------------------------------------------------------- //
36 public:
37  vector<int> S;
38  vector<bool> inS;
39 public:
40  void print(){
41  cout << "ConcordeSubtour: ";
42  for(int i = 0; i < static_cast<int>(inS.size()); i++){
43  if(inS[i]){
44  cout << i << " ";
45  }
46  }
47  cout << endl;
48  }
49  ConcordeSubtourCut(const int nVerts) :
50  S(),
51  inS(nVerts, false) {}
52 
54 };
55 
56 // --------------------------------------------------------------------- //
57 class TSP_Concorde {
58 public:
59  //define concorde subgraph
60  int m_nVerts;
61  vector<int> m_edgeList;
62  vector<double> m_edgeValue;
63 
64 public:
66  void clearSubGraph(){
67  m_nVerts = 0;
68  m_edgeList.clear();
69  m_edgeValue.clear();
70  }
71 
72  void buildSubGraph(const int nVerts,
73  const int nEdges,
74  const double * edgeWeight,
75  const double tol = 1.0e-6){
76 
77  int i;
78  clearSubGraph();
79  m_nVerts = nVerts;
80  m_edgeList.reserve(2*nEdges);
81  m_edgeValue.reserve(nEdges);
82  for(i = 0; i < nEdges; i++){
83  if(edgeWeight[i] <= tol)
84  continue;
85  pair<int,int> uv = UtilBothEndsU(i);
86  m_edgeValue.push_back(edgeWeight[i]);
87  m_edgeList.push_back(uv.first);
88  m_edgeList.push_back(uv.second);
89  }
90  }
91 
92  int generateCutsSubtour(vector<ConcordeSubtourCut> & subtourCuts){
93  CCtsp_lpcut_in * tsp_cuts = NULL;
94  CCtsp_lpcut_in * tsp_cut = NULL;
95  CCtsp_lpcut_in * tsp_cut_next = NULL;
96 
97  int i, c, tmp;
98  int n_subtour = 0;
99  int n_edgeList = static_cast<int>(m_edgeValue.size());
100  int retCode = CCtsp_exact_subtours(&tsp_cuts,
101  &n_subtour,
102  m_nVerts,
103  n_edgeList,
104  &m_edgeList[0],
105  &m_edgeValue[0]);
106  assert(!retCode);
107 
108  //cout << "CONCORDE found n_subtours : " << n_subtour << endl;
109 
110  tsp_cut = tsp_cuts;
111  for(c = 0; c < n_subtour; c++){
112  ConcordeSubtourCut subtourCut(m_nVerts);
113  assert(tsp_cut);
114  CC_FOREACH_NODE_IN_CLIQUE(i, tsp_cut->cliques[0], tmp){
115  subtourCut.S.push_back(i);
116  subtourCut.inS[i] = true;
117  }
118  //subtourCut.print();
119  subtourCuts.push_back(subtourCut);
120  tsp_cut = tsp_cut->next;
121  }
122 
123  for (tsp_cut = tsp_cuts; tsp_cut; tsp_cut = tsp_cut_next){
124  tsp_cut_next = tsp_cut->next;
125  CCtsp_free_lpcut_in(tsp_cut);
126  free(tsp_cut); //need UtilFree? for C style alloc/free?
127  }
128  tsp_cuts = NULL;
129  return static_cast<int>(subtourCuts.size());
130  }
131 
132 public:
134  m_nVerts (0),
135  m_edgeList (),
136  m_edgeValue()
137  {}
139  {}
140 };
141 
142 #endif
void clearSubGraph()
Definition: TSP_Concorde.h:66
void buildSubGraph(const int nVerts, const int nEdges, const double *edgeWeight, const double tol=1.0e-6)
Definition: TSP_Concorde.h:72
pair< int, int > UtilBothEndsU(const int index)
#define CC_FOREACH_NODE_IN_CLIQUE(i, c, tmp)
Definition: TSP_ConcordeI.h:49
ConcordeSubtourCut(const int nVerts)
Definition: TSP_Concorde.h:49
vector< double > m_edgeValue
Definition: TSP_Concorde.h:62
vector< int > S
Definition: TSP_Concorde.h:37
int generateCutsSubtour(vector< ConcordeSubtourCut > &subtourCuts)
Definition: TSP_Concorde.h:92
CCtsp_lpclique * cliques
Definition: TSP_ConcordeI.h:42
vector< bool > inS
Definition: TSP_Concorde.h:38
vector< int > m_edgeList
Definition: TSP_Concorde.h:61
void CCtsp_free_lpcut_in(CCtsp_lpcut_in *c)
struct CCtsp_lpcut_in * next
Definition: TSP_ConcordeI.h:45
int CCtsp_exact_subtours(CCtsp_lpcut_in **cuts, int *cutcount, int ncount, int ecount, int *elist, double *x)