Scippy

GCG

Branch-and-Price & Column Generation for Everyone

rowgraph_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 rowgraph_def.h
30 * @brief A row graph where each row is a node and rows are adjacent if they share a variable
31 * @author Martin Bergner
32 * @author Annika Thome
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36// #define SCIP_DEBUG
37
38#ifndef GCG_ROWGRAPH_DEF_H_
39#define GCG_ROWGRAPH_DEF_H_
40
41#include "graph/rowgraph.h"
42#include <algorithm>
43
44namespace gcg {
45
46template <class T>
48 GCG* gcgstruct, /**< GCG data structure */
49 Weights w /**< weights for the given graph */
50 ) : MatrixGraph<T>(gcgstruct,w), graph(gcgstruct)
51{
52 this->graphiface = &graph;
53 this->name = std::string("rowgraph");
54}
55
56template <class T>
58{
59 // TODO Auto-generated destructor stub
60}
61
62template <class T>
64 GCG_DECOMP** decomp /**< decomposition structure to generate */
65 )
66{
67 int nblocks;
68 SCIP_HASHMAP* constoblock = NULL;
69 SCIP* scip = GCGgetOrigprob(this->gcg);
70 int* nsubscipconss = NULL;
71 int i;
72 SCIP_CONS** conss = NULL;
73 SCIP_Bool emptyblocks = FALSE;
74 std::vector<int> partition = graph.getPartition();
75 conss = SCIPgetConss(scip);
76 nblocks = *(std::max_element(partition.begin(), partition.end()))+1;
77
78 SCIP_CALL( SCIPallocBufferArray(scip, &nsubscipconss, nblocks) );
79 BMSclearMemoryArray(nsubscipconss, nblocks);
80
81 SCIP_CALL( SCIPhashmapCreate(&constoblock, SCIPblkmem(scip), this->nconss) );
82
83 /* assign constraints to partition */
84 for( i = 0; i < this->nconss; i++ )
85 {
86 int block = partition[i];
87
88 if( block == -1 )
89 {
90 SCIP_CALL( SCIPhashmapInsert(constoblock, conss[i], (void*) (size_t) (nblocks +1)) );
91 }
92 else
93 {
94 assert(block >= 0);
95 assert(block < nblocks);
96 SCIP_CALL( SCIPhashmapInsert(constoblock, conss[i], (void*) (size_t) (block +1)) );
97 ++(nsubscipconss[block]);
98 }
99
100 }
101
102
103 // TODO: remove- FOR DEBUG ONLY!!!
104 std::vector<int> nsubscipconss_dbg(nblocks);
105
106 /* first, make sure that there are constraints in every block, otherwise the hole thing is useless */
107 for( i = 0; i < nblocks; ++i )
108 {
109 // TODO: remove- FOR DEBUG ONLY!!!
110 nsubscipconss_dbg[i] = nsubscipconss[i];
111 if( nsubscipconss[i] == 0 )
112 {
113 SCIPdebugMessage("Block %d does not have any constraints!\n", i);
114 emptyblocks = TRUE;
115 }
116 }
117
118 if( !emptyblocks )
119 {
120 SCIP_CALL( GCGdecompCreate(this->gcg, decomp) );
121 SCIP_CALL( GCGfilloutDecompFromConstoblock(this->gcg, *decomp, constoblock, nblocks, FALSE) );
122 }
123 else
124 {
125 SCIPhashmapFree(&constoblock);
126 *decomp = NULL;
127 }
128
129 SCIPfreeBufferArray(scip, &nsubscipconss);
130 return SCIP_OKAY;
131}
132
133template <class T>
135 PARTIALDECOMP* oldpartialdec,
136 PARTIALDECOMP** firstpartialdec,
137 PARTIALDECOMP** secondpartialdec,
138 DETPROBDATA* detprobdata
139 )
140{
141 int nblocks;
142 SCIP_HASHMAP* constoblock = NULL;
143 bool found;
144 int* nsubscipconss = NULL;
145 int i;
146 SCIP_Bool emptyblocks = FALSE;
147 SCIP* scip = GCGgetOrigprob(this->gcg);
148
149 assert(oldpartialdec != NULL);
150
151 //fillout conssForGraph
152 std::vector<int> conssForGraph; /** stores the conss included by the graph */
153 std::vector<bool> conssBool(oldpartialdec->getNConss(), false); /**< true, if the cons will be part of the graph */
154
155 if( firstpartialdec == NULL && secondpartialdec == NULL )
156 return SCIP_INVALIDDATA;
157
158 for( int c = 0; c < oldpartialdec->getNOpenconss(); ++c )
159 {
160 int cons = oldpartialdec->getOpenconss()[c];
161 found = false;
162 for( int v = 0; v < oldpartialdec->getNOpenvars() && !found; ++v )
163 {
164 int var = oldpartialdec->getOpenvars()[v];
165 for( i = 0; i < detprobdata->getNVarsForCons(cons) && !found; ++i )
166 {
167 if( var == detprobdata->getVarsForCons(cons)[i] )
168 {
169 conssBool[cons] = true;
170 found = true;
171 }
172 }
173 }
174 }
175
176 for( int c = 0; c < oldpartialdec->getNOpenconss(); ++c )
177 {
178 int cons = oldpartialdec->getOpenconss()[c];
179 if(conssBool[cons])
180 conssForGraph.push_back(cons);
181 }
182
183 std::vector<int> partition = graph.getPartition();
184 nblocks = *(std::max_element(partition.begin(), partition.end()))+1;
185
186 SCIP_CALL( SCIPallocBufferArray(scip, &nsubscipconss, nblocks) );
187 BMSclearMemoryArray(nsubscipconss, nblocks);
188
189 SCIP_CALL( SCIPhashmapCreate(&constoblock, SCIPblkmem(scip), this->nconss) );
190
191 /* assign constraints to partition */
192 for( i = 0; i < this->nconss; i++ )
193 {
194 int block = partition[i];
195
196 if( block == -1 )
197 {
198 SCIP_CALL( SCIPhashmapInsert(constoblock, (void*) (size_t) conssForGraph[i], (void*) (size_t) (nblocks +1)) );
199 }
200 else
201 {
202 assert(block >= 0);
203 assert(block < nblocks);
204 SCIP_CALL( SCIPhashmapInsert(constoblock, (void*) (size_t) conssForGraph[i], (void*) (size_t) (block +1)) );
205 ++(nsubscipconss[block]);
206 }
207
208 }
209
210 /* first, make sure that there are constraints in every block, otherwise the hole thing is useless */
211 for( i = 0; i < nblocks; ++i )
212 {
213 if( nsubscipconss[i] == 0 )
214 {
215 SCIPdebugMessage("Block %d does not have any constraints!\n", i);
216 emptyblocks = TRUE;
217 }
218 }
219
220 if( !emptyblocks )
221 {
222 if( firstpartialdec != NULL )
223 {
224 (*firstpartialdec) = new PARTIALDECOMP(oldpartialdec);
225 SCIP_CALL((*firstpartialdec)->assignPartialdecFromConstoblock(constoblock, nblocks));
226 }
227 if( secondpartialdec != NULL )
228 {
229 (*secondpartialdec) = new PARTIALDECOMP(oldpartialdec);
230 SCIP_CALL((*secondpartialdec)->assignBorderFromConstoblock(constoblock, nblocks));
231 }
232 SCIPhashmapFree(&constoblock);
233 }
234 else
235 {
236 SCIPhashmapFree(&constoblock);
237 if( firstpartialdec != NULL )
238 {
239 (*firstpartialdec) = NULL;
240 }
241 if( secondpartialdec != NULL )
242 {
243 (*secondpartialdec) = NULL;
244 }
245 }
246
247 SCIPfreeBufferArray(scip, &nsubscipconss);
248 return SCIP_OKAY;
249}
250
251template <class T>
253 SCIP_CONS** conss, /**< constraints for which graph should be created */
254 SCIP_VAR** vars, /**< variables for which graph should be created */
255 int nconss_, /**< number of constraints */
256 int nvars_ /**< number of variables */
257 )
258{
259 int i;
260 int j;
261 int k;
262 int l;
263 SCIP_Bool success;
264
265 std::pair< int, int> edge;
266 std::vector< std::pair< int, int> > edges;
267 SCIP* scip = GCGgetOrigprob(this->gcg);
268
269 assert(conss != NULL);
270 assert(vars != NULL);
271 assert(nvars_ > 0);
272 assert(nconss_ > 0);
273
274 this->nvars = nvars_;
275 this->nconss = nconss_;
276
277 /* go through all constraints */
278 for( i = 0; i < this->nconss; ++i )
279 {
280 TCLIQUE_WEIGHT weight;
281
282 /* calculate weight of node */
283 weight = this->weights.calculate(conss[i]);
284
285 this->graph.addNode(i, weight);
286 }
287
288 /* go through all constraints */
289 for( i = 0; i < this->nconss; ++i )
290 {
291 SCIP_VAR** curvars1 = NULL;
292
293 int ncurvars1;
294 SCIP_CALL( SCIPgetConsNVars(scip, conss[i], &ncurvars1, &success) );
295 assert(success);
296 if( ncurvars1 == 0 )
297 continue;
298
299 /*
300 * may work as is, as we are copying the constraint later regardless
301 * if there are variables in it or not
302 */
303 SCIP_CALL( SCIPallocBufferArray(scip, &curvars1, ncurvars1) );
304 SCIP_CALL( SCIPgetConsVars(scip, conss[i], curvars1, ncurvars1, &success) );
305 assert(success);
306
307 /* go through all constraints */
308 for( j = 0; j < i; ++j )
309 {
310 SCIP_VAR** curvars2 = NULL;
311 SCIP_Bool continueloop;
312 int ncurvars2;
313 SCIP_CALL( SCIPgetConsNVars(scip, conss[j], &ncurvars2, &success) );
314 assert(success);
315 if( ncurvars2 == 0 )
316 continue;
317
318 edge = std::make_pair(MIN(i, j), MAX(i, j) );
319
320 /* check if edge was not already added to graph */
321 if(edges.end() != std::find(edges.begin(), edges.end(), edge) )
322 continue;
323
324 /*if(this->graph.edge(i, j))
325 continue;
326 */
327 continueloop = FALSE;
328 /*
329 * may work as is, as we are copying the constraint later regardless
330 * if there are variables in it or not
331 */
332 SCIP_CALL( SCIPallocBufferArray(scip, &curvars2, ncurvars2) );
333 SCIP_CALL( SCIPgetConsVars(scip, conss[j], curvars2, ncurvars2, &success) );
334 assert(success);
335
336
337 /** @todo skip all variables that have a zero coeffient or where all coefficients add to zero */
338 /** @todo Do more then one entry per variable actually work? */
339
340 for( k = 0; k < ncurvars1; ++k )
341 {
342 SCIP_VAR* var1 = NULL;
343 int varIndex1;
344
346 var1 = SCIPvarGetProbvar(curvars1[k]);
347 else
348 var1 = curvars1[k];
349
350 if( !GCGisVarRelevant(var1) )
351 continue;
352
353 assert(var1 != NULL);
354 varIndex1 = SCIPvarGetProbindex(var1);
355 assert(varIndex1 >= 0);
356 assert(varIndex1 < this->nvars);
357
358 for( l = 0; l < ncurvars2; ++l )
359 {
360 SCIP_VAR* var2;
361 int varIndex2;
362
364 var2 = SCIPvarGetProbvar(curvars2[l]);
365 else
366 var2 = curvars2[l];
367
368 if( !GCGisVarRelevant(var2) )
369 continue;
370
371 assert(var2 != NULL);
372 varIndex2 = SCIPvarGetProbindex(var2);
373 assert(varIndex2 >= 0);
374 assert(varIndex2 < this->nvars);
375
376 if(varIndex1 == varIndex2)
377 {
378 SCIP_CALL( this->graph.addEdge(i, j) );
379
380 edges.push_back(edge);
381 std::sort(edges.begin(), edges.end());
382
383 /*
384 * this->graph.flush();
385 */
386
387 continueloop = TRUE;
388 break;
389 }
390 }
391 if(continueloop)
392 break;
393 }
394 SCIPfreeBufferArray(scip, &curvars2);
395 }
396 SCIPfreeBufferArray(scip, &curvars1);
397 }
398 this->graph.flush();
399
400 return SCIP_OKAY;
401}
402
403} /* namespace gcg */
404#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_VAR * w
GCG_EXPORT int getNVarsForCons(int consIndex)
returns the number of variables for a given constraint
GCG_EXPORT std::vector< int > & getVarsForCons(int consIndex)
returns the variable indices of the coefficient matrix for a constraint
std::string name
Definition: matrixgraph.h:57
GraphInterface * graphiface
Definition: matrixgraph.h:64
class to manage partial decompositions
GCG_EXPORT int getNConss()
Gets the number of constraints.
GCG_EXPORT int getNOpenvars()
Gets size of vector containing variables not assigned yet.
GCG_EXPORT int getNOpenconss()
Gets size of vector containing constraints not assigned yet.
GCG_EXPORT std::vector< int > & getOpenconss()
Gets vector containing constraints not assigned yet.
GCG_EXPORT std::vector< int > & getOpenvars()
Gets vector containing variables not assigned yet.
RowGraph(GCG *gcg, Weights w)
Definition: rowgraph_def.h:47
virtual SCIP_RETCODE createPartialdecFromPartition(PARTIALDECOMP *oldpartialdec, PARTIALDECOMP **firstpartialdec, PARTIALDECOMP **secondpartialdec, DETPROBDATA *detprobdata)
Definition: rowgraph_def.h:134
gcg::Graph< T > graph
Definition: rowgraph.h:49
virtual ~RowGraph()
Definition: rowgraph_def.h:57
virtual SCIP_RETCODE createFromMatrix(SCIP_CONS **conss, SCIP_VAR **vars, int nconss_, int nvars_)
Definition: rowgraph_def.h:252
virtual SCIP_RETCODE createDecompFromPartition(GCG_DECOMP **decomp)
Definition: rowgraph_def.h:63
SCIP_RETCODE GCGfilloutDecompFromConstoblock(GCG *gcg, GCG_DECOMP *decomp, SCIP_HASHMAP *constoblock, int nblocks, SCIP_Bool staircase)
Definition: decomp.c:1479
SCIP_RETCODE GCGdecompCreate(GCG *gcg, GCG_DECOMP **decdecomp)
Definition: decomp.c:480
#define SCIP_Bool
#define MIN(x, y)
#define MAX(x, y)
#define SCIP_CALL(x)
#define TRUE
#define FALSE
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
SCIP_STAGE SCIPgetStage(SCIP *scip)
SCIP_CONS ** SCIPgetConss(SCIP *scip)
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
SCIP_RETCODE SCIPhashmapInsert(SCIP_HASHMAP *hashmap, void *origin, void *image)
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
SCIP_Bool GCGisVarRelevant(SCIP_VAR *var)
Definition: scip_misc.c:42
SCIP_RETCODE SCIPgetConsNVars(SCIP *scip, SCIP_CONS *cons, int *nvars, SCIP_Bool *success)
SCIP_RETCODE SCIPgetConsVars(SCIP *scip, SCIP_CONS *cons, SCIP_VAR **vars, int varssize, SCIP_Bool *success)
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
#define SCIPallocBufferArray(scip, ptr, num)
#define SCIPfreeBufferArray(scip, ptr)
SCIP_VAR * SCIPvarGetProbvar(SCIP_VAR *var)
int SCIPvarGetProbindex(SCIP_VAR *var)
#define NULL
#define BMSclearMemoryArray(ptr, num)
#define SCIPdebugMessage
A row graph where each row is a node and rows are adjacent if they share a variable.
Definition: struct_gcg.h:55
int TCLIQUE_WEIGHT
SCIP_INVALIDDATA
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE
SCIP_STAGE_TRANSFORMED