GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
graph_def.h
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
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-2026 Operations Research, RWTH Aachen University */
10/* Zuse Institute Berlin (ZIB) */
11/* */
12/* Licensed under the Apache License, Version 2.0 (the "License"); */
13/* you may not use this file except in compliance with the License. */
14/* You may obtain a copy of the License at */
15/* */
16/* http://www.apache.org/licenses/LICENSE-2.0 */
17/* */
18/* Unless required by applicable law or agreed to in writing, software */
19/* distributed under the License is distributed on an "AS IS" BASIS, */
20/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
21/* See the License for the specific language governing permissions and */
22/* limitations under the License. */
23/* */
24/* You should have received a copy of the Apache-2.0 license */
25/* along with GCG; see the file LICENSE. If not visit gcg.or.rwth-aachen.de.*/
26/* */
27/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29/**@file graph_def.h
30 * @brief miscellaneous graph methods for structure detection
31 * @author Martin Bergner
32 */
33
34/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35
36#define SCIP_Debug
37
38#ifndef GCG_GRAPH_DEF_H_
39#define GCG_GRAPH_DEF_H_
40
41#include "scip/scip.h"
42#include "graph/graph.h"
43
44namespace gcg {
45
46template <class T>
48 GCG* gcgstruct /**< GCG data structure */
49) : name("graph"),gcg(gcgstruct),graph(NULL),nconss(0),nvars(0),nnonzeroes(0),dummynodes(0)
50{
51 graph = new T();
52}
53
54template <class T>
56{
57 if(graph != NULL)
58 delete graph;
59
60}
61
62template <class T>
64{
65 return graph->addNNodes(_n_nodes);
66}
67
68template <class T>
69SCIP_RETCODE Graph<T>::addNNodes(int _n_nodes, std::vector<int> weights)
70{
71 return graph->addNNodes(_n_nodes, weights);
72}
73
74template <class T>
76 return graph->getNNodes();
77}
78
79template <class T>
81 return graph->getNEdges();
82}
83
84template <class T>
85SCIP_RETCODE Graph<T>::getEdges(std::vector<void*>& edges)
86{
87 return graph->getEdges(edges);
88}
89
90template <class T>
92{
93 SCIP_CALL( graph->addNode(i, weight) );
94 return SCIP_OKAY;
95}
96
97template <class T>
99{
100 SCIP_CALL( graph->addNode() );
101 return SCIP_OKAY;
102}
103
104/** adds the edge to the graph */
105template <class T>
107{
108 SCIP_CALL( graph->addEdge(i, j) );
109 return SCIP_OKAY;
110}
111
112template <class T>
114{
115 SCIP_CALL( graph->flush() );
116 return SCIP_OKAY;
117}
118
119template <class T>
121{
122 SCIP_CALL( graph->normalize() );
123 return SCIP_OKAY;
124}
125
126template <class T>
127int Graph<T>::edge(int i, int j) {
128 assert( i>= 0);
129 assert(j >= 0);
130
131 int edge_ij=0;
132 std::vector<int> Neighbors;
133
134 Neighbors = getNeighbors(i);
135 for(int k=0; k<(int)Neighbors.size(); k++)
136 {
137 if(Neighbors[k] == j)
138 {
139 edge_ij = 1;
140 k = (int)Neighbors.size();
141 }
142 }
143 return edge_ij;
144}
145
146template <class T>
148 assert( i >= 0);
149 return graph->getNNeighbors(i);
150}
151
152template <class T>
153std::vector<int> Graph<T>::getNeighbors(int i) {
154 assert(i >= 0);
155
156 return graph->getNeighbors(i);
157}
158
159template <class T>
160void Graph<T>::setPartition(int i, int ID) {
161 partition.resize(getNNodes(), -1);
162 partition[i] = ID;
163}
164
165/** write the graph to a file */
166template <class T>
168 int fd,
169 SCIP_Bool writeweights
170 )
171{
172 int nnodes;
173 int nedges;
174 FILE* file;
175 file = fdopen(fd, "wx");
177
178 if( file == NULL )
180
182 nedges = Graph<T>::getNEdges();
183
184 SCIPinfoMessage(scip, file, "%d %d\n", nnodes+dummynodes, nedges/2);
185
186 for( int i = 0; i < nnodes; ++i )
187 {
188 int nneighbors = Graph<T>::getNNeighbors(i);
189 std::vector<int> neighbors = Graph<T>::getNeighbors(i);
190
191 if( writeweights )
192 {
194 }
195 for( int j = 0; j < nneighbors; ++j )
196 {
197 SCIPinfoMessage(scip, file, "%d ", neighbors[j]+1);
198 }
199 SCIPinfoMessage(scip, file, "\n");
200 }
201
202 for( int i = 0; i < dummynodes; ++i )
203 {
204 SCIPinfoMessage(scip, file, "\n");
205 }
206
207 return SCIP_OKAY;
208}
209
210
211/** read in the partition from a file */
212template <class T>
214 const char* filename
215)
216{
217 ifstream input(filename);
218 if( !input.good() )
219 {
220 SCIPerrorMessage("Could not open file <%s> for reading\n", filename);
221 return SCIP_READERROR;
222 }
223 partition.resize(getNNodes(), -1);
224 for( int i = 0; i < getNNodes(); ++i )
225 {
226 int part = 0;
227 if( !(input >> part) )
228 {
229 SCIPerrorMessage("Could not read from file <%s>. It may be in the wrong format\n", filename);
230 return SCIP_READERROR;
231 }
232 partition[i] = part;
233 }
234
235 input.close();
236 return SCIP_OKAY;
237}
238
239/** return the weight of given node */
240template <class T>
242 int i /**< the given node */
243 )
244{
245 return graph->graphGetWeights(i);
246}
247
248
249/** adds the weighted edge to the graph */
250template <class T>
251SCIP_RETCODE Graph<T>::addEdge(int i, int j, double weight)
252{
253 SCIP_CALL( graph->addEdge(i, j, weight) );
254 return SCIP_OKAY;
255}
256
257/** sets the weight of the edge in the graph */
258template <class T>
259SCIP_RETCODE Graph<T>::setEdge(int i, int j, double weight)
260{
261 SCIP_CALL( graph->setEdge(i, j, weight) );
262 return SCIP_OKAY;
263}
264
265/** returns the weight of the edge in the graph */
266template <class T>
268{
269 return graph->getEdgeWeight(i, j);
270}
271
272template <class T>
273std::vector<std::pair<int, double> > Graph<T>::getNeighborWeights(int i)
274{
275 return graph->getNeighborWeights(i);
276}
277
278
279template <class T>
281{
282 return graph->getEdgeWeightPercentile(q);
283}
284
285
286
287#ifdef WITH_GSL
288
289template <class T>
290void Graph<T>::expand(int factor)
291{
292 graph->expand(factor);
293}
294
295template <class T>
296void Graph<T>::inflate(double factor)
297{
298 graph->inflate(factor);
299}
300
301template <class T>
303{
304 graph->colL1Norm();
305}
306
307template <class T>
308void Graph<T>::prune()
309{
310 graph->prune();
311}
312
313template <class T>
314bool Graph<T>::stopMCL(int iter)
315{
316 return graph->stopMCL(iter);
317}
318
319template <class T>
320std::vector<int> Graph<T>::getClustersMCL()
321{
322 return graph->getClustersMCL();
323}
324
325
326template <class T>
328{
329 graph->initMCL();
330}
331
332template <class T>
334{
335 graph->clearMCL();
336}
337
338
339
340#endif
341
342
343} /* namespace gcg */
344
345#endif
std::vector< int > partition
int nnonzeroes
Definition graph.h:62
SCIP_RETCODE addNode()
Definition graph_def.h:98
virtual void setPartition(int i, int ID)
Definition graph_def.h:160
virtual int getNNeighbors(int i)
Definition graph_def.h:147
Bridge * graph
Definition graph.h:59
SCIP_RETCODE setEdge(int i, int j, double weight)
Definition graph_def.h:259
SCIP_RETCODE flush()
Definition graph_def.h:113
int nvars
Definition graph.h:61
SCIP_RETCODE getEdges(std::vector< void * > &edges)
Definition graph_def.h:85
SCIP_RETCODE addEdge(int i, int j)
Definition graph_def.h:106
int getNNodes()
Definition graph_def.h:75
Graph(GCG *gcgstruct)
Definition graph_def.h:47
SCIP_RETCODE normalize()
Definition graph_def.h:120
virtual double getEdgeWeightPercentile(double q)
Definition graph_def.h:280
int getNEdges()
Definition graph_def.h:80
virtual ~Graph()
Definition graph_def.h:55
std::string name
Definition graph.h:56
SCIP_RETCODE addNNodes(int _n_nodes)
Definition graph_def.h:63
double getEdgeWeight(int i, int j)
Definition graph_def.h:267
virtual SCIP_RETCODE readPartition(const char *filename)
Definition graph_def.h:213
virtual int getWeight(int i)
Definition graph_def.h:241
virtual SCIP_RETCODE writeToFile(int fd, SCIP_Bool writeweights)
Definition graph_def.h:167
int nconss
Definition graph.h:60
int dummynodes
Definition graph.h:63
std::vector< std::pair< int, double > > getNeighborWeights(int i)
Definition graph_def.h:273
virtual int edge(int i, int j)
Definition graph_def.h:127
virtual std::vector< int > getNeighbors(int i)
Definition graph_def.h:153
#define SCIP_Bool
miscellaneous graph methods for structure detection
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
int j
int i
SCIP_Longint nnodes
#define NULL
#define SCIPerrorMessage
struct Gcg GCG
Definition type_gcg.h:44
SCIP_FILECREATEERROR
SCIP_READERROR
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP