Scippy

SCIP

Solving Constraint Integer Programs

benders.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-2019 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 scip.zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file scip/src/scip/benders.c
17  * @brief methods for Benders' decomposition
18  * @author Stephen J. Maher
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <assert.h>
24 #include <string.h>
25 
26 #include "scip/def.h"
27 #include "scip/set.h"
28 #include "scip/clock.h"
29 #include "scip/paramset.h"
30 #include "scip/lp.h"
31 #include "scip/prob.h"
32 #include "scip/pricestore.h"
33 #include "scip/scip.h"
34 #include "scip/benders.h"
35 #include "scip/pub_message.h"
36 #include "scip/pub_misc.h"
37 #include "scip/cons_linear.h"
38 
39 #include "scip/struct_benders.h"
40 #include "scip/struct_benderscut.h"
41 
42 #include "scip/benderscut.h"
43 
44 /* Defaults for parameters */
45 #define SCIP_DEFAULT_TRANSFERCUTS TRUE /** should Benders' cuts generated in LNS heuristics be transferred to the main SCIP instance? */
46 #define SCIP_DEFAULT_CUTSASCONSS TRUE /** should the transferred cuts be added as constraints? */
47 #define SCIP_DEFAULT_LNSCHECK TRUE /** should the Benders' decomposition be used in LNS heuristics */
48 #define SCIP_DEFAULT_LNSMAXDEPTH -1 /** maximum depth at which the LNS check is performed */
49 #define SCIP_DEFAULT_SUBPROBFRAC 1.0 /** fraction of subproblems that are solved in each iteration */
50 #define SCIP_DEFAULT_UPDATEAUXVARBOUND TRUE /** should the auxiliary variable lower bound be updated by solving the subproblem */
51 
52 #define BENDERS_MAXPSEUDOSOLS 5 /** the maximum number of pseudo solutions checked before suggesting
53  merge candidates */
54 
55 #define AUXILIARYVAR_NAME "##bendersauxiliaryvar" /** the name for the Benders' auxiliary variables in the master problem */
56 
57 /* event handler properties */
58 #define NODEFOCUS_EVENTHDLR_NAME "bendersnodefocus"
59 #define NODEFOCUS_EVENTHDLR_DESC "node focus event handler for Benders' decomposition"
60 
61 #define MIPNODEFOCUS_EVENTHDLR_NAME "bendersmipsolvenodefocus"
62 #define MIPNODEFOCUS_EVENTHDLR_DESC "node focus event handler for the MIP solve method for Benders' decomposition"
63 
64 #define UPPERBOUND_EVENTHDLR_NAME "bendersupperbound"
65 #define UPPERBOUND_EVENTHDLR_DESC "found solution event handler to terminate subproblem solve for a given upper bound"
66 
67 #define NODESOLVED_EVENTHDLR_NAME "bendersnodesolved"
68 #define NODESOLVED_EVENTHDLR_DESC "node solved event handler for the Benders' integer cuts"
69 
70 
71 /** event handler data */
72 struct SCIP_EventhdlrData
73 {
74  int filterpos; /**< the event filter entry */
75  int numruns; /**< the number of times that the problem has been solved */
76  SCIP_Real upperbound; /**< an upper bound for the problem */
77  SCIP_Bool solvecip; /**< is the event called from a MIP subproblem solve*/
78 };
79 
80 
81 /* ---------------- Local methods for event handlers ---------------- */
82 
83 /** initialises the members of the eventhandler data */
84 static
86  SCIP* scip, /**< the SCIP data structure */
87  SCIP_EVENTHDLRDATA* eventhdlrdata /**< the event handler data */
88  )
89 {
90  assert(scip != NULL);
91  assert(eventhdlrdata != NULL);
92 
93  eventhdlrdata->filterpos = -1;
94  eventhdlrdata->numruns = 0;
95  eventhdlrdata->upperbound = -SCIPinfinity(scip);
96  eventhdlrdata->solvecip = FALSE;
97 
98  return SCIP_OKAY;
99 }
100 
101 /** initsol method for the event handlers */
102 static
104  SCIP* scip, /**< the SCIP data structure */
105  SCIP_EVENTHDLR* eventhdlr, /**< the event handlers data structure */
106  SCIP_EVENTTYPE eventtype /**< event type mask to select events to catch */
107  )
108 {
109  SCIP_EVENTHDLRDATA* eventhdlrdata;
110 
111  assert(scip != NULL);
112  assert(eventhdlr != NULL);
113 
114  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
115 
116  SCIP_CALL(SCIPcatchEvent(scip, eventtype, eventhdlr, NULL, &eventhdlrdata->filterpos));
117 
118  return SCIP_OKAY;
119 }
120 
121 /** the exit sol method for the event handlers */
122 static
124  SCIP* scip, /**< the SCIP data structure */
125  SCIP_EVENTHDLR* eventhdlr, /**< the event handlers data structure */
126  SCIP_EVENTTYPE eventtype /**< event type mask to select events to catch */
127  )
128 {
129  SCIP_EVENTHDLRDATA* eventhdlrdata;
130 
131  assert(scip != NULL);
132  assert(eventhdlr != NULL);
133 
134  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
135 
136  if( eventhdlrdata->filterpos >= 0 )
137  {
138  SCIP_CALL(SCIPdropEvent(scip, eventtype, eventhdlr, NULL, eventhdlrdata->filterpos));
139  eventhdlrdata->filterpos = -1;
140  }
141 
142  return SCIP_OKAY;
143 }
144 
145 /** the exit method for the event handlers */
146 static
148  SCIP* scip, /**< the SCIP data structure */
149  SCIP_EVENTHDLR* eventhdlr /**< the event handlers data structure */
150  )
151 {
152  SCIP_EVENTHDLRDATA* eventhdlrdata;
153 
154  assert(scip != NULL);
155  assert(eventhdlr != NULL);
156 
157  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
158 
159  /* reinitialise the event handler data */
160  SCIP_CALL( initEventhandlerData(scip, eventhdlrdata) );
161 
162  return SCIP_OKAY;
163 }
164 
165 /** free method for the event handler */
166 static
168  SCIP* scip, /**< the SCIP data structure */
169  SCIP_EVENTHDLR* eventhdlr /**< the event handlers data structure */
170  )
171 {
172  SCIP_EVENTHDLRDATA* eventhdlrdata;
173 
174  assert(scip != NULL);
175  assert(eventhdlr != NULL);
176 
177  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
178  assert(eventhdlrdata != NULL);
179 
180  SCIPfreeBlockMemory(scip, &eventhdlrdata);
181 
182  SCIPeventhdlrSetData(eventhdlr, NULL);
183 
184  return SCIP_OKAY;
185 }
186 
187 
188 
189 /* ---------------- Callback methods of node focus event handler ---------------- */
190 
191 /** exec the event handler */
192 static
193 SCIP_DECL_EVENTEXEC(eventExecBendersNodefocus)
194 { /*lint --e{715}*/
195  SCIP_EVENTHDLRDATA* eventhdlrdata;
196 
197  assert(scip != NULL);
198  assert(eventhdlr != NULL);
199  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), NODEFOCUS_EVENTHDLR_NAME) == 0);
200 
201  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
202 
203  /* sending an interrupt solve signal to return the control back to the Benders' decomposition plugin.
204  * This will ensure the SCIP stage is SCIP_STAGE_SOLVING, allowing the use of probing mode. */
206 
207  SCIP_CALL(SCIPdropEvent(scip, SCIP_EVENTTYPE_NODEFOCUSED, eventhdlr, NULL, eventhdlrdata->filterpos));
208  eventhdlrdata->filterpos = -1;
209 
210  return SCIP_OKAY;
211 }
212 
213 /** solving process initialization method of event handler (called when branch and bound process is about to begin) */
214 static
215 SCIP_DECL_EVENTINITSOL(eventInitsolBendersNodefocus)
216 {
217  assert(scip != NULL);
218  assert(eventhdlr != NULL);
219  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), NODEFOCUS_EVENTHDLR_NAME) == 0);
220 
222 
223  return SCIP_OKAY;
224 }
225 
226 /** solving process deinitialization method of event handler (called before branch and bound process data is freed) */
227 static
228 SCIP_DECL_EVENTEXITSOL(eventExitsolBendersNodefocus)
229 {
230  assert(scip != NULL);
231  assert(eventhdlr != NULL);
232  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), NODEFOCUS_EVENTHDLR_NAME) == 0);
233 
235 
236  return SCIP_OKAY;
237 }
238 
239 /** deinitialization method of event handler (called before transformed problem is freed) */
240 static
241 SCIP_DECL_EVENTEXIT(eventExitBendersNodefocus)
242 {
243  assert(scip != NULL);
244  assert(eventhdlr != NULL);
245  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), NODEFOCUS_EVENTHDLR_NAME) == 0);
246 
247  SCIP_CALL( exitEventhandler(scip, eventhdlr) );
248 
249  return SCIP_OKAY;
250 }
251 
252 /** deinitialization method of event handler (called before transformed problem is freed) */
253 static
254 SCIP_DECL_EVENTFREE(eventFreeBendersNodefocus)
255 {
256  assert(scip != NULL);
257  assert(eventhdlr != NULL);
258  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), NODEFOCUS_EVENTHDLR_NAME) == 0);
259 
260  SCIP_CALL( freeEventhandler(scip, eventhdlr) );
261 
262  return SCIP_OKAY;
263 }
264 
265 
266 /* ---------------- Callback methods of MIP solve node focus event handler ---------------- */
267 
268 /** exec the event handler */
269 static
270 SCIP_DECL_EVENTEXEC(eventExecBendersMipnodefocus)
271 { /*lint --e{715}*/
272  SCIP_EVENTHDLRDATA* eventhdlrdata;
273 
274  assert(scip != NULL);
275  assert(eventhdlr != NULL);
276  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), MIPNODEFOCUS_EVENTHDLR_NAME) == 0);
277 
278  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
279 
280  /* interrupting the solve so that the control is returned back to the Benders' core. */
281  if( eventhdlrdata->numruns == 0 && !eventhdlrdata->solvecip )
282  {
284  }
285 
286  SCIP_CALL(SCIPdropEvent(scip, SCIP_EVENTTYPE_NODEFOCUSED, eventhdlr, NULL, eventhdlrdata->filterpos));
287  eventhdlrdata->filterpos = -1;
288 
289  eventhdlrdata->numruns++;
290 
291  return SCIP_OKAY;
292 }
293 
294 /** solving process initialization method of event handler (called when branch and bound process is about to begin) */
295 static
296 SCIP_DECL_EVENTINITSOL(eventInitsolBendersMipnodefocus)
297 {
298  assert(scip != NULL);
299  assert(eventhdlr != NULL);
300  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), MIPNODEFOCUS_EVENTHDLR_NAME) == 0);
301 
303 
304  return SCIP_OKAY;
305 }
306 
307 /** solving process deinitialization method of event handler (called before branch and bound process data is freed) */
308 static
309 SCIP_DECL_EVENTEXITSOL(eventExitsolBendersMipnodefocus)
310 {
311  assert(scip != NULL);
312  assert(eventhdlr != NULL);
313  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), MIPNODEFOCUS_EVENTHDLR_NAME) == 0);
314 
316 
317  return SCIP_OKAY;
318 }
319 
320 /** deinitialization method of event handler (called before transformed problem is freed) */
321 static
322 SCIP_DECL_EVENTEXIT(eventExitBendersMipnodefocus)
323 {
324  assert(scip != NULL);
325  assert(eventhdlr != NULL);
326  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), MIPNODEFOCUS_EVENTHDLR_NAME) == 0);
327 
328  SCIP_CALL( exitEventhandler(scip, eventhdlr) );
329 
330  return SCIP_OKAY;
331 }
332 
333 /** deinitialization method of event handler (called before transformed problem is freed) */
334 static
335 SCIP_DECL_EVENTFREE(eventFreeBendersMipnodefocus)
336 {
337  assert(scip != NULL);
338  assert(eventhdlr != NULL);
339  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), MIPNODEFOCUS_EVENTHDLR_NAME) == 0);
340 
341  SCIP_CALL( freeEventhandler(scip, eventhdlr) );
342 
343  return SCIP_OKAY;
344 }
345 
346 /* ---------------- Callback methods of solution found event handler ---------------- */
347 
348 /** exec the event handler */
349 static
350 SCIP_DECL_EVENTEXEC(eventExecBendersUpperbound)
351 { /*lint --e{715}*/
352  SCIP_EVENTHDLRDATA* eventhdlrdata;
353  SCIP_SOL* bestsol;
354 
355  assert(scip != NULL);
356  assert(eventhdlr != NULL);
357  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), UPPERBOUND_EVENTHDLR_NAME) == 0);
358 
359  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
360  assert(eventhdlrdata != NULL);
361 
362  bestsol = SCIPgetBestSol(scip);
363 
364  if( SCIPisLT(scip, SCIPgetSolOrigObj(scip, bestsol)*(int)SCIPgetObjsense(scip), eventhdlrdata->upperbound) )
365  {
367  }
368 
369  return SCIP_OKAY;
370 }
371 
372 /** solving process initialization method of event handler (called when branch and bound process is about to begin) */
373 static
374 SCIP_DECL_EVENTINITSOL(eventInitsolBendersUpperbound)
375 {
376  assert(scip != NULL);
377  assert(eventhdlr != NULL);
378  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), UPPERBOUND_EVENTHDLR_NAME) == 0);
379 
381 
382  return SCIP_OKAY;
383 }
384 
385 /** solving process deinitialization method of event handler (called before branch and bound process data is freed) */
386 static
387 SCIP_DECL_EVENTEXITSOL(eventExitsolBendersUpperbound)
388 {
389  assert(scip != NULL);
390  assert(eventhdlr != NULL);
391  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), UPPERBOUND_EVENTHDLR_NAME) == 0);
392 
394 
395  return SCIP_OKAY;
396 }
397 
398 /** deinitialization method of event handler (called before transformed problem is freed) */
399 static
400 SCIP_DECL_EVENTEXIT(eventExitBendersUpperbound)
401 {
402  assert(scip != NULL);
403  assert(eventhdlr != NULL);
404  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), UPPERBOUND_EVENTHDLR_NAME) == 0);
405 
406  SCIP_CALL( exitEventhandler(scip, eventhdlr) );
407 
408  return SCIP_OKAY;
409 }
410 
411 /** deinitialization method of event handler (called before transformed problem is freed) */
412 static
413 SCIP_DECL_EVENTFREE(eventFreeBendersUpperbound)
414 {
415  assert(scip != NULL);
416  assert(eventhdlr != NULL);
417  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), UPPERBOUND_EVENTHDLR_NAME) == 0);
418 
419  SCIP_CALL( freeEventhandler(scip, eventhdlr) );
420 
421  return SCIP_OKAY;
422 }
423 
424 /** updates the upper bound in the event handler data */
425 static
427  SCIP_BENDERS* benders, /**< Benders' decomposition */
428  int probnumber, /**< the subproblem number */
429  SCIP_Real upperbound /**< the upper bound value */
430  )
431 {
432  SCIP_EVENTHDLR* eventhdlr;
433  SCIP_EVENTHDLRDATA* eventhdlrdata;
434 
435  assert(benders != NULL);
436  assert(probnumber >= 0 && probnumber < benders->nsubproblems);
437 
438  eventhdlr = SCIPfindEventhdlr(SCIPbendersSubproblem(benders, probnumber), UPPERBOUND_EVENTHDLR_NAME);
439  assert(eventhdlr != NULL);
440 
441  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
442  assert(eventhdlrdata != NULL);
443 
444  eventhdlrdata->upperbound = upperbound;
445 
446  return SCIP_OKAY;
447 }
448 
449 /* ---------------- Callback methods of the node solved event handler ---------------- */
450 
451 /** Updates the cut constant of the Benders' cuts data.
452  * This function solves the master problem with only the auxiliary variables in the objective function.
453  */
454 static
456  SCIP* masterprob, /**< the SCIP instance of the master problem */
457  SCIP_BENDERS* benders /**< Benders' decomposition */
458  )
459 {
460  SCIP_VAR** vars;
461  int nvars;
462  int nsubproblems;
463  int i;
464  SCIP_Bool lperror;
465  SCIP_Bool cutoff;
466 
467  assert(masterprob != NULL);
468  assert(benders != NULL);
469 
470  /* don't run in probing or in repropagation */
471  if( SCIPinProbing(masterprob) || SCIPinRepropagation(masterprob) || SCIPinDive(masterprob) )
472  return SCIP_OKAY;
473 
474  nsubproblems = SCIPbendersGetNSubproblems(benders);
475 
476  SCIP_CALL( SCIPstartProbing(masterprob) );
477 
478  /* change the master problem variables to 0 */
479  nvars = SCIPgetNVars(masterprob);
480  vars = SCIPgetVars(masterprob);
481 
482  /* setting the objective function coefficient to 0 for all variables */
483  for( i = 0; i < nvars; i++ )
484  {
485  if( SCIPvarGetStatus(vars[i]) == SCIP_VARSTATUS_COLUMN )
486  {
487  SCIP_CALL( SCIPchgVarObjProbing(masterprob, vars[i], 0.0) );
488  }
489  }
490 
491  /* solving an LP for all subproblems to find the lower bound */
492  for( i = 0; i < nsubproblems; i++)
493  {
494  SCIP_VAR* auxiliaryvar;
495 
496  auxiliaryvar = SCIPbendersGetAuxiliaryVar(benders, i);
497 
498  if( SCIPvarGetStatus(auxiliaryvar) != SCIP_VARSTATUS_COLUMN )
499  continue;
500 
501  SCIP_CALL( SCIPchgVarObjProbing(masterprob, auxiliaryvar, 1.0) );
502 
503  /* solving the probing LP to get a lower bound on the auxiliary variables */
504  SCIP_CALL( SCIPsolveProbingLP(masterprob, -1, &lperror, &cutoff) );
505 
506  if( !SCIPisInfinity(masterprob, -SCIPgetSolTransObj(masterprob, NULL)) )
508 
509  SCIPdebugMsg(masterprob, "Cut constant for subproblem %d: %g\n", i,
511 
512  SCIP_CALL( SCIPchgVarObjProbing(masterprob, auxiliaryvar, 0.0) );
513  }
514 
515  SCIP_CALL( SCIPendProbing(masterprob) );
516 
517  return SCIP_OKAY;
518 }
519 
520 /** exec the event handler */
521 static
522 SCIP_DECL_EVENTEXEC(eventExecBendersNodesolved)
523 { /*lint --e{715}*/
524  SCIP_BENDERS* benders;
525 
526  assert(scip != NULL);
527  assert(eventhdlr != NULL);
528  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), NODESOLVED_EVENTHDLR_NAME) == 0);
529 
530  benders = (SCIP_BENDERS*)SCIPeventhdlrGetData(eventhdlr); /*lint !e826*/
531 
532  if( SCIPbendersGetNSubproblems(benders) > 0
534  {
536  }
537 
539 
540  return SCIP_OKAY;
541 }
542 
543 /** solving process initialization method of event handler (called when branch and bound process is about to begin) */
544 static
545 SCIP_DECL_EVENTINITSOL(eventInitsolBendersNodesolved)
546 {
547  SCIP_BENDERS* benders;
548 
549  assert(scip != NULL);
550  assert(eventhdlr != NULL);
551  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), NODESOLVED_EVENTHDLR_NAME) == 0);
552 
553  /* getting the Benders' decomposition data structure */
554  benders = (SCIP_BENDERS*)SCIPeventhdlrGetData(eventhdlr); /*lint !e826*/
555 
556  /* The event is only caught if there is an active Benders' decomposition */
557  if( SCIPbendersIsActive(benders) && !SCIPbendersOnlyCheckConvexRelax(benders) )
558  {
560  }
561 
562  return SCIP_OKAY;
563 }
564 
565 
566 
567 /* Local methods */
568 
569 /** A workaround for GCG. This is a temp vardata that is set for the auxiliary variables */
570 struct SCIP_VarData
571 {
572  int vartype; /**< the variable type. In GCG this indicates whether the variable is a
573  * master problem or subproblem variable. */
574 };
575 
576 
577 /** adds the auxiliary variables to the Benders' decomposition master problem */
578 static
580  SCIP* scip, /**< SCIP data structure */
581  SCIP_BENDERS* benders /**< Benders' decomposition structure */
582  )
583 {
584  SCIP_BENDERS* topbenders; /* the highest priority Benders' decomposition */
585  SCIP_VAR* auxiliaryvar;
586  SCIP_VARDATA* vardata;
587  char varname[SCIP_MAXSTRLEN]; /* the name of the auxiliary variable */
588  SCIP_Bool shareauxvars;
589  int i;
590 
591  /* this is a workaround for GCG. GCG expects that the variable has vardata when added. So a dummy vardata is created */
592  SCIP_CALL( SCIPallocBlockMemory(scip, &vardata) );
593  vardata->vartype = -1;
594 
595  /* getting the highest priority Benders' decomposition */
596  topbenders = SCIPgetBenders(scip)[0];
597 
598  /* if the current Benders is the highest priority Benders, then we need to create the auxiliary variables.
599  * Otherwise, if the shareauxvars flag is set, then the auxiliary variables from the highest priority Benders' are
600  * stored with this Benders. */
601  shareauxvars = FALSE;
602  if( topbenders != benders && SCIPbendersShareAuxVars(benders) )
603  shareauxvars = TRUE;
604 
605  for( i = 0; i < SCIPbendersGetNSubproblems(benders); i++ )
606  {
607  /* if the auxiliary variables are shared, then a pointer to the variable is retrieved from topbenders,
608  * otherwise the auxiliaryvariable is created. */
609  if( shareauxvars )
610  {
611  auxiliaryvar = SCIPbendersGetAuxiliaryVar(topbenders, i);
612 
613  SCIP_CALL( SCIPcaptureVar(scip, auxiliaryvar) );
614  }
615  else
616  {
617  (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "%s_%d_%s", AUXILIARYVAR_NAME, i, SCIPbendersGetName(benders) );
618  SCIP_CALL( SCIPcreateVarBasic(scip, &auxiliaryvar, varname, -SCIPinfinity(scip), SCIPinfinity(scip), 1.0,
620 
621  SCIPvarSetData(auxiliaryvar, vardata);
622 
623  SCIP_CALL( SCIPaddVar(scip, auxiliaryvar) );
624  }
625 
626  benders->auxiliaryvars[i] = auxiliaryvar;
627  }
628 
629  SCIPfreeBlockMemory(scip, &vardata);
630 
631  return SCIP_OKAY;
632 }
633 
634 /** assigns the copied auxiliary variables in the target SCIP to the target Benders' decomposition data */
635 static
637  SCIP* scip, /**< SCIP data structure, the target scip */
638  SCIP_BENDERS* benders /**< Benders' decomposition */
639  )
640 {
641  SCIP_BENDERS* topbenders; /* the highest priority Benders' decomposition */
642  SCIP_VAR* targetvar;
643  SCIP_VARDATA* vardata;
644  char varname[SCIP_MAXSTRLEN]; /* the name of the auxiliary variable */
645  SCIP_Bool shareauxvars;
646  int i;
647 
648  assert(scip != NULL);
649  assert(benders != NULL);
650 
651  /* this is a workaround for GCG. GCG expects that the variable has vardata when added. So a dummy vardata is created */
652  SCIP_CALL( SCIPallocBlockMemory(scip, &vardata) );
653  vardata->vartype = -1;
654 
655  /* getting the highest priority Benders' decomposition */
656  topbenders = SCIPgetBenders(scip)[0];
657 
658  /* if the auxiliary variable are shared, then the variable name will have a suffix of the highest priority Benders'
659  * name. So the shareauxvars flag indicates how to search for the auxiliary variables */
660  shareauxvars = FALSE;
661  if( topbenders != benders && SCIPbendersShareAuxVars(benders) )
662  shareauxvars = TRUE;
663 
664  for( i = 0; i < SCIPbendersGetNSubproblems(benders); i++ )
665  {
666  if( shareauxvars )
667  (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "%s_%d_%s", AUXILIARYVAR_NAME, i, SCIPbendersGetName(topbenders));
668  else
669  (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "%s_%d_%s", AUXILIARYVAR_NAME, i, SCIPbendersGetName(benders));
670 
671  /* finding the variable in the copied problem that has the same name as the auxiliary variable */
672  targetvar = SCIPfindVar(scip, varname);
673  assert(targetvar != NULL);
674 
675  SCIPvarSetData(targetvar, vardata);
676 
677  benders->auxiliaryvars[i] = SCIPvarGetTransVar(targetvar);
678 
679  SCIP_CALL( SCIPcaptureVar(scip, benders->auxiliaryvars[i]) );
680  }
681 
682  SCIPfreeBlockMemory(scip, &vardata);
683 
684  return SCIP_OKAY;
685 }
686 
687 /** sets the subproblem objective value array to -infinity */
688 static
690  SCIP_BENDERS* benders /**< the Benders' decomposition structure */
691  )
692 {
693  SCIP* subproblem;
694  int nsubproblems;
695  int i;
696 
697  assert(benders != NULL);
698 
699  nsubproblems = SCIPbendersGetNSubproblems(benders);
700 
701  for( i = 0; i < nsubproblems; i++ )
702  {
703  subproblem = SCIPbendersSubproblem(benders, i);
704  SCIPbendersSetSubproblemObjval(benders, i, SCIPinfinity(subproblem));
705  }
706 }
707 
708 /** compares two Benders' decompositions w. r. to their priority */
709 SCIP_DECL_SORTPTRCOMP(SCIPbendersComp)
710 { /*lint --e{715}*/
711  return ((SCIP_BENDERS*)elem2)->priority - ((SCIP_BENDERS*)elem1)->priority;
712 }
713 
714 /** comparison method for sorting Benders' decompositions w.r.t. to their name */
715 SCIP_DECL_SORTPTRCOMP(SCIPbendersCompName)
716 {
717  return strcmp(SCIPbendersGetName((SCIP_BENDERS*)elem1), SCIPbendersGetName((SCIP_BENDERS*)elem2));
718 }
719 
720 /** method to call, when the priority of a Benders' decomposition was changed */
721 static
722 SCIP_DECL_PARAMCHGD(paramChgdBendersPriority)
723 { /*lint --e{715}*/
724  SCIP_PARAMDATA* paramdata;
725 
726  paramdata = SCIPparamGetData(param);
727  assert(paramdata != NULL);
728 
729  /* use SCIPsetBendersPriority() to mark the Benders' decompositions as unsorted */
730  SCIPsetBendersPriority(scip, (SCIP_BENDERS*)paramdata, SCIPparamGetInt(param)); /*lint !e740*/
731 
732  return SCIP_OKAY;
733 }
734 
735 /** creates a variable mapping between the master problem variables of the source scip and the sub scip */
736 static
738  SCIP_BENDERS* benders, /**< Benders' decomposition of the target SCIP instance */
739  SCIP_SET* sourceset, /**< global SCIP settings from the source SCIP */
740  SCIP_HASHMAP* varmap /**< a hashmap to store the mapping of source variables corresponding
741  * target variables; must not be NULL */
742  )
743 {
744  SCIP_VAR** vars;
745  SCIP_VAR* targetvar;
746  int nvars;
747  int i;
748 
749  assert(benders != NULL);
750  assert(sourceset != NULL);
751  assert(benders->iscopy);
752  assert(benders->mastervarsmap == NULL);
753 
754  /* getting the master problem variable data */
755  vars = SCIPgetVars(sourceset->scip);
756  nvars = SCIPgetNVars(sourceset->scip);
757 
758  /* creating the hashmap for the mapping between the master variable of the target and source scip */
759  SCIP_CALL( SCIPhashmapCreate(&benders->mastervarsmap, SCIPblkmem(sourceset->scip), nvars) );
760 
761  for( i = 0; i < nvars; i++ )
762  {
763  /* getting the variable pointer for the target SCIP variables. The variable mapping returns the target SCIP
764  * varibale for a given source SCIP variable. */
765  targetvar = (SCIP_VAR*) SCIPhashmapGetImage(varmap, vars[i]);
766  if( targetvar != NULL )
767  {
768  SCIP_CALL( SCIPhashmapInsert(benders->mastervarsmap, targetvar, vars[i]) );
769  SCIP_CALL( SCIPcaptureVar(sourceset->scip, vars[i]) );
770  }
771  }
772 
773  return SCIP_OKAY;
774 }
775 
776 /** copies the given Benders' decomposition to a new SCIP */
778  SCIP_BENDERS* benders, /**< Benders' decomposition */
779  SCIP_SET* sourceset, /**< SCIP_SET of SCIP to copy from */
780  SCIP_SET* targetset, /**< SCIP_SET of SCIP to copy to */
781  SCIP_HASHMAP* varmap, /**< a hashmap to store the mapping of source variables corresponding
782  * target variables; must not be NULL */
783  SCIP_Bool* valid /**< was the copying process valid? */
784  )
785 {
786  SCIP_BENDERS* targetbenders; /* the copy of the Benders' decomposition struct in the target set */
787  int i;
788 
789  assert(benders != NULL);
790  assert(targetset != NULL);
791  assert(varmap != NULL);
792  assert(valid != NULL);
793  assert(targetset->scip != NULL);
794 
795  (*valid) = FALSE;
796 
797  if( benders->benderscopy != NULL && targetset->benders_copybenders && SCIPbendersIsActive(benders) )
798  {
799  SCIPsetDebugMsg(targetset, "including Benders' decomposition %s in subscip %p\n", SCIPbendersGetName(benders), (void*)targetset->scip);
800  SCIP_CALL( benders->benderscopy(targetset->scip, benders) );
801 
802  /* copying the Benders' cuts */
803  targetbenders = SCIPsetFindBenders(targetset, SCIPbendersGetName(benders));
804 
805  /* storing the pointer to the source scip instance */
806  targetbenders->sourcescip = sourceset->scip;
807 
808  /* the flag is set to indicate that the Benders' decomposition is a copy */
809  targetbenders->iscopy = TRUE;
810 
811  /* storing whether the lnscheck should be performed */
812  targetbenders->lnscheck = benders->lnscheck;
813 
814  /* calling the copy method for the Benders' cuts */
816  for( i = 0; i < benders->nbenderscuts; i++ )
817  {
818  SCIP_CALL( SCIPbenderscutCopyInclude(targetbenders, benders->benderscuts[i], targetset) );
819  }
820 
821  /* When the Benders' decomposition is copied then a variable mapping between the master problem variables is
822  * required. This variable mapping is used to transfer the cuts generated in the target SCIP to the source SCIP.
823  * The variable map is stored in the target Benders' decomposition. This will be freed when the sub-SCIP is freed.
824  */
825  SCIP_CALL( createMasterVarMapping(targetbenders, sourceset, varmap) );
826  }
827 
828  /* if the Benders' decomposition is active, then copy is not valid. */
829  (*valid) = !SCIPbendersIsActive(benders);
830 
831  return SCIP_OKAY;
832 }
833 
834 /** internal method for creating a Benders' decomposition structure */
835 static
837  SCIP_BENDERS** benders, /**< pointer to Benders' decomposition data structure */
838  SCIP_SET* set, /**< global SCIP settings */
839  SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */
840  BMS_BLKMEM* blkmem, /**< block memory for parameter settings */
841  const char* name, /**< name of Benders' decomposition */
842  const char* desc, /**< description of Benders' decomposition */
843  int priority, /**< priority of the Benders' decomposition */
844  SCIP_Bool cutlp, /**< should Benders' cuts be generated for LP solutions */
845  SCIP_Bool cutpseudo, /**< should Benders' cuts be generated for pseudo solutions */
846  SCIP_Bool cutrelax, /**< should Benders' cuts be generated for relaxation solutions */
847  SCIP_Bool shareauxvars, /**< should this Benders' use the highest priority Benders aux vars */
848  SCIP_DECL_BENDERSCOPY ((*benderscopy)), /**< copy method of Benders' decomposition or NULL if you don't want to copy your plugin into sub-SCIPs */
849  SCIP_DECL_BENDERSFREE ((*bendersfree)), /**< destructor of Benders' decomposition */
850  SCIP_DECL_BENDERSINIT ((*bendersinit)), /**< initialize Benders' decomposition */
851  SCIP_DECL_BENDERSEXIT ((*bendersexit)), /**< deinitialize Benders' decomposition */
852  SCIP_DECL_BENDERSINITPRE((*bendersinitpre)),/**< presolving initialization method for Benders' decomposition */
853  SCIP_DECL_BENDERSEXITPRE((*bendersexitpre)),/**< presolving deinitialization method for Benders' decomposition */
854  SCIP_DECL_BENDERSINITSOL((*bendersinitsol)),/**< solving process initialization method of Benders' decomposition */
855  SCIP_DECL_BENDERSEXITSOL((*bendersexitsol)),/**< solving process deinitialization method of Benders' decomposition */
856  SCIP_DECL_BENDERSGETVAR((*bendersgetvar)),/**< returns the master variable for a given subproblem variable */
857  SCIP_DECL_BENDERSCREATESUB((*benderscreatesub)),/**< creates a Benders' decomposition subproblem */
858  SCIP_DECL_BENDERSPRESUBSOLVE((*benderspresubsolve)),/**< called prior to the subproblem solving loop */
859  SCIP_DECL_BENDERSSOLVESUBCONVEX((*benderssolvesubconvex)),/**< the solving method for convex Benders' decomposition subproblems */
860  SCIP_DECL_BENDERSSOLVESUB((*benderssolvesub)),/**< the solving method for the Benders' decomposition subproblems */
861  SCIP_DECL_BENDERSPOSTSOLVE((*benderspostsolve)),/**< called after the subproblems are solved. */
862  SCIP_DECL_BENDERSFREESUB((*bendersfreesub)),/**< the freeing method for the Benders' decomposition subproblems */
863  SCIP_BENDERSDATA* bendersdata /**< Benders' decomposition data */
864  )
865 {
866  char paramname[SCIP_MAXSTRLEN];
867  char paramdesc[SCIP_MAXSTRLEN];
868 
869  assert(benders != NULL);
870  assert(name != NULL);
871  assert(desc != NULL);
872 
873  /* Checking whether the benderssolvesub and the bendersfreesub are both implemented or both are not implemented */
874  if( (benderssolvesubconvex == NULL && benderssolvesub == NULL && bendersfreesub != NULL)
875  || ((benderssolvesubconvex != NULL || benderssolvesub != NULL) && bendersfreesub == NULL) )
876  {
877  SCIPerrorMessage("Benders' decomposition <%s> requires that if bendersFreesub%s is implemented, then at least "
878  "one of bendersSolvesubconvex%s or bendersSolvesub%s are implemented.\n", name, name, name, name);
879  return SCIP_INVALIDCALL;
880  }
881 
882  SCIP_ALLOC( BMSallocMemory(benders) );
883  BMSclearMemory(*benders);
884  SCIP_ALLOC( BMSduplicateMemoryArray(&(*benders)->name, name, strlen(name)+1) );
885  SCIP_ALLOC( BMSduplicateMemoryArray(&(*benders)->desc, desc, strlen(desc)+1) );
886  (*benders)->priority = priority;
887  (*benders)->cutlp = cutlp;
888  (*benders)->cutpseudo = cutpseudo;
889  (*benders)->cutrelax = cutrelax;
890  (*benders)->shareauxvars = shareauxvars;
891  (*benders)->benderscopy = benderscopy;
892  (*benders)->bendersfree = bendersfree;
893  (*benders)->bendersinit = bendersinit;
894  (*benders)->bendersexit = bendersexit;
895  (*benders)->bendersinitpre = bendersinitpre;
896  (*benders)->bendersexitpre = bendersexitpre;
897  (*benders)->bendersinitsol = bendersinitsol;
898  (*benders)->bendersexitsol = bendersexitsol;
899  (*benders)->bendersgetvar = bendersgetvar;
900  (*benders)->benderscreatesub = benderscreatesub;
901  (*benders)->benderspresubsolve = benderspresubsolve;
902  (*benders)->benderssolvesubconvex = benderssolvesubconvex;
903  (*benders)->benderssolvesub = benderssolvesub;
904  (*benders)->benderspostsolve = benderspostsolve;
905  (*benders)->bendersfreesub = bendersfreesub;
906  (*benders)->bendersdata = bendersdata;
907  SCIP_CALL( SCIPclockCreate(&(*benders)->setuptime, SCIP_CLOCKTYPE_DEFAULT) );
908  SCIP_CALL( SCIPclockCreate(&(*benders)->bendersclock, SCIP_CLOCKTYPE_DEFAULT) );
909 
910  /* add parameters */
911  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "benders/%s/priority", name);
912  (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "priority of Benders' decomposition <%s>", name);
913  SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc,
914  &(*benders)->priority, FALSE, priority, INT_MIN/4, INT_MAX/4,
915  paramChgdBendersPriority, (SCIP_PARAMDATA*)(*benders)) ); /*lint !e740*/
916 
917  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "benders/%s/cutlp", name);
918  SCIP_CALL( SCIPsetAddBoolParam(set, messagehdlr, blkmem, paramname,
919  "should Benders' cuts be generated for LP solutions?", &(*benders)->cutlp, FALSE, cutlp, NULL, NULL) ); /*lint !e740*/
920 
921  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "benders/%s/cutpseudo", name);
922  SCIP_CALL( SCIPsetAddBoolParam(set, messagehdlr, blkmem, paramname,
923  "should Benders' cuts be generated for pseudo solutions?", &(*benders)->cutpseudo, FALSE, cutpseudo, NULL, NULL) ); /*lint !e740*/
924 
925  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "benders/%s/cutrelax", name);
926  SCIP_CALL( SCIPsetAddBoolParam(set, messagehdlr, blkmem, paramname,
927  "should Benders' cuts be generated for relaxation solutions?", &(*benders)->cutrelax, FALSE, cutrelax, NULL, NULL) ); /*lint !e740*/
928 
929  /* These parameters are left for the user to decide in a settings file. This departs from the usual SCIP convention
930  * where the settings available at the creation of the plugin can be set in the function call.
931  */
932  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "benders/%s/transfercuts", name);
933  SCIP_CALL( SCIPsetAddBoolParam(set, messagehdlr, blkmem, paramname,
934  "should Benders' cuts from LNS heuristics be transferred to the main SCIP instance?", &(*benders)->transfercuts,
935  FALSE, SCIP_DEFAULT_TRANSFERCUTS, NULL, NULL) ); /*lint !e740*/
936 
937  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "benders/%s/lnscheck", name);
938  SCIP_CALL( SCIPsetAddBoolParam(set, messagehdlr, blkmem, paramname,
939  "should Benders' decomposition be used in LNS heurisics?", &(*benders)->lnscheck, FALSE, SCIP_DEFAULT_LNSCHECK,
940  NULL, NULL) ); /*lint !e740*/
941 
942  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "benders/%s/lnsmaxdepth", name);
943  SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname,
944  "maximum depth at which the LNS check is performed (-1: no limit)", &(*benders)->lnsmaxdepth, TRUE,
946 
947  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "benders/%s/cutsasconss", name);
948  SCIP_CALL( SCIPsetAddBoolParam(set, messagehdlr, blkmem, paramname,
949  "should the transferred cuts be added as constraints?", &(*benders)->cutsasconss, FALSE,
950  SCIP_DEFAULT_CUTSASCONSS, NULL, NULL) ); /*lint !e740*/
951 
952  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "benders/%s/subprobfrac", name);
953  SCIP_CALL( SCIPsetAddRealParam(set, messagehdlr, blkmem, paramname,
954  "fraction of subproblems that are solved in each iteration", &(*benders)->subprobfrac, FALSE,
955  SCIP_DEFAULT_SUBPROBFRAC, 0.0, 1.0, NULL, NULL) ); /*lint !e740*/
956 
957  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "benders/%s/updateauxvarbound", name);
958  SCIP_CALL( SCIPsetAddBoolParam(set, messagehdlr, blkmem, paramname,
959  "should the auxiliary variable bound be updated by solving the subproblem?", &(*benders)->updateauxvarbound,
960  FALSE, SCIP_DEFAULT_UPDATEAUXVARBOUND, NULL, NULL) ); /*lint !e740*/
961 
962  return SCIP_OKAY;
963 }
964 
965 /** creates a Benders' decomposition structure
966  *
967  * To use the Benders' decomposition for solving a problem, it first has to be activated with a call to SCIPactivateBenders().
968  */
970  SCIP_BENDERS** benders, /**< pointer to Benders' decomposition data structure */
971  SCIP_SET* set, /**< global SCIP settings */
972  SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */
973  BMS_BLKMEM* blkmem, /**< block memory for parameter settings */
974  const char* name, /**< name of Benders' decomposition */
975  const char* desc, /**< description of Benders' decomposition */
976  int priority, /**< priority of the Benders' decomposition */
977  SCIP_Bool cutlp, /**< should Benders' cuts be generated for LP solutions */
978  SCIP_Bool cutpseudo, /**< should Benders' cuts be generated for pseudo solutions */
979  SCIP_Bool cutrelax, /**< should Benders' cuts be generated for relaxation solutions */
980  SCIP_Bool shareauxvars, /**< should this Benders' use the highest priority Benders aux vars */
981  SCIP_DECL_BENDERSCOPY ((*benderscopy)), /**< copy method of Benders' decomposition or NULL if you don't want to copy your plugin into sub-SCIPs */
982  SCIP_DECL_BENDERSFREE ((*bendersfree)), /**< destructor of Benders' decomposition */
983  SCIP_DECL_BENDERSINIT ((*bendersinit)), /**< initialize Benders' decomposition */
984  SCIP_DECL_BENDERSEXIT ((*bendersexit)), /**< deinitialize Benders' decomposition */
985  SCIP_DECL_BENDERSINITPRE((*bendersinitpre)),/**< presolving initialization method for Benders' decomposition */
986  SCIP_DECL_BENDERSEXITPRE((*bendersexitpre)),/**< presolving deinitialization method for Benders' decomposition */
987  SCIP_DECL_BENDERSINITSOL((*bendersinitsol)),/**< solving process initialization method of Benders' decomposition */
988  SCIP_DECL_BENDERSEXITSOL((*bendersexitsol)),/**< solving process deinitialization method of Benders' decomposition */
989  SCIP_DECL_BENDERSGETVAR((*bendersgetvar)),/**< returns the master variable for a given subproblem variable */
990  SCIP_DECL_BENDERSCREATESUB((*benderscreatesub)),/**< creates a Benders' decomposition subproblem */
991  SCIP_DECL_BENDERSPRESUBSOLVE((*benderspresubsolve)),/**< called prior to the subproblem solving loop */
992  SCIP_DECL_BENDERSSOLVESUBCONVEX((*benderssolvesubconvex)),/**< the solving method for convex Benders' decomposition subproblems */
993  SCIP_DECL_BENDERSSOLVESUB((*benderssolvesub)),/**< the solving method for the Benders' decomposition subproblems */
994  SCIP_DECL_BENDERSPOSTSOLVE((*benderspostsolve)),/**< called after the subproblems are solved. */
995  SCIP_DECL_BENDERSFREESUB((*bendersfreesub)),/**< the freeing method for the Benders' decomposition subproblems */
996  SCIP_BENDERSDATA* bendersdata /**< Benders' decomposition data */
997  )
998 {
999  assert(benders != NULL);
1000  assert(name != NULL);
1001  assert(desc != NULL);
1002 
1003  SCIP_CALL_FINALLY( doBendersCreate(benders, set, messagehdlr, blkmem, name, desc, priority, cutlp, cutpseudo,
1004  cutrelax, shareauxvars, benderscopy, bendersfree, bendersinit, bendersexit, bendersinitpre, bendersexitpre,
1005  bendersinitsol, bendersexitsol, bendersgetvar, benderscreatesub, benderspresubsolve, benderssolvesubconvex,
1006  benderssolvesub, benderspostsolve, bendersfreesub, bendersdata), (void) SCIPbendersFree(benders, set) );
1007 
1008  return SCIP_OKAY;
1009 }
1010 
1011 
1012 /** releases the variables that have been captured in the hashmap */
1013 static
1015  SCIP* scip, /**< the SCIP data structure */
1016  SCIP_BENDERS* benders /**< Benders' decomposition */
1017  )
1018 {
1019  int nentries;
1020  int i;
1021 
1022  assert(scip != NULL);
1023  assert(benders != NULL);
1024 
1025  assert(benders->mastervarsmap != NULL);
1026 
1027  nentries = SCIPhashmapGetNEntries(benders->mastervarsmap);
1028 
1029  for( i = 0; i < nentries; ++i )
1030  {
1031  SCIP_HASHMAPENTRY* entry;
1032  entry = SCIPhashmapGetEntry(benders->mastervarsmap, i);
1033 
1034  if( entry != NULL )
1035  {
1036  SCIP_VAR* var;
1037  var = (SCIP_VAR*) SCIPhashmapEntryGetImage(entry);
1038 
1039  SCIP_CALL( SCIPreleaseVar(scip, &var) );
1040  }
1041  }
1042 
1043  return SCIP_OKAY;
1044 }
1045 
1046 
1047 /** calls destructor and frees memory of Benders' decomposition */
1049  SCIP_BENDERS** benders, /**< pointer to Benders' decomposition data structure */
1050  SCIP_SET* set /**< global SCIP settings */
1051  )
1052 {
1053  int i;
1054 
1055  assert(benders != NULL);
1056  assert(*benders != NULL);
1057  assert(!(*benders)->initialized);
1058  assert(set != NULL);
1059 
1060  /* call destructor of Benders' decomposition */
1061  if( (*benders)->bendersfree != NULL )
1062  {
1063  SCIP_CALL( (*benders)->bendersfree(set->scip, *benders) );
1064  }
1065 
1066  /* if the Benders' decomposition is a copy, then the variable map between the source and the target SCIP needs to be
1067  * freed.
1068  */
1069  if( (*benders)->iscopy )
1070  {
1071  SCIP_CALL( releaseVarMappingHashmapVars((*benders)->sourcescip, (*benders)) );
1072  SCIPhashmapFree(&(*benders)->mastervarsmap);
1073  }
1074 
1075  /* freeing the Benders' cuts */
1076  for( i = 0; i < (*benders)->nbenderscuts; i++ )
1077  {
1078  SCIP_CALL( SCIPbenderscutFree(&((*benders)->benderscuts[i]), set) );
1079  }
1080  BMSfreeMemoryArrayNull(&(*benders)->benderscuts);
1081 
1082  SCIPclockFree(&(*benders)->bendersclock);
1083  SCIPclockFree(&(*benders)->setuptime);
1084  BMSfreeMemoryArray(&(*benders)->name);
1085  BMSfreeMemoryArray(&(*benders)->desc);
1086  BMSfreeMemory(benders);
1087 
1088  return SCIP_OKAY;
1089 }
1090 
1091 /** initialises a MIP subproblem by putting the problem into SCIP_STAGE_SOLVING. This is achieved by calling SCIPsolve
1092  * and then interrupting the solve in a node focus event handler.
1093  * The LP subproblem is also initialised using this method; however, a different event handler is added. This event
1094  * handler will put the LP subproblem into probing mode.
1095  * The MIP solving function is called to initialise the subproblem because this function calls SCIPsolve with the
1096  * appropriate parameter settings for Benders' decomposition.
1097  */
1098 static
1100  SCIP_BENDERS* benders, /**< Benders' decomposition */
1101  SCIP_SET* set, /**< global SCIP settings */
1102  int probnumber, /**< the subproblem number */
1103  SCIP_Bool* success /**< was the initialisation process successful */
1104  )
1105 {
1106  SCIP* subproblem;
1107  SCIP_Bool infeasible;
1108  SCIP_Bool cutoff;
1109 
1110  assert(benders != NULL);
1111  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
1112  assert(success != NULL);
1113 
1114  (*success) = FALSE;
1115 
1116  subproblem = SCIPbendersSubproblem(benders, probnumber);
1117  assert(subproblem != NULL);
1118 
1119  /* Getting the problem into the right SCIP stage for solving */
1120  SCIP_CALL( SCIPbendersSolveSubproblemCIP(set->scip, benders, probnumber, &infeasible, SCIP_BENDERSENFOTYPE_LP,
1121  FALSE) );
1122 
1123  /* Constructing the LP that can be solved in later iterations */
1124  if( SCIPgetStatus(subproblem) != SCIP_STATUS_BESTSOLLIMIT && SCIPgetStatus(subproblem) != SCIP_STATUS_TIMELIMIT
1125  && SCIPgetStatus(subproblem) != SCIP_STATUS_MEMLIMIT )
1126  {
1127  assert(SCIPgetStage(subproblem) == SCIP_STAGE_SOLVING);
1128 
1129  SCIP_CALL( SCIPconstructLP(subproblem, &cutoff) );
1130  (*success) = TRUE;
1131  }
1132 
1133  return SCIP_OKAY;
1134 }
1135 
1136 
1137 /** initialises an LP subproblem by putting the problem into probing mode. The probing mode is invoked in a node focus
1138  * event handler. This event handler is added just prior to calling the initialise subproblem function.
1139  */
1140 static
1142  SCIP_BENDERS* benders, /**< Benders' decomposition */
1143  SCIP_SET* set, /**< global SCIP settings */
1144  int probnumber /**< the subproblem number */
1145  )
1146 {
1147  SCIP* subproblem;
1148  SCIP_EVENTHDLR* eventhdlr;
1149  SCIP_EVENTHDLRDATA* eventhdlrdata;
1150  SCIP_Bool success;
1151 
1152  assert(benders != NULL);
1153  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
1154 
1155  subproblem = SCIPbendersSubproblem(benders, probnumber);
1156  assert(subproblem != NULL);
1157 
1158  /* include event handler into SCIP */
1159  SCIP_CALL( SCIPallocBlockMemory(subproblem, &eventhdlrdata) );
1160 
1161  SCIP_CALL( initEventhandlerData(subproblem, eventhdlrdata) );
1162 
1164  eventExecBendersNodefocus, eventhdlrdata) );
1165  SCIP_CALL( SCIPsetEventhdlrInitsol(subproblem, eventhdlr, eventInitsolBendersNodefocus) );
1166  SCIP_CALL( SCIPsetEventhdlrExitsol(subproblem, eventhdlr, eventExitsolBendersNodefocus) );
1167  SCIP_CALL( SCIPsetEventhdlrExit(subproblem, eventhdlr, eventExitBendersNodefocus) );
1168  SCIP_CALL( SCIPsetEventhdlrFree(subproblem, eventhdlr, eventFreeBendersNodefocus) );
1169  assert(eventhdlr != NULL);
1170 
1171  /* calling an initial solve to put the problem into probing mode */
1172  SCIP_CALL( initialiseSubproblem(benders, set, probnumber, &success) );
1173 
1174  return SCIP_OKAY;
1175 }
1176 
1177 /** creates the subproblems and registers it with the Benders' decomposition struct */
1178 static
1180  SCIP_BENDERS* benders, /**< Benders' decomposition */
1181  SCIP_SET* set /**< global SCIP settings */
1182  )
1183 {
1184  SCIP* subproblem;
1185  SCIP_EVENTHDLR* eventhdlr;
1186  SCIP_VAR* mastervar;
1187  SCIP_VAR** vars;
1188  int nvars;
1189  int nbinvars;
1190  int nintvars;
1191  int nimplintvars;
1192  int nsubproblems;
1193  int i;
1194  int j;
1195 
1196  assert(benders != NULL);
1197  assert(set != NULL);
1198 
1199  /* if the subproblems have already been created, then they will not be created again. This is the case if the
1200  * transformed problem has been freed and then retransformed. The subproblems should only be created when the problem
1201  * is first transformed. */
1202  if( benders->subprobscreated )
1203  return SCIP_OKAY;
1204 
1205  nsubproblems = SCIPbendersGetNSubproblems(benders);
1206 
1207  /* creating all subproblems */
1208  for( i = 0; i < nsubproblems; i++ )
1209  {
1210  /* calling the create subproblem call back method */
1211  SCIP_CALL( benders->benderscreatesub(set->scip, benders, i) );
1212 
1213  subproblem = SCIPbendersSubproblem(benders, i);
1214 
1215  assert(subproblem != NULL);
1216 
1217  /* setting global limits for the subproblems. This overwrites the limits set by the user */
1218  SCIP_CALL( SCIPsetIntParam(subproblem, "limits/maxorigsol", 0) );
1219 
1220  /* getting the number of integer and binary variables to determine the problem type */
1221  SCIP_CALL( SCIPgetVarsData(subproblem, &vars, &nvars, &nbinvars, &nintvars, &nimplintvars, NULL) );
1222 
1223  /* The objective function coefficients of the master problem are set to zero. This is necessary for the Benders'
1224  * decomposition algorithm, since the cut methods and the objective function check assumes that the objective
1225  * coefficients of the master problem variables are zero.
1226  *
1227  * This only occurs if the Benders' decomposition is not a copy. It is assumed that the correct objective
1228  * coefficients are given during the first subproblem creation.
1229  */
1230  if( !benders->iscopy )
1231  {
1232  SCIP_Bool objchanged = FALSE;
1233 
1234  assert(SCIPgetStage(subproblem) == SCIP_STAGE_PROBLEM);
1235  for( j = 0; j < nvars; j++ )
1236  {
1237  /* retrieving the master problem variable */
1238  SCIP_CALL( SCIPbendersGetVar(benders, set, vars[j], &mastervar, -1) );
1239 
1240  /* if mastervar is not NULL, then the subproblem variable has a corresponding master problem variable */
1241  if( mastervar != NULL && !SCIPisZero(subproblem, SCIPvarGetObj(vars[j])) )
1242  {
1243  SCIPverbMessage(subproblem, SCIP_VERBLEVEL_FULL, NULL, "Benders' decomposition: Changing the objective "
1244  "coefficient of copy of master problem variable <%s> in subproblem %d to zero.\n",
1245  SCIPvarGetName(mastervar), i);
1246  /* changing the subproblem variable objective coefficient to zero */
1247  SCIP_CALL( SCIPchgVarObj(subproblem, vars[j], 0.0) );
1248 
1249  objchanged = TRUE;
1250  }
1251  }
1252 
1253  if( objchanged )
1254  {
1255  SCIPverbMessage(subproblem, SCIP_VERBLEVEL_HIGH, NULL, "Benders' decomposition: Objective coefficients of "
1256  "copied of master problem variables has been changed to zero.\n");
1257  }
1258  }
1259 
1260  /* if there are no binary and integer variables, then the subproblem is an LP.
1261  * In this case, the SCIP instance is put into probing mode via the use of an event handler.
1262  * The check for convexity is only performed if the user has not implemented subproblem solving functions.
1263  */
1264  if( benders->benderssolvesubconvex == NULL && benders->benderssolvesub == NULL )
1265  {
1266  if( nbinvars == 0 && nintvars == 0 && nimplintvars == 0 )
1267  {
1269 
1270  /* if the user has not implemented a solve subproblem callback, then the subproblem solves are performed
1271  * internally. To be more efficient the subproblem is put into probing mode. */
1272  if( SCIPgetStage(subproblem) <= SCIP_STAGE_PROBLEM )
1273  {
1274  SCIP_CALL( initialiseLPSubproblem(benders, set, i) );
1275  }
1276  }
1277  else
1278  {
1279  SCIP_EVENTHDLRDATA* eventhdlrdata_mipnodefocus;
1280  SCIP_EVENTHDLRDATA* eventhdlrdata_upperbound;
1281 
1283 
1284  /* because the subproblems could be reused in the copy, the event handler is not created again.
1285  * NOTE: This currently works with the benders_default implementation. It may not be very general. */
1286  if( !benders->iscopy )
1287  {
1288  SCIP_CALL( SCIPallocBlockMemory(subproblem, &eventhdlrdata_mipnodefocus) );
1289  SCIP_CALL( SCIPallocBlockMemory(subproblem, &eventhdlrdata_upperbound) );
1290 
1291  SCIP_CALL( initEventhandlerData(subproblem, eventhdlrdata_mipnodefocus) );
1292  SCIP_CALL( initEventhandlerData(subproblem, eventhdlrdata_upperbound) );
1293 
1294  /* include the first LP solved event handler into the subproblem */
1296  MIPNODEFOCUS_EVENTHDLR_DESC, eventExecBendersMipnodefocus, eventhdlrdata_mipnodefocus) );
1297  SCIP_CALL( SCIPsetEventhdlrInitsol(subproblem, eventhdlr, eventInitsolBendersMipnodefocus) );
1298  SCIP_CALL( SCIPsetEventhdlrExitsol(subproblem, eventhdlr, eventExitsolBendersMipnodefocus) );
1299  SCIP_CALL( SCIPsetEventhdlrExit(subproblem, eventhdlr, eventExitBendersMipnodefocus) );
1300  SCIP_CALL( SCIPsetEventhdlrFree(subproblem, eventhdlr, eventFreeBendersMipnodefocus) );
1301  assert(eventhdlr != NULL);
1302 
1303  /* include the upper bound interrupt event handler into the subproblem */
1305  UPPERBOUND_EVENTHDLR_DESC, eventExecBendersUpperbound, eventhdlrdata_upperbound) );
1306  SCIP_CALL( SCIPsetEventhdlrInitsol(subproblem, eventhdlr, eventInitsolBendersUpperbound) );
1307  SCIP_CALL( SCIPsetEventhdlrExitsol(subproblem, eventhdlr, eventExitsolBendersUpperbound) );
1308  SCIP_CALL( SCIPsetEventhdlrExit(subproblem, eventhdlr, eventExitBendersUpperbound) );
1309  SCIP_CALL( SCIPsetEventhdlrFree(subproblem, eventhdlr, eventFreeBendersUpperbound) );
1310  assert(eventhdlr != NULL);
1311  }
1312  }
1313  }
1314  }
1315 
1316  benders->subprobscreated = TRUE;
1317 
1318  return SCIP_OKAY;
1319 }
1320 
1321 
1322 /** initializes Benders' decomposition */
1324  SCIP_BENDERS* benders, /**< Benders' decomposition */
1325  SCIP_SET* set /**< global SCIP settings */
1326  )
1327 {
1328  int i;
1329 
1330  assert(benders != NULL);
1331  assert(set != NULL);
1332 
1333  if( benders->initialized )
1334  {
1335  SCIPerrorMessage("Benders' decomposition <%s> already initialized\n", benders->name);
1336  return SCIP_INVALIDCALL;
1337  }
1338 
1339  if( set->misc_resetstat )
1340  {
1341  SCIPclockReset(benders->setuptime);
1342  SCIPclockReset(benders->bendersclock);
1343 
1344  benders->ncalls = 0;
1345  benders->ncutsfound = 0;
1346  benders->ntransferred = 0;
1347  }
1348 
1349  /* start timing */
1350  SCIPclockStart(benders->setuptime, set);
1351 
1352  if( benders->bendersinit != NULL )
1353  {
1354  SCIP_CALL( benders->bendersinit(set->scip, benders) );
1355  }
1356 
1357  benders->initialized = TRUE;
1358 
1359  /* creates the subproblems and sets up the probing mode for LP subproblems. This function calls the benderscreatesub
1360  * callback. */
1361  SCIP_CALL( createSubproblems(benders, set) );
1362 
1363  /* initialising the Benders' cuts */
1364  SCIPbendersSortBenderscuts(benders);
1365  for( i = 0; i < benders->nbenderscuts; i++ )
1366  {
1367  SCIP_CALL( SCIPbenderscutInit(benders->benderscuts[i], set) );
1368  }
1369 
1370  /* stop timing */
1371  SCIPclockStop(benders->setuptime, set);
1372 
1373  return SCIP_OKAY;
1374 }
1375 
1376 
1377 /** Transfers Benders' cuts that were generated while solving a sub-SCIP to the original SCIP instance. This involves
1378  * creating a constraint/cut that is equivalent to the generated cut in the sub-SCIP. This new constraint/cut is then
1379  * added to the original SCIP instance.
1380  */
1381 static
1383  SCIP* sourcescip, /**< the source SCIP from when the Benders' decomposition was copied */
1384  SCIP_BENDERS* benders, /**< the Benders' decomposition structure of the sub SCIP */
1385  SCIP_VAR** vars, /**< the variables from the source constraint */
1386  SCIP_Real* vals, /**< the coefficients of the variables in the source constriant */
1387  SCIP_Real lhs, /**< the LHS of the source constraint */
1388  SCIP_Real rhs, /**< the RHS of the source constraint */
1389  int nvars /**< the number of variables in the source constraint */
1390  )
1391 {
1392  SCIP_BENDERS* sourcebenders; /* the Benders' decomposition of the source SCIP */
1393  SCIP_CONSHDLR* consbenders; /* a helper variable for the Benders' decomposition constraint handler */
1394  SCIP_CONS* transfercons; /* the constraint that is generated to transfer the constraints/cuts */
1395  SCIP_ROW* transfercut; /* the cut that is generated to transfer the constraints/cuts */
1396  SCIP_VAR* sourcevar; /* the source variable that will be added to the transferred cut */
1397  SCIP_VAR* origvar;
1398  SCIP_Real scalar;
1399  SCIP_Real constant;
1400  char cutname[SCIP_MAXSTRLEN]; /* the name of the transferred cut */
1401  int i;
1402  SCIP_Bool fail;
1403 
1404  assert(sourcescip != NULL);
1405  assert(benders != NULL);
1406  assert(vars != NULL);
1407  assert(vals != NULL);
1408 
1409  /* retrieving the source Benders' decomposition structure */
1410  sourcebenders = SCIPfindBenders(sourcescip, SCIPbendersGetName(benders));
1411 
1412  /* retrieving the Benders' decomposition constraint handler */
1413  consbenders = SCIPfindConshdlr(sourcescip, "benders");
1414 
1415  /* setting the name of the transferred cut */
1416  (void) SCIPsnprintf(cutname, SCIP_MAXSTRLEN, "transferredcut_%d",
1417  SCIPbendersGetNTransferredCuts(sourcebenders) );
1418 
1419  /* TODO: It could be more efficient to pass an updated vars array with the vals array to the
1420  * SCIPcreateConsBasicLinear/SCIPcreateEmptyRowCons. This should be implemented to improve the performance of the
1421  * Large Neighbourhood Benders Search.
1422  */
1423 
1424  /* creating an empty row/constraint for the transferred cut */
1425  if( sourcebenders->cutsasconss )
1426  {
1427  SCIP_CALL( SCIPcreateConsBasicLinear(sourcescip, &transfercons, cutname, 0, NULL, NULL, lhs, rhs) );
1428  SCIP_CALL( SCIPsetConsRemovable(sourcescip, transfercons, TRUE) );
1429  }
1430  else
1431  {
1432  SCIP_CALL( SCIPcreateEmptyRowCons(sourcescip, &transfercut, consbenders, cutname, lhs, rhs, FALSE,
1433  FALSE, TRUE) );
1434  }
1435 
1436  fail = FALSE;
1437  for( i = 0; i < nvars; i++ )
1438  {
1439  /* getting the original variable for the transformed variable */
1440  origvar = vars[i];
1441  scalar = 1.0;
1442  constant = 0.0;
1443  SCIP_CALL( SCIPvarGetOrigvarSum(&origvar, &scalar, &constant) );
1444 
1445  /* getting the source var from the hash map */
1446  sourcevar = (SCIP_VAR*) SCIPhashmapGetImage(benders->mastervarsmap, origvar);
1447 
1448  /* if the source variable is not found, then the mapping in incomplete. So the constraint can not be
1449  * transferred. */
1450  if( sourcevar == NULL )
1451  {
1452  fail = TRUE;
1453  break;
1454  }
1455 
1456  if( sourcebenders->cutsasconss )
1457  {
1458  SCIP_CALL( SCIPaddCoefLinear(sourcescip, transfercons, sourcevar, vals[i]) ); /*lint !e644*/
1459  }
1460  else
1461  {
1462  SCIP_CALL( SCIPaddVarToRow(sourcescip, transfercut, sourcevar, vals[i]) ); /*lint !e644*/
1463  }
1464  }
1465 
1466  /* if all of the source variables were found to generate the cut */
1467  if( !fail )
1468  {
1469  if( sourcebenders->cutsasconss )
1470  {
1471  SCIP_CALL( SCIPaddCons(sourcescip, transfercons) );
1472  }
1473  else
1474  {
1475  SCIP_CALL( SCIPaddPoolCut(sourcescip, transfercut) );
1476  }
1477 
1478  sourcebenders->ntransferred++;
1479  }
1480 
1481  /* release the row/constraint */
1482  if( sourcebenders->cutsasconss )
1483  {
1484  /* only release if the creation of the constraint failed. */
1485  SCIP_CALL( SCIPreleaseCons(sourcescip, &transfercons) );
1486  }
1487  else
1488  {
1489  SCIP_CALL( SCIPreleaseRow(sourcescip, &transfercut) );
1490  }
1491 
1492  return SCIP_OKAY;
1493 }
1494 
1495 
1496 /** transfers the cuts generated in a subscip to the source scip */
1497 static
1499  SCIP* sourcescip, /**< the source SCIP from when the Benders' decomposition was copied */
1500  SCIP* subscip, /**< the sub SCIP where the Benders' cuts were generated */
1501  SCIP_BENDERS* benders /**< the Benders' decomposition structure of the sub SCIP */
1502  )
1503 {
1504  SCIP_BENDERS* sourcebenders; /* the Benders' decomposition of the source SCIP */
1505  SCIP_BENDERSCUT* benderscut; /* a helper variable for the Benders' cut plugin */
1506  SCIP_VAR** vars; /* the variables of the added constraint/row */
1507  SCIP_Real* vals; /* the values of the added constraint/row */
1508  SCIP_Real lhs; /* the LHS of the added constraint/row */
1509  SCIP_Real rhs; /* the RHS of the added constraint/row */
1510  int naddedcuts;
1511  int nvars;
1512  int i;
1513  int j;
1514 
1515  assert(subscip != NULL);
1516  assert(benders != NULL);
1517 
1518  /* retrieving the source Benders' decomposition structure */
1519  sourcebenders = SCIPfindBenders(sourcescip, SCIPbendersGetName(benders));
1520 
1521  /* exit if the cuts should not be transferred from the sub SCIP to the source SCIP. */
1522  if( !sourcebenders->transfercuts )
1523  return SCIP_OKAY;
1524 
1525  for( i = 0; i < benders->nbenderscuts; i++ )
1526  {
1527  benderscut = benders->benderscuts[i];
1528 
1529  /* retreiving the number of stored Benders' cuts */
1530  naddedcuts = SCIPbenderscutGetNAddedCuts(benderscut);
1531 
1532  /* looping over all added cuts to construct the cut for the source scip */
1533  for( j = 0; j < naddedcuts; j++ )
1534  {
1535  /* collecting the variable information from the constraint */
1536  SCIP_CALL( SCIPbenderscutGetAddedCutData(benderscut, j, &vars, &vals, &lhs, &rhs, &nvars) );
1537 
1538  if( nvars > 0 )
1539  {
1540  /* create and add the cut to be transferred from the sub SCIP to the source SCIP */
1541  SCIP_CALL( createAndAddTransferredCut(sourcescip, benders, vars, vals, lhs, rhs, nvars) );
1542  }
1543  }
1544  }
1545 
1546  return SCIP_OKAY;
1547 }
1548 
1549 
1550 /** calls exit method of Benders' decomposition */
1552  SCIP_BENDERS* benders, /**< Benders' decomposition */
1553  SCIP_SET* set /**< global SCIP settings */
1554  )
1555 {
1556  int nsubproblems;
1557  int i;
1558 
1559  assert(benders != NULL);
1560  assert(set != NULL);
1561 
1562  if( !benders->initialized )
1563  {
1564  SCIPerrorMessage("Benders' decomposition <%s> not initialized\n", benders->name);
1565  return SCIP_INVALIDCALL;
1566  }
1567 
1568  /* start timing */
1569  SCIPclockStart(benders->setuptime, set);
1570 
1571  if( benders->bendersexit != NULL )
1572  {
1573  SCIP_CALL( benders->bendersexit(set->scip, benders) );
1574  }
1575 
1576  /* if the Benders' decomposition is a copy, then the generated cuts will be transferred to the source scip */
1577  if( benders->iscopy )
1578  {
1579  SCIP_CALL( transferBendersCuts(benders->sourcescip, set->scip, benders) );
1580  }
1581 
1582  /* releasing all of the auxiliary variables */
1583  nsubproblems = SCIPbendersGetNSubproblems(benders);
1584  for( i = 0; i < nsubproblems; i++ )
1585  {
1586  /* it is possible that the master problem is not solved. As such, the auxiliary variables will not be created. So
1587  * we don't need to release the variables
1588  */
1589  if( benders->auxiliaryvars[i] != NULL )
1590  {
1591  SCIP_CALL( SCIPreleaseVar(set->scip, &benders->auxiliaryvars[i]) );
1592  }
1593  }
1594 
1595  /* calling the exit method for the Benders' cuts */
1596  SCIPbendersSortBenderscuts(benders);
1597  for( i = 0; i < benders->nbenderscuts; i++ )
1598  {
1599  SCIP_CALL( SCIPbenderscutExit(benders->benderscuts[i], set) );
1600  }
1601 
1602  benders->initialized = FALSE;
1603 
1604  /* stop timing */
1605  SCIPclockStop(benders->setuptime, set);
1606 
1607  return SCIP_OKAY;
1608 }
1609 
1610 /** Checks whether a subproblem is independent. */
1611 static
1613  SCIP* scip, /**< the SCIP data structure */
1614  SCIP_BENDERS* benders /**< Benders' decomposition */
1615  )
1616 {
1617  SCIP_VAR** vars;
1618  int nvars;
1619  int nsubproblems;
1620  int i;
1621  int j;
1622 
1623  assert(scip != NULL);
1624  assert(benders != NULL);
1625 
1626  /* retrieving the master problem variables */
1627  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
1628 
1629  nsubproblems = SCIPbendersGetNSubproblems(benders);
1630 
1631  /* looping over all subproblems to check whether there exists at least one master problem variable */
1632  for( i = 0; i < nsubproblems; i++ )
1633  {
1634  SCIP_Bool independent = FALSE;
1635 
1636  /* if there are user defined solving or freeing functions, then it is not possible to declare the independence of
1637  * the subproblems.
1638  */
1639  if( benders->benderssolvesubconvex == NULL && benders->benderssolvesub == NULL
1640  && benders->bendersfreesub == NULL )
1641  {
1642  independent = TRUE;
1643 
1644  for( j = 0; j < nvars; j++ )
1645  {
1646  SCIP_VAR* subprobvar;
1647 
1648  /* getting the subproblem problem variable corresponding to the master problem variable */
1649  SCIP_CALL( SCIPgetBendersSubproblemVar(scip, benders, vars[j], &subprobvar, i) );
1650 
1651  /* if the subporblem variable is not NULL, then the subproblem depends on the master problem */
1652  if( subprobvar != NULL )
1653  {
1654  independent = FALSE;
1655  break;
1656  }
1657  }
1658 
1659  /* setting the independent flag */
1660  SCIPbendersSetSubproblemIsIndependent(benders, i, independent);
1661  }
1662  }
1663 
1664  return SCIP_OKAY;
1665 }
1666 
1667 /** informs the Benders' decomposition that the presolving process is being started */
1669  SCIP_BENDERS* benders, /**< Benders' decomposition */
1670  SCIP_SET* set, /**< global SCIP settings */
1671  SCIP_STAT* stat /**< dynamic problem statistics */
1672  )
1673 {
1674  assert(benders != NULL);
1675  assert(set != NULL);
1676  assert(stat != NULL);
1677 
1678  if( !benders->iscopy )
1679  {
1680  /* check the subproblem independence. This check is only performed if the user has not implemented a solve
1681  * subproblem function.
1682  */
1683  if( benders->benderssolvesubconvex == NULL && benders->benderssolvesub == NULL )
1684  SCIP_CALL( checkSubproblemIndependence(set->scip, benders) );
1685 
1686  /* adding the auxiliary variables to the master problem */
1687  SCIP_CALL( addAuxiliaryVariablesToMaster(set->scip, benders) );
1688  }
1689  else
1690  {
1691  /* the copied auxiliary variables must be assigned to the target Benders' decomposition */
1692  SCIP_CALL( assignAuxiliaryVariables(set->scip, benders) );
1693  }
1694 
1695  /* call presolving initialization method of Benders' decomposition */
1696  if( benders->bendersinitpre != NULL )
1697  {
1698  /* start timing */
1699  SCIPclockStart(benders->setuptime, set);
1700 
1701  SCIP_CALL( benders->bendersinitpre(set->scip, benders) );
1702 
1703  /* stop timing */
1704  SCIPclockStop(benders->setuptime, set);
1705  }
1706 
1707  return SCIP_OKAY;
1708 }
1709 
1710 
1711 /** informs the Benders' decomposition that the presolving process has completed */
1713  SCIP_BENDERS* benders, /**< Benders' decomposition */
1714  SCIP_SET* set, /**< global SCIP settings */
1715  SCIP_STAT* stat /**< dynamic problem statistics */
1716  )
1717 {
1718  assert(benders != NULL);
1719  assert(set != NULL);
1720  assert(stat != NULL);
1721 
1722  /* call presolving deinitialization method of Benders' decomposition */
1723  if( benders->bendersexitpre != NULL )
1724  {
1725  /* start timing */
1726  SCIPclockStart(benders->setuptime, set);
1727 
1728  SCIP_CALL( benders->bendersexitpre(set->scip, benders) );
1729 
1730  /* stop timing */
1731  SCIPclockStop(benders->setuptime, set);
1732  }
1733 
1734  return SCIP_OKAY;
1735 }
1736 
1737 /** informs Benders' decomposition that the branch and bound process is being started */
1739  SCIP_BENDERS* benders, /**< Benders' decomposition */
1740  SCIP_SET* set /**< global SCIP settings */
1741  )
1742 {
1743  int i;
1744 
1745  assert(benders != NULL);
1746  assert(set != NULL);
1747 
1748  /* call solving process initialization method of Benders' decomposition */
1749  if( benders->bendersinitsol != NULL )
1750  {
1751  /* start timing */
1752  SCIPclockStart(benders->setuptime, set);
1753 
1754  SCIP_CALL( benders->bendersinitsol(set->scip, benders) );
1755 
1756  /* stop timing */
1757  SCIPclockStop(benders->setuptime, set);
1758  }
1759 
1760  /* calling the initsol method for the Benders' cuts */
1761  SCIPbendersSortBenderscuts(benders);
1762  for( i = 0; i < benders->nbenderscuts; i++ )
1763  {
1764  SCIP_CALL( SCIPbenderscutInitsol(benders->benderscuts[i], set) );
1765  }
1766 
1767  return SCIP_OKAY;
1768 }
1769 
1770 /** informs Benders' decomposition that the branch and bound process data is being freed */
1772  SCIP_BENDERS* benders, /**< Benders' decomposition */
1773  SCIP_SET* set /**< global SCIP settings */
1774  )
1775 {
1776  int nsubproblems;
1777  int i;
1778 
1779  assert(benders != NULL);
1780  assert(set != NULL);
1781 
1782  nsubproblems = SCIPbendersGetNSubproblems(benders);
1783  /* freeing all subproblems that are independent, this is because they have not bee freed during the subproblem
1784  * solving loop.
1785  */
1786  for( i = 0; i < nsubproblems; i++ )
1787  {
1788  if( SCIPbendersSubproblemIsIndependent(benders, i) )
1789  {
1790  /* disabling the independence of the subproblem so that it can be freed */
1792 
1793  /* freeing the independent subproblem */
1794  SCIP_CALL( SCIPbendersFreeSubproblem(benders, set, i) );
1795  }
1796  }
1797 
1798  /* call solving process deinitialization method of Benders' decomposition */
1799  if( benders->bendersexitsol != NULL )
1800  {
1801  /* start timing */
1802  SCIPclockStart(benders->setuptime, set);
1803 
1804  SCIP_CALL( benders->bendersexitsol(set->scip, benders) );
1805 
1806  /* stop timing */
1807  SCIPclockStop(benders->setuptime, set);
1808  }
1809 
1810  /* sorting the Benders' decomposition cuts in order of priority. Only a single cut is generated for each subproblem
1811  * per solving iteration. This is particularly important in the case of the optimality and feasibility cuts. Since
1812  * these work on two different solutions to the subproblem, it is not necessary to generate both cuts. So, once the
1813  * feasibility cut is generated, then no other cuts will be generated.
1814  */
1815  SCIPbendersSortBenderscuts(benders);
1816 
1817  /* calling the exitsol method for the Benders' cuts */
1818  for( i = 0; i < benders->nbenderscuts; i++ )
1819  {
1820  SCIP_CALL( SCIPbenderscutExitsol(benders->benderscuts[i], set) );
1821  }
1822 
1823  return SCIP_OKAY;
1824 }
1825 
1826 /** activates Benders' decomposition such that it is called in LP solving loop */
1828  SCIP_BENDERS* benders, /**< the Benders' decomposition structure */
1829  SCIP_SET* set, /**< global SCIP settings */
1830  int nsubproblems /**< the number subproblems used in this decomposition */
1831  )
1832 {
1833  SCIP_EVENTHDLR* eventhdlr;
1834  SCIP_EVENTHDLRDATA* eventhdlrdata;
1835  int i;
1836 
1837  assert(benders != NULL);
1838  assert(set != NULL);
1839  assert(set->stage == SCIP_STAGE_INIT || set->stage == SCIP_STAGE_PROBLEM);
1840 
1841  if( !benders->active )
1842  {
1843  benders->active = TRUE;
1844  set->nactivebenders++;
1845  set->benderssorted = FALSE;
1846 
1847  benders->nsubproblems = nsubproblems;
1848  benders->nactivesubprobs = nsubproblems;
1849 
1850  /* allocating memory for the subproblems arrays */
1851  SCIP_ALLOC( BMSallocMemoryArray(&benders->subproblems, benders->nsubproblems) );
1852  SCIP_ALLOC( BMSallocMemoryArray(&benders->auxiliaryvars, benders->nsubproblems) );
1853  SCIP_ALLOC( BMSallocMemoryArray(&benders->subprobobjval, benders->nsubproblems) );
1857  SCIP_ALLOC( BMSallocMemoryArray(&benders->subprobsetup, benders->nsubproblems) );
1858  SCIP_ALLOC( BMSallocMemoryArray(&benders->indepsubprob, benders->nsubproblems) );
1861 
1862  for( i = 0; i < benders->nsubproblems; i++ )
1863  {
1864  benders->subproblems[i] = NULL;
1865  benders->auxiliaryvars[i] = NULL;
1866  benders->subprobobjval[i] = SCIPsetInfinity(set);
1867  benders->bestsubprobobjval[i] = SCIPsetInfinity(set);
1868  benders->subproblowerbound[i] = -SCIPsetInfinity(set);
1869  benders->subprobisconvex[i] = FALSE;
1870  benders->subprobsetup[i] = FALSE;
1871  benders->indepsubprob[i] = FALSE;
1872  benders->subprobenabled[i] = TRUE;
1873  benders->mastervarscont[i] = FALSE;
1874  }
1875 
1876  /* adding an eventhandler for updating the lower bound when the root node is solved. */
1877  eventhdlrdata = (SCIP_EVENTHDLRDATA*)benders;
1878 
1879  /* include event handler into SCIP */
1881  eventExecBendersNodesolved, eventhdlrdata) );
1882  SCIP_CALL( SCIPsetEventhdlrInitsol(set->scip, eventhdlr, eventInitsolBendersNodesolved) );
1883  assert(eventhdlr != NULL);
1884  }
1885 
1886  return SCIP_OKAY;
1887 }
1888 
1889 /** deactivates Benders' decomposition such that it is no longer called in LP solving loop */
1891  SCIP_BENDERS* benders, /**< the Benders' decomposition structure */
1892  SCIP_SET* set /**< global SCIP settings */
1893  )
1894 {
1895  assert(benders != NULL);
1896  assert(set != NULL);
1897  assert(set->stage == SCIP_STAGE_INIT || set->stage == SCIP_STAGE_PROBLEM);
1898 
1899  if( benders->active )
1900  {
1901 #ifndef NDEBUG
1902  int nsubproblems;
1903  int i;
1904 
1905  nsubproblems = SCIPbendersGetNSubproblems(benders);
1906 
1907  /* checking whether the auxiliary variables and subproblems are all NULL */
1908  for( i = 0; i < nsubproblems; i++ )
1909  assert(benders->auxiliaryvars[i] == NULL);
1910 #endif
1911 
1912  benders->active = FALSE;
1913  set->nactivebenders--;
1914  set->benderssorted = FALSE;
1915 
1916  /* freeing the memory allocated during the activation of the Benders' decomposition */
1919  BMSfreeMemoryArray(&benders->indepsubprob);
1920  BMSfreeMemoryArray(&benders->subprobsetup);
1924  BMSfreeMemoryArray(&benders->subprobobjval);
1925  BMSfreeMemoryArray(&benders->auxiliaryvars);
1926  BMSfreeMemoryArray(&benders->subproblems);
1927  }
1928 }
1929 
1930 /** returns whether the given Benders' decomposition is in use in the current problem */
1932  SCIP_BENDERS* benders /**< the Benders' decomposition structure */
1933  )
1934 {
1935  assert(benders != NULL);
1936 
1937  return benders->active;
1938 }
1939 
1940 /** updates the lower bound for all auxiliary variables. This is called if the first LP enforced is unbounded. */
1941 static
1943  SCIP_BENDERS* benders, /**< Benders' decomposition */
1944  SCIP_SET* set, /**< global SCIP settings */
1945  SCIP_RESULT* result /**< the result from updating the auxiliary variable lower bound */
1946  )
1947 {
1948  int nsubproblems;
1949  int i;
1950 
1951  assert(benders != NULL);
1952  assert(set != NULL);
1953 
1954  (*result) = SCIP_DIDNOTRUN;
1955 
1956  nsubproblems = SCIPbendersGetNSubproblems(benders);
1957 
1958  for( i = 0; i < nsubproblems; i++ )
1959  {
1960  SCIP_VAR* auxiliaryvar;
1961  SCIP_Real lowerbound;
1962  SCIP_Bool infeasible;
1963 
1964  infeasible = FALSE;
1965 
1966  /* computing the lower bound of the subproblem by solving it without any variable fixings */
1967  SCIP_CALL( SCIPbendersComputeSubproblemLowerbound(benders, set, i, &lowerbound, &infeasible) );
1968 
1969  /* if the subproblem is infeasible, then the original problem is infeasible */
1970  if( infeasible )
1971  {
1972  (*result) = SCIP_INFEASIBLE;
1973  break;
1974  }
1975 
1976  /* retrieving the auxiliary variable */
1977  auxiliaryvar = SCIPbendersGetAuxiliaryVar(benders, i);
1978 
1979  /* only update the lower bound if it is greater than the current lower bound */
1980  if( SCIPsetIsGT(set, lowerbound, SCIPvarGetLbGlobal(auxiliaryvar)) )
1981  {
1982  SCIPsetDebugMsg(set, "Tightened lower bound of <%s> to %g\n", SCIPvarGetName(auxiliaryvar), lowerbound);
1983  /* updating the lower bound of the auxiliary variable */
1984  SCIP_CALL( SCIPchgVarLb(set->scip, auxiliaryvar, lowerbound) );
1985  (*result) = SCIP_REDUCEDDOM;
1986  }
1987 
1988  /* stores the lower bound for the subproblem */
1989  SCIPbendersUpdateSubproblemLowerbound(benders, i, lowerbound);
1990  }
1991 
1992  return SCIP_OKAY;
1993 }
1994 
1995 /** Returns whether only the convex relaxations will be checked in this solve loop
1996  * when Benders' is used in the LNS heuristics, only the convex relaxations of the master/subproblems are checked,
1997  * i.e. no integer cuts are generated. In this case, then Benders' decomposition is performed under the assumption
1998  * that all subproblems are convex relaxations.
1999  */
2001  SCIP_BENDERS* benders /**< Benders' decomposition */
2002  )
2003 {
2004  return benders->iscopy && benders->lnscheck;
2005 }
2006 
2007 /** returns the number of subproblems that will be checked in this iteration */
2008 static
2010  SCIP_BENDERS* benders, /**< Benders' decomposition */
2011  SCIP_SET* set, /**< global SCIP settings */
2012  SCIP_BENDERSENFOTYPE type /**< the type of solution being enforced */
2013  )
2014 {
2015  if( benders->ncalls == 0 || type == SCIP_BENDERSENFOTYPE_CHECK || SCIPbendersOnlyCheckConvexRelax(benders) )
2016  return SCIPbendersGetNSubproblems(benders);
2017  else
2018  return (int) SCIPsetCeil(set, (SCIP_Real) SCIPbendersGetNSubproblems(benders)*benders->subprobfrac);
2019 }
2020 
2021 /** returns whether the solving of the given subproblem needs to be executed */
2022 static
2024  SCIP_BENDERS* benders, /**< Benders' decomposition */
2025  int probnumber /**< the subproblem index */
2026  )
2027 {
2028  return (!SCIPbendersSubproblemIsIndependent(benders, probnumber)
2029  && SCIPbendersSubproblemIsEnabled(benders, probnumber));
2030 }
2031 
2032 /** Solves each of the Benders' decomposition subproblems for the given solution. All, or a fraction, of subproblems are
2033  * solved before the Benders' decomposition cuts are generated.
2034  * Since a convex relaxation of the subproblem could be solved to generate cuts, a parameter nverified is used to
2035  * identified the number of subproblems that have been solved in their "original" form. For example, if the subproblem
2036  * is a MIP, then if the LP is solved to generate cuts, this does not constitute a verification. The verification is
2037  * only performed when the MIP is solved.
2038  */
2039 static
2041  SCIP_BENDERS* benders, /**< Benders' decomposition */
2042  SCIP_SET* set, /**< global SCIP settings */
2043  SCIP_SOL* sol, /**< primal CIP solution */
2044  SCIP_BENDERSENFOTYPE type, /**< the type of solution being enforced */
2045  SCIP_BENDERSSOLVELOOP solveloop, /**< the current solve loop */
2046  SCIP_Bool checkint, /**< are the subproblems called during a check/enforce of integer sols? */
2047  int* nchecked, /**< the number of subproblems checked in this solve loop, they may not be solved */
2048  int* nverified, /**< the number of subproblems verified in the current loop */
2049  SCIP_Bool** subprobsolved, /**< an array indicating the subproblems that were solved in this loop. */
2050  SCIP_BENDERSSUBSTATUS** substatus, /**< array to store the status of the subsystem */
2051  SCIP_Bool* infeasible, /**< is the master problem infeasible with respect to the Benders' cuts? */
2052  SCIP_Bool* optimal, /**< is the current solution optimal? */
2053  SCIP_Bool* stopped /**< was the solving process stopped? */
2054  )
2055 {
2056  SCIP_Bool onlyconvexcheck;
2057  int nsubproblems;
2058  int numtocheck;
2059  int numnotopt;
2060  int subproblemcount;
2061  int i;
2062 
2063  assert(benders != NULL);
2064  assert(set != NULL);
2065 
2066  (*stopped) = FALSE;
2067 
2068  /* getting the number of subproblems in the Benders' decompsition */
2069  nsubproblems = SCIPbendersGetNSubproblems(benders);
2070 
2071  /* in the case of an LNS check, only the convex relaxations of the subproblems will be solved. This is a performance
2072  * feature, since solving the convex relaxation is typically much faster than solving the corresponding CIP. While
2073  * the CIP is not solved during the LNS check, the solutions are still of higher quality than when Benders' is not
2074  * employed.
2075  */
2076  onlyconvexcheck = SCIPbendersOnlyCheckConvexRelax(benders);
2077 
2078  /* it is possible to only solve a subset of subproblems. This is given by a parameter. */
2079  numtocheck = numSubproblemsToCheck(benders, set, type);
2080 
2081  SCIPsetDebugMsg(set, "Performing the subproblem solving process. Number of subproblems to check %d\n", numtocheck);
2082 
2083  SCIPsetDebugMsg(set, "Benders' decomposition - solve loop %d\n", solveloop);
2084  numnotopt = 0;
2085  subproblemcount = 0;
2086 
2087  if( type == SCIP_BENDERSENFOTYPE_CHECK && sol == NULL )
2088  {
2089  /* TODO: Check whether this is absolutely necessary. I think that this if statment can be removed. */
2090  (*infeasible) = TRUE;
2091  }
2092  else
2093  {
2094  /* solving each of the subproblems for Benders' decomposition */
2095  /* TODO: ensure that the each of the subproblems solve and update the parameters with the correct return values
2096  */
2097  i = benders->firstchecked;
2098  /*for( i = 0; i < nsubproblems; i++ )*/
2099  while( subproblemcount < nsubproblems && numnotopt < numtocheck && !(*stopped) )
2100  {
2101  SCIP_Bool subinfeas = FALSE;
2102  SCIP_Bool convexsub = SCIPbendersSubproblemIsConvex(benders, i);
2103  SCIP_Bool solvesub = TRUE;
2104  SCIP_Bool solved;
2105 
2106  /* the subproblem is initially flagged as not solved for this solving loop */
2107  (*subprobsolved)[i] = FALSE;
2108 
2109  /* setting the subsystem status to UNKNOWN at the start of each solve loop */
2110  (*substatus)[i] = SCIP_BENDERSSUBSTATUS_UNKNOWN;
2111 
2112  /* for the second solving loop, if the problem is an LP, it is not solved again. If the problem is a MIP,
2113  * then the subproblem objective function value is set to infinity. However, if the subproblem is proven
2114  * infeasible from the LP, then the IP loop is not performed.
2115  * If the solve loop is SCIP_BENDERSSOLVELOOP_USERCIP, then nothing is done. It is assumed that the user will
2116  * correctly update the objective function within the user-defined solving function.
2117  */
2118  if( solveloop == SCIP_BENDERSSOLVELOOP_CIP )
2119  {
2120  if( convexsub || (*substatus)[i] == SCIP_BENDERSSUBSTATUS_INFEAS )
2121  solvesub = FALSE;
2122  else
2124  }
2125 
2126  /* if the subproblem is independent, then it does not need to be solved. In this case, the nverified flag will
2127  * increase by one. When the subproblem is not independent, then it needs to be checked.
2128  */
2129  if( !subproblemIsActive(benders, i) )
2130  {
2131  /* NOTE: There is no need to update the optimal flag. This is because optimal is always TRUE until a
2132  * non-optimal subproblem is found.
2133  */
2134  /* if the auxiliary variable value is infinity, then the subproblem has not been solved yet. Currently the
2135  * subproblem statue is unknown. */
2136  if( SCIPsetIsInfinity(set, SCIPbendersGetAuxiliaryVarVal(benders, set, sol, i))
2137  || SCIPsetIsInfinity(set, -SCIPbendersGetAuxiliaryVarVal(benders, set, sol, i))
2139  {
2141  (*substatus)[i] = SCIP_BENDERSSUBSTATUS_UNKNOWN;
2142  (*optimal) = FALSE;
2143 
2144  SCIPsetDebugMsg(set, "Benders' decomposition: subproblem %d is not active, but has not been solved."
2145  " setting status to UNKNOWN\n", i);
2146  }
2147  else
2148  {
2149  SCIP_Real soltol;
2150 
2151  SCIP_CALL( SCIPsetGetRealParam(set, "benders/solutiontol", &soltol) );
2152 
2154  SCIPbendersGetAuxiliaryVarVal(benders, set, sol, i)) < soltol )
2155  {
2156  SCIPbendersSetSubproblemObjval(benders, i, SCIPbendersGetAuxiliaryVarVal(benders, set, sol, i));
2157  (*substatus)[i] = SCIP_BENDERSSUBSTATUS_OPTIMAL;
2158  }
2159  else
2160  {
2162  (*substatus)[i] = SCIP_BENDERSSUBSTATUS_AUXVIOL;
2163  }
2164 
2165  SCIPsetDebugMsg(set, "Benders' decomposition: subproblem %d is not active, setting status to OPTIMAL\n", i);
2166  }
2167 
2168  (*subprobsolved)[i] = TRUE;
2169 
2170  /* the nverified counter is only increased in the convex solving loop */
2171  if( solveloop == SCIP_BENDERSSOLVELOOP_CONVEX || solveloop == SCIP_BENDERSSOLVELOOP_USERCONVEX )
2172  (*nverified)++;
2173  }
2174  else if( solvesub )
2175  {
2176  SCIP_CALL( SCIPbendersExecSubproblemSolve(benders, set, sol, i, solveloop, FALSE, &solved, &subinfeas, type) );
2177 
2178 #ifdef SCIP_DEBUG
2179  if( type == SCIP_BENDERSENFOTYPE_LP )
2180  {
2181  SCIPsetDebugMsg(set, "LP: Subproblem %d (%f < %f)\n", i, SCIPbendersGetAuxiliaryVarVal(benders, set, sol, i),
2182  SCIPbendersGetSubproblemObjval(benders, i));
2183  }
2184 #endif
2185  (*subprobsolved)[i] = solved;
2186 
2187  (*infeasible) = (*infeasible) || subinfeas;
2188  if( subinfeas )
2189  (*substatus)[i] = SCIP_BENDERSSUBSTATUS_INFEAS;
2190 
2191  /* if the subproblems are solved to check integer feasibility, then the optimality check must be performed.
2192  * This will only be performed if checkint is TRUE and the subproblem was solved. The subproblem may not be
2193  * solved if the user has defined a solving function
2194  */
2195  if( checkint && (*subprobsolved)[i] )
2196  {
2197  /* if the subproblem is feasible, then it is necessary to update the value of the auxiliary variable to the
2198  * objective function value of the subproblem.
2199  */
2200  if( !subinfeas )
2201  {
2202  SCIP_Bool subproboptimal = FALSE;
2203 
2204  SCIP_CALL( SCIPbendersCheckSubproblemOptimality(benders, set, sol, i, &subproboptimal) );
2205 
2206  if( subproboptimal )
2207  (*substatus)[i] = SCIP_BENDERSSUBSTATUS_OPTIMAL;
2208  else
2209  (*substatus)[i] = SCIP_BENDERSSUBSTATUS_AUXVIOL;
2210 
2211  /* It is only possible to determine the optimality of a solution within a given subproblem in four
2212  * different cases:
2213  * i) solveloop == SCIP_BENDERSSOLVELOOP_CONVEX or USERCONVEX and the subproblem is convex.
2214  * ii) solveloop == SCIP_BENDERSOLVELOOP_CONVEX and only the convex relaxations will be checked.
2215  * iii) solveloop == SCIP_BENDERSSOLVELOOP_USERCIP and the subproblem was solved, since the user has
2216  * defined a solve function, it is expected that the solving is correctly executed.
2217  * iv) solveloop == SCIP_BENDERSSOLVELOOP_CIP and the MIP for the subproblem has been solved.
2218  */
2219  if( convexsub || onlyconvexcheck
2220  || solveloop == SCIP_BENDERSSOLVELOOP_CIP
2221  || solveloop == SCIP_BENDERSSOLVELOOP_USERCIP )
2222  (*optimal) = (*optimal) && subproboptimal;
2223 
2224 #ifdef SCIP_DEBUG
2225  if( convexsub || solveloop >= SCIP_BENDERSSOLVELOOP_CIP )
2226  {
2227  if( subproboptimal )
2228  {
2229  SCIPsetDebugMsg(set, "Subproblem %d is Optimal (%f >= %f)\n", i,
2230  SCIPbendersGetAuxiliaryVarVal(benders, set, sol, i), SCIPbendersGetSubproblemObjval(benders, i));
2231  }
2232  else
2233  {
2234  SCIPsetDebugMsg(set, "Subproblem %d is NOT Optimal (%f < %f)\n", i,
2235  SCIPbendersGetAuxiliaryVarVal(benders, set, sol, i), SCIPbendersGetSubproblemObjval(benders, i));
2236  }
2237  }
2238 #endif
2239 
2240  /* the nverified variable is only incremented when the original form of the subproblem has been solved.
2241  * What is meant by "original" is that the LP relaxation of CIPs are solved to generate valid cuts. So
2242  * if the subproblem is defined as a CIP, then it is only classified as checked if the CIP is solved.
2243  * There are three cases where the "original" form is solved are:
2244  * i) solveloop == SCIP_BENDERSSOLVELOOP_CONVEX or USERCONVEX and the subproblem is an LP
2245  * - the original form has been solved.
2246  * ii) solveloop == SCIP_BENDERSSOLVELOOP_CIP or USERCIP and the CIP for the subproblem has been
2247  * solved.
2248  * iii) or, only a convex check is performed.
2249  */
2250  if( ((solveloop == SCIP_BENDERSSOLVELOOP_CONVEX || solveloop == SCIP_BENDERSSOLVELOOP_USERCONVEX)
2251  && convexsub)
2252  || ((solveloop == SCIP_BENDERSSOLVELOOP_CIP || solveloop == SCIP_BENDERSSOLVELOOP_USERCIP)
2253  && !convexsub)
2254  || onlyconvexcheck )
2255  (*nverified)++;
2256 
2257  if( !subproboptimal )
2258  {
2259  numnotopt++;
2260  assert(numnotopt <= nsubproblems);
2261  }
2262  }
2263  else
2264  {
2265  numnotopt++;
2266  assert(numnotopt <= nsubproblems);
2267  }
2268  }
2269  }
2270 
2271  subproblemcount++;
2272  i++;
2273  if( i >= nsubproblems )
2274  i = 0;
2275  benders->lastchecked = i;
2276 
2277  /* checking whether the limits have been exceeded in the master problem */
2278  (*stopped) = SCIPisStopped(set->scip);
2279  }
2280  }
2281 
2282  (*nchecked) = subproblemcount;
2283 
2284  return SCIP_OKAY;
2285 }
2286 
2287 /** Calls the Benders' decompsition cuts for the given solve loop. There are four cases:
2288  * i) solveloop == SCIP_BENDERSSOLVELOOP_CONVEX - only the LP Benders' cuts are called
2289  * ii) solveloop == SCIP_BENDERSSOLVELOOP_CIP - only the CIP Benders' cuts are called
2290  * iii) solveloop == SCIP_BENDERSSOLVELOOP_USERCONVEX - only the LP Benders' cuts are called
2291  * iv) solveloop == SCIP_BENDERSSOLVELOOP_USERCIP - only the CIP Benders' cuts are called
2292  *
2293  * The priority of the results are: SCIP_CONSADDED (SCIP_SEPARATED), SCIP_DIDNOTFIND, SCIP_FEASIBLE, SCIP_DIDNOTRUN. In
2294  * this function, there are four levels of results that need to be assessed. These are:
2295  * i) The result from the individual cut for the subproblem
2296  * ii) The overall result for the subproblem from all cuts
2297  * iii) the overall result for the solve loop from all cuts
2298  * iv) the over all result from all solve loops.
2299  * In each level, the priority of results must be adhered to.
2300  */
2301 static
2303  SCIP_BENDERS* benders, /**< Benders' decomposition */
2304  SCIP_SET* set, /**< global SCIP settings */
2305  SCIP_SOL* sol, /**< primal CIP solution */
2306  SCIP_RESULT* result, /**< result of the pricing process */
2307  SCIP_BENDERSENFOTYPE type, /**< the type of solution being enforced */
2308  SCIP_BENDERSSOLVELOOP solveloop, /**< the current solve loop */
2309  SCIP_Bool checkint, /**< are the subproblems called during a check/enforce of integer sols? */
2310  int nchecked, /**< the number of subproblems checked in this solve loop, they may not be solved */
2311  SCIP_Bool* subprobsolved, /**< an array indicating the subproblems that were solved in this loop. */
2312  SCIP_BENDERSSUBSTATUS* substatus, /**< array to store the status of the subsystem */
2313  int** mergecands, /**< the subproblems that are merge candidates */
2314  int* npriomergecands, /**< the number of priority merge candidates. */
2315  int* nmergecands, /**< the number of merge candidates. */
2316  int* nsolveloops /**< the number of solve loops, is updated w.r.t added cuts */
2317  )
2318 {
2319  SCIP_BENDERSCUT** benderscuts;
2320  SCIP_RESULT solveloopresult;
2321  int nbenderscuts;
2322  int nsubproblems;
2323  int subproblemcount;
2324  SCIP_Longint addedcuts = 0;
2325  int i;
2326  int j;
2327  SCIP_Bool onlyconvexcheck;
2328 
2329  assert(benders != NULL);
2330  assert(set != NULL);
2331 
2332  /* getting the Benders' decomposition cuts */
2333  benderscuts = SCIPbendersGetBenderscuts(benders);
2334  nbenderscuts = SCIPbendersGetNBenderscuts(benders);
2335 
2336  solveloopresult = SCIP_DIDNOTRUN;
2337 
2338  /* getting the number of subproblems in the Benders' decomposition */
2339  nsubproblems = SCIPbendersGetNSubproblems(benders);
2340 
2341  /* in the case of an LNS check, only the convex relaxations of the subproblems will be solved. This is a performance
2342  * feature, since solving the convex relaxation is typically much faster than solving the corresponding CIP. While
2343  * the CIP is not solved during the LNS check, the solutions are still of higher quality than when Benders' is not
2344  * employed.
2345  */
2346  onlyconvexcheck = SCIPbendersOnlyCheckConvexRelax(benders);
2347 
2348  /* It is only possible to add cuts to the problem if it has not already been solved */
2350  {
2351  /* This is done in two loops. The first is by subproblem and the second is by cut type. */
2352  i = benders->firstchecked;
2353  subproblemcount = 0;
2354  while( subproblemcount < nchecked )
2355  {
2356  SCIP_RESULT subprobresult;
2357  SCIP_Bool convexsub = SCIPbendersSubproblemIsConvex(benders, i);
2358 
2359  /* cuts can only be generated if the subproblem is not independent and if it has been solved. The subproblem
2360  * solved flag is important for the user-defined subproblem solving methods
2361  */
2362  if( subproblemIsActive(benders, i) && subprobsolved[i] )
2363  {
2364  subprobresult = SCIP_DIDNOTRUN;
2365  for( j = 0; j < nbenderscuts; j++ )
2366  {
2367  SCIP_RESULT cutresult;
2368  SCIP_Longint prevaddedcuts;
2369 
2370  assert(benderscuts[j] != NULL);
2371 
2372  prevaddedcuts = SCIPbenderscutGetNFound(benderscuts[j]);
2373  cutresult = SCIP_DIDNOTRUN;
2374 
2375  /* the result is updated only if a Benders' cut is generated or one was not found. However, if a cut has
2376  * been found in a previous iteration, then the result is returned as SCIP_CONSADDED or SCIP_SEPARATED.
2377  * This result is permitted because if a constraint was added, the solution that caused the error in the cut
2378  * generation will be cutoff from the master problem.
2379  */
2380  if( (SCIPbenderscutIsLPCut(benderscuts[j]) && (solveloop == SCIP_BENDERSSOLVELOOP_CONVEX
2381  || solveloop == SCIP_BENDERSSOLVELOOP_USERCONVEX))
2382  || (!SCIPbenderscutIsLPCut(benderscuts[j]) && ((solveloop == SCIP_BENDERSSOLVELOOP_CIP && !convexsub)
2383  || solveloop == SCIP_BENDERSSOLVELOOP_USERCIP)) )
2384  SCIP_CALL( SCIPbenderscutExec(benderscuts[j], set, benders, sol, i, type, &cutresult) );
2385 
2386  addedcuts += (SCIPbenderscutGetNFound(benderscuts[j]) - prevaddedcuts);
2387 
2388  /* the result is updated only if a Benders' cut is generated */
2389  if( cutresult == SCIP_CONSADDED || cutresult == SCIP_SEPARATED )
2390  {
2391  subprobresult = cutresult;
2392 
2393  benders->ncutsfound++;
2394 
2395  /* at most a single cut is generated for each subproblem */
2396  break;
2397  }
2398  else
2399  {
2400  /* checking from lowest priority result */
2401  if( subprobresult == SCIP_DIDNOTRUN )
2402  subprobresult = cutresult;
2403  else if( subprobresult == SCIP_FEASIBLE && cutresult == SCIP_DIDNOTFIND )
2404  subprobresult = cutresult;
2405  /* if the subprobresult is SCIP_DIDNOTFIND, then it can't be updated. */
2406  }
2407  }
2408 
2409  /* the highest priority for the results is CONSADDED and SEPARATED. The solveloopresult will always be
2410  * updated if the subprobresult is either of these.
2411  */
2412  if( subprobresult == SCIP_CONSADDED || subprobresult == SCIP_SEPARATED )
2413  {
2414  solveloopresult = subprobresult;
2415  }
2416  else if( subprobresult == SCIP_FEASIBLE )
2417  {
2418  /* updating the solve loop result based upon the priority */
2419  if( solveloopresult == SCIP_DIDNOTRUN )
2420  solveloopresult = subprobresult;
2421  }
2422  else if( subprobresult == SCIP_DIDNOTFIND )
2423  {
2424  /* updating the solve loop result based upon the priority */
2425  if( solveloopresult == SCIP_DIDNOTRUN || solveloopresult == SCIP_FEASIBLE )
2426  solveloopresult = subprobresult;
2427 
2428  /* since a cut was not found, then merging could be useful to avoid this in subsequent iterations. The
2429  * candidate is labelled as a non-priority merge candidate
2430  */
2431  if( substatus[i] != SCIP_BENDERSSUBSTATUS_OPTIMAL )
2432  {
2433  (*mergecands)[(*nmergecands)] = i;
2434  (*nmergecands)++;
2435  }
2436  }
2437  else if( subprobresult == SCIP_DIDNOTRUN )
2438  {
2439  /* if the subproblem is infeasible and no cut generation methods were run, then the infeasibility will
2440  * never be resolved. As such, the subproblem will be merged into the master problem. If the subproblem
2441  * was not infeasible, then it is added as a possible merge candidate
2442  */
2443  if( substatus[i] == SCIP_BENDERSSUBSTATUS_INFEAS )
2444  {
2445  (*mergecands)[(*nmergecands)] = (*mergecands)[(*npriomergecands)];
2446  (*mergecands)[(*npriomergecands)] = i;
2447  (*npriomergecands)++;
2448  (*nmergecands)++;
2449  }
2450  else if( substatus[i] != SCIP_BENDERSSUBSTATUS_OPTIMAL )
2451  {
2452  (*mergecands)[(*nmergecands)] = i;
2453  (*nmergecands)++;
2454  }
2455  }
2456  }
2457 
2458  subproblemcount++;
2459  i++;
2460  if( i >= nsubproblems )
2461  i = 0;
2462  }
2463  }
2464 
2465  /* updating the overall result based upon the priorities */
2466  if( solveloopresult == SCIP_CONSADDED || solveloopresult == SCIP_SEPARATED )
2467  {
2468  (*result) = solveloopresult;
2469  }
2470  else if( solveloopresult == SCIP_FEASIBLE )
2471  {
2472  /* updating the solve loop result based upon the priority */
2473  if( (*result) == SCIP_DIDNOTRUN )
2474  (*result) = solveloopresult;
2475  }
2476  else if( solveloopresult == SCIP_DIDNOTFIND )
2477  {
2478  /* updating the solve loop result based upon the priority */
2479  if( (*result) == SCIP_DIDNOTRUN || (*result) == SCIP_FEASIBLE )
2480  (*result) = solveloopresult;
2481  }
2482 
2483  /* if no cuts were added, then the number of solve loops is increased */
2484  if( addedcuts == 0 && SCIPbendersGetNConvexSubproblems(benders) < SCIPbendersGetNSubproblems(benders)
2485  && checkint && !onlyconvexcheck )
2486  (*nsolveloops) = 2;
2487 
2488  return SCIP_OKAY;
2489 }
2490 
2491 /** Solves the subproblem using the current master problem solution.
2492  *
2493  * The checkint flag indicates whether integer feasibility can be assumed. If it is not assumed, i.e. checkint ==
2494  * FALSE, then only the convex relaxations of the subproblems are solved. If integer feasibility is assumed, i.e.
2495  * checkint == TRUE, then the convex relaxations and the full CIP are solved to generate Benders' cuts and check
2496  * solution feasibility.
2497  *
2498  * TODO: consider allowing the possibility to pass solution information back from the subproblems instead of the scip
2499  * instance. This would allow the use of different solvers for the subproblems, more importantly allowing the use of an
2500  * LP solver for LP subproblems.
2501  */
2503  SCIP_BENDERS* benders, /**< Benders' decomposition */
2504  SCIP_SET* set, /**< global SCIP settings */
2505  SCIP_SOL* sol, /**< primal CIP solution */
2506  SCIP_RESULT* result, /**< result of the pricing process */
2507  SCIP_Bool* infeasible, /**< is the master problem infeasible with respect to the Benders' cuts? */
2508  SCIP_Bool* auxviol, /**< set to TRUE only if the solution is feasible but the aux vars are violated */
2509  SCIP_BENDERSENFOTYPE type, /**< the type of solution being enforced */
2510  SCIP_Bool checkint /**< should the integer solution be checked by the subproblems */
2511  )
2512 {
2513  int nsubproblems;
2514  int subproblemcount;
2515  int nchecked;
2516  int nsolveloops;
2517  int nverified;
2518  int* mergecands;
2519  int npriomergecands;
2520  int nmergecands;
2521  SCIP_Bool* subprobsolved;
2522  SCIP_BENDERSSUBSTATUS* substatus;
2523  SCIP_Bool optimal;
2524  SCIP_Bool allverified;
2525  SCIP_Bool success;
2526  SCIP_Bool stopped;
2527  int i;
2528  int l;
2529 
2530  success = TRUE;
2531  stopped = FALSE;
2532 
2533  SCIPsetDebugMsg(set, "Starting Benders' decomposition subproblem solving. type %d checkint %d\n", type, checkint);
2534 
2535  /* start timing */
2536  SCIPclockStart(benders->bendersclock, set);
2537 
2538  nsubproblems = SCIPbendersGetNSubproblems(benders);
2539 
2540  (*auxviol) = FALSE;
2541  (*infeasible) = FALSE;
2542 
2543  /* It is assumed that the problem is optimal, until a subproblem is found not to be optimal. However, not all
2544  * subproblems could be checked in each iteration. As such, it is not possible to state that the problem is optimal
2545  * if not all subproblems are checked. Situations where this may occur is when a subproblem is a MIP and only the LP
2546  * is solved. Also, in a distributed computation, then it may be advantageous to only solve some subproblems before
2547  * resolving the master problem. As such, for a problem to be optimal, then (optimal && allverified) == TRUE
2548  */
2549  optimal = TRUE;
2550  nverified = 0;
2551 
2552  assert(benders != NULL);
2553  assert(result != NULL);
2554  assert(infeasible != NULL);
2555  assert(auxviol != NULL);
2556 
2557  /* if the Benders' decomposition is called from a sub-scip, it is assumed that this is an LNS heuristic. As such, the
2558  * check is not performed and the solution is assumed to be feasible
2559  */
2560  if( benders->iscopy
2561  && (!benders->lnscheck
2562  || (benders->lnsmaxdepth > -1 && SCIPgetDepth(benders->sourcescip) > benders->lnsmaxdepth)) )
2563  {
2564  (*result) = SCIP_DIDNOTRUN;
2565  return SCIP_OKAY;
2566  }
2567 
2568  /* it is not necessary to check all primal solutions by solving the Benders' decomposition subproblems.
2569  * Only the improving solutions are checked to improve efficiency of the algorithm.
2570  * If the solution is non-improving, the result FEASIBLE is returned. While this may be incorrect w.r.t to the
2571  * Benders' subproblems, this solution will never be the optimal solution. A non-improving solution may be used
2572  * within LNS primal heuristics. If this occurs, the improving solution, if found, will be checked by the solving
2573  * the Benders' decomposition subproblems.
2574  * TODO: Add a parameter to control this behaviour.
2575  */
2576  if( checkint && SCIPsetIsFeasLE(set, SCIPgetPrimalbound(set->scip)*(int)SCIPgetObjsense(set->scip),
2577  SCIPgetSolOrigObj(set->scip, sol)*(int)SCIPgetObjsense(set->scip)) )
2578  {
2579  (*result) = SCIP_DIDNOTRUN;
2580  return SCIP_OKAY;
2581  }
2582 
2583  /* if the enforcement type is SCIP_BENDERSENFOTYPE_LP and the LP is currently unbounded. This could mean that there
2584  * is no lower bound on the auxiliary variables. In this case, we try to update the lower bound for the auxiliary
2585  * variables.
2586  */
2588  && benders->updateauxvarbound )
2589  {
2590  SCIP_CALL( updateAuxiliaryVarLowerbound(benders, set, result) );
2591 
2592  /* the auxiliary variable bound will only be updated once. */
2593  benders->updateauxvarbound = FALSE;
2594  }
2595 
2596  /* setting the first subproblem to check in this round of subproblem checks */
2597  benders->firstchecked = benders->lastchecked;
2598 
2599  /* sets the stored objective function values of the subproblems to infinity */
2601 
2602  *result = SCIP_DIDNOTRUN;
2603 
2604  if( benders->benderspresubsolve != NULL )
2605  {
2606  SCIP_Bool skipsolve;
2607 
2608  skipsolve = FALSE;
2609  SCIP_CALL( benders->benderspresubsolve(set->scip, benders, sol, type, checkint, &skipsolve, result) );
2610 
2611  /* evaluate result */
2612  if( (*result) != SCIP_DIDNOTRUN
2613  && (*result) != SCIP_FEASIBLE
2614  && (*result) != SCIP_INFEASIBLE
2615  && (*result) != SCIP_CONSADDED
2616  && (*result) != SCIP_SEPARATED )
2617  {
2618  SCIPerrorMessage("the user-defined pre subproblem solving method for the Benders' decomposition <%s> returned "
2619  "invalid result <%d>\n", benders->name, *result);
2620  return SCIP_INVALIDRESULT;
2621  }
2622 
2623  /* if the solve must be skipped, then the solving loop is exited and the user defined result is returned */
2624  if( skipsolve )
2625  {
2626  SCIPsetDebugMsg(set, "skipping the subproblem solving for Benders' decomposition <%s>. "
2627  "returning result <%d>\n", benders->name, *result);
2628  return SCIP_OKAY;
2629  }
2630  }
2631 
2632  /* allocating memory for the infeasible subproblem array */
2633  SCIP_CALL( SCIPallocClearBlockMemoryArray(set->scip, &subprobsolved, nsubproblems) );
2634  SCIP_CALL( SCIPallocClearBlockMemoryArray(set->scip, &substatus, nsubproblems) );
2635  SCIP_CALL( SCIPallocClearBlockMemoryArray(set->scip, &mergecands, nsubproblems) );
2636  npriomergecands = 0;
2637  nmergecands = 0;
2638 
2639  /* by default the number of solve loops is 1. This is the case if all subproblems are LP or the user has defined a
2640  * benderssolvesub callback. If there is a subproblem that is not an LP, then 2 solve loops are performed. The first
2641  * loop is the LP solving loop, the second solves the subproblem to integer optimality.
2642  */
2643  nsolveloops = 1;
2644 
2645  for( l = 0; l < nsolveloops; l++ )
2646  {
2647  SCIP_BENDERSSOLVELOOP solveloop; /* identifies what problem type is solve in this solve loop */
2648 
2649  /* if either benderssolvesubconvex or benderssolvesub are implemented, then the user callbacks are invoked */
2650  if( benders->benderssolvesubconvex != NULL || benders->benderssolvesub != NULL )
2651  {
2652  if( l == 0 )
2654  else
2655  solveloop = SCIP_BENDERSSOLVELOOP_USERCIP;
2656  }
2657  else
2658  solveloop = (SCIP_BENDERSSOLVELOOP) l;
2659 
2660  /* solving the subproblems for this round of enforcement/checking. */
2661  SCIP_CALL( solveBendersSubproblems(benders, set, sol, type, solveloop, checkint, &nchecked, &nverified,
2662  &subprobsolved, &substatus, infeasible, &optimal, &stopped) );
2663 
2664  /* if the solving has been stopped, then the subproblem solving and cut generation must terminate */
2665  if( stopped )
2666  goto TERMINATE;
2667 
2668  /* Generating cuts for the subproblems. Cuts are only generated when the solution is from primal heuristics,
2669  * relaxations or the LP
2670  */
2671  if( type != SCIP_BENDERSENFOTYPE_PSEUDO )
2672  {
2673  SCIP_CALL( generateBendersCuts(benders, set, sol, result, type, solveloop, checkint, nchecked,
2674  subprobsolved, substatus, &mergecands, &npriomergecands, &nmergecands, &nsolveloops) );
2675  }
2676  else
2677  {
2678  /* The first solving loop solves the convex subproblems and the convex relaxations of the CIP subproblems. The
2679  * second solving loop solves the CIP subproblems. The second solving loop is only called if the integer
2680  * feasibility is being checked and if the convex subproblems and convex relaxations are not infeasible.
2681  */
2682  if( !(*infeasible) && checkint && !SCIPbendersOnlyCheckConvexRelax(benders)
2684  nsolveloops = 2;
2685  }
2686  }
2687 
2688  allverified = (nverified == nsubproblems);
2689 
2690  SCIPsetDebugMsg(set, "End Benders' decomposition subproblem solve. result %d infeasible %d auxviol %d nverified %d\n",
2691  *result, *infeasible, *auxviol, nverified);
2692 
2693 #ifdef SCIP_DEBUG
2694  if( (*result) == SCIP_CONSADDED )
2695  {
2696  SCIPsetDebugMsg(set, "Benders' decomposition: Cut added\n");
2697  }
2698 #endif
2699 
2700  /* if the number of checked pseudo solutions exceeds a set limit, then all subproblems are passed as merge
2701  * candidates. Currently, merging subproblems into the master problem is the only method for resolving numerical
2702  * troubles.
2703  *
2704  * We are only interested in the pseudo solutions that have been checked completely for integrality. This is
2705  * identified by checkint == TRUE. This means that the Benders' decomposition constraint is one of the last
2706  * constraint handlers that must resolve the infeasibility. If the Benders' decomposition framework can't resolve the
2707  * infeasibility, then this will result in an error.
2708  */
2709  if( type == SCIP_BENDERSENFOTYPE_PSEUDO && checkint )
2710  {
2711  benders->npseudosols++;
2712 
2713  if( benders->npseudosols > BENDERS_MAXPSEUDOSOLS )
2714  {
2715  /* if a priority merge candidate already exists, then no other merge candidates need to be added.*/
2716  if( npriomergecands == 0 )
2717  {
2718  /* all subproblems are added to the merge candidate list. The first active subproblem is added as a
2719  * priority merge candidate
2720  */
2721  nmergecands = 0;
2722  npriomergecands = 1;
2723  for( i = 0; i < nsubproblems; i++ )
2724  {
2725  /* only active subproblems are added to the merge candidate list */
2726  if( subproblemIsActive(benders, i) )
2727  {
2728  mergecands[nmergecands] = i;
2729  nmergecands++;
2730  }
2731  }
2732 
2733  SCIPverbMessage(set->scip, SCIP_VERBLEVEL_HIGH, NULL, " The number of checked pseudo solutions exceeds the "
2734  "limit of %d. All active subproblems are merge candidates, with subproblem %d a priority candidate.\n",
2735  BENDERS_MAXPSEUDOSOLS, mergecands[0]);
2736  }
2737  }
2738  }
2739  else
2740  benders->npseudosols = 0;
2741 
2742  /* if the result is SCIP_DIDNOTFIND, then there was a error in generating cuts in all subproblems that are not
2743  * optimal. This result does not cutoff any solution, so the Benders' decomposition algorithm will fail.
2744  * TODO: Work out a way to ensure Benders' decomposition does not terminate due to a SCIP_DIDNOTFIND result.
2745  */
2746  if( (*result) == SCIP_DIDNOTFIND )
2747  {
2748  if( type == SCIP_BENDERSENFOTYPE_PSEUDO )
2749  (*result) = SCIP_SOLVELP;
2750  else
2751  (*result) = SCIP_INFEASIBLE;
2752 
2753  SCIPerrorMessage("An error was found when generating cuts for non-optimal subproblems of Benders' "
2754  "decomposition <%s>. Consider merging the infeasible subproblems into the master problem.\n", SCIPbendersGetName(benders));
2755 
2756  /* since no other cuts are generated, then this error will result in a crash. It is possible to avoid the error,
2757  * by merging the affected subproblem into the master problem.
2758  */
2759  success = FALSE;
2760 
2761  goto POSTSOLVE;
2762  }
2763 
2764  if( type == SCIP_BENDERSENFOTYPE_PSEUDO )
2765  {
2766  if( (*infeasible) || !allverified )
2767  (*result) = SCIP_SOLVELP;
2768  else
2769  {
2770  (*result) = SCIP_FEASIBLE;
2771 
2772  /* if the subproblems are not infeasible, but they are also not optimal. This means that there is a violation
2773  * in the auxiliary variable values. In this case, a feasible result is returned with the auxviol flag set to
2774  * TRUE.
2775  */
2776  (*auxviol) = !optimal;
2777  }
2778  }
2779  else if( checkint && (type == SCIP_BENDERSENFOTYPE_CHECK || (*result) != SCIP_CONSADDED) )
2780  {
2781  /* if the subproblems are being solved as part of conscheck, then the results flag must be returned after the solving
2782  * has completed.
2783  */
2784  if( (*infeasible) || !allverified )
2785  (*result) = SCIP_INFEASIBLE;
2786  else
2787  {
2788  (*result) = SCIP_FEASIBLE;
2789 
2790  /* if the subproblems are not infeasible, but they are also not optimal. This means that there is a violation
2791  * in the auxiliary variable values. In this case, a feasible result is returned with the auxviol flag set to
2792  * TRUE.
2793  */
2794  (*auxviol) = !optimal;
2795  }
2796  }
2797 
2798 POSTSOLVE:
2799  /* calling the post-solve call back for the Benders' decomposition algorithm. This allows the user to work directly
2800  * with the solved subproblems and the master problem */
2801  if( benders->benderspostsolve != NULL )
2802  {
2803  SCIP_Bool merged;
2804 
2805  merged = FALSE;
2806 
2807  SCIP_CALL( benders->benderspostsolve(set->scip, benders, sol, type, mergecands, npriomergecands, nmergecands,
2808  checkint, (*infeasible), &merged) );
2809 
2810  if( merged )
2811  {
2812  (*result) = SCIP_CONSADDED;
2813 
2814  /* since subproblems have been merged, then constraints have been added. This could resolve the unresolved
2815  * infeasibility, so the error has been corrected.
2816  */
2817  success = TRUE;
2818  }
2819  else if( !success )
2820  {
2821  SCIPerrorMessage("An error occurred during Benders' decomposition cut generations and no merging had been "
2822  "performed. It is not possible to continue solving the problem by Benders' decomposition\n");
2823  }
2824  }
2825 
2826 TERMINATE:
2827  /* freeing the subproblems after the cuts are generated */
2828  i = benders->firstchecked;
2829  subproblemcount = 0;
2830 
2831  /* if the solving process has stopped, then all subproblems need to be freed */
2832  if( stopped )
2833  nchecked = nsubproblems;
2834 
2835  while( subproblemcount < nchecked )
2836  {
2837  SCIP_CALL( SCIPbendersFreeSubproblem(benders, set, i) );
2838 
2839  subproblemcount++;
2840  i++;
2841  if( i >= nsubproblems )
2842  i = 0;
2843  }
2844 
2845 #ifndef NDEBUG
2846  for( i = 0; i < nsubproblems; i++ )
2848  || !SCIPinProbing(SCIPbendersSubproblem(benders, i)) || !subproblemIsActive(benders, i));
2849 #endif
2850 
2851  /* increment the number of calls to the Benders' decomposition subproblem solve */
2852  benders->ncalls++;
2853 
2854  SCIPsetDebugMsg(set, "End Benders' decomposition execution method. result %d infeasible %d auxviol %d\n", *result,
2855  *infeasible, *auxviol);
2856 
2857  /* end timing */
2858  SCIPclockStop(benders->bendersclock, set);
2859 
2860  /* freeing memory */
2861  SCIPfreeBlockMemoryArray(set->scip, &mergecands, nsubproblems);
2862  SCIPfreeBlockMemoryArray(set->scip, &substatus, nsubproblems);
2863  SCIPfreeBlockMemoryArray(set->scip, &subprobsolved, nsubproblems);
2864 
2865  if( !success )
2866  return SCIP_ERROR;
2867  else
2868  return SCIP_OKAY;
2869 }
2870 
2871 /** solves the user-defined subproblem solving function */
2872 static
2874  SCIP_BENDERS* benders, /**< Benders' decomposition */
2875  SCIP_SET* set, /**< global SCIP settings */
2876  SCIP_SOL* sol, /**< primal CIP solution */
2877  int probnumber, /**< the subproblem number */
2878  SCIP_BENDERSSOLVELOOP solveloop, /**< the solve loop iteration. The first iter is for LP, the second for IP */
2879  SCIP_Bool* infeasible, /**< returns whether the current subproblem is infeasible */
2880  SCIP_Real* objective, /**< the objective function value of the subproblem */
2881  SCIP_RESULT* result /**< the result from solving the subproblem */
2882  )
2883 {
2884  assert(benders != NULL);
2885  assert(probnumber >= 0 && probnumber < benders->nsubproblems);
2886  assert(benders->benderssolvesubconvex != NULL || benders->benderssolvesub != NULL);
2887 
2888  assert(solveloop == SCIP_BENDERSSOLVELOOP_USERCONVEX || solveloop == SCIP_BENDERSSOLVELOOP_USERCIP);
2889 
2890  (*objective) = -SCIPsetInfinity(set);
2891 
2892  /* calls the user defined subproblem solving method. Only the convex relaxations are solved during the Large
2893  * Neighbourhood Benders' Search. */
2894  if( solveloop == SCIP_BENDERSSOLVELOOP_USERCONVEX )
2895  {
2896  if( benders->benderssolvesubconvex != NULL )
2897  {
2898  SCIP_CALL( benders->benderssolvesubconvex(set->scip, benders, sol, probnumber,
2899  SCIPbendersOnlyCheckConvexRelax(benders), objective, result) );
2900  }
2901  else
2902  (*result) = SCIP_DIDNOTRUN;
2903  }
2904  else if( solveloop == SCIP_BENDERSSOLVELOOP_USERCIP )
2905  {
2906  if( benders->benderssolvesub != NULL )
2907  {
2908  SCIP_CALL( benders->benderssolvesub(set->scip, benders, sol, probnumber, objective, result) );
2909  }
2910  else
2911  (*result) = SCIP_DIDNOTRUN;
2912  }
2913 
2914  /* evaluate result */
2915  if( (*result) != SCIP_DIDNOTRUN
2916  && (*result) != SCIP_FEASIBLE
2917  && (*result) != SCIP_INFEASIBLE
2918  && (*result) != SCIP_UNBOUNDED )
2919  {
2920  SCIPerrorMessage("the user-defined solving method for the Benders' decomposition <%s> returned invalid result <%d>\n",
2921  benders->name, *result);
2922  return SCIP_INVALIDRESULT;
2923  }
2924 
2925  if( (*result) == SCIP_INFEASIBLE )
2926  (*infeasible) = TRUE;
2927 
2928  if( (*result) == SCIP_FEASIBLE
2929  && (SCIPsetIsInfinity(set, -(*objective)) || SCIPsetIsInfinity(set, (*objective))) )
2930  {
2931  SCIPerrorMessage("the user-defined solving method for the Benders' decomposition <%s> returned objective value %g\n",
2932  benders->name, (*objective));
2933  return SCIP_ERROR;
2934  }
2935 
2936  /* if the result is SCIP_DIDNOTFIND, then an error is returned and SCIP will terminate. */
2937  if( (*result) == SCIP_DIDNOTFIND )
2938  return SCIP_ERROR;
2939  else
2940  return SCIP_OKAY;
2941 }
2942 
2943 /** executes the subproblem solving process */
2945  SCIP_BENDERS* benders, /**< Benders' decomposition */
2946  SCIP_SET* set, /**< global SCIP settings */
2947  SCIP_SOL* sol, /**< primal CIP solution */
2948  int probnumber, /**< the subproblem number */
2949  SCIP_BENDERSSOLVELOOP solveloop, /**< the solve loop iteration. The first iter is for LP, the second for IP */
2950  SCIP_Bool enhancement, /**< is the solve performed as part of and enhancement? */
2951  SCIP_Bool* solved, /**< flag to indicate whether the subproblem was solved */
2952  SCIP_Bool* infeasible, /**< returns whether the current subproblem is infeasible */
2953  SCIP_BENDERSENFOTYPE type /**< the enforcement type calling this function */
2954  )
2955 {
2956  SCIP* subproblem;
2957  SCIP_SOL* bestsol;
2958  SCIP_RESULT result;
2959  SCIP_Real objective;
2960 
2961  assert(benders != NULL);
2962  assert(probnumber >= 0 && probnumber < benders->nsubproblems);
2963 
2964  SCIPsetDebugMsg(set, "Benders' decomposition: solving subproblem %d\n", probnumber);
2965 
2966  result = SCIP_DIDNOTRUN;
2967  objective = SCIPsetInfinity(set);
2968 
2969  subproblem = SCIPbendersSubproblem(benders, probnumber);
2970 
2971  /* initially setting the solved flag to FALSE */
2972  (*solved) = FALSE;
2973 
2974  /* if the subproblem solve callback is implemented, then that is used instead of the default setup */
2975  if( solveloop == SCIP_BENDERSSOLVELOOP_USERCONVEX || solveloop == SCIP_BENDERSSOLVELOOP_USERCIP )
2976  {
2977  /* calls the user defined subproblem solving method. Only the convex relaxations are solved during the Large
2978  * Neighbourhood Benders' Search. */
2979  SCIP_CALL( executeUserDefinedSolvesub(benders, set, sol, probnumber, solveloop, infeasible, &objective, &result) );
2980 
2981  /* if the result is DIDNOTRUN, then the subproblem was not solved */
2982  (*solved) = (result != SCIP_DIDNOTRUN);
2983  }
2984  else
2985  {
2986  /* setting up the subproblem */
2987  if( solveloop == SCIP_BENDERSSOLVELOOP_CONVEX )
2988  {
2989  SCIP_CALL( SCIPbendersSetupSubproblem(benders, set, sol, probnumber) );
2990 
2991  /* if the limits of the master problem were hit during the setup process, then the subproblem will not have
2992  * been setup. In this case, the solving function must be exited.
2993  */
2994  if( !SCIPbendersSubproblemIsSetup(benders, probnumber) )
2995  {
2996  SCIPbendersSetSubproblemObjval(benders, probnumber, SCIPsetInfinity(set));
2997  (*solved) = FALSE;
2998  return SCIP_OKAY;
2999  }
3000  }
3001  else
3002  {
3003  SCIP_CALL( updateEventhdlrUpperbound(benders, probnumber, SCIPbendersGetAuxiliaryVarVal(benders, set, sol, probnumber)) );
3004  }
3005 
3006  /* solving the subproblem
3007  * the LP of the subproblem is solved in the first solveloop.
3008  * In the second solve loop, the MIP problem is solved */
3009  if( solveloop == SCIP_BENDERSSOLVELOOP_CONVEX || SCIPbendersSubproblemIsConvex(benders, probnumber) )
3010  {
3011  SCIP_CALL( SCIPbendersSolveSubproblemLP(set->scip, benders, probnumber, infeasible) );
3012 
3013  /* if the LP was solved without error, then the subproblem is labelled as solved */
3014  if( SCIPgetLPSolstat(subproblem) == SCIP_LPSOLSTAT_OPTIMAL
3015  || SCIPgetLPSolstat(subproblem) == SCIP_LPSOLSTAT_INFEASIBLE )
3016  (*solved) = TRUE;
3017  }
3018  else
3019  {
3020  SCIP_CALL( SCIPbendersSolveSubproblemCIP(set->scip, benders, probnumber, infeasible, type, FALSE) );
3021 
3022  /* if the generic subproblem solving methods are used, then the CIP subproblems are always solved. */
3023  (*solved) = TRUE;
3024  }
3025  }
3026 
3027  bestsol = SCIPgetBestSol(subproblem);
3028 
3029  if( !enhancement )
3030  {
3031  /* The following handles the cases when the subproblem is OPTIMAL, INFEASIBLE and UNBOUNDED.
3032  * If a subproblem is unbounded, then the auxiliary variables are set to -infinity and the unbounded flag is
3033  * returned as TRUE. No cut will be generated, but the result will be set to SCIP_FEASIBLE.
3034  */
3035  if( solveloop == SCIP_BENDERSSOLVELOOP_CONVEX )
3036  {
3037  if( SCIPgetLPSolstat(subproblem) == SCIP_LPSOLSTAT_OPTIMAL )
3038  SCIPbendersSetSubproblemObjval(benders, probnumber,
3039  SCIPgetSolOrigObj(subproblem, NULL)*(int)SCIPgetObjsense(subproblem));
3040  else if( SCIPgetLPSolstat(subproblem) == SCIP_LPSOLSTAT_INFEASIBLE )
3041  SCIPbendersSetSubproblemObjval(benders, probnumber, SCIPsetInfinity(set));
3042  else if( SCIPgetLPSolstat(subproblem) == SCIP_LPSOLSTAT_UNBOUNDEDRAY )
3043  {
3044  SCIPerrorMessage("The LP of Benders' decomposition subproblem %d is unbounded. This should not happen.\n",
3045  probnumber);
3046  SCIPABORT();
3047  }
3048  else if( SCIPgetLPSolstat(subproblem) == SCIP_LPSOLSTAT_ERROR
3049  || SCIPgetLPSolstat(subproblem) == SCIP_LPSOLSTAT_NOTSOLVED
3050  || SCIPgetLPSolstat(subproblem) == SCIP_LPSOLSTAT_TIMELIMIT )
3051  {
3052  SCIPverbMessage(set->scip, SCIP_VERBLEVEL_FULL, NULL, " Benders' decomposition: Error solving LP "
3053  "relaxation of subproblem %d. No cut will be generated for this subproblem.\n", probnumber);
3054  SCIPbendersSetSubproblemObjval(benders, probnumber, SCIPsetInfinity(set));
3055  }
3056  else
3057  {
3058  SCIPerrorMessage("Invalid status returned from solving the LP of Benders' decomposition subproblem %d. LP status: %d\n",
3059  probnumber, SCIPgetLPSolstat(subproblem));
3060  SCIPABORT();
3061  }
3062  }
3063  else if( solveloop == SCIP_BENDERSSOLVELOOP_CIP )
3064  {
3065  /* TODO: Consider whether other solutions status should be handled */
3066  if( SCIPgetStatus(subproblem) == SCIP_STATUS_OPTIMAL )
3067  SCIPbendersSetSubproblemObjval(benders, probnumber,
3068  SCIPgetSolOrigObj(subproblem, bestsol)*(int)SCIPgetObjsense(subproblem));
3069  else if( SCIPgetStatus(subproblem) == SCIP_STATUS_INFEASIBLE )
3070  SCIPbendersSetSubproblemObjval(benders, probnumber, SCIPsetInfinity(set));
3071  else if( SCIPgetStatus(subproblem) == SCIP_STATUS_USERINTERRUPT || SCIPgetStatus(subproblem) == SCIP_STATUS_BESTSOLLIMIT )
3072  SCIPbendersSetSubproblemObjval(benders, probnumber,
3073  SCIPgetSolOrigObj(subproblem, bestsol)*(int)SCIPgetObjsense(subproblem));
3074  else if( SCIPgetStatus(subproblem) == SCIP_STATUS_MEMLIMIT
3075  || SCIPgetStatus(subproblem) == SCIP_STATUS_TIMELIMIT )
3076  {
3077  SCIPverbMessage(set->scip, SCIP_VERBLEVEL_FULL, NULL, " Benders' decomposition: Error solving CIP "
3078  "of subproblem %d. No cut will be generated for this subproblem.\n", probnumber);
3079  SCIPbendersSetSubproblemObjval(benders, probnumber, SCIPsetInfinity(set));
3080  }
3081  else if( SCIPgetStatus(subproblem) == SCIP_STATUS_UNBOUNDED )
3082  {
3083  SCIPerrorMessage("The Benders' decomposition subproblem %d is unbounded. This should not happen.\n",
3084  probnumber);
3085  SCIPABORT();
3086  }
3087  else
3088  {
3089  SCIPerrorMessage("Invalid status returned from solving Benders' decomposition subproblem %d. Solution status: %d\n",
3090  probnumber, SCIPgetStatus(subproblem));
3091  SCIPABORT();
3092  }
3093  }
3094  else
3095  {
3096  assert(solveloop == SCIP_BENDERSSOLVELOOP_USERCONVEX || solveloop == SCIP_BENDERSSOLVELOOP_USERCIP);
3097  if( result == SCIP_FEASIBLE )
3098  SCIPbendersSetSubproblemObjval(benders, probnumber, objective);
3099  else if( result == SCIP_INFEASIBLE )
3100  SCIPbendersSetSubproblemObjval(benders, probnumber, SCIPsetInfinity(set));
3101  else if( result == SCIP_UNBOUNDED )
3102  {
3103  SCIPerrorMessage("The Benders' decomposition subproblem %d is unbounded. This should not happen.\n",
3104  probnumber);
3105  SCIPABORT();
3106  }
3107  else if( result != SCIP_DIDNOTRUN )
3108  {
3109  SCIPerrorMessage("Invalid result <%d> from user-defined subproblem solving method. This should not happen.\n",
3110  result);
3111  }
3112  }
3113  }
3114 
3115  return SCIP_OKAY;
3116 }
3117 
3118 /** sets up the subproblem using the solution to the master problem */
3120  SCIP_BENDERS* benders, /**< Benders' decomposition */
3121  SCIP_SET* set, /**< global SCIP settings */
3122  SCIP_SOL* sol, /**< primal CIP solution */
3123  int probnumber /**< the subproblem number */
3124  )
3125 {
3126  SCIP* subproblem;
3127  SCIP_VAR** vars;
3128  SCIP_VAR* mastervar;
3129  SCIP_Real solval;
3130  int nvars;
3131  int i;
3132 
3133  assert(benders != NULL);
3134  assert(set != NULL);
3135  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
3136 
3137  /* changing all of the master problem variable to continuous. */
3138  SCIP_CALL( SCIPbendersChgMastervarsToCont(benders, set, probnumber) );
3139 
3140  subproblem = SCIPbendersSubproblem(benders, probnumber);
3141 
3142  /* if the Benders' decomposition subproblem is an LP, then probing mode must be started.
3143  * If the subproblem is a MIP, the problem must be initialised, put into SCIP_STAGE_SOLVING to be able to change the
3144  * variable bounds. The probing mode is entered once the variable bounds are set.
3145  * In the MIP case, the transformed problem is freed after each subproblem solve round. */
3146  if( SCIPbendersSubproblemIsConvex(benders, probnumber) )
3147  {
3148  SCIP_CALL( SCIPstartProbing(subproblem) );
3149  }
3150  else
3151  {
3152  SCIP_Bool success;
3153 
3154  SCIP_CALL( initialiseSubproblem(benders, set, probnumber, &success) );
3155 
3156  if( !success )
3157  {
3158  /* set the flag to indicate that the subproblems have been set up */
3159  SCIPbendersSetSubproblemIsSetup(benders, probnumber, FALSE);
3160 
3161  return SCIP_OKAY;
3162  }
3163  }
3164 
3165  vars = SCIPgetVars(subproblem);
3166  nvars = SCIPgetNVars(subproblem);
3167 
3168  /* looping over all variables in the subproblem to find those corresponding to the master problem variables. */
3169  /* TODO: It should be possible to store the pointers to the master variables to speed up the subproblem setup */
3170  for( i = 0; i < nvars; i++ )
3171  {
3172  SCIP_CALL( SCIPbendersGetVar(benders, set, vars[i], &mastervar, -1) );
3173 
3174  if( mastervar != NULL )
3175  {
3176  /* It is possible due to numerics that the solution value exceeds the upper or lower bounds. When this
3177  * happens, it causes an error in the LP solver as a result of inconsistent bounds. So the following statements
3178  * are used to ensure that the bounds are not exceeded when applying the fixings for the Benders'
3179  * decomposition subproblems
3180  */
3181  solval = SCIPgetSolVal(set->scip, sol, mastervar);
3182  if( solval > SCIPvarGetUbLocal(vars[i]) )
3183  solval = SCIPvarGetUbLocal(vars[i]);
3184  else if( solval < SCIPvarGetLbLocal(vars[i]) )
3185  solval = SCIPvarGetLbLocal(vars[i]);
3186 
3187  /* fixing the variable in the subproblem */
3188  if( !SCIPisEQ(subproblem, SCIPvarGetLbLocal(vars[i]), SCIPvarGetUbLocal(vars[i])) )
3189  {
3190  if( SCIPisGT(subproblem, solval, SCIPvarGetLbLocal(vars[i])) )
3191  {
3192  SCIP_CALL( SCIPchgVarLb(subproblem, vars[i], solval) );
3193  }
3194  if( SCIPisLT(subproblem, solval, SCIPvarGetUbLocal(vars[i])) )
3195  {
3196  SCIP_CALL( SCIPchgVarUb(subproblem, vars[i], solval) );
3197  }
3198  }
3199 
3200  assert(SCIPisEQ(subproblem, SCIPvarGetLbLocal(vars[i]), SCIPvarGetUbLocal(vars[i])));
3201  }
3202  }
3203 
3204  /* if the subproblem is a MIP, the probing mode is entered after setting up the subproblem */
3205  if( !SCIPbendersSubproblemIsConvex(benders, probnumber) )
3206  {
3207  SCIP_CALL( SCIPstartProbing(subproblem) );
3208  }
3209 
3210  /* set the flag to indicate that the subproblems have been set up */
3211  SCIPbendersSetSubproblemIsSetup(benders, probnumber, TRUE);
3212 
3213  return SCIP_OKAY;
3214 }
3215 
3216 /** Solve a Benders' decomposition subproblems. This will either call the user defined method or the generic solving
3217  * methods. If the generic method is called, then the subproblem must be set up before calling this method. */
3219  SCIP_BENDERS* benders, /**< Benders' decomposition */
3220  SCIP_SET* set, /**< global SCIP settings */
3221  SCIP_SOL* sol, /**< primal CIP solution, can be NULL */
3222  int probnumber, /**< the subproblem number */
3223  SCIP_Bool* infeasible, /**< returns whether the current subproblem is infeasible */
3224  SCIP_BENDERSENFOTYPE type, /**< the enforcement type calling this function */
3225  SCIP_Bool solvecip, /**< directly solve the CIP subproblem */
3226  SCIP_Real* objective /**< the objective function value of the subproblem, can be NULL */
3227  )
3228 {
3229  assert(benders != NULL);
3230  assert(set != NULL);
3231  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
3232 
3233  /* the subproblem must be set up before this function is called. */
3234  if( !SCIPbendersSubproblemIsSetup(benders, probnumber) && !SCIPbendersSubproblemIsIndependent(benders, probnumber) )
3235  {
3236  SCIPerrorMessage("Benders' decomposition subproblem %d must be set up before calling SCIPbendersSolveSubproblem(). Call SCIPsetupSubproblem() first.\n", probnumber);
3237  return SCIP_ERROR;
3238  }
3239 
3240  /* if the subproblem solve callback is implemented, then that is used instead of the default setup */
3241  if( benders->benderssolvesubconvex != NULL || benders->benderssolvesub != NULL)
3242  {
3243  SCIP_BENDERSSOLVELOOP solveloop;
3244  SCIP_RESULT result;
3245  SCIP_Real subobj;
3246 
3247  if( solvecip )
3248  solveloop = SCIP_BENDERSSOLVELOOP_USERCIP;
3249  else
3251 
3252  SCIP_CALL( executeUserDefinedSolvesub(benders, set, sol, probnumber, solveloop, infeasible, &subobj, &result) );
3253 
3254  if( objective != NULL )
3255  (*objective) = subobj;
3256  }
3257  else
3258  {
3259  SCIP* subproblem;
3260 
3261  subproblem = SCIPbendersSubproblem(benders, probnumber);
3262 
3263  /* solving the subproblem */
3264  if( solvecip && !SCIPbendersSubproblemIsConvex(benders, probnumber) )
3265  {
3266  SCIP_CALL( SCIPbendersSolveSubproblemCIP(set->scip, benders, probnumber, infeasible, type, solvecip) );
3267 
3268  if( objective != NULL )
3269  (*objective) = SCIPgetSolOrigObj(subproblem, SCIPgetBestSol(subproblem))*(int)SCIPgetObjsense(subproblem);
3270  }
3271  else
3272  {
3273  SCIP_Bool success;
3274 
3275  /* if the subproblem is an LP, then it should have been initialised and in SCIP_STAGE_SOLVING.
3276  * in this case, the subproblem only needs to be put into probing mode. */
3277  if( SCIPbendersSubproblemIsConvex(benders, probnumber) )
3278  {
3279  /* if the subproblem is not in probing mode, then it must be put into that mode for the LP solve. */
3280  if( !SCIPinProbing(subproblem) )
3281  {
3282  SCIP_CALL( SCIPstartProbing(subproblem) );
3283  }
3284 
3285  success = TRUE;
3286  }
3287  else
3288  {
3289  SCIP_CALL( initialiseSubproblem(benders, set, probnumber, &success) );
3290  }
3291 
3292  /* if setting up the subproblem was successful */
3293  if( success )
3294  {
3295  SCIP_CALL( SCIPbendersSolveSubproblemLP(set->scip, benders, probnumber, infeasible) );
3296 
3297  if( objective != NULL )
3298  (*objective) = SCIPgetSolOrigObj(subproblem, NULL)*(int)SCIPgetObjsense(subproblem);
3299  }
3300  else
3301  {
3302  if( objective != NULL )
3303  (*objective) = SCIPinfinity(subproblem);
3304  }
3305  }
3306  }
3307 
3308  return SCIP_OKAY;
3309 }
3310 
3311 /** copies the time and memory limit from the master problem to the subproblem */
3312 static
3314  SCIP* scip, /**< the SCIP data structure */
3315  SCIP* subproblem /**< the Benders' decomposition subproblem */
3316  )
3317 {
3318  SCIP_Real mastertimelimit;
3319  SCIP_Real subtimelimit;
3320  SCIP_Real maxsubtimelimit;
3321  SCIP_Real mastermemorylimit;
3322  SCIP_Real submemorylimit;
3323  SCIP_Real maxsubmemorylimit;
3324 
3325  assert(scip != NULL);
3326 
3327  /* setting the time limit for the Benders' decomposition subproblems. It is set to 102% of the remaining time. */
3328  SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &mastertimelimit) );
3329  maxsubtimelimit = SCIPparamGetRealMax(SCIPgetParam(subproblem, "limits/time"));
3330  subtimelimit = (mastertimelimit - SCIPgetSolvingTime(scip)) * 1.02;
3331  subtimelimit = MIN(subtimelimit, maxsubtimelimit);
3332  SCIP_CALL( SCIPsetRealParam(subproblem, "limits/time", MAX(0.0, subtimelimit)) );
3333 
3334  /* setting the memory limit for the Benders' decomposition subproblems. */
3335  SCIP_CALL( SCIPgetRealParam(scip, "limits/memory", &mastermemorylimit) );
3336  maxsubmemorylimit = SCIPparamGetRealMax(SCIPgetParam(subproblem, "limits/memory"));
3337  submemorylimit = mastermemorylimit - (SCIPgetMemUsed(scip) + SCIPgetMemExternEstim(scip))/1048576.0;
3338  submemorylimit = MIN(submemorylimit, maxsubmemorylimit);
3339  SCIP_CALL( SCIPsetRealParam(subproblem, "limits/memory", MAX(0.0, submemorylimit)) );
3340 
3341  return SCIP_OKAY;
3342 }
3343 
3344 /** stores the original parameters from the subproblem */
3345 static
3347  SCIP* subproblem, /**< the SCIP data structure */
3348  SCIP_SUBPROBPARAMS* origparams /**< the original subproblem parameters */
3349  )
3350 {
3351  assert(subproblem != NULL);
3352  assert(origparams != NULL);
3353 
3354  SCIP_CALL( SCIPgetRealParam(subproblem, "limits/memory", &origparams->limits_memory) );
3355  SCIP_CALL( SCIPgetRealParam(subproblem, "limits/time", &origparams->limits_time) );
3356  SCIP_CALL( SCIPgetBoolParam(subproblem, "conflict/enable", &origparams->conflict_enable) );
3357  SCIP_CALL( SCIPgetIntParam(subproblem, "lp/disablecutoff", &origparams->lp_disablecutoff) );
3358  SCIP_CALL( SCIPgetIntParam(subproblem, "lp/scaling", &origparams->lp_scaling) );
3359  SCIP_CALL( SCIPgetCharParam(subproblem, "lp/initalgorithm", &origparams->lp_initalg) );
3360  SCIP_CALL( SCIPgetCharParam(subproblem, "lp/resolvealgorithm", &origparams->lp_resolvealg) );
3361  SCIP_CALL( SCIPgetBoolParam(subproblem, "lp/alwaysgetduals", &origparams->lp_alwaysgetduals) );
3362  SCIP_CALL( SCIPgetBoolParam(subproblem, "misc/scaleobj", &origparams->misc_scaleobj) );
3363  SCIP_CALL( SCIPgetBoolParam(subproblem, "misc/catchctrlc", &origparams->misc_catchctrlc) );
3364  SCIP_CALL( SCIPgetIntParam(subproblem, "propagating/maxrounds", &origparams->prop_maxrounds) );
3365  SCIP_CALL( SCIPgetIntParam(subproblem, "propagating/maxroundsroot", &origparams->prop_maxroundsroot) );
3366  SCIP_CALL( SCIPgetIntParam(subproblem, "constraints/linear/propfreq", &origparams->cons_linear_propfreq) );
3367 
3368  return SCIP_OKAY;
3369 }
3370 
3371 /** sets the parameters for the subproblem */
3372 static
3374  SCIP* scip, /**< the SCIP data structure */
3375  SCIP* subproblem /**< the subproblem SCIP instance */
3376  )
3377 {
3378  assert(scip != NULL);
3379  assert(subproblem != NULL);
3380 
3381  /* copying memory and time limits */
3382  SCIP_CALL( copyMemoryAndTimeLimits(scip, subproblem) );
3383 
3384  /* Do we have to disable presolving? If yes, we have to store all presolving parameters. */
3386 
3387  /* Disabling heuristics so that the problem is not trivially solved */
3389 
3390  /* store parameters that are changed for the generation of the subproblem cuts */
3391  SCIP_CALL( SCIPsetParam(subproblem, "conflict/enable", FALSE) );
3392 
3393  SCIP_CALL( SCIPsetIntParam(subproblem, "lp/disablecutoff", 1) );
3394  SCIP_CALL( SCIPsetIntParam(subproblem, "lp/scaling", 0) );
3395 
3396  SCIP_CALL( SCIPsetCharParam(subproblem, "lp/initalgorithm", 'd') );
3397  SCIP_CALL( SCIPsetCharParam(subproblem, "lp/resolvealgorithm", 'd') );
3398 
3399  SCIP_CALL( SCIPsetBoolParam(subproblem, "lp/alwaysgetduals", TRUE) );
3400  SCIP_CALL( SCIPsetBoolParam(subproblem, "misc/scaleobj", FALSE) );
3401 
3402  /* do not abort subproblem on CTRL-C */
3403  SCIP_CALL( SCIPsetBoolParam(subproblem, "misc/catchctrlc", FALSE) );
3404 
3405  SCIP_CALL( SCIPsetIntParam(subproblem, "display/verblevel", (int)SCIP_VERBLEVEL_NONE) );
3406 
3407  SCIP_CALL( SCIPsetIntParam(subproblem, "propagating/maxrounds", 0) );
3408  SCIP_CALL( SCIPsetIntParam(subproblem, "propagating/maxroundsroot", 0) );
3409 
3410  SCIP_CALL( SCIPsetIntParam(subproblem, "constraints/linear/propfreq", -1) );
3411 
3412  return SCIP_OKAY;
3413 }
3414 
3415 /** resets the original parameters from the subproblem */
3416 static
3418  SCIP* subproblem, /**< the SCIP data structure */
3419  SCIP_SUBPROBPARAMS* origparams /**< the original subproblem parameters */
3420  )
3421 {
3422  assert(subproblem != NULL);
3423  assert(origparams != NULL);
3424 
3425  SCIP_CALL( SCIPsetRealParam(subproblem, "limits/memory", origparams->limits_memory) );
3426  SCIP_CALL( SCIPsetRealParam(subproblem, "limits/time", origparams->limits_time) );
3427  SCIP_CALL( SCIPsetBoolParam(subproblem, "conflict/enable", origparams->conflict_enable) );
3428  SCIP_CALL( SCIPsetIntParam(subproblem, "lp/disablecutoff", origparams->lp_disablecutoff) );
3429  SCIP_CALL( SCIPsetIntParam(subproblem, "lp/scaling", origparams->lp_scaling) );
3430  SCIP_CALL( SCIPsetCharParam(subproblem, "lp/initalgorithm", origparams->lp_initalg) );
3431  SCIP_CALL( SCIPsetCharParam(subproblem, "lp/resolvealgorithm", origparams->lp_resolvealg) );
3432  SCIP_CALL( SCIPsetBoolParam(subproblem, "lp/alwaysgetduals", origparams->lp_alwaysgetduals) );
3433  SCIP_CALL( SCIPsetBoolParam(subproblem, "misc/scaleobj", origparams->misc_scaleobj) );
3434  SCIP_CALL( SCIPsetBoolParam(subproblem, "misc/catchctrlc", origparams->misc_catchctrlc) );
3435  SCIP_CALL( SCIPsetIntParam(subproblem, "propagating/maxrounds", origparams->prop_maxrounds) );
3436  SCIP_CALL( SCIPsetIntParam(subproblem, "propagating/maxroundsroot", origparams->prop_maxroundsroot) );
3437  SCIP_CALL( SCIPsetIntParam(subproblem, "constraints/linear/propfreq", origparams->cons_linear_propfreq) );
3438 
3439  return SCIP_OKAY;
3440 }
3441 
3442 /** solves the LP of the Benders' decomposition subproblem
3443  *
3444  * This requires that the subproblem is in probing mode.
3445  */
3447  SCIP* scip, /**< the SCIP data structure */
3448  SCIP_BENDERS* benders, /**< the Benders' decomposition data structure */
3449  int probnumber, /**< the subproblem number */
3450  SCIP_Bool* infeasible /**< a flag to indicate whether all subproblems are feasible */
3451  )
3452 {
3453  SCIP* subproblem;
3454  SCIP_SUBPROBPARAMS* origparams;
3455  SCIP_Bool lperror;
3456  SCIP_Bool cutoff;
3457 
3458  assert(benders != NULL);
3459  assert(infeasible != NULL);
3460  assert(SCIPbendersSubproblemIsSetup(benders, probnumber));
3461 
3462  (*infeasible) = FALSE;
3463 
3464  /* TODO: This should be solved just as an LP, so as a MIP. There is too much overhead with the MIP.
3465  * Need to change status check for checking the LP. */
3466  subproblem = SCIPbendersSubproblem(benders, probnumber);
3467 
3468  assert(SCIPisLPConstructed(subproblem));
3469  assert(SCIPinProbing(subproblem));
3470 
3471  /* allocating memory for the parameter storage */
3472  SCIP_CALL( SCIPallocBlockMemory(subproblem, &origparams) );
3473 
3474  /* store the original parameters of the subproblem */
3475  SCIP_CALL( storeOrigSubproblemParams(subproblem, origparams) );
3476 
3477  /* setting the subproblem parameters */
3478  SCIP_CALL( setSubproblemParams(scip, subproblem) );
3479 
3480  SCIP_CALL( SCIPsolveProbingLP(subproblem, -1, &lperror, &cutoff) );
3481 
3482  if( SCIPgetLPSolstat(subproblem) == SCIP_LPSOLSTAT_INFEASIBLE )
3483  (*infeasible) = TRUE;
3484  else if( SCIPgetLPSolstat(subproblem) != SCIP_LPSOLSTAT_OPTIMAL
3486  && SCIPgetLPSolstat(subproblem) != SCIP_LPSOLSTAT_NOTSOLVED
3487  && SCIPgetLPSolstat(subproblem) != SCIP_LPSOLSTAT_ERROR
3488  && SCIPgetLPSolstat(subproblem) != SCIP_LPSOLSTAT_TIMELIMIT )
3489  {
3490  SCIPerrorMessage("Invalid status: %d. Solving the LP relaxation of Benders' decomposition subproblem %d.\n",
3491  SCIPgetLPSolstat(subproblem), probnumber);
3492  SCIPABORT();
3493  }
3494 
3495  /* resetting the subproblem parameters */
3496  SCIP_CALL( resetOrigSubproblemParams(subproblem, origparams) );
3497 
3498  /* freeing the parameter storage */
3499  SCIPfreeBlockMemory(subproblem, &origparams);
3500 
3501  return SCIP_OKAY;
3502 }
3503 
3504 /** solves the Benders' decomposition subproblem */
3506  SCIP* scip, /**< the SCIP data structure */
3507  SCIP_BENDERS* benders, /**< the Benders' decomposition data structure */
3508  int probnumber, /**< the subproblem number */
3509  SCIP_Bool* infeasible, /**< returns whether the current subproblem is infeasible */
3510  SCIP_BENDERSENFOTYPE type, /**< the enforcement type calling this function */
3511  SCIP_Bool solvecip /**< directly solve the CIP subproblem */
3512  )
3513 {
3514  SCIP* subproblem;
3515  SCIP_SUBPROBPARAMS* origparams;
3516 
3517  assert(benders != NULL);
3518  assert(infeasible != NULL);
3519 
3520  (*infeasible) = FALSE;
3521 
3522  subproblem = SCIPbendersSubproblem(benders, probnumber);
3523 
3524  /* allocating memory for the parameter storage */
3525  SCIP_CALL( SCIPallocBlockMemory(subproblem, &origparams) );
3526 
3527  /* store the original parameters of the subproblem */
3528  SCIP_CALL( storeOrigSubproblemParams(subproblem, origparams) );
3529 
3530  /* If the solve has been stopped for the subproblem, then we need to restart it to complete the solve. The subproblem
3531  * is stopped when it is a MIP so that LP cuts and IP cuts can be generated. */
3532  if( SCIPgetStage(subproblem) == SCIP_STAGE_SOLVING )
3533  {
3534  /* the subproblem should be in probing mode. Otherwise, the event handler did not work correctly */
3535  assert( SCIPinProbing(subproblem) );
3536 
3537  /* the probing mode needs to be stopped so that the MIP can be solved */
3538  SCIP_CALL( SCIPendProbing(subproblem) );
3539 
3540  /* the problem was interrupted in the event handler, so SCIP needs to be informed that the problem is to be restarted */
3541  SCIP_CALL( SCIPrestartSolve(subproblem) );
3542 
3543  /* if the solve type is for CHECK, then the FEASIBILITY emphasis setting is used. */
3544  if( type == SCIP_BENDERSENFOTYPE_CHECK )
3545  {
3547 
3548  /* the number of solution improvements is limited to try and prove feasibility quickly */
3549  /* NOTE: This should be a parameter */
3550  /* SCIP_CALL( SCIPsetIntParam(subproblem, "limits/bestsol", 5) ); */
3551  }
3552  }
3553  else if( solvecip )
3554  {
3555  /* if the MIP will be solved directly, then the probing mode needs to be skipped.
3556  * This is achieved by setting the solvecip flag in the event handler data to TRUE
3557  */
3558  SCIP_EVENTHDLR* eventhdlr;
3559  SCIP_EVENTHDLRDATA* eventhdlrdata;
3560 
3561  eventhdlr = SCIPfindEventhdlr(subproblem, MIPNODEFOCUS_EVENTHDLR_NAME);
3562  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
3563 
3564  eventhdlrdata->solvecip = TRUE;
3565  }
3566  else
3567  {
3568  /* if the problem is not in probing mode, then we need to solve the LP. That requires all methods that will
3569  * modify the structure of the problem need to be deactivated */
3570 
3571  /* setting the subproblem parameters */
3572  SCIP_CALL( setSubproblemParams(scip, subproblem) );
3573 
3574 #ifdef SCIP_MOREDEBUG
3575  SCIP_CALL( SCIPsetBoolParam(subproblem, "display/lpinfo", TRUE) );
3576 #endif
3577  }
3578 
3579 #ifdef SCIP_MOREDEBUG
3580  SCIP_CALL( SCIPsetIntParam(subproblem, "display/verblevel", (int)SCIP_VERBLEVEL_FULL) );
3581 #endif
3582 
3583  SCIP_CALL( SCIPsolve(subproblem) );
3584 
3585  if( SCIPgetStatus(subproblem) == SCIP_STATUS_INFEASIBLE )
3586  (*infeasible) = TRUE;
3587  else if( SCIPgetStatus(subproblem) != SCIP_STATUS_OPTIMAL && SCIPgetStatus(subproblem) != SCIP_STATUS_UNBOUNDED
3589  && SCIPgetStatus(subproblem) != SCIP_STATUS_TIMELIMIT && SCIPgetStatus(subproblem) != SCIP_STATUS_MEMLIMIT )
3590  {
3591  SCIPerrorMessage("Invalid status: %d. Solving the CIP of Benders' decomposition subproblem %d.\n",
3592  SCIPgetStatus(subproblem), probnumber);
3593  SCIPABORT();
3594  }
3595 
3596  /* resetting the subproblem parameters */
3597  SCIP_CALL( resetOrigSubproblemParams(subproblem, origparams) );
3598 
3599  /* freeing the parameter storage */
3600  SCIPfreeBlockMemory(subproblem, &origparams);
3601 
3602  return SCIP_OKAY;
3603 }
3604 
3605 /** frees the subproblems */
3607  SCIP_BENDERS* benders, /**< Benders' decomposition */
3608  SCIP_SET* set, /**< global SCIP settings */
3609  int probnumber /**< the subproblem number */
3610  )
3611 {
3612  assert(benders != NULL);
3613  assert(benders->bendersfreesub != NULL
3614  || (benders->bendersfreesub == NULL && benders->benderssolvesubconvex == NULL && benders->benderssolvesub == NULL));
3615  assert(probnumber >= 0 && probnumber < benders->nsubproblems);
3616 
3617  if( benders->bendersfreesub != NULL )
3618  {
3619  SCIP_CALL( benders->bendersfreesub(set->scip, benders, probnumber) );
3620  }
3621  else
3622  {
3623  /* the subproblem is only freed if it is not independent */
3624  if( subproblemIsActive(benders, probnumber) )
3625  {
3626  SCIP* subproblem = SCIPbendersSubproblem(benders, probnumber);
3627 
3628  if( SCIPbendersSubproblemIsConvex(benders, probnumber) )
3629  {
3630  /* ending probing mode to reset the current node. The probing mode will be restarted at the next solve */
3631  if( SCIPinProbing(subproblem) )
3632  {
3633  SCIP_CALL( SCIPendProbing(subproblem) );
3634  }
3635  }
3636  else
3637  {
3638  /* if the subproblems were solved as part of an enforcement stage, then they will still be in probing mode. The
3639  * probing mode must first be finished and then the problem can be freed */
3640  if( SCIPgetStage(subproblem) >= SCIP_STAGE_TRANSFORMED && SCIPinProbing(subproblem) )
3641  {
3642  SCIP_CALL( SCIPendProbing(subproblem) );
3643  }
3644 
3645  SCIP_CALL( SCIPfreeTransform(subproblem) );
3646  }
3647  }
3648  }
3649 
3650  /* setting the setup flag for the subproblem to FALSE */
3651  SCIPbendersSetSubproblemIsSetup(benders, probnumber, FALSE);
3652  return SCIP_OKAY;
3653 }
3654 
3655 /** compares the subproblem objective value with the auxiliary variable value for optimality */
3657  SCIP_BENDERS* benders, /**< the benders' decomposition structure */
3658  SCIP_SET* set, /**< global SCIP settings */
3659  SCIP_SOL* sol, /**< primal CIP solution */
3660  int probnumber, /**< the subproblem number */
3661  SCIP_Bool* optimal /**< flag to indicate whether the current subproblem is optimal for the master */
3662  )
3663 {
3664  SCIP_Real auxiliaryvarval;
3665  SCIP_Real soltol;
3666 
3667  assert(benders != NULL);
3668  assert(set != NULL);
3669  assert(probnumber >= 0 && probnumber < benders->nsubproblems);
3670 
3671  (*optimal) = FALSE;
3672 
3673  auxiliaryvarval = SCIPbendersGetAuxiliaryVarVal(benders, set, sol, probnumber);
3674 
3675  SCIP_CALL( SCIPsetGetRealParam(set, "benders/solutiontol", &soltol) );
3676 
3677  SCIPsetDebugMsg(set, "Subproblem %d - Auxiliary Variable: %g Subproblem Objective: %g Reldiff: %g Soltol: %g\n",
3678  probnumber, auxiliaryvarval, SCIPbendersGetSubproblemObjval(benders, probnumber),
3679  SCIPrelDiff(SCIPbendersGetSubproblemObjval(benders, probnumber), auxiliaryvarval), soltol);
3680 
3681  if( SCIPrelDiff(SCIPbendersGetSubproblemObjval(benders, probnumber), auxiliaryvarval) < soltol )
3682  (*optimal) = TRUE;
3683 
3684  return SCIP_OKAY;
3685 }
3686 
3687 /** returns the value of the auxiliary variable value in a master problem solution */
3689  SCIP_BENDERS* benders, /**< the benders' decomposition structure */
3690  SCIP_SET* set, /**< global SCIP settings */
3691  SCIP_SOL* sol, /**< primal CIP solution */
3692  int probnumber /**< the subproblem number */
3693  )
3694 {
3695  SCIP_VAR* auxiliaryvar;
3696 
3697  assert(benders != NULL);
3698  assert(set != NULL);
3699 
3700  auxiliaryvar = SCIPbendersGetAuxiliaryVar(benders, probnumber);
3701  assert(auxiliaryvar != NULL);
3702 
3703  return SCIPgetSolVal(set->scip, sol, auxiliaryvar);
3704 }
3705 
3706 /** Solves an independent subproblem to identify its lower bound. The lower bound is then used to update the bound on
3707  * the auxiliary variable.
3708  */
3710  SCIP_BENDERS* benders, /**< Benders' decomposition */
3711  SCIP_SET* set, /**< global SCIP settings */
3712  int probnumber, /**< the subproblem to be evaluated */
3713  SCIP_Real* lowerbound, /**< the lowerbound for the subproblem */
3714  SCIP_Bool* infeasible /**< was the subproblem found to be infeasible? */
3715  )
3716 {
3717  SCIP* subproblem;
3718  SCIP_Real memorylimit;
3719  SCIP_Real timelimit;
3720  SCIP_Longint totalnodes;
3721  int disablecutoff;
3722  int verblevel;
3723  SCIP_Bool lperror;
3724  SCIP_Bool cutoff;
3725 
3726  assert(benders != NULL);
3727  assert(set != NULL);
3728 
3729  if( benders->benderssolvesub != NULL || benders->benderssolvesubconvex != NULL )
3730  {
3731  (*lowerbound) = SCIPvarGetLbGlobal(SCIPbendersGetAuxiliaryVar(benders, probnumber));
3732  (*infeasible) = FALSE;
3733 
3734  SCIPinfoMessage(set->scip, NULL, "Benders' decomposition: a bendersSolvesub or bendersSolvesubconvex has been "
3735  "implemented. SCIPbendersComputeSubproblemLowerbound can not be executed.\n");
3736  SCIPinfoMessage(set->scip, NULL, "Set the auxiliary variable lower bound by calling "
3737  "SCIPbendersUpdateSubproblemLowerbound in bendersCreatesub. The auxiliary variable %d will remain as %g\n",
3738  probnumber, (*lowerbound));
3739 
3740  return SCIP_OKAY;
3741  }
3742  else
3743  {
3744  SCIPverbMessage(set->scip, SCIP_VERBLEVEL_FULL, NULL, "Benders' decomposition: Computing a lower bound for"
3745  " subproblem %d\n", probnumber);
3746  }
3747 
3748  /* getting the subproblem to evaluate */
3749  subproblem = SCIPbendersSubproblem(benders, probnumber);
3750 
3751  (*lowerbound) = -SCIPinfinity(subproblem);
3752  (*infeasible) = FALSE;
3753 
3754  SCIP_CALL( SCIPgetIntParam(subproblem, "display/verblevel", &verblevel) );
3755  SCIP_CALL( SCIPsetIntParam(subproblem, "display/verblevel", (int)SCIP_VERBLEVEL_NONE) );
3756 #ifdef SCIP_MOREDEBUG
3757  SCIP_CALL( SCIPsetIntParam(subproblem, "display/verblevel", (int)SCIP_VERBLEVEL_HIGH) );
3758 #endif
3759 
3760  /* copying memory and time limits */
3761  SCIP_CALL( SCIPgetRealParam(subproblem, "limits/time", &timelimit) );
3762  SCIP_CALL( SCIPgetRealParam(subproblem, "limits/memory", &memorylimit) );
3763  SCIP_CALL( copyMemoryAndTimeLimits(set->scip, subproblem) );
3764 
3765  /* if the subproblem is independent, then the default SCIP settings are used. Otherwise, only the root node is solved
3766  * to compute a lower bound on the subproblem
3767  */
3768  SCIP_CALL( SCIPgetLongintParam(subproblem, "limits/totalnodes", &totalnodes) );
3769  SCIP_CALL( SCIPgetIntParam(subproblem, "lp/disablecutoff", &disablecutoff) );
3770  if( !SCIPbendersSubproblemIsIndependent(benders, probnumber) )
3771  {
3772  SCIP_CALL( SCIPsetLongintParam(subproblem, "limits/totalnodes", 1LL) );
3773  SCIP_CALL( SCIPsetIntParam(subproblem, "lp/disablecutoff", 1) );
3774  }
3775 
3776  /* if the subproblem not independent and is convex, then the probing LP is solved. Otherwise, the MIP is solved */
3777  if( SCIPbendersSubproblemIsConvex(benders, probnumber) )
3778  {
3779  assert(SCIPisLPConstructed(subproblem));
3780 
3781  SCIP_CALL( SCIPstartProbing(subproblem) );
3782  SCIP_CALL( SCIPsolveProbingLP(subproblem, -1, &lperror, &cutoff) );
3783 
3784  if( SCIPgetLPSolstat(subproblem) == SCIP_LPSOLSTAT_INFEASIBLE )
3785  (*infeasible) = TRUE;
3786  }
3787  else
3788  {
3789  SCIP_EVENTHDLRDATA* eventhdlrdata;
3790 
3791  /* if the subproblem is not convex, then event handlers have been added to interrupt the solve. These must be
3792  * disabled
3793  */
3795  eventhdlrdata->solvecip = TRUE;
3796 
3797  SCIP_CALL( SCIPsolve(subproblem) );
3798 
3799  if( SCIPgetStatus(subproblem) == SCIP_STATUS_INFEASIBLE )
3800  (*infeasible) = TRUE;
3801  }
3802 
3803  /* getting the lower bound value */
3804  if( !(*infeasible) )
3805  (*lowerbound) = SCIPgetDualbound(subproblem);
3806  else
3807  (*lowerbound) = -SCIPinfinity(subproblem);
3808 
3809  if( !SCIPbendersSubproblemIsIndependent(benders, probnumber) )
3810  {
3811  SCIP_CALL( SCIPsetLongintParam(subproblem, "limits/totalnodes", totalnodes) );
3812  SCIP_CALL( SCIPsetIntParam(subproblem, "lp/disablecutoff", disablecutoff) );
3813  }
3814  SCIP_CALL( SCIPsetIntParam(subproblem, "display/verblevel", verblevel) );
3815  SCIP_CALL( SCIPsetRealParam(subproblem, "limits/memory", memorylimit) );
3816  SCIP_CALL( SCIPsetRealParam(subproblem, "limits/time", timelimit) );
3817 
3818  /* the subproblem must be freed so that it is reset for the subsequent Benders' decomposition solves. If the
3819  * subproblems are independent, they are not freed. SCIPfreeBendersSubproblem must still be called, but in this
3820  * function the independent subproblems are not freed. However, they will still be freed at the end of the
3821  * solving process for the master problem.
3822  */
3823  SCIP_CALL( SCIPbendersFreeSubproblem(benders, set, probnumber) );
3824 
3825  return SCIP_OKAY;
3826 }
3827 
3828 /** Merges a subproblem into the master problem. This process just adds a copy of the subproblem variables and
3829  * constraints to the master problem, but keeps the subproblem stored in the Benders' decomposition data structure. The reason for
3830  * keeping the subproblem available is for when it is queried for solutions after the problem is solved.
3831  *
3832  * Once the subproblem is merged into the master problem, then the subproblem is flagged as disabled. This means that
3833  * it will not be solved in the subsequent subproblem solving loops.
3834  *
3835  * The associated auxiliary variables are kept in the master problem. The objective function of the merged subproblem
3836  * is added as an underestimator constraint.
3837  */
3839  SCIP_BENDERS* benders, /**< Benders' decomposition */
3840  SCIP_SET* set, /**< global SCIP settings */
3841  SCIP_HASHMAP* varmap, /**< a hashmap to store the mapping of subproblem variables corresponding
3842  * to the newly created master variables, or NULL */
3843  SCIP_HASHMAP* consmap, /**< a hashmap to store the mapping of subproblem constraints to the
3844  corresponding newly created constraints, or NULL */
3845  int probnumber /**< the number of the subproblem that will be merged into the master problem*/
3846  )
3847 {
3848  SCIP* subproblem;
3849  SCIP_HASHMAP* localvarmap;
3850  SCIP_HASHMAP* localconsmap;
3851  SCIP_VAR** vars;
3852  SCIP_VAR* auxiliaryvar;
3853  SCIP_CONS** conss;
3854  SCIP_CONS* objcons;
3855  int nvars;
3856  int nconss;
3857  int i;
3858  SCIP_Bool uselocalvarmap;
3859  SCIP_Bool uselocalconsmap;
3860  char varname[SCIP_MAXSTRLEN];
3861  char consname[SCIP_MAXSTRLEN];
3862  const char* origvarname;
3863 
3864  assert(benders != NULL);
3865  assert(set != NULL);
3866  assert(probnumber >= 0 && probnumber < benders->nsubproblems);
3867 
3868  SCIPverbMessage(set->scip, SCIP_VERBLEVEL_HIGH, NULL, " Benders' decomposition: Infeasibility of subproblem %d can't "
3869  "be resolved. Subproblem %d is being merged into the master problem.\n", probnumber, probnumber);
3870 
3871  /* freeing the subproblem because it will be flagged as independent. Since the subproblem is flagged as independent,
3872  * it will no longer be solved or freed within the solving loop.
3873  */
3874  SCIP_CALL( SCIPbendersFreeSubproblem(benders, set, probnumber) );
3875 
3876  subproblem = SCIPbendersSubproblem(benders, probnumber);
3877 
3878  uselocalvarmap = (varmap == NULL);
3879  uselocalconsmap = (consmap == NULL);
3880 
3881  if( uselocalvarmap )
3882  {
3883  /* create the variable mapping hash map */
3884  SCIP_CALL( SCIPhashmapCreate(&localvarmap, SCIPblkmem(set->scip), SCIPgetNVars(subproblem)) );
3885  }
3886  else
3887  localvarmap = varmap;
3888 
3889  if( uselocalconsmap )
3890  {
3891  /* create the constraint mapping hash map */
3892  SCIP_CALL( SCIPhashmapCreate(&localconsmap, SCIPblkmem(set->scip), SCIPgetNConss(subproblem)) );
3893  }
3894  else
3895  localconsmap = consmap;
3896 
3897  /* retrieving the subproblem variable to build a subproblem mapping */
3898  vars = SCIPgetVars(subproblem);
3899  nvars = SCIPgetNVars(subproblem);
3900 
3901  /* creating the objective function constraint that will be added to the master problem */
3902  /* setting the name of the transferred cut */
3903  (void) SCIPsnprintf(consname, SCIP_MAXSTRLEN, "objectivecons_%d", probnumber );
3904  SCIP_CALL( SCIPcreateConsBasicLinear(set->scip, &objcons, consname, 0, NULL, NULL, -SCIPsetInfinity(set), 0.0) );
3905  SCIP_CALL( SCIPsetConsRemovable(set->scip, objcons, TRUE) );
3906 
3907  for( i = 0; i < nvars; i++ )
3908  {
3909  SCIP_VAR* mastervar = NULL;
3910  SCIP_Bool releasevar = FALSE;
3911 
3912  SCIP_CALL( SCIPgetBendersMasterVar(set->scip, benders, vars[i], &mastervar) );
3913 
3914  /* if the master problem variable is not NULL, then there is a corresponding variable in the master problem for
3915  * the given subproblem variable. In this case, the variable is added to the hashmap.
3916  */
3917  if( mastervar == NULL )
3918  {
3919  SCIP_VAR* origvar;
3920  SCIP_Real scalar;
3921  SCIP_Real constant;
3922 
3923  /* This is following the same process as in createVariableMappings. The original variable is used to map
3924  * between the subproblem and the master problem
3925  */
3926  origvar = vars[i];
3927  scalar = 1.0;
3928  constant = 0.0;
3929  SCIP_CALL( SCIPvarGetOrigvarSum(&origvar, &scalar, &constant) );
3930 
3931  /* retrieving the var name */
3932  origvarname = SCIPvarGetName(origvar);
3933  (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "%s", origvarname);
3934 
3935  /* creating and adding the variable to the Benders' decomposition master problem */
3936  SCIP_CALL( SCIPcreateVarBasic(set->scip, &mastervar, varname, SCIPvarGetLbOriginal(origvar),
3937  SCIPvarGetUbOriginal(origvar), 0.0, SCIPvarGetType(origvar)) );
3938 
3939  /* adding the variable to the master problem */
3940  SCIP_CALL( SCIPaddVar(set->scip, mastervar) );
3941 
3942  /* adds the variable to the objective function constraint */
3943  SCIP_CALL( SCIPaddCoefLinear(set->scip, objcons, mastervar, SCIPvarGetObj(origvar)) );
3944 
3945  /* the variable must be released */
3946  releasevar = TRUE;
3947  }
3948 
3949  /* creating the mapping betwen the subproblem var and the master var for the constraint copying */
3950  SCIP_CALL( SCIPhashmapInsert(localvarmap, vars[i], mastervar) );
3951 
3952  /* releasing the variable */
3953  if( releasevar )
3954  {
3955  SCIP_CALL( SCIPreleaseVar(set->scip, &mastervar) );
3956  }
3957  }
3958 
3959  /* getting the constraints from the subproblem that will be added to the master problem */
3960  conss = SCIPgetConss(subproblem);
3961  nconss = SCIPgetNConss(subproblem);
3962 
3963  /* getting a copy of all constraints and adding it to the master problem */
3964  for( i = 0; i < nconss; i++ )
3965  {
3966  SCIP_CONS* targetcons;
3967  SCIP_Bool initial;
3968  SCIP_Bool valid;
3969 
3970  /* NOTE: adding all subproblem constraints appears to cause an error when resolving the LP, which results in the
3971  * current incumbent being reported as optimal. To avoid this, only half of the subproblem constraints are added
3972  * the master problem. The remaining half are marked as lazy and are separated as required.
3973  */
3974  initial = (i < nconss/2);
3975 
3976  SCIP_CALL( SCIPgetConsCopy(subproblem, set->scip, conss[i], &targetcons, SCIPconsGetHdlr(conss[i]),
3977  localvarmap, localconsmap, NULL, initial, SCIPconsIsSeparated(conss[i]),
3978  SCIPconsIsEnforced(conss[i]), SCIPconsIsChecked(conss[i]), SCIPconsIsPropagated(conss[i]), FALSE,
3979  SCIPconsIsModifiable(conss[i]), SCIPconsIsDynamic(conss[i]), SCIPconsIsRemovable(conss[i]),
3980  FALSE, TRUE, &valid) );
3981  assert(SCIPconsIsInitial(conss[i]));
3982  assert(valid);
3983 
3984  SCIP_CALL( SCIPaddCons(set->scip, targetcons) );
3985 
3986  SCIP_CALL( SCIPreleaseCons(set->scip, &targetcons) );
3987  }
3988 
3989  /* freeing the hashmaps */
3990  if( uselocalvarmap )
3991  {
3992  /* free hash map */
3993  SCIPhashmapFree(&localvarmap);
3994  }
3995 
3996  if( uselocalconsmap )
3997  {
3998  /* free hash map */
3999  SCIPhashmapFree(&localconsmap);
4000  }
4001 
4002  /* adding the auxiliary variable to the objective constraint */
4003  auxiliaryvar = SCIPbendersGetAuxiliaryVar(benders, probnumber);
4004  SCIP_CALL( SCIPaddCoefLinear(set->scip, objcons, auxiliaryvar, -1.0) );
4005 
4006  /* adding the objective function constraint to the master problem */
4007  SCIP_CALL( SCIPaddCons(set->scip, objcons) );
4008 
4009  SCIP_CALL( SCIPreleaseCons(set->scip, &objcons) );
4010 
4011  /* the merged subproblem is no longer solved. This is indicated by setting the subproblem as disabled. The
4012  * subproblem still exists, but it is not solved in the solving loop.
4013  */
4014  SCIPbendersSetSubproblemEnabled(benders, probnumber, FALSE);
4015 
4016  return SCIP_OKAY;
4017 }
4018 
4019 /** Returns the corresponding master or subproblem variable for the given variable.
4020  * This provides a call back for the variable mapping between the master and subproblems. */
4022  SCIP_BENDERS* benders, /**< Benders' decomposition */
4023  SCIP_SET* set, /**< global SCIP settings */
4024  SCIP_VAR* var, /**< the variable for which the corresponding variable is desired */
4025  SCIP_VAR** mappedvar, /**< the variable that is mapped to var */
4026  int probnumber /**< the problem number for the desired variable, -1 for the master problem */
4027  )
4028 {
4029  assert(benders != NULL);
4030  assert(set != NULL);
4031  assert(var != NULL);
4032  assert(mappedvar != NULL);
4033  assert(benders->bendersgetvar != NULL);
4034 
4035  (*mappedvar) = NULL;
4036 
4037  /* if the variable name matches the auxiliary variable, then the master variable is returned as NULL */
4038  if( strstr(SCIPvarGetName(var), AUXILIARYVAR_NAME) != NULL )
4039  return SCIP_OKAY;
4040 
4041  SCIP_CALL( benders->bendersgetvar(set->scip, benders, var, mappedvar, probnumber) );
4042 
4043  return SCIP_OKAY;
4044 }
4045 
4046 /** gets user data of Benders' decomposition */
4048  SCIP_BENDERS* benders /**< Benders' decomposition */
4049  )
4050 {
4051  assert(benders != NULL);
4052 
4053  return benders->bendersdata;
4054 }
4055 
4056 /** sets user data of Benders' decomposition; user has to free old data in advance! */
4058  SCIP_BENDERS* benders, /**< Benders' decomposition */
4059  SCIP_BENDERSDATA* bendersdata /**< new Benders' decomposition user data */
4060  )
4061 {
4062  assert(benders != NULL);
4063 
4064  benders->bendersdata = bendersdata;
4065 }
4066 
4067 /** sets copy callback of Benders' decomposition */
4069  SCIP_BENDERS* benders, /**< Benders' decomposition */
4070  SCIP_DECL_BENDERSCOPY ((*benderscopy)) /**< copy callback of Benders' decomposition */
4071  )
4072 {
4073  assert(benders != NULL);
4074 
4075  benders->benderscopy = benderscopy;
4076 }
4077 
4078 /** sets destructor callback of Benders' decomposition */
4080  SCIP_BENDERS* benders, /**< Benders' decomposition */
4081  SCIP_DECL_BENDERSFREE ((*bendersfree)) /**< destructor of Benders' decomposition */
4082  )
4083 {
4084  assert(benders != NULL);
4085 
4086  benders->bendersfree = bendersfree;
4087 }
4088 
4089 /** sets initialization callback of Benders' decomposition */
4091  SCIP_BENDERS* benders, /**< Benders' decomposition */
4092  SCIP_DECL_BENDERSINIT((*bendersinit)) /**< initialize the Benders' decomposition */
4093  )
4094 {
4095  assert(benders != NULL);
4096 
4097  benders->bendersinit = bendersinit;
4098 }
4099 
4100 /** sets deinitialization callback of Benders' decomposition */
4102  SCIP_BENDERS* benders, /**< Benders' decomposition */
4103  SCIP_DECL_BENDERSEXIT((*bendersexit)) /**< deinitialize the Benders' decomposition */
4104  )
4105 {
4106  assert(benders != NULL);
4107 
4108  benders->bendersexit = bendersexit;
4109 }
4110 
4111 /** sets presolving initialization callback of Benders' decomposition */
4113  SCIP_BENDERS* benders, /**< Benders' decomposition */
4114  SCIP_DECL_BENDERSINITPRE((*bendersinitpre))/**< initialize presolving for Benders' decomposition */
4115  )
4116 {
4117  assert(benders != NULL);
4118 
4119  benders->bendersinitpre = bendersinitpre;
4120 }
4121 
4122 /** sets presolving deinitialization callback of Benders' decomposition */
4124  SCIP_BENDERS* benders, /**< Benders' decomposition */
4125  SCIP_DECL_BENDERSEXITPRE((*bendersexitpre))/**< deinitialize presolving for Benders' decomposition */
4126  )
4127 {
4128  assert(benders != NULL);
4129 
4130  benders->bendersexitpre = bendersexitpre;
4131 }
4132 
4133 /** sets solving process initialization callback of Benders' decomposition */
4135  SCIP_BENDERS* benders, /**< Benders' decomposition */
4136  SCIP_DECL_BENDERSINITSOL((*bendersinitsol))/**< solving process initialization callback of Benders' decomposition */
4137  )
4138 {
4139  assert(benders != NULL);
4140 
4141  benders->bendersinitsol = bendersinitsol;
4142 }
4143 
4144 /** sets solving process deinitialization callback of Benders' decomposition */
4146  SCIP_BENDERS* benders, /**< Benders' decomposition */
4147  SCIP_DECL_BENDERSEXITSOL((*bendersexitsol))/**< solving process deinitialization callback of Benders' decomposition */
4148  )
4149 {
4150  assert(benders != NULL);
4151 
4152  benders->bendersexitsol = bendersexitsol;
4153 }
4154 
4155 /** sets the pre subproblem solve callback of Benders' decomposition */
4157  SCIP_BENDERS* benders, /**< Benders' decomposition */
4158  SCIP_DECL_BENDERSPRESUBSOLVE((*benderspresubsolve))/**< called prior to the subproblem solving loop */
4159  )
4160 {
4161  assert(benders != NULL);
4162 
4163  benders->benderspresubsolve = benderspresubsolve;
4164 }
4165 
4166 /** sets convex solve callback of Benders' decomposition */
4168  SCIP_BENDERS* benders, /**< Benders' decomposition */
4169  SCIP_DECL_BENDERSSOLVESUBCONVEX((*benderssolvesubconvex))/**< solving method for the convex Benders' decomposition subproblem */
4170  )
4171 {
4172  assert(benders != NULL);
4173 
4174  benders->benderssolvesubconvex = benderssolvesubconvex;
4175 }
4176 
4177 /** sets solve callback of Benders' decomposition */
4179  SCIP_BENDERS* benders, /**< Benders' decomposition */
4180  SCIP_DECL_BENDERSSOLVESUB((*benderssolvesub))/**< solving method for a Benders' decomposition subproblem */
4181  )
4182 {
4183  assert(benders != NULL);
4184 
4185  benders->benderssolvesub = benderssolvesub;
4186 }
4187 
4188 /** sets post-solve callback of Benders' decomposition */
4190  SCIP_BENDERS* benders, /**< Benders' decomposition */
4191  SCIP_DECL_BENDERSPOSTSOLVE((*benderspostsolve))/**< solving process deinitialization callback of Benders' decomposition */
4192  )
4193 {
4194  assert(benders != NULL);
4195 
4196  benders->benderspostsolve = benderspostsolve;
4197 }
4198 
4199 /** sets free subproblem callback of Benders' decomposition */
4201  SCIP_BENDERS* benders, /**< Benders' decomposition */
4202  SCIP_DECL_BENDERSFREESUB((*bendersfreesub))/**< the freeing callback for the subproblem */
4203  )
4204 {
4205  assert(benders != NULL);
4206 
4207  benders->bendersfreesub = bendersfreesub;
4208 }
4209 
4210 /** gets name of Benders' decomposition */
4212  SCIP_BENDERS* benders /**< Benders' decomposition */
4213  )
4214 {
4215  assert(benders != NULL);
4216 
4217  return benders->name;
4218 }
4219 
4220 /** gets description of Benders' decomposition */
4222  SCIP_BENDERS* benders /**< Benders' decomposition */
4223  )
4224 {
4225  assert(benders != NULL);
4226 
4227  return benders->desc;
4228 }
4229 
4230 /** gets priority of Benders' decomposition */
4232  SCIP_BENDERS* benders /**< Benders' decomposition */
4233  )
4234 {
4235  assert(benders != NULL);
4236 
4237  return benders->priority;
4238 }
4239 
4240 /** sets priority of Benders' decomposition */
4242  SCIP_BENDERS* benders, /**< Benders' decomposition */
4243  SCIP_SET* set, /**< global SCIP settings */
4244  int priority /**< new priority of the Benders' decomposition */
4245  )
4246 {
4247  assert(benders != NULL);
4248  assert(set != NULL);
4249 
4250  benders->priority = priority;
4251  set->benderssorted = FALSE;
4252 }
4253 
4254 /** gets the number of subproblems for the Benders' decomposition */
4256  SCIP_BENDERS* benders /**< the Benders' decomposition data structure */
4257  )
4258 {
4259  assert(benders != NULL);
4260 
4261  return benders->nsubproblems;
4262 }
4263 
4264 /** returns the SCIP instance for a given subproblem */
4266  SCIP_BENDERS* benders, /**< the Benders' decomposition data structure */
4267  int probnumber /**< the subproblem number */
4268  )
4269 {
4270  assert(benders != NULL);
4271  assert(probnumber >= 0 && probnumber < benders->nsubproblems);
4272 
4273  return benders->subproblems[probnumber];
4274 }
4275 
4276 /** gets the number of times, the Benders' decomposition was called and tried to find a variable with negative reduced costs */
4278  SCIP_BENDERS* benders /**< Benders' decomposition */
4279  )
4280 {
4281  assert(benders != NULL);
4282 
4283  return benders->ncalls;
4284 }
4285 
4286 /** gets the number of optimality cuts found by the collection of Benders' decomposition subproblems */
4288  SCIP_BENDERS* benders /**< Benders' decomposition */
4289  )
4290 {
4291  assert(benders != NULL);
4292 
4293  return benders->ncutsfound;
4294 }
4295 
4296 /** gets time in seconds used in this Benders' decomposition for setting up for next stages */
4298  SCIP_BENDERS* benders /**< Benders' decomposition */
4299  )
4300 {
4301  assert(benders != NULL);
4302 
4303  return SCIPclockGetTime(benders->setuptime);
4304 }
4305 
4306 /** gets time in seconds used in this Benders' decomposition */
4308  SCIP_BENDERS* benders /**< Benders' decomposition */
4309  )
4310 {
4311  assert(benders != NULL);
4312 
4313  return SCIPclockGetTime(benders->bendersclock);
4314 }
4315 
4316 /** enables or disables all clocks of the Benders' decomposition, depending on the value of the flag */
4318  SCIP_BENDERS* benders, /**< the Benders' decomposition for which all clocks should be enabled or disabled */
4319  SCIP_Bool enable /**< should the clocks of the Benders' decomposition be enabled? */
4320  )
4321 {
4322  assert(benders != NULL);
4323 
4324  SCIPclockEnableOrDisable(benders->setuptime, enable);
4325  SCIPclockEnableOrDisable(benders->bendersclock, enable);
4326 }
4327 
4328 /** is Benders' decomposition initialized? */
4330  SCIP_BENDERS* benders /**< Benders' decomposition */
4331  )
4332 {
4333  assert(benders != NULL);
4334 
4335  return benders->initialized;
4336 }
4337 
4338 /** Are Benders' cuts generated from the LP solutions? */
4340  SCIP_BENDERS* benders /**< Benders' decomposition */
4341  )
4342 {
4343  assert(benders != NULL);
4344 
4345  return benders->cutlp;
4346 }
4347 
4348 /** Are Benders' cuts generated from the pseudo solutions? */
4350  SCIP_BENDERS* benders /**< Benders' decomposition */
4351  )
4352 {
4353  assert(benders != NULL);
4354 
4355  return benders->cutpseudo;
4356 }
4357 
4358 /** Are Benders' cuts generated from the relaxation solutions? */
4360  SCIP_BENDERS* benders /**< Benders' decomposition */
4361  )
4362 {
4363  assert(benders != NULL);
4364 
4365  return benders->cutrelax;
4366 }
4367 
4368 /** should this Benders' use the auxiliary variables from the highest priority Benders' */
4370  SCIP_BENDERS* benders /**< Benders' decomposition */
4371  )
4372 {
4373  assert(benders != NULL);
4374 
4375  return benders->shareauxvars;
4376 }
4377 
4378 /** adds a subproblem to the Benders' decomposition data */
4380  SCIP_BENDERS* benders, /**< Benders' decomposition */
4381  SCIP* subproblem /**< subproblem to be added to the data storage */
4382  )
4383 {
4384  assert(benders != NULL);
4385  assert(subproblem != NULL);
4386  assert(benders->subproblems != NULL);
4387  assert(benders->naddedsubprobs + 1 <= benders->nsubproblems);
4388 
4389  benders->subproblems[benders->naddedsubprobs] = subproblem;
4390 
4391  benders->naddedsubprobs++;
4392 
4393  return SCIP_OKAY;
4394 }
4395 
4396 /** removes the subproblems from the Benders' decomposition data */
4398  SCIP_BENDERS* benders /**< Benders' decomposition */
4399  )
4400 {
4401  assert(benders != NULL);
4402  assert(benders->subproblems != NULL);
4403 
4404  BMSclearMemoryArray(&benders->subproblems, benders->naddedsubprobs);
4405  benders->naddedsubprobs = 0;
4406 }
4407 
4408 /** returns the auxiliary variable for the given subproblem */
4410  SCIP_BENDERS* benders, /**< Benders' decomposition */
4411  int probnumber /**< the subproblem number */
4412  )
4413 {
4414  assert(benders != NULL);
4415  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4416 
4417  return benders->auxiliaryvars[probnumber];
4418 }
4419 
4420 /** returns all auxiliary variables */
4422  SCIP_BENDERS* benders /**< Benders' decomposition */
4423  )
4424 {
4425  assert(benders != NULL);
4426 
4427  return benders->auxiliaryvars;
4428 }
4429 
4430 /** stores the objective function value of the subproblem for use in cut generation */
4432  SCIP_BENDERS* benders, /**< the Benders' decomposition structure */
4433  int probnumber, /**< the subproblem number */
4434  SCIP_Real objval /**< the objective function value for the subproblem */
4435  )
4436 {
4437  assert(benders != NULL);
4438  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4439 
4440  /* updating the best objval */
4441  if( objval < benders->bestsubprobobjval[probnumber] )
4442  benders->bestsubprobobjval[probnumber] = objval;
4443 
4444  benders->subprobobjval[probnumber] = objval;
4445 }
4446 
4447 /** returns the objective function value of the subproblem for use in cut generation */
4449  SCIP_BENDERS* benders, /**< Benders' decomposition */
4450  int probnumber /**< the subproblem number */
4451  )
4452 {
4453  assert(benders != NULL);
4454  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4455 
4456  return benders->subprobobjval[probnumber];
4457 }
4458 
4459 /** sets the flag indicating whether a subproblem is convex
4460  *
4461  * It is possible that this can change during the solving process. One example is when the three-phase method is
4462  * employed, where the first phase solves the convex relaxation of both the master and subproblems, the second phase
4463  * reintroduces the integrality constraints to the master problem and the third phase then reintroduces integrality
4464  * constraints to the subproblems.
4465  */
4467  SCIP_BENDERS* benders, /**< Benders' decomposition */
4468  int probnumber, /**< the subproblem number */
4469  SCIP_Bool isconvex /**< flag to indicate whether the subproblem is convex */
4470  )
4471 {
4472  assert(benders != NULL);
4473  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4474 
4475  if( isconvex && !benders->subprobisconvex[probnumber] )
4476  benders->nconvexsubprobs++;
4477  else if( !isconvex && benders->subprobisconvex[probnumber] )
4478  benders->nconvexsubprobs--;
4479 
4480  benders->subprobisconvex[probnumber] = isconvex;
4481 
4482  assert(benders->nconvexsubprobs >= 0 && benders->nconvexsubprobs <= benders->nsubproblems);
4483 }
4484 
4485 /** returns whether the subproblem is convex
4486  *
4487  * This means that the dual solution can be used to generate cuts.
4488  */
4490  SCIP_BENDERS* benders, /**< Benders' decomposition */
4491  int probnumber /**< the subproblem number */
4492  )
4493 {
4494  assert(benders != NULL);
4495  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4496 
4497  return benders->subprobisconvex[probnumber];
4498 }
4499 
4500 /** returns the number of subproblems that are convex */
4502  SCIP_BENDERS* benders /**< Benders' decomposition */
4503  )
4504 {
4505  assert(benders != NULL);
4506 
4507  return benders->nconvexsubprobs;
4508 }
4509 
4510 /** changes all of the master problem variables in the given subproblem to continuous. */
4512  SCIP_BENDERS* benders, /**< Benders' decomposition */
4513  SCIP_SET* set, /**< global SCIP settings */
4514  int probnumber /**< the subproblem number */
4515  )
4516 {
4517  SCIP* subproblem;
4518  SCIP_VAR** vars;
4519  int nbinvars;
4520  int nintvars;
4521  int nimplvars;
4522  int chgvarscount;
4523  int origintvars;
4524  int i;
4525  SCIP_Bool infeasible;
4526 
4527  assert(benders != NULL);
4528  assert(set != NULL);
4529  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4530 
4531  subproblem = SCIPbendersSubproblem(benders, probnumber);
4532  assert(subproblem != NULL);
4533 
4534  /* only set the master problem variable to continuous if they have not already been changed. */
4535  if( !SCIPbendersGetMastervarsCont(benders, probnumber) )
4536  {
4537  SCIP_VAR* mastervar;
4538 
4539  /* retrieving the variable data */
4540  SCIP_CALL( SCIPgetVarsData(subproblem, &vars, NULL, &nbinvars, &nintvars, &nimplvars, NULL) );
4541 
4542  origintvars = nbinvars + nintvars + nimplvars;
4543 
4544  chgvarscount = 0;
4545 
4546  /* looping over all integer variables to change the master variables to continuous */
4547  i = 0;
4548  while( i < nbinvars + nintvars + nimplvars )
4549  {
4550  SCIP_CALL( SCIPbendersGetVar(benders, set, vars[i], &mastervar, -1) );
4551 
4552  if( SCIPvarGetType(vars[i]) != SCIP_VARTYPE_CONTINUOUS && mastervar != NULL )
4553  {
4554  /* changing the type of the subproblem variable corresponding to mastervar to CONTINUOUS */
4555  SCIP_CALL( SCIPchgVarType(subproblem, vars[i], SCIP_VARTYPE_CONTINUOUS, &infeasible) );
4556 
4557  assert(!infeasible);
4558 
4559  chgvarscount++;
4560  SCIP_CALL( SCIPgetVarsData(subproblem, NULL, NULL, &nbinvars, &nintvars, &nimplvars, NULL) );
4561  }
4562  else
4563  i++;
4564  }
4565 
4566  /* if all of the integer variables have been changed to continuous, then the subproblem must now be an LP. In this
4567  * case, the subproblem is initialised and then put into probing mode */
4568  if( chgvarscount > 0 && chgvarscount == origintvars )
4569  {
4570  SCIP_CALL( initialiseLPSubproblem(benders, set, probnumber) );
4571  SCIPbendersSetSubproblemIsConvex(benders, probnumber, TRUE);
4572  }
4573 
4574  SCIP_CALL( SCIPbendersSetMastervarsCont(benders, probnumber, TRUE) );
4575  }
4576 
4577  return SCIP_OKAY;
4578 }
4579 
4580 /** sets the subproblem setup flag */
4582  SCIP_BENDERS* benders, /**< Benders' decomposition */
4583  int probnumber, /**< the subproblem number */
4584  SCIP_Bool issetup /**< flag to indicate whether the subproblem has been setup */
4585  )
4586 {
4587  assert(benders != NULL);
4588  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4589 
4590  benders->subprobsetup[probnumber] = issetup;
4591 }
4592 
4593 /** returns the subproblem setup flag */
4595  SCIP_BENDERS* benders, /**< Benders' decomposition */
4596  int probnumber /**< the subproblem number */
4597  )
4598 {
4599  assert(benders != NULL);
4600  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4601 
4602  return benders->subprobsetup[probnumber];
4603 }
4604 
4605 /** sets the independent subproblem flag */
4607  SCIP_BENDERS* benders, /**< Benders' decomposition */
4608  int probnumber, /**< the subproblem number */
4609  SCIP_Bool isindep /**< flag to indicate whether the subproblem is independent */
4610  )
4611 {
4612  assert(benders != NULL);
4613  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4614 
4615  /* if the user has defined solving or freeing functions, then it is not possible to declare a subproblem as
4616  * independent. This is because declaring a subproblem as independent changes the solving loop, so it would change
4617  * the expected behaviour of the user defined plugin. If a user calls this function, then an error will be returned.
4618  */
4619  if( benders->benderssolvesubconvex != NULL || benders->benderssolvesub != NULL || benders->bendersfreesub != NULL )
4620  {
4621  SCIPerrorMessage("The user has defined either bendersSolvesubconvex%d, bendersSolvesub%d or bendersFreesub%s. "
4622  "Thus, it is not possible to declare the independence of a subproblem.\n", benders->name, benders->name,
4623  benders->name);
4624  SCIPABORT();
4625  }
4626  else
4627  {
4628  SCIP_Bool activesubprob;
4629 
4630  /* if the active status of the subproblem changes, then we must update the activesubprobs counter */
4631  activesubprob = subproblemIsActive(benders, probnumber);
4632 
4633  benders->indepsubprob[probnumber] = isindep;
4634 
4635  /* updating the activesubprobs counter */
4636  if( activesubprob && !subproblemIsActive(benders, probnumber) )
4637  benders->nactivesubprobs--;
4638  else if( !activesubprob && subproblemIsActive(benders, probnumber) )
4639  benders->nactivesubprobs++;
4640 
4641  assert(benders->nactivesubprobs >= 0 && benders->nactivesubprobs <= SCIPbendersGetNSubproblems(benders));
4642  }
4643 }
4644 
4645 /** returns whether the subproblem is independent */
4647  SCIP_BENDERS* benders, /**< Benders' decomposition */
4648  int probnumber /**< the subproblem number */
4649  )
4650 {
4651  assert(benders != NULL);
4652  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4653 
4654  return benders->indepsubprob[probnumber];
4655 }
4656 
4657 /** Sets whether the subproblem is enabled or disabled. A subproblem is disabled if it has been merged into the master
4658  * problem.
4659  */
4661  SCIP_BENDERS* benders, /**< Benders' decomposition */
4662  int probnumber, /**< the subproblem number */
4663  SCIP_Bool enabled /**< flag to indicate whether the subproblem is enabled */
4664  )
4665 {
4666  SCIP_Bool activesubprob;
4667 
4668  assert(benders != NULL);
4669  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4670 
4671  /* if the active status of the subproblem changes, then we must update the activesubprobs counter */
4672  activesubprob = subproblemIsActive(benders, probnumber);
4673 
4674  benders->subprobenabled[probnumber] = enabled;
4675 
4676  /* updating the activesubprobs counter */
4677  if( activesubprob && !subproblemIsActive(benders, probnumber) )
4678  benders->nactivesubprobs--;
4679  else if( !activesubprob && subproblemIsActive(benders, probnumber) )
4680  benders->nactivesubprobs++;
4681 
4682  assert(benders->nactivesubprobs >= 0 && benders->nactivesubprobs <= SCIPbendersGetNSubproblems(benders));
4683 }
4684 
4685 /** returns whether the subproblem is enabled, i.e. the subproblem is still solved in the solving loop. */
4687  SCIP_BENDERS* benders, /**< Benders' decomposition */
4688  int probnumber /**< the subproblem number */
4689  )
4690 {
4691  assert(benders != NULL);
4692  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4693 
4694  return benders->subprobenabled[probnumber];
4695 }
4696 
4697 /** sets a flag to indicate whether the master variables are all set to continuous */
4699  SCIP_BENDERS* benders, /**< Benders' decomposition */
4700  int probnumber, /**< the subproblem number */
4701  SCIP_Bool arecont /**< flag to indicate whether the master problem variables are continuous */
4702  )
4703 {
4704  assert(benders != NULL);
4705  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4706 
4707  /* if the master variables were all continuous and now are not, then the subproblem must exit probing mode and be
4708  * changed to non-LP subproblem */
4709  if( benders->mastervarscont[probnumber] && !arecont )
4710  {
4711  if( SCIPinProbing(SCIPbendersSubproblem(benders, probnumber)) )
4712  {
4713  SCIP_CALL( SCIPendProbing(SCIPbendersSubproblem(benders, probnumber)) );
4714  }
4715 
4716  SCIPbendersSetSubproblemIsConvex(benders, probnumber, FALSE);
4717  }
4718 
4719  benders->mastervarscont[probnumber] = arecont;
4720 
4721  return SCIP_OKAY;
4722 }
4723 
4724 /** returns whether the master variables are all set to continuous */
4726  SCIP_BENDERS* benders, /**< Benders' decomposition */
4727  int probnumber /**< the subproblem number */
4728  )
4729 {
4730  assert(benders != NULL);
4731  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4732 
4733  return benders->mastervarscont[probnumber];
4734 }
4735 
4736 /** returns the number of cuts that have been transferred from sub SCIPs to the master SCIP */
4738  SCIP_BENDERS* benders /**< the Benders' decomposition data structure */
4739  )
4740 {
4741  assert(benders != NULL);
4742 
4743  return benders->ntransferred;
4744 }
4745 
4746 /** updates the lower bound for the subproblem. If the lower bound is not greater than the previously stored lowerbound,
4747  * then no update occurs.
4748  */
4750  SCIP_BENDERS* benders, /**< Benders' decomposition */
4751  int probnumber, /**< the subproblem number */
4752  SCIP_Real lowerbound /**< the lower bound */
4753  )
4754 {
4755  assert(benders != NULL);
4756  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4757 
4758  if( lowerbound > benders->subproblowerbound[probnumber] )
4759  benders->subproblowerbound[probnumber] = lowerbound;
4760  else
4761  {
4762  SCIPdebugMessage("The lowerbound %g for subproblem %d is less than the currently stored lower bound %g\n",
4763  lowerbound, probnumber, benders->subproblowerbound[probnumber]);
4764  }
4765 }
4766 
4767 /** returns the stored lower bound for the given subproblem */
4769  SCIP_BENDERS* benders, /**< Benders' decomposition */
4770  int probnumber /**< the subproblem number */
4771  )
4772 {
4773  assert(benders != NULL);
4774  assert(probnumber >= 0 && probnumber < SCIPbendersGetNSubproblems(benders));
4775 
4776  return benders->subproblowerbound[probnumber];
4777 }
4778 
4779 /** sets the sorted flags in the Benders' decomposition */
4781  SCIP_BENDERS* benders, /**< Benders' decomposition structure */
4782  SCIP_Bool sorted /**< the value to set the sorted flag to */
4783  )
4784 {
4785  assert(benders != NULL);
4786 
4787  benders->benderscutssorted = sorted;
4788  benders->benderscutsnamessorted = sorted;
4789 }
4790 
4791 /** inserts a Benders' cut into the Benders' cuts list */
4793  SCIP_BENDERS* benders, /**< Benders' decomposition structure */
4794  SCIP_SET* set, /**< global SCIP settings */
4795  SCIP_BENDERSCUT* benderscut /**< Benders' cut */
4796  )
4797 {
4798  assert(benders != NULL);
4799  assert(benderscut != NULL);
4800 
4801  if( benders->nbenderscuts >= benders->benderscutssize )
4802  {
4803  benders->benderscutssize = SCIPsetCalcMemGrowSize(set, benders->nbenderscuts+1);
4805  }
4806  assert(benders->nbenderscuts < benders->benderscutssize);
4807 
4808  benders->benderscuts[benders->nbenderscuts] = benderscut;
4809  benders->nbenderscuts++;
4810  benders->benderscutssorted = FALSE;
4811 
4812  return SCIP_OKAY;
4813 }
4814 
4815 /** returns the Benders' cut of the given name, or NULL if not existing */
4817  SCIP_BENDERS* benders, /**< Benders' decomposition */
4818  const char* name /**< name of Benderscut' decomposition */
4819  )
4820 {
4821  int i;
4822 
4823  assert(benders != NULL);
4824  assert(name != NULL);
4825 
4826  for( i = 0; i < benders->nbenderscuts; i++ )
4827  {
4828  if( strcmp(SCIPbenderscutGetName(benders->benderscuts[i]), name) == 0 )
4829  return benders->benderscuts[i];
4830  }
4831 
4832  return NULL;
4833 }
4834 
4835 /** returns the array of currently available Benders' cuts; active Benders' decomposition are in the first slots of
4836  * the array
4837  */
4839  SCIP_BENDERS* benders /**< Benders' decomposition */
4840  )
4841 {
4842  assert(benders != NULL);
4843 
4844  if( !benders->benderscutssorted )
4845  {
4846  SCIPsortPtr((void**)benders->benderscuts, SCIPbenderscutComp, benders->nbenderscuts);
4847  benders->benderscutssorted = TRUE;
4848  benders->benderscutsnamessorted = FALSE;
4849  }
4850 
4851  return benders->benderscuts;
4852 }
4853 
4854 /** returns the number of currently available Benders' cuts */
4856  SCIP_BENDERS* benders /**< Benders' decomposition */
4857  )
4858 {
4859  assert(benders != NULL);
4860 
4861  return benders->nbenderscuts;
4862 }
4863 
4864 /** sets the priority of a Benders' decomposition */
4866  SCIP_BENDERS* benders, /**< Benders' decomposition */
4867  SCIP_BENDERSCUT* benderscut, /**< Benders' cut */
4868  int priority /**< new priority of the Benders' decomposition */
4869  )
4870 {
4871  assert(benders != NULL);
4872  assert(benderscut != NULL);
4873 
4874  benderscut->priority = priority;
4875  benders->benderscutssorted = FALSE;
4876 
4877  return SCIP_OKAY;
4878 }
4879 
4880 /** sorts Benders' decomposition cuts by priorities */
4882  SCIP_BENDERS* benders /**< Benders' decomposition */
4883  )
4884 {
4885  assert(benders != NULL);
4886 
4887  if( !benders->benderscutssorted )
4888  {
4889  SCIPsortPtr((void**)benders->benderscuts, SCIPbenderscutComp, benders->nbenderscuts);
4890  benders->benderscutssorted = TRUE;
4891  benders->benderscutsnamessorted = FALSE;
4892  }
4893 }
4894 
4895 /** sorts Benders' decomposition cuts by name */
4897  SCIP_BENDERS* benders /**< Benders' decomposition */
4898  )
4899 {
4900  assert(benders != NULL);
4901 
4902  if( !benders->benderscutsnamessorted )
4903  {
4904  SCIPsortPtr((void**)benders->benderscuts, SCIPbenderscutCompName, benders->nbenderscuts);
4905  benders->benderscutssorted = FALSE;
4906  benders->benderscutsnamessorted = TRUE;
4907  }
4908 }
enum SCIP_Result SCIP_RESULT
Definition: type_result.h:52
SCIP_RETCODE SCIPbenderscutExec(SCIP_BENDERSCUT *benderscut, SCIP_SET *set, SCIP_BENDERS *benders, SCIP_SOL *sol, int probnumber, SCIP_BENDERSENFOTYPE type, SCIP_RESULT *result)
Definition: benderscut.c:362
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
void SCIPeventhdlrSetData(SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: event.c:334
void SCIPbendersSetData(SCIP_BENDERS *benders, SCIP_BENDERSDATA *bendersdata)
Definition: benders.c:4057
#define SCIPfreeBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:97
#define SCIP_DECL_BENDERSCREATESUB(x)
Definition: type_benders.h:170
int SCIPbenderscutGetNAddedCuts(SCIP_BENDERSCUT *benderscut)
Definition: benderscut.c:599
#define UPPERBOUND_EVENTHDLR_DESC
Definition: benders.c:65
SCIP_RETCODE SCIPhashmapInsert(SCIP_HASHMAP *hashmap, void *origin, void *image)
Definition: misc.c:2973
static SCIP_DECL_EVENTEXIT(eventExitBendersNodefocus)
Definition: benders.c:241
SCIP_VAR * SCIPbendersGetAuxiliaryVar(SCIP_BENDERS *benders, int probnumber)
Definition: benders.c:4409
SCIP_Bool SCIPsetIsInfinity(SCIP_SET *set, SCIP_Real val)
Definition: set.c:5953
SCIP_Bool SCIPbendersShareAuxVars(SCIP_BENDERS *benders)
Definition: benders.c:4369
SCIP_Bool benders_copybenders
Definition: struct_set.h:439
#define NULL
Definition: def.h:253
static SCIP_RETCODE updateAuxiliaryVarLowerbound(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_RESULT *result)
Definition: benders.c:1942
SCIP_Bool SCIPisStopped(SCIP *scip)
Definition: scip_general.c:686
SCIP_RETCODE SCIPbendersIncludeBenderscut(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_BENDERSCUT *benderscut)
Definition: benders.c:4792
SCIP_RETCODE SCIPgetBendersSubproblemVar(SCIP *scip, SCIP_BENDERS *benders, SCIP_VAR *var, SCIP_VAR **mappedvar, int probnumber)
Definition: scip_benders.c:658
SCIP_CONSHDLR * SCIPfindConshdlr(SCIP *scip, const char *name)
Definition: scip_cons.c:876
void SCIPbendersDeactivate(SCIP_BENDERS *benders, SCIP_SET *set)
Definition: benders.c:1890
#define BMSfreeMemoryArrayNull(ptr)
Definition: memory.h:138
SCIP_BENDERSDATA * bendersdata
SCIP_Bool SCIPbendersSubproblemIsIndependent(SCIP_BENDERS *benders, int probnumber)
Definition: benders.c:4646
SCIP_HASHMAP * mastervarsmap
#define SCIP_DECL_BENDERSINITSOL(x)
Definition: type_benders.h:130
SCIP_RETCODE SCIPbendersSetupSubproblem(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_SOL *sol, int probnumber)
Definition: benders.c:3119
SCIP_STATUS SCIPgetStatus(SCIP *scip)
Definition: scip_general.c:466
SCIP_EXPORT SCIP_VAR * SCIPvarGetTransVar(SCIP_VAR *var)
Definition: var.c:17055
static SCIP_DECL_EVENTEXEC(eventExecBendersNodefocus)
Definition: benders.c:193
void SCIPbendersUpdateSubproblemLowerbound(SCIP_BENDERS *benders, int probnumber, SCIP_Real lowerbound)
Definition: benders.c:4749
#define SCIP_DECL_BENDERSFREE(x)
Definition: type_benders.h:82
SCIP_RETCODE SCIPinterruptSolve(SCIP *scip)
Definition: scip_solve.c:3399
SCIP_Bool SCIPconsIsChecked(SCIP_CONS *cons)
Definition: cons.c:8275
SCIP_BENDERS ** SCIPgetBenders(SCIP *scip)
Definition: scip_benders.c:470
SCIP_PARAMDATA * SCIPparamGetData(SCIP_PARAM *param)
Definition: paramset.c:661
SCIP_Bool SCIPbendersCutPseudo(SCIP_BENDERS *benders)
Definition: benders.c:4349
#define SCIP_MAXSTRLEN
Definition: def.h:274
SCIP_EVENTHDLR * SCIPfindEventhdlr(SCIP *scip, const char *name)
Definition: scip_event.c:224
internal methods for clocks and timing issues
#define SCIPallocClearBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:84
#define SCIP_DECL_BENDERSINITPRE(x)
Definition: type_benders.h:111
struct SCIP_ParamData SCIP_PARAMDATA
Definition: type_paramset.h:76
void SCIPbendersSetSolvesub(SCIP_BENDERS *benders, SCIP_DECL_BENDERSSOLVESUB((*benderssolvesub)))
Definition: benders.c:4178
#define SCIP_CALL_FINALLY(x, y)
Definition: def.h:407
SCIP_RETCODE SCIPcaptureVar(SCIP *scip, SCIP_VAR *var)
Definition: scip_var.c:1217
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip_sol.c:1352
SCIP_RETCODE SCIPaddCoefLinear(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real val)
#define NODESOLVED_EVENTHDLR_DESC
Definition: benders.c:68
SCIP_Bool SCIPbendersSubproblemIsEnabled(SCIP_BENDERS *benders, int probnumber)
Definition: benders.c:4686
static SCIP_RETCODE solveBendersSubproblems(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_SOL *sol, SCIP_BENDERSENFOTYPE type, SCIP_BENDERSSOLVELOOP solveloop, SCIP_Bool checkint, int *nchecked, int *nverified, SCIP_Bool **subprobsolved, SCIP_BENDERSSUBSTATUS **substatus, SCIP_Bool *infeasible, SCIP_Bool *optimal, SCIP_Bool *stopped)
Definition: benders.c:2040
SCIP_RETCODE SCIPgetConsCopy(SCIP *sourcescip, SCIP *targetscip, SCIP_CONS *sourcecons, SCIP_CONS **targetcons, SCIP_CONSHDLR *sourceconshdlr, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, const char *name, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode, SCIP_Bool global, SCIP_Bool *valid)
Definition: scip_copy.c:1285
struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
Definition: type_event.h:138
#define SCIP_DEFAULT_CUTSASCONSS
Definition: benders.c:46
SCIP_DECL_SORTPTRCOMP(SCIPbendersComp)
Definition: benders.c:709
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
Definition: scip_param.c:240
SCIP_Real SCIPsetInfinity(SCIP_SET *set)
Definition: set.c:5813
#define SCIP_DECL_BENDERSGETVAR(x)
Definition: type_benders.h:339
SCIP_RETCODE SCIPcreateVarBasic(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype)
Definition: scip_var.c:184
SCIP_Bool cutsasconss
static SCIP_RETCODE updateSubproblemLowerbound(SCIP *masterprob, SCIP_BENDERS *benders)
Definition: benders.c:455
static int numSubproblemsToCheck(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_BENDERSENFOTYPE type)
Definition: benders.c:2009
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:314
int SCIPgetNConss(SCIP *scip)
Definition: scip_prob.c:3037
int SCIPgetNVars(SCIP *scip)
Definition: scip_prob.c:1987
void SCIPclockStop(SCIP_CLOCK *clck, SCIP_SET *set)
Definition: clock.c:350
SCIP_PARAM * SCIPgetParam(SCIP *scip, const char *name)
Definition: scip_param.c:224
SCIP_Real SCIPgetSolvingTime(SCIP *scip)
Definition: scip_timing.c:359
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip_message.c:215
int SCIPbendersGetNSubproblems(SCIP_BENDERS *benders)
Definition: benders.c:4255
#define FALSE
Definition: def.h:73
SCIP_RETCODE SCIPbendersExit(SCIP_BENDERS *benders, SCIP_SET *set)
Definition: benders.c:1551
static SCIP_Bool subproblemIsActive(SCIP_BENDERS *benders, int probnumber)
Definition: benders.c:2023
SCIP_Longint SCIPbenderscutGetNFound(SCIP_BENDERSCUT *benderscut)
Definition: benderscut.c:559
SCIP_RETCODE SCIPbendersCopyInclude(SCIP_BENDERS *benders, SCIP_SET *sourceset, SCIP_SET *targetset, SCIP_HASHMAP *varmap, SCIP_Bool *valid)
Definition: benders.c:777
SCIP_RETCODE SCIPbendersExitpre(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_STAT *stat)
Definition: benders.c:1712
struct SCIP_VarData SCIP_VARDATA
Definition: type_var.h:107
SCIP_EXPORT SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
Definition: var.c:17200
internal methods for Benders&#39; decomposition
void SCIPclockStart(SCIP_CLOCK *clck, SCIP_SET *set)
Definition: clock.c:280
SCIP_CLOCK * bendersclock
void * SCIPhashmapEntryGetImage(SCIP_HASHMAPENTRY *entry)
Definition: misc.c:3387
SCIP_RETCODE SCIPsetEventhdlrInitsol(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTINITSOL((*eventinitsol)))
Definition: scip_event.c:182
SCIP_Real * bestsubprobobjval
SCIP_RETCODE SCIPcreateConsBasicLinear(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *vals, SCIP_Real lhs, SCIP_Real rhs)
static SCIP_RETCODE initsolEventhandler(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTTYPE eventtype)
Definition: benders.c:103
SCIP_EXPORT SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
Definition: var.c:16903
#define TRUE
Definition: def.h:72
SCIP_CONS ** SCIPgetConss(SCIP *scip)
Definition: scip_prob.c:3083
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
enum SCIP_BendersEnfoType SCIP_BENDERSENFOTYPE
Definition: type_benders.h:42
#define SCIP_EVENTTYPE_NODEFOCUSED
Definition: type_event.h:77
SCIP_Bool * subprobenabled
SCIP_Bool SCIPbendersGetMastervarsCont(SCIP_BENDERS *benders, int probnumber)
Definition: benders.c:4725
SCIP_Bool SCIPconsIsInitial(SCIP_CONS *cons)
Definition: cons.c:8245
SCIP_Bool SCIPisLPConstructed(SCIP *scip)
Definition: scip_lp.c:91
int SCIPsetCalcMemGrowSize(SCIP_SET *set, int num)
Definition: set.c:5503
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip_solve.c:2535
void SCIPbendersRemoveSubproblems(SCIP_BENDERS *benders)
Definition: benders.c:4397
SCIP_RETCODE SCIPsetHeuristics(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip_param.c:914
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:3078
#define BMSallocMemoryArray(ptr, num)
Definition: memory.h:113
SCIP_Bool benderscutssorted
SCIP_Real SCIPbendersGetSubproblemObjval(SCIP_BENDERS *benders, int probnumber)
Definition: benders.c:4448
void SCIPbendersSetFree(SCIP_BENDERS *benders, SCIP_DECL_BENDERSFREE((*bendersfree)))
Definition: benders.c:4079
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:95
#define SCIP_DECL_BENDERSINIT(x)
Definition: type_benders.h:91
void SCIPbendersSetExitpre(SCIP_BENDERS *benders, SCIP_DECL_BENDERSEXITPRE((*bendersexitpre)))
Definition: benders.c:4123
SCIP_Longint SCIPgetMemUsed(SCIP *scip)
Definition: scip_mem.c:90
#define SCIPdebugMessage
Definition: pub_message.h:77
SCIP_EXPORT SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
Definition: var.c:16857
SCIP_RETCODE SCIPbenderscutCopyInclude(SCIP_BENDERS *benders, SCIP_BENDERSCUT *benderscut, SCIP_SET *set)
Definition: benderscut.c:78
static SCIP_RETCODE addAuxiliaryVariablesToMaster(SCIP *scip, SCIP_BENDERS *benders)
Definition: benders.c:579
SCIP_Bool * indepsubprob
SCIP_Bool updateauxvarbound
internal methods for handling parameter settings
SCIP_Real SCIPbendersGetSubproblemLowerbound(SCIP_BENDERS *benders, int probnumber)
Definition: benders.c:4768
void SCIPclockEnableOrDisable(SCIP_CLOCK *clck, SCIP_Bool enable)
Definition: clock.c:250
static SCIP_RETCODE executeUserDefinedSolvesub(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_SOL *sol, int probnumber, SCIP_BENDERSSOLVELOOP solveloop, SCIP_Bool *infeasible, SCIP_Real *objective, SCIP_RESULT *result)
Definition: benders.c:2873
SCIP_RETCODE SCIPbendersCheckSubproblemOptimality(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_SOL *sol, int probnumber, SCIP_Bool *optimal)
Definition: benders.c:3656
SCIP_Bool SCIPinRepropagation(SCIP *scip)
Definition: scip_tree.c:135
#define BMSfreeMemory(ptr)
Definition: memory.h:135
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:78
void SCIPbendersSetInit(SCIP_BENDERS *benders, SCIP_DECL_BENDERSINIT((*bendersinit)))
Definition: benders.c:4090
void SCIPbendersSetExit(SCIP_BENDERS *benders, SCIP_DECL_BENDERSEXIT((*bendersexit)))
Definition: benders.c:4101
SCIP_Bool SCIPbendersCutRelaxation(SCIP_BENDERS *benders)
Definition: benders.c:4359
#define SCIPdebugMsg
Definition: scip_message.h:69
void SCIPbendersSetPostsolve(SCIP_BENDERS *benders, SCIP_DECL_BENDERSPOSTSOLVE((*benderspostsolve)))
Definition: benders.c:4189
SCIP_Real SCIPsetCeil(SCIP_SET *set, SCIP_Real val)
Definition: set.c:6151
#define MIPNODEFOCUS_EVENTHDLR_NAME
Definition: benders.c:61
SCIP_Bool subprobscreated
internal methods for LP management
SCIP_LPSOLSTAT SCIPgetLPSolstat(SCIP *scip)
Definition: scip_lp.c:158
void SCIPsetBendersPriority(SCIP *scip, SCIP_BENDERS *benders, int priority)
Definition: scip_benders.c:552
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
datastructures for Benders&#39; decomposition cuts techniques
SCIP_RETCODE SCIPgetCharParam(SCIP *scip, const char *name, char *value)
Definition: scip_param.c:316
#define BENDERS_MAXPSEUDOSOLS
Definition: benders.c:52
SCIP * scip
Definition: struct_set.h:64
static SCIP_RETCODE resetOrigSubproblemParams(SCIP *subproblem, SCIP_SUBPROBPARAMS *origparams)
Definition: benders.c:3417
void SCIPbendersSetFreesub(SCIP_BENDERS *benders, SCIP_DECL_BENDERSFREESUB((*bendersfreesub)))
Definition: benders.c:4200
SCIP_RETCODE SCIPbendersAddSubproblem(SCIP_BENDERS *benders, SCIP *subproblem)
Definition: benders.c:4379
SCIP ** subproblems
SCIP_RETCODE SCIPsetConsRemovable(SCIP *scip, SCIP_CONS *cons, SCIP_Bool removable)
Definition: scip_cons.c:1410
SCIP_RETCODE SCIPgetBendersMasterVar(SCIP *scip, SCIP_BENDERS *benders, SCIP_VAR *var, SCIP_VAR **mappedvar)
Definition: scip_benders.c:622
SCIP_RETCODE SCIPsetRealParam(SCIP *scip, const char *name, SCIP_Real value)
Definition: scip_param.c:612
SCIP_BENDERS * SCIPfindBenders(SCIP *scip, const char *name)
Definition: scip_benders.c:455
SCIP_EVENTHDLRDATA * SCIPeventhdlrGetData(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:324
const char * SCIPbenderscutGetName(SCIP_BENDERSCUT *benderscut)
Definition: benderscut.c:508
SCIP_Real SCIPbendersGetTime(SCIP_BENDERS *benders)
Definition: benders.c:4307
SCIP * SCIPbendersSubproblem(SCIP_BENDERS *benders, int probnumber)
Definition: benders.c:4265
SCIP_Real * subprobobjval
#define SCIP_DECL_BENDERSFREESUB(x)
Definition: type_benders.h:323
SCIP_RETCODE SCIPbenderscutGetAddedCutData(SCIP_BENDERSCUT *benderscut, int cutidx, SCIP_VAR ***vars, SCIP_Real **vals, SCIP_Real *lhs, SCIP_Real *rhs, int *nvars)
Definition: benderscut.c:609
SCIP_Bool lnscheck
SCIP_HASHMAPENTRY * SCIPhashmapGetEntry(SCIP_HASHMAP *hashmap, int entryidx)
Definition: misc.c:3366
void SCIPbendersSetSolvesubconvex(SCIP_BENDERS *benders, SCIP_DECL_BENDERSSOLVESUBCONVEX((*benderssolvesubconvex)))
Definition: benders.c:4167
SCIP_Real SCIPrelDiff(SCIP_Real val1, SCIP_Real val2)
Definition: misc.c:10571
SCIP_BENDERSDATA * SCIPbendersGetData(SCIP_BENDERS *benders)
Definition: benders.c:4047
SCIP_Bool SCIPbendersCutLP(SCIP_BENDERS *benders)
Definition: benders.c:4339
static SCIP_RETCODE createSubproblems(SCIP_BENDERS *benders, SCIP_SET *set)
Definition: benders.c:1179
void SCIPbendersSortBenderscutsName(SCIP_BENDERS *benders)
Definition: benders.c:4896
SCIP_RETCODE SCIPsetEventhdlrFree(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTFREE((*eventfree)))
Definition: scip_event.c:140
SCIP_EXPORT const char * SCIPvarGetName(SCIP_VAR *var)
Definition: var.c:16738
SCIP_RETCODE SCIPbendersInitsol(SCIP_BENDERS *benders, SCIP_SET *set)
Definition: benders.c:1738
#define BMSfreeMemoryArray(ptr)
Definition: memory.h:137
SCIP_RETCODE SCIPchgVarLb(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound)
Definition: scip_var.c:4614
static SCIP_RETCODE createMasterVarMapping(SCIP_BENDERS *benders, SCIP_SET *sourceset, SCIP_HASHMAP *varmap)
Definition: benders.c:737
int SCIPbendersGetNTransferredCuts(SCIP_BENDERS *benders)
Definition: benders.c:4737
internal methods for storing and manipulating the main problem
SCIP_RETCODE SCIPbendersFreeSubproblem(SCIP_BENDERS *benders, SCIP_SET *set, int probnumber)
Definition: benders.c:3606
#define SCIPerrorMessage
Definition: pub_message.h:45
SCIP * sourcescip
static SCIP_RETCODE copyMemoryAndTimeLimits(SCIP *scip, SCIP *subproblem)
Definition: benders.c:3313
static SCIP_RETCODE exitsolEventhandler(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTTYPE eventtype)
Definition: benders.c:123
SCIP_RETCODE SCIPreleaseRow(SCIP *scip, SCIP_ROW **row)
Definition: scip_lp.c:1406
void SCIPclockReset(SCIP_CLOCK *clck)
Definition: clock.c:199
SCIP_RETCODE SCIPbenderscutFree(SCIP_BENDERSCUT **benderscut, SCIP_SET *set)
Definition: benderscut.c:198
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip_event.c:276
#define SCIP_DECL_BENDERSEXIT(x)
Definition: type_benders.h:100
SCIP_VAR ** SCIPgetVars(SCIP *scip)
Definition: scip_prob.c:1942
#define SCIP_DECL_BENDERSSOLVESUB(x)
Definition: type_benders.h:265
void SCIPbendersSetExitsol(SCIP_BENDERS *benders, SCIP_DECL_BENDERSEXITSOL((*bendersexitsol)))
Definition: benders.c:4145
static SCIP_RETCODE freeEventhandler(SCIP *scip, SCIP_EVENTHDLR *eventhdlr)
Definition: benders.c:167
SCIP_Bool * subprobisconvex
int SCIPhashmapGetNEntries(SCIP_HASHMAP *hashmap)
Definition: misc.c:3358
void SCIPbendersSetPriority(SCIP_BENDERS *benders, SCIP_SET *set, int priority)
Definition: benders.c:4241
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip_mem.c:47
static SCIP_RETCODE initialiseLPSubproblem(SCIP_BENDERS *benders, SCIP_SET *set, int probnumber)
Definition: benders.c:1141
SCIP_RETCODE SCIPbendersSolveSubproblem(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_SOL *sol, int probnumber, SCIP_Bool *infeasible, SCIP_BENDERSENFOTYPE type, SCIP_Bool solvecip, SCIP_Real *objective)
Definition: benders.c:3218
static SCIP_RETCODE initEventhandlerData(SCIP *scip, SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: benders.c:85
int SCIPbendersGetNCutsFound(SCIP_BENDERS *benders)
Definition: benders.c:4287
SCIP_RETCODE SCIPbendersExitsol(SCIP_BENDERS *benders, SCIP_SET *set)
Definition: benders.c:1771
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
#define SCIP_DEFAULT_UPDATEAUXVARBOUND
Definition: benders.c:50
static SCIP_RETCODE generateBendersCuts(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_SOL *sol, SCIP_RESULT *result, SCIP_BENDERSENFOTYPE type, SCIP_BENDERSSOLVELOOP solveloop, SCIP_Bool checkint, int nchecked, SCIP_Bool *subprobsolved, SCIP_BENDERSSUBSTATUS *substatus, int **mergecands, int *npriomergecands, int *nmergecands, int *nsolveloops)
Definition: benders.c:2302
SCIP_Bool SCIPinProbing(SCIP *scip)
Definition: scip_probing.c:87
SCIP_Real SCIPclockGetTime(SCIP_CLOCK *clck)
Definition: clock.c:428
SCIP_Bool transfercuts
SCIP_Bool benderscutsnamessorted
SCIP_OBJSENSE SCIPgetObjsense(SCIP *scip)
Definition: scip_prob.c:1224
SCIP_RETCODE SCIPendProbing(SCIP *scip)
Definition: scip_probing.c:250
#define AUXILIARYVAR_NAME
Definition: benders.c:55
struct SCIP_BendersData SCIP_BENDERSDATA
Definition: type_benders.h:63
SCIP_RETCODE SCIPbendersSetMastervarsCont(SCIP_BENDERS *benders, int probnumber, SCIP_Bool arecont)
Definition: benders.c:4698
static SCIP_DECL_EVENTEXITSOL(eventExitsolBendersNodefocus)
Definition: benders.c:228
void SCIPbendersEnableOrDisableClocks(SCIP_BENDERS *benders, SCIP_Bool enable)
Definition: benders.c:4317
SCIP_BENDERSCUT * SCIPfindBenderscut(SCIP_BENDERS *benders, const char *name)
Definition: benders.c:4816
internal methods for global SCIP settings
#define SCIP_CALL(x)
Definition: def.h:365
SCIP_RETCODE SCIPsetPresolving(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip_param.c:940
#define SCIP_DECL_BENDERSSOLVESUBCONVEX(x)
Definition: type_benders.h:232
SCIP_RETCODE SCIPsetCharParam(SCIP *scip, const char *name, char value)
Definition: scip_param.c:670
SCIP_RETCODE SCIPbendersExec(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_SOL *sol, SCIP_RESULT *result, SCIP_Bool *infeasible, SCIP_Bool *auxviol, SCIP_BENDERSENFOTYPE type, SCIP_Bool checkint)
Definition: benders.c:2502
void SCIPbendersSetSubproblemIsConvex(SCIP_BENDERS *benders, int probnumber, SCIP_Bool isconvex)
Definition: benders.c:4466
internal methods for storing priced variables
SCIP_RETCODE SCIPsetAddIntParam(SCIP_SET *set, SCIP_MESSAGEHDLR *messagehdlr, BMS_BLKMEM *blkmem, const char *name, const char *desc, int *valueptr, SCIP_Bool isadvanced, int defaultvalue, int minvalue, int maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: set.c:2879
void SCIPbendersSetInitsol(SCIP_BENDERS *benders, SCIP_DECL_BENDERSINITSOL((*bendersinitsol)))
Definition: benders.c:4134
SCIP_EXPORT void SCIPsortPtr(void **ptrarray, SCIP_DECL_SORTPTRCOMP((*ptrcomp)), int len)
SCIP_Bool shareauxvars
SCIP_Bool SCIPsetIsFeasLE(SCIP_SET *set, SCIP_Real val1, SCIP_Real val2)
Definition: set.c:6395
SCIP_RETCODE SCIPbendersCreate(SCIP_BENDERS **benders, SCIP_SET *set, SCIP_MESSAGEHDLR *messagehdlr, BMS_BLKMEM *blkmem, const char *name, const char *desc, int priority, SCIP_Bool cutlp, SCIP_Bool cutpseudo, SCIP_Bool cutrelax, SCIP_Bool shareauxvars, SCIP_DECL_BENDERSCOPY((*benderscopy)), SCIP_DECL_BENDERSFREE((*bendersfree)), SCIP_DECL_BENDERSINIT((*bendersinit)), SCIP_DECL_BENDERSEXIT((*bendersexit)), SCIP_DECL_BENDERSINITPRE((*bendersinitpre)), SCIP_DECL_BENDERSEXITPRE((*bendersexitpre)), SCIP_DECL_BENDERSINITSOL((*bendersinitsol)), SCIP_DECL_BENDERSEXITSOL((*bendersexitsol)), SCIP_DECL_BENDERSGETVAR((*bendersgetvar)), SCIP_DECL_BENDERSCREATESUB((*benderscreatesub)), SCIP_DECL_BENDERSPRESUBSOLVE((*benderspresubsolve)), SCIP_DECL_BENDERSSOLVESUBCONVEX((*benderssolvesubconvex)), SCIP_DECL_BENDERSSOLVESUB((*benderssolvesub)), SCIP_DECL_BENDERSPOSTSOLVE((*benderspostsolve)), SCIP_DECL_BENDERSFREESUB((*bendersfreesub)), SCIP_BENDERSDATA *bendersdata)
Definition: benders.c:969
#define BMSduplicateMemoryArray(ptr, source, num)
Definition: memory.h:133
SCIP_RETCODE SCIPgetIntParam(SCIP *scip, const char *name, int *value)
Definition: scip_param.c:259
SCIP_RETCODE SCIPclockCreate(SCIP_CLOCK **clck, SCIP_CLOCKTYPE clocktype)
Definition: clock.c:160
SCIP_RETCODE SCIPgetRealParam(SCIP *scip, const char *name, SCIP_Real *value)
Definition: scip_param.c:297
SCIP_RETCODE SCIPbendersChgMastervarsToCont(SCIP_BENDERS *benders, SCIP_SET *set, int probnumber)
Definition: benders.c:4511
SCIP_Bool SCIPconsIsRemovable(SCIP_CONS *cons)
Definition: cons.c:8345
SCIP_Real SCIPbendersGetSetupTime(SCIP_BENDERS *benders)
Definition: benders.c:4297
SCIP_RETCODE SCIPsetParam(SCIP *scip, const char *name, void *value)
Definition: scip_param.c:393
#define SCIP_DEFAULT_LNSMAXDEPTH
Definition: benders.c:48
void SCIPbendersSetSubproblemIsIndependent(SCIP_BENDERS *benders, int probnumber, SCIP_Bool isindep)
Definition: benders.c:4606
#define SCIP_DECL_BENDERSCOPY(x)
Definition: type_benders.h:74
SCIP_RETCODE SCIPbendersInitpre(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_STAT *stat)
Definition: benders.c:1668
SCIP_Bool SCIPbendersIsActive(SCIP_BENDERS *benders)
Definition: benders.c:1931
SCIP_Real SCIPinfinity(SCIP *scip)
void SCIPbendersSetPresubsolve(SCIP_BENDERS *benders, SCIP_DECL_BENDERSPRESUBSOLVE((*benderspresubsolve)))
Definition: benders.c:4156
public data structures and miscellaneous methods
SCIP_Bool cutrelax
int SCIPgetDepth(SCIP *scip)
Definition: scip_tree.c:637
#define SCIP_Bool
Definition: def.h:70
SCIP_RETCODE SCIPbendersComputeSubproblemLowerbound(SCIP_BENDERS *benders, SCIP_SET *set, int probnumber, SCIP_Real *lowerbound, SCIP_Bool *infeasible)
Definition: benders.c:3709
SCIP_RETCODE SCIPconstructLP(SCIP *scip, SCIP_Bool *cutoff)
Definition: scip_lp.c:114
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition: misc.c:2891
void SCIPclockFree(SCIP_CLOCK **clck)
Definition: clock.c:175
SCIP_Bool SCIPbenderscutIsLPCut(SCIP_BENDERSCUT *benderscut)
Definition: benderscut.c:685
void SCIPbendersSetInitpre(SCIP_BENDERS *benders, SCIP_DECL_BENDERSINITPRE((*bendersinitpre)))
Definition: benders.c:4112
#define MIN(x, y)
Definition: def.h:223
SCIP_RETCODE SCIPcreateEmptyRowCons(SCIP *scip, SCIP_ROW **row, SCIP_CONSHDLR *conshdlr, const char *name, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool removable)
Definition: scip_lp.c:1268
#define SCIPsetDebugMsg
Definition: set.h:1720
SCIP_RETCODE SCIPbendersFree(SCIP_BENDERS **benders, SCIP_SET *set)
Definition: benders.c:1048
SCIP_EXPORT void SCIPvarSetData(SCIP_VAR *var, SCIP_VARDATA *vardata)
Definition: var.c:16768
static SCIP_RETCODE setSubproblemParams(SCIP *scip, SCIP *subproblem)
Definition: benders.c:3373
data structures required for Benders&#39; decomposition
static SCIP_RETCODE createAndAddTransferredCut(SCIP *sourcescip, SCIP_BENDERS *benders, SCIP_VAR **vars, SCIP_Real *vals, SCIP_Real lhs, SCIP_Real rhs, int nvars)
Definition: benders.c:1382
SCIP_RETCODE SCIPbenderscutInitsol(SCIP_BENDERSCUT *benderscut, SCIP_SET *set)
Definition: benderscut.c:314
#define SCIP_DEFAULT_TRANSFERCUTS
Definition: benders.c:45
SCIP_RETCODE SCIPbendersMergeSubproblemIntoMaster(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, int probnumber)
Definition: benders.c:3838
int SCIPbendersGetNConvexSubproblems(SCIP_BENDERS *benders)
Definition: benders.c:4501
SCIP_RETCODE SCIPsetEventhdlrExitsol(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXITSOL((*eventexitsol)))
Definition: scip_event.c:196
SCIP_Bool SCIPconsIsSeparated(SCIP_CONS *cons)
Definition: cons.c:8255
static SCIP_RETCODE transferBendersCuts(SCIP *sourcescip, SCIP *subscip, SCIP_BENDERS *benders)
Definition: benders.c:1498
SCIP_Bool SCIPbendersOnlyCheckConvexRelax(SCIP_BENDERS *benders)
Definition: benders.c:2000
SCIP_Real SCIPbendersGetAuxiliaryVarVal(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_SOL *sol, int probnumber)
Definition: benders.c:3688
static void resetSubproblemObjectiveValue(SCIP_BENDERS *benders)
Definition: benders.c:689
Constraint handler for linear constraints in their most general form, .
SCIP_EXPORT SCIP_Real SCIPvarGetLbOriginal(SCIP_VAR *var)
Definition: var.c:17298
SCIP_RETCODE SCIPbendersActivate(SCIP_BENDERS *benders, SCIP_SET *set, int nsubproblems)
Definition: benders.c:1827
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
Definition: scip_prob.c:1667
#define UPPERBOUND_EVENTHDLR_NAME
Definition: benders.c:64
#define BMSclearMemory(ptr)
Definition: memory.h:119
static SCIP_RETCODE assignAuxiliaryVariables(SCIP *scip, SCIP_BENDERS *benders)
Definition: benders.c:636
SCIP_RETCODE SCIPchgVarUb(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound)
Definition: scip_var.c:4704
SCIP_Bool SCIPconsIsDynamic(SCIP_CONS *cons)
Definition: cons.c:8335
SCIP_EXPORT SCIP_Real SCIPvarGetLbLocal(SCIP_VAR *var)
Definition: var.c:17408
#define SCIP_MAXTREEDEPTH
Definition: def.h:301
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:94
SCIP_Bool cutpseudo
SCIP_RETCODE SCIPbendersGetVar(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_VAR *var, SCIP_VAR **mappedvar, int probnumber)
Definition: benders.c:4021
#define MIPNODEFOCUS_EVENTHDLR_DESC
Definition: benders.c:62
SCIP_VAR * SCIPfindVar(SCIP *scip, const char *name)
Definition: scip_prob.c:2680
static SCIP_RETCODE exitEventhandler(SCIP *scip, SCIP_EVENTHDLR *eventhdlr)
Definition: benders.c:147
#define SCIP_EVENTTYPE_NODESOLVED
Definition: type_event.h:119
int SCIPparamGetInt(SCIP_PARAM *param)
Definition: paramset.c:716
SCIP_RETCODE SCIPsetGetRealParam(SCIP_SET *set, const char *name, SCIP_Real *value)
Definition: set.c:3060
SCIP_Bool SCIPinDive(SCIP *scip)
Definition: scip_lp.c:2594
void SCIPbendersSetSubproblemIsSetup(SCIP_BENDERS *benders, int probnumber, SCIP_Bool issetup)
Definition: benders.c:4581
SCIP_EXPORT SCIP_Real SCIPvarGetUbLocal(SCIP_VAR *var)
Definition: var.c:17418
SCIP_RETCODE SCIPsetLongintParam(SCIP *scip, const char *name, SCIP_Longint value)
Definition: scip_param.c:554
SCIP_RETCODE SCIPbendersExecSubproblemSolve(SCIP_BENDERS *benders, SCIP_SET *set, SCIP_SOL *sol, int probnumber, SCIP_BENDERSSOLVELOOP solveloop, SCIP_Bool enhancement, SCIP_Bool *solved, SCIP_Bool *infeasible, SCIP_BENDERSENFOTYPE type)
Definition: benders.c:2944
#define SCIP_EVENTTYPE_BESTSOLFOUND
Definition: type_event.h:88
#define MAX(x, y)
Definition: def.h:222
static SCIP_RETCODE doBendersCreate(SCIP_BENDERS **benders, SCIP_SET *set, SCIP_MESSAGEHDLR *messagehdlr, BMS_BLKMEM *blkmem, const char *name, const char *desc, int priority, SCIP_Bool cutlp, SCIP_Bool cutpseudo, SCIP_Bool cutrelax, SCIP_Bool shareauxvars, SCIP_DECL_BENDERSCOPY((*benderscopy)), SCIP_DECL_BENDERSFREE((*bendersfree)), SCIP_DECL_BENDERSINIT((*bendersinit)), SCIP_DECL_BENDERSEXIT((*bendersexit)), SCIP_DECL_BENDERSINITPRE((*bendersinitpre)), SCIP_DECL_BENDERSEXITPRE((*bendersexitpre)), SCIP_DECL_BENDERSINITSOL((*bendersinitsol)), SCIP_DECL_BENDERSEXITSOL((*bendersexitsol)), SCIP_DECL_BENDERSGETVAR((*bendersgetvar)), SCIP_DECL_BENDERSCREATESUB((*benderscreatesub)), SCIP_DECL_BENDERSPRESUBSOLVE((*benderspresubsolve)), SCIP_DECL_BENDERSSOLVESUBCONVEX((*benderssolvesubconvex)), SCIP_DECL_BENDERSSOLVESUB((*benderssolvesub)), SCIP_DECL_BENDERSPOSTSOLVE((*benderspostsolve)), SCIP_DECL_BENDERSFREESUB((*bendersfreesub)), SCIP_BENDERSDATA *bendersdata)
Definition: benders.c:836
SCIP_Bool * mastervarscont
int SCIPbendersGetNBenderscuts(SCIP_BENDERS *benders)
Definition: benders.c:4855
enum SCIP_BendersSolveLoop SCIP_BENDERSSOLVELOOP
Definition: type_benders.h:51
SCIP_BENDERS * SCIPsetFindBenders(SCIP_SET *set, const char *name)
Definition: set.c:3691
SCIP_RETCODE SCIPchgVarObj(SCIP *scip, SCIP_VAR *var, SCIP_Real newobj)
Definition: scip_var.c:4451
void SCIPbendersSortBenderscuts(SCIP_BENDERS *benders)
Definition: benders.c:4881
SCIP_Bool SCIPconsIsEnforced(SCIP_CONS *cons)
Definition: cons.c:8265
#define SCIP_DECL_BENDERSEXITSOL(x)
Definition: type_benders.h:141
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip_event.c:310
static SCIP_RETCODE checkSubproblemIndependence(SCIP *scip, SCIP_BENDERS *benders)
Definition: benders.c:1612
void SCIPbendersSetBenderscutsSorted(SCIP_BENDERS *benders, SCIP_Bool sorted)
Definition: benders.c:4780
SCIP_EXPORT SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:17352
static SCIP_RETCODE releaseVarMappingHashmapVars(SCIP *scip, SCIP_BENDERS *benders)
Definition: benders.c:1014
SCIP_RETCODE SCIPbenderscutExit(SCIP_BENDERSCUT *benderscut, SCIP_SET *set)
Definition: benderscut.c:270
SCIP_Bool active
SCIP_RETCODE SCIPaddVarToRow(SCIP *scip, SCIP_ROW *row, SCIP_VAR *var, SCIP_Real val)
Definition: scip_lp.c:1539
SCIP_RETCODE SCIPstartProbing(SCIP *scip)
Definition: scip_probing.c:109
const char * SCIPbendersGetDesc(SCIP_BENDERS *benders)
Definition: benders.c:4221
public methods for message output
SCIP_RETCODE SCIPgetVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition: scip_prob.c:1861
SCIP_Real SCIPgetSolTransObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip_sol.c:1482
static SCIP_RETCODE updateEventhdlrUpperbound(SCIP_BENDERS *benders, int probnumber, SCIP_Real upperbound)
Definition: benders.c:426
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10263
SCIP_Bool * subprobsetup
SCIP_Bool SCIPbendersSubproblemIsSetup(SCIP_BENDERS *benders, int probnumber)
Definition: benders.c:4594
SCIP_Bool SCIPsetIsGT(SCIP_SET *set, SCIP_Real val1, SCIP_Real val2)
Definition: set.c:6029
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition: misc.c:2925
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition: scip_var.c:1251
#define SCIP_Real
Definition: def.h:164
SCIP_VAR ** SCIPbendersGetAuxiliaryVars(SCIP_BENDERS *benders)
Definition: benders.c:4421
#define NODEFOCUS_EVENTHDLR_DESC
Definition: benders.c:59
SCIP_RETCODE SCIPgetLongintParam(SCIP *scip, const char *name, SCIP_Longint *value)
Definition: scip_param.c:278
SCIP_CONSHDLR * SCIPconsGetHdlr(SCIP_CONS *cons)
Definition: cons.c:8096
#define NODEFOCUS_EVENTHDLR_NAME
Definition: benders.c:58
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
#define NODESOLVED_EVENTHDLR_NAME
Definition: benders.c:67
SCIP_RETCODE SCIPbendersInit(SCIP_BENDERS *benders, SCIP_SET *set)
Definition: benders.c:1323
SCIP_EXPORT SCIP_RETCODE SCIPvarGetOrigvarSum(SCIP_VAR **var, SCIP_Real *scalar, SCIP_Real *constant)
Definition: var.c:12268
#define BMSallocMemory(ptr)
Definition: memory.h:109
#define BMSreallocMemoryArray(ptr, num)
Definition: memory.h:117
SCIP_Real SCIPgetPrimalbound(SCIP *scip)
SCIP_Bool SCIPbendersIsInitialized(SCIP_BENDERS *benders)
Definition: benders.c:4329
#define SCIP_DEFAULT_LNSCHECK
Definition: benders.c:47
#define SCIP_Longint
Definition: def.h:149
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip_param.c:438
SCIP_Bool iscopy
SCIP_Bool cutlp
#define SCIP_DECL_BENDERSPOSTSOLVE(x)
Definition: type_benders.h:301
void SCIPbendersSetCopy(SCIP_BENDERS *benders, SCIP_DECL_BENDERSCOPY((*benderscopy)))
Definition: benders.c:4068
#define SCIP_DECL_BENDERSEXITPRE(x)
Definition: type_benders.h:119
SCIP_STAGE SCIPsetGetStage(SCIP_SET *set)
Definition: set.c:2847
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip_prob.c:2765
SCIP_RETCODE SCIPaddPoolCut(SCIP *scip, SCIP_ROW *row)
Definition: scip_cut.c:331
static SCIP_DECL_EVENTFREE(eventFreeBendersNodefocus)
Definition: benders.c:254
SCIP_RETCODE SCIPbenderscutExitsol(SCIP_BENDERSCUT *benderscut, SCIP_SET *set)
Definition: benderscut.c:338
static SCIP_DECL_PARAMCHGD(paramChgdBendersPriority)
Definition: benders.c:722
static SCIP_RETCODE storeOrigSubproblemParams(SCIP *subproblem, SCIP_SUBPROBPARAMS *origparams)
Definition: benders.c:3346
#define BMSclearMemoryArray(ptr, num)
Definition: memory.h:120
const char * SCIPbendersGetName(SCIP_BENDERS *benders)
Definition: benders.c:4211
SCIP_Longint SCIPgetMemExternEstim(SCIP *scip)
Definition: scip_mem.c:116
common defines and data types used in all packages of SCIP
SCIP_EXPORT SCIP_Real SCIPvarGetUbOriginal(SCIP_VAR *var)
Definition: var.c:17318
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:198
struct BMS_BlkMem BMS_BLKMEM
Definition: memory.h:427
SCIP_RETCODE SCIPbendersSetBenderscutPriority(SCIP_BENDERS *benders, SCIP_BENDERSCUT *benderscut, int priority)
Definition: benders.c:4865
SCIP_RETCODE SCIPbendersSolveSubproblemCIP(SCIP *scip, SCIP_BENDERS *benders, int probnumber, SCIP_Bool *infeasible, SCIP_BENDERSENFOTYPE type, SCIP_Bool solvecip)
Definition: benders.c:3505
SCIP_RETCODE SCIPsetAddRealParam(SCIP_SET *set, SCIP_MESSAGEHDLR *messagehdlr, BMS_BLKMEM *blkmem, 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: set.c:2927
static SCIP_DECL_EVENTINITSOL(eventInitsolBendersNodefocus)
Definition: benders.c:215
enum SCIP_BendersSubStatus SCIP_BENDERSSUBSTATUS
Definition: type_benders.h:60
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip_cons.c:1109
SCIP_VAR ** auxiliaryvars
SCIP_RETCODE SCIPchgVarType(SCIP *scip, SCIP_VAR *var, SCIP_VARTYPE vartype, SCIP_Bool *infeasible)
Definition: scip_var.c:8084
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip_general.c:355
SCIP_BENDERSCUT ** SCIPbendersGetBenderscuts(SCIP_BENDERS *benders)
Definition: benders.c:4838
#define SCIP_ALLOC(x)
Definition: def.h:376
#define SCIPABORT()
Definition: def.h:337
int SCIPbendersGetNCalls(SCIP_BENDERS *benders)
Definition: benders.c:4277
SCIP_Real SCIPgetDualbound(SCIP *scip)
SCIP_RETCODE SCIPsetAddBoolParam(SCIP_SET *set, SCIP_MESSAGEHDLR *messagehdlr, BMS_BLKMEM *blkmem, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: set.c:2857
SCIP_Bool initialized
int SCIPbendersGetPriority(SCIP_BENDERS *benders)
Definition: benders.c:4231
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip_sol.c:2304
SCIP_Bool SCIPconsIsPropagated(SCIP_CONS *cons)
Definition: cons.c:8295
#define SCIP_DEFAULT_SUBPROBFRAC
Definition: benders.c:49
SCIP_Real * subproblowerbound
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip_param.c:496
SCIP_BENDERSCUT ** benderscuts
internal methods for Benders&#39; decomposition cuts
void SCIPbendersSetSubproblemObjval(SCIP_BENDERS *benders, int probnumber, SCIP_Real objval)
Definition: benders.c:4431
static SCIP_RETCODE initialiseSubproblem(SCIP_BENDERS *benders, SCIP_SET *set, int probnumber, SCIP_Bool *success)
Definition: benders.c:1099
SCIP_RETCODE SCIPbenderscutInit(SCIP_BENDERSCUT *benderscut, SCIP_SET *set)
Definition: benderscut.c:224
SCIP_Real SCIPparamGetRealMax(SCIP_PARAM *param)
Definition: paramset.c:835
SCIP callable library.
void SCIPbendersSetSubproblemEnabled(SCIP_BENDERS *benders, int probnumber, SCIP_Bool enabled)
Definition: benders.c:4660
#define SCIP_DECL_BENDERSPRESUBSOLVE(x)
Definition: type_benders.h:192
SCIP_RETCODE SCIPsolveProbingLP(SCIP *scip, int itlim, SCIP_Bool *lperror, SCIP_Bool *cutoff)
Definition: scip_probing.c:801
SCIP_Bool SCIPconsIsModifiable(SCIP_CONS *cons)
Definition: cons.c:8325
SCIP_RETCODE SCIPsetEventhdlrExit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXIT((*eventexit)))
Definition: scip_event.c:168
SCIP_RETCODE SCIPrestartSolve(SCIP *scip)
Definition: scip_solve.c:3425
SCIP_Bool SCIPbendersSubproblemIsConvex(SCIP_BENDERS *benders, int probnumber)
Definition: benders.c:4489
uint64_t SCIP_EVENTTYPE
Definition: type_event.h:134
SCIP_Real subprobfrac
SCIP_RETCODE SCIPbendersSolveSubproblemLP(SCIP *scip, SCIP_BENDERS *benders, int probnumber, SCIP_Bool *infeasible)
Definition: benders.c:3446
SCIP_RETCODE SCIPfreeTransform(SCIP *scip)
Definition: scip_solve.c:3327
SCIP_RETCODE SCIPchgVarObjProbing(SCIP *scip, SCIP_VAR *var, SCIP_Real newobj)
Definition: scip_probing.c:464
SCIP_Real SCIPgetSolOrigObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip_sol.c:1435
SCIP_CLOCK * setuptime