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