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-2020 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file 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];
1126  }
1127  /* item is not profitable */
1128  else if( profits[j] <= 0.0 )
1129  {
1130  if( solitems != NULL )
1131  nonsolitems[(*nnonsolitems)++] = items[j];
1132  }
1133  /* item always fits */
1134  else if( weights[j] == 0 )
1135  {
1136  if( solitems != NULL )
1137  solitems[(*nsolitems)++] = items[j];
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];
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  solitems[(*nsolitems)++] = myitems[p];
1242  for( j = nmyitems - 1; j >= 0; --j )
1243  {
1244  if( j != p )
1245  nonsolitems[(*nnonsolitems)++] = myitems[j];
1246  }
1247  }
1248  /* update solution value */
1249  if( solval != NULL )
1250  *solval += myprofits[p];
1251 
1252  goto TERMINATE;
1253  }
1254 
1255  /* if all items have the same weight, then take the best */
1256  if( eqweights )
1257  {
1258  SCIP_Real addval = 0.0;
1259 
1260  SCIPdebugMsg(scip, "All weights are equal, so take the best.\n");
1261 
1262  SCIPsortDownRealIntLong(myprofits, myitems, myweights, nmyitems);
1263 
1264  /* update solution information */
1265  if( solitems != NULL || solval != NULL )
1266  {
1267  SCIP_Longint i;
1268 
1269  /* if all items would fit we had handled this case before */
1270  assert((SCIP_Longint) nmyitems > capacity);
1271 
1272  /* take the first best items into the solution */
1273  for( i = capacity - 1; i >= 0; --i )
1274  {
1275  if( solitems != NULL )
1276  solitems[(*nsolitems)++] = myitems[i];
1277  addval += myprofits[i];
1278  }
1279 
1280  if( solitems != NULL )
1281  {
1282  /* the rest are not in the solution */
1283  for( i = nmyitems - 1; i >= capacity; --i )
1284  nonsolitems[(*nnonsolitems)++] = myitems[i];
1285  }
1286  }
1287  /* update solution value */
1288  if( solval != NULL )
1289  {
1290  assert(addval > 0.0);
1291  *solval += addval;
1292  }
1293 
1294  goto TERMINATE;
1295  }
1296 
1297  SCIPdebugMsg(scip, "Determine greedy solution.\n");
1298 
1299  /* sort myitems (plus corresponding arrays myweights and myprofits) such that
1300  * p_1/w_1 >= p_2/w_2 >= ... >= p_n/w_n, this is only used for the greedy solution
1301  */
1302  SCIP_CALL( SCIPallocBufferArray(scip, &tempsort, nmyitems) );
1303  SCIP_CALL( SCIPallocBufferArray(scip, &realweights, nmyitems) );
1304 
1305  for( j = 0; j < nmyitems; ++j )
1306  {
1307  tempsort[j] = myprofits[j]/((SCIP_Real) myweights[j]);
1308  realweights[j] = (SCIP_Real)myweights[j];
1309  }
1310 
1311  SCIPselectWeightedDownRealLongRealInt(tempsort, myweights, myprofits, myitems, realweights,
1312  (SCIP_Real)capacity, nmyitems, &greedymedianpos);
1313 
1314  SCIPfreeBufferArray(scip, &realweights);
1315  SCIPfreeBufferArray(scip, &tempsort);
1316 
1317  /* initialize values for greedy solution information */
1318  greedysolweight = 0;
1319  greedysolvalue = 0.0;
1320 
1321  /* determine greedy solution */
1322  for( j = 0; j < greedymedianpos; ++j )
1323  {
1324  assert(myweights[j] <= capacity);
1325 
1326  /* update greedy solution weight and value */
1327  greedysolweight += myweights[j];
1328  greedysolvalue += myprofits[j];
1329  }
1330 
1331  assert(0 < greedysolweight && greedysolweight <= capacity);
1332  assert(greedysolvalue > 0.0);
1333 
1334  /* If the greedy solution is optimal by comparing to the LP solution, we take this solution. This happens if:
1335  * - the greedy solution reaches the capacity, because then the LP solution is integral;
1336  * - the greedy solution has an objective that is at least the LP value rounded down in case that all profits are integer, too. */
1337  greedyupperbound = greedysolvalue + myprofits[j] * (SCIP_Real) (capacity - greedysolweight)/((SCIP_Real) myweights[j]);
1338  if( intprofits )
1339  greedyupperbound = SCIPfloor(scip, greedyupperbound);
1340  if( greedysolweight == capacity || SCIPisGE(scip, greedysolvalue, greedyupperbound) )
1341  {
1342  SCIPdebugMsg(scip, "Greedy solution is optimal.\n");
1343 
1344  /* update solution information */
1345  if( solitems != NULL )
1346  {
1347  int l;
1348 
1349  /* collect items */
1350  for( l = 0; l < j; ++l )
1351  solitems[(*nsolitems)++] = myitems[l];
1352  for ( ; l < nmyitems; ++l )
1353  nonsolitems[(*nnonsolitems)++] = myitems[l];
1354  }
1355  /* update solution value */
1356  if( solval != NULL )
1357  {
1358  assert(greedysolvalue > 0.0);
1359  *solval += greedysolvalue;
1360  }
1361 
1362  goto TERMINATE;
1363  }
1364 
1365  /* in the following table we do not need the first minweight columns */
1366  capacity -= (minweight - 1);
1367 
1368  /* we can only handle integers */
1369  if( capacity >= INT_MAX )
1370  {
1371  SCIPdebugMsg(scip, "Capacity is to big, so we cannot handle it here.\n");
1372 
1373  *success = FALSE;
1374  goto TERMINATE;
1375  }
1376  assert(capacity < INT_MAX);
1377 
1378  intcap = (int)capacity;
1379  assert(intcap >= 0);
1380  assert(nmyitems > 0);
1381  assert(sizeof(size_t) >= sizeof(int)); /*lint !e506*/ /* no following conversion should be messed up */
1382 
1383  /* this condition checks whether we will try to allocate a correct number of bytes and do not have an overflow, while
1384  * computing the size for the allocation
1385  */
1386  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*/
1387  {
1388  SCIPdebugMsg(scip, "Too much memory (%lu) would be consumed.\n", (unsigned long) (((size_t)nmyitems) * ((size_t)intcap) * sizeof(*optvalues))); /*lint !e571*/
1389 
1390  *success = FALSE;
1391  goto TERMINATE;
1392  }
1393 
1394  /* allocate temporary memory and check for memory exceedance */
1395  retcode = SCIPallocBufferArray(scip, &optvalues, nmyitems * intcap);
1396  if( retcode == SCIP_NOMEMORY )
1397  {
1398  SCIPdebugMsg(scip, "Did not get enough memory.\n");
1399 
1400  *success = FALSE;
1401  goto TERMINATE;
1402  }
1403  else
1404  {
1405  SCIP_CALL( retcode );
1406  }
1407 
1408  SCIPdebugMsg(scip, "Start real exact algorithm.\n");
1409 
1410  /* we memorize at each step the current minimal weight to later on know which value in our optvalues matrix is valid;
1411  * each value entries of the j-th row of optvalues is valid if the index is >= allcurrminweight[j], otherwise it is
1412  * invalid; a second possibility would be to clear the whole optvalues, which should be more expensive than storing
1413  * 'nmyitem' values
1414  */
1415  SCIP_CALL( SCIPallocBufferArray(scip, &allcurrminweight, nmyitems) );
1416  assert(myweights[0] - minweight < INT_MAX);
1417  currminweight = (int) (myweights[0] - minweight);
1418  allcurrminweight[0] = currminweight;
1419 
1420  /* fills first row of dynamic programming table with optimal values */
1421  for( d = currminweight; d < intcap; ++d )
1422  optvalues[d] = myprofits[0];
1423 
1424  /* fills dynamic programming table with optimal values */
1425  for( j = 1; j < nmyitems; ++j )
1426  {
1427  int intweight;
1428 
1429  /* compute important part of weight, which will be represented in the table */
1430  intweight = (int)(myweights[j] - minweight);
1431  assert(0 <= intweight && intweight < intcap);
1432 
1433  /* copy all nonzeros from row above */
1434  for( d = currminweight; d < intweight && d < intcap; ++d )
1435  optvalues[IDX(j,d)] = optvalues[IDX(j-1,d)];
1436 
1437  /* update corresponding row */
1438  for( d = intweight; d < intcap; ++d )
1439  {
1440  /* if index d < current minweight then optvalues[IDX(j-1,d)] is not initialized, i.e. should be 0 */
1441  if( d < currminweight )
1442  optvalues[IDX(j,d)] = myprofits[j];
1443  else
1444  {
1445  SCIP_Real sumprofit;
1446 
1447  if( d - myweights[j] < currminweight )
1448  sumprofit = myprofits[j];
1449  else
1450  sumprofit = optvalues[IDX(j-1,(int)(d-myweights[j]))] + myprofits[j];
1451 
1452  optvalues[IDX(j,d)] = MAX(sumprofit, optvalues[IDX(j-1,d)]);
1453  }
1454  }
1455 
1456  /* update currminweight */
1457  if( intweight < currminweight )
1458  currminweight = intweight;
1459 
1460  allcurrminweight[j] = currminweight;
1461  }
1462 
1463  /* update optimal solution by following the table */
1464  if( solitems != NULL )
1465  {
1466  d = intcap - 1;
1467 
1468  SCIPdebugMsg(scip, "Fill the solution vector after solving exactly.\n");
1469 
1470  /* insert all items in (non-) solution vector */
1471  for( j = nmyitems - 1; j > 0; --j )
1472  {
1473  /* if the following condition holds this means all remaining items does not fit anymore */
1474  if( d < allcurrminweight[j] )
1475  {
1476  /* we cannot have exceeded our capacity */
1477  assert((SCIP_Longint) d >= -minweight);
1478  break;
1479  }
1480 
1481  /* collect solution items; the first condition means that no further item can fit anymore, but this does */
1482  if( d < allcurrminweight[j-1] || optvalues[IDX(j,d)] > optvalues[IDX(j-1,d)] )
1483  {
1484  solitems[(*nsolitems)++] = myitems[j];
1485 
1486  /* check that we do not have an underflow */
1487  assert(myweights[j] <= (INT_MAX + (SCIP_Longint) d));
1488  d = (int)(d - myweights[j]);
1489  }
1490  /* collect non-solution items */
1491  else
1492  nonsolitems[(*nnonsolitems)++] = myitems[j];
1493  }
1494 
1495  /* insert remaining items */
1496  if( d >= allcurrminweight[j] )
1497  {
1498  assert(j == 0);
1499  solitems[(*nsolitems)++] = myitems[j];
1500  }
1501  else
1502  {
1503  assert(j >= 0);
1504  assert(d < allcurrminweight[j]);
1505 
1506  for( ; j >= 0; --j )
1507  nonsolitems[(*nnonsolitems)++] = myitems[j];
1508  }
1509 
1510  assert(*nsolitems + *nnonsolitems == nitems);
1511  }
1512 
1513  /* update solution value */
1514  if( solval != NULL )
1515  *solval += optvalues[IDX(nmyitems-1,intcap-1)];
1516  SCIPfreeBufferArray(scip, &allcurrminweight);
1517 
1518  /* free all temporary memory */
1519  SCIPfreeBufferArray(scip, &optvalues);
1520 
1521  TERMINATE:
1522  SCIPfreeBufferArray(scip, &myitems);
1523  SCIPfreeBufferArray(scip, &myprofits);
1524  SCIPfreeBufferArray(scip, &myweights);
1525 
1526  return SCIP_OKAY;
1527 }
1528 
1529 /** solves knapsack problem in maximization form approximately by solving the LP-relaxation of the problem using Dantzig's
1530  * method and rounding down the solution; if needed, one can provide arrays to store all selected items and all not
1531  * selected items
1532  */
1534  SCIP* scip, /**< SCIP data structure */
1535  int nitems, /**< number of available items */
1536  SCIP_Longint* weights, /**< item weights */
1537  SCIP_Real* profits, /**< item profits */
1538  SCIP_Longint capacity, /**< capacity of knapsack */
1539  int* items, /**< item numbers */
1540  int* solitems, /**< array to store items in solution, or NULL */
1541  int* nonsolitems, /**< array to store items not in solution, or NULL */
1542  int* nsolitems, /**< pointer to store number of items in solution, or NULL */
1543  int* nnonsolitems, /**< pointer to store number of items not in solution, or NULL */
1544  SCIP_Real* solval /**< pointer to store optimal solution value, or NULL */
1545  )
1546 {
1547  SCIP_Real* tempsort;
1548  SCIP_Longint solitemsweight;
1549  SCIP_Real* realweights;
1550  int j;
1551  int criticalindex;
1552 
1553  assert(weights != NULL);
1554  assert(profits != NULL);
1555  assert(capacity >= 0);
1556  assert(items != NULL);
1557  assert(nitems >= 0);
1558 
1559  if( solitems != NULL )
1560  {
1561  *nsolitems = 0;
1562  *nnonsolitems = 0;
1563  }
1564  if( solval != NULL )
1565  *solval = 0.0;
1566 
1567  /* initialize data for median search */
1568  SCIP_CALL( SCIPallocBufferArray(scip, &tempsort, nitems) );
1569  SCIP_CALL( SCIPallocBufferArray(scip, &realweights, nitems) );
1570  for( j = nitems - 1; j >= 0; --j )
1571  {
1572  tempsort[j] = profits[j]/((SCIP_Real) weights[j]);
1573  realweights[j] = (SCIP_Real)weights[j];
1574  }
1575 
1576  /* partially sort indices such that all elements that are larger than the break item appear first */
1577  SCIPselectWeightedDownRealLongRealInt(tempsort, weights, profits, items, realweights, (SCIP_Real)capacity, nitems, &criticalindex);
1578 
1579  /* selects items as long as they fit into the knapsack */
1580  solitemsweight = 0;
1581  for( j = 0; j < nitems && solitemsweight + weights[j] <= capacity; ++j )
1582  {
1583  if( solitems != NULL )
1584  solitems[(*nsolitems)++] = items[j];
1585 
1586  if( solval != NULL )
1587  (*solval) += profits[j];
1588  solitemsweight += weights[j];
1589  }
1590  if ( solitems != NULL )
1591  {
1592  for( ; j < nitems; j++ )
1593  nonsolitems[(*nnonsolitems)++] = items[j];
1594  }
1595 
1596  SCIPfreeBufferArray(scip, &realweights);
1597  SCIPfreeBufferArray(scip, &tempsort);
1598 
1599  return SCIP_OKAY;
1600 }
1601 
1602 #ifdef SCIP_DEBUG
1603 /** prints all nontrivial GUB constraints and their LP solution values */
1604 static
1605 void GUBsetPrint(
1606  SCIP* scip, /**< SCIP data structure */
1607  SCIP_GUBSET* gubset, /**< GUB set data structure */
1608  SCIP_VAR** vars, /**< variables in knapsack constraint */
1609  SCIP_Real* solvals /**< solution values of variables in knapsack constraint; or NULL */
1610  )
1611 {
1612  int nnontrivialgubconss;
1613  int c;
1614 
1615  nnontrivialgubconss = 0;
1616 
1617  SCIPdebugMsg(scip, " Nontrivial GUBs of current GUB set:\n");
1618 
1619  /* print out all nontrivial GUB constraints, i.e., with more than one variable */
1620  for( c = 0; c < gubset->ngubconss; c++ )
1621  {
1622  SCIP_Real gubsolval;
1623 
1624  assert(gubset->gubconss[c]->ngubvars >= 0);
1625 
1626  /* nontrivial GUB */
1627  if( gubset->gubconss[c]->ngubvars > 1 )
1628  {
1629  int v;
1630 
1631  gubsolval = 0.0;
1632  SCIPdebugMsg(scip, " GUB<%d>:\n", c);
1633 
1634  /* print GUB var */
1635  for( v = 0; v < gubset->gubconss[c]->ngubvars; v++ )
1636  {
1637  int currentvar;
1638 
1639  currentvar = gubset->gubconss[c]->gubvars[v];
1640  if( solvals != NULL )
1641  {
1642  gubsolval += solvals[currentvar];
1643  SCIPdebugMsg(scip, " +<%s>(%4.2f)\n", SCIPvarGetName(vars[currentvar]), solvals[currentvar]);
1644  }
1645  else
1646  {
1647  SCIPdebugMsg(scip, " +<%s>\n", SCIPvarGetName(vars[currentvar]));
1648  }
1649  }
1650 
1651  /* check whether LP solution satisfies the GUB constraint */
1652  if( solvals != NULL )
1653  {
1654  SCIPdebugMsg(scip, " =%4.2f <= 1 %s\n", gubsolval,
1655  SCIPisFeasGT(scip, gubsolval, 1.0) ? "--> violated" : "");
1656  }
1657  else
1658  {
1659  SCIPdebugMsg(scip, " <= 1 %s\n", SCIPisFeasGT(scip, gubsolval, 1.0) ? "--> violated" : "");
1660  }
1661  nnontrivialgubconss++;
1662  }
1663  }
1664 
1665  SCIPdebugMsg(scip, " --> %d/%d nontrivial GUBs\n", nnontrivialgubconss, gubset->ngubconss);
1666 }
1667 #endif
1668 
1669 /** creates an empty GUB constraint */
1670 static
1672  SCIP* scip, /**< SCIP data structure */
1673  SCIP_GUBCONS** gubcons /**< pointer to store GUB constraint data */
1674  )
1675 {
1676  assert(scip != NULL);
1677  assert(gubcons != NULL);
1678 
1679  /* allocate memory for GUB constraint data structures */
1680  SCIP_CALL( SCIPallocBuffer(scip, gubcons) );
1681  (*gubcons)->gubvarssize = GUBCONSGROWVALUE;
1682  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubcons)->gubvars, (*gubcons)->gubvarssize) );
1683  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubcons)->gubvarsstatus, (*gubcons)->gubvarssize) );
1684 
1685  (*gubcons)->ngubvars = 0;
1686 
1687  return SCIP_OKAY;
1688 }
1689 
1690 /** frees GUB constraint */
1691 static
1692 void GUBconsFree(
1693  SCIP* scip, /**< SCIP data structure */
1694  SCIP_GUBCONS** gubcons /**< pointer to GUB constraint data structure */
1695  )
1696 {
1697  assert(scip != NULL);
1698  assert(gubcons != NULL);
1699  assert((*gubcons)->gubvars != NULL);
1700  assert((*gubcons)->gubvarsstatus != NULL);
1701 
1702  /* free allocated memory */
1703  SCIPfreeBufferArray(scip, &(*gubcons)->gubvarsstatus);
1704  SCIPfreeBufferArray(scip, &(*gubcons)->gubvars);
1705  SCIPfreeBuffer(scip, gubcons);
1706 }
1707 
1708 /** adds variable to given GUB constraint */
1709 static
1711  SCIP* scip, /**< SCIP data structure */
1712  SCIP_GUBCONS* gubcons, /**< GUB constraint data */
1713  int var /**< index of given variable in knapsack constraint */
1714  )
1715 {
1716  assert(scip != NULL);
1717  assert(gubcons != NULL);
1718  assert(gubcons->ngubvars >= 0 && gubcons->ngubvars < gubcons->gubvarssize);
1719  assert(gubcons->gubvars != NULL);
1720  assert(gubcons->gubvarsstatus != NULL);
1721  assert(var >= 0);
1722 
1723  /* add variable to GUB constraint */
1724  gubcons->gubvars[gubcons->ngubvars] = var;
1725  gubcons->gubvarsstatus[gubcons->ngubvars] = GUBVARSTATUS_UNINITIAL;
1726  gubcons->ngubvars++;
1727 
1728  /* increase space allocated to GUB constraint if the number of variables reaches the size */
1729  if( gubcons->ngubvars == gubcons->gubvarssize )
1730  {
1731  int newlen;
1732 
1733  newlen = gubcons->gubvarssize + GUBCONSGROWVALUE;
1734  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvars, newlen) );
1735  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvarsstatus, newlen) );
1736 
1737  gubcons->gubvarssize = newlen;
1738  }
1739 
1740  return SCIP_OKAY;
1741 }
1742 
1743 /** deletes variable from its current GUB constraint */
1744 static
1746  SCIP* scip, /**< SCIP data structure */
1747  SCIP_GUBCONS* gubcons, /**< GUB constraint data */
1748  int var, /**< index of given variable in knapsack constraint */
1749  int gubvarsidx /**< index of the variable in its current GUB constraint */
1750  )
1751 {
1752  assert(scip != NULL);
1753  assert(gubcons != NULL);
1754  assert(var >= 0);
1755  assert(gubvarsidx >= 0 && gubvarsidx < gubcons->ngubvars);
1756  assert(gubcons->ngubvars >= gubvarsidx+1);
1757  assert(gubcons->gubvars[gubvarsidx] == var);
1758 
1759  /* delete variable from GUB by swapping it replacing in by the last variable in the GUB constraint */
1760  gubcons->gubvars[gubvarsidx] = gubcons->gubvars[gubcons->ngubvars-1];
1761  gubcons->gubvarsstatus[gubvarsidx] = gubcons->gubvarsstatus[gubcons->ngubvars-1];
1762  gubcons->ngubvars--;
1763 
1764  /* decrease space allocated for the GUB constraint, if the last GUBCONSGROWVALUE+1 array entries are now empty */
1765  if( gubcons->ngubvars < gubcons->gubvarssize - GUBCONSGROWVALUE && gubcons->ngubvars > 0 )
1766  {
1767  int newlen;
1768 
1769  newlen = gubcons->gubvarssize - GUBCONSGROWVALUE;
1770 
1771  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvars, newlen) );
1772  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvarsstatus, newlen) );
1773 
1774  gubcons->gubvarssize = newlen;
1775  }
1776 
1777  return SCIP_OKAY;
1778 }
1779 
1780 /** moves variable from current GUB constraint to a different existing (nonempty) GUB constraint */
1781 static
1783  SCIP* scip, /**< SCIP data structure */
1784  SCIP_GUBSET* gubset, /**< GUB set data structure */
1785  SCIP_VAR** vars, /**< variables in knapsack constraint */
1786  int var, /**< index of given variable in knapsack constraint */
1787  int oldgubcons, /**< index of old GUB constraint of given variable */
1788  int newgubcons /**< index of new GUB constraint of given variable */
1789  )
1791  int oldgubvaridx;
1792  int replacevar;
1793  int j;
1794 
1795  assert(scip != NULL);
1796  assert(gubset != NULL);
1797  assert(var >= 0);
1798  assert(oldgubcons >= 0 && oldgubcons < gubset->ngubconss);
1799  assert(newgubcons >= 0 && newgubcons < gubset->ngubconss);
1800  assert(oldgubcons != newgubcons);
1801  assert(gubset->gubconssidx[var] == oldgubcons);
1802  assert(gubset->gubconss[oldgubcons]->ngubvars > 0);
1803  assert(gubset->gubconss[newgubcons]->ngubvars >= 0);
1804 
1805  SCIPdebugMsg(scip, " moving variable<%s> from GUB<%d> to GUB<%d>\n", SCIPvarGetName(vars[var]), oldgubcons, newgubcons);
1806 
1807  oldgubvaridx = gubset->gubvarsidx[var];
1808 
1809  /* delete variable from old GUB constraint by replacing it by the last variable of the GUB constraint */
1810  SCIP_CALL( GUBconsDelVar(scip, gubset->gubconss[oldgubcons], var, oldgubvaridx) );
1811 
1812  /* in GUB set, update stored index of variable in old GUB constraint for the variable used for replacement;
1813  * replacement variable is given by old position of the deleted variable
1814  */
1815  replacevar = gubset->gubconss[oldgubcons]->gubvars[oldgubvaridx];
1816  assert(gubset->gubvarsidx[replacevar] == gubset->gubconss[oldgubcons]->ngubvars);
1817  gubset->gubvarsidx[replacevar] = oldgubvaridx;
1818 
1819  /* add variable to the end of new GUB constraint */
1820  SCIP_CALL( GUBconsAddVar(scip, gubset->gubconss[newgubcons], var) );
1821  assert(gubset->gubconss[newgubcons]->gubvars[gubset->gubconss[newgubcons]->ngubvars-1] == var);
1822 
1823  /* in GUB set, update stored index of GUB of moved variable and stored index of variable in this GUB constraint */
1824  gubset->gubconssidx[var] = newgubcons;
1825  gubset->gubvarsidx[var] = gubset->gubconss[newgubcons]->ngubvars-1;
1826 
1827  /* delete old GUB constraint if it became empty */
1828  if( gubset->gubconss[oldgubcons]->ngubvars == 0 )
1829  {
1830  SCIPdebugMsg(scip, "deleting empty GUB cons<%d> from current GUB set\n", oldgubcons);
1831 #ifdef SCIP_DEBUG
1832  GUBsetPrint(scip, gubset, vars, NULL);
1833 #endif
1834 
1835  /* free old GUB constraint */
1836  GUBconsFree(scip, &gubset->gubconss[oldgubcons]);
1837 
1838  /* if empty GUB was not the last one in GUB set data structure, replace it by last GUB constraint */
1839  if( oldgubcons != gubset->ngubconss-1 )
1840  {
1841  gubset->gubconss[oldgubcons] = gubset->gubconss[gubset->ngubconss-1];
1842  gubset->gubconsstatus[oldgubcons] = gubset->gubconsstatus[gubset->ngubconss-1];
1843 
1844  /* in GUB set, update stored index of GUB constraint for all variable of the GUB constraint used for replacement;
1845  * replacement GUB is given by old position of the deleted GUB
1846  */
1847  for( j = 0; j < gubset->gubconss[oldgubcons]->ngubvars; j++ )
1848  {
1849  assert(gubset->gubconssidx[gubset->gubconss[oldgubcons]->gubvars[j]] == gubset->ngubconss-1);
1850  gubset->gubconssidx[gubset->gubconss[oldgubcons]->gubvars[j]] = oldgubcons;
1851  }
1852  }
1853 
1854  /* update number of GUB constraints */
1855  gubset->ngubconss--;
1856 
1857  /* variable should be at given new position, unless new GUB constraint replaced empty old GUB constraint
1858  * (because it was at the end of the GUB constraint array)
1859  */
1860  assert(gubset->gubconssidx[var] == newgubcons
1861  || (newgubcons == gubset->ngubconss && gubset->gubconssidx[var] == oldgubcons));
1862  }
1863 #ifndef NDEBUG
1864  else
1865  assert(gubset->gubconssidx[var] == newgubcons);
1866 #endif
1867 
1868  return SCIP_OKAY;
1869 }
1870 
1871 /** swaps two variables in the same GUB constraint */
1872 static
1873 void GUBsetSwapVars(
1874  SCIP* scip, /**< SCIP data structure */
1875  SCIP_GUBSET* gubset, /**< GUB set data structure */
1876  int var1, /**< first variable to be swapped */
1877  int var2 /**< second variable to be swapped */
1878  )
1879 {
1880  int gubcons;
1881  int var1idx;
1882  GUBVARSTATUS var1status;
1883  int var2idx;
1884  GUBVARSTATUS var2status;
1885 
1886  assert(scip != NULL);
1887  assert(gubset != NULL);
1888 
1889  gubcons = gubset->gubconssidx[var1];
1890  assert(gubcons == gubset->gubconssidx[var2]);
1891 
1892  /* nothing to be done if both variables are the same */
1893  if( var1 == var2 )
1894  return;
1895 
1896  /* swap index and status of variables in GUB constraint */
1897  var1idx = gubset->gubvarsidx[var1];
1898  var1status = gubset->gubconss[gubcons]->gubvarsstatus[var1idx];
1899  var2idx = gubset->gubvarsidx[var2];
1900  var2status = gubset->gubconss[gubcons]->gubvarsstatus[var2idx];
1901 
1902  gubset->gubvarsidx[var1] = var2idx;
1903  gubset->gubconss[gubcons]->gubvars[var1idx] = var2;
1904  gubset->gubconss[gubcons]->gubvarsstatus[var1idx] = var2status;
1905 
1906  gubset->gubvarsidx[var2] = var1idx;
1907  gubset->gubconss[gubcons]->gubvars[var2idx] = var1;
1908  gubset->gubconss[gubcons]->gubvarsstatus[var2idx] = var1status;
1909 }
1910 
1911 /** initializes partition of knapsack variables into nonoverlapping trivial GUB constraints (GUB with one variable) */
1912 static
1914  SCIP* scip, /**< SCIP data structure */
1915  SCIP_GUBSET** gubset, /**< pointer to store GUB set data structure */
1916  int nvars, /**< number of variables in the knapsack constraint */
1917  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
1918  SCIP_Longint capacity /**< capacity of knapsack */
1919  )
1920 {
1921  int i;
1922 
1923  assert(scip != NULL);
1924  assert(gubset != NULL);
1925  assert(nvars > 0);
1926  assert(weights != NULL);
1927  assert(capacity >= 0);
1928 
1929  /* allocate memory for GUB set data structures */
1930  SCIP_CALL( SCIPallocBuffer(scip, gubset) );
1931  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubconss, nvars) );
1932  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubconsstatus, nvars) );
1933  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubconssidx, nvars) );
1934  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubvarsidx, nvars) );
1935  (*gubset)->ngubconss = nvars;
1936  (*gubset)->nvars = nvars;
1937 
1938  /* initialize the set of GUB constraints */
1939  for( i = 0; i < nvars; i++ )
1940  {
1941  /* assign each variable to a new (trivial) GUB constraint */
1942  SCIP_CALL( GUBconsCreate(scip, &(*gubset)->gubconss[i]) );
1943  SCIP_CALL( GUBconsAddVar(scip, (*gubset)->gubconss[i], i) );
1944 
1945  /* set status of GUB constraint to initial */
1946  (*gubset)->gubconsstatus[i] = GUBCONSSTATUS_UNINITIAL;
1947 
1948  (*gubset)->gubconssidx[i] = i;
1949  (*gubset)->gubvarsidx[i] = 0;
1950  assert((*gubset)->gubconss[i]->ngubvars == 1);
1951 
1952  /* already updated status of variable in GUB constraint if it exceeds the capacity of the knapsack */
1953  if( weights[i] > capacity )
1954  (*gubset)->gubconss[(*gubset)->gubconssidx[i]]->gubvarsstatus[(*gubset)->gubvarsidx[i]] = GUBVARSTATUS_CAPACITYEXCEEDED;
1955  }
1956 
1957  return SCIP_OKAY;
1958 }
1959 
1960 /** frees GUB set data structure */
1961 static
1962 void GUBsetFree(
1963  SCIP* scip, /**< SCIP data structure */
1964  SCIP_GUBSET** gubset /**< pointer to GUB set data structure */
1965  )
1966 {
1967  int i;
1968 
1969  assert(scip != NULL);
1970  assert(gubset != NULL);
1971  assert((*gubset)->gubconss != NULL);
1972  assert((*gubset)->gubconsstatus != NULL);
1973  assert((*gubset)->gubconssidx != NULL);
1974  assert((*gubset)->gubvarsidx != NULL);
1975 
1976  /* free all GUB constraints */
1977  for( i = (*gubset)->ngubconss-1; i >= 0; --i )
1978  {
1979  assert((*gubset)->gubconss[i] != NULL);
1980  GUBconsFree(scip, &(*gubset)->gubconss[i]);
1981  }
1982 
1983  /* free allocated memory */
1984  SCIPfreeBufferArray( scip, &(*gubset)->gubvarsidx );
1985  SCIPfreeBufferArray( scip, &(*gubset)->gubconssidx );
1986  SCIPfreeBufferArray( scip, &(*gubset)->gubconsstatus );
1987  SCIPfreeBufferArray( scip, &(*gubset)->gubconss );
1988  SCIPfreeBuffer(scip, gubset);
1989 }
1990 
1991 #ifndef NDEBUG
1992 /** checks whether GUB set data structure is consistent */
1993 static
1995  SCIP* scip, /**< SCIP data structure */
1996  SCIP_GUBSET* gubset, /**< GUB set data structure */
1997  SCIP_VAR** vars /**< variables in the knapsack constraint */
1998  )
1999 {
2000  int i;
2001  int gubconsidx;
2002  int gubvaridx;
2003  SCIP_VAR* var1;
2004  SCIP_VAR* var2;
2005  SCIP_Bool var1negated;
2006  SCIP_Bool var2negated;
2007 
2008  assert(scip != NULL);
2009  assert(gubset != NULL);
2010 
2011  SCIPdebugMsg(scip, " GUB set consistency check:\n");
2012 
2013  /* checks for all knapsack vars consistency of stored index of associated gubcons and corresponding index in gubvars */
2014  for( i = 0; i < gubset->nvars; i++ )
2015  {
2016  gubconsidx = gubset->gubconssidx[i];
2017  gubvaridx = gubset->gubvarsidx[i];
2018 
2019  if( gubset->gubconss[gubconsidx]->gubvars[gubvaridx] != i )
2020  {
2021  SCIPdebugMsg(scip, " var<%d> should be in GUB<%d> at position<%d>, but stored is var<%d> instead\n", i,
2022  gubconsidx, gubvaridx, gubset->gubconss[gubconsidx]->gubvars[gubvaridx] );
2023  }
2024  assert(gubset->gubconss[gubconsidx]->gubvars[gubvaridx] == i);
2025  }
2026 
2027  /* checks for each GUB whether all pairs of its variables have a common clique */
2028  for( i = 0; i < gubset->ngubconss; i++ )
2029  {
2030  int j;
2031 
2032  for( j = 0; j < gubset->gubconss[i]->ngubvars; j++ )
2033  {
2034  int k;
2035 
2036  /* get corresponding active problem variable */
2037  var1 = vars[gubset->gubconss[i]->gubvars[j]];
2038  var1negated = FALSE;
2039  SCIP_CALL( SCIPvarGetProbvarBinary(&var1, &var1negated) );
2040 
2041  for( k = j+1; k < gubset->gubconss[i]->ngubvars; k++ )
2042  {
2043  /* get corresponding active problem variable */
2044  var2 = vars[gubset->gubconss[i]->gubvars[k]];
2045  var2negated = FALSE;
2046  SCIP_CALL( SCIPvarGetProbvarBinary(&var2, &var2negated) );
2047 
2048  if( !SCIPvarsHaveCommonClique(var1, !var1negated, var2, !var2negated, TRUE) )
2049  {
2050  SCIPdebugMsg(scip, " GUB<%d>: var<%d,%s> and var<%d,%s> do not share a clique\n", i, j,
2051  SCIPvarGetName(vars[gubset->gubconss[i]->gubvars[j]]), k,
2052  SCIPvarGetName(vars[gubset->gubconss[i]->gubvars[k]]));
2053  SCIPdebugMsg(scip, " GUB<%d>: var<%d,%s> and var<%d,%s> do not share a clique\n", i, j,
2054  SCIPvarGetName(var1), k,
2055  SCIPvarGetName(var2));
2056  }
2057 
2058  /* @todo: in case we used also negated cliques for the GUB partition, this assert has to be changed */
2059  assert(SCIPvarsHaveCommonClique(var1, !var1negated, var2, !var2negated, TRUE));
2060  }
2061  }
2062  }
2063  SCIPdebugMsg(scip, " --> successful\n");
2064 
2065  return SCIP_OKAY;
2066 }
2067 #endif
2068 
2069 /** calculates a partition of the given set of binary variables into cliques;
2070  * afterwards the output array contains one value for each variable, such that two variables got the same value iff they
2071  * were assigned to the same clique;
2072  * the first variable is always assigned to clique 0, and a variable can only be assigned to clique i if at least one of
2073  * the preceding variables was assigned to clique i-1;
2074  * note: in contrast to SCIPcalcCliquePartition(), variables with LP value 1 are put into trivial cliques (with one
2075  * variable) and for the remaining variables, a partition with a small number of cliques is constructed
2076  */
2077 
2078 static
2080  SCIP*const scip, /**< SCIP data structure */
2081  SCIP_VAR**const vars, /**< binary variables in the clique from which at most one can be set to 1 */
2082  int const nvars, /**< number of variables in the clique */
2083  int*const cliquepartition, /**< array of length nvars to store the clique partition */
2084  int*const ncliques, /**< pointer to store number of cliques actually contained in the partition */
2085  SCIP_Real* solvals /**< solution values of all given binary variables */
2086  )
2088  SCIP_VAR** tmpvars;
2089  SCIP_VAR** cliquevars;
2090  SCIP_Bool* cliquevalues;
2091  SCIP_Bool* tmpvalues;
2092  int* varseq;
2093  int* sortkeys;
2094  int ncliquevars;
2095  int maxncliquevarscomp;
2096  int nignorevars;
2097  int nvarsused;
2098  int i;
2099 
2100  assert(scip != NULL);
2101  assert(nvars == 0 || vars != NULL);
2102  assert(nvars == 0 || cliquepartition != NULL);
2103  assert(ncliques != NULL);
2104 
2105  if( nvars == 0 )
2106  {
2107  *ncliques = 0;
2108  return SCIP_OKAY;
2109  }
2110 
2111  /* allocate temporary memory for storing the variables of the current clique */
2112  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevars, nvars) );
2113  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevalues, nvars) );
2114  SCIP_CALL( SCIPallocBufferArray(scip, &tmpvalues, nvars) );
2115  SCIP_CALL( SCIPduplicateBufferArray(scip, &tmpvars, vars, nvars) );
2116  SCIP_CALL( SCIPallocBufferArray(scip, &varseq, nvars) );
2117  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, nvars) );
2118 
2119  /* initialize the cliquepartition array with -1 */
2120  /* initialize the tmpvalues array */
2121  for( i = nvars - 1; i >= 0; --i )
2122  {
2123  tmpvalues[i] = TRUE;
2124  cliquepartition[i] = -1;
2125  }
2126 
2127  /* get corresponding active problem variables */
2128  SCIP_CALL( SCIPvarsGetProbvarBinary(&tmpvars, &tmpvalues, nvars) );
2129 
2130  /* ignore variables with LP value 1 (will be assigned to trivial GUBs at the end) and sort remaining variables
2131  * by nondecreasing number of cliques the variables are in
2132  */
2133  nignorevars = 0;
2134  nvarsused = 0;
2135  for( i = 0; i < nvars; i++ )
2136  {
2137  if( SCIPisFeasEQ(scip, solvals[i], 1.0) )
2138  {
2139  /* variables with LP value 1 are put to the end of varseq array and will not be sorted */
2140  varseq[nvars-1-nignorevars] = i;
2141  nignorevars++;
2142  }
2143  else
2144  {
2145  /* remaining variables are put to the front of varseq array and will be sorted by their number of cliques */
2146  varseq[nvarsused] = i;
2147  sortkeys[nvarsused] = SCIPvarGetNCliques(tmpvars[i], tmpvalues[i]);
2148  nvarsused++;
2149  }
2150  }
2151  assert(nvarsused + nignorevars == nvars);
2152 
2153  /* sort variables with LP value less than 1 by nondecreasing order of the number of cliques they are in */
2154  SCIPsortIntInt(sortkeys, varseq, nvarsused);
2155 
2156  maxncliquevarscomp = MIN(nvars*nvars, MAXNCLIQUEVARSCOMP);
2157 
2158  /* calculate the clique partition */
2159  *ncliques = 0;
2160  for( i = 0; i < nvars; ++i )
2161  {
2162  if( cliquepartition[varseq[i]] == -1 )
2163  {
2164  int j;
2165 
2166  /* variable starts a new clique */
2167  cliquepartition[varseq[i]] = *ncliques;
2168  cliquevars[0] = tmpvars[varseq[i]];
2169  cliquevalues[0] = tmpvalues[varseq[i]];
2170  ncliquevars = 1;
2171 
2172  /* if variable is not active (multi-aggregated or fixed), it cannot be in any clique and
2173  * if the variable has LP value 1 we do not want it to be in nontrivial cliques
2174  */
2175  if( SCIPvarIsActive(tmpvars[varseq[i]]) && i < nvarsused )
2176  {
2177  /* greedily fill up the clique */
2178  for( j = i + 1; j < nvarsused; ++j )
2179  {
2180  /* if variable is not active (multi-aggregated or fixed), it cannot be in any clique */
2181  if( cliquepartition[varseq[j]] == -1 && SCIPvarIsActive(tmpvars[varseq[j]]) )
2182  {
2183  int k;
2184 
2185  /* check if every variable in the actual clique is in clique with the new variable */
2186  for( k = ncliquevars - 1; k >= 0; --k )
2187  {
2188  if( !SCIPvarsHaveCommonClique(tmpvars[varseq[j]], tmpvalues[varseq[j]], cliquevars[k],
2189  cliquevalues[k], TRUE) )
2190  break;
2191  }
2192 
2193  if( k == -1 )
2194  {
2195  /* put the variable into the same clique */
2196  cliquepartition[varseq[j]] = cliquepartition[varseq[i]];
2197  cliquevars[ncliquevars] = tmpvars[varseq[j]];
2198  cliquevalues[ncliquevars] = tmpvalues[varseq[j]];
2199  ++ncliquevars;
2200  }
2201  }
2202  }
2203  }
2204 
2205  /* this clique is finished */
2206  ++(*ncliques);
2207  }
2208  assert(cliquepartition[varseq[i]] >= 0 && cliquepartition[varseq[i]] < i + 1);
2209 
2210  /* break if we reached the maximal number of comparisons */
2211  if( i * nvars > maxncliquevarscomp )
2212  break;
2213  }
2214  /* if we had too many variables fill up the cliquepartition and put each variable in a separate clique */
2215  for( ; i < nvars; ++i )
2216  {
2217  if( cliquepartition[varseq[i]] == -1 )
2218  {
2219  cliquepartition[varseq[i]] = *ncliques;
2220  ++(*ncliques);
2221  }
2222  }
2223 
2224  /* free temporary memory */
2225  SCIPfreeBufferArray(scip, &sortkeys);
2226  SCIPfreeBufferArray(scip, &varseq);
2227  SCIPfreeBufferArray(scip, &tmpvars);
2228  SCIPfreeBufferArray(scip, &tmpvalues);
2229  SCIPfreeBufferArray(scip, &cliquevalues);
2230  SCIPfreeBufferArray(scip, &cliquevars);
2231 
2232  return SCIP_OKAY;
2233 }
2234 
2235 /** constructs sophisticated partition of knapsack variables into non-overlapping GUBs; current partition uses trivial GUBs */
2236 static
2238  SCIP* scip, /**< SCIP data structure */
2239  SCIP_GUBSET* gubset, /**< GUB set data structure */
2240  SCIP_VAR** vars, /**< variables in the knapsack constraint */
2241  SCIP_Real* solvals /**< solution values of all knapsack variables */
2242  )
2243 {
2244  int* cliquepartition;
2245  int* gubfirstvar;
2246  int ncliques;
2247  int currentgubconsidx;
2248  int newgubconsidx;
2249  int cliqueidx;
2250  int nvars;
2251  int i;
2252 
2253  assert(scip != NULL);
2254  assert(gubset != NULL);
2255  assert(vars != NULL);
2256 
2257  nvars = gubset->nvars;
2258  assert(nvars >= 0);
2259 
2260  /* allocate temporary memory for clique partition */
2261  SCIP_CALL( SCIPallocBufferArray(scip, &cliquepartition, nvars) );
2262 
2263  /* compute sophisticated clique partition */
2264  SCIP_CALL( GUBsetCalcCliquePartition(scip, vars, nvars, cliquepartition, &ncliques, solvals) );
2265 
2266  /* allocate temporary memory for GUB set data structure */
2267  SCIP_CALL( SCIPallocBufferArray(scip, &gubfirstvar, ncliques) );
2268 
2269  /* translate GUB partition into GUB set data structure */
2270  for( i = 0; i < ncliques; i++ )
2271  {
2272  /* initialize first variable for every GUB */
2273  gubfirstvar[i] = -1;
2274  }
2275  /* move every knapsack variable into GUB defined by clique partition */
2276  for( i = 0; i < nvars; i++ )
2277  {
2278  assert(cliquepartition[i] >= 0);
2279 
2280  cliqueidx = cliquepartition[i];
2281  currentgubconsidx = gubset->gubconssidx[i];
2282  assert(gubset->gubconss[currentgubconsidx]->ngubvars == 1 );
2283 
2284  /* variable is first element in GUB constraint defined by clique partition */
2285  if( gubfirstvar[cliqueidx] == -1 )
2286  {
2287  /* corresponding GUB constraint in GUB set data structure was already constructed (as initial trivial GUB);
2288  * note: no assert for gubconssidx, because it can changed due to deleting empty GUBs in GUBsetMoveVar()
2289  */
2290  assert(gubset->gubvarsidx[i] == 0);
2291  assert(gubset->gubconss[gubset->gubconssidx[i]]->gubvars[gubset->gubvarsidx[i]] == i);
2292 
2293  /* remember the first variable found for the current GUB */
2294  gubfirstvar[cliqueidx] = i;
2295  }
2296  /* variable is additional element of GUB constraint defined by clique partition */
2297  else
2298  {
2299  assert(gubfirstvar[cliqueidx] >= 0 && gubfirstvar[cliqueidx] < i);
2300 
2301  /* move variable to GUB constraint defined by clique partition; index of this GUB constraint is given by the
2302  * first variable of this GUB constraint
2303  */
2304  newgubconsidx = gubset->gubconssidx[gubfirstvar[cliqueidx]];
2305  assert(newgubconsidx != currentgubconsidx); /* because initially every variable is in a different GUB */
2306  SCIP_CALL( GUBsetMoveVar(scip, gubset, vars, i, currentgubconsidx, newgubconsidx) );
2307 
2308  assert(gubset->gubconss[gubset->gubconssidx[i]]->gubvars[gubset->gubvarsidx[i]] == i);
2309  }
2310  }
2311 
2312 #ifdef SCIP_DEBUG
2313  /* prints GUB set data structure */
2314  GUBsetPrint(scip, gubset, vars, solvals);
2315 #endif
2316 
2317 #ifndef NDEBUG
2318  /* checks consistency of GUB set data structure */
2319  SCIP_CALL( GUBsetCheck(scip, gubset, vars) );
2320 #endif
2321 
2322  /* free temporary memory */
2323  SCIPfreeBufferArray(scip, &gubfirstvar);
2324  SCIPfreeBufferArray(scip, &cliquepartition);
2325 
2326  return SCIP_OKAY;
2327 }
2328 
2329 /** 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$
2330  * 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
2331  * \f$j \in N \setminus C\f$, if \f$j \in N_0 = \{j \in N : x^*_j = 0\}\f$, if one exists.
2332  */
2333 static
2335  SCIP* scip, /**< SCIP data structure */
2336  SCIP_VAR** vars, /**< variables in knapsack constraint */
2337  int nvars, /**< number of variables in knapsack constraint */
2338  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2339  SCIP_Longint capacity, /**< capacity of knapsack */
2340  SCIP_Real* solvals, /**< solution values of all problem variables */
2341  int* covervars, /**< pointer to store cover variables */
2342  int* noncovervars, /**< pointer to store noncover variables */
2343  int* ncovervars, /**< pointer to store number of cover variables */
2344  int* nnoncovervars, /**< pointer to store number of noncover variables */
2345  SCIP_Longint* coverweight, /**< pointer to store weight of cover */
2346  SCIP_Bool* found, /**< pointer to store whether a cover was found */
2347  SCIP_Bool modtransused, /**< should modified transformed separation problem be used to find cover */
2348  int* ntightened, /**< pointer to store number of variables with tightened upper bound */
2349  SCIP_Bool* fractional /**< pointer to store whether the LP sol for knapsack vars is fractional */
2350  )
2351 {
2352  SCIP_Longint* transweights;
2353  SCIP_Real* transprofits;
2354  SCIP_Longint transcapacity;
2355  SCIP_Longint fixedonesweight;
2356  SCIP_Longint itemsweight;
2357  SCIP_Bool infeasible;
2358  int* fixedones;
2359  int* fixedzeros;
2360  int* items;
2361  int nfixedones;
2362  int nfixedzeros;
2363  int nitems;
2364  int j;
2365 
2366  assert(scip != NULL);
2367  assert(vars != NULL);
2368  assert(nvars > 0);
2369  assert(weights != NULL);
2370  assert(capacity >= 0);
2371  assert(solvals != NULL);
2372  assert(covervars != NULL);
2373  assert(noncovervars != NULL);
2374  assert(ncovervars != NULL);
2375  assert(nnoncovervars != NULL);
2376  assert(coverweight != NULL);
2377  assert(found != NULL);
2378  assert(ntightened != NULL);
2379  assert(fractional != NULL);
2380 
2381  SCIPdebugMsg(scip, " get cover for knapsack constraint\n");
2382 
2383  /* allocates temporary memory */
2384  SCIP_CALL( SCIPallocBufferArray(scip, &transweights, nvars) );
2385  SCIP_CALL( SCIPallocBufferArray(scip, &transprofits, nvars) );
2386  SCIP_CALL( SCIPallocBufferArray(scip, &fixedones, nvars) );
2387  SCIP_CALL( SCIPallocBufferArray(scip, &fixedzeros, nvars) );
2388  SCIP_CALL( SCIPallocBufferArray(scip, &items, nvars) );
2389 
2390  *found = FALSE;
2391  *ncovervars = 0;
2392  *nnoncovervars = 0;
2393  *coverweight = 0;
2394  *fractional = TRUE;
2395 
2396  /* gets the following sets
2397  * N_1 = {j in N : x*_j = 1} (fixedones),
2398  * N_0 = {j in N : x*_j = 0} (fixedzeros) and
2399  * N\(N_0 & N_1) (items),
2400  * where x*_j is the solution value of variable x_j
2401  */
2402  nfixedones = 0;
2403  nfixedzeros = 0;
2404  nitems = 0;
2405  fixedonesweight = 0;
2406  itemsweight = 0;
2407  *ntightened = 0;
2408  for( j = 0; j < nvars; j++ )
2409  {
2410  assert(SCIPvarIsBinary(vars[j]));
2411 
2412  /* tightens upper bound of x_j if weight of x_j is greater than capacity of knapsack */
2413  if( weights[j] > capacity )
2414  {
2415  SCIP_CALL( SCIPtightenVarUb(scip, vars[j], 0.0, FALSE, &infeasible, NULL) );
2416  assert(!infeasible);
2417  (*ntightened)++;
2418  continue;
2419  }
2420 
2421  /* variable x_j has solution value one */
2422  if( SCIPisFeasEQ(scip, solvals[j], 1.0) )
2423  {
2424  fixedones[nfixedones] = j;
2425  nfixedones++;
2426  fixedonesweight += weights[j];
2427  }
2428  /* variable x_j has solution value zero */
2429  else if( SCIPisFeasEQ(scip, solvals[j], 0.0) )
2430  {
2431  fixedzeros[nfixedzeros] = j;
2432  nfixedzeros++;
2433  }
2434  /* variable x_j has fractional solution value */
2435  else
2436  {
2437  assert( SCIPisFeasGT(scip, solvals[j], 0.0) && SCIPisFeasLT(scip, solvals[j], 1.0) );
2438  items[nitems] = j;
2439  nitems++;
2440  itemsweight += weights[j];
2441  }
2442  }
2443  assert(nfixedones + nfixedzeros + nitems == nvars - (*ntightened));
2444 
2445  /* sets whether the LP solution x* for the knapsack variables is fractional; if it is not fractional we stop
2446  * the separation routine
2447  */
2448  assert(nitems >= 0);
2449  if( nitems == 0 )
2450  {
2451  *fractional = FALSE;
2452  goto TERMINATE;
2453  }
2454  assert(*fractional);
2455 
2456  /* transforms the traditional separation problem (under consideration of the following fixing:
2457  * z_j = 1 for all j in N_1, z_j = 0 for all j in N_0)
2458  *
2459  * min sum_{j in N\(N_0 & N_1)} (1 - x*_j) z_j
2460  * sum_{j in N\(N_0 & N_1)} a_j z_j >= (a_0 + 1) - sum_{j in N_1} a_j
2461  * z_j in {0,1}, j in N\(N_0 & N_1)
2462  *
2463  * to a knapsack problem in maximization form by complementing the variables
2464  *
2465  * sum_{j in N\(N_0 & N_1)} (1 - x*_j) -
2466  * max sum_{j in N\(N_0 & N_1)} (1 - x*_j) z_j
2467  * sum_{j in N\(N_0 & N_1)} a_j z_j <= sum_{j in N\N_0} a_j - (a_0 + 1)
2468  * z_j in {0,1}, j in N\(N_0 & N_1)
2469  */
2470 
2471  /* gets weight and profit of variables in transformed knapsack problem */
2472  for( j = 0; j < nitems; j++ )
2473  {
2474  transweights[j] = weights[items[j]];
2475  transprofits[j] = 1.0 - solvals[items[j]];
2476  }
2477  /* gets capacity of transformed knapsack problem */
2478  transcapacity = fixedonesweight + itemsweight - capacity - 1;
2479 
2480  /* if capacity of transformed knapsack problem is less than zero, there is no cover
2481  * (when variables fixed to zero are not used)
2482  */
2483  if( transcapacity < 0 )
2484  {
2485  assert(!(*found));
2486  goto TERMINATE;
2487  }
2488 
2489  if( modtransused )
2490  {
2491  /* transforms the modified separation problem (under consideration of the following fixing:
2492  * z_j = 1 for all j in N_1, z_j = 0 for all j in N_0)
2493  *
2494  * min sum_{j in N\(N_0 & N_1)} (1 - x*_j) a_j z_j
2495  * sum_{j in N\(N_0 & N_1)} a_j z_j >= (a_0 + 1) - sum_{j in N_1} a_j
2496  * z_j in {0,1}, j in N\(N_0 & N_1)
2497  *
2498  * to a knapsack problem in maximization form by complementing the variables
2499  *
2500  * sum_{j in N\(N_0 & N_1)} (1 - x*_j) a_j -
2501  * max sum_{j in N\(N_0 & N_1)} (1 - x*_j) a_j z_j
2502  * sum_{j in N\(N_0 & N_1)} a_j z_j <= sum_{j in N\N_0} a_j - (a_0 + 1)
2503  * z_j in {0,1}, j in N\(N_0 & N_1)
2504  */
2505 
2506  /* gets weight and profit of variables in modified transformed knapsack problem */
2507  for( j = 0; j < nitems; j++ )
2508  {
2509  transprofits[j] *= weights[items[j]];
2510  assert(SCIPisFeasPositive(scip, transprofits[j]));
2511  }
2512  }
2513 
2514  /* solves (modified) transformed knapsack problem approximately by solving the LP-relaxation of the (modified)
2515  * transformed knapsack problem using Dantzig's method and rounding down the solution.
2516  * let z* be the solution, then
2517  * j in C, if z*_j = 0 and
2518  * i in N\C, if z*_j = 1.
2519  */
2520  SCIP_CALL( SCIPsolveKnapsackApproximately(scip, nitems, transweights, transprofits, transcapacity, items,
2521  noncovervars, covervars, nnoncovervars, ncovervars, NULL) );
2522  /*assert(checkSolveKnapsack(scip, nitems, transweights, transprofits, items, weights, solvals, modtransused));*/
2523 
2524  /* constructs cover C (sum_{j in C} a_j > a_0) */
2525  for( j = 0; j < *ncovervars; j++ )
2526  {
2527  (*coverweight) += weights[covervars[j]];
2528  }
2529 
2530  /* adds all variables from N_1 to C */
2531  for( j = 0; j < nfixedones; j++ )
2532  {
2533  covervars[*ncovervars] = fixedones[j];
2534  (*ncovervars)++;
2535  (*coverweight) += weights[fixedones[j]];
2536  }
2537 
2538  /* adds all variables from N_0 to N\C */
2539  for( j = 0; j < nfixedzeros; j++ )
2540  {
2541  noncovervars[*nnoncovervars] = fixedzeros[j];
2542  (*nnoncovervars)++;
2543  }
2544  assert((*ncovervars) + (*nnoncovervars) == nvars - (*ntightened));
2545  assert((*coverweight) > capacity);
2546  *found = TRUE;
2547 
2548  TERMINATE:
2549  /* frees temporary memory */
2550  SCIPfreeBufferArray(scip, &items);
2551  SCIPfreeBufferArray(scip, &fixedzeros);
2552  SCIPfreeBufferArray(scip, &fixedones);
2553  SCIPfreeBufferArray(scip, &transprofits);
2554  SCIPfreeBufferArray(scip, &transweights);
2555 
2556  SCIPdebugMsg(scip, " get cover for knapsack constraint -- end\n");
2557 
2558  return SCIP_OKAY;
2559 }
2560 
2561 #ifndef NDEBUG
2562 /** checks if minweightidx is set correctly
2563  */
2564 static
2566  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2567  SCIP_Longint capacity, /**< capacity of knapsack */
2568  int* covervars, /**< pointer to store cover variables */
2569  int ncovervars, /**< pointer to store number of cover variables */
2570  SCIP_Longint coverweight, /**< pointer to store weight of cover */
2571  int minweightidx, /**< index of variable in cover variables with minimum weight */
2572  int j /**< current index in cover variables */
2573  )
2574 {
2575  SCIP_Longint minweight;
2576  int i;
2577 
2578  assert(weights != NULL);
2579  assert(covervars != NULL);
2580  assert(ncovervars > 0);
2581 
2582  minweight = weights[covervars[minweightidx]];
2583 
2584  /* checks if all cover variables before index j have weight greater than minweight */
2585  for( i = 0; i < j; i++ )
2586  {
2587  assert(weights[covervars[i]] > minweight);
2588  if( weights[covervars[i]] <= minweight )
2589  return FALSE;
2590  }
2591 
2592  /* checks if all variables before index j cannot be removed, i.e. i cannot be the next minweightidx */
2593  for( i = 0; i < j; i++ )
2594  {
2595  assert(coverweight - weights[covervars[i]] <= capacity);
2596  if( coverweight - weights[covervars[i]] > capacity )
2597  return FALSE;
2598  }
2599  return TRUE;
2600 }
2601 #endif
2602 
2603 
2604 /** 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$,
2605  * 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$
2606  */
2607 static
2609  SCIP* scip, /**< SCIP data structure */
2610  SCIP_Real* solvals, /**< solution values of all problem variables */
2611  int* covervars, /**< cover variables */
2612  int ncovervars, /**< number of cover variables */
2613  int* varsC1, /**< pointer to store variables in C1 */
2614  int* varsC2, /**< pointer to store variables in C2 */
2615  int* nvarsC1, /**< pointer to store number of variables in C1 */
2616  int* nvarsC2 /**< pointer to store number of variables in C2 */
2617  )
2618 {
2619  int j;
2620 
2621  assert(scip != NULL);
2622  assert(ncovervars >= 0);
2623  assert(solvals != NULL);
2624  assert(covervars != NULL);
2625  assert(varsC1 != NULL);
2626  assert(varsC2 != NULL);
2627  assert(nvarsC1 != NULL);
2628  assert(nvarsC2 != NULL);
2629 
2630  *nvarsC1 = 0;
2631  *nvarsC2 = 0;
2632  for( j = 0; j < ncovervars; j++ )
2633  {
2634  assert(SCIPisFeasGT(scip, solvals[covervars[j]], 0.0));
2635 
2636  /* variable has solution value one */
2637  if( SCIPisGE(scip, solvals[covervars[j]], 1.0) )
2638  {
2639  varsC2[*nvarsC2] = covervars[j];
2640  (*nvarsC2)++;
2641  }
2642  /* variable has solution value less than one */
2643  else
2644  {
2645  assert(SCIPisLT(scip, solvals[covervars[j]], 1.0));
2646  varsC1[*nvarsC1] = covervars[j];
2647  (*nvarsC1)++;
2648  }
2649  }
2650  assert((*nvarsC1) + (*nvarsC2) == ncovervars);
2651 }
2652 
2653 /** changes given partition (C_1,C_2) of minimal cover C, if |C1| = 1, by moving one and two (if possible) variables from
2654  * C2 to C1 if |C1| = 1 and |C1| = 0, respectively.
2655  */
2656 static
2658  SCIP* scip, /**< SCIP data structure */
2659  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2660  int* varsC1, /**< pointer to store variables in C1 */
2661  int* varsC2, /**< pointer to store variables in C2 */
2662  int* nvarsC1, /**< pointer to store number of variables in C1 */
2663  int* nvarsC2 /**< pointer to store number of variables in C2 */
2664  )
2666  SCIP_Real* sortkeysC2;
2667  int j;
2668 
2669  assert(*nvarsC1 >= 0 && *nvarsC1 <= 1);
2670  assert(*nvarsC2 > 0);
2671 
2672  /* allocates temporary memory */
2673  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, *nvarsC2) );
2674 
2675  /* sorts variables in C2 such that a_1 >= .... >= a_|C2| */
2676  for( j = 0; j < *nvarsC2; j++ )
2677  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
2678  SCIPsortDownRealInt(sortkeysC2, varsC2, *nvarsC2);
2679 
2680  /* adds one or two variable from C2 with smallest weight to C1 and removes them from C2 */
2681  assert(*nvarsC2 == 1 || weights[varsC2[(*nvarsC2)-1]] <= weights[varsC2[(*nvarsC2)-2]]);
2682  while( *nvarsC1 < 2 && *nvarsC2 > 0 )
2683  {
2684  varsC1[*nvarsC1] = varsC2[(*nvarsC2)-1];
2685  (*nvarsC1)++;
2686  (*nvarsC2)--;
2687  }
2688 
2689  /* frees temporary memory */
2690  SCIPfreeBufferArray(scip, &sortkeysC2);
2691 
2692  return SCIP_OKAY;
2693 }
2694 
2695 /** changes given partition (C_1,C_2) of feasible set C, if |C1| = 1, by moving one variable from C2 to C1 */
2696 static
2698  SCIP* scip, /**< SCIP data structure */
2699  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2700  int* varsC1, /**< pointer to store variables in C1 */
2701  int* varsC2, /**< pointer to store variables in C2 */
2702  int* nvarsC1, /**< pointer to store number of variables in C1 */
2703  int* nvarsC2 /**< pointer to store number of variables in C2 */
2704  )
2706  SCIP_Real* sortkeysC2;
2707  int j;
2708 
2709  assert(*nvarsC1 >= 0 && *nvarsC1 <= 1);
2710  assert(*nvarsC2 > 0);
2711 
2712  /* allocates temporary memory */
2713  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, *nvarsC2) );
2714 
2715  /* sorts variables in C2 such that a_1 >= .... >= a_|C2| */
2716  for( j = 0; j < *nvarsC2; j++ )
2717  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
2718  SCIPsortDownRealInt(sortkeysC2, varsC2, *nvarsC2);
2719 
2720  /* adds variable from C2 with smallest weight to C1 and removes it from C2 */
2721  assert(*nvarsC2 == 1 || weights[varsC2[(*nvarsC2)-1]] <= weights[varsC2[(*nvarsC2)-2]]);
2722  varsC1[*nvarsC1] = varsC2[(*nvarsC2)-1];
2723  (*nvarsC1)++;
2724  (*nvarsC2)--;
2725 
2726  /* frees temporary memory */
2727  SCIPfreeBufferArray(scip, &sortkeysC2);
2728 
2729  return SCIP_OKAY;
2730 }
2731 
2732 
2733 /** 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$
2734  * and \f$F \cap R = \emptyset\f$; chooses partition as follows \f$R = \{ j \in N \setminus C : x^*_j = 0 \}\f$ and
2735  * \f$F = (N \setminus C) \setminus F\f$
2736  */
2737 static
2739  SCIP* scip, /**< SCIP data structure */
2740  SCIP_Real* solvals, /**< solution values of all problem variables */
2741  int* noncovervars, /**< noncover variables */
2742  int nnoncovervars, /**< number of noncover variables */
2743  int* varsF, /**< pointer to store variables in F */
2744  int* varsR, /**< pointer to store variables in R */
2745  int* nvarsF, /**< pointer to store number of variables in F */
2746  int* nvarsR /**< pointer to store number of variables in R */
2747  )
2748 {
2749  int j;
2750 
2751  assert(scip != NULL);
2752  assert(nnoncovervars >= 0);
2753  assert(solvals != NULL);
2754  assert(noncovervars != NULL);
2755  assert(varsF != NULL);
2756  assert(varsR != NULL);
2757  assert(nvarsF != NULL);
2758  assert(nvarsR != NULL);
2759 
2760  *nvarsF = 0;
2761  *nvarsR = 0;
2762 
2763  for( j = 0; j < nnoncovervars; j++ )
2764  {
2765  /* variable has solution value zero */
2766  if( SCIPisFeasEQ(scip, solvals[noncovervars[j]], 0.0) )
2767  {
2768  varsR[*nvarsR] = noncovervars[j];
2769  (*nvarsR)++;
2770  }
2771  /* variable has solution value greater than zero */
2772  else
2773  {
2774  assert(SCIPisFeasGT(scip, solvals[noncovervars[j]], 0.0));
2775  varsF[*nvarsF] = noncovervars[j];
2776  (*nvarsF)++;
2777  }
2778  }
2779  assert((*nvarsF) + (*nvarsR) == nnoncovervars);
2780 }
2781 
2782 /** sorts variables in F, C_2, and R according to the second level lifting sequence that will be used in the sequential
2783  * lifting procedure
2784  */
2785 static
2787  SCIP* scip, /**< SCIP data structure */
2788  SCIP_Real* solvals, /**< solution values of all problem variables */
2789  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2790  int* varsF, /**< pointer to store variables in F */
2791  int* varsC2, /**< pointer to store variables in C2 */
2792  int* varsR, /**< pointer to store variables in R */
2793  int nvarsF, /**< number of variables in F */
2794  int nvarsC2, /**< number of variables in C2 */
2795  int nvarsR /**< number of variables in R */
2796  )
2797 {
2798  SORTKEYPAIR** sortkeypairsF;
2799  SORTKEYPAIR* sortkeypairsFstore;
2800  SCIP_Real* sortkeysC2;
2801  SCIP_Real* sortkeysR;
2802  int j;
2803 
2804  assert(scip != NULL);
2805  assert(solvals != NULL);
2806  assert(weights != NULL);
2807  assert(varsF != NULL);
2808  assert(varsC2 != NULL);
2809  assert(varsR != NULL);
2810  assert(nvarsF >= 0);
2811  assert(nvarsC2 >= 0);
2812  assert(nvarsR >= 0);
2813 
2814  /* allocates temporary memory */
2815  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairsF, nvarsF) );
2816  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairsFstore, nvarsF) );
2817  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, nvarsC2) );
2818  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysR, nvarsR) );
2819 
2820  /* gets sorting key for variables in F corresponding to the following lifting sequence
2821  * sequence 1: non-increasing absolute difference between x*_j and the value the variable is fixed to, i.e.
2822  * x*_1 >= x*_2 >= ... >= x*_|F|
2823  * in case of equality uses
2824  * sequence 4: non-increasing a_j, i.e. a_1 >= a_2 >= ... >= a_|C_2|
2825  */
2826  for( j = 0; j < nvarsF; j++ )
2827  {
2828  sortkeypairsF[j] = &(sortkeypairsFstore[j]);
2829  sortkeypairsF[j]->key1 = solvals[varsF[j]];
2830  sortkeypairsF[j]->key2 = (SCIP_Real) weights[varsF[j]];
2831  }
2832 
2833  /* gets sorting key for variables in C_2 corresponding to the following lifting sequence
2834  * sequence 4: non-increasing a_j, i.e. a_1 >= a_2 >= ... >= a_|C_2|
2835  */
2836  for( j = 0; j < nvarsC2; j++ )
2837  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
2838 
2839  /* gets sorting key for variables in R corresponding to the following lifting sequence
2840  * sequence 4: non-increasing a_j, i.e. a_1 >= a_2 >= ... >= a_|R|
2841  */
2842  for( j = 0; j < nvarsR; j++ )
2843  sortkeysR[j] = (SCIP_Real) weights[varsR[j]];
2844 
2845  /* sorts F, C2 and R */
2846  if( nvarsF > 0 )
2847  {
2848  SCIPsortDownPtrInt((void**)sortkeypairsF, varsF, compSortkeypairs, nvarsF);
2849  }
2850  if( nvarsC2 > 0 )
2851  {
2852  SCIPsortDownRealInt(sortkeysC2, varsC2, nvarsC2);
2853  }
2854  if( nvarsR > 0)
2855  {
2856  SCIPsortDownRealInt(sortkeysR, varsR, nvarsR);
2857  }
2858 
2859  /* frees temporary memory */
2860  SCIPfreeBufferArray(scip, &sortkeysR);
2861  SCIPfreeBufferArray(scip, &sortkeysC2);
2862  SCIPfreeBufferArray(scip, &sortkeypairsFstore);
2863  SCIPfreeBufferArray(scip, &sortkeypairsF);
2864 
2865  return SCIP_OKAY;
2866 }
2867 
2868 /** categorizes GUBs of knapsack GUB partion into GOC1, GNC1, GF, GC2, and GR and computes a lifting sequence of the GUBs
2869  * for the sequential GUB wise lifting procedure
2870  */
2871 static
2873  SCIP* scip, /**< SCIP data structure */
2874  SCIP_GUBSET* gubset, /**< GUB set data structure */
2875  SCIP_Real* solvals, /**< solution values of variables in knapsack constraint */
2876  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2877  int* varsC1, /**< variables in C1 */
2878  int* varsC2, /**< variables in C2 */
2879  int* varsF, /**< variables in F */
2880  int* varsR, /**< variables in R */
2881  int nvarsC1, /**< number of variables in C1 */
2882  int nvarsC2, /**< number of variables in C2 */
2883  int nvarsF, /**< number of variables in F */
2884  int nvarsR, /**< number of variables in R */
2885  int* gubconsGC1, /**< pointer to store GUBs in GC1(GNC1+GOC1) */
2886  int* gubconsGC2, /**< pointer to store GUBs in GC2 */
2887  int* gubconsGFC1, /**< pointer to store GUBs in GFC1(GNC1+GF) */
2888  int* gubconsGR, /**< pointer to store GUBs in GR */
2889  int* ngubconsGC1, /**< pointer to store number of GUBs in GC1(GNC1+GOC1) */
2890  int* ngubconsGC2, /**< pointer to store number of GUBs in GC2 */
2891  int* ngubconsGFC1, /**< pointer to store number of GUBs in GFC1(GNC1+GF) */
2892  int* ngubconsGR, /**< pointer to store number of GUBs in GR */
2893  int* ngubconscapexceed, /**< pointer to store number of GUBs with only capacity exceeding variables */
2894  int* maxgubvarssize /**< pointer to store the maximal size of GUB constraints */
2895  )
2896 {
2897  SORTKEYPAIR** sortkeypairsGFC1;
2898  SORTKEYPAIR* sortkeypairsGFC1store;
2899  SCIP_Real* sortkeysC1;
2900  SCIP_Real* sortkeysC2;
2901  SCIP_Real* sortkeysR;
2902  int* nC1varsingubcons;
2903  int var;
2904  int gubconsidx;
2905  int varidx;
2906  int ngubconss;
2907  int ngubconsGOC1;
2908  int targetvar;
2909  int nvarsprocessed;
2910  int i;
2911  int j;
2912 
2913 #if GUBSPLITGNC1GUBS
2914  SCIP_Bool gubconswithF;
2915  int origngubconss;
2916  origngubconss = gubset->ngubconss;
2917 #endif
2918 
2919  assert(scip != NULL);
2920  assert(gubset != NULL);
2921  assert(solvals != NULL);
2922  assert(weights != NULL);
2923  assert(varsC1 != NULL);
2924  assert(varsC2 != NULL);
2925  assert(varsF != NULL);
2926  assert(varsR != NULL);
2927  assert(nvarsC1 > 0);
2928  assert(nvarsC2 >= 0);
2929  assert(nvarsF >= 0);
2930  assert(nvarsR >= 0);
2931  assert(gubconsGC1 != NULL);
2932  assert(gubconsGC2 != NULL);
2933  assert(gubconsGFC1 != NULL);
2934  assert(gubconsGR != NULL);
2935  assert(ngubconsGC1 != NULL);
2936  assert(ngubconsGC2 != NULL);
2937  assert(ngubconsGFC1 != NULL);
2938  assert(ngubconsGR != NULL);
2939  assert(maxgubvarssize != NULL);
2940 
2941  ngubconss = gubset->ngubconss;
2942  nvarsprocessed = 0;
2943  ngubconsGOC1 = 0;
2944 
2945  /* GUBs are categorized into different types according to the variables in volved
2946  * - GOC1: involves variables in C1 only -- no C2, R, F
2947  * - GNC1: involves variables in C1 and F (and R) -- no C2
2948  * - GF: involves variables in F (and R) only -- no C1, C2
2949  * - GC2: involves variables in C2 only -- no C1, R, F
2950  * - GR: involves variables in R only -- no C1, C2, F
2951  * which requires splitting GUBs in case they include variable in F and R.
2952  *
2953  * afterwards all GUBs (except GOC1 GUBs, which we do not need to lift) are sorted by a two level lifting sequence.
2954  * - first ordering level is: GFC1 (GNC1+GF), GC2, and GR.
2955  * - second ordering level is
2956  * GFC1: non-increasing number of variables in F and non-increasing max{x*_k : k in GFC1_j} in case of equality
2957  * GC2: non-increasing max{ a_k : k in GC2_j}; note that |GFC2_j| = 1
2958  * GR: non-increasing max{ a_k : k in GR_j}
2959  *
2960  * in additon, another GUB union, which is helpful for the lifting procedure, is formed
2961  * - GC1: GUBs of category GOC1 and GNC1
2962  * with second ordering level non-decreasing min{ a_k : k in GC1_j };
2963  * note that min{ a_k : k in GC1_j } always comes from the first variable in the GUB
2964  */
2965 
2966  /* allocates temporary memory */
2967  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC1, nvarsC1) );
2968  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, nvarsC2) );
2969  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysR, nvarsR) );
2970 
2971  /* to get the GUB lifting sequence, we first sort all variables in F, C2, and R
2972  * - F: non-increasing x*_j and non-increasing a_j in case of equality
2973  * - C2: non-increasing a_j
2974  * - R: non-increasing a_j
2975  * furthermore, sort C1 variables as needed for initializing the minweight table (non-increasing a_j).
2976  */
2977 
2978  /* gets sorting key for variables in C1 corresponding to the following ordering
2979  * non-decreasing a_j, i.e. a_1 <= a_2 <= ... <= a_|C_1|
2980  */
2981  for( j = 0; j < nvarsC1; j++ )
2982  {
2983  /* gets sortkeys */
2984  sortkeysC1[j] = (SCIP_Real) weights[varsC1[j]];
2985 
2986  /* update status of variable in its gub constraint */
2987  gubconsidx = gubset->gubconssidx[varsC1[j]];
2988  varidx = gubset->gubvarsidx[varsC1[j]];
2989  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_C1;
2990  }
2991 
2992  /* gets sorting key for variables in F corresponding to the following ordering
2993  * non-increasing x*_j, i.e., x*_1 >= x*_2 >= ... >= x*_|F|, and
2994  * non-increasing a_j, i.e., a_1 >= a_2 >= ... >= a_|F| in case of equality
2995  * and updates status of each variable in F in GUB set data structure
2996  */
2997  for( j = 0; j < nvarsF; j++ )
2998  {
2999  /* update status of variable in its gub constraint */
3000  gubconsidx = gubset->gubconssidx[varsF[j]];
3001  varidx = gubset->gubvarsidx[varsF[j]];
3002  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_F;
3003  }
3004 
3005  /* gets sorting key for variables in C2 corresponding to the following ordering
3006  * non-increasing a_j, i.e., a_1 >= a_2 >= ... >= a_|C2|
3007  * and updates status of each variable in F in GUB set data structure
3008  */
3009  for( j = 0; j < nvarsC2; j++ )
3010  {
3011  /* gets sortkeys */
3012  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
3013 
3014  /* update status of variable in its gub constraint */
3015  gubconsidx = gubset->gubconssidx[varsC2[j]];
3016  varidx = gubset->gubvarsidx[varsC2[j]];
3017  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_C2;
3018  }
3019 
3020  /* gets sorting key for variables in R corresponding to the following ordering
3021  * non-increasing a_j, i.e., a_1 >= a_2 >= ... >= a_|R|
3022  * and updates status of each variable in F in GUB set data structure
3023  */
3024  for( j = 0; j < nvarsR; j++ )
3025  {
3026  /* gets sortkeys */
3027  sortkeysR[j] = (SCIP_Real) weights[varsR[j]];
3028 
3029  /* update status of variable in its gub constraint */
3030  gubconsidx = gubset->gubconssidx[varsR[j]];
3031  varidx = gubset->gubvarsidx[varsR[j]];
3032  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_R;
3033  }
3034 
3035  /* sorts C1, F, C2 and R */
3036  assert(nvarsC1 > 0);
3037  SCIPsortRealInt(sortkeysC1, varsC1, nvarsC1);
3038 
3039  if( nvarsC2 > 0 )
3040  {
3041  SCIPsortDownRealInt(sortkeysC2, varsC2, nvarsC2);
3042  }
3043  if( nvarsR > 0)
3044  {
3045  SCIPsortDownRealInt(sortkeysR, varsR, nvarsR);
3046  }
3047 
3048  /* frees temporary memory */
3049  SCIPfreeBufferArray(scip, &sortkeysR);
3050  SCIPfreeBufferArray(scip, &sortkeysC2);
3051  SCIPfreeBufferArray(scip, &sortkeysC1);
3052 
3053  /* allocate and initialize temporary memory for sorting GUB constraints */
3054  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairsGFC1, ngubconss) );
3055  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairsGFC1store, ngubconss) );
3056  SCIP_CALL( SCIPallocBufferArray(scip, &nC1varsingubcons, ngubconss) );
3057  BMSclearMemoryArray(nC1varsingubcons, ngubconss);
3058  for( i = 0; i < ngubconss; i++)
3059  {
3060  sortkeypairsGFC1[i] = &(sortkeypairsGFC1store[i]);
3061  sortkeypairsGFC1[i]->key1 = 0.0;
3062  sortkeypairsGFC1[i]->key2 = 0.0;
3063  }
3064  *ngubconsGC1 = 0;
3065  *ngubconsGC2 = 0;
3066  *ngubconsGFC1 = 0;
3067  *ngubconsGR = 0;
3068  *ngubconscapexceed = 0;
3069  *maxgubvarssize = 0;
3070 
3071 #ifndef NDEBUG
3072  for( i = 0; i < gubset->ngubconss; i++ )
3073  assert(gubset->gubconsstatus[i] == GUBCONSSTATUS_UNINITIAL);
3074 #endif
3075 
3076  /* stores GUBs of group GC1 (GOC1+GNC1) and part of the GUBs of group GFC1 (GNC1 GUBs) and sorts variables in these GUBs
3077  * s.t. C1 variables come first (will automatically be sorted by non-decreasing weight).
3078  * gets sorting keys for GUBs of type GFC1 corresponding to the following ordering
3079  * non-increasing number of variables in F, and
3080  * non-increasing max{x*_k : k in GFC1_j} in case of equality
3081  */
3082  for( i = 0; i < nvarsC1; i++ )
3083  {
3084  int nvarsC1capexceed;
3085 
3086  nvarsC1capexceed = 0;
3087 
3088  var = varsC1[i];
3089  gubconsidx = gubset->gubconssidx[var];
3090  varidx = gubset->gubvarsidx[var];
3091 
3092  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3093  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_C1);
3094 
3095  /* current C1 variable is put to the front of its GUB where C1 part is stored by non-decreasing weigth;
3096  * note that variables in C1 are already sorted by non-decreasing weigth
3097  */
3098  targetvar = gubset->gubconss[gubconsidx]->gubvars[nC1varsingubcons[gubconsidx]];
3099  GUBsetSwapVars(scip, gubset, var, targetvar);
3100  nC1varsingubcons[gubconsidx]++;
3101 
3102  /* the GUB was already handled (status set and stored in its group) by another variable of the GUB */
3103  if( gubset->gubconsstatus[gubconsidx] != GUBCONSSTATUS_UNINITIAL )
3104  {
3105  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GOC1
3106  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3107  continue;
3108  }
3109 
3110  /* determine the status of the current GUB constraint, GOC1 or GNC1; GUBs involving R variables are split into
3111  * GOC1/GNC1 and GF, if wanted. also update sorting key if GUB is of type GFC1 (GNC1)
3112  */
3113 #if GUBSPLITGNC1GUBS
3114  gubconswithF = FALSE;
3115 #endif
3116  for( j = 0; j < gubset->gubconss[gubconsidx]->ngubvars; j++ )
3117  {
3118  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[j] != GUBVARSTATUS_BELONGSTOSET_C2);
3119 
3120  /* C1-variable: update number of C1/capacity exceeding variables */
3121  if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_C1 )
3122  {
3123  nvarsC1capexceed++;
3124  nvarsprocessed++;
3125  }
3126  /* F-variable: update sort key (number of F variables in GUB) of corresponding GFC1-GUB */
3127  else if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_F )
3128  {
3129 #if GUBSPLITGNC1GUBS
3130  gubconswithF = TRUE;
3131 #endif
3132  sortkeypairsGFC1[*ngubconsGFC1]->key1 += 1.0;
3133 
3134  if( solvals[gubset->gubconss[gubconsidx]->gubvars[j]] > sortkeypairsGFC1[*ngubconsGFC1]->key2 )
3135  sortkeypairsGFC1[*ngubconsGFC1]->key2 = solvals[gubset->gubconss[gubconsidx]->gubvars[j]];
3136  }
3137  else if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_CAPACITYEXCEEDED )
3138  {
3139  nvarsC1capexceed++;
3140  }
3141  else
3142  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_R);
3143  }
3144 
3145  /* update set of GC1 GUBs */
3146  gubconsGC1[*ngubconsGC1] = gubconsidx;
3147  (*ngubconsGC1)++;
3148 
3149  /* update maximum size of all GUB constraints */
3150  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3151  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3152 
3153  /* set status of GC1-GUB (GOC1 or GNC1) and update set of GFC1 GUBs */
3154  if( nvarsC1capexceed == gubset->gubconss[gubconsidx]->ngubvars )
3155  {
3156  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GOC1;
3157  ngubconsGOC1++;
3158  }
3159  else
3160  {
3161 #if GUBSPLITGNC1GUBS
3162  /* only variables in C1 and R -- no in F: GUB will be split into GR and GOC1 GUBs */
3163  if( !gubconswithF )
3164  {
3165  GUBVARSTATUS movevarstatus;
3166 
3167  assert(gubset->ngubconss < gubset->nvars);
3168 
3169  /* create a new GUB for GR part of splitting */
3170  SCIP_CALL( GUBconsCreate(scip, &gubset->gubconss[gubset->ngubconss]) );
3171  gubset->ngubconss++;
3172  ngubconss = gubset->ngubconss;
3173 
3174  /* fill GR with R variables in current GUB */
3175  for( j = gubset->gubconss[gubconsidx]->ngubvars-1; j >= 0; j-- )
3176  {
3177  movevarstatus = gubset->gubconss[gubconsidx]->gubvarsstatus[j];
3178  if( movevarstatus != GUBVARSTATUS_BELONGSTOSET_C1 )
3179  {
3180  assert(movevarstatus == GUBVARSTATUS_BELONGSTOSET_R || movevarstatus == GUBVARSTATUS_CAPACITYEXCEEDED);
3181  SCIP_CALL( GUBsetMoveVar(scip, gubset, vars, gubset->gubconss[gubconsidx]->gubvars[j],
3182  gubconsidx, ngubconss-1) );
3183  gubset->gubconss[ngubconss-1]->gubvarsstatus[gubset->gubconss[ngubconss-1]->ngubvars-1] =
3184  movevarstatus;
3185  }
3186  }
3187 
3188  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GOC1;
3189  ngubconsGOC1++;
3190 
3191  gubset->gubconsstatus[ngubconss-1] = GUBCONSSTATUS_BELONGSTOSET_GR;
3192  gubconsGR[*ngubconsGR] = ngubconss-1;
3193  (*ngubconsGR)++;
3194  }
3195  /* variables in C1, F, and maybe R: GNC1 GUB */
3196  else
3197  {
3198  assert(gubconswithF);
3199 
3200  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GNC1;
3201  gubconsGFC1[*ngubconsGFC1] = gubconsidx;
3202  (*ngubconsGFC1)++;
3203  }
3204 #else
3205  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GNC1;
3206  gubconsGFC1[*ngubconsGFC1] = gubconsidx;
3207  (*ngubconsGFC1)++;
3208 #endif
3209  }
3210  }
3211 
3212  /* stores GUBs of group GC2 (only trivial GUBs); sorting is not required because the C2 variables (which we loop over)
3213  * are already sorted correctly
3214  */
3215  for( i = 0; i < nvarsC2; i++ )
3216  {
3217  var = varsC2[i];
3218  gubconsidx = gubset->gubconssidx[var];
3219  varidx = gubset->gubvarsidx[var];
3220 
3221  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3222  assert(gubset->gubconss[gubconsidx]->ngubvars == 1);
3223  assert(varidx == 0);
3224  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_C2);
3225  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_UNINITIAL);
3226 
3227  /* set status of GC2 GUB */
3228  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GC2;
3229 
3230  /* update group of GC2 GUBs */
3231  gubconsGC2[*ngubconsGC2] = gubconsidx;
3232  (*ngubconsGC2)++;
3233 
3234  /* update maximum size of all GUB constraints */
3235  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3236  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3237 
3238  nvarsprocessed++;
3239  }
3240 
3241  /* stores remaining part of the GUBs of group GFC1 (GF GUBs) and gets GUB sorting keys corresp. to following ordering
3242  * non-increasing number of variables in F, and
3243  * non-increasing max{x*_k : k in GFC1_j} in case of equality
3244  */
3245  for( i = 0; i < nvarsF; i++ )
3246  {
3247  var = varsF[i];
3248  gubconsidx = gubset->gubconssidx[var];
3249  varidx = gubset->gubvarsidx[var];
3250 
3251  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3252  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_F);
3253 
3254  nvarsprocessed++;
3255 
3256  /* the GUB was already handled (status set and stored in its group) by another variable of the GUB */
3257  if( gubset->gubconsstatus[gubconsidx] != GUBCONSSTATUS_UNINITIAL )
3258  {
3259  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF
3260  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3261  continue;
3262  }
3263 
3264  /* set status of GF GUB */
3265  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GF;
3266 
3267  /* update sorting key of corresponding GFC1 GUB */
3268  for( j = 0; j < gubset->gubconss[gubconsidx]->ngubvars; j++ )
3269  {
3270  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[j] != GUBVARSTATUS_BELONGSTOSET_C2
3271  && gubset->gubconss[gubconsidx]->gubvarsstatus[j] != GUBVARSTATUS_BELONGSTOSET_C1);
3272 
3273  /* F-variable: update sort key (number of F variables in GUB) of corresponding GFC1-GUB */
3274  if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_F )
3275  {
3276  sortkeypairsGFC1[*ngubconsGFC1]->key1 += 1.0;
3277 
3278  if( solvals[gubset->gubconss[gubconsidx]->gubvars[j]] > sortkeypairsGFC1[*ngubconsGFC1]->key2 )
3279  sortkeypairsGFC1[*ngubconsGFC1]->key2 = solvals[gubset->gubconss[gubconsidx]->gubvars[j]];
3280  }
3281  }
3282 
3283  /* update set of GFC1 GUBs */
3284  gubconsGFC1[*ngubconsGFC1] = gubconsidx;
3285  (*ngubconsGFC1)++;
3286 
3287  /* update maximum size of all GUB constraints */
3288  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3289  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3290  }
3291 
3292  /* stores GUBs of group GR; sorting is not required because the R variables (which we loop over) are already sorted
3293  * correctly
3294  */
3295  for( i = 0; i < nvarsR; i++ )
3296  {
3297  var = varsR[i];
3298  gubconsidx = gubset->gubconssidx[var];
3299  varidx = gubset->gubvarsidx[var];
3300 
3301  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3302  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_R);
3303 
3304  nvarsprocessed++;
3305 
3306  /* the GUB was already handled (status set and stored in its group) by another variable of the GUB */
3307  if( gubset->gubconsstatus[gubconsidx] != GUBCONSSTATUS_UNINITIAL )
3308  {
3309  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GR
3310  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF
3311  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3312  continue;
3313  }
3314 
3315  /* set status of GR GUB */
3316  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GR;
3317 
3318  /* update set of GR GUBs */
3319  gubconsGR[*ngubconsGR] = gubconsidx;
3320  (*ngubconsGR)++;
3321 
3322  /* update maximum size of all GUB constraints */
3323  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3324  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3325  }
3326  assert(nvarsprocessed == nvarsC1 + nvarsC2 + nvarsF + nvarsR);
3327 
3328  /* update number of GUBs with only capacity exceeding variables (will not be used for lifting) */
3329  (*ngubconscapexceed) = ngubconss - (ngubconsGOC1 + (*ngubconsGC2) + (*ngubconsGFC1) + (*ngubconsGR));
3330  assert(*ngubconscapexceed >= 0);
3331 #ifndef NDEBUG
3332  {
3333  int check;
3334 
3335  check = 0;
3336 
3337  /* remaining not handled GUBs should only contain capacity exceeding variables */
3338  for( i = 0; i < ngubconss; i++ )
3339  {
3340  if( gubset->gubconsstatus[i] == GUBCONSSTATUS_UNINITIAL )
3341  check++;
3342  }
3343  assert(check == *ngubconscapexceed);
3344  }
3345 #endif
3346 
3347  /* sort GFCI GUBs according to computed sorting keys */
3348  if( (*ngubconsGFC1) > 0 )
3349  {
3350  SCIPsortDownPtrInt((void**)sortkeypairsGFC1, gubconsGFC1, compSortkeypairs, (*ngubconsGFC1));
3351  }
3352 
3353  /* free temporary memory */
3354 #if GUBSPLITGNC1GUBS
3355  ngubconss = origngubconss;
3356 #endif
3357  SCIPfreeBufferArray(scip, &nC1varsingubcons);
3358  SCIPfreeBufferArray(scip, &sortkeypairsGFC1store);
3359  SCIPfreeBufferArray(scip, &sortkeypairsGFC1);
3360 
3361  return SCIP_OKAY;
3362 }
3363 
3364 /** enlarges minweight table to at least the given length */
3365 static
3367  SCIP* scip, /**< SCIP data structure */
3368  SCIP_Longint** minweightsptr, /**< pointer to minweights table */
3369  int* minweightslen, /**< pointer to store number of entries in minweights table (incl. z=0) */
3370  int* minweightssize, /**< pointer to current size of minweights table */
3371  int newlen /**< new length of minweights table */
3372  )
3373 {
3374  int j;
3375 
3376  assert(minweightsptr != NULL);
3377  assert(*minweightsptr != NULL);
3378  assert(minweightslen != NULL);
3379  assert(*minweightslen >= 0);
3380  assert(minweightssize != NULL);
3381  assert(*minweightssize >= 0);
3382 
3383  if( newlen > *minweightssize )
3384  {
3385  int newsize;
3386 
3387  /* reallocate table memory */
3388  newsize = SCIPcalcMemGrowSize(scip, newlen);
3389  SCIP_CALL( SCIPreallocBufferArray(scip, minweightsptr, newsize) );
3390  *minweightssize = newsize;
3391  }
3392  assert(newlen <= *minweightssize);
3393 
3394  /* initialize new elements */
3395  for( j = *minweightslen; j < newlen; ++j )
3396  (*minweightsptr)[j] = SCIP_LONGINT_MAX;
3397  *minweightslen = newlen;
3398 
3399  return SCIP_OKAY;
3400 }
3401 
3402 /** lifts given inequality
3403  * sum_{j in M_1} x_j <= alpha_0
3404  * valid for
3405  * 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 }
3406  * to a valid inequality
3407  * 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
3408  * <= alpha_0 + sum_{j in M_2} alpha_j
3409  * for
3410  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 };
3411  * uses sequential up-lifting for the variables in F, sequential down-lifting for the variable in M_2, and
3412  * sequential up-lifting for the variables in R; procedure can be used to strengthen minimal cover inequalities and
3413  * extended weight inequalities.
3414  */
3415 static
3417  SCIP* scip, /**< SCIP data structure */
3418  SCIP_VAR** vars, /**< variables in knapsack constraint */
3419  int nvars, /**< number of variables in knapsack constraint */
3420  int ntightened, /**< number of variables with tightened upper bound */
3421  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
3422  SCIP_Longint capacity, /**< capacity of knapsack */
3423  SCIP_Real* solvals, /**< solution values of all problem variables */
3424  int* varsM1, /**< variables in M_1 */
3425  int* varsM2, /**< variables in M_2 */
3426  int* varsF, /**< variables in F */
3427  int* varsR, /**< variables in R */
3428  int nvarsM1, /**< number of variables in M_1 */
3429  int nvarsM2, /**< number of variables in M_2 */
3430  int nvarsF, /**< number of variables in F */
3431  int nvarsR, /**< number of variables in R */
3432  int alpha0, /**< rights hand side of given valid inequality */
3433  int* liftcoefs, /**< pointer to store lifting coefficient of vars in knapsack constraint */
3434  SCIP_Real* cutact, /**< pointer to store activity of lifted valid inequality */
3435  int* liftrhs /**< pointer to store right hand side of the lifted valid inequality */
3436  )
3437 {
3438  SCIP_Longint* minweights;
3439  SCIP_Real* sortkeys;
3440  SCIP_Longint fixedonesweight;
3441  int minweightssize;
3442  int minweightslen;
3443  int j;
3444  int w;
3445 
3446  assert(scip != NULL);
3447  assert(vars != NULL);
3448  assert(nvars >= 0);
3449  assert(weights != NULL);
3450  assert(capacity >= 0);
3451  assert(solvals != NULL);
3452  assert(varsM1 != NULL);
3453  assert(varsM2 != NULL);
3454  assert(varsF != NULL);
3455  assert(varsR != NULL);
3456  assert(nvarsM1 >= 0 && nvarsM1 <= nvars - ntightened);
3457  assert(nvarsM2 >= 0 && nvarsM2 <= nvars - ntightened);
3458  assert(nvarsF >= 0 && nvarsF <= nvars - ntightened);
3459  assert(nvarsR >= 0 && nvarsR <= nvars - ntightened);
3460  assert(nvarsM1 + nvarsM2 + nvarsF + nvarsR == nvars - ntightened);
3461  assert(alpha0 >= 0);
3462  assert(liftcoefs != NULL);
3463  assert(cutact != NULL);
3464  assert(liftrhs != NULL);
3465 
3466  /* allocates temporary memory */
3467  minweightssize = nvarsM1 + 1;
3468  SCIP_CALL( SCIPallocBufferArray(scip, &minweights, minweightssize) );
3469  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, nvarsM1) );
3470 
3471  /* initializes data structures */
3472  BMSclearMemoryArray(liftcoefs, nvars);
3473  *cutact = 0.0;
3474 
3475  /* sets lifting coefficient of variables in M1, sorts variables in M1 such that a_1 <= a_2 <= ... <= a_|M1|
3476  * and calculates activity of the current valid inequality
3477  */
3478  for( j = 0; j < nvarsM1; j++ )
3479  {
3480  assert(liftcoefs[varsM1[j]] == 0);
3481  liftcoefs[varsM1[j]] = 1;
3482  sortkeys[j] = (SCIP_Real) (weights[varsM1[j]]);
3483  (*cutact) += solvals[varsM1[j]];
3484  }
3485 
3486  SCIPsortRealInt(sortkeys, varsM1, nvarsM1);
3487 
3488  /* initializes (i = 1) the minweight table, defined as: minweights_i[w] =
3489  * min sum_{j in M_1} a_j x_j + sum_{k=1}^{i-1} a_{j_k} x_{j_k}
3490  * s.t. sum_{j in M_1} x_j + sum_{k=1}^{i-1} alpha_{j_k} x_{j_k} >= w
3491  * x_j in {0,1} for j in M_1 & {j_i,...,j_i-1},
3492  * for i = 1,...,t with t = |N\M1| and w = 0,...,|M1| + sum_{k=1}^{i-1} alpha_{j_k};
3493  */
3494  minweights[0] = 0;
3495  for( w = 1; w <= nvarsM1; w++ )
3496  minweights[w] = minweights[w-1] + weights[varsM1[w-1]];
3497  minweightslen = nvarsM1 + 1;
3498 
3499  /* gets sum of weights of variables fixed to one, i.e. sum of weights of variables in M_2 */
3500  fixedonesweight = 0;
3501  for( j = 0; j < nvarsM2; j++ )
3502  fixedonesweight += weights[varsM2[j]];
3503  assert(fixedonesweight >= 0);
3504 
3505  /* initializes right hand side of lifted valid inequality */
3506  *liftrhs = alpha0;
3507 
3508  /* sequentially up-lifts all variables in F: */
3509  for( j = 0; j < nvarsF; j++ )
3510  {
3511  SCIP_Longint weight;
3512  int liftvar;
3513  int liftcoef;
3514  int z;
3515 
3516  liftvar = varsF[j];
3517  weight = weights[liftvar];
3518  assert(liftvar >= 0 && liftvar < nvars);
3519  assert(SCIPisFeasGT(scip, solvals[liftvar], 0.0));
3520  assert(weight > 0);
3521 
3522  /* knapsack problem is infeasible:
3523  * sets z = 0
3524  */
3525  if( capacity - fixedonesweight - weight < 0 )
3526  {
3527  z = 0;
3528  }
3529  /* knapsack problem is feasible:
3530  * sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i} } = liftrhs,
3531  * if minweights_i[liftrhs] <= a_0 - fixedonesweight - a_{j_i}
3532  */
3533  else if( minweights[*liftrhs] <= capacity - fixedonesweight - weight )
3534  {
3535  z = *liftrhs;
3536  }
3537  /* knapsack problem is feasible:
3538  * uses binary search to find z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i} }
3539  */
3540  else
3541  {
3542  int left;
3543  int right;
3544  int middle;
3545 
3546  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - fixedonesweight - weight);
3547  left = 0;
3548  right = (*liftrhs) + 1;
3549  while( left < right - 1 )
3550  {
3551  middle = (left + right) / 2;
3552  assert(0 <= middle && middle < minweightslen);
3553  if( minweights[middle] <= capacity - fixedonesweight - weight )
3554  left = middle;
3555  else
3556  right = middle;
3557  }
3558  assert(left == right - 1);
3559  assert(0 <= left && left < minweightslen);
3560  assert(minweights[left] <= capacity - fixedonesweight - weight );
3561  assert(left == minweightslen - 1 || minweights[left+1] > capacity - fixedonesweight - weight);
3562 
3563  /* now z = left */
3564  z = left;
3565  assert(z <= *liftrhs);
3566  }
3567 
3568  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
3569  liftcoef = (*liftrhs) - z;
3570  liftcoefs[liftvar] = liftcoef;
3571  assert(liftcoef >= 0 && liftcoef <= (*liftrhs) + 1);
3572 
3573  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
3574  if( liftcoef == 0 )
3575  continue;
3576 
3577  /* updates activity of current valid inequality */
3578  (*cutact) += liftcoef * solvals[liftvar];
3579 
3580  /* enlarges current minweight table:
3581  * from minweightlen = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 entries
3582  * to |M1| + sum_{k=1}^{i } alpha_{j_k} + 1 entries
3583  * and sets minweights_i[w] = infinity for
3584  * w = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 , ... , |M1| + sum_{k=1}^{i} alpha_{j_k}
3585  */
3586  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + liftcoef) );
3587 
3588  /* updates minweight table: minweight_i+1[w] =
3589  * min{ minweights_i[w], a_{j_i}}, if w < alpha_j_i
3590  * min{ minweights_i[w], minweights_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
3591  */
3592  for( w = minweightslen - 1; w >= 0; w-- )
3593  {
3594  SCIP_Longint min;
3595  if( w < liftcoef )
3596  {
3597  min = MIN(minweights[w], weight);
3598  minweights[w] = min;
3599  }
3600  else
3601  {
3602  assert(w >= liftcoef);
3603  min = MIN(minweights[w], minweights[w - liftcoef] + weight);
3604  minweights[w] = min;
3605  }
3606  }
3607  }
3608  assert(minweights[0] == 0);
3609 
3610  /* sequentially down-lifts all variables in M_2: */
3611  for( j = 0; j < nvarsM2; j++ )
3612  {
3613  SCIP_Longint weight;
3614  int liftvar;
3615  int liftcoef;
3616  int left;
3617  int right;
3618  int middle;
3619  int z;
3620 
3621  liftvar = varsM2[j];
3622  weight = weights[liftvar];
3623  assert(SCIPisFeasEQ(scip, solvals[liftvar], 1.0));
3624  assert(liftvar >= 0 && liftvar < nvars);
3625  assert(weight > 0);
3626 
3627  /* uses binary search to find
3628  * z = max { w : 0 <= w <= |M_1| + sum_{k=1}^{i-1} alpha_{j_k}, minweights_[w] <= a_0 - fixedonesweight + a_{j_i}}
3629  */
3630  left = 0;
3631  right = minweightslen;
3632  while( left < right - 1 )
3633  {
3634  middle = (left + right) / 2;
3635  assert(0 <= middle && middle < minweightslen);
3636  if( minweights[middle] <= capacity - fixedonesweight + weight )
3637  left = middle;
3638  else
3639  right = middle;
3640  }
3641  assert(left == right - 1);
3642  assert(0 <= left && left < minweightslen);
3643  assert(minweights[left] <= capacity - fixedonesweight + weight );
3644  assert(left == minweightslen - 1 || minweights[left+1] > capacity - fixedonesweight + weight);
3645 
3646  /* now z = left */
3647  z = left;
3648  assert(z >= *liftrhs);
3649 
3650  /* calculates lifting coefficients alpha_{j_i} = z - liftrhs */
3651  liftcoef = z - (*liftrhs);
3652  liftcoefs[liftvar] = liftcoef;
3653  assert(liftcoef >= 0);
3654 
3655  /* updates sum of weights of variables fixed to one */
3656  fixedonesweight -= weight;
3657 
3658  /* updates right-hand side of current valid inequality */
3659  (*liftrhs) += liftcoef;
3660  assert(*liftrhs >= alpha0);
3661 
3662  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
3663  if( liftcoef == 0 )
3664  continue;
3665 
3666  /* updates activity of current valid inequality */
3667  (*cutact) += liftcoef * solvals[liftvar];
3668 
3669  /* enlarges current minweight table:
3670  * from minweightlen = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 entries
3671  * to |M1| + sum_{k=1}^{i } alpha_{j_k} + 1 entries
3672  * and sets minweights_i[w] = infinity for
3673  * w = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 , ... , |M1| + sum_{k=1}^{i} alpha_{j_k}
3674  */
3675  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + liftcoef) );
3676 
3677  /* updates minweight table: minweight_i+1[w] =
3678  * min{ minweights_i[w], a_{j_i}}, if w < alpha_j_i
3679  * min{ minweights_i[w], minweights_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
3680  */
3681  for( w = minweightslen - 1; w >= 0; w-- )
3682  {
3683  SCIP_Longint min;
3684  if( w < liftcoef )
3685  {
3686  min = MIN(minweights[w], weight);
3687  minweights[w] = min;
3688  }
3689  else
3690  {
3691  assert(w >= liftcoef);
3692  min = MIN(minweights[w], minweights[w - liftcoef] + weight);
3693  minweights[w] = min;
3694  }
3695  }
3696  }
3697  assert(fixedonesweight == 0);
3698  assert(*liftrhs >= alpha0);
3699 
3700  /* sequentially up-lifts all variables in R: */
3701  for( j = 0; j < nvarsR; j++ )
3702  {
3703  SCIP_Longint weight;
3704  int liftvar;
3705  int liftcoef;
3706  int z;
3707 
3708  liftvar = varsR[j];
3709  weight = weights[liftvar];
3710  assert(liftvar >= 0 && liftvar < nvars);
3711  assert(SCIPisFeasEQ(scip, solvals[liftvar], 0.0));
3712  assert(weight > 0);
3713  assert(capacity - weight >= 0);
3714  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - weight);
3715 
3716  /* sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} } = liftrhs,
3717  * if minweights_i[liftrhs] <= a_0 - a_{j_i}
3718  */
3719  if( minweights[*liftrhs] <= capacity - weight )
3720  {
3721  z = *liftrhs;
3722  }
3723  /* uses binary search to find z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} }
3724  */
3725  else
3726  {
3727  int left;
3728  int right;
3729  int middle;
3730 
3731  left = 0;
3732  right = (*liftrhs) + 1;
3733  while( left < right - 1)
3734  {
3735  middle = (left + right) / 2;
3736  assert(0 <= middle && middle < minweightslen);
3737  if( minweights[middle] <= capacity - weight )
3738  left = middle;
3739  else
3740  right = middle;
3741  }
3742  assert(left == right - 1);
3743  assert(0 <= left && left < minweightslen);
3744  assert(minweights[left] <= capacity - weight );
3745  assert(left == minweightslen - 1 || minweights[left+1] > capacity - weight);
3746 
3747  /* now z = left */
3748  z = left;
3749  assert(z <= *liftrhs);
3750  }
3751 
3752  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
3753  liftcoef = (*liftrhs) - z;
3754  liftcoefs[liftvar] = liftcoef;
3755  assert(liftcoef >= 0 && liftcoef <= *liftrhs);
3756 
3757  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
3758  if( liftcoef == 0 )
3759  continue;
3760 
3761  /* updates activity of current valid inequality */
3762  (*cutact) += liftcoef * solvals[liftvar];
3763 
3764  /* updates minweight table: minweight_i+1[w] =
3765  * min{ minweight_i[w], a_{j_i}}, if w < alpha_j_i
3766  * min{ minweight_i[w], minweight_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
3767  */
3768  for( w = *liftrhs; w >= 0; w-- )
3769  {
3770  SCIP_Longint min;
3771  if( w < liftcoef )
3772  {
3773  min = MIN(minweights[w], weight);
3774  minweights[w] = min;
3775  }
3776  else
3777  {
3778  assert(w >= liftcoef);
3779  min = MIN(minweights[w], minweights[w - liftcoef] + weight);
3780  minweights[w] = min;
3781  }
3782  }
3783  }
3784 
3785  /* frees temporary memory */
3786  SCIPfreeBufferArray(scip, &sortkeys);
3787  SCIPfreeBufferArray(scip, &minweights);
3788 
3789  return SCIP_OKAY;
3790 }
3791 
3792 /** adds two minweight values in a safe way, i.e,, ensures no overflow */
3793 static
3795  SCIP_Longint val1, /**< first value to add */
3796  SCIP_Longint val2 /**< second value to add */
3797  )
3798 {
3799  assert(val1 >= 0);
3800  assert(val2 >= 0);
3801 
3802  if( val1 >= SCIP_LONGINT_MAX || val2 >= SCIP_LONGINT_MAX )
3803  return SCIP_LONGINT_MAX;
3804  else
3805  {
3806  assert(val1 <= SCIP_LONGINT_MAX - val2);
3807  return (val1 + val2);
3808  }
3809 }
3810 
3811 /** computes minweights table for lifting with GUBs by combining unfished and fished tables */
3812 static
3814  SCIP_Longint* minweights, /**< minweight table to compute */
3815  SCIP_Longint* finished, /**< given finished table */
3816  SCIP_Longint* unfinished, /**< given unfinished table */
3817  int minweightslen /**< length of minweight, finished, and unfinished tables */
3818  )
3819 {
3820  int w1;
3821  int w2;
3822 
3823  /* minweights_i[w] = min{finished_i[w1] + unfinished_i[w2] : w1>=0, w2>=0, w1+w2=w};
3824  * note that finished and unfished arrays sorted by non-decreasing weight
3825  */
3826 
3827  /* initialize minweight with w2 = 0 */
3828  w2 = 0;
3829  assert(unfinished[w2] == 0);
3830  for( w1 = 0; w1 < minweightslen; w1++ )
3831  minweights[w1] = finished[w1];
3832 
3833  /* consider w2 = 1, ..., minweightslen-1 */
3834  for( w2 = 1; w2 < minweightslen; w2++ )
3835  {
3836  if( unfinished[w2] >= SCIP_LONGINT_MAX )
3837  break;
3838 
3839  for( w1 = 0; w1 < minweightslen - w2; w1++ )
3840  {
3841  SCIP_Longint temp;
3842 
3843  temp = safeAddMinweightsGUB(finished[w1], unfinished[w2]);
3844  if( temp <= minweights[w1+w2] )
3845  minweights[w1+w2] = temp;
3846  }
3847  }
3848 }
3849 
3850 /** lifts given inequality
3851  * sum_{j in C_1} x_j <= alpha_0
3852  * valid for
3853  * 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;
3854  * sum_{j in Q_i} x_j <= 1, forall i in I }
3855  * to a valid inequality
3856  * 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
3857  * <= alpha_0 + sum_{j in C_2} alpha_j
3858  * for
3859  * 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 };
3860  * uses sequential up-lifting for the variables in GUB constraints in gubconsGFC1,
3861  * sequential down-lifting for the variables in GUB constraints in gubconsGC2, and
3862  * sequential up-lifting for the variabels in GUB constraints in gubconsGR.
3863  */
3864 static
3866  SCIP* scip, /**< SCIP data structure */
3867  SCIP_GUBSET* gubset, /**< GUB set data structure */
3868  SCIP_VAR** vars, /**< variables in knapsack constraint */
3869  int ngubconscapexceed, /**< number of GUBs with only capacity exceeding variables */
3870  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
3871  SCIP_Longint capacity, /**< capacity of knapsack */
3872  SCIP_Real* solvals, /**< solution values of all knapsack variables */
3873  int* gubconsGC1, /**< GUBs in GC1(GNC1+GOC1) */
3874  int* gubconsGC2, /**< GUBs in GC2 */
3875  int* gubconsGFC1, /**< GUBs in GFC1(GNC1+GF) */
3876  int* gubconsGR, /**< GUBs in GR */
3877  int ngubconsGC1, /**< number of GUBs in GC1(GNC1+GOC1) */
3878  int ngubconsGC2, /**< number of GUBs in GC2 */
3879  int ngubconsGFC1, /**< number of GUBs in GFC1(GNC1+GF) */
3880  int ngubconsGR, /**< number of GUBs in GR */
3881  int alpha0, /**< rights hand side of given valid inequality */
3882  int* liftcoefs, /**< pointer to store lifting coefficient of vars in knapsack constraint */
3883  SCIP_Real* cutact, /**< pointer to store activity of lifted valid inequality */
3884  int* liftrhs, /**< pointer to store right hand side of the lifted valid inequality */
3885  int maxgubvarssize /**< maximal size of GUB constraints */
3886  )
3887 {
3888  SCIP_Longint* minweights;
3889  SCIP_Longint* finished;
3890  SCIP_Longint* unfinished;
3891  int* gubconsGOC1;
3892  int* gubconsGNC1;
3893  int* liftgubvars;
3894  SCIP_Longint fixedonesweight;
3895  SCIP_Longint weight;
3896  SCIP_Longint weightdiff1;
3897  SCIP_Longint weightdiff2;
3898  SCIP_Longint min;
3899  int minweightssize;
3900  int minweightslen;
3901  int nvars;
3902  int varidx;
3903  int liftgubconsidx;
3904  int liftvar;
3905  int sumliftcoef;
3906  int liftcoef;
3907  int ngubconsGOC1;
3908  int ngubconsGNC1;
3909  int left;
3910  int right;
3911  int middle;
3912  int nliftgubvars;
3913  int tmplen;
3914  int tmpsize;
3915  int j;
3916  int k;
3917  int w;
3918  int z;
3919 #ifndef NDEBUG
3920  int ngubconss;
3921  int nliftgubC1;
3922 
3923  assert(gubset != NULL);
3924  ngubconss = gubset->ngubconss;
3925 #else
3926  assert(gubset != NULL);
3927 #endif
3928 
3929  nvars = gubset->nvars;
3930 
3931  assert(scip != NULL);
3932  assert(vars != NULL);
3933  assert(nvars >= 0);
3934  assert(weights != NULL);
3935  assert(capacity >= 0);
3936  assert(solvals != NULL);
3937  assert(gubconsGC1 != NULL);
3938  assert(gubconsGC2 != NULL);
3939  assert(gubconsGFC1 != NULL);
3940  assert(gubconsGR != NULL);
3941  assert(ngubconsGC1 >= 0 && ngubconsGC1 <= ngubconss - ngubconscapexceed);
3942  assert(ngubconsGC2 >= 0 && ngubconsGC2 <= ngubconss - ngubconscapexceed);
3943  assert(ngubconsGFC1 >= 0 && ngubconsGFC1 <= ngubconss - ngubconscapexceed);
3944  assert(ngubconsGR >= 0 && ngubconsGR <= ngubconss - ngubconscapexceed);
3945  assert(alpha0 >= 0);
3946  assert(liftcoefs != NULL);
3947  assert(cutact != NULL);
3948  assert(liftrhs != NULL);
3949 
3950  minweightssize = ngubconsGC1+1;
3951 
3952  /* allocates temporary memory */
3953  SCIP_CALL( SCIPallocBufferArray(scip, &liftgubvars, maxgubvarssize) );
3954  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGOC1, ngubconsGC1) );
3955  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGNC1, ngubconsGC1) );
3956  SCIP_CALL( SCIPallocBufferArray(scip, &minweights, minweightssize) );
3957  SCIP_CALL( SCIPallocBufferArray(scip, &finished, minweightssize) );
3958  SCIP_CALL( SCIPallocBufferArray(scip, &unfinished, minweightssize) );
3959 
3960  /* initializes data structures */
3961  BMSclearMemoryArray(liftcoefs, nvars);
3962  *cutact = 0.0;
3963 
3964  /* gets GOC1 and GNC1 GUBs, sets lifting coefficient of variables in C1 and calculates activity of the current
3965  * valid inequality
3966  */
3967  ngubconsGOC1 = 0;
3968  ngubconsGNC1 = 0;
3969  for( j = 0; j < ngubconsGC1; j++ )
3970  {
3971  if( gubset->gubconsstatus[gubconsGC1[j]] == GUBCONSSTATUS_BELONGSTOSET_GOC1 )
3972  {
3973  gubconsGOC1[ngubconsGOC1] = gubconsGC1[j];
3974  ngubconsGOC1++;
3975  }
3976  else
3977  {
3978  assert(gubset->gubconsstatus[gubconsGC1[j]] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3979  gubconsGNC1[ngubconsGNC1] = gubconsGC1[j];
3980  ngubconsGNC1++;
3981  }
3982  for( k = 0; k < gubset->gubconss[gubconsGC1[j]]->ngubvars
3983  && gubset->gubconss[gubconsGC1[j]]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
3984  {
3985  varidx = gubset->gubconss[gubconsGC1[j]]->gubvars[k];
3986  assert(varidx >= 0 && varidx < nvars);
3987  assert(liftcoefs[varidx] == 0);
3988 
3989  liftcoefs[varidx] = 1;
3990  (*cutact) += solvals[varidx];
3991  }
3992  assert(k >= 1);
3993  }
3994  assert(ngubconsGOC1 + ngubconsGFC1 + ngubconsGC2 + ngubconsGR == ngubconss - ngubconscapexceed);
3995  assert(ngubconsGOC1 + ngubconsGNC1 == ngubconsGC1);
3996 
3997  /* initialize the minweight tables, defined as: for i = 1,...,m with m = |I| and w = 0,...,|gubconsGC1|;
3998  * - finished_i[w] =
3999  * min sum_{k = 1,2,...,i-1} sum_{j in Q_k} a_j x_j
4000  * s.t. sum_{k = 1,2,...,i-1} sum_{j in Q_k} alpha_j x_j >= w
4001  * sum_{j in Q_k} x_j <= 1
4002  * x_j in {0,1} forall j in Q_k forall k = 1,2,...,i-1,
4003  * - unfinished_i[w] =
4004  * min sum_{k = i+1,...,m} sum_{j in Q_k && j in C1} a_j x_j
4005  * s.t. sum_{k = i+1,...,m} sum_{j in Q_k && j in C1} x_j >= w
4006  * sum_{j in Q_k} x_j <= 1
4007  * x_j in {0,1} forall j in Q_k forall k = 1,2,...,i-1,
4008  * - minweights_i[w] = min{finished_i[w1] + unfinished_i[w2] : w1>=0, w2>=0, w1+w2=w};
4009  */
4010 
4011  /* initialize finished table; note that variables in GOC1 GUBs (includes C1 and capacity exceeding variables)
4012  * are sorted s.t. C1 variables come first and are sorted by non-decreasing weight.
4013  * 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
4014  * comes from the first variable in the GUB
4015  */
4016  assert(ngubconsGOC1 <= ngubconsGC1);
4017  finished[0] = 0;
4018  for( w = 1; w <= ngubconsGOC1; w++ )
4019  {
4020  liftgubconsidx = gubconsGOC1[w-1];
4021 
4022  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GOC1);
4023  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4024 
4025  varidx = gubset->gubconss[liftgubconsidx]->gubvars[0];
4026 
4027  assert(varidx >= 0 && varidx < nvars);
4028  assert(liftcoefs[varidx] == 1);
4029 
4030  min = weights[varidx];
4031  finished[w] = finished[w-1] + min;
4032 
4033 #ifndef NDEBUG
4034  for( k = 1; k < gubset->gubconss[liftgubconsidx]->ngubvars
4035  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4036  {
4037  varidx = gubset->gubconss[liftgubconsidx]->gubvars[k];
4038  assert(varidx >= 0 && varidx < nvars);
4039  assert(liftcoefs[varidx] == 1);
4040  assert(weights[varidx] >= min);
4041  }
4042 #endif
4043  }
4044  for( w = ngubconsGOC1+1; w <= ngubconsGC1; w++ )
4045  finished[w] = SCIP_LONGINT_MAX;
4046 
4047  /* initialize unfinished table; note that variables in GNC1 GUBs
4048  * are sorted s.t. C1 variables come first and are sorted by non-decreasing weight.
4049  * 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
4050  * comes from the first variable in the GUB
4051  */
4052  assert(ngubconsGNC1 <= ngubconsGC1);
4053  unfinished[0] = 0;
4054  for( w = 1; w <= ngubconsGNC1; w++ )
4055  {
4056  liftgubconsidx = gubconsGNC1[w-1];
4057 
4058  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
4059  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4060 
4061  varidx = gubset->gubconss[liftgubconsidx]->gubvars[0];
4062 
4063  assert(varidx >= 0 && varidx < nvars);
4064  assert(liftcoefs[varidx] == 1);
4065 
4066  min = weights[varidx];
4067  unfinished[w] = unfinished[w-1] + min;
4068 
4069 #ifndef NDEBUG
4070  for( k = 1; k < gubset->gubconss[liftgubconsidx]->ngubvars
4071  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4072  {
4073  varidx = gubset->gubconss[liftgubconsidx]->gubvars[k];
4074  assert(varidx >= 0 && varidx < nvars);
4075  assert(liftcoefs[varidx] == 1);
4076  assert(weights[varidx] >= min );
4077  }
4078 #endif
4079  }
4080  for( w = ngubconsGNC1 + 1; w <= ngubconsGC1; w++ )
4081  unfinished[w] = SCIP_LONGINT_MAX;
4082 
4083  /* initialize minweights table; note that variables in GC1 GUBs
4084  * are sorted s.t. C1 variables come first and are sorted by non-decreasing weight.
4085  * we can directly initialize minweights instead of computing it from finished and unfinished (which would be more time
4086  * consuming) because is it has to be build using weights from C1 only.
4087  */
4088  assert(ngubconsGOC1 + ngubconsGNC1 == ngubconsGC1);
4089  minweights[0] = 0;
4090  for( w = 1; w <= ngubconsGC1; w++ )
4091  {
4092  liftgubconsidx = gubconsGC1[w-1];
4093 
4094  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GOC1
4095  || gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
4096  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4097 
4098  varidx = gubset->gubconss[liftgubconsidx]->gubvars[0];
4099 
4100  assert(varidx >= 0 && varidx < nvars);
4101  assert(liftcoefs[varidx] == 1);
4102 
4103  min = weights[varidx];
4104  minweights[w] = minweights[w-1] + min;
4105 
4106 #ifndef NDEBUG
4107  for( k = 1; k < gubset->gubconss[liftgubconsidx]->ngubvars
4108  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4109  {
4110  varidx = gubset->gubconss[liftgubconsidx]->gubvars[k];
4111  assert(varidx >= 0 && varidx < nvars);
4112  assert(liftcoefs[varidx] == 1);
4113  assert(weights[varidx] >= min);
4114  }
4115 #endif
4116  }
4117  minweightslen = ngubconsGC1 + 1;
4118 
4119  /* gets sum of weights of variables fixed to one, i.e. sum of weights of C2 variables GC2 GUBs */
4120  fixedonesweight = 0;
4121  for( j = 0; j < ngubconsGC2; j++ )
4122  {
4123  varidx = gubset->gubconss[gubconsGC2[j]]->gubvars[0];
4124 
4125  assert(gubset->gubconss[gubconsGC2[j]]->ngubvars == 1);
4126  assert(varidx >= 0 && varidx < nvars);
4127  assert(gubset->gubconss[gubconsGC2[j]]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C2);
4128 
4129  fixedonesweight += weights[varidx];
4130  }
4131  assert(fixedonesweight >= 0);
4132 
4133  /* initializes right hand side of lifted valid inequality */
4134  *liftrhs = alpha0;
4135 
4136  /* sequentially up-lifts all variables in GFC1 GUBs */
4137  for( j = 0; j < ngubconsGFC1; j++ )
4138  {
4139  liftgubconsidx = gubconsGFC1[j];
4140  assert(liftgubconsidx >= 0 && liftgubconsidx < ngubconss);
4141 
4142  /* GNC1 GUB: update unfinished table (remove current GUB, i.e., remove min weight of C1 vars in GUB) and
4143  * compute minweight table via updated unfinished table and aleady upto date finished table;
4144  */
4145  k = 0;
4146  if( gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1 )
4147  {
4148  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
4149  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4150  assert(ngubconsGNC1 > 0);
4151 
4152  /* get number of C1 variables of current GNC1 GUB and put them into array of variables in GUB that
4153  * are considered for the lifting, i.e., not capacity exceeding
4154  */
4155  for( ; k < gubset->gubconss[liftgubconsidx]->ngubvars
4156  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4157  liftgubvars[k] = gubset->gubconss[liftgubconsidx]->gubvars[k];
4158  assert(k >= 1);
4159 
4160  /* update unfinished table by removing current GNC1 GUB, i.e, remove C1 variable with minimal weight
4161  * unfinished[w] = MAX{unfinished[w], unfinished[w+1] - weight}, "weight" is the minimal weight of current GUB
4162  */
4163  weight = weights[liftgubvars[0]];
4164 
4165  weightdiff2 = unfinished[ngubconsGNC1] - weight;
4166  unfinished[ngubconsGNC1] = SCIP_LONGINT_MAX;
4167  for( w = ngubconsGNC1-1; w >= 1; w-- )
4168  {
4169  weightdiff1 = weightdiff2;
4170  weightdiff2 = unfinished[w] - weight;
4171 
4172  if( unfinished[w] < weightdiff1 )
4173  unfinished[w] = weightdiff1;
4174  else
4175  break;
4176  }
4177  ngubconsGNC1--;
4178 
4179  /* computes minweights table by combining unfished and fished tables */
4180  computeMinweightsGUB(minweights, finished, unfinished, minweightslen);
4181  assert(minweights[0] == 0);
4182  }
4183  /* GF GUB: no update of unfinished table (and minweight table) required because GF GUBs have no C1 variables and
4184  * are therefore not in the unfinished table
4185  */
4186  else
4187  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF);
4188 
4189 #ifndef NDEBUG
4190  nliftgubC1 = k;
4191 #endif
4192  nliftgubvars = k;
4193  sumliftcoef = 0;
4194 
4195  /* compute lifting coefficient of F and R variables in GNC1 and GF GUBs (C1 vars have already liftcoef 1) */
4196  for( ; k < gubset->gubconss[liftgubconsidx]->ngubvars; k++ )
4197  {
4198  if( gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_F
4199  || gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_R )
4200  {
4201  liftvar = gubset->gubconss[liftgubconsidx]->gubvars[k];
4202  weight = weights[liftvar];
4203  assert(weight > 0);
4204  assert(liftvar >= 0 && liftvar < nvars);
4205  assert(capacity - weight >= 0);
4206 
4207  /* put variable into array of variables in GUB that are considered for the lifting,
4208  * i.e., not capacity exceeding
4209  */
4210  liftgubvars[nliftgubvars] = liftvar;
4211  nliftgubvars++;
4212 
4213  /* knapsack problem is infeasible:
4214  * sets z = 0
4215  */
4216  if( capacity - fixedonesweight - weight < 0 )
4217  {
4218  z = 0;
4219  }
4220  /* knapsack problem is feasible:
4221  * sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i} } = liftrhs,
4222  * if minweights_i[liftrhs] <= a_0 - fixedonesweight - a_{j_i}
4223  */
4224  else if( minweights[*liftrhs] <= capacity - fixedonesweight - weight )
4225  {
4226  z = *liftrhs;
4227  }
4228  /* knapsack problem is feasible:
4229  * binary search to find z = max {w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i}}
4230  */
4231  else
4232  {
4233  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - fixedonesweight - weight);
4234  left = 0;
4235  right = (*liftrhs) + 1;
4236  while( left < right - 1 )
4237  {
4238  middle = (left + right) / 2;
4239  assert(0 <= middle && middle < minweightslen);
4240  if( minweights[middle] <= capacity - fixedonesweight - weight )
4241  left = middle;
4242  else
4243  right = middle;
4244  }
4245  assert(left == right - 1);
4246  assert(0 <= left && left < minweightslen);
4247  assert(minweights[left] <= capacity - fixedonesweight - weight);
4248  assert(left == minweightslen - 1 || minweights[left+1] > capacity - fixedonesweight - weight);
4249 
4250  /* now z = left */
4251  z = left;
4252  assert(z <= *liftrhs);
4253  }
4254 
4255  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
4256  liftcoef = (*liftrhs) - z;
4257  liftcoefs[liftvar] = liftcoef;
4258  assert(liftcoef >= 0 && liftcoef <= (*liftrhs) + 1);
4259 
4260  /* updates activity of current valid inequality */
4261  (*cutact) += liftcoef * solvals[liftvar];
4262 
4263  /* updates sum of all lifting coefficients in GUB */
4264  sumliftcoef += liftcoefs[liftvar];
4265  }
4266  else
4267  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_CAPACITYEXCEEDED);
4268  }
4269  /* at least one variable is in F or R (j = number of C1 variables in current GUB) */
4270  assert(nliftgubvars > nliftgubC1);
4271 
4272  /* activity of current valid inequality will not change if (sum of alpha_{j_i} in GUB) = 0
4273  * and finished and minweight table can be updated easily as only C1 variables need to be considered;
4274  * not needed for GF GUBs
4275  */
4276  if( sumliftcoef == 0 )
4277  {
4278  if( gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1 )
4279  {
4280  weight = weights[liftgubvars[0]];
4281  /* update finished table and minweights table by applying special case of
4282  * finished[w] = MIN{finished[w], finished[w-1] + weight}, "weight" is the minimal weight of current GUB
4283  * minweights[w] = MIN{minweights[w], minweights[w-1] + weight}, "weight" is the minimal weight of current GUB
4284  */
4285  for( w = minweightslen-1; w >= 1; w-- )
4286  {
4287  SCIP_Longint tmpval;
4288 
4289  tmpval = safeAddMinweightsGUB(finished[w-1], weight);
4290  finished[w] = MIN(finished[w], tmpval);
4291 
4292  tmpval = safeAddMinweightsGUB(minweights[w-1], weight);
4293  minweights[w] = MIN(minweights[w], tmpval);
4294  }
4295  }
4296  else
4297  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF);
4298 
4299  continue;
4300  }
4301 
4302  /* enlarges current minweights tables(finished, unfinished, minweights):
4303  * from minweightlen = |gubconsGC1| + sum_{k=1,2,...,i-1}sum_{j in Q_k} alpha_j + 1 entries
4304  * to |gubconsGC1| + sum_{k=1,2,...,i }sum_{j in Q_k} alpha_j + 1 entries
4305  * and sets minweights_i[w] = infinity for
4306  * 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
4307  */
4308  tmplen = minweightslen; /* will be updated in enlargeMinweights() */
4309  tmpsize = minweightssize;
4310  SCIP_CALL( enlargeMinweights(scip, &unfinished, &tmplen, &tmpsize, tmplen + sumliftcoef) );
4311  tmplen = minweightslen;
4312  tmpsize = minweightssize;
4313  SCIP_CALL( enlargeMinweights(scip, &finished, &tmplen, &tmpsize, tmplen + sumliftcoef) );
4314  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + sumliftcoef) );
4315 
4316  /* update finished table and minweight table;
4317  * note that instead of computing minweight table from updated finished and updated unfinished table again
4318  * (for the lifting coefficient, we had to update unfinished table and compute minweight table), we here
4319  * only need to update the minweight table and the updated finished in the same way (i.e., computing for minweight
4320  * not needed because only finished table changed at this point and the change was "adding" one weight)
4321  *
4322  * update formular for minweight table is: minweight_i+1[w] =
4323  * min{ minweights_i[w], min{ minweights_i[w - alpha_k]^{+} + a_k : k in GUB_j_i } }
4324  * formular for finished table has the same pattern.
4325  */
4326  for( w = minweightslen-1; w >= 0; w-- )
4327  {
4328  SCIP_Longint minminweight;
4329  SCIP_Longint minfinished;
4330 
4331  for( k = 0; k < nliftgubvars; k++ )
4332  {
4333  liftcoef = liftcoefs[liftgubvars[k]];
4334  weight = weights[liftgubvars[k]];
4335 
4336  if( w < liftcoef )
4337  {
4338  minfinished = MIN(finished[w], weight);
4339  minminweight = MIN(minweights[w], weight);
4340 
4341  finished[w] = minfinished;
4342  minweights[w] = minminweight;
4343  }
4344  else
4345  {
4346  SCIP_Longint tmpval;
4347 
4348  assert(w >= liftcoef);
4349 
4350  tmpval = safeAddMinweightsGUB(finished[w-liftcoef], weight);
4351  minfinished = MIN(finished[w], tmpval);
4352 
4353  tmpval = safeAddMinweightsGUB(minweights[w-liftcoef], weight);
4354  minminweight = MIN(minweights[w], tmpval);
4355 
4356  finished[w] = minfinished;
4357  minweights[w] = minminweight;
4358  }
4359  }
4360  }
4361  assert(minweights[0] == 0);
4362  }
4363  assert(ngubconsGNC1 == 0);
4364 
4365  /* note: now the unfinished table no longer exists, i.e., it is "0, MAX, MAX, ..." and minweight equals to finished;
4366  * therefore, only work with minweight table from here on
4367  */
4368 
4369  /* sequentially down-lifts C2 variables contained in trivial GC2 GUBs */
4370  for( j = 0; j < ngubconsGC2; j++ )
4371  {
4372  liftgubconsidx = gubconsGC2[j];
4373 
4374  assert(liftgubconsidx >=0 && liftgubconsidx < ngubconss);
4375  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GC2);
4376  assert(gubset->gubconss[liftgubconsidx]->ngubvars == 1);
4377  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C2);
4378 
4379  liftvar = gubset->gubconss[liftgubconsidx]->gubvars[0]; /* C2 GUBs contain only one variable */
4380  weight = weights[liftvar];
4381 
4382  assert(liftvar >= 0 && liftvar < nvars);
4383  assert(SCIPisFeasEQ(scip, solvals[liftvar], 1.0));
4384  assert(weight > 0);
4385 
4386  /* uses binary search to find
4387  * z = max { w : 0 <= w <= |C_1| + sum_{k=1}^{i-1} alpha_{j_k}, minweights_[w] <= a_0 - fixedonesweight + a_{j_i}}
4388  */
4389  left = 0;
4390  right = minweightslen;
4391  while( left < right - 1 )
4392  {
4393  middle = (left + right) / 2;
4394  assert(0 <= middle && middle < minweightslen);
4395  if( minweights[middle] <= capacity - fixedonesweight + weight )
4396  left = middle;
4397  else
4398  right = middle;
4399  }
4400  assert(left == right - 1);
4401  assert(0 <= left && left < minweightslen);
4402  assert(minweights[left] <= capacity - fixedonesweight + weight);
4403  assert(left == minweightslen - 1 || minweights[left + 1] > capacity - fixedonesweight + weight);
4404 
4405  /* now z = left */
4406  z = left;
4407  assert(z >= *liftrhs);
4408 
4409  /* calculates lifting coefficients alpha_{j_i} = z - liftrhs */
4410  liftcoef = z - (*liftrhs);
4411  liftcoefs[liftvar] = liftcoef;
4412  assert(liftcoef >= 0);
4413 
4414  /* updates sum of weights of variables fixed to one */
4415  fixedonesweight -= weight;
4416 
4417  /* updates right-hand side of current valid inequality */
4418  (*liftrhs) += liftcoef;
4419  assert(*liftrhs >= alpha0);
4420 
4421  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
4422  if( liftcoef == 0 )
4423  continue;
4424 
4425  /* updates activity of current valid inequality */
4426  (*cutact) += liftcoef * solvals[liftvar];
4427 
4428  /* enlarges current minweight table:
4429  * from minweightlen = |gubconsGC1| + sum_{k=1,2,...,i-1}sum_{j in Q_k} alpha_j + 1 entries
4430  * to |gubconsGC1| + sum_{k=1,2,...,i }sum_{j in Q_k} alpha_j + 1 entries
4431  * and sets minweights_i[w] = infinity for
4432  * 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
4433  */
4434  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + liftcoef) );
4435 
4436  /* updates minweight table: minweight_i+1[w] =
4437  * min{ minweights_i[w], a_{j_i}}, if w < alpha_j_i
4438  * min{ minweights_i[w], minweights_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
4439  */
4440  for( w = minweightslen - 1; w >= 0; w-- )
4441  {
4442  if( w < liftcoef )
4443  {
4444  min = MIN(minweights[w], weight);
4445  minweights[w] = min;
4446  }
4447  else
4448  {
4449  SCIP_Longint tmpval;
4450 
4451  assert(w >= liftcoef);
4452 
4453  tmpval = safeAddMinweightsGUB(minweights[w-liftcoef], weight);
4454  min = MIN(minweights[w], tmpval);
4455  minweights[w] = min;
4456  }
4457  }
4458  }
4459  assert(fixedonesweight == 0);
4460  assert(*liftrhs >= alpha0);
4461 
4462  /* sequentially up-lifts variables in GUB constraints in GR GUBs */
4463  for( j = 0; j < ngubconsGR; j++ )
4464  {
4465  liftgubconsidx = gubconsGR[j];
4466 
4467  assert(liftgubconsidx >=0 && liftgubconsidx < ngubconss);
4468  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GR);
4469 
4470  sumliftcoef = 0;
4471  nliftgubvars = 0;
4472  for( k = 0; k < gubset->gubconss[liftgubconsidx]->ngubvars; k++ )
4473  {
4474  if(gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_R )
4475  {
4476  liftvar = gubset->gubconss[liftgubconsidx]->gubvars[k];
4477  weight = weights[liftvar];
4478  assert(weight > 0);
4479  assert(liftvar >= 0 && liftvar < nvars);
4480  assert(capacity - weight >= 0);
4481  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - weight);
4482 
4483  /* put variable into array of variables in GUB that are considered for the lifting,
4484  * i.e., not capacity exceeding
4485  */
4486  liftgubvars[nliftgubvars] = liftvar;
4487  nliftgubvars++;
4488 
4489  /* sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} } = liftrhs,
4490  * if minweights_i[liftrhs] <= a_0 - a_{j_i}
4491  */
4492  if( minweights[*liftrhs] <= capacity - weight )
4493  {
4494  z = *liftrhs;
4495  }
4496  /* uses binary search to find z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} }
4497  */
4498  else
4499  {
4500  left = 0;
4501  right = (*liftrhs) + 1;
4502  while( left < right - 1 )
4503  {
4504  middle = (left + right) / 2;
4505  assert(0 <= middle && middle < minweightslen);
4506  if( minweights[middle] <= capacity - weight )
4507  left = middle;
4508  else
4509  right = middle;
4510  }
4511  assert(left == right - 1);
4512  assert(0 <= left && left < minweightslen);
4513  assert(minweights[left] <= capacity - weight);
4514  assert(left == minweightslen - 1 || minweights[left + 1] > capacity - weight);
4515 
4516  /* now z = left */
4517  z = left;
4518  assert(z <= *liftrhs);
4519  }
4520  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
4521  liftcoef = (*liftrhs) - z;
4522  liftcoefs[liftvar] = liftcoef;
4523  assert(liftcoef >= 0 && liftcoef <= (*liftrhs) + 1);
4524 
4525  /* updates activity of current valid inequality */
4526  (*cutact) += liftcoef * solvals[liftvar];
4527 
4528  /* updates sum of all lifting coefficients in GUB */
4529  sumliftcoef += liftcoefs[liftvar];
4530  }
4531  else
4532  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_CAPACITYEXCEEDED);
4533  }
4534  assert(nliftgubvars >= 1); /* at least one variable is in R */
4535 
4536  /* minweight table and activity of current valid inequality will not change if (sum of alpha_{j_i} in GUB) = 0 */
4537  if( sumliftcoef == 0 )
4538  continue;
4539 
4540  /* updates minweight table: minweight_i+1[w] =
4541  * min{ minweights_i[w], min{ minweights_i[w - alpha_k]^{+} + a_k : k in GUB_j_i } }
4542  */
4543  for( w = *liftrhs; w >= 0; w-- )
4544  {
4545  for( k = 0; k < nliftgubvars; k++ )
4546  {
4547  liftcoef = liftcoefs[liftgubvars[k]];
4548  weight = weights[liftgubvars[k]];
4549 
4550  if( w < liftcoef )
4551  {
4552  min = MIN(minweights[w], weight);
4553  minweights[w] = min;
4554  }
4555  else
4556  {
4557  SCIP_Longint tmpval;
4558 
4559  assert(w >= liftcoef);
4560 
4561  tmpval = safeAddMinweightsGUB(minweights[w-liftcoef], weight);
4562  min = MIN(minweights[w], tmpval);
4563  minweights[w] = min;
4564  }
4565  }
4566  }
4567  assert(minweights[0] == 0);
4568  }
4569 
4570  /* frees temporary memory */
4571  SCIPfreeBufferArray(scip, &minweights);
4572  SCIPfreeBufferArray(scip, &finished);
4573  SCIPfreeBufferArray(scip, &unfinished);
4574  SCIPfreeBufferArray(scip, &liftgubvars);
4575  SCIPfreeBufferArray(scip, &gubconsGOC1 );
4576  SCIPfreeBufferArray(scip, &gubconsGNC1);
4577 
4578  return SCIP_OKAY;
4579 }
4580 
4581 /** lifts given minimal cover inequality
4582  * \f[
4583  * \sum_{j \in C} x_j \leq |C| - 1
4584  * \f]
4585  * valid for
4586  * \f[
4587  * S^0 = \{ x \in {0,1}^{|C|} : \sum_{j \in C} a_j x_j \leq a_0 \}
4588  * \f]
4589  * to a valid inequality
4590  * \f[
4591  * \sum_{j \in C} x_j + \sum_{j \in N \setminus C} \alpha_j x_j \leq |C| - 1
4592  * \f]
4593  * for
4594  * \f[
4595  * S = \{ x \in {0,1}^{|N|} : \sum_{j \in N} a_j x_j \leq a_0 \};
4596  * \f]
4597  * uses superadditive up-lifting for the variables in \f$N \setminus C\f$.
4598  */
4599 static
4601  SCIP* scip, /**< SCIP data structure */
4602  SCIP_VAR** vars, /**< variables in knapsack constraint */
4603  int nvars, /**< number of variables in knapsack constraint */
4604  int ntightened, /**< number of variables with tightened upper bound */
4605  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
4606  SCIP_Longint capacity, /**< capacity of knapsack */
4607  SCIP_Real* solvals, /**< solution values of all problem variables */
4608  int* covervars, /**< cover variables */
4609  int* noncovervars, /**< noncover variables */
4610  int ncovervars, /**< number of cover variables */
4611  int nnoncovervars, /**< number of noncover variables */
4612  SCIP_Longint coverweight, /**< weight of cover */
4613  SCIP_Real* liftcoefs, /**< pointer to store lifting coefficient of vars in knapsack constraint */
4614  SCIP_Real* cutact /**< pointer to store activity of lifted valid inequality */
4615  )
4616 {
4617  SCIP_Longint* maxweightsums;
4618  SCIP_Longint* intervalends;
4619  SCIP_Longint* rhos;
4620  SCIP_Real* sortkeys;
4621  SCIP_Longint lambda;
4622  int j;
4623  int h;
4624 
4625  assert(scip != NULL);
4626  assert(vars != NULL);
4627  assert(nvars >= 0);
4628  assert(weights != NULL);
4629  assert(capacity >= 0);
4630  assert(solvals != NULL);
4631  assert(covervars != NULL);
4632  assert(noncovervars != NULL);
4633  assert(ncovervars > 0 && ncovervars <= nvars);
4634  assert(nnoncovervars >= 0 && nnoncovervars <= nvars - ntightened);
4635  assert(ncovervars + nnoncovervars == nvars - ntightened);
4636  assert(liftcoefs != NULL);
4637  assert(cutact != NULL);
4638 
4639  /* allocates temporary memory */
4640  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, ncovervars) );
4641  SCIP_CALL( SCIPallocBufferArray(scip, &maxweightsums, ncovervars + 1) );
4642  SCIP_CALL( SCIPallocBufferArray(scip, &intervalends, ncovervars) );
4643  SCIP_CALL( SCIPallocBufferArray(scip, &rhos, ncovervars) );
4644 
4645  /* initializes data structures */
4646  BMSclearMemoryArray(liftcoefs, nvars);
4647  *cutact = 0.0;
4648 
4649  /* sets lifting coefficient of variables in C, sorts variables in C such that a_1 >= a_2 >= ... >= a_|C|
4650  * and calculates activity of current valid inequality
4651  */
4652  for( j = 0; j < ncovervars; j++ )
4653  {
4654  assert(liftcoefs[covervars[j]] == 0.0);
4655  liftcoefs[covervars[j]] = 1.0;
4656  sortkeys[j] = (SCIP_Real) weights[covervars[j]];
4657  (*cutact) += solvals[covervars[j]];
4658  }
4659  SCIPsortDownRealInt(sortkeys, covervars, ncovervars);
4660 
4661  /* calculates weight excess of cover C */
4662  lambda = coverweight - capacity;
4663  assert(lambda > 0);
4664 
4665  /* calculates A_h for h = 0,...,|C|, I_h for h = 1,...,|C| and rho_h for h = 1,...,|C| */
4666  maxweightsums[0] = 0;
4667  for( h = 1; h <= ncovervars; h++ )
4668  {
4669  maxweightsums[h] = maxweightsums[h-1] + weights[covervars[h-1]];
4670  intervalends[h-1] = maxweightsums[h] - lambda;
4671  rhos[h-1] = MAX(0, weights[covervars[h-1]] - weights[covervars[0]] + lambda);
4672  }
4673 
4674  /* sorts variables in N\C such that a_{j_1} <= a_{j_2} <= ... <= a_{j_t} */
4675  for( j = 0; j < nnoncovervars; j++ )
4676  sortkeys[j] = (SCIP_Real) (weights[noncovervars[j]]);
4677  SCIPsortRealInt(sortkeys, noncovervars, nnoncovervars);
4678 
4679  /* calculates lifting coefficient for all variables in N\C */
4680  h = 0;
4681  for( j = 0; j < nnoncovervars; j++ )
4682  {
4683  int liftvar;
4684  SCIP_Longint weight;
4685  SCIP_Real liftcoef;
4686 
4687  liftvar = noncovervars[j];
4688  weight = weights[liftvar];
4689 
4690  while( intervalends[h] < weight )
4691  h++;
4692 
4693  if( h == 0 )
4694  liftcoef = h;
4695  else
4696  {
4697  if( weight <= intervalends[h-1] + rhos[h] )
4698  {
4699  SCIP_Real tmp1;
4700  SCIP_Real tmp2;
4701  tmp1 = (SCIP_Real) (intervalends[h-1] + rhos[h] - weight);
4702  tmp2 = (SCIP_Real) rhos[1];
4703  liftcoef = h - ( tmp1 / tmp2 );
4704  }
4705  else
4706  liftcoef = h;
4707  }
4708 
4709  /* sets lifting coefficient */
4710  assert(liftcoefs[liftvar] == 0.0);
4711  liftcoefs[liftvar] = liftcoef;
4712 
4713  /* updates activity of current valid inequality */
4714  (*cutact) += liftcoef * solvals[liftvar];
4715  }
4716 
4717  /* frees temporary memory */
4718  SCIPfreeBufferArray(scip, &rhos);
4719  SCIPfreeBufferArray(scip, &intervalends);
4720  SCIPfreeBufferArray(scip, &maxweightsums);
4721  SCIPfreeBufferArray(scip, &sortkeys);
4722 
4723  return SCIP_OKAY;
4724 }
4725 
4726 
4727 /** separates lifted minimal cover inequalities using sequential up- and down-lifting and GUB information, if wanted, for
4728  * given knapsack problem
4729 */
4730 static
4732  SCIP* scip, /**< SCIP data structure */
4733  SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
4734  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
4735  SCIP_VAR** vars, /**< variables in knapsack constraint */
4736  int nvars, /**< number of variables in knapsack constraint */
4737  int ntightened, /**< number of variables with tightened upper bound */
4738  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
4739  SCIP_Longint capacity, /**< capacity of knapsack */
4740  SCIP_Real* solvals, /**< solution values of all problem variables */
4741  int* mincovervars, /**< mincover variables */
4742  int* nonmincovervars, /**< nonmincover variables */
4743  int nmincovervars, /**< number of mincover variables */
4744  int nnonmincovervars, /**< number of nonmincover variables */
4745  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
4746  SCIP_GUBSET* gubset, /**< GUB set data structure, NULL if no GUB information should be used */
4747  SCIP_Bool* cutoff, /**< pointer to store whether a cutoff has been detected */
4748  int* ncuts /**< pointer to add up the number of found cuts */
4749  )
4750 {
4751  int* varsC1;
4752  int* varsC2;
4753  int* varsF;
4754  int* varsR;
4755  int nvarsC1;
4756  int nvarsC2;
4757  int nvarsF;
4758  int nvarsR;
4759  SCIP_Real cutact;
4760  int* liftcoefs;
4761  int liftrhs;
4762 
4763  assert( cutoff != NULL );
4764  *cutoff = FALSE;
4765 
4766  /* allocates temporary memory */
4767  SCIP_CALL( SCIPallocBufferArray(scip, &varsC1, nvars) );
4768  SCIP_CALL( SCIPallocBufferArray(scip, &varsC2, nvars) );
4769  SCIP_CALL( SCIPallocBufferArray(scip, &varsF, nvars) );
4770  SCIP_CALL( SCIPallocBufferArray(scip, &varsR, nvars) );
4771  SCIP_CALL( SCIPallocBufferArray(scip, &liftcoefs, nvars) );
4772 
4773  /* 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
4774  * as follows
4775  * C_2 = { j in C : x*_j = 1 } and
4776  * C_1 = C\C_2
4777  */
4778  getPartitionCovervars(scip, solvals, mincovervars, nmincovervars, varsC1, varsC2, &nvarsC1, &nvarsC2);
4779  assert(nvarsC1 + nvarsC2 == nmincovervars);
4780  assert(nmincovervars > 0);
4781  assert(nvarsC1 >= 0); /* nvarsC1 > 0 does not always hold, because relaxed knapsack conss may already be violated */
4782 
4783  /* changes partition (C_1,C_2) of minimal cover C, if |C1| = 1, by moving one variable from C2 to C1 */
4784  if( nvarsC1 < 2 && nvarsC2 > 0)
4785  {
4786  SCIP_CALL( changePartitionCovervars(scip, weights, varsC1, varsC2, &nvarsC1, &nvarsC2) );
4787  assert(nvarsC1 >= 1);
4788  }
4789  assert(nvarsC2 == 0 || nvarsC1 >= 1);
4790 
4791  /* gets partition (F,R) of N\C, i.e. F & R = N\C and F cap R = emptyset; chooses partition as follows
4792  * R = { j in N\C : x*_j = 0 } and
4793  * F = (N\C)\F
4794  */
4795  getPartitionNoncovervars(scip, solvals, nonmincovervars, nnonmincovervars, varsF, varsR, &nvarsF, &nvarsR);
4796  assert(nvarsF + nvarsR == nnonmincovervars);
4797  assert(nvarsC1 + nvarsC2 + nvarsF + nvarsR == nvars - ntightened);
4798 
4799  /* lift cuts without GUB information */
4800  if( gubset == NULL )
4801  {
4802  /* sorts variables in F, C_2, R according to the second level lifting sequence that will be used in the sequential
4803  * lifting procedure
4804  */
4805  SCIP_CALL( getLiftingSequence(scip, solvals, weights, varsF, varsC2, varsR, nvarsF, nvarsC2, nvarsR) );
4806 
4807  /* lifts minimal cover inequality sum_{j in C_1} x_j <= |C_1| - 1 valid for
4808  *
4809  * 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 }
4810  *
4811  * 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
4812  *
4813  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 },
4814  *
4815  * uses sequential up-lifting for the variables in F, sequential down-lifting for the variable in C_2 and sequential
4816  * up-lifting for the variables in R according to the second level lifting sequence
4817  */
4818  SCIP_CALL( sequentialUpAndDownLifting(scip, vars, nvars, ntightened, weights, capacity, solvals, varsC1, varsC2,
4819  varsF, varsR, nvarsC1, nvarsC2, nvarsF, nvarsR, nvarsC1 - 1, liftcoefs, &cutact, &liftrhs) );
4820  }
4821  /* lift cuts with GUB information */
4822  else
4823  {
4824  int* gubconsGC1;
4825  int* gubconsGC2;
4826  int* gubconsGFC1;
4827  int* gubconsGR;
4828  int ngubconsGC1;
4829  int ngubconsGC2;
4830  int ngubconsGFC1;
4831  int ngubconsGR;
4832  int ngubconss;
4833  int nconstightened;
4834  int maxgubvarssize;
4835 
4836  assert(nvars == gubset->nvars);
4837 
4838  ngubconsGC1 = 0;
4839  ngubconsGC2 = 0;
4840  ngubconsGFC1 = 0;
4841  ngubconsGR = 0;
4842  ngubconss = gubset->ngubconss;
4843  nconstightened = 0;
4844  maxgubvarssize = 0;
4845 
4846  /* allocates temporary memory */
4847  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGC1, ngubconss) );
4848  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGC2, ngubconss) );
4849  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGFC1, ngubconss) );
4850  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGR, ngubconss) );
4851 
4852  /* categorizies GUBs of knapsack GUB partion into GOC1, GNC1, GF, GC2, and GR and computes a lifting sequence of
4853  * the GUBs for the sequential GUB wise lifting procedure
4854  */
4855  SCIP_CALL( getLiftingSequenceGUB(scip, gubset, solvals, weights, varsC1, varsC2, varsF, varsR, nvarsC1,
4856  nvarsC2, nvarsF, nvarsR, gubconsGC1, gubconsGC2, gubconsGFC1, gubconsGR, &ngubconsGC1, &ngubconsGC2,
4857  &ngubconsGFC1, &ngubconsGR, &nconstightened, &maxgubvarssize) );
4858 
4859  /* lifts minimal cover inequality sum_{j in C_1} x_j <= |C_1| - 1 valid for
4860  *
4861  * 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,
4862  * sum_{j in Q_i} x_j <= 1, forall i in I }
4863  *
4864  * 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
4865  *
4866  * 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 },
4867  *
4868  * uses sequential up-lifting for the variables in GUB constraints in gubconsGFC1,
4869  * sequential down-lifting for the variables in GUB constraints in gubconsGC2, and
4870  * sequential up-lifting for the variabels in GUB constraints in gubconsGR.
4871  */
4872  SCIP_CALL( sequentialUpAndDownLiftingGUB(scip, gubset, vars, nconstightened, weights, capacity, solvals, gubconsGC1,
4873  gubconsGC2, gubconsGFC1, gubconsGR, ngubconsGC1, ngubconsGC2, ngubconsGFC1, ngubconsGR,
4874  MIN(nvarsC1 - 1, ngubconsGC1), liftcoefs, &cutact, &liftrhs, maxgubvarssize) );
4875 
4876  /* frees temporary memory */
4877  SCIPfreeBufferArray(scip, &gubconsGR);
4878  SCIPfreeBufferArray(scip, &gubconsGFC1);
4879  SCIPfreeBufferArray(scip, &gubconsGC2);
4880  SCIPfreeBufferArray(scip, &gubconsGC1);
4881  }
4882 
4883  /* checks, if lifting yielded a violated cut */
4884  if( SCIPisEfficacious(scip, (cutact - liftrhs)/sqrt((SCIP_Real)MAX(liftrhs, 1))) )
4885  {
4886  SCIP_ROW* row;
4887  char name[SCIP_MAXSTRLEN];
4888  int j;
4889 
4890  /* creates LP row */
4891  assert( cons == NULL || sepa == NULL );
4892  if ( cons != NULL )
4893  {
4894  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcseq%" SCIP_LONGINT_FORMAT "", SCIPconsGetName(cons), SCIPconshdlrGetNCutsFound(SCIPconsGetHdlr(cons)));
4895  SCIP_CALL( SCIPcreateEmptyRowCons(scip, &row, cons, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs,
4896  cons != NULL ? SCIPconsIsLocal(cons) : FALSE, FALSE,
4897  cons != NULL ? SCIPconsIsRemovable(cons) : TRUE) );
4898  }
4899  else if ( sepa != NULL )
4900  {
4901  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcseq_%" SCIP_LONGINT_FORMAT "", SCIPsepaGetName(sepa), SCIPsepaGetNCutsFound(sepa));
4902  SCIP_CALL( SCIPcreateEmptyRowSepa(scip, &row, sepa, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
4903  }
4904  else
4905  {
4906  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nn_mcseq_%d", *ncuts);
4907  SCIP_CALL( SCIPcreateEmptyRowUnspec(scip, &row, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
4908  }
4909 
4910  /* adds all variables in the knapsack constraint with calculated lifting coefficient to the cut */
4911  SCIP_CALL( SCIPcacheRowExtensions(scip, row) );
4912  assert(nvarsC1 + nvarsC2 + nvarsF + nvarsR == nvars - ntightened);
4913  for( j = 0; j < nvarsC1; j++ )
4914  {
4915  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsC1[j]], 1.0) );
4916  }
4917  for( j = 0; j < nvarsC2; j++ )
4918  {
4919  if( liftcoefs[varsC2[j]] > 0 )
4920  {
4921  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsC2[j]], (SCIP_Real)liftcoefs[varsC2[j]]) );
4922  }
4923  }
4924  for( j = 0; j < nvarsF; j++ )
4925  {
4926  if( liftcoefs[varsF[j]] > 0 )
4927  {
4928  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsF[j]], (SCIP_Real)liftcoefs[varsF[j]]) );
4929  }
4930  }
4931  for( j = 0; j < nvarsR; j++ )
4932  {
4933  if( liftcoefs[varsR[j]] > 0 )
4934  {
4935  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsR[j]], (SCIP_Real)liftcoefs[varsR[j]]) );
4936  }
4937  }
4938  SCIP_CALL( SCIPflushRowExtensions(scip, row) );
4939 
4940  /* checks, if cut is violated enough */
4941  if( SCIPisCutEfficacious(scip, sol, row) )
4942  {
4943  if( cons != NULL )
4944  {
4945  SCIP_CALL( SCIPresetConsAge(scip, cons) );
4946  }
4947  SCIP_CALL( SCIPaddRow(scip, row, FALSE, cutoff) );
4948  (*ncuts)++;
4949  }
4950  SCIP_CALL( SCIPreleaseRow(scip, &row) );
4951  }
4952 
4953  /* frees temporary memory */
4954  SCIPfreeBufferArray(scip, &liftcoefs);
4955  SCIPfreeBufferArray(scip, &varsR);
4956  SCIPfreeBufferArray(scip, &varsF);
4957  SCIPfreeBufferArray(scip, &varsC2);
4958  SCIPfreeBufferArray(scip, &varsC1);
4959 
4960  return SCIP_OKAY;
4961 }
4962 
4963 /** separates lifted extended weight inequalities using sequential up- and down-lifting for given knapsack problem */
4964 static
4966  SCIP* scip, /**< SCIP data structure */
4967  SCIP_CONS* cons, /**< constraint that originates the knapsack problem, or NULL */
4968  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
4969  SCIP_VAR** vars, /**< variables in knapsack constraint */
4970  int nvars, /**< number of variables in knapsack constraint */
4971  int ntightened, /**< number of variables with tightened upper bound */
4972  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
4973  SCIP_Longint capacity, /**< capacity of knapsack */
4974  SCIP_Real* solvals, /**< solution values of all problem variables */
4975  int* feassetvars, /**< variables in feasible set */
4976  int* nonfeassetvars, /**< variables not in feasible set */
4977  int nfeassetvars, /**< number of variables in feasible set */
4978  int nnonfeassetvars, /**< number of variables not in feasible set */
4979  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
4980  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
4981  int* ncuts /**< pointer to add up the number of found cuts */
4982  )
4983 {
4984  int* varsT1;
4985  int* varsT2;
4986  int* varsF;
4987  int* varsR;
4988  int* liftcoefs;
4989  SCIP_Real cutact;
4990  int nvarsT1;
4991  int nvarsT2;
4992  int nvarsF;
4993  int nvarsR;
4994  int liftrhs;
4995  int j;
4996 
4997  assert( cutoff != NULL );
4998  *cutoff = FALSE;
4999 
5000  /* allocates temporary memory */
5001  SCIP_CALL( SCIPallocBufferArray(scip, &varsT1, nvars) );
5002  SCIP_CALL( SCIPallocBufferArray(scip, &varsT2, nvars) );
5003  SCIP_CALL( SCIPallocBufferArray(scip, &varsF, nvars) );
5004  SCIP_CALL( SCIPallocBufferArray(scip, &varsR, nvars) );
5005  SCIP_CALL( SCIPallocBufferArray(scip, &liftcoefs, nvars) );
5006 
5007  /* 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
5008  * as follows
5009  * T_2 = { j in T : x*_j = 1 } and
5010  * T_1 = T\T_2
5011  */
5012  getPartitionCovervars(scip, solvals, feassetvars, nfeassetvars, varsT1, varsT2, &nvarsT1, &nvarsT2);
5013  assert(nvarsT1 + nvarsT2 == nfeassetvars);
5014 
5015  /* changes partition (T_1,T_2) of feasible set T, if |T1| = 0, by moving one variable from T2 to T1 */
5016  if( nvarsT1 == 0 && nvarsT2 > 0)
5017  {
5018  SCIP_CALL( changePartitionFeasiblesetvars(scip, weights, varsT1, varsT2, &nvarsT1, &nvarsT2) );
5019  assert(nvarsT1 == 1);
5020  }
5021  assert(nvarsT2 == 0 || nvarsT1 > 0);
5022 
5023  /* gets partition (F,R) of N\T, i.e. F & R = N\T and F cap R = emptyset; chooses partition as follows
5024  * R = { j in N\T : x*_j = 0 } and
5025  * F = (N\T)\F
5026  */
5027  getPartitionNoncovervars(scip, solvals, nonfeassetvars, nnonfeassetvars, varsF, varsR, &nvarsF, &nvarsR);
5028  assert(nvarsF + nvarsR == nnonfeassetvars);
5029  assert(nvarsT1 + nvarsT2 + nvarsF + nvarsR == nvars - ntightened);
5030 
5031  /* sorts variables in F, T_2, and R according to the second level lifting sequence that will be used in the sequential
5032  * lifting procedure (the variable removed last from the initial cover does not have to be lifted first, therefore it
5033  * is included in the sorting routine)
5034  */
5035  SCIP_CALL( getLiftingSequence(scip, solvals, weights, varsF, varsT2, varsR, nvarsF, nvarsT2, nvarsR) );
5036 
5037  /* lifts extended weight inequality sum_{j in T_1} x_j <= |T_1| valid for
5038  *
5039  * 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 }
5040  *
5041  * 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
5042  *
5043  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 },
5044  *
5045  * uses sequential up-lifting for the variables in F, sequential down-lifting for the variable in T_2 and sequential
5046  * up-lifting for the variabels in R according to the second level lifting sequence
5047  */
5048  SCIP_CALL( sequentialUpAndDownLifting(scip, vars, nvars, ntightened, weights, capacity, solvals, varsT1, varsT2, varsF, varsR,
5049  nvarsT1, nvarsT2, nvarsF, nvarsR, nvarsT1, liftcoefs, &cutact, &liftrhs) );
5050 
5051  /* checks, if lifting yielded a violated cut */
5052  if( SCIPisEfficacious(scip, (cutact - liftrhs)/sqrt((SCIP_Real)MAX(liftrhs, 1))) )
5053  {
5054  SCIP_ROW* row;
5055  char name[SCIP_MAXSTRLEN];
5056 
5057  /* creates LP row */
5058  assert( cons == NULL || sepa == NULL );
5059  if( cons != NULL )
5060  {
5061  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_ewseq%" SCIP_LONGINT_FORMAT "", SCIPconsGetName(cons), SCIPconshdlrGetNCutsFound(SCIPconsGetHdlr(cons)));
5062  SCIP_CALL( SCIPcreateEmptyRowConshdlr(scip, &row, SCIPconsGetHdlr(cons), name, -SCIPinfinity(scip), (SCIP_Real)liftrhs,
5063  cons != NULL ? SCIPconsIsLocal(cons) : FALSE, FALSE,
5064  cons != NULL ? SCIPconsIsRemovable(cons) : TRUE) );
5065  }
5066  else if ( sepa != NULL )
5067  {
5068  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_ewseq_%" SCIP_LONGINT_FORMAT "", SCIPsepaGetName(sepa), SCIPsepaGetNCutsFound(sepa));
5069  SCIP_CALL( SCIPcreateEmptyRowSepa(scip, &row, sepa, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5070  }
5071  else
5072  {
5073  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nn_ewseq_%d", *ncuts);
5074  SCIP_CALL( SCIPcreateEmptyRowUnspec(scip, &row, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5075  }
5076 
5077  /* adds all variables in the knapsack constraint with calculated lifting coefficient to the cut */
5078  SCIP_CALL( SCIPcacheRowExtensions(scip, row) );
5079  assert(nvarsT1 + nvarsT2 + nvarsF + nvarsR == nvars - ntightened);
5080  for( j = 0; j < nvarsT1; j++ )
5081  {
5082  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsT1[j]], 1.0) );
5083  }
5084  for( j = 0; j < nvarsT2; j++ )
5085  {
5086  if( liftcoefs[varsT2[j]] > 0 )
5087  {
5088  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsT2[j]], (SCIP_Real)liftcoefs[varsT2[j]]) );
5089  }
5090  }
5091  for( j = 0; j < nvarsF; j++ )
5092  {
5093  if( liftcoefs[varsF[j]] > 0 )
5094  {
5095  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsF[j]], (SCIP_Real)liftcoefs[varsF[j]]) );
5096  }
5097  }
5098  for( j = 0; j < nvarsR; j++ )
5099  {
5100  if( liftcoefs[varsR[j]] > 0 )
5101  {
5102  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsR[j]], (SCIP_Real)liftcoefs[varsR[j]]) );
5103  }
5104  }
5105  SCIP_CALL( SCIPflushRowExtensions(scip, row) );
5106 
5107  /* checks, if cut is violated enough */
5108  if( SCIPisCutEfficacious(scip, sol, row) )
5109  {
5110  if( cons != NULL )
5111  {
5112  SCIP_CALL( SCIPresetConsAge(scip, cons) );
5113  }
5114  SCIP_CALL( SCIPaddRow(scip, row, FALSE, cutoff) );
5115  (*ncuts)++;
5116  }
5117  SCIP_CALL( SCIPreleaseRow(scip, &row) );
5118  }
5119 
5120  /* frees temporary memory */
5121  SCIPfreeBufferArray(scip, &liftcoefs);
5122  SCIPfreeBufferArray(scip, &varsR);
5123  SCIPfreeBufferArray(scip, &varsF);
5124  SCIPfreeBufferArray(scip, &varsT2);
5125  SCIPfreeBufferArray(scip, &varsT1);
5126 
5127  return SCIP_OKAY;
5128 }
5129 
5130 /** separates lifted minimal cover inequalities using superadditive up-lifting for given knapsack problem */
5131 static
5133  SCIP* scip, /**< SCIP data structure */
5134  SCIP_CONS* cons, /**< constraint that originates the knapsack problem, or NULL */
5135  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5136  SCIP_VAR** vars, /**< variables in knapsack constraint */
5137  int nvars, /**< number of variables in knapsack constraint */
5138  int ntightened, /**< number of variables with tightened upper bound */
5139  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5140  SCIP_Longint capacity, /**< capacity of knapsack */
5141  SCIP_Real* solvals, /**< solution values of all problem variables */
5142  int* mincovervars, /**< mincover variables */
5143  int* nonmincovervars, /**< nonmincover variables */
5144  int nmincovervars, /**< number of mincover variables */
5145  int nnonmincovervars, /**< number of nonmincover variables */
5146  SCIP_Longint mincoverweight, /**< weight of minimal cover */
5147  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
5148  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
5149  int* ncuts /**< pointer to add up the number of found cuts */
5150  )
5151 {
5152  SCIP_Real* realliftcoefs;
5153  SCIP_Real cutact;
5154  int liftrhs;
5155 
5156  assert( cutoff != NULL );
5157  *cutoff = FALSE;
5158  cutact = 0.0;
5159 
5160  /* allocates temporary memory */
5161  SCIP_CALL( SCIPallocBufferArray(scip, &realliftcoefs, nvars) );
5162 
5163  /* lifts minimal cover inequality sum_{j in C} x_j <= |C| - 1 valid for
5164  *
5165  * S^0 = { x in {0,1}^|C| : sum_{j in C} a_j x_j <= a_0 }
5166  *
5167  * to a valid inequality sum_{j in C} x_j + sum_{j in N\C} alpha_j x_j <= |C| - 1 for
5168  *
5169  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 },
5170  *
5171  * uses superadditive up-lifting for the variables in N\C.
5172  */
5173  SCIP_CALL( superadditiveUpLifting(scip, vars, nvars, ntightened, weights, capacity, solvals, mincovervars,
5174  nonmincovervars, nmincovervars, nnonmincovervars, mincoverweight, realliftcoefs, &cutact) );
5175  liftrhs = nmincovervars - 1;
5176 
5177  /* checks, if lifting yielded a violated cut */
5178  if( SCIPisEfficacious(scip, (cutact - liftrhs)/sqrt((SCIP_Real)MAX(liftrhs, 1))) )
5179  {
5180  SCIP_ROW* row;
5181  char name[SCIP_MAXSTRLEN];
5182  int j;
5183 
5184  /* creates LP row */
5185  assert( cons == NULL || sepa == NULL );
5186  if ( cons != NULL )
5187  {
5188  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcsup%" SCIP_LONGINT_FORMAT "", SCIPconsGetName(cons), SCIPconshdlrGetNCutsFound(SCIPconsGetHdlr(cons)));
5189  SCIP_CALL( SCIPcreateEmptyRowConshdlr(scip, &row, SCIPconsGetHdlr(cons), name, -SCIPinfinity(scip), (SCIP_Real)liftrhs,
5190  cons != NULL ? SCIPconsIsLocal(cons) : FALSE, FALSE,
5191  cons != NULL ? SCIPconsIsRemovable(cons) : TRUE) );
5192  }
5193  else if ( sepa != NULL )
5194  {
5195  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcsup%" SCIP_LONGINT_FORMAT "", SCIPsepaGetName(sepa), SCIPsepaGetNCutsFound(sepa));
5196  SCIP_CALL( SCIPcreateEmptyRowSepa(scip, &row, sepa, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5197  }
5198  else
5199  {
5200  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nn_mcsup_%d", *ncuts);
5201  SCIP_CALL( SCIPcreateEmptyRowUnspec(scip, &row, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5202  }
5203 
5204  /* adds all variables in the knapsack constraint with calculated lifting coefficient to the cut */
5205  SCIP_CALL( SCIPcacheRowExtensions(scip, row) );
5206  assert(nmincovervars + nnonmincovervars == nvars - ntightened);
5207  for( j = 0; j < nmincovervars; j++ )
5208  {
5209  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[mincovervars[j]], 1.0) );
5210  }
5211  for( j = 0; j < nnonmincovervars; j++ )
5212  {
5213  assert(SCIPisFeasGE(scip, realliftcoefs[nonmincovervars[j]], 0.0));
5214  if( SCIPisFeasGT(scip, realliftcoefs[nonmincovervars[j]], 0.0) )
5215  {
5216  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[nonmincovervars[j]], realliftcoefs[nonmincovervars[j]]) );
5217  }
5218  }
5219  SCIP_CALL( SCIPflushRowExtensions(scip, row) );
5220 
5221  /* checks, if cut is violated enough */
5222  if( SCIPisCutEfficacious(scip, sol, row) )
5223  {
5224  if( cons != NULL )
5225  {
5226  SCIP_CALL( SCIPresetConsAge(scip, cons) );
5227  }
5228  SCIP_CALL( SCIPaddRow(scip, row, FALSE, cutoff) );
5229  (*ncuts)++;
5230  }
5231  SCIP_CALL( SCIPreleaseRow(scip, &row) );
5232  }
5233 
5234  /* frees temporary memory */
5235  SCIPfreeBufferArray(scip, &realliftcoefs);
5236 
5237  return SCIP_OKAY;
5238 }
5239 
5240 /** converts given cover C to a minimal cover by removing variables in the reverse order in which the variables were chosen
5241  * 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
5242  * C and in the order of non-increasing (1 - x*_j), if the modified transformed separation problem was used to find C;
5243  * note that all variables with x*_j = 1 will be removed last
5244  */
5245 static
5247  SCIP* scip, /**< SCIP data structure */
5248  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5249  SCIP_Longint capacity, /**< capacity of knapsack */
5250  SCIP_Real* solvals, /**< solution values of all problem variables */
5251  int* covervars, /**< pointer to store cover variables */
5252  int* noncovervars, /**< pointer to store noncover variables */
5253  int* ncovervars, /**< pointer to store number of cover variables */
5254  int* nnoncovervars, /**< pointer to store number of noncover variables */
5255  SCIP_Longint* coverweight, /**< pointer to store weight of cover */
5256  SCIP_Bool modtransused /**< TRUE if mod trans sepa prob was used to find cover */
5257  )
5258 {
5259  SORTKEYPAIR** sortkeypairs;
5260  SORTKEYPAIR** sortkeypairssorted;
5261  SCIP_Longint minweight;
5262  int nsortkeypairs;
5263  int minweightidx;
5264  int j;
5265  int k;
5266 
5267  assert(scip != NULL);
5268  assert(covervars != NULL);
5269  assert(noncovervars != NULL);
5270  assert(ncovervars != NULL);
5271  assert(*ncovervars > 0);
5272  assert(nnoncovervars != NULL);
5273  assert(*nnoncovervars >= 0);
5274  assert(coverweight != NULL);
5275  assert(*coverweight > 0);
5276  assert(*coverweight > capacity);
5277 
5278  /* allocates temporary memory; we need two arrays for the keypairs in order to be able to free them in the correct
5279  * order */
5280  nsortkeypairs = *ncovervars;
5281  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairs, nsortkeypairs) );
5282  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairssorted, nsortkeypairs) );
5283 
5284  /* sorts C in the reverse order in which the variables were chosen to be in the cover, i.e.
5285  * such that (1 - x*_1)/a_1 >= ... >= (1 - x*_|C|)/a_|C|, if trans separation problem was used to find C
5286  * such that (1 - x*_1) >= ... >= (1 - x*_|C|), if modified trans separation problem was used to find C
5287  * note that all variables with x*_j = 1 are in the end of the sorted C, so they will be removed last from C
5288  */
5289  assert(*ncovervars == nsortkeypairs);
5290  if( modtransused )
5291  {
5292  for( j = 0; j < *ncovervars; j++ )
5293  {
5294  SCIP_CALL( SCIPallocBuffer(scip, &(sortkeypairs[j])) ); /*lint !e866 */
5295  sortkeypairssorted[j] = sortkeypairs[j];
5296 
5297  sortkeypairs[j]->key1 = solvals[covervars[j]];
5298  sortkeypairs[j]->key2 = (SCIP_Real) weights[covervars[j]];
5299  }
5300  }
5301  else
5302  {
5303  for( j = 0; j < *ncovervars; j++ )
5304  {
5305  SCIP_CALL( SCIPallocBuffer(scip, &(sortkeypairs[j])) ); /*lint !e866 */
5306  sortkeypairssorted[j] = sortkeypairs[j];
5307 
5308  sortkeypairs[j]->key1 = (solvals[covervars[j]] - 1.0) / ((SCIP_Real) weights[covervars[j]]);
5309  sortkeypairs[j]->key2 = (SCIP_Real) (-weights[covervars[j]]);
5310  }
5311  }
5312  SCIPsortPtrInt((void**)sortkeypairssorted, covervars, compSortkeypairs, *ncovervars);
5313 
5314  /* gets j' with a_j' = min{ a_j : j in C } */
5315  minweightidx = 0;
5316  minweight = weights[covervars[minweightidx]];
5317  for( j = 1; j < *ncovervars; j++ )
5318  {
5319  if( weights[covervars[j]] <= minweight )
5320  {
5321  minweightidx = j;
5322  minweight = weights[covervars[minweightidx]];
5323  }
5324  }
5325  assert(minweightidx >= 0 && minweightidx < *ncovervars);
5326  assert(minweight > 0 && minweight <= *coverweight);
5327 
5328  j = 0;
5329  /* removes variables from C until the remaining variables form a minimal cover */
5330  while( j < *ncovervars && ((*coverweight) - minweight > capacity) )
5331  {
5332  assert(minweightidx >= j);
5333  assert(checkMinweightidx(weights, capacity, covervars, *ncovervars, *coverweight, minweightidx, j));
5334 
5335  /* if sum_{i in C} a_i - a_j <= a_0, j cannot be removed from C */
5336  if( (*coverweight) - weights[covervars[j]] <= capacity )
5337  {
5338  ++j;
5339  continue;
5340  }
5341 
5342  /* adds j to N\C */
5343  noncovervars[*nnoncovervars] = covervars[j];
5344  (*nnoncovervars)++;
5345 
5346  /* removes j from C */
5347  (*coverweight) -= weights[covervars[j]];
5348  for( k = j; k < (*ncovervars) - 1; k++ )
5349  covervars[k] = covervars[k+1];
5350  (*ncovervars)--;
5351 
5352  /* updates j' with a_j' = min{ a_j : j in C } */
5353  if( j == minweightidx )
5354  {
5355  minweightidx = 0;
5356  minweight = weights[covervars[minweightidx]];
5357  for( k = 1; k < *ncovervars; k++ )
5358  {
5359  if( weights[covervars[k]] <= minweight )
5360  {
5361  minweightidx = k;
5362  minweight = weights[covervars[minweightidx]];
5363  }
5364  }
5365  assert(minweight > 0 && minweight <= *coverweight);
5366  assert(minweightidx >= 0 && minweightidx < *ncovervars);
5367  }
5368  else
5369  {
5370  assert(minweightidx > j);
5371  minweightidx--;
5372  }
5373  /* j needs to stay the same */
5374  }
5375  assert((*coverweight) > capacity);
5376  assert((*coverweight) - minweight <= capacity);
5377 
5378  /* frees temporary memory */
5379  for( j = nsortkeypairs-1; j >= 0; j-- )
5380  SCIPfreeBuffer(scip, &(sortkeypairs[j])); /*lint !e866 */
5381  SCIPfreeBufferArray(scip, &sortkeypairssorted);
5382  SCIPfreeBufferArray(scip, &sortkeypairs);
5383 
5384  return SCIP_OKAY;
5385 }
5386 
5387 /** converts given initial cover C_init to a feasible set by removing variables in the reverse order in which
5388  * they were chosen to be in C_init:
5389  * non-increasing (1 - x*_j)/a_j, if transformed separation problem was used to find C_init
5390  * non-increasing (1 - x*_j), if modified transformed separation problem was used to find C_init.
5391  * separates lifted extended weight inequalities using sequential up- and down-lifting for this feasible set
5392  * and all subsequent feasible sets.
5393  */
5394 static
5396  SCIP* scip, /**< SCIP data structure */
5397  SCIP_CONS* cons, /**< constraint that originates the knapsack problem */
5398  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5399  SCIP_VAR** vars, /**< variables in knapsack constraint */
5400  int nvars, /**< number of variables in knapsack constraint */
5401  int ntightened, /**< number of variables with tightened upper bound */
5402  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5403  SCIP_Longint capacity, /**< capacity of knapsack */
5404  SCIP_Real* solvals, /**< solution values of all problem variables */
5405  int* covervars, /**< pointer to store cover variables */
5406  int* noncovervars, /**< pointer to store noncover variables */
5407  int* ncovervars, /**< pointer to store number of cover variables */
5408  int* nnoncovervars, /**< pointer to store number of noncover variables */
5409  SCIP_Longint* coverweight, /**< pointer to store weight of cover */
5410  SCIP_Bool modtransused, /**< TRUE if mod trans sepa prob was used to find cover */
5411  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
5412  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
5413  int* ncuts /**< pointer to add up the number of found cuts */
5414  )
5415 {
5416  SCIP_Real* sortkeys;
5417  int j;
5418  int k;
5419 
5420  assert(scip != NULL);
5421  assert(covervars != NULL);
5422  assert(noncovervars != NULL);
5423  assert(ncovervars != NULL);
5424  assert(*ncovervars > 0);
5425  assert(nnoncovervars != NULL);
5426  assert(*nnoncovervars >= 0);
5427  assert(coverweight != NULL);
5428  assert(*coverweight > 0);
5429  assert(*coverweight > capacity);
5430  assert(*ncovervars + *nnoncovervars == nvars - ntightened);
5431  assert(cutoff != NULL);
5432 
5433  *cutoff = FALSE;
5434 
5435  /* allocates temporary memory */
5436  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, *ncovervars) );
5437 
5438  /* sorts C in the reverse order in which the variables were chosen to be in the cover, i.e.
5439  * such that (1 - x*_1)/a_1 >= ... >= (1 - x*_|C|)/a_|C|, if trans separation problem was used to find C
5440  * such that (1 - x*_1) >= ... >= (1 - x*_|C|), if modified trans separation problem was used to find C
5441  * note that all variables with x*_j = 1 are in the end of the sorted C, so they will be removed last from C
5442  */
5443  if( modtransused )
5444  {
5445  for( j = 0; j < *ncovervars; j++ )
5446  {
5447  sortkeys[j] = solvals[covervars[j]];
5448  assert(SCIPisFeasGE(scip, sortkeys[j], 0.0));
5449  }
5450  }
5451  else
5452  {
5453  for( j = 0; j < *ncovervars; j++ )
5454  {
5455  sortkeys[j] = (solvals[covervars[j]] - 1.0) / ((SCIP_Real) weights[covervars[j]]);
5456  assert(SCIPisFeasLE(scip, sortkeys[j], 0.0));
5457  }
5458  }
5459  SCIPsortRealInt(sortkeys, covervars, *ncovervars);
5460 
5461  /* removes variables from C_init and separates lifted extended weight inequalities using sequential up- and down-lifting;
5462  * in addition to an extended weight inequality this gives cardinality inequalities */
5463  while( *ncovervars >= 2 )
5464  {
5465  /* adds first element of C_init to N\C_init */
5466  noncovervars[*nnoncovervars] = covervars[0];
5467  (*nnoncovervars)++;
5468 
5469  /* removes first element from C_init */
5470  (*coverweight) -= weights[covervars[0]];
5471  for( k = 0; k < (*ncovervars) - 1; k++ )
5472  covervars[k] = covervars[k+1];
5473  (*ncovervars)--;
5474 
5475  assert(*ncovervars + *nnoncovervars == nvars - ntightened);
5476  if( (*coverweight) <= capacity )
5477  {
5478  SCIP_CALL( separateSequLiftedExtendedWeightInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity, solvals,
5479  covervars, noncovervars, *ncovervars, *nnoncovervars, sol, cutoff, ncuts) );
5480  }
5481 
5482  /* stop if cover is too large */
5483  if ( *ncovervars >= MAXCOVERSIZEITERLEWI )
5484  break;
5485  }
5486 
5487  /* frees temporary memory */
5488  SCIPfreeBufferArray(scip, &sortkeys);
5489 
5490  return SCIP_OKAY;
5491 }
5492 
5493 /** separates different classes of valid inequalities for the 0-1 knapsack problem */
5495  SCIP* scip, /**< SCIP data structure */
5496  SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
5497  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5498  SCIP_VAR** vars, /**< variables in knapsack constraint */
5499  int nvars, /**< number of variables in knapsack constraint */
5500  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5501  SCIP_Longint capacity, /**< capacity of knapsack */
5502  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
5503  SCIP_Bool usegubs, /**< should GUB information be used for separation? */
5504  SCIP_Bool* cutoff, /**< pointer to store whether a cutoff has been detected */
5505  int* ncuts /**< pointer to add up the number of found cuts */
5506  )
5507 {
5508  SCIP_Real* solvals;
5509  int* covervars;
5510  int* noncovervars;
5511  SCIP_Bool coverfound;
5512  SCIP_Bool fractional;
5513  SCIP_Bool modtransused;
5514  SCIP_Longint coverweight;
5515  int ncovervars;
5516  int nnoncovervars;
5517  int ntightened;
5518 
5519  assert(scip != NULL);
5520  assert(capacity >= 0);
5521  assert(cutoff != NULL);
5522  assert(ncuts != NULL);
5523 
5524  *cutoff = FALSE;
5525 
5526  if( nvars == 0 )
5527  return SCIP_OKAY;
5528 
5529  assert(vars != NULL);
5530  assert(nvars > 0);
5531  assert(weights != NULL);
5532 
5533  /* increase age of constraint (age is reset to zero, if a cut was found) */
5534  if( cons != NULL )
5535  {
5536  SCIP_CALL( SCIPincConsAge(scip, cons) );
5537  }
5538 
5539  /* allocates temporary memory */
5540  SCIP_CALL( SCIPallocBufferArray(scip, &solvals, nvars) );
5541  SCIP_CALL( SCIPallocBufferArray(scip, &covervars, nvars) );
5542  SCIP_CALL( SCIPallocBufferArray(scip, &noncovervars, nvars) );
5543 
5544  /* gets solution values of all problem variables */
5545  SCIP_CALL( SCIPgetSolVals(scip, sol, nvars, vars, solvals) );
5546 
5547 #ifdef SCIP_DEBUG
5548  {
5549  int i;
5550 
5551  SCIPdebugMsg(scip, "separate cuts for knapsack constraint originated by cons <%s>:\n",
5552  cons == NULL ? "-" : SCIPconsGetName(cons));
5553  for( i = 0; i < nvars; ++i )
5554  {
5555  SCIPdebugMsgPrint(scip, "%+" SCIP_LONGINT_FORMAT "<%s>(%g)", weights[i], SCIPvarGetName(vars[i]), solvals[i]);
5556  }
5557  SCIPdebugMsgPrint(scip, " <= %" SCIP_LONGINT_FORMAT "\n", capacity);
5558  }
5559 #endif
5560 
5561  /* LMCI1 (lifted minimal cover inequalities using sequential up- and down-lifting) using GUB information
5562  */
5563  if( usegubs )
5564  {
5565  SCIP_GUBSET* gubset;
5566 
5567  SCIPdebugMsg(scip, "separate LMCI1-GUB cuts:\n");
5568 
5569  /* initializes partion of knapsack variables into nonoverlapping GUB constraints */
5570  SCIP_CALL( GUBsetCreate(scip, &gubset, nvars, weights, capacity) );
5571 
5572  /* constructs sophisticated partition of knapsack variables into nonoverlapping GUBs */
5573  SCIP_CALL( GUBsetGetCliquePartition(scip, gubset, vars, solvals) );
5574  assert(gubset->ngubconss <= nvars);
5575 
5576  /* gets a most violated initial cover C_init ( sum_{j in C_init} a_j > a_0 ) by using the
5577  * MODIFIED transformed separation problem and taking into account the following fixing:
5578  * j in C_init, if j in N_1 = {j in N : x*_j = 1} and
5579  * j in N\C_init, if j in N_0 = {j in N : x*_j = 0},
5580  * if one exists
5581  */
5582  modtransused = TRUE;
5583  SCIP_CALL( getCover(scip, vars, nvars, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5584  &nnoncovervars, &coverweight, &coverfound, modtransused, &ntightened, &fractional) );
5585 
5586  assert(!coverfound || !fractional || ncovervars + nnoncovervars == nvars - ntightened);
5587 
5588  /* if x* is not fractional we stop the separation routine */
5589  if( !fractional )
5590  {
5591  SCIPdebugMsg(scip, " LMCI1-GUB terminated by no variable with fractional LP value.\n");
5592 
5593  /* frees memory for GUB set data structure */
5594  GUBsetFree(scip, &gubset);
5595 
5596  goto TERMINATE;
5597  }
5598 
5599  /* if no cover was found we stop the separation routine for lifted minimal cover inequality */
5600  if( coverfound )
5601  {
5602  /* converts initial cover C_init to a minimal cover C by removing variables in the reverse order in which the
5603  * variables were chosen to be in C_init; note that variables with x*_j = 1 will be removed last
5604  */
5605  SCIP_CALL( makeCoverMinimal(scip, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5606  &nnoncovervars, &coverweight, modtransused) );
5607 
5608  /* only separate with GUB information if we have at least one nontrivial GUB (with more than one variable) */
5609  if( gubset->ngubconss < nvars )
5610  {
5611  /* separates lifted minimal cover inequalities using sequential up- and down-lifting and GUB information */
5612  SCIP_CALL( separateSequLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5613  solvals, covervars, noncovervars, ncovervars, nnoncovervars, sol, gubset, cutoff, ncuts) );
5614  }
5615  else
5616  {
5617  /* separates lifted minimal cover inequalities using sequential up- and down-lifting, but do not use trivial
5618  * GUB information
5619  */
5620  SCIP_CALL( separateSequLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5621  solvals, covervars, noncovervars, ncovervars, nnoncovervars, sol, NULL, cutoff, ncuts) );
5622  }
5623  }
5624 
5625  /* frees memory for GUB set data structure */
5626  GUBsetFree(scip, &gubset);
5627  }
5628  else
5629  {
5630  /* LMCI1 (lifted minimal cover inequalities using sequential up- and down-lifting)
5631  * (and LMCI2 (lifted minimal cover inequalities using superadditive up-lifting))
5632  */
5633 
5634  /* gets a most violated initial cover C_init ( sum_{j in C_init} a_j > a_0 ) by using the
5635  * MODIFIED transformed separation problem and taking into account the following fixing:
5636  * j in C_init, if j in N_1 = {j in N : x*_j = 1} and
5637  * j in N\C_init, if j in N_0 = {j in N : x*_j = 0},
5638  * if one exists
5639  */
5640  SCIPdebugMsg(scip, "separate LMCI1 cuts:\n");
5641  modtransused = TRUE;
5642  SCIP_CALL( getCover(scip, vars, nvars, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5643  &nnoncovervars, &coverweight, &coverfound, modtransused, &ntightened, &fractional) );
5644  assert(!coverfound || !fractional || ncovervars + nnoncovervars == nvars - ntightened);
5645 
5646  /* if x* is not fractional we stop the separation routine */
5647  if( !fractional )
5648  goto TERMINATE;
5649 
5650  /* if no cover was found we stop the separation routine for lifted minimal cover inequality */
5651  if( coverfound )
5652  {
5653  /* converts initial cover C_init to a minimal cover C by removing variables in the reverse order in which the
5654  * variables were chosen to be in C_init; note that variables with x*_j = 1 will be removed last
5655  */
5656  SCIP_CALL( makeCoverMinimal(scip, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5657  &nnoncovervars, &coverweight, modtransused) );
5658 
5659  /* separates lifted minimal cover inequalities using sequential up- and down-lifting */
5660  SCIP_CALL( separateSequLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5661  solvals, covervars, noncovervars, ncovervars, nnoncovervars, sol, NULL, cutoff, ncuts) );
5662 
5663  if( USESUPADDLIFT ) /*lint !e506 !e774*/
5664  {
5665  SCIPdebugMsg(scip, "separate LMCI2 cuts:\n");
5666  /* separates lifted minimal cover inequalities using superadditive up-lifting */
5667  SCIP_CALL( separateSupLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5668  solvals, covervars, noncovervars, ncovervars, nnoncovervars, coverweight, sol, cutoff, ncuts) );
5669  }
5670  }
5671  }
5672 
5673  /* LEWI (lifted extended weight inequalities using sequential up- and down-lifting) */
5674  if ( ! (*cutoff) )
5675  {
5676  /* gets a most violated initial cover C_init ( sum_{j in C_init} a_j > a_0 ) by using the
5677  * transformed separation problem and taking into account the following fixing:
5678  * j in C_init, if j in N_1 = {j in N : x*_j = 1} and
5679  * j in N\C_init, if j in N_0 = {j in N : x*_j = 0},
5680  * if one exists
5681  */
5682  SCIPdebugMsg(scip, "separate LEWI cuts:\n");
5683  modtransused = FALSE;
5684  SCIP_CALL( getCover(scip, vars, nvars, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5685  &nnoncovervars, &coverweight, &coverfound, modtransused, &ntightened, &fractional) );
5686  assert(fractional);
5687  assert(!coverfound || ncovervars + nnoncovervars == nvars - ntightened);
5688 
5689  /* if no cover was found we stop the separation routine */
5690  if( coverfound )
5691  {
5692  /* converts initial cover C_init to a feasible set by removing variables in the reverse order in which
5693  * they were chosen to be in C_init and separates lifted extended weight inequalities using sequential
5694  * up- and down-lifting for this feasible set and all subsequent feasible sets.
5695  */
5696  SCIP_CALL( getFeasibleSet(scip, cons, sepa, vars, nvars, ntightened, weights, capacity, solvals, covervars, noncovervars,
5697  &ncovervars, &nnoncovervars, &coverweight, modtransused, sol, cutoff, ncuts) );
5698  }
5699  }
5700 
5701  TERMINATE:
5702  /* frees temporary memory */
5703  SCIPfreeBufferArray(scip, &noncovervars);
5704  SCIPfreeBufferArray(scip, &covervars);
5705  SCIPfreeBufferArray(scip, &solvals);
5706 
5707  return SCIP_OKAY;
5708 }
5709 
5710 /* relaxes given general linear constraint into a knapsack constraint and separates lifted knapsack cover inequalities */
5712  SCIP* scip, /**< SCIP data structure */
5713  SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
5714  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5715  int nknapvars, /**< number of variables in the continuous knapsack constraint */
5716  SCIP_VAR** knapvars, /**< variables in the continuous knapsack constraint */
5717  SCIP_Real* knapvals, /**< coefficients of the variables in the continuous knapsack constraint */
5718  SCIP_Real valscale, /**< -1.0 if lhs of row is used as rhs of c. k. constraint, +1.0 otherwise */
5719  SCIP_Real rhs, /**< right hand side of the continuous knapsack constraint */
5720  SCIP_SOL* sol, /**< primal CIP solution, NULL for current LP solution */
5721  SCIP_Bool* cutoff, /**< pointer to store whether a cutoff was found */
5722  int* ncuts /**< pointer to add up the number of found cuts */
5723  )
5724 {
5725  SCIP_VAR** binvars;
5726  SCIP_VAR** consvars;
5727  SCIP_Real* binvals;
5728  SCIP_Longint* consvals;
5729  SCIP_Longint minact;
5730  SCIP_Longint maxact;
5731  SCIP_Real intscalar;
5732  SCIP_Bool success;
5733  int nbinvars;
5734  int nconsvars;
5735  int i;
5736 
5737  int* tmpindices;
5738  int tmp;
5739  SCIP_CONSHDLR* conshdlr;
5740  SCIP_CONSHDLRDATA* conshdlrdata;
5741  SCIP_Bool noknapsackconshdlr;
5742  SCIP_Bool usegubs;
5743 
5744  assert(nknapvars > 0);
5745  assert(knapvars != NULL);
5746  assert(cutoff != NULL);
5747 
5748  tmpindices = NULL;
5749 
5750  SCIPdebugMsg(scip, "separate linear constraint <%s> relaxed to knapsack\n", cons != NULL ? SCIPconsGetName(cons) : "-");
5751  SCIPdebug( if( cons != NULL ) { SCIPdebugPrintCons(scip, cons, NULL); } );
5752 
5753  binvars = SCIPgetVars(scip);
5754 
5755  /* all variables which are of integral type can be potentially of binary type; this can be checked via the method SCIPvarIsBinary(var) */
5756  nbinvars = SCIPgetNVars(scip) - SCIPgetNContVars(scip);
5757 
5758  *cutoff = FALSE;
5759 
5760  if( nbinvars == 0 )
5761  return SCIP_OKAY;
5762 
5763  /* set up data structures */
5764  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nbinvars) );
5765  SCIP_CALL( SCIPallocBufferArray(scip, &consvals, nbinvars) );
5766 
5767  /* get conshdlrdata to use cleared memory */
5768  conshdlr = SCIPfindConshdlr(scip, CONSHDLR_NAME);
5769  if( conshdlr == NULL )
5770  {
5771  noknapsackconshdlr = TRUE;
5772  usegubs = DEFAULT_USEGUBS;
5773 
5774  SCIP_CALL( SCIPallocBufferArray(scip, &binvals, nbinvars) );
5775  BMSclearMemoryArray(binvals, nbinvars);
5776  }
5777  else
5778  {
5779  noknapsackconshdlr = FALSE;
5780  conshdlrdata = SCIPconshdlrGetData(conshdlr);
5781  assert(conshdlrdata != NULL);
5782  usegubs = conshdlrdata->usegubs;
5783 
5784  SCIP_CALL( SCIPallocBufferArray(scip, &tmpindices, nknapvars) );
5785 
5786  /* increase array size to avoid an endless loop in the next block; this might happen if continuous variables
5787  * change their types to SCIP_VARTYPE_BINARY during presolving
5788  */
5789  if( conshdlrdata->reals1size == 0 )
5790  {
5791  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &conshdlrdata->reals1, conshdlrdata->reals1size, 1) );
5792  conshdlrdata->reals1size = 1;
5793  conshdlrdata->reals1[0] = 0.0;
5794  }
5795 
5796  assert(conshdlrdata->reals1size > 0);
5797 
5798  /* next if condition should normally not be true, because it means that presolving has created more binary
5799  * variables than binary + integer variables existed at the constraint initialization method, but for example if you would
5800  * transform all integers into their binary representation then it maybe happens
5801  */
5802  if( conshdlrdata->reals1size < nbinvars )
5803  {
5804  int oldsize = conshdlrdata->reals1size;
5805 
5806  conshdlrdata->reals1size = nbinvars;
5807  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &conshdlrdata->reals1, oldsize, conshdlrdata->reals1size) );
5808  BMSclearMemoryArray(&(conshdlrdata->reals1[oldsize]), conshdlrdata->reals1size - oldsize); /*lint !e866 */
5809  }
5810  binvals = conshdlrdata->reals1;
5811 
5812  /* check for cleared array, all entries have to be zero */
5813 #ifndef NDEBUG
5814  for( tmp = nbinvars - 1; tmp >= 0; --tmp )
5815  {
5816  assert(binvals[tmp] == 0);
5817  }
5818 #endif
5819  }
5820 
5821  tmp = 0;
5822 
5823  /* relax continuous knapsack constraint:
5824  * 1. make all variables binary:
5825  * if x_j is continuous or integer variable substitute:
5826  * - a_j < 0: x_j = lb or x_j = b*z + d with variable lower bound b*z + d with binary variable z
5827  * - a_j > 0: x_j = ub or x_j = b*z + d with variable upper bound b*z + d with binary variable z
5828  * 2. convert coefficients of all variables to positive integers:
5829  * - scale all coefficients a_j to a~_j integral
5830  * - substitute x~_j = 1 - x_j if a~_j < 0
5831  */
5832 
5833  /* replace integer and continuous variables with binary variables */
5834  for( i = 0; i < nknapvars; i++ )
5835  {
5836  SCIP_VAR* var;
5837 
5838  var = knapvars[i];
5839 
5840  if( SCIPvarIsBinary(var) && SCIPvarIsActive(var) )
5841  {
5842  SCIP_Real solval;
5843  assert(0 <= SCIPvarGetProbindex(var) && SCIPvarGetProbindex(var) < nbinvars);
5844 
5845  solval = SCIPgetSolVal(scip, sol, var);
5846 
5847  /* knapsack relaxation assumes solution values between 0.0 and 1.0 for binary variables */
5848  if( SCIPisFeasLT(scip, solval, 0.0 )
5849  || SCIPisFeasGT(scip, solval, 1.0) )
5850  {
5851  SCIPdebugMsg(scip, "Solution value %.15g <%s> outside domain [0.0, 1.0]\n",
5852  solval, SCIPvarGetName(var));
5853  goto TERMINATE;
5854  }
5855 
5856  binvals[SCIPvarGetProbindex(var)] += valscale * knapvals[i];
5857  if( !noknapsackconshdlr )
5858  {
5859  assert(tmpindices != NULL);
5860 
5861  tmpindices[tmp] = SCIPvarGetProbindex(var);
5862  ++tmp;
5863  }
5864  SCIPdebugMsg(scip, " -> binary variable %+.15g<%s>(%.15g)\n", valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var));
5865  }
5866  else if( valscale * knapvals[i] > 0.0 )
5867  {
5868  SCIP_VAR** zvlb;
5869  SCIP_Real* bvlb;
5870  SCIP_Real* dvlb;
5871  SCIP_Real bestlbsol;
5872  int bestlbtype;
5873  int nvlb;
5874  int j;
5875 
5876  /* a_j > 0: substitution with lb or vlb */
5877  nvlb = SCIPvarGetNVlbs(var);
5878  zvlb = SCIPvarGetVlbVars(var);
5879  bvlb = SCIPvarGetVlbCoefs(var);
5880  dvlb = SCIPvarGetVlbConstants(var);
5881 
5882  /* search for lb or vlb with maximal bound value */
5883  bestlbsol = SCIPvarGetLbGlobal(var);
5884  bestlbtype = -1;
5885  for( j = 0; j < nvlb; j++ )
5886  {
5887  /* use only numerical stable vlb with binary variable z */
5888  if( SCIPvarIsBinary(zvlb[j]) && SCIPvarIsActive(zvlb[j]) && REALABS(bvlb[j]) <= MAXABSVBCOEF )
5889  {
5890  SCIP_Real vlbsol;
5891 
5892  if( (bvlb[j] >= 0.0 && SCIPisGT(scip, bvlb[j] * SCIPvarGetLbLocal(zvlb[j]) + dvlb[j], SCIPvarGetUbLocal(var))) ||
5893  (bvlb[j] <= 0.0 && SCIPisGT(scip, bvlb[j] * SCIPvarGetUbLocal(zvlb[j]) + dvlb[j], SCIPvarGetUbLocal(var))) )
5894  {
5895  *cutoff = TRUE;
5896  SCIPdebugMsg(scip, "variable bound <%s>[%g,%g] >= %g<%s>[%g,%g] + %g implies local cutoff\n",
5898  bvlb[j], SCIPvarGetName(zvlb[j]), SCIPvarGetLbLocal(zvlb[j]), SCIPvarGetUbLocal(zvlb[j]), dvlb[j]);
5899  goto TERMINATE;
5900  }
5901 
5902  assert(0 <= SCIPvarGetProbindex(zvlb[j]) && SCIPvarGetProbindex(zvlb[j]) < nbinvars);
5903  vlbsol = bvlb[j] * SCIPgetSolVal(scip, sol, zvlb[j]) + dvlb[j];
5904  if( SCIPisGE(scip, vlbsol, bestlbsol) )
5905  {
5906  bestlbsol = vlbsol;
5907  bestlbtype = j;
5908  }
5909  }
5910  }
5911 
5912  /* if no lb or vlb with binary variable was found, we have to abort */
5913  if( SCIPisInfinity(scip, -bestlbsol) )
5914  goto TERMINATE;
5915 
5916  if( bestlbtype == -1 )
5917  {
5918  rhs -= valscale * knapvals[i] * bestlbsol;
5919  SCIPdebugMsg(scip, " -> non-binary variable %+.15g<%s>(%.15g) replaced with lower bound %.15g (rhs=%.15g)\n",
5920  valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var), SCIPvarGetLbGlobal(var), rhs);
5921  }
5922  else
5923  {
5924  assert(0 <= SCIPvarGetProbindex(zvlb[bestlbtype]) && SCIPvarGetProbindex(zvlb[bestlbtype]) < nbinvars);
5925  rhs -= valscale * knapvals[i] * dvlb[bestlbtype];
5926  binvals[SCIPvarGetProbindex(zvlb[bestlbtype])] += valscale * knapvals[i] * bvlb[bestlbtype];
5927 
5928  if( SCIPisInfinity(scip, REALABS(binvals[SCIPvarGetProbindex(zvlb[bestlbtype])])) )
5929  goto TERMINATE;
5930 
5931  if( !noknapsackconshdlr )
5932  {
5933  assert(tmpindices != NULL);
5934 
5935  tmpindices[tmp] = SCIPvarGetProbindex(zvlb[bestlbtype]);
5936  ++tmp;
5937  }
5938  SCIPdebugMsg(scip, " -> non-binary variable %+.15g<%s>(%.15g) replaced with variable lower bound %+.15g<%s>(%.15g) %+.15g (rhs=%.15g)\n",
5939  valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var),
5940  bvlb[bestlbtype], SCIPvarGetName(zvlb[bestlbtype]),
5941  SCIPgetSolVal(scip, sol, zvlb[bestlbtype]), dvlb[bestlbtype], rhs);
5942  }
5943  }
5944  else
5945  {
5946  SCIP_VAR** zvub;
5947  SCIP_Real* bvub;
5948  SCIP_Real* dvub;
5949  SCIP_Real bestubsol;
5950  int bestubtype;
5951  int nvub;
5952  int j;
5953 
5954  assert(valscale * knapvals[i] < 0.0);
5955 
5956  /* a_j < 0: substitution with ub or vub */
5957  nvub = SCIPvarGetNVubs(var);
5958  zvub = SCIPvarGetVubVars(var);
5959  bvub = SCIPvarGetVubCoefs(var);
5960  dvub = SCIPvarGetVubConstants(var);
5961 
5962  /* search for ub or vub with minimal bound value */
5963  bestubsol = SCIPvarGetUbGlobal(var);
5964  bestubtype = -1;
5965  for( j = 0; j < nvub; j++ )
5966  {
5967  /* use only numerical stable vub with active binary variable z */
5968  if( SCIPvarIsBinary(zvub[j]) && SCIPvarIsActive(zvub[j]) && REALABS(bvub[j]) <= MAXABSVBCOEF )
5969  {
5970  SCIP_Real vubsol;
5971 
5972  if( (bvub[j] >= 0.0 && SCIPisLT(scip, bvub[j] * SCIPvarGetUbLocal(zvub[j]) + dvub[j], SCIPvarGetLbLocal(var))) ||
5973  (bvub[j] <= 0.0 && SCIPisLT(scip, bvub[j] * SCIPvarGetLbLocal(zvub[j]) + dvub[j], SCIPvarGetLbLocal(var))) )
5974  {
5975  *cutoff = TRUE;
5976  SCIPdebugMsg(scip, "variable bound <%s>[%g,%g] <= %g<%s>[%g,%g] + %g implies local cutoff\n",
5978  bvub[j], SCIPvarGetName(zvub[j]), SCIPvarGetLbLocal(zvub[j]), SCIPvarGetUbLocal(zvub[j]), dvub[j]);
5979  goto TERMINATE;
5980  }
5981 
5982  assert(0 <= SCIPvarGetProbindex(zvub[j]) && SCIPvarGetProbindex(zvub[j]) < nbinvars);
5983  vubsol = bvub[j] * SCIPgetSolVal(scip, sol, zvub[j]) + dvub[j];
5984  if( SCIPisLE(scip, vubsol, bestubsol) )
5985  {
5986  bestubsol = vubsol;
5987  bestubtype = j;
5988  }
5989  }
5990  }
5991 
5992  /* if no ub or vub with binary variable was found, we have to abort */
5993  if( SCIPisInfinity(scip, bestubsol) )
5994  goto TERMINATE;
5995 
5996  if( bestubtype == -1 )
5997  {
5998  rhs -= valscale * knapvals[i] * bestubsol;
5999  SCIPdebugMsg(scip, " -> non-binary variable %+.15g<%s>(%.15g) replaced with upper bound %.15g (rhs=%.15g)\n",
6000  valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var), SCIPvarGetUbGlobal(var), rhs);
6001  }
6002  else
6003  {
6004  assert(0 <= SCIPvarGetProbindex(zvub[bestubtype]) && SCIPvarGetProbindex(zvub[bestubtype]) < nbinvars);
6005  rhs -= valscale * knapvals[i] * dvub[bestubtype];
6006  binvals[SCIPvarGetProbindex(zvub[bestubtype])] += valscale * knapvals[i] * bvub[bestubtype];
6007 
6008  if( SCIPisInfinity(scip, REALABS(binvals[SCIPvarGetProbindex</