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