Scippy

SCIP

Solving Constraint Integer Programs

presol_inttobinary.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_inttobinary.c
17  * @brief presolver that converts integer variables with domain [a,a+1] to binaries
18  * @author Tobias Achterberg
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include "blockmemshell/memory.h"
25 #include "scip/pub_message.h"
26 #include "scip/pub_misc.h"
27 #include "scip/pub_presol.h"
28 #include "scip/pub_var.h"
29 #include "scip/scip_mem.h"
30 #include "scip/scip_message.h"
31 #include "scip/scip_numerics.h"
32 #include "scip/scip_presol.h"
33 #include "scip/scip_prob.h"
34 #include "scip/scip_var.h"
35 #include <string.h>
36 
37 #define PRESOL_NAME "inttobinary"
38 #define PRESOL_DESC "converts integer variables with domain [a,a+1] to binaries"
39 #define PRESOL_PRIORITY +7000000 /**< priority of the presolver (>= 0: before, < 0: after constraint handlers) */
40 #define PRESOL_MAXROUNDS -1 /**< maximal number of presolving rounds the presolver participates in (-1: no limit) */
41 #define PRESOL_TIMING SCIP_PRESOLTIMING_FAST /* timing of the presolver (fast, medium, or exhaustive) */
42 
43 /*
44  * Callback methods of presolver
45  */
46 
47 /** copy method for constraint handler plugins (called when SCIP copies plugins) */
48 static
49 SCIP_DECL_PRESOLCOPY(presolCopyInttobinary)
50 { /*lint --e{715}*/
51  assert(scip != NULL);
52  assert(presol != NULL);
53  assert(strcmp(SCIPpresolGetName(presol), PRESOL_NAME) == 0);
54 
55  /* call inclusion method of presolver */
57 
58  return SCIP_OKAY;
59 }
60 
61 
62 /** presolving execution method */
63 static
64 SCIP_DECL_PRESOLEXEC(presolExecInttobinary)
65 { /*lint --e{715}*/
66  SCIP_VAR** scipvars;
67  SCIP_VAR** vars;
68  int nbinvars;
69  int nintvars;
70  int v;
71 
72  assert(result != NULL);
73 
74  *result = SCIP_DIDNOTRUN;
75 
76  if( SCIPdoNotAggr(scip) )
77  return SCIP_OKAY;
78 
79  /* get the problem variables */
80  scipvars = SCIPgetVars(scip);
81  nbinvars = SCIPgetNBinVars(scip);
82  nintvars = SCIPgetNIntVars(scip);
83  if( nintvars == 0 )
84  return SCIP_OKAY;
85 
86  *result = SCIP_DIDNOTFIND;
87 
88  /* copy the integer variables into an own array, since adding binary variables affects the left-most slots in the
89  * array and thereby interferes with our search loop
90  */
91  SCIP_CALL( SCIPduplicateBufferArray(scip, &vars, &scipvars[nbinvars], nintvars) );
92 
93  /* scan the integer variables for possible conversion into binaries;
94  * we have to collect the variables first in an own
95  */
96  for( v = 0; v < nintvars; ++v )
97  {
98  SCIP_Real lb;
99  SCIP_Real ub;
100 
101  assert(SCIPvarGetType(vars[v]) == SCIP_VARTYPE_INTEGER);
102 
103  /* get variable's bounds */
104  lb = SCIPvarGetLbGlobal(vars[v]);
105  ub = SCIPvarGetUbGlobal(vars[v]);
106 
107  /* check if bounds are exactly one apart */
108  if( SCIPisEQ(scip, lb, ub - 1.0) )
109  {
110  SCIP_VAR* binvar;
111  char binvarname[SCIP_MAXSTRLEN];
112  SCIP_Bool infeasible;
113  SCIP_Bool redundant;
114  SCIP_Bool aggregated;
115 
116  SCIPdebugMsg(scip, "converting <%s>[%g,%g] into binary variable\n", SCIPvarGetName(vars[v]), lb, ub);
117 
118  /* create binary variable */
119  (void) SCIPsnprintf(binvarname, SCIP_MAXSTRLEN, "%s_bin", SCIPvarGetName(vars[v]));
120  SCIP_CALL( SCIPcreateVar(scip, &binvar, binvarname, 0.0, 1.0, 0.0, SCIP_VARTYPE_BINARY,
121  SCIPvarIsInitial(vars[v]), SCIPvarIsRemovable(vars[v]), NULL, NULL, NULL, NULL, NULL) );
122  SCIP_CALL( SCIPaddVar(scip, binvar) );
123 
124  /* aggregate integer and binary variable */
125  SCIP_CALL( SCIPaggregateVars(scip, vars[v], binvar, 1.0, -1.0, lb, &infeasible, &redundant, &aggregated) );
126 
127  /* release binary variable */
128  SCIP_CALL( SCIPreleaseVar(scip, &binvar) );
129 
130  /* it can be the case that this aggregation detects an infeasibility; for example, during the copy of the
131  * variable bounds from the integer variable to the binary variable, infeasibility can be detected; this can
132  * happen because an upper bound or a lower bound of such a variable bound variable was "just" changed and the
133  * varbound constraint handler, who would detect that infeasibility (since it was creating it from a varbound
134  * constraint), was called before that bound change was detected due to the presolving priorities;
135  */
136  if( infeasible )
137  {
138  *result = SCIP_CUTOFF;
139  break;
140  }
141 
142  assert(redundant);
143  assert(aggregated);
144  (*nchgvartypes)++;
145  ++(*naggrvars);
146  *result = SCIP_SUCCESS;
147  }
148  }
149 
150  /* free temporary memory */
151  SCIPfreeBufferArray(scip, &vars);
152 
153  return SCIP_OKAY;
154 }
155 
156 
157 /*
158  * presolver specific interface methods
159  */
160 
161 /** creates the inttobinary presolver and includes it in SCIP */
163  SCIP* scip /**< SCIP data structure */
164  )
165 {
166  SCIP_PRESOLDATA* presoldata;
167  SCIP_PRESOL* presolptr;
168 
169  /* create inttobinary presolver data */
170  presoldata = NULL;
171 
172  /* include presolver */
174  presolExecInttobinary,
175  presoldata) );
176 
177  assert(presolptr != NULL);
178 
179  SCIP_CALL( SCIPsetPresolCopy(scip, presolptr, presolCopyInttobinary) );
180 
181  return SCIP_OKAY;
182 }
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
int SCIPgetNIntVars(SCIP *scip)
Definition: scip_prob.c:2134
struct SCIP_PresolData SCIP_PRESOLDATA
Definition: type_presol.h:37
#define NULL
Definition: def.h:239
public methods for memory management
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:17343
#define SCIP_MAXSTRLEN
Definition: def.h:260
#define PRESOL_DESC
SCIP_Bool SCIPvarIsInitial(SCIP_VAR *var)
Definition: var.c:16930
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition: scip_var.c:1251
public methods for presolving plugins
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10017
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
public methods for problem variables
#define SCIPduplicateBufferArray(scip, ptr, source, num)
Definition: scip_mem.h:138
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip_mem.h:142
SCIP_Bool SCIPvarIsRemovable(SCIP_VAR *var)
Definition: var.c:16940
public methods for SCIP variables
#define SCIPdebugMsg
Definition: scip_message.h:88
static SCIP_DECL_PRESOLEXEC(presolExecInttobinary)
public methods for numerical tolerances
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition: var.c:17353
const char * SCIPvarGetName(SCIP_VAR *var)
Definition: var.c:16729
#define SCIP_CALL(x)
Definition: def.h:351
#define PRESOL_TIMING
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:62
#define PRESOL_NAME
#define PRESOL_PRIORITY
const char * SCIPpresolGetName(SCIP_PRESOL *presol)
Definition: presol.c:589
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
public methods for presolvers
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
#define PRESOL_MAXROUNDS
static SCIP_DECL_PRESOLCOPY(presolCopyInttobinary)
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
SCIP_RETCODE SCIPincludePresolInttobinary(SCIP *scip)
public methods for message handling
SCIP_Bool SCIPdoNotAggr(SCIP *scip)
Definition: scip_var.c:8441
presolver that converts integer variables with domain [a,a+1] to binaries
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
Definition: var.c:16894
public methods for global and local (sub)problems
memory allocation routines