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