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