Scippy

SCIP

Solving Constraint Integer Programs

heur_subnlp.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-2014 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_subnlp.c
17  * @brief NLP local search primal heuristic using sub-SCIPs
18  * @author Stefan Vigerske
19  *
20  * @todo set cutoff or similar in NLP
21  */
22 
23 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
24 
25 #include <assert.h>
26 #include <string.h>
27 
28 #include "scip/heur_subnlp.h"
29 #include "nlpi/nlpi.h"
30 #include "scip/cons_linear.h"
31 #include "scip/cons_varbound.h"
32 #include "scip/cons_logicor.h"
33 #include "scip/cons_setppc.h"
34 #include "scip/cons_knapsack.h"
36 
37 #define HEUR_NAME "subnlp"
38 #define HEUR_DESC "primal heuristic that performs a local search in an NLP after fixing integer variables and presolving"
39 #define HEUR_DISPCHAR 'q'
40 #define HEUR_PRIORITY -2000000
41 #define HEUR_FREQ 1
42 #define HEUR_FREQOFS 0
43 #define HEUR_MAXDEPTH -1
44 #define HEUR_TIMING SCIP_HEURTIMING_AFTERNODE
45 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? we set this to FALSE because we want this heuristic to also run within other heuristics */
46 
47 /*
48  * Data structures
49  */
50 
51 /** primal heuristic data */
52 struct SCIP_HeurData
53 {
54  SCIP* subscip; /**< copy of CIP where presolving and NLP solving is done */
55  SCIP_Bool triedsetupsubscip; /**< whether we have tried to setup a sub-SCIP */
56  SCIP_Bool subscipisvalid; /**< whether all constraints have been copied */
57  int nseriousnlpierror; /**< number of consecutive serious NLP solver failures (memout, ...) */
58  SCIP_EVENTHDLR* eventhdlr; /**< event handler for global bound change events */
59 
60  int nvars; /**< number of active transformed variables in SCIP */
61  int nsubvars; /**< number of original variables in sub-SCIP */
62  SCIP_VAR** var_subscip2scip; /**< mapping variables in sub-SCIP to SCIP variables */
63  SCIP_VAR** var_scip2subscip; /**< mapping variables in SCIP to sub-SCIP variables */
64 
65  SCIP_SOL* startcand; /**< candidate for start point for heuristic */
66  SCIP_Real startcandviol; /**< violation of start point candidate w.r.t. constraint that reported this candidate */
67 
68  SCIP_NLPSTATISTICS* nlpstatistics; /**< statistics from NLP solver */
69  SCIP_Bool comblinearconsadded;/**< whether the linear constraint adding method has been called for combinatorial constraints already */
70  SCIP_Bool contlinearconsadded;/**< whether the linear constraint adding method has been called for continuous constraints already */
71 
72  int nlpverblevel; /**< verbosity level of NLP solver */
73  int nlpiterlimit; /**< iteration limit of NLP solver; 0 for off */
74  SCIP_Real nlptimelimit; /**< time limit of NLP solver; 0 for off */
75  SCIP_Real resolvetolfactor; /**< factor for feasibility tolerance when resolving NLP due to disagreement of feasibility */
76  SCIP_Bool resolvefromscratch; /**< whether a resolve of an NLP due to disagreement of feasibility should be from the original starting point or the infeasible solution */
77  char* nlpoptfile; /**< name of NLP solver specific option file */
78  SCIP_Real minimprove; /**< desired minimal improvement in objective function value when running heuristic */
79  int maxpresolverounds; /**< limit on number of presolve rounds in sub-SCIP */
80  SCIP_Bool forbidfixings; /**< whether to add constraints that forbid specific fixations that turned out to be infeasible */
81  SCIP_Bool keepcopy; /**< whether to keep SCIP copy or to create new copy each time heuristic is applied */
82 
83  SCIP_Longint iterused; /**< number of iterations used so far */
84  int iteroffset; /**< number of iterations added to the contingent of the total number of iterations */
85  SCIP_Real iterquot; /**< contingent of NLP iterations in relation to the number of nodes in SCIP */
86  int itermin; /**< minimal number of iterations required to start local search */
87  SCIP_Bool runalways; /**< whether to run NLP heuristic always (independent of iteroffset,iterquot,itermin) */
88 };
89 
90 
91 /*
92  * Local methods
93  */
94 
95 /** indicates whether the heuristic should be running, i.e., whether we expect something nonlinear after fixing all discrete variables */
96 static
98  SCIP* scip /**< SCIP data structure */
99  )
100 {
101  assert(scip != NULL);
102 
103  /* do not run heuristic if no NLP solver is available */
104  if( SCIPgetNNlpis(scip) <= 0 )
105  return FALSE;
106 
107  /* do not run heuristic if no continuous nonlinear variables are present */
109  return FALSE;
110 
111  return TRUE;
112 }
113 
114 /** creates copy of CIP from problem in SCIP */
115 static
117  SCIP* scip, /**< SCIP data structure */
118  SCIP_HEURDATA* heurdata /**< heuristic data structure */
119  )
120 {
121  int nvars;
122  SCIP_VAR** vars;
123  SCIP_VAR** subvars;
124  SCIP_VAR* var;
125  SCIP_VAR* subvar;
126  SCIP_Bool success;
127  char probname[SCIP_MAXSTRLEN];
128  int i;
129  SCIP_HASHMAP* varsmap;
130  SCIP_HASHMAP* conssmap;
131  SCIP_HASHMAPLIST* list;
132 #ifdef SCIP_DEBUG
133  static const SCIP_Bool copydisplays = TRUE;
134  static const SCIP_Bool copyreader = TRUE;
135 #else
136  static const SCIP_Bool copydisplays = FALSE;
137  static const SCIP_Bool copyreader = FALSE;
138 #endif
139 
140  assert(heurdata != NULL);
141  assert(heurdata->subscip == NULL);
142 
143  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
144 
145  heurdata->triedsetupsubscip = TRUE;
146 
147  /* initializing the subproblem */
148  SCIP_CALL( SCIPcreate(&heurdata->subscip) );
149 
150  /* create variable hash mapping scip -> subscip */
151  SCIP_CALL( SCIPhashmapCreate(&varsmap, SCIPblkmem(scip), MAX(nvars, 5)) );
152 
153  /* create sub-SCIP copy of CIP */
154 
155  /* copy interesting plugins */
156  success = TRUE;
157  SCIP_CALL( SCIPcopyPlugins(scip, heurdata->subscip,
158  copyreader, /* readers */
159  FALSE, /* pricers */
160  TRUE, /* conshdlrs */
161  FALSE, /* conflicthdlrs */
162  TRUE, /* presolvers */
163  FALSE, /* relaxators */
164  FALSE, /* separators */
165  TRUE, /* propagators */
166  FALSE, /* heuristics */
167  TRUE, /* eventhandler */
168  TRUE, /* nodeselectors (SCIP gives an error if there is none) */
169  FALSE, /* branchrules */
170  copydisplays, /* displays */
171  FALSE, /* dialogs */
172  TRUE, /* nlpis */
173  TRUE, /* message handler */
174  &success) );
175  if( !success )
176  {
177  SCIPverbMessage(scip, SCIP_VERBLEVEL_FULL, NULL, "In heur_subnlp: failed to copy some plugins to sub-SCIP, continue anyway\n");
178  }
179 
180  /* check if we still have NLPI's in subscip */
181  if( SCIPgetNNlpis(heurdata->subscip) <= 0 )
182  {
183  SCIPdebugMessage("some NLPIs from main SCIP did not copy into sub-SCIP, give up heuristic.\n");
184  SCIP_CALL( SCIPfree(&heurdata->subscip) );
185  SCIPhashmapFree(&varsmap);
186 
187  return SCIP_OKAY;
188  }
189 
190  /* copy parameter settings */
191  SCIP_CALL( SCIPcopyParamSettings(scip, heurdata->subscip) );
192 
193  /* create problem in sub-SCIP */
194  /* get name of the original problem and add "subnlp" */
195  (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s_subnlp", SCIPgetProbName(scip));
196  SCIP_CALL( SCIPcreateProb(heurdata->subscip, probname, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
197 
198  /* copy all variables */
199  SCIP_CALL( SCIPcopyVars(scip, heurdata->subscip, varsmap, NULL, TRUE) );
200 
201  /* copy as many constraints as possible */
203  SCIP_CALL( SCIPcopyConss(scip, heurdata->subscip, varsmap, conssmap, TRUE, FALSE, &heurdata->subscipisvalid) );
204  SCIPhashmapFree(&conssmap);
205  if( !heurdata->subscipisvalid )
206  {
207  SCIPverbMessage(scip, SCIP_VERBLEVEL_FULL, NULL, "In heur_subnlp: failed to copy some constraints to sub-SCIP, continue anyway\n");
208  SCIPdebugMessage("In heur_subnlp: failed to copy some constraints to sub-SCIP, continue anyway\n");
209  }
210 
211  /* create arrays translating scip transformed vars to subscip original vars, and vice versa
212  * capture variables in SCIP and sub-SCIP
213  * catch global bound change events
214  */
215 
216  SCIP_CALL( SCIPgetVarsData(heurdata->subscip, &subvars, &heurdata->nsubvars, NULL, NULL, NULL, NULL) );
217 
218  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &heurdata->var_subscip2scip, heurdata->nsubvars) );
219 #ifndef NDEBUG
220  BMSclearMemoryArray(heurdata->var_subscip2scip, heurdata->nsubvars);
221 #endif
222 
223  heurdata->nvars = nvars;
224  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &heurdata->var_scip2subscip, heurdata->nvars) );
225 #ifndef NDEBUG
226  BMSclearMemoryArray(heurdata->var_scip2subscip, heurdata->nvars);
227 #endif
228 
229  /* we need to get all subscip variables, also those which are copies of fixed variables from the main scip
230  * therefore we iterate over the hashmap
231  */
232  for( i = 0; i < SCIPhashmapGetNLists(varsmap); ++i )
233  {
234  for( list = SCIPhashmapGetList(varsmap, i); list != NULL; list = SCIPhashmapListGetNext(list) )
235  {
236  var = (SCIP_VAR*)SCIPhashmapListGetOrigin(list);
237  subvar = (SCIP_VAR*)SCIPhashmapListGetImage(list);
238 
239  assert(SCIPvarGetProbindex(subvar) >= 0);
240  assert(SCIPvarGetProbindex(subvar) <= heurdata->nsubvars);
241 
242  if( SCIPvarIsActive(var) )
243  {
244  assert(SCIPvarGetProbindex(var) <= heurdata->nvars);
245  assert(heurdata->var_scip2subscip[SCIPvarGetProbindex(var)] == NULL); /* assert that we have no mapping for this var yet */
246  heurdata->var_scip2subscip[SCIPvarGetProbindex(var)] = subvar;
247  }
248 
249  assert(heurdata->var_subscip2scip[SCIPvarGetProbindex(subvar)] == NULL); /* assert that we have no mapping for this subvar yet */
250  heurdata->var_subscip2scip[SCIPvarGetProbindex(subvar)] = var;
251 
252  SCIP_CALL( SCIPcaptureVar(scip, var) );
253  SCIP_CALL( SCIPcaptureVar(heurdata->subscip, subvar) );
254 
255  assert(SCIPisFeasEQ(scip, SCIPvarGetLbGlobal(var), SCIPvarGetLbGlobal(subvar)));
256  assert(SCIPisFeasEQ(scip, SCIPvarGetUbGlobal(var), SCIPvarGetUbGlobal(subvar)));
257 
258  SCIP_CALL( SCIPcatchVarEvent(scip, var, SCIP_EVENTTYPE_GBDCHANGED, heurdata->eventhdlr, (SCIP_EVENTDATA*)heurdata, NULL) );
259  }
260  }
261 
262 #ifndef NDEBUG
263  for( i = 0; i < heurdata->nvars; ++i )
264  {
265  assert(heurdata->var_scip2subscip[i] != NULL);
266  assert((SCIP_VAR*)SCIPhashmapGetImage(varsmap, (void*)vars[i]) == heurdata->var_scip2subscip[i]);
267  }
268  for( i = 0; i < heurdata->nsubvars; ++i )
269  {
270  assert(heurdata->var_subscip2scip[i] != NULL);
271  assert((SCIP_VAR*)SCIPhashmapGetImage(varsmap, (void*)heurdata->var_subscip2scip[i]) == subvars[i]);
272  }
273 #endif
274 
275  /* do not need hashmap anymore */
276  SCIPhashmapFree(&varsmap);
277 
278  /* initialize data structure for NLP solve statistics */
279  SCIP_CALL( SCIPnlpStatisticsCreate(&heurdata->nlpstatistics) );
280 
281  /* do not abort subproblem on CTRL-C */
282  SCIP_CALL( SCIPsetBoolParam(heurdata->subscip, "misc/catchctrlc", FALSE) );
283 
284  /* disable keeping solutions from one subscip solve for next solve (with usually different fixings) */
285  SCIP_CALL( SCIPsetIntParam(heurdata->subscip, "limits/maxorigsol", 0) );
286 
287  /* disable output to console */
288  SCIP_CALL( SCIPsetIntParam(heurdata->subscip, "display/verblevel", 0) );
289 
290  /* reset some limits to default values, in case users changed them in main scip (SCIPcopy copies parameter values :-() */
291  SCIP_CALL( SCIPresetParam(heurdata->subscip, "limits/absgap") );
292  SCIP_CALL( SCIPresetParam(heurdata->subscip, "limits/bestsol") );
293  SCIP_CALL( SCIPresetParam(heurdata->subscip, "limits/gap") );
294  SCIP_CALL( SCIPresetParam(heurdata->subscip, "limits/restarts") );
295  SCIP_CALL( SCIPresetParam(heurdata->subscip, "limits/solutions") );
296  SCIP_CALL( SCIPresetParam(heurdata->subscip, "limits/time") );
297  SCIP_CALL( SCIPresetParam(heurdata->subscip, "limits/totalnodes") );
298 
299  /* disable conflict analysis and separation
300  * keep normal presolving, but disable probing and restarts
301  * disable LP solve
302  * set nodelimit to 0
303  * heuristics and separators were not copied into subscip, so should not need to switch off
304  */
305  if( !SCIPisParamFixed(heurdata->subscip, "presolving/maxrounds") )
306  {
307  SCIP_CALL( SCIPsetIntParam(heurdata->subscip, "presolving/maxrounds", heurdata->maxpresolverounds) );
308  }
309  if( !SCIPisParamFixed(heurdata->subscip, "propagating/probing/maxprerounds") )
310  {
311  SCIP_CALL( SCIPsetIntParam(heurdata->subscip, "propagating/probing/maxprerounds", 0) );
312  }
313  if( !SCIPisParamFixed(heurdata->subscip, "presolving/maxrestarts") )
314  {
315  SCIP_CALL( SCIPsetIntParam(heurdata->subscip, "presolving/maxrestarts", 0) );
316  }
317 
318 #ifdef SCIP_DEBUG
319  /* for debugging, enable SCIP output */
320  SCIP_CALL( SCIPsetIntParam(heurdata->subscip, "display/verblevel", 5) );
321 #endif
322 
323  return SCIP_OKAY;
324 }
325 
326 /** free sub-SCIP data structure */
327 static
329  SCIP* scip, /**< SCIP data structure */
330  SCIP_HEURDATA* heurdata /**< heuristic data structure */
331  )
332 {
333  SCIP_VAR** subvars;
334  int nsubvars;
335  int i;
336  SCIP_VAR* var;
337  SCIP_VAR* subvar;
338 
339  assert(scip != NULL);
340  assert(heurdata != NULL);
341 
342  assert(heurdata->subscip != NULL);
343 
344  /* free NLP statistics */
345  if( heurdata->nlpstatistics != NULL )
346  SCIPnlpStatisticsFree(&heurdata->nlpstatistics);
347  assert(heurdata->nlpstatistics == NULL);
348 
349  SCIP_CALL( SCIPgetOrigVarsData(heurdata->subscip, &subvars, &nsubvars, NULL, NULL, NULL, NULL) );
350  assert(nsubvars == heurdata->nsubvars);
351 
352  /* drop global bound change events
353  * release variables in SCIP and sub-SCIP
354  */
355  for( i = 0; i < heurdata->nsubvars; ++i )
356  {
357  subvar = subvars[i];
358  assert(subvar != NULL);
359  assert(SCIPvarGetProbindex(subvar) == i);
360 
361  var = heurdata->var_subscip2scip[SCIPvarGetProbindex(subvar)];
362  assert(var != NULL);
363  assert(SCIPvarGetProbindex(var) <= heurdata->nvars);
364  assert(!SCIPvarIsActive(var) || heurdata->var_scip2subscip[SCIPvarGetProbindex(var)] == subvar);
365 
366  SCIP_CALL( SCIPdropVarEvent(scip, var, SCIP_EVENTTYPE_GBDCHANGED, heurdata->eventhdlr, (SCIP_EVENTDATA*)heurdata, -1) );
367 
368  SCIP_CALL( SCIPreleaseVar(heurdata->subscip, &subvar) );
369  SCIP_CALL( SCIPreleaseVar(scip, &var) );
370  }
371 
372  /* free variable mappings subscip -> scip and scip -> subscip */
373  SCIPfreeBlockMemoryArray(scip, &heurdata->var_subscip2scip, heurdata->nsubvars);
374  SCIPfreeBlockMemoryArray(scip, &heurdata->var_scip2subscip, heurdata->nvars);
375  heurdata->nsubvars = 0;
376  heurdata->nvars = 0;
377 
378  /* free sub-SCIP */
379  SCIP_CALL( SCIPfree(&heurdata->subscip) );
380 
381  return SCIP_OKAY;
382 }
383 
384 /** process variable global bound change event */
385 static
386 SCIP_DECL_EVENTEXEC(processVarEvent)
387 {
388  SCIP_HEURDATA* heurdata;
389  SCIP_VAR* var;
390  SCIP_VAR* subvar;
391  int idx;
392 
393  assert(scip != NULL);
394  assert(event != NULL);
395  assert(eventdata != NULL);
396  assert(eventhdlr != NULL);
397 
398  heurdata = (SCIP_HEURDATA*)eventdata;
399  assert(heurdata != NULL);
400 
401  var = SCIPeventGetVar(event);
402  assert(var != NULL);
403 
404  idx = SCIPvarGetProbindex(var);
405  /* if event corresponds to an active variable, we can easily look up the corresponding subvar
406  * if it is an inactive variable that has been copied to the subproblem,
407  * then we need to check the subscip2scip mapping
408  * @todo we could do this faster if we keep the variables mapping from SCIPcopy around
409  */
410  if( idx >= 0 )
411  {
412  assert(idx < heurdata->nvars);
413 
414  subvar = heurdata->var_scip2subscip[idx];
415  }
416  else
417  {
418  for( idx = 0; idx < heurdata->nsubvars; ++idx )
419  {
420  if( heurdata->var_subscip2scip[idx] == var )
421  break;
422  }
423  assert(idx < heurdata->nsubvars);
424  subvar = SCIPgetVars(heurdata->subscip)[idx];
425  }
426  assert(subvar != NULL);
427 
429  {
430  SCIP_CALL( SCIPchgVarLbGlobal(heurdata->subscip, subvar, SCIPvarGetLbGlobal(var)) );
431  }
432 
434  {
435  SCIP_CALL( SCIPchgVarUbGlobal(heurdata->subscip, subvar, SCIPvarGetUbGlobal(var)) );
436  }
437 
438  return SCIP_OKAY;
439 }
440 
441 /** adds linear constraints from a SCIP instance to its NLP */
442 static
444  SCIP* scip, /**< SCIP data structure */
445  SCIP_CONSHDLR* conshdlr, /**< constraint handler for linear constraints */
446  SCIP_Bool addcombconss, /**< whether to add combinatorial linear constraints to NLP */
447  SCIP_Bool addcontconss /**< whether to add continuous linear constraints to NLP */
448  )
449 {
450  SCIP_CONS** conss;
451  int nconss;
452  SCIP_NLROW* nlrow;
453  int i;
454  int j;
455  SCIP_Bool iscombinatorial;
456  int nvars;
457  SCIP_VAR** vars;
458 
459  assert(scip != NULL);
460  assert(conshdlr != NULL);
461 
462  nconss = SCIPconshdlrGetNActiveConss(conshdlr);
463  conss = SCIPconshdlrGetConss(conshdlr);
464 
465  if( nconss == 0 )
466  return SCIP_OKAY;
467 
468  for( i = 0; i < nconss; ++i )
469  {
470  /* skip local and redundant constraints */
471  if( !SCIPconsIsEnabled(conss[i]) || !SCIPconsIsChecked(conss[i]) )
472  continue;
473 
474  /* under some circumstances, this method may be called even though the problem has been shown to be infeasible in presolve already
475  * this infeasibility may come from a linear constraint with lhs > rhs
476  * the NLP does not allow such constraints, so we skip them here
477  */
478  if( !SCIPisRelLE(scip, SCIPgetLhsLinear(scip, conss[i]), SCIPgetRhsLinear(scip, conss[i])) )
479  continue;
480 
481  nvars = SCIPgetNVarsLinear(scip, conss[i]);
482  vars = SCIPgetVarsLinear(scip, conss[i]);
483 
484  /* check if constraint should be added, only need this check if we do not wanna any constraint anyway */
485  if( !addcombconss || !addcontconss )
486  {
487  iscombinatorial = TRUE;
488 
489  for( j = 0; j < nvars; ++j )
490  if( SCIPvarGetType(vars[j]) >= SCIP_VARTYPE_CONTINUOUS )
491  {
492  iscombinatorial = FALSE;
493  break;
494  }
495 
496  /* skip constraint, if not of interest */
497  if( (iscombinatorial && !addcombconss) || (!iscombinatorial && !addcontconss) )
498  continue;
499  }
500 
501  SCIP_CALL( SCIPcreateNlRow(scip, &nlrow, SCIPconsGetName(conss[i]), 0.0,
502  SCIPgetNVarsLinear(scip, conss[i]), SCIPgetVarsLinear(scip, conss[i]), SCIPgetValsLinear(scip, conss[i]),
503  0, NULL, 0, NULL, NULL,
504  SCIPgetLhsLinear(scip, conss[i]), SCIPgetRhsLinear(scip, conss[i])) );
505 
506  SCIP_CALL( SCIPaddNlRow(scip, nlrow) );
507  SCIP_CALL( SCIPreleaseNlRow(scip, &nlrow) );
508  }
509 
510  return SCIP_OKAY;
511 }
512 
513 /** adds variable bound constraints from a SCIP instance to its NLP */
514 static
516  SCIP* scip, /**< SCIP data structure */
517  SCIP_CONSHDLR* conshdlr, /**< constraint handler for linear constraints */
518  SCIP_Bool addcombconss, /**< whether to add combinatorial linear constraints to NLP */
519  SCIP_Bool addcontconss /**< whether to add continuous linear constraints to NLP */
520  )
521 {
522  SCIP_CONS** conss;
523  int nconss;
524  SCIP_NLROW* nlrow;
525  int i;
526  SCIP_VAR* vars[2];
527  SCIP_Real coefs[2];
528  SCIP_Bool iscombinatorial;
529 
530  assert(scip != NULL);
531  assert(conshdlr != NULL);
532 
533  nconss = SCIPconshdlrGetNActiveConss(conshdlr);
534  conss = SCIPconshdlrGetConss(conshdlr);
535 
536  if( nconss == 0 )
537  return SCIP_OKAY;
538 
539  for( i = 0; i < nconss; ++i )
540  {
541  /* skip local and redundant constraints */
542  if( !SCIPconsIsEnabled(conss[i]) || !SCIPconsIsChecked(conss[i]) )
543  continue;
544 
545  vars[0] = SCIPgetVarVarbound(scip, conss[i]);
546  vars[1] = SCIPgetVbdvarVarbound(scip, conss[i]);
547 
548  iscombinatorial = SCIPvarGetType(vars[0]) < SCIP_VARTYPE_CONTINUOUS && SCIPvarGetType(vars[1]) < SCIP_VARTYPE_CONTINUOUS;
549 
550  /* skip constraint, if not of interest */
551  if( (iscombinatorial && !addcombconss) || (!iscombinatorial && !addcontconss) )
552  continue;
553 
554  coefs[0] = 1.0;
555  coefs[1] = SCIPgetVbdcoefVarbound(scip, conss[i]);
556 
557  SCIP_CALL( SCIPcreateNlRow(scip, &nlrow, SCIPconsGetName(conss[i]), 0.0,
558  2, vars, coefs,
559  0, NULL, 0, NULL, NULL,
560  SCIPgetLhsVarbound(scip, conss[i]), SCIPgetRhsVarbound(scip, conss[i])) );
561 
562  SCIP_CALL( SCIPaddNlRow(scip, nlrow) );
563  SCIP_CALL( SCIPreleaseNlRow(scip, &nlrow) );
564  }
565 
566  return SCIP_OKAY;
567 }
568 
569 /** adds logic-or constraints to NLP */
570 static
572  SCIP* scip, /**< SCIP data structure */
573  SCIP_CONSHDLR* conshdlr /**< constraint handler for linear constraints */
574  )
575 {
576  SCIP_CONS** conss;
577  int nconss;
578  SCIP_NLROW* nlrow;
579  int i;
580  int j;
581  SCIP_Real* coefs;
582  int coefssize;
583  int nvars;
584 
585  assert(scip != NULL);
586  assert(conshdlr != NULL);
587 
588  nconss = SCIPconshdlrGetNActiveConss(conshdlr);
589  if( !nconss )
590  return SCIP_OKAY;
591 
592  conss = SCIPconshdlrGetConss(conshdlr);
593 
594  coefs = NULL;
595  coefssize = 0;
596 
597  for( i = 0; i < nconss; ++i )
598  {
599  /* skip local and redundant constraints */
600  if( !SCIPconsIsEnabled(conss[i]) || !SCIPconsIsChecked(conss[i]) )
601  continue;
602 
603  nvars = SCIPgetNVarsLogicor(scip, conss[i]);
604 
605  if( coefssize < nvars )
606  {
607  if( coefs == NULL )
608  {
609  SCIP_CALL( SCIPallocBufferArray(scip, &coefs, nvars) );
610  }
611  else
612  {
613  SCIP_CALL( SCIPreallocBufferArray(scip, &coefs, nvars) );
614  }
615  for( j = coefssize; j < nvars; ++j )
616  coefs[j] = 1.0;
617  coefssize = nvars;
618  }
619 
620  /* logic or constraints: 1 == sum_j x_j */
621 
622  SCIP_CALL( SCIPcreateNlRow(scip, &nlrow, SCIPconsGetName(conss[i]), 0.0,
623  nvars, SCIPgetVarsLogicor(scip, conss[i]), coefs,
624  0, NULL, 0, NULL, NULL,
625  1.0, 1.0) );
626 
627  SCIP_CALL( SCIPaddNlRow(scip, nlrow) );
628  SCIP_CALL( SCIPreleaseNlRow(scip, &nlrow) );
629  }
630 
631  SCIPfreeBufferArrayNull(scip, &coefs);
632 
633  return SCIP_OKAY;
634 }
635 
636 /** adds setppc constraints to NLP */
637 static
639  SCIP* scip, /**< SCIP data structure */
640  SCIP_CONSHDLR* conshdlr /**< constraint handler for linear constraints */
641  )
642 {
643  SCIP_CONS** conss;
644  int nconss;
645  SCIP_NLROW* nlrow;
646  int i;
647  int j;
648  SCIP_Real* coefs;
649  int coefssize;
650  int nvars;
651  SCIP_Real lhs;
652  SCIP_Real rhs;
653 
654  assert(scip != NULL);
655  assert(conshdlr != NULL);
656 
657  nconss = SCIPconshdlrGetNActiveConss(conshdlr);
658  if( !nconss )
659  return SCIP_OKAY;
660 
661  conss = SCIPconshdlrGetConss(conshdlr);
662 
663  coefs = NULL;
664  coefssize = 0;
665 
666  for( i = 0; i < nconss; ++i )
667  {
668  /* skip local and redundant constraints */
669  if( !SCIPconsIsEnabled(conss[i]) || !SCIPconsIsChecked(conss[i]) )
670  continue;
671 
672  nvars = SCIPgetNVarsSetppc(scip, conss[i]);
673 
674  if( coefssize < nvars )
675  {
676  if( coefs == NULL )
677  {
678  SCIP_CALL( SCIPallocBufferArray(scip, &coefs, nvars) );
679  }
680  else
681  {
682  SCIP_CALL( SCIPreallocBufferArray(scip, &coefs, nvars) );
683  }
684  for( j = coefssize; j < nvars; ++j )
685  coefs[j] = 1.0;
686  coefssize = nvars;
687  }
688 
689  /* setppc constraint: 1 ~ sum_j x_j */
690 
691  switch( SCIPgetTypeSetppc(scip, conss[i]) )
692  {
694  lhs = 1.0;
695  rhs = 1.0;
696  break;
697 
699  lhs = -SCIPinfinity(scip);
700  rhs = 1.0;
701  break;
702 
704  lhs = 1.0;
705  rhs = SCIPinfinity(scip);
706  break;
707 
708  default:
709  SCIPerrorMessage("unexpected setppc type\n");
710  return SCIP_ERROR;
711  }
712 
713  SCIP_CALL( SCIPcreateNlRow(scip, &nlrow, SCIPconsGetName(conss[i]), 0.0,
714  nvars, SCIPgetVarsSetppc(scip, conss[i]), coefs,
715  0, NULL, 0, NULL, NULL,
716  lhs, rhs) );
717 
718  SCIP_CALL( SCIPaddNlRow(scip, nlrow) );
719  SCIP_CALL( SCIPreleaseNlRow(scip, &nlrow) );
720  }
721 
722  SCIPfreeBufferArrayNull(scip, &coefs);
723 
724  return SCIP_OKAY;
725 }
726 
727 /** adds knapsack constraints to NLP */
728 static
730  SCIP* scip, /**< SCIP data structure */
731  SCIP_CONSHDLR* conshdlr /**< constraint handler for linear constraints */
732  )
733 {
734  SCIP_CONS** conss;
735  int nconss;
736  SCIP_NLROW* nlrow;
737  int i;
738  int j;
739  SCIP_Real* coefs;
740  int coefssize;
741  int nvars;
742 
743  assert(scip != NULL);
744  assert(conshdlr != NULL);
745 
746  nconss = SCIPconshdlrGetNActiveConss(conshdlr);
747  if( !nconss )
748  return SCIP_OKAY;
749 
750  conss = SCIPconshdlrGetConss(conshdlr);
751  assert(conss != NULL);
752 
753  coefs = NULL;
754  coefssize = 0;
755 
756  for( i = 0; i < nconss; ++i )
757  {
758  SCIP_Longint* weights;
759 
760  /* skip local and redundant constraints */
761  if( !SCIPconsIsEnabled(conss[i]) || !SCIPconsIsChecked(conss[i]) )
762  continue;
763 
764  nvars = SCIPgetNVarsKnapsack(scip, conss[i]);
765 
766  if( coefssize < nvars )
767  {
768  if( coefs == NULL )
769  {
770  SCIP_CALL( SCIPallocBufferArray(scip, &coefs, nvars) );
771  }
772  else
773  {
774  SCIP_CALL( SCIPreallocBufferArray(scip, &coefs, nvars) );
775  }
776  coefssize = nvars;
777  }
778 
779  weights = SCIPgetWeightsKnapsack(scip, conss[i]);
780  for( j = 0; j < nvars; ++j )
781  coefs[j] = (SCIP_Real)weights[j]; /*lint !e613*/
782 
783  SCIP_CALL( SCIPcreateNlRow(scip, &nlrow, SCIPconsGetName(conss[i]), 0.0,
784  nvars, SCIPgetVarsKnapsack(scip, conss[i]), coefs,
785  0, NULL, 0, NULL, NULL,
786  -SCIPinfinity(scip), (SCIP_Real)SCIPgetCapacityKnapsack(scip, conss[i])) );
787 
788  SCIP_CALL( SCIPaddNlRow(scip, nlrow) );
789  SCIP_CALL( SCIPreleaseNlRow(scip, &nlrow) );
790  }
791 
792  SCIPfreeBufferArrayNull(scip, &coefs);
793 
794  return SCIP_OKAY;
795 }
796 
797 /** adds combinatorial and/or continuous variants of linear constraints from a SCIP instance to its NLP */
798 static
800  SCIP* scip, /**< SCIP data structure */
801  SCIP_Bool addcombconss, /**< whether to add combinatorial linear constraints to NLP */
802  SCIP_Bool addcontconss /**< whether to add continuous linear constraints to NLP */
803  )
804 {
805  SCIP_CONSHDLR* conshdlr;
806 
807  /* add linear constraints */
808  conshdlr = SCIPfindConshdlr(scip, "linear");
809  if( conshdlr != NULL )
810  {
811  SCIP_CALL( addLinearConstraints(scip, conshdlr, addcombconss, addcontconss) );
812  }
813 
814  /* add varbound constraints */
815  conshdlr = SCIPfindConshdlr(scip, "varbound");
816  if( conshdlr != NULL )
817  {
818  SCIP_CALL( addVarboundConstraints(scip, conshdlr, addcombconss, addcontconss) );
819  }
820 
821  if( addcombconss )
822  {
823  /* add logic-or constraints */
824  conshdlr = SCIPfindConshdlr(scip, "logicor");
825  if( conshdlr != NULL )
826  {
827  SCIP_CALL( addLogicOrConstraints(scip, conshdlr) );
828  }
829 
830  /* add setppc constraints */
831  conshdlr = SCIPfindConshdlr(scip, "setppc");
832  if( conshdlr != NULL )
833  {
834  SCIP_CALL( addSetppcConstraints(scip, conshdlr) );
835  }
836 
837  /* add knapsack constraints */
838  conshdlr = SCIPfindConshdlr(scip, "knapsack");
839  if( conshdlr != NULL )
840  {
841  SCIP_CALL( addKnapsackConstraints(scip, conshdlr) );
842  }
843  }
844 
845  return SCIP_OKAY;
846 }
847 
848 /* creates a SCIP_SOL in our SCIP space out of the solution from NLP solver in sub-SCIP */
849 static
851  SCIP* scip, /**< SCIP data structure */
852  SCIP_HEUR* heur, /**< heuristic data structure */
853  SCIP_SOL** sol /**< buffer to store solution value; if pointing to NULL, then a new solution is created, otherwise values in the given one are overwritten */
854  )
855 {
856  SCIP_HEURDATA* heurdata;
857  SCIP_VAR* var;
858  SCIP_VAR* subvar;
859  int i;
860 
861  assert(scip != NULL);
862  assert(heur != NULL);
863  assert(sol != NULL);
864 
865  heurdata = SCIPheurGetData(heur);
866  assert(heurdata != NULL);
867 
868  if( *sol == NULL )
869  {
870  SCIP_CALL( SCIPcreateSol(scip, sol, heur) );
871  }
872 
873  /* sub-SCIP may have more variables than the number of active (transformed) variables in the main SCIP
874  * since constraint copying may have required the copy of variables that are fixed in the main SCIP
875  */
876  assert(heurdata->nsubvars <= SCIPgetNOrigVars(heurdata->subscip));
877 
878  for( i = 0; i < heurdata->nsubvars; ++i )
879  {
880  var = heurdata->var_subscip2scip[i];
881  if( var == NULL || !SCIPvarIsActive(var) )
882  continue;
883 
884  subvar = SCIPgetOrigVars(heurdata->subscip)[i];
885  assert(subvar != NULL);
886 
887  assert(SCIPvarGetNLPSol(subvar) != SCIP_INVALID); /*lint !e777*/
888  SCIP_CALL( SCIPsetSolVal(scip, *sol, var, SCIPvarGetNLPSol(subvar)) );
889  }
890 
891  return SCIP_OKAY;
892 }
893 
894 /* creates a SCIP_SOL in our SCIP space out of the SCIP_SOL from a sub-SCIP */
895 static
897  SCIP* scip, /**< SCIP data structure */
898  SCIP_HEUR* heur, /**< heuristic data structure */
899  SCIP_SOL** sol, /**< buffer to store solution value; if pointing to NULL, then a new solution is created, otherwise values in the given one are overwritten */
900  SCIP_SOL* subsol /**< solution of sub-SCIP */
901  )
902 {
903  SCIP_HEURDATA* heurdata;
904  int i;
905 
906  assert(scip != NULL);
907  assert(heur != NULL);
908  assert(sol != NULL);
909  assert(subsol != NULL);
910 
911  heurdata = SCIPheurGetData(heur);
912  assert(heurdata != NULL);
913 
914  if( *sol == NULL )
915  {
916  SCIP_CALL( SCIPcreateSol(scip, sol, heur) );
917  }
918 
919  assert(heurdata->nsubvars == SCIPgetNOrigVars(heurdata->subscip));
920  for( i = 0; i < heurdata->nsubvars; ++i )
921  {
922  if( heurdata->var_subscip2scip[i] == NULL || !SCIPvarIsActive(heurdata->var_subscip2scip[i]) )
923  continue;
924  SCIP_CALL( SCIPsetSolVal(scip, *sol, heurdata->var_subscip2scip[i],
925  SCIPgetSolVal(heurdata->subscip, subsol, SCIPgetOrigVars(heurdata->subscip)[i])) );
926  }
927 
928  return SCIP_OKAY;
929 }
930 
931 /* solves the subNLP specified in subscip */
932 static
934  SCIP* scip, /**< original SCIP data structure */
935  SCIP_HEUR* heur, /**< heuristic data structure */
936  SCIP_RESULT* result, /**< buffer to store result, DIDNOTFIND, FOUNDSOL, or CUTOFF */
937  SCIP_SOL* refpoint, /**< point to take fixation of discrete variables from, and startpoint for NLP solver; if NULL, then LP solution is used */
938  SCIP_Longint itercontingent, /**< iteration limit for NLP solver, or -1 for default of NLP heuristic */
939  SCIP_Real timelimit, /**< time limit for NLP solver */
940  SCIP_Longint* iterused, /**< buffer to store number of iterations used by NLP solver, or NULL if not of interest */
941  SCIP_Bool tighttolerances, /**< whether to use tight feasibility tolerances and reduce presolve */
942  SCIP_SOL* resultsol /**< a solution where to store found solution values, if any, or NULL if to try adding to SCIP */
943  )
944 {
945  SCIP_HEURDATA* heurdata;
946  SCIP_RETCODE retcode;
947  SCIP_Real* startpoint;
948  SCIP_VAR* var;
949  SCIP_VAR* subvar;
950  int i;
951 
952  assert(scip != NULL);
953  assert(heur != NULL);
954  assert(result != NULL);
955 
956  heurdata = SCIPheurGetData(heur);
957  assert(heurdata != NULL);
958 
959  if( tighttolerances )
960  {
961  SCIP_Real sumepsilon;
962 
963  /* reduce feasibility tolerance of sub-SCIP and do less aggressive presolve */
964  SCIP_CALL( SCIPsetRealParam(heurdata->subscip, "numerics/feastol", heurdata->resolvetolfactor*SCIPfeastol(scip)) );
965  SCIP_CALL( SCIPsetRealParam(heurdata->subscip, "numerics/epsilon", heurdata->resolvetolfactor*SCIPepsilon(scip)) );
966  SCIP_CALL( SCIPgetRealParam(scip, "numerics/sumepsilon", &sumepsilon) );
967  SCIP_CALL( SCIPsetRealParam(heurdata->subscip, "numerics/sumepsilon", heurdata->resolvetolfactor*sumepsilon) );
968  SCIP_CALL( SCIPsetPresolving(heurdata->subscip, SCIP_PARAMSETTING_FAST, TRUE) );
969 
970  if( !SCIPisParamFixed(heurdata->subscip, "constraints/linear/aggregatevariables") )
971  {
972  SCIP_CALL( SCIPsetBoolParam(heurdata->subscip, "constraints/linear/aggregatevariables", FALSE) );
973  }
974  }
975 
976  /* transform sub-SCIP */
977  SCIP_CALL( SCIPtransformProb(heurdata->subscip) );
978 
979  /* presolve sub-SCIP
980  * set node limit to 1 so that presolve can go
981  * reset maxpresolverounds, in case user changed
982  */
983  SCIP_CALL( SCIPsetLongintParam(heurdata->subscip, "limits/nodes", 1LL) );
984  if( !SCIPisParamFixed(heurdata->subscip, "presolving/maxrounds") )
985  {
986  SCIP_CALL( SCIPsetIntParam(heurdata->subscip, "presolving/maxrounds", heurdata->maxpresolverounds) );
987  }
988  SCIP_CALL( SCIPpresolve(heurdata->subscip) );
989  if( SCIPpressedCtrlC(heurdata->subscip) )
990  {
991  SCIPdebugMessage("SCIP presolve interrupted by user\n");
992  goto CLEANUP;
993  }
994  if( SCIPgetStage(heurdata->subscip) == SCIP_STAGE_SOLVED )
995  {
996  /* presolve probably found the subproblem infeasible */
997  SCIPdebugMessage("SCIP returned from presolve in stage solved with status %d\n", SCIPgetStatus(heurdata->subscip));
998  /* if presolve found subproblem infeasible, report this to caller by setting *result to cutoff */
999  if( SCIPgetStatus(heurdata->subscip) == SCIP_STATUS_INFEASIBLE )
1000  *result = SCIP_CUTOFF;
1001  goto CLEANUP;
1002  }
1003  assert(SCIPgetStage(heurdata->subscip) == SCIP_STAGE_PRESOLVED);
1004 
1005  retcode = SCIP_OKAY;
1006  if( SCIPgetNVars(heurdata->subscip) > 0 )
1007  {
1008  /* do initial solve, i.e., "solve" root node with node limit 0 (should do scip.c::initSolve and then stop immediately in solve.c::SCIPsolveCIP) */
1009  SCIP_CALL( SCIPsetLongintParam(heurdata->subscip, "limits/nodes", 0LL) );
1010  retcode = SCIPsolve(heurdata->subscip);
1011 
1012  /* If no NLP was constructed, then there were no nonlinearities after presolve.
1013  * So we increase the nodelimit to 1 and hope that SCIP will find some solution to this probably linear subproblem.
1014  */
1015  if( retcode == SCIP_OKAY && SCIPgetStage(heurdata->subscip) != SCIP_STAGE_SOLVED && !SCIPisNLPConstructed(heurdata->subscip) )
1016  {
1017  SCIP_CALL( SCIPsetLongintParam(heurdata->subscip, "limits/nodes", 1LL) );
1018  retcode = SCIPsolve(heurdata->subscip);
1019  }
1020  }
1021  else
1022  {
1023  /* If all variables were removed by presolve, but presolve did not end with status SOLVED,
1024  * then we run solve, still with nodelimit=1, and hope to find some (maybe trivial) solution.
1025  */
1026  retcode = SCIPsolve(heurdata->subscip);
1027  }
1028 
1029  /* errors in solving the subproblem should not kill the overall solving process;
1030  * hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop. */
1031  if ( retcode != SCIP_OKAY )
1032  {
1033 #ifndef NDEBUG
1034  SCIP_CALL( retcode );
1035 #endif
1036  SCIPwarningMessage(scip, "Error while solving subproblem in subnlp heuristic; sub-SCIP terminated with code <%d>\n", retcode);
1037  goto CLEANUP;
1038  }
1039 
1040  /* if sub-SCIP found solutions already, then pass them to main scip */
1041  for( i = 0; i < SCIPgetNSols(heurdata->subscip); ++i )
1042  {
1043  SCIP_Bool stored;
1044 
1045  if( resultsol == NULL )
1046  {
1047  SCIP_SOL* sol;
1048 
1049  sol = NULL;
1050  SCIP_CALL( createSolFromSubScipSol(scip, heur, &sol, SCIPgetSols(heurdata->subscip)[i]) );
1051 
1052  SCIP_CALL( SCIPtrySolFree(scip, &sol, FALSE, TRUE, FALSE, TRUE, &stored) );
1053  if( stored )
1054  {
1055  SCIPdebugMessage("SCIP stored solution from sub-SCIP root node\n");
1056  *result = SCIP_FOUNDSOL;
1057  break;
1058  }
1059  else
1060  {
1061  SCIPdebugMessage("SCIP did not store sub-SCIP optimal solution\n");
1062  }
1063  }
1064  else
1065  {
1066  SCIP_CALL( createSolFromSubScipSol(scip, heur, &resultsol, SCIPgetSols(heurdata->subscip)[i]) );
1067 
1068  SCIP_CALL( SCIPcheckSol(scip, resultsol, FALSE, TRUE, FALSE, TRUE, &stored) );
1069  if( stored )
1070  {
1071  SCIPdebugMessage("SCIP solution from sub-SCIP root node is feasible\n");
1072  *result = SCIP_FOUNDSOL;
1073  break;
1074  }
1075  else
1076  {
1077  SCIPdebugMessage("SCIP solution form sub-SCIP root node is not feasible\n");
1078  }
1079  }
1080  }
1081 
1082  /* we should either have variables, or the problem was trivial, in which case it should have been solved */
1083  assert(SCIPgetNVars(heurdata->subscip) > 0 || SCIPgetStage(heurdata->subscip) == SCIP_STAGE_SOLVED);
1084 
1085  /* if subscip is infeasible here, we signal this to the caller */
1086  if( SCIPgetStatus(heurdata->subscip) == SCIP_STATUS_INFEASIBLE )
1087  {
1088  assert(SCIPgetStage(heurdata->subscip) == SCIP_STAGE_SOLVED);
1089  *result = SCIP_CUTOFF;
1090  goto CLEANUP;
1091  }
1092 
1093  /* if we stopped for some other reason, or there is no NLP, we also stop */
1094  if( SCIPgetStage(heurdata->subscip) == SCIP_STAGE_SOLVED || !SCIPisNLPConstructed(heurdata->subscip) )
1095  goto CLEANUP;
1096 
1097  /* in most cases, the status should be nodelimit
1098  * in some cases, if the sub-SCIP is very easy, it may report optimal, so we do not need invoke an NLP solver
1099  * if the presolve found the problem infeasible, then there is no use in solving an NLP
1100  * if the user interrupted or a timelimit was reached, then we should also stop here
1101  * unbounded is very unlikely to happen, in most cases, it should have been concluded in the main scip already
1102  */
1103  switch( SCIPgetStatus(heurdata->subscip) )
1104  {
1105  case SCIP_STATUS_NODELIMIT:
1106  break; /* this is the status that is most likely happening */
1109  case SCIP_STATUS_GAPLIMIT:
1110  case SCIP_STATUS_SOLLIMIT:
1112  /* these should not happen, but if one does, it's save to go to CLEANUP */
1113  SCIPABORT();
1114  case SCIP_STATUS_OPTIMAL:
1115  case SCIP_STATUS_INFEASIBLE:
1117  case SCIP_STATUS_TIMELIMIT:
1118  case SCIP_STATUS_MEMLIMIT:
1119  case SCIP_STATUS_UNBOUNDED:
1120  case SCIP_STATUS_INFORUNBD:
1121  goto CLEANUP;
1122  default:
1123  SCIPerrorMessage("unexpected status of sub-SCIP: <%d>\n", SCIPgetStatus(heurdata->subscip));
1124  return SCIP_ERROR;
1125  } /*lint !e788*/
1126 
1127  /* if NLP timelimit is set to 0.0, then caller just wanted to see if the instance is still feasible after presolve */
1128  if( timelimit == 0.0 )
1129  goto CLEANUP;
1130 
1131  /* add non-combinatorial linear constraints from subscip into subNLP (shall be replaced by catching row events in NLP) */
1132  SCIP_CALL( addLinearConstraintsToNlp(heurdata->subscip, FALSE, TRUE) );
1133 
1134  /* set starting values (=refpoint, if not NULL; otherwise LP solution (or pseudo solution)) */
1135  SCIP_CALL( SCIPallocBufferArray(scip, &startpoint, SCIPgetNNLPVars(heurdata->subscip)) );
1136  for( i = 0; i < SCIPgetNNLPVars(heurdata->subscip); ++i )
1137  {
1138  SCIP_Real scalar;
1139  SCIP_Real constant;
1140 
1141  subvar = SCIPgetNLPVars(heurdata->subscip)[i];
1142 
1143  /* gets corresponding original variable */
1144  scalar = 1.0;
1145  constant = 0.0;
1146  SCIP_CALL( SCIPvarGetOrigvarSum(&subvar, &scalar, &constant) );
1147  if( subvar == NULL )
1148  {
1149  startpoint[i] = constant;
1150  continue;
1151  }
1152 
1153  assert(SCIPvarGetProbindex(subvar) >= 0);
1154  assert(SCIPvarGetProbindex(subvar) < heurdata->nsubvars);
1155  var = heurdata->var_subscip2scip[SCIPvarGetProbindex(subvar)];
1156  if( var == NULL || REALABS(SCIPgetSolVal(scip, refpoint, var)) > 1.0e+12 )
1157  startpoint[i] = MIN(MAX(0.0, SCIPvarGetLbGlobal(subvar)), SCIPvarGetUbGlobal(subvar)); /*lint !e666*/
1158  else
1159  /* scalar*subvar+constant corresponds to nlpvar[i], so nlpvar[i] gets value scalar*varval+constant */
1160  startpoint[i] = scalar * SCIPgetSolVal(scip, refpoint, var) + constant;
1161  }
1162  SCIP_CALL( SCIPsetNLPInitialGuess(heurdata->subscip, startpoint) );
1163 
1164  SCIPfreeBufferArray(scip, &startpoint);
1165 
1166  *result = SCIP_DIDNOTFIND;
1167 
1168  /* setup NLP parameters */
1169 
1170  if( tighttolerances )
1171  {
1172  /* set feasibility tolerance, if tighttolerances is set */
1173  SCIP_CALL( SCIPsetNLPRealPar(heurdata->subscip, SCIP_NLPPAR_FEASTOL, heurdata->resolvetolfactor*SCIPfeastol(scip)) );
1174  }
1175 
1176  /* set option file to use by NLP solver */
1177  if( heurdata->nlpoptfile != NULL && *heurdata->nlpoptfile != '\0' )
1178  {
1179  SCIP_CALL( SCIPsetNLPStringPar(heurdata->subscip, SCIP_NLPPAR_OPTFILE, heurdata->nlpoptfile) );
1180  }
1181 
1182  /* set iteration limit for NLP solver */
1183  if( itercontingent == -1 && heurdata->nlpiterlimit > 0 )
1184  itercontingent = heurdata->nlpiterlimit;
1185  if( itercontingent > 0 )
1186  {
1187  SCIP_CALL( SCIPsetNLPIntPar(heurdata->subscip, SCIP_NLPPAR_ITLIM, (int)MIN(INT_MAX, itercontingent)) );
1188  }
1189 
1190  /* set time limit for NLP solver */
1191  SCIP_CALL( SCIPsetNLPRealPar(heurdata->subscip, SCIP_NLPPAR_TILIM, timelimit) );
1192 
1193  /* set verbosity of NLP solver */
1194  SCIP_CALL( SCIPsetNLPIntPar(heurdata->subscip, SCIP_NLPPAR_VERBLEVEL, heurdata->nlpverblevel) );
1195 
1196 
1197  /* let the NLP solver do its magic */
1198  SCIPdebugMessage("start NLP solve with iteration limit %"SCIP_LONGINT_FORMAT" and timelimit %g\n", itercontingent, timelimit);
1199  SCIP_CALL( SCIPsolveNLP(heurdata->subscip) );
1200 
1201  SCIPdebugMessage("NLP solver returned with termination status %d and solution status %d, objective value is %g\n",
1202  SCIPgetNLPTermstat(heurdata->subscip), SCIPgetNLPSolstat(heurdata->subscip), SCIPgetNLPObjval(heurdata->subscip));
1203 
1204  if( SCIPgetNLPTermstat(heurdata->subscip) >= SCIP_NLPTERMSTAT_MEMERR )
1205  {
1206  /* oops, something did not go well at all */
1207  ++heurdata->nseriousnlpierror;
1209  "NLP solver in subNLP heuristic for problem <%s> returned with bad termination status %d. This was the %d%s successive time.\n",
1210  SCIPgetProbName(scip), SCIPgetNLPTermstat(heurdata->subscip), heurdata->nseriousnlpierror,
1211  heurdata->nseriousnlpierror == 1 ? "st" : heurdata->nseriousnlpierror == 2 ? "nd" : heurdata->nseriousnlpierror == 3 ? "rd" : "th");
1212  if( heurdata->nseriousnlpierror >= 5 )
1213  {
1214  SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, "Will not run NLP heuristic again for this run.\n");
1215  SCIP_CALL( freeSubSCIP(scip, heurdata) );
1216  }
1217  goto CLEANUP;
1218  }
1219  heurdata->nseriousnlpierror = 0;
1220 
1221  SCIP_CALL( SCIPgetNLPStatistics(heurdata->subscip, heurdata->nlpstatistics) );
1222 
1223  if( iterused != NULL )
1224  *iterused += SCIPnlpStatisticsGetNIterations(heurdata->nlpstatistics);
1225  SCIPdebugMessage("NLP solver used %d iterations and %g seconds\n",
1226  SCIPnlpStatisticsGetNIterations(heurdata->nlpstatistics), SCIPnlpStatisticsGetTotalTime(heurdata->nlpstatistics));
1227 
1228  /* NLP solver claims it found a feasible (maybe even optimal) solution
1229  * if the objective value is better than our cutoff, then try to add it
1230  * if we do not plan to add the solution (resultsol != NULL), then also check it if objective value is not better than objlimit
1231  */
1232  if( SCIPgetNLPSolstat(heurdata->subscip) <= SCIP_NLPSOLSTAT_FEASIBLE && (resultsol != NULL || SCIPisLE(scip, SCIPgetNLPObjval(heurdata->subscip), SCIPgetObjlimit(heurdata->subscip))) )
1233  {
1234  if( resultsol == NULL )
1235  {
1236  SCIP_SOL* sol;
1237  SCIP_Bool stored;
1238 
1239  sol = NULL;
1240  SCIP_CALL( createSolFromNLP(scip, heur, &sol) );
1241 
1242  if( heurdata->resolvefromscratch )
1243  {
1244 #ifdef SCIP_DEBUG
1245  /* print the infeasibilities to stdout */
1246  SCIP_CALL( SCIPtrySolFree(scip, &sol, TRUE, TRUE, FALSE, TRUE, &stored) );
1247 #else
1248  SCIP_CALL( SCIPtrySolFree(scip, &sol, FALSE, TRUE, FALSE, TRUE, &stored) );
1249 #endif
1250  }
1251  else
1252  {
1253 #ifdef SCIP_DEBUG
1254  /* print the infeasibilities to stdout */
1255  SCIP_CALL( SCIPtrySol(scip, sol, TRUE, TRUE, FALSE, TRUE, &stored) );
1256 #else
1257  SCIP_CALL( SCIPtrySol(scip, sol, FALSE, TRUE, FALSE, TRUE, &stored) );
1258 #endif
1259  }
1260 
1261  if( stored )
1262  { /* SCIP stored solution (yippi!), so we are done */
1263  SCIPdebugMessage("SCIP stored solution\n");
1264  *result = SCIP_FOUNDSOL;
1265  }
1266  else if( !tighttolerances && heurdata->resolvetolfactor < 1.0 )
1267  {
1268  /* if SCIP does not like solution, we try again with tighter tolerances recreate subproblem and resolve with tighter tolerances */
1269  SCIPdebugMessage("solution reported by NLP solver not feasible for SCIP, resolve with feasibility tolerance %g and epsilon %g\n", heurdata->resolvetolfactor*SCIPfeastol(scip), heurdata->resolvetolfactor*SCIPepsilon(scip));
1270 
1271  /* free transformed problem */
1272  SCIP_CALL( SCIPfreeTransform(heurdata->subscip) );
1273 
1274  SCIP_CALL( solveSubNLP(scip, heur, result, heurdata->resolvefromscratch ? refpoint : sol, itercontingent, timelimit, iterused, TRUE, resultsol) );
1275  }
1276  else
1277  {
1278  SCIPdebugMessage("solution reported by NLP solver not stored by SCIP\n");
1279  }
1280 
1281  if( sol != NULL )
1282  {
1283  SCIP_CALL( SCIPfreeSol(scip, &sol) );
1284  }
1285  }
1286  else
1287  {
1288  SCIP_Bool feasible;
1289 
1290  SCIP_CALL( createSolFromNLP(scip, heur, &resultsol) );
1291 
1292 #ifdef SCIP_DEBUG
1293  /* print the infeasibilities to stdout */
1294  SCIP_CALL( SCIPcheckSol(scip, resultsol, TRUE, TRUE, FALSE, TRUE, &feasible) );
1295 #else
1296  SCIP_CALL( SCIPcheckSol(scip, resultsol, FALSE, TRUE, FALSE, TRUE, &feasible) );
1297 #endif
1298  if( feasible )
1299  {
1300  /* SCIP find solution feasible, so we are done */
1301  SCIPdebugMessage("solution reported by NLP solver feasible for SCIP\n");
1302  *result = SCIP_FOUNDSOL;
1303  }
1304  else if( !tighttolerances && heurdata->resolvetolfactor < 1.0 )
1305  {
1306  /* if SCIP does not like solution, we try again with tighter tolerances
1307  * recreate subproblem and resolve with tighter tolerances
1308  */
1309  SCIPdebugMessage("solution reported by NLP solver not feasible for SCIP, resolve with feasibility tolerance %g and epsilon %g\n", heurdata->resolvetolfactor*SCIPfeastol(scip), heurdata->resolvetolfactor*SCIPepsilon(scip));
1310 
1311  /* free transformed problem */
1312  SCIP_CALL( SCIPfreeTransform(heurdata->subscip) );
1313 
1314  SCIP_CALL( solveSubNLP(scip, heur, result, heurdata->resolvefromscratch ? refpoint : resultsol, itercontingent, timelimit, iterused, TRUE, resultsol) );
1315  }
1316  else
1317  {
1318  SCIPdebugMessage("solution reported by NLP solver not feasible for SCIP\n");
1319  }
1320  }
1321  }
1322 
1323  CLEANUP:
1324  if( heurdata->subscip != NULL )
1325  {
1326  SCIP_CALL( SCIPfreeTransform(heurdata->subscip) );
1327  if( tighttolerances )
1328  {
1329  SCIP_Real sumepsilon;
1330 
1331  /* reset feasibility tolerance of sub-SCIP and reset to normal presolve */
1332  SCIP_CALL( SCIPsetRealParam(heurdata->subscip, "numerics/feastol", SCIPfeastol(scip)) );
1333  SCIP_CALL( SCIPsetRealParam(heurdata->subscip, "numerics/epsilon", SCIPepsilon(scip)) );
1334  SCIP_CALL( SCIPgetRealParam(scip, "numerics/sumepsilon", &sumepsilon) );
1335  SCIP_CALL( SCIPsetRealParam(heurdata->subscip, "numerics/sumepsilon", sumepsilon) );
1337  SCIP_CALL( SCIPresetParam(heurdata->subscip, "constraints/linear/aggregatevariables") );
1338  }
1339  }
1340 
1341  if( iterused != NULL && *iterused == 0 )
1342  *iterused = 1;
1343 
1344  return SCIP_OKAY;
1345 }
1346 
1347 
1348 /** adds a set covering or bound disjunction constraint to the original problem */
1349 static
1351  SCIP* scip, /**< SCIP data structure */
1352  SCIP_HEURDATA* heurdata /**< heuristic data */
1353  )
1354 {
1355  SCIP_VAR** subvars;
1356  int nsubvars;
1357  int nsubbinvars;
1358  int nsubintvars;
1359  SCIP_VAR* var;
1360  SCIP_VAR* subvar;
1361  SCIP_CONS* cons;
1362  SCIP_VAR** consvars;
1363  int nconsvars;
1364  char name[SCIP_MAXSTRLEN];
1365  int i;
1366  SCIP_Real fixval;
1367 
1368  assert(scip != NULL);
1369 
1370  SCIP_CALL( SCIPgetOrigVarsData(heurdata->subscip, &subvars, &nsubvars, &nsubbinvars, &nsubintvars, NULL, NULL) );
1371  assert(nsubvars == heurdata->nsubvars);
1372 
1373  if( nsubbinvars == 0 && nsubintvars == 0 )
1374  {
1375  /* If we did not fix any discrete variables but found the "sub"CIP infeasible, then also the CIP is infeasible. */
1376  SCIPwarningMessage(scip, "heur_subnlp found subCIP infeasible after fixing no variables, something is strange here...\n");
1377  return SCIP_OKAY;
1378  }
1379 
1380  /* initialize */
1381  cons = NULL;
1382  consvars = NULL;
1383 
1384  /* create constraint name */
1385  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "subnlp_cutoff");
1386 
1387  /* if all discrete variables in the CIP are binary, then we create a set covering constraint
1388  * sum_{x_i fixed at 0} x_i + sum_{x_i fixed at 1} ~x_i >= 1
1389  */
1390  if( nsubintvars == 0 )
1391  {
1392  /* allocate memory for constraint variables */
1393  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nsubbinvars) );
1394 
1395  /* get fixations of discrete variables
1396  * to be sure, we take the values that were put into the subCIP before
1397  */
1398  nconsvars = 0;
1399  for( i = nsubbinvars - 1; i >= 0; --i )
1400  {
1401  subvar = subvars[i];
1402  assert(SCIPvarGetProbindex(subvar) == i);
1403 
1404  var = heurdata->var_subscip2scip[i];
1405  assert(var != NULL || SCIPisEQ(scip, SCIPvarGetLbGlobal(subvar), SCIPvarGetUbGlobal(subvar))); /* otherwise we should have exited in the variable fixation loop */
1406  if( var == NULL )
1407  continue;
1408 
1409  fixval = SCIPvarGetLbGlobal(subvar);
1410  assert(fixval == SCIPvarGetUbGlobal(subvar)); /* variable should be fixed in sub-SCIP */ /*lint !e777*/
1411  assert(fixval == 0.0 || fixval == 1.0); /* we have rounded values before fixing */
1412 
1413  if( fixval == 0.0 )
1414  {
1415  /* variable fixed at lower bound */
1416  consvars[nconsvars] = var;
1417  }
1418  else
1419  {
1420  SCIP_CALL( SCIPgetNegatedVar(scip, var, &consvars[nconsvars]) );
1421  }
1422 
1423  ++nconsvars;
1424  }
1425 
1426  /* create conflict constraint
1427  * In undercover, ConsLogicor is used, since then the inequality is not added to the LP.
1428  * However, I may want to use Setcover to avoid that the same fixing is computed by some LP based heuristic again.
1429  */
1430  SCIP_CALL( SCIPcreateConsSetcover(scip, &cons, name, nconsvars, consvars,
1432  }
1433  else
1434  {
1435  /* if there are also integer variable, then create a bound disjunction constraint
1436  * x_1 >= fixval_1 + 1 || x_1 <= fixval_1 - 1 || x_2 >= fixval_2 + 1 || x_2 <= fixval_2 - 1 || ...
1437  */
1438  SCIP_BOUNDTYPE* boundtypes;
1439  SCIP_Real* bounds;
1440 
1441  /* allocate memory for constraint variables, boundtypes, and bounds
1442  * (there should be at most two literals for each integer variable)
1443  */
1444  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nsubbinvars + 2*nsubintvars) );
1445  SCIP_CALL( SCIPallocBufferArray(scip, &boundtypes, nsubbinvars + 2*nsubintvars) );
1446  SCIP_CALL( SCIPallocBufferArray(scip, &bounds, nsubbinvars + 2*nsubintvars) );
1447 
1448  /* get fixations of discrete variables
1449  * to be sure, we take the values that were put into the subCIP before
1450  */
1451  nconsvars = 0;
1452  for( i = nsubbinvars + nsubintvars - 1; i >= 0; --i )
1453  {
1454  subvar = subvars[i];
1455  assert(SCIPvarGetProbindex(subvar) == i);
1456 
1457  var = heurdata->var_subscip2scip[i];
1458  assert(var != NULL || SCIPisEQ(scip, SCIPvarGetLbGlobal(subvar), SCIPvarGetUbGlobal(subvar))); /* otherwise we should have exited in the variable fixation loop */
1459 
1460  if( var == NULL )
1461  continue;
1462 
1463  fixval = SCIPvarGetLbGlobal(subvar);
1464  assert(fixval == SCIPvarGetUbGlobal(subvar)); /* variable should be fixed in sub-SCIP */ /*lint !e777*/
1465  assert((int)fixval == fixval); /* we have rounded values before fixing */
1466  assert(SCIPvarGetType(var) != SCIP_VARTYPE_BINARY || SCIPvarGetLbGlobal(var) == fixval || SCIPvarGetUbGlobal(var) == fixval); /* for binaries, the fixval should be either 0.0 or 1.0 */ /*lint !e777*/
1467 
1468  if( SCIPvarGetLbGlobal(var) < fixval )
1469  {
1470  assert(nconsvars < nsubbinvars + 2*nsubintvars);
1471 
1472  /* literal x_i <= fixval-1 */
1473  boundtypes[nconsvars] = SCIP_BOUNDTYPE_UPPER;
1474  bounds[nconsvars] = fixval - 1.0;
1475  consvars[nconsvars] = var;
1476  ++nconsvars;
1477  }
1478 
1479  if( SCIPvarGetUbGlobal(var) > fixval )
1480  {
1481  assert(nconsvars < nsubbinvars + 2*nsubintvars);
1482 
1483  /* literal x_i >= fixval+1 */
1484  boundtypes[nconsvars] = SCIP_BOUNDTYPE_LOWER;
1485  bounds[nconsvars] = fixval + 1.0;
1486  consvars[nconsvars] = var;
1487  ++nconsvars;
1488  }
1489  }
1490 
1491  /* create conflict constraint */
1492  SCIP_CALL( SCIPcreateConsBounddisjunction(scip, &cons, name, nconsvars, consvars, boundtypes, bounds,
1494 
1495  SCIPfreeBufferArray(scip, &consvars);
1496  SCIPfreeBufferArray(scip, &boundtypes);
1497  SCIPfreeBufferArray(scip, &bounds);
1498  }
1499 
1500  /* add and release constraint if created successfully */
1501  if( cons != NULL )
1502  {
1503  SCIPdebugMessage("adding constraint to forbid fixation in main problem\n");
1504  /* SCIPdebugPrintCons(scip, cons, NULL); */
1505  SCIP_CALL( SCIPaddCons(scip, cons) );
1506  SCIP_CALL( SCIPreleaseCons(scip, &cons) );
1507  }
1508 
1509  /* free memory */
1510  SCIPfreeBufferArrayNull(scip, &consvars);
1511 
1512  return SCIP_OKAY;
1513 }
1514 
1515 
1516 /** main procedure of the subNLP heuristic */
1518  SCIP* scip, /**< original SCIP data structure */
1519  SCIP_HEUR* heur, /**< heuristic data structure */
1520  SCIP_RESULT* result, /**< pointer to store result of: did not run, solution found, no solution found, or fixing is infeasible (cutoff) */
1521  SCIP_SOL* refpoint, /**< point to take fixation of discrete variables from, and startpoint for NLP solver; if NULL, then LP solution is used */
1522  SCIP_Longint itercontingent, /**< iteration limit for NLP solver, or -1 for default of NLP heuristic */
1523  SCIP_Real timelimit, /**< time limit for NLP solver */
1524  SCIP_Real minimprove, /**< desired minimal relative improvement in objective function value */
1525  SCIP_Longint* iterused /**< buffer to store number of iterations used by NLP solver, or NULL if not of interest */
1526  )
1527 {
1528  SCIP_HEURDATA* heurdata;
1529  SCIP_VAR* var;
1530  SCIP_VAR* subvar;
1531  int i;
1532  SCIP_Real cutoff;
1533 
1534  assert(scip != NULL);
1535  assert(heur != NULL);
1536 
1537  /* get heuristic's data */
1538  heurdata = SCIPheurGetData(heur);
1539  assert(heurdata != NULL);
1540 
1541  /* try to setup NLP if not tried before */
1542  if( heurdata->subscip == NULL && !heurdata->triedsetupsubscip )
1543  {
1544  SCIP_CALL( createSubSCIP(scip, heurdata) );
1545  }
1546 
1547  *result = SCIP_DIDNOTRUN;
1548 
1549  /* not initialized */
1550  if( heurdata->subscip == NULL )
1551  return SCIP_OKAY;
1552 
1553  assert(heurdata->nsubvars > 0);
1554  assert(heurdata->var_subscip2scip != NULL);
1555  assert(!SCIPisTransformed(heurdata->subscip));
1556 
1557  if( iterused != NULL )
1558  *iterused = 0;
1559 
1560  /* fix discrete variables in sub-SCIP */
1561  if( SCIPgetNBinVars(heurdata->subscip) || SCIPgetNIntVars(heurdata->subscip) )
1562  {
1563  SCIP_Real fixval;
1564  SCIP_VAR** subvars;
1565  int nsubvars;
1566  int nsubbinvars;
1567  int nsubintvars;
1568 
1569  SCIP_CALL( SCIPgetOrigVarsData(heurdata->subscip, &subvars, &nsubvars, &nsubbinvars, &nsubintvars, NULL, NULL) );
1570  assert(nsubvars == heurdata->nsubvars);
1571 
1572  /* fix discrete variables to values in startpoint */
1573  for( i = nsubbinvars + nsubintvars - 1; i >= 0; --i )
1574  {
1575  subvar = subvars[i];
1576  assert(SCIPvarGetProbindex(subvar) == i);
1577 
1578  var = heurdata->var_subscip2scip[i];
1579  assert(var != NULL);
1580 
1581  /* at this point, variables in subscip and in our scip should have same bounds */
1582  assert(SCIPisEQ(scip, SCIPvarGetLbGlobal(subvar), SCIPvarGetLbGlobal(var)));
1583  assert(SCIPisEQ(scip, SCIPvarGetUbGlobal(subvar), SCIPvarGetUbGlobal(var)));
1584 
1585  fixval = SCIPgetSolVal(scip, refpoint, var);
1586 
1587  /* only run heuristic on integer feasible points */
1588  if( !SCIPisFeasIntegral(scip, fixval) )
1589  {
1590  if( refpoint || SCIPgetLPSolstat(scip) == SCIP_LPSOLSTAT_OPTIMAL )
1591  {
1592  SCIPdebugMessage("skip NLP heuristic because start candidate not integer feasible: var <%s> has value %g\n", SCIPvarGetName(var), fixval);
1593  goto CLEANUP;
1594  }
1595  /* otherwise we desperately wanna run the NLP heur, so we continue and round what we have */
1596  }
1597  /* if we do not really have a startpoint, then we should take care that we do not fix variables to very large values
1598  * thus, we set to 0.0 here and project on bounds below
1599  */
1600  if( ABS(fixval) > 1E+10 && !refpoint && SCIPgetLPSolstat(scip) != SCIP_LPSOLSTAT_OPTIMAL )
1601  fixval = 0.0;
1602 
1603  /* round fractional variables to the nearest integer,
1604  * use exact integral value, if the variable is only integral within numerical tolerances
1605  */
1606  fixval = SCIPfloor(scip, fixval+0.5);
1607 
1608  /* adjust value to the global bounds of the corresponding SCIP variable */
1609  fixval = MAX(fixval, SCIPvarGetLbGlobal(var)); /*lint !e666*/
1610  fixval = MIN(fixval, SCIPvarGetUbGlobal(var)); /*lint !e666*/
1611 
1612  /* SCIPdebugMessage("fix variable <%s> to %g\n", SCIPvarGetName(var), fixval); */
1613  SCIP_CALL( SCIPchgVarLbGlobal(heurdata->subscip, subvar, fixval) );
1614  SCIP_CALL( SCIPchgVarUbGlobal(heurdata->subscip, subvar, fixval) );
1615  }
1616  }
1617 
1618  /* if there is already a solution, add an objective cutoff in sub-SCIP */
1619  if( SCIPgetNSols(scip) > 0 )
1620  {
1621  SCIP_Real upperbound;
1622 
1623  cutoff = SCIPinfinity(scip);
1624  assert( !SCIPisInfinity(scip, SCIPgetUpperbound(scip)) );
1625 
1626  upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip);
1627 
1628  if( !SCIPisInfinity(scip, -SCIPgetLowerbound(scip)) )
1629  {
1630  cutoff = (1-minimprove)*SCIPgetUpperbound(scip) + minimprove*SCIPgetLowerbound(scip);
1631  }
1632  else
1633  {
1634  if( SCIPgetUpperbound(scip) >= 0 )
1635  cutoff = ( 1.0 - minimprove ) * SCIPgetUpperbound(scip);
1636  else
1637  cutoff = ( 1.0 + minimprove ) * SCIPgetUpperbound(scip);
1638  }
1639  cutoff = MIN(upperbound, cutoff);
1640  SCIP_CALL( SCIPsetObjlimit(heurdata->subscip, cutoff) );
1641  SCIPdebugMessage("set objective limit %g\n", cutoff);
1642  }
1643  else
1644  cutoff = SCIPinfinity(scip);
1645 
1646  /* solve the subNLP and try to add solution to SCIP */
1647  SCIP_CALL( solveSubNLP(scip, heur, result, refpoint, itercontingent, timelimit, iterused, FALSE, NULL) );
1648 
1649  if( heurdata->subscip == NULL )
1650  {
1651  /* something horrible must have happened that we decided to give up completely on this heuristic */
1652  *result = SCIP_DIDNOTFIND;
1653  return SCIP_OKAY;
1654  }
1655  assert(!SCIPisTransformed(heurdata->subscip));
1656 
1657  if( *result == SCIP_CUTOFF )
1658  {
1659  if( heurdata->subscipisvalid && SCIPgetNActivePricers(scip) == 0 )
1660  {
1661  /* if the subNLP is valid and turned out to be globally infeasible (i.e., proven by SCIP), then we forbid this fixation in the main problem */
1662  if( SCIPisInfinity(scip, cutoff) && heurdata->forbidfixings )
1663  {
1664  SCIP_CALL( forbidFixation(scip, heurdata) );
1665  }
1666  }
1667  else
1668  {
1669  /* if the subNLP turned out to be globally infeasible but we are not sure that we have a valid copy, we change to DIDNOTFIND */
1670  *result = SCIP_DIDNOTFIND;
1671  }
1672  }
1673 
1674  CLEANUP:
1675  /* if the heuristic was applied before solving has started, then destroy subSCIP, since EXITSOL may not be called
1676  * also if keepcopy is disabled, then destroy subSCIP
1677  */
1678  if( SCIPgetStage(scip) < SCIP_STAGE_SOLVING || !heurdata->keepcopy )
1679  {
1680  SCIP_CALL( freeSubSCIP(scip, heurdata) );
1681  heurdata->triedsetupsubscip = FALSE;
1682  }
1683  else if( SCIPgetNBinVars(heurdata->subscip) || SCIPgetNIntVars(heurdata->subscip) )
1684  {
1685  /* undo fixing of discrete variables in sub-SCIP */
1686  SCIP_VAR** subvars;
1687  int nsubvars;
1688  int nsubbinvars;
1689  int nsubintvars;
1690 
1691  SCIP_CALL( SCIPgetOrigVarsData(heurdata->subscip, &subvars, &nsubvars, &nsubbinvars, &nsubintvars, NULL, NULL) );
1692  assert(nsubvars == heurdata->nsubvars);
1693 
1694  /* set bounds of discrete variables to original values */
1695  for( i = nsubbinvars + nsubintvars - 1; i >= 0; --i )
1696  {
1697  subvar = subvars[i];
1698  assert(SCIPvarGetProbindex(subvar) == i);
1699 
1700  var = heurdata->var_subscip2scip[i];
1701  assert(var != NULL);
1702 
1703  SCIP_CALL( SCIPchgVarLbGlobal(heurdata->subscip, subvar, SCIPvarGetLbGlobal(var)) );
1704  SCIP_CALL( SCIPchgVarUbGlobal(heurdata->subscip, subvar, SCIPvarGetUbGlobal(var)) );
1705  }
1706  }
1707 
1708  return SCIP_OKAY;
1709 }
1710 
1711 /** for a given solution, resolves the corresponding subNLP and updates solution values for continuous variables, if NLP solution is feasible in original problem */
1713  SCIP* scip, /**< original SCIP data structure */
1714  SCIP_HEUR* heur, /**< heuristic data structure */
1715  SCIP_SOL* sol, /**< solution for which to solve NLP, and where to store resolved solution values */
1716  SCIP_Bool* success, /**< buffer where to store whether a feasible solution was found */
1717  SCIP_Longint itercontingent, /**< iteration limit for NLP solver, or -1 for default of NLP heuristic */
1718  SCIP_Real timelimit /**< time limit for NLP solver */
1719  )
1720 {
1721  SCIP_HEURDATA* heurdata;
1722  SCIP_VAR* var;
1723  SCIP_VAR* subvar;
1724  int i;
1725  SCIP_Real cutoff;
1726  SCIP_RESULT result;
1727 
1728  assert(scip != NULL);
1729  assert(heur != NULL);
1730  assert(sol != NULL);
1731  assert(success != NULL);
1732 
1733  /* get heuristic's data */
1734  heurdata = SCIPheurGetData(heur);
1735  assert(heurdata != NULL);
1736 
1737  /* try to setup NLP if not tried before */
1738  if( heurdata->subscip == NULL && !heurdata->triedsetupsubscip )
1739  {
1740  SCIP_CALL( createSubSCIP(scip, heurdata) );
1741  }
1742 
1743  *success = FALSE;
1744 
1745  /* not initialized */
1746  if( heurdata->subscip == NULL )
1747  return SCIP_OKAY;
1748 
1749  assert(heurdata->nsubvars > 0);
1750  assert(heurdata->var_subscip2scip != NULL);
1751  assert(!SCIPisTransformed(heurdata->subscip));
1752 
1753  result = SCIP_DIDNOTRUN;
1754 
1755  /* fix discrete variables in subSCIP */
1756  if( SCIPgetNBinVars(heurdata->subscip) || SCIPgetNIntVars(heurdata->subscip) )
1757  {
1758  SCIP_Real fixval;
1759  SCIP_VAR** subvars;
1760  int nsubvars;
1761  int nsubbinvars;
1762  int nsubintvars;
1763 
1764  SCIP_CALL( SCIPgetOrigVarsData(heurdata->subscip, &subvars, &nsubvars, &nsubbinvars, &nsubintvars, NULL, NULL) );
1765  assert(nsubvars == heurdata->nsubvars);
1766 
1767  /* fix discrete variables to values in startpoint */
1768  for( i = nsubbinvars + nsubintvars - 1; i >= 0; --i )
1769  {
1770  subvar = subvars[i];
1771  assert(SCIPvarGetProbindex(subvar) == i);
1772 
1773  var = heurdata->var_subscip2scip[i];
1774  assert(var != NULL);
1775 
1776  /* at this point, variables in subscip and in our scip should have same bounds */
1777  assert(SCIPisEQ(scip, SCIPvarGetLbGlobal(subvar), SCIPvarGetLbGlobal(var)));
1778  assert(SCIPisEQ(scip, SCIPvarGetUbGlobal(subvar), SCIPvarGetUbGlobal(var)));
1779 
1780  fixval = SCIPgetSolVal(scip, sol, var);
1781 
1782  /* only run heuristic on integer feasible points */
1783  if( !SCIPisFeasIntegral(scip, fixval) )
1784  {
1785  SCIPdebugMessage("skip NLP heuristic because start candidate not integer feasible: var <%s> has value %g\n", SCIPvarGetName(var), fixval);
1786  goto CLEANUP;
1787  /* otherwise we desperately want to run the NLP heur, so we continue and round what we have */
1788  }
1789 
1790  /* round fractional variables to the nearest integer,
1791  * use exact integral value, if the variable is only integral within numerical tolerances
1792  */
1793  fixval = SCIPround(scip, fixval);
1794 
1795  /* adjust value to the global bounds of the corresponding SCIP variable */
1796  fixval = MAX(fixval, SCIPvarGetLbGlobal(var)); /*lint !e666*/
1797  fixval = MIN(fixval, SCIPvarGetUbGlobal(var)); /*lint !e666*/
1798 
1799  /* SCIPdebugMessage("fix variable <%s> to %g\n", SCIPvarGetName(var), fixval); */
1800  SCIP_CALL( SCIPchgVarLbGlobal(heurdata->subscip, subvar, fixval) );
1801  SCIP_CALL( SCIPchgVarUbGlobal(heurdata->subscip, subvar, fixval) );
1802  }
1803  }
1804 
1805  /* if there is already a solution, add an objective cutoff in subSCIP */
1806  cutoff = SCIPgetSolOrigObj(scip, sol);
1808  {
1809  cutoff += 0.01 * REALABS(cutoff);
1810  }
1811  else
1812  {
1813  cutoff -= 0.01 * REALABS(cutoff);
1814  }
1815  cutoff = SCIPtransformObj(scip, cutoff);
1816  SCIPdebugMessage("set objective limit %g\n", cutoff);
1817  SCIP_CALL( SCIPsetObjlimit(heurdata->subscip, cutoff) );
1818 
1819  /* solve the subNLP and try to add solution to SCIP */
1820  SCIP_CALL( solveSubNLP(scip, heur, &result, sol, itercontingent, timelimit, NULL, FALSE, sol) );
1821 
1822  if( heurdata->subscip == NULL )
1823  {
1824  /* something horrible must have happened that we decided to give up completely on this heuristic */
1825  return SCIP_OKAY;
1826  }
1827  assert(!SCIPisTransformed(heurdata->subscip));
1828 
1829  if( result == SCIP_FOUNDSOL )
1830  *success = TRUE;
1831 
1832  CLEANUP:
1833  /* if the heuristic was applied before solving has started, then destroy subSCIP, since EXITSOL may not be called
1834  * also if keepcopy is not set, then destroy subSCIP
1835  */
1836  if( SCIPgetStage(scip) < SCIP_STAGE_SOLVING || !heurdata->keepcopy )
1837  {
1838  SCIP_CALL( freeSubSCIP(scip, heurdata) );
1839  heurdata->triedsetupsubscip = FALSE;
1840  }
1841  else if( SCIPgetNBinVars(heurdata->subscip) || SCIPgetNIntVars(heurdata->subscip) )
1842  {
1843  /* undo fixing of discrete variables in subSCIP */
1844  SCIP_VAR** subvars;
1845  int nsubvars;
1846  int nsubbinvars;
1847  int nsubintvars;
1848 
1849  SCIP_CALL( SCIPgetOrigVarsData(heurdata->subscip, &subvars, &nsubvars, &nsubbinvars, &nsubintvars, NULL, NULL) );
1850  assert(nsubvars == heurdata->nsubvars);
1851 
1852  /* set bounds of discrete variables to original values */
1853  for( i = nsubbinvars + nsubintvars - 1; i >= 0; --i )
1854  {
1855  subvar = subvars[i];
1856  assert(SCIPvarGetProbindex(subvar) == i);
1857 
1858  var = heurdata->var_subscip2scip[i];
1859  assert(var != NULL);
1860 
1861  SCIP_CALL( SCIPchgVarLbGlobal(heurdata->subscip, subvar, SCIPvarGetLbGlobal(var)) );
1862  SCIP_CALL( SCIPchgVarUbGlobal(heurdata->subscip, subvar, SCIPvarGetUbGlobal(var)) );
1863  }
1864  }
1865 
1866  return SCIP_OKAY;
1867 }
1868 
1869 /*
1870  * Callback methods of primal heuristic
1871  */
1872 
1873 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
1874 static
1875 SCIP_DECL_HEURCOPY(heurCopySubNlp)
1876 { /*lint --e{715}*/
1877  assert(scip != NULL);
1878  assert(heur != NULL);
1879  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
1880 
1881  /* call inclusion method of primal heuristic */
1883 
1884  return SCIP_OKAY;
1885 }
1886 
1887 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
1888 static
1889 SCIP_DECL_HEURFREE(heurFreeSubNlp)
1890 {
1891  SCIP_HEURDATA* heurdata;
1892  assert(scip != NULL);
1893  assert(heur != NULL);
1894 
1895  heurdata = SCIPheurGetData(heur);
1896  assert(heurdata != NULL);
1897  assert(heurdata->subscip == NULL);
1898  assert(heurdata->var_subscip2scip == NULL);
1899  assert(heurdata->var_scip2subscip == NULL);
1900  assert(heurdata->startcand == NULL);
1901 
1902  SCIPfreeMemory(scip, &heurdata);
1903 
1904  return SCIP_OKAY;
1905 }
1906 
1907 /** solving process initialization method of primal heuristic (called when branch and bound process is about to begin) */
1908 static
1909 SCIP_DECL_HEURINITSOL(heurInitsolSubNlp)
1910 {
1911  SCIP_HEURDATA* heurdata;
1912 
1913  assert(scip != NULL);
1914  assert(heur != NULL);
1915 
1916  /* skip setting up sub-SCIP if heuristic is disabled or we do not want to run the heuristic */
1917  if( SCIPheurGetFreq(heur) < 0 || !runHeuristic(scip) )
1918  return SCIP_OKAY;
1919 
1920  heurdata = SCIPheurGetData(heur);
1921  assert(heurdata != NULL);
1922  assert(heurdata->subscip == NULL);
1923 
1924  if( heurdata->keepcopy )
1925  {
1926  /* create sub-SCIP for later use */
1927  SCIP_CALL( createSubSCIP(scip, heurdata) );
1928 
1929  /* creating sub-SCIP may fail if the NLP solver interfaces did not copy into subscip */
1930  if( heurdata->subscip == NULL )
1931  return SCIP_OKAY;
1932  }
1933 
1934  /* if the heuristic is called at the root node, we want to be called directly after the initial root LP solve */
1935  if( SCIPheurGetFreqofs(heur) == 0 )
1937 
1938  return SCIP_OKAY;
1939 }
1940 
1941 /** solving process deinitialization method of primal heuristic (called before branch and bound process data is freed) */
1942 static
1943 SCIP_DECL_HEUREXITSOL(heurExitsolSubNlp)
1944 {
1945  SCIP_HEURDATA* heurdata;
1946  assert(scip != NULL);
1947  assert(heur != NULL);
1948 
1949  /* get heuristic's data */
1950  heurdata = SCIPheurGetData(heur);
1951  assert(heurdata != NULL);
1952 
1953  if( heurdata->subscip != NULL )
1954  {
1955  SCIP_CALL( freeSubSCIP(scip, heurdata) );
1956  }
1957 
1958  /* free start candidate */
1959  if( heurdata->startcand != NULL )
1960  {
1961  SCIP_CALL( SCIPfreeSol(scip, &heurdata->startcand) );
1962  }
1963 
1965 
1966  /* reset some flags and counters */
1967  heurdata->triedsetupsubscip = FALSE;
1968  heurdata->comblinearconsadded = FALSE;
1969  heurdata->contlinearconsadded = FALSE;
1970  heurdata->nseriousnlpierror = 0;
1971  heurdata->iterused = 0;
1972 
1973  return SCIP_OKAY;
1974 }
1975 
1976 
1977 /** execution method of primal heuristic */
1978 static
1979 SCIP_DECL_HEUREXEC(heurExecSubNlp)
1980 { /*lint --e{666,715}*/
1981  SCIP_HEURDATA* heurdata;
1982  SCIP_Longint itercontingent;
1983  SCIP_Real timelimit;
1984  SCIP_Longint iterused;
1985 
1986  assert(scip != NULL);
1987  assert(heur != NULL);
1988 
1989  /* obviously, we did not do anything yet */
1990  *result = SCIP_DIDNOTRUN;
1991 
1992  /* get heuristic's data */
1993  heurdata = SCIPheurGetData(heur);
1994  assert(heurdata != NULL);
1995 
1996  /* if keepcopy and subscip == NULL, then InitsolNlp decided that we do not need an NLP solver,
1997  * probably because we do not have nonlinear continuous or implicit integer variables
1998  * if triedsetupsubscip and subscip == NULL, then we run the heuristic already, but gave up due to some serious error
1999  * in both cases, we do not want to run
2000  *
2001  * otherwise, we continue and let SCIPapplyHeurSubNlp try to create subscip
2002  */
2003  if( heurdata->subscip == NULL && (heurdata->keepcopy || heurdata->triedsetupsubscip) )
2004  return SCIP_OKAY;
2005 
2006  /* if we recreate the subSCIP in every run, then also check whether we want to run the heuristic at all */
2007  if( !heurdata->keepcopy && !runHeuristic(scip) )
2008  return SCIP_OKAY;
2009 
2010  if( heurdata->startcand == NULL )
2011  {
2012  /* if no start candidate is given, we consider the LP solution of the current node */
2013 
2014  /* however, if the node was already detected to be infeasible, then there is no point to look at its LP solution */
2015  if( nodeinfeasible )
2016  return SCIP_OKAY;
2017 
2018  /* at least if we are not called the first time, we call the heuristic only if an optimal LP solution is available
2019  * if we are called the first time and the LP is unbounded, then we are quite desperate and still give the NLP a try
2020  */
2022  {
2023  if( SCIPgetNNodes(scip) > 1 || SCIPgetLPSolstat(scip) != SCIP_LPSOLSTAT_UNBOUNDEDRAY )
2024  {
2025  *result = SCIP_DELAYED;
2026  SCIPdebugMessage("NLP heuristic delayed because no start candidate given and no LP solution available; LP status = %d\n", SCIPgetLPSolstat(scip));
2027  return SCIP_OKAY;
2028  }
2029  else
2030  {
2031  SCIPdebugMessage("LP is unbounded in root node, so we are quite desperate; run NLP heuristic and pray\n");
2032  }
2033  }
2034  else if( SCIPgetNLPBranchCands(scip) > 0 )
2035  {
2036  /* only call heuristic, if there are no fractional variables */
2037  *result = SCIP_DELAYED;
2038  SCIPdebugMessage("NLP heuristic delayed because no start candidate given and current LP solution is fractional\n");
2039  return SCIP_OKAY;
2040  }
2041  else if( !SCIPisInfinity(scip, SCIPgetPrimalbound(scip)) && SCIPisEQ(scip, SCIPgetLocalDualbound(scip), SCIPgetPrimalbound(scip)) )
2042  {
2043  /* only call heuristic, if there is still room for improvement in the current node */
2044  SCIPdebugMessage("NLP heuristic delayed because lower and upper bound coincide in current node\n");
2045  return SCIP_OKAY;
2046  }
2047  SCIPdebugMessage("using current LP solution as startcand\n");
2048  }
2049  else
2050  {
2051  SCIPdebugMessage("have startcand from heur %s\n", SCIPsolGetHeur(heurdata->startcand) ? SCIPheurGetName(SCIPsolGetHeur(heurdata->startcand)) : "NULL");
2052  }
2053 
2054  if( !heurdata->runalways )
2055  {
2056  /* check if enough nodes have been processed so that we want to run the heuristic again */
2057 
2058  /* compute the contingent on number of iterations that the NLP solver is allowed to use
2059  * we make it depending on the current number of processed nodes
2060  */
2061  itercontingent = (SCIP_Longint)(heurdata->iterquot * SCIPgetNNodes(scip));
2062 
2063  /* weight by previous success of heuristic */
2064  itercontingent = (SCIP_Longint)(itercontingent * 3.0 * (SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur) + 1.0));
2065  /* add the fixed offset */
2066  itercontingent += heurdata->iteroffset;
2067  /* subtract the number of iterations used so far */
2068  itercontingent -= heurdata->iterused;
2069 
2070  if( itercontingent < heurdata->itermin )
2071  {
2072  /* not enough iterations left to start NLP solver */
2073  SCIPdebugMessage("skip NLP heuristic; contingent=%"SCIP_LONGINT_FORMAT"; minimal number of iterations=%d; success ratio=%g\n",
2074  itercontingent, heurdata->itermin, (SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur) + 1.0));
2075  return SCIP_OKAY;
2076  }
2077 
2078  /* enforce user given iteration limit, if given */
2079  if( heurdata->nlpiterlimit > 0 )
2080  itercontingent = MIN(itercontingent, heurdata->nlpiterlimit);
2081  }
2082  else
2083  {
2084  itercontingent = -1;
2085  }
2086 
2087  /* check whether there is enough time left */
2088  SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) );
2089  if( !SCIPisInfinity(scip, timelimit) )
2090  {
2091  timelimit -= SCIPgetSolvingTime(scip);
2092  if( timelimit <= 0.0 )
2093  {
2094  SCIPdebugMessage("skip NLP heuristic; no time left\n");
2095  return SCIP_OKAY;
2096  }
2097  }
2098  /* enforce user given time limit, if given */
2099  if( heurdata->nlptimelimit > 0 )
2100  timelimit = MIN(heurdata->nlptimelimit, timelimit);
2101 
2102  /* so far we have not found any solution, but now we are willing to search for one */
2103  *result = SCIP_DIDNOTFIND;
2104 
2105  SCIP_CALL( SCIPapplyHeurSubNlp(scip, heur, result, heurdata->startcand, itercontingent, timelimit, heurdata->minimprove, &iterused) );
2106  heurdata->iterused += iterused;
2107 
2108  /* SCIP does not like cutoff as return, so we say didnotfind, since we did not find a solution */
2109  if( *result == SCIP_CUTOFF )
2110  *result = SCIP_DIDNOTFIND;
2111 
2112  /* forget startcand */
2113  if( heurdata->startcand != NULL )
2114  {
2115  SCIP_CALL( SCIPfreeSol(scip, &heurdata->startcand) );
2116  }
2117 
2118  /* reset timing, if it was changed temporary (at the root node) */
2119  if( heurtiming != HEUR_TIMING )
2121 
2122  return SCIP_OKAY;
2123 }
2124 
2125 
2126 /*
2127  * primal heuristic specific interface methods
2128  */
2129 
2130 /** creates the NLP local search primal heuristic and includes it in SCIP */
2132  SCIP* scip /**< SCIP data structure */
2133  )
2134 {
2135  SCIP_HEURDATA* heurdata;
2136  SCIP_HEUR* heur;
2137 
2138  /* create Nlp primal heuristic data */
2139  SCIP_CALL( SCIPallocMemory(scip, &heurdata) );
2140  BMSclearMemory(heurdata);
2141 
2142  /* include variable event handler */
2143  heurdata->eventhdlr = NULL;
2144  SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &heurdata->eventhdlr, HEUR_NAME, "propagates a global bound change to the sub-SCIP",
2145  processVarEvent, NULL) );
2146  assert(heurdata->eventhdlr != NULL);
2147 
2148  /* include primal heuristic */
2149  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
2151  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecSubNlp, heurdata) );
2152 
2153  assert(heur != NULL);
2154 
2155  /* set non-NULL pointers to callback methods */
2156  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopySubNlp) );
2157  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeSubNlp) );
2158  SCIP_CALL( SCIPsetHeurInitsol(scip, heur, heurInitsolSubNlp) );
2159  SCIP_CALL( SCIPsetHeurExitsol(scip, heur, heurExitsolSubNlp) );
2160 
2161  /* add Nlp primal heuristic parameters */
2162  SCIP_CALL( SCIPaddIntParam (scip, "heuristics/"HEUR_NAME"/nlpverblevel",
2163  "verbosity level of NLP solver",
2164  &heurdata->nlpverblevel, FALSE, 0, 0, INT_MAX, NULL, NULL) );
2165 
2166  SCIP_CALL( SCIPaddIntParam (scip, "heuristics/"HEUR_NAME"/nlpiterlimit",
2167  "iteration limit of NLP solver; 0 to use solver default",
2168  &heurdata->nlpiterlimit, FALSE, 0, 0, INT_MAX, NULL, NULL) );
2169 
2170  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/nlptimelimit",
2171  "time limit of NLP solver; 0 to use solver default",
2172  &heurdata->nlptimelimit, FALSE, 0.0, 0.0, SCIPinfinity(scip), NULL, NULL) );
2173 
2174  SCIP_CALL( SCIPaddStringParam(scip, "heuristics/"HEUR_NAME"/nlpoptfile",
2175  "name of an NLP solver specific options file",
2176  &heurdata->nlpoptfile, TRUE, "", NULL, NULL) );
2177 
2178  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/resolvetolfactor",
2179  "if SCIP does not accept a NLP feasible solution, resolve NLP with feas. tolerance reduced by this factor (set to 1.0 to turn off resolve)",
2180  &heurdata->resolvetolfactor, TRUE, 0.001, 0.0, 1.0, NULL, NULL) );
2181 
2182  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/"HEUR_NAME"/resolvefromscratch",
2183  "should the NLP resolve be started from the original starting point or the infeasible solution?",
2184  &heurdata->resolvefromscratch, TRUE, TRUE, NULL, NULL) );
2185 
2186  SCIP_CALL( SCIPaddIntParam (scip, "heuristics/"HEUR_NAME"/iteroffset",
2187  "number of iterations added to the contingent of the total number of iterations",
2188  &heurdata->iteroffset, FALSE, 500, 0, INT_MAX, NULL, NULL) );
2189 
2190  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/iterquotient",
2191  "contingent of NLP iterations in relation to the number of nodes in SCIP",
2192  &heurdata->iterquot, FALSE, 0.1, 0.0, SCIPinfinity(scip), NULL, NULL) );
2193 
2194  SCIP_CALL( SCIPaddIntParam (scip, "heuristics/"HEUR_NAME"/itermin",
2195  "contingent of NLP iterations in relation to the number of nodes in SCIP",
2196  &heurdata->itermin, FALSE, 300, 0, INT_MAX, NULL, NULL) );
2197 
2198  SCIP_CALL( SCIPaddBoolParam (scip, "heuristics/"HEUR_NAME"/runalways",
2199  "whether to run NLP heuristic always if starting point available (does not use iteroffset,iterquot,itermin)",
2200  &heurdata->runalways, FALSE, FALSE, NULL, NULL) );
2201 
2202  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/minimprove",
2203  "factor by which NLP heuristic should at least improve the incumbent",
2204  &heurdata->minimprove, TRUE, 0.01, 0.0, 1.0, NULL, NULL) );
2205 
2206  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/maxpresolverounds",
2207  "limit on number of presolve rounds in sub-SCIP (-1 for unlimited, 0 for no presolve)",
2208  &heurdata->maxpresolverounds, TRUE, -1, -1, INT_MAX, NULL, NULL) );
2209 
2210  SCIP_CALL( SCIPaddBoolParam (scip, "heuristics/"HEUR_NAME"/forbidfixings",
2211  "whether to add constraints that forbid specific fixings that turned out to be infeasible",
2212  &heurdata->forbidfixings, FALSE, TRUE, NULL, NULL) );
2213 
2214  SCIP_CALL( SCIPaddBoolParam (scip, "heuristics/"HEUR_NAME"/keepcopy",
2215  "whether to keep SCIP copy or to create new copy each time heuristic is applied",
2216  &heurdata->keepcopy, TRUE, TRUE, NULL, NULL) );
2217 
2218  return SCIP_OKAY;
2219 }
2220 
2221 /** adds all known linear constraint to the NLP, if initialized and not done already
2222  * This function is temporary and will hopefully become obsolete in the near future.
2223  */
2225  SCIP* scip, /**< original SCIP data structure */
2226  SCIP_HEUR* heur, /**< heuristic data structure */
2227  SCIP_Bool addcombconss, /**< whether to add combinatorial linear constraints, i.e., linear constraints that involve only discrete variables */
2228  SCIP_Bool addcontconss /**< whether to add continuous linear constraints, i.e., linear constraints that involve not only discrete variables */
2229  )
2230 {
2231  SCIP_HEURDATA* heurdata;
2232 
2233  assert(scip != NULL);
2234  assert(heur != NULL);
2235  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
2236 
2237  heurdata = SCIPheurGetData(heur);
2238  assert(heurdata != NULL);
2239 
2240  /* return, if nothing to do */
2241  if( (!addcombconss || heurdata->comblinearconsadded) && (!addcontconss || heurdata->contlinearconsadded) )
2242  return SCIP_OKAY;
2243 
2245  addcombconss && !heurdata->comblinearconsadded,
2246  addcontconss && !heurdata->contlinearconsadded) );
2247 
2248  heurdata->comblinearconsadded |= addcombconss;
2249  heurdata->contlinearconsadded |= addcontconss;
2250 
2251  return SCIP_OKAY;
2252 }
2253 
2254 /** updates the starting point for the NLP heuristic
2255  *
2256  * Is called by a constraint handler that handles nonlinear constraints when a check on feasibility of a solution fails.
2257  */
2259  SCIP* scip, /**< SCIP data structure */
2260  SCIP_HEUR* heur, /**< NLP heuristic */
2261  SCIP_SOL* solcand, /**< solution candidate */
2262  SCIP_Real violation /**< constraint violation of solution candidate */
2263  )
2264 {
2265  SCIP_HEURDATA* heurdata;
2266 
2267  assert(scip != NULL);
2268  assert(heur != NULL);
2269  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
2270  assert(solcand != NULL);
2271  assert(SCIPisPositive(scip, violation));
2272 
2273  /* too early or the game is over already: no more interest in starting points */
2274  if( SCIPgetStage(scip) != SCIP_STAGE_SOLVING )
2275  return SCIP_OKAY;
2276 
2277  heurdata = SCIPheurGetData(heur);
2278  assert(heurdata != NULL);
2279 
2280  /* we do not have a sub-SCIP, so we also do not need a starting point */
2281  if( heurdata->subscip == NULL )
2282  return SCIP_OKAY;
2283 
2284  /* if the solution is from our heuristic, then it is useless to use it as starting point again */
2285  if( SCIPsolGetHeur(solcand) == heur )
2286  return SCIP_OKAY;
2287 
2288  SCIPdebugMessage("consider solution candidate with violation %g and objective %g from %s\n",
2289  violation, SCIPgetSolTransObj(scip, solcand), SCIPsolGetHeur(solcand) ? SCIPheurGetName(SCIPsolGetHeur(solcand)) : "tree");
2290 
2291  /* if we have no point yet, or the new point has a lower constraint violation, or it has a better objective function value, then take the new point */
2292  if( heurdata->startcand == NULL || SCIPisGT(scip, heurdata->startcandviol, violation) ||
2293  SCIPisRelGT(scip, SCIPgetSolTransObj(scip, heurdata->startcand), SCIPgetSolTransObj(scip, solcand)) )
2294  {
2295  if( heurdata->startcand != NULL )
2296  {
2297  SCIP_CALL( SCIPfreeSol(scip, &heurdata->startcand) );
2298  }
2299  SCIP_CALL( SCIPcreateSolCopy(scip, &heurdata->startcand, solcand) );
2300  SCIP_CALL( SCIPunlinkSol(scip, heurdata->startcand) );
2301  heurdata->startcandviol = violation;
2302  }
2303 
2304  return SCIP_OKAY;
2305 }
2306 
2307 /** gets sub-SCIP used by NLP heuristic, or NULL if none */
2309  SCIP* scip, /**< original SCIP data structure */
2310  SCIP_HEUR* heur /**< heuristic data structure */
2311  )
2312 {
2313  SCIP_HEURDATA* heurdata;
2314 
2315  assert(heur != NULL);
2316  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
2317 
2318  heurdata = SCIPheurGetData(heur);
2319  assert(heurdata != NULL);
2320 
2321  return heurdata->subscip;
2322 }
2323 
2324 /** gets mapping of SCIP variables to sub-SCIP variables */
2326  SCIP* scip, /**< original SCIP data structure */
2327  SCIP_HEUR* heur /**< heuristic data structure */
2328  )
2329 {
2330  SCIP_HEURDATA* heurdata;
2331 
2332  assert(heur != NULL);
2333  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
2334 
2335  heurdata = SCIPheurGetData(heur);
2336  assert(heurdata != NULL);
2337 
2338  return heurdata->var_scip2subscip;
2339 }
2340 
2341 /** gets mapping of sub-SCIP variables to SCIP variables */
2343  SCIP* scip, /**< original SCIP data structure */
2344  SCIP_HEUR* heur /**< heuristic data structure */
2345  )
2346 {
2347  SCIP_HEURDATA* heurdata;
2348 
2349  assert(heur != NULL);
2350  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
2351 
2352  heurdata = SCIPheurGetData(heur);
2353  assert(heurdata != NULL);
2354 
2355  return heurdata->var_subscip2scip;
2356 }
2357 
2358 /** gets startpoint candidate to be used in next call to NLP heuristic, or NULL if none */
2360  SCIP* scip, /**< original SCIP data structure */
2361  SCIP_HEUR* heur /**< heuristic data structure */
2362  )
2363 {
2364  SCIP_HEURDATA* heurdata;
2365 
2366  assert(heur != NULL);
2367  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
2368 
2369  heurdata = SCIPheurGetData(heur);
2370  assert(heurdata != NULL);
2371 
2372  return heurdata->startcand;
2373 }
2374