Scippy

SCIP

Solving Constraint Integer Programs

cons_knapsack.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-2021 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file cons_knapsack.c
17  * @ingroup DEFPLUGINS_CONS
18  * @brief Constraint handler for knapsack constraints of the form \f$a^T x \le b\f$, x binary and \f$a \ge 0\f$.
19  * @author Tobias Achterberg
20  * @author Xin Liu
21  * @author Kati Wolter
22  * @author Michael Winkler
23  * @author Tobias Fischer
24  */
25 
26 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
27 
28 #include "blockmemshell/memory.h"
29 #include "scip/cons_knapsack.h"
30 #include "scip/cons_linear.h"
31 #include "scip/cons_logicor.h"
32 #include "scip/cons_setppc.h"
33 #include "scip/pub_cons.h"
34 #include "scip/pub_event.h"
35 #include "scip/pub_implics.h"
36 #include "scip/pub_lp.h"
37 #include "scip/pub_message.h"
38 #include "scip/pub_misc.h"
39 #include "scip/pub_misc_select.h"
40 #include "scip/pub_misc_sort.h"
41 #include "scip/pub_sepa.h"
42 #include "scip/pub_var.h"
43 #include "scip/scip_branch.h"
44 #include "scip/scip_conflict.h"
45 #include "scip/scip_cons.h"
46 #include "scip/scip_copy.h"
47 #include "scip/scip_cut.h"
48 #include "scip/scip_event.h"
49 #include "scip/scip_general.h"
50 #include "scip/scip_lp.h"
51 #include "scip/scip_mem.h"
52 #include "scip/scip_message.h"
53 #include "scip/scip_numerics.h"
54 #include "scip/scip_param.h"
55 #include "scip/scip_prob.h"
56 #include "scip/scip_probing.h"
57 #include "scip/scip_sol.h"
58 #include "scip/scip_solvingstats.h"
59 #include "scip/scip_tree.h"
60 #include "scip/scip_var.h"
61 #include <ctype.h>
62 #include <string.h>
63 
64 #ifdef WITH_CARDINALITY_UPGRADE
65 #include "scip/cons_cardinality.h"
66 #endif
67 
68 /* constraint handler properties */
69 #define CONSHDLR_NAME "knapsack"
70 #define CONSHDLR_DESC "knapsack constraint of the form a^T x <= b, x binary and a >= 0"
71 #define CONSHDLR_SEPAPRIORITY +600000 /**< priority of the constraint handler for separation */
72 #define CONSHDLR_ENFOPRIORITY -600000 /**< priority of the constraint handler for constraint enforcing */
73 #define CONSHDLR_CHECKPRIORITY -600000 /**< priority of the constraint handler for checking feasibility */
74 #define CONSHDLR_SEPAFREQ 0 /**< frequency for separating cuts; zero means to separate only in the root node */
75 #define CONSHDLR_PROPFREQ 1 /**< frequency for propagating domains; zero means only preprocessing propagation */
76 #define CONSHDLR_EAGERFREQ 100 /**< frequency for using all instead of only the useful constraints in separation,
77  * propagation and enforcement, -1 for no eager evaluations, 0 for first only */
78 #define CONSHDLR_MAXPREROUNDS -1 /**< maximal number of presolving rounds the constraint handler participates in (-1: no limit) */
79 #define CONSHDLR_DELAYSEPA FALSE /**< should separation method be delayed, if other separators found cuts? */
80 #define CONSHDLR_DELAYPROP FALSE /**< should propagation method be delayed, if other propagators found reductions? */
81 #define CONSHDLR_NEEDSCONS TRUE /**< should the constraint handler be skipped, if no constraints are available? */
82 
83 #define CONSHDLR_PRESOLTIMING SCIP_PRESOLTIMING_ALWAYS
84 #define CONSHDLR_PROP_TIMING SCIP_PROPTIMING_BEFORELP
85 
86 #define EVENTHDLR_NAME "knapsack"
87 #define EVENTHDLR_DESC "bound change event handler for knapsack constraints"
88 #define EVENTTYPE_KNAPSACK SCIP_EVENTTYPE_LBCHANGED \
89  | SCIP_EVENTTYPE_UBTIGHTENED \
90  | SCIP_EVENTTYPE_VARFIXED \
91  | SCIP_EVENTTYPE_VARDELETED \
92  | SCIP_EVENTTYPE_IMPLADDED /**< variable events that should be caught by the event handler */
93 
94 #define LINCONSUPGD_PRIORITY +100000 /**< priority of the constraint handler for upgrading of linear constraints */
95 
96 #define MAX_USECLIQUES_SIZE 1000 /**< maximal number of items in knapsack where clique information is used */
97 #define MAX_ZEROITEMS_SIZE 10000 /**< maximal number of items to store in the zero list in preprocessing */
98 
99 #define KNAPSACKRELAX_MAXDELTA 0.1 /**< maximal allowed rounding distance for scaling in knapsack relaxation */
100 #define KNAPSACKRELAX_MAXDNOM 1000LL /**< maximal allowed denominator in knapsack rational relaxation */
101 #define KNAPSACKRELAX_MAXSCALE 1000.0 /**< maximal allowed scaling factor in knapsack rational relaxation */
103 #define DEFAULT_SEPACARDFREQ 1 /**< multiplier on separation frequency, how often knapsack cuts are separated */
104 #define DEFAULT_MAXROUNDS 5 /**< maximal number of separation rounds per node (-1: unlimited) */
105 #define DEFAULT_MAXROUNDSROOT -1 /**< maximal number of separation rounds in the root node (-1: unlimited) */
106 #define DEFAULT_MAXSEPACUTS 50 /**< maximal number of cuts separated per separation round */
107 #define DEFAULT_MAXSEPACUTSROOT 200 /**< maximal number of cuts separated per separation round in the root node */
108 #define DEFAULT_MAXCARDBOUNDDIST 0.0 /**< maximal relative distance from current node's dual bound to primal bound compared
109  * to best node's dual bound for separating knapsack cuts */
110 #define DEFAULT_DISAGGREGATION TRUE /**< should disaggregation of knapsack constraints be allowed in preprocessing? */
111 #define DEFAULT_SIMPLIFYINEQUALITIES TRUE/**< should presolving try to simplify knapsacks */
112 #define DEFAULT_NEGATEDCLIQUE TRUE /**< should negated clique information be used in solving process */
114 #define MAXABSVBCOEF 1e+5 /**< maximal absolute coefficient in variable bounds used for knapsack relaxation */
115 #define USESUPADDLIFT FALSE /**< should lifted minimal cover inequalities using superadditive up-lifting be separated in addition */
117 #define DEFAULT_PRESOLUSEHASHING TRUE /**< should hash table be used for detecting redundant constraints in advance */
118 #define HASHSIZE_KNAPSACKCONS 500 /**< minimal size of hash table in linear constraint tables */
120 #define DEFAULT_PRESOLPAIRWISE TRUE /**< should pairwise constraint comparison be performed in presolving? */
121 #define NMINCOMPARISONS 200000 /**< number for minimal pairwise presolving comparisons */
122 #define MINGAINPERNMINCOMPARISONS 1e-06 /**< minimal gain per minimal pairwise presolving comparisons to repeat pairwise
123  * comparison round */
124 #define DEFAULT_DUALPRESOLVING TRUE /**< should dual presolving steps be performed? */
125 #define DEFAULT_DETECTCUTOFFBOUND TRUE /**< should presolving try to detect constraints parallel to the objective
126  * function defining an upper bound and prevent these constraints from
127  * entering the LP */
128 #define DEFAULT_DETECTLOWERBOUND TRUE /**< should presolving try to detect constraints parallel to the objective
129  * function defining a lower bound and prevent these constraints from
130  * entering the LP */
131 #define DEFAULT_CLIQUEEXTRACTFACTOR 0.5 /**< lower clique size limit for greedy clique extraction algorithm (relative to largest clique) */
132 #define MAXCOVERSIZEITERLEWI 1000 /**< maximal size for which LEWI are iteratively separated by reducing the feasible set */
134 #define DEFAULT_USEGUBS FALSE /**< should GUB information be used for separation? */
135 #define GUBCONSGROWVALUE 6 /**< memory growing value for GUB constraint array */
136 #define GUBSPLITGNC1GUBS FALSE /**< should GNC1 GUB conss without F vars be split into GOC1 and GR GUB conss? */
137 #define DEFAULT_CLQPARTUPDATEFAC 1.5 /**< factor on the growth of global cliques to decide when to update a previous
138  * (negated) clique partition (used only if updatecliquepartitions is set to TRUE) */
139 #define DEFAULT_UPDATECLIQUEPARTITIONS FALSE /**< should clique partition information be updated when old partition seems outdated? */
140 #define MAXNCLIQUEVARSCOMP 1000000 /**< limit on number of pairwise comparisons in clique partitioning algorithm */
141 #ifdef WITH_CARDINALITY_UPGRADE
142 #define DEFAULT_UPGDCARDINALITY FALSE /**< if TRUE then try to update knapsack constraints to cardinality constraints */
143 #endif
145 /* @todo maybe use event SCIP_EVENTTYPE_VARUNLOCKED to decide for another dual-presolving run on a constraint */
146 
147 /*
148  * Data structures
149  */
150 
151 /** constraint handler data */
152 struct SCIP_ConshdlrData
153 {
154  int* ints1; /**< cleared memory array, all entries are set to zero in initpre, if you use this
155  * you have to clear it at the end, exists only in presolving stage */
156  int* ints2; /**< cleared memory array, all entries are set to zero in initpre, if you use this
157  * you have to clear it at the end, exists only in presolving stage */
158  SCIP_Longint* longints1; /**< cleared memory array, all entries are set to zero in initpre, if you use this
159  * you have to clear it at the end, exists only in presolving stage */
160  SCIP_Longint* longints2; /**< cleared memory array, all entries are set to zero in initpre, if you use this
161  * you have to clear it at the end, exists only in presolving stage */
162  SCIP_Bool* bools1; /**< cleared memory array, all entries are set to zero in initpre, if you use this
163  * you have to clear it at the end, exists only in presolving stage */
164  SCIP_Bool* bools2; /**< cleared memory array, all entries are set to zero in initpre, if you use this
165  * you have to clear it at the end, exists only in presolving stage */
166  SCIP_Bool* bools3; /**< cleared memory array, all entries are set to zero in initpre, if you use this
167  * you have to clear it at the end, exists only in presolving stage */
168  SCIP_Bool* bools4; /**< cleared memory array, all entries are set to zero in initpre, if you use this
169  * you have to clear it at the end, exists only in presolving stage */
170  SCIP_Real* reals1; /**< cleared memory array, all entries are set to zero in consinit, if you use this
171  * you have to clear it at the end */
172  int ints1size; /**< size of ints1 array */
173  int ints2size; /**< size of ints2 array */
174  int longints1size; /**< size of longints1 array */
175  int longints2size; /**< size of longints2 array */
176  int bools1size; /**< size of bools1 array */
177  int bools2size; /**< size of bools2 array */
178  int bools3size; /**< size of bools3 array */
179  int bools4size; /**< size of bools4 array */
180  int reals1size; /**< size of reals1 array */
181  SCIP_EVENTHDLR* eventhdlr; /**< event handler for bound change events */
182  SCIP_Real maxcardbounddist; /**< maximal relative distance from current node's dual bound to primal bound compared
183  * to best node's dual bound for separating knapsack cuts */
184  int sepacardfreq; /**< multiplier on separation frequency, how often knapsack cuts are separated */
185  int maxrounds; /**< maximal number of separation rounds per node (-1: unlimited) */
186  int maxroundsroot; /**< maximal number of separation rounds in the root node (-1: unlimited) */
187  int maxsepacuts; /**< maximal number of cuts separated per separation round */
188  int maxsepacutsroot; /**< maximal number of cuts separated per separation round in the root node */
189  SCIP_Bool disaggregation; /**< should disaggregation of knapsack constraints be allowed in preprocessing? */
190  SCIP_Bool simplifyinequalities;/**< should presolving try to cancel down or delete coefficients in inequalities */
191  SCIP_Bool negatedclique; /**< should negated clique information be used in solving process */
192  SCIP_Bool presolpairwise; /**< should pairwise constraint comparison be performed in presolving? */
193  SCIP_Bool presolusehashing; /**< should hash table be used for detecting redundant constraints in advance */
194  SCIP_Bool dualpresolving; /**< should dual presolving steps be performed? */
195  SCIP_Bool usegubs; /**< should GUB information be used for separation? */
196  SCIP_Bool detectcutoffbound; /**< should presolving try to detect constraints parallel to the objective
197  * function defining an upper bound and prevent these constraints from
198  * entering the LP */
199  SCIP_Bool detectlowerbound; /**< should presolving try to detect constraints parallel to the objective
200  * function defining a lower bound and prevent these constraints from
201  * entering the LP */
202  SCIP_Bool updatecliquepartitions; /**< should clique partition information be updated when old partition seems outdated? */
203  SCIP_Real cliqueextractfactor;/**< lower clique size limit for greedy clique extraction algorithm (relative to largest clique) */
204  SCIP_Real clqpartupdatefac; /**< factor on the growth of global cliques to decide when to update a previous
205  * (negated) clique partition (used only if updatecliquepartitions is set to TRUE) */
206 #ifdef WITH_CARDINALITY_UPGRADE
207  SCIP_Bool upgdcardinality; /**< if TRUE then try to update knapsack constraints to cardinality constraints */
208  SCIP_Bool upgradedcard; /**< whether we have already upgraded knapsack constraints to cardinality constraints */
209 #endif
210 };
211 
212 
213 /** constraint data for knapsack constraints */
214 struct SCIP_ConsData
215 {
216  SCIP_VAR** vars; /**< variables in knapsack constraint */
217  SCIP_Longint* weights; /**< weights of variables in knapsack constraint */
218  SCIP_EVENTDATA** eventdata; /**< event data for bound change events of the variables */
219  int* cliquepartition; /**< clique indices of the clique partition */
220  int* negcliquepartition; /**< clique indices of the negated clique partition */
221  SCIP_ROW* row; /**< corresponding LP row */
222  int nvars; /**< number of variables in knapsack constraint */
223  int varssize; /**< size of vars, weights, and eventdata arrays */
224  int ncliques; /**< number of cliques in the clique partition */
225  int nnegcliques; /**< number of cliques in the negated clique partition */
226  int ncliqueslastnegpart;/**< number of global cliques the last time a negated clique partition was computed */
227  int ncliqueslastpart; /**< number of global cliques the last time a clique partition was computed */
228  SCIP_Longint capacity; /**< capacity of knapsack */
229  SCIP_Longint weightsum; /**< sum of all weights */
230  SCIP_Longint onesweightsum; /**< sum of weights of variables fixed to one */
231  unsigned int presolvedtiming:5; /**< max level in which the knapsack constraint is already presolved */
232  unsigned int sorted:1; /**< are the knapsack items sorted by weight? */
233  unsigned int cliquepartitioned:1;/**< is the clique partition valid? */
234  unsigned int negcliquepartitioned:1;/**< is the negated clique partition valid? */
235  unsigned int merged:1; /**< are the constraint's equal variables already merged? */
236  unsigned int cliquesadded:1; /**< were the cliques of the knapsack already added to clique table? */
237  unsigned int varsdeleted:1; /**< were variables deleted after last cleanup? */
238  unsigned int existmultaggr:1; /**< does this constraint contain multi-aggregations */
239 };
240 
241 /** event data for bound changes events */
242 struct SCIP_EventData
243 {
244  SCIP_CONS* cons; /**< knapsack constraint to process the bound change for */
245  SCIP_Longint weight; /**< weight of variable */
246  int filterpos; /**< position of event in variable's event filter */
247 };
248 
249 
250 /** data structure to combine two sorting key values */
251 struct sortkeypair
252 {
253  SCIP_Real key1; /**< first sort key value */
254  SCIP_Real key2; /**< second sort key value */
255 };
256 typedef struct sortkeypair SORTKEYPAIR;
257 
258 /** status of GUB constraint */
259 enum GUBVarstatus
260 {
261  GUBVARSTATUS_UNINITIAL = -1, /** unintitialized variable status */
262  GUBVARSTATUS_CAPACITYEXCEEDED = 0, /** variable with weight exceeding the knapsack capacity */
263  GUBVARSTATUS_BELONGSTOSET_R = 1, /** variable in noncovervars R */
264  GUBVARSTATUS_BELONGSTOSET_F = 2, /** variable in noncovervars F */
265  GUBVARSTATUS_BELONGSTOSET_C2 = 3, /** variable in covervars C2 */
266  GUBVARSTATUS_BELONGSTOSET_C1 = 4 /** variable in covervars C1 */
267 };
268 typedef enum GUBVarstatus GUBVARSTATUS;
270 /** status of variable in GUB constraint */
272 {
273  GUBCONSSTATUS_UNINITIAL = -1, /** unintitialized GUB constraint status */
274  GUBCONSSTATUS_BELONGSTOSET_GR = 0, /** all GUB variables are in noncovervars R */
275  GUBCONSSTATUS_BELONGSTOSET_GF = 1, /** all GUB variables are in noncovervars F (and noncovervars R) */
276  GUBCONSSTATUS_BELONGSTOSET_GC2 = 2, /** all GUB variables are in covervars C2 */
277  GUBCONSSTATUS_BELONGSTOSET_GNC1 = 3, /** some GUB variables are in covervars C1, others in noncovervars R or F */
278  GUBCONSSTATUS_BELONGSTOSET_GOC1 = 4 /** all GUB variables are in covervars C1 */
279 };
280 typedef enum GUBConsstatus GUBCONSSTATUS;
282 /** data structure of GUB constraints */
284 {
285  int* gubvars; /**< indices of GUB variables in knapsack constraint */
286  GUBVARSTATUS* gubvarsstatus; /**< status of GUB variables */
287  int ngubvars; /**< number of GUB variables */
288  int gubvarssize; /**< size of gubvars array */
289 };
290 typedef struct SCIP_GUBCons SCIP_GUBCONS;
292 /** data structure of a set of GUB constraints */
294 {
295  SCIP_GUBCONS** gubconss; /**< GUB constraints in GUB set */
296  GUBCONSSTATUS* gubconsstatus; /**< status of GUB constraints */
297  int ngubconss; /**< number of GUB constraints */
298  int nvars; /**< number of variables in knapsack constraint */
299  int* gubconssidx; /**< index of GUB constraint (in gubconss array) of each knapsack variable */
300  int* gubvarsidx; /**< index in GUB constraint (in gubvars array) of each knapsack variable */
301 };
302 typedef struct SCIP_GUBSet SCIP_GUBSET;
304 /*
305  * Local methods
306  */
308 /** comparison method for two sorting key pairs */
309 static
310 SCIP_DECL_SORTPTRCOMP(compSortkeypairs)
311 {
312  SORTKEYPAIR* sortkeypair1 = (SORTKEYPAIR*)elem1;
313  SORTKEYPAIR* sortkeypair2 = (SORTKEYPAIR*)elem2;
314 
315  if( sortkeypair1->key1 < sortkeypair2->key1 )
316  return -1;
317  else if( sortkeypair1->key1 > sortkeypair2->key1 )
318  return +1;
319  else if( sortkeypair1->key2 < sortkeypair2->key2 )
320  return -1;
321  else if( sortkeypair1->key2 > sortkeypair2->key2 )
322  return +1;
323  else
324  return 0;
325 }
326 
327 /** creates event data */
328 static
330  SCIP* scip, /**< SCIP data structure */
331  SCIP_EVENTDATA** eventdata, /**< pointer to store event data */
332  SCIP_CONS* cons, /**< constraint */
333  SCIP_Longint weight /**< weight of variable */
334  )
335 {
336  assert(eventdata != NULL);
338  SCIP_CALL( SCIPallocBlockMemory(scip, eventdata) );
339  (*eventdata)->cons = cons;
340  (*eventdata)->weight = weight;
341 
342  return SCIP_OKAY;
343 }
344 
345 /** frees event data */
346 static
348  SCIP* scip, /**< SCIP data structure */
349  SCIP_EVENTDATA** eventdata /**< pointer to event data */
350  )
351 {
352  assert(eventdata != NULL);
353 
354  SCIPfreeBlockMemory(scip, eventdata);
356  return SCIP_OKAY;
357 }
358 
359 /** sorts items in knapsack with nonincreasing weights */
360 static
361 void sortItems(
362  SCIP_CONSDATA* consdata /**< constraint data */
363  )
364 {
365  assert(consdata != NULL);
366  assert(consdata->nvars == 0 || consdata->vars != NULL);
367  assert(consdata->nvars == 0 || consdata->weights != NULL);
368  assert(consdata->nvars == 0 || consdata->eventdata != NULL);
369  assert(consdata->nvars == 0 || (consdata->cliquepartition != NULL && consdata->negcliquepartition != NULL));
370 
371  if( !consdata->sorted )
372  {
373  int pos;
374  int lastcliquenum;
375  int v;
376 
377  /* sort of five joint arrays of Long/pointer/pointer/ints/ints,
378  * sorted by first array in non-increasing order via sort template */
380  consdata->weights,
381  (void**)consdata->vars,
382  (void**)consdata->eventdata,
383  consdata->cliquepartition,
384  consdata->negcliquepartition,
385  consdata->nvars);
386 
387  v = consdata->nvars - 1;
388  /* sort all items with same weight according to their variable index, used for hash value for fast pairwise comparison of all constraints */
389  while( v >= 0 )
390  {
391  int w = v - 1;
392 
393  while( w >= 0 && consdata->weights[v] == consdata->weights[w] )
394  --w;
395 
396  if( v - w > 1 )
397  {
398  /* sort all corresponding parts of arrays for which the weights are equal by using the variable index */
400  (void**)(&(consdata->vars[w+1])),
401  (void**)(&(consdata->eventdata[w+1])),
402  &(consdata->cliquepartition[w+1]),
403  &(consdata->negcliquepartition[w+1]),
404  SCIPvarComp,
405  v - w);
406  }
407  v = w;
408  }
409 
410  /* we need to make sure that our clique numbers of our normal clique will be in increasing order without gaps */
411  if( consdata->cliquepartitioned )
412  {
413  lastcliquenum = 0;
414 
415  for( pos = 0; pos < consdata->nvars; ++pos )
416  {
417  /* if the clique number in the normal clique at position pos is greater than the last found clique number the
418  * partition is invalid */
419  if( consdata->cliquepartition[pos] > lastcliquenum )
420  {
421  consdata->cliquepartitioned = FALSE;
422  break;
423  }
424  else if( consdata->cliquepartition[pos] == lastcliquenum )
425  ++lastcliquenum;
426  }
427  }
428  /* we need to make sure that our clique numbers of our negated clique will be in increasing order without gaps */
429  if( consdata->negcliquepartitioned )
430  {
431  lastcliquenum = 0;
432 
433  for( pos = 0; pos < consdata->nvars; ++pos )
434  {
435  /* if the clique number in the negated clique at position pos is greater than the last found clique number the
436  * partition is invalid */
437  if( consdata->negcliquepartition[pos] > lastcliquenum )
438  {
439  consdata->negcliquepartitioned = FALSE;
440  break;
441  }
442  else if( consdata->negcliquepartition[pos] == lastcliquenum )
443  ++lastcliquenum;
444  }
445  }
446 
447  consdata->sorted = TRUE;
448  }
449 #ifndef NDEBUG
450  {
451  /* check if the weight array is sorted in a non-increasing way */
452  int i;
453  for( i = 0; i < consdata->nvars-1; ++i )
454  assert(consdata->weights[i] >= consdata->weights[i+1]);
455  }
456 #endif
457 }
458 
459 /** calculates a partition of the variables into cliques */
460 static
462  SCIP* scip, /**< SCIP data structure */
463  SCIP_CONSHDLRDATA* conshdlrdata, /**< knapsack constraint handler data */
464  SCIP_CONSDATA* consdata, /**< constraint data */
465  SCIP_Bool normalclique, /**< Should normal cliquepartition be created? */
466  SCIP_Bool negatedclique /**< Should negated cliquepartition be created? */
467  )
468 {
469  SCIP_Bool ispartitionoutdated;
470  SCIP_Bool isnegpartitionoutdated;
471  assert(consdata != NULL);
472  assert(consdata->nvars == 0 || (consdata->cliquepartition != NULL && consdata->negcliquepartition != NULL));
473 
474  /* rerun eventually if number of global cliques increased considerably since last partition */
475  ispartitionoutdated = (conshdlrdata->updatecliquepartitions && consdata->ncliques > 1
476  && SCIPgetNCliques(scip) >= (int)(conshdlrdata->clqpartupdatefac * consdata->ncliqueslastpart));
477 
478  if( normalclique && ( !consdata->cliquepartitioned || ispartitionoutdated ) )
479  {
480  SCIP_CALL( SCIPcalcCliquePartition(scip, consdata->vars, consdata->nvars, consdata->cliquepartition, &consdata->ncliques) );
481  consdata->cliquepartitioned = TRUE;
482  consdata->ncliqueslastpart = SCIPgetNCliques(scip);
483  }
484 
485  /* rerun eventually if number of global cliques increased considerably since last negated partition */
486  isnegpartitionoutdated = (conshdlrdata->updatecliquepartitions && consdata->nnegcliques > 1
487  && SCIPgetNCliques(scip) >= (int)(conshdlrdata->clqpartupdatefac * consdata->ncliqueslastnegpart));
488 
489  if( negatedclique && (!consdata->negcliquepartitioned || isnegpartitionoutdated) )
490  {
491  SCIP_CALL( SCIPcalcNegatedCliquePartition(scip, consdata->vars, consdata->nvars, consdata->negcliquepartition, &consdata->nnegcliques) );
492  consdata->negcliquepartitioned = TRUE;
493  consdata->ncliqueslastnegpart = SCIPgetNCliques(scip);
494  }
495  assert(!consdata->cliquepartitioned || consdata->ncliques <= consdata->nvars);
496  assert(!consdata->negcliquepartitioned || consdata->nnegcliques <= consdata->nvars);
497 
498  return SCIP_OKAY;
499 }
500 
501 /** installs rounding locks for the given variable in the given knapsack constraint */
502 static
504  SCIP* scip, /**< SCIP data structure */
505  SCIP_CONS* cons, /**< knapsack constraint */
506  SCIP_VAR* var /**< variable of constraint entry */
507  )
508 {
509  SCIP_CALL( SCIPlockVarCons(scip, var, cons, FALSE, TRUE) );
510 
511  return SCIP_OKAY;
512 }
513 
514 /** removes rounding locks for the given variable in the given knapsack constraint */
515 static
517  SCIP* scip, /**< SCIP data structure */
518  SCIP_CONS* cons, /**< knapsack constraint */
519  SCIP_VAR* var /**< variable of constraint entry */
520  )
521 {
522  SCIP_CALL( SCIPunlockVarCons(scip, var, cons, FALSE, TRUE) );
523 
524  return SCIP_OKAY;
525 }
526 
527 /** catches bound change events for variables in knapsack */
528 static
530  SCIP* scip, /**< SCIP data structure */
531  SCIP_CONS* cons, /**< constraint */
532  SCIP_CONSDATA* consdata, /**< constraint data */
533  SCIP_EVENTHDLR* eventhdlr /**< event handler to call for the event processing */
534  )
535 {
536  int i;
538  assert(cons != NULL);
539  assert(consdata != NULL);
540  assert(consdata->nvars == 0 || consdata->vars != NULL);
541  assert(consdata->nvars == 0 || consdata->weights != NULL);
542  assert(consdata->nvars == 0 || consdata->eventdata != NULL);
543 
544  for( i = 0; i < consdata->nvars; i++)
545  {
546  SCIP_CALL( eventdataCreate(scip, &consdata->eventdata[i], cons, consdata->weights[i]) );
547  SCIP_CALL( SCIPcatchVarEvent(scip, consdata->vars[i], EVENTTYPE_KNAPSACK,
548  eventhdlr, consdata->eventdata[i], &consdata->eventdata[i]->filterpos) );
549  }
550 
551  return SCIP_OKAY;
552 }
553 
554 /** drops bound change events for variables in knapsack */
555 static
557  SCIP* scip, /**< SCIP data structure */
558  SCIP_CONSDATA* consdata, /**< constraint data */
559  SCIP_EVENTHDLR* eventhdlr /**< event handler to call for the event processing */
560  )
561 {
562  int i;
563 
564  assert(consdata != NULL);
565  assert(consdata->nvars == 0 || consdata->vars != NULL);
566  assert(consdata->nvars == 0 || consdata->weights != NULL);
567  assert(consdata->nvars == 0 || consdata->eventdata != NULL);
568 
569  for( i = 0; i < consdata->nvars; i++)
570  {
571  SCIP_CALL( SCIPdropVarEvent(scip, consdata->vars[i], EVENTTYPE_KNAPSACK,
572  eventhdlr, consdata->eventdata[i], consdata->eventdata[i]->filterpos) );
573  SCIP_CALL( eventdataFree(scip, &consdata->eventdata[i]) );
574  }
575 
576  return SCIP_OKAY;
577 }
578 
579 /** ensures, that vars and vals arrays can store at least num entries */
580 static
582  SCIP* scip, /**< SCIP data structure */
583  SCIP_CONSDATA* consdata, /**< knapsack constraint data */
584  int num, /**< minimum number of entries to store */
585  SCIP_Bool transformed /**< is constraint from transformed problem? */
586  )
587 {
588  assert(consdata != NULL);
589  assert(consdata->nvars <= consdata->varssize);
590 
591  if( num > consdata->varssize )
592  {
593  int newsize;
594 
595  newsize = SCIPcalcMemGrowSize(scip, num);
596  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->vars, consdata->varssize, newsize) );
597  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->weights, consdata->varssize, newsize) );
598  if( transformed )
599  {
600  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->eventdata, consdata->varssize, newsize) );
601  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->cliquepartition, consdata->varssize, newsize) );
602  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->negcliquepartition, consdata->varssize, newsize) );
603  }
604  else
605  {
606  assert(consdata->eventdata == NULL);
607  assert(consdata->cliquepartition == NULL);
608  assert(consdata->negcliquepartition == NULL);
609  }
610  consdata->varssize = newsize;
611  }
612  assert(num <= consdata->varssize);
613 
614  return SCIP_OKAY;
615 }
616 
617 /** updates all weight sums for fixed and unfixed variables */
618 static
619 void updateWeightSums(
620  SCIP_CONSDATA* consdata, /**< knapsack constraint data */
621  SCIP_VAR* var, /**< variable for this weight */
622  SCIP_Longint weightdelta /**< difference between the old and the new weight of the variable */
623  )
624 {
625  assert(consdata != NULL);
626  assert(var != NULL);
628  consdata->weightsum += weightdelta;
629 
630  if( SCIPvarGetLbLocal(var) > 0.5 )
631  consdata->onesweightsum += weightdelta;
632 
633  assert(consdata->weightsum >= 0);
634  assert(consdata->onesweightsum >= 0);
635 }
636 
637 /** creates knapsack constraint data */
638 static
640  SCIP* scip, /**< SCIP data structure */
641  SCIP_CONSDATA** consdata, /**< pointer to store constraint data */
642  int nvars, /**< number of variables in knapsack */
643  SCIP_VAR** vars, /**< variables of knapsack */
644  SCIP_Longint* weights, /**< weights of knapsack items */
645  SCIP_Longint capacity /**< capacity of knapsack */
646  )
647 {
648  int v;
649  SCIP_Longint constant;
650 
651  assert(consdata != NULL);
652 
653  SCIP_CALL( SCIPallocBlockMemory(scip, consdata) );
654 
655  constant = 0L;
656  (*consdata)->vars = NULL;
657  (*consdata)->weights = NULL;
658  (*consdata)->nvars = 0;
659  if( nvars > 0 )
660  {
661  SCIP_VAR** varsbuffer;
662  SCIP_Longint* weightsbuffer;
663  int k;
664 
665  SCIP_CALL( SCIPallocBufferArray(scip, &varsbuffer, nvars) );
666  SCIP_CALL( SCIPallocBufferArray(scip, &weightsbuffer, nvars) );
667 
668  k = 0;
669  for( v = 0; v < nvars; ++v )
670  {
671  assert(vars[v] != NULL);
672  assert(SCIPvarIsBinary(vars[v]));
673 
674  /* all weight have to be non negative */
675  assert( weights[v] >= 0 );
676 
677  if( weights[v] > 0 )
678  {
679  /* treat fixed variables as constants if problem compression is enabled */
680  if( SCIPisConsCompressionEnabled(scip) && SCIPvarGetLbGlobal(vars[v]) > SCIPvarGetUbGlobal(vars[v]) - 0.5 )
681  {
682  /* only if the variable is fixed to 1, we add its weight to the constant */
683  if( SCIPvarGetUbGlobal(vars[v]) > 0.5 )
684  constant += weights[v];
685  }
686  else
687  {
688  varsbuffer[k] = vars[v];
689  weightsbuffer[k] = weights[v];
690  ++k;
691  }
692  }
693  }
694  assert(k >= 0);
695 
696  (*consdata)->nvars = k;
697 
698  /* copy the active variables and weights into the constraint data structure */
699  if( k > 0 )
700  {
701  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->vars, varsbuffer, k) );
702  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->weights, weightsbuffer, k) );
703  }
704 
705  /* free buffer storage */
706  SCIPfreeBufferArray(scip, &weightsbuffer);
707  SCIPfreeBufferArray(scip, &varsbuffer);
708  }
709 
710  /* capacity has to be greater or equal to zero */
711  assert(capacity >= 0);
712  assert(constant >= 0);
713 
714  (*consdata)->varssize = (*consdata)->nvars;
715  (*consdata)->capacity = capacity - constant;
716  (*consdata)->eventdata = NULL;
717  (*consdata)->cliquepartition = NULL;
718  (*consdata)->negcliquepartition = NULL;
719  (*consdata)->row = NULL;
720  (*consdata)->weightsum = 0;
721  (*consdata)->onesweightsum = 0;
722  (*consdata)->ncliques = 0;
723  (*consdata)->nnegcliques = 0;
724  (*consdata)->presolvedtiming = 0;
725  (*consdata)->sorted = FALSE;
726  (*consdata)->cliquepartitioned = FALSE;
727  (*consdata)->negcliquepartitioned = FALSE;
728  (*consdata)->ncliqueslastpart = -1;
729  (*consdata)->ncliqueslastnegpart = -1;
730  (*consdata)->merged = FALSE;
731  (*consdata)->cliquesadded = FALSE;
732  (*consdata)->varsdeleted = FALSE;
733  (*consdata)->existmultaggr = FALSE;
734 
735  /* get transformed variables, if we are in the transformed problem */
736  if( SCIPisTransformed(scip) )
737  {
738  SCIP_CALL( SCIPgetTransformedVars(scip, (*consdata)->nvars, (*consdata)->vars, (*consdata)->vars) );
739 
740  for( v = 0; v < (*consdata)->nvars; v++ )
741  {
742  SCIP_VAR* var = SCIPvarGetProbvar((*consdata)->vars[v]);
743  assert(var != NULL);
744  (*consdata)->existmultaggr = (*consdata)->existmultaggr || (SCIPvarGetStatus(var) == SCIP_VARSTATUS_MULTAGGR);
745  }
746 
747  /* allocate memory for additional data structures */
748  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*consdata)->eventdata, (*consdata)->nvars) );
749  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*consdata)->cliquepartition, (*consdata)->nvars) );
750  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*consdata)->negcliquepartition, (*consdata)->nvars) );
751  }
752 
753  /* calculate sum of weights and capture variables */
754  for( v = 0; v < (*consdata)->nvars; ++v )
755  {
756  /* calculate sum of weights */
757  updateWeightSums(*consdata, (*consdata)->vars[v], (*consdata)->weights[v]);
758 
759  /* capture variables */
760  SCIP_CALL( SCIPcaptureVar(scip, (*consdata)->vars[v]) );
761  }
762  return SCIP_OKAY;
763 }
764 
765 /** frees knapsack constraint data */
766 static
768  SCIP* scip, /**< SCIP data structure */
769  SCIP_CONSDATA** consdata, /**< pointer to the constraint data */
770  SCIP_EVENTHDLR* eventhdlr /**< event handler to call for the event processing */
771  )
772 {
773  assert(consdata != NULL);
774  assert(*consdata != NULL);
776  if( (*consdata)->row != NULL )
777  {
778  SCIP_CALL( SCIPreleaseRow(scip, &(*consdata)->row) );
779  }
780  if( (*consdata)->eventdata != NULL )
781  {
782  SCIP_CALL( dropEvents(scip, *consdata, eventhdlr) );
783  SCIPfreeBlockMemoryArray(scip, &(*consdata)->eventdata, (*consdata)->varssize);
784  }
785  if( (*consdata)->negcliquepartition != NULL )
786  {
787  SCIPfreeBlockMemoryArray(scip, &(*consdata)->negcliquepartition, (*consdata)->varssize);
788  }
789  if( (*consdata)->cliquepartition != NULL )
790  {
791  SCIPfreeBlockMemoryArray(scip, &(*consdata)->cliquepartition, (*consdata)->varssize);
792  }
793  if( (*consdata)->vars != NULL )
794  {
795  int v;
796 
797  /* release variables */
798  for( v = 0; v < (*consdata)->nvars; v++ )
799  {
800  assert((*consdata)->vars[v] != NULL);
801  SCIP_CALL( SCIPreleaseVar(scip, &((*consdata)->vars[v])) );
802  }
803 
804  assert( (*consdata)->weights != NULL );
805  assert( (*consdata)->varssize > 0 );
806  SCIPfreeBlockMemoryArray(scip, &(*consdata)->vars, (*consdata)->varssize);
807  SCIPfreeBlockMemoryArray(scip, &(*consdata)->weights, (*consdata)->varssize);
808  }
809 
810  SCIPfreeBlockMemory(scip, consdata);
811 
812  return SCIP_OKAY;
813 }
814 
815 /** changes a single weight in knapsack constraint data */
816 static
817 void consdataChgWeight(
818  SCIP_CONSDATA* consdata, /**< knapsack constraint data */
819  int item, /**< item number */
820  SCIP_Longint newweight /**< new weight of item */
821  )
822 {
823  SCIP_Longint oldweight;
824  SCIP_Longint weightdiff;
826  assert(consdata != NULL);
827  assert(0 <= item && item < consdata->nvars);
828 
829  oldweight = consdata->weights[item];
830  weightdiff = newweight - oldweight;
831  consdata->weights[item] = newweight;
832 
833  /* update weight sums for all and fixed variables */
834  updateWeightSums(consdata, consdata->vars[item], weightdiff);
835 
836  if( consdata->eventdata != NULL )
837  {
838  assert(consdata->eventdata[item] != NULL);
839  assert(consdata->eventdata[item]->weight == oldweight);
840  consdata->eventdata[item]->weight = newweight;
841  }
842 
843  consdata->presolvedtiming = 0;
844  consdata->sorted = FALSE;
845 
846  /* recalculate cliques extraction after a weight was increased */
847  if( oldweight < newweight )
848  {
849  consdata->cliquesadded = FALSE;
850  }
851 }
852 
853 /** creates LP row corresponding to knapsack constraint */
854 static
856  SCIP* scip, /**< SCIP data structure */
857  SCIP_CONS* cons /**< knapsack constraint */
858  )
859 {
860  SCIP_CONSDATA* consdata;
861  int i;
862 
863  consdata = SCIPconsGetData(cons);
864  assert(consdata != NULL);
865  assert(consdata->row == NULL);
866 
867  SCIP_CALL( SCIPcreateEmptyRowCons(scip, &consdata->row, cons, SCIPconsGetName(cons),
868  -SCIPinfinity(scip), (SCIP_Real)consdata->capacity,
870 
871  SCIP_CALL( SCIPcacheRowExtensions(scip, consdata->row) );
872  for( i = 0; i < consdata->nvars; ++i )
873  {
874  SCIP_CALL( SCIPaddVarToRow(scip, consdata->row, consdata->vars[i], (SCIP_Real)consdata->weights[i]) );
875  }
876  SCIP_CALL( SCIPflushRowExtensions(scip, consdata->row) );
877 
878  return SCIP_OKAY;
879 }
880 
881 /** adds linear relaxation of knapsack constraint to the LP */
882 static
884  SCIP* scip, /**< SCIP data structure */
885  SCIP_CONS* cons, /**< knapsack constraint */
886  SCIP_Bool* cutoff /**< whether a cutoff has been detected */
887  )
888 {
889  SCIP_CONSDATA* consdata;
890 
891  assert( cutoff != NULL );
892  *cutoff = FALSE;
893 
894  consdata = SCIPconsGetData(cons);
895  assert(consdata != NULL);
896 
897  if( consdata->row == NULL )
898  {
899  SCIP_CALL( createRelaxation(scip, cons) );
900  }
901  assert(consdata->row != NULL);
902 
903  /* insert LP row as cut */
904  if( !SCIProwIsInLP(consdata->row) )
905  {
906  SCIPdebugMsg(scip, "adding relaxation of knapsack constraint <%s> (capacity %" SCIP_LONGINT_FORMAT "): ",
907  SCIPconsGetName(cons), consdata->capacity);
908  SCIPdebug( SCIP_CALL(SCIPprintRow(scip, consdata->row, NULL)) );
909  SCIP_CALL( SCIPaddRow(scip, consdata->row, FALSE, cutoff) );
910  }
911 
912  return SCIP_OKAY;
913 }
914 
915 /** checks knapsack constraint for feasibility of given solution: returns TRUE iff constraint is feasible */
916 static
918  SCIP* scip, /**< SCIP data structure */
919  SCIP_CONS* cons, /**< constraint to check */
920  SCIP_SOL* sol, /**< solution to check, NULL for current solution */
921  SCIP_Bool checklprows, /**< Do constraints represented by rows in the current LP have to be checked? */
922  SCIP_Bool printreason, /**< Should the reason for the violation be printed? */
923  SCIP_Bool* violated /**< pointer to store whether the constraint is violated */
924  )
925 {
926  SCIP_CONSDATA* consdata;
927 
928  assert(violated != NULL);
929 
930  consdata = SCIPconsGetData(cons);
931  assert(consdata != NULL);
932 
933  SCIPdebugMsg(scip, "checking knapsack constraint <%s> for feasibility of solution %p (lprows=%u)\n",
934  SCIPconsGetName(cons), (void*)sol, checklprows);
935 
936  *violated = FALSE;
937 
938  if( checklprows || consdata->row == NULL || !SCIProwIsInLP(consdata->row) )
939  {
940  SCIP_Real sum;
941  SCIP_Longint integralsum;
942  SCIP_Bool ishuge;
943  SCIP_Real absviol;
944  SCIP_Real relviol;
945  int v;
946 
947  /* increase age of constraint; age is reset to zero, if a violation was found only in case we are in
948  * enforcement
949  */
950  if( sol == NULL )
951  {
952  SCIP_CALL( SCIPincConsAge(scip, cons) );
953  }
954 
955  sum = 0.0;
956  integralsum = 0;
957  /* we perform a more exact comparison if the capacity does not exceed the huge value */
958  if( SCIPisHugeValue(scip, (SCIP_Real) consdata->capacity) )
959  {
960  ishuge = TRUE;
961 
962  /* sum over all weight times the corresponding solution value */
963  for( v = consdata->nvars - 1; v >= 0; --v )
964  {
965  assert(SCIPvarIsBinary(consdata->vars[v]));
966  sum += consdata->weights[v] * SCIPgetSolVal(scip, sol, consdata->vars[v]);
967  }
968  }
969  else
970  {
971  ishuge = FALSE;
972 
973  /* sum over all weight for which the variable has a solution value of 1 in feastol */
974  for( v = consdata->nvars - 1; v >= 0; --v )
975  {
976  assert(SCIPvarIsBinary(consdata->vars[v]));
977 
978  if( SCIPgetSolVal(scip, sol, consdata->vars[v]) > 0.5 )
979  integralsum += consdata->weights[v];
980  }
981  }
982 
983  /* calculate constraint violation and update it in solution */
984  absviol = ishuge ? sum : (SCIP_Real)integralsum;
985  absviol -= consdata->capacity;
986  relviol = SCIPrelDiff(absviol + consdata->capacity, (SCIP_Real)consdata->capacity);
987  if( sol != NULL )
988  SCIPupdateSolLPConsViolation(scip, sol, absviol, relviol);
989 
990  if( SCIPisFeasPositive(scip, absviol) )
991  {
992  *violated = TRUE;
993 
994  /* only reset constraint age if we are in enforcement */
995  if( sol == NULL )
996  {
997  SCIP_CALL( SCIPresetConsAge(scip, cons) );
998  }
999 
1000  if( printreason )
1001  {
1002  SCIP_CALL( SCIPprintCons(scip, cons, NULL) );
1003 
1004  SCIPinfoMessage(scip, NULL, ";\n");
1005  SCIPinfoMessage(scip, NULL, "violation: the capacity is violated by %.15g\n", absviol);
1006  }
1007  }
1008  }
1009 
1010  return SCIP_OKAY;
1011 }
1012 
1013 /* IDX computes the integer index for the optimal solution array */
1014 #define IDX(j,d) ((j)*(intcap)+(d))
1015 
1016 /** solves knapsack problem in maximization form exactly using dynamic programming;
1017  * if needed, one can provide arrays to store all selected items and all not selected items
1018  *
1019  * @note in case you provide the solitems or nonsolitems array you also have to provide the counter part, as well
1020  *
1021  * @note the algorithm will first compute a greedy solution and terminate
1022  * if the greedy solution is proven to be optimal.
1023  * The dynamic programming algorithm runs with a time and space complexity
1024  * of O(nitems * capacity).
1025  *
1026  * @todo If only the objective is relevant, it is easy to change the code to use only one slice with O(capacity) space.
1027  * There are recursive methods (see the book by Kellerer et al.) that require O(capacity) space, but it remains
1028  * to be checked whether they are faster and whether they can reconstruct the solution.
1029  * Dembo and Hammer (see Kellerer et al. Section 5.1.3, page 126) found a method that relies on a fast probing method.
1030  * This fixes additional elements to 0 or 1 similar to a reduced cost fixing.
1031  * This could be implemented, however, it would be technically a bit cumbersome,
1032  * since one needs the greedy solution and the LP-value for this.
1033  * This is currently only available after the redundant items have already been sorted out.
1034  */
1036  SCIP* scip, /**< SCIP data structure */
1037  int nitems, /**< number of available items */
1038  SCIP_Longint* weights, /**< item weights */
1039  SCIP_Real* profits, /**< item profits */
1040  SCIP_Longint capacity, /**< capacity of knapsack */
1041  int* items, /**< item numbers */
1042  int* solitems, /**< array to store items in solution, or NULL */
1043  int* nonsolitems, /**< array to store items not in solution, or NULL */
1044  int* nsolitems, /**< pointer to store number of items in solution, or NULL */
1045  int* nnonsolitems, /**< pointer to store number of items not in solution, or NULL */
1046  SCIP_Real* solval, /**< pointer to store optimal solution value, or NULL */
1047  SCIP_Bool* success /**< pointer to store if an error occured during solving
1048  * (normally a memory problem) */
1049  )
1050 {
1051  SCIP_RETCODE retcode;
1052  SCIP_Real* tempsort;
1053  SCIP_Real* optvalues;
1054  int intcap;
1055  int d;
1056  int j;
1057  int greedymedianpos;
1058  SCIP_Longint weightsum;
1059  int* myitems;
1060  SCIP_Longint* myweights;
1061  SCIP_Real* realweights;
1062  int* allcurrminweight;
1063  SCIP_Real* myprofits;
1064  int nmyitems;
1065  SCIP_Longint gcd;
1066  SCIP_Longint minweight;
1067  SCIP_Longint maxweight;
1068  int currminweight;
1069  SCIP_Longint greedysolweight;
1070  SCIP_Real greedysolvalue;
1071  SCIP_Real greedyupperbound;
1072  SCIP_Bool eqweights;
1073  SCIP_Bool intprofits;
1074 
1075  assert(weights != NULL);
1076  assert(profits != NULL);
1077  assert(capacity >= 0);
1078  assert(items != NULL);
1079  assert(nitems >= 0);
1080  assert(success != NULL);
1081 
1082  *success = TRUE;
1083 
1084 #ifndef NDEBUG
1085  for( j = nitems - 1; j >= 0; --j )
1086  assert(weights[j] >= 0);
1087 #endif
1088 
1089  SCIPdebugMsg(scip, "Solving knapsack exactly.\n");
1090 
1091  /* initializing solution value */
1092  if( solval != NULL )
1093  *solval = 0.0;
1094 
1095  /* init solution information */
1096  if( solitems != NULL )
1097  {
1098  assert(items != NULL);
1099  assert(nsolitems != NULL);
1100  assert(nonsolitems != NULL);
1101  assert(nnonsolitems != NULL);
1102 
1103  *nnonsolitems = 0;
1104  *nsolitems = 0;
1105  }
1106 
1107  /* allocate temporary memory */
1108  SCIP_CALL( SCIPallocBufferArray(scip, &myweights, nitems) );
1109  SCIP_CALL( SCIPallocBufferArray(scip, &myprofits, nitems) );
1110  SCIP_CALL( SCIPallocBufferArray(scip, &myitems, nitems) );
1111  nmyitems = 0;
1112  weightsum = 0;
1113  minweight = SCIP_LONGINT_MAX;
1114  maxweight = 0;
1115 
1116  /* remove unnecessary items */
1117  for( j = 0; j < nitems; ++j )
1118  {
1119  assert(0 <= weights[j] && weights[j] < SCIP_LONGINT_MAX);
1120 
1121  /* item does not fit */
1122  if( weights[j] > capacity )
1123  {
1124  if( solitems != NULL )
1125  nonsolitems[(*nnonsolitems)++] = items[j]; /*lint !e413*/
1126  }
1127  /* item is not profitable */
1128  else if( profits[j] <= 0.0 )
1129  {
1130  if( solitems != NULL )
1131  nonsolitems[(*nnonsolitems)++] = items[j]; /*lint !e413*/
1132  }
1133  /* item always fits */
1134  else if( weights[j] == 0 )
1135  {
1136  if( solitems != NULL )
1137  solitems[(*nsolitems)++] = items[j]; /*lint !e413*/
1138 
1139  if( solval != NULL )
1140  *solval += profits[j];
1141  }
1142  /* all important items */
1143  else
1144  {
1145  myweights[nmyitems] = weights[j];
1146  myprofits[nmyitems] = profits[j];
1147  myitems[nmyitems] = items[j];
1148 
1149  /* remember smallest item */
1150  if( myweights[nmyitems] < minweight )
1151  minweight = myweights[nmyitems];
1152 
1153  /* remember bigest item */
1154  if( myweights[nmyitems] > maxweight )
1155  maxweight = myweights[nmyitems];
1156 
1157  weightsum += myweights[nmyitems];
1158  ++nmyitems;
1159  }
1160  }
1161 
1162  intprofits = TRUE;
1163  /* check if all profits are integer to strengthen the upper bound on the greedy solution */
1164  for( j = 0; j < nmyitems && intprofits; ++j )
1165  intprofits = intprofits && SCIPisIntegral(scip, myprofits[j]);
1166 
1167  /* if no item is left then goto end */
1168  if( nmyitems == 0 )
1169  {
1170  SCIPdebugMsg(scip, "After preprocessing no items are left.\n");
1171 
1172  goto TERMINATE;
1173  }
1174 
1175  /* if all items fit, we also do not need to do the expensive stuff later on */
1176  if( weightsum > 0 && weightsum <= capacity )
1177  {
1178  SCIPdebugMsg(scip, "After preprocessing all items fit into knapsack.\n");
1179 
1180  for( j = nmyitems - 1; j >= 0; --j )
1181  {
1182  if( solitems != NULL )
1183  solitems[(*nsolitems)++] = myitems[j]; /*lint !e413*/
1184 
1185  if( solval != NULL )
1186  *solval += myprofits[j];
1187  }
1188 
1189  goto TERMINATE;
1190  }
1191 
1192  assert(0 < minweight && minweight <= capacity );
1193  assert(0 < maxweight && maxweight <= capacity);
1194 
1195  /* make weights relatively prime */
1196  eqweights = TRUE;
1197  if( maxweight > 1 )
1198  {
1199  /* determine greatest common divisor */
1200  gcd = myweights[nmyitems - 1];
1201  for( j = nmyitems - 2; j >= 0 && gcd >= 2; --j )
1202  gcd = SCIPcalcGreComDiv(gcd, myweights[j]);
1203 
1204  SCIPdebugMsg(scip, "Gcd is %" SCIP_LONGINT_FORMAT ".\n", gcd);
1205 
1206  /* divide by greatest common divisor */
1207  if( gcd > 1 )
1208  {
1209  for( j = nmyitems - 1; j >= 0; --j )
1210  {
1211  myweights[j] /= gcd;
1212  eqweights = eqweights && (myweights[j] == 1);
1213  }
1214  capacity /= gcd;
1215  minweight /= gcd;
1216  }
1217  else
1218  eqweights = FALSE;
1219  }
1220  assert(minweight <= capacity);
1221 
1222  /* if only one item fits, then take the best */
1223  if( minweight > capacity / 2 )
1224  {
1225  int p;
1226 
1227  SCIPdebugMsg(scip, "Only one item fits into knapsack, so take the best.\n");
1228 
1229  p = nmyitems - 1;
1230 
1231  /* find best item */
1232  for( j = nmyitems - 2; j >= 0; --j )
1233  {
1234  if( myprofits[j] > myprofits[p] )
1235  p = j;
1236  }
1237 
1238  /* update solution information */
1239  if( solitems != NULL )
1240  {
1241  assert(nsolitems != NULL && nonsolitems != NULL && nnonsolitems != NULL);
1242 
1243  solitems[(*nsolitems)++] = myitems[p];
1244  for( j = nmyitems - 1; j >= 0; --j )
1245  {
1246  if( j != p )
1247  nonsolitems[(*nnonsolitems)++] = myitems[j];
1248  }
1249  }
1250  /* update solution value */
1251  if( solval != NULL )
1252  *solval += myprofits[p];
1253 
1254  goto TERMINATE;
1255  }
1256 
1257  /* if all items have the same weight, then take the best */
1258  if( eqweights )
1259  {
1260  SCIP_Real addval = 0.0;
1261 
1262  SCIPdebugMsg(scip, "All weights are equal, so take the best.\n");
1263 
1264  SCIPsortDownRealIntLong(myprofits, myitems, myweights, nmyitems);
1265 
1266  /* update solution information */
1267  if( solitems != NULL || solval != NULL )
1268  {
1269  SCIP_Longint i;
1270 
1271  /* if all items would fit we had handled this case before */
1272  assert((SCIP_Longint) nmyitems > capacity);
1273  assert(nsolitems != NULL && nonsolitems != NULL && nnonsolitems != NULL);
1274 
1275  /* take the first best items into the solution */
1276  for( i = capacity - 1; i >= 0; --i )
1277  {
1278  if( solitems != NULL )
1279  solitems[(*nsolitems)++] = myitems[i];
1280  addval += myprofits[i];
1281  }
1282 
1283  if( solitems != NULL )
1284  {
1285  /* the rest are not in the solution */
1286  for( i = nmyitems - 1; i >= capacity; --i )
1287  nonsolitems[(*nnonsolitems)++] = myitems[i];
1288  }
1289  }
1290  /* update solution value */
1291  if( solval != NULL )
1292  {
1293  assert(addval > 0.0);
1294  *solval += addval;
1295  }
1296 
1297  goto TERMINATE;
1298  }
1299 
1300  SCIPdebugMsg(scip, "Determine greedy solution.\n");
1301 
1302  /* sort myitems (plus corresponding arrays myweights and myprofits) such that
1303  * p_1/w_1 >= p_2/w_2 >= ... >= p_n/w_n, this is only used for the greedy solution
1304  */
1305  SCIP_CALL( SCIPallocBufferArray(scip, &tempsort, nmyitems) );
1306  SCIP_CALL( SCIPallocBufferArray(scip, &realweights, nmyitems) );
1307 
1308  for( j = 0; j < nmyitems; ++j )
1309  {
1310  tempsort[j] = myprofits[j]/((SCIP_Real) myweights[j]);
1311  realweights[j] = (SCIP_Real)myweights[j];
1312  }
1313 
1314  SCIPselectWeightedDownRealLongRealInt(tempsort, myweights, myprofits, myitems, realweights,
1315  (SCIP_Real)capacity, nmyitems, &greedymedianpos);
1316 
1317  SCIPfreeBufferArray(scip, &realweights);
1318  SCIPfreeBufferArray(scip, &tempsort);
1319 
1320  /* initialize values for greedy solution information */
1321  greedysolweight = 0;
1322  greedysolvalue = 0.0;
1323 
1324  /* determine greedy solution */
1325  for( j = 0; j < greedymedianpos; ++j )
1326  {
1327  assert(myweights[j] <= capacity);
1328 
1329  /* update greedy solution weight and value */
1330  greedysolweight += myweights[j];
1331  greedysolvalue += myprofits[j];
1332  }
1333 
1334  assert(0 < greedysolweight && greedysolweight <= capacity);
1335  assert(greedysolvalue > 0.0);
1336 
1337  /* If the greedy solution is optimal by comparing to the LP solution, we take this solution. This happens if:
1338  * - the greedy solution reaches the capacity, because then the LP solution is integral;
1339  * - the greedy solution has an objective that is at least the LP value rounded down in case that all profits are integer, too. */
1340  greedyupperbound = greedysolvalue + myprofits[j] * (SCIP_Real) (capacity - greedysolweight)/((SCIP_Real) myweights[j]);
1341  if( intprofits )
1342  greedyupperbound = SCIPfloor(scip, greedyupperbound);
1343  if( greedysolweight == capacity || SCIPisGE(scip, greedysolvalue, greedyupperbound) )
1344  {
1345  SCIPdebugMsg(scip, "Greedy solution is optimal.\n");
1346 
1347  /* update solution information */
1348  if( solitems != NULL )
1349  {
1350  int l;
1351 
1352  assert(nsolitems != NULL && nonsolitems != NULL && nnonsolitems != NULL);
1353 
1354  /* collect items */
1355  for( l = 0; l < j; ++l )
1356  solitems[(*nsolitems)++] = myitems[l];
1357  for ( ; l < nmyitems; ++l )
1358  nonsolitems[(*nnonsolitems)++] = myitems[l];
1359  }
1360  /* update solution value */
1361  if( solval != NULL )
1362  {
1363  assert(greedysolvalue > 0.0);
1364  *solval += greedysolvalue;
1365  }
1366 
1367  goto TERMINATE;
1368  }
1369 
1370  /* in the following table we do not need the first minweight columns */
1371  capacity -= (minweight - 1);
1372 
1373  /* we can only handle integers */
1374  if( capacity >= INT_MAX )
1375  {
1376  SCIPdebugMsg(scip, "Capacity is to big, so we cannot handle it here.\n");
1377 
1378  *success = FALSE;
1379  goto TERMINATE;
1380  }
1381  assert(capacity < INT_MAX);
1382 
1383  intcap = (int)capacity;
1384  assert(intcap >= 0);
1385  assert(nmyitems > 0);
1386  assert(sizeof(size_t) >= sizeof(int)); /*lint !e506*/ /* no following conversion should be messed up */
1387 
1388  /* this condition checks whether we will try to allocate a correct number of bytes and do not have an overflow, while
1389  * computing the size for the allocation
1390  */
1391  if( intcap < 0 || (intcap > 0 && (((size_t)nmyitems) > (SIZE_MAX / (size_t)intcap / sizeof(*optvalues)) || ((size_t)nmyitems) * ((size_t)intcap) * sizeof(*optvalues) > ((size_t)INT_MAX) )) ) /*lint !e571*/
1392  {
1393  SCIPdebugMsg(scip, "Too much memory (%lu) would be consumed.\n", (unsigned long) (((size_t)nmyitems) * ((size_t)intcap) * sizeof(*optvalues))); /*lint !e571*/
1394 
1395  *success = FALSE;
1396  goto TERMINATE;
1397  }
1398 
1399  /* allocate temporary memory and check for memory exceedance */
1400  retcode = SCIPallocBufferArray(scip, &optvalues, nmyitems * intcap);
1401  if( retcode == SCIP_NOMEMORY )
1402  {
1403  SCIPdebugMsg(scip, "Did not get enough memory.\n");
1404 
1405  *success = FALSE;
1406  goto TERMINATE;
1407  }
1408  else
1409  {
1410  SCIP_CALL( retcode );
1411  }
1412 
1413  SCIPdebugMsg(scip, "Start real exact algorithm.\n");
1414 
1415  /* we memorize at each step the current minimal weight to later on know which value in our optvalues matrix is valid;
1416  * each value entries of the j-th row of optvalues is valid if the index is >= allcurrminweight[j], otherwise it is
1417  * invalid; a second possibility would be to clear the whole optvalues, which should be more expensive than storing
1418  * 'nmyitem' values
1419  */
1420  SCIP_CALL( SCIPallocBufferArray(scip, &allcurrminweight, nmyitems) );
1421  assert(myweights[0] - minweight < INT_MAX);
1422  currminweight = (int) (myweights[0] - minweight);
1423  allcurrminweight[0] = currminweight;
1424 
1425  /* fills first row of dynamic programming table with optimal values */
1426  for( d = currminweight; d < intcap; ++d )
1427  optvalues[d] = myprofits[0];
1428 
1429  /* fills dynamic programming table with optimal values */
1430  for( j = 1; j < nmyitems; ++j )
1431  {
1432  int intweight;
1433 
1434  /* compute important part of weight, which will be represented in the table */
1435  intweight = (int)(myweights[j] - minweight);
1436  assert(0 <= intweight && intweight < intcap);
1437 
1438  /* copy all nonzeros from row above */
1439  for( d = currminweight; d < intweight && d < intcap; ++d )
1440  optvalues[IDX(j,d)] = optvalues[IDX(j-1,d)];
1441 
1442  /* update corresponding row */
1443  for( d = intweight; d < intcap; ++d )
1444  {
1445  /* if index d < current minweight then optvalues[IDX(j-1,d)] is not initialized, i.e. should be 0 */
1446  if( d < currminweight )
1447  optvalues[IDX(j,d)] = myprofits[j];
1448  else
1449  {
1450  SCIP_Real sumprofit;
1451 
1452  if( d - myweights[j] < currminweight )
1453  sumprofit = myprofits[j];
1454  else
1455  sumprofit = optvalues[IDX(j-1,(int)(d-myweights[j]))] + myprofits[j];
1456 
1457  optvalues[IDX(j,d)] = MAX(sumprofit, optvalues[IDX(j-1,d)]);
1458  }
1459  }
1460 
1461  /* update currminweight */
1462  if( intweight < currminweight )
1463  currminweight = intweight;
1464 
1465  allcurrminweight[j] = currminweight;
1466  }
1467 
1468  /* update optimal solution by following the table */
1469  if( solitems != NULL )
1470  {
1471  assert(nsolitems != NULL && nonsolitems != NULL && nnonsolitems != NULL);
1472  d = intcap - 1;
1473 
1474  SCIPdebugMsg(scip, "Fill the solution vector after solving exactly.\n");
1475 
1476  /* insert all items in (non-) solution vector */
1477  for( j = nmyitems - 1; j > 0; --j )
1478  {
1479  /* if the following condition holds this means all remaining items does not fit anymore */
1480  if( d < allcurrminweight[j] )
1481  {
1482  /* we cannot have exceeded our capacity */
1483  assert((SCIP_Longint) d >= -minweight);
1484  break;
1485  }
1486 
1487  /* collect solution items; the first condition means that no further item can fit anymore, but this does */
1488  if( d < allcurrminweight[j-1] || optvalues[IDX(j,d)] > optvalues[IDX(j-1,d)] )
1489  {
1490  solitems[(*nsolitems)++] = myitems[j];
1491 
1492  /* check that we do not have an underflow */
1493  assert(myweights[j] <= (INT_MAX + (SCIP_Longint) d));
1494  d = (int)(d - myweights[j]);
1495  }
1496  /* collect non-solution items */
1497  else
1498  nonsolitems[(*nnonsolitems)++] = myitems[j];
1499  }
1500 
1501  /* insert remaining items */
1502  if( d >= allcurrminweight[j] )
1503  {
1504  assert(j == 0);
1505  solitems[(*nsolitems)++] = myitems[j];
1506  }
1507  else
1508  {
1509  assert(j >= 0);
1510  assert(d < allcurrminweight[j]);
1511 
1512  for( ; j >= 0; --j )
1513  nonsolitems[(*nnonsolitems)++] = myitems[j];
1514  }
1515 
1516  assert(*nsolitems + *nnonsolitems == nitems);
1517  }
1518 
1519  /* update solution value */
1520  if( solval != NULL )
1521  *solval += optvalues[IDX(nmyitems-1,intcap-1)];
1522  SCIPfreeBufferArray(scip, &allcurrminweight);
1523 
1524  /* free all temporary memory */
1525  SCIPfreeBufferArray(scip, &optvalues);
1526 
1527  TERMINATE:
1528  SCIPfreeBufferArray(scip, &myitems);
1529  SCIPfreeBufferArray(scip, &myprofits);
1530  SCIPfreeBufferArray(scip, &myweights);
1531 
1532  return SCIP_OKAY;
1533 }
1534 
1535 /** solves knapsack problem in maximization form approximately by solving the LP-relaxation of the problem using Dantzig's
1536  * method and rounding down the solution; if needed, one can provide arrays to store all selected items and all not
1537  * selected items
1538  */
1540  SCIP* scip, /**< SCIP data structure */
1541  int nitems, /**< number of available items */
1542  SCIP_Longint* weights, /**< item weights */
1543  SCIP_Real* profits, /**< item profits */
1544  SCIP_Longint capacity, /**< capacity of knapsack */
1545  int* items, /**< item numbers */
1546  int* solitems, /**< array to store items in solution, or NULL */
1547  int* nonsolitems, /**< array to store items not in solution, or NULL */
1548  int* nsolitems, /**< pointer to store number of items in solution, or NULL */
1549  int* nnonsolitems, /**< pointer to store number of items not in solution, or NULL */
1550  SCIP_Real* solval /**< pointer to store optimal solution value, or NULL */
1551  )
1552 {
1553  SCIP_Real* tempsort;
1554  SCIP_Longint solitemsweight;
1555  SCIP_Real* realweights;
1556  int j;
1557  int criticalindex;
1558 
1559  assert(weights != NULL);
1560  assert(profits != NULL);
1561  assert(capacity >= 0);
1562  assert(items != NULL);
1563  assert(nitems >= 0);
1564 
1565  if( solitems != NULL )
1566  {
1567  *nsolitems = 0;
1568  *nnonsolitems = 0;
1569  }
1570  if( solval != NULL )
1571  *solval = 0.0;
1572 
1573  /* initialize data for median search */
1574  SCIP_CALL( SCIPallocBufferArray(scip, &tempsort, nitems) );
1575  SCIP_CALL( SCIPallocBufferArray(scip, &realweights, nitems) );
1576  for( j = nitems - 1; j >= 0; --j )
1577  {
1578  tempsort[j] = profits[j]/((SCIP_Real) weights[j]);
1579  realweights[j] = (SCIP_Real)weights[j];
1580  }
1581 
1582  /* partially sort indices such that all elements that are larger than the break item appear first */
1583  SCIPselectWeightedDownRealLongRealInt(tempsort, weights, profits, items, realweights, (SCIP_Real)capacity, nitems, &criticalindex);
1584 
1585  /* selects items as long as they fit into the knapsack */
1586  solitemsweight = 0;
1587  for( j = 0; j < nitems && solitemsweight + weights[j] <= capacity; ++j )
1588  {
1589  if( solitems != NULL )
1590  solitems[(*nsolitems)++] = items[j];
1591 
1592  if( solval != NULL )
1593  (*solval) += profits[j];
1594  solitemsweight += weights[j];
1595  }
1596  if ( solitems != NULL )
1597  {
1598  for( ; j < nitems; j++ )
1599  nonsolitems[(*nnonsolitems)++] = items[j];
1600  }
1601 
1602  SCIPfreeBufferArray(scip, &realweights);
1603  SCIPfreeBufferArray(scip, &tempsort);
1604 
1605  return SCIP_OKAY;
1606 }
1607 
1608 #ifdef SCIP_DEBUG
1609 /** prints all nontrivial GUB constraints and their LP solution values */
1610 static
1611 void GUBsetPrint(
1612  SCIP* scip, /**< SCIP data structure */
1613  SCIP_GUBSET* gubset, /**< GUB set data structure */
1614  SCIP_VAR** vars, /**< variables in knapsack constraint */
1615  SCIP_Real* solvals /**< solution values of variables in knapsack constraint; or NULL */
1616  )
1617 {
1618  int nnontrivialgubconss;
1619  int c;
1620 
1621  nnontrivialgubconss = 0;
1622 
1623  SCIPdebugMsg(scip, " Nontrivial GUBs of current GUB set:\n");
1624 
1625  /* print out all nontrivial GUB constraints, i.e., with more than one variable */
1626  for( c = 0; c < gubset->ngubconss; c++ )
1627  {
1628  SCIP_Real gubsolval;
1629 
1630  assert(gubset->gubconss[c]->ngubvars >= 0);
1631 
1632  /* nontrivial GUB */
1633  if( gubset->gubconss[c]->ngubvars > 1 )
1634  {
1635  int v;
1636 
1637  gubsolval = 0.0;
1638  SCIPdebugMsg(scip, " GUB<%d>:\n", c);
1639 
1640  /* print GUB var */
1641  for( v = 0; v < gubset->gubconss[c]->ngubvars; v++ )
1642  {
1643  int currentvar;
1644 
1645  currentvar = gubset->gubconss[c]->gubvars[v];
1646  if( solvals != NULL )
1647  {
1648  gubsolval += solvals[currentvar];
1649  SCIPdebugMsg(scip, " +<%s>(%4.2f)\n", SCIPvarGetName(vars[currentvar]), solvals[currentvar]);
1650  }
1651  else
1652  {
1653  SCIPdebugMsg(scip, " +<%s>\n", SCIPvarGetName(vars[currentvar]));
1654  }
1655  }
1656 
1657  /* check whether LP solution satisfies the GUB constraint */
1658  if( solvals != NULL )
1659  {
1660  SCIPdebugMsg(scip, " =%4.2f <= 1 %s\n", gubsolval,
1661  SCIPisFeasGT(scip, gubsolval, 1.0) ? "--> violated" : "");
1662  }
1663  else
1664  {
1665  SCIPdebugMsg(scip, " <= 1 %s\n", SCIPisFeasGT(scip, gubsolval, 1.0) ? "--> violated" : "");
1666  }
1667  nnontrivialgubconss++;
1668  }
1669  }
1670 
1671  SCIPdebugMsg(scip, " --> %d/%d nontrivial GUBs\n", nnontrivialgubconss, gubset->ngubconss);
1672 }
1673 #endif
1674 
1675 /** creates an empty GUB constraint */
1676 static
1678  SCIP* scip, /**< SCIP data structure */
1679  SCIP_GUBCONS** gubcons /**< pointer to store GUB constraint data */
1680  )
1681 {
1682  assert(scip != NULL);
1683  assert(gubcons != NULL);
1684 
1685  /* allocate memory for GUB constraint data structures */
1686  SCIP_CALL( SCIPallocBuffer(scip, gubcons) );
1687  (*gubcons)->gubvarssize = GUBCONSGROWVALUE;
1688  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubcons)->gubvars, (*gubcons)->gubvarssize) );
1689  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubcons)->gubvarsstatus, (*gubcons)->gubvarssize) );
1690 
1691  (*gubcons)->ngubvars = 0;
1692 
1693  return SCIP_OKAY;
1694 }
1695 
1696 /** frees GUB constraint */
1697 static
1698 void GUBconsFree(
1699  SCIP* scip, /**< SCIP data structure */
1700  SCIP_GUBCONS** gubcons /**< pointer to GUB constraint data structure */
1701  )
1702 {
1703  assert(scip != NULL);
1704  assert(gubcons != NULL);
1705  assert((*gubcons)->gubvars != NULL);
1706  assert((*gubcons)->gubvarsstatus != NULL);
1707 
1708  /* free allocated memory */
1709  SCIPfreeBufferArray(scip, &(*gubcons)->gubvarsstatus);
1710  SCIPfreeBufferArray(scip, &(*gubcons)->gubvars);
1711  SCIPfreeBuffer(scip, gubcons);
1712 }
1713 
1714 /** adds variable to given GUB constraint */
1715 static
1717  SCIP* scip, /**< SCIP data structure */
1718  SCIP_GUBCONS* gubcons, /**< GUB constraint data */
1719  int var /**< index of given variable in knapsack constraint */
1720  )
1721 {
1722  assert(scip != NULL);
1723  assert(gubcons != NULL);
1724  assert(gubcons->ngubvars >= 0 && gubcons->ngubvars < gubcons->gubvarssize);
1725  assert(gubcons->gubvars != NULL);
1726  assert(gubcons->gubvarsstatus != NULL);
1727  assert(var >= 0);
1728 
1729  /* add variable to GUB constraint */
1730  gubcons->gubvars[gubcons->ngubvars] = var;
1731  gubcons->gubvarsstatus[gubcons->ngubvars] = GUBVARSTATUS_UNINITIAL;
1732  gubcons->ngubvars++;
1733 
1734  /* increase space allocated to GUB constraint if the number of variables reaches the size */
1735  if( gubcons->ngubvars == gubcons->gubvarssize )
1736  {
1737  int newlen;
1738 
1739  newlen = gubcons->gubvarssize + GUBCONSGROWVALUE;
1740  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvars, newlen) );
1741  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvarsstatus, newlen) );
1742 
1743  gubcons->gubvarssize = newlen;
1744  }
1745 
1746  return SCIP_OKAY;
1747 }
1748 
1749 /** deletes variable from its current GUB constraint */
1750 static
1752  SCIP* scip, /**< SCIP data structure */
1753  SCIP_GUBCONS* gubcons, /**< GUB constraint data */
1754  int var, /**< index of given variable in knapsack constraint */
1755  int gubvarsidx /**< index of the variable in its current GUB constraint */
1756  )
1757 {
1758  assert(scip != NULL);
1759  assert(gubcons != NULL);
1760  assert(var >= 0);
1761  assert(gubvarsidx >= 0 && gubvarsidx < gubcons->ngubvars);
1762  assert(gubcons->ngubvars >= gubvarsidx+1);
1763  assert(gubcons->gubvars[gubvarsidx] == var);
1764 
1765  /* delete variable from GUB by swapping it replacing in by the last variable in the GUB constraint */
1766  gubcons->gubvars[gubvarsidx] = gubcons->gubvars[gubcons->ngubvars-1];
1767  gubcons->gubvarsstatus[gubvarsidx] = gubcons->gubvarsstatus[gubcons->ngubvars-1];
1768  gubcons->ngubvars--;
1769 
1770  /* decrease space allocated for the GUB constraint, if the last GUBCONSGROWVALUE+1 array entries are now empty */
1771  if( gubcons->ngubvars < gubcons->gubvarssize - GUBCONSGROWVALUE && gubcons->ngubvars > 0 )
1772  {
1773  int newlen;
1774 
1775  newlen = gubcons->gubvarssize - GUBCONSGROWVALUE;
1776 
1777  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvars, newlen) );
1778  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvarsstatus, newlen) );
1779 
1780  gubcons->gubvarssize = newlen;
1781  }
1782 
1783  return SCIP_OKAY;
1784 }
1785 
1786 /** moves variable from current GUB constraint to a different existing (nonempty) GUB constraint */
1787 static
1789  SCIP* scip, /**< SCIP data structure */
1790  SCIP_GUBSET* gubset, /**< GUB set data structure */
1791  SCIP_VAR** vars, /**< variables in knapsack constraint */
1792  int var, /**< index of given variable in knapsack constraint */
1793  int oldgubcons, /**< index of old GUB constraint of given variable */
1794  int newgubcons /**< index of new GUB constraint of given variable */
1795  )
1797  int oldgubvaridx;
1798  int replacevar;
1799  int j;
1800 
1801  assert(scip != NULL);
1802  assert(gubset != NULL);
1803  assert(var >= 0);
1804  assert(oldgubcons >= 0 && oldgubcons < gubset->ngubconss);
1805  assert(newgubcons >= 0 && newgubcons < gubset->ngubconss);
1806  assert(oldgubcons != newgubcons);
1807  assert(gubset->gubconssidx[var] == oldgubcons);
1808  assert(gubset->gubconss[oldgubcons]->ngubvars > 0);
1809  assert(gubset->gubconss[newgubcons]->ngubvars >= 0);
1810 
1811  SCIPdebugMsg(scip, " moving variable<%s> from GUB<%d> to GUB<%d>\n", SCIPvarGetName(vars[var]), oldgubcons, newgubcons);
1812 
1813  oldgubvaridx = gubset->gubvarsidx[var];
1814 
1815  /* delete variable from old GUB constraint by replacing it by the last variable of the GUB constraint */
1816  SCIP_CALL( GUBconsDelVar(scip, gubset->gubconss[oldgubcons], var, oldgubvaridx) );
1817 
1818  /* in GUB set, update stored index of variable in old GUB constraint for the variable used for replacement;
1819  * replacement variable is given by old position of the deleted variable
1820  */
1821  replacevar = gubset->gubconss[oldgubcons]->gubvars[oldgubvaridx];
1822  assert(gubset->gubvarsidx[replacevar] == gubset->gubconss[oldgubcons]->ngubvars);
1823  gubset->gubvarsidx[replacevar] = oldgubvaridx;
1824 
1825  /* add variable to the end of new GUB constraint */
1826  SCIP_CALL( GUBconsAddVar(scip, gubset->gubconss[newgubcons], var) );
1827  assert(gubset->gubconss[newgubcons]->gubvars[gubset->gubconss[newgubcons]->ngubvars-1] == var);
1828 
1829  /* in GUB set, update stored index of GUB of moved variable and stored index of variable in this GUB constraint */
1830  gubset->gubconssidx[var] = newgubcons;
1831  gubset->gubvarsidx[var] = gubset->gubconss[newgubcons]->ngubvars-1;
1832 
1833  /* delete old GUB constraint if it became empty */
1834  if( gubset->gubconss[oldgubcons]->ngubvars == 0 )
1835  {
1836  SCIPdebugMsg(scip, "deleting empty GUB cons<%d> from current GUB set\n", oldgubcons);
1837 #ifdef SCIP_DEBUG
1838  GUBsetPrint(scip, gubset, vars, NULL);
1839 #endif
1840 
1841  /* free old GUB constraint */
1842  GUBconsFree(scip, &gubset->gubconss[oldgubcons]);
1843 
1844  /* if empty GUB was not the last one in GUB set data structure, replace it by last GUB constraint */
1845  if( oldgubcons != gubset->ngubconss-1 )
1846  {
1847  gubset->gubconss[oldgubcons] = gubset->gubconss[gubset->ngubconss-1];
1848  gubset->gubconsstatus[oldgubcons] = gubset->gubconsstatus[gubset->ngubconss-1];
1849 
1850  /* in GUB set, update stored index of GUB constraint for all variable of the GUB constraint used for replacement;
1851  * replacement GUB is given by old position of the deleted GUB
1852  */
1853  for( j = 0; j < gubset->gubconss[oldgubcons]->ngubvars; j++ )
1854  {
1855  assert(gubset->gubconssidx[gubset->gubconss[oldgubcons]->gubvars[j]] == gubset->ngubconss-1);
1856  gubset->gubconssidx[gubset->gubconss[oldgubcons]->gubvars[j]] = oldgubcons;
1857  }
1858  }
1859 
1860  /* update number of GUB constraints */
1861  gubset->ngubconss--;
1862 
1863  /* variable should be at given new position, unless new GUB constraint replaced empty old GUB constraint
1864  * (because it was at the end of the GUB constraint array)
1865  */
1866  assert(gubset->gubconssidx[var] == newgubcons
1867  || (newgubcons == gubset->ngubconss && gubset->gubconssidx[var] == oldgubcons));
1868  }
1869 #ifndef NDEBUG
1870  else
1871  assert(gubset->gubconssidx[var] == newgubcons);
1872 #endif
1873 
1874  return SCIP_OKAY;
1875 }
1876 
1877 /** swaps two variables in the same GUB constraint */
1878 static
1879 void GUBsetSwapVars(
1880  SCIP* scip, /**< SCIP data structure */
1881  SCIP_GUBSET* gubset, /**< GUB set data structure */
1882  int var1, /**< first variable to be swapped */
1883  int var2 /**< second variable to be swapped */
1884  )
1885 {
1886  int gubcons;
1887  int var1idx;
1888  GUBVARSTATUS var1status;
1889  int var2idx;
1890  GUBVARSTATUS var2status;
1891 
1892  assert(scip != NULL);
1893  assert(gubset != NULL);
1894 
1895  gubcons = gubset->gubconssidx[var1];
1896  assert(gubcons == gubset->gubconssidx[var2]);
1897 
1898  /* nothing to be done if both variables are the same */
1899  if( var1 == var2 )
1900  return;
1901 
1902  /* swap index and status of variables in GUB constraint */
1903  var1idx = gubset->gubvarsidx[var1];
1904  var1status = gubset->gubconss[gubcons]->gubvarsstatus[var1idx];
1905  var2idx = gubset->gubvarsidx[var2];
1906  var2status = gubset->gubconss[gubcons]->gubvarsstatus[var2idx];
1907 
1908  gubset->gubvarsidx[var1] = var2idx;
1909  gubset->gubconss[gubcons]->gubvars[var1idx] = var2;
1910  gubset->gubconss[gubcons]->gubvarsstatus[var1idx] = var2status;
1911 
1912  gubset->gubvarsidx[var2] = var1idx;
1913  gubset->gubconss[gubcons]->gubvars[var2idx] = var1;
1914  gubset->gubconss[gubcons]->gubvarsstatus[var2idx] = var1status;
1915 }
1916 
1917 /** initializes partition of knapsack variables into nonoverlapping trivial GUB constraints (GUB with one variable) */
1918 static
1920  SCIP* scip, /**< SCIP data structure */
1921  SCIP_GUBSET** gubset, /**< pointer to store GUB set data structure */
1922  int nvars, /**< number of variables in the knapsack constraint */
1923  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
1924  SCIP_Longint capacity /**< capacity of knapsack */
1925  )
1926 {
1927  int i;
1928 
1929  assert(scip != NULL);
1930  assert(gubset != NULL);
1931  assert(nvars > 0);
1932  assert(weights != NULL);
1933  assert(capacity >= 0);
1934 
1935  /* allocate memory for GUB set data structures */
1936  SCIP_CALL( SCIPallocBuffer(scip, gubset) );
1937  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubconss, nvars) );
1938  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubconsstatus, nvars) );
1939  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubconssidx, nvars) );
1940  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubvarsidx, nvars) );
1941  (*gubset)->ngubconss = nvars;
1942  (*gubset)->nvars = nvars;
1943 
1944  /* initialize the set of GUB constraints */
1945  for( i = 0; i < nvars; i++ )
1946  {
1947  /* assign each variable to a new (trivial) GUB constraint */
1948  SCIP_CALL( GUBconsCreate(scip, &(*gubset)->gubconss[i]) );
1949  SCIP_CALL( GUBconsAddVar(scip, (*gubset)->gubconss[i], i) );
1950 
1951  /* set status of GUB constraint to initial */
1952  (*gubset)->gubconsstatus[i] = GUBCONSSTATUS_UNINITIAL;
1953 
1954  (*gubset)->gubconssidx[i] = i;
1955  (*gubset)->gubvarsidx[i] = 0;
1956  assert((*gubset)->gubconss[i]->ngubvars == 1);
1957 
1958  /* already updated status of variable in GUB constraint if it exceeds the capacity of the knapsack */
1959  if( weights[i] > capacity )
1960  (*gubset)->gubconss[(*gubset)->gubconssidx[i]]->gubvarsstatus[(*gubset)->gubvarsidx[i]] = GUBVARSTATUS_CAPACITYEXCEEDED;
1961  }
1962 
1963  return SCIP_OKAY;
1964 }
1965 
1966 /** frees GUB set data structure */
1967 static
1968 void GUBsetFree(
1969  SCIP* scip, /**< SCIP data structure */
1970  SCIP_GUBSET** gubset /**< pointer to GUB set data structure */
1971  )
1972 {
1973  int i;
1974 
1975  assert(scip != NULL);
1976  assert(gubset != NULL);
1977  assert((*gubset)->gubconss != NULL);
1978  assert((*gubset)->gubconsstatus != NULL);
1979  assert((*gubset)->gubconssidx != NULL);
1980  assert((*gubset)->gubvarsidx != NULL);
1981 
1982  /* free all GUB constraints */
1983  for( i = (*gubset)->ngubconss-1; i >= 0; --i )
1984  {
1985  assert((*gubset)->gubconss[i] != NULL);
1986  GUBconsFree(scip, &(*gubset)->gubconss[i]);
1987  }
1988 
1989  /* free allocated memory */
1990  SCIPfreeBufferArray( scip, &(*gubset)->gubvarsidx );
1991  SCIPfreeBufferArray( scip, &(*gubset)->gubconssidx );
1992  SCIPfreeBufferArray( scip, &(*gubset)->gubconsstatus );
1993  SCIPfreeBufferArray( scip, &(*gubset)->gubconss );
1994  SCIPfreeBuffer(scip, gubset);
1995 }
1996 
1997 #ifndef NDEBUG
1998 /** checks whether GUB set data structure is consistent */
1999 static
2001  SCIP* scip, /**< SCIP data structure */
2002  SCIP_GUBSET* gubset, /**< GUB set data structure */
2003  SCIP_VAR** vars /**< variables in the knapsack constraint */
2004  )
2005 {
2006  int i;
2007  int gubconsidx;
2008  int gubvaridx;
2009  SCIP_VAR* var1;
2010  SCIP_VAR* var2;
2011  SCIP_Bool var1negated;
2012  SCIP_Bool var2negated;
2013 
2014  assert(scip != NULL);
2015  assert(gubset != NULL);
2016 
2017  SCIPdebugMsg(scip, " GUB set consistency check:\n");
2018 
2019  /* checks for all knapsack vars consistency of stored index of associated gubcons and corresponding index in gubvars */
2020  for( i = 0; i < gubset->nvars; i++ )
2021  {
2022  gubconsidx = gubset->gubconssidx[i];
2023  gubvaridx = gubset->gubvarsidx[i];
2024 
2025  if( gubset->gubconss[gubconsidx]->gubvars[gubvaridx] != i )
2026  {
2027  SCIPdebugMsg(scip, " var<%d> should be in GUB<%d> at position<%d>, but stored is var<%d> instead\n", i,
2028  gubconsidx, gubvaridx, gubset->gubconss[gubconsidx]->gubvars[gubvaridx] );
2029  }
2030  assert(gubset->gubconss[gubconsidx]->gubvars[gubvaridx] == i);
2031  }
2032 
2033  /* checks for each GUB whether all pairs of its variables have a common clique */
2034  for( i = 0; i < gubset->ngubconss; i++ )
2035  {
2036  int j;
2037 
2038  for( j = 0; j < gubset->gubconss[i]->ngubvars; j++ )
2039  {
2040  int k;
2041 
2042  /* get corresponding active problem variable */
2043  var1 = vars[gubset->gubconss[i]->gubvars[j]];
2044  var1negated = FALSE;
2045  SCIP_CALL( SCIPvarGetProbvarBinary(&var1, &var1negated) );
2046 
2047  for( k = j+1; k < gubset->gubconss[i]->ngubvars; k++ )
2048  {
2049  /* get corresponding active problem variable */
2050  var2 = vars[gubset->gubconss[i]->gubvars[k]];
2051  var2negated = FALSE;
2052  SCIP_CALL( SCIPvarGetProbvarBinary(&var2, &var2negated) );
2053 
2054  if( !SCIPvarsHaveCommonClique(var1, !var1negated, var2, !var2negated, TRUE) )
2055  {
2056  SCIPdebugMsg(scip, " GUB<%d>: var<%d,%s> and var<%d,%s> do not share a clique\n", i, j,
2057  SCIPvarGetName(vars[gubset->gubconss[i]->gubvars[j]]), k,
2058  SCIPvarGetName(vars[gubset->gubconss[i]->gubvars[k]]));
2059  SCIPdebugMsg(scip, " GUB<%d>: var<%d,%s> and var<%d,%s> do not share a clique\n", i, j,
2060  SCIPvarGetName(var1), k,
2061  SCIPvarGetName(var2));
2062  }
2063 
2064  /* @todo: in case we used also negated cliques for the GUB partition, this assert has to be changed */
2065  assert(SCIPvarsHaveCommonClique(var1, !var1negated, var2, !var2negated, TRUE));
2066  }
2067  }
2068  }
2069  SCIPdebugMsg(scip, " --> successful\n");
2070 
2071  return SCIP_OKAY;
2072 }
2073 #endif
2074 
2075 /** calculates a partition of the given set of binary variables into cliques;
2076  * afterwards the output array contains one value for each variable, such that two variables got the same value iff they
2077  * were assigned to the same clique;
2078  * the first variable is always assigned to clique 0, and a variable can only be assigned to clique i if at least one of
2079  * the preceding variables was assigned to clique i-1;
2080  * note: in contrast to SCIPcalcCliquePartition(), variables with LP value 1 are put into trivial cliques (with one
2081  * variable) and for the remaining variables, a partition with a small number of cliques is constructed
2082  */
2083 
2084 static
2086  SCIP*const scip, /**< SCIP data structure */
2087  SCIP_VAR**const vars, /**< binary variables in the clique from which at most one can be set to 1 */
2088  int const nvars, /**< number of variables in the clique */
2089  int*const cliquepartition, /**< array of length nvars to store the clique partition */
2090  int*const ncliques, /**< pointer to store number of cliques actually contained in the partition */
2091  SCIP_Real* solvals /**< solution values of all given binary variables */
2092  )
2094  SCIP_VAR** tmpvars;
2095  SCIP_VAR** cliquevars;
2096  SCIP_Bool* cliquevalues;
2097  SCIP_Bool* tmpvalues;
2098  int* varseq;
2099  int* sortkeys;
2100  int ncliquevars;
2101  int maxncliquevarscomp;
2102  int nignorevars;
2103  int nvarsused;
2104  int i;
2105 
2106  assert(scip != NULL);
2107  assert(nvars == 0 || vars != NULL);
2108  assert(nvars == 0 || cliquepartition != NULL);
2109  assert(ncliques != NULL);
2110 
2111  if( nvars == 0 )
2112  {
2113  *ncliques = 0;
2114  return SCIP_OKAY;
2115  }
2116 
2117  /* allocate temporary memory for storing the variables of the current clique */
2118  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevars, nvars) );
2119  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevalues, nvars) );
2120  SCIP_CALL( SCIPallocBufferArray(scip, &tmpvalues, nvars) );
2121  SCIP_CALL( SCIPduplicateBufferArray(scip, &tmpvars, vars, nvars) );
2122  SCIP_CALL( SCIPallocBufferArray(scip, &varseq, nvars) );
2123  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, nvars) );
2124 
2125  /* initialize the cliquepartition array with -1 */
2126  /* initialize the tmpvalues array */
2127  for( i = nvars - 1; i >= 0; --i )
2128  {
2129  tmpvalues[i] = TRUE;
2130  cliquepartition[i] = -1;
2131  }
2132 
2133  /* get corresponding active problem variables */
2134  SCIP_CALL( SCIPvarsGetProbvarBinary(&tmpvars, &tmpvalues, nvars) );
2135 
2136  /* ignore variables with LP value 1 (will be assigned to trivial GUBs at the end) and sort remaining variables
2137  * by nondecreasing number of cliques the variables are in
2138  */
2139  nignorevars = 0;
2140  nvarsused = 0;
2141  for( i = 0; i < nvars; i++ )
2142  {
2143  if( SCIPisFeasEQ(scip, solvals[i], 1.0) )
2144  {
2145  /* variables with LP value 1 are put to the end of varseq array and will not be sorted */
2146  varseq[nvars-1-nignorevars] = i;
2147  nignorevars++;
2148  }
2149  else
2150  {
2151  /* remaining variables are put to the front of varseq array and will be sorted by their number of cliques */
2152  varseq[nvarsused] = i;
2153  sortkeys[nvarsused] = SCIPvarGetNCliques(tmpvars[i], tmpvalues[i]);
2154  nvarsused++;
2155  }
2156  }
2157  assert(nvarsused + nignorevars == nvars);
2158 
2159  /* sort variables with LP value less than 1 by nondecreasing order of the number of cliques they are in */
2160  SCIPsortIntInt(sortkeys, varseq, nvarsused);
2161 
2162  maxncliquevarscomp = MIN(nvars*nvars, MAXNCLIQUEVARSCOMP);
2163 
2164  /* calculate the clique partition */
2165  *ncliques = 0;
2166  for( i = 0; i < nvars; ++i )
2167  {
2168  if( cliquepartition[varseq[i]] == -1 )
2169  {
2170  int j;
2171 
2172  /* variable starts a new clique */
2173  cliquepartition[varseq[i]] = *ncliques;
2174  cliquevars[0] = tmpvars[varseq[i]];
2175  cliquevalues[0] = tmpvalues[varseq[i]];
2176  ncliquevars = 1;
2177 
2178  /* if variable is not active (multi-aggregated or fixed), it cannot be in any clique and
2179  * if the variable has LP value 1 we do not want it to be in nontrivial cliques
2180  */
2181  if( SCIPvarIsActive(tmpvars[varseq[i]]) && i < nvarsused )
2182  {
2183  /* greedily fill up the clique */
2184  for( j = i + 1; j < nvarsused; ++j )
2185  {
2186  /* if variable is not active (multi-aggregated or fixed), it cannot be in any clique */
2187  if( cliquepartition[varseq[j]] == -1 && SCIPvarIsActive(tmpvars[varseq[j]]) )
2188  {
2189  int k;
2190 
2191  /* check if every variable in the actual clique is in clique with the new variable */
2192  for( k = ncliquevars - 1; k >= 0; --k )
2193  {
2194  if( !SCIPvarsHaveCommonClique(tmpvars[varseq[j]], tmpvalues[varseq[j]], cliquevars[k],
2195  cliquevalues[k], TRUE) )
2196  break;
2197  }
2198 
2199  if( k == -1 )
2200  {
2201  /* put the variable into the same clique */
2202  cliquepartition[varseq[j]] = cliquepartition[varseq[i]];
2203  cliquevars[ncliquevars] = tmpvars[varseq[j]];
2204  cliquevalues[ncliquevars] = tmpvalues[varseq[j]];
2205  ++ncliquevars;
2206  }
2207  }
2208  }
2209  }
2210 
2211  /* this clique is finished */
2212  ++(*ncliques);
2213  }
2214  assert(cliquepartition[varseq[i]] >= 0 && cliquepartition[varseq[i]] < i + 1);
2215 
2216  /* break if we reached the maximal number of comparisons */
2217  if( i * nvars > maxncliquevarscomp )
2218  break;
2219  }
2220  /* if we had too many variables fill up the cliquepartition and put each variable in a separate clique */
2221  for( ; i < nvars; ++i )
2222  {
2223  if( cliquepartition[varseq[i]] == -1 )
2224  {
2225  cliquepartition[varseq[i]] = *ncliques;
2226  ++(*ncliques);
2227  }
2228  }
2229 
2230  /* free temporary memory */
2231  SCIPfreeBufferArray(scip, &sortkeys);
2232  SCIPfreeBufferArray(scip, &varseq);
2233  SCIPfreeBufferArray(scip, &tmpvars);
2234  SCIPfreeBufferArray(scip, &tmpvalues);
2235  SCIPfreeBufferArray(scip, &cliquevalues);
2236  SCIPfreeBufferArray(scip, &cliquevars);
2237 
2238  return SCIP_OKAY;
2239 }
2240 
2241 /** constructs sophisticated partition of knapsack variables into non-overlapping GUBs; current partition uses trivial GUBs */
2242 static
2244  SCIP* scip, /**< SCIP data structure */
2245  SCIP_GUBSET* gubset, /**< GUB set data structure */
2246  SCIP_VAR** vars, /**< variables in the knapsack constraint */
2247  SCIP_Real* solvals /**< solution values of all knapsack variables */
2248  )
2249 {
2250  int* cliquepartition;
2251  int* gubfirstvar;
2252  int ncliques;
2253  int currentgubconsidx;
2254  int newgubconsidx;
2255  int cliqueidx;
2256  int nvars;
2257  int i;
2258 
2259  assert(scip != NULL);
2260  assert(gubset != NULL);
2261  assert(vars != NULL);
2262 
2263  nvars = gubset->nvars;
2264  assert(nvars >= 0);
2265 
2266  /* allocate temporary memory for clique partition */
2267  SCIP_CALL( SCIPallocBufferArray(scip, &cliquepartition, nvars) );
2268 
2269  /* compute sophisticated clique partition */
2270  SCIP_CALL( GUBsetCalcCliquePartition(scip, vars, nvars, cliquepartition, &ncliques, solvals) );
2271 
2272  /* allocate temporary memory for GUB set data structure */
2273  SCIP_CALL( SCIPallocBufferArray(scip, &gubfirstvar, ncliques) );
2274 
2275  /* translate GUB partition into GUB set data structure */
2276  for( i = 0; i < ncliques; i++ )
2277  {
2278  /* initialize first variable for every GUB */
2279  gubfirstvar[i] = -1;
2280  }
2281  /* move every knapsack variable into GUB defined by clique partition */
2282  for( i = 0; i < nvars; i++ )
2283  {
2284  assert(cliquepartition[i] >= 0);
2285 
2286  cliqueidx = cliquepartition[i];
2287  currentgubconsidx = gubset->gubconssidx[i];
2288  assert(gubset->gubconss[currentgubconsidx]->ngubvars == 1 );
2289 
2290  /* variable is first element in GUB constraint defined by clique partition */
2291  if( gubfirstvar[cliqueidx] == -1 )
2292  {
2293  /* corresponding GUB constraint in GUB set data structure was already constructed (as initial trivial GUB);
2294  * note: no assert for gubconssidx, because it can changed due to deleting empty GUBs in GUBsetMoveVar()
2295  */
2296  assert(gubset->gubvarsidx[i] == 0);
2297  assert(gubset->gubconss[gubset->gubconssidx[i]]->gubvars[gubset->gubvarsidx[i]] == i);
2298 
2299  /* remember the first variable found for the current GUB */
2300  gubfirstvar[cliqueidx] = i;
2301  }
2302  /* variable is additional element of GUB constraint defined by clique partition */
2303  else
2304  {
2305  assert(gubfirstvar[cliqueidx] >= 0 && gubfirstvar[cliqueidx] < i);
2306 
2307  /* move variable to GUB constraint defined by clique partition; index of this GUB constraint is given by the
2308  * first variable of this GUB constraint
2309  */
2310  newgubconsidx = gubset->gubconssidx[gubfirstvar[cliqueidx]];
2311  assert(newgubconsidx != currentgubconsidx); /* because initially every variable is in a different GUB */
2312  SCIP_CALL( GUBsetMoveVar(scip, gubset, vars, i, currentgubconsidx, newgubconsidx) );
2313 
2314  assert(gubset->gubconss[gubset->gubconssidx[i]]->gubvars[gubset->gubvarsidx[i]] == i);
2315  }
2316  }
2317 
2318 #ifdef SCIP_DEBUG
2319  /* prints GUB set data structure */
2320  GUBsetPrint(scip, gubset, vars, solvals);
2321 #endif
2322 
2323 #ifndef NDEBUG
2324  /* checks consistency of GUB set data structure */
2325  SCIP_CALL( GUBsetCheck(scip, gubset, vars) );
2326 #endif
2327 
2328  /* free temporary memory */
2329  SCIPfreeBufferArray(scip, &gubfirstvar);
2330  SCIPfreeBufferArray(scip, &cliquepartition);
2331 
2332  return SCIP_OKAY;
2333 }
2334 
2335 /** gets a most violated cover C (\f$\sum_{j \in C} a_j > a_0\f$) for a given knapsack constraint \f$\sum_{j \in N} a_j x_j \leq a_0\f$
2336  * taking into consideration the following fixing: \f$j \in C\f$, if \f$j \in N_1 = \{j \in N : x^*_j = 1\}\f$ and
2337  * \f$j \in N \setminus C\f$, if \f$j \in N_0 = \{j \in N : x^*_j = 0\}\f$, if one exists.
2338  */
2339 static
2341  SCIP* scip, /**< SCIP data structure */
2342  SCIP_VAR** vars, /**< variables in knapsack constraint */
2343  int nvars, /**< number of variables in knapsack constraint */
2344  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2345  SCIP_Longint capacity, /**< capacity of knapsack */
2346  SCIP_Real* solvals, /**< solution values of all problem variables */
2347  int* covervars, /**< pointer to store cover variables */
2348  int* noncovervars, /**< pointer to store noncover variables */
2349  int* ncovervars, /**< pointer to store number of cover variables */
2350  int* nnoncovervars, /**< pointer to store number of noncover variables */
2351  SCIP_Longint* coverweight, /**< pointer to store weight of cover */
2352  SCIP_Bool* found, /**< pointer to store whether a cover was found */
2353  SCIP_Bool modtransused, /**< should modified transformed separation problem be used to find cover */
2354  int* ntightened, /**< pointer to store number of variables with tightened upper bound */
2355  SCIP_Bool* fractional /**< pointer to store whether the LP sol for knapsack vars is fractional */
2356  )
2357 {
2358  SCIP_Longint* transweights;
2359  SCIP_Real* transprofits;
2360  SCIP_Longint transcapacity;
2361  SCIP_Longint fixedonesweight;
2362  SCIP_Longint itemsweight;
2363  SCIP_Bool infeasible;
2364  int* fixedones;
2365  int* fixedzeros;
2366  int* items;
2367  int nfixedones;
2368  int nfixedzeros;
2369  int nitems;
2370  int j;
2371 
2372  assert(scip != NULL);
2373  assert(vars != NULL);
2374  assert(nvars > 0);
2375  assert(weights != NULL);
2376  assert(capacity >= 0);
2377  assert(solvals != NULL);
2378  assert(covervars != NULL);
2379  assert(noncovervars != NULL);
2380  assert(ncovervars != NULL);
2381  assert(nnoncovervars != NULL);
2382  assert(coverweight != NULL);
2383  assert(found != NULL);
2384  assert(ntightened != NULL);
2385  assert(fractional != NULL);
2386 
2387  SCIPdebugMsg(scip, " get cover for knapsack constraint\n");
2388 
2389  /* allocates temporary memory */
2390  SCIP_CALL( SCIPallocBufferArray(scip, &transweights, nvars) );
2391  SCIP_CALL( SCIPallocBufferArray(scip, &transprofits, nvars) );
2392  SCIP_CALL( SCIPallocBufferArray(scip, &fixedones, nvars) );
2393  SCIP_CALL( SCIPallocBufferArray(scip, &fixedzeros, nvars) );
2394  SCIP_CALL( SCIPallocBufferArray(scip, &items, nvars) );
2395 
2396  *found = FALSE;
2397  *ncovervars = 0;
2398  *nnoncovervars = 0;
2399  *coverweight = 0;
2400  *fractional = TRUE;
2401 
2402  /* gets the following sets
2403  * N_1 = {j in N : x*_j = 1} (fixedones),
2404  * N_0 = {j in N : x*_j = 0} (fixedzeros) and
2405  * N\(N_0 & N_1) (items),
2406  * where x*_j is the solution value of variable x_j
2407  */
2408  nfixedones = 0;
2409  nfixedzeros = 0;
2410  nitems = 0;
2411  fixedonesweight = 0;
2412  itemsweight = 0;
2413  *ntightened = 0;
2414  for( j = 0; j < nvars; j++ )
2415  {
2416  assert(SCIPvarIsBinary(vars[j]));
2417 
2418  /* tightens upper bound of x_j if weight of x_j is greater than capacity of knapsack */
2419  if( weights[j] > capacity )
2420  {
2421  SCIP_CALL( SCIPtightenVarUb(scip, vars[j], 0.0, FALSE, &infeasible, NULL) );
2422  assert(!infeasible);
2423  (*ntightened)++;
2424  continue;
2425  }
2426 
2427  /* variable x_j has solution value one */
2428  if( SCIPisFeasEQ(scip, solvals[j], 1.0) )
2429  {
2430  fixedones[nfixedones] = j;
2431  nfixedones++;
2432  fixedonesweight += weights[j];
2433  }
2434  /* variable x_j has solution value zero */
2435  else if( SCIPisFeasEQ(scip, solvals[j], 0.0) )
2436  {
2437  fixedzeros[nfixedzeros] = j;
2438  nfixedzeros++;
2439  }
2440  /* variable x_j has fractional solution value */
2441  else
2442  {
2443  assert( SCIPisFeasGT(scip, solvals[j], 0.0) && SCIPisFeasLT(scip, solvals[j], 1.0) );
2444  items[nitems] = j;
2445  nitems++;
2446  itemsweight += weights[j];
2447  }
2448  }
2449  assert(nfixedones + nfixedzeros + nitems == nvars - (*ntightened));
2450 
2451  /* sets whether the LP solution x* for the knapsack variables is fractional; if it is not fractional we stop
2452  * the separation routine
2453  */
2454  assert(nitems >= 0);
2455  if( nitems == 0 )
2456  {
2457  *fractional = FALSE;
2458  goto TERMINATE;
2459  }
2460  assert(*fractional);
2461 
2462  /* transforms the traditional separation problem (under consideration of the following fixing:
2463  * z_j = 1 for all j in N_1, z_j = 0 for all j in N_0)
2464  *
2465  * min sum_{j in N\(N_0 & N_1)} (1 - x*_j) z_j
2466  * sum_{j in N\(N_0 & N_1)} a_j z_j >= (a_0 + 1) - sum_{j in N_1} a_j
2467  * z_j in {0,1}, j in N\(N_0 & N_1)
2468  *
2469  * to a knapsack problem in maximization form by complementing the variables
2470  *
2471  * sum_{j in N\(N_0 & N_1)} (1 - x*_j) -
2472  * max sum_{j in N\(N_0 & N_1)} (1 - x*_j) z_j
2473  * sum_{j in N\(N_0 & N_1)} a_j z_j <= sum_{j in N\N_0} a_j - (a_0 + 1)
2474  * z_j in {0,1}, j in N\(N_0 & N_1)
2475  */
2476 
2477  /* gets weight and profit of variables in transformed knapsack problem */
2478  for( j = 0; j < nitems; j++ )
2479  {
2480  transweights[j] = weights[items[j]];
2481  transprofits[j] = 1.0 - solvals[items[j]];
2482  }
2483  /* gets capacity of transformed knapsack problem */
2484  transcapacity = fixedonesweight + itemsweight - capacity - 1;
2485 
2486  /* if capacity of transformed knapsack problem is less than zero, there is no cover
2487  * (when variables fixed to zero are not used)
2488  */
2489  if( transcapacity < 0 )
2490  {
2491  assert(!(*found));
2492  goto TERMINATE;
2493  }
2494 
2495  if( modtransused )
2496  {
2497  /* transforms the modified separation problem (under consideration of the following fixing:
2498  * z_j = 1 for all j in N_1, z_j = 0 for all j in N_0)
2499  *
2500  * min sum_{j in N\(N_0 & N_1)} (1 - x*_j) a_j z_j
2501  * sum_{j in N\(N_0 & N_1)} a_j z_j >= (a_0 + 1) - sum_{j in N_1} a_j
2502  * z_j in {0,1}, j in N\(N_0 & N_1)
2503  *
2504  * to a knapsack problem in maximization form by complementing the variables
2505  *
2506  * sum_{j in N\(N_0 & N_1)} (1 - x*_j) a_j -
2507  * max sum_{j in N\(N_0 & N_1)} (1 - x*_j) a_j z_j
2508  * sum_{j in N\(N_0 & N_1)} a_j z_j <= sum_{j in N\N_0} a_j - (a_0 + 1)
2509  * z_j in {0,1}, j in N\(N_0 & N_1)
2510  */
2511 
2512  /* gets weight and profit of variables in modified transformed knapsack problem */
2513  for( j = 0; j < nitems; j++ )
2514  {
2515  transprofits[j] *= weights[items[j]];
2516  assert(SCIPisFeasPositive(scip, transprofits[j]));
2517  }
2518  }
2519 
2520  /* solves (modified) transformed knapsack problem approximately by solving the LP-relaxation of the (modified)
2521  * transformed knapsack problem using Dantzig's method and rounding down the solution.
2522  * let z* be the solution, then
2523  * j in C, if z*_j = 0 and
2524  * i in N\C, if z*_j = 1.
2525  */
2526  SCIP_CALL( SCIPsolveKnapsackApproximately(scip, nitems, transweights, transprofits, transcapacity, items,
2527  noncovervars, covervars, nnoncovervars, ncovervars, NULL) );
2528  /*assert(checkSolveKnapsack(scip, nitems, transweights, transprofits, items, weights, solvals, modtransused));*/
2529 
2530  /* constructs cover C (sum_{j in C} a_j > a_0) */
2531  for( j = 0; j < *ncovervars; j++ )
2532  {
2533  (*coverweight) += weights[covervars[j]];
2534  }
2535 
2536  /* adds all variables from N_1 to C */
2537  for( j = 0; j < nfixedones; j++ )
2538  {
2539  covervars[*ncovervars] = fixedones[j];
2540  (*ncovervars)++;
2541  (*coverweight) += weights[fixedones[j]];
2542  }
2543 
2544  /* adds all variables from N_0 to N\C */
2545  for( j = 0; j < nfixedzeros; j++ )
2546  {
2547  noncovervars[*nnoncovervars] = fixedzeros[j];
2548  (*nnoncovervars)++;
2549  }
2550  assert((*ncovervars) + (*nnoncovervars) == nvars - (*ntightened));
2551  assert((*coverweight) > capacity);
2552  *found = TRUE;
2553 
2554  TERMINATE:
2555  /* frees temporary memory */
2556  SCIPfreeBufferArray(scip, &items);
2557  SCIPfreeBufferArray(scip, &fixedzeros);
2558  SCIPfreeBufferArray(scip, &fixedones);
2559  SCIPfreeBufferArray(scip, &transprofits);
2560  SCIPfreeBufferArray(scip, &transweights);
2561 
2562  SCIPdebugMsg(scip, " get cover for knapsack constraint -- end\n");
2563 
2564  return SCIP_OKAY;
2565 }
2566 
2567 #ifndef NDEBUG
2568 /** checks if minweightidx is set correctly
2569  */
2570 static
2572  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2573  SCIP_Longint capacity, /**< capacity of knapsack */
2574  int* covervars, /**< pointer to store cover variables */
2575  int ncovervars, /**< pointer to store number of cover variables */
2576  SCIP_Longint coverweight, /**< pointer to store weight of cover */
2577  int minweightidx, /**< index of variable in cover variables with minimum weight */
2578  int j /**< current index in cover variables */
2579  )
2580 {
2581  SCIP_Longint minweight;
2582  int i;
2583 
2584  assert(weights != NULL);
2585  assert(covervars != NULL);
2586  assert(ncovervars > 0);
2587 
2588  minweight = weights[covervars[minweightidx]];
2589 
2590  /* checks if all cover variables before index j have weight greater than minweight */
2591  for( i = 0; i < j; i++ )
2592  {
2593  assert(weights[covervars[i]] > minweight);
2594  if( weights[covervars[i]] <= minweight )
2595  return FALSE;
2596  }
2597 
2598  /* checks if all variables before index j cannot be removed, i.e. i cannot be the next minweightidx */
2599  for( i = 0; i < j; i++ )
2600  {
2601  assert(coverweight - weights[covervars[i]] <= capacity);
2602  if( coverweight - weights[covervars[i]] > capacity )
2603  return FALSE;
2604  }
2605  return TRUE;
2606 }
2607 #endif
2608 
2609 
2610 /** gets partition \f$(C_1,C_2)\f$ of minimal cover \f$C\f$, i.e. \f$C_1 \cup C_2 = C\f$ and \f$C_1 \cap C_2 = \emptyset\f$,
2611  * with \f$C_1\f$ not empty; chooses partition as follows \f$C_2 = \{ j \in C : x^*_j = 1 \}\f$ and \f$C_1 = C \setminus C_2\f$
2612  */
2613 static
2615  SCIP* scip, /**< SCIP data structure */
2616  SCIP_Real* solvals, /**< solution values of all problem variables */
2617  int* covervars, /**< cover variables */
2618  int ncovervars, /**< number of cover variables */
2619  int* varsC1, /**< pointer to store variables in C1 */
2620  int* varsC2, /**< pointer to store variables in C2 */
2621  int* nvarsC1, /**< pointer to store number of variables in C1 */
2622  int* nvarsC2 /**< pointer to store number of variables in C2 */
2623  )
2624 {
2625  int j;
2626 
2627  assert(scip != NULL);
2628  assert(ncovervars >= 0);
2629  assert(solvals != NULL);
2630  assert(covervars != NULL);
2631  assert(varsC1 != NULL);
2632  assert(varsC2 != NULL);
2633  assert(nvarsC1 != NULL);
2634  assert(nvarsC2 != NULL);
2635 
2636  *nvarsC1 = 0;
2637  *nvarsC2 = 0;
2638  for( j = 0; j < ncovervars; j++ )
2639  {
2640  assert(SCIPisFeasGT(scip, solvals[covervars[j]], 0.0));
2641 
2642  /* variable has solution value one */
2643  if( SCIPisGE(scip, solvals[covervars[j]], 1.0) )
2644  {
2645  varsC2[*nvarsC2] = covervars[j];
2646  (*nvarsC2)++;
2647  }
2648  /* variable has solution value less than one */
2649  else
2650  {
2651  assert(SCIPisLT(scip, solvals[covervars[j]], 1.0));
2652  varsC1[*nvarsC1] = covervars[j];
2653  (*nvarsC1)++;
2654  }
2655  }
2656  assert((*nvarsC1) + (*nvarsC2) == ncovervars);
2657 }
2658 
2659 /** changes given partition (C_1,C_2) of minimal cover C, if |C1| = 1, by moving one and two (if possible) variables from
2660  * C2 to C1 if |C1| = 1 and |C1| = 0, respectively.
2661  */
2662 static
2664  SCIP* scip, /**< SCIP data structure */
2665  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2666  int* varsC1, /**< pointer to store variables in C1 */
2667  int* varsC2, /**< pointer to store variables in C2 */
2668  int* nvarsC1, /**< pointer to store number of variables in C1 */
2669  int* nvarsC2 /**< pointer to store number of variables in C2 */
2670  )
2672  SCIP_Real* sortkeysC2;
2673  int j;
2674 
2675  assert(*nvarsC1 >= 0 && *nvarsC1 <= 1);
2676  assert(*nvarsC2 > 0);
2677 
2678  /* allocates temporary memory */
2679  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, *nvarsC2) );
2680 
2681  /* sorts variables in C2 such that a_1 >= .... >= a_|C2| */
2682  for( j = 0; j < *nvarsC2; j++ )
2683  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
2684  SCIPsortDownRealInt(sortkeysC2, varsC2, *nvarsC2);
2685 
2686  /* adds one or two variable from C2 with smallest weight to C1 and removes them from C2 */
2687  assert(*nvarsC2 == 1 || weights[varsC2[(*nvarsC2)-1]] <= weights[varsC2[(*nvarsC2)-2]]);
2688  while( *nvarsC1 < 2 && *nvarsC2 > 0 )
2689  {
2690  varsC1[*nvarsC1] = varsC2[(*nvarsC2)-1];
2691  (*nvarsC1)++;
2692  (*nvarsC2)--;
2693  }
2694 
2695  /* frees temporary memory */
2696  SCIPfreeBufferArray(scip, &sortkeysC2);
2697 
2698  return SCIP_OKAY;
2699 }
2700 
2701 /** changes given partition (C_1,C_2) of feasible set C, if |C1| = 1, by moving one variable from C2 to C1 */
2702 static
2704  SCIP* scip, /**< SCIP data structure */
2705  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2706  int* varsC1, /**< pointer to store variables in C1 */
2707  int* varsC2, /**< pointer to store variables in C2 */
2708  int* nvarsC1, /**< pointer to store number of variables in C1 */
2709  int* nvarsC2 /**< pointer to store number of variables in C2 */
2710  )
2712  SCIP_Real* sortkeysC2;
2713  int j;
2714 
2715  assert(*nvarsC1 >= 0 && *nvarsC1 <= 1);
2716  assert(*nvarsC2 > 0);
2717 
2718  /* allocates temporary memory */
2719  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, *nvarsC2) );
2720 
2721  /* sorts variables in C2 such that a_1 >= .... >= a_|C2| */
2722  for( j = 0; j < *nvarsC2; j++ )
2723  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
2724  SCIPsortDownRealInt(sortkeysC2, varsC2, *nvarsC2);
2725 
2726  /* adds variable from C2 with smallest weight to C1 and removes it from C2 */
2727  assert(*nvarsC2 == 1 || weights[varsC2[(*nvarsC2)-1]] <= weights[varsC2[(*nvarsC2)-2]]);
2728  varsC1[*nvarsC1] = varsC2[(*nvarsC2)-1];
2729  (*nvarsC1)++;
2730  (*nvarsC2)--;
2731 
2732  /* frees temporary memory */
2733  SCIPfreeBufferArray(scip, &sortkeysC2);
2734 
2735  return SCIP_OKAY;
2736 }
2737 
2738 
2739 /** gets partition \f$(F,R)\f$ of \f$N \setminus C\f$ where \f$C\f$ is a minimal cover, i.e. \f$F \cup R = N \setminus C\f$
2740  * and \f$F \cap R = \emptyset\f$; chooses partition as follows \f$R = \{ j \in N \setminus C : x^*_j = 0 \}\f$ and
2741  * \f$F = (N \setminus C) \setminus F\f$
2742  */
2743 static
2745  SCIP* scip, /**< SCIP data structure */
2746  SCIP_Real* solvals, /**< solution values of all problem variables */
2747  int* noncovervars, /**< noncover variables */
2748  int nnoncovervars, /**< number of noncover variables */
2749  int* varsF, /**< pointer to store variables in F */
2750  int* varsR, /**< pointer to store variables in R */
2751  int* nvarsF, /**< pointer to store number of variables in F */
2752  int* nvarsR /**< pointer to store number of variables in R */
2753  )
2754 {
2755  int j;
2756 
2757  assert(scip != NULL);
2758  assert(nnoncovervars >= 0);
2759  assert(solvals != NULL);
2760  assert(noncovervars != NULL);
2761  assert(varsF != NULL);
2762  assert(varsR != NULL);
2763  assert(nvarsF != NULL);
2764  assert(nvarsR != NULL);
2765 
2766  *nvarsF = 0;
2767  *nvarsR = 0;
2768 
2769  for( j = 0; j < nnoncovervars; j++ )
2770  {
2771  /* variable has solution value zero */
2772  if( SCIPisFeasEQ(scip, solvals[noncovervars[j]], 0.0) )
2773  {
2774  varsR[*nvarsR] = noncovervars[j];
2775  (*nvarsR)++;
2776  }
2777  /* variable has solution value greater than zero */
2778  else
2779  {
2780  assert(SCIPisFeasGT(scip, solvals[noncovervars[j]], 0.0));
2781  varsF[*nvarsF] = noncovervars[j];
2782  (*nvarsF)++;
2783  }
2784  }
2785  assert((*nvarsF) + (*nvarsR) == nnoncovervars);
2786 }
2787 
2788 /** sorts variables in F, C_2, and R according to the second level lifting sequence that will be used in the sequential
2789  * lifting procedure
2790  */
2791 static
2793  SCIP* scip, /**< SCIP data structure */
2794  SCIP_Real* solvals, /**< solution values of all problem variables */
2795  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2796  int* varsF, /**< pointer to store variables in F */
2797  int* varsC2, /**< pointer to store variables in C2 */
2798  int* varsR, /**< pointer to store variables in R */
2799  int nvarsF, /**< number of variables in F */
2800  int nvarsC2, /**< number of variables in C2 */
2801  int nvarsR /**< number of variables in R */
2802  )
2803 {
2804  SORTKEYPAIR** sortkeypairsF;
2805  SORTKEYPAIR* sortkeypairsFstore;
2806  SCIP_Real* sortkeysC2;
2807  SCIP_Real* sortkeysR;
2808  int j;
2809 
2810  assert(scip != NULL);
2811  assert(solvals != NULL);
2812  assert(weights != NULL);
2813  assert(varsF != NULL);
2814  assert(varsC2 != NULL);
2815  assert(varsR != NULL);
2816  assert(nvarsF >= 0);
2817  assert(nvarsC2 >= 0);
2818  assert(nvarsR >= 0);
2819 
2820  /* allocates temporary memory */
2821  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairsF, nvarsF) );
2822  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairsFstore, nvarsF) );
2823  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, nvarsC2) );
2824  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysR, nvarsR) );
2825 
2826  /* gets sorting key for variables in F corresponding to the following lifting sequence
2827  * sequence 1: non-increasing absolute difference between x*_j and the value the variable is fixed to, i.e.
2828  * x*_1 >= x*_2 >= ... >= x*_|F|
2829  * in case of equality uses
2830  * sequence 4: non-increasing a_j, i.e. a_1 >= a_2 >= ... >= a_|C_2|
2831  */
2832  for( j = 0; j < nvarsF; j++ )
2833  {
2834  sortkeypairsF[j] = &(sortkeypairsFstore[j]);
2835  sortkeypairsF[j]->key1 = solvals[varsF[j]];
2836  sortkeypairsF[j]->key2 = (SCIP_Real) weights[varsF[j]];
2837  }
2838 
2839  /* gets sorting key for variables in C_2 corresponding to the following lifting sequence
2840  * sequence 4: non-increasing a_j, i.e. a_1 >= a_2 >= ... >= a_|C_2|
2841  */
2842  for( j = 0; j < nvarsC2; j++ )
2843  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
2844 
2845  /* gets sorting key for variables in R corresponding to the following lifting sequence
2846  * sequence 4: non-increasing a_j, i.e. a_1 >= a_2 >= ... >= a_|R|
2847  */
2848  for( j = 0; j < nvarsR; j++ )
2849  sortkeysR[j] = (SCIP_Real) weights[varsR[j]];
2850 
2851  /* sorts F, C2 and R */
2852  if( nvarsF > 0 )
2853  {
2854  SCIPsortDownPtrInt((void**)sortkeypairsF, varsF, compSortkeypairs, nvarsF);
2855  }
2856  if( nvarsC2 > 0 )
2857  {
2858  SCIPsortDownRealInt(sortkeysC2, varsC2, nvarsC2);
2859  }
2860  if( nvarsR > 0)
2861  {
2862  SCIPsortDownRealInt(sortkeysR, varsR, nvarsR);
2863  }
2864 
2865  /* frees temporary memory */
2866  SCIPfreeBufferArray(scip, &sortkeysR);
2867  SCIPfreeBufferArray(scip, &sortkeysC2);
2868  SCIPfreeBufferArray(scip, &sortkeypairsFstore);
2869  SCIPfreeBufferArray(scip, &sortkeypairsF);
2870 
2871  return SCIP_OKAY;
2872 }
2873 
2874 /** categorizes GUBs of knapsack GUB partion into GOC1, GNC1, GF, GC2, and GR and computes a lifting sequence of the GUBs
2875  * for the sequential GUB wise lifting procedure
2876  */
2877 static
2879  SCIP* scip, /**< SCIP data structure */
2880  SCIP_GUBSET* gubset, /**< GUB set data structure */
2881  SCIP_Real* solvals, /**< solution values of variables in knapsack constraint */
2882  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2883  int* varsC1, /**< variables in C1 */
2884  int* varsC2, /**< variables in C2 */
2885  int* varsF, /**< variables in F */
2886  int* varsR, /**< variables in R */
2887  int nvarsC1, /**< number of variables in C1 */
2888  int nvarsC2, /**< number of variables in C2 */
2889  int nvarsF, /**< number of variables in F */
2890  int nvarsR, /**< number of variables in R */
2891  int* gubconsGC1, /**< pointer to store GUBs in GC1(GNC1+GOC1) */
2892  int* gubconsGC2, /**< pointer to store GUBs in GC2 */
2893  int* gubconsGFC1, /**< pointer to store GUBs in GFC1(GNC1+GF) */
2894  int* gubconsGR, /**< pointer to store GUBs in GR */
2895  int* ngubconsGC1, /**< pointer to store number of GUBs in GC1(GNC1+GOC1) */
2896  int* ngubconsGC2, /**< pointer to store number of GUBs in GC2 */
2897  int* ngubconsGFC1, /**< pointer to store number of GUBs in GFC1(GNC1+GF) */
2898  int* ngubconsGR, /**< pointer to store number of GUBs in GR */
2899  int* ngubconscapexceed, /**< pointer to store number of GUBs with only capacity exceeding variables */
2900  int* maxgubvarssize /**< pointer to store the maximal size of GUB constraints */
2901  )
2902 {
2903  SORTKEYPAIR** sortkeypairsGFC1;
2904  SORTKEYPAIR* sortkeypairsGFC1store;
2905  SCIP_Real* sortkeysC1;
2906  SCIP_Real* sortkeysC2;
2907  SCIP_Real* sortkeysR;
2908  int* nC1varsingubcons;
2909  int var;
2910  int gubconsidx;
2911  int varidx;
2912  int ngubconss;
2913  int ngubconsGOC1;
2914  int targetvar;
2915  int nvarsprocessed;
2916  int i;
2917  int j;
2918 
2919 #if GUBSPLITGNC1GUBS
2920  SCIP_Bool gubconswithF;
2921  int origngubconss;
2922  origngubconss = gubset->ngubconss;
2923 #endif
2924 
2925  assert(scip != NULL);
2926  assert(gubset != NULL);
2927  assert(solvals != NULL);
2928  assert(weights != NULL);
2929  assert(varsC1 != NULL);
2930  assert(varsC2 != NULL);
2931  assert(varsF != NULL);
2932  assert(varsR != NULL);
2933  assert(nvarsC1 > 0);
2934  assert(nvarsC2 >= 0);
2935  assert(nvarsF >= 0);
2936  assert(nvarsR >= 0);
2937  assert(gubconsGC1 != NULL);
2938  assert(gubconsGC2 != NULL);
2939  assert(gubconsGFC1 != NULL);
2940  assert(gubconsGR != NULL);
2941  assert(ngubconsGC1 != NULL);
2942  assert(ngubconsGC2 != NULL);
2943  assert(ngubconsGFC1 != NULL);
2944  assert(ngubconsGR != NULL);
2945  assert(maxgubvarssize != NULL);
2946 
2947  ngubconss = gubset->ngubconss;
2948  nvarsprocessed = 0;
2949  ngubconsGOC1 = 0;
2950 
2951  /* GUBs are categorized into different types according to the variables in volved
2952  * - GOC1: involves variables in C1 only -- no C2, R, F
2953  * - GNC1: involves variables in C1 and F (and R) -- no C2
2954  * - GF: involves variables in F (and R) only -- no C1, C2
2955  * - GC2: involves variables in C2 only -- no C1, R, F
2956  * - GR: involves variables in R only -- no C1, C2, F
2957  * which requires splitting GUBs in case they include variable in F and R.
2958  *
2959  * afterwards all GUBs (except GOC1 GUBs, which we do not need to lift) are sorted by a two level lifting sequence.
2960  * - first ordering level is: GFC1 (GNC1+GF), GC2, and GR.
2961  * - second ordering level is
2962  * GFC1: non-increasing number of variables in F and non-increasing max{x*_k : k in GFC1_j} in case of equality
2963  * GC2: non-increasing max{ a_k : k in GC2_j}; note that |GFC2_j| = 1
2964  * GR: non-increasing max{ a_k : k in GR_j}
2965  *
2966  * in additon, another GUB union, which is helpful for the lifting procedure, is formed
2967  * - GC1: GUBs of category GOC1 and GNC1
2968  * with second ordering level non-decreasing min{ a_k : k in GC1_j };
2969  * note that min{ a_k : k in GC1_j } always comes from the first variable in the GUB
2970  */
2971 
2972  /* allocates temporary memory */
2973  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC1, nvarsC1) );
2974  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, nvarsC2) );
2975  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysR, nvarsR) );
2976 
2977  /* to get the GUB lifting sequence, we first sort all variables in F, C2, and R
2978  * - F: non-increasing x*_j and non-increasing a_j in case of equality
2979  * - C2: non-increasing a_j
2980  * - R: non-increasing a_j
2981  * furthermore, sort C1 variables as needed for initializing the minweight table (non-increasing a_j).
2982  */
2983 
2984  /* gets sorting key for variables in C1 corresponding to the following ordering
2985  * non-decreasing a_j, i.e. a_1 <= a_2 <= ... <= a_|C_1|
2986  */
2987  for( j = 0; j < nvarsC1; j++ )
2988  {
2989  /* gets sortkeys */
2990  sortkeysC1[j] = (SCIP_Real) weights[varsC1[j]];
2991 
2992  /* update status of variable in its gub constraint */
2993  gubconsidx = gubset->gubconssidx[varsC1[j]];
2994  varidx = gubset->gubvarsidx[varsC1[j]];
2995  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_C1;
2996  }
2997 
2998  /* gets sorting key for variables in F corresponding to the following ordering
2999  * non-increasing x*_j, i.e., x*_1 >= x*_2 >= ... >= x*_|F|, and
3000  * non-increasing a_j, i.e., a_1 >= a_2 >= ... >= a_|F| in case of equality
3001  * and updates status of each variable in F in GUB set data structure
3002  */
3003  for( j = 0; j < nvarsF; j++ )
3004  {
3005  /* update status of variable in its gub constraint */
3006  gubconsidx = gubset->gubconssidx[varsF[j]];
3007  varidx = gubset->gubvarsidx[varsF[j]];
3008  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_F;
3009  }
3010 
3011  /* gets sorting key for variables in C2 corresponding to the following ordering
3012  * non-increasing a_j, i.e., a_1 >= a_2 >= ... >= a_|C2|
3013  * and updates status of each variable in F in GUB set data structure
3014  */
3015  for( j = 0; j < nvarsC2; j++ )
3016  {
3017  /* gets sortkeys */
3018  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
3019 
3020  /* update status of variable in its gub constraint */
3021  gubconsidx = gubset->gubconssidx[varsC2[j]];
3022  varidx = gubset->gubvarsidx[varsC2[j]];
3023  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_C2;
3024  }
3025 
3026  /* gets sorting key for variables in R corresponding to the following ordering
3027  * non-increasing a_j, i.e., a_1 >= a_2 >= ... >= a_|R|
3028  * and updates status of each variable in F in GUB set data structure
3029  */
3030  for( j = 0; j < nvarsR; j++ )
3031  {
3032  /* gets sortkeys */
3033  sortkeysR[j] = (SCIP_Real) weights[varsR[j]];
3034 
3035  /* update status of variable in its gub constraint */
3036  gubconsidx = gubset->gubconssidx[varsR[j]];
3037  varidx = gubset->gubvarsidx[varsR[j]];
3038  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_R;
3039  }
3040 
3041  /* sorts C1, F, C2 and R */
3042  assert(nvarsC1 > 0);
3043  SCIPsortRealInt(sortkeysC1, varsC1, nvarsC1);
3044 
3045  if( nvarsC2 > 0 )
3046  {
3047  SCIPsortDownRealInt(sortkeysC2, varsC2, nvarsC2);
3048  }
3049  if( nvarsR > 0)
3050  {
3051  SCIPsortDownRealInt(sortkeysR, varsR, nvarsR);
3052  }
3053 
3054  /* frees temporary memory */
3055  SCIPfreeBufferArray(scip, &sortkeysR);
3056  SCIPfreeBufferArray(scip, &sortkeysC2);
3057  SCIPfreeBufferArray(scip, &sortkeysC1);
3058 
3059  /* allocate and initialize temporary memory for sorting GUB constraints */
3060  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairsGFC1, ngubconss) );
3061  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairsGFC1store, ngubconss) );
3062  SCIP_CALL( SCIPallocBufferArray(scip, &nC1varsingubcons, ngubconss) );
3063  BMSclearMemoryArray(nC1varsingubcons, ngubconss);
3064  for( i = 0; i < ngubconss; i++)
3065  {
3066  sortkeypairsGFC1[i] = &(sortkeypairsGFC1store[i]);
3067  sortkeypairsGFC1[i]->key1 = 0.0;
3068  sortkeypairsGFC1[i]->key2 = 0.0;
3069  }
3070  *ngubconsGC1 = 0;
3071  *ngubconsGC2 = 0;
3072  *ngubconsGFC1 = 0;
3073  *ngubconsGR = 0;
3074  *ngubconscapexceed = 0;
3075  *maxgubvarssize = 0;
3076 
3077 #ifndef NDEBUG
3078  for( i = 0; i < gubset->ngubconss; i++ )
3079  assert(gubset->gubconsstatus[i] == GUBCONSSTATUS_UNINITIAL);
3080 #endif
3081 
3082  /* stores GUBs of group GC1 (GOC1+GNC1) and part of the GUBs of group GFC1 (GNC1 GUBs) and sorts variables in these GUBs
3083  * s.t. C1 variables come first (will automatically be sorted by non-decreasing weight).
3084  * gets sorting keys for GUBs of type GFC1 corresponding to the following ordering
3085  * non-increasing number of variables in F, and
3086  * non-increasing max{x*_k : k in GFC1_j} in case of equality
3087  */
3088  for( i = 0; i < nvarsC1; i++ )
3089  {
3090  int nvarsC1capexceed;
3091 
3092  nvarsC1capexceed = 0;
3093 
3094  var = varsC1[i];
3095  gubconsidx = gubset->gubconssidx[var];
3096  varidx = gubset->gubvarsidx[var];
3097 
3098  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3099  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_C1);
3100 
3101  /* current C1 variable is put to the front of its GUB where C1 part is stored by non-decreasing weigth;
3102  * note that variables in C1 are already sorted by non-decreasing weigth
3103  */
3104  targetvar = gubset->gubconss[gubconsidx]->gubvars[nC1varsingubcons[gubconsidx]];
3105  GUBsetSwapVars(scip, gubset, var, targetvar);
3106  nC1varsingubcons[gubconsidx]++;
3107 
3108  /* the GUB was already handled (status set and stored in its group) by another variable of the GUB */
3109  if( gubset->gubconsstatus[gubconsidx] != GUBCONSSTATUS_UNINITIAL )
3110  {
3111  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GOC1
3112  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3113  continue;
3114  }
3115 
3116  /* determine the status of the current GUB constraint, GOC1 or GNC1; GUBs involving R variables are split into
3117  * GOC1/GNC1 and GF, if wanted. also update sorting key if GUB is of type GFC1 (GNC1)
3118  */
3119 #if GUBSPLITGNC1GUBS
3120  gubconswithF = FALSE;
3121 #endif
3122  for( j = 0; j < gubset->gubconss[gubconsidx]->ngubvars; j++ )
3123  {
3124  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[j] != GUBVARSTATUS_BELONGSTOSET_C2);
3125 
3126  /* C1-variable: update number of C1/capacity exceeding variables */
3127  if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_C1 )
3128  {
3129  nvarsC1capexceed++;
3130  nvarsprocessed++;
3131  }
3132  /* F-variable: update sort key (number of F variables in GUB) of corresponding GFC1-GUB */
3133  else if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_F )
3134  {
3135 #if GUBSPLITGNC1GUBS
3136  gubconswithF = TRUE;
3137 #endif
3138  sortkeypairsGFC1[*ngubconsGFC1]->key1 += 1.0;
3139 
3140  if( solvals[gubset->gubconss[gubconsidx]->gubvars[j]] > sortkeypairsGFC1[*ngubconsGFC1]->key2 )
3141  sortkeypairsGFC1[*ngubconsGFC1]->key2 = solvals[gubset->gubconss[gubconsidx]->gubvars[j]];
3142  }
3143  else if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_CAPACITYEXCEEDED )
3144  {
3145  nvarsC1capexceed++;
3146  }
3147  else
3148  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_R);
3149  }
3150 
3151  /* update set of GC1 GUBs */
3152  gubconsGC1[*ngubconsGC1] = gubconsidx;
3153  (*ngubconsGC1)++;
3154 
3155  /* update maximum size of all GUB constraints */
3156  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3157  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3158 
3159  /* set status of GC1-GUB (GOC1 or GNC1) and update set of GFC1 GUBs */
3160  if( nvarsC1capexceed == gubset->gubconss[gubconsidx]->ngubvars )
3161  {
3162  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GOC1;
3163  ngubconsGOC1++;
3164  }
3165  else
3166  {
3167 #if GUBSPLITGNC1GUBS
3168  /* only variables in C1 and R -- no in F: GUB will be split into GR and GOC1 GUBs */
3169  if( !gubconswithF )
3170  {
3171  GUBVARSTATUS movevarstatus;
3172 
3173  assert(gubset->ngubconss < gubset->nvars);
3174 
3175  /* create a new GUB for GR part of splitting */
3176  SCIP_CALL( GUBconsCreate(scip, &gubset->gubconss[gubset->ngubconss]) );
3177  gubset->ngubconss++;
3178  ngubconss = gubset->ngubconss;
3179 
3180  /* fill GR with R variables in current GUB */
3181  for( j = gubset->gubconss[gubconsidx]->ngubvars-1; j >= 0; j-- )
3182  {
3183  movevarstatus = gubset->gubconss[gubconsidx]->gubvarsstatus[j];
3184  if( movevarstatus != GUBVARSTATUS_BELONGSTOSET_C1 )
3185  {
3186  assert(movevarstatus == GUBVARSTATUS_BELONGSTOSET_R || movevarstatus == GUBVARSTATUS_CAPACITYEXCEEDED);
3187  SCIP_CALL( GUBsetMoveVar(scip, gubset, vars, gubset->gubconss[gubconsidx]->gubvars[j],
3188  gubconsidx, ngubconss-1) );
3189  gubset->gubconss[ngubconss-1]->gubvarsstatus[gubset->gubconss[ngubconss-1]->ngubvars-1] =
3190  movevarstatus;
3191  }
3192  }
3193 
3194  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GOC1;
3195  ngubconsGOC1++;
3196 
3197  gubset->gubconsstatus[ngubconss-1] = GUBCONSSTATUS_BELONGSTOSET_GR;
3198  gubconsGR[*ngubconsGR] = ngubconss-1;
3199  (*ngubconsGR)++;
3200  }
3201  /* variables in C1, F, and maybe R: GNC1 GUB */
3202  else
3203  {
3204  assert(gubconswithF);
3205 
3206  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GNC1;
3207  gubconsGFC1[*ngubconsGFC1] = gubconsidx;
3208  (*ngubconsGFC1)++;
3209  }
3210 #else
3211  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GNC1;
3212  gubconsGFC1[*ngubconsGFC1] = gubconsidx;
3213  (*ngubconsGFC1)++;
3214 #endif
3215  }
3216  }
3217 
3218  /* stores GUBs of group GC2 (only trivial GUBs); sorting is not required because the C2 variables (which we loop over)
3219  * are already sorted correctly
3220  */
3221  for( i = 0; i < nvarsC2; i++ )
3222  {
3223  var = varsC2[i];
3224  gubconsidx = gubset->gubconssidx[var];
3225  varidx = gubset->gubvarsidx[var];
3226 
3227  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3228  assert(gubset->gubconss[gubconsidx]->ngubvars == 1);
3229  assert(varidx == 0);
3230  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_C2);
3231  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_UNINITIAL);
3232 
3233  /* set status of GC2 GUB */
3234  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GC2;
3235 
3236  /* update group of GC2 GUBs */
3237  gubconsGC2[*ngubconsGC2] = gubconsidx;
3238  (*ngubconsGC2)++;
3239 
3240  /* update maximum size of all GUB constraints */
3241  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3242  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3243 
3244  nvarsprocessed++;
3245  }
3246 
3247  /* stores remaining part of the GUBs of group GFC1 (GF GUBs) and gets GUB sorting keys corresp. to following ordering
3248  * non-increasing number of variables in F, and
3249  * non-increasing max{x*_k : k in GFC1_j} in case of equality
3250  */
3251  for( i = 0; i < nvarsF; i++ )
3252  {
3253  var = varsF[i];
3254  gubconsidx = gubset->gubconssidx[var];
3255  varidx = gubset->gubvarsidx[var];
3256 
3257  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3258  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_F);
3259 
3260  nvarsprocessed++;
3261 
3262  /* the GUB was already handled (status set and stored in its group) by another variable of the GUB */
3263  if( gubset->gubconsstatus[gubconsidx] != GUBCONSSTATUS_UNINITIAL )
3264  {
3265  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF
3266  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3267  continue;
3268  }
3269 
3270  /* set status of GF GUB */
3271  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GF;
3272 
3273  /* update sorting key of corresponding GFC1 GUB */
3274  for( j = 0; j < gubset->gubconss[gubconsidx]->ngubvars; j++ )
3275  {
3276  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[j] != GUBVARSTATUS_BELONGSTOSET_C2
3277  && gubset->gubconss[gubconsidx]->gubvarsstatus[j] != GUBVARSTATUS_BELONGSTOSET_C1);
3278 
3279  /* F-variable: update sort key (number of F variables in GUB) of corresponding GFC1-GUB */
3280  if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_F )
3281  {
3282  sortkeypairsGFC1[*ngubconsGFC1]->key1 += 1.0;
3283 
3284  if( solvals[gubset->gubconss[gubconsidx]->gubvars[j]] > sortkeypairsGFC1[*ngubconsGFC1]->key2 )
3285  sortkeypairsGFC1[*ngubconsGFC1]->key2 = solvals[gubset->gubconss[gubconsidx]->gubvars[j]];
3286  }
3287  }
3288 
3289  /* update set of GFC1 GUBs */
3290  gubconsGFC1[*ngubconsGFC1] = gubconsidx;
3291  (*ngubconsGFC1)++;
3292 
3293  /* update maximum size of all GUB constraints */
3294  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3295  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3296  }
3297 
3298  /* stores GUBs of group GR; sorting is not required because the R variables (which we loop over) are already sorted
3299  * correctly
3300  */
3301  for( i = 0; i < nvarsR; i++ )
3302  {
3303  var = varsR[i];
3304  gubconsidx = gubset->gubconssidx[var];
3305  varidx = gubset->gubvarsidx[var];
3306 
3307  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3308  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_R);
3309 
3310  nvarsprocessed++;
3311 
3312  /* the GUB was already handled (status set and stored in its group) by another variable of the GUB */
3313  if( gubset->gubconsstatus[gubconsidx] != GUBCONSSTATUS_UNINITIAL )
3314  {
3315  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GR
3316  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF
3317  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3318  continue;
3319  }
3320 
3321  /* set status of GR GUB */
3322  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GR;
3323 
3324  /* update set of GR GUBs */
3325  gubconsGR[*ngubconsGR] = gubconsidx;
3326  (*ngubconsGR)++;
3327 
3328  /* update maximum size of all GUB constraints */
3329  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3330  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3331  }
3332  assert(nvarsprocessed == nvarsC1 + nvarsC2 + nvarsF + nvarsR);
3333 
3334  /* update number of GUBs with only capacity exceeding variables (will not be used for lifting) */
3335  (*ngubconscapexceed) = ngubconss - (ngubconsGOC1 + (*ngubconsGC2) + (*ngubconsGFC1) + (*ngubconsGR));
3336  assert(*ngubconscapexceed >= 0);
3337 #ifndef NDEBUG
3338  {
3339  int check;
3340 
3341  check = 0;
3342 
3343  /* remaining not handled GUBs should only contain capacity exceeding variables */
3344  for( i = 0; i < ngubconss; i++ )
3345  {
3346  if( gubset->gubconsstatus[i] == GUBCONSSTATUS_UNINITIAL )
3347  check++;
3348  }
3349  assert(check == *ngubconscapexceed);
3350  }
3351 #endif
3352 
3353  /* sort GFCI GUBs according to computed sorting keys */
3354  if( (*ngubconsGFC1) > 0 )
3355  {
3356  SCIPsortDownPtrInt((void**)sortkeypairsGFC1, gubconsGFC1, compSortkeypairs, (*ngubconsGFC1));
3357  }
3358 
3359  /* free temporary memory */
3360 #if GUBSPLITGNC1GUBS
3361  ngubconss = origngubconss;
3362 #endif
3363  SCIPfreeBufferArray(scip, &nC1varsingubcons);
3364  SCIPfreeBufferArray(scip, &sortkeypairsGFC1store);
3365  SCIPfreeBufferArray(scip, &sortkeypairsGFC1);
3366 
3367  return SCIP_OKAY;
3368 }
3369 
3370 /** enlarges minweight table to at least the given length */
3371 static
3373  SCIP* scip, /**< SCIP data structure */
3374  SCIP_Longint** minweightsptr, /**< pointer to minweights table */
3375  int* minweightslen, /**< pointer to store number of entries in minweights table (incl. z=0) */
3376  int* minweightssize, /**< pointer to current size of minweights table */
3377  int newlen /**< new length of minweights table */
3378  )
3379 {
3380  int j;
3381 
3382  assert(minweightsptr != NULL);
3383  assert(*minweightsptr != NULL);
3384  assert(minweightslen != NULL);
3385  assert(*minweightslen >= 0);
3386  assert(minweightssize != NULL);
3387  assert(*minweightssize >= 0);
3388 
3389  if( newlen > *minweightssize )
3390  {
3391  int newsize;
3392 
3393  /* reallocate table memory */
3394  newsize = SCIPcalcMemGrowSize(scip, newlen);
3395  SCIP_CALL( SCIPreallocBufferArray(scip, minweightsptr, newsize) );
3396  *minweightssize = newsize;
3397  }
3398  assert(newlen <= *minweightssize);
3399 
3400  /* initialize new elements */
3401  for( j = *minweightslen; j < newlen; ++j )
3402  (*minweightsptr)[j] = SCIP_LONGINT_MAX;
3403  *minweightslen = newlen;
3404 
3405  return SCIP_OKAY;
3406 }
3407 
3408 /** lifts given inequality
3409  * sum_{j in M_1} x_j <= alpha_0
3410  * valid for
3411  * S^0 = { x in {0,1}^|M_1| : sum_{j in M_1} a_j x_j <= a_0 - sum_{j in M_2} a_j }
3412  * to a valid inequality
3413  * sum_{j in M_1} x_j + sum_{j in F} alpha_j x_j + sum_{j in M_2} alpha_j x_j + sum_{j in R} alpha_j x_j
3414  * <= alpha_0 + sum_{j in M_2} alpha_j
3415  * for
3416  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 };
3417  * uses sequential up-lifting for the variables in F, sequential down-lifting for the variable in M_2, and
3418  * sequential up-lifting for the variables in R; procedure can be used to strengthen minimal cover inequalities and
3419  * extended weight inequalities.
3420  */
3421 static
3423  SCIP* scip, /**< SCIP data structure */
3424  SCIP_VAR** vars, /**< variables in knapsack constraint */
3425  int nvars, /**< number of variables in knapsack constraint */
3426  int ntightened, /**< number of variables with tightened upper bound */
3427  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
3428  SCIP_Longint capacity, /**< capacity of knapsack */
3429  SCIP_Real* solvals, /**< solution values of all problem variables */
3430  int* varsM1, /**< variables in M_1 */
3431  int* varsM2, /**< variables in M_2 */
3432  int* varsF, /**< variables in F */
3433  int* varsR, /**< variables in R */
3434  int nvarsM1, /**< number of variables in M_1 */
3435  int nvarsM2, /**< number of variables in M_2 */
3436  int nvarsF, /**< number of variables in F */
3437  int nvarsR, /**< number of variables in R */
3438  int alpha0, /**< rights hand side of given valid inequality */
3439  int* liftcoefs, /**< pointer to store lifting coefficient of vars in knapsack constraint */
3440  SCIP_Real* cutact, /**< pointer to store activity of lifted valid inequality */
3441  int* liftrhs /**< pointer to store right hand side of the lifted valid inequality */
3442  )
3443 {
3444  SCIP_Longint* minweights;
3445  SCIP_Real* sortkeys;
3446  SCIP_Longint fixedonesweight;
3447  int minweightssize;
3448  int minweightslen;
3449  int j;
3450  int w;
3451 
3452  assert(scip != NULL);
3453  assert(vars != NULL);
3454  assert(nvars >= 0);
3455  assert(weights != NULL);
3456  assert(capacity >= 0);
3457  assert(solvals != NULL);
3458  assert(varsM1 != NULL);
3459  assert(varsM2 != NULL);
3460  assert(varsF != NULL);
3461  assert(varsR != NULL);
3462  assert(nvarsM1 >= 0 && nvarsM1 <= nvars - ntightened);
3463  assert(nvarsM2 >= 0 && nvarsM2 <= nvars - ntightened);
3464  assert(nvarsF >= 0 && nvarsF <= nvars - ntightened);
3465  assert(nvarsR >= 0 && nvarsR <= nvars - ntightened);
3466  assert(nvarsM1 + nvarsM2 + nvarsF + nvarsR == nvars - ntightened);
3467  assert(alpha0 >= 0);
3468  assert(liftcoefs != NULL);
3469  assert(cutact != NULL);
3470  assert(liftrhs != NULL);
3471 
3472  /* allocates temporary memory */
3473  minweightssize = nvarsM1 + 1;
3474  SCIP_CALL( SCIPallocBufferArray(scip, &minweights, minweightssize) );
3475  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, nvarsM1) );
3476 
3477  /* initializes data structures */
3478  BMSclearMemoryArray(liftcoefs, nvars);
3479  *cutact = 0.0;
3480 
3481  /* sets lifting coefficient of variables in M1, sorts variables in M1 such that a_1 <= a_2 <= ... <= a_|M1|
3482  * and calculates activity of the current valid inequality
3483  */
3484  for( j = 0; j < nvarsM1; j++ )
3485  {
3486  assert(liftcoefs[varsM1[j]] == 0);
3487  liftcoefs[varsM1[j]] = 1;
3488  sortkeys[j] = (SCIP_Real) (weights[varsM1[j]]);
3489  (*cutact) += solvals[varsM1[j]];
3490  }
3491 
3492  SCIPsortRealInt(sortkeys, varsM1, nvarsM1);
3493 
3494  /* initializes (i = 1) the minweight table, defined as: minweights_i[w] =
3495  * min sum_{j in M_1} a_j x_j + sum_{k=1}^{i-1} a_{j_k} x_{j_k}
3496  * s.t. sum_{j in M_1} x_j + sum_{k=1}^{i-1} alpha_{j_k} x_{j_k} >= w
3497  * x_j in {0,1} for j in M_1 & {j_i,...,j_i-1},
3498  * for i = 1,...,t with t = |N\M1| and w = 0,...,|M1| + sum_{k=1}^{i-1} alpha_{j_k};
3499  */
3500  minweights[0] = 0;
3501  for( w = 1; w <= nvarsM1; w++ )
3502  minweights[w] = minweights[w-1] + weights[varsM1[w-1]];
3503  minweightslen = nvarsM1 + 1;
3504 
3505  /* gets sum of weights of variables fixed to one, i.e. sum of weights of variables in M_2 */
3506  fixedonesweight = 0;
3507  for( j = 0; j < nvarsM2; j++ )
3508  fixedonesweight += weights[varsM2[j]];
3509  assert(fixedonesweight >= 0);
3510 
3511  /* initializes right hand side of lifted valid inequality */
3512  *liftrhs = alpha0;
3513 
3514  /* sequentially up-lifts all variables in F: */
3515  for( j = 0; j < nvarsF; j++ )
3516  {
3517  SCIP_Longint weight;
3518  int liftvar;
3519  int liftcoef;
3520  int z;
3521 
3522  liftvar = varsF[j];
3523  weight = weights[liftvar];
3524  assert(liftvar >= 0 && liftvar < nvars);
3525  assert(SCIPisFeasGT(scip, solvals[liftvar], 0.0));
3526  assert(weight > 0);
3527 
3528  /* knapsack problem is infeasible:
3529  * sets z = 0
3530  */
3531  if( capacity - fixedonesweight - weight < 0 )
3532  {
3533  z = 0;
3534  }
3535  /* knapsack problem is feasible:
3536  * sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i} } = liftrhs,
3537  * if minweights_i[liftrhs] <= a_0 - fixedonesweight - a_{j_i}
3538  */
3539  else if( minweights[*liftrhs] <= capacity - fixedonesweight - weight )
3540  {
3541  z = *liftrhs;
3542  }
3543  /* knapsack problem is feasible:
3544  * uses binary search to find z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i} }
3545  */
3546  else
3547  {
3548  int left;
3549  int right;
3550  int middle;
3551 
3552  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - fixedonesweight - weight);
3553  left = 0;
3554  right = (*liftrhs) + 1;
3555  while( left < right - 1 )
3556  {
3557  middle = (left + right) / 2;
3558  assert(0 <= middle && middle < minweightslen);
3559  if( minweights[middle] <= capacity - fixedonesweight - weight )
3560  left = middle;
3561  else
3562  right = middle;
3563  }
3564  assert(left == right - 1);
3565  assert(0 <= left && left < minweightslen);
3566  assert(minweights[left] <= capacity - fixedonesweight - weight );
3567  assert(left == minweightslen - 1 || minweights[left+1] > capacity - fixedonesweight - weight);
3568 
3569  /* now z = left */
3570  z = left;
3571  assert(z <= *liftrhs);
3572  }
3573 
3574  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
3575  liftcoef = (*liftrhs) - z;
3576  liftcoefs[liftvar] = liftcoef;
3577  assert(liftcoef >= 0 && liftcoef <= (*liftrhs) + 1);
3578 
3579  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
3580  if( liftcoef == 0 )
3581  continue;
3582 
3583  /* updates activity of current valid inequality */
3584  (*cutact) += liftcoef * solvals[liftvar];
3585 
3586  /* enlarges current minweight table:
3587  * from minweightlen = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 entries
3588  * to |M1| + sum_{k=1}^{i } alpha_{j_k} + 1 entries
3589  * and sets minweights_i[w] = infinity for
3590  * w = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 , ... , |M1| + sum_{k=1}^{i} alpha_{j_k}
3591  */
3592  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + liftcoef) );
3593 
3594  /* updates minweight table: minweight_i+1[w] =
3595  * min{ minweights_i[w], a_{j_i}}, if w < alpha_j_i
3596  * min{ minweights_i[w], minweights_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
3597  */
3598  for( w = minweightslen - 1; w >= 0; w-- )
3599  {
3600  SCIP_Longint min;
3601  if( w < liftcoef )
3602  {
3603  min = MIN(minweights[w], weight);
3604  minweights[w] = min;
3605  }
3606  else
3607  {
3608  assert(w >= liftcoef);
3609  min = MIN(minweights[w], minweights[w - liftcoef] + weight);
3610  minweights[w] = min;
3611  }
3612  }
3613  }
3614  assert(minweights[0] == 0);
3615 
3616  /* sequentially down-lifts all variables in M_2: */
3617  for( j = 0; j < nvarsM2; j++ )
3618  {
3619  SCIP_Longint weight;
3620  int liftvar;
3621  int liftcoef;
3622  int left;
3623  int right;
3624  int middle;
3625  int z;
3626 
3627  liftvar = varsM2[j];
3628  weight = weights[liftvar];
3629  assert(SCIPisFeasEQ(scip, solvals[liftvar], 1.0));
3630  assert(liftvar >= 0 && liftvar < nvars);
3631  assert(weight > 0);
3632 
3633  /* uses binary search to find
3634  * z = max { w : 0 <= w <= |M_1| + sum_{k=1}^{i-1} alpha_{j_k}, minweights_[w] <= a_0 - fixedonesweight + a_{j_i}}
3635  */
3636  left = 0;
3637  right = minweightslen;
3638  while( left < right - 1 )
3639  {
3640  middle = (left + right) / 2;
3641  assert(0 <= middle && middle < minweightslen);
3642  if( minweights[middle] <= capacity - fixedonesweight + weight )
3643  left = middle;
3644  else
3645  right = middle;
3646  }
3647  assert(left == right - 1);
3648  assert(0 <= left && left < minweightslen);
3649  assert(minweights[left] <= capacity - fixedonesweight + weight );
3650  assert(left == minweightslen - 1 || minweights[left+1] > capacity - fixedonesweight + weight);
3651 
3652  /* now z = left */
3653  z = left;
3654  assert(z >= *liftrhs);
3655 
3656  /* calculates lifting coefficients alpha_{j_i} = z - liftrhs */
3657  liftcoef = z - (*liftrhs);
3658  liftcoefs[liftvar] = liftcoef;
3659  assert(liftcoef >= 0);
3660 
3661  /* updates sum of weights of variables fixed to one */
3662  fixedonesweight -= weight;
3663 
3664  /* updates right-hand side of current valid inequality */
3665  (*liftrhs) += liftcoef;
3666  assert(*liftrhs >= alpha0);
3667 
3668  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
3669  if( liftcoef == 0 )
3670  continue;
3671 
3672  /* updates activity of current valid inequality */
3673  (*cutact) += liftcoef * solvals[liftvar];
3674 
3675  /* enlarges current minweight table:
3676  * from minweightlen = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 entries
3677  * to |M1| + sum_{k=1}^{i } alpha_{j_k} + 1 entries
3678  * and sets minweights_i[w] = infinity for
3679  * w = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 , ... , |M1| + sum_{k=1}^{i} alpha_{j_k}
3680  */
3681  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + liftcoef) );
3682 
3683  /* updates minweight table: minweight_i+1[w] =
3684  * min{ minweights_i[w], a_{j_i}}, if w < alpha_j_i
3685  * min{ minweights_i[w], minweights_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
3686  */
3687  for( w = minweightslen - 1; w >= 0; w-- )
3688  {
3689  SCIP_Longint min;
3690  if( w < liftcoef )
3691  {
3692  min = MIN(minweights[w], weight);
3693  minweights[w] = min;
3694  }
3695  else
3696  {
3697  assert(w >= liftcoef);
3698  min = MIN(minweights[w], minweights[w - liftcoef] + weight);
3699  minweights[w] = min;
3700  }
3701  }
3702  }
3703  assert(fixedonesweight == 0);
3704  assert(*liftrhs >= alpha0);
3705 
3706  /* sequentially up-lifts all variables in R: */
3707  for( j = 0; j < nvarsR; j++ )
3708  {
3709  SCIP_Longint weight;
3710  int liftvar;
3711  int liftcoef;
3712  int z;
3713 
3714  liftvar = varsR[j];
3715  weight = weights[liftvar];
3716  assert(liftvar >= 0 && liftvar < nvars);
3717  assert(SCIPisFeasEQ(scip, solvals[liftvar], 0.0));
3718  assert(weight > 0);
3719  assert(capacity - weight >= 0);
3720  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - weight);
3721 
3722  /* sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} } = liftrhs,
3723  * if minweights_i[liftrhs] <= a_0 - a_{j_i}
3724  */
3725  if( minweights[*liftrhs] <= capacity - weight )
3726  {
3727  z = *liftrhs;
3728  }
3729  /* uses binary search to find z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} }
3730  */
3731  else
3732  {
3733  int left;
3734  int right;
3735  int middle;
3736 
3737  left = 0;
3738  right = (*liftrhs) + 1;
3739  while( left < right - 1)
3740  {
3741  middle = (left + right) / 2;
3742  assert(0 <= middle && middle < minweightslen);
3743  if( minweights[middle] <= capacity - weight )
3744  left = middle;
3745  else
3746  right = middle;
3747  }
3748  assert(left == right - 1);
3749  assert(0 <= left && left < minweightslen);
3750  assert(minweights[left] <= capacity - weight );
3751  assert(left == minweightslen - 1 || minweights[left+1] > capacity - weight);
3752 
3753  /* now z = left */
3754  z = left;
3755  assert(z <= *liftrhs);
3756  }
3757 
3758  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
3759  liftcoef = (*liftrhs) - z;
3760  liftcoefs[liftvar] = liftcoef;
3761  assert(liftcoef >= 0 && liftcoef <= *liftrhs);
3762 
3763  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
3764  if( liftcoef == 0 )
3765  continue;
3766 
3767  /* updates activity of current valid inequality */
3768  (*cutact) += liftcoef * solvals[liftvar];
3769 
3770  /* updates minweight table: minweight_i+1[w] =
3771  * min{ minweight_i[w], a_{j_i}}, if w < alpha_j_i
3772  * min{ minweight_i[w], minweight_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
3773  */
3774  for( w = *liftrhs; w >= 0; w-- )
3775  {
3776  SCIP_Longint min;
3777  if( w < liftcoef )
3778  {
3779  min = MIN(minweights[w], weight);
3780  minweights[w] = min;
3781  }
3782  else
3783  {
3784  assert(w >= liftcoef);
3785  min = MIN(minweights[w], minweights[w - liftcoef] + weight);
3786  minweights[w] = min;
3787  }
3788  }
3789  }
3790 
3791  /* frees temporary memory */
3792  SCIPfreeBufferArray(scip, &sortkeys);
3793  SCIPfreeBufferArray(scip, &minweights);
3794 
3795  return SCIP_OKAY;
3796 }
3797 
3798 /** adds two minweight values in a safe way, i.e,, ensures no overflow */
3799 static
3801  SCIP_Longint val1, /**< first value to add */
3802  SCIP_Longint val2 /**< second value to add */
3803  )
3804 {
3805  assert(val1 >= 0);
3806  assert(val2 >= 0);
3807 
3808  if( val1 >= SCIP_LONGINT_MAX || val2 >= SCIP_LONGINT_MAX )
3809  return SCIP_LONGINT_MAX;
3810  else
3811  {
3812  assert(val1 <= SCIP_LONGINT_MAX - val2);
3813  return (val1 + val2);
3814  }
3815 }
3816 
3817 /** computes minweights table for lifting with GUBs by combining unfished and fished tables */
3818 static
3820  SCIP_Longint* minweights, /**< minweight table to compute */
3821  SCIP_Longint* finished, /**< given finished table */
3822  SCIP_Longint* unfinished, /**< given unfinished table */
3823  int minweightslen /**< length of minweight, finished, and unfinished tables */
3824  )
3825 {
3826  int w1;
3827  int w2;
3828 
3829  /* minweights_i[w] = min{finished_i[w1] + unfinished_i[w2] : w1>=0, w2>=0, w1+w2=w};
3830  * note that finished and unfished arrays sorted by non-decreasing weight
3831  */
3832 
3833  /* initialize minweight with w2 = 0 */
3834  w2 = 0;
3835  assert(unfinished[w2] == 0);
3836  for( w1 = 0; w1 < minweightslen; w1++ )
3837  minweights[w1] = finished[w1];
3838 
3839  /* consider w2 = 1, ..., minweightslen-1 */
3840  for( w2 = 1; w2 < minweightslen; w2++ )
3841  {
3842  if( unfinished[w2] >= SCIP_LONGINT_MAX )
3843  break;
3844 
3845  for( w1 = 0; w1 < minweightslen - w2; w1++ )
3846  {
3847  SCIP_Longint temp;
3848 
3849  temp = safeAddMinweightsGUB(finished[w1], unfinished[w2]);
3850  if( temp <= minweights[w1+w2] )
3851  minweights[w1+w2] = temp;
3852  }
3853  }
3854 }
3855 
3856 /** lifts given inequality
3857  * sum_{j in C_1} x_j <= alpha_0
3858  * valid for
3859  * S^0 = { x in {0,1}^|C_1| : sum_{j in C_1} a_j x_j <= a_0 - sum_{j in C_2} a_j;
3860  * sum_{j in Q_i} x_j <= 1, forall i in I }
3861  * to a valid inequality
3862  * sum_{j in C_1} x_j + sum_{j in F} alpha_j x_j + sum_{j in C_2} alpha_j x_j + sum_{j in R} alpha_j x_j
3863  * <= alpha_0 + sum_{j in C_2} alpha_j
3864  * for
3865  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0; sum_{j in Q_i} x_j <= 1, forall i in I };
3866  * uses sequential up-lifting for the variables in GUB constraints in gubconsGFC1,
3867  * sequential down-lifting for the variables in GUB constraints in gubconsGC2, and
3868  * sequential up-lifting for the variabels in GUB constraints in gubconsGR.
3869  */
3870 static
3872  SCIP* scip, /**< SCIP data structure */
3873  SCIP_GUBSET* gubset, /**< GUB set data structure */
3874  SCIP_VAR** vars, /**< variables in knapsack constraint */
3875  int ngubconscapexceed, /**< number of GUBs with only capacity exceeding variables */
3876  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
3877  SCIP_Longint capacity, /**< capacity of knapsack */
3878  SCIP_Real* solvals, /**< solution values of all knapsack variables */
3879  int* gubconsGC1, /**< GUBs in GC1(GNC1+GOC1) */
3880  int* gubconsGC2, /**< GUBs in GC2 */
3881  int* gubconsGFC1, /**< GUBs in GFC1(GNC1+GF) */
3882  int* gubconsGR, /**< GUBs in GR */
3883  int ngubconsGC1, /**< number of GUBs in GC1(GNC1+GOC1) */
3884  int ngubconsGC2, /**< number of GUBs in GC2 */
3885  int ngubconsGFC1, /**< number of GUBs in GFC1(GNC1+GF) */
3886  int ngubconsGR, /**< number of GUBs in GR */
3887  int alpha0, /**< rights hand side of given valid inequality */
3888  int* liftcoefs, /**< pointer to store lifting coefficient of vars in knapsack constraint */
3889  SCIP_Real* cutact, /**< pointer to store activity of lifted valid inequality */
3890  int* liftrhs, /**< pointer to store right hand side of the lifted valid inequality */
3891  int maxgubvarssize /**< maximal size of GUB constraints */
3892  )
3893 {
3894  SCIP_Longint* minweights;
3895  SCIP_Longint* finished;
3896  SCIP_Longint* unfinished;
3897  int* gubconsGOC1;
3898  int* gubconsGNC1;
3899  int* liftgubvars;
3900  SCIP_Longint fixedonesweight;
3901  SCIP_Longint weight;
3902  SCIP_Longint weightdiff1;
3903  SCIP_Longint weightdiff2;
3904  SCIP_Longint min;
3905  int minweightssize;
3906  int minweightslen;
3907  int nvars;
3908  int varidx;
3909  int liftgubconsidx;
3910  int liftvar;
3911  int sumliftcoef;
3912  int liftcoef;
3913  int ngubconsGOC1;
3914  int ngubconsGNC1;
3915  int left;
3916  int right;
3917  int middle;
3918  int nliftgubvars;
3919  int tmplen;
3920  int tmpsize;
3921  int j;
3922  int k;
3923  int w;
3924  int z;
3925 #ifndef NDEBUG
3926  int ngubconss;
3927  int nliftgubC1;
3928 
3929  assert(gubset != NULL);
3930  ngubconss = gubset->ngubconss;
3931 #else
3932  assert(gubset != NULL);
3933 #endif
3934 
3935  nvars = gubset->nvars;
3936 
3937  assert(scip != NULL);
3938  assert(vars != NULL);
3939  assert(nvars >= 0);
3940  assert(weights != NULL);
3941  assert(capacity >= 0);
3942  assert(solvals != NULL);
3943  assert(gubconsGC1 != NULL);
3944  assert(gubconsGC2 != NULL);
3945  assert(gubconsGFC1 != NULL);
3946  assert(gubconsGR != NULL);
3947  assert(ngubconsGC1 >= 0 && ngubconsGC1 <= ngubconss - ngubconscapexceed);
3948  assert(ngubconsGC2 >= 0 && ngubconsGC2 <= ngubconss - ngubconscapexceed);
3949  assert(ngubconsGFC1 >= 0 && ngubconsGFC1 <= ngubconss - ngubconscapexceed);
3950  assert(ngubconsGR >= 0 && ngubconsGR <= ngubconss - ngubconscapexceed);
3951  assert(alpha0 >= 0);
3952  assert(liftcoefs != NULL);
3953  assert(cutact != NULL);
3954  assert(liftrhs != NULL);
3955 
3956  minweightssize = ngubconsGC1+1;
3957 
3958  /* allocates temporary memory */
3959  SCIP_CALL( SCIPallocBufferArray(scip, &liftgubvars, maxgubvarssize) );
3960  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGOC1, ngubconsGC1) );
3961  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGNC1, ngubconsGC1) );
3962  SCIP_CALL( SCIPallocBufferArray(scip, &minweights, minweightssize) );
3963  SCIP_CALL( SCIPallocBufferArray(scip, &finished, minweightssize) );
3964  SCIP_CALL( SCIPallocBufferArray(scip, &unfinished, minweightssize) );
3965 
3966  /* initializes data structures */
3967  BMSclearMemoryArray(liftcoefs, nvars);
3968  *cutact = 0.0;
3969 
3970  /* gets GOC1 and GNC1 GUBs, sets lifting coefficient of variables in C1 and calculates activity of the current
3971  * valid inequality
3972  */
3973  ngubconsGOC1 = 0;
3974  ngubconsGNC1 = 0;
3975  for( j = 0; j < ngubconsGC1; j++ )
3976  {
3977  if( gubset->gubconsstatus[gubconsGC1[j]] == GUBCONSSTATUS_BELONGSTOSET_GOC1 )
3978  {
3979  gubconsGOC1[ngubconsGOC1] = gubconsGC1[j];
3980  ngubconsGOC1++;
3981  }
3982  else
3983  {
3984  assert(gubset->gubconsstatus[gubconsGC1[j]] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3985  gubconsGNC1[ngubconsGNC1] = gubconsGC1[j];
3986  ngubconsGNC1++;
3987  }
3988  for( k = 0; k < gubset->gubconss[gubconsGC1[j]]->ngubvars
3989  && gubset->gubconss[gubconsGC1[j]]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
3990  {
3991  varidx = gubset->gubconss[gubconsGC1[j]]->gubvars[k];
3992  assert(varidx >= 0 && varidx < nvars);
3993  assert(liftcoefs[varidx] == 0);
3994 
3995  liftcoefs[varidx] = 1;
3996  (*cutact) += solvals[varidx];
3997  }
3998  assert(k >= 1);
3999  }
4000  assert(ngubconsGOC1 + ngubconsGFC1 + ngubconsGC2 + ngubconsGR == ngubconss - ngubconscapexceed);
4001  assert(ngubconsGOC1 + ngubconsGNC1 == ngubconsGC1);
4002 
4003  /* initialize the minweight tables, defined as: for i = 1,...,m with m = |I| and w = 0,...,|gubconsGC1|;
4004  * - finished_i[w] =
4005  * min sum_{k = 1,2,...,i-1} sum_{j in Q_k} a_j x_j
4006  * s.t. sum_{k = 1,2,...,i-1} sum_{j in Q_k} alpha_j x_j >= w
4007  * sum_{j in Q_k} x_j <= 1
4008  * x_j in {0,1} forall j in Q_k forall k = 1,2,...,i-1,
4009  * - unfinished_i[w] =
4010  * min sum_{k = i+1,...,m} sum_{j in Q_k && j in C1} a_j x_j
4011  * s.t. sum_{k = i+1,...,m} sum_{j in Q_k && j in C1} x_j >= w
4012  * sum_{j in Q_k} x_j <= 1
4013  * x_j in {0,1} forall j in Q_k forall k = 1,2,...,i-1,
4014  * - minweights_i[w] = min{finished_i[w1] + unfinished_i[w2] : w1>=0, w2>=0, w1+w2=w};
4015  */
4016 
4017  /* initialize finished table; note that variables in GOC1 GUBs (includes C1 and capacity exceeding variables)
4018  * are sorted s.t. C1 variables come first and are sorted by non-decreasing weight.
4019  * GUBs in the group GCI are sorted by non-decreasing min{ a_k : k in GC1_j } where min{ a_k : k in GC1_j } always
4020  * comes from the first variable in the GUB
4021  */
4022  assert(ngubconsGOC1 <= ngubconsGC1);
4023  finished[0] = 0;
4024  for( w = 1; w <= ngubconsGOC1; w++ )
4025  {
4026  liftgubconsidx = gubconsGOC1[w-1];
4027 
4028  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GOC1);
4029  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4030 
4031  varidx = gubset->gubconss[liftgubconsidx]->gubvars[0];
4032 
4033  assert(varidx >= 0 && varidx < nvars);
4034  assert(liftcoefs[varidx] == 1);
4035 
4036  min = weights[varidx];
4037  finished[w] = finished[w-1] + min;
4038 
4039 #ifndef NDEBUG
4040  for( k = 1; k < gubset->gubconss[liftgubconsidx]->ngubvars
4041  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4042  {
4043  varidx = gubset->gubconss[liftgubconsidx]->gubvars[k];
4044  assert(varidx >= 0 && varidx < nvars);
4045  assert(liftcoefs[varidx] == 1);
4046  assert(weights[varidx] >= min);
4047  }
4048 #endif
4049  }
4050  for( w = ngubconsGOC1+1; w <= ngubconsGC1; w++ )
4051  finished[w] = SCIP_LONGINT_MAX;
4052 
4053  /* initialize unfinished table; note that variables in GNC1 GUBs
4054  * are sorted s.t. C1 variables come first and are sorted by non-decreasing weight.
4055  * GUBs in the group GCI are sorted by non-decreasing min{ a_k : k in GC1_j } where min{ a_k : k in GC1_j } always
4056  * comes from the first variable in the GUB
4057  */
4058  assert(ngubconsGNC1 <= ngubconsGC1);
4059  unfinished[0] = 0;
4060  for( w = 1; w <= ngubconsGNC1; w++ )
4061  {
4062  liftgubconsidx = gubconsGNC1[w-1];
4063 
4064  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
4065  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4066 
4067  varidx = gubset->gubconss[liftgubconsidx]->gubvars[0];
4068 
4069  assert(varidx >= 0 && varidx < nvars);
4070  assert(liftcoefs[varidx] == 1);
4071 
4072  min = weights[varidx];
4073  unfinished[w] = unfinished[w-1] + min;
4074 
4075 #ifndef NDEBUG
4076  for( k = 1; k < gubset->gubconss[liftgubconsidx]->ngubvars
4077  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4078  {
4079  varidx = gubset->gubconss[liftgubconsidx]->gubvars[k];
4080  assert(varidx >= 0 && varidx < nvars);
4081  assert(liftcoefs[varidx] == 1);
4082  assert(weights[varidx] >= min );
4083  }
4084 #endif
4085  }
4086  for( w = ngubconsGNC1 + 1; w <= ngubconsGC1; w++ )
4087  unfinished[w] = SCIP_LONGINT_MAX;
4088 
4089  /* initialize minweights table; note that variables in GC1 GUBs
4090  * are sorted s.t. C1 variables come first and are sorted by non-decreasing weight.
4091  * we can directly initialize minweights instead of computing it from finished and unfinished (which would be more time
4092  * consuming) because is it has to be build using weights from C1 only.
4093  */
4094  assert(ngubconsGOC1 + ngubconsGNC1 == ngubconsGC1);
4095  minweights[0] = 0;
4096  for( w = 1; w <= ngubconsGC1; w++ )
4097  {
4098  liftgubconsidx = gubconsGC1[w-1];
4099 
4100  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GOC1
4101  || gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
4102  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4103 
4104  varidx = gubset->gubconss[liftgubconsidx]->gubvars[0];
4105 
4106  assert(varidx >= 0 && varidx < nvars);
4107  assert(liftcoefs[varidx] == 1);
4108 
4109  min = weights[varidx];
4110  minweights[w] = minweights[w-1] + min;
4111 
4112 #ifndef NDEBUG
4113  for( k = 1; k < gubset->gubconss[liftgubconsidx]->ngubvars
4114  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4115  {
4116  varidx = gubset->gubconss[liftgubconsidx]->gubvars[k];
4117  assert(varidx >= 0 && varidx < nvars);
4118  assert(liftcoefs[varidx] == 1);
4119  assert(weights[varidx] >= min);
4120  }
4121 #endif
4122  }
4123  minweightslen = ngubconsGC1 + 1;
4124 
4125  /* gets sum of weights of variables fixed to one, i.e. sum of weights of C2 variables GC2 GUBs */
4126  fixedonesweight = 0;
4127  for( j = 0; j < ngubconsGC2; j++ )
4128  {
4129  varidx = gubset->gubconss[gubconsGC2[j]]->gubvars[0];
4130 
4131  assert(gubset->gubconss[gubconsGC2[j]]->ngubvars == 1);
4132  assert(varidx >= 0 && varidx < nvars);
4133  assert(gubset->gubconss[gubconsGC2[j]]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C2);
4134 
4135  fixedonesweight += weights[varidx];
4136  }
4137  assert(fixedonesweight >= 0);
4138 
4139  /* initializes right hand side of lifted valid inequality */
4140  *liftrhs = alpha0;
4141 
4142  /* sequentially up-lifts all variables in GFC1 GUBs */
4143  for( j = 0; j < ngubconsGFC1; j++ )
4144  {
4145  liftgubconsidx = gubconsGFC1[j];
4146  assert(liftgubconsidx >= 0 && liftgubconsidx < ngubconss);
4147 
4148  /* GNC1 GUB: update unfinished table (remove current GUB, i.e., remove min weight of C1 vars in GUB) and
4149  * compute minweight table via updated unfinished table and aleady upto date finished table;
4150  */
4151  k = 0;
4152  if( gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1 )
4153  {
4154  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
4155  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4156  assert(ngubconsGNC1 > 0);
4157 
4158  /* get number of C1 variables of current GNC1 GUB and put them into array of variables in GUB that
4159  * are considered for the lifting, i.e., not capacity exceeding
4160  */
4161  for( ; k < gubset->gubconss[liftgubconsidx]->ngubvars
4162  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4163  liftgubvars[k] = gubset->gubconss[liftgubconsidx]->gubvars[k];
4164  assert(k >= 1);
4165 
4166  /* update unfinished table by removing current GNC1 GUB, i.e, remove C1 variable with minimal weight
4167  * unfinished[w] = MAX{unfinished[w], unfinished[w+1] - weight}, "weight" is the minimal weight of current GUB
4168  */
4169  weight = weights[liftgubvars[0]];
4170 
4171  weightdiff2 = unfinished[ngubconsGNC1] - weight;
4172  unfinished[ngubconsGNC1] = SCIP_LONGINT_MAX;
4173  for( w = ngubconsGNC1-1; w >= 1; w-- )
4174  {
4175  weightdiff1 = weightdiff2;
4176  weightdiff2 = unfinished[w] - weight;
4177 
4178  if( unfinished[w] < weightdiff1 )
4179  unfinished[w] = weightdiff1;
4180  else
4181  break;
4182  }
4183  ngubconsGNC1--;
4184 
4185  /* computes minweights table by combining unfished and fished tables */
4186  computeMinweightsGUB(minweights, finished, unfinished, minweightslen);
4187  assert(minweights[0] == 0);
4188  }
4189  /* GF GUB: no update of unfinished table (and minweight table) required because GF GUBs have no C1 variables and
4190  * are therefore not in the unfinished table
4191  */
4192  else
4193  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF);
4194 
4195 #ifndef NDEBUG
4196  nliftgubC1 = k;
4197 #endif
4198  nliftgubvars = k;
4199  sumliftcoef = 0;
4200 
4201  /* compute lifting coefficient of F and R variables in GNC1 and GF GUBs (C1 vars have already liftcoef 1) */
4202  for( ; k < gubset->gubconss[liftgubconsidx]->ngubvars; k++ )
4203  {
4204  if( gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_F
4205  || gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_R )
4206  {
4207  liftvar = gubset->gubconss[liftgubconsidx]->gubvars[k];
4208  weight = weights[liftvar];
4209  assert(weight > 0);
4210  assert(liftvar >= 0 && liftvar < nvars);
4211  assert(capacity - weight >= 0);
4212 
4213  /* put variable into array of variables in GUB that are considered for the lifting,
4214  * i.e., not capacity exceeding
4215  */
4216  liftgubvars[nliftgubvars] = liftvar;
4217  nliftgubvars++;
4218 
4219  /* knapsack problem is infeasible:
4220  * sets z = 0
4221  */
4222  if( capacity - fixedonesweight - weight < 0 )
4223  {
4224  z = 0;
4225  }
4226  /* knapsack problem is feasible:
4227  * sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i} } = liftrhs,
4228  * if minweights_i[liftrhs] <= a_0 - fixedonesweight - a_{j_i}
4229  */
4230  else if( minweights[*liftrhs] <= capacity - fixedonesweight - weight )
4231  {
4232  z = *liftrhs;
4233  }
4234  /* knapsack problem is feasible:
4235  * binary search to find z = max {w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i}}
4236  */
4237  else
4238  {
4239  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - fixedonesweight - weight);
4240  left = 0;
4241  right = (*liftrhs) + 1;
4242  while( left < right - 1 )
4243  {
4244  middle = (left + right) / 2;
4245  assert(0 <= middle && middle < minweightslen);
4246  if( minweights[middle] <= capacity - fixedonesweight - weight )
4247  left = middle;
4248  else
4249  right = middle;
4250  }
4251  assert(left == right - 1);
4252  assert(0 <= left && left < minweightslen);
4253  assert(minweights[left] <= capacity - fixedonesweight - weight);
4254  assert(left == minweightslen - 1 || minweights[left+1] > capacity - fixedonesweight - weight);
4255 
4256  /* now z = left */
4257  z = left;
4258  assert(z <= *liftrhs);
4259  }
4260 
4261  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
4262  liftcoef = (*liftrhs) - z;
4263  liftcoefs[liftvar] = liftcoef;
4264  assert(liftcoef >= 0 && liftcoef <= (*liftrhs) + 1);
4265 
4266  /* updates activity of current valid inequality */
4267  (*cutact) += liftcoef * solvals[liftvar];
4268 
4269  /* updates sum of all lifting coefficients in GUB */
4270  sumliftcoef += liftcoefs[liftvar];
4271  }
4272  else
4273  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_CAPACITYEXCEEDED);
4274  }
4275  /* at least one variable is in F or R (j = number of C1 variables in current GUB) */
4276  assert(nliftgubvars > nliftgubC1);
4277 
4278  /* activity of current valid inequality will not change if (sum of alpha_{j_i} in GUB) = 0
4279  * and finished and minweight table can be updated easily as only C1 variables need to be considered;
4280  * not needed for GF GUBs
4281  */
4282  if( sumliftcoef == 0 )
4283  {
4284  if( gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1 )
4285  {
4286  weight = weights[liftgubvars[0]];
4287  /* update finished table and minweights table by applying special case of
4288  * finished[w] = MIN{finished[w], finished[w-1] + weight}, "weight" is the minimal weight of current GUB
4289  * minweights[w] = MIN{minweights[w], minweights[w-1] + weight}, "weight" is the minimal weight of current GUB
4290  */
4291  for( w = minweightslen-1; w >= 1; w-- )
4292  {
4293  SCIP_Longint tmpval;
4294 
4295  tmpval = safeAddMinweightsGUB(finished[w-1], weight);
4296  finished[w] = MIN(finished[w], tmpval);
4297 
4298  tmpval = safeAddMinweightsGUB(minweights[w-1], weight);
4299  minweights[w] = MIN(minweights[w], tmpval);
4300  }
4301  }
4302  else
4303  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF);
4304 
4305  continue;
4306  }
4307 
4308  /* enlarges current minweights tables(finished, unfinished, minweights):
4309  * from minweightlen = |gubconsGC1| + sum_{k=1,2,...,i-1}sum_{j in Q_k} alpha_j + 1 entries
4310  * to |gubconsGC1| + sum_{k=1,2,...,i }sum_{j in Q_k} alpha_j + 1 entries
4311  * and sets minweights_i[w] = infinity for
4312  * w = |gubconsGC1| + sum_{k=1,2,..,i-1}sum_{j in Q_k} alpha_j+1,..,|C1| + sum_{k=1,2,..,i}sum_{j in Q_k} alpha_j
4313  */
4314  tmplen = minweightslen; /* will be updated in enlargeMinweights() */
4315  tmpsize = minweightssize;
4316  SCIP_CALL( enlargeMinweights(scip, &unfinished, &tmplen, &tmpsize, tmplen + sumliftcoef) );
4317  tmplen = minweightslen;
4318  tmpsize = minweightssize;
4319  SCIP_CALL( enlargeMinweights(scip, &finished, &tmplen, &tmpsize, tmplen + sumliftcoef) );
4320  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + sumliftcoef) );
4321 
4322  /* update finished table and minweight table;
4323  * note that instead of computing minweight table from updated finished and updated unfinished table again
4324  * (for the lifting coefficient, we had to update unfinished table and compute minweight table), we here
4325  * only need to update the minweight table and the updated finished in the same way (i.e., computing for minweight
4326  * not needed because only finished table changed at this point and the change was "adding" one weight)
4327  *
4328  * update formular for minweight table is: minweight_i+1[w] =
4329  * min{ minweights_i[w], min{ minweights_i[w - alpha_k]^{+} + a_k : k in GUB_j_i } }
4330  * formular for finished table has the same pattern.
4331  */
4332  for( w = minweightslen-1; w >= 0; w-- )
4333  {
4334  SCIP_Longint minminweight;
4335  SCIP_Longint minfinished;
4336 
4337  for( k = 0; k < nliftgubvars; k++ )
4338  {
4339  liftcoef = liftcoefs[liftgubvars[k]];
4340  weight = weights[liftgubvars[k]];
4341 
4342  if( w < liftcoef )
4343  {
4344  minfinished = MIN(finished[w], weight);
4345  minminweight = MIN(minweights[w], weight);
4346 
4347  finished[w] = minfinished;
4348  minweights[w] = minminweight;
4349  }
4350  else
4351  {
4352  SCIP_Longint tmpval;
4353 
4354  assert(w >= liftcoef);
4355 
4356  tmpval = safeAddMinweightsGUB(finished[w-liftcoef], weight);
4357  minfinished = MIN(finished[w], tmpval);
4358 
4359  tmpval = safeAddMinweightsGUB(minweights[w-liftcoef], weight);
4360  minminweight = MIN(minweights[w], tmpval);
4361 
4362  finished[w] = minfinished;
4363  minweights[w] = minminweight;
4364  }
4365  }
4366  }
4367  assert(minweights[0] == 0);
4368  }
4369  assert(ngubconsGNC1 == 0);
4370 
4371  /* note: now the unfinished table no longer exists, i.e., it is "0, MAX, MAX, ..." and minweight equals to finished;
4372  * therefore, only work with minweight table from here on
4373  */
4374 
4375  /* sequentially down-lifts C2 variables contained in trivial GC2 GUBs */
4376  for( j = 0; j < ngubconsGC2; j++ )
4377  {
4378  liftgubconsidx = gubconsGC2[j];
4379 
4380  assert(liftgubconsidx >=0 && liftgubconsidx < ngubconss);
4381  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GC2);
4382  assert(gubset->gubconss[liftgubconsidx]->ngubvars == 1);
4383  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C2);
4384 
4385  liftvar = gubset->gubconss[liftgubconsidx]->gubvars[0]; /* C2 GUBs contain only one variable */
4386  weight = weights[liftvar];
4387 
4388  assert(liftvar >= 0 && liftvar < nvars);
4389  assert(SCIPisFeasEQ(scip, solvals[liftvar], 1.0));
4390  assert(weight > 0);
4391 
4392  /* uses binary search to find
4393  * z = max { w : 0 <= w <= |C_1| + sum_{k=1}^{i-1} alpha_{j_k}, minweights_[w] <= a_0 - fixedonesweight + a_{j_i}}
4394  */
4395  left = 0;
4396  right = minweightslen;
4397  while( left < right - 1 )
4398  {
4399  middle = (left + right) / 2;
4400  assert(0 <= middle && middle < minweightslen);
4401  if( minweights[middle] <= capacity - fixedonesweight + weight )
4402  left = middle;
4403  else
4404  right = middle;
4405  }
4406  assert(left == right - 1);
4407  assert(0 <= left && left < minweightslen);
4408  assert(minweights[left] <= capacity - fixedonesweight + weight);
4409  assert(left == minweightslen - 1 || minweights[left + 1] > capacity - fixedonesweight + weight);
4410 
4411  /* now z = left */
4412  z = left;
4413  assert(z >= *liftrhs);
4414 
4415  /* calculates lifting coefficients alpha_{j_i} = z - liftrhs */
4416  liftcoef = z - (*liftrhs);
4417  liftcoefs[liftvar] = liftcoef;
4418  assert(liftcoef >= 0);
4419 
4420  /* updates sum of weights of variables fixed to one */
4421  fixedonesweight -= weight;
4422 
4423  /* updates right-hand side of current valid inequality */
4424  (*liftrhs) += liftcoef;
4425  assert(*liftrhs >= alpha0);
4426 
4427  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
4428  if( liftcoef == 0 )
4429  continue;
4430 
4431  /* updates activity of current valid inequality */
4432  (*cutact) += liftcoef * solvals[liftvar];
4433 
4434  /* enlarges current minweight table:
4435  * from minweightlen = |gubconsGC1| + sum_{k=1,2,...,i-1}sum_{j in Q_k} alpha_j + 1 entries
4436  * to |gubconsGC1| + sum_{k=1,2,...,i }sum_{j in Q_k} alpha_j + 1 entries
4437  * and sets minweights_i[w] = infinity for
4438  * w = |C1| + sum_{k=1,2,...,i-1}sum_{j in Q_k} alpha_j + 1 , ... , |C1| + sum_{k=1,2,...,i}sum_{j in Q_k} alpha_j
4439  */
4440  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + liftcoef) );
4441 
4442  /* updates minweight table: minweight_i+1[w] =
4443  * min{ minweights_i[w], a_{j_i}}, if w < alpha_j_i
4444  * min{ minweights_i[w], minweights_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
4445  */
4446  for( w = minweightslen - 1; w >= 0; w-- )
4447  {
4448  if( w < liftcoef )
4449  {
4450  min = MIN(minweights[w], weight);
4451  minweights[w] = min;
4452  }
4453  else
4454  {
4455  SCIP_Longint tmpval;
4456 
4457  assert(w >= liftcoef);
4458 
4459  tmpval = safeAddMinweightsGUB(minweights[w-liftcoef], weight);
4460  min = MIN(minweights[w], tmpval);
4461  minweights[w] = min;
4462  }
4463  }
4464  }
4465  assert(fixedonesweight == 0);
4466  assert(*liftrhs >= alpha0);
4467 
4468  /* sequentially up-lifts variables in GUB constraints in GR GUBs */
4469  for( j = 0; j < ngubconsGR; j++ )
4470  {
4471  liftgubconsidx = gubconsGR[j];
4472 
4473  assert(liftgubconsidx >=0 && liftgubconsidx < ngubconss);
4474  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GR);
4475 
4476  sumliftcoef = 0;
4477  nliftgubvars = 0;
4478  for( k = 0; k < gubset->gubconss[liftgubconsidx]->ngubvars; k++ )
4479  {
4480  if(gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_R )
4481  {
4482  liftvar = gubset->gubconss[liftgubconsidx]->gubvars[k];
4483  weight = weights[liftvar];
4484  assert(weight > 0);
4485  assert(liftvar >= 0 && liftvar < nvars);
4486  assert(capacity - weight >= 0);
4487  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - weight);
4488 
4489  /* put variable into array of variables in GUB that are considered for the lifting,
4490  * i.e., not capacity exceeding
4491  */
4492  liftgubvars[nliftgubvars] = liftvar;
4493  nliftgubvars++;
4494 
4495  /* sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} } = liftrhs,
4496  * if minweights_i[liftrhs] <= a_0 - a_{j_i}
4497  */
4498  if( minweights[*liftrhs] <= capacity - weight )
4499  {
4500  z = *liftrhs;
4501  }
4502  /* uses binary search to find z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} }
4503  */
4504  else
4505  {
4506  left = 0;
4507  right = (*liftrhs) + 1;
4508  while( left < right - 1 )
4509  {
4510  middle = (left + right) / 2;
4511  assert(0 <= middle && middle < minweightslen);
4512  if( minweights[middle] <= capacity - weight )
4513  left = middle;
4514  else
4515  right = middle;
4516  }
4517  assert(left == right - 1);
4518  assert(0 <= left && left < minweightslen);
4519  assert(minweights[left] <= capacity - weight);
4520  assert(left == minweightslen - 1 || minweights[left + 1] > capacity - weight);
4521 
4522  /* now z = left */
4523  z = left;
4524  assert(z <= *liftrhs);
4525  }
4526  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
4527  liftcoef = (*liftrhs) - z;
4528  liftcoefs[liftvar] = liftcoef;
4529  assert(liftcoef >= 0 && liftcoef <= (*liftrhs) + 1);
4530 
4531  /* updates activity of current valid inequality */
4532  (*cutact) += liftcoef * solvals[liftvar];
4533 
4534  /* updates sum of all lifting coefficients in GUB */
4535  sumliftcoef += liftcoefs[liftvar];
4536  }
4537  else
4538  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_CAPACITYEXCEEDED);
4539  }
4540  assert(nliftgubvars >= 1); /* at least one variable is in R */
4541 
4542  /* minweight table and activity of current valid inequality will not change if (sum of alpha_{j_i} in GUB) = 0 */
4543  if( sumliftcoef == 0 )
4544  continue;
4545 
4546  /* updates minweight table: minweight_i+1[w] =
4547  * min{ minweights_i[w], min{ minweights_i[w - alpha_k]^{+} + a_k : k in GUB_j_i } }
4548  */
4549  for( w = *liftrhs; w >= 0; w-- )
4550  {
4551  for( k = 0; k < nliftgubvars; k++ )
4552  {
4553  liftcoef = liftcoefs[liftgubvars[k]];
4554  weight = weights[liftgubvars[k]];
4555 
4556  if( w < liftcoef )
4557  {
4558  min = MIN(minweights[w], weight);
4559  minweights[w] = min;
4560  }
4561  else
4562  {
4563  SCIP_Longint tmpval;
4564 
4565  assert(w >= liftcoef);
4566 
4567  tmpval = safeAddMinweightsGUB(minweights[w-liftcoef], weight);
4568  min = MIN(minweights[w], tmpval);
4569  minweights[w] = min;
4570  }
4571  }
4572  }
4573  assert(minweights[0] == 0);
4574  }
4575 
4576  /* frees temporary memory */
4577  SCIPfreeBufferArray(scip, &minweights);
4578  SCIPfreeBufferArray(scip, &finished);
4579  SCIPfreeBufferArray(scip, &unfinished);
4580  SCIPfreeBufferArray(scip, &liftgubvars);
4581  SCIPfreeBufferArray(scip, &gubconsGOC1 );
4582  SCIPfreeBufferArray(scip, &gubconsGNC1);
4583 
4584  return SCIP_OKAY;
4585 }
4586 
4587 /** lifts given minimal cover inequality
4588  * \f[
4589  * \sum_{j \in C} x_j \leq |C| - 1
4590  * \f]
4591  * valid for
4592  * \f[
4593  * S^0 = \{ x \in {0,1}^{|C|} : \sum_{j \in C} a_j x_j \leq a_0 \}
4594  * \f]
4595  * to a valid inequality
4596  * \f[
4597  * \sum_{j \in C} x_j + \sum_{j \in N \setminus C} \alpha_j x_j \leq |C| - 1
4598  * \f]
4599  * for
4600  * \f[
4601  * S = \{ x \in {0,1}^{|N|} : \sum_{j \in N} a_j x_j \leq a_0 \};
4602  * \f]
4603  * uses superadditive up-lifting for the variables in \f$N \setminus C\f$.
4604  */
4605 static
4607  SCIP* scip, /**< SCIP data structure */
4608  SCIP_VAR** vars, /**< variables in knapsack constraint */
4609  int nvars, /**< number of variables in knapsack constraint */
4610  int ntightened, /**< number of variables with tightened upper bound */
4611  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
4612  SCIP_Longint capacity, /**< capacity of knapsack */
4613  SCIP_Real* solvals, /**< solution values of all problem variables */
4614  int* covervars, /**< cover variables */
4615  int* noncovervars, /**< noncover variables */
4616  int ncovervars, /**< number of cover variables */
4617  int nnoncovervars, /**< number of noncover variables */
4618  SCIP_Longint coverweight, /**< weight of cover */
4619  SCIP_Real* liftcoefs, /**< pointer to store lifting coefficient of vars in knapsack constraint */
4620  SCIP_Real* cutact /**< pointer to store activity of lifted valid inequality */
4621  )
4622 {
4623  SCIP_Longint* maxweightsums;
4624  SCIP_Longint* intervalends;
4625  SCIP_Longint* rhos;
4626  SCIP_Real* sortkeys;
4627  SCIP_Longint lambda;
4628  int j;
4629  int h;
4630 
4631  assert(scip != NULL);
4632  assert(vars != NULL);
4633  assert(nvars >= 0);
4634  assert(weights != NULL);
4635  assert(capacity >= 0);
4636  assert(solvals != NULL);
4637  assert(covervars != NULL);
4638  assert(noncovervars != NULL);
4639  assert(ncovervars > 0 && ncovervars <= nvars);
4640  assert(nnoncovervars >= 0 && nnoncovervars <= nvars - ntightened);
4641  assert(ncovervars + nnoncovervars == nvars - ntightened);
4642  assert(liftcoefs != NULL);
4643  assert(cutact != NULL);
4644 
4645  /* allocates temporary memory */
4646  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, ncovervars) );
4647  SCIP_CALL( SCIPallocBufferArray(scip, &maxweightsums, ncovervars + 1) );
4648  SCIP_CALL( SCIPallocBufferArray(scip, &intervalends, ncovervars) );
4649  SCIP_CALL( SCIPallocBufferArray(scip, &rhos, ncovervars) );
4650 
4651  /* initializes data structures */
4652  BMSclearMemoryArray(liftcoefs, nvars);
4653  *cutact = 0.0;
4654 
4655  /* sets lifting coefficient of variables in C, sorts variables in C such that a_1 >= a_2 >= ... >= a_|C|
4656  * and calculates activity of current valid inequality
4657  */
4658  for( j = 0; j < ncovervars; j++ )
4659  {
4660  assert(liftcoefs[covervars[j]] == 0.0);
4661  liftcoefs[covervars[j]] = 1.0;
4662  sortkeys[j] = (SCIP_Real) weights[covervars[j]];
4663  (*cutact) += solvals[covervars[j]];
4664  }
4665  SCIPsortDownRealInt(sortkeys, covervars, ncovervars);
4666 
4667  /* calculates weight excess of cover C */
4668  lambda = coverweight - capacity;
4669  assert(lambda > 0);
4670 
4671  /* calculates A_h for h = 0,...,|C|, I_h for h = 1,...,|C| and rho_h for h = 1,...,|C| */
4672  maxweightsums[0] = 0;
4673  for( h = 1; h <= ncovervars; h++ )
4674  {
4675  maxweightsums[h] = maxweightsums[h-1] + weights[covervars[h-1]];
4676  intervalends[h-1] = maxweightsums[h] - lambda;
4677  rhos[h-1] = MAX(0, weights[covervars[h-1]] - weights[covervars[0]] + lambda);
4678  }
4679 
4680  /* sorts variables in N\C such that a_{j_1} <= a_{j_2} <= ... <= a_{j_t} */
4681  for( j = 0; j < nnoncovervars; j++ )
4682  sortkeys[j] = (SCIP_Real) (weights[noncovervars[j]]);
4683  SCIPsortRealInt(sortkeys, noncovervars, nnoncovervars);
4684 
4685  /* calculates lifting coefficient for all variables in N\C */
4686  h = 0;
4687  for( j = 0; j < nnoncovervars; j++ )
4688  {
4689  int liftvar;
4690  SCIP_Longint weight;
4691  SCIP_Real liftcoef;
4692 
4693  liftvar = noncovervars[j];
4694  weight = weights[liftvar];
4695 
4696  while( intervalends[h] < weight )
4697  h++;
4698 
4699  if( h == 0 )
4700  liftcoef = h;
4701  else
4702  {
4703  if( weight <= intervalends[h-1] + rhos[h] )
4704  {
4705  SCIP_Real tmp1;
4706  SCIP_Real tmp2;
4707  tmp1 = (SCIP_Real) (intervalends[h-1] + rhos[h] - weight);
4708  tmp2 = (SCIP_Real) rhos[1];
4709  liftcoef = h - ( tmp1 / tmp2 );
4710  }
4711  else
4712  liftcoef = h;
4713  }
4714 
4715  /* sets lifting coefficient */
4716  assert(liftcoefs[liftvar] == 0.0);
4717  liftcoefs[liftvar] = liftcoef;
4718 
4719  /* updates activity of current valid inequality */
4720  (*cutact) += liftcoef * solvals[liftvar];
4721  }
4722 
4723  /* frees temporary memory */
4724  SCIPfreeBufferArray(scip, &rhos);
4725  SCIPfreeBufferArray(scip, &intervalends);
4726  SCIPfreeBufferArray(scip, &maxweightsums);
4727  SCIPfreeBufferArray(scip, &sortkeys);
4728 
4729  return SCIP_OKAY;
4730 }
4731 
4732 
4733 /** separates lifted minimal cover inequalities using sequential up- and down-lifting and GUB information, if wanted, for
4734  * given knapsack problem
4735 */
4736 static
4738  SCIP* scip, /**< SCIP data structure */
4739  SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
4740  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
4741  SCIP_VAR** vars, /**< variables in knapsack constraint */
4742  int nvars, /**< number of variables in knapsack constraint */
4743  int ntightened, /**< number of variables with tightened upper bound */
4744  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
4745  SCIP_Longint capacity, /**< capacity of knapsack */
4746  SCIP_Real* solvals, /**< solution values of all problem variables */
4747  int* mincovervars, /**< mincover variables */
4748  int* nonmincovervars, /**< nonmincover variables */
4749  int nmincovervars, /**< number of mincover variables */
4750  int nnonmincovervars, /**< number of nonmincover variables */
4751  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
4752  SCIP_GUBSET* gubset, /**< GUB set data structure, NULL if no GUB information should be used */
4753  SCIP_Bool* cutoff, /**< pointer to store whether a cutoff has been detected */
4754  int* ncuts /**< pointer to add up the number of found cuts */
4755  )
4756 {
4757  int* varsC1;
4758  int* varsC2;
4759  int* varsF;
4760  int* varsR;
4761  int nvarsC1;
4762  int nvarsC2;
4763  int nvarsF;
4764  int nvarsR;
4765  SCIP_Real cutact;
4766  int* liftcoefs;
4767  int liftrhs;
4768 
4769  assert( cutoff != NULL );
4770  *cutoff = FALSE;
4771 
4772  /* allocates temporary memory */
4773  SCIP_CALL( SCIPallocBufferArray(scip, &varsC1, nvars) );
4774  SCIP_CALL( SCIPallocBufferArray(scip, &varsC2, nvars) );
4775  SCIP_CALL( SCIPallocBufferArray(scip, &varsF, nvars) );
4776  SCIP_CALL( SCIPallocBufferArray(scip, &varsR, nvars) );
4777  SCIP_CALL( SCIPallocBufferArray(scip, &liftcoefs, nvars) );
4778 
4779  /* gets partition (C_1,C_2) of C, i.e. C_1 & C_2 = C and C_1 cap C_2 = emptyset, with C_1 not empty; chooses partition
4780  * as follows
4781  * C_2 = { j in C : x*_j = 1 } and
4782  * C_1 = C\C_2
4783  */
4784  getPartitionCovervars(scip, solvals, mincovervars, nmincovervars, varsC1, varsC2, &nvarsC1, &nvarsC2);
4785  assert(nvarsC1 + nvarsC2 == nmincovervars);
4786  assert(nmincovervars > 0);
4787  assert(nvarsC1 >= 0); /* nvarsC1 > 0 does not always hold, because relaxed knapsack conss may already be violated */
4788 
4789  /* changes partition (C_1,C_2) of minimal cover C, if |C1| = 1, by moving one variable from C2 to C1 */
4790  if( nvarsC1 < 2 && nvarsC2 > 0)
4791  {
4792  SCIP_CALL( changePartitionCovervars(scip, weights, varsC1, varsC2, &nvarsC1, &nvarsC2) );
4793  assert(nvarsC1 >= 1);
4794  }
4795  assert(nvarsC2 == 0 || nvarsC1 >= 1);
4796 
4797  /* gets partition (F,R) of N\C, i.e. F & R = N\C and F cap R = emptyset; chooses partition as follows
4798  * R = { j in N\C : x*_j = 0 } and
4799  * F = (N\C)\F
4800  */
4801  getPartitionNoncovervars(scip, solvals, nonmincovervars, nnonmincovervars, varsF, varsR, &nvarsF, &nvarsR);
4802  assert(nvarsF + nvarsR == nnonmincovervars);
4803  assert(nvarsC1 + nvarsC2 + nvarsF + nvarsR == nvars - ntightened);
4804 
4805  /* lift cuts without GUB information */
4806  if( gubset == NULL )
4807  {
4808  /* sorts variables in F, C_2, R according to the second level lifting sequence that will be used in the sequential
4809  * lifting procedure
4810  */
4811  SCIP_CALL( getLiftingSequence(scip, solvals, weights, varsF, varsC2, varsR, nvarsF, nvarsC2, nvarsR) );
4812 
4813  /* lifts minimal cover inequality sum_{j in C_1} x_j <= |C_1| - 1 valid for
4814  *
4815  * S^0 = { x in {0,1}^|C_1| : sum_{j in C_1} a_j x_j <= a_0 - sum_{j in C_2} a_j }
4816  *
4817  * to a valid inequality sum_{j in C_1} x_j + sum_{j in N\C_1} alpha_j x_j <= |C_1| - 1 + sum_{j in C_2} alpha_j for
4818  *
4819  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 },
4820  *
4821  * uses sequential up-lifting for the variables in F, sequential down-lifting for the variable in C_2 and sequential
4822  * up-lifting for the variables in R according to the second level lifting sequence
4823  */
4824  SCIP_CALL( sequentialUpAndDownLifting(scip, vars, nvars, ntightened, weights, capacity, solvals, varsC1, varsC2,
4825  varsF, varsR, nvarsC1, nvarsC2, nvarsF, nvarsR, nvarsC1 - 1, liftcoefs, &cutact, &liftrhs) );
4826  }
4827  /* lift cuts with GUB information */
4828  else
4829  {
4830  int* gubconsGC1;
4831  int* gubconsGC2;
4832  int* gubconsGFC1;
4833  int* gubconsGR;
4834  int ngubconsGC1;
4835  int ngubconsGC2;
4836  int ngubconsGFC1;
4837  int ngubconsGR;
4838  int ngubconss;
4839  int nconstightened;
4840  int maxgubvarssize;
4841 
4842  assert(nvars == gubset->nvars);
4843 
4844  ngubconsGC1 = 0;
4845  ngubconsGC2 = 0;
4846  ngubconsGFC1 = 0;
4847  ngubconsGR = 0;
4848  ngubconss = gubset->ngubconss;
4849  nconstightened = 0;
4850  maxgubvarssize = 0;
4851 
4852  /* allocates temporary memory */
4853  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGC1, ngubconss) );
4854  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGC2, ngubconss) );
4855  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGFC1, ngubconss) );
4856  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGR, ngubconss) );
4857 
4858  /* categorizies GUBs of knapsack GUB partion into GOC1, GNC1, GF, GC2, and GR and computes a lifting sequence of
4859  * the GUBs for the sequential GUB wise lifting procedure
4860  */
4861  SCIP_CALL( getLiftingSequenceGUB(scip, gubset, solvals, weights, varsC1, varsC2, varsF, varsR, nvarsC1,
4862  nvarsC2, nvarsF, nvarsR, gubconsGC1, gubconsGC2, gubconsGFC1, gubconsGR, &ngubconsGC1, &ngubconsGC2,
4863  &ngubconsGFC1, &ngubconsGR, &nconstightened, &maxgubvarssize) );
4864 
4865  /* lifts minimal cover inequality sum_{j in C_1} x_j <= |C_1| - 1 valid for
4866  *
4867  * S^0 = { x in {0,1}^|C_1| : sum_{j in C_1} a_j x_j <= a_0 - sum_{j in C_2} a_j,
4868  * sum_{j in Q_i} x_j <= 1, forall i in I }
4869  *
4870  * to a valid inequality sum_{j in C_1} x_j + sum_{j in N\C_1} alpha_j x_j <= |C_1| - 1 + sum_{j in C_2} alpha_j for
4871  *
4872  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0, sum_{j in Q_i} x_j <= 1, forall i in I },
4873  *
4874  * uses sequential up-lifting for the variables in GUB constraints in gubconsGFC1,
4875  * sequential down-lifting for the variables in GUB constraints in gubconsGC2, and
4876  * sequential up-lifting for the variabels in GUB constraints in gubconsGR.
4877  */
4878  SCIP_CALL( sequentialUpAndDownLiftingGUB(scip, gubset, vars, nconstightened, weights, capacity, solvals, gubconsGC1,
4879  gubconsGC2, gubconsGFC1, gubconsGR, ngubconsGC1, ngubconsGC2, ngubconsGFC1, ngubconsGR,
4880  MIN(nvarsC1 - 1, ngubconsGC1), liftcoefs, &cutact, &liftrhs, maxgubvarssize) );
4881 
4882  /* frees temporary memory */
4883  SCIPfreeBufferArray(scip, &gubconsGR);
4884  SCIPfreeBufferArray(scip, &gubconsGFC1);
4885  SCIPfreeBufferArray(scip, &gubconsGC2);
4886  SCIPfreeBufferArray(scip, &gubconsGC1);
4887  }
4888 
4889  /* checks, if lifting yielded a violated cut */
4890  if( SCIPisEfficacious(scip, (cutact - liftrhs)/sqrt((SCIP_Real)MAX(liftrhs, 1))) )
4891  {
4892  SCIP_ROW* row;
4893  char name[SCIP_MAXSTRLEN];
4894  int j;
4895 
4896  /* creates LP row */
4897  assert( cons == NULL || sepa == NULL );
4898  if ( cons != NULL )
4899  {
4900  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcseq%" SCIP_LONGINT_FORMAT "", SCIPconsGetName(cons), SCIPconshdlrGetNCutsFound(SCIPconsGetHdlr(cons)));
4901  SCIP_CALL( SCIPcreateEmptyRowCons(scip, &row, cons, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs,
4902  cons != NULL ? SCIPconsIsLocal(cons) : FALSE, FALSE,
4903  cons != NULL ? SCIPconsIsRemovable(cons) : TRUE) );
4904  }
4905  else if ( sepa != NULL )
4906  {
4907  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcseq_%" SCIP_LONGINT_FORMAT "", SCIPsepaGetName(sepa), SCIPsepaGetNCutsFound(sepa));
4908  SCIP_CALL( SCIPcreateEmptyRowSepa(scip, &row, sepa, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
4909  }
4910  else
4911  {
4912  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nn_mcseq_%d", *ncuts);
4913  SCIP_CALL( SCIPcreateEmptyRowUnspec(scip, &row, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
4914  }
4915 
4916  /* adds all variables in the knapsack constraint with calculated lifting coefficient to the cut */
4917  SCIP_CALL( SCIPcacheRowExtensions(scip, row) );
4918  assert(nvarsC1 + nvarsC2 + nvarsF + nvarsR == nvars - ntightened);
4919  for( j = 0; j < nvarsC1; j++ )
4920  {
4921  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsC1[j]], 1.0) );
4922  }
4923  for( j = 0; j < nvarsC2; j++ )
4924  {
4925  if( liftcoefs[varsC2[j]] > 0 )
4926  {
4927  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsC2[j]], (SCIP_Real)liftcoefs[varsC2[j]]) );
4928  }
4929  }
4930  for( j = 0; j < nvarsF; j++ )
4931  {
4932  if( liftcoefs[varsF[j]] > 0 )
4933  {
4934  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsF[j]], (SCIP_Real)liftcoefs[varsF[j]]) );
4935  }
4936  }
4937  for( j = 0; j < nvarsR; j++ )
4938  {
4939  if( liftcoefs[varsR[j]] > 0 )
4940  {
4941  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsR[j]], (SCIP_Real)liftcoefs[varsR[j]]) );
4942  }
4943  }
4944  SCIP_CALL( SCIPflushRowExtensions(scip, row) );
4945 
4946  /* checks, if cut is violated enough */
4947  if( SCIPisCutEfficacious(scip, sol, row) )
4948  {
4949  if( cons != NULL )
4950  {
4951  SCIP_CALL( SCIPresetConsAge(scip, cons) );
4952  }
4953  SCIP_CALL( SCIPaddRow(scip, row, FALSE, cutoff) );
4954  (*ncuts)++;
4955  }
4956  SCIP_CALL( SCIPreleaseRow(scip, &row) );
4957  }
4958 
4959  /* frees temporary memory */
4960  SCIPfreeBufferArray(scip, &liftcoefs);
4961  SCIPfreeBufferArray(scip, &varsR);
4962  SCIPfreeBufferArray(scip, &varsF);
4963  SCIPfreeBufferArray(scip, &varsC2);
4964  SCIPfreeBufferArray(scip, &varsC1);
4965 
4966  return SCIP_OKAY;
4967 }
4968 
4969 /** separates lifted extended weight inequalities using sequential up- and down-lifting for given knapsack problem */
4970 static
4972  SCIP* scip, /**< SCIP data structure */
4973  SCIP_CONS* cons, /**< constraint that originates the knapsack problem, or NULL */
4974  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
4975  SCIP_VAR** vars, /**< variables in knapsack constraint */
4976  int nvars, /**< number of variables in knapsack constraint */
4977  int ntightened, /**< number of variables with tightened upper bound */
4978  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
4979  SCIP_Longint capacity, /**< capacity of knapsack */
4980  SCIP_Real* solvals, /**< solution values of all problem variables */
4981  int* feassetvars, /**< variables in feasible set */
4982  int* nonfeassetvars, /**< variables not in feasible set */
4983  int nfeassetvars, /**< number of variables in feasible set */
4984  int nnonfeassetvars, /**< number of variables not in feasible set */
4985  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
4986  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
4987  int* ncuts /**< pointer to add up the number of found cuts */
4988  )
4989 {
4990  int* varsT1;
4991  int* varsT2;
4992  int* varsF;
4993  int* varsR;
4994  int* liftcoefs;
4995  SCIP_Real cutact;
4996  int nvarsT1;
4997  int nvarsT2;
4998  int nvarsF;
4999  int nvarsR;
5000  int liftrhs;
5001  int j;
5002 
5003  assert( cutoff != NULL );
5004  *cutoff = FALSE;
5005 
5006  /* allocates temporary memory */
5007  SCIP_CALL( SCIPallocBufferArray(scip, &varsT1, nvars) );
5008  SCIP_CALL( SCIPallocBufferArray(scip, &varsT2, nvars) );
5009  SCIP_CALL( SCIPallocBufferArray(scip, &varsF, nvars) );
5010  SCIP_CALL( SCIPallocBufferArray(scip, &varsR, nvars) );
5011  SCIP_CALL( SCIPallocBufferArray(scip, &liftcoefs, nvars) );
5012 
5013  /* gets partition (T_1,T_2) of T, i.e. T_1 & T_2 = T and T_1 cap T_2 = emptyset, with T_1 not empty; chooses partition
5014  * as follows
5015  * T_2 = { j in T : x*_j = 1 } and
5016  * T_1 = T\T_2
5017  */
5018  getPartitionCovervars(scip, solvals, feassetvars, nfeassetvars, varsT1, varsT2, &nvarsT1, &nvarsT2);
5019  assert(nvarsT1 + nvarsT2 == nfeassetvars);
5020 
5021  /* changes partition (T_1,T_2) of feasible set T, if |T1| = 0, by moving one variable from T2 to T1 */
5022  if( nvarsT1 == 0 && nvarsT2 > 0)
5023  {
5024  SCIP_CALL( changePartitionFeasiblesetvars(scip, weights, varsT1, varsT2, &nvarsT1, &nvarsT2) );
5025  assert(nvarsT1 == 1);
5026  }
5027  assert(nvarsT2 == 0 || nvarsT1 > 0);
5028 
5029  /* gets partition (F,R) of N\T, i.e. F & R = N\T and F cap R = emptyset; chooses partition as follows
5030  * R = { j in N\T : x*_j = 0 } and
5031  * F = (N\T)\F
5032  */
5033  getPartitionNoncovervars(scip, solvals, nonfeassetvars, nnonfeassetvars, varsF, varsR, &nvarsF, &nvarsR);
5034  assert(nvarsF + nvarsR == nnonfeassetvars);
5035  assert(nvarsT1 + nvarsT2 + nvarsF + nvarsR == nvars - ntightened);
5036 
5037  /* sorts variables in F, T_2, and R according to the second level lifting sequence that will be used in the sequential
5038  * lifting procedure (the variable removed last from the initial cover does not have to be lifted first, therefore it
5039  * is included in the sorting routine)
5040  */
5041  SCIP_CALL( getLiftingSequence(scip, solvals, weights, varsF, varsT2, varsR, nvarsF, nvarsT2, nvarsR) );
5042 
5043  /* lifts extended weight inequality sum_{j in T_1} x_j <= |T_1| valid for
5044  *
5045  * S^0 = { x in {0,1}^|T_1| : sum_{j in T_1} a_j x_j <= a_0 - sum_{j in T_2} a_j }
5046  *
5047  * to a valid inequality sum_{j in T_1} x_j + sum_{j in N\T_1} alpha_j x_j <= |T_1| + sum_{j in T_2} alpha_j for
5048  *
5049  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 },
5050  *
5051  * uses sequential up-lifting for the variables in F, sequential down-lifting for the variable in T_2 and sequential
5052  * up-lifting for the variabels in R according to the second level lifting sequence
5053  */
5054  SCIP_CALL( sequentialUpAndDownLifting(scip, vars, nvars, ntightened, weights, capacity, solvals, varsT1, varsT2, varsF, varsR,
5055  nvarsT1, nvarsT2, nvarsF, nvarsR, nvarsT1, liftcoefs, &cutact, &liftrhs) );
5056 
5057  /* checks, if lifting yielded a violated cut */
5058  if( SCIPisEfficacious(scip, (cutact - liftrhs)/sqrt((SCIP_Real)MAX(liftrhs, 1))) )
5059  {
5060  SCIP_ROW* row;
5061  char name[SCIP_MAXSTRLEN];
5062 
5063  /* creates LP row */
5064  assert( cons == NULL || sepa == NULL );
5065  if( cons != NULL )
5066  {
5067  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_ewseq%" SCIP_LONGINT_FORMAT "", SCIPconsGetName(cons), SCIPconshdlrGetNCutsFound(SCIPconsGetHdlr(cons)));
5068  SCIP_CALL( SCIPcreateEmptyRowConshdlr(scip, &row, SCIPconsGetHdlr(cons), name, -SCIPinfinity(scip), (SCIP_Real)liftrhs,
5069  cons != NULL ? SCIPconsIsLocal(cons) : FALSE, FALSE,
5070  cons != NULL ? SCIPconsIsRemovable(cons) : TRUE) );
5071  }
5072  else if ( sepa != NULL )
5073  {
5074  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_ewseq_%" SCIP_LONGINT_FORMAT "", SCIPsepaGetName(sepa), SCIPsepaGetNCutsFound(sepa));
5075  SCIP_CALL( SCIPcreateEmptyRowSepa(scip, &row, sepa, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5076  }
5077  else
5078  {
5079  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nn_ewseq_%d", *ncuts);
5080  SCIP_CALL( SCIPcreateEmptyRowUnspec(scip, &row, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5081  }
5082 
5083  /* adds all variables in the knapsack constraint with calculated lifting coefficient to the cut */
5084  SCIP_CALL( SCIPcacheRowExtensions(scip, row) );
5085  assert(nvarsT1 + nvarsT2 + nvarsF + nvarsR == nvars - ntightened);
5086  for( j = 0; j < nvarsT1; j++ )
5087  {
5088  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsT1[j]], 1.0) );
5089  }
5090  for( j = 0; j < nvarsT2; j++ )
5091  {
5092  if( liftcoefs[varsT2[j]] > 0 )
5093  {
5094  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsT2[j]], (SCIP_Real)liftcoefs[varsT2[j]]) );
5095  }
5096  }
5097  for( j = 0; j < nvarsF; j++ )
5098  {
5099  if( liftcoefs[varsF[j]] > 0 )
5100  {
5101  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsF[j]], (SCIP_Real)liftcoefs[varsF[j]]) );
5102  }
5103  }
5104  for( j = 0; j < nvarsR; j++ )
5105  {
5106  if( liftcoefs[varsR[j]] > 0 )
5107  {
5108  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsR[j]], (SCIP_Real)liftcoefs[varsR[j]]) );
5109  }
5110  }
5111  SCIP_CALL( SCIPflushRowExtensions(scip, row) );
5112 
5113  /* checks, if cut is violated enough */
5114  if( SCIPisCutEfficacious(scip, sol, row) )
5115  {
5116  if( cons != NULL )
5117  {
5118  SCIP_CALL( SCIPresetConsAge(scip, cons) );
5119  }
5120  SCIP_CALL( SCIPaddRow(scip, row, FALSE, cutoff) );
5121  (*ncuts)++;
5122  }
5123  SCIP_CALL( SCIPreleaseRow(scip, &row) );
5124  }
5125 
5126  /* frees temporary memory */
5127  SCIPfreeBufferArray(scip, &liftcoefs);
5128  SCIPfreeBufferArray(scip, &varsR);
5129  SCIPfreeBufferArray(scip, &varsF);
5130  SCIPfreeBufferArray(scip, &varsT2);
5131  SCIPfreeBufferArray(scip, &varsT1);
5132 
5133  return SCIP_OKAY;
5134 }
5135 
5136 /** separates lifted minimal cover inequalities using superadditive up-lifting for given knapsack problem */
5137 static
5139  SCIP* scip, /**< SCIP data structure */
5140  SCIP_CONS* cons, /**< constraint that originates the knapsack problem, or NULL */
5141  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5142  SCIP_VAR** vars, /**< variables in knapsack constraint */
5143  int nvars, /**< number of variables in knapsack constraint */
5144  int ntightened, /**< number of variables with tightened upper bound */
5145  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5146  SCIP_Longint capacity, /**< capacity of knapsack */
5147  SCIP_Real* solvals, /**< solution values of all problem variables */
5148  int* mincovervars, /**< mincover variables */
5149  int* nonmincovervars, /**< nonmincover variables */
5150  int nmincovervars, /**< number of mincover variables */
5151  int nnonmincovervars, /**< number of nonmincover variables */
5152  SCIP_Longint mincoverweight, /**< weight of minimal cover */
5153  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
5154  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
5155  int* ncuts /**< pointer to add up the number of found cuts */
5156  )
5157 {
5158  SCIP_Real* realliftcoefs;
5159  SCIP_Real cutact;
5160  int liftrhs;
5161 
5162  assert( cutoff != NULL );
5163  *cutoff = FALSE;
5164  cutact = 0.0;
5165 
5166  /* allocates temporary memory */
5167  SCIP_CALL( SCIPallocBufferArray(scip, &realliftcoefs, nvars) );
5168 
5169  /* lifts minimal cover inequality sum_{j in C} x_j <= |C| - 1 valid for
5170  *
5171  * S^0 = { x in {0,1}^|C| : sum_{j in C} a_j x_j <= a_0 }
5172  *
5173  * to a valid inequality sum_{j in C} x_j + sum_{j in N\C} alpha_j x_j <= |C| - 1 for
5174  *
5175  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 },
5176  *
5177  * uses superadditive up-lifting for the variables in N\C.
5178  */
5179  SCIP_CALL( superadditiveUpLifting(scip, vars, nvars, ntightened, weights, capacity, solvals, mincovervars,
5180  nonmincovervars, nmincovervars, nnonmincovervars, mincoverweight, realliftcoefs, &cutact) );
5181  liftrhs = nmincovervars - 1;
5182 
5183  /* checks, if lifting yielded a violated cut */
5184  if( SCIPisEfficacious(scip, (cutact - liftrhs)/sqrt((SCIP_Real)MAX(liftrhs, 1))) )
5185  {
5186  SCIP_ROW* row;
5187  char name[SCIP_MAXSTRLEN];
5188  int j;
5189 
5190  /* creates LP row */
5191  assert( cons == NULL || sepa == NULL );
5192  if ( cons != NULL )
5193  {
5194  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcsup%" SCIP_LONGINT_FORMAT "", SCIPconsGetName(cons), SCIPconshdlrGetNCutsFound(SCIPconsGetHdlr(cons)));
5195  SCIP_CALL( SCIPcreateEmptyRowConshdlr(scip, &row, SCIPconsGetHdlr(cons), name, -SCIPinfinity(scip), (SCIP_Real)liftrhs,
5196  cons != NULL ? SCIPconsIsLocal(cons) : FALSE, FALSE,
5197  cons != NULL ? SCIPconsIsRemovable(cons) : TRUE) );
5198  }
5199  else if ( sepa != NULL )
5200  {
5201  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcsup%" SCIP_LONGINT_FORMAT "", SCIPsepaGetName(sepa), SCIPsepaGetNCutsFound(sepa));
5202  SCIP_CALL( SCIPcreateEmptyRowSepa(scip, &row, sepa, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5203  }
5204  else
5205  {
5206  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nn_mcsup_%d", *ncuts);
5207  SCIP_CALL( SCIPcreateEmptyRowUnspec(scip, &row, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5208  }
5209 
5210  /* adds all variables in the knapsack constraint with calculated lifting coefficient to the cut */
5211  SCIP_CALL( SCIPcacheRowExtensions(scip, row) );
5212  assert(nmincovervars + nnonmincovervars == nvars - ntightened);
5213  for( j = 0; j < nmincovervars; j++ )
5214  {
5215  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[mincovervars[j]], 1.0) );
5216  }
5217  for( j = 0; j < nnonmincovervars; j++ )
5218  {
5219  assert(SCIPisFeasGE(scip, realliftcoefs[nonmincovervars[j]], 0.0));
5220  if( SCIPisFeasGT(scip, realliftcoefs[nonmincovervars[j]], 0.0) )
5221  {
5222  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[nonmincovervars[j]], realliftcoefs[nonmincovervars[j]]) );
5223  }
5224  }
5225  SCIP_CALL( SCIPflushRowExtensions(scip, row) );
5226 
5227  /* checks, if cut is violated enough */
5228  if( SCIPisCutEfficacious(scip, sol, row) )
5229  {
5230  if( cons != NULL )
5231  {
5232  SCIP_CALL( SCIPresetConsAge(scip, cons) );
5233  }
5234  SCIP_CALL( SCIPaddRow(scip, row, FALSE, cutoff) );
5235  (*ncuts)++;
5236  }
5237  SCIP_CALL( SCIPreleaseRow(scip, &row) );
5238  }
5239 
5240  /* frees temporary memory */
5241  SCIPfreeBufferArray(scip, &realliftcoefs);
5242 
5243  return SCIP_OKAY;
5244 }
5245 
5246 /** converts given cover C to a minimal cover by removing variables in the reverse order in which the variables were chosen
5247  * to be in C, i.e. in the order of non-increasing (1 - x*_j)/a_j, if the transformed separation problem was used to find
5248  * C and in the order of non-increasing (1 - x*_j), if the modified transformed separation problem was used to find C;
5249  * note that all variables with x*_j = 1 will be removed last
5250  */
5251 static
5253  SCIP* scip, /**< SCIP data structure */
5254  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5255  SCIP_Longint capacity, /**< capacity of knapsack */
5256  SCIP_Real* solvals, /**< solution values of all problem variables */
5257  int* covervars, /**< pointer to store cover variables */
5258  int* noncovervars, /**< pointer to store noncover variables */
5259  int* ncovervars, /**< pointer to store number of cover variables */
5260  int* nnoncovervars, /**< pointer to store number of noncover variables */
5261  SCIP_Longint* coverweight, /**< pointer to store weight of cover */
5262  SCIP_Bool modtransused /**< TRUE if mod trans sepa prob was used to find cover */
5263  )
5264 {
5265  SORTKEYPAIR** sortkeypairs;
5266  SORTKEYPAIR** sortkeypairssorted;
5267  SCIP_Longint minweight;
5268  int nsortkeypairs;
5269  int minweightidx;
5270  int j;
5271  int k;
5272 
5273  assert(scip != NULL);
5274  assert(covervars != NULL);
5275  assert(noncovervars != NULL);
5276  assert(ncovervars != NULL);
5277  assert(*ncovervars > 0);
5278  assert(nnoncovervars != NULL);
5279  assert(*nnoncovervars >= 0);
5280  assert(coverweight != NULL);
5281  assert(*coverweight > 0);
5282  assert(*coverweight > capacity);
5283 
5284  /* allocates temporary memory; we need two arrays for the keypairs in order to be able to free them in the correct
5285  * order */
5286  nsortkeypairs = *ncovervars;
5287  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairs, nsortkeypairs) );
5288  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairssorted, nsortkeypairs) );
5289 
5290  /* sorts C in the reverse order in which the variables were chosen to be in the cover, i.e.
5291  * such that (1 - x*_1)/a_1 >= ... >= (1 - x*_|C|)/a_|C|, if trans separation problem was used to find C
5292  * such that (1 - x*_1) >= ... >= (1 - x*_|C|), if modified trans separation problem was used to find C
5293  * note that all variables with x*_j = 1 are in the end of the sorted C, so they will be removed last from C
5294  */
5295  assert(*ncovervars == nsortkeypairs);
5296  if( modtransused )
5297  {
5298  for( j = 0; j < *ncovervars; j++ )
5299  {
5300  SCIP_CALL( SCIPallocBuffer(scip, &(sortkeypairs[j])) ); /*lint !e866 */
5301  sortkeypairssorted[j] = sortkeypairs[j];
5302 
5303  sortkeypairs[j]->key1 = solvals[covervars[j]];
5304  sortkeypairs[j]->key2 = (SCIP_Real) weights[covervars[j]];
5305  }
5306  }
5307  else
5308  {
5309  for( j = 0; j < *ncovervars; j++ )
5310  {
5311  SCIP_CALL( SCIPallocBuffer(scip, &(sortkeypairs[j])) ); /*lint !e866 */
5312  sortkeypairssorted[j] = sortkeypairs[j];
5313 
5314  sortkeypairs[j]->key1 = (solvals[covervars[j]] - 1.0) / ((SCIP_Real) weights[covervars[j]]);
5315  sortkeypairs[j]->key2 = (SCIP_Real) (-weights[covervars[j]]);
5316  }
5317  }
5318  SCIPsortPtrInt((void**)sortkeypairssorted, covervars, compSortkeypairs, *ncovervars);
5319 
5320  /* gets j' with a_j' = min{ a_j : j in C } */
5321  minweightidx = 0;
5322  minweight = weights[covervars[minweightidx]];
5323  for( j = 1; j < *ncovervars; j++ )
5324  {
5325  if( weights[covervars[j]] <= minweight )
5326  {
5327  minweightidx = j;
5328  minweight = weights[covervars[minweightidx]];
5329  }
5330  }
5331  assert(minweightidx >= 0 && minweightidx < *ncovervars);
5332  assert(minweight > 0 && minweight <= *coverweight);
5333 
5334  j = 0;
5335  /* removes variables from C until the remaining variables form a minimal cover */
5336  while( j < *ncovervars && ((*coverweight) - minweight > capacity) )
5337  {
5338  assert(minweightidx >= j);
5339  assert(checkMinweightidx(weights, capacity, covervars, *ncovervars, *coverweight, minweightidx, j));
5340 
5341  /* if sum_{i in C} a_i - a_j <= a_0, j cannot be removed from C */
5342  if( (*coverweight) - weights[covervars[j]] <= capacity )
5343  {
5344  ++j;
5345  continue;
5346  }
5347 
5348  /* adds j to N\C */
5349  noncovervars[*nnoncovervars] = covervars[j];
5350  (*nnoncovervars)++;
5351 
5352  /* removes j from C */
5353  (*coverweight) -= weights[covervars[j]];
5354  for( k = j; k < (*ncovervars) - 1; k++ )
5355  covervars[k] = covervars[k+1];
5356  (*ncovervars)--;
5357 
5358  /* updates j' with a_j' = min{ a_j : j in C } */
5359  if( j == minweightidx )
5360  {
5361  minweightidx = 0;
5362  minweight = weights[covervars[minweightidx]];
5363  for( k = 1; k < *ncovervars; k++ )
5364  {
5365  if( weights[covervars[k]] <= minweight )
5366  {
5367  minweightidx = k;
5368  minweight = weights[covervars[minweightidx]];
5369  }
5370  }
5371  assert(minweight > 0 && minweight <= *coverweight);
5372  assert(minweightidx >= 0 && minweightidx < *ncovervars);
5373  }
5374  else
5375  {
5376  assert(minweightidx > j);
5377  minweightidx--;
5378  }
5379  /* j needs to stay the same */
5380  }
5381  assert((*coverweight) > capacity);
5382  assert((*coverweight) - minweight <= capacity);
5383 
5384  /* frees temporary memory */
5385  for( j = nsortkeypairs-1; j >= 0; j-- )
5386  SCIPfreeBuffer(scip, &(sortkeypairs[j])); /*lint !e866 */
5387  SCIPfreeBufferArray(scip, &sortkeypairssorted);
5388  SCIPfreeBufferArray(scip, &sortkeypairs);
5389 
5390  return SCIP_OKAY;
5391 }
5392 
5393 /** converts given initial cover C_init to a feasible set by removing variables in the reverse order in which
5394  * they were chosen to be in C_init:
5395  * non-increasing (1 - x*_j)/a_j, if transformed separation problem was used to find C_init
5396  * non-increasing (1 - x*_j), if modified transformed separation problem was used to find C_init.
5397  * separates lifted extended weight inequalities using sequential up- and down-lifting for this feasible set
5398  * and all subsequent feasible sets.
5399  */
5400 static
5402  SCIP* scip, /**< SCIP data structure */
5403  SCIP_CONS* cons, /**< constraint that originates the knapsack problem */
5404  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5405  SCIP_VAR** vars, /**< variables in knapsack constraint */
5406  int nvars, /**< number of variables in knapsack constraint */
5407  int ntightened, /**< number of variables with tightened upper bound */
5408  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5409  SCIP_Longint capacity, /**< capacity of knapsack */
5410  SCIP_Real* solvals, /**< solution values of all problem variables */
5411  int* covervars, /**< pointer to store cover variables */
5412  int* noncovervars, /**< pointer to store noncover variables */
5413  int* ncovervars, /**< pointer to store number of cover variables */
5414  int* nnoncovervars, /**< pointer to store number of noncover variables */
5415  SCIP_Longint* coverweight, /**< pointer to store weight of cover */
5416  SCIP_Bool modtransused, /**< TRUE if mod trans sepa prob was used to find cover */
5417  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
5418  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
5419  int* ncuts /**< pointer to add up the number of found cuts */
5420  )
5421 {
5422  SCIP_Real* sortkeys;
5423  int j;
5424  int k;
5425 
5426  assert(scip != NULL);
5427  assert(covervars != NULL);
5428  assert(noncovervars != NULL);
5429  assert(ncovervars != NULL);
5430  assert(*ncovervars > 0);
5431  assert(nnoncovervars != NULL);
5432  assert(*nnoncovervars >= 0);
5433  assert(coverweight != NULL);
5434  assert(*coverweight > 0);
5435  assert(*coverweight > capacity);
5436  assert(*ncovervars + *nnoncovervars == nvars - ntightened);
5437  assert(cutoff != NULL);
5438 
5439  *cutoff = FALSE;
5440 
5441  /* allocates temporary memory */
5442  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, *ncovervars) );
5443 
5444  /* sorts C in the reverse order in which the variables were chosen to be in the cover, i.e.
5445  * such that (1 - x*_1)/a_1 >= ... >= (1 - x*_|C|)/a_|C|, if trans separation problem was used to find C
5446  * such that (1 - x*_1) >= ... >= (1 - x*_|C|), if modified trans separation problem was used to find C
5447  * note that all variables with x*_j = 1 are in the end of the sorted C, so they will be removed last from C
5448  */
5449  if( modtransused )
5450  {
5451  for( j = 0; j < *ncovervars; j++ )
5452  {
5453  sortkeys[j] = solvals[covervars[j]];
5454  assert(SCIPisFeasGE(scip, sortkeys[j], 0.0));
5455  }
5456  }
5457  else
5458  {
5459  for( j = 0; j < *ncovervars; j++ )
5460  {
5461  sortkeys[j] = (solvals[covervars[j]] - 1.0) / ((SCIP_Real) weights[covervars[j]]);
5462  assert(SCIPisFeasLE(scip, sortkeys[j], 0.0));
5463  }
5464  }
5465  SCIPsortRealInt(sortkeys, covervars, *ncovervars);
5466 
5467  /* removes variables from C_init and separates lifted extended weight inequalities using sequential up- and down-lifting;
5468  * in addition to an extended weight inequality this gives cardinality inequalities */
5469  while( *ncovervars >= 2 )
5470  {
5471  /* adds first element of C_init to N\C_init */
5472  noncovervars[*nnoncovervars] = covervars[0];
5473  (*nnoncovervars)++;
5474 
5475  /* removes first element from C_init */
5476  (*coverweight) -= weights[covervars[0]];
5477  for( k = 0; k < (*ncovervars) - 1; k++ )
5478  covervars[k] = covervars[k+1];
5479  (*ncovervars)--;
5480 
5481  assert(*ncovervars + *nnoncovervars == nvars - ntightened);
5482  if( (*coverweight) <= capacity )
5483  {
5484  SCIP_CALL( separateSequLiftedExtendedWeightInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity, solvals,
5485  covervars, noncovervars, *ncovervars, *nnoncovervars, sol, cutoff, ncuts) );
5486  }
5487 
5488  /* stop if cover is too large */
5489  if ( *ncovervars >= MAXCOVERSIZEITERLEWI )
5490  break;
5491  }
5492 
5493  /* frees temporary memory */
5494  SCIPfreeBufferArray(scip, &sortkeys);
5495 
5496  return SCIP_OKAY;
5497 }
5498 
5499 /** separates different classes of valid inequalities for the 0-1 knapsack problem */
5501  SCIP* scip, /**< SCIP data structure */
5502  SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
5503  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5504  SCIP_VAR** vars, /**< variables in knapsack constraint */
5505  int nvars, /**< number of variables in knapsack constraint */
5506  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5507  SCIP_Longint capacity, /**< capacity of knapsack */
5508  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
5509  SCIP_Bool usegubs, /**< should GUB information be used for separation? */
5510  SCIP_Bool* cutoff, /**< pointer to store whether a cutoff has been detected */
5511  int* ncuts /**< pointer to add up the number of found cuts */
5512  )
5513 {
5514  SCIP_Real* solvals;
5515  int* covervars;
5516  int* noncovervars;
5517  SCIP_Bool coverfound;
5518  SCIP_Bool fractional;
5519  SCIP_Bool modtransused;
5520  SCIP_Longint coverweight;
5521  int ncovervars;
5522  int nnoncovervars;
5523  int ntightened;
5524 
5525  assert(scip != NULL);
5526  assert(capacity >= 0);
5527  assert(cutoff != NULL);
5528  assert(ncuts != NULL);
5529 
5530  *cutoff = FALSE;
5531 
5532  if( nvars == 0 )
5533  return SCIP_OKAY;
5534 
5535  assert(vars != NULL);
5536  assert(nvars > 0);
5537  assert(weights != NULL);
5538 
5539  /* increase age of constraint (age is reset to zero, if a cut was found) */
5540  if( cons != NULL )
5541  {
5542  SCIP_CALL( SCIPincConsAge(scip, cons) );
5543  }
5544 
5545  /* allocates temporary memory */
5546  SCIP_CALL( SCIPallocBufferArray(scip, &solvals, nvars) );
5547  SCIP_CALL( SCIPallocBufferArray(scip, &covervars, nvars) );
5548  SCIP_CALL( SCIPallocBufferArray(scip, &noncovervars, nvars) );
5549 
5550  /* gets solution values of all problem variables */
5551  SCIP_CALL( SCIPgetSolVals(scip, sol, nvars, vars, solvals) );
5552 
5553 #ifdef SCIP_DEBUG
5554  {
5555  int i;
5556 
5557  SCIPdebugMsg(scip, "separate cuts for knapsack constraint originated by cons <%s>:\n",
5558  cons == NULL ? "-" : SCIPconsGetName(cons));
5559  for( i = 0; i < nvars; ++i )
5560  {
5561  SCIPdebugMsgPrint(scip, "%+" SCIP_LONGINT_FORMAT "<%s>(%g)", weights[i], SCIPvarGetName(vars[i]), solvals[i]);
5562  }
5563  SCIPdebugMsgPrint(scip, " <= %" SCIP_LONGINT_FORMAT "\n", capacity);
5564  }
5565 #endif
5566 
5567  /* LMCI1 (lifted minimal cover inequalities using sequential up- and down-lifting) using GUB information
5568  */
5569  if( usegubs )
5570  {
5571  SCIP_GUBSET* gubset;
5572 
5573  SCIPdebugMsg(scip, "separate LMCI1-GUB cuts:\n");
5574 
5575  /* initializes partion of knapsack variables into nonoverlapping GUB constraints */
5576  SCIP_CALL( GUBsetCreate(scip, &gubset, nvars, weights, capacity) );
5577 
5578  /* constructs sophisticated partition of knapsack variables into nonoverlapping GUBs */
5579  SCIP_CALL( GUBsetGetCliquePartition(scip, gubset, vars, solvals) );
5580  assert(gubset->ngubconss <= nvars);
5581 
5582  /* gets a most violated initial cover C_init ( sum_{j in C_init} a_j > a_0 ) by using the
5583  * MODIFIED transformed separation problem and taking into account the following fixing:
5584  * j in C_init, if j in N_1 = {j in N : x*_j = 1} and
5585  * j in N\C_init, if j in N_0 = {j in N : x*_j = 0},
5586  * if one exists
5587  */
5588  modtransused = TRUE;
5589  SCIP_CALL( getCover(scip, vars, nvars, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5590  &nnoncovervars, &coverweight, &coverfound, modtransused, &ntightened, &fractional) );
5591 
5592  assert(!coverfound || !fractional || ncovervars + nnoncovervars == nvars - ntightened);
5593 
5594  /* if x* is not fractional we stop the separation routine */
5595  if( !fractional )
5596  {
5597  SCIPdebugMsg(scip, " LMCI1-GUB terminated by no variable with fractional LP value.\n");
5598 
5599  /* frees memory for GUB set data structure */
5600  GUBsetFree(scip, &gubset);
5601 
5602  goto TERMINATE;
5603  }
5604 
5605  /* if no cover was found we stop the separation routine for lifted minimal cover inequality */
5606  if( coverfound )
5607  {
5608  /* converts initial cover C_init to a minimal cover C by removing variables in the reverse order in which the
5609  * variables were chosen to be in C_init; note that variables with x*_j = 1 will be removed last
5610  */
5611  SCIP_CALL( makeCoverMinimal(scip, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5612  &nnoncovervars, &coverweight, modtransused) );
5613 
5614  /* only separate with GUB information if we have at least one nontrivial GUB (with more than one variable) */
5615  if( gubset->ngubconss < nvars )
5616  {
5617  /* separates lifted minimal cover inequalities using sequential up- and down-lifting and GUB information */
5618  SCIP_CALL( separateSequLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5619  solvals, covervars, noncovervars, ncovervars, nnoncovervars, sol, gubset, cutoff, ncuts) );
5620  }
5621  else
5622  {
5623  /* separates lifted minimal cover inequalities using sequential up- and down-lifting, but do not use trivial
5624  * GUB information
5625  */
5626  SCIP_CALL( separateSequLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5627  solvals, covervars, noncovervars, ncovervars, nnoncovervars, sol, NULL, cutoff, ncuts) );
5628  }
5629  }
5630 
5631  /* frees memory for GUB set data structure */
5632  GUBsetFree(scip, &gubset);
5633  }
5634  else
5635  {
5636  /* LMCI1 (lifted minimal cover inequalities using sequential up- and down-lifting)
5637  * (and LMCI2 (lifted minimal cover inequalities using superadditive up-lifting))
5638  */
5639 
5640  /* gets a most violated initial cover C_init ( sum_{j in C_init} a_j > a_0 ) by using the
5641  * MODIFIED transformed separation problem and taking into account the following fixing:
5642  * j in C_init, if j in N_1 = {j in N : x*_j = 1} and
5643  * j in N\C_init, if j in N_0 = {j in N : x*_j = 0},
5644  * if one exists
5645  */
5646  SCIPdebugMsg(scip, "separate LMCI1 cuts:\n");
5647  modtransused = TRUE;
5648  SCIP_CALL( getCover(scip, vars, nvars, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5649  &nnoncovervars, &coverweight, &coverfound, modtransused, &ntightened, &fractional) );
5650  assert(!coverfound || !fractional || ncovervars + nnoncovervars == nvars - ntightened);
5651 
5652  /* if x* is not fractional we stop the separation routine */
5653  if( !fractional )
5654  goto TERMINATE;
5655 
5656  /* if no cover was found we stop the separation routine for lifted minimal cover inequality */
5657  if( coverfound )
5658  {
5659  /* converts initial cover C_init to a minimal cover C by removing variables in the reverse order in which the
5660  * variables were chosen to be in C_init; note that variables with x*_j = 1 will be removed last
5661  */
5662  SCIP_CALL( makeCoverMinimal(scip, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5663  &nnoncovervars, &coverweight, modtransused) );
5664 
5665  /* separates lifted minimal cover inequalities using sequential up- and down-lifting */
5666  SCIP_CALL( separateSequLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5667  solvals, covervars, noncovervars, ncovervars, nnoncovervars, sol, NULL, cutoff, ncuts) );
5668 
5669  if( USESUPADDLIFT ) /*lint !e506 !e774*/
5670  {
5671  SCIPdebugMsg(scip, "separate LMCI2 cuts:\n");
5672  /* separates lifted minimal cover inequalities using superadditive up-lifting */
5673  SCIP_CALL( separateSupLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5674  solvals, covervars, noncovervars, ncovervars, nnoncovervars, coverweight, sol, cutoff, ncuts) );
5675  }
5676  }
5677  }
5678 
5679  /* LEWI (lifted extended weight inequalities using sequential up- and down-lifting) */
5680  if ( ! (*cutoff) )
5681  {
5682  /* gets a most violated initial cover C_init ( sum_{j in C_init} a_j > a_0 ) by using the
5683  * transformed separation problem and taking into account the following fixing:
5684  * j in C_init, if j in N_1 = {j in N : x*_j = 1} and
5685  * j in N\C_init, if j in N_0 = {j in N : x*_j = 0},
5686  * if one exists
5687  */
5688  SCIPdebugMsg(scip, "separate LEWI cuts:\n");
5689  modtransused = FALSE;
5690  SCIP_CALL( getCover(scip, vars, nvars, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5691  &nnoncovervars, &coverweight, &coverfound, modtransused, &ntightened, &fractional) );
5692  assert(fractional);
5693  assert(!coverfound || ncovervars + nnoncovervars == nvars - ntightened);
5694 
5695  /* if no cover was found we stop the separation routine */
5696  if( coverfound )
5697  {
5698  /* converts initial cover C_init to a feasible set by removing variables in the reverse order in which
5699  * they were chosen to be in C_init and separates lifted extended weight inequalities using sequential
5700  * up- and down-lifting for this feasible set and all subsequent feasible sets.
5701  */
5702  SCIP_CALL( getFeasibleSet(scip, cons, sepa, vars, nvars, ntightened, weights, capacity, solvals, covervars, noncovervars,
5703  &ncovervars, &nnoncovervars, &coverweight, modtransused, sol, cutoff, ncuts) );
5704  }
5705  }
5706 
5707  TERMINATE:
5708  /* frees temporary memory */
5709  SCIPfreeBufferArray(scip, &noncovervars);
5710  SCIPfreeBufferArray(scip, &covervars);
5711  SCIPfreeBufferArray(scip, &solvals);
5712 
5713  return SCIP_OKAY;
5714 }
5715 
5716 /* relaxes given general linear constraint into a knapsack constraint and separates lifted knapsack cover inequalities */
5718  SCIP* scip, /**< SCIP data structure */
5719  SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
5720  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5721  int nknapvars, /**< number of variables in the continuous knapsack constraint */
5722  SCIP_VAR** knapvars, /**< variables in the continuous knapsack constraint */
5723  SCIP_Real* knapvals, /**< coefficients of the variables in the continuous knapsack constraint */
5724  SCIP_Real valscale, /**< -1.0 if lhs of row is used as rhs of c. k. constraint, +1.0 otherwise */
5725  SCIP_Real rhs, /**< right hand side of the continuous knapsack constraint */
5726  SCIP_SOL* sol, /**< primal CIP solution, NULL for current LP solution */
5727  SCIP_Bool* cutoff, /**< pointer to store whether a cutoff was found */
5728  int* ncuts /**< pointer to add up the number of found cuts */
5729  )
5730 {
5731  SCIP_VAR** binvars;
5732  SCIP_VAR** consvars;
5733  SCIP_Real* binvals;
5734  SCIP_Longint* consvals;
5735  SCIP_Longint minact;
5736  SCIP_Longint maxact;
5737  SCIP_Real intscalar;
5738  SCIP_Bool success;
5739  int nbinvars;
5740  int nconsvars;
5741  int i;
5742 
5743  int* tmpindices;
5744  int tmp;
5745  SCIP_CONSHDLR* conshdlr;
5746  SCIP_CONSHDLRDATA* conshdlrdata;
5747  SCIP_Bool noknapsackconshdlr;
5748  SCIP_Bool usegubs;
5749 
5750  assert(nknapvars > 0);
5751  assert(knapvars != NULL);
5752  assert(cutoff != NULL);
5753 
5754  tmpindices = NULL;
5755 
5756  SCIPdebugMsg(scip, "separate linear constraint <%s> relaxed to knapsack\n", cons != NULL ? SCIPconsGetName(cons) : "-");
5757  SCIPdebug( if( cons != NULL ) { SCIPdebugPrintCons(scip, cons, NULL); } );
5758 
5759  binvars = SCIPgetVars(scip);
5760 
5761  /* all variables which are of integral type can be potentially of binary type; this can be checked via the method SCIPvarIsBinary(var) */
5762  nbinvars = SCIPgetNVars(scip) - SCIPgetNContVars(scip);
5763 
5764  *cutoff = FALSE;
5765 
5766  if( nbinvars == 0 )
5767  return SCIP_OKAY;
5768 
5769  /* set up data structures */
5770  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nbinvars) );
5771  SCIP_CALL( SCIPallocBufferArray(scip, &consvals, nbinvars) );
5772 
5773  /* get conshdlrdata to use cleared memory */
5774  conshdlr = SCIPfindConshdlr(scip, CONSHDLR_NAME);
5775  if( conshdlr == NULL )
5776  {
5777  noknapsackconshdlr = TRUE;
5778  usegubs = DEFAULT_USEGUBS;
5779 
5780  SCIP_CALL( SCIPallocBufferArray(scip, &binvals, nbinvars) );
5781  BMSclearMemoryArray(binvals, nbinvars);
5782  }
5783  else
5784  {
5785  noknapsackconshdlr = FALSE;
5786  conshdlrdata = SCIPconshdlrGetData(conshdlr);
5787  assert(conshdlrdata != NULL);
5788  usegubs = conshdlrdata->usegubs;
5789 
5790  SCIP_CALL( SCIPallocBufferArray(scip, &tmpindices, nknapvars) );
5791 
5792  /* increase array size to avoid an endless loop in the next block; this might happen if continuous variables
5793  * change their types to SCIP_VARTYPE_BINARY during presolving
5794  */
5795  if( conshdlrdata->reals1size == 0 )
5796  {
5797  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &conshdlrdata->reals1, conshdlrdata->reals1size, 1) );
5798  conshdlrdata->reals1size = 1;
5799  conshdlrdata->reals1[0] = 0.0;
5800  }
5801 
5802  assert(conshdlrdata->reals1size > 0);
5803 
5804  /* next if condition should normally not be true, because it means that presolving has created more binary
5805  * variables than binary + integer variables existed at the constraint initialization method, but for example if you would
5806  * transform all integers into their binary representation then it maybe happens
5807  */
5808  if( conshdlrdata->reals1size < nbinvars )
5809  {
5810  int oldsize = conshdlrdata->reals1size;
5811 
5812  conshdlrdata->reals1size = nbinvars;
5813  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &conshdlrdata->reals1, oldsize, conshdlrdata->reals1size) );
5814  BMSclearMemoryArray(&(conshdlrdata->reals1[oldsize]), conshdlrdata->reals1size - oldsize); /*lint !e866 */
5815  }
5816  binvals = conshdlrdata->reals1;
5817 
5818  /* check for cleared array, all entries have to be zero */
5819 #ifndef NDEBUG
5820  for( tmp = nbinvars - 1; tmp >= 0; --tmp )
5821  {
5822  assert(binvals[tmp] == 0);
5823  }
5824 #endif
5825  }
5826 
5827  tmp = 0;
5828 
5829  /* relax continuous knapsack constraint:
5830  * 1. make all variables binary:
5831  * if x_j is continuous or integer variable substitute:
5832  * - a_j < 0: x_j = lb or x_j = b*z + d with variable lower bound b*z + d with binary variable z
5833  * - a_j > 0: x_j = ub or x_j = b*z + d with variable upper bound b*z + d with binary variable z
5834  * 2. convert coefficients of all variables to positive integers:
5835  * - scale all coefficients a_j to a~_j integral
5836  * - substitute x~_j = 1 - x_j if a~_j < 0
5837  */
5838 
5839  /* replace integer and continuous variables with binary variables */
5840  for( i = 0; i < nknapvars; i++ )
5841  {
5842  SCIP_VAR* var;
5843 
5844  var = knapvars[i];
5845 
5846  if( SCIPvarIsBinary(var) && SCIPvarIsActive(var) )
5847  {
5848  SCIP_Real solval;
5849  assert(0 <= SCIPvarGetProbindex(var) && SCIPvarGetProbindex(var) < nbinvars);
5850 
5851  solval = SCIPgetSolVal(scip, sol, var);
5852 
5853  /* knapsack relaxation assumes solution values between 0.0 and 1.0 for binary variables */
5854  if( SCIPisFeasLT(scip, solval, 0.0 )
5855  || SCIPisFeasGT(scip, solval, 1.0) )
5856  {
5857  SCIPdebugMsg(scip, "Solution value %.15g <%s> outside domain [0.0, 1.0]\n",
5858  solval, SCIPvarGetName(var));
5859  goto TERMINATE;
5860  }
5861 
5862  binvals[SCIPvarGetProbindex(var)] += valscale * knapvals[i];
5863  if( !noknapsackconshdlr )
5864  {
5865  assert(tmpindices != NULL);
5866 
5867  tmpindices[tmp] = SCIPvarGetProbindex(var);
5868  ++tmp;
5869  }
5870  SCIPdebugMsg(scip, " -> binary variable %+.15g<%s>(%.15g)\n", valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var));
5871  }
5872  else if( valscale * knapvals[i] > 0.0 )
5873  {
5874  SCIP_VAR** zvlb;
5875  SCIP_Real* bvlb;
5876  SCIP_Real* dvlb;
5877  SCIP_Real bestlbsol;
5878  int bestlbtype;
5879  int nvlb;
5880  int j;
5881 
5882  /* a_j > 0: substitution with lb or vlb */
5883  nvlb = SCIPvarGetNVlbs(var);
5884  zvlb = SCIPvarGetVlbVars(var);
5885  bvlb = SCIPvarGetVlbCoefs(var);
5886  dvlb = SCIPvarGetVlbConstants(var);
5887 
5888  /* search for lb or vlb with maximal bound value */
5889  bestlbsol = SCIPvarGetLbGlobal(var);
5890  bestlbtype = -1;
5891  for( j = 0; j < nvlb; j++ )
5892  {
5893  /* use only numerical stable vlb with binary variable z */
5894  if( SCIPvarIsBinary(zvlb[j]) && SCIPvarIsActive(zvlb[j]) && REALABS(bvlb[j]) <= MAXABSVBCOEF )
5895  {
5896  SCIP_Real vlbsol;
5897 
5898  if( (bvlb[j] >= 0.0 && SCIPisGT(scip, bvlb[j] * SCIPvarGetLbLocal(zvlb[j]) + dvlb[j], SCIPvarGetUbLocal(var))) ||
5899  (bvlb[j] <= 0.0 && SCIPisGT(scip, bvlb[j] * SCIPvarGetUbLocal(zvlb[j]) + dvlb[j], SCIPvarGetUbLocal(var))) )
5900  {
5901  *cutoff = TRUE;
5902  SCIPdebugMsg(scip, "variable bound <%s>[%g,%g] >= %g<%s>[%g,%g] + %g implies local cutoff\n",
5904  bvlb[j], SCIPvarGetName(zvlb[j]), SCIPvarGetLbLocal(zvlb[j]), SCIPvarGetUbLocal(zvlb[j]), dvlb[j]);
5905  goto TERMINATE;
5906  }
5907 
5908  assert(0 <= SCIPvarGetProbindex(zvlb[j]) && SCIPvarGetProbindex(zvlb[j]) < nbinvars);
5909  vlbsol = bvlb[j] * SCIPgetSolVal(scip, sol, zvlb[j]) + dvlb[j];
5910  if( SCIPisGE(scip, vlbsol, bestlbsol) )
5911  {
5912  bestlbsol = vlbsol;
5913  bestlbtype = j;
5914  }
5915  }
5916  }
5917 
5918  /* if no lb or vlb with binary variable was found, we have to abort */
5919  if( SCIPisInfinity(scip, -bestlbsol) )
5920  goto TERMINATE;
5921 
5922  if( bestlbtype == -1 )
5923  {
5924  rhs -= valscale * knapvals[i] * bestlbsol;
5925  SCIPdebugMsg(scip, " -> non-binary variable %+.15g<%s>(%.15g) replaced with lower bound %.15g (rhs=%.15g)\n",
5926  valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var), SCIPvarGetLbGlobal(var), rhs);
5927  }
5928  else
5929  {
5930  assert(0 <= SCIPvarGetProbindex(zvlb[bestlbtype]) && SCIPvarGetProbindex(zvlb[bestlbtype]) < nbinvars);
5931  rhs -= valscale * knapvals[i] * dvlb[bestlbtype];
5932  binvals[SCIPvarGetProbindex(zvlb[bestlbtype])] += valscale * knapvals[i] * bvlb[bestlbtype];
5933 
5934  if( SCIPisInfinity(scip, REALABS(binvals[SCIPvarGetProbindex(zvlb[bestlbtype])])) )
5935  goto TERMINATE;
5936 
5937  if( !noknapsackconshdlr )
5938  {
5939  assert(tmpindices != NULL);
5940 
5941  tmpindices[tmp] = SCIPvarGetProbindex(zvlb[bestlbtype]);
5942  ++tmp;
5943  }
5944  SCIPdebugMsg(scip, " -> non-binary variable %+.15g<%s>(%.15g) replaced with variable lower bound %+.15g<%s>(%.15g) %+.15g (rhs=%.15g)\n",
5945  valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var),
5946  bvlb[bestlbtype], SCIPvarGetName(zvlb[bestlbtype]),
5947  SCIPgetSolVal(scip, sol, zvlb[bestlbtype]), dvlb[bestlbtype], rhs);
5948  }
5949  }
5950  else
5951  {
5952  SCIP_VAR** zvub;
5953  SCIP_Real* bvub;
5954  SCIP_Real* dvub;
5955  SCIP_Real bestubsol;
5956  int bestubtype;
5957  int nvub;
5958  int j;
5959 
5960  assert(valscale * knapvals[i] < 0.0);
5961 
5962  /* a_j < 0: substitution with ub or vub */
5963  nvub = SCIPvarGetNVubs(var);
5964  zvub = SCIPvarGetVubVars(var);
5965  bvub = SCIPvarGetVubCoefs(var);
5966  dvub = SCIPvarGetVubConstants(var);
5967 
5968  /* search for ub or vub with minimal bound value */
5969  bestubsol = SCIPvarGetUbGlobal(var);
5970  bestubtype = -1;
5971  for( j = 0; j < nvub; j++ )
5972  {
5973  /* use only numerical stable vub with active binary variable z */
5974  if( SCIPvarIsBinary(zvub[j]) && SCIPvarIsActive(zvub[j]) && REALABS(bvub[j]) <= MAXABSVBCOEF )
5975  {
5976  SCIP_Real vubsol;
5977 
5978  if( (bvub[j] >= 0.0 && SCIPisLT(scip, bvub[j] * SCIPvarGetUbLocal(zvub[j]) + dvub[j], SCIPvarGetLbLocal(var))) ||
5979  (bvub[j] <= 0.0 && SCIPisLT(scip, bvub[j] * SCIPvarGetLbLocal(zvub[j]) + dvub[j], SCIPvarGetLbLocal(var))) )
5980  {
5981  *cutoff = TRUE;
5982  SCIPdebugMsg(scip, "variable bound <%s>[%g,%g] <= %g<%s>[%g,%g] + %g implies local cutoff\n",
5984  bvub[j], SCIPvarGetName(zvub[j]), SCIPvarGetLbLocal(zvub[j]), SCIPvarGetUbLocal(zvub[j]), dvub[j]);
5985  goto TERMINATE;
5986  }
5987 
5988  assert(0 <= SCIPvarGetProbindex(zvub[j]) && SCIPvarGetProbindex(zvub[j]) < nbinvars);
5989  vubsol = bvub[j] * SCIPgetSolVal(scip, sol, zvub[j]) + dvub[j];
5990  if( SCIPisLE(scip, vubsol, bestubsol) )
5991  {
5992  bestubsol = vubsol;
5993  bestubtype = j;
5994  }
5995  }
5996  }
5997 
5998  /* if no ub or vub with binary variable was found, we have to abort */
5999  if( SCIPisInfinity(scip, bestubsol) )
6000  goto TERMINATE;
6001 
6002  if( bestubtype == -1 )
6003  {
6004  rhs -= valscale * knapvals[i] * bestubsol;
6005  SCIPdebugMsg(scip, " -> non-binary variable %+.15g<%s>(%.15g) replaced with upper bound %.15g (rhs=%.15g)\n",
6006  valscale * knapvals[i], SCIPvarGetName(var),