Scippy

SCIP

Solving Constraint Integer Programs

presol_boundshift.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-2022 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file presol_boundshift.c
17  * @ingroup DEFPLUGINS_PRESOL
18  * @brief presolver that converts variables with domain [a,b] to variables with domain [0,b-a]
19  * @author Stefan Heinz
20  * @author Michael Winkler
21  */
22 
23 /**@todo test this presolving step to decide whether to turn it in default mode on or off */
24 
25 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
26 
27 #include "blockmemshell/memory.h"
28 #include "scip/presol_boundshift.h"
29 #include "scip/pub_message.h"
30 #include "scip/pub_misc.h"
31 #include "scip/pub_presol.h"
32 #include "scip/pub_var.h"
33 #include "scip/scip_mem.h"
34 #include "scip/scip_message.h"
35 #include "scip/scip_numerics.h"
36 #include "scip/scip_param.h"
37 #include "scip/scip_presol.h"
38 #include "scip/scip_prob.h"
39 #include "scip/scip_var.h"
40 #include "scip/debug.h"
41 #include <string.h>
42 
43 #define PRESOL_NAME "boundshift"
44 #define PRESOL_DESC "converts variables with domain [a,b] to variables with domain [0,b-a]"
45 #define PRESOL_PRIORITY 7900000 /**< priority of the presolver (>= 0: before, < 0: after constraint handlers) */
46 #define PRESOL_MAXROUNDS 0 /**< maximal number of presolving rounds the presolver participates in (-1: no limit) */
47 #define PRESOL_TIMING SCIP_PRESOLTIMING_FAST /* timing of the presolver (fast, medium, or exhaustive) */
48 
49 #define MAXABSBOUND 1000.0 /**< maximum absolute variable bounds for aggregation */
50 
51 /*
52  * Default parameter settings
53  */
54 
55 #define DEFAULT_MAXSHIFT SCIP_LONGINT_MAX /**< absolute value of maximum shift */
56 #define DEFAULT_FLIPPING TRUE /**< is flipping allowed? */
57 #define DEFAULT_INTEGER TRUE /**< are only integer ranges shifted */
58 
59 /*
60  * Data structures
61  */
62 
63 /** presolver data */
64 struct SCIP_PresolData
65 {
66  SCIP_Longint maxshift; /**< absolute value of maximum shift */
67  SCIP_Bool flipping; /**< is flipping allowed? */
68  SCIP_Bool integer; /**< shift only integer values? */
69 };
70 
71 
72 /*
73  * Local methods
74  */
75 
76 /*
77  * Callback methods of presolver
78  */
79 
80 /** copy method for constraint handler plugins (called when SCIP copies plugins) */
81 static
82 SCIP_DECL_PRESOLCOPY(presolCopyBoundshift)
83 { /*lint --e{715}*/
84  assert(scip != NULL);
85  assert(presol != NULL);
86  assert(strcmp(SCIPpresolGetName(presol), PRESOL_NAME) == 0);
87 
88  /* call inclusion method of presolver */
90 
91  return SCIP_OKAY;
92 }
93 
94 
95 /** destructor of presolver to free user data (called when SCIP is exiting) */
96 /**! [SnippetPresolFreeBoundshift] */
97 static
98 SCIP_DECL_PRESOLFREE(presolFreeBoundshift)
99 { /*lint --e{715}*/
100  SCIP_PRESOLDATA* presoldata;
101 
102  /* free presolver data */
103  presoldata = SCIPpresolGetData(presol);
104  assert(presoldata != NULL);
105 
106  SCIPfreeBlockMemory(scip, &presoldata);
107  SCIPpresolSetData(presol, NULL);
108 
109  return SCIP_OKAY;
110 }
111 /**! [SnippetPresolFreeBoundshift] */
112 
113 
114 /** presolving execution method */
115 static
116 SCIP_DECL_PRESOLEXEC(presolExecBoundshift)
117 { /*lint --e{715}*/
118  SCIP_PRESOLDATA* presoldata;
119  SCIP_VAR** scipvars;
120  SCIP_VAR** vars;
121  int nbinvars;
122  int nvars;
123  int v;
124 
125  assert(scip != NULL);
126  assert(presol != NULL);
127  assert(strcmp(SCIPpresolGetName(presol), PRESOL_NAME) == 0);
128  assert(result != NULL);
129 
130  *result = SCIP_DIDNOTRUN;
131 
132  if( SCIPdoNotAggr(scip) )
133  return SCIP_OKAY;
134 
135  /* get presolver data */
136  presoldata = SCIPpresolGetData(presol);
137  assert(presoldata != NULL);
138 
139  /* get the problem variables */
140  scipvars = SCIPgetVars(scip);
141  nbinvars = SCIPgetNBinVars(scip);
142  nvars = SCIPgetNVars(scip) - nbinvars;
143 
144  if( nvars == 0 )
145  return SCIP_OKAY;
146 
147  *result = SCIP_DIDNOTFIND;
148 
149  /* copy the integer/continuous variables into an own array, since adding new variables affects the left-most slots in
150  * the array and thereby interferes with our search loop
151  */
152  SCIP_CALL( SCIPduplicateBufferArray(scip, &vars, &scipvars[nbinvars], nvars) );
153 
154  /* scan the integer, implicit, and continuous variables for possible conversion */
155  for( v = nvars - 1; v >= 0; --v )
156  {
157  SCIP_VAR* var = vars[v];
158  SCIP_Real lb;
159  SCIP_Real ub;
160 
161  assert(SCIPvarGetType(var) != SCIP_VARTYPE_BINARY);
162 
163  /* do not shift non-active (fixed or (multi-)aggregated) variables */
164  if( !SCIPvarIsActive(var) )
165  continue;
166 
167  /* get current variable's bounds */
168  lb = SCIPvarGetLbGlobal(var);
169  ub = SCIPvarGetUbGlobal(var);
170 
171  /* it can happen that the variable bounds of integer variables have not been propagated yet or contain
172  * some small noise; this will result in an aggregation that might trigger assertions when updating bounds of
173  * aggregated variables (see #1817)
174  */
175  if( SCIPvarIsIntegral(var) )
176  {
177  assert(SCIPisIntegral(scip, lb));
178  assert(SCIPisIntegral(scip, ub));
179 
180  lb = SCIPadjustedVarLb(scip, var, lb);
181  ub = SCIPadjustedVarUb(scip, var, ub);
182  }
183 
184  assert( SCIPisLE(scip, lb, ub) );
185  if( SCIPisEQ(scip, lb, ub) )
186  continue;
187  if( presoldata->integer && !SCIPisIntegral(scip, ub - lb) )
188  continue;
189 
190  /* check if bounds are shiftable */
191  if( !SCIPisEQ(scip, lb, 0.0) && /* lower bound != 0.0 */
192  SCIPisLT(scip, ub, SCIPinfinity(scip)) && /* upper bound != infinity */
193  SCIPisGT(scip, lb, -SCIPinfinity(scip)) && /* lower bound != -infinity */
194  SCIPisLT(scip, ub - lb, (SCIP_Real) presoldata->maxshift) && /* less than max shifting */
195  SCIPisLE(scip, REALABS(lb), MAXABSBOUND) && /* ensures a small constant in aggregation */
196  SCIPisLE(scip, REALABS(ub), MAXABSBOUND) ) /* ensures a small constant in aggregation */
197  {
198  SCIP_VAR* newvar;
199  char newvarname[SCIP_MAXSTRLEN];
200  SCIP_Bool infeasible;
201  SCIP_Bool redundant;
202  SCIP_Bool aggregated;
203 
204  SCIPdebugMsg(scip, "convert range <%s>[%g,%g] to [%g,%g]\n", SCIPvarGetName(var), lb, ub, 0.0, (ub - lb) );
205 
206  /* create new variable */
207  (void) SCIPsnprintf(newvarname, SCIP_MAXSTRLEN, "%s_shift", SCIPvarGetName(var));
208  SCIP_CALL( SCIPcreateVar(scip, &newvar, newvarname, 0.0, (ub - lb), 0.0, SCIPvarGetType(var),
210  SCIP_CALL( SCIPaddVar(scip, newvar) );
211 
212 #ifdef WITH_DEBUG_SOLUTION
213  if( SCIPdebugIsMainscip(scip) )
214  {
215  /* calculate and store debug solution value of shift variable */
216  SCIP_Real val;
217 
218  SCIP_CALL( SCIPdebugGetSolVal(scip, var, &val) );
219  SCIPdebugMsg(scip, "debug solution value: <%s> = %g", SCIPvarGetName(var), val);
220 
221  if( presoldata->flipping )
222  {
223  if( REALABS(ub) < REALABS(lb) )
224  val = ub - val;
225  else
226  val = val - lb;
227  }
228  else
229  {
230  val = val - lb;
231  }
232  SCIPdebugMsgPrint(scip, " -> <%s> = %g\n", SCIPvarGetName(newvar), val);
233 
234  SCIP_CALL( SCIPdebugAddSolVal(scip, newvar, val) );
235  }
236 #endif
237 
238  /* aggregate old variable with new variable */
239  if( presoldata->flipping )
240  {
241  if( REALABS(ub) < REALABS(lb) )
242  {
243  SCIP_CALL( SCIPaggregateVars(scip, var, newvar, 1.0, 1.0, ub, &infeasible, &redundant, &aggregated) );
244  }
245  else
246  {
247  SCIP_CALL( SCIPaggregateVars(scip, var, newvar, 1.0, -1.0, lb, &infeasible, &redundant, &aggregated) );
248  }
249  }
250  else
251  {
252  SCIP_CALL( SCIPaggregateVars(scip, var, newvar, 1.0, -1.0, lb, &infeasible, &redundant, &aggregated) );
253  }
254 
255  if( infeasible )
256  *result = SCIP_CUTOFF;
257  else
258  {
259  assert(redundant);
260  assert(aggregated);
261  SCIPdebugMsg(scip, "var <%s> with bounds [%f,%f] has obj %f\n",
262  SCIPvarGetName(newvar), SCIPvarGetLbGlobal(newvar), SCIPvarGetUbGlobal(newvar), SCIPvarGetObj(newvar));
263 
264  /* take care of statistics */
265  (*naggrvars)++;
266  *result = SCIP_SUCCESS;
267  }
268 
269  /* release variable */
270  SCIP_CALL( SCIPreleaseVar(scip, &newvar) );
271  }
272  }
273 
274  /* free temporary memory */
275  SCIPfreeBufferArray(scip, &vars);
276 
277  return SCIP_OKAY;
278 }
279 
280 
281 /*
282  * presolver specific interface methods
283  */
284 
285 /** creates the boundshift presolver and includes it in SCIP */
287  SCIP* scip /**< SCIP data structure */
288  )
289 {
290  SCIP_PRESOLDATA* presoldata;
291  SCIP_PRESOL* presolptr;
292 
293  /* create boundshift presolver data */
294  SCIP_CALL( SCIPallocBlockMemory(scip, &presoldata) );
295 
296  /* include presolver */
298  presolExecBoundshift,
299  presoldata) );
300 
301  assert(presolptr != NULL);
302 
303  SCIP_CALL( SCIPsetPresolCopy(scip, presolptr, presolCopyBoundshift) );
304  SCIP_CALL( SCIPsetPresolFree(scip, presolptr, presolFreeBoundshift) );
305 
306  /* add probing presolver parameters */
308  "presolving/boundshift/maxshift",
309  "absolute value of maximum shift",
310  &presoldata->maxshift, TRUE, DEFAULT_MAXSHIFT, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
311 
313  "presolving/boundshift/flipping",
314  "is flipping allowed (multiplying with -1)?",
315  &presoldata->flipping, TRUE, DEFAULT_FLIPPING, NULL, NULL) );
316 
318  "presolving/boundshift/integer",
319  "shift only integer ranges?",
320  &presoldata->integer, TRUE, DEFAULT_INTEGER, NULL, NULL) );
321 
322  return SCIP_OKAY;
323 }
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)
Definition: scip_presol.c:96
struct SCIP_PresolData SCIP_PRESOLDATA
Definition: type_presol.h:42
#define PRESOL_NAME
#define PRESOL_MAXROUNDS
SCIP_RETCODE SCIPsetPresolFree(SCIP *scip, SCIP_PRESOL *presol, SCIP_DECL_PRESOLFREE((*presolfree)))
Definition: scip_presol.c:147
public methods for SCIP parameter handling
public methods for memory management
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:17910
#define SCIP_MAXSTRLEN
Definition: def.h:293
SCIP_Bool SCIPvarIsInitial(SCIP_VAR *var)
Definition: var.c:17452
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition: scip_var.c:1245
#define DEFAULT_FLIPPING
SCIP_Real SCIPadjustedVarUb(SCIP *scip, SCIP_VAR *var, SCIP_Real ub)
Definition: scip_var.c:4642
public methods for presolving plugins
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:102
#define PRESOL_PRIORITY
SCIP_Real SCIPinfinity(SCIP *scip)
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10755
#define TRUE
Definition: def.h:86
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
SCIP_RETCODE SCIPincludePresolBoundshift(SCIP *scip)
SCIP_PRESOLDATA * SCIPpresolGetData(SCIP_PRESOL *presol)
Definition: presol.c:503
static SCIP_DECL_PRESOLEXEC(presolExecBoundshift)
public methods for problem variables
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:99
#define SCIPduplicateBufferArray(scip, ptr, source, num)
Definition: scip_mem.h:123
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
#define SCIP_LONGINT_MAX
Definition: def.h:163
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip_mem.h:127
SCIP_Real SCIPadjustedVarLb(SCIP *scip, SCIP_VAR *var, SCIP_Real lb)
Definition: scip_var.c:4610
SCIP_Bool SCIPvarIsRemovable(SCIP_VAR *var)
Definition: var.c:17462
public methods for SCIP variables
#define SCIPdebugMsgPrint
Definition: scip_message.h:70
#define SCIPdebugMsg
Definition: scip_message.h:69
static SCIP_DECL_PRESOLCOPY(presolCopyBoundshift)
public methods for numerical tolerances
#define PRESOL_TIMING
static SCIP_DECL_PRESOLFREE(presolFreeBoundshift)
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition: var.c:17920
#define DEFAULT_INTEGER
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
void SCIPpresolSetData(SCIP_PRESOL *presol, SCIP_PRESOLDATA *presoldata)
Definition: presol.c:513
const char * SCIPvarGetName(SCIP_VAR *var)
Definition: var.c:17251
#define NULL
Definition: lpi_spx1.cpp:155
#define REALABS(x)
Definition: def.h:201
#define SCIP_CALL(x)
Definition: def.h:384
#define SCIPdebugGetSolVal(scip, var, val)
Definition: debug.h:281
#define PRESOL_DESC
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:84
methods for debugging
#define DEFAULT_MAXSHIFT
const char * SCIPpresolGetName(SCIP_PRESOL *presol)
Definition: presol.c:590
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
Definition: var.c:17758
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:105
int SCIPgetNBinVars(SCIP *scip)
Definition: scip_prob.c:2035
int SCIPgetNVars(SCIP *scip)
Definition: scip_prob.c:1990
public methods for presolvers
#define MAXABSBOUND
SCIP_Bool SCIPisGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisIntegral(SCIP *scip, SCIP_Real val)
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
Definition: scip_prob.c:1666
SCIP_RETCODE SCIPsetPresolCopy(SCIP *scip, SCIP_PRESOL *presol, SCIP_DECL_PRESOLCOPY((*presolcopy)))
Definition: scip_presol.c:131
public methods for message output
SCIP_VAR ** SCIPgetVars(SCIP *scip)
Definition: scip_prob.c:1945
SCIP_RETCODE SCIPaggregateVars(SCIP *scip, SCIP_VAR *varx, SCIP_VAR *vary, SCIP_Real scalarx, SCIP_Real scalary, SCIP_Real rhs, SCIP_Bool *infeasible, SCIP_Bool *redundant, SCIP_Bool *aggregated)
Definition: scip_var.c:8398
#define SCIP_Real
Definition: def.h:177
public methods for message handling
SCIP_Bool SCIPdoNotAggr(SCIP *scip)
Definition: scip_var.c:8562
#define SCIP_Longint
Definition: def.h:162
#define SCIPdebugAddSolVal(scip, var, val)
Definition: debug.h:280
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
Definition: var.c:17416
SCIP_Bool SCIPisLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIPallocBlockMemory(scip, subsol))
public methods for global and local (sub)problems
SCIP_Bool SCIPvarIsIntegral(SCIP_VAR *var)
Definition: var.c:17442
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:48
SCIP_Bool SCIPvarIsActive(SCIP_VAR *var)
Definition: var.c:17580
presolver that converts integer variables with domain [a,b] to integer variables with domain [0...
memory allocation routines