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