Scippy

SCIP

Solving Constraint Integer Programs

heur_mutation.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_mutation.c
17  * @brief LNS heuristic that tries to randomly mutate the incumbent solution
18  * @author Timo Berthold
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 "scip/scip.h"
26 #include "scip/scipdefplugins.h"
27 #include "scip/cons_linear.h"
28 #include "scip/heur_mutation.h"
29 #include "scip/pub_misc.h"
30 
31 #define HEUR_NAME "mutation"
32 #define HEUR_DESC "mutation heuristic randomly fixing variables"
33 #define HEUR_DISPCHAR 'M'
34 #define HEUR_PRIORITY -1103000
35 #define HEUR_FREQ -1
36 #define HEUR_FREQOFS 8
37 #define HEUR_MAXDEPTH -1
38 #define HEUR_TIMING SCIP_HEURTIMING_AFTERNODE
39 #define HEUR_USESSUBSCIP TRUE /**< does the heuristic use a secondary SCIP instance? */
40 
41 #define DEFAULT_NODESOFS 500 /* number of nodes added to the contingent of the total nodes */
42 #define DEFAULT_MAXNODES 5000 /* maximum number of nodes to regard in the subproblem */
43 #define DEFAULT_MINIMPROVE 0.01 /* factor by which Mutation should at least improve the incumbent */
44 #define DEFAULT_MINNODES 500 /* minimum number of nodes to regard in the subproblem */
45 #define DEFAULT_MINFIXINGRATE 0.8 /* minimum percentage of integer variables that have to be fixed */
46 #define DEFAULT_NODESQUOT 0.1 /* subproblem nodes in relation to nodes of the original problem */
47 #define DEFAULT_NWAITINGNODES 200 /* number of nodes without incumbent change that heuristic should wait */
48 #define DEFAULT_USELPROWS FALSE /* should subproblem be created out of the rows in the LP rows,
49  * otherwise, the copy constructors of the constraints handlers are used */
50 #define DEFAULT_COPYCUTS TRUE /* if DEFAULT_USELPROWS is FALSE, then should all active cuts from the
51  * cutpool of the original scip be copied to constraints of the subscip */
52 
53 
54 /*
55  * Data structures
56  */
57 
58 /** primal heuristic data */
59 struct SCIP_HeurData
60 {
61  int nodesofs; /**< number of nodes added to the contingent of the total nodes */
62  int maxnodes; /**< maximum number of nodes to regard in the subproblem */
63  int minnodes; /**< minimum number of nodes to regard in the subproblem */
64  SCIP_Real minfixingrate; /**< minimum percentage of integer variables that have to be fixed */
65  int nwaitingnodes; /**< number of nodes without incumbent change that heuristic should wait */
66  SCIP_Real minimprove; /**< factor by which Mutation should at least improve the incumbent */
67  SCIP_Longint usednodes; /**< nodes already used by Mutation in earlier calls */
68  SCIP_Real nodesquot; /**< subproblem nodes in relation to nodes of the original problem */
69  unsigned int randseed; /**< seed value for random number generator */
70  SCIP_Bool uselprows; /**< should subproblem be created out of the rows in the LP rows? */
71  SCIP_Bool copycuts; /**< if uselprows == FALSE, should all active cuts from cutpool be copied
72  * to constraints in subproblem?
73  */
74 };
75 
76 
77 /*
78  * Local methods
79  */
80 
81 /** creates a subproblem for subscip by fixing a number of variables */
82 static
84  SCIP* scip, /**< original SCIP data structure */
85  SCIP* subscip, /**< SCIP data structure for the subproblem */
86  SCIP_VAR** subvars, /**< the variables of the subproblem */
87  SCIP_Real minfixingrate, /**< percentage of integer variables that have to be fixed */
88  unsigned int* randseed, /**< a seed value for the random number generator */
89  SCIP_Bool uselprows /**< should subproblem be created out of the rows in the LP rows? */
90  )
91 {
92  SCIP_VAR** vars; /* original scip variables */
93  SCIP_SOL* sol; /* pool of solutions */
94  SCIP_Bool* marked; /* array of markers, which variables to fixed */
95  SCIP_Bool fixingmarker; /* which flag should label a fixed variable? */
96 
97  int nvars;
98  int nbinvars;
99  int nintvars;
100  int i;
101  int j;
102  int nmarkers;
103 
104  /* get required data of the original problem */
105  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, &nbinvars, &nintvars, NULL, NULL) );
106  sol = SCIPgetBestSol(scip);
107  assert(sol != NULL);
108 
109 
110  SCIP_CALL( SCIPallocBufferArray(scip, &marked, nbinvars+nintvars) );
111 
112  if( minfixingrate > 0.5 )
113  {
114  nmarkers = nbinvars + nintvars - (int) SCIPfloor(scip, minfixingrate*(nbinvars+nintvars));
115  fixingmarker = FALSE;
116  }
117  else
118  {
119  nmarkers = (int) SCIPceil(scip, minfixingrate*(nbinvars+nintvars));
120  fixingmarker = TRUE;
121  }
122  assert( 0 <= nmarkers && nmarkers <= SCIPceil(scip,(nbinvars+nintvars)/2.0 ) );
123 
124  j = 0;
125  BMSclearMemoryArray(marked, nbinvars+nintvars);
126  while( j < nmarkers )
127  {
128  do
129  {
130  i = SCIPgetRandomInt(0, nbinvars+nintvars-1, randseed);
131  }
132  while( marked[i] );
133  marked[i] = TRUE;
134  j++;
135  }
136  assert( j == nmarkers );
137 
138  /* change bounds of variables of the subproblem */
139  for( i = 0; i < nbinvars + nintvars; i++ )
140  {
141  /* fix all randomly marked variables */
142  if( marked[i] == fixingmarker )
143  {
144  SCIP_Real solval;
145  SCIP_Real lb;
146  SCIP_Real ub;
147 
148  solval = SCIPgetSolVal(scip, sol, vars[i]);
149  lb = SCIPvarGetLbGlobal(subvars[i]);
150  ub = SCIPvarGetUbGlobal(subvars[i]);
151  assert(SCIPisLE(scip, lb, ub));
152 
153  /* due to dual reductions, it may happen that the solution value is not in
154  the variable's domain anymore */
155  if( SCIPisLT(scip, solval, lb) )
156  solval = lb;
157  else if( SCIPisGT(scip, solval, ub) )
158  solval = ub;
159 
160  /* perform the bound change */
161  if( !SCIPisInfinity(scip, solval) && !SCIPisInfinity(scip, -solval) )
162  {
163  SCIP_CALL( SCIPchgVarLbGlobal(subscip, subvars[i], solval) );
164  SCIP_CALL( SCIPchgVarUbGlobal(subscip, subvars[i], solval) );
165  }
166  }
167  }
168 
169  if( uselprows )
170  {
171  SCIP_ROW** rows; /* original scip rows */
172  int nrows;
173 
174  /* get the rows and their number */
175  SCIP_CALL( SCIPgetLPRowsData(scip, &rows, &nrows) );
176 
177  /* copy all rows to linear constraints */
178  for( i = 0; i < nrows; i++ )
179  {
180  SCIP_CONS* cons;
181  SCIP_VAR** consvars;
182  SCIP_COL** cols;
183  SCIP_Real constant;
184  SCIP_Real lhs;
185  SCIP_Real rhs;
186  SCIP_Real* vals;
187  int nnonz;
188 
189  /* ignore rows that are only locally valid */
190  if( SCIProwIsLocal(rows[i]) )
191  continue;
192 
193  /* get the row's data */
194  constant = SCIProwGetConstant(rows[i]);
195  lhs = SCIProwGetLhs(rows[i]) - constant;
196  rhs = SCIProwGetRhs(rows[i]) - constant;
197  vals = SCIProwGetVals(rows[i]);
198  nnonz = SCIProwGetNNonz(rows[i]);
199  cols = SCIProwGetCols(rows[i]);
200 
201  assert( lhs <= rhs );
202 
203  /* allocate memory array to be filled with the corresponding subproblem variables */
204  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nnonz) );
205  for( j = 0; j < nnonz; j++ )
206  consvars[j] = subvars[SCIPvarGetProbindex(SCIPcolGetVar(cols[j]))];
207 
208  /* create a new linear constraint and add it to the subproblem */
209  SCIP_CALL( SCIPcreateConsLinear(subscip, &cons, SCIProwGetName(rows[i]), nnonz, consvars, vals, lhs, rhs,
210  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE) );
211  SCIP_CALL( SCIPaddCons(subscip, cons) );
212  SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
213 
214  /* free temporary memory */
215  SCIPfreeBufferArray(scip, &consvars);
216  }
217  }
218 
219  SCIPfreeBufferArray(scip, &marked);
220  return SCIP_OKAY;
221 }
222 
223 /** creates a new solution for the original problem by copying the solution of the subproblem */
224 static
226  SCIP* scip, /**< original SCIP data structure */
227  SCIP* subscip, /**< SCIP structure of the subproblem */
228  SCIP_VAR** subvars, /**< the variables of the subproblem */
229  SCIP_HEUR* heur, /**< mutation heuristic structure */
230  SCIP_SOL* subsol, /**< solution of the subproblem */
231  SCIP_Bool* success /**< used to store whether new solution was found or not */
232 )
233 {
234  SCIP_VAR** vars; /* the original problem's variables */
235  int nvars;
236  SCIP_Real* subsolvals; /* solution values of the subproblem */
237  SCIP_SOL* newsol; /* solution to be created for the original problem */
238 
239  assert( scip != NULL );
240  assert( subscip != NULL );
241  assert( subvars != NULL );
242  assert( subsol != NULL );
243 
244  /* get variables' data */
245  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
246  /* sub-SCIP may have more variables than the number of active (transformed) variables in the main SCIP
247  * since constraint copying may have required the copy of variables that are fixed in the main SCIP
248  */
249  assert(nvars <= SCIPgetNOrigVars(subscip));
250 
251  SCIP_CALL( SCIPallocBufferArray(scip, &subsolvals, nvars) );
252 
253  /* copy the solution */
254  SCIP_CALL( SCIPgetSolVals(subscip, subsol, nvars, subvars, subsolvals) );
255 
256  /* create new solution for the original problem */
257  SCIP_CALL( SCIPcreateSol(scip, &newsol, heur) );
258  SCIP_CALL( SCIPsetSolVals(scip, newsol, nvars, vars, subsolvals) );
259 
260  /* try to add new solution to scip and free it immediately */
261  SCIP_CALL( SCIPtrySolFree(scip, &newsol, FALSE, TRUE, TRUE, TRUE, success) );
262 
263  SCIPfreeBufferArray(scip, &subsolvals);
264 
265  return SCIP_OKAY;
266 }
267 
268 
269 /*
270  * Callback methods of primal heuristic
271  */
272 
273 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
274 static
275 SCIP_DECL_HEURCOPY(heurCopyMutation)
276 { /*lint --e{715}*/
277  assert(scip != NULL);
278  assert(heur != NULL);
279  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
280 
281  /* call inclusion method of primal heuristic */
283 
284  return SCIP_OKAY;
285 }
286 
287 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
288 static
289 SCIP_DECL_HEURFREE(heurFreeMutation)
290 { /*lint --e{715}*/
291  SCIP_HEURDATA* heurdata;
292 
293  assert( heur != NULL );
294  assert( scip != NULL );
295 
296  /* get heuristic data */
297  heurdata = SCIPheurGetData(heur);
298  assert( heurdata != NULL );
299 
300  /* free heuristic data */
301  SCIPfreeMemory(scip, &heurdata);
302  SCIPheurSetData(heur, NULL);
303 
304  return SCIP_OKAY;
305 }
306 
307 /** initialization method of primal heuristic (called after problem was transformed) */
308 static
309 SCIP_DECL_HEURINIT(heurInitMutation)
310 { /*lint --e{715}*/
311  SCIP_HEURDATA* heurdata;
312 
313  assert( heur != NULL );
314  assert( scip != NULL );
315 
316  /* get heuristic's data */
317  heurdata = SCIPheurGetData(heur);
318  assert( heurdata != NULL );
319 
320  /* initialize data */
321  heurdata->usednodes = 0;
322  heurdata->randseed = 0;
323 
324  return SCIP_OKAY;
325 }
326 
327 
328 /** execution method of primal heuristic */
329 static
330 SCIP_DECL_HEUREXEC(heurExecMutation)
331 { /*lint --e{715}*/
332  SCIP_Longint maxnnodes;
333  SCIP_Longint nsubnodes; /* node limit for the subproblem */
334 
335  SCIP_HEURDATA* heurdata; /* heuristic's data */
336  SCIP* subscip; /* the subproblem created by mutation */
337  SCIP_VAR** vars; /* original problem's variables */
338  SCIP_VAR** subvars; /* subproblem's variables */
339  SCIP_HASHMAP* varmapfw; /* mapping of SCIP variables to sub-SCIP variables */
340 
341  SCIP_Real cutoff; /* objective cutoff for the subproblem */
342  SCIP_Real maxnnodesr;
343  SCIP_Real memorylimit;
344  SCIP_Real timelimit; /* timelimit for the subproblem */
345  SCIP_Real upperbound;
346 
347  int nvars; /* number of original problem's variables */
348  int i;
349 
350  SCIP_Bool success;
351 
352  SCIP_RETCODE retcode;
353 
354  assert( heur != NULL );
355  assert( scip != NULL );
356  assert( result != NULL );
357 
358  /* get heuristic's data */
359  heurdata = SCIPheurGetData(heur);
360  assert( heurdata != NULL );
361 
362  *result = SCIP_DELAYED;
363 
364  /* only call heuristic, if feasible solution is available */
365  if( SCIPgetNSols(scip) <= 0 )
366  return SCIP_OKAY;
367 
368  /* only call heuristic, if the best solution comes from transformed problem */
369  assert( SCIPgetBestSol(scip) != NULL );
370  if( SCIPsolIsOriginal(SCIPgetBestSol(scip)) )
371  return SCIP_OKAY;
372 
373  /* only call heuristic, if enough nodes were processed since last incumbent */
374  if( SCIPgetNNodes(scip) - SCIPgetSolNodenum(scip,SCIPgetBestSol(scip)) < heurdata->nwaitingnodes)
375  return SCIP_OKAY;
376 
377  *result = SCIP_DIDNOTRUN;
378 
379  /* only call heuristic, if discrete variables are present */
380  if( SCIPgetNBinVars(scip) == 0 && SCIPgetNIntVars(scip) == 0 )
381  return SCIP_OKAY;
382 
383  /* calculate the maximal number of branching nodes until heuristic is aborted */
384  maxnnodesr = heurdata->nodesquot * SCIPgetNNodes(scip);
385 
386  /* reward mutation if it succeeded often, count the setup costs for the sub-MIP as 100 nodes */
387  maxnnodesr *= 1.0 + 2.0 * (SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur) + 1.0);
388  maxnnodes = (SCIP_Longint) maxnnodesr - 100 * SCIPheurGetNCalls(heur);
389  maxnnodes += heurdata->nodesofs;
390 
391  /* determine the node limit for the current process */
392  nsubnodes = maxnnodes - heurdata->usednodes;
393  nsubnodes = MIN(nsubnodes, heurdata->maxnodes);
394 
395  /* check whether we have enough nodes left to call subproblem solving */
396  if( nsubnodes < heurdata->minnodes )
397  return SCIP_OKAY;
398 
399  if( SCIPisStopped(scip) )
400  return SCIP_OKAY;
401 
402  *result = SCIP_DIDNOTFIND;
403 
404  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
405 
406  /* initializing the subproblem */
407  SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) );
408  SCIP_CALL( SCIPcreate(&subscip) );
409 
410  /* create the variable mapping hash map */
411  SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), SCIPcalcHashtableSize(5 * nvars)) );
412 
413  if( heurdata->uselprows )
414  {
415  char probname[SCIP_MAXSTRLEN];
416 
417  /* copy all plugins */
419 
420  /* get name of the original problem and add the string "_mutationsub" */
421  (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s_mutationsub", SCIPgetProbName(scip));
422 
423  /* create the subproblem */
424  SCIP_CALL( SCIPcreateProb(subscip, probname, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
425 
426  /* copy all variables */
427  SCIP_CALL( SCIPcopyVars(scip, subscip, varmapfw, NULL, TRUE) );
428  }
429  else
430  {
431  SCIP_Bool valid;
432  valid = FALSE;
433 
434  SCIP_CALL( SCIPcopy(scip, subscip, varmapfw, NULL, "rens", TRUE, FALSE, TRUE, &valid) );
435 
436  if( heurdata->copycuts )
437  {
438  /* copies all active cuts from cutpool of sourcescip to linear constraints in targetscip */
439  SCIP_CALL( SCIPcopyCuts(scip, subscip, varmapfw, NULL, TRUE, NULL) );
440  }
441 
442  SCIPdebugMessage("Copying the SCIP instance was %s complete.\n", valid ? "" : "not ");
443  }
444 
445  for( i = 0; i < nvars; i++ )
446  subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]);
447 
448  /* free hash map */
449  SCIPhashmapFree(&varmapfw);
450 
451  /* create a new problem, which fixes variables with same value in bestsol and LP relaxation */
452  SCIP_CALL( createSubproblem(scip, subscip, subvars, heurdata->minfixingrate, &heurdata->randseed, heurdata->uselprows) );
453 
454  /* do not abort subproblem on CTRL-C */
455  SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
456 
457  /* disable output to console */
458  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
459 
460  /* check whether there is enough time and memory left */
461  SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) );
462  if( !SCIPisInfinity(scip, timelimit) )
463  timelimit -= SCIPgetSolvingTime(scip);
464  SCIP_CALL( SCIPgetRealParam(scip, "limits/memory", &memorylimit) );
465 
466  /* substract the memory already used by the main SCIP and the estimated memory usage of external software */
467  if( !SCIPisInfinity(scip, memorylimit) )
468  {
469  memorylimit -= SCIPgetMemUsed(scip)/1048576.0;
470  memorylimit -= SCIPgetMemExternEstim(scip)/1048576.0;
471  }
472 
473  /* abort if no time is left or not enough memory to create a copy of SCIP, including external memory usage */
474  if( timelimit <= 0.0 || memorylimit <= 2.0*SCIPgetMemExternEstim(scip)/1048576.0 )
475  goto TERMINATE;
476 
477  /* set limits for the subproblem */
478  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", nsubnodes) );
479  SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) );
480  SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) );
481 
482  /* forbid recursive call of heuristics and separators solving subMIPs */
483  SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
484 
485  /* disable cutting plane separation */
487 
488  /* disable expensive presolving */
490 
491  /* use best estimate node selection */
492  if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") )
493  {
494  SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) );
495  }
496 
497  /* use inference branching */
498  if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") )
499  {
500  SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) );
501  }
502 
503  /* disable conflict analysis */
504  if( !SCIPisParamFixed(subscip, "conflict/useprop") )
505  {
506  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useprop", FALSE) );
507  }
508  if( !SCIPisParamFixed(subscip, "conflict/useinflp") )
509  {
510  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useinflp", FALSE) );
511  }
512  if( !SCIPisParamFixed(subscip, "conflict/useboundlp") )
513  {
514  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useboundlp", FALSE) );
515  }
516  if( !SCIPisParamFixed(subscip, "conflict/usesb") )
517  {
518  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usesb", FALSE) );
519  }
520  if( !SCIPisParamFixed(subscip, "conflict/usepseudo") )
521  {
522  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usepseudo", FALSE) );
523  }
524 
525  /* employ a limit on the number of enforcement rounds in the quadratic constraint handlers; this fixes the issue that
526  * sometimes the quadratic constraint handler needs hundreds or thousands of enforcement rounds to determine the
527  * feasibility status of a single node without fractional branching candidates by separation (namely for uflquad
528  * instances); however, the solution status of the sub-SCIP might get corrupted by this; hence no decutions shall be
529  * made for the original SCIP
530  */
531  if( SCIPfindConshdlr(subscip, "quadratic") != NULL && !SCIPisParamFixed(subscip, "constraints/quadratic/enfolplimit") )
532  {
533  SCIP_CALL( SCIPsetIntParam(subscip, "constraints/quadratic/enfolplimit", 10) );
534  }
535 
536  /* add an objective cutoff */
537  cutoff = SCIPinfinity(scip);
538  assert( !SCIPisInfinity(scip, SCIPgetUpperbound(scip)) );
539 
540  upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip);
541  if( !SCIPisInfinity(scip, -1.0 * SCIPgetLowerbound(scip)) )
542  {
543  cutoff = (1-heurdata->minimprove) * SCIPgetUpperbound(scip) + heurdata->minimprove * SCIPgetLowerbound(scip);
544  }
545  else
546  {
547  if( SCIPgetUpperbound ( scip ) >= 0 )
548  cutoff = ( 1 - heurdata->minimprove ) * SCIPgetUpperbound ( scip );
549  else
550  cutoff = ( 1 + heurdata->minimprove ) * SCIPgetUpperbound ( scip );
551  }
552  cutoff = MIN(upperbound, cutoff );
553  SCIP_CALL( SCIPsetObjlimit(subscip, cutoff) );
554 
555  /* solve the subproblem */
556  SCIPdebugMessage("Solve Mutation subMIP\n");
557  retcode = SCIPsolve(subscip);
558 
559  /* Errors in solving the subproblem should not kill the overall solving process
560  * Hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
561  */
562  if( retcode != SCIP_OKAY )
563  {
564 #ifndef NDEBUG
565  SCIP_CALL( retcode );
566 #endif
567  SCIPwarningMessage(scip, "Error while solving subproblem in Mutation heuristic; sub-SCIP terminated with code <%d>\n",retcode);
568  }
569 
570  heurdata->usednodes += SCIPgetNNodes(subscip);
571 
572  /* check, whether a solution was found */
573  if( SCIPgetNSols(subscip) > 0 )
574  {
575  SCIP_SOL** subsols;
576  int nsubsols;
577 
578  /* check, whether a solution was found;
579  * due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted
580  */
581  nsubsols = SCIPgetNSols(subscip);
582  subsols = SCIPgetSols(subscip);
583  success = FALSE;
584  for( i = 0; i < nsubsols && !success; ++i )
585  {
586  SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) );
587  }
588  if( success )
589  *result = SCIP_FOUNDSOL;
590  }
591 
592  TERMINATE:
593  /* free subproblem */
594  SCIPfreeBufferArray(scip, &subvars);
595  SCIP_CALL( SCIPfree(&subscip) );
596 
597  return SCIP_OKAY;
598 }
599 
600 /*
601  * primal heuristic specific interface methods
602  */
604 /** creates the mutation primal heuristic and includes it in SCIP */
606  SCIP* scip /**< SCIP data structure */
607  )
608 {
609  SCIP_HEURDATA* heurdata;
610  SCIP_HEUR* heur;
611 
612  /* create Mutation primal heuristic data */
613  SCIP_CALL( SCIPallocMemory(scip, &heurdata) );
614 
615  /* include primal heuristic */
616  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
618  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecMutation, heurdata) );
619 
620  assert(heur != NULL);
621 
622  /* set non-NULL pointers to callback methods */
623  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyMutation) );
624  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeMutation) );
625  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitMutation) );
626 
627  /* add mutation primal heuristic parameters */
628  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/nodesofs",
629  "number of nodes added to the contingent of the total nodes",
630  &heurdata->nodesofs, FALSE, DEFAULT_NODESOFS, 0, INT_MAX, NULL, NULL) );
631 
632  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/maxnodes",
633  "maximum number of nodes to regard in the subproblem",
634  &heurdata->maxnodes, TRUE, DEFAULT_MAXNODES, 0, INT_MAX, NULL, NULL) );
635 
636  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/minnodes",
637  "minimum number of nodes required to start the subproblem",
638  &heurdata->minnodes, TRUE, DEFAULT_MINNODES, 0, INT_MAX, NULL, NULL) );
639 
640  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/nwaitingnodes",
641  "number of nodes without incumbent change that heuristic should wait",
642  &heurdata->nwaitingnodes, TRUE, DEFAULT_NWAITINGNODES, 0, INT_MAX, NULL, NULL) );
643 
644  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/nodesquot",
645  "contingent of sub problem nodes in relation to the number of nodes of the original problem",
646  &heurdata->nodesquot, FALSE, DEFAULT_NODESQUOT, 0.0, 1.0, NULL, NULL) );
647 
648  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/minfixingrate",
649  "percentage of integer variables that have to be fixed",
650  &heurdata->minfixingrate, FALSE, DEFAULT_MINFIXINGRATE, SCIPsumepsilon(scip), 1.0-SCIPsumepsilon(scip), NULL, NULL) );
651 
652  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/minimprove",
653  "factor by which "HEUR_NAME" should at least improve the incumbent",
654  &heurdata->minimprove, TRUE, DEFAULT_MINIMPROVE, 0.0, 1.0, NULL, NULL) );
655 
656  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/"HEUR_NAME"/uselprows",
657  "should subproblem be created out of the rows in the LP rows?",
658  &heurdata->uselprows, TRUE, DEFAULT_USELPROWS, NULL, NULL) );
659 
660  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/"HEUR_NAME"/copycuts",
661  "if uselprows == FALSE, should all active cuts from cutpool be copied to constraints in subproblem?",
662  &heurdata->copycuts, TRUE, DEFAULT_COPYCUTS, NULL, NULL) );
663 
664  return SCIP_OKAY;
665 }
666