Scippy

GCG

Branch-and-Price & Column Generation for Everyone

gcgcgcut.c
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 gcgcgcut.c
30 * @brief methods for working with Chvatal-Gomory cuts
31 * @author Chantal Reinartz Groba
32 * @author Erik Muehmer
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
37#include "scip/scip.h"
38#include "gcg/gcg.h"
41#include "gcg/struct_gcgcol.h"
42
43/** additional data for subset row cuts */
45{
46 int* conssindices; /**< indices of constraints used to create cut */
47 SCIP_Real* weights; /**< weights used to create cut */
48 int nconssindices; /**< number of constraints used to create cut */
49
50};
51
52/**< creates a Chvatal-Gomory cut */
54 GCG* gcg, /**< GCG data structure */
55 GCG_MASTERSEPACUT** mastersepacut, /**< pointer to store separator master cut */
56 GCG_SEPA* sepa, /**< separator creating this cut */
57 GCG_VARHISTORY* varhistory, /**< variables history of Chvatal-Gomory cut */
58 SCIP_Real* weights, /**< weights which were used to create the cut */
59 int* indices, /**< indices of constraints used to create the cut */
60 int n /**< number of constraints used to create the cut */
61 )
62{
63 SCIP* masterscip;
65
66 assert(gcg != NULL);
67 assert(mastersepacut != NULL);
68 assert(n >= 0);
69
70 masterscip = GCGgetMasterprob(gcg);
71
72 SCIP_CALL( SCIPallocBlockMemory(masterscip, &data) );
73 data->nconssindices = n;
74 data->weights = NULL;
75 data->conssindices = NULL;
76
77 if( n > 0 )
78 {
79 SCIP_CALL( SCIPduplicateBlockMemoryArray(masterscip, &(data->weights), weights, n) );
80 SCIP_CALL( SCIPduplicateBlockMemoryArray(masterscip, &(data->conssindices), indices, n) );
81 }
82
83 SCIP_CALL( GCGcreateMastersepacut(gcg, mastersepacut, sepa, varhistory, data) );
84 return SCIP_OKAY;
85}
86
87/** frees data of Chvatal-Gomory cut */
89 GCG* gcg, /**< GCG data structure */
90 GCG_MASTERSEPACUTDATA** data /**< pointer to data of subset row cut */
91 )
92{
93 SCIP* masterscip;
94 if( *data == NULL )
95 return SCIP_OKAY;
96
97 masterscip = GCGgetMasterprob(gcg);
98
99 if( (*data)->nconssindices > 0 )
100 {
101 SCIPfreeBlockMemoryArrayNull(masterscip, &((*data)->weights), (*data)->nconssindices);
102 SCIPfreeBlockMemoryArrayNull(masterscip, &((*data)->conssindices), (*data)->nconssindices);
103 }
104
105 assert((*data)->weights == NULL);
106 assert((*data)->conssindices == NULL);
107
108 SCIPfreeBlockMemory(masterscip, data);
109 *data = NULL;
110
111 return SCIP_OKAY;
112}
113
114
115/**< returns the number of weights of Chvatal-Gomory cut */
117 GCG_MASTERSEPACUT* mastersepacut /**< separator master cut */
118 )
119{
121
122 assert(mastersepacut != NULL);
123
124 data = GCGmastersepacutGetData(mastersepacut);
125 assert(data != NULL);
126
127 return data->nconssindices;
128}
129
130/**< returns the weights of Chvatal-Gomory cut */
132 GCG_MASTERSEPACUT* mastersepacut /**< separator master cut */
133 )
134{
136
137 assert(mastersepacut != NULL);
138
139 data = GCGmastersepacutGetData(mastersepacut);
140 assert(data != NULL);
141
142 return data->weights;
143}
144
145/**< returns the constraint indices of Chvatal-Gomory cut */
147 GCG_MASTERSEPACUT* mastersepacut /**< separator master cut */
148 )
149{
151
152 assert(mastersepacut != NULL);
153
154 data = GCGmastersepacutGetData(mastersepacut);
155 assert(data != NULL);
156
157 return data->conssindices;
158}
159
160/** computes the coefficient of a column for a Chvatal-Gomory cut */
162 GCG* gcg, /**< GCG data structure */
163 GCG_EXTENDEDMASTERCONSDATA* mastercutdata, /**< master separator cut */
164 GCG_COL* gcgcol, /**< gcg column */
165 SCIP_Real* coeff /**< pointer to store the coefficient */
166 )
167{
168 SCIP* scip;
169 SCIP_Real* weights;
170 SCIP_Real* mastercoeffs;
171 int* conssindices;
172 int n;
173 int i;
175
176 assert(gcg != NULL);
177 assert(gcgcol != NULL);
178 assert(GCGcolGetInitializedCoefs(gcgcol));
179
181 cut = GCGextendedmasterconsGetMastersepacut(mastercutdata);
182 mastercoeffs = GCGcolGetMastercoefs(gcgcol);
183 weights = GCGchvatalGomoryCutGetWeights(cut);
184 conssindices = GCGchvatalGomoryCutGetConssIndices(cut);
186 assert(mastercoeffs != NULL);
187 assert(weights != NULL);
188 assert(conssindices != NULL);
189
190 /* use the coefficients of the master constraints to compute coefficient for cut */
191 *coeff = 0.0;
192 for( i = 0; i < n; i++ )
193 {
194 SCIPdebugMessage("w[%i]: %f, i[%i]: %i --> %f\n", i, weights[i], i, conssindices[i], mastercoeffs[conssindices[i]]);
195 *coeff += weights[i] * mastercoeffs[conssindices[i]];
196 }
197
198 *coeff = SCIPfeasFloor(scip, *coeff);
199 SCIPdebugMessage("column coefficient: %f\n", *coeff);
200 return SCIP_OKAY;
201}
202
203/** computes the coefficient of a master variable for a Chvatal-Gomory cut */
205 GCG* gcg, /**< GCG data structure */
206 GCG_EXTENDEDMASTERCONSDATA* mastercutdata, /**< master separator cut */
207 SCIP_VAR** vars, /**< pricing variables which define the master variable */
208 SCIP_Real* vals, /**< values of the pricing variables which define the master variables */
209 int nvars, /**< number of pricing variables which define the master variable */
210 int probnr, /**< index of the pricing problem which generated the master variable */
211 SCIP_Real* coef /**< pointer to store the coefficient */
212 )
213{
214 SCIP* scip;
215 SCIP* pricingscip;
216 GCG_PRICINGMODIFICATION* pricingmod;
217 SCIP_CONS** pricingconss;
218 SCIP_VAR** pricingconsvars;
219 SCIP_Bool success;
220 SCIP_Bool found;
221 SCIP_Real* pricingconscoeffs;
222 int npricingconsvars;
223 int i;
224 int pos;
225
226 assert(gcg != NULL);
227 assert(mastercutdata != NULL);
228
230 *coef = 0.0;
231 pricingmod = GCGextendedmasterconsGetPricingModification(gcg, mastercutdata, probnr);
232
233 /* no pricing modification for this problem: coefficient is zero */
234 if( pricingmod == NULL )
235 {
236 SCIPdebugMessage("no pricing modification for pp%i --> variable coefficient 0\n", probnr);
237 return SCIP_OKAY;
238 }
239
240 pricingscip = GCGgetPricingprob(gcg, probnr);
241 pricingconss = GCGpricingmodificationGetAdditionalConss(pricingmod);
242
243 /* get all the pricing variables and their coefficients in the constraint */
244 SCIP_CALL( SCIPgetConsNVars(pricingscip, pricingconss[0], &npricingconsvars, &success) );
245 assert(success);
246 SCIPallocBufferArray(scip, &pricingconsvars, npricingconsvars);
247 SCIPallocBufferArray(scip, &pricingconscoeffs, npricingconsvars);
248 SCIP_CALL( SCIPgetConsVars(pricingscip, pricingconss[0], pricingconsvars, npricingconsvars, &success) );
249 assert(success);
250 SCIP_CALL( SCIPgetConsVals(pricingscip, pricingconss[0], pricingconscoeffs, npricingconsvars, &success) );
251 assert(success);
252
253#ifndef NDEBUG
254 for( i = 1; i < nvars; i++ )
255 assert(SCIPvarCompare(vars[i-1], vars[i]) < 0);
256#endif
257
258 /* compute w^TAx using the pricing constraint */
259 for( i = 0; i < npricingconsvars; i++ )
260 {
261 if( GCGvarIsInferredPricing(pricingconsvars[i]) )
262 continue;
263
264 found = SCIPsortedvecFindPtr((void**) vars, SCIPvarComp, (void*) pricingconsvars[i], nvars, &pos);
265
266 if( !found )
267 continue;
268
269 *coef += pricingconscoeffs[i] * vals[pos];
270 }
271
272 /* finally, we round down w^TAx */
273 SCIPdebugMessage("variable coefficient %f\n", *coef);
274 *coef = SCIPfeasFloor(scip, *coef);
275
276 /* clean-up */
277 SCIPfreeBufferArray(scip, &pricingconsvars);
278 SCIPfreeBufferArray(scip, &pricingconscoeffs);
279
280 return SCIP_OKAY;
281}
#define SCIP_Bool
#define SCIP_Real
#define SCIP_CALL(x)
SCIP_CONS ** GCGpricingmodificationGetAdditionalConss(GCG_PRICINGMODIFICATION *pricingmodification)
GCG_PRICINGMODIFICATION * GCGextendedmasterconsGetPricingModification(GCG *gcg, GCG_EXTENDEDMASTERCONSDATA *extendedmasterconsdata, int blocknr)
GCG interface methods.
int GCGchvatalGomoryCutGetNWeights(GCG_MASTERSEPACUT *mastersepacut)
Definition: gcgcgcut.c:116
SCIP_RETCODE GCGchvatalGomoryCutGetColumnCoefficient(GCG *gcg, GCG_EXTENDEDMASTERCONSDATA *mastercutdata, GCG_COL *gcgcol, SCIP_Real *coeff)
Definition: gcgcgcut.c:161
SCIP_RETCODE GCGcreateChvatalGomoryCut(GCG *gcg, GCG_MASTERSEPACUT **mastersepacut, GCG_SEPA *sepa, GCG_VARHISTORY *varhistory, SCIP_Real *weights, int *indices, int n)
Definition: gcgcgcut.c:53
int * GCGchvatalGomoryCutGetConssIndices(GCG_MASTERSEPACUT *mastersepacut)
Definition: gcgcgcut.c:146
SCIP_RETCODE GCGfreeChvatalGomoryCutData(GCG *gcg, GCG_MASTERSEPACUTDATA **data)
Definition: gcgcgcut.c:88
SCIP_RETCODE GCGchvatalGomoryCutGetVariableCoefficient(GCG *gcg, GCG_EXTENDEDMASTERCONSDATA *mastercutdata, SCIP_VAR **vars, SCIP_Real *vals, int nvars, int probnr, SCIP_Real *coef)
Definition: gcgcgcut.c:204
SCIP_Real * GCGchvatalGomoryCutGetWeights(GCG_MASTERSEPACUT *mastersepacut)
Definition: gcgcgcut.c:131
GCG_EXPORT SCIP_Bool GCGvarIsInferredPricing(SCIP_VAR *var)
Definition: gcgvar.c:195
SCIP_Real * GCGcolGetMastercoefs(GCG_COL *gcgcol)
Definition: gcgcol.c:509
SCIP_Bool GCGcolGetInitializedCoefs(GCG_COL *gcgcol)
Definition: gcgcol.c:627
GCG_EXPORT SCIP * GCGgetPricingprob(GCG *gcg, int pricingprobnr)
Definition: relax_gcg.c:4075
SCIP * GCGgetMasterprob(GCG *gcg)
Definition: gcg.cpp:99
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)
#define SCIPfreeBufferArray(scip, ptr)
#define SCIPfreeBlockMemory(scip, ptr)
#define SCIPfreeBlockMemoryArrayNull(scip, ptr, num)
#define SCIPallocBlockMemory(scip, ptr)
#define SCIPduplicateBlockMemoryArray(scip, ptr, source, num)
SCIP_Real SCIPfeasFloor(SCIP *scip, SCIP_Real val)
int SCIPvarCompare(SCIP_VAR *var1, SCIP_VAR *var2)
SCIP_Bool SCIPsortedvecFindPtr(void **ptrarray, SCIP_DECL_SORTPTRCOMP((*ptrcomp)), void *val, int len, int *pos)
#define NULL
GCG_MASTERSEPACUTDATA * GCGmastersepacutGetData(GCG_MASTERSEPACUT *mastersepacut)
GCG_MASTERSEPACUT * GCGextendedmasterconsGetMastersepacut(GCG_EXTENDEDMASTERCONSDATA *extendedmasterconsdata)
SCIP_RETCODE GCGcreateMastersepacut(GCG *gcg, GCG_MASTERSEPACUT **mastersepacut, GCG_SEPA *sepa, GCG_VARHISTORY *varhistory, GCG_MASTERSEPACUTDATA *mastersepacutdata)
SCIP_RETCODE SCIPgetConsVals(SCIP *scip, SCIP_CONS *cons, SCIP_Real *vals, int varssize, SCIP_Bool *success)
#define SCIPdebugMessage
SCIP_Real * weights
Definition: gcgcgcut.c:47
Definition: struct_gcg.h:55
data structure to store columns (solutions from a pricing problem)
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE