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-2016 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file 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  SCIP_Bool* success /**< used to store whether the creation of the subproblem worked */
91  )
92 {
93  SCIP_VAR** vars; /* original scip variables */
94  SCIP_SOL* sol; /* pool of solutions */
95  SCIP_Bool* marked; /* array of markers, which variables to fixed */
96  SCIP_Bool fixingmarker; /* which flag should label a fixed variable? */
97 
98  int nvars;
99  int nbinvars;
100  int nintvars;
101  int i;
102  int j;
103  int nmarkers;
104  int maxiters;
105 
106  *success = TRUE;
107 
108  /* get required data of the original problem */
109  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, &nbinvars, &nintvars, NULL, NULL) );
110  sol = SCIPgetBestSol(scip);
111  assert(sol != NULL);
112 
113 
114  SCIP_CALL( SCIPallocBufferArray(scip, &marked, nbinvars+nintvars) );
115 
116  if( minfixingrate > 0.5 )
117  {
118  nmarkers = nbinvars + nintvars - (int) SCIPfloor(scip, minfixingrate*(nbinvars+nintvars));
119  fixingmarker = FALSE;
120  }
121  else
122  {
123  nmarkers = (int) SCIPceil(scip, minfixingrate*(nbinvars+nintvars));
124  fixingmarker = TRUE;
125  }
126  assert( 0 <= nmarkers && nmarkers <= SCIPceil(scip,(nbinvars+nintvars)/2.0 ) );
127 
128  j = 0;
129  BMSclearMemoryArray(marked, nbinvars+nintvars);
130 
131  /* leave the loop after at most that many iterations
132  * @todo change this method to a single random permutation, which is guaranteed to succeed, and maybe even faster */
133  maxiters = 3 * (nbinvars + nintvars);
134 
135  while( j < nmarkers && maxiters > 0 )
136  {
137  do
138  {
139  i = SCIPgetRandomInt(0, nbinvars+nintvars-1, randseed);
140  --maxiters;
141  }
142  while( marked[i] && maxiters > 0 );
143 
144  j = marked[i] ? j : j+1;
145  marked[i] = TRUE;
146  }
147 
148  /* abort if it was not possible to fix enough variables */
149  if( j < nmarkers )
150  {
151  *success = FALSE;
152  assert(maxiters == 0);
153  goto TERMINATE;
154  }
155 
156  assert( j == nmarkers );
157 
158  /* change bounds of variables of the subproblem */
159  for( i = 0; i < nbinvars + nintvars; i++ )
160  {
161  /* fix all randomly marked variables */
162  if( marked[i] == fixingmarker )
163  {
164  SCIP_Real solval;
165  SCIP_Real lb;
166  SCIP_Real ub;
167 
168  solval = SCIPgetSolVal(scip, sol, vars[i]);
169  lb = SCIPvarGetLbGlobal(subvars[i]);
170  ub = SCIPvarGetUbGlobal(subvars[i]);
171  assert(SCIPisLE(scip, lb, ub));
172 
173  /* due to dual reductions, it may happen that the solution value is not in
174  the variable's domain anymore */
175  if( SCIPisLT(scip, solval, lb) )
176  solval = lb;
177  else if( SCIPisGT(scip, solval, ub) )
178  solval = ub;
179 
180  /* perform the bound change */
181  if( !SCIPisInfinity(scip, solval) && !SCIPisInfinity(scip, -solval) )
182  {
183  SCIP_CALL( SCIPchgVarLbGlobal(subscip, subvars[i], solval) );
184  SCIP_CALL( SCIPchgVarUbGlobal(subscip, subvars[i], solval) );
185  }
186  }
187  }
188 
189  if( uselprows )
190  {
191  SCIP_ROW** rows; /* original scip rows */
192  int nrows;
193 
194  /* get the rows and their number */
195  SCIP_CALL( SCIPgetLPRowsData(scip, &rows, &nrows) );
196 
197  /* copy all rows to linear constraints */
198  for( i = 0; i < nrows; i++ )
199  {
200  SCIP_CONS* cons;
201  SCIP_VAR** consvars;
202  SCIP_COL** cols;
203  SCIP_Real constant;
204  SCIP_Real lhs;
205  SCIP_Real rhs;
206  SCIP_Real* vals;
207  int nnonz;
208 
209  /* ignore rows that are only locally valid */
210  if( SCIProwIsLocal(rows[i]) )
211  continue;
212 
213  /* get the row's data */
214  constant = SCIProwGetConstant(rows[i]);
215  lhs = SCIProwGetLhs(rows[i]) - constant;
216  rhs = SCIProwGetRhs(rows[i]) - constant;
217  vals = SCIProwGetVals(rows[i]);
218  nnonz = SCIProwGetNNonz(rows[i]);
219  cols = SCIProwGetCols(rows[i]);
220 
221  assert( lhs <= rhs );
222 
223  /* allocate memory array to be filled with the corresponding subproblem variables */
224  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nnonz) );
225  for( j = 0; j < nnonz; j++ )
226  consvars[j] = subvars[SCIPvarGetProbindex(SCIPcolGetVar(cols[j]))];
227 
228  /* create a new linear constraint and add it to the subproblem */
229  SCIP_CALL( SCIPcreateConsLinear(subscip, &cons, SCIProwGetName(rows[i]), nnonz, consvars, vals, lhs, rhs,
230  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE) );
231  SCIP_CALL( SCIPaddCons(subscip, cons) );
232  SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
233 
234  /* free temporary memory */
235  SCIPfreeBufferArray(scip, &consvars);
236  }
237  }
238 
239  TERMINATE:
240  SCIPfreeBufferArray(scip, &marked);
241  return SCIP_OKAY;
242 }
243 
244 /** creates a new solution for the original problem by copying the solution of the subproblem */
245 static
247  SCIP* scip, /**< original SCIP data structure */
248  SCIP* subscip, /**< SCIP structure of the subproblem */
249  SCIP_VAR** subvars, /**< the variables of the subproblem */
250  SCIP_HEUR* heur, /**< mutation heuristic structure */
251  SCIP_SOL* subsol, /**< solution of the subproblem */
252  SCIP_Bool* success /**< used to store whether new solution was found or not */
253  )
254 {
255  SCIP_VAR** vars; /* the original problem's variables */
256  int nvars;
257  SCIP_Real* subsolvals; /* solution values of the subproblem */
258  SCIP_SOL* newsol; /* solution to be created for the original problem */
259 
260  assert( scip != NULL );
261  assert( subscip != NULL );
262  assert( subvars != NULL );
263  assert( subsol != NULL );
264 
265  /* get variables' data */
266  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
267  /* sub-SCIP may have more variables than the number of active (transformed) variables in the main SCIP
268  * since constraint copying may have required the copy of variables that are fixed in the main SCIP
269  */
270  assert(nvars <= SCIPgetNOrigVars(subscip));
271 
272  SCIP_CALL( SCIPallocBufferArray(scip, &subsolvals, nvars) );
273 
274  /* copy the solution */
275  SCIP_CALL( SCIPgetSolVals(subscip, subsol, nvars, subvars, subsolvals) );
276 
277  /* create new solution for the original problem */
278  SCIP_CALL( SCIPcreateSol(scip, &newsol, heur) );
279  SCIP_CALL( SCIPsetSolVals(scip, newsol, nvars, vars, subsolvals) );
280 
281  /* try to add new solution to scip and free it immediately */
282  SCIP_CALL( SCIPtrySolFree(scip, &newsol, FALSE, TRUE, TRUE, TRUE, success) );
283 
284  SCIPfreeBufferArray(scip, &subsolvals);
285 
286  return SCIP_OKAY;
287 }
288 
289 
290 /*
291  * Callback methods of primal heuristic
292  */
293 
294 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
295 static
296 SCIP_DECL_HEURCOPY(heurCopyMutation)
297 { /*lint --e{715}*/
298  assert(scip != NULL);
299  assert(heur != NULL);
300  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
301 
302  /* call inclusion method of primal heuristic */
304 
305  return SCIP_OKAY;
306 }
307 
308 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
309 static
310 SCIP_DECL_HEURFREE(heurFreeMutation)
311 { /*lint --e{715}*/
312  SCIP_HEURDATA* heurdata;
313 
314  assert( heur != NULL );
315  assert( scip != NULL );
316 
317  /* get heuristic data */
318  heurdata = SCIPheurGetData(heur);
319  assert( heurdata != NULL );
320 
321  /* free heuristic data */
322  SCIPfreeMemory(scip, &heurdata);
323  SCIPheurSetData(heur, NULL);
324 
325  return SCIP_OKAY;
326 }
327 
328 /** initialization method of primal heuristic (called after problem was transformed) */
329 static
330 SCIP_DECL_HEURINIT(heurInitMutation)
331 { /*lint --e{715}*/
332  SCIP_HEURDATA* heurdata;
333 
334  assert( heur != NULL );
335  assert( scip != NULL );
336 
337  /* get heuristic's data */
338  heurdata = SCIPheurGetData(heur);
339  assert( heurdata != NULL );
340 
341  /* initialize data */
342  heurdata->usednodes = 0;
343  heurdata->randseed = 0;
344 
345  return SCIP_OKAY;
346 }
347 
348 
349 /** execution method of primal heuristic */
350 static
351 SCIP_DECL_HEUREXEC(heurExecMutation)
352 { /*lint --e{715}*/
353  SCIP_Longint maxnnodes;
354  SCIP_Longint nsubnodes; /* node limit for the subproblem */
355 
356  SCIP_HEURDATA* heurdata; /* heuristic's data */
357  SCIP* subscip; /* the subproblem created by mutation */
358  SCIP_VAR** vars; /* original problem's variables */
359  SCIP_VAR** subvars; /* subproblem's variables */
360  SCIP_HASHMAP* varmapfw; /* mapping of SCIP variables to sub-SCIP variables */
361 
362  SCIP_Real cutoff; /* objective cutoff for the subproblem */
363  SCIP_Real maxnnodesr;
364  SCIP_Real memorylimit;
365  SCIP_Real timelimit; /* timelimit for the subproblem */
366  SCIP_Real upperbound;
367 
368  int nvars; /* number of original problem's variables */
369  int i;
370 
371  SCIP_Bool success;
372 
373  SCIP_RETCODE retcode;
374 
375  assert( heur != NULL );
376  assert( scip != NULL );
377  assert( result != NULL );
378 
379  /* get heuristic's data */
380  heurdata = SCIPheurGetData(heur);
381  assert( heurdata != NULL );
382 
383  *result = SCIP_DELAYED;
384 
385  /* only call heuristic, if feasible solution is available */
386  if( SCIPgetNSols(scip) <= 0 )
387  return SCIP_OKAY;
388 
389  /* only call heuristic, if the best solution comes from transformed problem */
390  assert( SCIPgetBestSol(scip) != NULL );
392  return SCIP_OKAY;
393 
394  /* only call heuristic, if enough nodes were processed since last incumbent */
395  if( SCIPgetNNodes(scip) - SCIPgetSolNodenum(scip,SCIPgetBestSol(scip)) < heurdata->nwaitingnodes)
396  return SCIP_OKAY;
397 
398  *result = SCIP_DIDNOTRUN;
399 
400  /* only call heuristic, if discrete variables are present */
401  if( SCIPgetNBinVars(scip) == 0 && SCIPgetNIntVars(scip) == 0 )
402  return SCIP_OKAY;
403 
404  /* calculate the maximal number of branching nodes until heuristic is aborted */
405  maxnnodesr = heurdata->nodesquot * SCIPgetNNodes(scip);
406 
407  /* reward mutation if it succeeded often, count the setup costs for the sub-MIP as 100 nodes */
408  maxnnodesr *= 1.0 + 2.0 * (SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur) + 1.0);
409  maxnnodes = (SCIP_Longint) maxnnodesr - 100 * SCIPheurGetNCalls(heur);
410  maxnnodes += heurdata->nodesofs;
411 
412  /* determine the node limit for the current process */
413  nsubnodes = maxnnodes - heurdata->usednodes;
414  nsubnodes = MIN(nsubnodes, heurdata->maxnodes);
415 
416  /* check whether we have enough nodes left to call subproblem solving */
417  if( nsubnodes < heurdata->minnodes )
418  return SCIP_OKAY;
419 
420  if( SCIPisStopped(scip) )
421  return SCIP_OKAY;
422 
423  *result = SCIP_DIDNOTFIND;
424 
425  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
426 
427  /* initializing the subproblem */
428  SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) );
429  SCIP_CALL( SCIPcreate(&subscip) );
430 
431  /* create the variable mapping hash map */
432  SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), SCIPcalcHashtableSize(5 * nvars)) );
433 
434  if( heurdata->uselprows )
435  {
436  char probname[SCIP_MAXSTRLEN];
437 
438  /* copy all plugins */
440 
441  /* get name of the original problem and add the string "_mutationsub" */
442  (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s_mutationsub", SCIPgetProbName(scip));
443 
444  /* create the subproblem */
445  SCIP_CALL( SCIPcreateProb(subscip, probname, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
446 
447  /* copy all variables */
448  SCIP_CALL( SCIPcopyVars(scip, subscip, varmapfw, NULL, TRUE) );
449  }
450  else
451  {
452  SCIP_Bool valid;
453  valid = FALSE;
454 
455  SCIP_CALL( SCIPcopy(scip, subscip, varmapfw, NULL, "rens", TRUE, FALSE, TRUE, &valid) );
456 
457  if( heurdata->copycuts )
458  {
459  /* copies all active cuts from cutpool of sourcescip to linear constraints in targetscip */
460  SCIP_CALL( SCIPcopyCuts(scip, subscip, varmapfw, NULL, TRUE, NULL) );
461  }
462 
463  SCIPdebugMessage("Copying the SCIP instance was %s complete.\n", valid ? "" : "not ");
464  }
465 
466  for( i = 0; i < nvars; i++ )
467  subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]);
468 
469  /* free hash map */
470  SCIPhashmapFree(&varmapfw);
471 
472  /* create a new problem, which fixes variables with same value in bestsol and LP relaxation */
473  SCIP_CALL( createSubproblem(scip, subscip, subvars, heurdata->minfixingrate, &heurdata->randseed,
474  heurdata->uselprows, &success) );
475 
476  /* terminate if it was not possible to create the subproblem */
477  if( !success )
478  {
479  SCIPdebugMessage("Could not create the subproblem -> skip call\n");
480  goto TERMINATE;
481  }
482 
483  /* do not abort subproblem on CTRL-C */
484  SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
485 
486  /* disable output to console */
487  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
488 
489  /* check whether there is enough time and memory left */
490  SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) );
491  if( !SCIPisInfinity(scip, timelimit) )
492  timelimit -= SCIPgetSolvingTime(scip);
493  SCIP_CALL( SCIPgetRealParam(scip, "limits/memory", &memorylimit) );
494 
495  /* substract the memory already used by the main SCIP and the estimated memory usage of external software */
496  if( !SCIPisInfinity(scip, memorylimit) )
497  {
498  memorylimit -= SCIPgetMemUsed(scip)/1048576.0;
499  memorylimit -= SCIPgetMemExternEstim(scip)/1048576.0;
500  }
501 
502  /* abort if no time is left or not enough memory to create a copy of SCIP, including external memory usage */
503  if( timelimit <= 0.0 || memorylimit <= 2.0*SCIPgetMemExternEstim(scip)/1048576.0 )
504  goto TERMINATE;
505 
506  /* disable statistic timing inside sub SCIP */
507  SCIP_CALL( SCIPsetBoolParam(subscip, "timing/statistictiming", FALSE) );
508 
509  /* set limits for the subproblem */
510  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", nsubnodes) );
511  SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) );
512  SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) );
513 
514  /* forbid recursive call of heuristics and separators solving subMIPs */
515  SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
516 
517  /* disable cutting plane separation */
519 
520  /* disable expensive presolving */
522 
523  /* use best estimate node selection */
524  if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") )
525  {
526  SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) );
527  }
528 
529  /* use inference branching */
530  if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") )
531  {
532  SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) );
533  }
534 
535  /* disable conflict analysis */
536  if( !SCIPisParamFixed(subscip, "conflict/useprop") )
537  {
538  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useprop", FALSE) );
539  }
540  if( !SCIPisParamFixed(subscip, "conflict/useinflp") )
541  {
542  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useinflp", FALSE) );
543  }
544  if( !SCIPisParamFixed(subscip, "conflict/useboundlp") )
545  {
546  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useboundlp", FALSE) );
547  }
548  if( !SCIPisParamFixed(subscip, "conflict/usesb") )
549  {
550  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usesb", FALSE) );
551  }
552  if( !SCIPisParamFixed(subscip, "conflict/usepseudo") )
553  {
554  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usepseudo", FALSE) );
555  }
556 
557  /* employ a limit on the number of enforcement rounds in the quadratic constraint handlers; this fixes the issue that
558  * sometimes the quadratic constraint handler needs hundreds or thousands of enforcement rounds to determine the
559  * feasibility status of a single node without fractional branching candidates by separation (namely for uflquad
560  * instances); however, the solution status of the sub-SCIP might get corrupted by this; hence no decutions shall be
561  * made for the original SCIP
562  */
563  if( SCIPfindConshdlr(subscip, "quadratic") != NULL && !SCIPisParamFixed(subscip, "constraints/quadratic/enfolplimit") )
564  {
565  SCIP_CALL( SCIPsetIntParam(subscip, "constraints/quadratic/enfolplimit", 10) );
566  }
567 
568  /* add an objective cutoff */
569  cutoff = SCIPinfinity(scip);
571 
572  upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip);
573  if( !SCIPisInfinity(scip, -1.0 * SCIPgetLowerbound(scip)) )
574  {
575  cutoff = (1 - heurdata->minimprove) * SCIPgetUpperbound(scip)
576  + heurdata->minimprove * SCIPgetLowerbound(scip);
577  }
578  else
579  {
580  if( SCIPgetUpperbound(scip) >= 0 )
581  cutoff = (1 - heurdata->minimprove) * SCIPgetUpperbound(scip);
582  else
583  cutoff = (1 + heurdata->minimprove) * SCIPgetUpperbound(scip);
584  }
585  cutoff = MIN(upperbound, cutoff);
586  SCIP_CALL(SCIPsetObjlimit(subscip, cutoff));
587 
588  /* solve the subproblem */
589  SCIPdebugMessage("Solve Mutation subMIP\n");
590  retcode = SCIPsolve(subscip);
591 
592  /* Errors in solving the subproblem should not kill the overall solving process
593  * Hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
594  */
595  if( retcode != SCIP_OKAY )
596  {
597 #ifndef NDEBUG
598  SCIP_CALL( retcode );
599 #endif
600  SCIPwarningMessage(scip, "Error while solving subproblem in Mutation heuristic; sub-SCIP terminated with code <%d>\n",retcode);
601  }
602  else
603  {
604  /* transfer variable statistics from sub-SCIP */
605  SCIP_CALL( SCIPmergeVariableStatistics(subscip, scip, subvars, vars, nvars) );
606  }
607 
608  heurdata->usednodes += SCIPgetNNodes(subscip);
609 
610  /* check, whether a solution was found */
611  if( SCIPgetNSols(subscip) > 0 )
612  {
613  SCIP_SOL** subsols;
614  int nsubsols;
615 
616  /* check, whether a solution was found;
617  * due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted
618  */
619  nsubsols = SCIPgetNSols(subscip);
620  subsols = SCIPgetSols(subscip);
621  success = FALSE;
622  for( i = 0; i < nsubsols && !success; ++i )
623  {
624  SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) );
625  }
626  if( success )
627  *result = SCIP_FOUNDSOL;
628  }
629 
630  TERMINATE:
631  /* free subproblem */
632  SCIPfreeBufferArray(scip, &subvars);
633  SCIP_CALL( SCIPfree(&subscip) );
634 
635  return SCIP_OKAY;
636 }
637 
638 /*
639  * primal heuristic specific interface methods
640  */
641 
642 /** creates the mutation primal heuristic and includes it in SCIP */
644  SCIP* scip /**< SCIP data structure */
645  )
646 {
647  SCIP_HEURDATA* heurdata;
648  SCIP_HEUR* heur;
649 
650  /* create Mutation primal heuristic data */
651  SCIP_CALL( SCIPallocMemory(scip, &heurdata) );
652 
653  /* include primal heuristic */
654  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
656  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecMutation, heurdata) );
657 
658  assert(heur != NULL);
659 
660  /* set non-NULL pointers to callback methods */
661  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyMutation) );
662  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeMutation) );
663  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitMutation) );
664 
665  /* add mutation primal heuristic parameters */
666  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/nodesofs",
667  "number of nodes added to the contingent of the total nodes",
668  &heurdata->nodesofs, FALSE, DEFAULT_NODESOFS, 0, INT_MAX, NULL, NULL) );
669 
670  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/maxnodes",
671  "maximum number of nodes to regard in the subproblem",
672  &heurdata->maxnodes, TRUE, DEFAULT_MAXNODES, 0, INT_MAX, NULL, NULL) );
673 
674  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/minnodes",
675  "minimum number of nodes required to start the subproblem",
676  &heurdata->minnodes, TRUE, DEFAULT_MINNODES, 0, INT_MAX, NULL, NULL) );
677 
678  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/nwaitingnodes",
679  "number of nodes without incumbent change that heuristic should wait",
680  &heurdata->nwaitingnodes, TRUE, DEFAULT_NWAITINGNODES, 0, INT_MAX, NULL, NULL) );
681 
682  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/nodesquot",
683  "contingent of sub problem nodes in relation to the number of nodes of the original problem",
684  &heurdata->nodesquot, FALSE, DEFAULT_NODESQUOT, 0.0, 1.0, NULL, NULL) );
685 
686  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/minfixingrate",
687  "percentage of integer variables that have to be fixed",
688  &heurdata->minfixingrate, FALSE, DEFAULT_MINFIXINGRATE, SCIPsumepsilon(scip), 1.0-SCIPsumepsilon(scip), NULL, NULL) );
689 
690  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/minimprove",
691  "factor by which " HEUR_NAME " should at least improve the incumbent",
692  &heurdata->minimprove, TRUE, DEFAULT_MINIMPROVE, 0.0, 1.0, NULL, NULL) );
693 
694  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/uselprows",
695  "should subproblem be created out of the rows in the LP rows?",
696  &heurdata->uselprows, TRUE, DEFAULT_USELPROWS, NULL, NULL) );
697 
698  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/copycuts",
699  "if uselprows == FALSE, should all active cuts from cutpool be copied to constraints in subproblem?",
700  &heurdata->copycuts, TRUE, DEFAULT_COPYCUTS, NULL, NULL) );
701 
702  return SCIP_OKAY;
703 }
#define DEFAULT_COPYCUTS
Definition: heur_mutation.c:50
static SCIP_DECL_HEURCOPY(heurCopyMutation)
SCIP_CONSHDLR * SCIPfindConshdlr(SCIP *scip, const char *name)
Definition: scip.c:5878
#define SCIPallocMemory(scip, ptr)
Definition: scip.h:20526
static SCIP_DECL_HEURFREE(heurFreeMutation)
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
Definition: scip.c:41648
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1147
SCIP_Longint SCIPheurGetNCalls(SCIP_HEUR *heur)
Definition: heur.c:1273
#define DEFAULT_NWAITINGNODES
Definition: heur_mutation.c:47
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:7297
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip.c:1248
#define SCIP_MAXSTRLEN
Definition: def.h:201
#define NULL
Definition: lpi_spx.cpp:130
SCIP_Bool SCIPisStopped(SCIP *scip)
Definition: scip.c:1125
static SCIP_RETCODE createSubproblem(SCIP *scip, SCIP *subscip, SCIP_VAR **subvars, SCIP_Real minfixingrate, unsigned int *randseed, SCIP_Bool uselprows, SCIP_Bool *success)
Definition: heur_mutation.c:83
SCIP_Real SCIProwGetLhs(SCIP_ROW *row)
Definition: lp.c:18915
SCIP_COL ** SCIProwGetCols(SCIP_ROW *row)
Definition: lp.c:18861
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition: var.c:17067
#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:7252
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:56
#define HEUR_DISPCHAR
Definition: heur_mutation.c:33
SCIP_Real SCIPgetSolvingTime(SCIP *scip)
Definition: scip.c:41009
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition: misc.c:2057
SCIP_RETCODE SCIPsetLongintParam(SCIP *scip, const char *name, SCIP_Longint value)
Definition: scip.c:4046
int SCIPgetNBinVars(SCIP *scip)
Definition: scip.c:10743
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:8174
#define TRUE
Definition: def.h:55
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
#define DEFAULT_MINFIXINGRATE
Definition: heur_mutation.c:45
#define SCIP_CALL(x)
Definition: def.h:266
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:36299
SCIP_RETCODE SCIPsetRealParam(SCIP *scip, const char *name, SCIP_Real value)
Definition: scip.c:4109
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
#define SCIPdebugMessage
Definition: pub_message.h:77
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:34983
#define HEUR_NAME
Definition: heur_mutation.c:31
#define BMSclearMemoryArray(ptr, num)
Definition: memory.h:85
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:2116
SCIP_Real SCIProwGetConstant(SCIP_ROW *row)
Definition: lp.c:18881
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip.c:24949
SCIP_Real SCIPgetLowerbound(SCIP *scip)
Definition: scip.c:38393
SCIP_RETCODE SCIPcopyCuts(SCIP *sourcescip, SCIP *targetscip, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, SCIP_Bool global, int *ncutsadded)
Definition: scip.c:2883
int SCIPgetNSols(SCIP *scip)
Definition: scip.c:35668
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:3547
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:3573
int SCIPgetNOrigVars(SCIP *scip)
Definition: scip.c:11138
static SCIP_DECL_HEUREXEC(heurExecMutation)
#define SCIPfreeMemory(scip, ptr)
Definition: scip.h:20542
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:41585
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:3254
SCIP_RETCODE SCIPincludeHeurMutation(SCIP *scip)
SCIP_RETCODE SCIPchgVarUbGlobal(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound)
Definition: scip.c:20049
SCIP_Real SCIProwGetRhs(SCIP_ROW *row)
Definition: lp.c:18925
SCIP_RETCODE SCIPmergeVariableStatistics(SCIP *sourcescip, SCIP *targetscip, SCIP_VAR **sourcevars, SCIP_VAR **targetvars, int nvars)
Definition: scip.c:2260
int SCIPcalcHashtableSize(int minsize)
Definition: misc.c:1157
SCIP_Bool SCIPisLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:41598
#define DEFAULT_NODESQUOT
Definition: heur_mutation.c:46
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:34002
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip.c:41353
SCIP_Real SCIPfloor(SCIP *scip, SCIP_Real val)
Definition: scip.c:41758
SCIP_Bool SCIProwIsLocal(SCIP_ROW *row)
Definition: lp.c:19024
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition: misc.c:2075
SCIP_RETCODE SCIPsetSeparating(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:4457
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:9019
SCIP_RETCODE SCIPgetLPRowsData(SCIP *scip, SCIP_ROW ***rows, int *nrows)
Definition: scip.c:26750
SCIP_RETCODE SCIPgetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:35020
SCIP_Real SCIPinfinity(SCIP *scip)
Definition: scip.c:41637
#define DEFAULT_MAXNODES
Definition: heur_mutation.c:42
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip.c:766
SCIP_SOL ** SCIPgetSols(SCIP *scip)
Definition: scip.c:35717
const char * SCIProwGetName(SCIP_ROW *row)
Definition: lp.c:18974
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1068
#define DEFAULT_MINIMPROVE
Definition: heur_mutation.c:43
#define HEUR_MAXDEPTH
Definition: heur_mutation.c:37
SCIP_Real SCIPgetUpperbound(SCIP *scip)
Definition: scip.c:38534
SCIP_Bool SCIPisGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:41611
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:53
SCIP_RETCODE SCIPincludeDefaultPlugins(SCIP *scip)
#define HEUR_USESSUBSCIP
Definition: heur_mutation.c:39
SCIP_Longint SCIPgetSolNodenum(SCIP *scip, SCIP_SOL *sol)
Definition: scip.c:35279
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip.c:4001
#define HEUR_PRIORITY
Definition: heur_mutation.c:34
SCIP_Bool SCIPisParamFixed(SCIP *scip, const char *name)
Definition: scip.c:3709
SCIP_RETCODE SCIPsetSubscipsOff(SCIP *scip, SCIP_Bool quiet)
Definition: scip.c:4382
SCIP_RETCODE SCIPcopyVars(SCIP *sourcescip, SCIP *targetscip, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, SCIP_Bool global)
Definition: scip.c:2179
SCIP_Longint SCIPgetMemUsed(SCIP *scip)
Definition: scip.c:41396
SCIP_RETCODE SCIPsetPresolving(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:4431
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:2179
int SCIPgetRandomInt(int minrandval, int maxrandval, unsigned int *seedp)
Definition: misc.c:7700
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
Definition: scip.c:7329
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip.c:11477
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip.c:14503
#define HEUR_FREQOFS
Definition: heur_mutation.c:36
SCIP_Real SCIPceil(SCIP *scip, SCIP_Real val)
Definition: scip.c:41770
SCIP_Longint SCIPheurGetNBestSolsFound(SCIP_HEUR *heur)
Definition: heur.c:1293
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip.h:20585
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:17057
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip.c:692
int SCIPgetNIntVars(SCIP *scip)
Definition: scip.c:10788
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:18871
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:7313
int SCIPvarGetProbindex(SCIP_VAR *var)
Definition: var.c:16750
SCIP_BRANCHRULE * SCIPfindBranchrule(SCIP *scip, const char *name)
Definition: scip.c:8436
#define HEUR_DESC
Definition: heur_mutation.c:32
#define HEUR_TIMING
Definition: heur_mutation.c:38
#define SCIP_Real
Definition: def.h:127
#define MIN(x, y)
Definition: memory.c:67
SCIP_RETCODE SCIPchgVarLbGlobal(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound)
Definition: scip.c:19972
#define HEUR_FREQ
Definition: heur_mutation.c:35
SCIP_RETCODE SCIPsetObjlimit(SCIP *scip, SCIP_Real objlimit)
Definition: scip.c:10194
const char * SCIPgetProbName(SCIP *scip)
Definition: scip.c:9941
static SCIP_DECL_HEURINIT(heurInitMutation)
#define SCIP_Longint
Definition: def.h:112
SCIP_RETCODE SCIPsetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:34885
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip.c:3938
int SCIProwGetNNonz(SCIP_ROW *row)
Definition: lp.c:18836
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1058
SCIP_RETCODE SCIPgetVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition: scip.c:10572
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip.h:20597
SCIP_RETCODE SCIPgetRealParam(SCIP *scip, const char *name, SCIP_Real *value)
Definition: scip.c:3797
SCIP_NODESEL * SCIPfindNodesel(SCIP *scip, const char *name)
Definition: scip.c:8124
SCIP_VAR * SCIPcolGetVar(SCIP_COL *col)
Definition: lp.c:18685
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip.c:35767
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:3629
SCIP_Real SCIPsumepsilon(SCIP *scip)
Definition: scip.c:41132
default SCIP plugins
SCIP_Longint SCIPgetNNodes(SCIP *scip)
Definition: scip.c:37372
SCIP callable library.
#define DEFAULT_NODESOFS
Definition: heur_mutation.c:41
SCIP_Longint SCIPgetMemExternEstim(SCIP *scip)
Definition: scip.c:41409