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-2017 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 /** main procedure of the OFINS heuristic, creates and solves a sub-SCIP */
152 static
154  SCIP* scip, /**< original SCIP data structure */
155  SCIP_HEUR* heur, /**< heuristic data structure */
156  SCIP_HEURDATA* heurdata, /**< euristic's private data structure */
157  SCIP_RESULT* result, /**< result data structure */
158  SCIP_Longint nstallnodes, /**< number of stalling nodes for the subproblem */
159  SCIP_Bool* chgcoeffs /**< array of changed coefficients */
160  )
161 {
162  SCIP* subscip; /* the subproblem created by OFINS */
163  SCIP_HASHMAP* varmapfw; /* mapping of SCIP variables to sub-SCIP variables */
164  SCIP_VAR** vars; /* source problem's variables */
165  SCIP_VAR** subvars; /* subproblem's variables */
166  SCIP_EVENTHDLR* eventhdlr; /* event handler for LP events */
167 
168  SCIP_SOL* sol;
169  SCIP_VAR** fixedvars;
170  SCIP_Real* fixedvals;
171  int nfixedvars;
172 
173  int nvars; /* number of source problem's variables */
174  int nintvars;
175  int i;
176 
177  SCIP_SOL** subsols;
178  int nsubsols = 0;
179 
180  SCIP_Bool success;
181  SCIP_RETCODE retcode;
182  SCIP_STATUS status;
183 
184  assert(scip != NULL);
185  assert(heur != NULL);
186  assert(heurdata != NULL);
187  assert(result != NULL);
188  assert(chgcoeffs != NULL);
189 
190  *result = SCIP_DIDNOTRUN;
191 
192  SCIPdebugMsg(scip, "+---+ Start OFINS heuristic +---+\n");
193 
194  /* check whether there is enough time and memory left */
195  SCIP_CALL( SCIPcheckCopyLimits(scip, &success) );
196 
197  if( !success )
198  return SCIP_OKAY;
199 
200  *result = SCIP_DIDNOTFIND;
201 
202  /* do not run, if no solution was found */
203  if ( SCIPgetReoptLastOptSol(scip) == NULL )
204  return SCIP_OKAY;
205 
206  /* get variable data */
207  vars = SCIPgetVars(scip);
208  nvars = SCIPgetNVars(scip);
209 
210  /* initialize the subproblem */
211  SCIP_CALL( SCIPcreate(&subscip) );
212 
213  /* create the variable mapping hash map */
214  SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), nvars) );
215 
216  /* get optimal solution of the last iteration */
217  sol = SCIPgetReoptLastOptSol(scip);
218 
219  /* if the solution is NULL the last problem was infeasible */
220  if( sol == NULL )
221  return SCIP_OKAY;
222 
223  nintvars = SCIPgetNBinVars(scip) + SCIPgetNIntVars(scip) + SCIPgetNImplVars(scip);
224  SCIP_CALL( SCIPallocBufferArray(scip, &fixedvars, nvars) );
225  SCIP_CALL( SCIPallocBufferArray(scip, &fixedvals, nvars) );
226 
227  /* determine variables to fix in the sub-SCIP */
228  nfixedvars = 0;
229  for( i = 0; i < nintvars; i++ )
230  {
231  if( !chgcoeffs[i] )
232  {
233  fixedvars[nfixedvars] = vars[i];
234  fixedvals[nfixedvars] = SCIPgetSolVal(scip, sol, vars[i]);
235  ++nfixedvars;
236  }
237  }
238 
239  /* create a problem copy as sub SCIP */
240  SCIP_CALL( SCIPcopyLargeNeighborhoodSearch(scip, subscip, varmapfw, "ofins", fixedvars, fixedvals, nfixedvars, FALSE,
241  FALSE, &success, NULL) );
242  assert(success);
243 
244  SCIPfreeBufferArrayNull(scip, &fixedvals);
245  SCIPfreeBufferArrayNull(scip, &fixedvars);
246 
247  /* create event handler for LP events */
248  eventhdlr = NULL;
249  SCIP_CALL( SCIPincludeEventhdlrBasic(subscip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecOfins, NULL) );
250  if( eventhdlr == NULL )
251  {
252  SCIPerrorMessage("event handler for " HEUR_NAME " heuristic not found.\n");
253  return SCIP_PLUGINNOTFOUND;
254  }
255 
256  SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) );
257  for( i = 0; i < nvars; i++ )
258  {
259  subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]);
260  assert(subvars[i] != NULL);
261  }
262 
263  /* free hash map */
264  SCIPhashmapFree(&varmapfw);
265 
266  /* set an objective limit */
267  SCIPdebugMsg(scip, "set objective limit of %g to sub-SCIP\n", SCIPgetUpperbound(scip));
268  SCIP_CALL( SCIPsetObjlimit(subscip, SCIPgetUpperbound(scip)) );
269 
270  SCIPdebugMsg(scip, "OFINS subproblem: %d vars, %d cons\n", SCIPgetNVars(subscip), SCIPgetNConss(subscip));
271 
272  /* do not abort subproblem on CTRL-C */
273  SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
274 
275 #ifdef SCIP_DEBUG
276  /* for debugging, enable full output */
277  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 5) );
278  SCIP_CALL( SCIPsetIntParam(subscip, "display/freq", 100000000) );
279 #else
280  /* disable statistic timing inside sub SCIP and output to console */
281  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
282  SCIP_CALL( SCIPsetBoolParam(subscip, "timing/statistictiming", FALSE) );
283 #endif
284 
285  /* set limits for the subproblem */
286  SCIP_CALL( SCIPcopyLimits(scip, subscip) );
287  heurdata->nodelimit = heurdata->maxnodes;
288  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/stallnodes", nstallnodes) );
289  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", heurdata->maxnodes) );
290 
291  /* forbid recursive call of heuristics and separators solving sub-SCIPs */
292  SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
293 
294  /* disable cutting plane separation */
296 
297  /* disable expensive presolving */
299 
300  /* use best estimate node selection */
301  if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") )
302  {
303  SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) );
304  }
305 
306  /* use inference branching */
307  if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") )
308  {
309  SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) );
310  }
311 
312  /* disable conflict analysis */
313  if( !SCIPisParamFixed(subscip, "conflict/enable") )
314  {
315  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/enable", FALSE) );
316  }
317 
318  /* speed up sub-SCIP by not checking dual LP feasibility */
319  SCIP_CALL( SCIPsetBoolParam(subscip, "lp/checkdualfeas", FALSE) );
320 
321  /* presolve the subproblem */
322  retcode = SCIPpresolve(subscip);
323 
324  /* errors in solving the subproblem should not kill the overall solving process;
325  * hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
326  */
327  if( retcode != SCIP_OKAY )
328  {
329  SCIPwarningMessage(scip, "Error while presolving subproblem in %s heuristic; sub-SCIP terminated with code <%d>\n", HEUR_NAME, retcode);
330 
331  SCIPABORT(); /*lint --e{527}*/
332 
333  /* free */
334  SCIPfreeBufferArray(scip, &subvars);
335  SCIP_CALL( SCIPfree(&subscip) );
336  return SCIP_OKAY;
337  }
338 
339  SCIPdebugMsg(scip, "%s presolved subproblem: %d vars, %d cons\n", HEUR_NAME, SCIPgetNVars(subscip), SCIPgetNConss(subscip));
340 
341  assert(eventhdlr != NULL);
342 
343  SCIP_CALL( SCIPtransformProb(subscip) );
344  SCIP_CALL( SCIPcatchEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, NULL) );
345 
346  /* solve the subproblem */
347  SCIPdebugMsg(scip, "solving subproblem: nstallnodes=%" SCIP_LONGINT_FORMAT ", maxnodes=%" SCIP_LONGINT_FORMAT "\n", nstallnodes, heurdata->maxnodes);
348 
349  /* errors in solving the subproblem should not kill the overall solving process;
350  * hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
351  */
352  SCIP_CALL_ABORT( SCIPsolve(subscip) );
353 
354  SCIP_CALL( SCIPdropEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, -1) );
355 
356  /* print solving statistics of subproblem if we are in SCIP's debug mode */
358 
359  status = SCIPgetStatus(subscip);
360 
361  switch (status) {
363  break;
366  {
367  int nsubvars;
368 
369  nsubvars = SCIPgetNOrigVars(subscip);
370 
371  /* transfer the primal ray from the sub-SCIP to the main SCIP */
372  if( SCIPhasPrimalRay(subscip) )
373  {
374  SCIP_SOL* primalray;
375 
376  SCIP_CALL( SCIPcreateSol(scip, &primalray, heur) );
377 
378  /* transform the ray into the space of the source scip */
379  for( i = 0; i < nsubvars; i++ )
380  {
381  SCIP_CALL( SCIPsetSolVal(scip, primalray, vars[SCIPvarGetProbindex(subvars[i])],
382  SCIPgetPrimalRayVal(subscip, subvars[i])) );
383  }
384 
385  SCIPdebug( SCIP_CALL( SCIPprintRay(scip, primalray, 0, FALSE) ); );
386 
387  /* update the primal ray of the source scip */
388  SCIP_CALL( SCIPupdatePrimalRay(scip, primalray) );
389  SCIP_CALL( SCIPfreeSol(scip, &primalray) );
390 
391  *result = SCIP_UNBOUNDED;
392  }
393 
394  break;
395  }
396  default:
397  /* check, whether a solution was found;
398  * due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted
399  */
400  nsubsols = SCIPgetNSols(subscip);
401  subsols = SCIPgetSols(subscip);
402  success = FALSE;
403  for( i = 0; i < nsubsols && (!success || heurdata->addallsols); i++ )
404  {
405  SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) );
406  if( success )
407  *result = SCIP_FOUNDSOL;
408  }
409  break;
410  } /*lint !e788*/
411 
412  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",
413  HEUR_NAME, 0.0, SCIPgetSolvingTime(subscip), SCIPgetNNodes(subscip), success ? SCIPgetPrimalbound(scip) : SCIPinfinity(scip),
414  nsubsols > 0 ? SCIPsolGetNodenum(SCIPgetBestSol(subscip)) : -1 );
415 
416  /* free subproblem */
417  SCIPfreeBufferArray(scip, &subvars);
418  SCIP_CALL( SCIPfree(&subscip) );
419 
420  return SCIP_OKAY;
421 }
422 
423 
424 
425 
426 /*
427  * Callback methods of primal heuristic
428  */
429 
430 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
431 static
432 SCIP_DECL_HEURCOPY(heurCopyOfins)
433 { /*lint --e{715}*/
434  assert(scip != NULL);
435  assert(heur != NULL);
436  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
437 
438  /* call inclusion method of primal heuristic */
440 
441  return SCIP_OKAY;
442 }
443 
444 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
445 static
446 SCIP_DECL_HEURFREE(heurFreeOfins)
447 { /*lint --e{715}*/
448  SCIP_HEURDATA* heurdata;
449 
450  assert(heur != NULL);
451  assert(scip != NULL);
452 
453  /* get heuristic data */
454  heurdata = SCIPheurGetData(heur);
455  assert(heurdata != NULL);
456 
457  /* free heuristic data */
458  SCIPfreeBlockMemory(scip, &heurdata);
459  SCIPheurSetData(heur, NULL);
460 
461  return SCIP_OKAY;
462 }
463 
464 /** execution method of primal heuristic */
465 static
466 SCIP_DECL_HEUREXEC(heurExecOfins)
467 {/*lint --e{715}*/
468  SCIP_HEURDATA* heurdata;
469  SCIP_VAR** vars;
470  SCIP_Bool* chgcoeffs;
471  SCIP_Longint nstallnodes;
472  int nchgcoefs;
473  int nvars;
474  int v;
475 
476  assert( heur != NULL );
477  assert( scip != NULL );
478  assert( result != NULL );
479 
480  *result = SCIP_DELAYED;
481 
482  /* do not call heuristic of node was already detected to be infeasible */
483  if( nodeinfeasible )
484  return SCIP_OKAY;
485 
486  /* get heuristic data */
487  heurdata = SCIPheurGetData(heur);
488  assert( heurdata != NULL );
489 
490  /* only call heuristic, if reoptimization is enabled */
491  if( !SCIPisReoptEnabled(scip) )
492  return SCIP_OKAY;
493 
494  /* only call the heuristic, if we are in run >= 2 */
495  if( SCIPgetNReoptRuns(scip) <= 1 )
496  return SCIP_OKAY;
497 
498  *result = SCIP_DIDNOTRUN;
499 
500  if( SCIPisStopped(scip) )
501  return SCIP_OKAY;
502 
503  /* calculate the maximal number of branching nodes until heuristic is aborted */
504  nstallnodes = (SCIP_Longint)(heurdata->nodesquot * SCIPgetNNodes(scip));
505 
506  /* reward OFINS if it succeeded often */
507  nstallnodes = (SCIP_Longint)(nstallnodes * 3.0 * (SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur) + 1.0));
508  nstallnodes -= 100 * SCIPheurGetNCalls(heur); /* count the setup costs for the sub-SCIP as 100 nodes */
509  nstallnodes += heurdata->nodesofs;
510 
511  /* determine the node limit for the current process */
512  nstallnodes = MIN(nstallnodes, heurdata->maxnodes);
513 
514  /* check whether we have enough nodes left to call subproblem solving */
515  if( nstallnodes < heurdata->minnodes )
516  {
517  SCIPdebugMsg(scip, "skipping OFINS: nstallnodes=%" SCIP_LONGINT_FORMAT ", minnodes=%" SCIP_LONGINT_FORMAT "\n", nstallnodes, heurdata->minnodes);
518  return SCIP_OKAY;
519  }
520 
521  /* get variable data and check which coefficient has changed */
522  vars = SCIPgetVars(scip);
523  nvars = SCIPgetNBinVars(scip) + SCIPgetNIntVars(scip) + SCIPgetNImplVars(scip);
524  nchgcoefs = 0;
525 
526  SCIP_CALL( SCIPallocBufferArray(scip, &chgcoeffs, nvars) );
527 
528  for( v = 0; v < nvars; v++ )
529  {
530  SCIP_Real newcoef;
531  SCIP_Real oldcoef;
532  SCIP_Real newcoefabs;
533  SCIP_Real oldcoefabs;
534  SCIP_Real frac;
535 
536  /* we only want to count variables that are unfixed after the presolving */
537  assert(SCIPvarGetStatus(vars[v]) != SCIP_VARSTATUS_ORIGINAL);
538  assert(SCIPvarIsActive(vars[v]));
539 
540  SCIP_CALL( SCIPgetReoptOldObjCoef(scip, vars[v], SCIPgetNReoptRuns(scip), &newcoef) );
541  SCIP_CALL( SCIPgetReoptOldObjCoef(scip, vars[v], SCIPgetNReoptRuns(scip)-1, &oldcoef) );
542  newcoefabs = REALABS(newcoef);
543  oldcoefabs = REALABS(oldcoef);
544 
545  /* if both coefficients are zero nothing has changed */
546  if( SCIPisZero(scip, newcoef) && SCIPisZero(scip, oldcoef) )
547  {
548  frac = 0;
549  }
550  /* if exactly one coefficient is zero, the other need to be close to zero */
551  else if( SCIPisZero(scip, newcoef) || SCIPisZero(scip, oldcoef) )
552  {
553  assert(SCIPisZero(scip, newcoef) != SCIPisZero(scip, oldcoef));
554  if( !SCIPisZero(scip, newcoef) )
555  frac = MIN(1, newcoefabs);
556  else
557  frac = MIN(1, oldcoefabs);
558  }
559  /* if both coefficients have the same sign we calculate the quotient
560  * MIN(newcoefabs, oldcoefabs)/MAX(newcoefabs, oldcoefabs)
561  */
562  else if( SCIPisPositive(scip, newcoef) == SCIPisPositive(scip, oldcoef) )
563  {
564  frac = 1.0 - MIN(newcoefabs, oldcoefabs)/MAX(newcoefabs, oldcoefabs);
565  }
566  /* if both coefficients have a different sign, we set frac = 1 */
567  else
568  {
569  assert((SCIPisPositive(scip, newcoef) && SCIPisNegative(scip, oldcoef))
570  || (SCIPisNegative(scip, newcoef) && SCIPisPositive(scip, oldcoef)));
571 
572  frac = 1;
573  }
574 
575  if( frac > heurdata->maxchange )
576  {
577  chgcoeffs[v] = TRUE;
578  nchgcoefs++;
579  }
580  else
581  chgcoeffs[v] = FALSE;
582  }
583 
584  SCIPdebugMsg(scip, "%d (rate %.4f) changed coefficients\n", nchgcoefs, nchgcoefs/((SCIP_Real)nvars));
585 
586  /* we only want to run the heuristic, if there at least 3 changed coefficients.
587  * if the number of changed coefficients is 2 the trivialnegation heuristic will construct an
588  * optimal solution without solving a MIP.
589  */
590  if( nchgcoefs < 3 )
591  goto TERMINATE;
592 
593  /* run the heuristic, if not too many coefficients have changed */
594  if( nchgcoefs/((SCIP_Real)nvars) > heurdata->maxchangerate )
595  goto TERMINATE;
596 
597  SCIP_CALL( applyOfins(scip, heur, heurdata, result, nstallnodes, chgcoeffs) );
598 
599  TERMINATE:
600  SCIPfreeBufferArray(scip, &chgcoeffs);
601 
602  return SCIP_OKAY;
603 }
604 
605 
606 /*
607  * primal heuristic specific interface methods
608  */
609 
610 /** creates the ofins primal heuristic and includes it in SCIP */
612  SCIP* scip /**< SCIP data structure */
613  )
614 {
615  SCIP_HEURDATA* heurdata;
616  SCIP_HEUR* heur;
617 
618  /* create ofins primal heuristic data */
619  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
620  assert(heurdata != NULL);
621 
622  /* include primal heuristic */
623  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
625  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecOfins, heurdata) );
626 
627  assert(heur != NULL);
628 
629  /* set non fundamental callbacks via setter functions */
630  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyOfins) );
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:52
int SCIPgetNIntVars(SCIP *scip)
Definition: scip.c:11721
SCIP_Real SCIPgetSolvingTime(SCIP *scip)
Definition: scip.c:45137
#define SCIP_EVENTTYPE_LPSOLVED
Definition: type_event.h:84
SCIP_RETCODE SCIPsetSeparating(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:5095
static SCIP_DECL_HEURCOPY(heurCopyOfins)
Definition: heur_ofins.c:433
SCIP_Real SCIPgetPrimalbound(SCIP *scip)
Definition: scip.c:42448
SCIP_Longint SCIPheurGetNBestSolsFound(SCIP_HEUR *heur)
Definition: heur.c:1327
static SCIP_DECL_HEURFREE(heurFreeOfins)
Definition: heur_ofins.c:447
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
Definition: scip.c:45876
int SCIPgetNOrigVars(SCIP *scip)
Definition: scip.c:12071
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:8526
#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:11505
#define DEFAULT_LPLIMFAC
Definition: heur_ofins.c:53
SCIP_SOL ** SCIPgetSols(SCIP *scip)
Definition: scip.c:38881
#define FALSE
Definition: def.h:64
SCIP_RETCODE SCIPincludeHeurOfins(SCIP *scip)
Definition: heur_ofins.c:612
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition: misc.c:2765
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:4230
SCIP_RETCODE SCIPcopyLimits(SCIP *sourcescip, SCIP *targetscip)
Definition: scip.c:4126
SCIP_Real SCIPinfinity(SCIP *scip)
Definition: scip.c:45816
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
Definition: scip.c:45888
static SCIP_DECL_HEUREXEC(heurExecOfins)
Definition: heur_ofins.c:467
#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:5069
SCIP_BRANCHRULE * SCIPfindBranchrule(SCIP *scip, const char *name)
Definition: scip.c:9184
int SCIPvarGetProbindex(SCIP_VAR *var)
Definition: var.c:16859
#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:21907
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:7999
#define HEUR_FREQ
Definition: heur_ofins.c:35
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:2903
#define SCIP_LONGINT_MAX
Definition: def.h:121
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip.h:21937
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip.c:696
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1102
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip.h:21890
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip.c:1260
#define SCIPdebugMsg
Definition: scip.h:451
#define HEUR_TIMING
Definition: heur_ofins.c:38
SCIP_RETCODE SCIPprintStatistics(SCIP *scip, FILE *file)
Definition: scip.c:44425
#define HEUR_MAXDEPTH
Definition: heur_ofins.c:37
#define EVENTHDLR_DESC
Definition: heur_ofins.c:57
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip.c:15777
#define HEUR_DESC
Definition: heur_ofins.c:32
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1181
#define SCIPerrorMessage
Definition: pub_message.h:45
SCIP_Bool SCIPisParamFixed(SCIP *scip, const char *name)
Definition: scip.c:4338
#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:8060
SCIP_RETCODE SCIPgetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:38044
#define SCIPfreeBufferArrayNull(scip, ptr)
Definition: scip.h:21938
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip.c:4567
SCIP_STATUS SCIPgetStatus(SCIP *scip)
Definition: scip.c:921
SCIP_RETCODE SCIPpresolve(SCIP *scip)
Definition: scip.c:15616
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip.c:45519
SCIP_Bool SCIPisReoptEnabled(SCIP *scip)
Definition: scip.c:16981
struct SCIP_EventData SCIP_EVENTDATA
Definition: type_event.h:155
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition: misc.c:2798
#define NULL
Definition: lpi_spx1.cpp:137
#define REALABS(x)
Definition: def.h:159
#define SCIP_CALL(x)
Definition: def.h:306
SCIP_SOL * SCIPgetReoptLastOptSol(SCIP *scip)
Definition: scip.c:16310
#define SCIPstatisticPrintf
Definition: pub_message.h:107
SCIP_Longint SCIPheurGetNCalls(SCIP_HEUR *heur)
Definition: heur.c:1307
#define HEUR_USESSUBSCIP
Definition: heur_ofins.c:39
SCIP_Bool SCIPhasPrimalRay(SCIP *scip)
Definition: scip.c:40124
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip.h:21925
SCIP_RETCODE SCIPsetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real val)
Definition: scip.c:37867
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:40207
int SCIPgetNImplVars(SCIP *scip)
Definition: scip.c:11766
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:959
SCIP_Longint SCIPsolGetNodenum(SCIP_SOL *sol)
Definition: sol.c:2362
#define DEFAULT_MINIMPROVE
Definition: heur_ofins.c:48
SCIP_RETCODE SCIPprintRay(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
Definition: scip.c:38796
enum SCIP_Status SCIP_STATUS
Definition: type_stat.h:57
SCIP_RETCODE SCIPsetObjlimit(SCIP *scip, SCIP_Real objlimit)
Definition: scip.c:11078
#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:39843
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip.c:4625
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip.c:40241
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip.c:37631
#define HEUR_NAME
Definition: heur_ofins.c:31
int SCIPgetNSols(SCIP *scip)
Definition: scip.c:38832
int SCIPgetNBinVars(SCIP *scip)
Definition: scip.c:11676
int SCIPgetNVars(SCIP *scip)
Definition: scip.c:11631
#define SCIP_REAL_MAX
Definition: def.h:136
SCIP_RETCODE SCIPgetReoptOldObjCoef(SCIP *scip, SCIP_VAR *var, int run, SCIP_Real *objcoef)
Definition: scip.c:16337
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip.c:38931
int SCIPgetNReoptRuns(SCIP *scip)
Definition: scip.c:41126
#define DEFAULT_MAXNODES
Definition: heur_ofins.c:42
int SCIPgetNConss(SCIP *scip)
Definition: scip.c:12679
SCIP_NODESEL * SCIPfindNodesel(SCIP *scip, const char *name)
Definition: scip.c:8872
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:899
SCIP_VAR ** SCIPgetVars(SCIP *scip)
Definition: scip.c:11586
SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
Definition: var.c:16671
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:40169
#define SCIP_Real
Definition: def.h:135
SCIP_Bool SCIPisStopped(SCIP *scip)
Definition: scip.c:1138
#define DEFAULT_MAXCHGRATE
Definition: heur_ofins.c:43
#define MIN(x, y)
Definition: memory.c:75
#define SCIP_Longint
Definition: def.h:120
SCIP_RETCODE SCIPcheckCopyLimits(SCIP *sourcescip, SCIP_Bool *success)
Definition: scip.c:4090
SCIP_RETCODE SCIPsetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:37909
SCIP_RETCODE SCIPtransformProb(SCIP *scip)
Definition: scip.c:13668
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
Definition: scip.c:45864
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:8044
SCIP_RETCODE SCIPinterruptSolve(SCIP *scip)
Definition: scip.c:16942
SCIP_Real SCIPgetUpperbound(SCIP *scip)
Definition: scip.c:42472
#define SCIP_CALL_ABORT(x)
Definition: def.h:285
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1092
SCIP_Longint SCIPgetNNodes(SCIP *scip)
Definition: scip.c:41182
SCIP_Longint SCIPgetNLPs(SCIP *scip)
Definition: scip.c:41363
#define SCIPABORT()
Definition: def.h:278
#define DEFAULT_MAXCHANGE
Definition: heur_ofins.c:47
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:38007
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:4258
SCIP_RETCODE SCIPsetSubscipsOff(SCIP *scip, SCIP_Bool quiet)
Definition: scip.c:5020
SCIP_RETCODE SCIPsetLongintParam(SCIP *scip, const char *name, SCIP_Longint value)
Definition: scip.c:4683
SCIP_Real SCIPgetPrimalRayVal(SCIP *scip, SCIP_VAR *var)
Definition: scip.c:40142
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:154
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:4176
SCIP_Bool SCIPvarIsActive(SCIP_VAR *var)
Definition: var.c:16839
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip.c:774
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:37005
OFINS - Objective Function Induced Neighborhood Search - a primal heuristic for reoptimization.