Scippy

GCG

Branch-and-Price & Column Generation for Everyone

graph.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.h
29  * @brief miscellaneous graph methods for structure detection
30  * @author Martin Bergner
31  * @author Annika Thome
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 
37 
38 #ifndef GCG_GRAPH_H_
39 #define GCG_GRAPH_H_
40 #include "objscip/objscip.h"
41 #include "tclique/tclique.h"
42 #include "weights.h"
43 #include "pub_decomp.h"
44 #include "bridge.h"
45 #include "graph_interface.h"
46 #include <exception>
47 #include <vector>
48 #include <string>
49 
50 namespace gcg {
51 
52 template <class T>
53 class Graph : public GraphInterface {
54 public:
55  std::string name;
56 protected:
57  SCIP* scip_;
59  int nconss;
60  int nvars;
63 
64 public:
65  /** Constructor */
66  Graph(
67  SCIP* scip /**< SCIP data structure */
68  );
69 
70  void swap(Graph & other) // the swap member function (should never fail!)
71  {
72  // swap all the members (and base subobject, if applicable) with other
73  std::swap(partition, other.partition);
74  std::swap(scip_ , other.scip_);
75  std::swap(graph , other.graph);
76  std::swap(nconss , other.nconss);
77  std::swap(nvars , other.nvars);
78  std::swap(nnonzeroes , other.nnonzeroes);
79  std::swap(dummynodes, other.dummynodes);
80  }
81 
82  Graph& operator=(Graph other) // note: argument passed by value!
83  {
84  // swap this with other
85  swap(other);
86 
87  return *this;
88  }
89 
90  /** Destruktor */
91  virtual ~Graph();
92 
93  /** adds n nodes in the graph at the same time. it is much faster than to call addNode() many times */
94  SCIP_RETCODE addNNodes(int _n_nodes);
95 
96  /** adds n nodes in the graph at the same time. it is much faster than to call addNode() many times. weights represent node weights */
97  SCIP_RETCODE addNNodes(int _n_nodes, std::vector<int> weights);
98 
99  /** adds the node with the given weight to the graph */
100  SCIP_RETCODE addNode(int i,int weight);
101 
102  /** adds the node with the 0 weight to the graph */
103  SCIP_RETCODE addNode();
104 
105  /** adds the edge to the graph */
106  SCIP_RETCODE addEdge(int i, int j);
107 
108  /** adds the weighted edge to the graph */
109  SCIP_RETCODE addEdge(int i, int j, double weight);
110 
111  /** sets the weight of the edge in the graph */
112  SCIP_RETCODE setEdge(int i, int j, double weight);
113 
114  /** returns the weight of the edge in the graph */
115  double getEdgeWeight(int i, int j);
116 
117  std::vector<std::pair<int, double> > getNeighborWeights(int i);
118 
119  /** return the number of nodes */
120  int getNNodes();
121 
122  /** return the number of edges (or hyperedges) */
123  int getNEdges();
124 
125  /** get list of edges in the graph (not defined how edges are implemented) */
126  SCIP_RETCODE getEdges(std::vector<void*>& edges);
127 
128  /** returns whether there is an edge between nodes i and j */
129  virtual int edge(int i, int j);
130 
131  /** return the number of neighbor nodes of given node */
132  virtual int getNNeighbors(
133  int i /**< the given node */
134  );
135 
136  /** return the neighboring nodes of a given node */
137  virtual std::vector<int> getNeighbors(
138  int i /**< the given node */
139  );
140 
141  /** assigns partition to a given node*/
142  virtual void setPartition(int i, int ID);
143 
144  /** create graph from the matrix, to be overriden by the implementation*/
145  virtual SCIP_RETCODE createFromMatrix(
146  SCIP_CONS** conss, /**< constraints for which graph should be created */
147  SCIP_VAR** vars, /**< variables for which graph should be created */
148  int nconss_, /**< number of constraints */
149  int nvars_ /**< number of variables */
150  ) { return SCIP_ERROR; }
151 
152  /** writes the graph to the given file.
153  * The format is graph dependent
154  */
155  virtual SCIP_RETCODE writeToFile(
156  int fd, /**< filename where the graph should be written to */
157  SCIP_Bool writeweights /**< whether to write weights */
158  );
159 
160 
161  /**
162  * reads the partition from the given file.
163  * The format is graph dependent. The default is a file with one line for each node a
164  */
165  virtual SCIP_RETCODE readPartition(
166  const char* filename /**< filename where the partition is stored */
167  );
168 
169  int getNNonzeroes() const
170  {
171  return nnonzeroes;
172  }
173 
174  /** return the weight of given node */
175  virtual int getWeight(
176  int i /**< the given node */
177  );
178 
179  /** set the number of dummy nodes */
180  void setDummynodes(int dummynodes_)
181  {
182  dummynodes = dummynodes_;
183  }
184 
185  int getDummynodes() const
186  {
187  return dummynodes;
188  }
189 
190  SCIP_RETCODE flush();
191 
192  SCIP_RETCODE normalize();
193 
194  virtual double getEdgeWeightPercentile(double q);
195 
196 #ifdef WITH_GSL
197  /** function needed for MST clustering */
198  virtual void expand(int factor);
199 
200  /** function needed for MST clustering */
201  virtual void inflate(double factor);
202 
203  /** function needed for MST clustering */
204  virtual void colL1Norm();
205 
206  /** function needed for MST clustering */
207  virtual void prune();
208 
209  /** function needed for MST clustering */
210  virtual bool stopMCL(int iter);
211 
212  /** function needed for MST clustering */
213  virtual std::vector<int> getClustersMCL();
214 
215  /** function needed for MST clustering */
216  virtual void initMCL();
217 
218  /** function needed for MST clustering */
219  virtual void clearMCL();
220 #endif
221 
222 };
223 
224 }
225 
226 #endif
miscellaneous graph interface methods
SCIP_RETCODE addEdge(int i, int j)
Definition: graph_def.h:105
int nconss
Definition: graph.h:59
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
weight class for graphs
virtual int getWeight(int i)
Definition: graph_def.h:238
SCIP * scip_
Definition: graph.h:57
Graph & operator=(Graph other)
Definition: graph.h:82
void setDummynodes(int dummynodes_)
Definition: graph.h:180
SCIP_RETCODE addNNodes(int _n_nodes)
Definition: graph_def.h:62
virtual SCIP_RETCODE createFromMatrix(SCIP_CONS **conss, SCIP_VAR **vars, int nconss_, int nvars_)
Definition: graph.h:145
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
int dummynodes
Definition: graph.h:62
SCIP_RETCODE setEdge(int i, int j, double weight)
Definition: graph_def.h:256
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
std::vector< int > partition
int nvars
Definition: graph.h:60
int getNEdges()
Definition: graph_def.h:79
int nnonzeroes
Definition: graph.h:61
bridge
virtual SCIP_RETCODE readPartition(const char *filename)
Definition: graph_def.h:210
int getNNonzeroes() const
Definition: graph.h:169
SCIP_RETCODE getEdges(std::vector< void * > &edges)
Definition: graph_def.h:84
int getDummynodes() const
Definition: graph.h:185
void swap(Graph &other)
Definition: graph.h:70
SCIP_RETCODE addNode()
Definition: graph_def.h:97
Graph(SCIP *scip)
Definition: graph_def.h:46
std::string name
Definition: graph.h:55
int getNNodes()
Definition: graph_def.h:74
public methods for working with decomposition structures