Scippy

SCIP

Solving Constraint Integer Programs

heur_dins.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 heur_dins.c
17  * @brief DINS primal heuristic (according to Ghosh)
18  * @author Timo Berthold
19  * @author Robert Waniek
20  */
21 
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include <assert.h>
25 #include <string.h>
26 #include "scip/scip.h"
27 #include "scip/scipdefplugins.h"
28 #include "scip/cons_linear.h"
29 #include "scip/heur_dins.h"
30 
31 #define HEUR_NAME "dins"
32 #define HEUR_DESC "distance induced neighborhood search by Ghosh"
33 #define HEUR_DISPCHAR 'D'
34 #define HEUR_PRIORITY -1105000
35 #define HEUR_FREQ -1
36 #define HEUR_FREQOFS 0
37 #define HEUR_MAXDEPTH -1
38 #define HEUR_TIMING SCIP_HEURTIMING_AFTERLPNODE
39 #define HEUR_USESSUBSCIP TRUE /**< does the heuristic use a secondary SCIP instance? */
40 
41 #define DEFAULT_NODESOFS 5000LL /* number of nodes added to the contingent of the total nodes */
42 #define DEFAULT_MAXNODES 5000LL /* maximum number of nodes to regard in the subproblem */
43 #define DEFAULT_MINNODES 50LL /* minimum number of nodes to regard in the subproblem */
44 #define DEFAULT_MINIMPROVE 0.01 /* factor by which DINS should at least improve the incumbent */
45 #define DEFAULT_NODESQUOT 0.05 /* subproblem nodes in relation to nodes of the original problem */
46 #define DEFAULT_LPLIMFAC 1.5 /* factor by which the limit on the number of LP depends on the node limit */
47 #define DEFAULT_MINFIXINGRATE 0.3 /* minimum percentage of integer variables that have to be fixed */
48 #define DEFAULT_NWAITINGNODES 0LL /* number of nodes without incumbent change that heuristic should wait */
49 #define DEFAULT_NEIGHBORHOODSIZE 18 /* radius of the incumbents neighborhood to be searched */
50 #define DEFAULT_SOLNUM 5 /* number of pool-solutions to be checked for flag array update */
51 #define DEFAULT_USELPROWS FALSE /* should subproblem be created out of the rows in the LP rows,
52  * otherwise, the copy constructors of the constraints handlers are used */
53 #define DEFAULT_COPYCUTS TRUE /* if DEFAULT_USELPROWS is FALSE, then should all active cuts from the cutpool
54  * of the original scip be copied to constraints of the subscip */
55 
56 /* event handler properties */
57 #define EVENTHDLR_NAME "Dins"
58 #define EVENTHDLR_DESC "LP event handler for "HEUR_NAME" heuristic"
59 
60 /*
61  * Data structures
62  */
63 
64 /** DINS primal heuristic data */
65 struct SCIP_HeurData
66 {
67  SCIP_Longint nodesofs; /**< number of nodes added to the contingent of the total nodes */
68  SCIP_Longint maxnodes; /**< maximum number of nodes to regard in the subproblem */
69  SCIP_Longint minnodes; /**< minimum number of nodes to regard in the subproblem */
70  SCIP_Real minfixingrate; /**< minimum percentage of integer variables that have to be fixed */
71  SCIP_Longint nwaitingnodes; /**< number of nodes without incumbent change that heuristic should wait */
72  SCIP_Real minimprove; /**< factor by which DINS should at least improve the incumbent */
73  SCIP_Longint usednodes; /**< nodes already used by DINS in earlier calls */
74  SCIP_Real nodesquot; /**< subproblem nodes in relation to nodes of the original problem */
75  SCIP_Real nodelimit; /**< the nodelimit employed in the current sub-SCIP, for the event handler*/
76  SCIP_Real lplimfac; /**< factor by which the limit on the number of LP depends on the node limit */
77  int neighborhoodsize; /**< radius of the incumbent's neighborhood to be searched */
78  SCIP_Bool* delta; /**< stores whether a variable kept its value from root LP all the time */
79  int deltalength; /**< if there are no binary variables, we need no flag array */
80  SCIP_Longint lastnsolsfound; /**< solutions found until the last call of DINS */
81  int solnum; /**< number of pool-solutions to be checked for flag array update */
82  SCIP_Bool uselprows; /**< should subproblem be created out of the rows in the LP rows? */
83  SCIP_Bool copycuts; /**< if uselprows == FALSE, should all active cuts from cutpool be copied
84  * to constraints in subproblem?
85  */
86 };
87 
88 
89 /*
90  * Local methods
91  */
92 
93 /** creates a subproblem for subscip by fixing a number of variables */
94 static
96  SCIP* scip, /**< SCIP data structure of the original problem */
97  SCIP* subscip, /**< SCIP data structure of the subproblem */
98  SCIP_VAR** vars, /**< variables of the original problem */
99  SCIP_VAR** subvars, /**< variables of the subproblem */
100  int nbinvars, /**< number of binary variables of problem and subproblem */
101  int nintvars, /**< number of general integer variables of problem and subproblem */
102  int* fixingcounter, /**< number of integer variables that get fixed */
103  SCIP_Bool uselprows /**< should subproblem be created out of the rows in the LP rows? */
104  )
105 {
106  SCIP_SOL* bestsol;
107  int i;
108 
109  assert(scip != NULL);
110  assert(subscip != NULL);
111  assert(vars != NULL);
112  assert(subvars != NULL);
113 
114  /* get the best MIP-solution known so far */
115  bestsol = SCIPgetBestSol(scip);
116  assert(bestsol != NULL);
117 
118  /* create the rebounded general integer variables of the subproblem */
119  for( i = nbinvars; i < nbinvars + nintvars; i++ )
120  {
121  SCIP_Real mipsol;
122  SCIP_Real lpsol;
123 
124  SCIP_Real lbglobal;
125  SCIP_Real ubglobal;
126 
127  /* get the bounds for each variable */
128  lbglobal = SCIPvarGetLbGlobal(vars[i]);
129  ubglobal = SCIPvarGetUbGlobal(vars[i]);
130 
131  assert(SCIPvarGetType(vars[i]) == SCIP_VARTYPE_INTEGER);
132  /* get the current LP solution for each variable */
133  lpsol = SCIPvarGetLPSol(vars[i]);
134  /* get the current MIP solution for each variable */
135  mipsol = SCIPgetSolVal(scip, bestsol, vars[i]);
136 
137  /* if the solution values differ by 0.5 or more, the variable is rebounded, otherwise it is just copied */
138  if( REALABS(lpsol-mipsol) >= 0.5 )
139  {
140  SCIP_Real lb;
141  SCIP_Real ub;
142  SCIP_Real range;
143 
144  lb = lbglobal;
145  ub = ubglobal;
146 
147  /* create a equally sized range around lpsol for general integers: bounds are lpsol +- (mipsol-lpsol) */
148  range = 2*lpsol-mipsol;
149 
150  if( mipsol >= lpsol )
151  {
152  range = SCIPfeasCeil(scip, range);
153  lb = MAX(lb, range);
154 
155  /* when the bound new upper bound is equal to the current MIP solution, we set both bounds to the integral bound (without eps) */
156  if( SCIPisFeasEQ(scip, mipsol, lb) )
157  ub = lb;
158  else
159  ub = mipsol;
160  }
161  else
162  {
163  range = SCIPfeasFloor(scip, range);
164  ub = MIN(ub, range);
165 
166  /* when the bound new upper bound is equal to the current MIP solution, we set both bounds to the integral bound (without eps) */
167  if( SCIPisFeasEQ(scip, mipsol, ub) )
168  lb = ub;
169  else
170  lb = mipsol;
171  }
172 
173  /* the global domain of variables might have been reduced since incumbent was found: adjust lb and ub accordingly */
174  lb = MAX(lb, lbglobal);
175  ub = MIN(ub, ubglobal);
176 
177  /* perform the bound change */
178  SCIP_CALL( SCIPchgVarLbGlobal(subscip, subvars[i], lb) );
179  SCIP_CALL( SCIPchgVarUbGlobal(subscip, subvars[i], ub) );
180 
181  if( ub-lb < 0.5 )
182  (*fixingcounter)++;
183  }
184  else
185  {
186  /* the global domain of variables might have been reduced since incumbent was found: adjust it accordingly */
187  mipsol = MAX(mipsol, lbglobal);
188  mipsol = MIN(mipsol, ubglobal);
189 
190  /* hard fixing for general integer variables with abs(mipsol-lpsol) < 0.5 */
191  SCIP_CALL( SCIPchgVarLbGlobal(subscip, subvars[i], mipsol) );
192  SCIP_CALL( SCIPchgVarUbGlobal(subscip, subvars[i], mipsol) );
193  (*fixingcounter)++;
194  }
195  }
196 
197  if( uselprows )
198  {
199  SCIP_ROW** rows; /* original scip rows */
200  int nrows;
201 
202  /* get the rows and their number */
203  SCIP_CALL( SCIPgetLPRowsData(scip, &rows, &nrows) );
204 
205  /* copy all rows to linear constraints */
206  for( i = 0; i < nrows; i++ )
207  {
208  SCIP_CONS* cons;
209  SCIP_VAR** consvars;
210  SCIP_COL** cols;
211  SCIP_Real constant;
212  SCIP_Real lhs;
213  SCIP_Real rhs;
214  SCIP_Real* vals;
215 
216  int nnonz;
217  int j;
218 
219  /* ignore rows that are only locally valid */
220  if( SCIProwIsLocal(rows[i]) )
221  continue;
222 
223  /* get the row's data */
224  constant = SCIProwGetConstant(rows[i]);
225  lhs = SCIProwGetLhs(rows[i]) - constant;
226  rhs = SCIProwGetRhs(rows[i]) - constant;
227  vals = SCIProwGetVals(rows[i]);
228  nnonz = SCIProwGetNNonz(rows[i]);
229  cols = SCIProwGetCols(rows[i]);
230 
231  assert(lhs <= rhs);
232 
233  /* allocate memory array to be filled with the corresponding subproblem variables */
234  SCIP_CALL( SCIPallocBufferArray(subscip, &consvars, nnonz) );
235  for( j = 0; j < nnonz; j++ )
236  consvars[j] = subvars [ SCIPvarGetProbindex(SCIPcolGetVar(cols[j])) ];
237 
238  /* create a new linear constraint and add it to the subproblem */
239  SCIP_CALL( SCIPcreateConsLinear(subscip, &cons, SCIProwGetName(rows[i]), nnonz, consvars, vals, lhs, rhs,
240  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE) );
241  SCIP_CALL( SCIPaddCons(subscip, cons) );
242  SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
243 
244  /* free temporary memory */
245  SCIPfreeBufferArray(subscip, &consvars);
246  }
247  }
248 
249  return SCIP_OKAY;
250 }
251 
252 /** create the extra constraint of local branching and add it to subscip */
253 static
255  SCIP* scip, /**< SCIP data structure of the original problem */
256  SCIP* subscip, /**< SCIP data structure of the subproblem */
257  SCIP_VAR** subvars, /**< variables of the subproblem */
258  SCIP_HEURDATA* heurdata, /**< heuristic's data structure */
259  SCIP_Bool* fixed /**< TRUE --> include variable in LB constraint */
260  )
261 {
262  SCIP_CONS* cons; /* local branching constraint to create */
263  SCIP_VAR** consvars;
264  SCIP_VAR** vars;
265  SCIP_SOL* bestsol;
266 
267  SCIP_Real* consvals;
268  SCIP_Real solval;
269  SCIP_Real lhs;
270  SCIP_Real rhs;
271 
272  char consname[SCIP_MAXSTRLEN];
273 
274  int nbinvars;
275  int i;
276 
277  (void) SCIPsnprintf(consname, SCIP_MAXSTRLEN, "%s_dinsLBcons", SCIPgetProbName(scip));
278 
279  /* get the data of the variables and the best solution */
280  SCIP_CALL( SCIPgetVarsData(scip, &vars, NULL, &nbinvars, NULL, NULL, NULL) );
281  bestsol = SCIPgetBestSol(scip);
282  assert(bestsol != NULL);
283 
284  /* memory allocation */
285  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nbinvars) );
286  SCIP_CALL( SCIPallocBufferArray(scip, &consvals, nbinvars) );
287 
288  /* set initial left and right hand sides of local branching constraint */
289  lhs = 0.0;
290  rhs = (SCIP_Real) heurdata->neighborhoodsize;
291 
292  /* create the distance function of the binary variables (to incumbent solution) */
293  for( i = 0; i < nbinvars; i++ )
294  {
295  consvars[i] = subvars[i];
296  assert(SCIPvarGetType(consvars[i]) == SCIP_VARTYPE_BINARY);
297  if( fixed[i] )
298  {
299  consvals[i]=0.0;
300  continue;
301  }
302 
303  solval = SCIPgetSolVal(scip, bestsol, vars[i]);
304  assert(SCIPisFeasIntegral(scip, solval));
305 
306  /* is variable i part of the binary support of the current solution? */
307  if( SCIPisFeasEQ(scip, solval, 1.0) )
308  {
309  consvals[i] = -1.0;
310  rhs -= 1.0;
311  lhs -= 1.0;
312  }
313  else
314  consvals[i] = 1.0;
315  }
316 
317  /* creates local branching constraint and adds it to subscip */
318  SCIP_CALL( SCIPcreateConsLinear(subscip, &cons, consname, nbinvars, consvars, consvals,
319  lhs, rhs, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE) );
320  SCIP_CALL( SCIPaddCons(subscip, cons) );
321  SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
322 
323  /* free local memory */
324  SCIPfreeBufferArray(scip, &consvals);
325  SCIPfreeBufferArray(scip, &consvars);
326 
327  return SCIP_OKAY;
328 }
329 
330 /** creates a new solution for the original problem by copying the solution of the subproblem */
331 static
333  SCIP* scip, /**< original SCIP data structure */
334  SCIP* subscip, /**< SCIP structure of the subproblem */
335  SCIP_VAR** subvars, /**< the variables of the subproblem */
336  SCIP_HEUR* heur, /**< DINS heuristic structure */
337  SCIP_SOL* subsol, /**< solution of the subproblem */
338  SCIP_Bool* success /**< used to store whether new solution was found or not */
339  )
340 {
341  SCIP_VAR** vars; /* the original problem's variables */
342  int nvars;
343  SCIP_Real* subsolvals; /* solution values of the subproblem */
344  SCIP_SOL* newsol; /* solution to be created for the original problem */
345 
346  assert(scip != NULL);
347  assert(heur != NULL);
348  assert(subscip != NULL);
349  assert(subvars != NULL);
350  assert(subsol != NULL);
351 
352  /* get variables' data */
353  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
354  /* sub-SCIP may have more variables than the number of active (transformed) variables in the main SCIP
355  * since constraint copying may have required the copy of variables that are fixed in the main SCIP
356  */
357  assert(nvars <= SCIPgetNOrigVars(subscip));
358 
359  SCIP_CALL( SCIPallocBufferArray(scip, &subsolvals, nvars) );
360 
361  /* copy the solution */
362  SCIP_CALL( SCIPgetSolVals(subscip, subsol, nvars, subvars, subsolvals) );
363 
364  /* create new solution for the original problem */
365  SCIP_CALL( SCIPcreateSol(scip, &newsol, heur) );
366  SCIP_CALL( SCIPsetSolVals(scip, newsol, nvars, vars, subsolvals) );
367 
368  /* try to add new solution to scip and free it immediately */
369  SCIP_CALL( SCIPtrySolFree(scip, &newsol, FALSE, TRUE, TRUE, TRUE, success) );
370  if( *success )
371  {
372  SCIPdebugMessage("DINS successfully found new solution\n");
373  }
374 
375  SCIPfreeBufferArray(scip, &subsolvals);
376  return SCIP_OKAY;
377 }
378 
379 
380 /* ---------------- Callback methods of event handler ---------------- */
381 
382 /* exec the event handler
383  *
384  * we interrupt the solution process
385  */
386 static
387 SCIP_DECL_EVENTEXEC(eventExecRins)
388 {
389  SCIP_HEURDATA* heurdata;
390 
391  assert(eventhdlr != NULL);
392  assert(eventdata != NULL);
393  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
394  assert(event != NULL);
395  assert(SCIPeventGetType(event) & SCIP_EVENTTYPE_LPSOLVED);
396 
397  heurdata = (SCIP_HEURDATA*)eventdata;
398  assert(heurdata != NULL);
399 
400  /* interrupt solution process of sub-SCIP */
401  if( SCIPgetNLPs(scip) > heurdata->lplimfac * heurdata->nodelimit )
402  {
403  SCIPdebugMessage("interrupt after %"SCIP_LONGINT_FORMAT" LPs\n",SCIPgetNLPs(scip));
404  SCIP_CALL( SCIPinterruptSolve(scip) );
405  }
406 
407  return SCIP_OKAY;
408 }
409 
410 
411 /*
412  * Callback methods of primal heuristic
413  */
414 
415 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
416 static
417 SCIP_DECL_HEURCOPY(heurCopyDins)
418 { /*lint --e{715}*/
419  assert(scip != NULL);
420  assert(heur != NULL);
421  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
422 
423  /* call inclusion method of primal heuristic */
425 
426  return SCIP_OKAY;
427 }
428 
429 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
430 static
431 SCIP_DECL_HEURFREE(heurFreeDins)
432 { /*lint --e{715}*/
433  SCIP_HEURDATA* heurdata;
434 
435  assert(heur != NULL);
436  assert(scip != NULL);
437 
438  /* get heuristic data */
439  heurdata = SCIPheurGetData(heur);
440  assert(heurdata != NULL);
441 
442  /* free heuristic data */
443  SCIPfreeMemory(scip, &heurdata);
444  SCIPheurSetData(heur, NULL);
445 
446  return SCIP_OKAY;
447 }
448 
449 
450 /** solving process initialization method of primal heuristic (called when branch and bound process is about to begin) */
451 static
452 SCIP_DECL_HEURINITSOL(heurInitsolDins)
453 {
454  SCIP_HEURDATA* heurdata;
455  int i;
456 
457  assert(heur != NULL);
458  assert(scip != NULL);
459 
460  /* get heuristic's data */
461  heurdata = SCIPheurGetData(heur);
462  assert(heurdata != NULL);
463 
464  /* initialize data */
465  heurdata->usednodes = 0;
466  heurdata->lastnsolsfound = 0;
467 
468  /* create flag array */
469  heurdata->deltalength = SCIPgetNBinVars(scip);
470 
471  /* no binvars => no flag array needed */
472  if( heurdata->deltalength > 0 )
473  {
474  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(heurdata->delta), heurdata->deltalength) );
475  for( i = 0; i < heurdata->deltalength; i++ )
476  heurdata->delta[i] = TRUE;
477  }
478  return SCIP_OKAY;
479 }
480 
481 /** solving process deinitialization method of primal heuristic (called before branch and bound process data is freed) */
482 static
483 SCIP_DECL_HEUREXITSOL(heurExitsolDins)
484 { /*lint --e{715}*/
485  SCIP_HEURDATA* heurdata;
486 
487  assert(heur != NULL);
488  assert(scip != NULL);
489 
490  /* get heuristic data */
491  heurdata = SCIPheurGetData(heur);
492  assert(heurdata != NULL);
493 
494  /* free flag array if exist */
495  if( heurdata->deltalength > 0 )
496  {
497  SCIPfreeBlockMemoryArray(scip, &(heurdata->delta), heurdata->deltalength);
498  }
499  return SCIP_OKAY;
500 }
501 
502 /** execution method of primal heuristic */
503 static
504 SCIP_DECL_HEUREXEC(heurExecDins)
505 { /*lint --e{715}*/
506  SCIP_HEURDATA* heurdata;
507  SCIP* subscip; /* the subproblem created by DINS */
508  SCIP_VAR** subvars; /* subproblem's variables */
509  SCIP_VAR** vars; /* variables of the original problem */
510  SCIP_HASHMAP* varmapfw; /* mapping of SCIP variables to sub-SCIP variables */
511  SCIP_SOL* bestsol; /* best solution known so far */
512  SCIP_SOL** sols; /* list of known solutions */
513  SCIP_EVENTHDLR* eventhdlr; /* event handler for LP events */
514 
515  SCIP_Bool* fixed; /* fixing flag array */
516  SCIP_Bool* delta; /* flag array if variable value changed during solution process */
517 
518 
519  SCIP_Longint maxnnodes; /* maximum number of subnodes */
520  SCIP_Longint nsubnodes; /* nodelimit for subscip */
521  SCIP_Longint nsolsfound;
522 
523  SCIP_Real timelimit; /* timelimit for subscip (equals remaining time of scip) */
524  SCIP_Real cutoff; /* objective cutoff for the subproblem */
525  SCIP_Real upperbound;
526  SCIP_Real memorylimit; /* memory limit for solution process of subscip */
527  SCIP_Real lpsolval;
528  SCIP_Real rootlpsolval;
529  SCIP_Real mipsolval;
530  SCIP_Real solval;
531 
532  int ufcount; /* counts the number of true fixing flag entries */
533  int nvars; /* number of variables in original SCIP */
534  int nbinvars; /* number of binary variables in original SCIP */
535  int nintvars; /* number of general integer variables in original SCIP */
536  int nsols; /* number of known solutions */
537  int nsubsols;
538  int checklength;
539  int fixingcounter;
540  int i;
541  int j;
542 
543  SCIP_Bool success; /* used to store whether new solution was found or not */
544  SCIP_Bool infeasible; /* stores whether the hard fixing of a variables was feasible or not */
545 
546  SCIP_RETCODE retcode;
547 
548  assert(heur != NULL);
549  assert(scip != NULL);
550  assert(result != NULL);
551  assert(SCIPhasCurrentNodeLP(scip));
552 
553  *result = SCIP_DELAYED;
554 
555  /* do not call heuristic of node was already detected to be infeasible */
556  if( nodeinfeasible )
557  return SCIP_OKAY;
558 
559  /* only call heuristic, if a CIP solution is at hand */
560  if( SCIPgetNSols(scip) <= 0 )
561  return SCIP_OKAY;
562 
563  /* only call heuristic, if an optimal LP solution is at hand */
565  return SCIP_OKAY;
566 
567  /* only call heuristic, if the LP objective value is smaller than the cutoff bound */
568  if( SCIPisGE(scip, SCIPgetLPObjval(scip), SCIPgetCutoffbound(scip)) )
569  return SCIP_OKAY;
570 
571  /* get heuristic's data */
572  heurdata = SCIPheurGetData(heur);
573  assert(heurdata != NULL);
574  delta = heurdata->delta;
575 
576  /* only call heuristic, if enough nodes were processed since last incumbent */
577  if( SCIPgetNNodes(scip) - SCIPgetSolNodenum(scip, SCIPgetBestSol(scip)) < heurdata->nwaitingnodes )
578  return SCIP_OKAY;
579 
580  *result = SCIP_DIDNOTRUN;
581 
582  /* determine the node limit for the current process */
583  maxnnodes = (SCIP_Longint) (heurdata->nodesquot * SCIPgetNNodes(scip));
584 
585  /* reward DINS if it succeeded often */
586  maxnnodes = (SCIP_Longint) (maxnnodes * (1.0 + 2.0 * (SCIPheurGetNBestSolsFound(heur)+1.0) / (SCIPheurGetNCalls(heur) + 1.0)));
587 
588  /* count the setup costs for the sub-MIP as 100 nodes */
589  maxnnodes -= 100 * SCIPheurGetNCalls(heur);
590  maxnnodes += heurdata->nodesofs;
591 
592  /* determine the node limit for the current process */
593  nsubnodes = maxnnodes - heurdata->usednodes;
594  nsubnodes = MIN(nsubnodes , heurdata->maxnodes);
595 
596  /* check whether we have enough nodes left to call sub problem solving */
597  if( nsubnodes < heurdata->minnodes )
598  return SCIP_OKAY;
599 
600  if( SCIPisStopped(scip) )
601  return SCIP_OKAY;
602 
603  /* get required data of the original problem */
604  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, &nbinvars, &nintvars, NULL, NULL) );
605  assert(nbinvars <= nvars);
606 
607  /* do not run heuristic if only continuous variables are present */
608  if( nbinvars == 0 && nintvars == 0 )
609  return SCIP_OKAY;
610 
611  assert(vars != NULL);
612 
613  /* initialize the subproblem */
614  SCIP_CALL( SCIPcreate(&subscip) );
615 
616  /* create the variable mapping hash map */
617  SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) );
618  SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), SCIPcalcHashtableSize(5 * nvars)) );
619 
620  success = FALSE;
621  eventhdlr = NULL;
622 
623  if( heurdata->uselprows )
624  {
625  char probname[SCIP_MAXSTRLEN];
626 
627  /* copy all plugins */
629 
630  /* get name of the original problem and add the string "_dinssub" */
631  (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s_dinssub", SCIPgetProbName(scip));
632 
633  /* create the subproblem */
634  SCIP_CALL( SCIPcreateProb(subscip, probname, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
635 
636  /* copy all variables */
637  SCIP_CALL( SCIPcopyVars(scip, subscip, varmapfw, NULL, TRUE) );
638  }
639  else
640  {
641  SCIP_CALL( SCIPcopy(scip, subscip, varmapfw, NULL, "dins", TRUE, FALSE, TRUE, &success) );
642 
643  if( heurdata->copycuts )
644  {
645  /* copies all active cuts from cutpool of sourcescip to linear constraints in targetscip */
646  SCIP_CALL( SCIPcopyCuts(scip, subscip, varmapfw, NULL, TRUE, NULL) );
647  }
648 
649  /* create event handler for LP events */
650  SCIP_CALL( SCIPincludeEventhdlrBasic(subscip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecRins, NULL) );
651  if( eventhdlr == NULL )
652  {
653  SCIPerrorMessage("event handler for "HEUR_NAME" heuristic not found.\n");
654  return SCIP_PLUGINNOTFOUND;
655  }
656 
657  SCIPdebugMessage("Copying the SCIP instance was %ssuccessful.\n", success ? "" : "not ");
658  }
659 
660  for( i = 0; i < nvars; i++ )
661  subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]);
662 
663  /* free hash map */
664  SCIPhashmapFree(&varmapfw);
665 
666  /* create variables and rebound them if their bounds differ by more than 0.5 */
667  fixingcounter = 0;
668  SCIP_CALL( createSubproblem(scip, subscip, vars, subvars, nbinvars, nintvars, &fixingcounter, heurdata->uselprows) );
669  SCIPdebugMessage("DINS subproblem: %d vars (%d binvars & %d intvars), %d cons\n",
670  SCIPgetNVars(subscip), SCIPgetNBinVars(subscip) , SCIPgetNIntVars(subscip) , SCIPgetNConss(subscip));
671 
672  *result = SCIP_DIDNOTFIND;
673 
674  /* do not abort subproblem on CTRL-C */
675  SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
676 
677  /* disable output to console */
678  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
679 
680 #ifdef SCIP_DEBUG
681  /* for debugging DINS, enable MIP output */
682  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 5) );
683  SCIP_CALL( SCIPsetIntParam(subscip, "display/freq", 100000000) );
684 #endif
685 
686  /* check whether there is enough time and memory left */
687  SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) );
688  if( !SCIPisInfinity(scip, timelimit) )
689  timelimit -= SCIPgetSolvingTime(scip);
690  SCIP_CALL( SCIPgetRealParam(scip, "limits/memory", &memorylimit) );
691 
692  /* substract the memory already used by the main SCIP and the estimated memory usage of external software */
693  if( !SCIPisInfinity(scip, memorylimit) )
694  {
695  memorylimit -= SCIPgetMemUsed(scip)/1048576.0;
696  memorylimit -= SCIPgetMemExternEstim(scip)/1048576.0;
697  }
698 
699  /* abort if no time is left or not enough memory to create a copy of SCIP, including external memory usage */
700  if( timelimit <= 0.0 || memorylimit <= 2.0*SCIPgetMemExternEstim(scip)/1048576.0 )
701  goto TERMINATE;
702 
703  /* set limits for the subproblem */
704  heurdata->nodelimit = nsubnodes;
705  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", nsubnodes) );
706  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/stallnodes", MAX(10, nsubnodes/10)) );
707  SCIP_CALL( SCIPsetIntParam(subscip, "limits/bestsol", 3) );
708  SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) );
709  SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) );
710 
711  /* forbid recursive call of heuristics and separators solving subMIPs */
712  SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
713 
714  /* disable cutting plane separation */
716 
717  /* disable expensive presolving */
719 
720  /* use best estimate node selection */
721  if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") )
722  {
723  SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) );
724  }
725 
726  /* use inference branching */
727  if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") )
728  {
729  SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) );
730  }
731 
732  /* disable conflict analysis */
733  if( !SCIPisParamFixed(subscip, "conflict/useprop") )
734  {
735  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useprop", FALSE) );
736  }
737  if( !SCIPisParamFixed(subscip, "conflict/useinflp") )
738  {
739  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useinflp", FALSE) );
740  }
741  if( !SCIPisParamFixed(subscip, "conflict/useboundlp") )
742  {
743  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useboundlp", FALSE) );
744  }
745  if( !SCIPisParamFixed(subscip, "conflict/usesb") )
746  {
747  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usesb", FALSE) );
748  }
749  if( !SCIPisParamFixed(subscip, "conflict/usepseudo") )
750  {
751  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usepseudo", FALSE) );
752  }
753 
754  /* employ a limit on the number of enforcement rounds in the quadratic constraint handler; this fixes the issue that
755  * sometimes the quadratic constraint handler needs hundreds or thousands of enforcement rounds to determine the
756  * feasibility status of a single node without fractional branching candidates by separation (namely for uflquad
757  * instances); however, the solution status of the sub-SCIP might get corrupted by this; hence no deductions shall be
758  * made for the original SCIP
759  */
760  if( SCIPfindConshdlr(subscip, "quadratic") != NULL && !SCIPisParamFixed(subscip, "constraints/quadratic/enfolplimit") )
761  {
762  SCIP_CALL( SCIPsetIntParam(subscip, "constraints/quadratic/enfolplimit", 500) );
763  }
764 
765  /* get the best MIP-solution known so far */
766  bestsol = SCIPgetBestSol(scip);
767  assert(bestsol != NULL);
768 
769  /* get solution pool and number of solutions in pool */
770  sols = SCIPgetSols(scip);
771  nsols = SCIPgetNSols(scip);
772  nsolsfound = SCIPgetNSolsFound(scip);
773  checklength = MIN(nsols, heurdata->solnum);
774  assert(sols != NULL);
775  assert(nsols > 0);
776 
777  /* create fixing flag array */
778  SCIP_CALL( SCIPallocBufferArray(scip, &fixed, nbinvars) );
779 
780  /* if new binary variables have been created, e.g., due to column generation, reallocate the delta array */
781  if( heurdata->deltalength < nbinvars )
782  {
783  int newsize;
784 
785  newsize = SCIPcalcMemGrowSize(scip, nbinvars);
786  assert(newsize >= nbinvars);
787 
788  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &heurdata->delta, heurdata->deltalength, newsize) );
789  delta = heurdata->delta;
790 
791  /* initialize new part of delta array */
792  for( i = heurdata->deltalength; i < newsize; i++ )
793  delta[i] = TRUE;
794 
795  heurdata->deltalength = newsize;
796  }
797 
798  /* fixing for binary variables */
799  /* hard fixing for some with mipsol(s)=lpsolval=rootlpsolval and preparation for soft fixing for the remaining */
800  ufcount = 0;
801  for( i = 0; i < nbinvars; i++ )
802  {
803  /* soft fixing if the variable somewhen changed its value or the relaxations differ by adding a local branching constraint */
804  fixed[i] = FALSE;
805 
806  /* get the current LP solution for each variable */
807  lpsolval = SCIPvarGetLPSol(vars[i]);
808  /* get the current MIP solution for each variable */
809  mipsolval = SCIPgetSolVal(scip, bestsol, vars[i]);
810  /* get the root LP solution for each variable */
811  rootlpsolval = SCIPvarGetRootSol(vars[i]);
812 
813  if( SCIPisFeasEQ(scip, lpsolval, mipsolval) && SCIPisFeasEQ(scip, mipsolval, rootlpsolval) )
814  {
815  /* update delta */
816  if( nsols > 1 && heurdata->lastnsolsfound != nsolsfound && delta[i] ) /* no need to update delta[i] if already FALSE */
817  {
818  /* no need to update delta[i] if already FALSE or sols[i] already checked on previous run or worse than DINS-solution of last run */
819  for( j = 0; delta[i] && j < checklength && SCIPgetSolHeur(scip, sols[j]) != heur ; j++ )
820  {
821  solval = SCIPgetSolVal(scip, sols[j], vars[i]);
822  delta[i] = delta[i] && SCIPisFeasEQ(scip, mipsolval, solval);
823  }
824  }
825 
826  /* hard fixing if rootlpsolval=nodelpsolval=mipsolval(s) and delta (is TRUE) */
827  if( delta[i] && SCIPisFeasEQ(scip, mipsolval, lpsolval) && SCIPisFeasEQ(scip, mipsolval, rootlpsolval)
828  && SCIPisFeasEQ(scip, rootlpsolval, lpsolval)
829  && !SCIPisFeasEQ(scip, SCIPvarGetLbGlobal(subvars[i]), SCIPvarGetUbGlobal(subvars[i])) )
830  {
831  SCIP_CALL( SCIPfixVar(subscip, subvars[i], mipsolval, &infeasible, &success) );
832  fixed[i] = !infeasible;
833 
834  if( success )
835  fixingcounter++;
836  else
837  {
838  SCIPdebugMessage("variable %d was already fixed\n", i);
839  }
840 
841  if( infeasible )
842  {
843  SCIPdebugMessage("fixing of variable %d to value %f was infeasible\n", i, mipsolval);
844  }
845  }
846  }
847  if( !fixed[i] )
848  ufcount++;
849  }
850 
851  /* store the number of found solutions for next run */
852  heurdata->lastnsolsfound = nsolsfound;
853 
854  /* perform prepared softfixing for all unfixed vars if the number of unfixed vars is larger than the neighborhoodsize (otherwise it will be useless) */
855  if( ufcount > heurdata->neighborhoodsize )
856  {
857  SCIP_CALL( addLocalBranchingConstraint(scip, subscip, subvars, heurdata, fixed) );
858  }
859 
860  /* free fixing flag array */
861  SCIPfreeBufferArray(scip, &fixed);
862 
863  /* abort, if all integer variables were fixed (which should not happen for MIP),
864  * but frequently happens for MINLPs using an LP relaxation
865  */
866  if( fixingcounter == nbinvars + nintvars )
867  goto TERMINATE;
868 
869  /* abort, if the amount of fixed variables is insufficient */
870  if( fixingcounter / (SCIP_Real)(MAX(nbinvars + nintvars, 1)) < heurdata->minfixingrate )
871  goto TERMINATE;
872 
873  /* add an objective cutoff */
874  cutoff = SCIPinfinity(scip);
875  assert(!SCIPisInfinity(scip, SCIPgetUpperbound(scip)));
876 
877  if( !SCIPisInfinity(scip, -1.0*SCIPgetLowerbound(scip)) )
878  {
879  cutoff = (1 - heurdata->minimprove) * SCIPgetUpperbound(scip) + heurdata->minimprove * SCIPgetLowerbound(scip);
880  upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip);
881  cutoff = MIN(upperbound, cutoff);
882  }
883  else
884  {
885  if( SCIPgetUpperbound(scip) >= 0 )
886  cutoff = (1 - heurdata->minimprove) * SCIPgetUpperbound(scip);
887  else
888  cutoff = (1 + heurdata->minimprove) * SCIPgetUpperbound(scip);
889  upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip);
890  cutoff = MIN(upperbound, cutoff);
891  }
892  SCIP_CALL( SCIPsetObjlimit(subscip, cutoff) );
893 
894  /* catch LP events of sub-SCIP */
895  if( !heurdata->uselprows )
896  {
897  assert(eventhdlr != NULL);
898 
899  SCIP_CALL( SCIPtransformProb(subscip) );
900  SCIP_CALL( SCIPcatchEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, NULL) );
901  }
902 
903  /* solve the subproblem */
904  SCIPdebugMessage("solving DINS sub-MIP with neighborhoodsize %d and maxnodes %"SCIP_LONGINT_FORMAT"\n", heurdata->neighborhoodsize, nsubnodes);
905  retcode = SCIPsolve(subscip);
906 
907  /* drop LP events of sub-SCIP */
908  if( !heurdata->uselprows )
909  {
910  assert(eventhdlr != NULL);
911 
912  SCIP_CALL( SCIPdropEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, -1) );
913  }
914 
915  /* Errors in solving the subproblem should not kill the overall solving process
916  * Hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
917  */
918  if( retcode != SCIP_OKAY )
919  {
920 #ifndef NDEBUG
921  SCIP_CALL( retcode );
922 #endif
923  SCIPwarningMessage(scip, "Error while solving subproblem in DINS heuristic; sub-SCIP terminated with code <%d>\n", retcode);
924  }
925 
926  /* print solving statistics of subproblem if we are in SCIP's debug mode */
928 
929  heurdata->usednodes += SCIPgetNNodes(subscip);
930  nsubsols = SCIPgetNSols(subscip);
931  SCIPdebugMessage("DINS used %"SCIP_LONGINT_FORMAT"/%"SCIP_LONGINT_FORMAT" nodes and found %d solutions\n", SCIPgetNNodes(subscip), nsubnodes, nsubsols);
932 
933  /* check, whether a (new) solution was found */
934  if( nsubsols > 0 )
935  {
936  SCIP_SOL** subsols;
937 
938  /* check, whether a solution was found; due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted */
939  subsols = SCIPgetSols(subscip);
940  success = FALSE;
941  for( i = 0; i < nsubsols && !success; ++i )
942  {
943  SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) );
944  }
945  if( success )
946  *result = SCIP_FOUNDSOL;
947  }
948 
949  TERMINATE:
950  /* free subproblem */
951  SCIPfreeBufferArray(scip, &subvars);
952  SCIP_CALL( SCIPfree(&subscip) );
953 
954  return SCIP_OKAY;
955 }
956 
957 
958 /*
959  * primal heuristic specific interface methods
960  */
962 /** creates the DINS primal heuristic and includes it in SCIP */
964  SCIP* scip /**< SCIP data structure */
965  )
966 {
967  SCIP_HEURDATA* heurdata;
968  SCIP_HEUR* heur;
969 
970  /* create Dins primal heuristic data */
971  SCIP_CALL( SCIPallocMemory(scip, &heurdata) );
972 
973  /* include primal heuristic */
974  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
976  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecDins, heurdata) );
977 
978  assert(heur != NULL);
979 
980  /* set non-NULL pointers to callback methods */
981  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyDins) );
982  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeDins) );
983  SCIP_CALL( SCIPsetHeurInitsol(scip, heur, heurInitsolDins) );
984  SCIP_CALL( SCIPsetHeurExitsol(scip, heur, heurExitsolDins) );
985 
986  /* add DINS primal heuristic parameters */
987  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/"HEUR_NAME"/nodesofs",
988  "number of nodes added to the contingent of the total nodes",
989  &heurdata->nodesofs, FALSE, DEFAULT_NODESOFS, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
990  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/nodesquot",
991  "contingent of sub problem nodes in relation to the number of nodes of the original problem",
992  &heurdata->nodesquot, FALSE, DEFAULT_NODESQUOT, 0.0, 1.0, NULL, NULL) );
993  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/"HEUR_NAME"/minnodes",
994  "minimum number of nodes required to start the subproblem",
995  &heurdata->minnodes, FALSE, DEFAULT_MINNODES, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
996  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/solnum",
997  "number of pool-solutions to be checked for flag array update (for hard fixing of binary variables)",
998  &heurdata->solnum, FALSE, DEFAULT_SOLNUM, 1, INT_MAX, NULL, NULL) );
999  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/neighborhoodsize",
1000  "radius (using Manhattan metric) of the incumbent's neighborhood to be searched",
1001  &heurdata->neighborhoodsize, FALSE, DEFAULT_NEIGHBORHOODSIZE, 1, INT_MAX, NULL, NULL) );
1002  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/"HEUR_NAME"/maxnodes",
1003  "maximum number of nodes to regard in the subproblem",
1004  &heurdata->maxnodes,TRUE,DEFAULT_MAXNODES, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
1005  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/minimprove",
1006  "factor by which "HEUR_NAME" should at least improve the incumbent",
1007  &heurdata->minimprove, TRUE, DEFAULT_MINIMPROVE, 0.0, 1.0, NULL, NULL) );
1008  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/"HEUR_NAME"/nwaitingnodes",
1009  "number of nodes without incumbent change that heuristic should wait",
1010  &heurdata->nwaitingnodes, TRUE, DEFAULT_NWAITINGNODES, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
1011 
1012  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/lplimfac",
1013  "factor by which the limit on the number of LP depends on the node limit",
1014  &heurdata->lplimfac, TRUE, DEFAULT_LPLIMFAC, 1.0, SCIP_REAL_MAX, NULL, NULL) );
1015 
1016  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/minfixingrate",
1017  "minimum percentage of integer variables that have to be fixable",
1018  &heurdata->minfixingrate, FALSE, DEFAULT_MINFIXINGRATE, 0.0, 1.0, NULL, NULL) );
1019 
1020  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/"HEUR_NAME"/uselprows",
1021  "should subproblem be created out of the rows in the LP rows?",
1022  &heurdata->uselprows, TRUE, DEFAULT_USELPROWS, NULL, NULL) );
1023 
1024  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/"HEUR_NAME"/copycuts",
1025  "if uselprows == FALSE, should all active cuts from cutpool be copied to constraints in subproblem?",
1026  &heurdata->copycuts, TRUE, DEFAULT_COPYCUTS, NULL, NULL) );
1027 
1028  return SCIP_OKAY;
1029 }
1030