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