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-2014 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 /*
180  * constraint specific interface methods
181  */
182 
183 /** creates the handler for integrality constraint and includes it in SCIP */
185  SCIP* scip /**< SCIP data structure */
186  )
187 {
188  SCIP_CONSHDLRDATA* conshdlrdata;
189  SCIP_CONSHDLR* conshdlr;
190 
191  /* create integral constraint handler data */
192  conshdlrdata = NULL;
193 
194  /* include constraint handler */
197  consEnfolpIntegral, consEnfopsIntegral, consCheckIntegral, consLockIntegral,
198  conshdlrdata) );
199 
200  assert(conshdlr != NULL);
201 
202  /* set non-fundamental callbacks via specific setter functions */
203  SCIP_CALL( SCIPsetConshdlrCopy(scip, conshdlr, conshdlrCopyIntegral, consCopyIntegral) );
204 
205  return SCIP_OKAY;
206 }
207