Scippy

SCIP

Solving Constraint Integer Programs

cons_integral.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-2017 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 email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file cons_integral.c
17  * @brief constraint handler for the integrality constraint
18  * @author Tobias Achterberg
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <assert.h>
24 #include <string.h>
25 #include <limits.h>
26 
27 #include "scip/cons_integral.h"
28 
29 
30 #define CONSHDLR_NAME "integral"
31 #define CONSHDLR_DESC "integrality constraint"
32 #define CONSHDLR_ENFOPRIORITY 0 /**< priority of the constraint handler for constraint enforcing */
33 #define CONSHDLR_CHECKPRIORITY 0 /**< priority of the constraint handler for checking feasibility */
34 #define CONSHDLR_EAGERFREQ -1 /**< frequency for using all instead of only the useful constraints in separation,
35  * propagation and enforcement, -1 for no eager evaluations, 0 for first only */
36 #define CONSHDLR_NEEDSCONS FALSE /**< should the constraint handler be skipped, if no constraints are available? */
37 
38 /*
39  * Callback methods
40  */
41 
42 /** copy method for constraint handler plugins (called when SCIP copies plugins) */
43 static
44 SCIP_DECL_CONSHDLRCOPY(conshdlrCopyIntegral)
45 { /*lint --e{715}*/
46  assert(scip != NULL);
47  assert(conshdlr != NULL);
48  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
49 
50  /* call inclusion method of constraint handler */
52 
53  *valid = TRUE;
54 
55  return SCIP_OKAY;
56 }
57 
58 #define consCopyIntegral NULL
59 
60 #define consEnfopsIntegral NULL
61 
62 /** constraint enforcing method of constraint handler for LP solutions */
63 static
64 SCIP_DECL_CONSENFOLP(consEnfolpIntegral)
65 { /*lint --e{715}*/
66  assert(conshdlr != NULL);
67  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
68  assert(scip != NULL);
69  assert(conss == NULL);
70  assert(nconss == 0);
71  assert(result != NULL);
72 
73  SCIPdebugMsg(scip, "Enfolp method of integrality constraint: %d fractional variables\n", SCIPgetNLPBranchCands(scip));
74 
75  /* if the root LP is unbounded, we want to terminate with UNBOUNDED or INFORUNBOUNDED,
76  * depending on whether we are able to construct an integral solution; in any case we do not want to branch
77  */
79  {
80  if( SCIPgetNLPBranchCands(scip) == 0 )
81  *result = SCIP_FEASIBLE;
82  else
83  *result = SCIP_INFEASIBLE;
84  return SCIP_OKAY;
85  }
86 
87  /* call branching methods */
88  SCIP_CALL( SCIPbranchLP(scip, result) );
89 
90  /* if no branching was done, the LP solution was not fractional */
91  if( *result == SCIP_DIDNOTRUN )
92  *result = SCIP_FEASIBLE;
93 
94  return SCIP_OKAY;
95 }
96 
97 /** constraint enforcing method of constraint handler for relaxation solutions */
98 static
99 SCIP_DECL_CONSENFORELAX(consEnforelaxIntegral)
100 { /*lint --e{715}*/
101  SCIP_VAR** vars;
102  int nbinvars;
103  int nintvars;
104  int i;
105 
106  assert(conshdlr != NULL);
107  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
108  assert(scip != NULL);
109  assert(conss == NULL);
110  assert(nconss == 0);
111  assert(result != NULL);
112 
113  SCIPdebugMsg(scip, "Enforelax method of integrality constraint\n");
114 
115  *result = SCIP_FEASIBLE;
116 
117  SCIP_CALL( SCIPgetVarsData(scip, &vars, NULL, &nbinvars, &nintvars, NULL, NULL) );
118 
119  for( i = 0; i < nbinvars + nintvars; ++i )
120  {
121  assert(vars[i] != NULL);
122  assert(SCIPvarIsIntegral(vars[i]));
123 
124  if( !SCIPisFeasIntegral(scip, SCIPgetSolVal(scip, sol, vars[i])) )
125  {
126  if( SCIPisFeasEQ(scip, SCIPvarGetLbLocal(vars[i]), SCIPvarGetUbLocal(vars[i])) )
127  {
128  SCIPdebugMsg(scip, "Cutoff for integral variable %s with bounds [%f, %f] and value %f\n", SCIPvarGetName(vars[i]),
129  SCIPvarGetLbLocal(vars[i]), SCIPvarGetUbLocal(vars[i]), SCIPgetSolVal(scip, sol, vars[i]));
130  *result = SCIP_CUTOFF;
131  return SCIP_OKAY;
132  }
133  else
134  {
135  /* @todo better way to handle this would be a BRANCHEXECRELAX callback that could also implement pseudo costs for
136  * relaxation solutions instead of using the enforelaxcallback which is mainly intended for spatial branching
137  */
138  SCIP_CALL( SCIPaddExternBranchCand(scip, vars[i], 0.2, SCIPgetSolVal(scip, sol, vars[i])) );
139  *result = SCIP_INFEASIBLE;
140  }
141  }
142  }
143 
144  /* if we have found a branching candidate, immediately branch to be able to return SCIP_BRANCHED and stop the
145  * enforcement loop
146  */
147  if( *result == SCIP_INFEASIBLE )
148  {
149  /* call branching methods for external candidates */
150  SCIP_CALL( SCIPbranchExtern(scip, result) );
151 
152  /* since we only call it if we added external candidates, the branching rule should always be able to branch */
153  assert(*result != SCIP_DIDNOTRUN);
154  }
155 
156  return SCIP_OKAY;
157 }
158 
159 /** feasibility check method of constraint handler for integral solutions */
160 static
161 SCIP_DECL_CONSCHECK(consCheckIntegral)
162 { /*lint --e{715}*/
163  SCIP_VAR** vars;
164  SCIP_Real solval;
165  int nallinteger;
166  int ninteger;
167  int nbin;
168  int nint;
169  int nimpl;
170  int v;
171 
172  assert(conshdlr != NULL);
173  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
174  assert(scip != NULL);
175 
176  SCIPdebugMsg(scip, "Check method of integrality constraint (checkintegrality=%u)\n", checkintegrality);
177 
178  SCIP_CALL( SCIPgetSolVarsData(scip, sol, &vars, NULL, &nbin, &nint, &nimpl, NULL) );
179 
180  *result = SCIP_FEASIBLE;
181 
182  ninteger = nbin + nint;
183 
184  if( checkintegrality )
185  {
186  for( v = 0; v < ninteger; ++v )
187  {
188  solval = SCIPgetSolVal(scip, sol, vars[v]);
189  if( !SCIPisFeasIntegral(scip, solval) )
190  {
191  *result = SCIP_INFEASIBLE;
192 
193  if( printreason )
194  {
195  SCIPinfoMessage(scip, NULL, "violation: integrality condition of variable <%s> = %.15g\n",
196  SCIPvarGetName(vars[v]), solval);
197  }
198  if( !completely )
199  break;
200  }
201  }
202  }
203 #ifndef NDEBUG
204  else
205  {
206  for( v = 0; v < ninteger; ++v )
207  {
208  solval = SCIPgetSolVal(scip, sol, vars[v]);
209  assert(SCIPisFeasIntegral(scip, solval));
210  }
211  }
212 #endif
213 
214  nallinteger = ninteger + nimpl;
215  for( v = ninteger; v < nallinteger; ++v )
216  {
217  solval = SCIPgetSolVal(scip, sol, vars[v]);
218  if( !SCIPisFeasIntegral(scip, solval) )
219  {
220  *result = SCIP_INFEASIBLE;
221 
222  if( printreason )
223  {
224  SCIPinfoMessage(scip, NULL, "violation: integrality condition of implicit integral variable <%s> = %.15g\n",
225  SCIPvarGetName(vars[v]), solval);
226  }
227  if( !completely )
228  break;
229  }
230  }
231 
232  return SCIP_OKAY;
233 }
234 
235 /** variable rounding lock method of constraint handler */
236 static
237 SCIP_DECL_CONSLOCK(consLockIntegral)
238 { /*lint --e{715}*/
239  return SCIP_OKAY;
240 }
241 
242 /** constraint handler method to suggest dive bound changes during the generic diving algorithm */
243 static
244 SCIP_DECL_CONSGETDIVEBDCHGS(consGetDiveBdChgsIntegral)
245 { /*lint --e{715}*/
246  SCIP_VAR** vars;
247  SCIP_Real solval;
248  SCIP_Real score;
249  SCIP_Real bestscore;
250  SCIP_Bool roundup;
251  int ninteger;
252  int nbin;
253  int nint;
254  int nimpl;
255  int v;
256  int bestcandidx;
257 
258  assert(scip != NULL);
259  assert(sol != NULL);
260  assert(diveset != NULL);
261 
262  assert(conshdlr != NULL);
263  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
264  assert(scip != NULL);
265 
266  SCIPdebugMsg(scip, "integral constraint handler: determine diving bound changes\n");
267 
268  SCIP_CALL( SCIPgetSolVarsData(scip, sol, &vars, NULL, &nbin, &nint, &nimpl, NULL) );
269 
270  ninteger = nbin + nint + nimpl;
271  bestscore = SCIP_REAL_MIN;
272  bestcandidx = -1;
273  *success = FALSE;
274  roundup = FALSE; /* only for lint */
275 
276  /* loop over solution values and get score of fractional variables */
277  for( v = 0; v < ninteger; ++v )
278  {
279  solval = SCIPgetSolVal(scip, sol, vars[v]);
280 
281  /* skip variable if solution value disagrees with the local bounds */
282  if( ! SCIPisFeasIntegral(scip, solval) && SCIPisGE(scip, solval, SCIPvarGetLbLocal(vars[v])) && SCIPisLE(scip, solval, SCIPvarGetUbLocal(vars[v])) )
283  {
284  SCIP_CALL( SCIPgetDivesetScore(scip, diveset, SCIP_DIVETYPE_INTEGRALITY, vars[v], solval,
285  solval - SCIPfloor(scip, solval), &score, &roundup) );
286 
287  /* we search for candidates with maximum score */
288  if( score > bestscore )
289  {
290  bestcandidx = v;
291  bestscore = score;
292  *success = TRUE;
293  }
294  }
295  }
296 
297  assert(!(*success) || bestcandidx >= 0);
298 
299  if( *success )
300  {
301  solval = SCIPgetSolVal(scip, sol, vars[bestcandidx]);
302 
303  /* if we want to round up the best candidate, it is added as the preferred bound change */
305  SCIPceil(scip, solval), roundup) );
307  SCIPfloor(scip, solval), ! roundup) );
308  }
309 
310  return SCIP_OKAY;
311 
312 }
313 
314 /*
315  * constraint specific interface methods
316  */
317 
318 /** creates the handler for integrality constraint and includes it in SCIP */
320  SCIP* scip /**< SCIP data structure */
321  )
322 {
323  SCIP_CONSHDLR* conshdlr;
324 
325  /* include constraint handler */
328  consEnfolpIntegral, consEnfopsIntegral, consCheckIntegral, consLockIntegral, NULL) );
329 
330  assert(conshdlr != NULL);
331 
332  /* set non-fundamental callbacks via specific setter functions */
333  SCIP_CALL( SCIPsetConshdlrCopy(scip, conshdlr, conshdlrCopyIntegral, consCopyIntegral) );
334  SCIP_CALL( SCIPsetConshdlrGetDiveBdChgs(scip, conshdlr, consGetDiveBdChgsIntegral) );
335  SCIP_CALL( SCIPsetConshdlrEnforelax(scip, conshdlr, consEnforelaxIntegral) );
336 
337  return SCIP_OKAY;
338 }
SCIP_Bool SCIPisFeasEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:46086
static SCIP_DECL_CONSLOCK(consLockIntegral)
SCIP_RETCODE SCIPgetSolVarsData(SCIP *scip, SCIP_SOL *sol, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition: scip.c:12260
#define consCopyIntegral
Definition: cons_integral.c:59
SCIP_RETCODE SCIPsetConshdlrEnforelax(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSENFORELAX((*consenforelax)))
Definition: scip.c:5973
SCIP_Real SCIPvarGetLbLocal(SCIP_VAR *var)
Definition: var.c:17222
SCIP_Bool SCIPisGE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:45803
SCIP_RETCODE SCIPgetVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition: scip.c:11505
#define FALSE
Definition: def.h:64
static SCIP_DECL_CONSHDLRCOPY(conshdlrCopyIntegral)
Definition: cons_integral.c:45
#define CONSHDLR_NAME
Definition: cons_integral.c:30
#define CONSHDLR_EAGERFREQ
Definition: cons_integral.c:34
SCIP_RETCODE SCIPincludeConshdlrBasic(SCIP *scip, SCIP_CONSHDLR **conshdlrptr, const char *name, const char *desc, int enfopriority, int chckpriority, int eagerfreq, SCIP_Bool needscons, SCIP_DECL_CONSENFOLP((*consenfolp)), SCIP_DECL_CONSENFOPS((*consenfops)), SCIP_DECL_CONSCHECK((*conscheck)), SCIP_DECL_CONSLOCK((*conslock)), SCIP_CONSHDLRDATA *conshdlrdata)
Definition: scip.c:5831
#define TRUE
Definition: def.h:63
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
SCIP_RETCODE SCIPaddDiveBoundChange(SCIP *scip, SCIP_VAR *var, SCIP_BRANCHDIR dir, SCIP_Real value, SCIP_Bool preferred)
Definition: scip.c:36042
int SCIPgetNLPBranchCands(SCIP *scip)
Definition: scip.c:36161
#define SCIPdebugMsg
Definition: scip.h:451
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip.c:1336
#define consEnfopsIntegral
Definition: cons_integral.c:61
SCIP_RETCODE SCIPsetConshdlrGetDiveBdChgs(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSGETDIVEBDCHGS((*consgetdivebdchgs)))
Definition: scip.c:6527
SCIP_RETCODE SCIPsetConshdlrCopy(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSHDLRCOPY((*conshdlrcopy)), SCIP_DECL_CONSCOPY((*conscopy)))
Definition: scip.c:5997
const char * SCIPconshdlrGetName(SCIP_CONSHDLR *conshdlr)
Definition: cons.c:4113
#define CONSHDLR_ENFOPRIORITY
Definition: cons_integral.c:32
const char * SCIPvarGetName(SCIP_VAR *var)
Definition: var.c:16552
#define NULL
Definition: lpi_spx1.cpp:137
static SCIP_DECL_CONSGETDIVEBDCHGS(consGetDiveBdChgsIntegral)
#define SCIP_CALL(x)
Definition: def.h:306
static SCIP_DECL_CONSENFORELAX(consEnforelaxIntegral)
#define SCIP_Bool
Definition: def.h:61
SCIP_LPSOLSTAT SCIPgetLPSolstat(SCIP *scip)
Definition: scip.c:28854
#define CONSHDLR_NEEDSCONS
Definition: cons_integral.c:37
static SCIP_DECL_CONSCHECK(consCheckIntegral)
SCIP_RETCODE SCIPaddExternBranchCand(SCIP *scip, SCIP_VAR *var, SCIP_Real score, SCIP_Real solval)
Definition: scip.c:36399
#define SCIP_REAL_MIN
Definition: def.h:137
static SCIP_DECL_CONSENFOLP(consEnfolpIntegral)
Definition: cons_integral.c:65
constraint handler for the integrality constraint
#define SCIP_Real
Definition: def.h:135
SCIP_RETCODE SCIPbranchLP(SCIP *scip, SCIP_RESULT *result)
Definition: scip.c:36921
SCIP_RETCODE SCIPbranchExtern(SCIP *scip, SCIP_RESULT *result)
Definition: scip.c:36945
SCIP_Bool SCIPisLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:45777
SCIP_Real SCIPvarGetUbLocal(SCIP_VAR *var)
Definition: var.c:17232
SCIP_RETCODE SCIPgetDivesetScore(SCIP *scip, SCIP_DIVESET *diveset, SCIP_DIVETYPE divetype, SCIP_VAR *divecand, SCIP_Real divecandsol, SCIP_Real divecandfrac, SCIP_Real *candscore, SCIP_Bool *roundup)
Definition: scip.c:35902
SCIP_Bool SCIPisFeasIntegral(SCIP *scip, SCIP_Real val)
Definition: scip.c:46187
#define CONSHDLR_DESC
Definition: cons_integral.c:31
SCIP_Real SCIPceil(SCIP *scip, SCIP_Real val)
Definition: scip.c:45949
#define SCIP_DIVETYPE_INTEGRALITY
Definition: type_heur.h:45
#define CONSHDLR_CHECKPRIORITY
Definition: cons_integral.c:33
SCIP_Bool SCIPvarIsIntegral(SCIP_VAR *var)
Definition: var.c:16743
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:38007
SCIP_Real SCIPfloor(SCIP *scip, SCIP_Real val)
Definition: scip.c:45937
SCIP_RETCODE SCIPincludeConshdlrIntegral(SCIP *scip)