SCIP Doxygen Documentation
Loading...
Searching...
No Matches
presol_convertinttobin.c
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
4/* SCIP --- Solving Constraint Integer Programs */
5/* */
6/* Copyright (c) 2002-2026 Zuse Institute Berlin (ZIB) */
7/* */
8/* Licensed under the Apache License, Version 2.0 (the "License"); */
9/* you may not use this file except in compliance with the License. */
10/* You may obtain a copy of the License at */
11/* */
12/* http://www.apache.org/licenses/LICENSE-2.0 */
13/* */
14/* Unless required by applicable law or agreed to in writing, software */
15/* distributed under the License is distributed on an "AS IS" BASIS, */
16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17/* See the License for the specific language governing permissions and */
18/* limitations under the License. */
19/* */
20/* You should have received a copy of the Apache-2.0 license */
21/* along with SCIP; see the file LICENSE. If not visit scipopt.org. */
22/* */
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25/**@file presol_convertinttobin.c
26 * @ingroup DEFPLUGINS_PRESOL
27 * @brief presolver that converts integer variables to binaries
28 * @author Michael Winkler
29 *
30 * Converts integer variables at the beginning of Presolving into their binary representation. If necessary adds a
31 * bounding knapsack constraint.
32 *
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
38#include "scip/cons_knapsack.h"
39#include "scip/debug.h"
41#include "scip/pub_message.h"
42#include "scip/pub_misc.h"
43#include "scip/pub_presol.h"
44#include "scip/pub_var.h"
45#include "scip/scip_cons.h"
46#include "scip/scip_mem.h"
47#include "scip/scip_message.h"
48#include "scip/scip_numerics.h"
49#include "scip/scip_param.h"
50#include "scip/scip_presol.h"
51#include "scip/scip_prob.h"
52#include "scip/scip_var.h"
53
54
55#define PRESOL_NAME "convertinttobin"
56#define PRESOL_DESC "converts integer variables to binaries"
57#define PRESOL_PRIORITY +6000000 /**< priority of the presolver (>= 0: before, < 0: after constraint handlers) */
58#define PRESOL_MAXROUNDS 0 /**< maximal number of presolving rounds the presolver participates in (-1: no
59 * limit) */
60#define PRESOL_TIMING SCIP_PRESOLTIMING_FAST /* timing of the presolver (fast, medium, or exhaustive) */
61
62#define DEFAULT_MAXDOMAINSIZE SCIP_LONGINT_MAX /**< absolute value of maximum domain size which will be converted */
63#define DEFAULT_ONLYPOWERSOFTWO FALSE /**< should only integer variables with a domain size of 2^p - 1 be
64 * converted(, there we don't need an knapsack-constraint) */
65#define DEFAULT_SAMELOCKSINBOTHDIRECTIONS FALSE /**< should only integer variables with uplocks equals downlocks be converted */
66
67/** presolver data */
68struct SCIP_PresolData
69{
70 SCIP_Longint maxdomainsize; /**< absolute value of maximum domain size */
71 SCIP_Bool onlypoweroftwo; /**< should only integer variables with a domain size of 2^p - 1 be converted */
72 SCIP_Bool samelocksinbothdirections; /**< should only integer variables with uplocks equals downlocks be converted */
73};
74
75/*
76 * Callback methods of presolver
77 */
78
79/** copy method for constraint handler plugins (called when SCIP copies plugins) */
80static
81SCIP_DECL_PRESOLCOPY(presolCopyConvertinttobin)
82{ /*lint --e{715}*/
83 assert(scip != NULL);
84 assert(presol != NULL);
85
87
88 /* call inclusion method of presolver */
90
91 return SCIP_OKAY;
92}
93
94/** destructor of presolver to free user data (called when SCIP is exiting) */
95static
96SCIP_DECL_PRESOLFREE(presolFreeConvertinttobin)
97{ /*lint --e{715}*/
98 SCIP_PRESOLDATA* presoldata;
99
100 /* free presolver data */
101 presoldata = SCIPpresolGetData(presol);
102 assert(presoldata != NULL);
103
104 SCIPfreeBlockMemory(scip, &presoldata);
105 SCIPpresolSetData(presol, NULL);
106
107 return SCIP_OKAY;
108}
109
110/** presolving execution method */
111static
112SCIP_DECL_PRESOLEXEC(presolExecConvertinttobin)
113{ /*lint --e{715}*/
114 SCIP_VAR** scipvars;
115 SCIP_VAR** vars;
116 SCIP_PRESOLDATA* presoldata;
117 int nbinvars;
118 int nintvars;
119 int v;
120
121 assert(scip != NULL);
122 assert(presol != NULL);
123 assert(result != NULL);
124
126
128
129 /* get the problem variables */
130 scipvars = SCIPgetVars(scip);
131 nbinvars = SCIPgetNBinVars(scip);
132 nintvars = SCIPgetNIntVars(scip);
133 if( nintvars == 0 )
134 return SCIP_OKAY;
135
136 /* get presolver data */
137 presoldata = SCIPpresolGetData(presol);
138 assert(presoldata != NULL);
139
141
142 /* copy the integer variables into an own array, since adding binary variables affects the left-most slots in the
143 * array and thereby interferes with our search loop
144 */
145 SCIP_CALL( SCIPduplicateBufferArray(scip, &vars, &scipvars[nbinvars], nintvars) );
146
147 /* scan the integer variables for possible conversion into binaries;
148 * we have to collect the variables first in an own
149 */
150 for( v = 0; v < nintvars; ++v )
151 {
152 SCIP_VAR** newbinvars;
153 SCIP_Real* newbinvarcoeffs;
154 SCIP_Longint* weights;
155 SCIP_CONS* newcons;
156 SCIP_Real lb;
157 SCIP_Real ub;
158 SCIP_Longint domainsize;
159 char newbinvarname[SCIP_MAXSTRLEN];
160 char newconsname[SCIP_MAXSTRLEN];
161 int nnewbinvars;
162 int v2;
163 SCIP_Longint scalar;
164 SCIP_Bool infeasible;
165 SCIP_Bool aggregated;
166 SCIP_Bool noconsknapsack;
167
169
170 /* skip variables which cannot be multi-aggregated */
172 continue;
173
174 /* check for correct locks */
175 if( presoldata->samelocksinbothdirections
177 continue;
178
179 /* get variable's bounds */
180 lb = SCIPvarGetLbGlobal(vars[v]);
181 ub = SCIPvarGetUbGlobal(vars[v]);
182 assert( SCIPisIntegral(scip, lb) );
183 assert( SCIPisIntegral(scip, ub) );
184
185 if( SCIPisInfinity(scip, ub - lb) )
186 domainsize = SCIP_LONGINT_MAX;
187 else
188 domainsize = (SCIP_Longint) SCIPceil(scip, ub - lb);
189
190 assert(domainsize >= 0);
191
192 /* check for allowed domainsize */
193 if( SCIPisInfinity(scip, -lb) || SCIPisInfinity(scip, ub) || domainsize > presoldata->maxdomainsize )
194 continue;
195
196 /* check for domainsize is not 2^p - 1 if necessary */
197 if( presoldata->onlypoweroftwo )
198 {
199 /* stop if domainsize is not 2^p - 1*/
200 SCIP_Longint tmp;
201
202 assert(domainsize < SCIP_LONGINT_MAX);
203 tmp = domainsize + 1;
204
205 while( tmp % 2 == 0 )
206 tmp /= 2;
207 if( tmp != 1 )
208 continue;
209 }
210
211 noconsknapsack = FALSE;
212
213 nnewbinvars = (int)SCIPfloor(scip, (log((SCIP_Real) domainsize)/log(2.0))) + 1;
214
215 SCIPdebugMsg(scip, "integer variable <%s> [%g,%g], domainsize %" SCIP_LONGINT_FORMAT "\n, <uplocks = %d, downlocks = %d will be 'binarized' by %d binary variables\n ",
218
219 assert(nnewbinvars > 0);
220
221 scalar = (SCIP_Longint)pow(2.0, nnewbinvars); /*lint !e747*/
222 /* because of rounding errors */
223 if( scalar == domainsize )
224 {
225 scalar *= 2;
226 nnewbinvars++;
227 }
228 else if( scalar == domainsize + 1 )
229 noconsknapsack = TRUE;
230
231 assert(scalar > domainsize);
232
233 SCIP_CALL( SCIPallocBufferArray(scip, &newbinvars, nnewbinvars) );
234 SCIP_CALL( SCIPallocBufferArray(scip, &newbinvarcoeffs, nnewbinvars) );
235 SCIP_CALL( SCIPallocBufferArray(scip, &weights, nnewbinvars) );
236
237 for( v2 = nnewbinvars - 1; v2 >= 0; --v2 )
238 {
239 SCIPdebugMsg(scip, "creating for <%s>[%g,%g] %d. binary variable\n", SCIPvarGetName(vars[v]), lb, ub, v2);
240
241 /* create binary variable */
242 (void) SCIPsnprintf(newbinvarname, SCIP_MAXSTRLEN, "%s_bin_%d", SCIPvarGetName(vars[v]), v2);
243 SCIP_CALL( SCIPcreateVar(scip, &newbinvars[v2], newbinvarname, 0.0, 1.0, 0.0, SCIP_VARTYPE_BINARY,
245 SCIP_CALL( SCIPaddVar(scip, newbinvars[v2]) );
246
247 scalar /= 2;
248 assert(scalar > 0);
249
250 newbinvarcoeffs[v2] = (SCIP_Real)scalar;
251 weights[v2] = scalar;
252 }
253
254#ifdef WITH_DEBUG_SOLUTION
255 /* set the debug solution values */
256 if( SCIPdebugIsMainscip(scip) )
257 {
258 SCIP_Real varval;
259
260 SCIP_CALL( SCIPdebugGetSolVal(scip, vars[v], &varval) );
261 assert(SCIPisIntegral(scip, varval));
262
263 if( SCIPisPositive(scip, varval) )
264 {
265 SCIP_Real resvarval = varval;
266
267 for( v2 = nnewbinvars - 1; v2 >= 0; --v2 )
268 {
269 assert(SCIPisPositive(scip, resvarval));
270 assert(SCIPisIntegral(scip, resvarval));
271
272 if( SCIPisLE(scip, newbinvarcoeffs[v2], resvarval) )
273 {
274 SCIP_CALL( SCIPdebugAddSolVal(scip, newbinvars[v2], 1.0) );
275 resvarval -= newbinvarcoeffs[v2];
276 }
277
278 if( SCIPisZero(scip, resvarval) )
279 break;
280 }
281 }
282 }
283#endif
284
285 /* aggregate integer and binary variable */
286 SCIP_CALL( SCIPmultiaggregateVar(scip, vars[v], nnewbinvars, newbinvars, (SCIP_Real*)newbinvarcoeffs, lb, &infeasible, &aggregated) );
287 assert(!infeasible);
288 assert(aggregated);
289
290 (void) SCIPsnprintf(newconsname, SCIP_MAXSTRLEN, "%s_bin_knapsack", SCIPvarGetName(vars[v]));
291
292 if( !noconsknapsack )
293 {
294 int nodd;
295 nodd = 0;
296 while( domainsize % 2 == 1 )
297 {
298 nodd++;
299 domainsize = (domainsize - 1) / 2;
300 }
301 if( nodd > 0 )
302 {
303 SCIP_Longint divisor;
304
305 divisor = (SCIP_Longint)pow(2.0, nodd); /*lint !e747*/
306 assert(divisor >= 2);
307
308 for( v2 = nodd; v2 < nnewbinvars; ++v2 )
309 {
310 weights[v2] /= divisor;
311 }
312 }
313
314 SCIP_CALL( SCIPcreateConsKnapsack(scip, &newcons, newconsname, nnewbinvars - nodd, &newbinvars[nodd],
315 &weights[nodd], domainsize,
317 SCIP_CALL( SCIPaddCons(scip, newcons) );
318 SCIPdebugPrintCons(scip, newcons, NULL);
319 SCIP_CALL( SCIPreleaseCons(scip, &newcons) );
320 }
321
322 for( v2 = nnewbinvars - 1; v2 >= 0; --v2 )
323 {
324 /* release binary variable */
325 SCIP_CALL( SCIPreleaseVar(scip, &newbinvars[v2]) );
326 (*nchgvartypes)++;
327 }
328
329 SCIPfreeBufferArray(scip, &newbinvars);
330 SCIPfreeBufferArray(scip, &newbinvarcoeffs);
331 SCIPfreeBufferArray(scip, &weights);
332
333 if( aggregated ) /*lint !e774*/
335 }
336
337 /* free temporary memory */
339
340 return SCIP_OKAY;
341}
342
343
344/*
345 * presolver specific interface methods
346 */
347
348/** creates the convertinttobin presolver and includes it in SCIP */
350 SCIP* scip /**< SCIP data structure */
351 )
352{
353 SCIP_PRESOLDATA* presoldata;
354 SCIP_PRESOL* presolptr;
355
356 /* create convertinttobin presolver data */
357 SCIP_CALL( SCIPallocBlockMemory(scip, &presoldata) );
358
359 presoldata->maxdomainsize = DEFAULT_MAXDOMAINSIZE;
360 presoldata->onlypoweroftwo = DEFAULT_ONLYPOWERSOFTWO;
361
362 /* include presolver */
364 presolExecConvertinttobin,
365 presoldata) );
366 assert(presolptr != NULL);
367
368 SCIP_CALL( SCIPsetPresolCopy(scip, presolptr, presolCopyConvertinttobin) );
369 SCIP_CALL( SCIPsetPresolFree(scip, presolptr, presolFreeConvertinttobin) );
370
371 /* add convertinttobin presolver parameters */
373 "presolving/" PRESOL_NAME "/maxdomainsize",
374 "absolute value of maximum domain size for converting an integer variable to binaries variables",
375 &presoldata->maxdomainsize, TRUE, DEFAULT_MAXDOMAINSIZE, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
376
377 /* add convertinttobin presolver parameters */
379 "presolving/" PRESOL_NAME "/onlypoweroftwo",
380 "should only integer variables with a domain size of 2^p - 1 be converted(, there we don't need an knapsack-constraint for restricting the sum of the binaries)",
381 &presoldata->onlypoweroftwo, TRUE, DEFAULT_ONLYPOWERSOFTWO, NULL, NULL) );
382
383 /* add convertinttobin presolver parameters */
385 "presolving/" PRESOL_NAME "/samelocksinbothdirections",
386 "should only integer variables with uplocks equals downlocks be converted",
387 &presoldata->samelocksinbothdirections, TRUE, DEFAULT_SAMELOCKSINBOTHDIRECTIONS, NULL, NULL) );
388
389 return SCIP_OKAY;
390}
Constraint handler for knapsack constraints of the form , x binary and .
methods for debugging
#define SCIPdebugGetSolVal(scip, var, val)
Definition debug.h:313
#define SCIPdebugAddSolVal(scip, var, val)
Definition debug.h:312
#define NULL
Definition def.h:257
#define SCIP_MAXSTRLEN
Definition def.h:278
#define SCIP_Longint
Definition def.h:150
#define SCIP_Bool
Definition def.h:100
#define SCIP_STRINGEQ(name, reference, retcode)
Definition def.h:454
#define SCIP_Real
Definition def.h:165
#define TRUE
Definition def.h:102
#define FALSE
Definition def.h:103
#define SCIP_LONGINT_FORMAT
Definition def.h:157
#define SCIP_LONGINT_MAX
Definition def.h:151
#define SCIP_CALL(x)
Definition def.h:364
SCIP_RETCODE SCIPcreateConsKnapsack(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
Definition scip_prob.c:1907
int SCIPgetNIntVars(SCIP *scip)
Definition scip_prob.c:2340
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition scip_prob.c:3274
SCIP_VAR ** SCIPgetVars(SCIP *scip)
Definition scip_prob.c:2201
int SCIPgetNBinVars(SCIP *scip)
Definition scip_prob.c:2293
#define SCIPdebugMsg
SCIP_RETCODE SCIPaddLongintParam(SCIP *scip, const char *name, const char *desc, SCIP_Longint *valueptr, SCIP_Bool isadvanced, SCIP_Longint defaultvalue, SCIP_Longint minvalue, SCIP_Longint maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition scip_param.c:111
SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition scip_param.c:57
SCIP_RETCODE SCIPincludePresolConvertinttobin(SCIP *scip)
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition scip_cons.c:1173
#define SCIPallocBufferArray(scip, ptr, num)
Definition scip_mem.h:124
#define SCIPfreeBufferArray(scip, ptr)
Definition scip_mem.h:136
#define SCIPduplicateBufferArray(scip, ptr, source, num)
Definition scip_mem.h:132
#define SCIPfreeBlockMemory(scip, ptr)
Definition scip_mem.h:108
#define SCIPallocBlockMemory(scip, ptr)
Definition scip_mem.h:89
SCIP_RETCODE SCIPsetPresolFree(SCIP *scip, SCIP_PRESOL *presol,)
void SCIPpresolSetData(SCIP_PRESOL *presol, SCIP_PRESOLDATA *presoldata)
Definition presol.c:538
SCIP_PRESOLDATA * SCIPpresolGetData(SCIP_PRESOL *presol)
Definition presol.c:528
SCIP_RETCODE SCIPsetPresolCopy(SCIP *scip, SCIP_PRESOL *presol,)
SCIP_RETCODE SCIPincludePresolBasic(SCIP *scip, SCIP_PRESOL **presolptr, const char *name, const char *desc, int priority, int maxrounds, SCIP_PRESOLTIMING timing, SCIP_DECL_PRESOLEXEC((*presolexec)), SCIP_PRESOLDATA *presoldata)
const char * SCIPpresolGetName(SCIP_PRESOL *presol)
Definition presol.c:625
SCIP_Bool SCIPisIntegral(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Real SCIPfloor(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPceil(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPvarIsInitial(SCIP_VAR *var)
Definition var.c:23546
int SCIPvarGetNLocksUpType(SCIP_VAR *var, SCIP_LOCKTYPE locktype)
Definition var.c:4380
SCIP_Bool SCIPvarIsImpliedIntegral(SCIP_VAR *var)
Definition var.c:23530
SCIP_Bool SCIPdoNotMultaggrVar(SCIP *scip, SCIP_VAR *var)
Definition scip_var.c:10942
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
Definition var.c:23485
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition var.c:24174
const char * SCIPvarGetName(SCIP_VAR *var)
Definition var.c:23299
SCIP_RETCODE SCIPmultiaggregateVar(SCIP *scip, SCIP_VAR *var, int naggvars, SCIP_VAR **aggvars, SCIP_Real *scalars, SCIP_Real constant, SCIP_Bool *infeasible, SCIP_Bool *aggregated)
Definition scip_var.c:10834
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition scip_var.c:1887
SCIP_Bool SCIPvarIsRemovable(SCIP_VAR *var)
Definition var.c:23556
SCIP_RETCODE SCIPcreateVar(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype, SCIP_Bool initial, SCIP_Bool removable, SCIP_DECL_VARDELORIG((*vardelorig)), SCIP_DECL_VARTRANS((*vartrans)), SCIP_DECL_VARDELTRANS((*vardeltrans)), SCIP_DECL_VARCOPY((*varcopy)), SCIP_VARDATA *vardata)
Definition scip_var.c:120
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition var.c:24152
int SCIPvarGetNLocksDownType(SCIP_VAR *var, SCIP_LOCKTYPE locktype)
Definition var.c:4322
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition misc.c:10827
return SCIP_OKAY
assert(minobj< SCIPgetCutoffbound(scip))
static SCIP_VAR ** vars
memory allocation routines
#define PRESOL_NAME
#define PRESOL_PRIORITY
#define PRESOL_MAXROUNDS
#define PRESOL_TIMING
#define PRESOL_DESC
#define DEFAULT_MAXDOMAINSIZE
#define DEFAULT_SAMELOCKSINBOTHDIRECTIONS
#define DEFAULT_ONLYPOWERSOFTWO
presolver that converts integer variables with domain [a,a+1] to binaries
public methods for message output
#define SCIPdebugPrintCons(x, y, z)
public data structures and miscellaneous methods
public methods for presolvers
public methods for problem variables
public methods for constraint handler plugins and constraints
public methods for memory management
public methods for message handling
public methods for numerical tolerances
public methods for SCIP parameter handling
public methods for presolving plugins
public methods for global and local (sub)problems
public methods for SCIP variables
struct SCIP_Cons SCIP_CONS
Definition type_cons.h:63
#define SCIP_DECL_PRESOLCOPY(x)
Definition type_presol.h:60
struct SCIP_PresolData SCIP_PRESOLDATA
Definition type_presol.h:51
#define SCIP_DECL_PRESOLFREE(x)
Definition type_presol.h:68
struct SCIP_Presol SCIP_PRESOL
Definition type_presol.h:50
#define SCIP_DECL_PRESOLEXEC(x)
@ SCIP_DIDNOTRUN
Definition type_result.h:42
@ SCIP_DIDNOTFIND
Definition type_result.h:44
@ SCIP_SUCCESS
Definition type_result.h:58
@ SCIP_INVALIDCALL
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
Definition type_scip.h:39
struct SCIP_Var SCIP_VAR
Definition type_var.h:166
@ SCIP_VARTYPE_INTEGER
Definition type_var.h:65
@ SCIP_VARTYPE_BINARY
Definition type_var.h:64
@ SCIP_LOCKTYPE_MODEL
Definition type_var.h:141