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-2016 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  SCIPdebugMessage("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 
98 /** feasibility check method of constraint handler for integral solutions */
99 static
100 SCIP_DECL_CONSCHECK(consCheckIntegral)
101 { /*lint --e{715}*/
102  SCIP_VAR** vars;
103  SCIP_Real solval;
104  int nallinteger;
105  int ninteger;
106  int nbin;
107  int nint;
108  int nimpl;
109  int v;
110 
111  assert(conshdlr != NULL);
112  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
113  assert(scip != NULL);
114 
115  SCIPdebugMessage("Check method of integrality constraint (checkintegrality=%u)\n", checkintegrality);
116 
117  SCIP_CALL( SCIPgetSolVarsData(scip, sol, &vars, NULL, &nbin, &nint, &nimpl, NULL) );
118 
119  *result = SCIP_FEASIBLE;
120 
121  ninteger = nbin + nint;
122 
123  if( checkintegrality )
124  {
125  for( v = 0; v < ninteger; ++v )
126  {
127  solval = SCIPgetSolVal(scip, sol, vars[v]);
128  if( !SCIPisFeasIntegral(scip, solval) )
129  {
130  *result = SCIP_INFEASIBLE;
131 
132  if( printreason )
133  {
134  SCIPinfoMessage(scip, NULL, "violation: integrality condition of variable <%s> = %.15g\n",
135  SCIPvarGetName(vars[v]), solval);
136  }
137  break;
138  }
139  }
140  }
141 #ifndef NDEBUG
142  else
143  {
144  for( v = 0; v < ninteger; ++v )
145  {
146  solval = SCIPgetSolVal(scip, sol, vars[v]);
147  assert(SCIPisFeasIntegral(scip, solval));
148  }
149  }
150 #endif
151 
152  nallinteger = ninteger + nimpl;
153  for( v = ninteger; v < nallinteger; ++v )
154  {
155  solval = SCIPgetSolVal(scip, sol, vars[v]);
156  if( !SCIPisFeasIntegral(scip, solval) )
157  {
158  *result = SCIP_INFEASIBLE;
159 
160  if( printreason )
161  {
162  SCIPinfoMessage(scip, NULL, "violation: integrality condition of implicit integral variable <%s> = %.15g\n",
163  SCIPvarGetName(vars[v]), solval);
164  }
165  break;
166  }
167  }
168 
169  return SCIP_OKAY;
170 }
171 
172 /** variable rounding lock method of constraint handler */
173 static
174 SCIP_DECL_CONSLOCK(consLockIntegral)
175 { /*lint --e{715}*/
176  return SCIP_OKAY;
177 }
178 
179 /** constraint handler method to suggest dive bound changes during the generic diving algorithm */
180 static
181 SCIP_DECL_CONSGETDIVEBDCHGS(consGetDiveBdChgsIntegral)
182 { /*lint --e{715}*/
183  SCIP_VAR** vars;
184  SCIP_Real solval;
185  SCIP_Real score;
186  SCIP_Real bestscore;
187  SCIP_Bool roundup;
188  int ninteger;
189  int nbin;
190  int nint;
191  int nimpl;
192  int v;
193  int bestcandidx;
194 
195  assert(scip != NULL);
196  assert(sol != NULL);
197  assert(diveset != NULL);
198 
199  assert(conshdlr != NULL);
200  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
201  assert(scip != NULL);
202 
203  SCIPdebugMessage("integral constraint handler: determine diving bound changes\n");
204 
205  SCIP_CALL( SCIPgetSolVarsData(scip, sol, &vars, NULL, &nbin, &nint, &nimpl, NULL) );
206 
207  ninteger = nbin + nint + nimpl;
208  bestscore = SCIP_REAL_MIN;
209  bestcandidx = -1;
210  *success = FALSE;
211  roundup = FALSE; /* only for lint */
212 
213  /* loop over solution values and get score of fractional variables */
214  for( v = 0; v < ninteger; ++v )
215  {
216  solval = SCIPgetSolVal(scip, sol, vars[v]);
217 
218  /* skip variable if solution value disagrees with the local bounds */
219  if( ! SCIPisFeasIntegral(scip, solval) && SCIPisGE(scip, solval, SCIPvarGetLbLocal(vars[v])) && SCIPisLE(scip, solval, SCIPvarGetUbLocal(vars[v])) )
220  {
221  SCIP_CALL( SCIPgetDivesetScore(scip, diveset, SCIP_DIVETYPE_INTEGRALITY, vars[v], solval, solval - SCIPfloor(scip, solval), &score, &roundup) );
222 
223  /* we search for candidates with maximum score */
224  if( score > bestscore )
225  {
226  bestcandidx = v;
227  bestscore = score;
228  *success = TRUE;
229  }
230  }
231  }
232 
233  assert(!(*success) || bestcandidx >= 0);
234 
235  if( *success )
236  {
237  solval = SCIPgetSolVal(scip, sol, vars[bestcandidx]);
238 
239  /* if we want to round up the best candidate, it is added as the preferred bound change */
241  SCIPceil(scip, solval), roundup) );
243  SCIPfloor(scip, solval), ! roundup) );
244  }
245 
246  return SCIP_OKAY;
247 
248 }
249 
250 /*
251  * constraint specific interface methods
252  */
253 
254 /** creates the handler for integrality constraint and includes it in SCIP */
256  SCIP* scip /**< SCIP data structure */
257  )
258 {
259  SCIP_CONSHDLRDATA* conshdlrdata;
260  SCIP_CONSHDLR* conshdlr;
261 
262  /* create integral constraint handler data */
263  conshdlrdata = NULL;
264 
265  /* include constraint handler */
268  consEnfolpIntegral, consEnfopsIntegral, consCheckIntegral, consLockIntegral,
269  conshdlrdata) );
270 
271  assert(conshdlr != NULL);
272 
273  /* set non-fundamental callbacks via specific setter functions */
274  SCIP_CALL( SCIPsetConshdlrCopy(scip, conshdlr, conshdlrCopyIntegral, consCopyIntegral) );
275  SCIP_CALL( SCIPsetConshdlrGetDiveBdChgs(scip, conshdlr, consGetDiveBdChgsIntegral) );
276 
277  return SCIP_OKAY;
278 }
int SCIPgetNLPBranchCands(SCIP *scip)
Definition: scip.c:33158
SCIP_RETCODE SCIPaddDiveBoundChange(SCIP *scip, SCIP_VAR *var, SCIP_BRANCHDIR dir, SCIP_Real value, SCIP_Bool preferred)
Definition: scip.c:33039
const char * SCIPvarGetName(SCIP_VAR *var)
Definition: var.c:16443
static SCIP_DECL_CONSLOCK(consLockIntegral)
#define consCopyIntegral
Definition: cons_integral.c:59
#define NULL
Definition: lpi_spx.cpp:130
SCIP_Real SCIPvarGetLbLocal(SCIP_VAR *var)
Definition: var.c:17113
SCIP_RETCODE SCIPsetConshdlrCopy(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSHDLRCOPY((*conshdlrcopy)), SCIP_DECL_CONSCOPY((*conscopy)))
Definition: scip.c:5334
#define FALSE
Definition: def.h:56
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
#define TRUE
Definition: def.h:55
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
#define SCIP_CALL(x)
Definition: def.h:266
SCIP_Bool SCIPisFeasIntegral(SCIP *scip, SCIP_Real val)
Definition: scip.c:42008
SCIP_LPSOLSTAT SCIPgetLPSolstat(SCIP *scip)
Definition: scip.c:26439
#define SCIPdebugMessage
Definition: pub_message.h:77
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:34983
#define consEnfopsIntegral
Definition: cons_integral.c:61
const char * SCIPconshdlrGetName(SCIP_CONSHDLR *conshdlr)
Definition: cons.c:3897
SCIP_Bool SCIPisLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:41598
SCIP_Real SCIPfloor(SCIP *scip, SCIP_Real val)
Definition: scip.c:41758
#define CONSHDLR_ENFOPRIORITY
Definition: cons_integral.c:32
SCIP_RETCODE SCIPgetSolVarsData(SCIP *scip, SCIP_SOL *sol, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition: scip.c:11327
static SCIP_DECL_CONSGETDIVEBDCHGS(consGetDiveBdChgsIntegral)
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:5192
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:32901
SCIP_Real SCIPvarGetUbLocal(SCIP_VAR *var)
Definition: var.c:17123
#define SCIP_Bool
Definition: def.h:53
#define CONSHDLR_NEEDSCONS
Definition: cons_integral.c:37
SCIP_RETCODE SCIPsetConshdlrGetDiveBdChgs(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSGETDIVEBDCHGS((*consgetdivebdchgs)))
Definition: scip.c:5864
static SCIP_DECL_CONSCHECK(consCheckIntegral)
SCIP_RETCODE SCIPbranchLP(SCIP *scip, SCIP_RESULT *result)
Definition: scip.c:33918
SCIP_Bool SCIPisGE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:41624
#define SCIP_REAL_MIN
Definition: def.h:129
SCIP_Real SCIPceil(SCIP *scip, SCIP_Real val)
Definition: scip.c:41770
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip.c:1281
static SCIP_DECL_CONSENFOLP(consEnfolpIntegral)
Definition: cons_integral.c:65
constraint handler for the integrality constraint
#define SCIP_Real
Definition: def.h:127
struct SCIP_ConshdlrData SCIP_CONSHDLRDATA
Definition: type_cons.h:49
#define CONSHDLR_DESC
Definition: cons_integral.c:31
#define SCIP_DIVETYPE_INTEGRALITY
Definition: type_heur.h:45
#define CONSHDLR_CHECKPRIORITY
Definition: cons_integral.c:33
SCIP_RETCODE SCIPincludeConshdlrIntegral(SCIP *scip)