Dip  0.92.4
MAD_Cliquer.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 MAD_CLIQUER_INCLUDED
16 #define MAD_CLIQUER_INCLUDED
17 
18 //---
19 //--- The MAD Cliquer -- it sounds like a college football mascot.
20 //---
21 //--- Ironic, since my college baseball team's mascot was the
22 //--- "MAD Hatter" (Stetson University '94-'98). They use to pay
23 //--- a guy to get drunk and run around the games dresesd as a
24 //--- a big cowboy hat. We were the coolest team in the league.
25 //---
26 
27 // --------------------------------------------------------------------- //
28 extern "C"{
29 #include "MAD_CliquerI.h"
30 }
31 
32 // --------------------------------------------------------------------- //
34  graph_t * g,
35  clique_options * opts) {
36 
37  //---
38  //--- make a copy of clique and append to clique_list
39  //---
40  int * clique_countPtr = (int*) opts->user_data;
41  if ((*clique_countPtr) >= opts->clique_list_length) {
42  opts->clique_list
43  = (set_t*) realloc(opts->clique_list,
44  (opts->clique_list_length+512)*sizeof(set_t));
45  opts->clique_list_length += 512;
46  }
47  opts->clique_list[*clique_countPtr] = set_duplicate(s);
48  (*clique_countPtr)++;
49  return 1;
50 }
51 
52 // --------------------------------------------------------------------- //
53 static int set_clique_func(set_t s,
54  graph_t * g,
55  clique_options * opts) {
56 
57  //---
58  //--- append clique to clique_list
59  //---
60 
61  //---
62  //--- store clique_list_size in user_data
63  //---
64  int * clique_countPtr = (int*) opts->user_data;
65  if ((*clique_countPtr) >= opts->clique_list_length) {
66  opts->clique_list
67  = (set_t*) realloc(opts->clique_list,
68  (opts->clique_list_length+512)*sizeof(set_t));
69  opts->clique_list_length += 512;
70  }
71  opts->clique_list[*clique_countPtr] = s;
72  (*clique_countPtr)++;
73  return 1;
74 }
75 
76 
77 // --------------------------------------------------------------------- //
79  public:
83 
85 
86  public:
87  inline graph_t * graphNew(const int n_verts){
88  return graph_new(n_verts);
89  }
90 
91  inline void graphFree(graph_t * g){
92  graph_free(g);
93  }
94 
95  inline void copyGraphNonPos(graph_t * gDest,
96  const graph_t * gSource,
97  const double * weights){
98  int i,j;
99  CoinAssert(gSource->n == gDest->n);
100  for(i = 0; i < gSource->n; i++) {
101  if(weights[i] < -DecompEpsilon){
102  for (j = 0; j < gSource->n; j++) {
103  if(weights[j] < -DecompEpsilon){
104  if (SET_CONTAINS_FAST(gSource->edges[i], j)) {
105  addEdge(gDest, i, j);
106  }
107  }
108  }
109  }
110  }
111  }
112 
113  inline void addEdge(graph_t * g,
114  const int i,
115  const int j){
116  GRAPH_ADD_EDGE(g, i, j);
117  }
118  inline void delEdge(graph_t * g,
119  const int i,
120  const int j){
121  GRAPH_DEL_EDGE(g, i, j);
122  }
123  inline int getNumVertices(graph_t * g){
124  return g->n;
125  }
126  inline int * getVertWeight(graph_t * g){
127  return g->weights;
128  }
129  inline void setVertWeight(graph_t * g,
130  const int * vertWeight){
131  memcpy(g->weights, vertWeight, g->n * sizeof(int));
132  }
133 
134  inline void cliqueFindAll(graph_t * g,
135  int minWeight,
136  int maxWeight,
137  int maximal){
138  clique_find_all(g, minWeight, maxWeight, maximal, m_copts);
139  m_clique_count = *(int*)m_copts->user_data;
140  }
141 
143  int minWeight,
144  int maxWeight,
145  int maximal){
146  clique_unweighted_find_all(g, minWeight, maxWeight, maximal, m_copts);
147  m_clique_count = *(int*)m_copts->user_data;
148  }
149 
150  inline void cliqueFindOne(graph_t * g,
151  int minWeight,
152  int maxWeight,
153  int maximal){
154  set_t s = clique_find_single(g, minWeight, maxWeight, maximal, m_copts);
155  set_clique_func(s, g, m_copts);
156  m_clique_count = *(int*)m_copts->user_data;
157  }
158 
159  inline void cliquePrint(graph_t * g,
160  set_t s) {
161  unsigned int i;
162  printf("size=%d, weight=%d: ",
163  set_size(s), graph_subgraph_weight(g, s));
164  for (i = 0; i < SET_MAX_SIZE(s); i++) {
165  if (SET_CONTAINS(s,i)) {
166  printf(" %d",i);
167  }
168  }
169  printf("\n");
170  return;
171  }
172 
173  inline void cliquePopulateVector(const int index,
174  vector<int> & clique) {
175  unsigned int i;
176  set_t s = m_copts->clique_list[index];
177  for (i = 0; i < SET_MAX_SIZE(s); i++) {
178  if (SET_CONTAINS(s,i)) {
179  clique.push_back(i);
180  }
181  }
182  }
183 
184  inline void cliquePrintAll(graph_t * g){
185  int i;
186  for(i = 0; i < *(int*)m_copts->user_data; i++) {
188  }
189  }
190 
191  inline void cliqueFreeMemory(){
192  int i;
193  for(i = 0; i < m_clique_count; i++) {
195  }
196  m_clique_count = 0;
197  m_copts->user_data = (void*) &m_clique_count;
199  }
200 
201  inline void printGraph(graph_t * g){
202  return graph_print(g);
203  }
204 
205  inline void cliqueFindOneQualex(vector<int> & ind){
206  string gfilename = "dimacs_g.";
207  string wfilename = "dimacs_w.";
208 
209  gfilename += UtilIntToStr(qualex_count);
210  wfilename += UtilIntToStr(qualex_count);
211 
212 #ifdef _MSC_VER
213  string cmd = "C:\\cygwin\\home\\magala\\COIN\\coin-Decomp\\TempFix\\qualex-ms\\qualex-ms ";
214 #else
215  string cmd = "~/COIN/coin-Decomp/TempFix/qualex-ms/qualex-ms ";
216 #endif
217  cmd += gfilename + " -w" + wfilename;
218  printf("\ncmd = %s", cmd.c_str());
219  fflush(stdout);
220  system(cmd.c_str());
221 
222  readDimacsSolution(ind);
223 
224  qualex_count++;
225  }
226 
227  inline void printGraphDimacs(graph_t * g){
228 
229  string filename = "dimacs_g.";
230  filename += UtilIntToStr(qualex_count);
231  ofstream os(filename.c_str());
232  int i,j;
233  os << "p edge " << g->n << " " << graph_edge_count(g) << endl;
234  for(i = 0; i < g->n; i++) {
235  for (j = 0; j <= i; j++) {
236  if (SET_CONTAINS_FAST(g->edges[i], j)) {
237  os << "e " << i+1 << " " << j+1 << endl;
238  }
239  }
240  }
241  os.close();
242  }
243 
244  inline void readDimacsSolution(vector<int> & ind){
245  string filename = "dimacs_g.";
246  filename += UtilIntToStr(qualex_count);
247  filename += ".sol";
248  ifstream is(filename.c_str());
249  CoinAssert(is);
250  char dummy[1000];
251  is.getline(dummy,1000);
252  is.getline(dummy,1000);
253  int i;
254  string dummyStr;
255  while(!is.eof()){
256  is >> dummyStr >> i;
257  //printf("\ni = %d", i);
258  //fflush(stdout);
259  if(is.eof())
260  break;
261  ind.push_back(i);
262  }
263  is.close();
264  }
265 
266 
267  inline void printWeightDimacs(const int n_verts,
268  const double * weight){
269  string filename = "dimacs_w.";
270  filename += UtilIntToStr(qualex_count);
271  ofstream os(filename.c_str());
272  int i;
273  for(i = 0; i < n_verts; i++) {
274  os << weight[i] << endl;
275  }
276  os.close();
277  }
278 
279  public:
280  MAD_Cliquer(const int n_verts) :
281  m_clique_count(0),
282  m_g (0),
283  m_copts (0),
284  qualex_count(0)
285  {
286  m_g = graphNew(n_verts);
287  CoinAssertHint(m_g, "Error: Out of Memory");
288 
290  m_copts->user_data = (void*) &m_clique_count;
293  }
296  graphFree(m_g);
297  }
298 };
299 
300 
301 #endif
#define SET_CONTAINS_FAST(s, a)
Definition: MAD_CliquerI.h:100
#define SET_CONTAINS(s, a)
Definition: MAD_CliquerI.h:102
int * weights
Definition: MAD_CliquerI.h:165
void readDimacsSolution(vector< int > &ind)
Definition: MAD_Cliquer.h:244
graph_t * m_g
Definition: MAD_Cliquer.h:81
void cliquePopulateVector(const int index, vector< int > &clique)
Definition: MAD_Cliquer.h:173
int clique_find_all(graph_t *g, int min_weight, int max_weight, int maximal, clique_options *opts)
void delEdge(graph_t *g, const int i, const int j)
Definition: MAD_Cliquer.h:118
int graph_edge_count(graph_t *g)
int clique_unweighted_find_all(graph_t *g, int min_size, int max_size, int maximal, clique_options *opts)
void cliqueUnweightedFindAll(graph_t *g, int minWeight, int maxWeight, int maximal)
Definition: MAD_Cliquer.h:142
static void set_free(set_t s)
Definition: MAD_CliquerI.h:143
void graph_print(graph_t *g)
void printGraphDimacs(graph_t *g)
Definition: MAD_Cliquer.h:227
int * getVertWeight(graph_t *g)
Definition: MAD_Cliquer.h:126
void cliquePrintAll(graph_t *g)
Definition: MAD_Cliquer.h:184
#define SET_MAX_SIZE(s)
Definition: MAD_CliquerI.h:105
void printGraph(graph_t *g)
Definition: MAD_Cliquer.h:201
graph_t * graph_new(int n)
void addEdge(graph_t *g, const int i, const int j)
Definition: MAD_Cliquer.h:113
void cliqueFindOne(graph_t *g, int minWeight, int maxWeight, int maximal)
Definition: MAD_Cliquer.h:150
graph_t * graphNew(const int n_verts)
Definition: MAD_Cliquer.h:87
#define CoinAssertHint(expression, hint)
Definition: CoinError.hpp:184
int(* user_function)(set_t, graph_t *, clique_options *)
Definition: MAD_CliquerI.h:213
set_t clique_find_single(graph_t *g, int min_weight, int max_weight, int maximal, clique_options *opts)
void graphFree(graph_t *g)
Definition: MAD_Cliquer.h:91
set_t * clique_list
Definition: MAD_CliquerI.h:215
int qualex_count
Definition: MAD_Cliquer.h:84
setelement * set_t
Definition: MAD_CliquerI.h:29
clique_options * m_copts
Definition: MAD_Cliquer.h:82
static int record_clique_func(set_t s, graph_t *g, clique_options *opts)
Definition: MAD_Cliquer.h:33
#define GRAPH_DEL_EDGE(g, i, j)
Definition: MAD_CliquerI.h:173
const double DecompEpsilon
Definition: Decomp.h:100
static int set_size(set_t s)
Definition: MAD_CliquerI.h:115
static int graph_subgraph_weight(graph_t *g, set_t s)
Definition: MAD_CliquerI.h:186
int getNumVertices(graph_t *g)
Definition: MAD_Cliquer.h:123
set_t * edges
Definition: MAD_CliquerI.h:164
MAD_Cliquer(const int n_verts)
Definition: MAD_Cliquer.h:280
static set_t set_duplicate(set_t s)
Definition: MAD_CliquerI.h:152
clique_options * clique_default_options
int m_clique_count
Definition: MAD_Cliquer.h:80
string UtilIntToStr(const int i)
Definition: UtilMacros.h:279
void cliqueFindOneQualex(vector< int > &ind)
Definition: MAD_Cliquer.h:205
static int set_clique_func(set_t s, graph_t *g, clique_options *opts)
Definition: MAD_Cliquer.h:53
#define GRAPH_ADD_EDGE(g, i, j)
Definition: MAD_CliquerI.h:169
void copyGraphNonPos(graph_t *gDest, const graph_t *gSource, const double *weights)
Definition: MAD_Cliquer.h:95
void graph_free(graph_t *g)
#define CoinAssert(expression)
Definition: CoinError.hpp:183
void cliquePrint(graph_t *g, set_t s)
Definition: MAD_Cliquer.h:159
void setVertWeight(graph_t *g, const int *vertWeight)
Definition: MAD_Cliquer.h:129
void cliqueFindAll(graph_t *g, int minWeight, int maxWeight, int maximal)
Definition: MAD_Cliquer.h:134
void printWeightDimacs(const int n_verts, const double *weight)
Definition: MAD_Cliquer.h:267
void cliqueFreeMemory()
Definition: MAD_Cliquer.h:191