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-2020 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 visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file heur_ofins.c
17  * @ingroup DEFPLUGINS_HEUR
18  * @brief OFINS - Objective Function Induced Neighborhood Search - a primal heuristic for reoptimization
19  * @author Jakob Witzig
20  */
21 
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include "blockmemshell/memory.h"
25 #include "scip/heuristics.h"
26 #include "scip/heur_ofins.h"
27 #include "scip/pub_event.h"
28 #include "scip/pub_heur.h"
29 #include "scip/pub_message.h"
30 #include "scip/pub_misc.h"
31 #include "scip/pub_sol.h"
32 #include "scip/pub_var.h"
33 #include "scip/scip_branch.h"
34 #include "scip/scip_copy.h"
35 #include "scip/scip_event.h"
36 #include "scip/scip_general.h"
37 #include "scip/scip_heur.h"
38 #include "scip/scip_mem.h"
39 #include "scip/scip_message.h"
40 #include "scip/scip_nodesel.h"
41 #include "scip/scip_numerics.h"
42 #include "scip/scip_param.h"
43 #include "scip/scip_prob.h"
44 #include "scip/scip_sol.h"
45 #include "scip/scip_solve.h"
46 #include "scip/scip_solvingstats.h"
47 #include "scip/scip_timing.h"
48 #include <string.h>
49 
50 #define HEUR_NAME "ofins"
51 #define HEUR_DESC "primal heuristic for reoptimization, objective function induced neighborhood search"
52 #define HEUR_DISPCHAR SCIP_HEURDISPCHAR_LNS
53 #define HEUR_PRIORITY 60000
54 #define HEUR_FREQ 0
55 #define HEUR_FREQOFS 0
56 #define HEUR_MAXDEPTH 0
57 #define HEUR_TIMING SCIP_HEURTIMING_BEFORENODE
58 #define HEUR_USESSUBSCIP TRUE /**< does the heuristic use a secondary SCIP instance? */
59 
60 /* default values for OFINS-specific plugins */
61 #define DEFAULT_MAXNODES 5000LL /**< maximum number of nodes to regard in the subproblem */
62 #define DEFAULT_MAXCHGRATE 0.50 /**< maximum percentage of changed objective coefficients */
63 #define DEFAULT_COPYCUTS TRUE /**< if DEFAULT_USELPROWS is FALSE, then should all active cuts from the cutpool
64  * of the original scip be copied to constraints of the subscip */
65 #define DEFAULT_MAXCHANGE 0.04 /**< maximal rate of change per coefficient to get fixed */
66 #define DEFAULT_MINIMPROVE 0.01 /**< factor by which OFINS should at least improve the incumbent */
67 #define DEFAULT_ADDALLSOLS FALSE /**< should all subproblem solutions be added to the original SCIP? */
68 #define DEFAULT_MINNODES 50LL /**< minimum number of nodes to regard in the subproblem */
69 #define DEFAULT_NODESOFS 500LL /**< number of nodes added to the contingent of the total nodes */
70 #define DEFAULT_NODESQUOT 0.1 /**< subproblem nodes in relation to nodes of the original problem */
71 #define DEFAULT_LPLIMFAC 2.0 /**< factor by which the limit on the number of LP depends on the node limit */
72 
73 /* event handler properties */
74 #define EVENTHDLR_NAME "Ofins"
75 #define EVENTHDLR_DESC "LP event handler for " HEUR_NAME " heuristic"
76 
77 
78 /** primal heuristic data */
79 struct SCIP_HeurData
80 {
81  SCIP_Real maxchangerate; /**< maximal rate of changed coefficients in the objective function */
82  SCIP_Longint maxnodes; /**< maximum number of nodes to regard in the subproblem */
83  SCIP_Bool copycuts; /**< should all active cuts from cutpool be copied to constraints in subproblem? */
84  SCIP_Bool addallsols; /**< should all subproblem solutions be added to the original SCIP? */
85  SCIP_Longint minnodes; /**< minimum number of nodes to regard in the subproblem */
86  SCIP_Longint nodesofs; /**< number of nodes added to the contingent of the total nodes */
87  SCIP_Real maxchange; /**< maximal rate of change per coefficient to get fixed */
88  SCIP_Real minimprove; /**< factor by which OFINS should at least improve the incumbent */
89  SCIP_Real nodesquot; /**< subproblem nodes in relation to nodes of the original problem */
90  SCIP_Real nodelimit; /**< the nodelimit employed in the current sub-SCIP, for the event handler*/
91  SCIP_Real lplimfac; /**< factor by which the limit on the number of LP depends on the node limit */
92 };
93 
94 /* ---------------- Callback methods of event handler ---------------- */
95 
96 /* exec the event handler
97  *
98  * we interrupt the solution process
99  */
100 static
101 SCIP_DECL_EVENTEXEC(eventExecOfins)
102 {
103  SCIP_HEURDATA* heurdata;
104 
105  assert(eventhdlr != NULL);
106  assert(eventdata != NULL);
107  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
108  assert(event != NULL);
109  assert(SCIPeventGetType(event) & SCIP_EVENTTYPE_LPSOLVED);
110 
111  heurdata = (SCIP_HEURDATA*)eventdata;
112  assert(heurdata != NULL);
113 
114  /* interrupt solution process of sub-SCIP */
115  if( SCIPgetNLPs(scip) > heurdata->lplimfac * heurdata->nodelimit )
116  {
117  SCIPdebugMsg(scip, "interrupt after %" SCIP_LONGINT_FORMAT " LPs\n",SCIPgetNLPs(scip));
119  }
120 
121  return SCIP_OKAY;
122 }
123 
124 /* setup and solve the sub-SCIP */
125 static
127  SCIP* scip, /**< original SCIP data structure */
128  SCIP* subscip, /**< sub-SCIP data structure */
129  SCIP_HEUR* heur, /**< heuristic data structure */
130  SCIP_HEURDATA* heurdata, /**< euristic's private data structure */
131  SCIP_RESULT* result, /**< result data structure */
132  SCIP_Longint nstallnodes, /**< number of stalling nodes for the subproblem */
133  SCIP_Bool* chgcoeffs /**< array of changed coefficients */
134 
135  )
136 {
137  SCIP_HASHMAP* varmapfw;
138  SCIP_VAR** vars;
139  SCIP_VAR** subvars;
140  SCIP_EVENTHDLR* eventhdlr;
141 
142  SCIP_SOL* sol;
143  SCIP_VAR** fixedvars;
144  SCIP_Real* fixedvals;
145  int nfixedvars;
146 
147  int nvars;
148  int nintvars;
149  int i;
150 
151  SCIP_SOL** subsols;
152  int nsubsols = 0;
153 
154  SCIP_Bool success;
155  SCIP_RETCODE retcode;
156  SCIP_STATUS status;
157 
158  assert(scip != NULL);
159  assert(subscip != NULL);
160  assert(heur != NULL);
161  assert(heurdata != NULL);
162  assert(result != NULL);
163  assert(chgcoeffs != NULL);
164 
165  SCIPdebugMsg(scip, "+---+ Start OFINS heuristic +---+\n");
166 
167  /* get variable data */
168  vars = SCIPgetVars(scip);
169  nvars = SCIPgetNVars(scip);
170 
171  /* create the variable mapping hash map */
172  SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), nvars) );
173 
174  /* get optimal solution of the last iteration */
175  sol = SCIPgetReoptLastOptSol(scip);
176 
177  /* if the solution is NULL the last problem was infeasible */
178  if( sol == NULL )
179  return SCIP_OKAY;
180 
181  nintvars = SCIPgetNBinVars(scip) + SCIPgetNIntVars(scip) + SCIPgetNImplVars(scip);
182  SCIP_CALL( SCIPallocBufferArray(scip, &fixedvars, nvars) );
183  SCIP_CALL( SCIPallocBufferArray(scip, &fixedvals, nvars) );
184 
185  /* determine variables to fix in the sub-SCIP */
186  nfixedvars = 0;
187  for( i = 0; i < nintvars; i++ )
188  {
189  if( !chgcoeffs[i] )
190  {
191  fixedvars[nfixedvars] = vars[i];
192  fixedvals[nfixedvars] = SCIPgetSolVal(scip, sol, vars[i]);
193  ++nfixedvars;
194  }
195  }
196 
197  /* create a problem copy as sub SCIP */
198  SCIP_CALL( SCIPcopyLargeNeighborhoodSearch(scip, subscip, varmapfw, "ofins", fixedvars, fixedvals, nfixedvars, FALSE,
199  FALSE, &success, NULL) );
200  assert(success);
201 
202  SCIPfreeBufferArrayNull(scip, &fixedvals);
203  SCIPfreeBufferArrayNull(scip, &fixedvars);
204 
205  /* create event handler for LP events */
206  eventhdlr = NULL;
207  SCIP_CALL( SCIPincludeEventhdlrBasic(subscip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecOfins, NULL) );
208  if( eventhdlr == NULL )
209  {
210  SCIPerrorMessage("event handler for " HEUR_NAME " heuristic not found.\n");
211  return SCIP_PLUGINNOTFOUND;
212  }
213 
214  SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) );
215  for( i = 0; i < nvars; i++ )
216  subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]);
217 
218  /* free hash map */
219  SCIPhashmapFree(&varmapfw);
220 
221  /* set an objective limit */
222  SCIPdebugMsg(scip, "set objective limit of %g to sub-SCIP\n", SCIPgetUpperbound(scip));
223  SCIP_CALL( SCIPsetObjlimit(subscip, SCIPgetUpperbound(scip)) );
224 
225  SCIPdebugMsg(scip, "OFINS subproblem: %d vars, %d cons\n", SCIPgetNVars(subscip), SCIPgetNConss(subscip));
226 
227  /* do not abort subproblem on CTRL-C */
228  SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
229 
230 #ifdef SCIP_DEBUG
231  /* for debugging, enable full output */
232  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 5) );
233  SCIP_CALL( SCIPsetIntParam(subscip, "display/freq", 100000000) );
234 #else
235  /* disable statistic timing inside sub SCIP and output to console */
236  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
237  SCIP_CALL( SCIPsetBoolParam(subscip, "timing/statistictiming", FALSE) );
238 #endif
239 
240  /* set limits for the subproblem */
241  SCIP_CALL( SCIPcopyLimits(scip, subscip) );
242  heurdata->nodelimit = heurdata->maxnodes;
243  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/stallnodes", nstallnodes) );
244  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", heurdata->maxnodes) );
245 
246  /* forbid recursive call of heuristics and separators solving sub-SCIPs */
247  SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
248 
249  /* disable cutting plane separation */
251 
252  /* disable expensive presolving */
254 
255  /* use best estimate node selection */
256  if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") )
257  {
258  SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) );
259  }
260 
261  /* use inference branching */
262  if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") )
263  {
264  SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) );
265  }
266 
267  /* disable conflict analysis */
268  if( !SCIPisParamFixed(subscip, "conflict/enable") )
269  {
270  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/enable", FALSE) );
271  }
272 
273  /* speed up sub-SCIP by not checking dual LP feasibility */
274  SCIP_CALL( SCIPsetBoolParam(subscip, "lp/checkdualfeas", FALSE) );
275 
276  /* presolve the subproblem */
277  retcode = SCIPpresolve(subscip);
278 
279  /* errors in solving the subproblem should not kill the overall solving process;
280  * hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
281  */
282  if( retcode != SCIP_OKAY )
283  {
284  SCIPwarningMessage(scip, "Error while presolving subproblem in %s heuristic; sub-SCIP terminated with code <%d>\n", HEUR_NAME, retcode);
285 
286  SCIPABORT(); /*lint --e{527}*/
287 
288  /* free */
289  SCIPfreeBufferArray(scip, &subvars);
290  return SCIP_OKAY;
291  }
292 
293  SCIPdebugMsg(scip, "%s presolved subproblem: %d vars, %d cons\n", HEUR_NAME, SCIPgetNVars(subscip), SCIPgetNConss(subscip));
294 
295  assert(eventhdlr != NULL);
296 
297  SCIP_CALL( SCIPtransformProb(subscip) );
298  SCIP_CALL( SCIPcatchEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, NULL) );
299 
300  /* solve the subproblem */
301  SCIPdebugMsg(scip, "solving subproblem: nstallnodes=%" SCIP_LONGINT_FORMAT ", maxnodes=%" SCIP_LONGINT_FORMAT "\n", nstallnodes, heurdata->maxnodes);
302 
303  /* errors in solving the subproblem should not kill the overall solving process;
304  * hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
305  */
306  SCIP_CALL_ABORT( SCIPsolve(subscip) );
307 
308  SCIP_CALL( SCIPdropEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, -1) );
309 
310  /* print solving statistics of subproblem if we are in SCIP's debug mode */
312 
313  status = SCIPgetStatus(subscip);
314 
315  switch (status) {
317  break;
320  {
321  /* transfer the primal ray from the sub-SCIP to the main SCIP */
322  if( SCIPhasPrimalRay(subscip) )
323  {
324  SCIP_SOL* primalray;
325 
326  SCIP_CALL( SCIPcreateSol(scip, &primalray, heur) );
327 
328  /* transform the ray into the space of the source scip */
329  for( i = 0; i < nvars; i++ )
330  {
331  SCIP_CALL( SCIPsetSolVal(scip, primalray, vars[i],
332  subvars[i] != NULL ? SCIPgetPrimalRayVal(subscip, subvars[i]) : 0.0) );
333  }
334 
335  SCIPdebug( SCIP_CALL( SCIPprintRay(scip, primalray, 0, FALSE) ); );
336 
337  /* update the primal ray of the source scip */
338  SCIP_CALL( SCIPupdatePrimalRay(scip, primalray) );
339  SCIP_CALL( SCIPfreeSol(scip, &primalray) );
340 
341  *result = SCIP_UNBOUNDED;
342  }
343 
344  break;
345  }
346  default:
347  /* check, whether a solution was found;
348  * due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted
349  */
350  nsubsols = SCIPgetNSols(subscip);
351  subsols = SCIPgetSols(subscip);
352  success = FALSE;
353  for( i = 0; i < nsubsols && (!success || heurdata->addallsols); i++ )
354  {
355  SCIP_SOL* newsol;
356 
357  SCIP_CALL( SCIPtranslateSubSol(scip, subscip, subsols[i], heur, subvars, &newsol) );
358 
359  /* try to add new solution to scip and free it immediately */
360  SCIP_CALL( SCIPtrySolFree(scip, &newsol, FALSE, FALSE, TRUE, TRUE, TRUE, &success) );
361 
362  if( success )
363  *result = SCIP_FOUNDSOL;
364  }
365  break;
366  } /*lint !e788*/
367 
368  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",
369  HEUR_NAME, 0.0, SCIPgetSolvingTime(subscip), SCIPgetNNodes(subscip), success ? SCIPgetPrimalbound(scip) : SCIPinfinity(scip),
370  nsubsols > 0 ? SCIPsolGetNodenum(SCIPgetBestSol(subscip)) : -1 );
371 
372  /* free subproblem */
373  SCIPfreeBufferArray(scip, &subvars);
374 
375  return SCIP_OKAY;
376 }
377 
378 /** main procedure of the OFINS heuristic, creates and solves a sub-SCIP */
379 static
381  SCIP* scip, /**< original SCIP data structure */
382  SCIP_HEUR* heur, /**< heuristic data structure */
383  SCIP_HEURDATA* heurdata, /**< euristic's private data structure */
384  SCIP_RESULT* result, /**< result data structure */
385  SCIP_Longint nstallnodes, /**< number of stalling nodes for the subproblem */
386  SCIP_Bool* chgcoeffs /**< array of changed coefficients */
387  )
388 {
389  SCIP* subscip;
390  SCIP_RETCODE retcode;
391  SCIP_Bool success;
392 
393  assert(scip != NULL);
394  assert(heur != NULL);
395  assert(heurdata != NULL);
396  assert(result != NULL);
397  assert(chgcoeffs != NULL);
398 
399  *result = SCIP_DIDNOTRUN;
400 
401  /* check whether there is enough time and memory left */
402  SCIP_CALL( SCIPcheckCopyLimits(scip, &success) );
403 
404  if( !success )
405  return SCIP_OKAY;
406 
407  *result = SCIP_DIDNOTFIND;
408 
409  /* do not run, if no solution was found */
410  if ( SCIPgetReoptLastOptSol(scip) == NULL )
411  return SCIP_OKAY;
412 
413  /* initialize the subproblem */
414  SCIP_CALL( SCIPcreate(&subscip) );
415 
416  retcode = setupAndSolve(scip, subscip, heur, heurdata, result, nstallnodes, chgcoeffs);
417 
418  SCIP_CALL( SCIPfree(&subscip) );
419 
420  SCIP_CALL( retcode );
421 
422  return SCIP_OKAY;
423 }
424 
425 
426 
427 
428 /*
429  * Callback methods of primal heuristic
430  */
431 
432 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
433 static
434 SCIP_DECL_HEURCOPY(heurCopyOfins)
435 { /*lint --e{715}*/
436  assert(scip != NULL);
437  assert(heur != NULL);
438  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
439 
440  /* call inclusion method of primal heuristic */
442 
443  return SCIP_OKAY;
444 }
445 
446 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
447 static
448 SCIP_DECL_HEURFREE(heurFreeOfins)
449 { /*lint --e{715}*/
450  SCIP_HEURDATA* heurdata;
451 
452  assert(heur != NULL);
453  assert(scip != NULL);
454 
455  /* get heuristic data */
456  heurdata = SCIPheurGetData(heur);
457  assert(heurdata != NULL);
458 
459  /* free heuristic data */
460  SCIPfreeBlockMemory(scip, &heurdata);
461  SCIPheurSetData(heur, NULL);
462 
463  return SCIP_OKAY;
464 }
465 
466 /** execution method of primal heuristic */
467 static
468 SCIP_DECL_HEUREXEC(heurExecOfins)
469 {/*lint --e{715}*/
470  SCIP_HEURDATA* heurdata;
471  SCIP_VAR** vars;
472  SCIP_Bool* chgcoeffs;
473  SCIP_Longint nstallnodes;
474  int nchgcoefs;
475  int nvars;
476  int v;
477 
478  assert( heur != NULL );
479  assert( scip != NULL );
480  assert( result != NULL );
481 
482  *result = SCIP_DELAYED;
483 
484  /* do not call heuristic of node was already detected to be infeasible */
485  if( nodeinfeasible )
486  return SCIP_OKAY;
487 
488  /* get heuristic data */
489  heurdata = SCIPheurGetData(heur);
490  assert( heurdata != NULL );
491 
492  /* only call heuristic, if reoptimization is enabled */
493  if( !SCIPisReoptEnabled(scip) )
494  return SCIP_OKAY;
495 
496  /* only call the heuristic, if we are in run >= 2 */
497  if( SCIPgetNReoptRuns(scip) <= 1 )
498  return SCIP_OKAY;
499 
500  *result = SCIP_DIDNOTRUN;
501 
502  if( SCIPisStopped(scip) )
503  return SCIP_OKAY;
504 
505  /* calculate the maximal number of branching nodes until heuristic is aborted */
506  nstallnodes = (SCIP_Longint)(heurdata->nodesquot * SCIPgetNNodes(scip));
507 
508  /* reward OFINS if it succeeded often */
509  nstallnodes = (SCIP_Longint)(nstallnodes * 3.0 * (SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur) + 1.0));
510  nstallnodes -= 100 * SCIPheurGetNCalls(heur); /* count the setup costs for the sub-SCIP as 100 nodes */
511  nstallnodes += heurdata->nodesofs;
512 
513  /* determine the node limit for the current process */
514  nstallnodes = MIN(nstallnodes, heurdata->maxnodes);
515 
516  /* check whether we have enough nodes left to call subproblem solving */
517  if( nstallnodes < heurdata->minnodes )
518  {
519  SCIPdebugMsg(scip, "skipping OFINS: nstallnodes=%" SCIP_LONGINT_FORMAT ", minnodes=%" SCIP_LONGINT_FORMAT "\n", nstallnodes, heurdata->minnodes);
520  return SCIP_OKAY;
521  }
522 
523  /* get variable data and check which coefficient has changed */
524  vars = SCIPgetVars(scip);
525  nvars = SCIPgetNBinVars(scip) + SCIPgetNIntVars(scip) + SCIPgetNImplVars(scip);
526  nchgcoefs = 0;
527 
528  SCIP_CALL( SCIPallocBufferArray(scip, &chgcoeffs, nvars) );
529 
530  for( v = 0; v < nvars; v++ )
531  {
532  SCIP_Real newcoef;
533  SCIP_Real oldcoef;
534  SCIP_Real newcoefabs;
535  SCIP_Real oldcoefabs;
536  SCIP_Real frac;
537 
538  /* we only want to count variables that are unfixed after the presolving */
539  assert(SCIPvarGetStatus(vars[v]) != SCIP_VARSTATUS_ORIGINAL);
540  assert(SCIPvarIsActive(vars[v]));
541 
542  SCIP_CALL( SCIPgetReoptOldObjCoef(scip, vars[v], SCIPgetNReoptRuns(scip), &newcoef) );
543  SCIP_CALL( SCIPgetReoptOldObjCoef(scip, vars[v], SCIPgetNReoptRuns(scip)-1, &oldcoef) );
544  newcoefabs = REALABS(newcoef);
545  oldcoefabs = REALABS(oldcoef);
546 
547  /* if both coefficients are zero nothing has changed */
548  if( SCIPisZero(scip, newcoef) && SCIPisZero(scip, oldcoef) )
549  {
550  frac = 0;
551  }
552  /* if exactly one coefficient is zero, the other need to be close to zero */
553  else if( SCIPisZero(scip, newcoef) || SCIPisZero(scip, oldcoef) )
554  {
555  assert(SCIPisZero(scip, newcoef) != SCIPisZero(scip, oldcoef));
556  if( !SCIPisZero(scip, newcoef) )
557  frac = MIN(1, newcoefabs);
558  else
559  frac = MIN(1, oldcoefabs);
560  }
561  /* if both coefficients have the same sign we calculate the quotient
562  * MIN(newcoefabs, oldcoefabs)/MAX(newcoefabs, oldcoefabs)
563  */
564  else if( SCIPisPositive(scip, newcoef) == SCIPisPositive(scip, oldcoef) )
565  {
566  frac = 1.0 - MIN(newcoefabs, oldcoefabs)/MAX(newcoefabs, oldcoefabs);
567  }
568  /* if both coefficients have a different sign, we set frac = 1 */
569  else
570  {
571  assert((SCIPisPositive(scip, newcoef) && SCIPisNegative(scip, oldcoef))
572  || (SCIPisNegative(scip, newcoef) && SCIPisPositive(scip, oldcoef)));
573 
574  frac = 1;
575  }
576 
577  if( frac > heurdata->maxchange )
578  {
579  chgcoeffs[v] = TRUE;
580  nchgcoefs++;
581  }
582  else
583  chgcoeffs[v] = FALSE;
584  }
585 
586  SCIPdebugMsg(scip, "%d (rate %.4f) changed coefficients\n", nchgcoefs, nchgcoefs/((SCIP_Real)nvars));
587 
588  /* we only want to run the heuristic, if there at least 3 changed coefficients.
589  * if the number of changed coefficients is 2 the trivialnegation heuristic will construct an
590  * optimal solution without solving a MIP.
591  */
592  if( nchgcoefs < 3 )
593  goto TERMINATE;
594 
595  /* run the heuristic, if not too many coefficients have changed */
596  if( nchgcoefs/((SCIP_Real)nvars) > heurdata->maxchangerate )
597  goto TERMINATE;
598 
599  SCIP_CALL( applyOfins(scip, heur, heurdata, result, nstallnodes, chgcoeffs) );
600 
601  TERMINATE:
602  SCIPfreeBufferArray(scip, &chgcoeffs);
603 
604  return SCIP_OKAY;
605 }
606 
607 
608 /*
609  * primal heuristic specific interface methods
610  */
611 
612 /** creates the ofins primal heuristic and includes it in SCIP */
614  SCIP* scip /**< SCIP data structure */
615  )
616 {
617  SCIP_HEURDATA* heurdata;
618  SCIP_HEUR* heur;
619 
620  /* create ofins primal heuristic data */
621  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
622  assert(heurdata != NULL);
623 
624  /* include primal heuristic */
625  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
627  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecOfins, heurdata) );
628 
629  assert(heur != NULL);
630 
631  /* set non fundamental callbacks via setter functions */
632  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyOfins) );
633  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeOfins) );
634 
635  /* add ofins primal heuristic parameters */
636 
637  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/" HEUR_NAME "/maxnodes",
638  "maximum number of nodes to regard in the subproblem",
639  &heurdata->maxnodes, TRUE, DEFAULT_MAXNODES, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
640 
641  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/" HEUR_NAME "/minnodes",
642  "minimum number of nodes required to start the subproblem",
643  &heurdata->minnodes, TRUE, DEFAULT_MINNODES, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
644 
645  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/maxchangerate",
646  "maximal rate of changed coefficients",
647  &heurdata->maxchangerate, FALSE, DEFAULT_MAXCHGRATE, 0.0, 1.0, NULL, NULL) );
648 
649  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/maxchange",
650  "maximal rate of change per coefficient to get fixed",
651  &heurdata->maxchange, FALSE, DEFAULT_MAXCHANGE, 0.0, 1.0, NULL, NULL) );
652 
653  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/copycuts",
654  "should all active cuts from cutpool be copied to constraints in subproblem?",
655  &heurdata->copycuts, TRUE, DEFAULT_COPYCUTS, NULL, NULL) );
656 
657  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/addallsols",
658  "should all subproblem solutions be added to the original SCIP?",
659  &heurdata->addallsols, TRUE, DEFAULT_ADDALLSOLS, NULL, NULL) );
660 
661  SCIP_CALL( SCIPaddLongintParam(scip, "heuristics/" HEUR_NAME "/nodesofs",
662  "number of nodes added to the contingent of the total nodes",
663  &heurdata->nodesofs, FALSE, DEFAULT_NODESOFS, 0LL, SCIP_LONGINT_MAX, NULL, NULL) );
664 
665  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/nodesquot",
666  "contingent of sub problem nodes in relation to the number of nodes of the original problem",
667  &heurdata->nodesquot, FALSE, DEFAULT_NODESQUOT, 0.0, 1.0, NULL, NULL) );
668 
669  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/minimprove",
670  "factor by which RENS should at least improve the incumbent",
671  &heurdata->minimprove, TRUE, DEFAULT_MINIMPROVE, 0.0, 1.0, NULL, NULL) );
672 
673  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/lplimfac",
674  "factor by which the limit on the number of LP depends on the node limit",
675  &heurdata->lplimfac, TRUE, DEFAULT_LPLIMFAC, 1.0, SCIP_REAL_MAX, NULL, NULL) );
676 
677  return SCIP_OKAY;
678 }
enum SCIP_Result SCIP_RESULT
Definition: type_result.h:52
SCIP_Real SCIPgetPrimalRayVal(SCIP *scip, SCIP_VAR *var)
Definition: scip_sol.c:3553
SCIP_Longint SCIPheurGetNCalls(SCIP_HEUR *heur)
Definition: heur.c:1555
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip_sol.c:977
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
#define SCIP_EVENTTYPE_LPSOLVED
Definition: type_event.h:92
SCIP_Bool SCIPisStopped(SCIP *scip)
Definition: scip_general.c:687
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1429
SCIP_RETCODE SCIPsetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real val)
Definition: scip_sol.c:1213
static SCIP_DECL_HEURCOPY(heurCopyOfins)
Definition: heur_ofins.c:435
public methods for SCIP parameter handling
public methods for node selector plugins
public methods for memory management
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1340
SCIP_STATUS SCIPgetStatus(SCIP *scip)
Definition: scip_general.c:467
SCIP_RETCODE SCIPtranslateSubSol(SCIP *scip, SCIP *subscip, SCIP_SOL *subsol, SCIP_HEUR *heur, SCIP_VAR **subvars, SCIP_SOL **newsol)
Definition: scip_copy.c:1356
SCIP_RETCODE SCIPinterruptSolve(SCIP *scip)
Definition: scip_solve.c:3407
SCIP_Bool SCIPisReoptEnabled(SCIP *scip)
Definition: scip_solve.c:3446
static SCIP_DECL_HEURFREE(heurFreeOfins)
Definition: heur_ofins.c:449
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip_message.c:123
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip_sol.c:1353
public solving methods
static SCIP_DECL_EVENTEXEC(eventExecOfins)
Definition: heur_ofins.c:102
public methods for timing
SCIP_RETCODE SCIPtransformProb(SCIP *scip)
Definition: scip_solve.c:357
#define DEFAULT_COPYCUTS
Definition: heur_ofins.c:63
#define DEFAULT_NODESQUOT
Definition: heur_ofins.c:71
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:315
int SCIPgetNConss(SCIP *scip)
Definition: scip_prob.c:3036
int SCIPgetNVars(SCIP *scip)
Definition: scip_prob.c:1986
SCIP_Real SCIPgetSolvingTime(SCIP *scip)
Definition: scip_timing.c:360
#define DEFAULT_LPLIMFAC
Definition: heur_ofins.c:72
#define FALSE
Definition: def.h:73
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:127
static SCIP_DECL_HEUREXEC(heurExecOfins)
Definition: heur_ofins.c:469
#define TRUE
Definition: def.h:72
#define SCIPdebug(x)
Definition: pub_message.h:84
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
methods commonly used by primal heuristics
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip_solve.c:2527
#define HEUR_DISPCHAR
Definition: heur_ofins.c:52
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:3200
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:67
int SCIPgetNImplVars(SCIP *scip)
Definition: scip_prob.c:2121
SCIP_EXPORT SCIP_Bool SCIPvarIsActive(SCIP_VAR *var)
Definition: var.c:17335
public methods for problem variables
#define HEUR_PRIORITY
Definition: heur_ofins.c:53
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_param.c:48
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:95
#define HEUR_FREQ
Definition: heur_ofins.c:54
SCIP_EXPORT SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
Definition: var.c:17131
SCIP_RETCODE SCIPsetSubscipsOff(SCIP *scip, SCIP_Bool quiet)
Definition: scip_param.c:893
SCIP_Real SCIPgetUpperbound(SCIP *scip)
#define SCIP_LONGINT_MAX
Definition: def.h:149
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip_mem.h:123
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:78
int SCIPgetNReoptRuns(SCIP *scip)
SCIP_RETCODE SCIPpresolve(SCIP *scip)
Definition: scip_solve.c:2366
#define SCIPdebugMsg
Definition: scip_message.h:69
#define HEUR_TIMING
Definition: heur_ofins.c:57
#define HEUR_MAXDEPTH
Definition: heur_ofins.c:56
SCIP_RETCODE SCIPcheckCopyLimits(SCIP *sourcescip, SCIP_Bool *success)
Definition: scip_copy.c:3192
SCIP_Longint SCIPheurGetNBestSolsFound(SCIP_HEUR *heur)
Definition: heur.c:1575
int SCIPgetNIntVars(SCIP *scip)
Definition: scip_prob.c:2076
public methods for numerical tolerances
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip_sol.c:320
SCIP_Longint SCIPgetNLPs(SCIP *scip)
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:916
SCIP_RETCODE SCIPsetObjlimit(SCIP *scip, SCIP_Real objlimit)
Definition: scip_prob.c:1420
public methods for querying solving statistics
int SCIPgetNSols(SCIP *scip)
Definition: scip_sol.c:2206
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_heur.c:108
SCIP_Longint SCIPgetNNodes(SCIP *scip)
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip_general.c:283
#define EVENTHDLR_DESC
Definition: heur_ofins.c:76
#define HEUR_DESC
Definition: heur_ofins.c:51
#define SCIPerrorMessage
Definition: pub_message.h:55
#define DEFAULT_MINNODES
Definition: heur_ofins.c:69
#define HEUR_FREQOFS
Definition: heur_ofins.c:55
SCIP_BRANCHRULE * SCIPfindBranchrule(SCIP *scip, const char *name)
Definition: scip_branch.c:288
public methods for event handler plugins and event handlers
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip_event.c:277
SCIP_VAR ** SCIPgetVars(SCIP *scip)
Definition: scip_prob.c:1941
#define SCIPfreeBufferArrayNull(scip, ptr)
Definition: scip_mem.h:124
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip_mem.c:48
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
struct SCIP_EventData SCIP_EVENTDATA
Definition: type_event.h:164
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1350
#define NULL
Definition: lpi_spx1.cpp:155
#define REALABS(x)
Definition: def.h:187
public methods for problem copies
public methods for primal CIP solutions
SCIP_NODESEL * SCIPfindNodesel(SCIP *scip, const char *name)
Definition: scip_nodesel.c:225
#define SCIP_CALL(x)
Definition: def.h:364
SCIP_RETCODE SCIPsetPresolving(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip_param.c:942
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:1021
#define SCIPstatisticPrintf
Definition: pub_message.h:117
#define HEUR_USESSUBSCIP
Definition: heur_ofins.c:58
SCIP_SOL * SCIPgetReoptLastOptSol(SCIP *scip)
Definition: scip_solve.c:3116
public methods for primal heuristic plugins and divesets
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip_mem.h:111
SCIP_Real SCIPinfinity(SCIP *scip)
public data structures and miscellaneous methods
#define DEFAULT_ADDALLSOLS
Definition: heur_ofins.c:68
#define SCIP_Bool
Definition: def.h:70
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition: misc.c:3013
#define DEFAULT_MINIMPROVE
Definition: heur_ofins.c:67
enum SCIP_Status SCIP_STATUS
Definition: type_stat.h:58
#define EVENTHDLR_NAME
Definition: heur_ofins.c:75
SCIP_Bool SCIPisParamFixed(SCIP *scip, const char *name)
Definition: scip_param.c:210
#define MAX(x, y)
Definition: tclique_def.h:83
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
#define HEUR_NAME
Definition: heur_ofins.c:50
SCIP_RETCODE SCIPprintStatistics(SCIP *scip, FILE *file)
SCIP_RETCODE SCIPcopyLimits(SCIP *sourcescip, SCIP *targetscip)
Definition: scip_copy.c:3228
SCIP_RETCODE SCIPincludeEventhdlrBasic(SCIP *scip, SCIP_EVENTHDLR **eventhdlrptr, const char *name, const char *desc, SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip_event.c:95
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_param.c:130
#define SCIP_REAL_MAX
Definition: def.h:164
SCIP_RETCODE SCIPgetReoptOldObjCoef(SCIP *scip, SCIP_VAR *var, int run, SCIP_Real *objcoef)
Definition: scip_solve.c:3143
SCIP_RETCODE SCIPupdatePrimalRay(SCIP *scip, SCIP_SOL *primalray)
Definition: scip_sol.c:3580
SCIP_RETCODE SCIPsetLongintParam(SCIP *scip, const char *name, SCIP_Longint value)
Definition: scip_param.c:555
SCIP_RETCODE SCIPincludeHeurOfins(SCIP *scip)
Definition: heur_ofins.c:614
public methods for branching rule plugins and branching
public methods for managing events
general public methods
public methods for solutions
#define DEFAULT_MAXNODES
Definition: heur_ofins.c:61
SCIP_SOL ** SCIPgetSols(SCIP *scip)
Definition: scip_sol.c:2255
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip_event.c:311
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip_heur.c:153
SCIP_EXPORT SCIP_Longint SCIPsolGetNodenum(SCIP_SOL *sol)
Definition: sol.c:2584
public methods for message output
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition: misc.c:3047
#define SCIP_Real
Definition: def.h:163
#define DEFAULT_MAXCHGRATE
Definition: heur_ofins.c:62
public methods for message handling
SCIP_Real SCIPgetPrimalbound(SCIP *scip)
SCIP_Bool SCIPhasPrimalRay(SCIP *scip)
Definition: scip_sol.c:3535
#define SCIP_Longint
Definition: def.h:148
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip_param.c:439
int SCIPgetNBinVars(SCIP *scip)
Definition: scip_prob.c:2031
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip_heur.c:169
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_param.c:102
SCIP_RETCODE SCIPprintRay(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
Definition: scip_sol.c:2170
public methods for primal heuristics
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip_general.c:315
#define SCIP_CALL_ABORT(x)
Definition: def.h:343
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_sol.c:3231
#define SCIPABORT()
Definition: def.h:336
public methods for global and local (sub)problems
#define DEFAULT_MAXCHANGE
Definition: heur_ofins.c:66
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip_sol.c:2305
#define DEFAULT_NODESOFS
Definition: heur_ofins.c:70
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip_param.c:497
SCIP_RETCODE SCIPsetSeparating(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip_param.c:968
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:381
OFINS - Objective Function Induced Neighborhood Search - a primal heuristic for reoptimization.
memory allocation routines