Scippy

SCIP

Solving Constraint Integer Programs

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