Scippy

GCG

Branch-and-Price & Column Generation for Everyone

hypercolgraph_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 hypercolgraph_def.h
30 * @brief Column hypergraph
31 * @author Martin Bergner
32 * @author Annika Thome
33 *
34 * A hypergraph structure with a node for every constraint and a hyperedge for every variable.
35 */
36
37/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
38
39#ifndef GCG_HYPERCOLGRAPH_DEF_H_
40#define GCG_HYPERCOLGRAPH_DEF_H_
41
42#include "graph/hypercolgraph.h"
45#include <set>
46#include <algorithm>
47#include <vector>
48#include <iostream>
49
50namespace gcg
51{
52template <class T>
54 GCG* gcgstruct, /**< GCG data structure */
55 Weights w /**< weights for the given graph */
56): MatrixGraph<T>(gcgstruct, w), graph(gcgstruct)
57{
58 this->graphiface = &graph;
59 this->name = std::string("hypercol");
60}
61
62template <class T>
64{
65 // TODO Auto-generated destructor stub
66}
67
68
69/** writes the graph to the given file.
70 * The format is graph dependent
71 */
72template <class T>
74 int fd, /**< filename where the graph should be written to */
75 SCIP_Bool edgeweights /**< whether to write edgeweights */
76 )
77{
78 function f(this->nvars);
79 FILE* file;
80 SCIP* scip = GCGgetOrigprob(this->gcg);
81 file = fdopen(fd, "w");
82 if( file == NULL )
84
85 SCIPinfoMessage(scip, file, "%d %d %d\n", getNEdges(), getNNodes()+this->dummynodes, edgeweights ? 1 :0);
86
87 for( int i = 0; i < getNEdges(); ++i )
88 {
89 std::vector<int> neighbors = getHyperedgeNodes(i);
90
91 if( edgeweights )
92 {
93 SCIPinfoMessage(scip, file, "%d ", graph.getHyperedgeWeight(i));
94 }
95 for( size_t j = 0; j < neighbors.size(); ++j )
96 {
97 SCIPinfoMessage(scip, file, "%d ",neighbors[j]+1);
98 }
99 SCIPinfoMessage(scip, file, "\n");
100 }
101 if( !fclose(file) )
102 return SCIP_OKAY;
103 else
104 return SCIP_WRITEERROR;
105}
106
107template <class T>
109{
110 return this->nvars;
111}
112
113template <class T>
115{
116 return this->nconss;
117}
118
119
120template <class T>
122 int i
123)
124{
125 assert(i >= 0);
126 assert(i < getNEdges());
127
128 std::vector<int> neighbors = graph.getHyperedgeNodes(i);
129 return neighbors;
130}
131
132
133template <class T>
135 SCIP_CONS** conss, /**< constraints for which graph should be created */
136 SCIP_VAR** vars, /**< variables for which graph should be created */
137 int nconss_, /**< number of constraints */
138 int nvars_ /**< number of variables */
139 )
140{
141 int i;
142 int k;
143 SCIP_Bool success;
144 std::vector< std::vector<int> > hyperedges;
145 SCIP* scip = GCGgetOrigprob(this->gcg);
146
147 assert(conss != NULL);
148 assert(vars != NULL);
149 assert(nvars_ > 0);
150 assert(nconss_ > 0);
151
152 this->nvars = nvars_;
153 this->nconss = nconss_;
154
155 /* go through all constraints */
156 for( i = 0; i < this->nconss; ++i )
157 {
158 TCLIQUE_WEIGHT weight;
159
160 /* calculate weight of node */
161 weight = this->weights.calculate(conss[i]);
162
163 this->graph.addNode(i, weight);
164 }
165
166 hyperedges.resize(this->nvars);
167
168 /* go through all constraints */
169 for( i = 0; i < this->nconss; ++i )
170 {
171 SCIP_VAR** curvars1 = NULL;
172
173 int ncurvars1;
174 SCIP_CALL( SCIPgetConsNVars(scip, conss[i], &ncurvars1, &success) );
175 assert(success);
176 if( ncurvars1 == 0 )
177 continue;
178
179 /*
180 * may work as is, as we are copying the constraint later regardless
181 * if there are variables in it or not
182 */
183 SCIP_CALL( SCIPallocBufferArray(scip, &curvars1, ncurvars1) );
184 SCIP_CALL( SCIPgetConsVars(scip, conss[i], curvars1, ncurvars1, &success) );
185 assert(success);
186
187 /** @todo skip all variables that have a zero coeffient or where all coefficients add to zero */
188 /** @todo Do more then one entry per variable actually work? */
189
190 for( k = 0; k < ncurvars1; ++k )
191 {
192 SCIP_VAR* var1 = NULL;
193 int varIndex1;
194
196 var1 = SCIPvarGetProbvar(curvars1[k]);
197 else
198 var1 = curvars1[k];
199
200 if( !GCGisVarRelevant(var1) )
201 continue;
202
203 assert(var1 != NULL);
204 varIndex1 = SCIPvarGetProbindex(var1);
205 assert(varIndex1 >= 0);
206 assert(varIndex1 < this->nvars);
207
208 hyperedges[varIndex1].insert(hyperedges[varIndex1].end(), i);
209 }
210 SCIPfreeBufferArray(scip, &curvars1);
211 }
212
213 /* go through all variables */
214 for( i = 0; i < this->nvars; ++i )
215 {
216 TCLIQUE_WEIGHT weight;
217
218 /* calculate weight of node */
219 weight = this->weights.calculate(vars[i]);
220
221 this->graph.addHyperedge(hyperedges[i], weight);
222 }
223 this->graph.flush();
224
225 return SCIP_OKAY;
226}
227
228template <class T>
230 DETPROBDATA* detprobdata,
231 PARTIALDECOMP* partialdec
232 )
233{
234 int i;
235 int j;
236 TCLIQUE_WEIGHT weight;
237 std::vector< std::vector<int> > hyperedges;
238 unordered_map<int, int> oldToNewConsIndex;
239 vector<bool> varsBool(partialdec->getNVars(), false); /**< true, if the var will be part of the graph */
240 vector<bool> conssBool(partialdec->getNConss(), false); /**< true, if the cons will be part of the graph */
241 vector<int> conssForGraph; /** stores the conss included by the graph */
242 vector<int> varsForGraph; /** stores the vars included by the graph */
243
244 //fillout conssForGraph and varsForGraph
245 for(int c = 0; c < partialdec->getNOpenconss(); ++c)
246 {
247 int cons = partialdec->getOpenconss()[c];
248 for(int v = 0; v < partialdec->getNOpenvars(); ++v)
249 {
250 int var = partialdec->getOpenvars()[v];
251 for(i = 0; i < detprobdata->getNVarsForCons(cons); ++i)
252 {
253 if(var == detprobdata->getVarsForCons(cons)[i])
254 {
255 varsBool[var] = true;
256 conssBool[cons] = true;
257 }
258 }
259 }
260 }
261
262 for(int v = 0; v < partialdec->getNOpenvars(); ++v)
263 {
264 int var = partialdec->getOpenvars()[v];
265 if(varsBool[var])
266 varsForGraph.push_back(var);
267 }
268 for(int c = 0; c < partialdec->getNOpenconss(); ++c)
269 {
270 int cons = partialdec->getOpenconss()[c];
271 if(conssBool[cons])
272 conssForGraph.push_back(cons);
273 }
274
275 this->nconss = (int)conssForGraph.size();
276 this->nvars = (int)varsForGraph.size();
277
278 /* go through all open constraints */
279 for( i = 0; i < this->nconss; ++i )
280 {
281 int oldConsId = conssForGraph[i];
282
283 /* calculate weight of node */
284 weight = this->weights.calculate(detprobdata->getCons(oldConsId));
285
286 oldToNewConsIndex.insert({oldConsId,i});
287
288 this->graph.addNode(i, weight);
289 }
290
291
292
293 /* go through all open variables */
294 for( i = 0; i < this->nvars; ++i )
295 {
296 std::vector<int> hyperedge;
297 int oldVarId = varsForGraph[i];
298
299 for( j = 0; j < detprobdata->getNConssForVar(oldVarId); ++j )
300 {
301 int oldConsId = detprobdata->getConssForVar(oldVarId)[j];
302 if(!conssBool[oldConsId])
303 continue;
304 hyperedge.insert(hyperedge.end(), oldToNewConsIndex[oldConsId]);
305 }
306 /* calculate weight of hyperedge */
307 weight = this->weights.calculate(detprobdata->getVar(oldVarId));
308 this->graph.addHyperedge(hyperedge, weight);
309 }
310
311
312 this->graph.flush();
313
314 return SCIP_OKAY;
315}
316
317
318template <class T>
320 GCG_DECOMP** decomp /**< decomposition structure to generate */
321 )
322{
323 SCIP_HASHMAP* constoblock;
324 SCIP_CONS** conss;
325 int nblocks;
326 SCIP* scip = GCGgetOrigprob(this->gcg);
327
328 assert(decomp != NULL);
329 std::vector<int> partition = this->getPartition();
330 conss = SCIPgetConss(scip);
331
332 SCIP_CALL( SCIPhashmapCreate(&constoblock, SCIPblkmem(scip), this->nconss) );
333
334 assert((size_t)SCIPgetNConss(scip) == partition.size());
335 nblocks = 1+*std::max_element(partition.begin(), partition.end() );
336
337 for( int c = 0; c < this->nconss; ++c )
338 {
339 int consblock = partition[c]+1;
340
341 SCIP_CALL( SCIPhashmapInsert(constoblock, conss[c], (void*) (size_t) consblock) );
342 }
343
344 SCIP_CALL( GCGdecompCreate(this->gcg, decomp) );
345 SCIP_CALL( GCGfilloutDecompFromConstoblock(this->gcg, *decomp, constoblock, nblocks, FALSE) );
346
347 return SCIP_OKAY;
348}
349
350template <class T>
352 PARTIALDECOMP** firstpartialdec,
353 PARTIALDECOMP** secondpartialdec,
354 DETPROBDATA* detprobdata
355 )
356{
357 SCIP_HASHMAP* constoblock;
358 SCIP_CONS** conss;
359 int nblocks;
360
361 std::vector<int> partition;
362 std::vector<bool> isEmptyBlock;
363 std::vector<int> nEmptyBlocksBefore;
364 SCIP* scip = GCGgetOrigprob(this->gcg);
365
366 if( firstpartialdec == NULL && secondpartialdec == NULL )
367 return SCIP_INVALIDDATA;
368
369 partition = this->getPartition();
370 conss = SCIPgetConss(scip);
371
372 SCIP_CALL( SCIPhashmapCreate(&constoblock, SCIPblkmem(scip), this->nconss) );
373
374 assert((size_t)SCIPgetNConss(scip) == partition.size());
375 nblocks = 1+*std::max_element(partition.begin(), partition.end() );
376
377 /** add data structures to handle empty blocks */
378
379 isEmptyBlock = std::vector<bool>(nblocks, true);
380 nEmptyBlocksBefore = std::vector<int>(nblocks, 0);
381
382 for( int c = 0; c < this->nconss; ++c )
383 {
384 int consblock = partition[c]+1;
385 isEmptyBlock[consblock-1] = false;
386 }
387
388 for(int b1 = 0; b1 < nblocks; ++b1)
389 {
390 if (isEmptyBlock[b1] )
391 {
392 std::cout << "block " << b1 << " is an empty block " << std::endl;
393 for(int b2 = b1+1; b2 < nblocks; ++b2)
394 nEmptyBlocksBefore[b2]++;
395 }
396 }
397
398 for( int c = 0; c < this->nconss; ++c )
399 {
400 int consblock = partition[c]+1;
401 consblock -= nEmptyBlocksBefore[partition[c] ];
402 SCIP_CALL( SCIPhashmapInsert(constoblock, (void*) (size_t) detprobdata->getIndexForCons(conss[c]), (void*) (size_t) consblock) );
403 }
404
405 bool original = detprobdata->isAssignedToOrigProb();
406 if( firstpartialdec != NULL )
407 {
408 (*firstpartialdec) = new PARTIALDECOMP(this->gcg, original);
409 SCIP_CALL((*firstpartialdec)->filloutPartialdecFromConstoblock(constoblock, nblocks));
410 }
411 if( secondpartialdec != NULL )
412 {
413 (*secondpartialdec) = new PARTIALDECOMP(this->gcg, original);
414 SCIP_CALL((*secondpartialdec)->filloutBorderFromConstoblock(constoblock, nblocks));
415 }
416 SCIPhashmapFree(&constoblock);
417
418 return SCIP_OKAY;
419}
420
421template <class T>
423 PARTIALDECOMP* oldpartialdec,
424 PARTIALDECOMP** firstpartialdec,
425 PARTIALDECOMP** secondpartialdec,
426 DETPROBDATA* detprobdata
427 )
428{
429 SCIP_HASHMAP* constoblock;
430 int nblocks;
431 std::vector<bool> isEmptyBlock;
432 std::vector<int> nEmptyBlocksBefore;
433 int nEmptyBlocks = 0;
434 SCIP* scip = GCGgetOrigprob(this->gcg);
435
436 assert(oldpartialdec != NULL);
437
438 if( firstpartialdec == NULL && secondpartialdec == NULL )
439 return SCIP_INVALIDDATA;
440
441 if( this->nconss == 0 )
442 {
443 if( firstpartialdec != NULL )
444 (*firstpartialdec) = NULL;
445 if( secondpartialdec != NULL )
446 (*secondpartialdec) = NULL;
447 return SCIP_OKAY;
448 }
449
450 std::vector<int> partition = this->getPartition();
451
452 //fillout conssForGraph
453 vector<int> conssForGraph; /** stores the conss included by the graph */
454 vector<bool> conssBool(oldpartialdec->getNConss(), false); /**< true, if the cons will be part of the graph */
455 bool found;
456
457 for( int c = 0; c < oldpartialdec->getNOpenconss(); ++c )
458 {
459 int cons = oldpartialdec->getOpenconss()[c];
460 found = false;
461 for( int v = 0; v < oldpartialdec->getNOpenvars() && !found; ++v )
462 {
463 int var = oldpartialdec->getOpenvars()[v];
464 for( int i = 0; i < detprobdata->getNVarsForCons(cons) && !found; ++i )
465 {
466 if( var == detprobdata->getVarsForCons(cons)[i] )
467 {
468 conssBool[cons] = true;
469 found = true;
470 }
471 }
472 }
473 }
474
475 for( int c = 0; c < oldpartialdec->getNOpenconss(); ++c )
476 {
477 int cons = oldpartialdec->getOpenconss()[c];
478 if( conssBool[cons] )
479 conssForGraph.push_back(cons);
480 }
481
482 SCIP_CALL( SCIPhashmapCreate(&constoblock, SCIPblkmem(scip), this->nconss) );
483 nblocks = 1+*std::max_element(partition.begin(), partition.end() );
484 /** add data structures to handle empty blocks */
485
486 isEmptyBlock = std::vector<bool>(nblocks, true);
487 nEmptyBlocksBefore = std::vector<int>(nblocks, 0);
488
489 for( int c = 0; c < this->nconss; ++c )
490 {
491 int consblock = partition[c]+1;
492 isEmptyBlock[consblock-1] = false;
493 }
494
495 for( int b1 = 0; b1 < nblocks; ++b1 )
496 {
497 if (isEmptyBlock[b1] )
498 {
499 nEmptyBlocks++;
500 for( int b2 = b1+1; b2 < nblocks; ++b2 )
501 nEmptyBlocksBefore[b2]++;
502 }
503 }
504
505 for( int c = 0; c < this->nconss; ++c )
506 {
507 int consblock = partition[c]+1;
508 consblock -= nEmptyBlocksBefore[partition[c] ];
509 SCIP_CALL( SCIPhashmapInsert(constoblock, (void*) (size_t) conssForGraph[c], (void*) (size_t) consblock) );
510 }
511
512 nblocks -= nEmptyBlocks;
513
514 if( firstpartialdec != NULL )
515 {
516 (*firstpartialdec) = new PARTIALDECOMP(oldpartialdec);
517 SCIP_CALL((*firstpartialdec)->assignPartialdecFromConstoblock(constoblock, nblocks));
518 }
519 if( secondpartialdec != NULL )
520 {
521 (*secondpartialdec) = new PARTIALDECOMP(oldpartialdec);
522 SCIP_CALL((*secondpartialdec)->assignBorderFromConstoblock(constoblock, nblocks));
523 }
524 SCIPhashmapFree(&constoblock);
525
526 return SCIP_OKAY;
527}
528
529
530} /* namespace gcg */
531
532#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
class storing partialdecs and the problem matrix
class storing (potentially incomplete) decompositions
GCG_EXPORT std::vector< int > & getConssForVar(int varIndex)
returns the constraint indices of the coefficient matrix for a variable
GCG_EXPORT int getIndexForCons(SCIP_CONS *cons)
returns the constraint index related to a SCIP constraint
GCG_EXPORT int getNVarsForCons(int consIndex)
returns the number of variables for a given constraint
GCG_EXPORT int getNConssForVar(int varIndex)
returns the number of constraints for a given variable where the var has a nonzero entry in
GCG_EXPORT SCIP_VAR * getVar(int varIndex)
returns SCIP variable related to a variable index
GCG_EXPORT std::vector< int > & getVarsForCons(int consIndex)
returns the variable indices of the coefficient matrix for a constraint
GCG_EXPORT SCIP_CONS * getCons(int consIndex)
returns the SCIP constraint related to a constraint index
GCG_EXPORT SCIP_Bool isAssignedToOrigProb()
returns true if the matrix structure corresponds to the presolved problem
virtual int getNNodes()
virtual SCIP_RETCODE createPartialdecFromPartition(PARTIALDECOMP **firstpartialdec, PARTIALDECOMP **secondpartialdec, DETPROBDATA *detprobdata)
virtual std::vector< int > getHyperedgeNodes(int i)
SCIP_RETCODE writeToFile(int fd, SCIP_Bool edgeweights)
HypercolGraph(GCG *gcg, Weights w)
virtual SCIP_RETCODE createDecompFromPartition(GCG_DECOMP **decomp)
virtual int getNEdges()
virtual SCIP_RETCODE createFromPartialMatrix(DETPROBDATA *detprobdata, PARTIALDECOMP *partialdec)
virtual SCIP_RETCODE createFromMatrix(SCIP_CONS **conss, SCIP_VAR **vars, int nconss_, int nvars_)
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 int getNVars()
Gets number of vars.
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.
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 SCIP_CALL(x)
#define FALSE
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
SCIP_STAGE SCIPgetStage(SCIP *scip)
SCIP_CONS ** SCIPgetConss(SCIP *scip)
int SCIPgetNConss(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
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
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)
Column hypergraph.
#define NULL
Definition: struct_gcg.h:55
int TCLIQUE_WEIGHT
SCIP_FILECREATEERROR
SCIP_INVALIDDATA
SCIP_WRITEERROR
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE
SCIP_STAGE_TRANSFORMED