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  /* disable statistic timing inside sub SCIP */
478  SCIP_CALL( SCIPsetBoolParam(subscip, "timing/statistictiming", FALSE) );
479 
480  /* set limits for the subproblem */
481  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", nsubnodes) );
482  SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) );
483  SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) );
484 
485  /* forbid recursive call of heuristics and separators solving subMIPs */
486  SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
487 
488  /* disable cutting plane separation */
490 
491  /* disable expensive presolving */
493 
494  /* use best estimate node selection */
495  if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") )
496  {
497  SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) );
498  }
499 
500  /* use inference branching */
501  if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") )
502  {
503  SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) );
504  }
505 
506  /* disable conflict analysis */
507  if( !SCIPisParamFixed(subscip, "conflict/useprop") )
508  {
509  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useprop", FALSE) );
510  }
511  if( !SCIPisParamFixed(subscip, "conflict/useinflp") )
512  {
513  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useinflp", FALSE) );
514  }
515  if( !SCIPisParamFixed(subscip, "conflict/useboundlp") )
516  {
517  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useboundlp", FALSE) );
518  }
519  if( !SCIPisParamFixed(subscip, "conflict/usesb") )
520  {
521  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usesb", FALSE) );
522  }
523  if( !SCIPisParamFixed(subscip, "conflict/usepseudo") )
524  {
525  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usepseudo", FALSE) );
526  }
527 
528  /* employ a limit on the number of enforcement rounds in the quadratic constraint handlers; this fixes the issue that
529  * sometimes the quadratic constraint handler needs hundreds or thousands of enforcement rounds to determine the
530  * feasibility status of a single node without fractional branching candidates by separation (namely for uflquad
531  * instances); however, the solution status of the sub-SCIP might get corrupted by this; hence no decutions shall be
532  * made for the original SCIP
533  */
534  if( SCIPfindConshdlr(subscip, "quadratic") != NULL && !SCIPisParamFixed(subscip, "constraints/quadratic/enfolplimit") )
535  {
536  SCIP_CALL( SCIPsetIntParam(subscip, "constraints/quadratic/enfolplimit", 10) );
537  }
538 
539  /* add an objective cutoff */
540  cutoff = SCIPinfinity(scip);
541  assert( !SCIPisInfinity(scip, SCIPgetUpperbound(scip)) );
542 
543  upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip);
544  if( !SCIPisInfinity(scip, -1.0 * SCIPgetLowerbound(scip)) )
545  {
546  cutoff = (1-heurdata->minimprove) * SCIPgetUpperbound(scip) + heurdata->minimprove * SCIPgetLowerbound(scip);
547  }
548  else
549  {
550  if( SCIPgetUpperbound ( scip ) >= 0 )
551  cutoff = ( 1 - heurdata->minimprove ) * SCIPgetUpperbound ( scip );
552  else
553  cutoff = ( 1 + heurdata->minimprove ) * SCIPgetUpperbound ( scip );
554  }
555  cutoff = MIN(upperbound, cutoff );
556  SCIP_CALL( SCIPsetObjlimit(subscip, cutoff) );
557 
558  /* solve the subproblem */
559  SCIPdebugMessage("Solve Mutation subMIP\n");
560  retcode = SCIPsolve(subscip);
561 
562  /* Errors in solving the subproblem should not kill the overall solving process
563  * Hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
564  */
565  if( retcode != SCIP_OKAY )
566  {
567 #ifndef NDEBUG
568  SCIP_CALL( retcode );
569 #endif
570  SCIPwarningMessage(scip, "Error while solving subproblem in Mutation heuristic; sub-SCIP terminated with code <%d>\n",retcode);
571  }
572 
573  heurdata->usednodes += SCIPgetNNodes(subscip);
574 
575  /* check, whether a solution was found */
576  if( SCIPgetNSols(subscip) > 0 )
577  {
578  SCIP_SOL** subsols;
579  int nsubsols;
580 
581  /* check, whether a solution was found;
582  * due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted
583  */
584  nsubsols = SCIPgetNSols(subscip);
585  subsols = SCIPgetSols(subscip);
586  success = FALSE;
587  for( i = 0; i < nsubsols && !success; ++i )
588  {
589  SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) );
590  }
591  if( success )
592  *result = SCIP_FOUNDSOL;
593  }
594 
595  TERMINATE:
596  /* free subproblem */
597  SCIPfreeBufferArray(scip, &subvars);
598  SCIP_CALL( SCIPfree(&subscip) );
599 
600  return SCIP_OKAY;
601 }
602 
603 /*
604  * primal heuristic specific interface methods
605  */
606 
607 /** creates the mutation primal heuristic and includes it in SCIP */
609  SCIP* scip /**< SCIP data structure */
610  )
611 {
612  SCIP_HEURDATA* heurdata;
613  SCIP_HEUR* heur;
614 
615  /* create Mutation primal heuristic data */
616  SCIP_CALL( SCIPallocMemory(scip, &heurdata) );
617 
618  /* include primal heuristic */
619  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
621  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecMutation, heurdata) );
622 
623  assert(heur != NULL);
624 
625  /* set non-NULL pointers to callback methods */
626  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyMutation) );
627  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeMutation) );
628  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitMutation) );
629 
630  /* add mutation primal heuristic parameters */
631  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/nodesofs",
632  "number of nodes added to the contingent of the total nodes",
633  &heurdata->nodesofs, FALSE, DEFAULT_NODESOFS, 0, INT_MAX, NULL, NULL) );
634 
635  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/maxnodes",
636  "maximum number of nodes to regard in the subproblem",
637  &heurdata->maxnodes, TRUE, DEFAULT_MAXNODES, 0, INT_MAX, NULL, NULL) );
638 
639  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/minnodes",
640  "minimum number of nodes required to start the subproblem",
641  &heurdata->minnodes, TRUE, DEFAULT_MINNODES, 0, INT_MAX, NULL, NULL) );
642 
643  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/nwaitingnodes",
644  "number of nodes without incumbent change that heuristic should wait",
645  &heurdata->nwaitingnodes, TRUE, DEFAULT_NWAITINGNODES, 0, INT_MAX, NULL, NULL) );
646 
647  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/nodesquot",
648  "contingent of sub problem nodes in relation to the number of nodes of the original problem",
649  &heurdata->nodesquot, FALSE, DEFAULT_NODESQUOT, 0.0, 1.0, NULL, NULL) );
650 
651  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/minfixingrate",
652  "percentage of integer variables that have to be fixed",
653  &heurdata->minfixingrate, FALSE, DEFAULT_MINFIXINGRATE, SCIPsumepsilon(scip), 1.0-SCIPsumepsilon(scip), NULL, NULL) );
654 
655  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/minimprove",
656  "factor by which "HEUR_NAME" should at least improve the incumbent",
657  &heurdata->minimprove, TRUE, DEFAULT_MINIMPROVE, 0.0, 1.0, NULL, NULL) );
658 
659  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/"HEUR_NAME"/uselprows",
660  "should subproblem be created out of the rows in the LP rows?",
661  &heurdata->uselprows, TRUE, DEFAULT_USELPROWS, NULL, NULL) );
662 
663  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/"HEUR_NAME"/copycuts",
664  "if uselprows == FALSE, should all active cuts from cutpool be copied to constraints in subproblem?",
665  &heurdata->copycuts, TRUE, DEFAULT_COPYCUTS, NULL, NULL) );
666 
667  return SCIP_OKAY;
668 }
#define DEFAULT_COPYCUTS
Definition: heur_mutation.c:50
static SCIP_DECL_HEURCOPY(heurCopyMutation)
SCIP_CONSHDLR * SCIPfindConshdlr(SCIP *scip, const char *name)
Definition: scip.c:5600
static SCIP_DECL_HEURFREE(heurFreeMutation)
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
Definition: scip.c:38360
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:591
SCIP_Longint SCIPheurGetNCalls(SCIP_HEUR *heur)
Definition: heur.c:717
#define DEFAULT_NWAITINGNODES
Definition: heur_mutation.c:47
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:7014
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip.c:1206
#define SCIP_MAXSTRLEN
Definition: def.h:196
#define NULL
Definition: lpi_spx.cpp:129
SCIP_Bool SCIPisStopped(SCIP *scip)
Definition: scip.c:1111
SCIP_Real SCIProwGetLhs(SCIP_ROW *row)
Definition: lp.c:18637
SCIP_COL ** SCIProwGetCols(SCIP_ROW *row)
Definition: lp.c:18583
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition: var.c:16380
#define DEFAULT_MINNODES
Definition: heur_mutation.c:44
SCIP_RETCODE SCIPincludeHeurBasic(SCIP *scip, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, unsigned int timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
Definition: scip.c:6969
static SCIP_RETCODE createNewSol(SCIP *scip, SCIP *subscip, SCIP_VAR **subvars, SCIP_HEUR *heur, SCIP_SOL *subsol, SCIP_Bool *success)
#define FALSE
Definition: def.h:52
#define HEUR_DISPCHAR
Definition: heur_mutation.c:33
SCIP_Real SCIPgetSolvingTime(SCIP *scip)
Definition: scip.c:37596
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition: misc.c:1864
SCIP_RETCODE SCIPsetLongintParam(SCIP *scip, const char *name, SCIP_Longint value)
Definition: scip.c:3869
int SCIPgetNBinVars(SCIP *scip)
Definition: scip.c:10116
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:7579
#define TRUE
Definition: def.h:51
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
#define DEFAULT_MINFIXINGRATE
Definition: heur_mutation.c:45
SCIP_RETCODE SCIPtrySolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool printreason, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
Definition: scip.c:33058
SCIP_RETCODE SCIPsetRealParam(SCIP *scip, const char *name, SCIP_Real value)
Definition: scip.c:3914
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:44
#define SCIPdebugMessage
Definition: pub_message.h:77
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:31775
#define HEUR_NAME
Definition: heur_mutation.c:31
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip.h:19214
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:1923
SCIP_Real SCIProwGetConstant(SCIP_ROW *row)
Definition: lp.c:18603
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip.c:22651
SCIP_Real SCIPgetLowerbound(SCIP *scip)
Definition: scip.c:35061
SCIP_RETCODE SCIPcopyCuts(SCIP *sourcescip, SCIP *targetscip, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, SCIP_Bool global, int *ncutsadded)
Definition: scip.c:2726
int SCIPgetNSols(SCIP *scip)
Definition: scip.c:32432
SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:3388
SCIP_RETCODE SCIPaddIntParam(SCIP *scip, const char *name, const char *desc, int *valueptr, SCIP_Bool isadvanced, int defaultvalue, int minvalue, int maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:3414
int SCIPgetNOrigVars(SCIP *scip)
Definition: scip.c:10511
static SCIP_RETCODE createSubproblem(SCIP *scip, SCIP *subscip, SCIP_VAR **subvars, SCIP_Real minfixingrate, unsigned int *randseed, SCIP_Bool uselprows)
Definition: heur_mutation.c:83
static SCIP_DECL_HEUREXEC(heurExecMutation)
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38273
SCIP_RETCODE SCIPcopy(SCIP *sourcescip, SCIP *targetscip, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, const char *suffix, SCIP_Bool global, SCIP_Bool enablepricing, SCIP_Bool passmessagehdlr, SCIP_Bool *valid)
Definition: scip.c:3095
#define SCIPallocMemory(scip, ptr)
Definition: scip.h:19159
SCIP_RETCODE SCIPincludeHeurMutation(SCIP *scip)
SCIP_RETCODE SCIPchgVarUbGlobal(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound)
Definition: scip.c:18227
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip.h:19221
SCIP_Real SCIProwGetRhs(SCIP_ROW *row)
Definition: lp.c:18647
int SCIPcalcHashtableSize(int minsize)
Definition: misc.c:964
SCIP_Bool SCIPisLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38292
#define DEFAULT_NODESQUOT
Definition: heur_mutation.c:46
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:30856
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip.c:37940
SCIP_Real SCIPfloor(SCIP *scip, SCIP_Real val)
Definition: scip.c:38470
SCIP_Bool SCIProwIsLocal(SCIP_ROW *row)
Definition: lp.c:18746
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition: misc.c:1882
SCIP_RETCODE SCIPsetSeparating(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:4199
LNS heuristic that tries to randomly mutate the incumbent solution.
SCIP_RETCODE SCIPcreateProb(SCIP *scip, const char *name, SCIP_DECL_PROBDELORIG((*probdelorig)), SCIP_DECL_PROBTRANS((*probtrans)), SCIP_DECL_PROBDELTRANS((*probdeltrans)), SCIP_DECL_PROBINITSOL((*probinitsol)), SCIP_DECL_PROBEXITSOL((*probexitsol)), SCIP_DECL_PROBCOPY((*probcopy)), SCIP_PROBDATA *probdata)
Definition: scip.c:8453
SCIP_RETCODE SCIPgetLPRowsData(SCIP *scip, SCIP_ROW ***rows, int *nrows)
Definition: scip.c:24447
SCIP_RETCODE SCIPgetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:31809
SCIP_Real SCIPinfinity(SCIP *scip)
Definition: scip.c:38349
#define DEFAULT_MAXNODES
Definition: heur_mutation.c:42
#define SCIP_CALL(x)
Definition: def.h:258
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip.c:755
SCIP_SOL ** SCIPgetSols(SCIP *scip)
Definition: scip.c:32481
const char * SCIProwGetName(SCIP_ROW *row)
Definition: lp.c:18696
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:512
#define DEFAULT_MINIMPROVE
Definition: heur_mutation.c:43
#define HEUR_MAXDEPTH
Definition: heur_mutation.c:37
SCIP_Real SCIPgetUpperbound(SCIP *scip)
Definition: scip.c:35202
SCIP_Bool SCIPisGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38311
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:49
SCIP_RETCODE SCIPincludeDefaultPlugins(SCIP *scip)
#define HEUR_USESSUBSCIP
Definition: heur_mutation.c:39
SCIP_Longint SCIPgetSolNodenum(SCIP *scip, SCIP_SOL *sol)
Definition: scip.c:32043
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip.c:3824
#define HEUR_PRIORITY
Definition: heur_mutation.c:34
SCIP_Bool SCIPisParamFixed(SCIP *scip, const char *name)
Definition: scip.c:3550
SCIP_RETCODE SCIPsetSubscipsOff(SCIP *scip, SCIP_Bool quiet)
Definition: scip.c:4124
SCIP_RETCODE SCIPcopyVars(SCIP *sourcescip, SCIP *targetscip, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, SCIP_Bool global)
Definition: scip.c:2087
SCIP_Longint SCIPgetMemUsed(SCIP *scip)
Definition: scip.c:37975
SCIP_RETCODE SCIPsetPresolving(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:4173
Constraint handler for linear constraints in their most general form, .
#define DEFAULT_USELPROWS
Definition: heur_mutation.c:48
SCIP_Bool SCIPsolIsOriginal(SCIP_SOL *sol)
Definition: sol.c:2119
int SCIPgetRandomInt(int minrandval, int maxrandval, unsigned int *seedp)
Definition: misc.c:7212
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
Definition: scip.c:7046
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip.c:10850
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip.c:13596
#define HEUR_FREQOFS
Definition: heur_mutation.c:36
SCIP_Real SCIPceil(SCIP *scip, SCIP_Real val)
Definition: scip.c:38482
SCIP_Longint SCIPheurGetNBestSolsFound(SCIP_HEUR *heur)
Definition: heur.c:737
#define SCIPfreeMemory(scip, ptr)
Definition: scip.h:19176
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:16370
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip.c:682
int SCIPgetNIntVars(SCIP *scip)
Definition: scip.c:10161
SCIP_RETCODE SCIPcreateConsLinear(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *vals, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
SCIP_Real * SCIProwGetVals(SCIP_ROW *row)
Definition: lp.c:18593
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:7030
int SCIPvarGetProbindex(SCIP_VAR *var)
Definition: var.c:16072
SCIP_BRANCHRULE * SCIPfindBranchrule(SCIP *scip, const char *name)
Definition: scip.c:7867
#define HEUR_DESC
Definition: heur_mutation.c:32
#define HEUR_TIMING
Definition: heur_mutation.c:38
#define SCIP_Real
Definition: def.h:123
#define MIN(x, y)
Definition: memory.c:59
SCIP_RETCODE SCIPchgVarLbGlobal(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound)
Definition: scip.c:18150
#define HEUR_FREQ
Definition: heur_mutation.c:35
SCIP_RETCODE SCIPsetObjlimit(SCIP *scip, SCIP_Real objlimit)
Definition: scip.c:9567
const char * SCIPgetProbName(SCIP *scip)
Definition: scip.c:9314
static SCIP_DECL_HEURINIT(heurInitMutation)
#define SCIP_Longint
Definition: def.h:107
SCIP_RETCODE SCIPsetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:31679
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip.c:3779
int SCIProwGetNNonz(SCIP_ROW *row)
Definition: lp.c:18558
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:502
SCIP_RETCODE SCIPgetVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition: scip.c:9945
SCIP_RETCODE SCIPgetRealParam(SCIP *scip, const char *name, SCIP_Real *value)
Definition: scip.c:3638
#define BMSclearMemoryArray(ptr, num)
Definition: memory.h:98
SCIP_NODESEL * SCIPfindNodesel(SCIP *scip, const char *name)
Definition: scip.c:7555
SCIP_VAR * SCIPcolGetVar(SCIP_COL *col)
Definition: lp.c:18407
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip.c:32531
SCIP_RETCODE SCIPaddRealParam(SCIP *scip, const char *name, const char *desc, SCIP_Real *valueptr, SCIP_Bool isadvanced, SCIP_Real defaultvalue, SCIP_Real minvalue, SCIP_Real maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:3470
SCIP_Real SCIPsumepsilon(SCIP *scip)
Definition: scip.c:37719
default SCIP plugins
SCIP_Longint SCIPgetNNodes(SCIP *scip)
Definition: scip.c:34065
SCIP callable library.
#define DEFAULT_NODESOFS
Definition: heur_mutation.c:41
SCIP_Longint SCIPgetMemExternEstim(SCIP *scip)
Definition: scip.c:37988