Scippy

GCG

Branch-and-Price & Column Generation for Everyone

graph_def.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program */
4 /* GCG --- Generic Column Generation */
5 /* a Dantzig-Wolfe decomposition based extension */
6 /* of the branch-cut-and-price framework */
7 /* SCIP --- Solving Constraint Integer Programs */
8 /* */
9 /* Copyright (C) 2010-2021 Operations Research, RWTH Aachen University */
10 /* Zuse Institute Berlin (ZIB) */
11 /* */
12 /* This program is free software; you can redistribute it and/or */
13 /* modify it under the terms of the GNU Lesser General Public License */
14 /* as published by the Free Software Foundation; either version 3 */
15 /* of the License, or (at your option) any later version. */
16 /* */
17 /* This program is distributed in the hope that it will be useful, */
18 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
19 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
20 /* GNU Lesser General Public License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with this program; if not, write to the Free Software */
24 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
25 /* */
26 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 
28 /**@file graph_def.h
29  * @brief miscellaneous graph methods for structure detection
30  * @author Martin Bergner
31  */
32 
33 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
34 
35 #define SCIP_Debug
36 
37 #ifndef GCG_GRAPH_DEF_H_
38 #define GCG_GRAPH_DEF_H_
39 
40 #include "scip/scip.h"
41 #include "graph.h"
42 
43 namespace gcg {
44 
45 template <class T>
47  SCIP* scip /**< SCIP data structure */
48 ) : name("graph"),scip_(scip),graph(NULL),nconss(0),nvars(0),nnonzeroes(0),dummynodes(0)
49 {
50  graph = new T();
51 }
52 
53 template <class T>
55 {
56  if(graph != NULL)
57  delete graph;
58 
59 }
60 
61 template <class T>
62 SCIP_RETCODE Graph<T>::addNNodes(int _n_nodes)
63 {
64  return graph->addNNodes(_n_nodes);
65 }
66 
67 template <class T>
68 SCIP_RETCODE Graph<T>::addNNodes(int _n_nodes, std::vector<int> weights)
69 {
70  return graph->addNNodes(_n_nodes, weights);
71 }
72 
73 template <class T>
75  return graph->getNNodes();
76 }
77 
78 template <class T>
80  return graph->getNEdges();
81 }
82 
83 template <class T>
84 SCIP_RETCODE Graph<T>::getEdges(std::vector<void*>& edges)
85 {
86  return graph->getEdges(edges);
87 }
88 
89 template <class T>
90 SCIP_RETCODE Graph<T>::addNode(int i,int weight)
91 {
92  SCIP_CALL( graph->addNode(i, weight) );
93  return SCIP_OKAY;
94 }
95 
96 template <class T>
97 SCIP_RETCODE Graph<T>::addNode()
98 {
99  SCIP_CALL( graph->addNode() );
100  return SCIP_OKAY;
101 }
102 
103 /** adds the edge to the graph */
104 template <class T>
105 SCIP_RETCODE Graph<T>::addEdge(int i, int j)
106 {
107  SCIP_CALL( graph->addEdge(i, j) );
108  return SCIP_OKAY;
109 }
110 
111 template <class T>
112 SCIP_RETCODE Graph<T>::flush()
113 {
114  SCIP_CALL( graph->flush() );
115  return SCIP_OKAY;
116 }
117 
118 template <class T>
119 SCIP_RETCODE Graph<T>::normalize()
120 {
121  SCIP_CALL( graph->normalize() );
122  return SCIP_OKAY;
123 }
124 
125 template <class T>
126 int Graph<T>::edge(int i, int j) {
127  assert( i>= 0);
128  assert(j >= 0);
129 
130  int edge_ij=0;
131  std::vector<int> Neighbors;
132 
133  Neighbors = getNeighbors(i);
134  for(int k=0; k<(int)Neighbors.size(); k++)
135  {
136  if(Neighbors[k] == j)
137  {
138  edge_ij = 1;
139  k = (int)Neighbors.size();
140  }
141  }
142  return edge_ij;
143 }
144 
145 template <class T>
147  assert( i >= 0);
148  return graph->getNNeighbors(i);
149 }
150 
151 template <class T>
152 std::vector<int> Graph<T>::getNeighbors(int i) {
153  assert(i >= 0);
154 
155  return graph->getNeighbors(i);
156 }
157 
158 template <class T>
159 void Graph<T>::setPartition(int i, int ID) {
160  partition.resize(getNNodes(), -1);
161  partition[i] = ID;
162 }
163 
164 /** write the graph to a file */
165 template <class T>
167  int fd,
168  SCIP_Bool writeweights
169  )
170 {
171  int nnodes;
172  int nedges;
173  FILE* file;
174  file = fdopen(fd, "wx");
175  if( file == NULL )
176  return SCIP_FILECREATEERROR;
177 
178  nnodes = Graph<T>::getNNodes();
179  nedges = Graph<T>::getNEdges();
180 
181  SCIPinfoMessage(scip_, file, "%d %d\n", nnodes+dummynodes, nedges/2);
182 
183  for( int i = 0; i < nnodes; ++i )
184  {
185  int nneighbors = Graph<T>::getNNeighbors(i);
186  std::vector<int> neighbors = Graph<T>::getNeighbors(i);
187 
188  if( writeweights )
189  {
190  SCIPinfoMessage(scip_, file, "%d ", Graph<T>::getWeight(i));
191  }
192  for( int j = 0; j < nneighbors; ++j )
193  {
194  SCIPinfoMessage(scip_, file, "%d ", neighbors[j]+1);
195  }
196  SCIPinfoMessage(scip_, file, "\n");
197  }
198 
199  for( int i = 0; i < dummynodes; ++i )
200  {
201  SCIPinfoMessage(scip_, file, "\n");
202  }
203 
204  return SCIP_OKAY;
205 }
206 
207 
208 /** read in the partition from a file */
209 template <class T>
211  const char* filename
212 )
213 {
214  ifstream input(filename);
215  if( !input.good() )
216  {
217  SCIPerrorMessage("Could not open file <%s> for reading\n", filename);
218  return SCIP_READERROR;
219  }
220  partition.resize(getNNodes(), -1);
221  for( int i = 0; i < getNNodes(); ++i )
222  {
223  int part = 0;
224  if( !(input >> part) )
225  {
226  SCIPerrorMessage("Could not read from file <%s>. It may be in the wrong format\n", filename);
227  return SCIP_READERROR;
228  }
229  partition[i] = part;
230  }
231 
232  input.close();
233  return SCIP_OKAY;
234 }
235 
236 /** return the weight of given node */
237 template <class T>
239  int i /**< the given node */
240  )
241 {
242  return graph->graphGetWeights(i);
243 }
244 
245 
246 /** adds the weighted edge to the graph */
247 template <class T>
248 SCIP_RETCODE Graph<T>::addEdge(int i, int j, double weight)
249 {
250  SCIP_CALL( graph->addEdge(i, j, weight) );
251  return SCIP_OKAY;
252 }
253 
254 /** sets the weight of the edge in the graph */
255 template <class T>
256 SCIP_RETCODE Graph<T>::setEdge(int i, int j, double weight)
257 {
258  SCIP_CALL( graph->setEdge(i, j, weight) );
259  return SCIP_OKAY;
260 }
261 
262 /** returns the weight of the edge in the graph */
263 template <class T>
264 double Graph<T>::getEdgeWeight(int i, int j)
265 {
266  return graph->getEdgeWeight(i, j);
267 }
268 
269 template <class T>
270 std::vector<std::pair<int, double> > Graph<T>::getNeighborWeights(int i)
271 {
272  return graph->getNeighborWeights(i);
273 }
274 
275 
276 template <class T>
278 {
279  return graph->getEdgeWeightPercentile(q);
280 }
281 
282 
283 
284 #ifdef WITH_GSL
285 
286 template <class T>
287 void Graph<T>::expand(int factor)
288 {
289  graph->expand(factor);
290 }
291 
292 template <class T>
293 void Graph<T>::inflate(double factor)
294 {
295  graph->inflate(factor);
296 }
297 
298 template <class T>
299 void Graph<T>::colL1Norm()
300 {
301  graph->colL1Norm();
302 }
303 
304 template <class T>
305 void Graph<T>::prune()
306 {
307  graph->prune();
308 }
309 
310 template <class T>
311 bool Graph<T>::stopMCL(int iter)
312 {
313  return graph->stopMCL(iter);
314 }
315 
316 template <class T>
317 std::vector<int> Graph<T>::getClustersMCL()
318 {
319  return graph->getClustersMCL();
320 }
321 
322 
323 template <class T>
324 void Graph<T>::initMCL()
325 {
326  graph->initMCL();
327 }
328 
329 template <class T>
330 void Graph<T>::clearMCL()
331 {
332  graph->clearMCL();
333 }
334 
335 
336 
337 #endif
338 
339 
340 } /* namespace gcg */
341 
342 #endif
SCIP_RETCODE addEdge(int i, int j)
Definition: graph_def.h:105
virtual int edge(int i, int j)
Definition: graph_def.h:126
virtual double getEdgeWeightPercentile(double q)
Definition: graph_def.h:277
virtual ~Graph()
Definition: graph_def.h:54
virtual int getWeight(int i)
Definition: graph_def.h:238
SCIP_RETCODE addNNodes(int _n_nodes)
Definition: graph_def.h:62
SCIP_RETCODE flush()
Definition: graph_def.h:112
virtual SCIP_RETCODE writeToFile(int fd, SCIP_Bool writeweights)
Definition: graph_def.h:166
virtual void setPartition(int i, int ID)
Definition: graph_def.h:159
SCIP_RETCODE setEdge(int i, int j, double weight)
Definition: graph_def.h:256
static SCIP_RETCODE partition(SCIP *scip, SCIP_VAR **J, int *Jsize, SCIP_Longint *priority, SCIP_VAR **F, int Fsize, SCIP_VAR **origvar, SCIP_Real *median)
virtual std::vector< int > getNeighbors(int i)
Definition: graph_def.h:152
std::vector< std::pair< int, double > > getNeighborWeights(int i)
Definition: graph_def.h:270
Bridge * graph
Definition: graph.h:58
virtual int getNNeighbors(int i)
Definition: graph_def.h:146
double getEdgeWeight(int i, int j)
Definition: graph_def.h:264
SCIP_RETCODE normalize()
Definition: graph_def.h:119
int getNEdges()
Definition: graph_def.h:79
virtual SCIP_RETCODE readPartition(const char *filename)
Definition: graph_def.h:210
miscellaneous graph methods for structure detection
SCIP_RETCODE getEdges(std::vector< void * > &edges)
Definition: graph_def.h:84
SCIP_RETCODE addNode()
Definition: graph_def.h:97
Graph(SCIP *scip)
Definition: graph_def.h:46
int getNNodes()
Definition: graph_def.h:74