Scippy

GCG

Branch-and-Price & Column Generation for Everyone

heur_gcgsimplerounding.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 heur_gcgsimplerounding.c
30 * @brief simple and fast LP rounding heuristic
31 * @author Tobias Achterberg
32 * @author Christian Puchert
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
37#include <assert.h>
38#include <string.h>
39
41#include "gcg/gcg.h"
42
43
44#define HEUR_NAME "gcgsimplerounding"
45#define HEUR_DESC "simple and fast LP rounding heuristic on original variables"
46#define HEUR_DISPCHAR 'r'
47#define HEUR_PRIORITY 0
48#define HEUR_FREQ 1
49#define HEUR_FREQOFS 0
50#define HEUR_MAXDEPTH -1
51#define HEUR_TIMING SCIP_HEURTIMING_AFTERNODE
52#define HEUR_USESSUBSCIP FALSE
53
54
55/* locally defined heuristic data */
56struct SCIP_HeurData
57{
58 GCG* gcg; /**< GCG data structure */
59 SCIP_SOL* sol; /**< working solution */
60 SCIP_Longint lastlp; /**< last LP number where the heuristic was applied */
61 int nroundablevars; /**< number of variables that can be rounded (-1 if not yet calculated) */
62};
63
64
65
66
67/*
68 * Callback methods
69 */
70
71/** copy method for primal heuristic plugins (called when SCIP copies plugins) */
72#define heurCopyGcgsimplerounding NULL
73
74/** destructor of primal heuristic to free user data (called when SCIP is exiting) */
75static
76SCIP_DECL_HEURFREE(heurFreeGcgsimplerounding) /*lint --e{715}*/
77{ /*lint --e{715}*/
78 SCIP_HEURDATA* heurdata;
79
80 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
81
82 heurdata = SCIPheurGetData(heur);
83 assert(heurdata != NULL);
84
85 SCIPfreeMemory(scip, &heurdata);
86
87 return SCIP_OKAY;
88}
89
90
91/** initialization method of primal heuristic (called after problem was transformed) */
92static
93SCIP_DECL_HEURINIT(heurInitGcgsimplerounding) /*lint --e{715}*/
94{ /*lint --e{715}*/
95 SCIP_HEURDATA* heurdata;
96
97 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
98
99 heurdata = SCIPheurGetData(heur);
100 assert(heurdata != NULL);
101
102 /* create heuristic data */
103 SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
104 heurdata->lastlp = -1;
105 heurdata->nroundablevars = -1;
106
107 return SCIP_OKAY;
108}
109
110
111/** deinitialization method of primal heuristic (called before transformed problem is freed) */
112static
113SCIP_DECL_HEUREXIT(heurExitGcgsimplerounding) /*lint --e{715}*/
114{ /*lint --e{715}*/
115 SCIP_HEURDATA* heurdata;
116
117 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
118
119 /* free heuristic data */
120 heurdata = SCIPheurGetData(heur);
121 assert(heurdata != NULL);
122 SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
123
124 return SCIP_OKAY;
125}
126
127
128/** solving process initialization method of primal heuristic (called when branch and bound process is about to begin) */
129static
130SCIP_DECL_HEURINITSOL(heurInitsolGcgsimplerounding)
131{
132 SCIP_HEURDATA* heurdata;
133
134 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
135
136 heurdata = SCIPheurGetData(heur);
137 assert(heurdata != NULL);
138 heurdata->lastlp = -1;
139
140 return SCIP_OKAY;
141}
142
143
144/** solving process deinitialization method of primal heuristic (called before branch and bound process data is freed) */
145#define heurExitsolGcgsimplerounding NULL
146
147
148/** execution method of primal heuristic */
149static
150SCIP_DECL_HEUREXEC(heurExecGcgsimplerounding) /*lint --e{715}*/
151{ /*lint --e{715}*/
152 SCIP* masterprob;
153 SCIP_HEURDATA* heurdata;
154 SCIP_SOL* sol;
155 SCIP_VAR** lpcands;
156 SCIP_Real* lpcandssol;
157 SCIP_Longint nlps;
158 int nlpcands;
159 int c;
160
161 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
162 assert(result != NULL);
163
164 /* get heuristic data */
165 heurdata = SCIPheurGetData(heur);
166 assert(heurdata != NULL);
167
168 /* get master problem */
169 masterprob = GCGgetMasterprob(heurdata->gcg);
170 assert(masterprob != NULL);
171
172 *result = SCIP_DIDNOTRUN;
173
174 /* do not execute the heuristic on invalid relaxation solutions
175 * (which is the case if the node has been cut off)
176 */
178 {
179 SCIPdebugMessage("skipping GCG simple rounding: invalid relaxation solution\n");
180 return SCIP_OKAY;
181 }
182
183 /* only call heuristic, if an optimal LP solution is at hand */
184 if( SCIPgetStage(masterprob) > SCIP_STAGE_SOLVING || SCIPgetLPSolstat(masterprob) != SCIP_LPSOLSTAT_OPTIMAL )
185 return SCIP_OKAY;
186
187 /* on our first call, calculate the number of roundable variables */
188 if( heurdata->nroundablevars == -1 )
189 {
190 SCIP_VAR** vars;
191 int nvars;
192 int nroundablevars;
193 int i;
194
195 vars = SCIPgetVars(scip);
197 nroundablevars = 0;
198 for( i = 0; i < nvars; ++i )
199 {
200 if( SCIPvarMayRoundDown(vars[i]) || SCIPvarMayRoundUp(vars[i]) )
201 nroundablevars++;
202 }
203 heurdata->nroundablevars = nroundablevars;
204 }
205
206 /* don't call heuristic if there are no roundable variables */
207 if( heurdata->nroundablevars == 0 )
208 return SCIP_OKAY;
209
210 /* don't call heuristic, if we have already processed the current LP solution */
211 nlps = SCIPgetNLPs(masterprob);
212 if( nlps == heurdata->lastlp )
213 return SCIP_OKAY;
214 heurdata->lastlp = nlps;
215
216 /* get fractional variables, that should be integral */
217 SCIP_CALL( SCIPgetExternBranchCands(scip, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL) );
218
219 /* only call heuristic, if LP solution is fractional */
220 if( nlpcands == 0 )
221 return SCIP_OKAY;
222
223 /* don't call heuristic, if there are more fractional variables than roundable ones */
224 if( nlpcands > heurdata->nroundablevars )
225 return SCIP_OKAY;
226
227 *result = SCIP_DIDNOTFIND;
228
229 SCIPdebugMessage("executing GCG simple rounding heuristic: %d fractionals\n", nlpcands);
230
231 /* get the working solution from heuristic's local data */
232 sol = heurdata->sol;
233 assert(sol != NULL);
234
235 /* copy the current LP solution to the working solution */
237
238 /* round all roundable fractional columns in the corresponding direction as long as no unroundable column was found */
239 for( c = 0; c < nlpcands; ++c )
240 {
241 SCIP_VAR* var;
242 SCIP_Real oldsolval;
243 SCIP_Real newsolval;
244 SCIP_Bool mayrounddown;
245 SCIP_Bool mayroundup;
246
247 oldsolval = lpcandssol[c];
248 assert(!SCIPisFeasIntegral(scip, oldsolval));
249 var = lpcands[c];
251 mayrounddown = SCIPvarMayRoundDown(var);
252 mayroundup = SCIPvarMayRoundUp(var);
253 SCIPdebugMessage("GCG simple rounding heuristic: var <%s>, val=%g, rounddown=%u, roundup=%u\n",
254 SCIPvarGetName(var), oldsolval, mayrounddown, mayroundup);
255
256 /* choose rounding direction */
257 if( mayrounddown && mayroundup )
258 {
259 /* we can round in both directions: round in objective function direction */
260 if( SCIPvarGetObj(var) >= 0.0 )
261 newsolval = SCIPfeasFloor(scip, oldsolval);
262 else
263 newsolval = SCIPfeasCeil(scip, oldsolval);
264 }
265 else if( mayrounddown )
266 newsolval = SCIPfeasFloor(scip, oldsolval);
267 else if( mayroundup )
268 newsolval = SCIPfeasCeil(scip, oldsolval);
269 else
270 break;
271
272 /* store new solution value */
273 SCIP_CALL( SCIPsetSolVal(scip, sol, var, newsolval) );
274 }
275
276 /* check, if rounding was successful */
277 if( c == nlpcands )
278 {
279 SCIP_Bool stored;
280
281 /* check solution for feasibility, and add it to solution store if possible
282 * neither integrality nor feasibility of LP rows has to be checked, because all fractional
283 * variables were already moved in feasible direction to the next integer
284 */
285 SCIP_CALL( SCIPtrySol(scip, sol, FALSE, FALSE, FALSE, FALSE, FALSE, &stored) );
286
287 if( stored )
288 {
289#ifdef SCIP_DEBUG
290 SCIPdebugMessage("found feasible rounded solution:\n");
291 SCIPprintSol(scip, sol, NULL, FALSE);
292#endif
293 *result = SCIP_FOUNDSOL;
294 }
295 }
296
297 return SCIP_OKAY;
298}
299
300
301
302
303/*
304 * heuristic specific interface methods
305 */
306
307/** creates the GCG simple rounding heuristic and includes it in SCIP */
309 GCG* gcg /**< GCG data structure */
310 )
311{
312 SCIP_HEURDATA* heurdata;
313 SCIP* origprob = GCGgetOrigprob(gcg);
314
315 SCIP_CALL( SCIPallocMemory(origprob, &heurdata) );
316 heurdata->gcg = gcg;
317
318 /* include heuristic */
321 heurCopyGcgsimplerounding, heurFreeGcgsimplerounding, heurInitGcgsimplerounding, heurExitGcgsimplerounding,
322 heurInitsolGcgsimplerounding, heurExitsolGcgsimplerounding, heurExecGcgsimplerounding, heurdata) );
323
324 return SCIP_OKAY;
325}
326
#define SCIP_Longint
#define SCIP_Bool
#define SCIP_Real
#define SCIP_CALL(x)
#define FALSE
GCG interface methods.
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
SCIP * GCGgetMasterprob(GCG *gcg)
Definition: gcg.cpp:99
SCIP_STAGE SCIPgetStage(SCIP *scip)
int SCIPgetNIntVars(SCIP *scip)
SCIP_VAR ** SCIPgetVars(SCIP *scip)
int SCIPgetNBinVars(SCIP *scip)
SCIP_RETCODE SCIPgetExternBranchCands(SCIP *scip, SCIP_VAR ***externcands, SCIP_Real **externcandssol, SCIP_Real **externcandsscore, int *nexterncands, int *nprioexterncands, int *nprioexternbins, int *nprioexternints, int *nprioexternimpls)
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
SCIP_RETCODE SCIPincludeHeur(SCIP *scip, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, SCIP_HEURTIMING timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEURCOPY((*heurcopy)), SCIP_DECL_HEURFREE((*heurfree)), SCIP_DECL_HEURINIT((*heurinit)), SCIP_DECL_HEUREXIT((*heurexit)), SCIP_DECL_HEURINITSOL((*heurinitsol)), SCIP_DECL_HEUREXITSOL((*heurexitsol)), SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
const char * SCIPheurGetName(SCIP_HEUR *heur)
SCIP_LPSOLSTAT SCIPgetLPSolstat(SCIP *scip)
#define SCIPallocMemory(scip, ptr)
#define SCIPfreeMemory(scip, ptr)
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
SCIP_RETCODE SCIPprintSol(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
SCIP_RETCODE SCIPlinkRelaxSol(SCIP *scip, SCIP_SOL *sol)
SCIP_RETCODE SCIPtrySol(SCIP *scip, SCIP_SOL *sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
SCIP_RETCODE SCIPsetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real val)
SCIP_Longint SCIPgetNLPs(SCIP *scip)
SCIP_Real SCIPfeasCeil(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPfeasFloor(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisFeasIntegral(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
const char * SCIPvarGetName(SCIP_VAR *var)
SCIP_Bool SCIPisRelaxSolValid(SCIP *scip)
#define heurCopyGcgsimplerounding
static SCIP_DECL_HEURINITSOL(heurInitsolGcgsimplerounding)
static SCIP_DECL_HEURINIT(heurInitGcgsimplerounding)
#define HEUR_TIMING
#define heurExitsolGcgsimplerounding
#define HEUR_FREQOFS
#define HEUR_DESC
static SCIP_DECL_HEURFREE(heurFreeGcgsimplerounding)
SCIP_RETCODE GCGincludeHeurGcgsimplerounding(GCG *gcg)
#define HEUR_DISPCHAR
#define HEUR_MAXDEPTH
#define HEUR_PRIORITY
#define HEUR_NAME
static SCIP_DECL_HEUREXEC(heurExecGcgsimplerounding)
static SCIP_DECL_HEUREXIT(heurExitGcgsimplerounding)
#define HEUR_FREQ
#define HEUR_USESSUBSCIP
simple and fast LP rounding heuristic
#define NULL
#define SCIPdebugMessage
Definition: struct_gcg.h:55
SCIP_Longint lastlp
SCIP_SOL * sol
SCIP_LPSOLSTAT_OPTIMAL
SCIP_DIDNOTRUN
SCIP_DIDNOTFIND
SCIP_FOUNDSOL
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE
SCIP_STAGE_SOLVING
SCIP_VARSTATUS_COLUMN