Scippy

GCG

Branch-and-Price & Column Generation for Everyone

columngraph_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 columngraph_def.h
30 * @brief A row graph where each column is a node and columns are adjacent if they appear in one row
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#ifndef GCG_COLUMNGRAPH_DEF_H_
38#define GCG_COLUMNGRAPH_DEF_H_
39
40#include "graph/columngraph.h"
41#include <algorithm>
42#include <utility>
43#include <vector>
44
45namespace gcg {
46
47template <class T>
49 GCG* gcgstruct, /**< GCG data structure */
50 Weights w /**< weights for the given graph */
51 ) : MatrixGraph<T>(gcgstruct, w), graph(gcgstruct)
52{
53 this->graphiface = &graph;
54 this->name = std::string("columngraph");
55}
56
57template <class T>
59{
60 // TODO Auto-generated destructor stub
61}
62
63template <class T>
65 GCG_DECOMP** decomp
66 )
67{
68 int nblocks;
69 SCIP_HASHMAP* constoblock = NULL;
70 SCIP* scip = GCGgetOrigprob(this->gcg);
71 int* nsubscipconss = NULL;
72 int i;
73 SCIP_CONS **conss;
74 SCIP_Bool emptyblocks = FALSE;
75 std::vector<int> partition = graph.getPartition();
76 conss = SCIPgetConss(scip);
77 nblocks = *(std::max_element(partition.begin(), partition.end()))+1;
78
79 SCIP_CALL( SCIPallocBufferArray(scip, &nsubscipconss, nblocks) );
80 BMSclearMemoryArray(nsubscipconss, nblocks);
81
82 SCIP_CALL( SCIPhashmapCreate(&constoblock, SCIPblkmem(scip), this->nconss) );
83
84 /* assign constraints to partition */
85 for( i = 0; i < this->nconss; i++ )
86 {
87 int block = partition[i];
88 SCIP_CALL( SCIPhashmapInsert(constoblock, conss[i], (void*) (size_t) (block +1)) );
89 ++(nsubscipconss[block]);
90 }
91
92 /* first, make sure that there are constraints in every block, otherwise the hole thing is useless */
93 for( i = 0; i < nblocks; ++i )
94 {
95 if( nsubscipconss[i] == 0 )
96 {
97 SCIPdebugMessage("Block %d does not have any constraints!\n", i);
98 emptyblocks = TRUE;
99 }
100 }
101
102 if( !emptyblocks )
103 {
104 SCIP_CALL( GCGdecompCreate(this->gcg, decomp) );
105 SCIP_CALL( GCGfilloutDecompFromConstoblock(this->gcg, *decomp, constoblock, nblocks, FALSE) );
106 }
107 else {
108 SCIPhashmapFree(&constoblock);
109 *decomp = NULL;
110 }
111
112 SCIPfreeBufferArray(scip, &nsubscipconss);
113 return SCIP_OKAY;
114}
115
116
117template <class T>
119 SCIP_CONS** conss, /**< constraints for which graph should be created */
120 SCIP_VAR** vars, /**< variables for which graph should be created */
121 int nconss_, /**< number of constraints */
122 int nvars_ /**< number of variables */
123 )
124{
125 int i;
126 int j;
127 int k;
128 SCIP_Bool success;
129 std::pair< int, int> edge;
130 std::vector< std::pair< int, int> > edges;
131 SCIP* scip = GCGgetOrigprob(this->gcg);
132
133 assert(conss != NULL);
134 assert(vars != NULL);
135 assert(nvars_ > 0);
136 assert(nconss_ > 0);
137
138 this->nvars = nvars_;
139 this->nconss = nconss_;
140
141 /* go through all variables */
142 for( i = 0; i < this->nvars; ++i )
143 {
144 TCLIQUE_WEIGHT weight;
145
146 /* calculate weight of node */
147 weight = this->weights.calculate(vars[i]);
148
149 this->graph.addNode(i, weight);
150 }
151
152 /* go through all constraints */
153 for( i = 0; i < this->nconss; ++i )
154 {
155 SCIP_VAR** curvars = NULL;
156
157 int ncurvars;
158 SCIP_CALL( SCIPgetConsNVars(scip, conss[i], &ncurvars, &success) );
159 assert(success);
160 if( ncurvars == 0 )
161 continue;
162
163 /*
164 * may work as is, as we are copying the constraint later regardless
165 * if there are variables in it or not
166 */
167 SCIP_CALL( SCIPallocBufferArray(scip, &curvars, ncurvars) );
168 SCIP_CALL( SCIPgetConsVars(scip, conss[i], curvars, ncurvars, &success) );
169 assert(success);
170
171 /** @todo skip all variables that have a zero coeffient or where all coefficients add to zero */
172 /** @todo Do more then one entry per variable actually work? */
173
174 for( j = 0; j < ncurvars; ++j )
175 {
176 SCIP_VAR* var1;
177 int varIndex1;
178
180 var1 = SCIPvarGetProbvar(curvars[j]);
181 else
182 var1 = curvars[j];
183
184 if( !GCGisVarRelevant(var1) )
185 continue;
186
187 assert(var1 != NULL);
188 varIndex1 = SCIPvarGetProbindex(var1);
189 assert(varIndex1 >= 0);
190 assert(varIndex1 < this->nvars);
191
192 for( k = 0; k < j; ++k )
193 {
194 SCIP_VAR* var2;
195 int varIndex2;
196
198 var2 = SCIPvarGetProbvar(curvars[k]);
199 else
200 var2 = curvars[k];
201
202 if( !GCGisVarRelevant(var2) )
203 continue;
204
205 assert(var2 != NULL);
206 varIndex2 = SCIPvarGetProbindex(var2);
207 assert(varIndex2 >= 0);
208 assert(varIndex2 < this->nvars);
209
210 edge = std::make_pair(MIN(varIndex1, varIndex2), MAX(varIndex1, varIndex2) );
211
212 /* check if edge was not already added to graph */
213 if(edges.end() == std::find(edges.begin(), edges.end(), edge) )
214 {
215
216 SCIP_CALL( this->graph.addEdge(varIndex1, varIndex2) );
217 edges.push_back(edge);
218 std::sort(edges.begin(), edges.end());
219 }
220 }
221 }
222 SCIPfreeBufferArray(scip, &curvars);
223 }
224
225 this->graph.flush();
226 return SCIP_OKAY;
227}
228
229} /* namespace gcg */
230
231#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
virtual ~ColumnGraph()
virtual SCIP_RETCODE createFromMatrix(SCIP_CONS **conss, SCIP_VAR **vars, int nconss_, int nvars_)
virtual SCIP_RETCODE createDecompFromPartition(GCG_DECOMP **decomp)
ColumnGraph(GCG *gcg, Weights w)
std::string name
Definition: matrixgraph.h:57
GraphInterface * graphiface
Definition: matrixgraph.h:64
A row graph where each column is a node and columns are adjacent if they appear in a row.
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
Definition: struct_gcg.h:55
int TCLIQUE_WEIGHT
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE
SCIP_STAGE_TRANSFORMED