Scippy

GCG

Branch-and-Price & Column Generation for Everyone

hypergraph_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 hypergraph_def.h
30 * @brief miscellaneous hypergraph 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
37#ifndef GCG_HYPERGRAPH_DEF_H_
38#define GCG_HYPERGRAPH_DEF_H_
39
40#include "scip/scip.h"
41#include "graph/hypergraph.h"
42
43namespace gcg {
44
45template <class T>
47 GCG* gcgstruct /**< GCG data structure */
48) : name("hypergraph"),gcg(gcgstruct),graph(NULL),lastnode(0),dummynodes(0)
49{
50 SCIPdebugMessage("Creating graph\n");
51 graph = new Graph<T>(gcg);
52}
53
54template <class T>
56{
57 if(graph != NULL)
58 delete graph;
59
60}
61
62template <class T>
64{
65 int nodeid;
66 if( i < (int) nodes.size())
67 nodeid = nodes[i];
68 else
69 nodeid = lastnode;
70
71 SCIPdebugMessage("Nodeid %d is %d\n", i, nodeid);
72 return nodeid;
73}
74
75template <class T>
77{
78 int nodeid = lastnode;
79 SCIPdebugMessage("Adding node %d (id=%d)\n", i, nodeid);
80 SCIP_CALL( graph->addNode(nodeid, weight) );
81 nodes.push_back(nodeid);
82 mapping.resize(nodeid+1);
83 mapping[nodeid] = i;
84 ++lastnode;
85 return SCIP_OKAY;
86}
87
88/** adds the edge to the graph */
89template <class T>
90SCIP_RETCODE Hypergraph<T>::addHyperedge(std::vector<int> &edge, int weight)
91{
92 int edgenodeid = lastnode;
93 ++lastnode;
94 SCIPdebugMessage("Adding hyperedge %lu (id=%d)\n", hedges.size(), edgenodeid);
95 SCIP_CALL( graph->addNode(edgenodeid, weight) );
96
97 for( size_t i = 0; i < edge.size(); ++i )
98 {
99 SCIP_CALL( graph->addEdge(edgenodeid, computeNodeId(edge[i])) );
100 }
101 hedges.push_back(edgenodeid);
102 mapping.resize(edgenodeid+1);
103 mapping[edgenodeid] = hedges.size()-1;
104 return SCIP_OKAY;
105}
106
107/** adds the edge to the graph */
108template <class T>
110{
111 int edgenodeid = hedges[hedge];
112 int nodeid = nodes[node];
113 SCIP_CALL( graph->addEdge(edgenodeid, nodeid) );
114
115 return SCIP_OKAY;
116}
117
118
119template <class T>
121 return (int)nodes.size();
122}
123
124template <class T>
126 return (int)hedges.size();
127}
128
129template <class T>
131 assert( i >= 0);
132 return graph->getNNeighbors(i);
133}
134
135template <class T>
136std::vector<int> Hypergraph<T>::getNeighbors(int i) {
137 assert(i >= 0);
138 int nodeid = computeNodeId(i);
139 std::vector<int> edges = graph->getNeighbors(nodeid);
140
141 std::set<int> neighbors;
142 for( size_t j = 0; j < edges.size(); ++j )
143 {
144 std::vector<int> tempneighbors = graph->getNeighbors(edges[j]);
145 neighbors.insert(tempneighbors.begin(), tempneighbors.end());
146 }
147 std::vector<int> r(neighbors.begin(), neighbors.end());
148 for( size_t j = 0; j < r.size(); ++j)
149 {
150 r[j] = mapping[r[j]];
151 }
152 std::vector<int>::iterator it = std::remove(r.begin(), r.end(), nodeid);
153
154 return std::vector<int>(r.begin(), it);
155}
156
157template <class T>
159 int i
160 )
161{
162 std::vector<int> hnodes = graph->getNeighbors(hedges[i]);
163 for( size_t j = 0; j < hnodes.size(); ++j)
164 {
165 hnodes[j] = computeNodeId(hnodes[j]);
166 }
167 return hnodes;
168}
169
170template <class T>
172 int i
173 )
174{
175 return graph->getNNeighbors(hedges[i]);
176}
177
178template <class T>
179void Hypergraph<T>::setPartition(int i, int ID) {
180 partition.resize(getNNodes(), -1);
181 partition[i] = ID;
182}
183
184/** write the hypergraph to a file */
185template <class T>
187 int fd, /**< filename where the graph should be written to */
188 SCIP_Bool writeweights
189 )
190{
191 SCIP* scip = GCGgetOrigprob(this->gcg);
192 FILE* file;
193 file = fdopen(fd, "w");
194 if( file == NULL )
196
197 SCIPinfoMessage(scip, file, "%ld %ld\n", nodes.size()+dummynodes, hedges.size());
198
199 for( size_t i = 0; i < hedges.size(); ++i )
200 {
201 int nneighbors = graph->getNNeighbors(hedges[i]);
202 std::vector<int> neighbors = graph->getNeighbors(hedges[i]);
203
204 if( writeweights )
205 {
206 SCIPinfoMessage(scip, file, "%d ", graph->getWeight((int)i));
207 }
208 for( int j = 0; j < nneighbors; ++j )
209 {
210 SCIPinfoMessage(scip, file, "%d ", computeNodeId(neighbors[j])+1);
211 }
212 SCIPinfoMessage(scip, file, "\n");
213 }
214
215 return SCIP_OKAY;
216}
217
218/** read in the partition from a file */
219template <class T>
221 const char* filename
222)
223{
224 ifstream input(filename);
225 if( !input.good() )
226 {
227 SCIPerrorMessage("Could not open file <%s> for reading\n", filename);
228 return SCIP_READERROR;
229 }
230 partition.resize(getNNodes(), -1);
231 for( int i = 0; i < getNNodes(); ++i )
232 {
233 int part = 0;
234 if( !(input >> part) )
235 {
236 SCIPerrorMessage("Could not read from file <%s>. It may be in the wrong format\n", filename);
237 return SCIP_READERROR;
238 }
239 partition[i] = part;
240 }
241
242 input.close();
243 return SCIP_OKAY;
244}
245
246/** return the weight of given node */
247template <class T>
249 int i /**< the given node */
250 )
251{
252 return graph->getWeight(i);
253}
254
255/** return the weight of given hyperedge */
256template <class T>
258 int i /**< the given hyperedge */
259 )
260{
261 int edgenodeid = hedges[i];
262
263 return graph->getWeight(edgenodeid);
264}
265
266template <class T>
268{
269 SCIP_CALL( graph->flush() );
270 return SCIP_OKAY;
271}
272
273} /* namespace gcg */
274
275#endif
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)
SCIP_Real * r
Hypergraph(GCG *gcgstruct)
int getNNeighbors(int i)
SCIP_RETCODE addNodeToHyperedge(int node, int hedge)
SCIP_RETCODE addNode(int i, int weight)
SCIP_RETCODE addHyperedge(std::vector< int > &edge, int weight)
std::vector< int > getHyperedgeNodes(int i)
SCIP_RETCODE flush()
void setPartition(int i, int ID)
int getWeight(int i)
int getNHyperedgeNodes(int i)
Graph< T > * graph
Definition: hypergraph.h:60
std::vector< int > getNeighbors(int i)
int getHyperedgeWeight(int i)
SCIP_RETCODE readPartition(const char *filename)
SCIP_RETCODE writeToFile(int fd, SCIP_Bool writeweights)
#define SCIP_CALL(x)
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
miscellaneous hypergraph methods for structure detection
#define NULL
#define SCIPerrorMessage
#define SCIPdebugMessage
Definition: struct_gcg.h:55
SCIP_FILECREATEERROR
SCIP_READERROR
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE