GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
cons_integralorig.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 cons_integralorig.c
30 *
31 * @brief constraint handler for enforcing integrality of the transferred master solution in the original problem
32 * @author Gerald Gamrath
33 * Marcel Schmickerath
34 */
35
36/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
37/* #define SCIP_DEBUG */
38#include <assert.h>
39#include <string.h>
40
42#include "gcg/pricer_gcg.h"
44#include "gcg/pub_gcgvar.h"
45#include "scip/struct_branch.h"
46#include "gcg/relax_gcg.h"
47#include "gcg/gcg.h"
48
49#include "gcg/branch_orig.h"
50
51#define CONSHDLR_NAME "integralorig"
52#define CONSHDLR_DESC "integrality constraint"
53#define CONSHDLR_ENFOPRIORITY 1000 /**< priority of the constraint handler for constraint enforcing */
54#define CONSHDLR_CHECKPRIORITY 1000 /**< priority of the constraint handler for checking feasibility */
55#define CONSHDLR_EAGERFREQ -1 /**< frequency for using all instead of only the useful constraints in separation,
56 * propagation and enforcement, -1 for no eager evaluations, 0 for first only */
57#define CONSHDLR_NEEDSCONS FALSE /**< should the constraint handler be skipped, if no constraints are available? */
58
59
60/** constraint handler data */
62{
63 GCG* gcg; /**< GCG data structure */
64 SCIP_BRANCHRULE** branchrules; /**< stack for storing active branchrules */
65 int nbranchrules; /**< number of active branchrules */
66};
67
68/** insert branchrule in constraint handler data */
70 GCG* gcg,
71 SCIP_BRANCHRULE* branchrule
72 )
73{
74 SCIP_CONSHDLR* conshdlr;
75 SCIP_CONSHDLRDATA* conshdlrdata;
77
80 assert(conshdlr != NULL);
81
82 conshdlrdata = SCIPconshdlrGetData(conshdlr);
83 assert(conshdlrdata != NULL);
84
85 assert(conshdlrdata->nbranchrules >= 0);
86 if( conshdlrdata->nbranchrules == 0 )
87 {
88 SCIP_CALL( SCIPallocMemoryArray(masterprob, &(conshdlrdata->branchrules), 1) ); /*lint !e506*/
89 conshdlrdata->nbranchrules = 1;
90 }
91 else
92 {
93 SCIP_CALL( SCIPreallocMemoryArray(masterprob, &(conshdlrdata->branchrules), (size_t)conshdlrdata->nbranchrules+1) );
94 ++conshdlrdata->nbranchrules;
95 }
96
97 assert(conshdlrdata->nbranchrules > 0);
98
99 conshdlrdata->branchrules[conshdlrdata->nbranchrules-1] = branchrule;
100
101 return SCIP_OKAY;
102}
103
104/** sort branchrules with respect to priority */
105static
107 SCIP_BRANCHRULE** branchrules,
108 int nbranchrules
109 )
110{
111 SCIP_BRANCHRULE* tmp;
112 int pos;
113 int i;
114
115 tmp = NULL;
116
117 assert(nbranchrules >= 0);
118 assert(branchrules != NULL);
119
120 for( pos=0; pos<nbranchrules; ++pos )
121 {
122 int maxi = pos;
123 for( i=pos+1; i<nbranchrules; ++i )
124 {
125 if( branchrules[i]->priority > branchrules[maxi]->priority )
126 {
127 maxi = i;
128 }
129 }
130 if( maxi != pos )
131 {
132 tmp = branchrules[pos];
133 branchrules[pos] = branchrules[maxi];
134 branchrules[maxi] = tmp;
135 }
136 }
137}
138
139/*
140 * Callback methods
141 */
142
143/** constraint enforcing method of constraint handler for LP solutions */
144static
145SCIP_DECL_CONSENFOLP(consEnfolpIntegralOrig)
146{ /*lint --e{715}*/
147 SCIP* origprob;
148 SCIP_Bool discretization;
149 int i;
150 SCIP_CONSHDLRDATA* conshdlrdata;
151
152 assert(conshdlr != NULL);
153 assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
154 assert(scip != NULL);
155 assert(conss == NULL);
156 assert(nconss == 0);
157 assert(result != NULL);
158 assert(conshdlr != NULL);
159 assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
160 assert(scip != NULL);
161
162 conshdlrdata = SCIPconshdlrGetData(conshdlr);
163 assert(conshdlrdata != NULL);
164 origprob = GCGgetOrigprob(conshdlrdata->gcg);
165 assert(origprob != NULL);
166
167 SCIPdebugMessage("LP solution enforcing method of integralorig constraint\n");
168
170
171 /* if we use the discretization without continuous variables, we do not have to check for integrality of the solution in the
172 * original variable space, we obtain it by enforcing integrality of the master solution*/
173 SCIP_CALL( SCIPgetBoolParam(origprob, "relaxing/gcg/discretization", &discretization) );
174 if( discretization && SCIPgetNContVars(origprob) == 0 )
175 {
176 return SCIP_OKAY;
177 }
178
179 /* if the transferred master solution is feasible, the current node is solved to optimality and can be pruned */
180 if( GCGrelaxIsOrigSolFeasible(conshdlrdata->gcg) )
181 {
182 SCIPdebugMessage("Orig sol is feasible\n");
184 return SCIP_OKAY;
185 }
186
187 sortBranchrules(conshdlrdata->branchrules, conshdlrdata->nbranchrules);
188
189 i = 0;
190
191 while( *result != SCIP_BRANCHED &&
192 *result != SCIP_REDUCEDDOM &&
193 i < conshdlrdata->nbranchrules )
194 {
195 assert(conshdlrdata->branchrules[i] != NULL);
196
197 if( conshdlrdata->branchrules[i]->branchexeclp == NULL )
198 {
199 ++i;
200 continue;
201 }
202
203 SCIPdebugMessage("Call exec lp method of %s\n", SCIPbranchruleGetName(conshdlrdata->branchrules[i]));
204 /** todo handle bool allowaddcons; here default TRUE */
205 SCIP_CALL( conshdlrdata->branchrules[i]->branchexeclp(scip, conshdlrdata->branchrules[i], TRUE, result) );
206 ++i;
207 }
208
209 return SCIP_OKAY;
210
211}
212
213
214/** constraint enforcing method of constraint handler for pseudo solutions */
215static
216SCIP_DECL_CONSENFOPS(consEnfopsIntegralOrig)
217{ /*lint --e{715}*/
218 SCIP* origprob;
219 SCIP_Bool discretization;
220 SCIP_CONSHDLRDATA* conshdlrdata;
221 int i;
222
223 assert(conshdlr != NULL);
224 assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
225 assert(scip != NULL);
226 assert(conss == NULL);
227 assert(nconss == 0);
228 assert(result != NULL);
229
230 conshdlrdata = SCIPconshdlrGetData(conshdlr);
231 assert(conshdlrdata != NULL);
232 origprob = GCGgetOrigprob(conshdlrdata->gcg);
233 assert(origprob != NULL);
234
236 i = 0;
237
238 /* if we use the discretization without continuous variables, we do not have to check for integrality of the solution in the
239 * original variable space, we obtain it by enforcing integrality of the master solution*/
240 SCIP_CALL( SCIPgetBoolParam(origprob, "relaxing/gcg/discretization", &discretization) );
241 if( discretization && SCIPgetNContVars(origprob) == 0 )
242 {
243 return SCIP_OKAY;
244 }
245
247
248 sortBranchrules(conshdlrdata->branchrules, conshdlrdata->nbranchrules);
249
250 while( *result != SCIP_BRANCHED && i < conshdlrdata->nbranchrules )
251 {
252 assert(conshdlrdata->branchrules[i] != NULL);
253
254 if( conshdlrdata->branchrules[i]->branchexecps == NULL )
255 {
256 ++i;
257 continue;
258 }
259 /** todo handle bool allowaddcons; here default TRUE */
260 SCIP_CALL( conshdlrdata->branchrules[i]->branchexecps(scip, conshdlrdata->branchrules[i], TRUE, result) );
261 ++i;
262 }
263
264 return SCIP_OKAY;
265}
266
267
268/** feasibility check method of constraint handler for integral solutions */
269static
270SCIP_DECL_CONSCHECK(consCheckIntegralOrig)
271{ /*lint --e{715}*/
272 SCIP* origprob;
273 SCIP_SOL* origsol;
274 SCIP_VAR** origvars;
275 int norigvars;
277 SCIP_Bool discretization;
278 int v;
279 SCIP_Bool violatesvarbnds;
280 SCIP_CONSHDLRDATA* conshdlrdata;
281
282 assert(conshdlr != NULL);
283 assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
284 assert(scip != NULL);
285
286 conshdlrdata = SCIPconshdlrGetData(conshdlr);
287 assert(conshdlrdata != NULL);
288 origprob = GCGgetOrigprob(conshdlrdata->gcg);
289 assert(origprob != NULL);
290
291 SCIPdebugMessage("Check method of integralorig constraint\n");
292
294
295 /* if we use the discretization without continuous variables, we do not have to check for integrality of the solution in the
296 * original variable space, we obtain it by enforcing integrality of the master solution*/
297 SCIP_CALL( SCIPgetBoolParam(origprob, "relaxing/gcg/discretization", &discretization) );
298 if( discretization && SCIPgetNContVars(origprob) == 0 )
299 {
300 return SCIP_OKAY;
301 }
302
303 /* get corresponding origsol in order to check integrality */
304 SCIP_CALL( GCGtransformMastersolToOrigsol(conshdlrdata->gcg, sol, &origsol, TRUE, &violatesvarbnds) );
305
306 if( violatesvarbnds )
307 {
309 SCIPfreeSol(origprob, &origsol);
310 return SCIP_OKAY;
311 }
312
313 origvars = SCIPgetVars(origprob);
314 norigvars = SCIPgetNVars(origprob);
315
316 /* check for each integral original variable whether it has a fractional value */
317 for( v = 0; v < norigvars && *result == SCIP_FEASIBLE; v++ )
318 {
319 if( SCIPvarGetType(origvars[v]) == SCIP_VARTYPE_CONTINUOUS )
320 continue;
321
322 solval = 0.0;
323 assert(GCGvarIsOriginal(origvars[v]));
324
325 solval = SCIPgetSolVal(origprob, origsol, origvars[v]);
326
328 {
330
331 if( printreason )
332 {
333 SCIPinfoMessage(scip, NULL, "violation: integrality condition of variable <%s> = %.15g\n",
334 SCIPvarGetName(origvars[v]), solval);
335 }
336 }
337 }
338
339 SCIPfreeSol(origprob, &origsol);
340
341 return SCIP_OKAY;
342}
343
344
345/** variable rounding lock method of constraint handler */
346static
347SCIP_DECL_CONSLOCK(consLockIntegralOrig)
348{ /*lint --e{715}*/
349 return SCIP_OKAY;
350}
351
352/** destructor of constraint handler to free constraint handler data (called when SCIP is exiting) */
353static
354SCIP_DECL_CONSFREE(consFreeIntegralOrig)
355{
356 SCIP_CONSHDLRDATA* conshdlrdata;
357
358 assert(scip != NULL);
359 assert(conshdlr != NULL);
360 assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
361
362 conshdlrdata = SCIPconshdlrGetData(conshdlr);
363 assert(conshdlrdata != NULL);
364
365 SCIPdebugMessage("freeing integralorig constraint handler\n");
366
367 if( conshdlrdata->nbranchrules > 0 )
368 {
369 assert(conshdlrdata->branchrules != NULL);
370 SCIPfreeMemoryArray(scip, &(conshdlrdata->branchrules) );
371 conshdlrdata->branchrules = NULL;
372 conshdlrdata->nbranchrules = 0;
373 }
374
375 /* free constraint handler storage */
376 assert(conshdlrdata->branchrules == NULL);
377 SCIPfreeMemory(scip, &conshdlrdata);
378
379 return SCIP_OKAY;
380}
381
382/*
383 * constraint specific interface methods
384 */
385
386/** creates the handler for integrality constraint and includes it in SCIP */
388 GCG* gcg /**< GCG data structure */
389 )
390{
391 SCIP_CONSHDLR* conshdlr;
392 SCIP_CONSHDLRDATA* conshdlrdata;
394
395 assert(gcg != NULL);
396
397 /* create integral constraint handler data */
400 conshdlrdata = NULL;
401 SCIP_CALL( SCIPallocMemory(masterprob, &conshdlrdata) );
402 conshdlrdata->gcg = gcg;
403 conshdlrdata->branchrules = NULL;
404 conshdlrdata->nbranchrules = 0;
405
406 /* include constraint handler */
409 consEnfolpIntegralOrig, consEnfopsIntegralOrig, consCheckIntegralOrig, consLockIntegralOrig,
410 conshdlrdata) );
411 assert(conshdlr != NULL);
412
413 SCIP_CALL( SCIPsetConshdlrFree(masterprob, conshdlr, consFreeIntegralOrig) );
414
415 return SCIP_OKAY;
416}
SCIP_DECL_CONSENFOPS(ConshdlrSubtour::scip_enfops)
SCIP_DECL_CONSLOCK(ConshdlrSubtour::scip_lock)
SCIP_DECL_CONSCHECK(ConshdlrSubtour::scip_check)
SCIP_DECL_CONSENFOLP(ConshdlrSubtour::scip_enfolp)
branching rule for original problem in GCG
#define CONSHDLR_NEEDSCONS
#define CONSHDLR_CHECKPRIORITY
#define CONSHDLR_DESC
#define CONSHDLR_EAGERFREQ
#define CONSHDLR_ENFOPRIORITY
#define CONSHDLR_NAME
SCIP_RETCODE GCGincludeConshdlrIntegralOrig(GCG *gcg)
static SCIP_DECL_CONSFREE(consFreeIntegralOrig)
SCIP_RETCODE GCGconsIntegralorigAddBranchrule(GCG *gcg, SCIP_BRANCHRULE *branchrule)
static void sortBranchrules(SCIP_BRANCHRULE **branchrules, int nbranchrules)
constraint handler for the integrality constraint
constraint handler for storing the branching decisions at each node of the tree
#define SCIP_Bool
#define SCIP_Real
#define TRUE
GCG interface methods.
SCIP_Bool GCGvarIsOriginal(SCIP_VAR *var)
Definition gcgvar.c:231
SCIP_RETCODE GCGtransformMastersolToOrigsol(GCG *gcg, SCIP_SOL *mastersol, SCIP_SOL **origsol, SCIP_Bool ignorelocalvarbnds, SCIP_Bool *violatesvarbnds)
Definition misc.c:135
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
SCIP * GCGgetMasterprob(GCG *gcg)
Definition gcg.cpp:99
int SCIPgetNContVars(SCIP *scip)
int SCIPgetNVars(SCIP *scip)
SCIP_VAR ** SCIPgetVars(SCIP *scip)
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
const char * SCIPbranchruleGetName(SCIP_BRANCHRULE *branchrule)
int SCIPgetNPseudoBranchCands(SCIP *scip)
SCIP_RETCODE SCIPincludeConshdlrBasic(SCIP *scip, SCIP_CONSHDLR **conshdlrptr, const char *name, const char *desc, int enfopriority, int chckpriority, int eagerfreq, SCIP_Bool needscons, SCIP_DECL_CONSENFOLP((*consenfolp)), SCIP_DECL_CONSENFOPS((*consenfops)), SCIP_DECL_CONSCHECK((*conscheck)), SCIP_DECL_CONSLOCK((*conslock)), SCIP_CONSHDLRDATA *conshdlrdata)
SCIP_RETCODE SCIPsetConshdlrFree(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSFREE((*consfree)))
const char * SCIPconshdlrGetName(SCIP_CONSHDLR *conshdlr)
SCIP_CONSHDLR * SCIPfindConshdlr(SCIP *scip, const char *name)
SCIP_CONSHDLRDATA * SCIPconshdlrGetData(SCIP_CONSHDLR *conshdlr)
#define SCIPreallocMemoryArray(scip, ptr, newnum)
#define SCIPallocMemoryArray(scip, ptr, num)
#define SCIPallocMemory(scip, ptr)
#define SCIPfreeMemoryArray(scip, ptr)
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
SCIP_Bool SCIPisFeasIntegral(SCIP *scip, SCIP_Real val)
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
const char * SCIPvarGetName(SCIP_VAR *var)
SCIPfreeMemory(GCGgetOrigprob(gcg), &divingdata)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
SCIP * origprob
* result
SCIP_Real solval
SCIPdebugMessage("feasibility pump finished [%d iterations done].\n", nloops)
int i
SCIP_SOL * sol
#define NULL
GCG variable pricer.
public methods for GCG variables
SCIP_Bool GCGrelaxIsOrigSolFeasible(GCG *gcg)
Definition relax_gcg.c:4328
GCG relaxator.
SCIP_BRANCHRULE ** branchrules
struct SCIP_Branchrule SCIP_BRANCHRULE
struct SCIP_ConshdlrData SCIP_CONSHDLRDATA
struct SCIP_Conshdlr SCIP_CONSHDLR
struct Gcg GCG
Definition type_gcg.h:44
SCIP_FEASIBLE
SCIP_REDUCEDDOM
SCIP_BRANCHED
SCIP_INFEASIBLE
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
struct SCIP_Sol SCIP_SOL
struct SCIP_Var SCIP_VAR
SCIP_VARTYPE_CONTINUOUS