Scippy

SCIP

Solving Constraint Integer Programs

heur_ofins.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-2015 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_ofins.c
17  * @brief OFINS - Objective Function Induced Neighborhood Search. Primal heuristic for reoptimization
18  * @author Jakob Witzig
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 <stdio.h>
26 
27 #include "scip/heur_ofins.h"
28 #include "scip/scipdefplugins.h" /* needed for the secondary SCIP instance */
29 #include "scip/pub_misc.h"
30 
31 #define HEUR_NAME "ofins"
32 #define HEUR_DESC "primal heuristic for reoptimization, objective function induced neighborhood search"
33 #define HEUR_DISPCHAR 'A'
34 #define HEUR_PRIORITY 60000
35 #define HEUR_FREQ -1
36 #define HEUR_FREQOFS 0
37 #define HEUR_MAXDEPTH 0
38 #define HEUR_TIMING SCIP_HEURTIMING_BEFORENODE
39 #define HEUR_USESSUBSCIP TRUE /**< does the heuristic use a secondary SCIP instance? */
40 
41 /* default values for OFINS-specific plugins */
42 #define DEFAULT_MAXNODES 5000LL /* maximum number of nodes to regard in the subproblem */
43 #define DEFAULT_MAXCHGRATE 0.50 /* maximum percentage of changed objective coefficients */
44 #define DEFAULT_COPYCUTS TRUE /* if DEFAULT_USELPROWS is FALSE, then should all active cuts from the cutpool
45  * of the original scip be copied to constraints of the subscip
46  */
47 #define DEFAULT_MAXCHANGE 0.04 /* maximal rate of change per coefficient to get fixed */
48 #define DEFAULT_MINIMPROVE 0.01 /* factor by which OFINS should at least improve the incumbent */
49 #define DEFAULT_ADDALLSOLS FALSE /* should all subproblem solutions be added to the original SCIP? */
50 #define DEFAULT_MINNODES 50LL /* minimum number of nodes to regard in the subproblem */
51 #define DEFAULT_NODESOFS 500LL /* number of nodes added to the contingent of the total nodes */
52 #define DEFAULT_NODESQUOT 0.1 /* subproblem nodes in relation to nodes of the original problem */
53 #define DEFAULT_LPLIMFAC 2.0 /* factor by which the limit on the number of LP depends on the node limit */
54 
55 /* event handler properties */
56 #define EVENTHDLR_NAME "Ofins"
57 #define EVENTHDLR_DESC "LP event handler for " HEUR_NAME " heuristic"
58 
59 
60 /** primal heuristic data */
61 struct SCIP_HeurData
62 {
63  SCIP_Real maxchangerate; /**< maximal rate of changed coefficients in the objective function */
64  SCIP_Longint maxnodes; /**< maximum number of nodes to regard in the subproblem */
65  SCIP_Bool copycuts; /**< should all active cuts from cutpool be copied to constraints in subproblem? */
66  SCIP_Bool addallsols; /**< should all subproblem solutions be added to the original SCIP? */
67  SCIP_Longint minnodes; /**< minimum number of nodes to regard in the subproblem */
68  SCIP_Longint nodesofs; /**< number of nodes added to the contingent of the total nodes */
69  SCIP_Real maxchange; /**< maximal rate of change per coefficient to get fixed */
70  SCIP_Real minimprove; /**< factor by which OFINS should at least improve the incumbent */
71  SCIP_Real nodesquot; /**< subproblem nodes in relation to nodes of the original problem */
72  SCIP_Real nodelimit; /**< the nodelimit employed in the current sub-SCIP, for the event handler*/
73  SCIP_Real lplimfac; /**< factor by which the limit on the number of LP depends on the node limit */
74 };
75 
76 /* ---------------- Callback methods of event handler ---------------- */
77 
78 /* exec the event handler
79  *
80  * we interrupt the solution process
81  */
82 static
83 SCIP_DECL_EVENTEXEC(eventExecOfins)
84 {
85  SCIP_HEURDATA* heurdata;
86 
87  assert(eventhdlr != NULL);
88  assert(eventdata != NULL);
89  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
90  assert(event != NULL);
92 
93  heurdata = (SCIP_HEURDATA*)eventdata;
94  assert(heurdata != NULL);
95 
96  /* interrupt solution process of sub-SCIP */
97  if( SCIPgetNLPs(scip) > heurdata->lplimfac * heurdata->nodelimit )
98  {
99  SCIPdebugMessage("interrupt after %" SCIP_LONGINT_FORMAT " LPs\n",SCIPgetNLPs(scip));
100  SCIP_CALL( SCIPinterruptSolve(scip) );
101  }
102 
103  return SCIP_OKAY;
104 }
105 
106 /** creates a subproblem by fixing a number of variables */
107 static
109  SCIP* scip, /**< original SCIP data structure */
110  SCIP* subscip, /**< SCIP data structure for the subproblem */
111  SCIP_VAR** subvars, /**< the variables of the subproblem */
112  SCIP_Bool* chgcoeffs /**< array indicating which coefficients have changed */
113  )
114 {
115  SCIP_VAR** vars;
116  SCIP_SOL* sol;
117  int nvars;
118  int i;
119 
120  assert(scip != NULL);
121  assert(subscip != NULL);
122  assert(subvars != NULL);
123 
124  /* get binary variables; all continuous variables remain unfixed */
125  vars = SCIPgetVars(scip);
126  nvars = SCIPgetNBinVars(scip);
127 
128  /* get optimal solution of the last iteration */
129  sol = SCIPgetReoptLastOptSol(scip);
130  assert(sol != NULL);
131 
132  /* change bounds of variables of the subproblem */
133  for( i = 0; i < nvars; i++ )
134  {
135  SCIP_Real solval;
136 
137  if( !chgcoeffs[i] )
138  {
139  assert(subvars[i] != NULL);
140 
141  solval = SCIPgetSolVal(scip, sol, vars[i]);
142 
143  /* perform the bound change */
144  SCIP_CALL( SCIPchgVarLbGlobal(subscip, subvars[i], solval) );
145  SCIP_CALL( SCIPchgVarUbGlobal(subscip, subvars[i], solval) );
146  }
147  }
148 
149  /* set an objective limit */
150  SCIPdebugMessage("set objective limit of %g to sub-SCIP\n", SCIPgetUpperbound(scip));
151  SCIP_CALL( SCIPsetObjlimit(subscip, SCIPgetUpperbound(scip)) );
152 
153  return SCIP_OKAY;
154 }
155 
156 /** creates a new solution for the original problem by copying the solution of the subproblem */
157 static
159  SCIP* scip, /**< original SCIP data structure */
160  SCIP* subscip, /**< SCIP structure of the subproblem */
161  SCIP_VAR** subvars, /**< the variables of the subproblem */
162  SCIP_HEUR* heur, /**< RENS heuristic structure */
163  SCIP_SOL* subsol, /**< solution of the subproblem */
164  SCIP_Bool* success /**< used to store whether new solution was found or not */
165  )
166 {
167  SCIP_VAR** vars; /* the original problem's variables */
168  int nvars; /* the original problem's number of variables */
169  SCIP_Real* subsolvals; /* solution values of the subproblem */
170  SCIP_SOL* newsol; /* solution to be created for the original problem */
171 
172  assert(scip != NULL);
173  assert(subscip != NULL);
174  assert(subvars != NULL);
175  assert(subsol != NULL);
176 
177  /* get variables' data */
178  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
179 
180  /* sub-SCIP may have more variables than the number of active (transformed) variables in the main SCIP
181  * since constraint copying may have required the copy of variables that are fixed in the main SCIP
182  */
183  assert(nvars <= SCIPgetNOrigVars(subscip));
184 
185  SCIP_CALL( SCIPallocBufferArray(scip, &subsolvals, nvars) );
186 
187  /* copy the solution */
188  SCIP_CALL( SCIPgetSolVals(subscip, subsol, nvars, subvars, subsolvals) );
189 
190  /* create new solution for the original problem */
191  SCIP_CALL( SCIPcreateSol(scip, &newsol, heur) );
192  SCIP_CALL( SCIPsetSolVals(scip, newsol, nvars, vars, subsolvals) );
193 
194  /* try to add new solution to scip and free it immediately */
195  SCIP_CALL( SCIPtrySolFree(scip, &newsol, FALSE, TRUE, TRUE, TRUE, success) );
196 
197  SCIPfreeBufferArray(scip, &subsolvals);
198 
199  return SCIP_OKAY;
200 }
201 
202 /** main procedure of the OFINS heuristic, creates and solves a sub-SCIP */
203 static
205  SCIP* scip, /**< original SCIP data structure */
206  SCIP_HEUR* heur, /**< heuristic data structure */
207  SCIP_HEURDATA* heurdata, /**< euristic's private data structure */
208  SCIP_RESULT* result, /**< result data structure */
209  SCIP_Longint nstallnodes, /**< number of stalling nodes for the subproblem */
210  SCIP_Bool* chgcoeffs /**< array of changed coefficients */
211  )
212 {
213  SCIP* subscip; /* the subproblem created by OFINS */
214  SCIP_HASHMAP* varmapfw; /* mapping of SCIP variables to sub-SCIP variables */
215  SCIP_VAR** vars; /* source problem's variables */
216  SCIP_VAR** subvars; /* subproblem's variables */
217  SCIP_EVENTHDLR* eventhdlr; /* event handler for LP events */
218 
219  SCIP_Real timelimit; /* time limit for OFINS subproblem */
220  SCIP_Real memorylimit; /* memory limit for OFINS subproblem */
221 
222  int nvars; /* number of source problem's variables */
223  int i;
224 
225  SCIP_SOL** subsols;
226  int nsubsols;
227 
228  SCIP_Bool valid;
229  SCIP_Bool success;
230  SCIP_RETCODE retcode;
231 
232  assert(scip != NULL);
233  assert(heur != NULL);
234  assert(heurdata != NULL);
235  assert(result != NULL);
236  assert(chgcoeffs != NULL);
237 
238  *result = SCIP_DIDNOTRUN;
239 
240  SCIPdebugMessage("+---+ Start OFINS heuristic +---+\n");
241 
242  /* check whether there is enough time and memory left */
243  timelimit = 0.0;
244  memorylimit = 0.0;
245  SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) );
246  if( !SCIPisInfinity(scip, timelimit) )
247  timelimit -= SCIPgetSolvingTime(scip);
248  SCIP_CALL( SCIPgetRealParam(scip, "limits/memory", &memorylimit) );
249 
250  /* substract the memory already used by the main SCIP and the estimated memory usage of external software */
251  if( !SCIPisInfinity(scip, memorylimit) )
252  {
253  memorylimit -= SCIPgetMemUsed(scip)/1048576.0;
254  memorylimit -= SCIPgetMemExternEstim(scip)/1048576.0;
255  }
256 
257  /* abort if no time is left or not enough memory to create a copy of SCIP, including external memory usage */
258  if( timelimit <= 0.0 || memorylimit <= 2.0*SCIPgetMemExternEstim(scip)/1048576.0 )
259  {
260  SCIPdebugMessage("-> not enough memory left\n");
261  return SCIP_OKAY;
262  }
263 
264  *result = SCIP_DIDNOTFIND;
265 
266  /* get variable data */
267  vars = SCIPgetVars(scip);
268  nvars = SCIPgetNVars(scip);
269 
270  /* initialize the subproblem */
271  SCIP_CALL( SCIPcreate(&subscip) );
272 
273  /* create the variable mapping hash map */
274  SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), SCIPcalcHashtableSize(5 * nvars)) );
275  SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) );
276 
277  eventhdlr = NULL;
278 
279  valid = FALSE;
280 
281  /* copy complete SCIP instance */
282  SCIP_CALL( SCIPcopy(scip, subscip, varmapfw, NULL, "ofins", TRUE, FALSE, TRUE, &valid) );
283 
284  if( heurdata->copycuts )
285  {
286  /* copies all active cuts from cutpool of sourcescip to linear constraints in targetscip */
287  SCIP_CALL( SCIPcopyCuts(scip, subscip, varmapfw, NULL, TRUE, NULL) );
288  }
289 
290  SCIPdebugMessage("Copying the SCIP instance was %s complete.\n", valid ? "" : "not ");
291 
292  /* create event handler for LP events */
293  SCIP_CALL( SCIPincludeEventhdlrBasic(subscip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecOfins, NULL) );
294  if( eventhdlr == NULL )
295  {
296  SCIPerrorMessage("event handler for " HEUR_NAME " heuristic not found.\n");
297  return SCIP_PLUGINNOTFOUND;
298  }
299 
300  for( i = 0; i < nvars; i++ )
301  {
302  subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]);
303  assert(subvars[i] != NULL);
304  }
305  /* free hash map */
306  SCIPhashmapFree(&varmapfw);
307 
308  /* create a new problem, which fixes variables with same value in bestsol and LP relaxation */
309  SCIP_CALL( createSubproblem(scip, subscip, subvars, chgcoeffs) );
310  SCIPdebugMessage("OFINS subproblem: %d vars, %d cons\n", SCIPgetNVars(subscip), SCIPgetNConss(subscip));
311 
312  /* do not abort subproblem on CTRL-C */
313  SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
314 
315  /* disable output to console */
316  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
317 
318  /* set limits for the subproblem */
319  heurdata->nodelimit = heurdata->maxnodes;
320  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/stallnodes", nstallnodes) );
321  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", heurdata->maxnodes) );
322  SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) );
323  SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) );
324 
325  /* forbid recursive call of heuristics and separators solving sub-SCIPs */
326  SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
327 
328  /* disable cutting plane separation */
330 
331  /* disable expensive presolving */
333 
334  /* use best estimate node selection */
335  if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") )
336  {
337  SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) );
338  }
339 
340  /* use inference branching */
341  if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") )
342  {
343  SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) );
344  }
345 
346  /* disable conflict analysis */
347  if( !SCIPisParamFixed(subscip, "conflict/enable") )
348  {
349  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/enable", FALSE) );
350  }
351 
352 #ifdef SCIP_DEBUG
353  /* for debugging RENS, enable MIP output */
354  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 5) );
355  SCIP_CALL( SCIPsetIntParam(subscip, "display/freq", 100000000) );
356 #endif
357 
358  /* presolve the subproblem */
359  retcode = SCIPpresolve(subscip);
360 
361  /* errors in solving the subproblem should not kill the overall solving process;
362  * hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
363  */
364  if( retcode != SCIP_OKAY )
365  {
366 #ifndef NDEBUG
367  SCIP_CALL( retcode );
368 #endif
369  SCIPwarningMessage(scip, "Error while presolving subproblem in %s heuristic; sub-SCIP terminated with code <%d>\n", HEUR_NAME, retcode);
370 
371  /* free */
372  SCIPfreeBufferArray(scip, &subvars);
373  SCIP_CALL( SCIPfree(&subscip) );
374  return SCIP_OKAY;
375  }
376 
377  SCIPdebugMessage("%s presolved subproblem: %d vars, %d cons\n", HEUR_NAME, SCIPgetNVars(subscip), SCIPgetNConss(subscip));
378 
379  assert(eventhdlr != NULL);
380 
381  SCIP_CALL( SCIPtransformProb(subscip) );
382  SCIP_CALL( SCIPcatchEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, NULL) );
383 
384  /* solve the subproblem */
385  SCIPdebugMessage("solving subproblem: nstallnodes=%" SCIP_LONGINT_FORMAT ", maxnodes=%" SCIP_LONGINT_FORMAT "\n", nstallnodes, heurdata->maxnodes);
386  retcode = SCIPsolve(subscip);
387 
388  SCIP_CALL( SCIPdropEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, -1) );
389 
390  /* errors in solving the subproblem should not kill the overall solving process;
391  * hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
392  */
393  if( retcode != SCIP_OKAY )
394  {
395 #ifndef NDEBUG
396  SCIP_CALL( retcode );
397 #endif
398  SCIPwarningMessage(scip, "Error while solving subproblem in RENS heuristic; sub-SCIP terminated with code <%d>\n", retcode);
399  }
400 
401  /* print solving statistics of subproblem if we are in SCIP's debug mode */
403 
404  /* check, whether a solution was found;
405  * due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted
406  */
407  nsubsols = SCIPgetNSols(subscip);
408  subsols = SCIPgetSols(subscip);
409  success = FALSE;
410  for( i = 0; i < nsubsols && (!success || heurdata->addallsols); i++ )
411  {
412  SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) );
413  if( success )
414  *result = SCIP_FOUNDSOL;
415  }
416 
417  SCIPstatisticPrintf("%s statistic: fixed %6.3f integer variables, needed %6.1f seconds, %" SCIP_LONGINT_FORMAT " nodes, solution %10.4f found at node %" SCIP_LONGINT_FORMAT "\n",
418  HEUR_NAME, 0.0, SCIPgetSolvingTime(subscip), SCIPgetNNodes(subscip), success ? SCIPgetPrimalbound(scip) : SCIPinfinity(scip),
419  nsubsols > 0 ? SCIPsolGetNodenum(SCIPgetBestSol(subscip)) : -1 );
420 
421  /* free subproblem */
422  SCIPfreeBufferArray(scip, &subvars);
423  SCIP_CALL( SCIPfree(&subscip) );
424 
425  return SCIP_OKAY;
426 }
427 
428 /* put your local methods here, and declare them static */
429 
430 
431 /*
432  * Callback methods of primal heuristic
433  */
434 
435 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
436 static
437 SCIP_DECL_HEURFREE(heurFreeOfins)
438 { /*lint --e{715}*/
439  SCIP_HEURDATA* heurdata;
440 
441  assert(heur != NULL);
442  assert(scip != NULL);
443 
444  /* get heuristic data */
445  heurdata = SCIPheurGetData(heur);
446  assert(heurdata != NULL);
447 
448  /* free heuristic data */
449  SCIPfreeMemory(scip, &heurdata);
450  SCIPheurSetData(heur, NULL);
451 
452  return SCIP_OKAY;
453 }
454 
455 /** execution method of primal heuristic */
456 static
457 SCIP_DECL_HEUREXEC(heurExecOfins)
458 {/*lint --e{715}*/
459  SCIP_HEURDATA* heurdata;
460  SCIP_VAR** vars;
461  SCIP_Bool* chgcoeffs;
462  SCIP_Longint nstallnodes;
463  int nchgcoefs;
464  int nvars;
465  int v;
466 
467  assert( heur != NULL );
468  assert( scip != NULL );
469  assert( result != NULL );
470 
471  *result = SCIP_DELAYED;
472 
473  /* do not call heuristic of node was already detected to be infeasible */
474  if( nodeinfeasible )
475  return SCIP_OKAY;
476 
477  /* get heuristic data */
478  heurdata = SCIPheurGetData(heur);
479  assert( heurdata != NULL );
480 
481  /* only call heuristic, if reoptimization is enabled */
482  if( !SCIPisReoptEnabled(scip) )
483  return SCIP_OKAY;
484 
485  /* only call the heuristic, if we are in run >= 2 */
486  if( SCIPgetNReoptRuns(scip) <= 1 )
487  return SCIP_OKAY;
488 
489  *result = SCIP_DIDNOTRUN;
490 
491  if( SCIPisStopped(scip) )
492  return SCIP_OKAY;
493 
494  /* calculate the maximal number of branching nodes until heuristic is aborted */
495  nstallnodes = (SCIP_Longint)(heurdata->nodesquot * SCIPgetNNodes(scip));
496 
497  /* reward OFINS if it succeeded often */
498  nstallnodes = (SCIP_Longint)(nstallnodes * 3.0 * (SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur) + 1.0));
499  nstallnodes -= 100 * SCIPheurGetNCalls(heur); /* count the setup costs for the sub-SCIP as 100 nodes */
500  nstallnodes += heurdata->nodesofs;
501 
502  /* determine the node limit for the current process */
503  nstallnodes = MIN(nstallnodes, heurdata->maxnodes);
504 
505  /* check whether we have enough nodes left to call subproblem solving */
506  if( nstallnodes < heurdata->minnodes )
507  {
508  SCIPdebugMessage("skipping OFINS: nstallnodes=%" SCIP_LONGINT_FORMAT ", minnodes=%" SCIP_LONGINT_FORMAT "\n", nstallnodes, heurdata->minnodes);
509  return SCIP_OKAY;
510  }
511 
512  /* get variable data and check which coefficient has changed */
513  vars = SCIPgetVars(scip);
514  nvars = SCIPgetNBinVars(scip);
515  nchgcoefs = 0;
516 
517  SCIP_CALL( SCIPallocBufferArray(scip, &chgcoeffs, nvars) );
518 
519  for( v = 0; v < nvars; v++ )
520  {
521  SCIP_VAR* origvar;
522  SCIP_Real constant;
523  SCIP_Real scalar;
524  SCIP_Real newcoef;
525  SCIP_Real oldcoef;
526  SCIP_Real newcoefabs;
527  SCIP_Real oldcoefabs;
528  SCIP_Real frac;
529 
530  /* we only want to count variables that are unfixed after the presolving */
531  assert(SCIPvarGetStatus(vars[v]) != SCIP_VARSTATUS_ORIGINAL);
532  assert(SCIPvarIsActive(vars[v]));
533 
534  origvar = vars[v];
535  scalar = 1.0;
536  constant = 0.0;
537  SCIP_CALL( SCIPvarGetOrigvarSum(&origvar, &scalar, &constant) );
538 
539  newcoef = SCIPvarGetObj(origvar);
540  SCIP_CALL( SCIPgetReoptOldObjCoef(scip, origvar, SCIPgetNReoptRuns(scip)-1, &oldcoef) );
541  newcoefabs = fabs(newcoef);
542  oldcoefabs = fabs(oldcoef);
543 
544  frac = SCIP_INVALID;
545 
546  /* if both coefficients are zero nothing has changed */
547  if( SCIPisZero(scip, newcoef) && SCIPisZero(scip, oldcoef) )
548  {
549  frac = 0;
550  }
551  /* if exactly one coefficient is zero, the other need to be close to zero */
552  else if( SCIPisZero(scip, newcoef) || SCIPisZero(scip, oldcoef) )
553  {
554  assert(SCIPisZero(scip, newcoef) != SCIPisZero(scip, oldcoef));
555  if( !SCIPisZero(scip, newcoef) )
556  frac = MIN(1, newcoefabs);
557  else
558  frac = MIN(1, oldcoefabs);
559  }
560  /* if both coefficients have the same sign we calculate the quotient
561  * MIN(newcoefabs, oldcoefabs)/MAX(newcoefabs, oldcoefabs)
562  */
563  else if( SCIPisPositive(scip, newcoef) == SCIPisPositive(scip, oldcoef) )
564  {
565  frac = MIN(newcoefabs, oldcoefabs)/MAX(newcoefabs, oldcoefabs);
566  }
567  /* if both coefficients have a different sign, we set frac = 1 */
568  else
569  {
570  assert((SCIPisPositive(scip, newcoef) && SCIPisNegative(scip, oldcoef))
571  || (SCIPisNegative(scip, newcoef) && SCIPisPositive(scip, oldcoef)));
572 
573  frac = 1;
574  }
575 
576  if( frac > heurdata->maxchange )
577  {
578  chgcoeffs[v] = TRUE;
579  nchgcoefs++;
580  }
581  else
582  chgcoeffs[v] = FALSE;
583  }
584 
585  SCIPdebugMessage("%d (rate %.4f) changed coefficients\n", nchgcoefs, nchgcoefs/((SCIP_Real)nvars));
586 
587  /* we only want to run the heuristic, if there at least 3 changed coefficients.
588  * if the number of changed coefficients is 2 the trivialnegation heuristic will construct an
589  * optimal solution without solving a MIP.
590  */
591  if( nchgcoefs < 3 )
592  goto TERMINATE;
593 
594  /* run the heuristic, if not too many coefficients have changed */
595  if( nchgcoefs/((SCIP_Real)SCIPgetNBinVars(scip)) > heurdata->maxchangerate )
596  goto TERMINATE;
597 
598  SCIP_CALL( applyOfins(scip, heur, heurdata, result, nstallnodes, chgcoeffs) );
599 
600  TERMINATE:
601  SCIPfreeBufferArray(scip, &chgcoeffs);
602 
603  return SCIP_OKAY;
604 }
605 
606 
607 /*
608  * primal heuristic specific interface methods
609  */
610 
611 /** creates the ofins primal heuristic and includes it in SCIP */
613  SCIP* scip /**< SCIP data structure */
614  )
615 {
616  SCIP_HEURDATA* heurdata;
617  SCIP_HEUR* heur;
618 
619  /* create ofins primal heuristic data */
620  SCIP_CALL( SCIPallocMemory(scip, &heurdata) );
621  assert(heurdata != NULL);
622 
623  /* include primal heuristic */
624  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
626  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecOfins, heurdata) );
627 
628  assert(heur != NULL);
629 
630  /* set non fundamental callbacks via setter functions */
631  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeOfins) );
632 
633  /* add ofins primal heuristic parameters */
634 
635  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/" HEUR_NAME "/maxnodes",
636  "maximum number of nodes to regard in the subproblem",
637  &heurdata->maxnodes, TRUE, DEFAULT_MAXNODES, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
638 
639  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/" HEUR_NAME "/minnodes",
640  "minimum number of nodes required to start the subproblem",
641  &heurdata->minnodes, TRUE, DEFAULT_MINNODES, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
642 
643  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/maxchangerate",
644  "maximal rate of changed coefficients",
645  &heurdata->maxchangerate, FALSE, DEFAULT_MAXCHGRATE, 0.0, 1.0, NULL, NULL) );
646 
647  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/maxchange",
648  "maximal rate of change per coefficient to get fixed",
649  &heurdata->maxchange, FALSE, DEFAULT_MAXCHANGE, 0.0, 1.0, NULL, NULL) );
650 
651  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/copycuts",
652  "should all active cuts from cutpool be copied to constraints in subproblem?",
653  &heurdata->copycuts, TRUE, DEFAULT_COPYCUTS, NULL, NULL) );
654 
655  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/addallsols",
656  "should all subproblem solutions be added to the original SCIP?",
657  &heurdata->addallsols, TRUE, DEFAULT_ADDALLSOLS, NULL, NULL) );
658 
659  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/" HEUR_NAME "/nodesofs",
660  "number of nodes added to the contingent of the total nodes",
661  &heurdata->nodesofs, FALSE, DEFAULT_NODESOFS, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
662 
663  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/nodesquot",
664  "contingent of sub problem nodes in relation to the number of nodes of the original problem",
665  &heurdata->nodesquot, FALSE, DEFAULT_NODESQUOT, 0.0, 1.0, NULL, NULL) );
666 
667  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/minimprove",
668  "factor by which RENS should at least improve the incumbent",
669  &heurdata->minimprove, TRUE, DEFAULT_MINIMPROVE, 0.0, 1.0, NULL, NULL) );
670 
671  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/lplimfac",
672  "factor by which the limit on the number of LP depends on the node limit",
673  &heurdata->lplimfac, TRUE, DEFAULT_LPLIMFAC, 1.0, SCIP_REAL_MAX, NULL, NULL) );
674 
675  return SCIP_OKAY;
676 }
enum SCIP_Result SCIP_RESULT
Definition: type_result.h:51
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
Definition: scip.c:41293
SCIP_RETCODE SCIPprintStatistics(SCIP *scip, FILE *file)
Definition: scip.c:39903
int SCIPgetNVars(SCIP *scip)
Definition: scip.c:10735
#define SCIP_EVENTTYPE_LPSOLVED
Definition: type_event.h:78
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
Definition: scip.c:41256
SCIP_RETCODE SCIPvarGetOrigvarSum(SCIP_VAR **var, SCIP_Real *scalar, SCIP_Real *constant)
Definition: var.c:11991
SCIP_Longint SCIPheurGetNCalls(SCIP_HEUR *heur)
Definition: heur.c:1273
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip.c:1220
SCIP_VAR ** SCIPgetVars(SCIP *scip)
Definition: scip.c:10690
static SCIP_DECL_HEURFREE(heurFreeOfins)
Definition: heur_ofins.c:437
static SCIP_RETCODE createSubproblem(SCIP *scip, SCIP *subscip, SCIP_VAR **subvars, SCIP_Bool *chgcoeffs)
Definition: heur_ofins.c:108
#define NULL
Definition: lpi_spx.cpp:130
SCIP_Bool SCIPisStopped(SCIP *scip)
Definition: scip.c:1125
static SCIP_DECL_EVENTEXEC(eventExecOfins)
Definition: heur_ofins.c:83
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:7221
#define DEFAULT_COPYCUTS
Definition: heur_ofins.c:44
#define DEFAULT_NODESQUOT
Definition: heur_ofins.c:52
#define DEFAULT_LPLIMFAC
Definition: heur_ofins.c:53
#define FALSE
Definition: def.h:53
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition: misc.c:2052
SCIP_Real SCIPgetSolvingTime(SCIP *scip)
Definition: scip.c:40583
SCIP_RETCODE SCIPsetLongintParam(SCIP *scip, const char *name, SCIP_Longint value)
Definition: scip.c:4015
int SCIPgetNBinVars(SCIP *scip)
Definition: scip.c:10780
SCIP_RETCODE SCIPincludeEventhdlrBasic(SCIP *scip, SCIP_EVENTHDLR **eventhdlrptr, const char *name, const char *desc, SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip.c:7747
static SCIP_DECL_HEUREXEC(heurExecOfins)
Definition: heur_ofins.c:457
#define TRUE
Definition: def.h:52
#define SCIPdebug(x)
Definition: pub_message.h:74
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
SCIP_RETCODE SCIPgetReoptOldObjCoef(SCIP *scip, SCIP_VAR *var, int run, SCIP_Real *objcoef)
Definition: scip.c:14999
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:35909
SCIP_RETCODE SCIPsetRealParam(SCIP *scip, const char *name, SCIP_Real value)
Definition: scip.c:4078
#define HEUR_DISPCHAR
Definition: heur_ofins.c:33
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
#define HEUR_PRIORITY
Definition: heur_ofins.c:34
SCIP_RETCODE SCIPaddLongintParam(SCIP *scip, const char *name, const char *desc, SCIP_Longint *valueptr, SCIP_Bool isadvanced, SCIP_Longint defaultvalue, SCIP_Longint minvalue, SCIP_Longint maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:3570
#define SCIPdebugMessage
Definition: pub_message.h:77
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:34593
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip.h:20414
#define HEUR_FREQ
Definition: heur_ofins.c:35
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
Definition: var.c:16803
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:2111
#define SCIP_LONGINT_MAX
Definition: def.h:110
#define HEUR_TIMING
Definition: heur_ofins.c:38
#define HEUR_MAXDEPTH
Definition: heur_ofins.c:37
SCIP_RETCODE SCIPincludeHeurOfins(SCIP *scip)
Definition: heur_ofins.c:612
SCIP_RETCODE SCIPcopyCuts(SCIP *sourcescip, SCIP *targetscip, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, SCIP_Bool global, int *ncutsadded)
Definition: scip.c:2852
int SCIPgetNSols(SCIP *scip)
Definition: scip.c:35278
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:3516
SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
Definition: var.c:16460
int SCIPgetNOrigVars(SCIP *scip)
Definition: scip.c:11175
int SCIPgetNConss(SCIP *scip)
Definition: scip.c:11773
SCIP_RETCODE SCIPinterruptSolve(SCIP *scip)
Definition: scip.c:15528
#define EVENTHDLR_DESC
Definition: heur_ofins.c:57
#define HEUR_DESC
Definition: heur_ofins.c:32
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:3223
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
Definition: scip.c:41317
#define SCIPallocMemory(scip, ptr)
Definition: scip.h:20355
SCIP_RETCODE SCIPchgVarUbGlobal(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound)
Definition: scip.c:20115
#define SCIPerrorMessage
Definition: pub_message.h:45
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip.h:20426
#define DEFAULT_MINNODES
Definition: heur_ofins.c:50
#define HEUR_FREQOFS
Definition: heur_ofins.c:36
int SCIPcalcHashtableSize(int minsize)
Definition: misc.c:1152
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:33612
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip.c:40927
struct SCIP_EventData SCIP_EVENTDATA
Definition: type_event.h:146
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition: misc.c:2070
SCIP_RETCODE SCIPsetSeparating(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:4426
SCIP_RETCODE SCIPgetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:34630
SCIP_Real SCIPinfinity(SCIP *scip)
Definition: scip.c:41245
SCIP_RETCODE SCIPpresolve(SCIP *scip)
Definition: scip.c:14378
#define SCIP_CALL(x)
Definition: def.h:263
#define SCIPstatisticPrintf
Definition: pub_message.h:107
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip.c:766
SCIP_SOL ** SCIPgetSols(SCIP *scip)
Definition: scip.c:35327
SCIP_Longint SCIPgetNLPs(SCIP *scip)
Definition: scip.c:37045
#define HEUR_USESSUBSCIP
Definition: heur_ofins.c:39
SCIP_RETCODE SCIPtransformProb(SCIP *scip)
Definition: scip.c:12660
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1068
int SCIPgetNReoptRuns(SCIP *scip)
Definition: scip.c:36955
SCIP_Real SCIPgetUpperbound(SCIP *scip)
Definition: scip.c:38120
SCIP_SOL * SCIPgetReoptLastOptSol(SCIP *scip)
Definition: scip.c:14972
public data structures and miscellaneous methods
#define DEFAULT_ADDALLSOLS
Definition: heur_ofins.c:49
#define SCIP_Bool
Definition: def.h:50
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip.c:3970
#define DEFAULT_MINIMPROVE
Definition: heur_ofins.c:48
SCIP_Bool SCIPisParamFixed(SCIP *scip, const char *name)
Definition: scip.c:3678
#define EVENTHDLR_NAME
Definition: heur_ofins.c:56
SCIP_Longint SCIPsolGetNodenum(SCIP_SOL *sol)
Definition: sol.c:2173
SCIP_RETCODE SCIPsetSubscipsOff(SCIP *scip, SCIP_Bool quiet)
Definition: scip.c:4351
#define MAX(x, y)
Definition: tclique_def.h:75
SCIP_Longint SCIPgetMemUsed(SCIP *scip)
Definition: scip.c:40970
#define HEUR_NAME
Definition: heur_ofins.c:31
SCIP_RETCODE SCIPsetPresolving(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:4400
#define SCIP_REAL_MAX
Definition: def.h:125
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip.c:14539
SCIP_Longint SCIPheurGetNBestSolsFound(SCIP_HEUR *heur)
Definition: heur.c:1293
#define SCIPfreeMemory(scip, ptr)
Definition: scip.h:20371
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip.c:692
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip.c:36198
#define DEFAULT_MAXNODES
Definition: heur_ofins.c:42
SCIP_Real SCIPgetPrimalbound(SCIP *scip)
Definition: scip.c:38096
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:278
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:7282
SCIP_BRANCHRULE * SCIPfindBranchrule(SCIP *scip, const char *name)
Definition: scip.c:8405
static SCIP_RETCODE createNewSol(SCIP *scip, SCIP *subscip, SCIP_VAR **subvars, SCIP_HEUR *heur, SCIP_SOL *subsol, SCIP_Bool *success)
Definition: heur_ofins.c:158
#define SCIP_Real
Definition: def.h:124
#define DEFAULT_MAXCHGRATE
Definition: heur_ofins.c:43
#define MIN(x, y)
Definition: memory.c:63
SCIP_RETCODE SCIPchgVarLbGlobal(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound)
Definition: scip.c:20038
#define SCIP_INVALID
Definition: def.h:144
SCIP_RETCODE SCIPsetObjlimit(SCIP *scip, SCIP_Real objlimit)
Definition: scip.c:10231
#define SCIP_Longint
Definition: def.h:109
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:917
SCIP_RETCODE SCIPsetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:34495
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip.c:3907
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:10609
SCIP_Bool SCIPvarIsActive(SCIP_VAR *var)
Definition: var.c:16628
SCIP_RETCODE SCIPgetRealParam(SCIP *scip, const char *name, SCIP_Real *value)
Definition: scip.c:3766
SCIP_NODESEL * SCIPfindNodesel(SCIP *scip, const char *name)
Definition: scip.c:8093
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
Definition: scip.c:41305
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip.c:35377
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:3598
#define DEFAULT_MAXCHANGE
Definition: heur_ofins.c:47
default SCIP plugins
#define DEFAULT_NODESOFS
Definition: heur_ofins.c:51
SCIP_Bool SCIPisReoptEnabled(SCIP *scip)
Definition: scip.c:15567
SCIP_Longint SCIPgetNNodes(SCIP *scip)
Definition: scip.c:36982
static SCIP_RETCODE applyOfins(SCIP *scip, SCIP_HEUR *heur, SCIP_HEURDATA *heurdata, SCIP_RESULT *result, SCIP_Longint nstallnodes, SCIP_Bool *chgcoeffs)
Definition: heur_ofins.c:204
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip.c:36164
SCIP_Longint SCIPgetMemExternEstim(SCIP *scip)
Definition: scip.c:40983
OFINS - Objective Function Induced Neighborhood Search. Primal heuristic for reoptimization.