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-2018 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 0
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  SCIPdebugMsg(scip, "interrupt after %" SCIP_LONGINT_FORMAT " LPs\n",SCIPgetNLPs(scip));
100  }
101 
102  return SCIP_OKAY;
103 }
104 
105 /** creates a new solution for the original problem by copying the solution of the subproblem */
106 static
108  SCIP* scip, /**< original SCIP data structure */
109  SCIP* subscip, /**< SCIP structure of the subproblem */
110  SCIP_VAR** subvars, /**< the variables of the subproblem */
111  SCIP_HEUR* heur, /**< RENS heuristic structure */
112  SCIP_SOL* subsol, /**< solution of the subproblem */
113  SCIP_Bool* success /**< used to store whether new solution was found or not */
114  )
115 {
116  SCIP_VAR** vars; /* the original problem's variables */
117  int nvars; /* the original problem's number of variables */
118  SCIP_Real* subsolvals; /* solution values of the subproblem */
119  SCIP_SOL* newsol; /* solution to be created for the original problem */
120 
121  assert(scip != NULL);
122  assert(subscip != NULL);
123  assert(subvars != NULL);
124  assert(subsol != NULL);
125 
126  /* get variables' data */
127  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
128 
129  /* sub-SCIP may have more variables than the number of active (transformed) variables in the main SCIP
130  * since constraint copying may have required the copy of variables that are fixed in the main SCIP
131  */
132  assert(nvars <= SCIPgetNOrigVars(subscip));
133 
134  SCIP_CALL( SCIPallocBufferArray(scip, &subsolvals, nvars) );
135 
136  /* copy the solution */
137  SCIP_CALL( SCIPgetSolVals(subscip, subsol, nvars, subvars, subsolvals) );
138 
139  /* create new solution for the original problem */
140  SCIP_CALL( SCIPcreateSol(scip, &newsol, heur) );
141  SCIP_CALL( SCIPsetSolVals(scip, newsol, nvars, vars, subsolvals) );
142 
143  /* try to add new solution to scip and free it immediately */
144  SCIP_CALL( SCIPtrySolFree(scip, &newsol, TRUE, TRUE, TRUE, TRUE, TRUE, success) );
145 
146  SCIPfreeBufferArray(scip, &subsolvals);
147 
148  return SCIP_OKAY;
149 }
150 
151 /* setup and solve the sub-SCIP */
152 static
154  SCIP* scip, /**< original SCIP data structure */
155  SCIP* subscip, /**< sub-SCIP data structure */
156  SCIP_HEUR* heur, /**< heuristic data structure */
157  SCIP_HEURDATA* heurdata, /**< euristic's private data structure */
158  SCIP_RESULT* result, /**< result data structure */
159  SCIP_Longint nstallnodes, /**< number of stalling nodes for the subproblem */
160  SCIP_Bool* chgcoeffs /**< array of changed coefficients */
161 
162  )
163 {
164  SCIP_HASHMAP* varmapfw;
165  SCIP_VAR** vars;
166  SCIP_VAR** subvars;
167  SCIP_EVENTHDLR* eventhdlr;
168 
169  SCIP_SOL* sol;
170  SCIP_VAR** fixedvars;
171  SCIP_Real* fixedvals;
172  int nfixedvars;
173 
174  int nvars;
175  int nintvars;
176  int i;
177 
178  SCIP_SOL** subsols;
179  int nsubsols = 0;
180 
181  SCIP_Bool success;
182  SCIP_RETCODE retcode;
183  SCIP_STATUS status;
184 
185  assert(scip != NULL);
186  assert(subscip != NULL);
187  assert(heur != NULL);
188  assert(heurdata != NULL);
189  assert(result != NULL);
190  assert(chgcoeffs != NULL);
191 
192  SCIPdebugMsg(scip, "+---+ Start OFINS heuristic +---+\n");
193 
194  /* get variable data */
195  vars = SCIPgetVars(scip);
196  nvars = SCIPgetNVars(scip);
197 
198  /* create the variable mapping hash map */
199  SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), nvars) );
200 
201  /* get optimal solution of the last iteration */
202  sol = SCIPgetReoptLastOptSol(scip);
203 
204  /* if the solution is NULL the last problem was infeasible */
205  if( sol == NULL )
206  return SCIP_OKAY;
207 
208  nintvars = SCIPgetNBinVars(scip) + SCIPgetNIntVars(scip) + SCIPgetNImplVars(scip);
209  SCIP_CALL( SCIPallocBufferArray(scip, &fixedvars, nvars) );
210  SCIP_CALL( SCIPallocBufferArray(scip, &fixedvals, nvars) );
211 
212  /* determine variables to fix in the sub-SCIP */
213  nfixedvars = 0;
214  for( i = 0; i < nintvars; i++ )
215  {
216  if( !chgcoeffs[i] )
217  {
218  fixedvars[nfixedvars] = vars[i];
219  fixedvals[nfixedvars] = SCIPgetSolVal(scip, sol, vars[i]);
220  ++nfixedvars;
221  }
222  }
223 
224  /* create a problem copy as sub SCIP */
225  SCIP_CALL( SCIPcopyLargeNeighborhoodSearch(scip, subscip, varmapfw, "ofins", fixedvars, fixedvals, nfixedvars, FALSE,
226  FALSE, &success, NULL) );
227  assert(success);
228 
229  SCIPfreeBufferArrayNull(scip, &fixedvals);
230  SCIPfreeBufferArrayNull(scip, &fixedvars);
231 
232  /* create event handler for LP events */
233  eventhdlr = NULL;
234  SCIP_CALL( SCIPincludeEventhdlrBasic(subscip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecOfins, NULL) );
235  if( eventhdlr == NULL )
236  {
237  SCIPerrorMessage("event handler for " HEUR_NAME " heuristic not found.\n");
238  return SCIP_PLUGINNOTFOUND;
239  }
240 
241  SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) );
242  for( i = 0; i < nvars; i++ )
243  {
244  subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]);
245  assert(subvars[i] != NULL);
246  }
247 
248  /* free hash map */
249  SCIPhashmapFree(&varmapfw);
250 
251  /* set an objective limit */
252  SCIPdebugMsg(scip, "set objective limit of %g to sub-SCIP\n", SCIPgetUpperbound(scip));
253  SCIP_CALL( SCIPsetObjlimit(subscip, SCIPgetUpperbound(scip)) );
254 
255  SCIPdebugMsg(scip, "OFINS subproblem: %d vars, %d cons\n", SCIPgetNVars(subscip), SCIPgetNConss(subscip));
256 
257  /* do not abort subproblem on CTRL-C */
258  SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
259 
260 #ifdef SCIP_DEBUG
261  /* for debugging, enable full output */
262  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 5) );
263  SCIP_CALL( SCIPsetIntParam(subscip, "display/freq", 100000000) );
264 #else
265  /* disable statistic timing inside sub SCIP and output to console */
266  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
267  SCIP_CALL( SCIPsetBoolParam(subscip, "timing/statistictiming", FALSE) );
268 #endif
269 
270  /* set limits for the subproblem */
271  SCIP_CALL( SCIPcopyLimits(scip, subscip) );
272  heurdata->nodelimit = heurdata->maxnodes;
273  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/stallnodes", nstallnodes) );
274  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", heurdata->maxnodes) );
275 
276  /* forbid recursive call of heuristics and separators solving sub-SCIPs */
277  SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
278 
279  /* disable cutting plane separation */
281 
282  /* disable expensive presolving */
284 
285  /* use best estimate node selection */
286  if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") )
287  {
288  SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) );
289  }
290 
291  /* use inference branching */
292  if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") )
293  {
294  SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) );
295  }
296 
297  /* disable conflict analysis */
298  if( !SCIPisParamFixed(subscip, "conflict/enable") )
299  {
300  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/enable", FALSE) );
301  }
302 
303  /* speed up sub-SCIP by not checking dual LP feasibility */
304  SCIP_CALL( SCIPsetBoolParam(subscip, "lp/checkdualfeas", FALSE) );
305 
306  /* presolve the subproblem */
307  retcode = SCIPpresolve(subscip);
308 
309  /* errors in solving the subproblem should not kill the overall solving process;
310  * hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
311  */
312  if( retcode != SCIP_OKAY )
313  {
314  SCIPwarningMessage(scip, "Error while presolving subproblem in %s heuristic; sub-SCIP terminated with code <%d>\n", HEUR_NAME, retcode);
315 
316  SCIPABORT(); /*lint --e{527}*/
317 
318  /* free */
319  SCIPfreeBufferArray(scip, &subvars);
320  return SCIP_OKAY;
321  }
322 
323  SCIPdebugMsg(scip, "%s presolved subproblem: %d vars, %d cons\n", HEUR_NAME, SCIPgetNVars(subscip), SCIPgetNConss(subscip));
324 
325  assert(eventhdlr != NULL);
326 
327  SCIP_CALL( SCIPtransformProb(subscip) );
328  SCIP_CALL( SCIPcatchEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, NULL) );
329 
330  /* solve the subproblem */
331  SCIPdebugMsg(scip, "solving subproblem: nstallnodes=%" SCIP_LONGINT_FORMAT ", maxnodes=%" SCIP_LONGINT_FORMAT "\n", nstallnodes, heurdata->maxnodes);
332 
333  /* errors in solving the subproblem should not kill the overall solving process;
334  * hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
335  */
336  SCIP_CALL_ABORT( SCIPsolve(subscip) );
337 
338  SCIP_CALL( SCIPdropEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, -1) );
339 
340  /* print solving statistics of subproblem if we are in SCIP's debug mode */
341  SCIPdebug( SCIP_CALL( SCIPprintStatistics(subscip, NULL) ) );
342 
343  status = SCIPgetStatus(subscip);
344 
345  switch (status) {
347  break;
350  {
351  int nsubvars;
352 
353  nsubvars = SCIPgetNOrigVars(subscip);
354 
355  /* transfer the primal ray from the sub-SCIP to the main SCIP */
356  if( SCIPhasPrimalRay(subscip) )
357  {
358  SCIP_SOL* primalray;
359 
360  SCIP_CALL( SCIPcreateSol(scip, &primalray, heur) );
361 
362  /* transform the ray into the space of the source scip */
363  for( i = 0; i < nsubvars; i++ )
364  {
365  SCIP_CALL( SCIPsetSolVal(scip, primalray, vars[SCIPvarGetProbindex(subvars[i])],
366  SCIPgetPrimalRayVal(subscip, subvars[i])) );
367  }
368 
369  SCIPdebug( SCIP_CALL( SCIPprintRay(scip, primalray, 0, FALSE) ); );
370 
371  /* update the primal ray of the source scip */
372  SCIP_CALL( SCIPupdatePrimalRay(scip, primalray) );
373  SCIP_CALL( SCIPfreeSol(scip, &primalray) );
374 
375  *result = SCIP_UNBOUNDED;
376  }
377 
378  break;
379  }
380  default:
381  /* check, whether a solution was found;
382  * due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted
383  */
384  nsubsols = SCIPgetNSols(subscip);
385  subsols = SCIPgetSols(subscip);
386  success = FALSE;
387  for( i = 0; i < nsubsols && (!success || heurdata->addallsols); i++ )
388  {
389  SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) );
390  if( success )
391  *result = SCIP_FOUNDSOL;
392  }
393  break;
394  } /*lint !e788*/
395 
396  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",
397  HEUR_NAME, 0.0, SCIPgetSolvingTime(subscip), SCIPgetNNodes(subscip), success ? SCIPgetPrimalbound(scip) : SCIPinfinity(scip),
398  nsubsols > 0 ? SCIPsolGetNodenum(SCIPgetBestSol(subscip)) : -1 );
399 
400  /* free subproblem */
401  SCIPfreeBufferArray(scip, &subvars);
402 
403  return SCIP_OKAY;
404 }
405 
406 /** main procedure of the OFINS heuristic, creates and solves a sub-SCIP */
407 static
409  SCIP* scip, /**< original SCIP data structure */
410  SCIP_HEUR* heur, /**< heuristic data structure */
411  SCIP_HEURDATA* heurdata, /**< euristic's private data structure */
412  SCIP_RESULT* result, /**< result data structure */
413  SCIP_Longint nstallnodes, /**< number of stalling nodes for the subproblem */
414  SCIP_Bool* chgcoeffs /**< array of changed coefficients */
415  )
416 {
417  SCIP* subscip;
418  SCIP_RETCODE retcode;
419  SCIP_Bool success;
420 
421  assert(scip != NULL);
422  assert(heur != NULL);
423  assert(heurdata != NULL);
424  assert(result != NULL);
425  assert(chgcoeffs != NULL);
426 
427  *result = SCIP_DIDNOTRUN;
428 
429  /* check whether there is enough time and memory left */
430  SCIP_CALL( SCIPcheckCopyLimits(scip, &success) );
431 
432  if( !success )
433  return SCIP_OKAY;
434 
435  *result = SCIP_DIDNOTFIND;
436 
437  /* do not run, if no solution was found */
438  if ( SCIPgetReoptLastOptSol(scip) == NULL )
439  return SCIP_OKAY;
440 
441  /* initialize the subproblem */
442  SCIP_CALL( SCIPcreate(&subscip) );
443 
444  retcode = setupAndSolve(scip, subscip, heur, heurdata, result, nstallnodes, chgcoeffs);
445 
446  SCIP_CALL( SCIPfree(&subscip) );
447 
448  SCIP_CALL( retcode );
449 
450  return SCIP_OKAY;
451 }
452 
453 
454 
455 
456 /*
457  * Callback methods of primal heuristic
458  */
459 
460 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
461 static
462 SCIP_DECL_HEURCOPY(heurCopyOfins)
463 { /*lint --e{715}*/
464  assert(scip != NULL);
465  assert(heur != NULL);
466  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
467 
468  /* call inclusion method of primal heuristic */
470 
471  return SCIP_OKAY;
472 }
473 
474 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
475 static
476 SCIP_DECL_HEURFREE(heurFreeOfins)
477 { /*lint --e{715}*/
478  SCIP_HEURDATA* heurdata;
479 
480  assert(heur != NULL);
481  assert(scip != NULL);
482 
483  /* get heuristic data */
484  heurdata = SCIPheurGetData(heur);
485  assert(heurdata != NULL);
486 
487  /* free heuristic data */
488  SCIPfreeBlockMemory(scip, &heurdata);
489  SCIPheurSetData(heur, NULL);
490 
491  return SCIP_OKAY;
492 }
493 
494 /** execution method of primal heuristic */
495 static
496 SCIP_DECL_HEUREXEC(heurExecOfins)
497 {/*lint --e{715}*/
498  SCIP_HEURDATA* heurdata;
499  SCIP_VAR** vars;
500  SCIP_Bool* chgcoeffs;
501  SCIP_Longint nstallnodes;
502  int nchgcoefs;
503  int nvars;
504  int v;
505 
506  assert( heur != NULL );
507  assert( scip != NULL );
508  assert( result != NULL );
509 
510  *result = SCIP_DELAYED;
511 
512  /* do not call heuristic of node was already detected to be infeasible */
513  if( nodeinfeasible )
514  return SCIP_OKAY;
515 
516  /* get heuristic data */
517  heurdata = SCIPheurGetData(heur);
518  assert( heurdata != NULL );
519 
520  /* only call heuristic, if reoptimization is enabled */
521  if( !SCIPisReoptEnabled(scip) )
522  return SCIP_OKAY;
523 
524  /* only call the heuristic, if we are in run >= 2 */
525  if( SCIPgetNReoptRuns(scip) <= 1 )
526  return SCIP_OKAY;
527 
528  *result = SCIP_DIDNOTRUN;
529 
530  if( SCIPisStopped(scip) )
531  return SCIP_OKAY;
532 
533  /* calculate the maximal number of branching nodes until heuristic is aborted */
534  nstallnodes = (SCIP_Longint)(heurdata->nodesquot * SCIPgetNNodes(scip));
535 
536  /* reward OFINS if it succeeded often */
537  nstallnodes = (SCIP_Longint)(nstallnodes * 3.0 * (SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur) + 1.0));
538  nstallnodes -= 100 * SCIPheurGetNCalls(heur); /* count the setup costs for the sub-SCIP as 100 nodes */
539  nstallnodes += heurdata->nodesofs;
540 
541  /* determine the node limit for the current process */
542  nstallnodes = MIN(nstallnodes, heurdata->maxnodes);
543 
544  /* check whether we have enough nodes left to call subproblem solving */
545  if( nstallnodes < heurdata->minnodes )
546  {
547  SCIPdebugMsg(scip, "skipping OFINS: nstallnodes=%" SCIP_LONGINT_FORMAT ", minnodes=%" SCIP_LONGINT_FORMAT "\n", nstallnodes, heurdata->minnodes);
548  return SCIP_OKAY;
549  }
550 
551  /* get variable data and check which coefficient has changed */
552  vars = SCIPgetVars(scip);
553  nvars = SCIPgetNBinVars(scip) + SCIPgetNIntVars(scip) + SCIPgetNImplVars(scip);
554  nchgcoefs = 0;
555 
556  SCIP_CALL( SCIPallocBufferArray(scip, &chgcoeffs, nvars) );
557 
558  for( v = 0; v < nvars; v++ )
559  {
560  SCIP_Real newcoef;
561  SCIP_Real oldcoef;
562  SCIP_Real newcoefabs;
563  SCIP_Real oldcoefabs;
564  SCIP_Real frac;
565 
566  /* we only want to count variables that are unfixed after the presolving */
567  assert(SCIPvarGetStatus(vars[v]) != SCIP_VARSTATUS_ORIGINAL);
568  assert(SCIPvarIsActive(vars[v]));
569 
570  SCIP_CALL( SCIPgetReoptOldObjCoef(scip, vars[v], SCIPgetNReoptRuns(scip), &newcoef) );
571  SCIP_CALL( SCIPgetReoptOldObjCoef(scip, vars[v], SCIPgetNReoptRuns(scip)-1, &oldcoef) );
572  newcoefabs = REALABS(newcoef);
573  oldcoefabs = REALABS(oldcoef);
574 
575  /* if both coefficients are zero nothing has changed */
576  if( SCIPisZero(scip, newcoef) && SCIPisZero(scip, oldcoef) )
577  {
578  frac = 0;
579  }
580  /* if exactly one coefficient is zero, the other need to be close to zero */
581  else if( SCIPisZero(scip, newcoef) || SCIPisZero(scip, oldcoef) )
582  {
583  assert(SCIPisZero(scip, newcoef) != SCIPisZero(scip, oldcoef));
584  if( !SCIPisZero(scip, newcoef) )
585  frac = MIN(1, newcoefabs);
586  else
587  frac = MIN(1, oldcoefabs);
588  }
589  /* if both coefficients have the same sign we calculate the quotient
590  * MIN(newcoefabs, oldcoefabs)/MAX(newcoefabs, oldcoefabs)
591  */
592  else if( SCIPisPositive(scip, newcoef) == SCIPisPositive(scip, oldcoef) )
593  {
594  frac = 1.0 - MIN(newcoefabs, oldcoefabs)/MAX(newcoefabs, oldcoefabs);
595  }
596  /* if both coefficients have a different sign, we set frac = 1 */
597  else
598  {
599  assert((SCIPisPositive(scip, newcoef) && SCIPisNegative(scip, oldcoef))
600  || (SCIPisNegative(scip, newcoef) && SCIPisPositive(scip, oldcoef)));
601 
602  frac = 1;
603  }
604 
605  if( frac > heurdata->maxchange )
606  {
607  chgcoeffs[v] = TRUE;
608  nchgcoefs++;
609  }
610  else
611  chgcoeffs[v] = FALSE;
612  }
613 
614  SCIPdebugMsg(scip, "%d (rate %.4f) changed coefficients\n", nchgcoefs, nchgcoefs/((SCIP_Real)nvars));
615 
616  /* we only want to run the heuristic, if there at least 3 changed coefficients.
617  * if the number of changed coefficients is 2 the trivialnegation heuristic will construct an
618  * optimal solution without solving a MIP.
619  */
620  if( nchgcoefs < 3 )
621  goto TERMINATE;
622 
623  /* run the heuristic, if not too many coefficients have changed */
624  if( nchgcoefs/((SCIP_Real)nvars) > heurdata->maxchangerate )
625  goto TERMINATE;
626 
627  SCIP_CALL( applyOfins(scip, heur, heurdata, result, nstallnodes, chgcoeffs) );
628 
629  TERMINATE:
630  SCIPfreeBufferArray(scip, &chgcoeffs);
631 
632  return SCIP_OKAY;
633 }
634 
635 
636 /*
637  * primal heuristic specific interface methods
638  */
639 
640 /** creates the ofins primal heuristic and includes it in SCIP */
642  SCIP* scip /**< SCIP data structure */
643  )
644 {
645  SCIP_HEURDATA* heurdata;
646  SCIP_HEUR* heur;
647 
648  /* create ofins primal heuristic data */
649  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
650  assert(heurdata != NULL);
651 
652  /* include primal heuristic */
653  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
655  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecOfins, heurdata) );
656 
657  assert(heur != NULL);
658 
659  /* set non fundamental callbacks via setter functions */
660  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyOfins) );
661  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeOfins) );
662 
663  /* add ofins primal heuristic parameters */
664 
665  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/" HEUR_NAME "/maxnodes",
666  "maximum number of nodes to regard in the subproblem",
667  &heurdata->maxnodes, TRUE, DEFAULT_MAXNODES, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
668 
669  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/" HEUR_NAME "/minnodes",
670  "minimum number of nodes required to start the subproblem",
671  &heurdata->minnodes, TRUE, DEFAULT_MINNODES, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
672 
673  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/maxchangerate",
674  "maximal rate of changed coefficients",
675  &heurdata->maxchangerate, FALSE, DEFAULT_MAXCHGRATE, 0.0, 1.0, NULL, NULL) );
676 
677  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/maxchange",
678  "maximal rate of change per coefficient to get fixed",
679  &heurdata->maxchange, FALSE, DEFAULT_MAXCHANGE, 0.0, 1.0, NULL, NULL) );
680 
681  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/copycuts",
682  "should all active cuts from cutpool be copied to constraints in subproblem?",
683  &heurdata->copycuts, TRUE, DEFAULT_COPYCUTS, NULL, NULL) );
684 
685  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/addallsols",
686  "should all subproblem solutions be added to the original SCIP?",
687  &heurdata->addallsols, TRUE, DEFAULT_ADDALLSOLS, NULL, NULL) );
688 
689  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/" HEUR_NAME "/nodesofs",
690  "number of nodes added to the contingent of the total nodes",
691  &heurdata->nodesofs, FALSE, DEFAULT_NODESOFS, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
692 
693  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/nodesquot",
694  "contingent of sub problem nodes in relation to the number of nodes of the original problem",
695  &heurdata->nodesquot, FALSE, DEFAULT_NODESQUOT, 0.0, 1.0, NULL, NULL) );
696 
697  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/minimprove",
698  "factor by which RENS should at least improve the incumbent",
699  &heurdata->minimprove, TRUE, DEFAULT_MINIMPROVE, 0.0, 1.0, NULL, NULL) );
700 
701  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/lplimfac",
702  "factor by which the limit on the number of LP depends on the node limit",
703  &heurdata->lplimfac, TRUE, DEFAULT_LPLIMFAC, 1.0, SCIP_REAL_MAX, NULL, NULL) );
704 
705  return SCIP_OKAY;
706 }
enum SCIP_Result SCIP_RESULT
Definition: type_result.h:52
int SCIPgetNIntVars(SCIP *scip)
Definition: scip.c:11902
SCIP_Real SCIPgetSolvingTime(SCIP *scip)
Definition: scip.c:46306
#define SCIP_EVENTTYPE_LPSOLVED
Definition: type_event.h:84
SCIP_RETCODE SCIPsetSeparating(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:5158
static SCIP_DECL_HEURCOPY(heurCopyOfins)
Definition: heur_ofins.c:463
SCIP_Real SCIPgetPrimalbound(SCIP *scip)
Definition: scip.c:43402
SCIP_Longint SCIPheurGetNBestSolsFound(SCIP_HEUR *heur)
Definition: heur.c:1344
static SCIP_DECL_HEURFREE(heurFreeOfins)
Definition: heur_ofins.c:477
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
Definition: scip.c:47088
int SCIPgetNOrigVars(SCIP *scip)
Definition: scip.c:12252
static SCIP_DECL_EVENTEXEC(eventExecOfins)
Definition: heur_ofins.c:83
SCIP_RETCODE SCIPincludeEventhdlrBasic(SCIP *scip, SCIP_EVENTHDLR **eventhdlrptr, const char *name, const char *desc, SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip.c:8611
#define DEFAULT_COPYCUTS
Definition: heur_ofins.c:44
#define DEFAULT_NODESQUOT
Definition: heur_ofins.c:52
SCIP_RETCODE SCIPgetVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition: scip.c:11686
#define DEFAULT_LPLIMFAC
Definition: heur_ofins.c:53
SCIP_SOL ** SCIPgetSols(SCIP *scip)
Definition: scip.c:39832
#define FALSE
Definition: def.h:64
SCIP_RETCODE SCIPincludeHeurOfins(SCIP *scip)
Definition: heur_ofins.c:642
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition: misc.c:2793
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:278
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:4293
SCIP_RETCODE SCIPcopyLimits(SCIP *sourcescip, SCIP *targetscip)
Definition: scip.c:4192
static SCIP_RETCODE setupAndSolve(SCIP *scip, SCIP *subscip, SCIP_HEUR *heur, SCIP_HEURDATA *heurdata, SCIP_RESULT *result, SCIP_Longint nstallnodes, SCIP_Bool *chgcoeffs)
Definition: heur_ofins.c:154
SCIP_Real SCIPinfinity(SCIP *scip)
Definition: scip.c:47028
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
Definition: scip.c:47100
static SCIP_DECL_HEUREXEC(heurExecOfins)
Definition: heur_ofins.c:497
#define TRUE
Definition: def.h:63
#define SCIPdebug(x)
Definition: pub_message.h:74
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
SCIP_RETCODE SCIPsetPresolving(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:5132
SCIP_BRANCHRULE * SCIPfindBranchrule(SCIP *scip, const char *name)
Definition: scip.c:9269
int SCIPvarGetProbindex(SCIP_VAR *var)
Definition: var.c:16969
#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
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip.h:22602
SCIP_RETCODE SCIPincludeHeurBasic(SCIP *scip, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, SCIP_HEURTIMING timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
Definition: scip.c:8084
#define HEUR_FREQ
Definition: heur_ofins.c:35
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:2931
#define SCIP_LONGINT_MAX
Definition: def.h:135
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip.h:22632
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip.c:748
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1119
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip.h:22585
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip.c:1267
#define SCIPdebugMsg
Definition: scip.h:455
#define HEUR_TIMING
Definition: heur_ofins.c:38
SCIP_RETCODE SCIPprintStatistics(SCIP *scip, FILE *file)
Definition: scip.c:45651
#define HEUR_MAXDEPTH
Definition: heur_ofins.c:37
#define EVENTHDLR_DESC
Definition: heur_ofins.c:57
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip.c:16115
#define HEUR_DESC
Definition: heur_ofins.c:32
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1198
#define SCIPerrorMessage
Definition: pub_message.h:45
SCIP_Bool SCIPisParamFixed(SCIP *scip, const char *name)
Definition: scip.c:4401
#define DEFAULT_MINNODES
Definition: heur_ofins.c:50
#define HEUR_FREQOFS
Definition: heur_ofins.c:36
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:8145
SCIP_RETCODE SCIPgetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:38948
#define SCIPfreeBufferArrayNull(scip, ptr)
Definition: scip.h:22633
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip.c:4630
SCIP_STATUS SCIPgetStatus(SCIP *scip)
Definition: scip.c:928
SCIP_RETCODE SCIPpresolve(SCIP *scip)
Definition: scip.c:15954
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip.c:46731
SCIP_Bool SCIPisReoptEnabled(SCIP *scip)
Definition: scip.c:17363
struct SCIP_EventData SCIP_EVENTDATA
Definition: type_event.h:155
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition: misc.c:2826
#define REALABS(x)
Definition: def.h:173
#define SCIP_CALL(x)
Definition: def.h:350
SCIP_SOL * SCIPgetReoptLastOptSol(SCIP *scip)
Definition: scip.c:16692
#define SCIPstatisticPrintf
Definition: pub_message.h:107
SCIP_Longint SCIPheurGetNCalls(SCIP_HEUR *heur)
Definition: heur.c:1324
#define HEUR_USESSUBSCIP
Definition: heur_ofins.c:39
SCIP_Bool SCIPhasPrimalRay(SCIP *scip)
Definition: scip.c:41075
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip.h:22620
SCIP_RETCODE SCIPsetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real val)
Definition: scip.c:38771
public data structures and miscellaneous methods
#define DEFAULT_ADDALLSOLS
Definition: heur_ofins.c:49
#define SCIP_Bool
Definition: def.h:61
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip.c:41158
int SCIPgetNImplVars(SCIP *scip)
Definition: scip.c:11947
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:959
SCIP_Longint SCIPsolGetNodenum(SCIP_SOL *sol)
Definition: sol.c:2528
#define DEFAULT_MINIMPROVE
Definition: heur_ofins.c:48
SCIP_RETCODE SCIPprintRay(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
Definition: scip.c:39747
enum SCIP_Status SCIP_STATUS
Definition: type_stat.h:57
SCIP_RETCODE SCIPsetObjlimit(SCIP *scip, SCIP_Real objlimit)
Definition: scip.c:11246
#define EVENTHDLR_NAME
Definition: heur_ofins.c:56
#define MAX(x, y)
Definition: tclique_def.h:75
SCIP_RETCODE SCIPtrySolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
Definition: scip.c:40794
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip.c:4688
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip.c:41192
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip.c:38535
#define HEUR_NAME
Definition: heur_ofins.c:31
int SCIPgetNSols(SCIP *scip)
Definition: scip.c:39783
int SCIPgetNBinVars(SCIP *scip)
Definition: scip.c:11857
int SCIPgetNVars(SCIP *scip)
Definition: scip.c:11812
#define SCIP_REAL_MAX
Definition: def.h:150
SCIP_RETCODE SCIPgetReoptOldObjCoef(SCIP *scip, SCIP_VAR *var, int run, SCIP_Real *objcoef)
Definition: scip.c:16719
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip.c:39882
int SCIPgetNReoptRuns(SCIP *scip)
Definition: scip.c:42077
#define DEFAULT_MAXNODES
Definition: heur_ofins.c:42
int SCIPgetNConss(SCIP *scip)
Definition: scip.c:12862
SCIP_NODESEL * SCIPfindNodesel(SCIP *scip, const char *name)
Definition: scip.c:8957
SCIP_RETCODE SCIPcopyLargeNeighborhoodSearch(SCIP *sourcescip, SCIP *subscip, SCIP_HASHMAP *varmap, const char *suffix, SCIP_VAR **fixedvars, SCIP_Real *fixedvals, int nfixedvars, SCIP_Bool uselprows, SCIP_Bool copycuts, SCIP_Bool *success, SCIP_Bool *valid)
Definition: heuristics.c:903
SCIP_VAR ** SCIPgetVars(SCIP *scip)
Definition: scip.c:11767
SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
Definition: var.c:16781
static SCIP_RETCODE createNewSol(SCIP *scip, SCIP *subscip, SCIP_VAR **subvars, SCIP_HEUR *heur, SCIP_SOL *subsol, SCIP_Bool *success)
Definition: heur_ofins.c:108
SCIP_RETCODE SCIPupdatePrimalRay(SCIP *scip, SCIP_SOL *primalray)
Definition: scip.c:41120
#define SCIP_Real
Definition: def.h:149
SCIP_Bool SCIPisStopped(SCIP *scip)
Definition: scip.c:1145
#define DEFAULT_MAXCHGRATE
Definition: heur_ofins.c:43
#define SCIP_Longint
Definition: def.h:134
SCIP_RETCODE SCIPcheckCopyLimits(SCIP *sourcescip, SCIP_Bool *success)
Definition: scip.c:4156
SCIP_RETCODE SCIPsetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:38813
SCIP_RETCODE SCIPtransformProb(SCIP *scip)
Definition: scip.c:13935
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
Definition: scip.c:47076
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:8129
SCIP_RETCODE SCIPinterruptSolve(SCIP *scip)
Definition: scip.c:17324
SCIP_Real SCIPgetUpperbound(SCIP *scip)
Definition: scip.c:43426
#define SCIP_CALL_ABORT(x)
Definition: def.h:329
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1109
SCIP_Longint SCIPgetNNodes(SCIP *scip)
Definition: scip.c:42133
SCIP_Longint SCIPgetNLPs(SCIP *scip)
Definition: scip.c:42314
#define SCIPABORT()
Definition: def.h:322
#define DEFAULT_MAXCHANGE
Definition: heur_ofins.c:47
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:38911
default SCIP plugins
#define DEFAULT_NODESOFS
Definition: heur_ofins.c:51
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:4321
SCIP_RETCODE SCIPsetSubscipsOff(SCIP *scip, SCIP_Bool quiet)
Definition: scip.c:5083
SCIP_RETCODE SCIPsetLongintParam(SCIP *scip, const char *name, SCIP_Longint value)
Definition: scip.c:4746
SCIP_Real SCIPgetPrimalRayVal(SCIP *scip, SCIP_VAR *var)
Definition: scip.c:41093
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:409
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:4239
SCIP_Bool SCIPvarIsActive(SCIP_VAR *var)
Definition: var.c:16949
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip.c:780
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:37878
OFINS - Objective Function Induced Neighborhood Search - a primal heuristic for reoptimization.