GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
bipartitegraph_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 bipartitegraph_def.h
30 * @brief A bipartite graph
31 * @author Martin Bergner
32 */
33
34/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35
36#ifndef GCG_BIPARTITEGRAPH_DEF_H_
37#define GCG_BIPARTITEGRAPH_DEF_H_
38
40#include "gcg/scip_misc.h"
41
42namespace gcg {
43
44template <class T>
46 GCG* gcgstruct, /**< GCG data structure */
47 Weights w /**< weights for the given graph */
48 ): MatrixGraph<T>(gcgstruct,w), graph(gcgstruct)
49{
50 this->graphiface = &graph;
51 this->name = std::string("bipartite");
52}
53
54template <class T>
59
60
61/**
62 * Builds a bipartite graph structure out of the matrix.
63 *
64 * The function will create an node for every constraint and every variable.
65 * A constraint and a variable are adjacent if the variable appears in the constraint variable array.
66 *
67 * @todo The nonzeroness is not checked, all variables in the variable array are considered
68 */
69template <class T>
71 SCIP_CONS** conss, /**< constraints for which graph should be created */
72 SCIP_VAR** vars, /**< variables for which graph should be created */
73 int nconss_, /**< number of constraints */
74 int nvars_ /**< number of variables */
75 )
76{
77 int i;
78 int j;
80 SCIP* scip = GCGgetOrigprob(this->gcg);
81
82 assert(conss != NULL);
83 assert(vars != NULL);
84 assert(nvars_ > 0);
85 assert(nconss_ > 0);
86 this->nvars = nvars_;
87 this->nconss = nconss_;
88
89 for( i = 0; i < this->nvars + this->nconss; ++i )
90 {
91 TCLIQUE_WEIGHT weight;
92
93 /* note that the first nvars nodes correspond to variables */
94 if( i < this->nvars )
95 weight = this->weights.calculate(vars[i]);
96 else
97 weight = this->weights.calculate(conss[i-this->nvars]);
98
99 this->graph.addNode(i, weight);
100 }
101
102 /* go through all constraints */
103 for( i = 0; i < this->nconss; ++i )
104 {
105 SCIP_VAR** curvars = NULL;
106
107 int ncurvars;
108 SCIP_CALL( SCIPgetConsNVars(scip, conss[i], &ncurvars, &success) );
110 if( ncurvars == 0 )
111 continue;
112
113 /*
114 * may work as is, as we are copying the constraint later regardless
115 * if there are variables in it or not
116 */
117 SCIP_CALL( SCIPallocBufferArray(scip, &curvars, ncurvars) );
118 SCIP_CALL( SCIPgetConsVars(scip, conss[i], curvars, ncurvars, &success) );
120
121 /** @todo skip all variables that have a zero coeffient or where all coefficients add to zero */
122 /** @todo Do more then one entry per variable actually work? */
123
124 for( j = 0; j < ncurvars; ++j )
125 {
126 SCIP_VAR* var = NULL;
127 int varIndex;
128
130 var = SCIPvarGetProbvar(curvars[j]);
131 else
132 var = curvars[j];
133
134 if( !GCGisVarRelevant(var) )
135 continue;
136
137 assert(var != NULL);
138 varIndex = SCIPvarGetProbindex(var);
139 assert(varIndex >= 0);
140 assert(varIndex < this->nvars);
141
142 SCIP_CALL( this->graph.addEdge(varIndex, this->nvars+i) );
143 }
144 SCIPfreeBufferArray(scip, &curvars);
145 }
146
147 this->graph.flush();
148 return SCIP_OKAY;
149}
150
151
152template <class T>
154 DETPROBDATA* detprobdata,
155 PARTIALDECOMP* partialdec
156 ){
157
158 int i;
159 int j;
160 unordered_map<int, int> oldToNewVarIndex;
161 unordered_map<int, int> oldToNewConsIndex;
162 std::vector<bool> varsBool(partialdec->getNVars(), false); /* true, if the var will be part of the graph */
163 std::vector<bool> conssBool(partialdec->getNConss(), false); /* true, if the cons will be part of the graph */
164 std::vector<int> conssForGraph; /* stores the conss included by the graph */
165 std::vector<int> varsForGraph; /* stores the vars included by the graph */
166
167 //fillout conssForGraph and varsForGraph
168 for(int c = 0; c < partialdec->getNOpenconss(); ++c)
169 {
170 int cons = partialdec->getOpenconss()[c];
171 for(int v = 0; v < partialdec->getNOpenvars(); ++v)
172 {
173 int var = partialdec->getOpenvars()[v];
174 for(i = 0; i < detprobdata->getNVarsForCons(cons); ++i)
175 {
176 if(var == detprobdata->getVarsForCons(cons)[i])
177 {
178 varsBool[var] = true;
179 conssBool[cons] = true;
180 }
181 }
182 }
183 }
184
185 for(int v = 0; v < partialdec->getNOpenvars(); ++v)
186 {
187 int var = partialdec->getOpenvars()[v];
188 if(varsBool[var])
189 varsForGraph.push_back(var);
190 }
191 for(int c = 0; c < partialdec->getNOpenconss(); ++c)
192 {
193 int cons = partialdec->getOpenconss()[c];
194 if(conssBool[cons])
195 conssForGraph.push_back(cons);
196 }
197
198 this->nconss = (int)conssForGraph.size();
199 this->nvars = (int)varsForGraph.size();
200
201
202 /* add node for every var */
203 for( i = 0 ; i < this->nvars; ++i )
204 {
205 TCLIQUE_WEIGHT weight;
206 int var = varsForGraph[i];
207
208 /* note that the first nvars nodes correspond to variables */
209 weight = this->weights.calculate(detprobdata->getVar(var));
210 oldToNewVarIndex.insert({var,i});
211 this->graph.addNode(i, weight);
212 }
213
214
215 /* add node for every cons */
216 for( j = 0 ; j < this->nconss; ++j )
217 {
218 TCLIQUE_WEIGHT weight;
219 int cons = conssForGraph[j];
220
221 /* note that the first nvars nodes correspond to variables (legacy implementation) */
222 weight = this->weights.calculate(detprobdata->getCons(cons));
223 oldToNewConsIndex.insert({cons, j});
224 this->graph.addNode( this->nvars + j, weight);
225 }
226
227 /* go through all open constraints */
228 for( i = 0; i < this->nconss; ++i )
229 {
230 int oldConsId = conssForGraph[i];
231
232 for( j = 0; j < detprobdata->getNVarsForCons(oldConsId); ++j )
233 {
234 int oldVarId = detprobdata->getVarsForCons(oldConsId)[j];
235 if(!varsBool[oldVarId])
236 continue;
237 SCIP_CALL( this->graph.addEdge(oldToNewVarIndex[oldVarId], this->nvars+i) );
238 }
239 }
240
241 this->graph.flush();
242 return SCIP_OKAY;
243 }
244
245
246template <class T>
248{
249 return this->nconss;
250}
251
252template <class T>
254{
255 return this->nvars;
256}
257
258
259} /* namespace gcg */
260
261#endif
SCIP_VAR * w
virtual SCIP_RETCODE createFromMatrix(SCIP_CONS **conss, SCIP_VAR **vars, int nconss_, int nvars_)
BipartiteGraph(GCG *gcgstruct, Weights w)
virtual SCIP_RETCODE createFromPartialMatrix(DETPROBDATA *detprobdata, PARTIALDECOMP *partialdec)
GCG_EXPORT int getNVarsForCons(int consIndex)
returns the number of variables for a given constraint
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
MatrixGraph(GCG *gcgstruct, Weights w)
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.
#define SCIP_Bool
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
SCIP_STAGE SCIPgetStage(SCIP *scip)
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)
#define SCIPallocBufferArray(scip, ptr, num)
SCIP_VAR * SCIPvarGetProbvar(SCIP_VAR *var)
int SCIPvarGetProbindex(SCIP_VAR *var)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
SCIP_VAR * var
SCIP_VAR ** vars
int j
SCIP_Bool success
int i
SCIPfreeBufferArray(scip, &cycles)
#define NULL
various SCIP helper methods
int TCLIQUE_WEIGHT
struct SCIP_Cons SCIP_CONS
struct Gcg GCG
Definition type_gcg.h:44
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
SCIP_STAGE_TRANSFORMED
struct SCIP_Var SCIP_VAR