Scippy

SCIP

Solving Constraint Integer Programs

branch.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2014 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file branch.c
17  * @brief methods for branching rules and branching candidate storage
18  * @author Tobias Achterberg
19  * @author Timo Berthold
20  * @author Gerald Gamrath
21  * @author Stefan Heinz
22  * @author Michael Winkler
23  * @author Stefan Vigerske
24  */
25 
26 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
27 
28 #include <assert.h>
29 #include <string.h>
30 
31 #include "scip/def.h"
32 #include "blockmemshell/memory.h"
33 #include "scip/set.h"
34 #include "scip/stat.h"
35 #include "scip/clock.h"
36 #include "scip/paramset.h"
37 #include "scip/event.h"
38 #include "scip/lp.h"
39 #include "scip/var.h"
40 #include "scip/prob.h"
41 #include "scip/tree.h"
42 #include "scip/sepastore.h"
43 #include "scip/scip.h"
44 #include "scip/branch.h"
45 
46 #include "scip/struct_branch.h"
47 
48 /*
49  * memory growing methods for dynamically allocated arrays
50  */
51 
52 /** ensures, that lpcands array can store at least num entries */
53 static
55  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
56  SCIP_SET* set, /**< global SCIP settings */
57  int num /**< minimum number of entries to store */
58  )
59 {
60  assert(branchcand->nlpcands <= branchcand->lpcandssize);
61 
62  if( num > branchcand->lpcandssize )
63  {
64  int newsize;
65 
66  newsize = SCIPsetCalcMemGrowSize(set, num);
67  SCIP_ALLOC( BMSreallocMemoryArray(&branchcand->lpcands, newsize) );
68  SCIP_ALLOC( BMSreallocMemoryArray(&branchcand->lpcandssol, newsize) );
69  SCIP_ALLOC( BMSreallocMemoryArray(&branchcand->lpcandsfrac, newsize) );
70  branchcand->lpcandssize = newsize;
71  }
72  assert(num <= branchcand->lpcandssize);
73 
74  return SCIP_OKAY;
75 }
76 
77 /** ensures, that pseudocands array can store at least num entries */
78 static
80  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
81  SCIP_SET* set, /**< global SCIP settings */
82  int num /**< minimum number of entries to store */
83  )
84 {
85  assert(branchcand->npseudocands <= branchcand->pseudocandssize);
86 
87  if( num > branchcand->pseudocandssize )
88  {
89  int newsize;
90 
91  newsize = SCIPsetCalcMemGrowSize(set, num);
92  SCIP_ALLOC( BMSreallocMemoryArray(&branchcand->pseudocands, newsize) );
93  branchcand->pseudocandssize = newsize;
94  }
95  assert(num <= branchcand->pseudocandssize);
96 
97  return SCIP_OKAY;
98 }
99 
100 /** ensures, that externcands array can store at least num entries */
101 static
103  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
104  SCIP_SET* set, /**< global SCIP settings */
105  int num /**< minimum number of entries to store */
106  )
107 {
108  assert(branchcand->nexterncands <= branchcand->externcandssize);
109 
110  if( num > branchcand->externcandssize )
111  {
112  int newsize;
113 
114  newsize = SCIPsetCalcMemGrowSize(set, num);
115  SCIP_ALLOC( BMSreallocMemoryArray(&branchcand->externcands, newsize) );
116  SCIP_ALLOC( BMSreallocMemoryArray(&branchcand->externcandsscore, newsize) );
117  SCIP_ALLOC( BMSreallocMemoryArray(&branchcand->externcandssol, newsize) );
118  branchcand->externcandssize = newsize;
119  }
120  assert(num <= branchcand->externcandssize);
121 
122  return SCIP_OKAY;
123 }
124 
125 
126 
127 /*
128  * branching candidate storage methods
129  */
130 
131 /** creates a branching candidate storage */
133  SCIP_BRANCHCAND** branchcand /**< pointer to store branching candidate storage */
134  )
135 {
136  assert(branchcand != NULL);
137 
138  SCIP_ALLOC( BMSallocMemory(branchcand) );
139  (*branchcand)->lpcands = NULL;
140  (*branchcand)->lpcandssol = NULL;
141  (*branchcand)->lpcandsfrac = NULL;
142  (*branchcand)->externcands = NULL;
143  (*branchcand)->externcandssol = NULL;
144  (*branchcand)->externcandsscore = NULL;
145  (*branchcand)->pseudocands = NULL;
146  (*branchcand)->lpcandssize = 0;
147  (*branchcand)->nlpcands = 0;
148  (*branchcand)->nimpllpfracs = 0;
149  (*branchcand)->npriolpcands = 0;
150  (*branchcand)->npriolpbins = 0;
151  (*branchcand)->lpmaxpriority = INT_MIN;
152  (*branchcand)->externcandssize = 0;
153  (*branchcand)->nexterncands = 0;
154  (*branchcand)->nprioexterncands = 0;
155  (*branchcand)->nprioexternbins = 0;
156  (*branchcand)->nprioexternints = 0;
157  (*branchcand)->nprioexternimpls = 0;
158  (*branchcand)->externmaxpriority = INT_MIN;
159  (*branchcand)->pseudocandssize = 0;
160  (*branchcand)->npseudocands = 0;
161  (*branchcand)->npriopseudocands = 0;
162  (*branchcand)->npriopseudobins = 0;
163  (*branchcand)->npriopseudoints = 0;
164  (*branchcand)->pseudomaxpriority = INT_MIN;
165  (*branchcand)->validlpcandslp = -1;
166 
167  return SCIP_OKAY;
168 }
169 
170 /** frees branching candidate storage */
172  SCIP_BRANCHCAND** branchcand /**< pointer to store branching candidate storage */
173  )
174 {
175  assert(branchcand != NULL);
176 
177  BMSfreeMemoryArrayNull(&(*branchcand)->lpcands);
178  BMSfreeMemoryArrayNull(&(*branchcand)->lpcandssol);
179  BMSfreeMemoryArrayNull(&(*branchcand)->lpcandsfrac);
180  BMSfreeMemoryArrayNull(&(*branchcand)->pseudocands);
181  BMSfreeMemoryArrayNull(&(*branchcand)->externcands);
182  BMSfreeMemoryArrayNull(&(*branchcand)->externcandsscore);
183  BMSfreeMemoryArrayNull(&(*branchcand)->externcandssol);
184  BMSfreeMemory(branchcand);
185 
186  return SCIP_OKAY;
187 }
188 
189 /** calculates branching candidates for LP solution branching (fractional variables) */
190 static
192  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
193  SCIP_SET* set, /**< global SCIP settings */
194  SCIP_STAT* stat, /**< problem statistics */
195  SCIP_LP* lp /**< current LP data */
196  )
197 {
198  assert(branchcand != NULL);
199  assert(stat != NULL);
200  assert(branchcand->validlpcandslp <= stat->lpcount);
201  assert(lp != NULL);
202  assert(lp->solved);
204 
205  SCIPdebugMessage("calculating LP branching candidates: validlp=%"SCIP_LONGINT_FORMAT", lpcount=%"SCIP_LONGINT_FORMAT"\n",
206  branchcand->validlpcandslp, stat->lpcount);
207 
209  {
210  branchcand->lpmaxpriority = INT_MIN / 2;
211  branchcand->nlpcands = 0;
212  branchcand->npriolpcands = 0;
213  branchcand->npriolpbins = 0;
214  branchcand->nimpllpfracs = 0;
215  branchcand->validlpcandslp = stat->lpcount;
216 
217  SCIPdebugMessage(" LP is unbounded -> no branching candidates\n");
218  return SCIP_OKAY;
219  }
220 
221  /* check, if the current LP branching candidate array is invalid */
222  if( branchcand->validlpcandslp < stat->lpcount )
223  {
224  SCIP_COL** cols;
225  SCIP_VAR* var;
226  SCIP_COL* col;
227  SCIP_Real primsol;
228  SCIP_Real frac;
229  SCIP_VARTYPE vartype;
230  int branchpriority;
231  int ncols;
232  int c;
233  int insertpos;
234 
235  SCIPdebugMessage(" -> recalculating LP branching candidates\n");
236 
237  cols = SCIPlpGetCols(lp);
238  ncols = SCIPlpGetNCols(lp);
239 
240  /* construct the LP branching candidate set, moving the candidates with maximal priority to the front */
241  SCIP_CALL( ensureLpcandsSize(branchcand, set, ncols) );
242 
243  branchcand->lpmaxpriority = INT_MIN / 2;
244  branchcand->nlpcands = 0;
245  branchcand->nimpllpfracs = 0;
246  branchcand->npriolpcands = 0;
247  branchcand->npriolpbins = 0;
248  for( c = 0; c < ncols; ++c )
249  {
250  col = cols[c];
251  assert(col != NULL);
252  assert(col->lppos == c);
253  assert(col->lpipos >= 0);
254 
255  primsol = SCIPcolGetPrimsol(col);
256  assert(primsol < SCIP_INVALID);
257  assert(SCIPsetIsFeasGE(set, primsol, col->lb));
258  assert(SCIPsetIsFeasLE(set, primsol, col->ub));
259 
260  var = col->var;
261  assert(var != NULL);
262  assert(SCIPvarGetStatus(var) == SCIP_VARSTATUS_COLUMN);
263  assert(SCIPvarGetCol(var) == col);
264 
265  /* LP branching candidates are fractional binary and integer variables; implicit variables are kept at the end
266  * of the candidates array for some rounding heuristics
267  */
268  vartype = SCIPvarGetType(var);
269  if( vartype == SCIP_VARTYPE_CONTINUOUS )
270  continue;
271 
272  /* ignore fixed variables (due to numerics, it is possible, that the LP solution of a fixed integer variable
273  * (with large fixed value) is fractional in terms of absolute feasibility measure)
274  */
275  if( SCIPvarGetLbLocal(var) >= SCIPvarGetUbLocal(var) - 0.5 )
276  continue;
277 
278  /* check, if the LP solution value is fractional */
279  frac = SCIPsetFeasFrac(set, primsol);
280  if( SCIPsetIsFeasFracIntegral(set, frac) )
281  continue;
282 
283  /* insert candidate in candidate list */
284  branchpriority = SCIPvarGetBranchPriority(var);
285  insertpos = branchcand->nlpcands + branchcand->nimpllpfracs;
286  assert(insertpos < branchcand->lpcandssize);
287 
288  if( vartype == SCIP_VARTYPE_IMPLINT )
289  branchpriority = INT_MIN;
290 
291  assert(vartype == SCIP_VARTYPE_IMPLINT || branchpriority >= INT_MIN/2);
292  /* ensure that implicit variables are stored at the end of the array */
293  if( vartype != SCIP_VARTYPE_IMPLINT && branchcand->nimpllpfracs > 0 )
294  {
295  assert(branchcand->lpcands[branchcand->nlpcands] != NULL
296  && SCIPvarGetType(branchcand->lpcands[branchcand->nlpcands]) == SCIP_VARTYPE_IMPLINT );
297 
298  branchcand->lpcands[insertpos] = branchcand->lpcands[branchcand->nlpcands];
299  branchcand->lpcandssol[insertpos] = branchcand->lpcandssol[branchcand->nlpcands];
300  branchcand->lpcandsfrac[insertpos] = branchcand->lpcandsfrac[branchcand->nlpcands];
301 
302  insertpos = branchcand->nlpcands;
303  }
304 
305  if( branchpriority > branchcand->lpmaxpriority )
306  {
307  /* candidate has higher priority than the current maximum:
308  * move it to the front and declare it to be the single best candidate
309  */
310  if( insertpos != 0 )
311  {
312  branchcand->lpcands[insertpos] = branchcand->lpcands[0];
313  branchcand->lpcandssol[insertpos] = branchcand->lpcandssol[0];
314  branchcand->lpcandsfrac[insertpos] = branchcand->lpcandsfrac[0];
315  insertpos = 0;
316  }
317  branchcand->npriolpcands = 1;
318  branchcand->npriolpbins = (vartype == SCIP_VARTYPE_BINARY ? 1 : 0);
319  branchcand->lpmaxpriority = branchpriority;
320  }
321  else if( branchpriority == branchcand->lpmaxpriority )
322  {
323  /* candidate has equal priority as the current maximum:
324  * move away the first non-maximal priority candidate, move the current candidate to the correct
325  * slot (binaries first) and increase the number of maximal priority candidates
326  */
327  if( insertpos != branchcand->npriolpcands )
328  {
329  branchcand->lpcands[insertpos] = branchcand->lpcands[branchcand->npriolpcands];
330  branchcand->lpcandssol[insertpos] = branchcand->lpcandssol[branchcand->npriolpcands];
331  branchcand->lpcandsfrac[insertpos] = branchcand->lpcandsfrac[branchcand->npriolpcands];
332  insertpos = branchcand->npriolpcands;
333  }
334  branchcand->npriolpcands++;
335  if( vartype == SCIP_VARTYPE_BINARY )
336  {
337  if( insertpos != branchcand->npriolpbins )
338  {
339  branchcand->lpcands[insertpos] = branchcand->lpcands[branchcand->npriolpbins];
340  branchcand->lpcandssol[insertpos] = branchcand->lpcandssol[branchcand->npriolpbins];
341  branchcand->lpcandsfrac[insertpos] = branchcand->lpcandsfrac[branchcand->npriolpbins];
342  insertpos = branchcand->npriolpbins;
343  }
344  branchcand->npriolpbins++;
345  }
346  }
347  /* insert variable at the correct position of the candidates storage */
348  branchcand->lpcands[insertpos] = var;
349  branchcand->lpcandssol[insertpos] = primsol;
350  branchcand->lpcandsfrac[insertpos] = frac;
351 
352  /* increase the counter depending on the variable type */
353  if( vartype != SCIP_VARTYPE_IMPLINT )
354  branchcand->nlpcands++;
355  else
356  branchcand->nimpllpfracs++;
357 
358  SCIPdebugMessage(" -> candidate %d: var=<%s>, sol=%g, frac=%g, prio=%d (max: %d) -> pos %d\n",
359  branchcand->nlpcands, SCIPvarGetName(var), primsol, frac, branchpriority, branchcand->lpmaxpriority,
360  insertpos);
361  }
362 
363 #ifndef NDEBUG
364  /* in debug mode we assert that the variables are positioned correctly (binaries and integers first,
365  * implicit integers last)
366  */
367  for( c = 0; c < branchcand->nlpcands + branchcand->nimpllpfracs; ++c )
368  {
369  assert(c >= branchcand->nlpcands || SCIPvarGetType(branchcand->lpcands[c]) != SCIP_VARTYPE_IMPLINT);
370  assert(c < branchcand->nlpcands || SCIPvarGetType(branchcand->lpcands[c]) == SCIP_VARTYPE_IMPLINT);
371  }
372 #endif
373 
374  branchcand->validlpcandslp = stat->lpcount;
375  }
376  assert(0 <= branchcand->npriolpcands && branchcand->npriolpcands <= branchcand->nlpcands);
377 
378  SCIPdebugMessage(" -> %d fractional variables (%d of maximal priority)\n", branchcand->nlpcands, branchcand->npriolpcands);
379 
380  return SCIP_OKAY;
381 }
382 
383 /** gets branching candidates for LP solution branching (fractional variables) */
385  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
386  SCIP_SET* set, /**< global SCIP settings */
387  SCIP_STAT* stat, /**< problem statistics */
388  SCIP_LP* lp, /**< current LP data */
389  SCIP_VAR*** lpcands, /**< pointer to store the array of LP branching candidates, or NULL */
390  SCIP_Real** lpcandssol, /**< pointer to store the array of LP candidate solution values, or NULL */
391  SCIP_Real** lpcandsfrac, /**< pointer to store the array of LP candidate fractionalities, or NULL */
392  int* nlpcands, /**< pointer to store the number of LP branching candidates, or NULL */
393  int* npriolpcands, /**< pointer to store the number of candidates with maximal priority, or NULL */
394  int* nfracimplvars /**< pointer to store the number of implicit fractional variables, or NULL */
395  )
396 {
397  /* calculate branching candidates */
398  SCIP_CALL( branchcandCalcLPCands(branchcand, set, stat, lp) );
399 
400  /* assign return values */
401  if( lpcands != NULL )
402  *lpcands = branchcand->lpcands;
403  if( lpcandssol != NULL )
404  *lpcandssol = branchcand->lpcandssol;
405  if( lpcandsfrac != NULL )
406  *lpcandsfrac = branchcand->lpcandsfrac;
407  if( nlpcands != NULL )
408  *nlpcands = branchcand->nlpcands;
409  if( npriolpcands != NULL )
410  *npriolpcands = (set->branch_preferbinary && branchcand->npriolpbins > 0 ? branchcand->npriolpbins
411  : branchcand->npriolpcands);
412  if( nfracimplvars != NULL )
413  *nfracimplvars = branchcand->nimpllpfracs;
414 
415  return SCIP_OKAY;
416 }
417 
418 /** gets external branching candidates */
420  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
421  SCIP_VAR*** externcands, /**< pointer to store the array of external branching candidates, or NULL */
422  SCIP_Real** externcandssol, /**< pointer to store the array of external candidate solution values, or NULL */
423  SCIP_Real** externcandsscore, /**< pointer to store the array of external candidate scores, or NULL */
424  int* nexterncands, /**< pointer to store the number of external branching candidates, or NULL */
425  int* nprioexterncands, /**< pointer to store the number of candidates with maximal priority, or NULL */
426  int* nprioexternbins, /**< pointer to store the number of binary candidates with maximal priority, or NULL */
427  int* nprioexternints, /**< pointer to store the number of integer candidates with maximal priority, or NULL */
428  int* nprioexternimpls /**< pointer to store the number of implicit integer candidates with maximal priority,
429  * or NULL */
430  )
431 {
432 
433  assert(branchcand != NULL);
434 
435  /* assign return values */
436  if( externcands != NULL )
437  *externcands = branchcand->externcands;
438  if( externcandssol != NULL )
439  *externcandssol = branchcand->externcandssol;
440  if( externcandsscore != NULL )
441  *externcandsscore = branchcand->externcandsscore;
442  if( nexterncands != NULL )
443  *nexterncands = branchcand->nexterncands;
444  if( nprioexterncands != NULL )
445  *nprioexterncands = branchcand->nprioexterncands;
446  if( nprioexternbins != NULL )
447  *nprioexternbins = branchcand->nprioexternbins;
448  if( nprioexternints != NULL )
449  *nprioexternints = branchcand->nprioexternints;
450  if( nprioexternimpls != NULL )
451  *nprioexternimpls = branchcand->nprioexternimpls;
452 
453  return SCIP_OKAY;
454 }
455 
456 /** gets number of external branching candidates */
458  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
459  )
460 {
461  assert(branchcand != NULL);
462 
463  return branchcand->nexterncands;
464 }
465 
466 /** gets number of external branching candidates with maximal branch priority */
468  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
469  )
470 {
471  assert(branchcand != NULL);
472 
473  return branchcand->nprioexterncands;
474 }
475 
476 /** gets number of binary external branching candidates with maximal branch priority */
478  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
479  )
480 {
481  assert(branchcand != NULL);
482 
483  return branchcand->nprioexternbins;
484 }
485 
486 /** gets number of integer external branching candidates with maximal branch priority */
488  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
489  )
490 {
491  assert(branchcand != NULL);
492 
493  return branchcand->nprioexternints;
494 }
495 
496 /** gets number of implicit integer external branching candidates with maximal branch priority */
498  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
499  )
500 {
501  assert(branchcand != NULL);
502 
503  return branchcand->nprioexternimpls;
504 }
505 
506 /** gets number of continuous external branching candidates with maximal branch priority */
508  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
509  )
510 {
511  assert(branchcand != NULL);
512 
513  return branchcand->nprioexterncands - branchcand->nprioexternbins - branchcand->nprioexternints - branchcand->nprioexternimpls;
514 }
515 
516 /** insert variable, its score and its solution value into the external branching candidate storage
517  * the absolute difference of the current lower and upper bounds of the variable must be at least epsilon
518  */
520  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
521  SCIP_SET* set, /**< global SCIP settings */
522  SCIP_VAR* var, /**< variable to insert */
523  SCIP_Real score, /**< score of external candidate, e.g. infeasibility */
524  SCIP_Real solval /**< value of the variable in the current solution */
525  )
526 {
527  SCIP_VARTYPE vartype;
528  int branchpriority;
529  int insertpos;
530 
531  assert(branchcand != NULL);
532  assert(var != NULL);
533  assert(!SCIPsetIsEQ(set, SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var))); /* the variable should not be fixed yet */
534  assert(SCIPvarGetType(var) == SCIP_VARTYPE_CONTINUOUS || !SCIPsetIsEQ(set, SCIPsetCeil(set, SCIPvarGetLbLocal(var)), SCIPsetFloor(set, SCIPvarGetUbLocal(var)))); /* a discrete variable should also not be fixed, even after rounding bounds to integral values */
535  assert(SCIPvarGetStatus(var) != SCIP_VARSTATUS_MULTAGGR || !SCIPsetIsEQ(set, SCIPvarGetMultaggrLbLocal(var, set), SCIPvarGetMultaggrUbLocal(var, set))); /* also the current bounds of a multi-aggregated variable should not be fixed yet */
536  assert(branchcand->nprioexterncands <= branchcand->nexterncands);
537  assert(branchcand->nexterncands <= branchcand->externcandssize);
538 
539  vartype = SCIPvarGetType(var);
540  branchpriority = SCIPvarGetBranchPriority(var);
541  insertpos = branchcand->nexterncands;
542 
543  SCIP_CALL( ensureExterncandsSize(branchcand, set, branchcand->nexterncands+1) );
544 
545  SCIPdebugMessage("inserting external candidate <%s> of type %d and priority %d into candidate set (maxprio: %d), score = %g, solval = %g\n",
546  SCIPvarGetName(var), vartype, branchpriority, branchcand->externmaxpriority, score, solval);
547 
548  /* insert the variable into externcands, making sure, that the highest priority candidates are at the front
549  * and ordered binaries, integers, implicit integers, continuous
550  */
551  if( branchpriority > branchcand->externmaxpriority )
552  {
553  /* candidate has higher priority than the current maximum:
554  * move it to the front and declare it to be the single best candidate
555  */
556  branchcand->externcands[insertpos] = branchcand->externcands[0];
557  branchcand->externcandsscore[insertpos] = branchcand->externcandsscore[0];
558  branchcand->externcandssol[insertpos] = branchcand->externcandssol[0];
559 
560  insertpos = 0;
561 
562  branchcand->nprioexterncands = 1;
563  branchcand->nprioexternbins = (vartype == SCIP_VARTYPE_BINARY ? 1 : 0);
564  branchcand->nprioexternints = (vartype == SCIP_VARTYPE_INTEGER ? 1 : 0);
565  branchcand->nprioexternimpls = (vartype == SCIP_VARTYPE_IMPLINT ? 1 : 0);
566  branchcand->externmaxpriority = branchpriority;
567  }
568  else if( branchpriority == branchcand->externmaxpriority )
569  {
570  /* candidate has equal priority as the current maximum:
571  * move away the first non-maximal priority candidate, move the current candidate to the correct
572  * slot (binaries first, integers next, implicit integers next, continuous last) and increase the number
573  * of maximal priority candidates
574  */
575  if( insertpos != branchcand->nprioexterncands )
576  {
577  branchcand->externcands[insertpos] = branchcand->externcands[branchcand->nprioexterncands];
578  branchcand->externcandsscore[insertpos] = branchcand->externcandsscore[branchcand->nprioexterncands];
579  branchcand->externcandssol[insertpos] = branchcand->externcandssol[branchcand->nprioexterncands];
580 
581  insertpos = branchcand->nprioexterncands;
582  }
583  branchcand->nprioexterncands++;
584  if( vartype == SCIP_VARTYPE_BINARY || vartype == SCIP_VARTYPE_INTEGER || vartype == SCIP_VARTYPE_IMPLINT )
585  {
586  if( insertpos != branchcand->nprioexternbins + branchcand->nprioexternints + branchcand->nprioexternimpls )
587  {
588  branchcand->externcands[insertpos] =
589  branchcand->externcands[branchcand->nprioexternbins + branchcand->nprioexternints + branchcand->nprioexternimpls];
590  branchcand->externcandsscore[insertpos] =
591  branchcand->externcandsscore[branchcand->nprioexternbins + branchcand->nprioexternints + branchcand->nprioexternimpls];
592  branchcand->externcandssol[insertpos] =
593  branchcand->externcandssol[branchcand->nprioexternbins + branchcand->nprioexternints + branchcand->nprioexternimpls];
594 
595  insertpos = branchcand->nprioexternbins + branchcand->nprioexternints + branchcand->nprioexternimpls;
596  }
597  branchcand->nprioexternimpls++;
598 
599 
600  if( vartype == SCIP_VARTYPE_BINARY || vartype == SCIP_VARTYPE_INTEGER )
601  {
602  if( insertpos != branchcand->nprioexternbins + branchcand->nprioexternints )
603  {
604  branchcand->externcands[insertpos] =
605  branchcand->externcands[branchcand->nprioexternbins + branchcand->nprioexternints];
606  branchcand->externcandsscore[insertpos] =
607  branchcand->externcandsscore[branchcand->nprioexternbins + branchcand->nprioexternints];
608  branchcand->externcandssol[insertpos] =
609  branchcand->externcandssol[branchcand->nprioexternbins + branchcand->nprioexternints];
610 
611  insertpos = branchcand->nprioexternbins + branchcand->nprioexternints;
612  }
613  branchcand->nprioexternints++;
614  branchcand->nprioexternimpls--;
615 
616 
617  if( vartype == SCIP_VARTYPE_BINARY )
618  {
619  if( insertpos != branchcand->nprioexternbins )
620  {
621  branchcand->externcands[insertpos] = branchcand->externcands[branchcand->nprioexternbins];
622  branchcand->externcandsscore[insertpos] = branchcand->externcandsscore[branchcand->nprioexternbins];
623  branchcand->externcandssol[insertpos] = branchcand->externcandssol[branchcand->nprioexternbins];
624 
625  insertpos = branchcand->nprioexternbins;
626  }
627  branchcand->nprioexternbins++;
628  branchcand->nprioexternints--;
629  }
630  }
631  }
632  }
633  branchcand->externcands[insertpos] = var;
634  branchcand->externcandsscore[insertpos] = score;
635  branchcand->externcandssol[insertpos] = solval;
636  branchcand->nexterncands++;
637 
638  SCIPdebugMessage(" -> inserted at position %d (nprioexterncands=%d)\n", insertpos, branchcand->nprioexterncands);
639 
640  assert(0 <= branchcand->nprioexterncands && branchcand->nprioexterncands <= branchcand->nexterncands);
641  assert(0 <= branchcand->nprioexternbins && branchcand->nprioexternbins <= branchcand->nprioexterncands);
642  assert(0 <= branchcand->nprioexternints && branchcand->nprioexternints <= branchcand->nprioexterncands);
643  assert(0 <= branchcand->nprioexternimpls && branchcand->nprioexternimpls <= branchcand->nprioexterncands);
644 
645  return SCIP_OKAY;
646 }
647 
648 /** removes all external candidates from the storage for external branching */
650  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
651  )
652 {
653  assert(branchcand != NULL);
654 
655  branchcand->nexterncands = 0;
656  branchcand->nprioexterncands = 0;
657  branchcand->nprioexternbins = 0;
658  branchcand->nprioexternints = 0;
659  branchcand->nprioexternimpls = 0;
660  branchcand->externmaxpriority = INT_MIN;
661 }
662 
663 /** checks whether the given variable is contained in the candidate storage for external branching */
665  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
666  SCIP_VAR* var /**< variable to look for */
667  )
668 {
669  int branchpriority;
670  int i;
671 
672  assert(branchcand != NULL);
673  assert(var != NULL);
674  assert(branchcand->nprioexterncands <= branchcand->nexterncands);
675  assert(branchcand->nexterncands <= branchcand->externcandssize);
676 
677  branchpriority = SCIPvarGetBranchPriority(var);
678 
679  /* look for the variable in the externcands, using the fact, that the highest priority candidates are at the front
680  * and ordered binaries, integers, implicit integers, continuous
681  */
682  if( branchpriority > branchcand->externmaxpriority )
683  {
684  /* the branching priority of the variable is higher than the maximal priority contained in the array;
685  * the variable can thus not be contained */
686  return FALSE;
687  }
688  if( branchpriority == branchcand->externmaxpriority )
689  {
690  SCIP_VARTYPE vartype;
691 
692  vartype = SCIPvarGetType(var);
693 
694  /* variable has equal priority as the current maximum:
695  * look for it in the correct slot (binaries first, integers next, implicit integers next, continuous last)
696  */
697  if( vartype == SCIP_VARTYPE_BINARY )
698  {
699  /* the variable is binary, look at the first branchcand->nprioexternbins slots */
700  for( i = 0; i < branchcand->nprioexternbins; i++ )
701  if( branchcand->externcands[i] == var )
702  return TRUE;
703  return FALSE;
704  }
705  if( vartype == SCIP_VARTYPE_INTEGER )
706  {
707  /* the variable is integer, look at the slots containing integers */
708  for( i = 0; i < branchcand->nprioexternints; i++ )
709  if( branchcand->externcands[branchcand->nprioexternbins + i] == var )
710  return TRUE;
711  return FALSE;
712  }
713  if( vartype == SCIP_VARTYPE_IMPLINT )
714  {
715  /* the variable is implicit integer, look at the slots containing implicit integers */
716  for( i = 0; i < branchcand->nprioexternimpls; i++ )
717  if( branchcand->externcands[branchcand->nprioexternbins + branchcand->nprioexternints + i] == var )
718  return TRUE;
719  return FALSE;
720  }
721  /* the variable is continuous, look at the slots containing continuous variables */
722  assert(vartype == SCIP_VARTYPE_CONTINUOUS);
723  for( i = branchcand->nprioexternbins + branchcand->nprioexternints + branchcand->nprioexternimpls;
724  i < branchcand->nprioexterncands; i++ )
725  if( branchcand->externcands[i] == var )
726  return TRUE;
727  return FALSE;
728  }
729  /* the branching priority of the variable is lower than the maximal priority contained in the array;
730  * look for the variable in the candidates which do not have maximal priority */
731  assert(branchpriority < branchcand->externmaxpriority);
732 
733  for( i = branchcand->nprioexterncands; i < branchcand->nexterncands; i++ )
734  if( branchcand->externcands[i] == var )
735  return TRUE;
736  return FALSE;
737 }
738 
739 /** gets branching candidates for pseudo solution branching (non-fixed variables) */
741  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
742  SCIP_SET* set, /**< global SCIP settings */
743  SCIP_PROB* prob, /**< problem data */
744  SCIP_VAR*** pseudocands, /**< pointer to store the array of pseudo branching candidates, or NULL */
745  int* npseudocands, /**< pointer to store the number of pseudo branching candidates, or NULL */
746  int* npriopseudocands /**< pointer to store the number of candidates with maximal priority, or NULL */
747  )
748 {
749  assert(branchcand != NULL);
750 
751 #ifndef NDEBUG
752  /* check, if the current pseudo branching candidate array is correct */
753  {
754  SCIP_VAR* var;
755  int npcs;
756  int v;
757 
758  assert(prob != NULL);
759 
760  /* pseudo branching candidates are non-fixed binary, integer, and implicit integer variables */
761  npcs = 0;
762  for( v = 0; v < prob->nbinvars + prob->nintvars + prob->nimplvars; ++v )
763  {
764  var = prob->vars[v];
765  assert(var != NULL);
767  assert(SCIPvarGetType(var) == SCIP_VARTYPE_BINARY
770  assert(SCIPsetIsFeasIntegral(set, SCIPvarGetLbLocal(var)));
771  assert(SCIPsetIsFeasIntegral(set, SCIPvarGetUbLocal(var)));
772  assert(SCIPsetIsLE(set, SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var)));
773 
774  if( SCIPsetIsLT(set, SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var)) )
775  {
776  assert(0 <= var->pseudocandindex && var->pseudocandindex < branchcand->npseudocands);
777  assert(branchcand->pseudocands[var->pseudocandindex] == var);
778  npcs++;
779  }
780  else
781  {
782  assert(var->pseudocandindex == -1);
783  }
784  }
785  assert(branchcand->npseudocands == npcs);
786  }
787 #endif
788 
789  /* assign return values */
790  if( pseudocands != NULL )
791  *pseudocands = branchcand->pseudocands;
792  if( npseudocands != NULL )
793  *npseudocands = branchcand->npseudocands;
794  if( npriopseudocands != NULL )
795  *npriopseudocands = (set->branch_preferbinary && branchcand->npriopseudobins > 0 ? branchcand->npriopseudobins
796  : branchcand->npriopseudocands);
797 
798  return SCIP_OKAY;
799 }
800 
801 /** gets number of branching candidates for pseudo solution branching (non-fixed variables) */
803  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
804  )
805 {
806  assert(branchcand != NULL);
807 
808  return branchcand->npseudocands;
809 }
810 
811 /** gets number of branching candidates with maximal branch priority for pseudo solution branching */
813  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
814  )
815 {
816  assert(branchcand != NULL);
817 
818  return branchcand->npriopseudocands;
819 }
820 
821 /** gets number of binary branching candidates with maximal branch priority for pseudo solution branching */
823  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
824  )
825 {
826  assert(branchcand != NULL);
827 
828  return branchcand->npriopseudobins;
829 }
830 
831 /** gets number of integer branching candidates with maximal branch priority for pseudo solution branching */
833  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
834  )
835 {
836  assert(branchcand != NULL);
837 
838  return branchcand->npriopseudoints;
839 }
840 
841 /** gets number of implicit integer branching candidates with maximal branch priority for pseudo solution branching */
843  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
844  )
845 {
846  assert(branchcand != NULL);
847 
848  return branchcand->npriopseudocands - branchcand->npriopseudobins - branchcand->npriopseudoints;
849 }
850 
851 /** insert pseudocand at given position, or to the first positions of the maximal priority candidates, using the
852  * given position as free slot for the other candidates
853  */
854 static
856  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
857  SCIP_VAR* var, /**< variable to insert */
858  int insertpos /**< free position to insert the variable */
859  )
860 {
861  SCIP_VARTYPE vartype;
862  int branchpriority;
863 
864  assert(branchcand != NULL);
865  assert(var != NULL);
866  assert(branchcand->npriopseudocands <= insertpos && insertpos < branchcand->npseudocands);
867  assert(branchcand->npseudocands <= branchcand->pseudocandssize);
868 
869  vartype = SCIPvarGetType(var);
870  branchpriority = SCIPvarGetBranchPriority(var);
871 
872  SCIPdebugMessage("inserting pseudo candidate <%s> of type %d and priority %d into candidate set at position %d (maxprio: %d)\n",
873  SCIPvarGetName(var), vartype, branchpriority, insertpos, branchcand->pseudomaxpriority);
874 
875  /* insert the variable into pseudocands, making sure, that the highest priority candidates are at the front
876  * and ordered binaries, integers, implicit integers
877  */
878  if( branchpriority > branchcand->pseudomaxpriority )
879  {
880  /* candidate has higher priority than the current maximum:
881  * move it to the front and declare it to be the single best candidate
882  */
883  if( insertpos != 0 )
884  {
885  branchcand->pseudocands[insertpos] = branchcand->pseudocands[0];
886  branchcand->pseudocands[insertpos]->pseudocandindex = insertpos;
887  insertpos = 0;
888  }
889  branchcand->npriopseudocands = 1;
890  branchcand->npriopseudobins = (vartype == SCIP_VARTYPE_BINARY ? 1 : 0);
891  branchcand->npriopseudoints = (vartype == SCIP_VARTYPE_INTEGER ? 1 : 0);
892  branchcand->pseudomaxpriority = branchpriority;
893  }
894  else if( branchpriority == branchcand->pseudomaxpriority )
895  {
896  /* candidate has equal priority as the current maximum:
897  * move away the first non-maximal priority candidate, move the current candidate to the correct
898  * slot (binaries first, integers next, implicit integers last) and increase the number of maximal priority candidates
899  */
900  if( insertpos != branchcand->npriopseudocands )
901  {
902  branchcand->pseudocands[insertpos] = branchcand->pseudocands[branchcand->npriopseudocands];
903  branchcand->pseudocands[insertpos]->pseudocandindex = insertpos;
904  insertpos = branchcand->npriopseudocands;
905  }
906  branchcand->npriopseudocands++;
907  if( vartype == SCIP_VARTYPE_BINARY || vartype == SCIP_VARTYPE_INTEGER )
908  {
909  if( insertpos != branchcand->npriopseudobins + branchcand->npriopseudoints )
910  {
911  branchcand->pseudocands[insertpos] =
912  branchcand->pseudocands[branchcand->npriopseudobins + branchcand->npriopseudoints];
913  branchcand->pseudocands[insertpos]->pseudocandindex = insertpos;
914  insertpos = branchcand->npriopseudobins + branchcand->npriopseudoints;
915  }
916  branchcand->npriopseudoints++;
917 
918  if( vartype == SCIP_VARTYPE_BINARY )
919  {
920  if( insertpos != branchcand->npriopseudobins )
921  {
922  branchcand->pseudocands[insertpos] = branchcand->pseudocands[branchcand->npriopseudobins];
923  branchcand->pseudocands[insertpos]->pseudocandindex = insertpos;
924  insertpos = branchcand->npriopseudobins;
925  }
926  branchcand->npriopseudobins++;
927  branchcand->npriopseudoints--;
928  }
929  }
930  }
931  branchcand->pseudocands[insertpos] = var;
932  var->pseudocandindex = insertpos;
933 
934  SCIPdebugMessage(" -> inserted at position %d (npriopseudocands=%d)\n", insertpos, branchcand->npriopseudocands);
935 
936  assert(0 <= branchcand->npriopseudocands && branchcand->npriopseudocands <= branchcand->npseudocands);
937  assert(0 <= branchcand->npriopseudobins && branchcand->npriopseudobins <= branchcand->npriopseudocands);
938  assert(0 <= branchcand->npriopseudoints && branchcand->npriopseudoints <= branchcand->npriopseudocands);
939 }
940 
941 /** sorts the pseudo branching candidates, such that the candidates of maximal priority are at the front,
942  * ordered by binaries, integers, implicit integers
943  */
944 static
946  SCIP_BRANCHCAND* branchcand /**< branching candidate storage */
947  )
948 {
949  SCIP_VAR* var;
950  int i;
951 
952  assert(branchcand != NULL);
953  assert(branchcand->npriopseudocands == 0); /* is only be called after removal of last maximal candidate */
954  assert(branchcand->npriopseudobins == 0);
955  assert(branchcand->npriopseudoints == 0);
956 
957  SCIPdebugMessage("resorting pseudo candidates\n");
958 
959  branchcand->pseudomaxpriority = INT_MIN;
960 
961  for( i = 0; i < branchcand->npseudocands; ++i )
962  {
963  var = branchcand->pseudocands[i];
964  assert(var->pseudocandindex == i);
965 
966  if( SCIPvarGetBranchPriority(var) >= branchcand->pseudomaxpriority )
967  branchcandInsertPseudoCand(branchcand, var, i);
968  }
969 
970  assert(0 <= branchcand->npriopseudocands && branchcand->npriopseudocands <= branchcand->npseudocands);
971  assert(0 <= branchcand->npriopseudobins && branchcand->npriopseudobins <= branchcand->npriopseudocands);
972  assert(0 <= branchcand->npriopseudoints && branchcand->npriopseudoints <= branchcand->npriopseudocands);
973 }
974 
975 /** removes pseudo candidate from pseudocands array
976  */
977 static
979  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
980  SCIP_VAR* var /**< variable to remove */
981  )
982 {
983  int branchpriority;
984  int freepos;
985 
986  assert(branchcand != NULL);
987  assert(var != NULL);
988  assert(var->pseudocandindex < branchcand->npseudocands);
989  assert(branchcand->pseudocands[var->pseudocandindex] == var);
990  assert(branchcand->pseudocands[branchcand->npseudocands-1] != NULL);
991 
992  branchpriority = SCIPvarGetBranchPriority(var);
993 
994  SCIPdebugMessage("removing pseudo candidate <%s> of type %d and priority %d at %d from candidate set (maxprio: %d)\n",
995  SCIPvarGetName(var), SCIPvarGetType(var), branchpriority, var->pseudocandindex, branchcand->pseudomaxpriority);
996 
997  /* delete the variable from pseudocands, making sure, that the highest priority candidates are at the front
998  * and ordered binaries, integers, implicit integers
999  */
1000  freepos = var->pseudocandindex;
1001  var->pseudocandindex = -1;
1002  assert(0 <= freepos && freepos < branchcand->npseudocands);
1003 
1004  if( freepos < branchcand->npriopseudobins )
1005  {
1006  /* a binary candidate of maximal priority was removed */
1007  assert(SCIPvarGetType(var) == SCIP_VARTYPE_BINARY);
1008  assert(branchpriority == branchcand->pseudomaxpriority);
1009  if( freepos != branchcand->npriopseudobins - 1 )
1010  {
1011  branchcand->pseudocands[freepos] = branchcand->pseudocands[branchcand->npriopseudobins - 1];
1012  branchcand->pseudocands[freepos]->pseudocandindex = freepos;
1013  freepos = branchcand->npriopseudobins - 1;
1014  }
1015  branchcand->npriopseudobins--;
1016  branchcand->npriopseudoints++;
1017  }
1018  if( freepos < branchcand->npriopseudobins + branchcand->npriopseudoints )
1019  {
1020  /* a binary or integer candidate of maximal priority was removed */
1022  assert(branchpriority == branchcand->pseudomaxpriority);
1023  if( freepos != branchcand->npriopseudobins + branchcand->npriopseudoints - 1 )
1024  {
1025  branchcand->pseudocands[freepos] =
1026  branchcand->pseudocands[branchcand->npriopseudobins + branchcand->npriopseudoints - 1];
1027  branchcand->pseudocands[freepos]->pseudocandindex = freepos;
1028  freepos = branchcand->npriopseudobins + branchcand->npriopseudoints - 1;
1029  }
1030  branchcand->npriopseudoints--;
1031  }
1032  if( freepos < branchcand->npriopseudocands )
1033  {
1034  /* a candidate of maximal priority was removed */
1035  assert(branchpriority == branchcand->pseudomaxpriority);
1036  if( freepos != branchcand->npriopseudocands - 1 )
1037  {
1038  branchcand->pseudocands[freepos] = branchcand->pseudocands[branchcand->npriopseudocands - 1];
1039  branchcand->pseudocands[freepos]->pseudocandindex = freepos;
1040  freepos = branchcand->npriopseudocands - 1;
1041  }
1042  branchcand->npriopseudocands--;
1043  }
1044  if( freepos != branchcand->npseudocands - 1 )
1045  {
1046  branchcand->pseudocands[freepos] = branchcand->pseudocands[branchcand->npseudocands - 1];
1047  branchcand->pseudocands[freepos]->pseudocandindex = freepos;
1048  }
1049  branchcand->npseudocands--;
1050 
1051  assert(0 <= branchcand->npriopseudocands && branchcand->npriopseudocands <= branchcand->npseudocands);
1052  assert(0 <= branchcand->npriopseudobins && branchcand->npriopseudobins <= branchcand->npriopseudocands);
1053  assert(0 <= branchcand->npriopseudoints && branchcand->npriopseudoints <= branchcand->npriopseudocands);
1054 
1055  /* if all maximal priority candidates were removed, resort the array s.t. the new maximal priority candidates
1056  * are at the front
1057  */
1058  if( branchcand->npriopseudocands == 0 )
1059  branchcandSortPseudoCands(branchcand);
1060 }
1061 
1062 /** removes variable from branching candidate list */
1064  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
1065  SCIP_VAR* var /**< variable that changed its bounds */
1066  )
1067 {
1068  assert(var != NULL);
1069 
1070  /* make sure, variable is not member of the pseudo branching candidate list */
1071  if( var->pseudocandindex >= 0 )
1072  {
1073  branchcandRemovePseudoCand(branchcand, var);
1074  }
1075 
1076  return SCIP_OKAY;
1077 }
1078 
1079 /** updates branching candidate list for a given variable */
1081  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
1082  SCIP_SET* set, /**< global SCIP settings */
1083  SCIP_VAR* var /**< variable that changed its bounds */
1084  )
1085 {
1086  assert(branchcand != NULL);
1087  assert(var != NULL);
1088 
1091  && SCIPsetIsLT(set, SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var)) )
1092  {
1093  /* variable is neither continuous nor fixed and has non-empty domain: make sure it is member of the pseudo branching candidate list */
1094  if( var->pseudocandindex == -1 )
1095  {
1096  SCIP_CALL( ensurePseudocandsSize(branchcand, set, branchcand->npseudocands+1) );
1097 
1098  branchcand->npseudocands++;
1099  branchcandInsertPseudoCand(branchcand, var, branchcand->npseudocands-1);
1100  }
1101  }
1102  else
1103  {
1110  || SCIPsetIsGE(set, SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var)));
1111 
1112  /* variable is continuous or fixed or has empty domain: make sure it is not member of the pseudo branching candidate list */
1113  SCIP_CALL( SCIPbranchcandRemoveVar(branchcand, var) );
1114  }
1115 
1116  return SCIP_OKAY;
1117 }
1118 
1119 /** updates branching priority of the given variable and update the pseude candidate array if needed */
1121  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
1122  SCIP_SET* set, /**< global SCIP settings */
1123  SCIP_VAR* var, /**< variable that changed its bounds */
1124  int branchpriority /**< branch priority of the variable */
1125  )
1126 {
1127  int oldbranchpriority;
1128  int pseudomaxpriority;
1129 
1130  assert(branchcand != NULL);
1131 
1132  oldbranchpriority = SCIPvarGetBranchPriority(var);
1133 
1134  if( oldbranchpriority == branchpriority )
1135  return SCIP_OKAY;
1136 
1137  pseudomaxpriority = branchcand->pseudomaxpriority;
1138 
1139  /* if the variable currently belongs to priority set or the new branching priority is larger than the current one,
1140  * renmove it from the pseudo branch candidate array temporary
1141  */
1142  if( oldbranchpriority == pseudomaxpriority || branchpriority > pseudomaxpriority )
1143  {
1144  SCIP_CALL( SCIPbranchcandRemoveVar(branchcand, var) );
1145  assert(var->pseudocandindex == -1);
1146  }
1147 
1148  /* change the branching priority of the variable */
1149  SCIP_CALL( SCIPvarChgBranchPriority(var, branchpriority) );
1150 
1151  /* of the variable is not part of the pseudo branching candidate array; check if it is a pseudo branching candidate
1152  * and add it if so
1153  */
1154  SCIP_CALL( SCIPbranchcandUpdateVar(branchcand, set, var) );
1155 
1156  return SCIP_OKAY;
1157 }
1158 
1159 
1160 
1161 /*
1162  * branching rule methods
1163  */
1164 
1165 /** compares two branching rules w. r. to their priority */
1166 SCIP_DECL_SORTPTRCOMP(SCIPbranchruleComp)
1167 { /*lint --e{715}*/
1168  return ((SCIP_BRANCHRULE*)elem2)->priority - ((SCIP_BRANCHRULE*)elem1)->priority;
1169 }
1170 
1171 /** comparison method for sorting branching rules w.r.t. to their name */
1172 SCIP_DECL_SORTPTRCOMP(SCIPbranchruleCompName)
1173 {
1175 }
1176 
1177 /** method to call, when the priority of a branching rule was changed */
1178 static
1179 SCIP_DECL_PARAMCHGD(paramChgdBranchrulePriority)
1180 { /*lint --e{715}*/
1181  SCIP_PARAMDATA* paramdata;
1182 
1183  paramdata = SCIPparamGetData(param);
1184  assert(paramdata != NULL);
1185 
1186  /* use SCIPsetBranchrulePriority() to mark the branchrules unsorted */
1187  SCIP_CALL( SCIPsetBranchrulePriority(scip, (SCIP_BRANCHRULE*)paramdata, SCIPparamGetInt(param)) ); /*lint !e740*/
1188 
1189  return SCIP_OKAY;
1190 }
1191 
1192 /** copies the given branchrule to a new scip */
1194  SCIP_BRANCHRULE* branchrule, /**< branchrule */
1195  SCIP_SET* set /**< SCIP_SET of SCIP to copy to */
1196  )
1197 {
1198  assert(branchrule != NULL);
1199  assert(set != NULL);
1200  assert(set->scip != NULL);
1201 
1202  if( branchrule->branchcopy != NULL )
1203  {
1204  SCIPdebugMessage("including branching rule %s in subscip %p\n", SCIPbranchruleGetName(branchrule), (void*)set->scip);
1205  SCIP_CALL( branchrule->branchcopy(set->scip, branchrule) );
1206  }
1207 
1208  return SCIP_OKAY;
1209 }
1210 
1211 /** creates a branching rule */
1213  SCIP_BRANCHRULE** branchrule, /**< pointer to store branching rule */
1214  SCIP_SET* set, /**< global SCIP settings */
1215  SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */
1216  BMS_BLKMEM* blkmem, /**< block memory for parameter settings */
1217  const char* name, /**< name of branching rule */
1218  const char* desc, /**< description of branching rule */
1219  int priority, /**< priority of the branching rule */
1220  int maxdepth, /**< maximal depth level, up to which this branching rule should be used (or -1) */
1221  SCIP_Real maxbounddist, /**< maximal relative distance from current node's dual bound to primal bound
1222  * compared to best node's dual bound for applying branching rule
1223  * (0.0: only on current best node, 1.0: on all nodes) */
1224  SCIP_DECL_BRANCHCOPY ((*branchcopy)), /**< copy method of branching rule */
1225  SCIP_DECL_BRANCHFREE ((*branchfree)), /**< destructor of branching rule */
1226  SCIP_DECL_BRANCHINIT ((*branchinit)), /**< initialize branching rule */
1227  SCIP_DECL_BRANCHEXIT ((*branchexit)), /**< deinitialize branching rule */
1228  SCIP_DECL_BRANCHINITSOL((*branchinitsol)),/**< solving process initialization method of branching rule */
1229  SCIP_DECL_BRANCHEXITSOL((*branchexitsol)),/**< solving process deinitialization method of branching rule */
1230  SCIP_DECL_BRANCHEXECLP((*branchexeclp)), /**< branching execution method for fractional LP solutions */
1231  SCIP_DECL_BRANCHEXECEXT((*branchexecext)),/**< branching execution method for external solutions */
1232  SCIP_DECL_BRANCHEXECPS((*branchexecps)), /**< branching execution method for not completely fixed pseudo solutions */
1233  SCIP_BRANCHRULEDATA* branchruledata /**< branching rule data */
1234  )
1235 {
1236  char paramname[SCIP_MAXSTRLEN];
1237  char paramdesc[SCIP_MAXSTRLEN];
1238 
1239  assert(branchrule != NULL);
1240  assert(name != NULL);
1241  assert(desc != NULL);
1242 
1243  SCIP_ALLOC( BMSallocMemory(branchrule) );
1244  SCIP_ALLOC( BMSduplicateMemoryArray(&(*branchrule)->name, name, strlen(name)+1) );
1245  SCIP_ALLOC( BMSduplicateMemoryArray(&(*branchrule)->desc, desc, strlen(desc)+1) );
1246  (*branchrule)->priority = priority;
1247  (*branchrule)->maxdepth = maxdepth;
1248  (*branchrule)->maxbounddist = maxbounddist;
1249  (*branchrule)->branchcopy = branchcopy;
1250  (*branchrule)->branchfree = branchfree;
1251  (*branchrule)->branchinit = branchinit;
1252  (*branchrule)->branchexit = branchexit;
1253  (*branchrule)->branchinitsol = branchinitsol;
1254  (*branchrule)->branchexitsol = branchexitsol;
1255  (*branchrule)->branchexeclp = branchexeclp;
1256  (*branchrule)->branchexecext = branchexecext;
1257  (*branchrule)->branchexecps = branchexecps;
1258  (*branchrule)->branchruledata = branchruledata;
1259  SCIP_CALL( SCIPclockCreate(&(*branchrule)->setuptime, SCIP_CLOCKTYPE_DEFAULT) );
1260  SCIP_CALL( SCIPclockCreate(&(*branchrule)->branchclock, SCIP_CLOCKTYPE_DEFAULT) );
1261  (*branchrule)->nlpcalls = 0;
1262  (*branchrule)->nexterncalls = 0;
1263  (*branchrule)->npseudocalls = 0;
1264  (*branchrule)->ncutoffs = 0;
1265  (*branchrule)->ncutsfound = 0;
1266  (*branchrule)->nconssfound = 0;
1267  (*branchrule)->ndomredsfound = 0;
1268  (*branchrule)->nchildren = 0;
1269  (*branchrule)->initialized = FALSE;
1270 
1271  /* add parameters */
1272  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "branching/%s/priority", name);
1273  (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "priority of branching rule <%s>", name);
1274  SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc,
1275  &(*branchrule)->priority, FALSE, priority, INT_MIN/4, INT_MAX/4,
1276  paramChgdBranchrulePriority, (SCIP_PARAMDATA*)(*branchrule)) ); /*lint !e740*/
1277  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "branching/%s/maxdepth", name);
1278  (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "maximal depth level, up to which branching rule <%s> should be used (-1 for no limit)", name);
1279  SCIP_CALL( SCIPsetAddIntParam(set, messagehdlr, blkmem, paramname, paramdesc,
1280  &(*branchrule)->maxdepth, FALSE, maxdepth, -1, INT_MAX,
1281  NULL, NULL) ); /*lint !e740*/
1282  (void) SCIPsnprintf(paramname, SCIP_MAXSTRLEN, "branching/%s/maxbounddist", name);
1283  (void) SCIPsnprintf(paramdesc, SCIP_MAXSTRLEN, "maximal relative distance from current node's dual bound to primal bound compared to best node's dual bound for applying branching rule (0.0: only on current best node, 1.0: on all nodes)");
1284  SCIP_CALL( SCIPsetAddRealParam(set, messagehdlr, blkmem, paramname, paramdesc,
1285  &(*branchrule)->maxbounddist, FALSE, maxbounddist, 0.0, 1.0,
1286  NULL, NULL) ); /*lint !e740*/
1287 
1288  return SCIP_OKAY;
1289 }
1290 
1291 /** frees memory of branching rule */
1293  SCIP_BRANCHRULE** branchrule, /**< pointer to branching rule data structure */
1294  SCIP_SET* set /**< global SCIP settings */
1295  )
1296 {
1297  assert(branchrule != NULL);
1298  assert(*branchrule != NULL);
1299  assert(!(*branchrule)->initialized);
1300  assert(set != NULL);
1301 
1302  /* call destructor of branching rule */
1303  if( (*branchrule)->branchfree != NULL )
1304  {
1305  SCIP_CALL( (*branchrule)->branchfree(set->scip, *branchrule) );
1306  }
1307 
1308  SCIPclockFree(&(*branchrule)->branchclock);
1309  SCIPclockFree(&(*branchrule)->setuptime);
1310  BMSfreeMemoryArray(&(*branchrule)->name);
1311  BMSfreeMemoryArray(&(*branchrule)->desc);
1312  BMSfreeMemory(branchrule);
1313 
1314  return SCIP_OKAY;
1315 }
1316 
1317 /** initializes branching rule */
1319  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1320  SCIP_SET* set /**< global SCIP settings */
1321  )
1322 {
1323  assert(branchrule != NULL);
1324  assert(set != NULL);
1325 
1326  if( branchrule->initialized )
1327  {
1328  SCIPerrorMessage("branching rule <%s> already initialized\n", branchrule->name);
1329  return SCIP_INVALIDCALL;
1330  }
1331 
1332  if( set->misc_resetstat )
1333  {
1334  SCIPclockReset(branchrule->setuptime);
1335  SCIPclockReset(branchrule->branchclock);
1336  branchrule->nlpcalls = 0;
1337  branchrule->nexterncalls = 0;
1338  branchrule->npseudocalls = 0;
1339  branchrule->ncutoffs = 0;
1340  branchrule->ncutsfound = 0;
1341  branchrule->nconssfound = 0;
1342  branchrule->ndomredsfound = 0;
1343  branchrule->nchildren = 0;
1344  }
1345 
1346  if( branchrule->branchinit != NULL )
1347  {
1348  /* start timing */
1349  SCIPclockStart(branchrule->setuptime, set);
1350 
1351  SCIP_CALL( branchrule->branchinit(set->scip, branchrule) );
1352 
1353  /* stop timing */
1354  SCIPclockStop(branchrule->setuptime, set);
1355  }
1356  branchrule->initialized = TRUE;
1357 
1358  return SCIP_OKAY;
1359 }
1360 
1361 /** deinitializes branching rule */
1363  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1364  SCIP_SET* set /**< global SCIP settings */
1365  )
1366 {
1367  assert(branchrule != NULL);
1368  assert(set != NULL);
1369 
1370  if( !branchrule->initialized )
1371  {
1372  SCIPerrorMessage("branching rule <%s> not initialized\n", branchrule->name);
1373  return SCIP_INVALIDCALL;
1374  }
1375 
1376  if( branchrule->branchexit != NULL )
1377  {
1378  /* start timing */
1379  SCIPclockStart(branchrule->setuptime, set);
1380 
1381  SCIP_CALL( branchrule->branchexit(set->scip, branchrule) );
1382 
1383  /* stop timing */
1384  SCIPclockStop(branchrule->setuptime, set);
1385  }
1386  branchrule->initialized = FALSE;
1387 
1388  return SCIP_OKAY;
1389 }
1390 
1391 /** informs branching rule that the branch and bound process is being started */
1393  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1394  SCIP_SET* set /**< global SCIP settings */
1395  )
1396 {
1397  assert(branchrule != NULL);
1398  assert(set != NULL);
1399 
1400  /* call solving process initialization method of branching rule */
1401  if( branchrule->branchinitsol != NULL )
1402  {
1403  /* start timing */
1404  SCIPclockStart(branchrule->setuptime, set);
1405 
1406  SCIP_CALL( branchrule->branchinitsol(set->scip, branchrule) );
1407 
1408  /* stop timing */
1409  SCIPclockStop(branchrule->setuptime, set);
1410  }
1411 
1412  return SCIP_OKAY;
1413 }
1414 
1415 /** informs branching rule that the branch and bound process data is being freed */
1417  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1418  SCIP_SET* set /**< global SCIP settings */
1419  )
1420 {
1421  assert(branchrule != NULL);
1422  assert(set != NULL);
1423 
1424  /* call solving process deinitialization method of branching rule */
1425  if( branchrule->branchexitsol != NULL )
1426  {
1427  /* start timing */
1428  SCIPclockStart(branchrule->setuptime, set);
1429 
1430  SCIP_CALL( branchrule->branchexitsol(set->scip, branchrule) );
1431 
1432  /* stop timing */
1433  SCIPclockStop(branchrule->setuptime, set);
1434  }
1435 
1436  return SCIP_OKAY;
1437 }
1438 
1439 /** executes branching rule for fractional LP solution */
1441  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1442  SCIP_SET* set, /**< global SCIP settings */
1443  SCIP_STAT* stat, /**< problem statistics */
1444  SCIP_TREE* tree, /**< branch and bound tree */
1445  SCIP_SEPASTORE* sepastore, /**< separation storage */
1446  SCIP_Real cutoffbound, /**< global upper cutoff bound */
1447  SCIP_Bool allowaddcons, /**< should adding constraints be allowed to avoid a branching? */
1448  SCIP_RESULT* result /**< pointer to store the result of the callback method */
1449  )
1450 {
1451  assert(branchrule != NULL);
1452  assert(set != NULL);
1453  assert(tree != NULL);
1454  assert(tree->focusnode != NULL);
1455  assert(tree->nchildren == 0);
1456  assert(result != NULL);
1457 
1458  *result = SCIP_DIDNOTRUN;
1459  if( branchrule->branchexeclp != NULL
1460  && (branchrule->maxdepth == -1 || branchrule->maxdepth >= SCIPtreeGetCurrentDepth(tree)) )
1461  {
1462  SCIP_Real loclowerbound;
1463  SCIP_Real glblowerbound;
1464 
1465  loclowerbound = SCIPnodeGetLowerbound(tree->focusnode);
1466  glblowerbound = SCIPtreeGetLowerbound(tree, set);
1467  if( SCIPsetIsLE(set, loclowerbound - glblowerbound, branchrule->maxbounddist * (cutoffbound - glblowerbound)) )
1468  {
1469  SCIP_Longint oldndomchgs;
1470  SCIP_Longint oldnprobdomchgs;
1471  int oldncuts;
1472  int oldnactiveconss;
1473 
1474  SCIPdebugMessage("executing LP branching rule <%s>\n", branchrule->name);
1475 
1476  oldndomchgs = stat->nboundchgs + stat->nholechgs;
1477  oldnprobdomchgs = stat->nprobboundchgs + stat->nprobholechgs;
1478  oldncuts = SCIPsepastoreGetNCuts(sepastore);
1479  oldnactiveconss = stat->nactiveconss;
1480 
1481  /* start timing */
1482  SCIPclockStart(branchrule->branchclock, set);
1483 
1484  /* call external method */
1485  SCIP_CALL( branchrule->branchexeclp(set->scip, branchrule, allowaddcons, result) );
1486 
1487  /* stop timing */
1488  SCIPclockStop(branchrule->branchclock, set);
1489 
1490  /* evaluate result */
1491  if( *result != SCIP_CUTOFF
1492  && *result != SCIP_CONSADDED
1493  && *result != SCIP_REDUCEDDOM
1494  && *result != SCIP_SEPARATED
1495  && *result != SCIP_BRANCHED
1496  && *result != SCIP_DIDNOTFIND
1497  && *result != SCIP_DIDNOTRUN )
1498  {
1499  SCIPerrorMessage("branching rule <%s> returned invalid result code <%d> from LP solution branching\n",
1500  branchrule->name, *result);
1501  return SCIP_INVALIDRESULT;
1502  }
1503  if( *result == SCIP_CONSADDED && !allowaddcons )
1504  {
1505  SCIPerrorMessage("branching rule <%s> added a constraint in LP solution branching without permission\n",
1506  branchrule->name);
1507  return SCIP_INVALIDRESULT;
1508  }
1509 
1510  /* update statistics */
1511  if( *result != SCIP_DIDNOTRUN )
1512  branchrule->nlpcalls++;
1513  if( *result == SCIP_CUTOFF )
1514  branchrule->ncutoffs++;
1515  if( *result != SCIP_BRANCHED )
1516  {
1517  assert(tree->nchildren == 0);
1518 
1519  /* update domain reductions; therefore remove the domain
1520  * reduction counts which were generated in probing mode */
1521  branchrule->ndomredsfound += stat->nboundchgs + stat->nholechgs - oldndomchgs;
1522  branchrule->ndomredsfound -= (stat->nprobboundchgs + stat->nprobholechgs - oldnprobdomchgs);
1523 
1524  branchrule->ncutsfound += SCIPsepastoreGetNCuts(sepastore) - oldncuts; /*lint !e776*/
1525  branchrule->nconssfound += stat->nactiveconss - oldnactiveconss; /*lint !e776*/
1526  }
1527  else
1528  branchrule->nchildren += tree->nchildren;
1529  }
1530  }
1531 
1532  return SCIP_OKAY;
1533 }
1534 
1535 /** executes branching rule for external branching candidates */
1537  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1538  SCIP_SET* set, /**< global SCIP settings */
1539  SCIP_STAT* stat, /**< problem statistics */
1540  SCIP_TREE* tree, /**< branch and bound tree */
1541  SCIP_SEPASTORE* sepastore, /**< separation storage */
1542  SCIP_Real cutoffbound, /**< global upper cutoff bound */
1543  SCIP_Bool allowaddcons, /**< should adding constraints be allowed to avoid a branching? */
1544  SCIP_RESULT* result /**< pointer to store the result of the callback method */
1545  )
1546 {
1547  assert(branchrule != NULL);
1548  assert(set != NULL);
1549  assert(tree != NULL);
1550  assert(tree->focusnode != NULL);
1551  assert(tree->nchildren == 0);
1552  assert(result != NULL);
1553 
1554  *result = SCIP_DIDNOTRUN;
1555  if( branchrule->branchexecext != NULL
1556  && (branchrule->maxdepth == -1 || branchrule->maxdepth >= SCIPtreeGetCurrentDepth(tree)) )
1557  {
1558  SCIP_Real loclowerbound;
1559  SCIP_Real glblowerbound;
1560 
1561  loclowerbound = SCIPnodeGetLowerbound(tree->focusnode);
1562  glblowerbound = SCIPtreeGetLowerbound(tree, set);
1563  if( SCIPsetIsLE(set, loclowerbound - glblowerbound, branchrule->maxbounddist * (cutoffbound - glblowerbound)) )
1564  {
1565  SCIP_Longint oldndomchgs;
1566  SCIP_Longint oldnprobdomchgs;
1567  int oldncuts;
1568  int oldnactiveconss;
1569 
1570  SCIPdebugMessage("executing external solution branching rule <%s>\n", branchrule->name);
1571 
1572  oldndomchgs = stat->nboundchgs + stat->nholechgs;
1573  oldnprobdomchgs = stat->nprobboundchgs + stat->nprobholechgs;
1574  oldncuts = SCIPsepastoreGetNCuts(sepastore);
1575  oldnactiveconss = stat->nactiveconss;
1576 
1577  /* start timing */
1578  SCIPclockStart(branchrule->branchclock, set);
1579 
1580  /* call external method */
1581  SCIP_CALL( branchrule->branchexecext(set->scip, branchrule, allowaddcons, result) );
1582 
1583  /* stop timing */
1584  SCIPclockStop(branchrule->branchclock, set);
1585 
1586  /* evaluate result */
1587  if( *result != SCIP_CUTOFF
1588  && *result != SCIP_CONSADDED
1589  && *result != SCIP_REDUCEDDOM
1590  && *result != SCIP_SEPARATED
1591  && *result != SCIP_BRANCHED
1592  && *result != SCIP_DIDNOTFIND
1593  && *result != SCIP_DIDNOTRUN )
1594  {
1595  SCIPerrorMessage("branching rule <%s> returned invalid result code <%d> from external solution branching\n",
1596  branchrule->name, *result);
1597  return SCIP_INVALIDRESULT;
1598  }
1599  if( *result == SCIP_CONSADDED && !allowaddcons )
1600  {
1601  SCIPerrorMessage("branching rule <%s> added a constraint in external solution branching without permission\n",
1602  branchrule->name);
1603  return SCIP_INVALIDRESULT;
1604  }
1605 
1606  /* update statistics */
1607  if( *result != SCIP_DIDNOTRUN )
1608  branchrule->nexterncalls++;
1609  if( *result == SCIP_CUTOFF )
1610  branchrule->ncutoffs++;
1611  if( *result != SCIP_BRANCHED )
1612  {
1613  assert(tree->nchildren == 0);
1614 
1615  /* update domain reductions; therefore remove the domain
1616  * reduction counts which were generated in probing mode */
1617  branchrule->ndomredsfound += stat->nboundchgs + stat->nholechgs - oldndomchgs;
1618  branchrule->ndomredsfound -= (stat->nprobboundchgs + stat->nprobholechgs - oldnprobdomchgs);
1619 
1620  branchrule->ncutsfound += SCIPsepastoreGetNCuts(sepastore) - oldncuts; /*lint !e776*/
1621  branchrule->nconssfound += stat->nactiveconss - oldnactiveconss; /*lint !e776*/
1622  }
1623  else
1624  branchrule->nchildren += tree->nchildren;
1625  }
1626  }
1627  return SCIP_OKAY;
1628 }
1629 
1630 /** executes branching rule for not completely fixed pseudo solution */
1632  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1633  SCIP_SET* set, /**< global SCIP settings */
1634  SCIP_STAT* stat, /**< problem statistics */
1635  SCIP_TREE* tree, /**< branch and bound tree */
1636  SCIP_Real cutoffbound, /**< global upper cutoff bound */
1637  SCIP_Bool allowaddcons, /**< should adding constraints be allowed to avoid a branching? */
1638  SCIP_RESULT* result /**< pointer to store the result of the callback method */
1639  )
1640 {
1641  assert(branchrule != NULL);
1642  assert(set != NULL);
1643  assert(tree != NULL);
1644  assert(tree->nchildren == 0);
1645  assert(result != NULL);
1646 
1647  *result = SCIP_DIDNOTRUN;
1648  if( branchrule->branchexecps != NULL
1649  && (branchrule->maxdepth == -1 || branchrule->maxdepth >= SCIPtreeGetCurrentDepth(tree)) )
1650  {
1651  SCIP_Real loclowerbound;
1652  SCIP_Real glblowerbound;
1653 
1654  loclowerbound = SCIPnodeGetLowerbound(tree->focusnode);
1655  glblowerbound = SCIPtreeGetLowerbound(tree, set);
1656  if( SCIPsetIsLE(set, loclowerbound - glblowerbound, branchrule->maxbounddist * (cutoffbound - glblowerbound)) )
1657  {
1658  SCIP_Longint oldndomchgs;
1659  SCIP_Longint oldnprobdomchgs;
1660  SCIP_Longint oldnactiveconss;
1661 
1662  SCIPdebugMessage("executing pseudo branching rule <%s>\n", branchrule->name);
1663 
1664  oldndomchgs = stat->nboundchgs + stat->nholechgs;
1665  oldnprobdomchgs = stat->nprobboundchgs + stat->nprobholechgs;
1666  oldnactiveconss = stat->nactiveconss;
1667 
1668  /* start timing */
1669  SCIPclockStart(branchrule->branchclock, set);
1670 
1671  /* call external method */
1672  SCIP_CALL( branchrule->branchexecps(set->scip, branchrule, allowaddcons, result) );
1673 
1674  /* stop timing */
1675  SCIPclockStop(branchrule->branchclock, set);
1676 
1677  /* evaluate result */
1678  if( *result != SCIP_CUTOFF
1679  && *result != SCIP_CONSADDED
1680  && *result != SCIP_REDUCEDDOM
1681  && *result != SCIP_BRANCHED
1682  && *result != SCIP_DIDNOTFIND
1683  && *result != SCIP_DIDNOTRUN )
1684  {
1685  SCIPerrorMessage("branching rule <%s> returned invalid result code <%d> from pseudo solution branching\n",
1686  branchrule->name, *result);
1687  return SCIP_INVALIDRESULT;
1688  }
1689  if( *result == SCIP_CONSADDED && !allowaddcons )
1690  {
1691  SCIPerrorMessage("branching rule <%s> added a constraint in pseudo solution branching without permission\n",
1692  branchrule->name);
1693  return SCIP_INVALIDRESULT;
1694  }
1695 
1696  /* update statistics */
1697  if( *result != SCIP_DIDNOTRUN )
1698  branchrule->npseudocalls++;
1699  if( *result == SCIP_CUTOFF )
1700  branchrule->ncutoffs++;
1701  if( *result != SCIP_BRANCHED )
1702  {
1703  assert(tree->nchildren == 0);
1704 
1705  /* update domain reductions; therefore remove the domain
1706  * reduction counts which were generated in probing mode */
1707  branchrule->ndomredsfound += stat->nboundchgs + stat->nholechgs - oldndomchgs;
1708  branchrule->ndomredsfound -= (stat->nprobboundchgs + stat->nprobholechgs - oldnprobdomchgs);
1709 
1710  branchrule->nconssfound += stat->nactiveconss - oldnactiveconss;
1711  }
1712  else
1713  branchrule->nchildren += tree->nchildren;
1714  }
1715  }
1716 
1717  return SCIP_OKAY;
1718 }
1719 
1720 /** gets user data of branching rule */
1722  SCIP_BRANCHRULE* branchrule /**< branching rule */
1723  )
1724 {
1725  assert(branchrule != NULL);
1726 
1727  return branchrule->branchruledata;
1728 }
1729 
1730 /** sets user data of branching rule; user has to free old data in advance! */
1732  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1733  SCIP_BRANCHRULEDATA* branchruledata /**< new branching rule user data */
1734  )
1735 {
1736  assert(branchrule != NULL);
1737 
1738  branchrule->branchruledata = branchruledata;
1739 }
1740 
1741 /** sets copy method of branching rule */
1743  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1744  SCIP_DECL_BRANCHCOPY ((*branchcopy)) /**< copy method of branching rule or NULL if you don't want to copy your plugin into sub-SCIPs */
1745  )
1746 {
1747  assert(branchrule != NULL);
1748 
1749  branchrule->branchcopy = branchcopy;
1750 }
1751 
1752 /** sets destructor method of branching rule */
1754  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1755  SCIP_DECL_BRANCHFREE ((*branchfree)) /**< destructor of branching rule */
1756  )
1757 {
1758  assert(branchrule != NULL);
1759 
1760  branchrule->branchfree = branchfree;
1761 }
1762 
1763 /** sets initialization method of branching rule */
1765  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1766  SCIP_DECL_BRANCHINIT ((*branchinit)) /**< initialize branching rule */
1767  )
1768 {
1769  assert(branchrule != NULL);
1770 
1771  branchrule->branchinit = branchinit;
1772 }
1773 
1774 /** sets deinitialization method of branching rule */
1776  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1777  SCIP_DECL_BRANCHEXIT ((*branchexit)) /**< deinitialize branching rule */
1778  )
1779 {
1780  assert(branchrule != NULL);
1781 
1782  branchrule->branchexit = branchexit;
1783 }
1784 
1785 /** sets solving process initialization method of branching rule */
1787  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1788  SCIP_DECL_BRANCHINITSOL((*branchinitsol)) /**< solving process initialization method of branching rule */
1789  )
1790 {
1791  assert(branchrule != NULL);
1792 
1793  branchrule->branchinitsol = branchinitsol;
1794 }
1795 
1796 /** sets solving process deinitialization method of branching rule */
1798  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1799  SCIP_DECL_BRANCHEXITSOL((*branchexitsol)) /**< solving process deinitialization method of branching rule */
1800  )
1801 {
1802  assert(branchrule != NULL);
1803 
1804  branchrule->branchexitsol = branchexitsol;
1805 }
1806 
1807 
1808 
1809 /** sets branching execution method for fractional LP solutions */
1811  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1812  SCIP_DECL_BRANCHEXECLP((*branchexeclp)) /**< branching execution method for fractional LP solutions */
1813  )
1814 {
1815  assert(branchrule != NULL);
1816 
1817  branchrule->branchexeclp = branchexeclp;
1818 }
1819 
1820 /** sets branching execution method for external candidates */
1822  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1823  SCIP_DECL_BRANCHEXECEXT((*branchexecext)) /**< branching execution method for external candidates */
1824  )
1825 {
1826  assert(branchrule != NULL);
1827 
1828  branchrule->branchexecext = branchexecext;
1829 }
1830 
1831 /** sets branching execution method for not completely fixed pseudo solutions */
1833  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1834  SCIP_DECL_BRANCHEXECPS((*branchexecps)) /**< branching execution method for not completely fixed pseudo solutions */
1835  )
1836 {
1837  assert(branchrule != NULL);
1838 
1839  branchrule->branchexecps = branchexecps;
1840 }
1841 
1842 /** gets name of branching rule */
1844  SCIP_BRANCHRULE* branchrule /**< branching rule */
1845  )
1846 {
1847  assert(branchrule != NULL);
1848 
1849  return branchrule->name;
1850 }
1851 
1852 /** gets description of branching rule */
1854  SCIP_BRANCHRULE* branchrule /**< branching rule */
1855  )
1856 {
1857  assert(branchrule != NULL);
1858 
1859  return branchrule->desc;
1860 }
1861 
1862 /** gets priority of branching rule */
1864  SCIP_BRANCHRULE* branchrule /**< branching rule */
1865  )
1866 {
1867  assert(branchrule != NULL);
1868 
1869  return branchrule->priority;
1870 }
1871 
1872 /** sets priority of branching rule */
1874  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1875  SCIP_SET* set, /**< global SCIP settings */
1876  int priority /**< new priority of the branching rule */
1877  )
1878 {
1879  assert(branchrule != NULL);
1880  assert(set != NULL);
1881 
1882  branchrule->priority = priority;
1883  set->branchrulessorted = FALSE;
1884 }
1885 
1886 /** gets maximal depth level, up to which this branching rule should be used (-1 for no limit) */
1888  SCIP_BRANCHRULE* branchrule /**< branching rule */
1889  )
1890 {
1891  assert(branchrule != NULL);
1892 
1893  return branchrule->maxdepth;
1894 }
1895 
1896 /** sets maximal depth level, up to which this branching rule should be used (-1 for no limit) */
1898  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1899  int maxdepth /**< new maxdepth of the branching rule */
1900  )
1901 {
1902  assert(branchrule != NULL);
1903  assert(maxdepth >= -1);
1904 
1905  branchrule->maxdepth = maxdepth;
1906 }
1907 
1908 /** gets maximal relative distance from current node's dual bound to primal bound for applying branching rule */
1910  SCIP_BRANCHRULE* branchrule /**< branching rule */
1911  )
1912 {
1913  assert(branchrule != NULL);
1914 
1915  return branchrule->maxbounddist;
1916 }
1917 
1918 /** sets maximal relative distance from current node's dual bound to primal bound for applying branching rule */
1920  SCIP_BRANCHRULE* branchrule, /**< branching rule */
1921  SCIP_Real maxbounddist /**< new maxbounddist of the branching rule */
1922  )
1923 {
1924  assert(branchrule != NULL);
1925  assert(maxbounddist >= -1);
1926 
1927  branchrule->maxbounddist = maxbounddist;
1928 }
1929 
1930 /** gets time in seconds used in this branching rule for setting up for next stages */
1932  SCIP_BRANCHRULE* branchrule /**< branching rule */
1933  )
1934 {
1935  assert(branchrule != NULL);
1936 
1937  return SCIPclockGetTime(branchrule->setuptime);
1938 }
1939 
1940 /** gets time in seconds used in this branching rule */
1942  SCIP_BRANCHRULE* branchrule /**< branching rule */
1943  )
1944 {
1945  assert(branchrule != NULL);
1946 
1947  return SCIPclockGetTime(branchrule->branchclock);
1948 }
1949 
1950 /** gets the total number of times, the branching rule was called on an LP solution */
1952  SCIP_BRANCHRULE* branchrule /**< branching rule */
1953  )
1954 {
1955  assert(branchrule != NULL);
1956 
1957  return branchrule->nlpcalls;
1958 }
1959 
1960 /** gets the total number of times, the branching rule was called on an external solution */
1962  SCIP_BRANCHRULE* branchrule /**< branching rule */
1963  )
1964 {
1965  assert(branchrule != NULL);
1966 
1967  return branchrule->nexterncalls;
1968 }
1969 
1970 /** gets the total number of times, the branching rule was called on a pseudo solution */
1972  SCIP_BRANCHRULE* branchrule /**< branching rule */
1973  )
1974 {
1975  assert(branchrule != NULL);
1976 
1977  return branchrule->npseudocalls;
1978 }
1979 
1980 /** gets the total number of times, the branching rule detected a cutoff */
1982  SCIP_BRANCHRULE* branchrule /**< branching rule */
1983  )
1984 {
1985  assert(branchrule != NULL);
1986 
1987  return branchrule->ncutoffs;
1988 }
1989 
1990 /** gets the total number of cuts, the branching rule separated */
1992  SCIP_BRANCHRULE* branchrule /**< branching rule */
1993  )
1994 {
1995  assert(branchrule != NULL);
1996 
1997  return branchrule->ncutsfound;
1998 }
1999 
2000 /** gets the total number of constraints, the branching rule added to the respective local nodes (not counting constraints
2001  * that were added to the child nodes as branching decisions)
2002  */
2004  SCIP_BRANCHRULE* branchrule /**< branching rule */
2005  )
2006 {
2007  assert(branchrule != NULL);
2008 
2009  return branchrule->nconssfound;
2010 }
2011 
2012 /** gets the total number of domain reductions, the branching rule found */
2014  SCIP_BRANCHRULE* branchrule /**< branching rule */
2015  )
2016 {
2017  assert(branchrule != NULL);
2018 
2019  return branchrule->ndomredsfound;
2020 }
2021 
2022 /** gets the total number of children, the branching rule created */
2024  SCIP_BRANCHRULE* branchrule /**< branching rule */
2025  )
2026 {
2027  assert(branchrule != NULL);
2028 
2029  return branchrule->nchildren;
2030 }
2031 
2032 /** is branching rule initialized? */
2034  SCIP_BRANCHRULE* branchrule /**< branching rule */
2035  )
2036 {
2037  assert(branchrule != NULL);
2038 
2039  return branchrule->initialized;
2040 }
2041 
2042 
2043 
2044 
2045 /*
2046  * branching methods
2047  */
2048 
2049 /** calculates the branching score out of the gain predictions for a binary branching */
2051  SCIP_SET* set, /**< global SCIP settings */
2052  SCIP_VAR* var, /**< variable, of which the branching factor should be applied, or NULL */
2053  SCIP_Real downgain, /**< prediction of objective gain for rounding downwards */
2054  SCIP_Real upgain /**< prediction of objective gain for rounding upwards */
2055  )
2056 {
2057  SCIP_Real score;
2058  SCIP_Real eps;
2059 
2060  assert(set != NULL);
2061 
2062  /* slightly increase gains, such that for zero gains, the branch factor comes into account */
2063  eps = SCIPsetSumepsilon(set);
2064  downgain = MAX(downgain, eps);
2065  upgain = MAX(upgain, eps);
2066 
2067  switch( set->branch_scorefunc )
2068  {
2069  case 's': /* linear sum score function */
2070  /* weigh the two child nodes with branchscorefac and 1-branchscorefac */
2071  if( downgain > upgain )
2072  score = set->branch_scorefac * downgain + (1.0-set->branch_scorefac) * upgain;
2073  else
2074  score = set->branch_scorefac * upgain + (1.0-set->branch_scorefac) * downgain;
2075  break;
2076 
2077  case 'p': /* product score function */
2078  score = downgain * upgain;
2079  break;
2080  case 'q': /* quotient score function */
2081  if( downgain > upgain )
2082  score = upgain * upgain / downgain;
2083  else
2084  score = downgain * downgain / upgain;
2085  break;
2086  default:
2087  SCIPerrorMessage("invalid branching score function <%c>\n", set->branch_scorefunc);
2088  score = 0.0;
2089  SCIPABORT();
2090  }
2091 
2092  /* apply the branch factor of the variable */
2093  if( var != NULL )
2094  score *= SCIPvarGetBranchFactor(var);
2095 
2096  return score;
2097 }
2098 
2099 /** calculates the branching score out of the gain predictions for a branching with arbitrary many children */
2101  SCIP_SET* set, /**< global SCIP settings */
2102  SCIP_VAR* var, /**< variable, of which the branching factor should be applied, or NULL */
2103  int nchildren, /**< number of children that the branching will create */
2104  SCIP_Real* gains /**< prediction of objective gain for each child */
2105  )
2106 {
2107  SCIP_Real min1;
2108  SCIP_Real min2;
2109  int c;
2110 
2111  assert(nchildren == 0 || gains != NULL);
2112 
2113  /* search for the two minimal gains in the child list and use these to calculate the branching score */
2114  min1 = SCIPsetInfinity(set);
2115  min2 = SCIPsetInfinity(set);
2116  for( c = 0; c < nchildren; ++c )
2117  {
2118  if( gains[c] < min1 )
2119  {
2120  min2 = min1;
2121  min1 = gains[c];
2122  }
2123  else if( gains[c] < min2 )
2124  min2 = gains[c];
2125  }
2126 
2127  return SCIPbranchGetScore(set, var, min1, min2);
2128 }
2129 
2130 /** computes a branching point for a (not necessarily discrete) variable
2131  * a suggested branching point is first projected onto the box
2132  * if no point is suggested, then the value in the current LP or pseudo solution is used
2133  * if this value is at infinity, then 0.0 projected onto the bounds and then moved inside the interval is used
2134  * for a discrete variable, it is ensured that the returned value is fractional
2135  * for a continuous variable, the parameter branching/clamp defines how far a branching point need to be from the bounds of a variable
2136  * the latter is only applied if no point has been suggested, or the suggested point is not inside the variable's interval
2137  */
2139  SCIP_SET* set, /**< global SCIP settings */
2140  SCIP_TREE* tree, /**< branch and bound tree */
2141  SCIP_VAR* var, /**< variable, of which the branching point should be computed */
2142  SCIP_Real suggestion /**< suggestion for branching point, or SCIP_INVALID if no suggestion */
2143  )
2144 {
2145  SCIP_Real branchpoint;
2146  SCIP_Real lb;
2147  SCIP_Real ub;
2148 
2149  assert(set != NULL);
2150  assert(var != NULL);
2151 
2152  lb = SCIPvarGetLbLocal(var);
2153  ub = SCIPvarGetUbLocal(var);
2154 
2155  /* for a fixed variable, we cannot branch further */
2156  assert(!SCIPsetIsEQ(set, lb, ub));
2157 
2158  if( !SCIPsetIsInfinity(set, REALABS(suggestion)) )
2159  {
2160  /* use user suggested branching point */
2161 
2162  /* first, project it onto the current domain */
2163  branchpoint = MAX(lb, MIN(suggestion, ub));
2164 
2166  {
2167  /* if it is a discrete variable, then round it down and up and accept this choice */
2168  if( SCIPsetIsEQ(set, branchpoint, ub) )
2169  {
2170  /* in the right branch, variable is fixed to its current upper bound */
2171  return SCIPsetFloor(set, branchpoint) - 0.5;
2172  }
2173  else
2174  {
2175  /* in the left branch, variable is fixed to its current lower bound */
2176  return SCIPsetFloor(set, branchpoint) + 0.5;
2177  }
2178  }
2179  else if( (SCIPsetIsInfinity(set, -lb) || SCIPsetIsRelGT(set, branchpoint, lb)) &&
2180  (SCIPsetIsInfinity(set, ub) || SCIPsetIsRelLT(set, branchpoint, ub)) )
2181  {
2182  /* if it is continuous and inside the box, then accept it */
2183  return branchpoint;
2184  }
2185  /* if it is continuous and suggestion is on of the bounds, continue below */
2186  }
2187  else
2188  {
2189  /* if no point is suggested and the value in LP solution is not too big, try the LP or pseudo LP solution
2190  * otherwise, if the value in the LP or pseudosolution is large (here 1e+12), use 0.0
2191  * in both cases, project onto bounds of course
2192  */
2193  branchpoint = SCIPvarGetSol(var, SCIPtreeHasCurrentNodeLP(tree));
2194  if( REALABS(branchpoint) > 1e+12 )
2195  branchpoint = 0.0;
2196  branchpoint = MAX(lb, MIN(branchpoint, ub));
2197  }
2198 
2199  /* if value is at +/- infty, then choose some value a bit off from bounds or 0.0 */
2200  if( SCIPsetIsInfinity(set, branchpoint) )
2201  {
2202  /* if value is at +infty, then the upper bound should be at infinity */
2203  assert(SCIPsetIsInfinity(set, ub));
2204 
2205  /* choose 0.0 or something above lower bound if lower bound > 0 */
2206  if( SCIPsetIsPositive(set, lb) )
2207  branchpoint = lb + 1000.0;
2208  else
2209  branchpoint = 0.0;
2210  }
2211  else if( SCIPsetIsInfinity(set, -branchpoint) )
2212  {
2213  /* if value is at -infty, then the lower bound should be at -infinity */
2214  assert(SCIPsetIsInfinity(set, -lb));
2215 
2216  /* choose 0.0 or something below upper bound if upper bound < 0 */
2217  if( SCIPsetIsNegative(set, ub) )
2218  branchpoint = ub - 1000.0;
2219  else
2220  branchpoint = 0.0;
2221  }
2222 
2223  assert(SCIPsetIsInfinity(set, ub) || SCIPsetIsLE(set, branchpoint, ub));
2224  assert(SCIPsetIsInfinity(set, -lb) || SCIPsetIsGE(set, branchpoint, lb));
2225 
2227  {
2228  if( !SCIPsetIsInfinity(set, -lb) && !SCIPsetIsInfinity(set, ub) )
2229  {
2230  /* if branching point is too close to the bounds, move more into the middle of the interval */
2231  if( SCIPrelDiff(ub, lb) <= 2.02 * SCIPsetEpsilon(set) )
2232  {
2233  /* for very tiny intervals we set it exactly into the middle
2234  * very tiny means here an interval where we could not create two branches with reldiff > eps */
2235  branchpoint = (lb+ub)/2.0;
2236  }
2237  else
2238  {
2239  /* otherwise we project it away from the bounds */
2240  SCIP_Real minbrpoint;
2241  SCIP_Real maxbrpoint;
2242  SCIP_Real scale;
2243  SCIP_Real lbabs;
2244  SCIP_Real ubabs;
2245 
2246  lbabs = REALABS(lb);
2247  ubabs = REALABS(ub);
2248 
2249  scale = MAX3(lbabs, ubabs, 1.0);
2250 
2251  /* the minimal branching point should be
2252  * - set->clamp away from the lower bound - relative to the local domain size
2253  * - SCIPsetEpsilon(set)*scale above the lower bound - in absolute value
2254  */
2255  minbrpoint = (1.0 - set->branch_clamp) * lb + set->branch_clamp * ub;
2256  minbrpoint = MAX(lb + 1.01*SCIPsetEpsilon(set)*scale, minbrpoint); /*lint !e666*/
2257 
2258  /* the maximal branching point should be
2259  * - set->clamp away from the upper bound - relative to the local domain size
2260  * - SCIPsetEpsilon(set)*scale below the upper bound - in absolute value
2261  */
2262  maxbrpoint = set->branch_clamp * lb + (1.0 - set->branch_clamp) * ub;
2263  maxbrpoint = MIN(ub - 1.01*SCIPsetEpsilon(set)*scale, maxbrpoint); /*lint !e666*/
2264 
2265  /* project branchpoint into [minbrpoint, maxbrpoint] */
2266  branchpoint = MAX(minbrpoint, MIN(branchpoint, maxbrpoint));
2267 
2268  /* if selected branching point is close to 0.0 and bounds are away from 0.0, it often makes sense to branch exactly on 0.0 */
2269  if( SCIPsetIsFeasZero(set, branchpoint) && SCIPsetIsFeasNegative(set, lb) && SCIPsetIsFeasPositive(set, ub) )
2270  branchpoint = 0.0;
2271 
2272  assert(SCIPsetIsRelLT(set, lb, branchpoint));
2273  assert(SCIPsetIsRelLT(set, branchpoint, ub));
2274  }
2275  }
2276  else if( SCIPsetIsRelEQ(set, lb, ub) )
2277  {
2278  /* variable is almost fixed at -/+ infinity, so suggest this infinity as branching point, what else could we do? */
2279  branchpoint = lb < 0.0 ? -SCIPsetInfinity(set) : SCIPsetInfinity(set);
2280  }
2281  else if( !SCIPsetIsRelLT(set, lb, branchpoint) )
2282  {
2283  SCIP_Real lbabs;
2284  SCIP_Real delta1;
2285  SCIP_Real delta2;
2286 
2287  /* if branching point is too close to the lower bound and there is no upper bound, then move it to somewhere above the lower bound, but not above infinity */
2288  assert(!SCIPsetIsInfinity(set, -lb));
2289  assert( SCIPsetIsInfinity(set, ub));
2290  lbabs = REALABS(lb);
2291  delta1 = MAX(0.5 * lbabs, 1000);
2292  delta2 = 0.9*(SCIPsetInfinity(set)-lb);
2293  branchpoint = lb + MIN(delta1, delta2);
2294  }
2295  else if( !SCIPsetIsGT(set, ub, branchpoint) )
2296  {
2297  SCIP_Real ubabs;
2298  SCIP_Real delta1;
2299  SCIP_Real delta2;
2300 
2301  /* if branching point is too close to the upper bound and there is no lower bound, then move it to somewhere away from the upper bound, but not below infinity */
2302  assert( SCIPsetIsInfinity(set, -lb));
2303  assert(!SCIPsetIsInfinity(set, ub));
2304  ubabs = REALABS(ub);
2305  delta1 = MAX(0.5 * ubabs, 1000);
2306  delta2 = 0.9*(ub+SCIPsetInfinity(set));
2307  branchpoint = ub - MIN(delta1, delta2);
2308  }
2309 
2310  return branchpoint;
2311  }
2312  else
2313  {
2314  /* discrete variables */
2315  if( branchpoint <= lb + 0.5 )
2316  {
2317  /* if branchpoint is on lower bound, create one branch with x = lb and one with x >= lb+1 */
2318  return lb + 0.5;
2319  }
2320  else if( branchpoint >= ub - 0.5 )
2321  {
2322  /* if branchpoint is on upper bound, create one branch with x = ub and one with x <= ub-1 */
2323  return ub - 0.5;
2324  }
2325  else if( SCIPsetIsIntegral(set, branchpoint) )
2326  {
2327  /* if branchpoint is integral but not on bounds, then it should be one of the value {lb+1, ..., ub-1} */
2328  assert(SCIPsetIsGE(set, branchpoint, lb + 1.0));
2329  assert(SCIPsetIsLE(set, branchpoint, ub - 1.0));
2330  /* if branchpoint is integral, create one branch with x <= x'-1 and one with x >= x'
2331  * @todo could in the same way be x <= x' and x >= x'+1; is there some easy way to know which is better? */
2332  return branchpoint - 0.5;
2333  }
2334  else
2335  {
2336  /* branchpoint is somewhere between bounds and fractional, so just round down and up */
2337  return branchpoint;
2338  }
2339  }
2340 
2341  SCIPerrorMessage("you should not be here, this should not happen\n"); /*lint !e527*/
2342  SCIPABORT(); /*lint --e{527}*/
2343  return SCIP_INVALID; /*lint --e{527}*/
2344 }
2345 
2346 /** calls branching rules to branch on an LP solution; if no fractional variables exist, the result is SCIP_DIDNOTRUN;
2347  * if the branch priority of an unfixed variable is larger than the maximal branch priority of the fractional
2348  * variables, pseudo solution branching is applied on the unfixed variables with maximal branch priority
2349  */
2351  BMS_BLKMEM* blkmem, /**< block memory for parameter settings */
2352  SCIP_SET* set, /**< global SCIP settings */
2353  SCIP_STAT* stat, /**< problem statistics */
2354  SCIP_PROB* transprob, /**< transformed problem after presolve */
2355  SCIP_PROB* origprob, /**< original problem */
2356  SCIP_TREE* tree, /**< branch and bound tree */
2357  SCIP_LP* lp, /**< current LP data */
2358  SCIP_SEPASTORE* sepastore, /**< separation storage */
2359  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
2360  SCIP_EVENTQUEUE* eventqueue, /**< event queue */
2361  SCIP_Real cutoffbound, /**< global upper cutoff bound */
2362  SCIP_Bool allowaddcons, /**< should adding constraints be allowed to avoid a branching? */
2363  SCIP_RESULT* result /**< pointer to store the result of the branching (s. branch.h) */
2364  )
2365 {
2366  int i;
2367  int nalllpcands; /* sum of binary, integer, and implicit branching candidates */
2368 
2369  assert(branchcand != NULL);
2370  assert(result != NULL);
2371 
2372  *result = SCIP_DIDNOTRUN;
2373 
2374  /* calculate branching candidates */
2375  SCIP_CALL( branchcandCalcLPCands(branchcand, set, stat, lp) );
2376  assert(0 <= branchcand->npriolpcands && branchcand->npriolpcands <= branchcand->nlpcands);
2377  assert((branchcand->npriolpcands == 0) == (branchcand->nlpcands == 0));
2378 
2379  SCIPdebugMessage("branching on LP solution with %d (+%d) fractional (+implicit fractional) variables (%d of maximal priority)\n",
2380  branchcand->nlpcands, branchcand->nimpllpfracs, branchcand->npriolpcands);
2381 
2382  nalllpcands = branchcand->nlpcands + branchcand->nimpllpfracs;
2383  /* do nothing, if no fractional variables exist */
2384  if( nalllpcands == 0 )
2385  return SCIP_OKAY;
2386 
2387  /* if there is a non-fixed variable with higher priority than the maximal priority of the fractional candidates,
2388  * use pseudo solution branching instead
2389  */
2390  if( branchcand->pseudomaxpriority > branchcand->lpmaxpriority )
2391  {
2392  SCIP_CALL( SCIPbranchExecPseudo(blkmem, set, stat, transprob, origprob, tree, lp, branchcand, eventqueue, cutoffbound,
2393  allowaddcons, result) );
2394  assert(*result != SCIP_DIDNOTRUN && *result != SCIP_DIDNOTFIND);
2395  return SCIP_OKAY;
2396  }
2397 
2398  /* sort the branching rules by priority */
2400 
2401  /* try all branching rules until one succeeded to branch */
2402  for( i = 0; i < set->nbranchrules && (*result == SCIP_DIDNOTRUN || *result == SCIP_DIDNOTFIND); ++i )
2403  {
2404  SCIP_CALL( SCIPbranchruleExecLPSol(set->branchrules[i], set, stat, tree, sepastore, cutoffbound, allowaddcons, result) );
2405  }
2406 
2407  if( *result == SCIP_DIDNOTRUN || *result == SCIP_DIDNOTFIND )
2408  {
2409  SCIP_VAR* var;
2410  SCIP_Real factor;
2411  SCIP_Real bestfactor;
2412  int priority;
2413  int bestpriority;
2414  int bestcand;
2415 
2416  /* no branching method succeeded in choosing a branching: just branch on the first fractional variable with maximal
2417  * priority, and out of these on the one with maximal branch factor
2418  */
2419  bestcand = -1;
2420  bestpriority = INT_MIN;
2421  bestfactor = SCIP_REAL_MIN;
2422  for( i = 0; i < nalllpcands; ++i )
2423  {
2424  priority = SCIPvarGetBranchPriority(branchcand->lpcands[i]);
2425  factor = SCIPvarGetBranchFactor(branchcand->lpcands[i]);
2426  if( priority > bestpriority || (priority == bestpriority && factor > bestfactor) )
2427  {
2428  bestcand = i;
2429  bestpriority = priority;
2430  bestfactor = factor;
2431  }
2432  }
2433  assert(0 <= bestcand && bestcand < nalllpcands);
2434 
2435  var = branchcand->lpcands[bestcand];
2436  assert(SCIPvarGetType(var) != SCIP_VARTYPE_CONTINUOUS);
2437  assert(branchcand->nlpcands == 0 || SCIPvarGetType(var) != SCIP_VARTYPE_IMPLINT);
2438 
2439  assert(!SCIPsetIsEQ(set, SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var)));
2440 
2441  SCIP_CALL( SCIPtreeBranchVar(tree, blkmem, set, stat, transprob, origprob, lp, branchcand, eventqueue, var, SCIP_INVALID,
2442  NULL, NULL, NULL) );
2443 
2444  *result = SCIP_BRANCHED;
2445  }
2446 
2447  return SCIP_OKAY;
2448 }
2449 
2450 /** calls branching rules to branch on an external solution; if no external branching candidates exist, the result is SCIP_DIDNOTRUN */
2452  BMS_BLKMEM* blkmem, /**< block memory for parameter settings */
2453  SCIP_SET* set, /**< global SCIP settings */
2454  SCIP_STAT* stat, /**< problem statistics */
2455  SCIP_PROB* transprob, /**< transformed problem after presolve */
2456  SCIP_PROB* origprob, /**< original problem */
2457  SCIP_TREE* tree, /**< branch and bound tree */
2458  SCIP_LP* lp, /**< current LP data */
2459  SCIP_SEPASTORE* sepastore, /**< separation storage */
2460  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
2461  SCIP_EVENTQUEUE* eventqueue, /**< event queue */
2462  SCIP_Real cutoffbound, /**< global upper cutoff bound */
2463  SCIP_Bool allowaddcons, /**< should adding constraints be allowed to avoid a branching? */
2464  SCIP_RESULT* result /**< pointer to store the result of the branching (s. branch.h) */
2465  )
2466 {
2467  int i;
2468 
2469  assert(branchcand != NULL);
2470  assert(result != NULL);
2471  assert(0 <= branchcand->nprioexterncands && branchcand->nprioexterncands <= branchcand->nexterncands);
2472  assert((branchcand->nprioexterncands == 0) == (branchcand->nexterncands == 0));
2473 
2474  *result = SCIP_DIDNOTRUN;
2475 
2476  SCIPdebugMessage("branching on external solution with %d branching candidates (%d of maximal priority)\n",
2477  branchcand->nexterncands, branchcand->nprioexterncands);
2478 
2479  /* do nothing, if no external candidates exist */
2480  if( branchcand->nexterncands == 0 )
2481  return SCIP_OKAY;
2482 
2483  /* if there is a non-fixed variable with higher priority than the maximal priority of the external candidates,
2484  * use pseudo solution branching instead
2485  */
2486  if( branchcand->pseudomaxpriority > branchcand->externmaxpriority )
2487  {
2488  /* @todo: adjust this, that also LP branching might be called, if lpmaxpriority != externmaxpriority.
2489  * Therefor, it has to be clear which of both has the higher priority
2490  */
2491  SCIP_CALL( SCIPbranchExecPseudo(blkmem, set, stat, transprob, origprob, tree, lp, branchcand, eventqueue, cutoffbound,
2492  allowaddcons, result) );
2493  assert(*result != SCIP_DIDNOTRUN && *result != SCIP_DIDNOTFIND);
2494  return SCIP_OKAY;
2495  }
2496 
2497  /* sort the branching rules by priority */
2499 
2500  /* try all branching rules until one succeeded to branch */
2501  for( i = 0; i < set->nbranchrules && (*result == SCIP_DIDNOTRUN || *result == SCIP_DIDNOTFIND); ++i )
2502  {
2503  SCIP_CALL( SCIPbranchruleExecExternSol(set->branchrules[i], set, stat, tree, sepastore, cutoffbound, allowaddcons, result) );
2504  }
2505 
2506  if( *result == SCIP_DIDNOTRUN || *result == SCIP_DIDNOTFIND )
2507  {
2508  SCIP_VAR* var;
2509  SCIP_Real val;
2510  SCIP_Real bestfactor;
2511  SCIP_Real bestdomain;
2512  int bestpriority;
2513  int bestcand;
2514 
2515  /* if all branching rules did nothing, then they should also not have cleared all branching candidates */
2516  assert(branchcand->nexterncands > 0);
2517 
2518  /* no branching method succeeded in choosing a branching: just branch on the first branching candidates with maximal
2519  * priority, and out of these on the one with maximal branch factor, and out of these on the one with largest domain
2520  */
2521  bestcand = -1;
2522  bestpriority = INT_MIN;
2523  bestfactor = SCIP_REAL_MIN;
2524  bestdomain = 0.0;
2525  for( i = 0; i < branchcand->nexterncands; ++i )
2526  {
2527  SCIP_VAR* cand;
2528  SCIP_Real domain;
2529  SCIP_Real factor;
2530  int priority;
2531 
2532  cand = branchcand->externcands[i];
2533  priority = SCIPvarGetBranchPriority(cand);
2534  factor = SCIPvarGetBranchFactor(cand);
2535 
2536  /* the domain size is infinite, iff one of the bounds is infinite */
2538  domain = SCIPsetInfinity(set);
2539  else
2540  domain = SCIPvarGetUbLocal(cand) - SCIPvarGetLbLocal(cand);
2541 
2542  /* choose variable with higher priority, higher factor, larger domain (in that order) */
2543  if( priority > bestpriority || (priority == bestpriority && factor > bestfactor) || (priority == bestpriority && factor == bestfactor && domain > bestdomain) ) /*lint !e777*/
2544  {
2545  bestcand = i;
2546  bestpriority = priority;
2547  bestfactor = factor;
2548  bestdomain = domain;
2549  }
2550  }
2551  assert(0 <= bestcand && bestcand < branchcand->nexterncands);
2552 
2553  var = branchcand->externcands[bestcand];
2554  val = SCIPbranchGetBranchingPoint(set, tree, var, branchcand->externcandssol[bestcand]);
2555  assert(!SCIPsetIsEQ(set, SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var)));
2556  assert(SCIPsetIsLT(set, SCIPvarGetLbLocal(var), val));
2557  assert(SCIPsetIsLT(set, val, SCIPvarGetUbLocal(var)));
2558 
2559  SCIPdebugMessage("no branching method succeeded; fallback selected to branch on variable <%s> with bounds [%g, %g] on value %g\n",
2560  SCIPvarGetName(var), SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var), val);
2561 
2562  SCIP_CALL( SCIPtreeBranchVar(tree, blkmem, set, stat, transprob, origprob, lp, branchcand, eventqueue, var, val,
2563  NULL, NULL, NULL) );
2564 
2565  *result = SCIP_BRANCHED;
2566  }
2567 
2568  return SCIP_OKAY;
2569 }
2570 
2571 /** calls branching rules to branch on a pseudo solution; if no unfixed variables exist, the result is SCIP_DIDNOTRUN */
2573  BMS_BLKMEM* blkmem, /**< block memory for parameter settings */
2574  SCIP_SET* set, /**< global SCIP settings */
2575  SCIP_STAT* stat, /**< problem statistics */
2576  SCIP_PROB* transprob, /**< transformed problem after presolve */
2577  SCIP_PROB* origprob, /**< original problem */
2578  SCIP_TREE* tree, /**< branch and bound tree */
2579  SCIP_LP* lp, /**< current LP data */
2580  SCIP_BRANCHCAND* branchcand, /**< branching candidate storage */
2581  SCIP_EVENTQUEUE* eventqueue, /**< event queue */
2582  SCIP_Real cutoffbound, /**< global upper cutoff bound */
2583  SCIP_Bool allowaddcons, /**< should adding constraints be allowed to avoid a branching? */
2584  SCIP_RESULT* result /**< pointer to store the result of the branching (s. branch.h) */
2585  )
2586 {
2587  int i;
2588 
2589  assert(branchcand != NULL);
2590  assert(result != NULL);
2591 
2592  SCIPdebugMessage("branching on pseudo solution with %d unfixed variables\n", branchcand->npseudocands);
2593 
2594  *result = SCIP_DIDNOTRUN;
2595 
2596  /* do nothing, if no unfixed variables exist */
2597  if( branchcand->npseudocands == 0 )
2598  return SCIP_OKAY;
2599 
2600  /* sort the branching rules by priority */
2602 
2603  /* try all branching rules until one succeeded to branch */
2604  for( i = 0; i < set->nbranchrules && (*result == SCIP_DIDNOTRUN || *result == SCIP_DIDNOTFIND); ++i )
2605  {
2606  SCIP_CALL( SCIPbranchruleExecPseudoSol(set->branchrules[i], set, stat, tree, cutoffbound, allowaddcons, result) );
2607  }
2608 
2609  if( *result == SCIP_DIDNOTRUN || *result == SCIP_DIDNOTFIND )
2610  {
2611  SCIP_VAR* var;
2612  SCIP_Real factor;
2613  SCIP_Real bestfactor;
2614  int priority;
2615  int bestpriority;
2616  int bestcand;
2617 
2618  /* no branching method succeeded in choosing a branching: just branch on the first unfixed variable with maximal
2619  * priority, and out of these on the one with maximal branch factor
2620  */
2621  bestcand = -1;
2622  bestpriority = INT_MIN;
2623  bestfactor = SCIP_REAL_MIN;
2624  for( i = 0; i < branchcand->npseudocands; ++i )
2625  {
2626  priority = SCIPvarGetBranchPriority(branchcand->pseudocands[i]);
2627  factor = SCIPvarGetBranchFactor(branchcand->pseudocands[i]);
2628  if( priority > bestpriority || (priority == bestpriority && factor > bestfactor) )
2629  {
2630  bestcand = i;
2631  bestpriority = priority;
2632  bestfactor = factor;
2633  }
2634  }
2635  assert(0 <= bestcand && bestcand < branchcand->npseudocands);
2636 
2637  var = branchcand->pseudocands[bestcand];
2638  assert(SCIPvarGetType(var) != SCIP_VARTYPE_CONTINUOUS);
2639  assert(!SCIPsetIsEQ(set, SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var)));
2640 
2641  SCIP_CALL( SCIPtreeBranchVar(tree, blkmem, set, stat, transprob, origprob, lp, branchcand, eventqueue, var, SCIP_INVALID,
2642  NULL, NULL, NULL) );
2643 
2644  *result = SCIP_BRANCHED;
2645  }
2646 
2647  return SCIP_OKAY;
2648 }
2649 
2650