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-2014 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file cons_knapsack.c
17  * @brief Constraint handler for knapsack constraints of the form \f$a^T x \le b\f$, x binary and \f$a \ge 0\f$.
18  * @author Tobias Achterberg
19  * @author Xin Liu
20  * @author Kati Wolter
21  * @author Michael Winkler
22  */
23 
24 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
25 
26 #include <assert.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 
32 #include "scip/cons_knapsack.h"
33 #include "scip/cons_linear.h"
34 #include "scip/cons_logicor.h"
35 #include "scip/cons_setppc.h"
36 #include "scip/pub_misc.h"
37 
38 /* constraint handler properties */
39 #define CONSHDLR_NAME "knapsack"
40 #define CONSHDLR_DESC "knapsack constraint of the form a^T x <= b, x binary and a >= 0"
41 #define CONSHDLR_SEPAPRIORITY +600000 /**< priority of the constraint handler for separation */
42 #define CONSHDLR_ENFOPRIORITY -600000 /**< priority of the constraint handler for constraint enforcing */
43 #define CONSHDLR_CHECKPRIORITY -600000 /**< priority of the constraint handler for checking feasibility */
44 #define CONSHDLR_SEPAFREQ 0 /**< frequency for separating cuts; zero means to separate only in the root node */
45 #define CONSHDLR_PROPFREQ 1 /**< frequency for propagating domains; zero means only preprocessing propagation */
46 #define CONSHDLR_EAGERFREQ 100 /**< frequency for using all instead of only the useful constraints in separation,
47  * propagation and enforcement, -1 for no eager evaluations, 0 for first only */
48 #define CONSHDLR_MAXPREROUNDS -1 /**< maximal number of presolving rounds the constraint handler participates in (-1: no limit) */
49 #define CONSHDLR_DELAYSEPA FALSE /**< should separation method be delayed, if other separators found cuts? */
50 #define CONSHDLR_DELAYPROP FALSE /**< should propagation method be delayed, if other propagators found reductions? */
51 #define CONSHDLR_DELAYPRESOL FALSE /**< should presolving method be delayed, if other presolvers found reductions? */
52 #define CONSHDLR_NEEDSCONS TRUE /**< should the constraint handler be skipped, if no constraints are available? */
53 
54 #define CONSHDLR_PROP_TIMING SCIP_PROPTIMING_BEFORELP
55 
56 #define EVENTHDLR_NAME "knapsack"
57 #define EVENTHDLR_DESC "bound change event handler for knapsack constraints"
58 
59 #define LINCONSUPGD_PRIORITY +100000 /**< priority of the constraint handler for upgrading of linear constraints */
60 
61 #if 0
62 #define MAX_DYNPROG_CAPACITY 10000 /**< maximal capacity of knapsack to apply dynamic programming */
63 #endif
64 
65 #define MAX_USECLIQUES_SIZE 1000 /**< maximal number of items in knapsack where clique information is used */
66 #define MAX_ZEROITEMS_SIZE 10000 /**< maximal number of items to store in the zero list in preprocessing */
67 
68 #define KNAPSACKRELAX_MAXDELTA 0.1 /**< maximal allowed rounding distance for scaling in knapsack relaxation */
69 #define KNAPSACKRELAX_MAXDNOM 1000LL /**< maximal allowed denominator in knapsack rational relaxation */
70 #define KNAPSACKRELAX_MAXSCALE 1000.0 /**< maximal allowed scaling factor in knapsack rational relaxation */
71 
72 #define DEFAULT_SEPACARDFREQ 1 /**< multiplier on separation frequency, how often knapsack cuts are separated */
73 #define DEFAULT_MAXROUNDS 5 /**< maximal number of separation rounds per node (-1: unlimited) */
74 #define DEFAULT_MAXROUNDSROOT -1 /**< maximal number of separation rounds in the root node (-1: unlimited) */
75 #define DEFAULT_MAXSEPACUTS 50 /**< maximal number of cuts separated per separation round */
76 #define DEFAULT_MAXSEPACUTSROOT 200 /**< maximal number of cuts separated per separation round in the root node */
77 #define DEFAULT_MAXCARDBOUNDDIST 0.0 /**< maximal relative distance from current node's dual bound to primal bound compared
78  * to best node's dual bound for separating knapsack cuts */
79 #define DEFAULT_DISAGGREGATION TRUE /**< should disaggregation of knapsack constraints be allowed in preprocessing? */
80 #define DEFAULT_SIMPLIFYINEQUALITIES TRUE/**< should presolving try to simplify knapsacks */
81 #define DEFAULT_NEGATEDCLIQUE TRUE /**< should negated clique information be used in solving process */
82 
83 #define MAXABSVBCOEF 1e+5 /**< maximal absolute coefficient in variable bounds used for knapsack relaxation */
84 #define USESUPADDLIFT FALSE /**< should lifted minimal cover inequalities using superadditive up-lifting be separated in addition */
85 
86 #define DEFAULT_PRESOLUSEHASHING TRUE /**< should hash table be used for detecting redundant constraints in advance */
87 #define HASHSIZE_KNAPSACKCONS 131101 /**< minimal size of hash table in linear constraint tables */
88 
89 #define DEFAULT_PRESOLPAIRWISE TRUE /**< should pairwise constraint comparison be performed in presolving? */
90 #define NMINCOMPARISONS 200000 /**< number for minimal pairwise presolving comparisons */
91 #define MINGAINPERNMINCOMPARISONS 1e-06 /**< minimal gain per minimal pairwise presolving comparisons to repeat pairwise
92  * comparison round */
93 #define DEFAULT_DUALPRESOLVING TRUE /**< should dual presolving steps be performed? */
94 #define DEFAULT_DETECTCUTOFFBOUND TRUE /**< should presolving try to detect constraints parallel to the objective
95  * function defining an upper bound and prevent these constraints from
96  * entering the LP */
97 #define DEFAULT_DETECTLOWERBOUND TRUE /**< should presolving try to detect constraints parallel to the objective
98  * function defining a lower bound and prevent these constraints from
99  * entering the LP */
100 
101 #define MAXCOVERSIZEITERLEWI 1000 /**< maximal size for which LEWI are iteratively separated by reducing the feasible set */
102 
103 #define DEFAULT_USEGUBS FALSE /**< should GUB information be used for separation? */
104 #define GUBCONSGROWVALUE 6 /**< memory growing value for GUB constraint array */
105 #define GUBSPLITGNC1GUBS FALSE /**< should GNC1 GUB conss without F vars be split into GOC1 and GR GUB conss? */
106 
107 
108 /* @todo maybe use event SCIP_EVENTTYPE_VARUNLOCKED to decide for another dual-presolving run on a constraint */
109 
110 /*
111  * Data structures
112  */
113 
114 /** constraint handler data */
115 struct SCIP_ConshdlrData
116 {
117  int* ints1; /**< cleared memory array, all entries are set to zero in initpre, if you use this
118  * you have to clear it at the end, exists only in presolving stage */
119  int* ints2; /**< cleared memory array, all entries are set to zero in initpre, if you use this
120  * you have to clear it at the end, exists only in presolving stage */
121  SCIP_Longint* longints1; /**< cleared memory array, all entries are set to zero in initpre, if you use this
122  * you have to clear it at the end, exists only in presolving stage */
123  SCIP_Longint* longints2; /**< cleared memory array, all entries are set to zero in initpre, if you use this
124  * you have to clear it at the end, exists only in presolving stage */
125  SCIP_Bool* bools1; /**< cleared memory array, all entries are set to zero in initpre, if you use this
126  * you have to clear it at the end, exists only in presolving stage */
127  SCIP_Bool* bools2; /**< cleared memory array, all entries are set to zero in initpre, if you use this
128  * you have to clear it at the end, exists only in presolving stage */
129  SCIP_Bool* bools3; /**< cleared memory array, all entries are set to zero in initpre, if you use this
130  * you have to clear it at the end, exists only in presolving stage */
131  SCIP_Bool* bools4; /**< cleared memory array, all entries are set to zero in initpre, if you use this
132  * you have to clear it at the end, exists only in presolving stage */
133  SCIP_Real* reals1; /**< cleared memory array, all entries are set to zero in consinit, if you use this
134  * you have to clear it at the end */
135  int ints1size; /**< size of ints1 array */
136  int ints2size; /**< size of ints2 array */
137  int longints1size; /**< size of longints1 array */
138  int longints2size; /**< size of longints2 array */
139  int bools1size; /**< size of bools1 array */
140  int bools2size; /**< size of bools2 array */
141  int bools3size; /**< size of bools3 array */
142  int bools4size; /**< size of bools4 array */
143  int reals1size; /**< size of reals1 array */
144  SCIP_EVENTHDLR* eventhdlr; /**< event handler for bound change events */
145  SCIP_Real maxcardbounddist; /**< maximal relative distance from current node's dual bound to primal bound compared
146  * to best node's dual bound for separating knapsack cuts */
147  int sepacardfreq; /**< multiplier on separation frequency, how often knapsack cuts are separated */
148  int maxrounds; /**< maximal number of separation rounds per node (-1: unlimited) */
149  int maxroundsroot; /**< maximal number of separation rounds in the root node (-1: unlimited) */
150  int maxsepacuts; /**< maximal number of cuts separated per separation round */
151  int maxsepacutsroot; /**< maximal number of cuts separated per separation round in the root node */
152  SCIP_Bool disaggregation; /**< should disaggregation of knapsack constraints be allowed in preprocessing? */
153  SCIP_Bool simplifyinequalities;/**< should presolving try to cancel down or delete coefficients in inequalities */
154  SCIP_Bool negatedclique; /**< should negated clique information be used in solving process */
155  SCIP_Bool presolpairwise; /**< should pairwise constraint comparison be performed in presolving? */
156  SCIP_Bool presolusehashing; /**< should hash table be used for detecting redundant constraints in advance */
157  SCIP_Bool dualpresolving; /**< should dual presolving steps be performed? */
158  SCIP_Bool usegubs; /**< should GUB information be used for separation? */
159  SCIP_Bool detectcutoffbound; /**< should presolving try to detect constraints parallel to the objective
160  * function defining an upper bound and prevent these constraints from
161  * entering the LP */
162  SCIP_Bool detectlowerbound; /**< should presolving try to detect constraints parallel to the objective
163  * function defining a lower bound and prevent these constraints from
164  * entering the LP */
165 };
166 
167 
168 /** constraint data for knapsack constraints */
169 struct SCIP_ConsData
170 {
171  SCIP_VAR** vars; /**< variables in knapsack constraint */
172  SCIP_Longint* weights; /**< weights of variables in knapsack constraint */
173  SCIP_EVENTDATA** eventdatas; /**< event datas for bound change events of the variables */
174  int* cliquepartition; /**< clique indices of the clique partition */
175  int* negcliquepartition; /**< clique indices of the negated clique partition */
176  SCIP_ROW* row; /**< corresponding LP row */
177  int nvars; /**< number of variables in knapsack constraint */
178  int varssize; /**< size of vars, weights, and eventdatas arrays */
179  int ncliques; /**< number of cliques in the clique partition */
180  int nnegcliques; /**< number of cliques in the negated clique partition */
181  SCIP_Longint capacity; /**< capacity of knapsack */
182  SCIP_Longint weightsum; /**< sum of all weights */
183  SCIP_Longint onesweightsum; /**< sum of weights of variables fixed to one */
184  unsigned int propagated:1; /**< is the knapsack constraint already propagated? */
185  unsigned int presolved:1; /**< is the knapsack constraint already presolved? */
186  unsigned int sorted:1; /**< are the knapsack items sorted by weight? */
187  unsigned int cliquepartitioned:1;/**< is the clique partition valid? */
188  unsigned int negcliquepartitioned:1;/**< is the negated clique partition valid? */
189  unsigned int merged:1; /**< are the constraint's equal variables already merged? */
190  unsigned int cliquesadded:1; /**< were the cliques of the knapsack already added to clique table? */
191  unsigned int varsdeleted:1; /**< were variables deleted after last cleanup? */
192  unsigned int existmultaggr:1; /**< does this constraint contain multi-aggregations */
193 };
194 
195 
196 /** event data for bound changes events */
197 struct SCIP_EventData
198 {
199  SCIP_CONSDATA* consdata; /**< knapsack constraint data to process the bound change for */
200  SCIP_Longint weight; /**< weight of variable */
201  int filterpos; /**< position of event in variable's event filter */
202 };
203 
204 
205 /** data structure to combine two sorting key values */
206 struct sortkeypair
207 {
208  SCIP_Real key1; /**< first sort key value */
209  SCIP_Real key2; /**< second sort key value */
210 };
211 typedef struct sortkeypair SORTKEYPAIR;
212 
213 /** status of GUB constraint */
215 {
216  GUBVARSTATUS_UNINITIAL = -1, /** unintitialized variable status */
217  GUBVARSTATUS_CAPACITYEXCEEDED = 0, /** variable with weight exceeding the knapsack capacity */
218  GUBVARSTATUS_BELONGSTOSET_R = 1, /** variable in noncovervars R */
219  GUBVARSTATUS_BELONGSTOSET_F = 2, /** variable in noncovervars F */
220  GUBVARSTATUS_BELONGSTOSET_C2 = 3, /** variable in covervars C2 */
221  GUBVARSTATUS_BELONGSTOSET_C1 = 4 /** variable in covervars C1 */
222 };
224 
225 /** status of variable in GUB constraint */
227 {
228  GUBCONSSTATUS_UNINITIAL = -1, /** unintitialized GUB constraint status */
229  GUBCONSSTATUS_BELONGSTOSET_GR = 0, /** all GUB variables are in noncovervars R */
230  GUBCONSSTATUS_BELONGSTOSET_GF = 1, /** all GUB variables are in noncovervars F (and noncovervars R) */
231  GUBCONSSTATUS_BELONGSTOSET_GC2 = 2, /** all GUB variables are in covervars C2 */
232  GUBCONSSTATUS_BELONGSTOSET_GNC1 = 3, /** some GUB variables are in covervars C1, others in noncovervars R or F */
233  GUBCONSSTATUS_BELONGSTOSET_GOC1 = 4 /** all GUB variables are in covervars C1 */
234 };
236 
237 /** data structure of GUB constraints */
239 {
240  int* gubvars; /**< indices of GUB variables in knapsack constraint */
241  GUBVARSTATUS* gubvarsstatus; /**< status of GUB variables */
242  int ngubvars; /**< number of GUB variables */
243  int gubvarssize; /**< size of gubvars array */
244 };
245 typedef struct SCIP_GUBCons SCIP_GUBCONS;
246 
247 /** data structure of a set of GUB constraints */
249 {
250  SCIP_GUBCONS** gubconss; /**< GUB constraints in GUB set */
251  GUBCONSSTATUS* gubconsstatus; /**< status of GUB constraints */
252  int ngubconss; /**< number of GUB constraints */
253  int nvars; /**< number of variables in knapsack constraint */
254  int* gubconssidx; /**< index of GUB constraint (in gubconss array) of each knapsack variable */
255  int* gubvarsidx; /**< index in GUB constraint (in gubvars array) of each knapsack variable */
256 };
257 typedef struct SCIP_GUBSet SCIP_GUBSET;
258 
259 /*
260  * Local methods
261  */
262 
263 /** comparison method for two sorting key pairs */
264 static
265 SCIP_DECL_SORTPTRCOMP(compSortkeypairs)
266 {
267  SORTKEYPAIR* sortkeypair1 = (SORTKEYPAIR*)elem1;
268  SORTKEYPAIR* sortkeypair2 = (SORTKEYPAIR*)elem2;
269 
270  if( sortkeypair1->key1 < sortkeypair2->key1 )
271  return -1;
272  else if( sortkeypair1->key1 > sortkeypair2->key1 )
273  return +1;
274  else if( sortkeypair1->key2 < sortkeypair2->key2 )
275  return -1;
276  else if( sortkeypair1->key2 > sortkeypair2->key2 )
277  return +1;
278  else
279  return 0;
280 }
281 
282 /** creates event data */
283 static
285  SCIP* scip, /**< SCIP data structure */
286  SCIP_EVENTDATA** eventdata, /**< pointer to store event data */
287  SCIP_CONSDATA* consdata, /**< constraint data */
288  SCIP_Longint weight /**< weight of variable */
289  )
290 {
291  assert(eventdata != NULL);
292 
293  SCIP_CALL( SCIPallocBlockMemory(scip, eventdata) );
294  (*eventdata)->consdata = consdata;
295  (*eventdata)->weight = weight;
296 
297  return SCIP_OKAY;
298 }
299 
300 /** frees event data */
301 static
303  SCIP* scip, /**< SCIP data structure */
304  SCIP_EVENTDATA** eventdata /**< pointer to event data */
305  )
306 {
307  assert(eventdata != NULL);
308 
309  SCIPfreeBlockMemory(scip, eventdata);
310 
311  return SCIP_OKAY;
312 }
313 
314 /** sorts items in knapsack with nonincreasing weights */
315 static
317  SCIP_CONSDATA* consdata /**< constraint data */
318  )
319 {
320  assert(consdata != NULL);
321  assert(consdata->nvars == 0 || consdata->vars != NULL);
322  assert(consdata->nvars == 0 || consdata->weights != NULL);
323  assert(consdata->nvars == 0 || consdata->eventdatas != NULL);
324  assert(consdata->nvars == 0 || (consdata->cliquepartition != NULL && consdata->negcliquepartition != NULL));
325 
326  if( !consdata->sorted )
327  {
328  int pos;
329  int lastcliquenum;
330  int v;
331 
332  /* sort of five joint arrays of Long/pointer/pointer/ints/ints,
333  * sorted by first array in non-increasing order via sort template */
335  consdata->weights,
336  (void**)consdata->vars,
337  (void**)consdata->eventdatas,
338  consdata->cliquepartition,
339  consdata->negcliquepartition,
340  consdata->nvars);
341 
342  v = consdata->nvars - 1;
343  /* sort all items with same weight according to their variable index, used for hash value for fast pairwise comparison of all constraints */
344  while( v >= 0 )
345  {
346  int w = v - 1;
347 
348  while( w >= 0 && consdata->weights[v] == consdata->weights[w] )
349  --w;
350 
351  if( v - w > 1 )
352  {
353  /* sort all corresponding parts of arrays for which the weights are equal by using the variable index */
355  (void**)(&(consdata->vars[w+1])),
356  (void**)(&(consdata->eventdatas[w+1])),
357  &(consdata->cliquepartition[w+1]),
358  &(consdata->negcliquepartition[w+1]),
359  SCIPvarComp,
360  v - w);
361  }
362  v = w;
363  }
364 
365  /* we need to make sure that our clique numbers of our normal clique will be in increasing order without gaps */
366  if( consdata->cliquepartitioned )
367  {
368  lastcliquenum = 0;
369 
370  for( pos = 0; pos < consdata->nvars; ++pos )
371  {
372  /* if the clique number in the normal clique at position pos is greater than the last found clique number the
373  * partition is invalid */
374  if( consdata->cliquepartition[pos] > lastcliquenum )
375  {
376  consdata->cliquepartitioned = FALSE;
377  break;
378  }
379  else if( consdata->cliquepartition[pos] == lastcliquenum )
380  ++lastcliquenum;
381  }
382  }
383  /* we need to make sure that our clique numbers of our negated clique will be in increasing order without gaps */
384  if( consdata->negcliquepartitioned )
385  {
386  lastcliquenum = 0;
387 
388  for( pos = 0; pos < consdata->nvars; ++pos )
389  {
390  /* if the clique number in the negated clique at position pos is greater than the last found clique number the
391  * partition is invalid */
392  if( consdata->negcliquepartition[pos] > lastcliquenum )
393  {
394  consdata->negcliquepartitioned = FALSE;
395  break;
396  }
397  else if( consdata->negcliquepartition[pos] == lastcliquenum )
398  ++lastcliquenum;
399  }
400  }
401 
402  consdata->sorted = TRUE;
403  }
404 #ifndef NDEBUG
405  {
406  /* check if the weight array is sorted in a non-increasing way */
407  int i;
408  for( i = 0; i < consdata->nvars-1; ++i )
409  assert(consdata->weights[i] >= consdata->weights[i+1]);
410  }
411 #endif
412 }
413 
414 /** calculates a partition of the variables into cliques */
415 static
417  SCIP* scip, /**< SCIP data structure */
418  SCIP_CONSDATA* consdata, /**< constraint data */
419  SCIP_Bool normalclique, /**< Should normal cliquepartition be created? */
420  SCIP_Bool negatedclique /**< Should negated cliquepartition be created? */
421  )
422 {
423  assert(consdata != NULL);
424  assert(consdata->nvars == 0 || (consdata->cliquepartition != NULL && consdata->negcliquepartition != NULL));
425 
426  if( normalclique && !consdata->cliquepartitioned )
427  {
428  SCIP_CALL( SCIPcalcCliquePartition(scip, consdata->vars, consdata->nvars, consdata->cliquepartition, &consdata->ncliques) );
429  consdata->cliquepartitioned = TRUE;
430  }
431 
432  if( negatedclique && !consdata->negcliquepartitioned )
433  {
434  SCIP_CALL( SCIPcalcNegatedCliquePartition(scip, consdata->vars, consdata->nvars, consdata->negcliquepartition, &consdata->nnegcliques) );
435  consdata->negcliquepartitioned = TRUE;
436  }
437 
438  assert(!consdata->cliquepartitioned || consdata->ncliques <= consdata->nvars);
439  assert(!consdata->negcliquepartitioned || consdata->nnegcliques <= consdata->nvars);
440 
441  return SCIP_OKAY;
442 }
443 
444 /** installs rounding locks for the given variable in the given knapsack constraint */
445 static
447  SCIP* scip, /**< SCIP data structure */
448  SCIP_CONS* cons, /**< knapsack constraint */
449  SCIP_VAR* var /**< variable of constraint entry */
450  )
451 {
452  /* rounding up may violate the constraint */
453  SCIP_CALL( SCIPlockVarCons(scip, var, cons, FALSE, TRUE) );
454 
455  return SCIP_OKAY;
456 }
457 
458 /** removes rounding locks for the given variable in the given knapsack constraint */
459 static
461  SCIP* scip, /**< SCIP data structure */
462  SCIP_CONS* cons, /**< knapsack constraint */
463  SCIP_VAR* var /**< variable of constraint entry */
464  )
465 {
466  /* rounding up may violate the constraint */
467  SCIP_CALL( SCIPunlockVarCons(scip, var, cons, FALSE, TRUE) );
468 
469  return SCIP_OKAY;
470 }
471 
472 /** catches bound change events for variables in knapsack */
473 static
475  SCIP* scip, /**< SCIP data structure */
476  SCIP_CONSDATA* consdata, /**< constraint data */
477  SCIP_EVENTHDLR* eventhdlr /**< event handler to call for the event processing */
478  )
479 {
480  int i;
481 
482  assert(consdata != NULL);
483  assert(consdata->nvars == 0 || consdata->vars != NULL);
484  assert(consdata->nvars == 0 || consdata->weights != NULL);
485  assert(consdata->nvars == 0 || consdata->eventdatas != NULL);
486 
487  for( i = 0; i < consdata->nvars; i++)
488  {
489  SCIP_CALL( eventdataCreate(scip, &consdata->eventdatas[i], consdata, consdata->weights[i]) );
490  SCIP_CALL( SCIPcatchVarEvent(scip, consdata->vars[i],
493  eventhdlr, consdata->eventdatas[i], &consdata->eventdatas[i]->filterpos) );
494  }
495 
496  return SCIP_OKAY;
497 }
498 
499 /** drops bound change events for variables in knapsack */
500 static
502  SCIP* scip, /**< SCIP data structure */
503  SCIP_CONSDATA* consdata, /**< constraint data */
504  SCIP_EVENTHDLR* eventhdlr /**< event handler to call for the event processing */
505  )
506 {
507  int i;
508 
509  assert(consdata != NULL);
510  assert(consdata->nvars == 0 || consdata->vars != NULL);
511  assert(consdata->nvars == 0 || consdata->weights != NULL);
512  assert(consdata->nvars == 0 || consdata->eventdatas != NULL);
513 
514  for( i = 0; i < consdata->nvars; i++)
515  {
516  SCIP_CALL( SCIPdropVarEvent(scip, consdata->vars[i],
519  eventhdlr, consdata->eventdatas[i], consdata->eventdatas[i]->filterpos) );
520  SCIP_CALL( eventdataFree(scip, &consdata->eventdatas[i]) );
521  }
522 
523  return SCIP_OKAY;
524 }
525 
526 /** ensures, that vars and vals arrays can store at least num entries */
527 static
529  SCIP* scip, /**< SCIP data structure */
530  SCIP_CONSDATA* consdata, /**< knapsack constraint data */
531  int num, /**< minimum number of entries to store */
532  SCIP_Bool transformed /**< is constraint from transformed problem? */
533  )
534 {
535  assert(consdata != NULL);
536  assert(consdata->nvars <= consdata->varssize);
537 
538  if( num > consdata->varssize )
539  {
540  int newsize;
541 
542  newsize = SCIPcalcMemGrowSize(scip, num);
543  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->vars, consdata->varssize, newsize) );
544  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->weights, consdata->varssize, newsize) );
545  if( transformed )
546  {
547  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->eventdatas, consdata->varssize, newsize) );
548  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->cliquepartition, consdata->varssize, newsize) );
549  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->negcliquepartition, consdata->varssize, newsize) );
550  }
551  else
552  {
553  assert(consdata->eventdatas == NULL);
554  assert(consdata->cliquepartition == NULL);
555  assert(consdata->negcliquepartition == NULL);
556  }
557  consdata->varssize = newsize;
558  }
559  assert(num <= consdata->varssize);
560 
561  return SCIP_OKAY;
562 }
563 
564 /** creates knapsack constraint data */
565 static
567  SCIP* scip, /**< SCIP data structure */
568  SCIP_CONSDATA** consdata, /**< pointer to store constraint data */
569  SCIP_EVENTHDLR* eventhdlr, /**< event handler to call for the event processing */
570  int nvars, /**< number of variables in knapsack */
571  SCIP_VAR** vars, /**< variables of knapsack */
572  SCIP_Longint* weights, /**< weights of knapsack items */
573  SCIP_Longint capacity /**< capacity of knapsack */
574  )
575 {
576  int v;
577 
578  assert(consdata != NULL);
579 
580  SCIP_CALL( SCIPallocBlockMemory(scip, consdata) );
581  if( nvars > 0 )
582  {
583  int k;
584 
585  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->vars, vars, nvars) );
586  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->weights, weights, nvars) );
587 
588  k = 0;
589  for( v = 0; v < nvars; ++v )
590  {
591  assert(vars[v] != NULL);
592 
593  /* all weight have to be not negative */
594  assert( weights[v] >= 0 );
595 
596  if( weights[v] > 0 )
597  {
598  (*consdata)->vars[k] = vars[v];
599  (*consdata)->weights[k] = weights[v];
600  ++k;
601  }
602  }
603  assert(k >= 0);
604 
605  (*consdata)->nvars = k;
606  if( k < nvars )
607  {
608  if( k > 0 )
609  {
610  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &(*consdata)->vars, nvars, k) );
611  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &(*consdata)->weights, nvars, k) );
612  }
613  else
614  {
615  SCIPfreeBlockMemoryArray(scip, &(*consdata)->vars, nvars);
616  SCIPfreeBlockMemoryArray(scip, &(*consdata)->weights, nvars);
617 
618  (*consdata)->vars = NULL;
619  (*consdata)->weights = NULL;
620  assert( (*consdata)->nvars == 0 );
621  }
622  }
623  }
624  else
625  {
626  (*consdata)->vars = NULL;
627  (*consdata)->weights = NULL;
628  (*consdata)->nvars = 0;
629  }
630 
631  /* capacity has to be greater or equal to zero */
632  assert( capacity >= 0 );
633 
634  (*consdata)->varssize = (*consdata)->nvars;
635  (*consdata)->capacity = capacity;
636  (*consdata)->eventdatas = NULL;
637  (*consdata)->cliquepartition = NULL;
638  (*consdata)->negcliquepartition = NULL;
639  (*consdata)->row = NULL;
640  (*consdata)->weightsum = 0;
641  (*consdata)->onesweightsum = 0;
642  (*consdata)->ncliques = 0;
643  (*consdata)->nnegcliques = 0;
644  (*consdata)->propagated = FALSE;
645  (*consdata)->presolved = FALSE;
646  (*consdata)->sorted = FALSE;
647  (*consdata)->cliquepartitioned = FALSE;
648  (*consdata)->negcliquepartitioned = FALSE;
649  (*consdata)->merged = FALSE;
650  (*consdata)->cliquesadded = FALSE;
651  (*consdata)->varsdeleted = FALSE;
652  (*consdata)->existmultaggr = FALSE;
653 
654  /* get transformed variables, if we are in the transformed problem */
655  if( SCIPisTransformed(scip) )
656  {
657  SCIP_CALL( SCIPgetTransformedVars(scip, (*consdata)->nvars, (*consdata)->vars, (*consdata)->vars) );
658 
659  for( v = 0; v < (*consdata)->nvars; v++ )
660  {
661  SCIP_VAR* var = SCIPvarGetProbvar((*consdata)->vars[v]);
662  assert(var != NULL);
663  (*consdata)->existmultaggr = (*consdata)->existmultaggr || (SCIPvarGetStatus(var) == SCIP_VARSTATUS_MULTAGGR);
664  }
665 
666  /* allocate memory for additional data structures */
667  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*consdata)->eventdatas, (*consdata)->nvars) );
668  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*consdata)->cliquepartition, (*consdata)->nvars) );
669  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*consdata)->negcliquepartition, (*consdata)->nvars) );
670 
671  /* catch events for variables */
672  SCIP_CALL( catchEvents(scip, *consdata, eventhdlr) );
673  }
674 
675  /* calculate sum of weights and capture variables */
676  for( v = 0; v < (*consdata)->nvars; ++v )
677  {
678  (*consdata)->weightsum += (*consdata)->weights[v];
679  if( SCIPvarGetLbLocal((*consdata)->vars[v]) > 0.5 )
680  (*consdata)->onesweightsum += (*consdata)->weights[v];
681 
682  /* capture variables */
683  SCIP_CALL( SCIPcaptureVar(scip, (*consdata)->vars[v]) );
684  }
685 
686  return SCIP_OKAY;
687 }
688 
689 /** frees knapsack constraint data */
690 static
692  SCIP* scip, /**< SCIP data structure */
693  SCIP_CONSDATA** consdata, /**< pointer to the constraint data */
694  SCIP_EVENTHDLR* eventhdlr /**< event handler to call for the event processing */
695  )
696 {
697  assert(consdata != NULL);
698  assert(*consdata != NULL);
699 
700  if( (*consdata)->row != NULL )
701  {
702  SCIP_CALL( SCIPreleaseRow(scip, &(*consdata)->row) );
703  }
704  if( (*consdata)->eventdatas != NULL )
705  {
706  SCIP_CALL( dropEvents(scip, *consdata, eventhdlr) );
707  SCIPfreeBlockMemoryArray(scip, &(*consdata)->eventdatas, (*consdata)->varssize);
708  }
709  if( (*consdata)->negcliquepartition != NULL )
710  {
711  SCIPfreeBlockMemoryArray(scip, &(*consdata)->negcliquepartition, (*consdata)->varssize);
712  }
713  if( (*consdata)->cliquepartition != NULL )
714  {
715  SCIPfreeBlockMemoryArray(scip, &(*consdata)->cliquepartition, (*consdata)->varssize);
716  }
717  if( (*consdata)->vars != NULL )
718  {
719  int v;
720 
721  /* release variables */
722  for( v = 0; v < (*consdata)->nvars; v++ )
723  {
724  assert((*consdata)->vars[v] != NULL);
725  SCIP_CALL( SCIPreleaseVar(scip, &((*consdata)->vars[v])) );
726  }
727 
728  assert( (*consdata)->weights != NULL );
729  assert( (*consdata)->varssize > 0 );
730  SCIPfreeBlockMemoryArray(scip, &(*consdata)->vars, (*consdata)->varssize);
731  SCIPfreeBlockMemoryArray(scip, &(*consdata)->weights, (*consdata)->varssize);
732  }
733 
734  SCIPfreeBlockMemory(scip, consdata);
735 
736  return SCIP_OKAY;
737 }
738 
739 /** changes a single weight in knapsack constraint data */
740 static
742  SCIP_CONSDATA* consdata, /**< knapsack constraint data */
743  int item, /**< item number */
744  SCIP_Longint newweight /**< new weight of item */
745  )
746 {
747  SCIP_Longint oldweight;
748 
749  assert(consdata != NULL);
750  assert(0 <= item && item < consdata->nvars);
751 
752  oldweight = consdata->weights[item];
753  consdata->weights[item] = newweight;
754  consdata->weightsum += (newweight - oldweight);
755 
756  if( SCIPvarGetLbLocal(consdata->vars[item]) > 0.5 )
757  consdata->onesweightsum += (newweight - oldweight);
758 
759  if( consdata->eventdatas != NULL )
760  {
761  assert(consdata->eventdatas[item] != NULL);
762  assert(consdata->eventdatas[item]->weight == oldweight);
763  consdata->eventdatas[item]->weight = newweight;
764  }
765 
766  consdata->propagated = FALSE;
767  consdata->presolved = FALSE;
768  consdata->sorted = FALSE;
769 
770  /* recalculate cliques extraction after a weight was increased */
771  if( oldweight < newweight )
772  {
773  consdata->cliquesadded = FALSE;
774  }
775 }
776 
777 /** creates LP row corresponding to knapsack constraint */
778 static
780  SCIP* scip, /**< SCIP data structure */
781  SCIP_CONS* cons /**< knapsack constraint */
782  )
783 {
784  SCIP_CONSDATA* consdata;
785  int i;
786 
787  consdata = SCIPconsGetData(cons);
788  assert(consdata != NULL);
789  assert(consdata->row == NULL);
790 
791  SCIP_CALL( SCIPcreateEmptyRowCons(scip, &consdata->row, SCIPconsGetHdlr(cons), SCIPconsGetName(cons),
792  -SCIPinfinity(scip), (SCIP_Real)consdata->capacity,
794 
795  SCIP_CALL( SCIPcacheRowExtensions(scip, consdata->row) );
796  for( i = 0; i < consdata->nvars; ++i )
797  {
798  SCIP_CALL( SCIPaddVarToRow(scip, consdata->row, consdata->vars[i], (SCIP_Real)consdata->weights[i]) );
799  }
800  SCIP_CALL( SCIPflushRowExtensions(scip, consdata->row) );
801 
802  return SCIP_OKAY;
803 }
804 
805 /** adds linear relaxation of knapsack constraint to the LP */
806 static
808  SCIP* scip, /**< SCIP data structure */
809  SCIP_CONS* cons, /**< knapsack constraint */
810  SCIP_SOL* sol, /**< primal CIP solution, NULL for current LP solution */
811  SCIP_Bool* cutoff /**< whether a cutoff has been detected */
812  )
813 {
814  SCIP_CONSDATA* consdata;
815 
816  assert( cutoff != NULL );
817  *cutoff = FALSE;
818 
819  consdata = SCIPconsGetData(cons);
820  assert(consdata != NULL);
821 
822  if( consdata->row == NULL )
823  {
824  SCIP_CALL( createRelaxation(scip, cons) );
825  }
826  assert(consdata->row != NULL);
827 
828  /* insert LP row as cut */
829  if( !SCIProwIsInLP(consdata->row) )
830  {
831  SCIPdebugMessage("adding relaxation of knapsack constraint <%s> (capacity %"SCIP_LONGINT_FORMAT"): ",
832  SCIPconsGetName(cons), consdata->capacity);
833  SCIPdebug( SCIP_CALL(SCIPprintRow(scip, consdata->row, NULL)) );
834  SCIP_CALL( SCIPaddCut(scip, sol, consdata->row, FALSE, cutoff) );
835  }
836 
837  return SCIP_OKAY;
838 }
839 
840 /** checks knapsack constraint for feasibility of given solution: returns TRUE iff constraint is feasible */
841 static
843  SCIP* scip, /**< SCIP data structure */
844  SCIP_CONS* cons, /**< constraint to check */
845  SCIP_SOL* sol, /**< solution to check, NULL for current solution */
846  SCIP_Bool checklprows, /**< should LP rows be checked? */
847  SCIP_Bool printreason, /**< should the reason for the violation be printed? */
848  SCIP_Bool* violated /**< pointer to store whether the constraint is violated */
849  )
850 {
851  SCIP_CONSDATA* consdata;
852 
853  assert(violated != NULL);
854 
855  consdata = SCIPconsGetData(cons);
856  assert(consdata != NULL);
857 
858  SCIPdebugMessage("checking knapsack constraint <%s> for feasibility of solution %p (lprows=%u)\n",
859  SCIPconsGetName(cons), (void*)sol, checklprows);
860 
861  *violated = FALSE;
862 
863  if( checklprows || consdata->row == NULL || !SCIProwIsInLP(consdata->row) )
864  {
865  SCIP_Real sum;
866  SCIP_Longint integralsum;
867  SCIP_Bool ishuge;
868  int v;
869 
870  /* increase age of constraint; age is reset to zero, if a violation was found only in case we are in
871  * enforcement
872  */
873  if( sol == NULL )
874  {
875  SCIP_CALL( SCIPincConsAge(scip, cons) );
876  }
877 
878  sum = 0.0;
879  integralsum = 0;
880  /* we perform an more exact comparison if the capacity does not exceed the huge value */
881  if( SCIPisHugeValue(scip, (SCIP_Real) consdata->capacity) )
882  {
883  ishuge = TRUE;
884 
885  /* sum over all weight times the corresponding solution value */
886  for( v = consdata->nvars - 1; v >= 0; --v )
887  {
888  assert(SCIPvarIsBinary(consdata->vars[v]));
889  sum += consdata->weights[v] * SCIPgetSolVal(scip, sol, consdata->vars[v]);
890  }
891  }
892  else
893  {
894  ishuge = FALSE;
895 
896  /* sum over all weight for which the variable has a solution value of 1 in feastol */
897  for( v = consdata->nvars - 1; v >= 0; --v )
898  {
899  assert(SCIPvarIsBinary(consdata->vars[v]));
900 
901  if( SCIPgetSolVal(scip, sol, consdata->vars[v]) > 0.5 )
902  integralsum += consdata->weights[v];
903  }
904  }
905 
906  if( (!ishuge && integralsum > consdata->capacity) || (ishuge && SCIPisFeasGT(scip, sum, (SCIP_Real)consdata->capacity)) )
907  {
908  *violated = TRUE;
909 
910  /* only reset constraint age if we are in enforcement */
911  if( sol == NULL )
912  {
913  SCIP_CALL( SCIPresetConsAge(scip, cons) );
914  }
915 
916  if( printreason )
917  {
918  SCIP_Real viol = ishuge ? sum : (SCIP_Real)integralsum;
919 
920  viol -= consdata->capacity;
921  assert(viol > 0);
922 
923  SCIP_CALL( SCIPprintCons(scip, cons, NULL) );
924 
925  SCIPinfoMessage(scip, NULL, ";\n");
926  SCIPinfoMessage(scip, NULL, "violation: the capacity is violated by %.15g\n", viol);
927  }
928  }
929  }
930 
931  return SCIP_OKAY;
932 }
933 
934 /* IDX computes the integer index for the optimal solution array */
935 #define IDX(j,d) ((j)*(intcap)+(d))
936 
937 /** solves knapsack problem in maximization form exactly using dynamic programming;
938  * if needed, one can provide arrays to store all selected items and all not selected items
939  *
940  * @note in case you provide the solitems or nonsolitems array you also have to provide the counter part as well
941  */
943  SCIP* scip, /**< SCIP data structure */
944  int nitems, /**< number of available items */
945  SCIP_Longint* weights, /**< item weights */
946  SCIP_Real* profits, /**< item profits */
947  SCIP_Longint capacity, /**< capacity of knapsack */
948  int* items, /**< item numbers */
949  int* solitems, /**< array to store items in solution, or NULL */
950  int* nonsolitems, /**< array to store items not in solution, or NULL */
951  int* nsolitems, /**< pointer to store number of items in solution, or NULL */
952  int* nnonsolitems, /**< pointer to store number of items not in solution, or NULL */
953  SCIP_Real* solval, /**< pointer to store optimal solution value, or NULL */
954  SCIP_Bool* success /**< pointer to store if an error occured during solving(normally a memory
955  * problem) */
956  )
957 {
958  SCIP_RETCODE retcode;
959  SCIP_Real* tempsort;
960  SCIP_Real* optvalues;
961  int intcap;
962  int d;
963  int j;
964  SCIP_Longint weightsum;
965  int* myitems;
966  SCIP_Longint* myweights;
967  int* allcurrminweight;
968  SCIP_Real* myprofits;
969  int nmyitems;
970  SCIP_Longint gcd;
971  SCIP_Longint minweight;
972  SCIP_Longint maxweight;
973  int currminweight;
974  SCIP_Longint greedycap;
975  SCIP_Longint greedysolweight;
976  SCIP_Real greedysolvalue;
977  SCIP_Bool eqweights;
978  SCIP_Bool isoptimal;
979  const size_t maxsize_t = (size_t)(-1);
980 
981  assert(weights != NULL);
982  assert(profits != NULL);
983  assert(capacity >= 0);
984  assert(items != NULL);
985  assert(nitems >= 0);
986  assert(success != NULL);
987 
988  *success = TRUE;
989 
990 #ifndef NDEBUG
991  for( j = nitems - 1; j >= 0; --j )
992  assert(weights[j] >= 0);
993 #endif
994 
995  SCIPdebugMessage("Solving knapsack exactly.\n");
996 
997  /* initializing solution value */
998  if( solval != NULL )
999  *solval = 0.0;
1000 
1001  /* produces optimal solution by following the table */
1002  if( solitems != NULL)
1003  {
1004  assert(items != NULL);
1005  assert(nsolitems != NULL);
1006  assert(nonsolitems != NULL);
1007  assert(nnonsolitems != NULL);
1008 
1009  *nnonsolitems = 0;
1010  *nsolitems = 0;
1011  }
1012 
1013  /* allocate temporary memory */
1014  SCIP_CALL( SCIPallocBufferArray(scip, &myweights, nitems) );
1015  SCIP_CALL( SCIPallocBufferArray(scip, &myprofits, nitems) );
1016  SCIP_CALL( SCIPallocBufferArray(scip, &myitems, nitems) );
1017  nmyitems = 0;
1018  weightsum = 0;
1019  minweight = SCIP_LONGINT_MAX;
1020  maxweight = 0;
1021 
1022  /* remove unnecessary items */
1023  for( j = 0; j < nitems; ++j )
1024  {
1025  assert(0 <= weights[j] && weights[j] < SCIP_LONGINT_MAX);
1026  /* items does not fit */
1027  if( weights[j] > capacity )
1028  {
1029  if( solitems != NULL)
1030  {
1031  nonsolitems[*nnonsolitems] = items[j];
1032  ++(*nnonsolitems);
1033  }
1034  }
1035  /* items we does not want */
1036  else if( profits[j] <= 0.0 )
1037  {
1038  if( solitems != NULL)
1039  {
1040  nonsolitems[*nnonsolitems] = items[j];
1041  ++(*nnonsolitems);
1042  }
1043  }
1044  /* items which always fit */
1045  else if( weights[j] == 0 )
1046  {
1047  if( solitems != NULL)
1048  {
1049  solitems[*nsolitems] = items[j];
1050  ++(*nsolitems);
1051  }
1052  if( solval != NULL )
1053  *solval += profits[j];
1054  }
1055  /* all important items */
1056  else
1057  {
1058  myweights[nmyitems] = weights[j];
1059  myprofits[nmyitems] = profits[j];
1060  myitems[nmyitems] = items[j];
1061 
1062  /* remember smallest item */
1063  if( myweights[nmyitems] < minweight )
1064  minweight = myweights[nmyitems];
1065 
1066  /* remember bigest item */
1067  if( myweights[nmyitems] > maxweight )
1068  maxweight = myweights[nmyitems];
1069 
1070  weightsum += myweights[nmyitems];
1071  ++nmyitems;
1072  }
1073  }
1074 
1075  /* no item is left then goto end */
1076  if( nmyitems == 0 )
1077  {
1078  SCIPdebugMessage("After preprocessing no items are left.\n");
1079 
1080  goto TERMINATE;
1081  }
1082  /* if all items fit, we also do not need to do the expensive stuff later on */
1083  else if( weightsum > 0 && weightsum <= capacity )
1084  {
1085  SCIPdebugMessage("After preprocessing all items fit into knapsack.\n");
1086 
1087  for( j = nmyitems - 1; j >= 0; --j )
1088  {
1089  if( solitems != NULL )
1090  {
1091  solitems[*nsolitems] = myitems[j];
1092  ++(*nsolitems);
1093  }
1094  if( solval != NULL )
1095  *solval += myprofits[j];
1096  }
1097 
1098  goto TERMINATE;
1099  }
1100 
1101  assert(minweight > 0);
1102  assert(maxweight > 0);
1103 
1104  if( maxweight > 1 )
1105  {
1106  /* determine greatest common divisor */
1107  gcd = myweights[nmyitems - 1];
1108  for( j = nmyitems - 2; j >= 0 && gcd >= 2; --j )
1109  gcd = SCIPcalcGreComDiv(gcd, myweights[j]);
1110 
1111  SCIPdebugMessage("Gcd is %"SCIP_LONGINT_FORMAT".\n", gcd);
1112 
1113  /* divide by greatest common divisor */
1114  if( gcd > 1 )
1115  {
1116  eqweights = TRUE;
1117  for( j = nmyitems - 1; j >= 0; --j )
1118  {
1119  myweights[j] /= gcd;
1120  eqweights = eqweights && (myweights[j] == 1);
1121  }
1122  capacity /= gcd;
1123  minweight /= gcd;
1124  }
1125  else
1126  eqweights = FALSE;
1127  }
1128  else
1129  {
1130  assert(maxweight == 1);
1131  eqweights = TRUE;
1132  }
1133 
1134  assert(minweight <= capacity);
1135 
1136  /* only one item fits, than take the best */
1137  if( minweight > capacity / 2 )
1138  {
1139  int p;
1140 
1141  SCIPdebugMessage("Only one item fits into knapsack, so take the best.\n");
1142 
1143  p = nmyitems - 1;
1144 
1145  /* find best item */
1146  for( j = nmyitems - 2; j >= 0; --j )
1147  if( myprofits[j] > myprofits[p] )
1148  p = j;
1149 
1150  /* update solution information */
1151  if( solitems != NULL)
1152  {
1153  solitems[*nsolitems] = myitems[p];
1154  ++(*nsolitems);
1155  for( j = nmyitems - 1; j >= 0; --j )
1156  if( j != p )
1157  {
1158  nonsolitems[*nnonsolitems] = myitems[j];
1159  ++(*nnonsolitems);
1160  }
1161  }
1162  /* update solution value */
1163  if( solval != NULL )
1164  *solval += myprofits[p];
1165 
1166  goto TERMINATE;
1167  }
1168 
1169  /* all items have the same weight, than take the best */
1170  if( eqweights )
1171  {
1172  SCIP_Real addval;
1173 
1174  SCIPdebugMessage("All weights are equal, so take the best.\n");
1175 
1176  SCIPsortDownRealIntLong(myprofits, myitems, myweights, nmyitems);
1177 
1178  addval = 0.0;
1179  /* update solution information */
1180  if( solitems != NULL || solval != NULL )
1181  {
1182  SCIP_Longint i;
1183 
1184  /* if all items would fit we had handled this case before */
1185  assert((SCIP_Longint) nmyitems > capacity);
1186 
1187  /* take the first best items into the solution */
1188  for( i = capacity - 1; i >= 0; --i )
1189  {
1190  if( solitems != NULL)
1191  {
1192  assert(nonsolitems != NULL);
1193  solitems[*nsolitems] = myitems[i];
1194  ++(*nsolitems);
1195  }
1196  addval += myprofits[i];
1197  }
1198 
1199  if( solitems != NULL)
1200  {
1201  assert(nonsolitems != NULL);
1202 
1203  /* the rest are not in the solution */
1204  for( i = nmyitems - 1; i >= capacity; --i )
1205  {
1206  nonsolitems[*nnonsolitems] = myitems[i];
1207  ++(*nnonsolitems);
1208  }
1209  }
1210  }
1211  /* update solution value */
1212  if( solval != NULL )
1213  {
1214  assert(addval > 0.0);
1215  *solval += addval;
1216  }
1217 
1218  goto TERMINATE;
1219  }
1220 
1221  /* in the following table we do not need the first minweight columns */
1222  capacity -= (minweight - 1);
1223 
1224  /* we can only handle integers */
1225  if( capacity >= INT_MAX )
1226  {
1227  SCIPdebugMessage("Capacity is to big, so we cannot handle it here.\n");
1228 
1229  *success = FALSE;
1230  goto TERMINATE;
1231  }
1232  assert(capacity < INT_MAX);
1233 
1234  intcap = (int)capacity;
1235  assert(intcap >= 0);
1236  assert(nmyitems > 0);
1237  assert(sizeof(size_t) >= sizeof(int)); /* no following conversion should be messed up */
1238 
1239  /* this condition checks if we will try to allocate a correct number of bytes and do not have an overflow, while
1240  * computing the size for the allocation
1241  */
1242  if( intcap < 0 || (intcap > 0 && (((size_t)nmyitems) > (maxsize_t / (size_t)intcap / sizeof(*optvalues)) || ((size_t)nmyitems) * ((size_t)intcap) * sizeof(*optvalues) > ((size_t)INT_MAX) )) )
1243  {
1244  SCIPdebugMessage("Too much memory (%lu) would be consumed.\n", (unsigned long) (((size_t)nmyitems) * ((size_t)intcap) * sizeof(*optvalues)));
1245 
1246  *success = FALSE;
1247  goto TERMINATE;
1248  }
1249 
1250  /* allocate temporary memory and check for memory exceeding */
1251  /* @note we do allocate normal memory instead of buffer memory, because the buffer, will not be deleted directly and
1252  * might be a really huge block of memory which we will not use later on
1253  */
1254  retcode = SCIPallocMemoryArray(scip, &optvalues, nmyitems * intcap);
1255  if( retcode == SCIP_NOMEMORY )
1256  {
1257  SCIPdebugMessage("Did not get enough memory.\n");
1258 
1259  *success = FALSE;
1260  goto TERMINATE;
1261  }
1262  else
1263  {
1264  SCIP_CALL( retcode );
1265  }
1266 
1267  /* sort myitems (plus corresponding arrays myweights and myprofits) such that
1268  * p_1/w_1 >= p_2/w_2 >= ... >= p_n/w_n, this is only use for greedy solution
1269  */
1270  SCIP_CALL( SCIPallocBufferArray(scip, &tempsort, nmyitems) );
1271  for( j = nmyitems - 1; j >= 0; --j )
1272  tempsort[j] = myprofits[j]/((SCIP_Real) myweights[j]);
1273 
1274  SCIPsortDownRealLongRealInt(tempsort, myweights, myprofits, myitems, nmyitems);
1275 
1276  /* initialize values for greedy solution information */
1277  greedysolweight = 0;
1278  greedysolvalue = 0.0;
1279  isoptimal = TRUE;
1280  greedycap = capacity + (minweight - 1);
1281 
1282  SCIPdebugMessage("Determine greedy solution.\n");
1283 
1284  /* determine greedy solution */
1285  for( j = 0; j < nmyitems; ++j )
1286  {
1287  assert(myweights[j] <= greedycap);
1288 
1289  /* take all fitting items */
1290  if( myweights[j] + greedysolweight <= greedycap )
1291  {
1292  /* update greedy solution weight and value */
1293  greedysolweight += myweights[j];
1294  greedysolvalue += myprofits[j];
1295  continue;
1296  }
1297  else if( greedysolweight < greedycap )
1298  isoptimal = FALSE;
1299  break;
1300  }
1301  assert(greedysolweight > 0);
1302  assert(greedysolvalue > 0.0);
1303 
1304  /* greedy solution is optimal */
1305  if( isoptimal )
1306  {
1307  assert(greedysolweight == greedycap);
1308 
1309  SCIPdebugMessage("Greedy solution is optimal.\n");
1310 
1311  greedysolweight = 0;
1312 
1313  /* update solution information */
1314  if( solitems != NULL)
1315  {
1316  /* take the first best items into the solution */
1317  for( j = 0; j < nmyitems; ++j )
1318  {
1319  /* take all fitting items */
1320  if( myweights[j] + greedysolweight <= greedycap )
1321  {
1322  solitems[*nsolitems] = myitems[j];
1323  ++(*nsolitems);
1324  greedysolweight += myweights[j];
1325  }
1326  else
1327  {
1328  nonsolitems[*nnonsolitems] = myitems[j];
1329  ++(*nnonsolitems);
1330  }
1331  }
1332  }
1333  /* update solution value */
1334  if( solval != NULL )
1335  {
1336  assert(greedysolvalue > 0.0);
1337  *solval += greedysolvalue;
1338  }
1339 
1340  SCIPfreeBufferArray(scip, &tempsort);
1341  SCIPfreeMemoryArray(scip, &optvalues);
1342 
1343  goto TERMINATE;
1344  }
1345 
1346  SCIPdebugMessage("Start real exact algorithm.\n");
1347 
1348  /* we memorize at each step the current minimal weight to later on know which value in our optvalues matrix is valid;
1349  * all values entries of the j-th row of optvalues is valid if the index is >= allcurrminweight[j], otherwise it is
1350  * invalid, a second possibility would be to clear the whole optvalues, which should be more expensive than storing
1351  * 'nmyitem' values
1352  */
1353  SCIP_CALL( SCIPallocBufferArray(scip, &allcurrminweight, nmyitems) );
1354  assert(myweights[0] - minweight < INT_MAX);
1355  currminweight = (int) (myweights[0] - minweight);
1356  allcurrminweight[0] = currminweight;
1357 
1358  /* fills first row of dynamic programming table with optimal values */
1359  for( d = currminweight; d < intcap; ++d )
1360  optvalues[d] = myprofits[0];
1361  /* fills dynamic programming table with optimal values */
1362  for( j = 1; j < nmyitems; ++j )
1363  {
1364  int intweight;
1365 
1366  /* compute important part of weight, which will be represented in the table */
1367  intweight = (int)(myweights[j] - minweight);
1368  assert(0 <= intweight && intweight < intcap);
1369 
1370  /* copy all nonzeros from row above */
1371  for( d = currminweight; d < intweight && d < intcap; ++d )
1372  optvalues[IDX(j,d)] = optvalues[IDX(j-1,d)];
1373 
1374  /* update corresponding row */
1375  for( d = intweight; d < intcap; ++d )
1376  {
1377  /* if index d is smaller the the current minweight then optvalues[IDX(j-1,d)] is not initialized, i.e. should
1378  * be 0
1379  */
1380  if( d < currminweight )
1381  {
1382  optvalues[IDX(j,d)] = myprofits[j];
1383  }
1384  else
1385  {
1386  SCIP_Real sumprofit;
1387 
1388  if( d - myweights[j] < currminweight )
1389  sumprofit = myprofits[j];
1390  else
1391  sumprofit = optvalues[IDX(j-1,(int)(d-myweights[j]))] + myprofits[j];
1392 
1393  optvalues[IDX(j,d)] = MAX(sumprofit, optvalues[IDX(j-1,d)]);
1394  }
1395  }
1396  /* update currminweight */
1397  if( intweight < currminweight )
1398  currminweight = intweight;
1399 
1400  allcurrminweight[j] = currminweight;
1401  }
1402 
1403  /* update optimal solution by following the table */
1404  if( solitems != NULL)
1405  {
1406  d = intcap - 1;
1407 
1408  SCIPdebugMessage("Fill the solution vector after solving exactly.\n");
1409 
1410  /* insert all items in (non-) solution vector */
1411  for( j = nmyitems - 1; j > 0; --j )
1412  {
1413  /* if we cannot find any item anymore which is in our solution stop, if the following condition holds this
1414  * means all remaining items does not fit anymore
1415  */
1416  if( d < allcurrminweight[j] )
1417  {
1418  /* we cannot have exceeded our capacity */
1419  assert((SCIP_Longint) d >= -minweight);
1420  break;
1421  }
1422  /* collect solution items, first condition means that no next item can fit anymore, but this does */
1423  if( d < allcurrminweight[j-1] || optvalues[IDX(j,d)] > optvalues[IDX(j-1,d)] )
1424  {
1425  solitems[*nsolitems] = myitems[j];
1426  ++(*nsolitems);
1427 
1428  /* check that we do not have an underflow */
1429  assert(myweights[j] <= (INT_MAX + (SCIP_Longint) d));
1430  d = (int)(d - myweights[j]);
1431  }
1432  /* collect non-solution items */
1433  else
1434  {
1435  nonsolitems[*nnonsolitems] = myitems[j];
1436  ++(*nnonsolitems);
1437  }
1438  }
1439 
1440  /* insert remaining items */
1441  if( d >= allcurrminweight[j] )
1442  {
1443  assert(j == 0);
1444  solitems[*nsolitems] = myitems[j];
1445  ++(*nsolitems);
1446  }
1447  else
1448  {
1449  assert(j >= 0);
1450  assert(d < allcurrminweight[j]);
1451 
1452  for( ; j >= 0; --j )
1453  {
1454  nonsolitems[*nnonsolitems] = myitems[j];
1455  ++(*nnonsolitems);
1456  }
1457  }
1458 
1459  assert(*nsolitems + *nnonsolitems == nitems);
1460  }
1461 
1462  /* update solution value */
1463  if( solval != NULL )
1464  *solval += optvalues[IDX(nmyitems-1,intcap-1)];
1465 
1466  SCIPfreeBufferArray(scip, &allcurrminweight);
1467 
1468  /* free all temporary memory */
1469  SCIPfreeBufferArray(scip, &tempsort);
1470  SCIPfreeMemoryArray(scip, &optvalues);
1471 
1472  TERMINATE:
1473  SCIPfreeBufferArray(scip, &myitems);
1474  SCIPfreeBufferArray(scip, &myprofits);
1475  SCIPfreeBufferArray(scip, &myweights);
1476 
1477  return SCIP_OKAY;
1478 }
1479 
1480 
1481 /** solves knapsack problem in maximization form approximately by solving the LP-relaxation of the problem using Dantzig's
1482  * method and rounding down the solution; if needed, one can provide arrays to store all selected items and all not
1483  * selected items
1484  */
1486  SCIP* scip, /**< SCIP data structure */
1487  int nitems, /**< number of available items */
1488  SCIP_Longint* weights, /**< item weights */
1489  SCIP_Real* profits, /**< item profits */
1490  SCIP_Longint capacity, /**< capacity of knapsack */
1491  int* items, /**< item numbers */
1492  int* solitems, /**< array to store items in solution, or NULL */
1493  int* nonsolitems, /**< array to store items not in solution, or NULL */
1494  int* nsolitems, /**< pointer to store number of items in solution, or NULL */
1495  int* nnonsolitems, /**< pointer to store number of items not in solution, or NULL */
1496  SCIP_Real* solval /**< pointer to store optimal solution value, or NULL */
1497  )
1498 {
1499  SCIP_Real* tempsort;
1500  SCIP_Longint solitemsweight;
1501  int j;
1502 
1503  assert(weights != NULL);
1504  assert(profits != NULL);
1505  assert(capacity >= 0);
1506  assert(items != NULL);
1507  assert(nitems >= 0);
1508 
1509  if( solitems != NULL )
1510  {
1511  *nsolitems = 0;
1512  *nnonsolitems = 0;
1513  }
1514  if( solval != NULL )
1515  *solval = 0.0;
1516 
1517  /* sort items (plus corresponding arrays weights and profits) such that
1518  * p_1/w_1 >= p_2/w_2 >= ... >= p_n/w_n
1519  */
1520  SCIP_CALL( SCIPallocBufferArray(scip, &tempsort, nitems) );
1521  for( j = nitems - 1; j >= 0; --j )
1522  {
1523  tempsort[j] = profits[j]/((SCIP_Real) weights[j]);
1524  }
1525  SCIPsortDownRealLongRealInt(tempsort, weights, profits, items, nitems);
1526 
1527  /* selects items as long as they fit into the knapsack */
1528  solitemsweight = 0;
1529  for( j = 0; j < nitems && solitemsweight + weights[j] <= capacity; j++ )
1530  {
1531  if( solitems != NULL )
1532  {
1533  solitems[*nsolitems] = items[j];
1534  (*nsolitems)++;
1535  }
1536  if( solval != NULL )
1537  (*solval) += profits[j];
1538  solitemsweight += weights[j];
1539  }
1540  for( ; j < nitems && solitems != NULL; j++ )
1541  {
1542  nonsolitems[*nnonsolitems] = items[j];
1543  (*nnonsolitems)++;
1544  }
1545 
1546  SCIPfreeBufferArray(scip, &tempsort);
1547 
1548  return SCIP_OKAY;
1549 }
1550 
1551 #ifndef NDEBUG
1552 /** returns, whether the the arrays transweights, transprofits and items are sorted such that
1553  * p_1 / w_1 >= p_2 / w_2 >= ... >= p_n / w_n and these arrays are not changed
1554  */
1555 static
1557  SCIP* scip, /**< SCIP data structure */
1558  int nitems, /**< number of available items */
1559  SCIP_Longint* transweights, /**< item weights */
1560  SCIP_Real* transprofits, /**< item profits */
1561  int* items, /**< item numbers */
1562  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
1563  SCIP_Real* solvals, /**< solution values of all problem variables */
1564  SCIP_Bool modtransused /**< TRUE for mod trans sepa prob was used to find cover */
1565  )
1566 {
1567  int j;
1568 
1569  assert(scip != NULL);
1570  assert(nitems >= 0);
1571  assert(weights != NULL);
1572  assert(solvals != NULL);
1573  assert(transweights != NULL);
1574  assert(transprofits != NULL);
1575 
1576  for( j = 1; j < nitems; j++ )
1577  {
1578  assert(SCIPisFeasGE(scip, transprofits[j-1]/transweights[j-1], transprofits[j]/transweights[j]));
1579  if( SCIPisFeasLT(scip, transprofits[j-1]/transweights[j-1], transprofits[j]/transweights[j]) )
1580  return FALSE;
1581  assert(weights[items[j]] == transweights[j]);
1582  if( weights[items[j]] != transweights[j] )
1583  return FALSE;
1584  if( modtransused )
1585  {
1586  assert(SCIPisFeasEQ(scip, (1.0 - solvals[items[j]]) * weights[items[j]], transprofits[j]));
1587  if( !SCIPisFeasEQ(scip, (1.0 - solvals[items[j]]) * weights[items[j]], transprofits[j]) )
1588  return FALSE;
1589  }
1590  else
1591  {
1592  assert(SCIPisFeasEQ(scip, (1.0 - solvals[items[j]]), transprofits[j]));
1593  if( !SCIPisFeasEQ(scip, (1.0 - solvals[items[j]]), transprofits[j]) )
1594  return FALSE;
1595 
1596  }
1597  }
1598  return TRUE;
1599 }
1600 #endif
1601 
1602 #ifdef SCIP_DEBUG
1603 /** prints all nontrivial GUB constraints and their LP solution values */
1604 static
1605 void GUBsetPrint(
1606  SCIP* scip, /**< SCIP data structure */
1607  SCIP_GUBSET* gubset, /**< GUB set data structure */
1608  SCIP_VAR** vars, /**< variables in knapsack constraint */
1609  SCIP_Real* solvals /**< solution values of variables in knapsack constraint; or NULL */
1610  )
1611 {
1612  int nnontrivialgubconss;
1613  int c;
1614 
1615  nnontrivialgubconss = 0;
1616 
1617  SCIPdebugMessage(" Nontrivial GUBs of current GUB set:\n");
1618 
1619  /* print out all nontrivial GUB constraints, i.e., with more than one variable */
1620  for( c = 0; c < gubset->ngubconss; c++ )
1621  {
1622  SCIP_Real gubsolval;
1623 
1624  assert(gubset->gubconss[c]->ngubvars >= 0);
1625 
1626  /* nontrivial GUB */
1627  if( gubset->gubconss[c]->ngubvars > 1 )
1628  {
1629  int v;
1630 
1631  gubsolval = 0.0;
1632  SCIPdebugMessage(" GUB<%d>:\n", c);
1633 
1634  /* print GUB var */
1635  for( v = 0; v < gubset->gubconss[c]->ngubvars; v++ )
1636  {
1637  int currentvar;
1638 
1639  currentvar = gubset->gubconss[c]->gubvars[v];
1640  if( solvals != NULL )
1641  {
1642  gubsolval += solvals[currentvar];
1643  SCIPdebugMessage(" +<%s>(%4.2f)\n", SCIPvarGetName(vars[currentvar]), solvals[currentvar]);
1644  }
1645  else
1646  {
1647  SCIPdebugMessage(" +<%s>\n", SCIPvarGetName(vars[currentvar]));
1648  }
1649  }
1650 
1651  /* check whether LP solution satisfies the GUB constraint */
1652  if( solvals != NULL )
1653  {
1654  SCIPdebugMessage(" =%4.2f <= 1 %s\n", gubsolval,
1655  SCIPisFeasGT(scip, gubsolval, 1.0) ? "--> violated" : "");
1656  }
1657  else
1658  {
1659  SCIPdebugMessage(" <= 1 %s\n", SCIPisFeasGT(scip, gubsolval, 1.0) ? "--> violated" : "");
1660  }
1661  nnontrivialgubconss++;
1662  }
1663  }
1664 
1665  SCIPdebugMessage(" --> %d/%d nontrivial GUBs\n", nnontrivialgubconss, gubset->ngubconss);
1666 }
1667 #endif
1668 
1669 /** creates an empty GUB constraint */
1670 static
1672  SCIP* scip, /**< SCIP data structure */
1673  SCIP_GUBCONS** gubcons /**< pointer to store GUB constraint data */
1674  )
1675 {
1676  assert(scip != NULL);
1677  assert(gubcons != NULL);
1678 
1679  /* allocate memory for GUB constraint data structures */
1680  SCIP_CALL( SCIPallocMemory(scip, gubcons) );
1681  (*gubcons)->gubvarssize = GUBCONSGROWVALUE;
1682  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubcons)->gubvars, (*gubcons)->gubvarssize) );
1683  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubcons)->gubvarsstatus, (*gubcons)->gubvarssize) );
1684 
1685  (*gubcons)->ngubvars = 0;
1686 
1687  return SCIP_OKAY;
1688 }
1689 
1690 /** frees GUB constraint */
1691 static
1693  SCIP* scip, /**< SCIP data structure */
1694  SCIP_GUBCONS** gubcons /**< pointer to GUB constraint data structure */
1695  )
1696 {
1697  assert(scip != NULL);
1698  assert(gubcons != NULL);
1699  assert((*gubcons)->gubvars != NULL);
1700  assert((*gubcons)->gubvarsstatus != NULL);
1701 
1702  /* free allocated memory */
1703  SCIPfreeBufferArray(scip, &(*gubcons)->gubvarsstatus);
1704  SCIPfreeBufferArray(scip, &(*gubcons)->gubvars);
1705  SCIPfreeMemory(scip, gubcons);
1706 
1707  return SCIP_OKAY;
1708 }
1709 
1710 /** adds variable to given GUB constraint */
1711 static
1713  SCIP* scip, /**< SCIP data structure */
1714  SCIP_GUBCONS* gubcons, /**< GUB constraint data */
1715  int var /**< index of given variable in knapsack constraint */
1716  )
1717 {
1718  assert(scip != NULL);
1719  assert(gubcons != NULL);
1720  assert(gubcons->ngubvars >= 0 && gubcons->ngubvars < gubcons->gubvarssize);
1721  assert(gubcons->gubvars != NULL);
1722  assert(gubcons->gubvarsstatus != NULL);
1723  assert(var >= 0);
1724 
1725  /* add variable to GUB constraint */
1726  gubcons->gubvars[gubcons->ngubvars] = var;
1727  gubcons->gubvarsstatus[gubcons->ngubvars] = GUBVARSTATUS_UNINITIAL;
1728  gubcons->ngubvars++;
1729 
1730  /* increase space allocated to GUB constraint if the number of variables reaches the size */
1731  if( gubcons->ngubvars == gubcons->gubvarssize )
1732  {
1733  int newlen;
1734 
1735  newlen = gubcons->gubvarssize + GUBCONSGROWVALUE;
1736  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvars, newlen) );
1737  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvarsstatus, newlen) );
1738 
1739  gubcons->gubvarssize = newlen;
1740  }
1741 
1742  return SCIP_OKAY;
1743 }
1744 
1745 /** deletes variable from its current GUB constraint */
1746 static
1748  SCIP* scip, /**< SCIP data structure */
1749  SCIP_GUBCONS* gubcons, /**< GUB constraint data */
1750  int var, /**< index of given variable in knapsack constraint */
1751  int gubvarsidx /**< index of the variable in its current GUB constraint */
1752  )
1753 {
1754  assert(scip != NULL);
1755  assert(gubcons != NULL);
1756  assert(var >= 0);
1757  assert(gubvarsidx >= 0 && gubvarsidx < gubcons->ngubvars);
1758  assert(gubcons->ngubvars >= gubvarsidx+1);
1759  assert(gubcons->gubvars[gubvarsidx] == var);
1760 
1761  /* delete variable from GUB by swapping it replacing in by the last variable in the GUB constraint */
1762  gubcons->gubvars[gubvarsidx] = gubcons->gubvars[gubcons->ngubvars-1];
1763  gubcons->gubvarsstatus[gubvarsidx] = gubcons->gubvarsstatus[gubcons->ngubvars-1];
1764  gubcons->ngubvars--;
1765 
1766  /* decrease space allocated for the GUB constraint, if the last GUBCONSGROWVALUE+1 array entries are now empty */
1767  if( gubcons->ngubvars < gubcons->gubvarssize - GUBCONSGROWVALUE && gubcons->ngubvars > 0 )
1768  {
1769  int newlen;
1770 
1771  newlen = gubcons->gubvarssize - GUBCONSGROWVALUE;
1772 
1773  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvars, newlen) );
1774  SCIP_CALL( SCIPreallocBufferArray(scip, &gubcons->gubvarsstatus, newlen) );
1775 
1776  gubcons->gubvarssize = newlen;
1777  }
1778 
1779  return SCIP_OKAY;
1780 }
1781 
1782 /** moves variable from current GUB constraint to a different existing (nonempty) GUB constraint */
1783 static
1785  SCIP* scip, /**< SCIP data structure */
1786  SCIP_GUBSET* gubset, /**< GUB set data structure */
1787  SCIP_VAR** vars, /**< variables in knapsack constraint */
1788  int var, /**< index of given variable in knapsack constraint */
1789  int oldgubcons, /**< index of old GUB constraint of given variable */
1790  int newgubcons /**< index of new GUB constraint of given variable */
1791  )
1792 {
1793  int oldgubvaridx;
1794  int replacevar;
1795  int j;
1796 
1797  assert(scip != NULL);
1798  assert(gubset != NULL);
1799  assert(var >= 0);
1800  assert(oldgubcons >= 0 && oldgubcons < gubset->ngubconss);
1801  assert(newgubcons >= 0 && newgubcons < gubset->ngubconss);
1802  assert(oldgubcons != newgubcons);
1803  assert(gubset->gubconssidx[var] == oldgubcons);
1804  assert(gubset->gubconss[oldgubcons]->ngubvars > 0);
1805  assert(gubset->gubconss[newgubcons]->ngubvars >= 0);
1806 
1807  SCIPdebugMessage(" moving variable<%s> from GUB<%d> to GUB<%d>\n", SCIPvarGetName(vars[var]), oldgubcons, newgubcons);
1808 
1809  oldgubvaridx = gubset->gubvarsidx[var];
1810 
1811  /* delete variable from old GUB constraint by replacing it by the last variable of the GUB constraint */
1812  SCIP_CALL( GUBconsDelVar(scip, gubset->gubconss[oldgubcons], var, oldgubvaridx) );
1813 
1814  /* in GUB set, update stored index of variable in old GUB constraint for the variable used for replacement;
1815  * replacement variable is given by old position of the deleted variable
1816  */
1817  replacevar = gubset->gubconss[oldgubcons]->gubvars[oldgubvaridx];
1818  assert(gubset->gubvarsidx[replacevar] == gubset->gubconss[oldgubcons]->ngubvars);
1819  gubset->gubvarsidx[replacevar] = oldgubvaridx;
1820 
1821  /* add variable to the end of new GUB constraint */
1822  SCIP_CALL( GUBconsAddVar(scip, gubset->gubconss[newgubcons], var) );
1823  assert(gubset->gubconss[newgubcons]->gubvars[gubset->gubconss[newgubcons]->ngubvars-1] == var);
1824 
1825  /* in GUB set, update stored index of GUB of moved variable and stored index of variable in this GUB constraint */
1826  gubset->gubconssidx[var] = newgubcons;
1827  gubset->gubvarsidx[var] = gubset->gubconss[newgubcons]->ngubvars-1;
1828 
1829  /* delete old GUB constraint if it became empty */
1830  if( gubset->gubconss[oldgubcons]->ngubvars == 0 )
1831  {
1832  SCIPdebugMessage("deleting empty GUB cons<%d> from current GUB set\n", oldgubcons);
1833 #ifdef SCIP_DEBUG
1834  GUBsetPrint(scip, gubset, vars, NULL);
1835 #endif
1836 
1837  /* free old GUB constraint */
1838  SCIP_CALL( GUBconsFree(scip, &gubset->gubconss[oldgubcons]) );
1839 
1840  /* if empty GUB was not the last one in GUB set data structure, replace it by last GUB constraint */
1841  if( oldgubcons != gubset->ngubconss-1 )
1842  {
1843  gubset->gubconss[oldgubcons] = gubset->gubconss[gubset->ngubconss-1];
1844  gubset->gubconsstatus[oldgubcons] = gubset->gubconsstatus[gubset->ngubconss-1];
1845 
1846  /* in GUB set, update stored index of GUB constraint for all variable of the GUB constraint used for replacement;
1847  * replacement GUB is given by old position of the deleted GUB
1848  */
1849  for( j = 0; j < gubset->gubconss[oldgubcons]->ngubvars; j++ )
1850  {
1851  assert(gubset->gubconssidx[gubset->gubconss[oldgubcons]->gubvars[j]] == gubset->ngubconss-1);
1852  gubset->gubconssidx[gubset->gubconss[oldgubcons]->gubvars[j]] = oldgubcons;
1853  }
1854  }
1855 
1856  /* update number of GUB constraints */
1857  gubset->ngubconss--;
1858 
1859  /* variable should be at given new position, unless new GUB constraint replaced empty old GUB constraint
1860  * (because it was at the end of the GUB constraint array)
1861  */
1862  assert(gubset->gubconssidx[var] == newgubcons
1863  || (newgubcons == gubset->ngubconss && gubset->gubconssidx[var] == oldgubcons));
1864  }
1865 #ifndef NDEBUG
1866  else
1867  assert(gubset->gubconssidx[var] == newgubcons);
1868 #endif
1869 
1870  return SCIP_OKAY;
1871 }
1872 
1873 /** swaps two variables in the same GUB constraint */
1874 static
1876  SCIP* scip, /**< SCIP data structure */
1877  SCIP_GUBSET* gubset, /**< GUB set data structure */
1878  int var1, /**< first variable to be swapped */
1879  int var2 /**< second variable to be swapped */
1880  )
1881 {
1882  int gubcons;
1883  int var1idx;
1884  GUBVARSTATUS var1status;
1885  int var2idx;
1886  GUBVARSTATUS var2status;
1887 
1888  assert(scip != NULL);
1889  assert(gubset != NULL);
1890 
1891  gubcons = gubset->gubconssidx[var1];
1892  assert(gubcons == gubset->gubconssidx[var2]);
1893 
1894  /* nothing to be done if both variables are the same */
1895  if( var1 == var2 )
1896  return;
1897 
1898  /* swap index and status of variables in GUB constraint */
1899  var1idx = gubset->gubvarsidx[var1];
1900  var1status = gubset->gubconss[gubcons]->gubvarsstatus[var1idx];
1901  var2idx = gubset->gubvarsidx[var2];
1902  var2status = gubset->gubconss[gubcons]->gubvarsstatus[var2idx];
1903 
1904  gubset->gubvarsidx[var1] = var2idx;
1905  gubset->gubconss[gubcons]->gubvars[var1idx] = var2;
1906  gubset->gubconss[gubcons]->gubvarsstatus[var1idx] = var2status;
1907 
1908  gubset->gubvarsidx[var2] = var1idx;
1909  gubset->gubconss[gubcons]->gubvars[var2idx] = var1;
1910  gubset->gubconss[gubcons]->gubvarsstatus[var2idx] = var1status;
1911 }
1912 
1913 /** initializes partition of knapsack variables into nonoverlapping trivial GUB constraints (GUB with one variable) */
1914 static
1916  SCIP* scip, /**< SCIP data structure */
1917  SCIP_GUBSET** gubset, /**< pointer to store GUB set data structure */
1918  int nvars, /**< number of variables in the knapsack constraint */
1919  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
1920  SCIP_Longint capacity /**< capacity of knapsack */
1921  )
1922 {
1923  int i;
1924 
1925  assert(scip != NULL);
1926  assert(gubset != NULL);
1927  assert(nvars > 0);
1928  assert(weights != NULL);
1929  assert(capacity >= 0);
1930 
1931  /* allocate memory for GUB set data structures */
1932  SCIP_CALL( SCIPallocMemory(scip, gubset) );
1933  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubconss, nvars) );
1934  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubconsstatus, nvars) );
1935  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubconssidx, nvars) );
1936  SCIP_CALL( SCIPallocBufferArray(scip, &(*gubset)->gubvarsidx, nvars) );
1937  (*gubset)->ngubconss = nvars;
1938  (*gubset)->nvars = nvars;
1939 
1940  /* initialize the set of GUB constraints */
1941  for( i = 0; i < nvars; i++ )
1942  {
1943  /* assign each variable to a new (trivial) GUB constraint */
1944  SCIP_CALL( GUBconsCreate(scip, &(*gubset)->gubconss[i]) );
1945  SCIP_CALL( GUBconsAddVar(scip, (*gubset)->gubconss[i], i) );
1946 
1947  /* set status of GUB constraint to initial */
1948  (*gubset)->gubconsstatus[i] = GUBCONSSTATUS_UNINITIAL;
1949 
1950  (*gubset)->gubconssidx[i] = i;
1951  (*gubset)->gubvarsidx[i] = 0;
1952  assert((*gubset)->gubconss[i]->ngubvars == 1);
1953 
1954  /* already updated status of variable in GUB constraint if it exceeds the capacity of the knapsack */
1955  if( weights[i] > capacity )
1956  (*gubset)->gubconss[(*gubset)->gubconssidx[i]]->gubvarsstatus[(*gubset)->gubvarsidx[i]] = GUBVARSTATUS_CAPACITYEXCEEDED;
1957 
1958  }
1959 
1960  return SCIP_OKAY;
1961 }
1962 
1963 /** frees GUB set data structure */
1964 static
1966  SCIP* scip, /**< SCIP data structure */
1967  SCIP_GUBSET** gubset /**< pointer to GUB set data structure */
1968  )
1969 {
1970  int i;
1971 
1972  assert(scip != NULL);
1973  assert(gubset != NULL);
1974  assert((*gubset)->gubconss != NULL);
1975  assert((*gubset)->gubconsstatus != NULL);
1976  assert((*gubset)->gubconssidx != NULL);
1977  assert((*gubset)->gubvarsidx != NULL);
1978 
1979  /* free all GUB constraints */
1980  for( i = (*gubset)->ngubconss-1; i >= 0; --i )
1981  {
1982  assert((*gubset)->gubconss[i] != NULL);
1983  SCIP_CALL( GUBconsFree(scip, &(*gubset)->gubconss[i]) );
1984  }
1985 
1986  /* free allocated memory */
1987  SCIPfreeBufferArray( scip, &(*gubset)->gubvarsidx );
1988  SCIPfreeBufferArray( scip, &(*gubset)->gubconssidx );
1989  SCIPfreeBufferArray( scip, &(*gubset)->gubconsstatus );
1990  SCIPfreeBufferArray( scip, &(*gubset)->gubconss );
1991  SCIPfreeMemory(scip, gubset);
1992 
1993  return SCIP_OKAY;
1994 }
1995 
1996 #ifndef NDEBUG
1997 /** checks whether GUB set data structure is consistent */
1998 static
2000  SCIP* scip, /**< SCIP data structure */
2001  SCIP_GUBSET* gubset, /**< GUB set data structure */
2002  SCIP_VAR** vars /**< variables in the knapsack constraint */
2003  )
2004 {
2005  int i;
2006  int gubconsidx;
2007  int gubvaridx;
2008  SCIP_VAR* var1;
2009  SCIP_VAR* var2;
2010  SCIP_Bool var1negated;
2011  SCIP_Bool var2negated;
2012 
2013  assert(scip != NULL);
2014  assert(gubset != NULL);
2015 
2016  SCIPdebugMessage(" GUB set consistency check:\n");
2017 
2018  /* checks for all knapsack vars consistency of stored index of associated gubcons and corresponding index in gubvars */
2019  for( i = 0; i < gubset->nvars; i++ )
2020  {
2021  gubconsidx = gubset->gubconssidx[i];
2022  gubvaridx = gubset->gubvarsidx[i];
2023 
2024  if( gubset->gubconss[gubconsidx]->gubvars[gubvaridx] != i )
2025  {
2026  SCIPdebugMessage(" var<%d> should be in GUB<%d> at position<%d>, but stored is var<%d> instead\n", i,
2027  gubconsidx, gubvaridx, gubset->gubconss[gubconsidx]->gubvars[gubvaridx] );
2028  }
2029  assert(gubset->gubconss[gubconsidx]->gubvars[gubvaridx] == i);
2030  }
2031 
2032  /* checks for each GUB whether all pairs of its variables have a common clique */
2033  for( i = 0; i < gubset->ngubconss; i++ )
2034  {
2035  int j;
2036 
2037  for( j = 0; j < gubset->gubconss[i]->ngubvars; j++ )
2038  {
2039  int k;
2040 
2041  /* get corresponding active problem variable */
2042  var1 = vars[gubset->gubconss[i]->gubvars[j]];
2043  var1negated = FALSE;
2044  SCIP_CALL( SCIPvarGetProbvarBinary(&var1, &var1negated) );
2045 
2046  for( k = j+1; k < gubset->gubconss[i]->ngubvars; k++ )
2047  {
2048  /* get corresponding active problem variable */
2049  var2 = vars[gubset->gubconss[i]->gubvars[k]];
2050  var2negated = FALSE;
2051  SCIP_CALL( SCIPvarGetProbvarBinary(&var2, &var2negated) );
2052 
2053  if( !SCIPvarsHaveCommonClique(var1, !var1negated, var2, !var2negated, TRUE) )
2054  {
2055  SCIPdebugMessage(" GUB<%d>: var<%d,%s> and var<%d,%s> do not share a clique\n", i, j,
2056  SCIPvarGetName(vars[gubset->gubconss[i]->gubvars[j]]), k,
2057  SCIPvarGetName(vars[gubset->gubconss[i]->gubvars[k]]));
2058  SCIPdebugMessage(" GUB<%d>: var<%d,%s> and var<%d,%s> do not share a clique\n", i, j,
2059  SCIPvarGetName(var1), k,
2060  SCIPvarGetName(var2));
2061  }
2062 
2063  /* @todo: in case we used also negated cliques for the GUB partition, this assert has to be changed */
2064  assert(SCIPvarsHaveCommonClique(var1, !var1negated, var2, !var2negated, TRUE));
2065  }
2066  }
2067  }
2068  SCIPdebugMessage(" --> successful\n");
2069 
2070  return SCIP_OKAY;
2071 }
2072 #endif
2073 
2074 /** calculates a partition of the given set of binary variables into cliques;
2075  * afterwards the output array contains one value for each variable, such that two variables got the same value iff they
2076  * were assigned to the same clique;
2077  * the first variable is always assigned to clique 0, and a variable can only be assigned to clique i if at least one of
2078  * the preceding variables was assigned to clique i-1;
2079  * note: in contrast to SCIPcalcCliquePartition(), variables with LP value 1 are put into trivial cliques (with one
2080  * variable) and for the remaining variables, a partition with a small number of cliques is constructed
2081  */
2082 #define MAXNCLIQUEVARSCOMP 1000000
2083 static
2085  SCIP*const scip, /**< SCIP data structure */
2086  SCIP_VAR**const vars, /**< binary variables in the clique from which at most one can be set to 1 */
2087  int const nvars, /**< number of variables in the clique */
2088  int*const cliquepartition, /**< array of length nvars to store the clique partition */
2089  int*const ncliques, /**< pointer to store number of cliques actually contained in the partition */
2090  SCIP_Real* solvals /**< solution values of all given binary variables */
2091  )
2092 {
2093  SCIP_VAR** tmpvars;
2094  SCIP_VAR** cliquevars;
2095  SCIP_Bool* cliquevalues;
2096  SCIP_Bool* tmpvalues;
2097  int* varseq;
2098  int* sortkeys;
2099  int ncliquevars;
2100  int maxncliquevarscomp;
2101  int nignorevars;
2102  int nvarsused;
2103  int i;
2104 
2105  assert(scip != NULL);
2106  assert(nvars == 0 || vars != NULL);
2107  assert(nvars == 0 || cliquepartition != NULL);
2108  assert(ncliques != NULL);
2109 
2110  if( nvars == 0 )
2111  {
2112  *ncliques = 0;
2113  return SCIP_OKAY;
2114  }
2115 
2116  /* allocate temporary memory for storing the variables of the current clique */
2117  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevars, nvars) );
2118  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevalues, nvars) );
2119  SCIP_CALL( SCIPallocBufferArray(scip, &tmpvalues, nvars) );
2120  SCIP_CALL( SCIPduplicateBufferArray(scip, &tmpvars, vars, nvars) );
2121  SCIP_CALL( SCIPallocBufferArray(scip, &varseq, nvars) );
2122  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, nvars) );
2123  ncliquevars = 0;
2124 
2125  /* initialize the cliquepartition array with -1 */
2126  /* initialize the tmpvalues array */
2127  for( i = nvars - 1; i >= 0; --i )
2128  {
2129  tmpvalues[i] = TRUE;
2130  cliquepartition[i] = -1;
2131  }
2132 
2133  /* get corresponding active problem variables */
2134  SCIP_CALL( SCIPvarsGetProbvarBinary(&tmpvars, &tmpvalues, nvars) );
2135 
2136  /* ignore variables with LP value 1 (will be assigned to trivial GUBs at the end) and sort remaining variables
2137  * by nondecreasing number of cliques the variables are in
2138  */
2139  nignorevars = 0;
2140  nvarsused = 0;
2141  for( i = 0; i < nvars; i++ )
2142  {
2143  if( SCIPisFeasEQ(scip, solvals[i], 1.0) )
2144  {
2145  /* variables with LP value 1 are put to the end of varseq array and will not be sorted */
2146  varseq[nvars-1-nignorevars] = i;
2147  nignorevars++;
2148  }
2149  else
2150  {
2151  /* remaining variables are put to the front of varseq array and will be sorted by their number of cliques */
2152  varseq[nvarsused] = i;
2153  sortkeys[nvarsused] = SCIPvarGetNCliques(tmpvars[i], tmpvalues[i]);
2154  nvarsused++;
2155  }
2156  }
2157  assert(nvarsused + nignorevars == nvars);
2158 
2159  /* sort variables with LP value less than 1 by nondecreasing order of the number of cliques they are in */
2160  SCIPsortIntInt(sortkeys, varseq, nvarsused);
2161 
2162  maxncliquevarscomp = MIN(nvars*nvars, MAXNCLIQUEVARSCOMP);
2163 
2164  /* calculate the clique partition */
2165  *ncliques = 0;
2166  for( i = 0; i < nvars; ++i )
2167  {
2168  if( cliquepartition[varseq[i]] == -1 )
2169  {
2170  int j;
2171 
2172  /* variable starts a new clique */
2173  cliquepartition[varseq[i]] = *ncliques;
2174  cliquevars[0] = tmpvars[varseq[i]];
2175  cliquevalues[0] = tmpvalues[varseq[i]];
2176  ncliquevars = 1;
2177 
2178  /* if variable is not active (multi-aggregated or fixed), it cannot be in any clique and
2179  * if the variable has LP value 1 we do not want it to be in nontrivial cliques
2180  */
2181  if( SCIPvarIsActive(tmpvars[varseq[i]]) && i < nvarsused )
2182  {
2183  /* greedily fill up the clique */
2184  for( j = i + 1; j < nvarsused; ++j )
2185  {
2186  /* if variable is not active (multi-aggregated or fixed), it cannot be in any clique */
2187  if( cliquepartition[varseq[j]] == -1 && SCIPvarIsActive(tmpvars[varseq[j]]) )
2188  {
2189  int k;
2190 
2191  /* check if every variable in the actual clique is in clique with the new variable */
2192  for( k = ncliquevars - 1; k >= 0; --k )
2193  {
2194  if( !SCIPvarsHaveCommonClique(tmpvars[varseq[j]], tmpvalues[varseq[j]], cliquevars[k],
2195  cliquevalues[k], TRUE) )
2196  break;
2197  }
2198 
2199  if( k == -1 )
2200  {
2201  /* put the variable into the same clique */
2202  cliquepartition[varseq[j]] = cliquepartition[varseq[i]];
2203  cliquevars[ncliquevars] = tmpvars[varseq[j]];
2204  cliquevalues[ncliquevars] = tmpvalues[varseq[j]];
2205  ++ncliquevars;
2206  }
2207  }
2208  }
2209  }
2210 
2211  /* this clique is finished */
2212  ++(*ncliques);
2213  }
2214  assert(cliquepartition[varseq[i]] >= 0 && cliquepartition[varseq[i]] < i + 1);
2215 
2216  /* break if we reached the maximal number of comparisons */
2217  if( i * nvars > maxncliquevarscomp )
2218  break;
2219  }
2220  /* if we had to much variables fill up the cliquepartition and put each variable in a separate clique */
2221  for( ; i < nvars; ++i )
2222  {
2223  if( cliquepartition[varseq[i]] == -1 )
2224  {
2225  cliquepartition[varseq[i]] = *ncliques;
2226  ++(*ncliques);
2227  }
2228  }
2229 
2230  /* free temporary memory */
2231  SCIPfreeBufferArray(scip, &sortkeys);
2232  SCIPfreeBufferArray(scip, &varseq);
2233  SCIPfreeBufferArray(scip, &tmpvars);
2234  SCIPfreeBufferArray(scip, &tmpvalues);
2235  SCIPfreeBufferArray(scip, &cliquevalues);
2236  SCIPfreeBufferArray(scip, &cliquevars);
2237 
2238  return SCIP_OKAY;
2239 }
2240 
2241 /** constructs sophisticated partion of knapsack variables into nonoverlapping GUBs; current partion uses trivial GUBs */
2242 static
2244  SCIP* scip, /**< SCIP data structure */
2245  SCIP_GUBSET* gubset, /**< GUB set data structure */
2246  SCIP_VAR** vars, /**< variables in the knapsack constraint */
2247  SCIP_Real* solvals /**< solution values of all knapsack variables */
2248  )
2249 {
2250  int* cliquepartition;
2251  int* gubfirstvar;
2252  int ncliques;
2253  int currentgubconsidx;
2254  int newgubconsidx;
2255  int cliqueidx;
2256  int nvars;
2257  int i;
2258 
2259  assert(scip != NULL);
2260  assert(gubset != NULL);
2261  assert(vars != NULL);
2262 
2263  nvars = gubset->nvars;
2264  assert(nvars >= 0);
2265 
2266  /* allocate temporary memory for clique partition */
2267  SCIP_CALL( SCIPallocBufferArray(scip, &cliquepartition, nvars) );
2268 
2269  /* compute sophisticated clique partition */
2270  SCIP_CALL( GUBsetCalcCliquePartition(scip, vars, nvars, cliquepartition, &ncliques, solvals) );
2271 
2272  /* allocate temporary memory for GUB set data structure */
2273  SCIP_CALL( SCIPallocBufferArray(scip, &gubfirstvar, ncliques) );
2274 
2275  /* translate GUB partition into GUB set data structure */
2276  for( i = 0; i < ncliques; i++ )
2277  {
2278  /* initialize first variable for every GUB */
2279  gubfirstvar[i] = -1;
2280  }
2281  /* move every knapsack variable into GUB defined by clique partition */
2282  for( i = 0; i < nvars; i++ )
2283  {
2284  assert(cliquepartition[i] >= 0);
2285 
2286  cliqueidx = cliquepartition[i];
2287  currentgubconsidx = gubset->gubconssidx[i];
2288  assert(gubset->gubconss[currentgubconsidx]->ngubvars == 1 );
2289 
2290  /* variable is first element in GUB constraint defined by clique partition */
2291  if( gubfirstvar[cliqueidx] == -1 )
2292  {
2293  /* corresponding GUB constraint in GUB set data structure was already constructed (as initial trivial GUB);
2294  * note: no assert for gubconssidx, because it can changed due to deleting emtpy GUBs in GUBsetMoveVar()
2295  */
2296  assert(gubset->gubvarsidx[i] == 0);
2297  assert(gubset->gubconss[gubset->gubconssidx[i]]->gubvars[gubset->gubvarsidx[i]] == i);
2298 
2299  /* remember the first variable found for the current GUB */
2300  gubfirstvar[cliqueidx] = i;
2301  }
2302  /* variable is additional element of GUB constraint defined by clique partition */
2303  else
2304  {
2305  assert(gubfirstvar[cliqueidx] >= 0 && gubfirstvar[cliqueidx] < i);
2306 
2307  /* move variable to GUB constraint defined by clique partition; index of this GUB constraint is given by the
2308  * first variable of this GUB constraint
2309  */
2310  newgubconsidx = gubset->gubconssidx[gubfirstvar[cliqueidx]];
2311  assert(newgubconsidx != currentgubconsidx); /* because initially every variable is in a different GUB */
2312  SCIP_CALL( GUBsetMoveVar(scip, gubset, vars, i, currentgubconsidx, newgubconsidx) );
2313 
2314  assert(gubset->gubconss[gubset->gubconssidx[i]]->gubvars[gubset->gubvarsidx[i]] == i);
2315  }
2316  }
2317 
2318 #ifdef SCIP_DEBUG
2319  /* prints GUB set data structure */
2320  GUBsetPrint(scip, gubset, vars, solvals);
2321 #endif
2322 
2323 #ifndef NDEBUG
2324  /* checks consistency of GUB set data structure */
2325  SCIP_CALL( GUBsetCheck(scip, gubset, vars) );
2326 #endif
2327 
2328  /* free temporary memory */
2329  SCIPfreeBufferArray(scip, &gubfirstvar);
2330  SCIPfreeBufferArray(scip, &cliquepartition);
2331 
2332  return SCIP_OKAY;
2333 }
2334 
2335 /** 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$
2336  * 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
2337  * \f$j \in N \setminus C\f$, if \f$j \in N_0 = \{j \in N : x^*_j = 0\}\f$, if one exists.
2338  */
2339 static
2341  SCIP* scip, /**< SCIP data structure */
2342  SCIP_VAR** vars, /**< variables in knapsack constraint */
2343  int nvars, /**< number of variables in knapsack constraint */
2344  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2345  SCIP_Longint capacity, /**< capacity of knapsack */
2346  SCIP_Real* solvals, /**< solution values of all problem variables */
2347  int* covervars, /**< pointer to store cover variables */
2348  int* noncovervars, /**< pointer to store noncover variables */
2349  int* ncovervars, /**< pointer to store number of cover variables */
2350  int* nnoncovervars, /**< pointer to store number of noncover variables */
2351  SCIP_Longint* coverweight, /**< pointer to store weight of cover */
2352  SCIP_Bool* found, /**< pointer to store whether a cover was found */
2353  SCIP_Bool modtransused, /**< should modified transformed separation problem be used to find cover */
2354  int* ntightened, /**< pointer to store number of variables with tightened upper bound */
2355  SCIP_Bool* fractional /**< pointer to store whether the LP sol for knapsack vars is fractional */
2356  )
2357 {
2358  SCIP_Longint* transweights;
2359  SCIP_Real* transprofits;
2360  SCIP_Longint transcapacity;
2361  SCIP_Longint fixedonesweight;
2362  SCIP_Longint itemsweight;
2363  SCIP_Bool infeasible;
2364  int* fixedones;
2365  int* fixedzeros;
2366  int* items;
2367  int nfixedones;
2368  int nfixedzeros;
2369  int nitems;
2370  int j;
2371 
2372  assert(scip != NULL);
2373  assert(vars != NULL);
2374  assert(nvars > 0);
2375  assert(weights != NULL);
2376  assert(capacity >= 0);
2377  assert(solvals != NULL);
2378  assert(covervars != NULL);
2379  assert(noncovervars != NULL);
2380  assert(ncovervars != NULL);
2381  assert(nnoncovervars != NULL);
2382  assert(coverweight != NULL);
2383  assert(found != NULL);
2384  assert(ntightened != NULL);
2385  assert(fractional != NULL);
2386 
2387  SCIPdebugMessage(" get cover for knapsack constraint\n");
2388 
2389  /* allocates temporary memory */
2390  SCIP_CALL( SCIPallocBufferArray(scip, &transweights, nvars) );
2391  SCIP_CALL( SCIPallocBufferArray(scip, &transprofits, nvars) );
2392  SCIP_CALL( SCIPallocBufferArray(scip, &fixedones, nvars) );
2393  SCIP_CALL( SCIPallocBufferArray(scip, &fixedzeros, nvars) );
2394  SCIP_CALL( SCIPallocBufferArray(scip, &items, nvars) );
2395 
2396  *found = FALSE;
2397  *ncovervars = 0;
2398  *nnoncovervars = 0;
2399  *coverweight = 0;
2400  *fractional = TRUE;
2401 
2402  /* gets the following sets
2403  * N_1 = {j in N : x*_j = 1} (fixedones),
2404  * N_0 = {j in N : x*_j = 0} (fixedzeros) and
2405  * N\(N_0 & N_1) (items),
2406  * where x*_j is the solution value of variable x_j
2407  */
2408  nfixedones = 0;
2409  nfixedzeros = 0;
2410  nitems = 0;
2411  fixedonesweight = 0;
2412  itemsweight = 0;
2413  *ntightened = 0;
2414  for( j = 0; j < nvars; j++ )
2415  {
2416  assert(SCIPvarIsBinary(vars[j]));
2417 
2418  /* tightens upper bound of x_j if weight of x_j is greater than capacity of knapsack */
2419  if( weights[j] > capacity )
2420  {
2421  SCIP_CALL( SCIPtightenVarUb(scip, vars[j], 0.0, FALSE, &infeasible, NULL) );
2422  assert(!infeasible);
2423  (*ntightened)++;
2424  continue;
2425  }
2426 
2427  /* variable x_j has solution value one */
2428  if( SCIPisFeasEQ(scip, solvals[j], 1.0) )
2429  {
2430  fixedones[nfixedones] = j;
2431  nfixedones++;
2432  fixedonesweight += weights[j];
2433  }
2434  /* variable x_j has solution value zero */
2435  else if( SCIPisFeasEQ(scip, solvals[j], 0.0) )
2436  {
2437  fixedzeros[nfixedzeros] = j;
2438  nfixedzeros++;
2439  }
2440  /* variable x_j has fractional solution value */
2441  else
2442  {
2443  assert( SCIPisFeasGT(scip, solvals[j], 0.0) && SCIPisFeasLT(scip, solvals[j], 1.0) );
2444  items[nitems] = j;
2445  nitems++;
2446  itemsweight += weights[j];
2447  }
2448  }
2449  assert(nfixedones + nfixedzeros + nitems == nvars - (*ntightened));
2450 
2451  /* sets whether the LP solution x* for the knapsack variables is fractional; if it is not fractional we stop
2452  * the separation routine
2453  */
2454  assert(nitems >= 0);
2455  if( nitems == 0 )
2456  {
2457  *fractional = FALSE;
2458  goto TERMINATE;
2459  }
2460  assert(*fractional);
2461 
2462  /* transforms the traditional separation problem (under consideration of the following fixing:
2463  * z_j = 1 for all j in N_1, z_j = 0 for all j in N_0)
2464  *
2465  * min sum_{j in N\(N_0 & N_1)} (1 - x*_j) z_j
2466  * sum_{j in N\(N_0 & N_1)} a_j z_j >= (a_0 + 1) - sum_{j in N_1} a_j
2467  * z_j in {0,1}, j in N\(N_0 & N_1)
2468  *
2469  * to a knapsack problem in maximization form by complementing the variables
2470  *
2471  * sum_{j in N\(N_0 & N_1)} (1 - x*_j) -
2472  * max sum_{j in N\(N_0 & N_1)} (1 - x*_j) z°_j
2473  * sum_{j in N\(N_0 & N_1)} a_j z°_j <= sum_{j in N\N_0} a_j - (a_0 + 1)
2474  * z°_j in {0,1}, j in N\(N_0 & N_1)
2475  */
2476 
2477  /* gets weight and profit of variables in transformed knapsack problem */
2478  for( j = 0; j < nitems; j++ )
2479  {
2480  transweights[j] = weights[items[j]];
2481  transprofits[j] = 1.0 - solvals[items[j]];
2482  }
2483  /* gets capacity of transformed knapsack problem */
2484  transcapacity = fixedonesweight + itemsweight - capacity - 1;
2485 
2486  /* if capacity of transformed knapsack problem is less than zero, there is no cover
2487  * (when variables fixed to zero are not used)
2488  */
2489  if( transcapacity < 0 )
2490  {
2491  assert(!(*found));
2492  goto TERMINATE;
2493  }
2494 
2495  if( modtransused )
2496  {
2497  /* transforms the modified separation problem (under consideration of the following fixing:
2498  * z_j = 1 for all j in N_1, z_j = 0 for all j in N_0)
2499  *
2500  * min sum_{j in N\(N_0 & N_1)} (1 - x*_j) a_j z_j
2501  * sum_{j in N\(N_0 & N_1)} a_j z_j >= (a_0 + 1) - sum_{j in N_1} a_j
2502  * z_j in {0,1}, j in N\(N_0 & N_1)
2503  *
2504  * to a knapsack problem in maximization form by complementing the variables
2505  *
2506  * sum_{j in N\(N_0 & N_1)} (1 - x*_j) a_j -
2507  * max sum_{j in N\(N_0 & N_1)} (1 - x*_j) a_j z°_j
2508  * sum_{j in N\(N_0 & N_1)} a_j z°_j <= sum_{j in N\N_0} a_j - (a_0 + 1)
2509  * z°_j in {0,1}, j in N\(N_0 & N_1)
2510  */
2511 
2512  /* gets weight and profit of variables in modified transformed knapsack problem */
2513  for( j = 0; j < nitems; j++ )
2514  {
2515  transprofits[j] *= weights[items[j]];
2516  assert(SCIPisFeasPositive(scip, transprofits[j]));
2517  }
2518  }
2519 
2520  /* solves (modified) transformed knapsack problem approximately by solving the LP-relaxation of the (modified)
2521  * transformed knapsack problem using Dantzig's method and rounding down the solution.
2522  * let z°* be the solution, then
2523  * j in C, if z°*_j = 0 and
2524  * i in N\C, if z°*_j = 1.
2525  */
2526  SCIP_CALL( SCIPsolveKnapsackApproximately(scip, nitems, transweights, transprofits, transcapacity, items,
2527  noncovervars, covervars, nnoncovervars, ncovervars, NULL) );
2528  assert(checkSolveKnapsack(scip, nitems, transweights, transprofits, items, weights, solvals, modtransused));
2529 
2530  /* constructs cover C (sum_{j in C} a_j > a_0) */
2531  for( j = 0; j < *ncovervars; j++ )
2532  {
2533  (*coverweight) += weights[covervars[j]];
2534  }
2535 
2536  /* adds all variables from N_1 to C */
2537  for( j = 0; j < nfixedones; j++ )
2538  {
2539  covervars[*ncovervars] = fixedones[j];
2540  (*ncovervars)++;
2541  (*coverweight) += weights[fixedones[j]];
2542  }
2543 
2544  /* adds all variables from N_0 to N\C */
2545  for( j = 0; j < nfixedzeros; j++ )
2546  {
2547  noncovervars[*nnoncovervars] = fixedzeros[j];
2548  (*nnoncovervars)++;
2549  }
2550  assert((*ncovervars) + (*nnoncovervars) == nvars - (*ntightened));
2551  assert((*coverweight) > capacity);
2552  *found = TRUE;
2553 
2554  TERMINATE:
2555  /* frees temporary memory */
2556  SCIPfreeBufferArray(scip, &items);
2557  SCIPfreeBufferArray(scip, &fixedzeros);
2558  SCIPfreeBufferArray(scip, &fixedones);
2559  SCIPfreeBufferArray(scip, &transprofits);
2560  SCIPfreeBufferArray(scip, &transweights);
2561 
2562  SCIPdebugMessage(" get cover for knapsack constraint -- end\n");
2563 
2564  return SCIP_OKAY;
2565 }
2566 
2567 #ifndef NDEBUG
2568 /** checks if minweightidx is set correctly
2569  */
2570 static
2572  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2573  SCIP_Longint capacity, /**< capacity of knapsack */
2574  int* covervars, /**< pointer to store cover variables */
2575  int ncovervars, /**< pointer to store number of cover variables */
2576  SCIP_Longint coverweight, /**< pointer to store weight of cover */
2577  int minweightidx, /**< index of variable in cover variables with minimum weight */
2578  int j /**< current index in cover variables */
2579  )
2580 {
2581  SCIP_Longint minweight;
2582  int i;
2583 
2584  assert(weights != NULL);
2585  assert(covervars != NULL);
2586  assert(ncovervars > 0);
2587 
2588  minweight = weights[covervars[minweightidx]];
2589 
2590  /* checks if all cover variables before index j have weight greater than minweight */
2591  for( i = 0; i < j; i++ )
2592  {
2593  assert(weights[covervars[i]] > minweight);
2594  if( weights[covervars[i]] <= minweight )
2595  return FALSE;
2596  }
2597 
2598  /* checks if all variables before index j cannot be removed, i.e. i cannot be the next minweightidx */
2599  for( i = 0; i < j; i++ )
2600  {
2601  assert(coverweight - weights[covervars[i]] <= capacity);
2602  if( coverweight - weights[covervars[i]] > capacity )
2603  return FALSE;
2604  }
2605  return TRUE;
2606 }
2607 #endif
2608 
2609 
2610 /** 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$,
2611  * 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$
2612  */
2613 static
2615  SCIP* scip, /**< SCIP data structure */
2616  SCIP_Real* solvals, /**< solution values of all problem variables */
2617  int* covervars, /**< cover variables */
2618  int ncovervars, /**< number of cover variables */
2619  int* varsC1, /**< pointer to store variables in C1 */
2620  int* varsC2, /**< pointer to store variables in C2 */
2621  int* nvarsC1, /**< pointer to store number of variables in C1 */
2622  int* nvarsC2 /**< pointer to store number of variables in C2 */
2623  )
2624 {
2625  int j;
2626 
2627  assert(scip != NULL);
2628  assert(ncovervars >= 0);
2629  assert(solvals != NULL);
2630  assert(covervars != NULL);
2631  assert(varsC1 != NULL);
2632  assert(varsC2 != NULL);
2633  assert(nvarsC1 != NULL);
2634  assert(nvarsC2 != NULL);
2635 
2636  *nvarsC1 = 0;
2637  *nvarsC2 = 0;
2638  for( j = 0; j < ncovervars; j++ )
2639  {
2640  assert(SCIPisFeasGT(scip, solvals[covervars[j]], 0.0));
2641 
2642  /* variable has solution value one */
2643  if( SCIPisGE(scip, solvals[covervars[j]], 1.0) )
2644  {
2645  varsC2[*nvarsC2] = covervars[j];
2646  (*nvarsC2)++;
2647  }
2648  /* variable has solution value less than one */
2649  else
2650  {
2651  assert(SCIPisLT(scip, solvals[covervars[j]], 1.0));
2652  varsC1[*nvarsC1] = covervars[j];
2653  (*nvarsC1)++;
2654  }
2655  }
2656  assert((*nvarsC1) + (*nvarsC2) == ncovervars);
2657 }
2658 
2659 /** changes given partition (C_1,C_2) of minimal cover C, if |C1| = 1, by moving one and two (if possible) variables from
2660  * C2 to C1 if |C1| = 1 and |C1| = 0, respectively.
2661  */
2662 static
2664  SCIP* scip, /**< SCIP data structure */
2665  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2666  int* varsC1, /**< pointer to store variables in C1 */
2667  int* varsC2, /**< pointer to store variables in C2 */
2668  int* nvarsC1, /**< pointer to store number of variables in C1 */
2669  int* nvarsC2 /**< pointer to store number of variables in C2 */
2670  )
2671 {
2672  SCIP_Real* sortkeysC2;
2673  int j;
2674 
2675  assert(*nvarsC1 >= 0 && *nvarsC1 <= 1);
2676  assert(*nvarsC2 > 0);
2677 
2678  /* allocates temporary memory */
2679  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, *nvarsC2) );
2680 
2681  /* sorts variables in C2 such that a_1 >= .... >= a_|C2| */
2682  for( j = 0; j < *nvarsC2; j++ )
2683  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
2684  SCIPsortDownRealInt(sortkeysC2, varsC2, *nvarsC2);
2685 
2686  /* adds one or two variable from C2 with smallest weight to C1 and removes them from C2 */
2687  assert(*nvarsC2 == 1 || weights[varsC2[(*nvarsC2)-1]] <= weights[varsC2[(*nvarsC2)-2]]);
2688  while( *nvarsC1 < 2 && *nvarsC2 > 0 )
2689  {
2690  varsC1[*nvarsC1] = varsC2[(*nvarsC2)-1];
2691  (*nvarsC1)++;
2692  (*nvarsC2)--;
2693  }
2694 
2695  /* frees temporary memory */
2696  SCIPfreeBufferArray(scip, &sortkeysC2);
2697 
2698  return SCIP_OKAY;
2699 }
2700 
2701 /** changes given partition (C_1,C_2) of feasible set C, if |C1| = 1, by moving one variable from C2 to C1 */
2702 static
2704  SCIP* scip, /**< SCIP data structure */
2705  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2706  int* varsC1, /**< pointer to store variables in C1 */
2707  int* varsC2, /**< pointer to store variables in C2 */
2708  int* nvarsC1, /**< pointer to store number of variables in C1 */
2709  int* nvarsC2 /**< pointer to store number of variables in C2 */
2710  )
2711 {
2712  SCIP_Real* sortkeysC2;
2713  int j;
2714 
2715  assert(*nvarsC1 >= 0 && *nvarsC1 <= 1);
2716  assert(*nvarsC2 > 0);
2717 
2718  /* allocates temporary memory */
2719  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, *nvarsC2) );
2720 
2721  /* sorts variables in C2 such that a_1 >= .... >= a_|C2| */
2722  for( j = 0; j < *nvarsC2; j++ )
2723  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
2724  SCIPsortDownRealInt(sortkeysC2, varsC2, *nvarsC2);
2725 
2726  /* adds variable from C2 with smallest weight to C1 and removes it from C2 */
2727  assert(*nvarsC2 == 1 || weights[varsC2[(*nvarsC2)-1]] <= weights[varsC2[(*nvarsC2)-2]]);
2728  varsC1[*nvarsC1] = varsC2[(*nvarsC2)-1];
2729  (*nvarsC1)++;
2730  (*nvarsC2)--;
2731 
2732  /* frees temporary memory */
2733  SCIPfreeBufferArray(scip, &sortkeysC2);
2734 
2735  return SCIP_OKAY;
2736 }
2737 
2738 
2739 /** 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$
2740  * and \f$F \cap R = \emptyset\f$; chooses partition as follows \f$R = \{ j \in N \setminus C : x^*_j = 0 \}\f$ and
2741  * \f$F = (N \setminus C) \setminus F\f$
2742  */
2743 static
2745  SCIP* scip, /**< SCIP data structure */
2746  SCIP_Real* solvals, /**< solution values of all problem variables */
2747  int* noncovervars, /**< noncover variables */
2748  int nnoncovervars, /**< number of noncover variables */
2749  int* varsF, /**< pointer to store variables in F */
2750  int* varsR, /**< pointer to store variables in R */
2751  int* nvarsF, /**< pointer to store number of variables in F */
2752  int* nvarsR /**< pointer to store number of variables in R */
2753  )
2754 {
2755  int j;
2756 
2757  assert(scip != NULL);
2758  assert(nnoncovervars >= 0);
2759  assert(solvals != NULL);
2760  assert(noncovervars != NULL);
2761  assert(varsF != NULL);
2762  assert(varsR != NULL);
2763  assert(nvarsF != NULL);
2764  assert(nvarsR != NULL);
2765 
2766  *nvarsF = 0;
2767  *nvarsR = 0;
2768 
2769  for( j = 0; j < nnoncovervars; j++ )
2770  {
2771  /* variable has solution value zero */
2772  if( SCIPisFeasEQ(scip, solvals[noncovervars[j]], 0.0) )
2773  {
2774  varsR[*nvarsR] = noncovervars[j];
2775  (*nvarsR)++;
2776  }
2777  /* variable has solution value greater than zero */
2778  else
2779  {
2780  assert(SCIPisFeasGT(scip, solvals[noncovervars[j]], 0.0));
2781  varsF[*nvarsF] = noncovervars[j];
2782  (*nvarsF)++;
2783  }
2784  }
2785  assert((*nvarsF) + (*nvarsR) == nnoncovervars);
2786 }
2787 
2788 /** sorts variables in F, C_2, and R according to the second level lifting sequence that will be used in the sequential
2789  * lifting procedure
2790  */
2791 static
2793  SCIP* scip, /**< SCIP data structure */
2794  SCIP_Real* solvals, /**< solution values of all problem variables */
2795  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2796  int* varsF, /**< pointer to store variables in F */
2797  int* varsC2, /**< pointer to store variables in C2 */
2798  int* varsR, /**< pointer to store variables in R */
2799  int nvarsF, /**< number of variables in F */
2800  int nvarsC2, /**< number of variables in C2 */
2801  int nvarsR /**< number of variables in R */
2802  )
2803 {
2804  SORTKEYPAIR** sortkeypairsF;
2805  SORTKEYPAIR* sortkeypairsFstore;
2806  SCIP_Real* sortkeysC2;
2807  SCIP_Real* sortkeysR;
2808  int j;
2809 
2810  assert(scip != NULL);
2811  assert(solvals != NULL);
2812  assert(weights != NULL);
2813  assert(varsF != NULL);
2814  assert(varsC2 != NULL);
2815  assert(varsR != NULL);
2816  assert(nvarsF >= 0);
2817  assert(nvarsC2 >= 0);
2818  assert(nvarsR >= 0);
2819 
2820  /* allocates temporary memory */
2821  SCIP_CALL( SCIPallocMemoryArray(scip, &sortkeypairsF, nvarsF) );
2822  SCIP_CALL( SCIPallocMemoryArray(scip, &sortkeypairsFstore, nvarsF) );
2823  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, nvarsC2) );
2824  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysR, nvarsR) );
2825 
2826  /* gets sorting key for variables in F corresponding to the following lifting sequence
2827  * sequence 1: non-increasing absolute difference between x*_j and the value the variable is fixed to, i.e.
2828  * x*_1 >= x*_2 >= ... >= x*_|F|
2829  * in case of equality uses
2830  * sequence 4: non-increasing a_j, i.e. a_1 >= a_2 >= ... >= a_|C_2|
2831  */
2832  for( j = 0; j < nvarsF; j++ )
2833  {
2834  sortkeypairsF[j] = &(sortkeypairsFstore[j]);
2835  sortkeypairsF[j]->key1 = solvals[varsF[j]];
2836  sortkeypairsF[j]->key2 = (SCIP_Real) weights[varsF[j]];
2837  }
2838 
2839  /* gets sorting key for variables in C_2 corresponding to the following lifting sequence
2840  * sequence 4: non-increasing a_j, i.e. a_1 >= a_2 >= ... >= a_|C_2|
2841  */
2842  for( j = 0; j < nvarsC2; j++ )
2843  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
2844 
2845  /* gets sorting key for variables in R corresponding to the following lifting sequence
2846  * sequence 4: non-increasing a_j, i.e. a_1 >= a_2 >= ... >= a_|R|
2847  */
2848  for( j = 0; j < nvarsR; j++ )
2849  sortkeysR[j] = (SCIP_Real) weights[varsR[j]];
2850 
2851  /* sorts F, C2 and R */
2852  if( nvarsF > 0 )
2853  {
2854  SCIPsortDownPtrInt((void**)sortkeypairsF, varsF, compSortkeypairs, nvarsF);
2855  }
2856  if( nvarsC2 > 0 )
2857  {
2858  SCIPsortDownRealInt(sortkeysC2, varsC2, nvarsC2);
2859  }
2860  if( nvarsR > 0)
2861  {
2862  SCIPsortDownRealInt(sortkeysR, varsR, nvarsR);
2863  }
2864 
2865  /* frees temporary memory */
2866  SCIPfreeBufferArray(scip, &sortkeysR);
2867  SCIPfreeBufferArray(scip, &sortkeysC2);
2868  SCIPfreeMemoryArray(scip, &sortkeypairsFstore);
2869  SCIPfreeMemoryArray(scip, &sortkeypairsF);
2870 
2871  return SCIP_OKAY;
2872 }
2873 
2874 /** categorizies GUBs of knapsack GUB partion into GOC1, GNC1, GF, GC2, and GR and computes a lifting sequence of the GUBs
2875  * for the sequential GUB wise lifting procedure
2876  */
2877 static
2879  SCIP* scip, /**< SCIP data structure */
2880  SCIP_GUBSET* gubset, /**< GUB set data structure */
2881  SCIP_Real* solvals, /**< solution values of variables in knapsack constraint */
2882  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
2883  int* varsC1, /**< variables in C1 */
2884  int* varsC2, /**< variables in C2 */
2885  int* varsF, /**< variables in F */
2886  int* varsR, /**< variables in R */
2887  int nvarsC1, /**< number of variables in C1 */
2888  int nvarsC2, /**< number of variables in C2 */
2889  int nvarsF, /**< number of variables in F */
2890  int nvarsR, /**< number of variables in R */
2891  int* gubconsGC1, /**< pointer to store GUBs in GC1(GNC1+GOC1) */
2892  int* gubconsGC2, /**< pointer to store GUBs in GC2 */
2893  int* gubconsGFC1, /**< pointer to store GUBs in GFC1(GNC1+GF) */
2894  int* gubconsGR, /**< pointer to store GUBs in GR */
2895  int* ngubconsGC1, /**< pointer to store number of GUBs in GC1(GNC1+GOC1) */
2896  int* ngubconsGC2, /**< pointer to store number of GUBs in GC2 */
2897  int* ngubconsGFC1, /**< pointer to store number of GUBs in GFC1(GNC1+GF) */
2898  int* ngubconsGR, /**< pointer to store number of GUBs in GR */
2899  int* ngubconscapexceed, /**< pointer to store number of GUBs with only capacity exceeding variables */
2900  int* maxgubvarssize /**< pointer to store the maximal size of GUB constraints */
2901  )
2902 {
2903 #if 0 /* not required */
2904  SORTKEYPAIR** sortkeypairsF;
2905 #endif
2906  SORTKEYPAIR** sortkeypairsGFC1;
2907  SORTKEYPAIR* sortkeypairsGFC1store;
2908  SCIP_Real* sortkeysC1;
2909  SCIP_Real* sortkeysC2;
2910  SCIP_Real* sortkeysR;
2911  int* nC1varsingubcons;
2912  int var;
2913  int gubconsidx;
2914  int varidx;
2915  int ngubconss;
2916  int ngubconsGOC1;
2917  int targetvar;
2918  int nvarsprocessed;
2919  int i;
2920  int j;
2921 
2922 #if GUBSPLITGNC1GUBS
2923  SCIP_Bool gubconswithF;
2924  int origngubconss;
2925  origngubconss = gubset->ngubconss;
2926 #endif
2927 
2928  assert(scip != NULL);
2929  assert(gubset != NULL);
2930  assert(solvals != NULL);
2931  assert(weights != NULL);
2932  assert(varsC1 != NULL);
2933  assert(varsC2 != NULL);
2934  assert(varsF != NULL);
2935  assert(varsR != NULL);
2936  assert(nvarsC1 > 0);
2937  assert(nvarsC2 >= 0);
2938  assert(nvarsF >= 0);
2939  assert(nvarsR >= 0);
2940  assert(gubconsGC1 != NULL);
2941  assert(gubconsGC2 != NULL);
2942  assert(gubconsGFC1 != NULL);
2943  assert(gubconsGR != NULL);
2944  assert(ngubconsGC1 != NULL);
2945  assert(ngubconsGC2 != NULL);
2946  assert(ngubconsGFC1 != NULL);
2947  assert(ngubconsGR != NULL);
2948  assert(maxgubvarssize != NULL);
2949 
2950  ngubconss = gubset->ngubconss;
2951  nvarsprocessed = 0;
2952  ngubconsGOC1 = 0;
2953 
2954  /* GUBs are categorized into different types according to the variables in volved
2955  * - GOC1: involves variables in C1 only -- no C2, R, F
2956  * - GNC1: involves variables in C1 and F (and R) -- no C2
2957  * - GF: involves variables in F (and R) only -- no C1, C2
2958  * - GC2: involves variables in C2 only -- no C1, R, F
2959  * - GR: involves variables in R only -- no C1, C2, F
2960  * which requires splitting GUBs in case they include variable in F and R.
2961  *
2962  * afterwards all GUBs (except GOC1 GUBs, which we do not need to lift) are sorted by a two level lifting sequence.
2963  * - first ordering level is: GFC1 (GNC1+GF), GC2, and GR.
2964  * - second ordering level is
2965  * GFC1: non-increasing number of variables in F and non-increasing max{x*_k : k in GFC1_j} in case of equality
2966  * GC2: non-increasing max{ a_k : k in GC2_j}; note that |GFC2_j| = 1
2967  * GR: non-increasing max{ a_k : k in GR_j}
2968  *
2969  * in additon, another GUB union, which is helpful for the lifting procedure, is formed
2970  * - GC1: GUBs of category GOC1 and GNC1
2971  * with second ordering level non-decreasing min{ a_k : k in GC1_j };
2972  * note that min{ a_k : k in GC1_j } always comes from the first variable in the GUB
2973  */
2974 
2975  /* allocates temporary memory */
2976  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC1, nvarsC1) );
2977 #if 0 /* not required */
2978  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeypairsF, nvarsF) );
2979 #endif
2980  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysC2, nvarsC2) );
2981  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeysR, nvarsR) );
2982 
2983 
2984  /* to get the GUB lifting sequence, we first sort all variables in F, C2, and R
2985  * - F: non-increasing x*_j and non-increasing a_j in case of equality
2986  * - C2: non-increasing a_j
2987  * - R: non-increasing a_j
2988  * furthermore, sort C1 variables as needed for initializing the minweight table (non-increasing a_j).
2989  */
2990 
2991  /* gets sorting key for variables in C1 corresponding to the following ordering
2992  * non-decreasing a_j, i.e. a_1 <= a_2 <= ... <= a_|C_1|
2993  */
2994  for( j = 0; j < nvarsC1; j++ )
2995  {
2996  /* gets sortkeys */
2997  sortkeysC1[j] = (SCIP_Real) weights[varsC1[j]];
2998 
2999  /* update status of variable in its gub constraint */
3000  gubconsidx = gubset->gubconssidx[varsC1[j]];
3001  varidx = gubset->gubvarsidx[varsC1[j]];
3002  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_C1;
3003  }
3004 
3005  /* gets sorting key for variables in F corresponding to the following ordering
3006  * non-increasing x*_j, i.e., x*_1 >= x*_2 >= ... >= x*_|F|, and
3007  * non-increasing a_j, i.e., a_1 >= a_2 >= ... >= a_|F| in case of equality
3008  * and updates status of each variable in F in GUB set data structure
3009  */
3010  for( j = 0; j < nvarsF; j++ )
3011  {
3012 #if 0 /* not required */
3013  /* gets sortkeys */
3014  SCIP_CALL( SCIPallocBuffer(scip, &sortkeypairsF[j]) );
3015  sortkeypairsF[j]->key1 = solvals[varsF[j]];
3016  sortkeypairsF[j]->key2 = (SCIP_Real) weights[varsF[j]];
3017 #endif
3018 
3019  /* update status of variable in its gub constraint */
3020  gubconsidx = gubset->gubconssidx[varsF[j]];
3021  varidx = gubset->gubvarsidx[varsF[j]];
3022  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_F;
3023  }
3024 
3025  /* gets sorting key for variables in C2 corresponding to the following ordering
3026  * non-increasing a_j, i.e., a_1 >= a_2 >= ... >= a_|C2|
3027  * and updates status of each variable in F in GUB set data structure
3028  */
3029  for( j = 0; j < nvarsC2; j++ )
3030  {
3031  /* gets sortkeys */
3032  sortkeysC2[j] = (SCIP_Real) weights[varsC2[j]];
3033 
3034  /* update status of variable in its gub constraint */
3035  gubconsidx = gubset->gubconssidx[varsC2[j]];
3036  varidx = gubset->gubvarsidx[varsC2[j]];
3037  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_C2;
3038  }
3039 
3040  /* gets sorting key for variables in R corresponding to the following ordering
3041  * non-increasing a_j, i.e., a_1 >= a_2 >= ... >= a_|R|
3042  * and updates status of each variable in F in GUB set data structure
3043  */
3044  for( j = 0; j < nvarsR; j++ )
3045  {
3046  /* gets sortkeys */
3047  sortkeysR[j] = (SCIP_Real) weights[varsR[j]];
3048 
3049  /* update status of variable in its gub constraint */
3050  gubconsidx = gubset->gubconssidx[varsR[j]];
3051  varidx = gubset->gubvarsidx[varsR[j]];
3052  gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] = GUBVARSTATUS_BELONGSTOSET_R;
3053  }
3054 
3055  /* sorts C1, F, C2 and R */
3056  if( nvarsC1 > 0 )
3057  {
3058  SCIPsortRealInt(sortkeysC1, varsC1, nvarsC1);
3059  }
3060 #if 0 /* not required */
3061  if( nvarsF > 0 )
3062  {
3063  SCIPsortDownPtrInt((void**)sortkeypairsF, varsF, compSortkeypairs, nvarsF);
3064  }
3065 #endif
3066  if( nvarsC2 > 0 )
3067  {
3068  SCIPsortDownRealInt(sortkeysC2, varsC2, nvarsC2);
3069  }
3070  if( nvarsR > 0)
3071  {
3072  SCIPsortDownRealInt(sortkeysR, varsR, nvarsR);
3073  }
3074 
3075  /* frees temporary memory */
3076  SCIPfreeBufferArray(scip, &sortkeysR);
3077  SCIPfreeBufferArray(scip, &sortkeysC2);
3078 #if 0 /* not required */
3079  for( j = nvarsF-1; j >= 0; j-- )
3080  SCIPfreeBuffer(scip, &sortkeypairsF[j]);
3081  SCIPfreeBufferArray(scip, &sortkeypairsF);
3082 #endif
3083  SCIPfreeBufferArray(scip, &sortkeysC1);
3084 
3085  /* allocate and initialize temporary memory for sorting GUB constraints */
3086  SCIP_CALL( SCIPallocMemoryArray(scip, &sortkeypairsGFC1, ngubconss) );
3087  SCIP_CALL( SCIPallocMemoryArray(scip, &sortkeypairsGFC1store, ngubconss) );
3088  SCIP_CALL( SCIPallocBufferArray(scip, &nC1varsingubcons, ngubconss) );
3089  BMSclearMemoryArray(nC1varsingubcons, ngubconss);
3090  for( i = 0; i < ngubconss; i++)
3091  {
3092  sortkeypairsGFC1[i] = &(sortkeypairsGFC1store[i]);
3093  sortkeypairsGFC1[i]->key1 = 0.0;
3094  sortkeypairsGFC1[i]->key2 = 0.0;
3095  }
3096  *ngubconsGC1 = 0;
3097  *ngubconsGC2 = 0;
3098  *ngubconsGFC1 = 0;
3099  *ngubconsGR = 0;
3100  *ngubconscapexceed = 0;
3101  *maxgubvarssize = 0;
3102 
3103 #ifndef NDEBUG
3104  for( i = 0; i < gubset->ngubconss; i++ )
3105  assert(gubset->gubconsstatus[i] == GUBCONSSTATUS_UNINITIAL);
3106 #endif
3107 
3108  /* stores GUBs of group GC1 (GOC1+GNC1) and part of the GUBs of group GFC1 (GNC1 GUBs) and sorts variables in these GUBs
3109  * s.t. C1 variables come first (will automatically be sorted by non-decreasing weight).
3110  * gets sorting keys for GUBs of type GFC1 corresponding to the following ordering
3111  * non-increasing number of variables in F, and
3112  * non-increasing max{x*_k : k in GFC1_j} in case of equality
3113  */
3114  for( i = 0; i < nvarsC1; i++ )
3115  {
3116  int nvarsC1capexceed;
3117 
3118  nvarsC1capexceed = 0;
3119 
3120  var = varsC1[i];
3121  gubconsidx = gubset->gubconssidx[var];
3122  varidx = gubset->gubvarsidx[var];
3123 
3124  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3125  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_C1);
3126 
3127  /* current C1 variable is put to the front of its GUB where C1 part is stored by non-decreasing weigth;
3128  * note that variables in C1 are already sorted by non-decreasing weigth
3129  */
3130  targetvar = gubset->gubconss[gubconsidx]->gubvars[nC1varsingubcons[gubconsidx]];
3131  GUBsetSwapVars(scip, gubset, var, targetvar);
3132  nC1varsingubcons[gubconsidx]++;
3133 
3134  /* the GUB was already handled (status set and stored in its group) by another variable of the GUB */
3135  if( gubset->gubconsstatus[gubconsidx] != GUBCONSSTATUS_UNINITIAL )
3136  {
3137  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GOC1
3138  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3139  continue;
3140  }
3141 
3142  /* determine the status of the current GUB constraint, GOC1 or GNC1; GUBs involving R variables are split into
3143  * GOC1/GNC1 and GF, if wanted. also update sorting key if GUB is of type GFC1 (GNC1)
3144  */
3145 #if GUBSPLITGNC1GUBS
3146  gubconswithF = FALSE;
3147 #endif
3148  for( j = 0; j < gubset->gubconss[gubconsidx]->ngubvars; j++ )
3149  {
3150  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[j] != GUBVARSTATUS_BELONGSTOSET_C2);
3151 
3152  /* C1-variable: update number of C1/capacity exceeding variables */
3153  if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_C1 )
3154  {
3155  nvarsC1capexceed++;
3156  nvarsprocessed++;
3157  }
3158  /* F-variable: update sort key (number of F variables in GUB) of corresponding GFC1-GUB */
3159  else if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_F )
3160  {
3161 #if GUBSPLITGNC1GUBS
3162  gubconswithF = TRUE;
3163 #endif
3164  sortkeypairsGFC1[*ngubconsGFC1]->key1 += 1.0;
3165 
3166  if( solvals[gubset->gubconss[gubconsidx]->gubvars[j]] > sortkeypairsGFC1[*ngubconsGFC1]->key2 )
3167  sortkeypairsGFC1[*ngubconsGFC1]->key2 = solvals[gubset->gubconss[gubconsidx]->gubvars[j]];
3168  }
3169  else if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_CAPACITYEXCEEDED )
3170  {
3171  nvarsC1capexceed++;
3172  }
3173  else
3174  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_R);
3175  }
3176 
3177  /* update set of GC1 GUBs */
3178  gubconsGC1[*ngubconsGC1] = gubconsidx;
3179  (*ngubconsGC1)++;
3180 
3181  /* update maximum size of all GUB constraints */
3182  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3183  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3184 
3185  /* set status of GC1-GUB (GOC1 or GNC1) and update set of GFC1 GUBs */
3186  if( nvarsC1capexceed == gubset->gubconss[gubconsidx]->ngubvars )
3187  {
3188  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GOC1;
3189  ngubconsGOC1++;
3190  }
3191  else
3192  {
3193 #if GUBSPLITGNC1GUBS
3194  /* only variables in C1 and R -- no in F: GUB will be split into GR and GOC1 GUBs */
3195  if( !gubconswithF )
3196  {
3197  GUBVARSTATUS movevarstatus;
3198 
3199  assert(gubset->ngubconss < gubset->nvars);
3200 
3201  /* create a new GUB for GR part of splitting */
3202  SCIP_CALL( GUBconsCreate(scip, &gubset->gubconss[gubset->ngubconss]) );
3203  gubset->ngubconss++;
3204  ngubconss = gubset->ngubconss;
3205 
3206  /* fill GR with R variables in current GUB */
3207  for( j = gubset->gubconss[gubconsidx]->ngubvars-1; j >= 0; j-- )
3208  {
3209  movevarstatus = gubset->gubconss[gubconsidx]->gubvarsstatus[j];
3210  if( movevarstatus != GUBVARSTATUS_BELONGSTOSET_C1 )
3211  {
3212  assert(movevarstatus == GUBVARSTATUS_BELONGSTOSET_R || movevarstatus == GUBVARSTATUS_CAPACITYEXCEEDED);
3213  SCIP_CALL( GUBsetMoveVar(scip, gubset, vars, gubset->gubconss[gubconsidx]->gubvars[j],
3214  gubconsidx, ngubconss-1) );
3215  gubset->gubconss[ngubconss-1]->gubvarsstatus[gubset->gubconss[ngubconss-1]->ngubvars-1] =
3216  movevarstatus;
3217  }
3218  }
3219 
3220  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GOC1;
3221  ngubconsGOC1++;
3222 
3223  gubset->gubconsstatus[ngubconss-1] = GUBCONSSTATUS_BELONGSTOSET_GR;
3224  gubconsGR[*ngubconsGR] = ngubconss-1;
3225  (*ngubconsGR)++;
3226  }
3227  /* variables in C1, F, and maybe R: GNC1 GUB */
3228  else
3229  {
3230  assert(gubconswithF);
3231 
3232  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GNC1;
3233  gubconsGFC1[*ngubconsGFC1] = gubconsidx;
3234  (*ngubconsGFC1)++;
3235  }
3236 #else
3237  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GNC1;
3238  gubconsGFC1[*ngubconsGFC1] = gubconsidx;
3239  (*ngubconsGFC1)++;
3240 #endif
3241  }
3242  }
3243 
3244  /* stores GUBs of group GC2 (only trivial GUBs); sorting is not required because the C2 variables (which we loop over)
3245  * are already sorted correctly
3246  */
3247  for( i = 0; i < nvarsC2; i++ )
3248  {
3249  var = varsC2[i];
3250  gubconsidx = gubset->gubconssidx[var];
3251  varidx = gubset->gubvarsidx[var];
3252 
3253  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3254  assert(gubset->gubconss[gubconsidx]->ngubvars == 1);
3255  assert(varidx == 0);
3256  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_C2);
3257  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_UNINITIAL);
3258 
3259  /* set status of GC2 GUB */
3260  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GC2;
3261 
3262  /* update group of GC2 GUBs */
3263  gubconsGC2[*ngubconsGC2] = gubconsidx;
3264  (*ngubconsGC2)++;
3265 
3266  /* update maximum size of all GUB constraints */
3267  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3268  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3269 
3270  nvarsprocessed++;
3271  }
3272 
3273  /* stores remaining part of the GUBs of group GFC1 (GF GUBs) and gets GUB sorting keys corresp. to following ordering
3274  * non-increasing number of variables in F, and
3275  * non-increasing max{x*_k : k in GFC1_j} in case of equality
3276  */
3277  for( i = 0; i < nvarsF; i++ )
3278  {
3279  var = varsF[i];
3280  gubconsidx = gubset->gubconssidx[var];
3281  varidx = gubset->gubvarsidx[var];
3282 
3283  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3284  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_F);
3285 
3286  nvarsprocessed++;
3287 
3288  /* the GUB was already handled (status set and stored in its group) by another variable of the GUB */
3289  if( gubset->gubconsstatus[gubconsidx] != GUBCONSSTATUS_UNINITIAL )
3290  {
3291  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF
3292  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3293  continue;
3294  }
3295 
3296  /* set status of GF GUB */
3297  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GF;
3298 
3299  /* update sorting key of corresponding GFC1 GUB */
3300  for( j = 0; j < gubset->gubconss[gubconsidx]->ngubvars; j++ )
3301  {
3302  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[j] != GUBVARSTATUS_BELONGSTOSET_C2
3303  && gubset->gubconss[gubconsidx]->gubvarsstatus[j] != GUBVARSTATUS_BELONGSTOSET_C1);
3304 
3305  /* F-variable: update sort key (number of F variables in GUB) of corresponding GFC1-GUB */
3306  if( gubset->gubconss[gubconsidx]->gubvarsstatus[j] == GUBVARSTATUS_BELONGSTOSET_F )
3307  {
3308  sortkeypairsGFC1[*ngubconsGFC1]->key1 += 1.0;
3309 
3310  if( solvals[gubset->gubconss[gubconsidx]->gubvars[j]] > sortkeypairsGFC1[*ngubconsGFC1]->key2 )
3311  sortkeypairsGFC1[*ngubconsGFC1]->key2 = solvals[gubset->gubconss[gubconsidx]->gubvars[j]];
3312  }
3313  }
3314 
3315  /* update set of GFC1 GUBs */
3316  gubconsGFC1[*ngubconsGFC1] = gubconsidx;
3317  (*ngubconsGFC1)++;
3318 
3319  /* update maximum size of all GUB constraints */
3320  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3321  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3322  }
3323 
3324  /* stores GUBs of group GR; sorting is not required because the R variables (which we loop over) are already sorted
3325  * correctly
3326  */
3327  for( i = 0; i < nvarsR; i++ )
3328  {
3329  var = varsR[i];
3330  gubconsidx = gubset->gubconssidx[var];
3331  varidx = gubset->gubvarsidx[var];
3332 
3333  assert(gubconsidx >= 0 && gubconsidx < ngubconss);
3334  assert(gubset->gubconss[gubconsidx]->gubvarsstatus[varidx] == GUBVARSTATUS_BELONGSTOSET_R);
3335 
3336  nvarsprocessed++;
3337 
3338  /* the GUB was already handled (status set and stored in its group) by another variable of the GUB */
3339  if( gubset->gubconsstatus[gubconsidx] != GUBCONSSTATUS_UNINITIAL )
3340  {
3341  assert(gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GR
3342  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF
3343  || gubset->gubconsstatus[gubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
3344  continue;
3345  }
3346 
3347  /* set status of GR GUB */
3348  gubset->gubconsstatus[gubconsidx] = GUBCONSSTATUS_BELONGSTOSET_GR;
3349 
3350  /* update set of GR GUBs */
3351  gubconsGR[*ngubconsGR] = gubconsidx;
3352  (*ngubconsGR)++;
3353 
3354  /* update maximum size of all GUB constraints */
3355  if( gubset->gubconss[gubconsidx]->gubvarssize > *maxgubvarssize )
3356  *maxgubvarssize = gubset->gubconss[gubconsidx]->gubvarssize;
3357  }
3358  assert(nvarsprocessed == nvarsC1 + nvarsC2 + nvarsF + nvarsR);
3359 
3360  /* update number of GUBs with only capacity exceeding variables (will not be used for lifting) */
3361  (*ngubconscapexceed) = ngubconss - (ngubconsGOC1 + (*ngubconsGC2) + (*ngubconsGFC1) + (*ngubconsGR));
3362  assert(*ngubconscapexceed >= 0);
3363 #ifndef NDEBUG
3364  {
3365  int check;
3366 
3367  check = 0;
3368 
3369  /* remaining not handled GUBs should only contain capacity exceeding variables */
3370  for( i = 0; i < ngubconss; i++ )
3371  {
3372  if( gubset->gubconsstatus[i] == GUBCONSSTATUS_UNINITIAL )
3373  check++;
3374  }
3375  assert(check == *ngubconscapexceed);
3376  }
3377 #endif
3378 
3379  /* sort GFCI GUBs according to computed sorting keys */
3380  if( (*ngubconsGFC1) > 0 )
3381  {
3382  SCIPsortDownPtrInt((void**)sortkeypairsGFC1, gubconsGFC1, compSortkeypairs, (*ngubconsGFC1));
3383  }
3384 
3385  /* free temporary memory */
3386 #if GUBSPLITGNC1GUBS
3387  ngubconss = origngubconss;
3388 #endif
3389  SCIPfreeBufferArray(scip, &nC1varsingubcons);
3390  SCIPfreeMemoryArray(scip, &sortkeypairsGFC1store);
3391  SCIPfreeMemoryArray(scip, &sortkeypairsGFC1);
3392 
3393  return SCIP_OKAY;
3394 }
3395 
3396 /** enlarges minweight table to at least the given length */
3397 static
3399  SCIP* scip, /**< SCIP data structure */
3400  SCIP_Longint** minweightsptr, /**< pointer to minweights table */
3401  int* minweightslen, /**< pointer to store number of entries in minweights table (incl. z=0) */
3402  int* minweightssize, /**< pointer to current size of minweights table */
3403  int newlen /**< new length of minweights table */
3404  )
3405 {
3406  int j;
3407 
3408  assert(minweightsptr != NULL);
3409  assert(*minweightsptr != NULL);
3410  assert(minweightslen != NULL);
3411  assert(*minweightslen >= 0);
3412  assert(minweightssize != NULL);
3413  assert(*minweightssize >= 0);
3414 
3415  if( newlen > *minweightssize )
3416  {
3417  int newsize;
3418 
3419  /* reallocate table memory */
3420  newsize = SCIPcalcMemGrowSize(scip, newlen);
3421  SCIP_CALL( SCIPreallocBufferArray(scip, minweightsptr, newsize) );
3422  *minweightssize = newsize;
3423  }
3424  assert(newlen <= *minweightssize);
3425 
3426  /* initialize new elements */
3427  for( j = *minweightslen; j < newlen; ++j )
3428  (*minweightsptr)[j] = SCIP_LONGINT_MAX;
3429  *minweightslen = newlen;
3430 
3431  return SCIP_OKAY;
3432 }
3433 
3434 /** lifts given inequality
3435  * sum_{j in M_1} x_j <= alpha_0
3436  * valid for
3437  * 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 }
3438  * to a valid inequality
3439  * 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
3440  * <= alpha_0 + sum_{j in M_2} alpha_j
3441  * for
3442  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 };
3443  * uses sequential up-lifting for the variables in F, sequential down-lifting for the variable in M_2, and
3444  * sequential up-lifting for the variables in R; procedure can be used to strengthen minimal cover inequalities and
3445  * extended weight inequalities.
3446  */
3447 static
3449  SCIP* scip, /**< SCIP data structure */
3450  SCIP_VAR** vars, /**< variables in knapsack constraint */
3451  int nvars, /**< number of variables in knapsack constraint */
3452  int ntightened, /**< number of variables with tightened upper bound */
3453  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
3454  SCIP_Longint capacity, /**< capacity of knapsack */
3455  SCIP_Real* solvals, /**< solution values of all problem variables */
3456  int* varsM1, /**< variables in M_1 */
3457  int* varsM2, /**< variables in M_2 */
3458  int* varsF, /**< variables in F */
3459  int* varsR, /**< variables in R */
3460  int nvarsM1, /**< number of variables in M_1 */
3461  int nvarsM2, /**< number of variables in M_2 */
3462  int nvarsF, /**< number of variables in F */
3463  int nvarsR, /**< number of variables in R */
3464  int alpha0, /**< rights hand side of given valid inequality */
3465  int* liftcoefs, /**< pointer to store lifting coefficient of vars in knapsack constraint */
3466  SCIP_Real* cutact, /**< pointer to store activity of lifted valid inequality */
3467  int* liftrhs /**< pointer to store right hand side of the lifted valid inequality */
3468  )
3469 {
3470  SCIP_Longint* minweights;
3471  SCIP_Real* sortkeys;
3472  SCIP_Longint fixedonesweight;
3473  int minweightssize;
3474  int minweightslen;
3475  int j;
3476  int w;
3477 
3478  assert(scip != NULL);
3479  assert(vars != NULL);
3480  assert(nvars >= 0);
3481  assert(weights != NULL);
3482  assert(capacity >= 0);
3483  assert(solvals != NULL);
3484  assert(varsM1 != NULL);
3485  assert(varsM2 != NULL);
3486  assert(varsF != NULL);
3487  assert(varsR != NULL);
3488  assert(nvarsM1 >= 0 && nvarsM1 <= nvars - ntightened);
3489  assert(nvarsM2 >= 0 && nvarsM2 <= nvars - ntightened);
3490  assert(nvarsF >= 0 && nvarsF <= nvars - ntightened);
3491  assert(nvarsR >= 0 && nvarsR <= nvars - ntightened);
3492  assert(nvarsM1 + nvarsM2 + nvarsF + nvarsR == nvars - ntightened);
3493  assert(alpha0 >= 0);
3494  assert(liftcoefs != NULL);
3495  assert(cutact != NULL);
3496  assert(liftrhs != NULL);
3497 
3498  /* allocates temporary memory */
3499  minweightssize = nvarsM1 + 1;
3500  SCIP_CALL( SCIPallocBufferArray(scip, &minweights, minweightssize) );
3501  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, nvarsM1) );
3502 
3503  /* initializes data structures */
3504  BMSclearMemoryArray(liftcoefs, nvars);
3505  *cutact = 0.0;
3506 
3507  /* sets lifting coefficient of variables in M1, sorts variables in M1 such that a_1 <= a_2 <= ... <= a_|M1|
3508  * and calculates activity of the current valid inequality
3509  */
3510  for( j = 0; j < nvarsM1; j++ )
3511  {
3512  assert(liftcoefs[varsM1[j]] == 0);
3513  liftcoefs[varsM1[j]] = 1;
3514  sortkeys[j] = (SCIP_Real) (weights[varsM1[j]]);
3515  (*cutact) += solvals[varsM1[j]];
3516  }
3517 
3518  SCIPsortRealInt(sortkeys, varsM1, nvarsM1);
3519 
3520  /* initializes (i = 1) the minweight table, defined as: minweights_i[w] =
3521  * min sum_{j in M_1} a_j x_j + sum_{k=1}^{i-1} a_{j_k} x_{j_k}
3522  * s.t. sum_{j in M_1} x_j + sum_{k=1}^{i-1} alpha_{j_k} x_{j_k} >= w
3523  * x_j in {0,1} for j in M_1 & {j_i,...,j_i-1},
3524  * for i = 1,...,t with t = |N\M1| and w = 0,...,|M1| + sum_{k=1}^{i-1} alpha_{j_k};
3525  */
3526  minweights[0] = 0;
3527  for( w = 1; w <= nvarsM1; w++ )
3528  minweights[w] = minweights[w-1] + weights[varsM1[w-1]];
3529  minweightslen = nvarsM1 + 1;
3530 
3531  /* gets sum of weights of variables fixed to one, i.e. sum of weights of variables in M_2 */
3532  fixedonesweight = 0;
3533  for( j = 0; j < nvarsM2; j++ )
3534  fixedonesweight += weights[varsM2[j]];
3535  assert(fixedonesweight >= 0);
3536 
3537  /* initializes right hand side of lifted valid inequality */
3538  *liftrhs = alpha0;
3539 
3540  /* sequentially up-lifts all variables in F: */
3541  for( j = 0; j < nvarsF; j++ )
3542  {
3543  SCIP_Longint weight;
3544  int liftvar;
3545  int liftcoef;
3546  int z;
3547 
3548  liftvar = varsF[j];
3549  weight = weights[liftvar];
3550  assert(liftvar >= 0 && liftvar < nvars);
3551  assert(SCIPisFeasGT(scip, solvals[liftvar], 0.0));
3552  assert(weight > 0);
3553 
3554  /* knapsack problem is infeasible:
3555  * sets z = 0
3556  */
3557  if( capacity - fixedonesweight - weight < 0 )
3558  {
3559  z = 0;
3560  }
3561  /* knapsack problem is feasible:
3562  * sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i} } = liftrhs,
3563  * if minweights_i[liftrhs] <= a_0 - fixedonesweight - a_{j_i}
3564  */
3565  else if( minweights[*liftrhs] <= capacity - fixedonesweight - weight )
3566  {
3567  z = *liftrhs;
3568  }
3569  /* knapsack problem is feasible:
3570  * uses binary search to find z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i} }
3571  */
3572  else
3573  {
3574  int left;
3575  int right;
3576  int middle;
3577 
3578  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - fixedonesweight - weight);
3579  left = 0;
3580  right = (*liftrhs) + 1;
3581  while( left < right - 1 )
3582  {
3583  middle = (left + right) / 2;
3584  assert(0 <= middle && middle < minweightslen);
3585  if( minweights[middle] <= capacity - fixedonesweight - weight )
3586  left = middle;
3587  else
3588  right = middle;
3589  }
3590  assert(left == right - 1);
3591  assert(0 <= left && left < minweightslen);
3592  assert(minweights[left] <= capacity - fixedonesweight - weight );
3593  assert(left == minweightslen - 1 || minweights[left+1] > capacity - fixedonesweight - weight);
3594 
3595  /* now z = left */
3596  z = left;
3597  assert(z <= *liftrhs);
3598  }
3599 
3600  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
3601  liftcoef = (*liftrhs) - z;
3602  liftcoefs[liftvar] = liftcoef;
3603  assert(liftcoef >= 0 && liftcoef <= (*liftrhs) + 1);
3604 
3605  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
3606  if( liftcoef == 0 )
3607  continue;
3608 
3609  /* updates activity of current valid inequality */
3610  (*cutact) += liftcoef * solvals[liftvar];
3611 
3612  /* enlarges current minweight table:
3613  * from minweightlen = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 entries
3614  * to |M1| + sum_{k=1}^{i } alpha_{j_k} + 1 entries
3615  * and sets minweights_i[w] = infinity for
3616  * w = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 , ... , |M1| + sum_{k=1}^{i} alpha_{j_k}
3617  */
3618  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + liftcoef) );
3619 
3620  /* updates minweight table: minweight_i+1[w] =
3621  * min{ minweights_i[w], a_{j_i}}, if w < alpha_j_i
3622  * min{ minweights_i[w], minweights_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
3623  */
3624  for( w = minweightslen - 1; w >= 0; w-- )
3625  {
3626  SCIP_Longint min;
3627  if( w < liftcoef )
3628  {
3629  min = MIN(minweights[w], weight);
3630  minweights[w] = min;
3631  }
3632  else
3633  {
3634  assert(w >= liftcoef);
3635  min = MIN(minweights[w], minweights[w - liftcoef] + weight);
3636  minweights[w] = min;
3637  }
3638  }
3639  }
3640  assert(minweights[0] == 0);
3641 
3642  /* sequentially down-lifts all variables in M_2: */
3643  for( j = 0; j < nvarsM2; j++ )
3644  {
3645  SCIP_Longint weight;
3646  int liftvar;
3647  int liftcoef;
3648  int left;
3649  int right;
3650  int middle;
3651  int z;
3652 
3653  liftvar = varsM2[j];
3654  weight = weights[liftvar];
3655  assert(SCIPisFeasEQ(scip, solvals[liftvar], 1.0));
3656  assert(liftvar >= 0 && liftvar < nvars);
3657  assert(weight > 0);
3658 
3659  /* uses binary search to find
3660  * z = max { w : 0 <= w <= |M_1| + sum_{k=1}^{i-1} alpha_{j_k}, minweights_[w] <= a_0 - fixedonesweight + a_{j_i}}
3661  */
3662  left = 0;
3663  right = minweightslen;
3664  while( left < right - 1 )
3665  {
3666  middle = (left + right) / 2;
3667  assert(0 <= middle && middle < minweightslen);
3668  if( minweights[middle] <= capacity - fixedonesweight + weight )
3669  left = middle;
3670  else
3671  right = middle;
3672  }
3673  assert(left == right - 1);
3674  assert(0 <= left && left < minweightslen);
3675  assert(minweights[left] <= capacity - fixedonesweight + weight );
3676  assert(left == minweightslen - 1 || minweights[left+1] > capacity - fixedonesweight + weight);
3677 
3678  /* now z = left */
3679  z = left;
3680  assert(z >= *liftrhs);
3681 
3682  /* calculates lifting coefficients alpha_{j_i} = z - liftrhs */
3683  liftcoef = z - (*liftrhs);
3684  liftcoefs[liftvar] = liftcoef;
3685  assert(liftcoef >= 0);
3686 
3687  /* updates sum of weights of variables fixed to one */
3688  fixedonesweight -= weight;
3689 
3690  /* updates right-hand side of current valid inequality */
3691  (*liftrhs) += liftcoef;
3692  assert(*liftrhs >= alpha0);
3693 
3694  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
3695  if( liftcoef == 0 )
3696  continue;
3697 
3698  /* updates activity of current valid inequality */
3699  (*cutact) += liftcoef * solvals[liftvar];
3700 
3701  /* enlarges current minweight table:
3702  * from minweightlen = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 entries
3703  * to |M1| + sum_{k=1}^{i } alpha_{j_k} + 1 entries
3704  * and sets minweights_i[w] = infinity for
3705  * w = |M1| + sum_{k=1}^{i-1} alpha_{j_k} + 1 , ... , |M1| + sum_{k=1}^{i} alpha_{j_k}
3706  */
3707  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + liftcoef) );
3708 
3709  /* updates minweight table: minweight_i+1[w] =
3710  * min{ minweights_i[w], a_{j_i}}, if w < alpha_j_i
3711  * min{ minweights_i[w], minweights_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
3712  */
3713  for( w = minweightslen - 1; w >= 0; w-- )
3714  {
3715  SCIP_Longint min;
3716  if( w < liftcoef )
3717  {
3718  min = MIN(minweights[w], weight);
3719  minweights[w] = min;
3720  }
3721  else
3722  {
3723  assert(w >= liftcoef);
3724  min = MIN(minweights[w], minweights[w - liftcoef] + weight);
3725  minweights[w] = min;
3726  }
3727  }
3728  }
3729  assert(fixedonesweight == 0);
3730  assert(*liftrhs >= alpha0);
3731 
3732  /* sequentially up-lifts all variables in R: */
3733  for( j = 0; j < nvarsR; j++ )
3734  {
3735  SCIP_Longint weight;
3736  int liftvar;
3737  int liftcoef;
3738  int z;
3739 
3740  liftvar = varsR[j];
3741  weight = weights[liftvar];
3742  assert(liftvar >= 0 && liftvar < nvars);
3743  assert(SCIPisFeasEQ(scip, solvals[liftvar], 0.0));
3744  assert(weight > 0);
3745  assert(capacity - weight >= 0);
3746  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - weight);
3747 
3748  /* sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} } = liftrhs,
3749  * if minweights_i[liftrhs] <= a_0 - a_{j_i}
3750  */
3751  if( minweights[*liftrhs] <= capacity - weight )
3752  {
3753  z = *liftrhs;
3754  }
3755  /* uses binary search to find z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} }
3756  */
3757  else
3758  {
3759  int left;
3760  int right;
3761  int middle;
3762 
3763  left = 0;
3764  right = (*liftrhs) + 1;
3765  while( left < right - 1)
3766  {
3767  middle = (left + right) / 2;
3768  assert(0 <= middle && middle < minweightslen);
3769  if( minweights[middle] <= capacity - weight )
3770  left = middle;
3771  else
3772  right = middle;
3773  }
3774  assert(left == right - 1);
3775  assert(0 <= left && left < minweightslen);
3776  assert(minweights[left] <= capacity - weight );
3777  assert(left == minweightslen - 1 || minweights[left+1] > capacity - weight);
3778 
3779  /* now z = left */
3780  z = left;
3781  assert(z <= *liftrhs);
3782  }
3783 
3784  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
3785  liftcoef = (*liftrhs) - z;
3786  liftcoefs[liftvar] = liftcoef;
3787  assert(liftcoef >= 0 && liftcoef <= *liftrhs);
3788 
3789  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
3790  if( liftcoef == 0 )
3791  continue;
3792 
3793  /* updates activity of current valid inequality */
3794  (*cutact) += liftcoef * solvals[liftvar];
3795 
3796  /* updates minweight table: minweight_i+1[w] =
3797  * min{ minweight_i[w], a_{j_i}}, if w < alpha_j_i
3798  * min{ minweight_i[w], minweight_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
3799  */
3800  for( w = *liftrhs; w >= 0; w-- )
3801  {
3802  SCIP_Longint min;
3803  if( w < liftcoef )
3804  {
3805  min = MIN(minweights[w], weight);
3806  minweights[w] = min;
3807  }
3808  else
3809  {
3810  assert(w >= liftcoef);
3811  min = MIN(minweights[w], minweights[w - liftcoef] + weight);
3812  minweights[w] = min;
3813  }
3814  }
3815  }
3816 
3817  /* frees temporary memory */
3818  SCIPfreeBufferArray(scip, &sortkeys);
3819  SCIPfreeBufferArray(scip, &minweights);
3820 
3821  return SCIP_OKAY;
3822 }
3823 
3824 /** adds two minweight values in a safe way, i.e,, ensures no overflow */
3825 static
3827  SCIP_Longint val1, /**< first value to add */
3828  SCIP_Longint val2 /**< second value to add */
3829  )
3830 {
3831  assert(val1 >= 0);
3832  assert(val2 >= 0);
3833 
3834  if( val1 >= SCIP_LONGINT_MAX || val2 >= SCIP_LONGINT_MAX )
3835  return SCIP_LONGINT_MAX;
3836  else
3837  {
3838  assert(val1 <= SCIP_LONGINT_MAX - val2);
3839  return (val1 + val2);
3840  }
3841 }
3842 
3843 /** computes minweights table for lifting with GUBs by combining unfished and fished tables */
3844 static
3846  SCIP_Longint* minweights, /**< minweight table to compute */
3847  SCIP_Longint* finished, /**< given finished table */
3848  SCIP_Longint* unfinished, /**< given unfinished table */
3849  int minweightslen /**< length of minweight, finished, and unfinished tables */
3850  )
3851 {
3852  int w1;
3853  int w2;
3854 
3855  /* minweights_i[w] = min{finished_i[w1] + unfinished_i[w2] : w1>=0, w2>=0, w1+w2=w};
3856  * note that finished and unfished arrays sorted by non-decreasing weight
3857  */
3858 
3859  /* initialize minweight with w2 = 0 */
3860  w2 = 0;
3861  assert(unfinished[w2] == 0);
3862  for( w1 = 0; w1 < minweightslen; w1++ )
3863  minweights[w1] = finished[w1];
3864 
3865  /* consider w2 = 1, ..., minweightslen-1 */
3866  for( w2 = 1; w2 < minweightslen; w2++ )
3867  {
3868  if( unfinished[w2] >= SCIP_LONGINT_MAX )
3869  break;
3870 
3871  for( w1 = 0; w1 < minweightslen - w2; w1++ )
3872  {
3873  SCIP_Longint temp;
3874 
3875  temp = safeAddMinweightsGUB(finished[w1], unfinished[w2]);
3876  if( temp <= minweights[w1+w2] )
3877  minweights[w1+w2] = temp;
3878  }
3879  }
3880 }
3881 
3882 /** lifts given inequality
3883  * sum_{j in C_1} x_j <= alpha_0
3884  * valid for
3885  * 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;
3886  * sum_{j in Q_i} x_j <= 1, forall i in I }
3887  * to a valid inequality
3888  * 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
3889  * <= alpha_0 + sum_{j in C_2} alpha_j
3890  * for
3891  * 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 };
3892  * uses sequential up-lifting for the variables in GUB constraints in gubconsGFC1,
3893  * sequential down-lifting for the variables in GUB constraints in gubconsGC2, and
3894  * sequential up-lifting for the variabels in GUB constraints in gubconsGR.
3895  */
3896 static
3898  SCIP* scip, /**< SCIP data structure */
3899  SCIP_GUBSET* gubset, /**< GUB set data structure */
3900  SCIP_VAR** vars, /**< variables in knapsack constraint */
3901  int ngubconscapexceed, /**< number of GUBs with only capacity exceeding variables */
3902  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
3903  SCIP_Longint capacity, /**< capacity of knapsack */
3904  SCIP_Real* solvals, /**< solution values of all knapsack variables */
3905  int* gubconsGC1, /**< GUBs in GC1(GNC1+GOC1) */
3906  int* gubconsGC2, /**< GUBs in GC2 */
3907  int* gubconsGFC1, /**< GUBs in GFC1(GNC1+GF) */
3908  int* gubconsGR, /**< GUBs in GR */
3909  int ngubconsGC1, /**< number of GUBs in GC1(GNC1+GOC1) */
3910  int ngubconsGC2, /**< number of GUBs in GC2 */
3911  int ngubconsGFC1, /**< number of GUBs in GFC1(GNC1+GF) */
3912  int ngubconsGR, /**< number of GUBs in GR */
3913  int alpha0, /**< rights hand side of given valid inequality */
3914  int* liftcoefs, /**< pointer to store lifting coefficient of vars in knapsack constraint */
3915  SCIP_Real* cutact, /**< pointer to store activity of lifted valid inequality */
3916  int* liftrhs, /**< pointer to store right hand side of the lifted valid inequality */
3917  int maxgubvarssize /**< maximal size of GUB constraints */
3918  )
3919 {
3920  SCIP_Longint* minweights;
3921  SCIP_Longint* finished;
3922  SCIP_Longint* unfinished;
3923  int* gubconsGOC1;
3924  int* gubconsGNC1;
3925  int* liftgubvars;
3926  SCIP_Longint fixedonesweight;
3927  SCIP_Longint weight;
3928  SCIP_Longint weightdiff1;
3929  SCIP_Longint weightdiff2;
3930  SCIP_Longint min;
3931  int minweightssize;
3932  int minweightslen;
3933  int nvars;
3934  int varidx;
3935  int liftgubconsidx;
3936  int liftvar;
3937  int sumliftcoef;
3938  int liftcoef;
3939  int ngubconsGOC1;
3940  int ngubconsGNC1;
3941  int left;
3942  int right;
3943  int middle;
3944  int nliftgubvars;
3945  int tmplen;
3946  int tmpsize;
3947  int j;
3948  int k;
3949  int w;
3950  int z;
3951 #ifndef NDEBUG
3952  int ngubconss;
3953  int nliftgubC1;
3954 
3955  ngubconss = gubset->ngubconss;
3956 #endif
3957  nvars = gubset->nvars;
3958 
3959  assert(scip != NULL);
3960  assert(gubset != NULL);
3961  assert(vars != NULL);
3962  assert(nvars >= 0);
3963  assert(weights != NULL);
3964  assert(capacity >= 0);
3965  assert(solvals != NULL);
3966  assert(gubconsGC1 != NULL);
3967  assert(gubconsGC2 != NULL);
3968  assert(gubconsGFC1 != NULL);
3969  assert(gubconsGR != NULL);
3970  assert(ngubconsGC1 >= 0 && ngubconsGC1 <= ngubconss - ngubconscapexceed);
3971  assert(ngubconsGC2 >= 0 && ngubconsGC2 <= ngubconss - ngubconscapexceed);
3972  assert(ngubconsGFC1 >= 0 && ngubconsGFC1 <= ngubconss - ngubconscapexceed);
3973  assert(ngubconsGR >= 0 && ngubconsGR <= ngubconss - ngubconscapexceed);
3974  assert(alpha0 >= 0);
3975  assert(liftcoefs != NULL);
3976  assert(cutact != NULL);
3977  assert(liftrhs != NULL);
3978 
3979  minweightssize = ngubconsGC1+1;
3980 
3981  /* allocates temporary memory */
3982  SCIP_CALL( SCIPallocBufferArray(scip, &liftgubvars, maxgubvarssize) );
3983  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGOC1, ngubconsGC1) );
3984  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGNC1, ngubconsGC1) );
3985  SCIP_CALL( SCIPallocBufferArray(scip, &minweights, minweightssize) );
3986  SCIP_CALL( SCIPallocBufferArray(scip, &finished, minweightssize) );
3987  SCIP_CALL( SCIPallocBufferArray(scip, &unfinished, minweightssize) );
3988 
3989  /* initializes data structures */
3990  BMSclearMemoryArray(liftcoefs, nvars);
3991  *cutact = 0.0;
3992 
3993  /* gets GOC1 and GNC1 GUBs, sets lifting coefficient of variables in C1 and calculates activity of the current
3994  * valid inequality
3995  */
3996  ngubconsGOC1 = 0;
3997  ngubconsGNC1 = 0;
3998  for( j = 0; j < ngubconsGC1; j++ )
3999  {
4000  if( gubset->gubconsstatus[gubconsGC1[j]] == GUBCONSSTATUS_BELONGSTOSET_GOC1 )
4001  {
4002  gubconsGOC1[ngubconsGOC1] = gubconsGC1[j];
4003  ngubconsGOC1++;
4004  }
4005  else
4006  {
4007  assert(gubset->gubconsstatus[gubconsGC1[j]] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
4008  gubconsGNC1[ngubconsGNC1] = gubconsGC1[j];
4009  ngubconsGNC1++;
4010  }
4011  for( k = 0; k < gubset->gubconss[gubconsGC1[j]]->ngubvars
4012  && gubset->gubconss[gubconsGC1[j]]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4013  {
4014  varidx = gubset->gubconss[gubconsGC1[j]]->gubvars[k];
4015  assert(varidx >= 0 && varidx < nvars);
4016  assert(liftcoefs[varidx] == 0);
4017 
4018  liftcoefs[varidx] = 1;
4019  (*cutact) += solvals[varidx];
4020  }
4021  assert(k >= 1);
4022  }
4023  assert(ngubconsGOC1 + ngubconsGFC1 + ngubconsGC2 + ngubconsGR == ngubconss - ngubconscapexceed);
4024  assert(ngubconsGOC1 + ngubconsGNC1 == ngubconsGC1);
4025 
4026  /* initialize the minweight tables, defined as: for i = 1,...,m with m = |I| and w = 0,...,|gubconsGC1|;
4027  * - finished_i[w] =
4028  * min sum_{k = 1,2,...,i-1} sum_{j in Q_k} a_j x_j
4029  * s.t. sum_{k = 1,2,...,i-1} sum_{j in Q_k} alpha_j x_j >= w
4030  * sum_{j in Q_k} x_j <= 1
4031  * x_j in {0,1} forall j in Q_k forall k = 1,2,...,i-1,
4032  * - unfinished_i[w] =
4033  * min sum_{k = i+1,...,m} sum_{j in Q_k && j in C1} a_j x_j
4034  * s.t. sum_{k = i+1,...,m} sum_{j in Q_k && j in C1} x_j >= w
4035  * sum_{j in Q_k} x_j <= 1
4036  * x_j in {0,1} forall j in Q_k forall k = 1,2,...,i-1,
4037  * - minweights_i[w] = min{finished_i[w1] + unfinished_i[w2] : w1>=0, w2>=0, w1+w2=w};
4038  */
4039 
4040  /* initialize finished table; note that variables in GOC1 GUBs (includes C1 and capacity exceeding variables)
4041  * are sorted s.t. C1 variables come first and are sorted by non-decreasing weight.
4042  * GUBs in the group GCI are sorted by non-decreasing min{ a_k : k in GC1_j } where min{ a_k : k in GC1_j } always
4043  * comes from the first variable in the GUB
4044  */
4045  assert(ngubconsGOC1 <= ngubconsGC1);
4046  finished[0] = 0;
4047  for( w = 1; w <= ngubconsGOC1; w++ )
4048  {
4049  liftgubconsidx = gubconsGOC1[w-1];
4050 
4051  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GOC1);
4052  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4053 
4054  varidx = gubset->gubconss[liftgubconsidx]->gubvars[0];
4055 
4056  assert(varidx >= 0 && varidx < nvars);
4057  assert(liftcoefs[varidx] == 1);
4058 
4059  min = weights[varidx];
4060  finished[w] = finished[w-1] + min;
4061 
4062 #ifndef NDEBUG
4063  for( k = 1; k < gubset->gubconss[liftgubconsidx]->ngubvars
4064  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4065  {
4066  varidx = gubset->gubconss[liftgubconsidx]->gubvars[k];
4067  assert(varidx >= 0 && varidx < nvars);
4068  assert(liftcoefs[varidx] == 1);
4069  assert(weights[varidx] >= min);
4070  }
4071 #endif
4072  }
4073  for( w = ngubconsGOC1+1; w <= ngubconsGC1; w++ )
4074  finished[w] = SCIP_LONGINT_MAX;
4075 
4076  /* initialize unfinished table; note that variables in GNC1 GUBs
4077  * are sorted s.t. C1 variables come first and are sorted by non-decreasing weight.
4078  * GUBs in the group GCI are sorted by non-decreasing min{ a_k : k in GC1_j } where min{ a_k : k in GC1_j } always
4079  * comes from the first variable in the GUB
4080  */
4081  assert(ngubconsGNC1 <= ngubconsGC1);
4082  unfinished[0] = 0;
4083  for( w = 1; w <= ngubconsGNC1; w++ )
4084  {
4085  liftgubconsidx = gubconsGNC1[w-1];
4086 
4087  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
4088  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4089 
4090  varidx = gubset->gubconss[liftgubconsidx]->gubvars[0];
4091 
4092  assert(varidx >= 0 && varidx < nvars);
4093  assert(liftcoefs[varidx] == 1);
4094 
4095  min = weights[varidx];
4096  unfinished[w] = unfinished[w-1] + min;
4097 
4098 #ifndef NDEBUG
4099  for( k = 1; k < gubset->gubconss[liftgubconsidx]->ngubvars
4100  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4101  {
4102  varidx = gubset->gubconss[liftgubconsidx]->gubvars[k];
4103  assert(varidx >= 0 && varidx < nvars);
4104  assert(liftcoefs[varidx] == 1);
4105  assert(weights[varidx] >= min );
4106  }
4107 #endif
4108  }
4109  for( w = ngubconsGNC1 + 1; w <= ngubconsGC1; w++ )
4110  unfinished[w] = SCIP_LONGINT_MAX;
4111 
4112  /* initialize minweights table; note that variables in GC1 GUBs
4113  * are sorted s.t. C1 variables come first and are sorted by non-decreasing weight.
4114  * we can directly initialize minweights instead of computing it from finished and unfinished (which would be more time
4115  * consuming) because is it has to be build using weights from C1 only.
4116  */
4117  assert(ngubconsGOC1 + ngubconsGNC1 == ngubconsGC1);
4118  minweights[0] = 0;
4119  for( w = 1; w <= ngubconsGC1; w++ )
4120  {
4121  liftgubconsidx = gubconsGC1[w-1];
4122 
4123  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GOC1
4124  || gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
4125  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4126 
4127  varidx = gubset->gubconss[liftgubconsidx]->gubvars[0];
4128 
4129  assert(varidx >= 0 && varidx < nvars);
4130  assert(liftcoefs[varidx] == 1);
4131 
4132  min = weights[varidx];
4133  minweights[w] = minweights[w-1] + min;
4134 
4135 #ifndef NDEBUG
4136  for( k = 1; k < gubset->gubconss[liftgubconsidx]->ngubvars
4137  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4138  {
4139  varidx = gubset->gubconss[liftgubconsidx]->gubvars[k];
4140  assert(varidx >= 0 && varidx < nvars);
4141  assert(liftcoefs[varidx] == 1);
4142  assert(weights[varidx] >= min);
4143  }
4144 #endif
4145  }
4146  minweightslen = ngubconsGC1 + 1;
4147 
4148  /* gets sum of weights of variables fixed to one, i.e. sum of weights of C2 variables GC2 GUBs */
4149  fixedonesweight = 0;
4150  for( j = 0; j < ngubconsGC2; j++ )
4151  {
4152  varidx = gubset->gubconss[gubconsGC2[j]]->gubvars[0];
4153 
4154  assert(gubset->gubconss[gubconsGC2[j]]->ngubvars == 1);
4155  assert(varidx >= 0 && varidx < nvars);
4156  assert(gubset->gubconss[gubconsGC2[j]]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C2);
4157 
4158  fixedonesweight += weights[varidx];
4159  }
4160  assert(fixedonesweight >= 0);
4161 
4162  /* initializes right hand side of lifted valid inequality */
4163  *liftrhs = alpha0;
4164 
4165  /* sequentially up-lifts all variables in GFC1 GUBs */
4166  for( j = 0; j < ngubconsGFC1; j++ )
4167  {
4168  liftgubconsidx = gubconsGFC1[j];
4169  assert(liftgubconsidx >= 0 && liftgubconsidx < ngubconss);
4170 
4171  /* GNC1 GUB: update unfinished table (remove current GUB, i.e., remove min weight of C1 vars in GUB) and
4172  * compute minweight table via updated unfinished table and aleady upto date finished table;
4173  */
4174  k = 0;
4175  if( gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1 )
4176  {
4177  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1);
4178  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C1);
4179  assert(ngubconsGNC1 > 0);
4180 
4181  /* get number of C1 variables of current GNC1 GUB and put them into array of variables in GUB that
4182  * are considered for the lifting, i.e., not capacity exceeding
4183  */
4184  for( ; k < gubset->gubconss[liftgubconsidx]->ngubvars
4185  && gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_C1; k++ )
4186  liftgubvars[k] = gubset->gubconss[liftgubconsidx]->gubvars[k];
4187  assert(k >= 1);
4188 
4189  /* update unfinished table by removing current GNC1 GUB, i.e, remove C1 variable with minimal weight
4190  * unfinished[w] = MAX{unfinished[w], unfinished[w+1] - weight}, "weight" is the minimal weight of current GUB
4191  */
4192  weight = weights[liftgubvars[0]];
4193 
4194  weightdiff2 = unfinished[ngubconsGNC1] - weight;
4195  unfinished[ngubconsGNC1] = SCIP_LONGINT_MAX;
4196  for( w = ngubconsGNC1-1; w >= 1; w-- )
4197  {
4198  weightdiff1 = weightdiff2;
4199  weightdiff2 = unfinished[w] - weight;
4200 
4201  if( unfinished[w] < weightdiff1 )
4202  unfinished[w] = weightdiff1;
4203  else
4204  break;
4205  }
4206  ngubconsGNC1--;
4207 
4208  /* computes minweights table by combining unfished and fished tables */
4209  computeMinweightsGUB(minweights, finished, unfinished, minweightslen);
4210  assert(minweights[0] == 0);
4211  }
4212  /* GF GUB: no update of unfinished table (and minweight table) required because GF GUBs have no C1 variables and
4213  * are therefore not in the unfinished table
4214  */
4215  else
4216  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF);
4217 
4218 #ifndef NDEBUG
4219  nliftgubC1 = k;
4220 #endif
4221  nliftgubvars = k;
4222  sumliftcoef = 0;
4223 
4224  /* compute lifting coefficient of F and R variables in GNC1 and GF GUBs (C1 vars have already liftcoef 1) */
4225  for( ; k < gubset->gubconss[liftgubconsidx]->ngubvars; k++ )
4226  {
4227  if( gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_F
4228  || gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_R )
4229  {
4230  liftvar = gubset->gubconss[liftgubconsidx]->gubvars[k];
4231  weight = weights[liftvar];
4232  assert(weight > 0);
4233  assert(liftvar >= 0 && liftvar < nvars);
4234  assert(capacity - weight >= 0);
4235 
4236  /* put variable into array of variables in GUB that are considered for the lifting,
4237  * i.e., not capacity exceeding
4238  */
4239  liftgubvars[nliftgubvars] = liftvar;
4240  nliftgubvars++;
4241 
4242  /* knapsack problem is infeasible:
4243  * sets z = 0
4244  */
4245  if( capacity - fixedonesweight - weight < 0 )
4246  {
4247  z = 0;
4248  }
4249  /* knapsack problem is feasible:
4250  * sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i} } = liftrhs,
4251  * if minweights_i[liftrhs] <= a_0 - fixedonesweight - a_{j_i}
4252  */
4253  else if( minweights[*liftrhs] <= capacity - fixedonesweight - weight )
4254  {
4255  z = *liftrhs;
4256  }
4257  /* knapsack problem is feasible:
4258  * binary search to find z = max {w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - fixedonesweight - a_{j_i}}
4259  */
4260  else
4261  {
4262  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - fixedonesweight - weight);
4263  left = 0;
4264  right = (*liftrhs) + 1;
4265  while( left < right - 1 )
4266  {
4267  middle = (left + right) / 2;
4268  assert(0 <= middle && middle < minweightslen);
4269  if( minweights[middle] <= capacity - fixedonesweight - weight )
4270  left = middle;
4271  else
4272  right = middle;
4273  }
4274  assert(left == right - 1);
4275  assert(0 <= left && left < minweightslen);
4276  assert(minweights[left] <= capacity - fixedonesweight - weight);
4277  assert(left == minweightslen - 1 || minweights[left+1] > capacity - fixedonesweight - weight);
4278 
4279  /* now z = left */
4280  z = left;
4281  assert(z <= *liftrhs);
4282  }
4283 
4284  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
4285  liftcoef = (*liftrhs) - z;
4286  liftcoefs[liftvar] = liftcoef;
4287  assert(liftcoef >= 0 && liftcoef <= (*liftrhs) + 1);
4288 
4289  /* updates activity of current valid inequality */
4290  (*cutact) += liftcoef * solvals[liftvar];
4291 
4292  /* updates sum of all lifting coefficients in GUB */
4293  sumliftcoef += liftcoefs[liftvar];
4294  }
4295  else
4296  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_CAPACITYEXCEEDED);
4297  }
4298  /* at least one variable is in F or R (j = number of C1 variables in current GUB) */
4299  assert(nliftgubvars > nliftgubC1);
4300 
4301  /* activity of current valid inequality will not change if (sum of alpha_{j_i} in GUB) = 0
4302  * and finished and minweight table can be updated easily as only C1 variables need to be considered;
4303  * not needed for GF GUBs
4304  */
4305  if( sumliftcoef == 0 )
4306  {
4307  if( gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GNC1 )
4308  {
4309  weight = weights[liftgubvars[0]];
4310  /* update finished table and minweights table by applying special case of
4311  * finished[w] = MIN{finished[w], finished[w-1] + weight}, "weight" is the minimal weight of current GUB
4312  * minweights[w] = MIN{minweights[w], minweights[w-1] + weight}, "weight" is the minimal weight of current GUB
4313  */
4314  for( w = minweightslen-1; w >= 1; w-- )
4315  {
4316  SCIP_Longint tmpval;
4317 
4318  tmpval = safeAddMinweightsGUB(finished[w-1], weight);
4319  finished[w] = MIN(finished[w], tmpval);
4320 
4321  tmpval = safeAddMinweightsGUB(minweights[w-1], weight);
4322  minweights[w] = MIN(minweights[w], tmpval);
4323  }
4324  }
4325  else
4326  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GF);
4327 
4328  continue;
4329  }
4330 
4331  /* enlarges current minweights tables(finished, unfinished, minweights):
4332  * from minweightlen = |gubconsGC1| + sum_{k=1,2,...,i-1}sum_{j in Q_k} alpha_j + 1 entries
4333  * to |gubconsGC1| + sum_{k=1,2,...,i }sum_{j in Q_k} alpha_j + 1 entries
4334  * and sets minweights_i[w] = infinity for
4335  * w = |gubconsGC1| + sum_{k=1,2,..,i-1}sum_{j in Q_k} alpha_j+1,..,|C1| + sum_{k=1,2,..,i}sum_{j in Q_k} alpha_j
4336  */
4337  tmplen = minweightslen; /* will be updated in enlargeMinweights() */
4338  tmpsize = minweightssize;
4339  SCIP_CALL( enlargeMinweights(scip, &unfinished, &tmplen, &tmpsize, tmplen + sumliftcoef) );
4340  tmplen = minweightslen;
4341  tmpsize = minweightssize;
4342  SCIP_CALL( enlargeMinweights(scip, &finished, &tmplen, &tmpsize, tmplen + sumliftcoef) );
4343  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + sumliftcoef) );
4344 
4345  /* update finished table and minweight table;
4346  * note that instead of computing minweight table from updated finished and updated unfinished table again
4347  * (for the lifting coefficient, we had to update unfinished table and compute minweight table), we here
4348  * only need to update the minweight table and the updated finished in the same way (i.e., computing for minweight
4349  * not needed because only finished table changed at this point and the change was "adding" one weight)
4350  *
4351  * update formular for minweight table is: minweight_i+1[w] =
4352  * min{ minweights_i[w], min{ minweights_i[w - alpha_k]^{+} + a_k : k in GUB_j_i } }
4353  * formular for finished table has the same pattern.
4354  */
4355  for( w = minweightslen-1; w >= 0; w-- )
4356  {
4357  SCIP_Longint minminweight;
4358  SCIP_Longint minfinished;
4359 
4360  for( k = 0; k < nliftgubvars; k++ )
4361  {
4362  liftcoef = liftcoefs[liftgubvars[k]];
4363  weight = weights[liftgubvars[k]];
4364 
4365  if( w < liftcoef )
4366  {
4367  minfinished = MIN(finished[w], weight);
4368  minminweight = MIN(minweights[w], weight);
4369 
4370  finished[w] = minfinished;
4371  minweights[w] = minminweight;
4372  }
4373  else
4374  {
4375  SCIP_Longint tmpval;
4376 
4377  assert(w >= liftcoef);
4378 
4379  tmpval = safeAddMinweightsGUB(finished[w-liftcoef], weight);
4380  minfinished = MIN(finished[w], tmpval);
4381 
4382  tmpval = safeAddMinweightsGUB(minweights[w-liftcoef], weight);
4383  minminweight = MIN(minweights[w], tmpval);
4384 
4385  finished[w] = minfinished;
4386  minweights[w] = minminweight;
4387  }
4388  }
4389  }
4390  assert(minweights[0] == 0);
4391  }
4392  assert(ngubconsGNC1 == 0);
4393 
4394  /* note: now the unfinished table no longer exists, i.e., it is "0, MAX, MAX, ..." and minweight equals to finished;
4395  * therefore, only work with minweight table from here on
4396  */
4397 
4398  /* sequentially down-lifts C2 variables contained in trivial GC2 GUBs */
4399  for( j = 0; j < ngubconsGC2; j++ )
4400  {
4401  liftgubconsidx = gubconsGC2[j];
4402 
4403  assert(liftgubconsidx >=0 && liftgubconsidx < ngubconss);
4404  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GC2);
4405  assert(gubset->gubconss[liftgubconsidx]->ngubvars == 1);
4406  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[0] == GUBVARSTATUS_BELONGSTOSET_C2);
4407 
4408  liftvar = gubset->gubconss[liftgubconsidx]->gubvars[0]; /* C2 GUBs contain only one variable */
4409  weight = weights[liftvar];
4410 
4411  assert(liftvar >= 0 && liftvar < nvars);
4412  assert(SCIPisFeasEQ(scip, solvals[liftvar], 1.0));
4413  assert(weight > 0);
4414 
4415  /* uses binary search to find
4416  * z = max { w : 0 <= w <= |C_1| + sum_{k=1}^{i-1} alpha_{j_k}, minweights_[w] <= a_0 - fixedonesweight + a_{j_i}}
4417  */
4418  left = 0;
4419  right = minweightslen;
4420  while( left < right - 1 )
4421  {
4422  middle = (left + right) / 2;
4423  assert(0 <= middle && middle < minweightslen);
4424  if( minweights[middle] <= capacity - fixedonesweight + weight )
4425  left = middle;
4426  else
4427  right = middle;
4428  }
4429  assert(left == right - 1);
4430  assert(0 <= left && left < minweightslen);
4431  assert(minweights[left] <= capacity - fixedonesweight + weight);
4432  assert(left == minweightslen - 1 || minweights[left + 1] > capacity - fixedonesweight + weight);
4433 
4434  /* now z = left */
4435  z = left;
4436  assert(z >= *liftrhs);
4437 
4438  /* calculates lifting coefficients alpha_{j_i} = z - liftrhs */
4439  liftcoef = z - (*liftrhs);
4440  liftcoefs[liftvar] = liftcoef;
4441  assert(liftcoef >= 0);
4442 
4443  /* updates sum of weights of variables fixed to one */
4444  fixedonesweight -= weight;
4445 
4446  /* updates right-hand side of current valid inequality */
4447  (*liftrhs) += liftcoef;
4448  assert(*liftrhs >= alpha0);
4449 
4450  /* minweight table and activity of current valid inequality will not change, if alpha_{j_i} = 0 */
4451  if( liftcoef == 0 )
4452  continue;
4453 
4454  /* updates activity of current valid inequality */
4455  (*cutact) += liftcoef * solvals[liftvar];
4456 
4457  /* enlarges current minweight table:
4458  * from minweightlen = |gubconsGC1| + sum_{k=1,2,...,i-1}sum_{j in Q_k} alpha_j + 1 entries
4459  * to |gubconsGC1| + sum_{k=1,2,...,i }sum_{j in Q_k} alpha_j + 1 entries
4460  * and sets minweights_i[w] = infinity for
4461  * w = |C1| + sum_{k=1,2,...,i-1}sum_{j in Q_k} alpha_j + 1 , ... , |C1| + sum_{k=1,2,...,i}sum_{j in Q_k} alpha_j
4462  */
4463  SCIP_CALL( enlargeMinweights(scip, &minweights, &minweightslen, &minweightssize, minweightslen + liftcoef) );
4464 
4465  /* updates minweight table: minweight_i+1[w] =
4466  * min{ minweights_i[w], a_{j_i}}, if w < alpha_j_i
4467  * min{ minweights_i[w], minweights_i[w - alpha_j_i] + a_j_i}, if w >= alpha_j_i
4468  */
4469  for( w = minweightslen - 1; w >= 0; w-- )
4470  {
4471  if( w < liftcoef )
4472  {
4473  min = MIN(minweights[w], weight);
4474  minweights[w] = min;
4475  }
4476  else
4477  {
4478  SCIP_Longint tmpval;
4479 
4480  assert(w >= liftcoef);
4481 
4482  tmpval = safeAddMinweightsGUB(minweights[w-liftcoef], weight);
4483  min = MIN(minweights[w], tmpval);
4484  minweights[w] = min;
4485  }
4486  }
4487  }
4488  assert(fixedonesweight == 0);
4489  assert(*liftrhs >= alpha0);
4490 
4491  /* sequentially up-lifts variables in GUB constraints in GR GUBs */
4492  for( j = 0; j < ngubconsGR; j++ )
4493  {
4494  liftgubconsidx = gubconsGR[j];
4495 
4496  assert(liftgubconsidx >=0 && liftgubconsidx < ngubconss);
4497  assert(gubset->gubconsstatus[liftgubconsidx] == GUBCONSSTATUS_BELONGSTOSET_GR);
4498 
4499  sumliftcoef = 0;
4500  nliftgubvars = 0;
4501  for( k = 0; k < gubset->gubconss[liftgubconsidx]->ngubvars; k++ )
4502  {
4503  if(gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_BELONGSTOSET_R )
4504  {
4505  liftvar = gubset->gubconss[liftgubconsidx]->gubvars[k];
4506  weight = weights[liftvar];
4507  assert(weight > 0);
4508  assert(liftvar >= 0 && liftvar < nvars);
4509  assert(capacity - weight >= 0);
4510  assert((*liftrhs) + 1 >= minweightslen || minweights[(*liftrhs) + 1] > capacity - weight);
4511 
4512  /* put variable into array of variables in GUB that are considered for the lifting,
4513  * i.e., not capacity exceeding
4514  */
4515  liftgubvars[nliftgubvars] = liftvar;
4516  nliftgubvars++;
4517 
4518  /* sets z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} } = liftrhs,
4519  * if minweights_i[liftrhs] <= a_0 - a_{j_i}
4520  */
4521  if( minweights[*liftrhs] <= capacity - weight )
4522  {
4523  z = *liftrhs;
4524  }
4525  /* uses binary search to find z = max { w : 0 <= w <= liftrhs, minweights_i[w] <= a_0 - a_{j_i} }
4526  */
4527  else
4528  {
4529  left = 0;
4530  right = (*liftrhs) + 1;
4531  while( left < right - 1 )
4532  {
4533  middle = (left + right) / 2;
4534  assert(0 <= middle && middle < minweightslen);
4535  if( minweights[middle] <= capacity - weight )
4536  left = middle;
4537  else
4538  right = middle;
4539  }
4540  assert(left == right - 1);
4541  assert(0 <= left && left < minweightslen);
4542  assert(minweights[left] <= capacity - weight);
4543  assert(left == minweightslen - 1 || minweights[left + 1] > capacity - weight);
4544 
4545  /* now z = left */
4546  z = left;
4547  assert(z <= *liftrhs);
4548  }
4549  /* calculates lifting coefficients alpha_{j_i} = liftrhs - z */
4550  liftcoef = (*liftrhs) - z;
4551  liftcoefs[liftvar] = liftcoef;
4552  assert(liftcoef >= 0 && liftcoef <= (*liftrhs) + 1);
4553 
4554  /* updates activity of current valid inequality */
4555  (*cutact) += liftcoef * solvals[liftvar];
4556 
4557  /* updates sum of all lifting coefficients in GUB */
4558  sumliftcoef += liftcoefs[liftvar];
4559  }
4560  else
4561  assert(gubset->gubconss[liftgubconsidx]->gubvarsstatus[k] == GUBVARSTATUS_CAPACITYEXCEEDED);
4562  }
4563  assert(nliftgubvars >= 1); /* at least one variable is in R */
4564 
4565  /* minweight table and activity of current valid inequality will not change if (sum of alpha_{j_i} in GUB) = 0 */
4566  if( sumliftcoef == 0 )
4567  continue;
4568 
4569  /* updates minweight table: minweight_i+1[w] =
4570  * min{ minweights_i[w], min{ minweights_i[w - alpha_k]^{+} + a_k : k in GUB_j_i } }
4571  */
4572  for( w = *liftrhs; w >= 0; w-- )
4573  {
4574  for( k = 0; k < nliftgubvars; k++ )
4575  {
4576  liftcoef = liftcoefs[liftgubvars[k]];
4577  weight = weights[liftgubvars[k]];
4578 
4579  if( w < liftcoef )
4580  {
4581  min = MIN(minweights[w], weight);
4582  minweights[w] = min;
4583  }
4584  else
4585  {
4586  SCIP_Longint tmpval;
4587 
4588  assert(w >= liftcoef);
4589 
4590  tmpval = safeAddMinweightsGUB(minweights[w-liftcoef], weight);
4591  min = MIN(minweights[w], tmpval);
4592  minweights[w] = min;
4593  }
4594  }
4595  }
4596  assert(minweights[0] == 0);
4597  }
4598 
4599  /* frees temporary memory */
4600  SCIPfreeBufferArray(scip, &minweights);
4601  SCIPfreeBufferArray(scip, &finished);
4602  SCIPfreeBufferArray(scip, &unfinished);
4603  SCIPfreeBufferArray(scip, &liftgubvars);
4604  SCIPfreeBufferArray(scip, &gubconsGOC1 );
4605  SCIPfreeBufferArray(scip, &gubconsGNC1);
4606 
4607  return SCIP_OKAY;
4608 }
4609 
4610 /** lifts given minimal cover inequality
4611  * \f[
4612  * \sum_{j \in C} x_j \leq |C| - 1
4613  * \f]
4614  * valid for
4615  * \f[
4616  * S^0 = \{ x \in {0,1}^{|C|} : \sum_{j \in C} a_j x_j \leq a_0 \}
4617  * \f]
4618  * to a valid inequality
4619  * \f[
4620  * \sum_{j \in C} x_j + \sum_{j \in N \setminus C} \alpha_j x_j \leq |C| - 1
4621  * \f]
4622  * for
4623  * \f[
4624  * S = \{ x \in {0,1}^{|N|} : \sum_{j \in N} a_j x_j \leq a_0 \};
4625  * \f]
4626  * uses superadditive up-lifting for the variables in \f$N \setminus C\f$.
4627  */
4628 static
4630  SCIP* scip, /**< SCIP data structure */
4631  SCIP_VAR** vars, /**< variables in knapsack constraint */
4632  int nvars, /**< number of variables in knapsack constraint */
4633  int ntightened, /**< number of variables with tightened upper bound */
4634  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
4635  SCIP_Longint capacity, /**< capacity of knapsack */
4636  SCIP_Real* solvals, /**< solution values of all problem variables */
4637  int* covervars, /**< cover variables */
4638  int* noncovervars, /**< noncover variables */
4639  int ncovervars, /**< number of cover variables */
4640  int nnoncovervars, /**< number of noncover variables */
4641  SCIP_Longint coverweight, /**< weight of cover */
4642  SCIP_Real* liftcoefs, /**< pointer to store lifting coefficient of vars in knapsack constraint */
4643  SCIP_Real* cutact /**< pointer to store activity of lifted valid inequality */
4644  )
4645 {
4646  SCIP_Longint* maxweightsums;
4647  SCIP_Longint* intervalends;
4648  SCIP_Longint* rhos;
4649  SCIP_Real* sortkeys;
4650  SCIP_Longint lambda;
4651  int j;
4652  int h;
4653 
4654  assert(scip != NULL);
4655  assert(vars != NULL);
4656  assert(nvars >= 0);
4657  assert(weights != NULL);
4658  assert(capacity >= 0);
4659  assert(solvals != NULL);
4660  assert(covervars != NULL);
4661  assert(noncovervars != NULL);
4662  assert(ncovervars > 0 && ncovervars <= nvars);
4663  assert(nnoncovervars >= 0 && nnoncovervars <= nvars - ntightened);
4664  assert(ncovervars + nnoncovervars == nvars - ntightened);
4665  assert(liftcoefs != NULL);
4666  assert(cutact != NULL);
4667 
4668  /* allocates temporary memory */
4669  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, ncovervars) );
4670  SCIP_CALL( SCIPallocBufferArray(scip, &maxweightsums, ncovervars + 1) );
4671  SCIP_CALL( SCIPallocBufferArray(scip, &intervalends, ncovervars) );
4672  SCIP_CALL( SCIPallocBufferArray(scip, &rhos, ncovervars) );
4673 
4674  /* initializes data structures */
4675  BMSclearMemoryArray(liftcoefs, nvars);
4676  *cutact = 0.0;
4677 
4678  /* sets lifting coefficient of variables in C, sorts variables in C such that a_1 >= a_2 >= ... >= a_|C|
4679  * and calculates activity of current valid inequality
4680  */
4681  for( j = 0; j < ncovervars; j++ )
4682  {
4683  assert(liftcoefs[covervars[j]] == 0.0);
4684  liftcoefs[covervars[j]] = 1.0;
4685  sortkeys[j] = (SCIP_Real) weights[covervars[j]];
4686  (*cutact) += solvals[covervars[j]];
4687  }
4688  SCIPsortDownRealInt(sortkeys, covervars, ncovervars);
4689 
4690  /* calculates weight excess of cover C */
4691  lambda = coverweight - capacity;
4692  assert(lambda > 0);
4693 
4694  /* calculates A_h for h = 0,...,|C|, I_h for h = 1,...,|C| and rho_h for h = 1,...,|C| */
4695  maxweightsums[0] = 0;
4696  for( h = 1; h <= ncovervars; h++ )
4697  {
4698  maxweightsums[h] = maxweightsums[h-1] + weights[covervars[h-1]];
4699  intervalends[h-1] = maxweightsums[h] - lambda;
4700  rhos[h-1] = MAX(0, weights[covervars[h-1]] - weights[covervars[0]] + lambda);
4701  }
4702 
4703  /* sorts variables in N\C such that a_{j_1} <= a_{j_2} <= ... <= a_{j_t} */
4704  for( j = 0; j < nnoncovervars; j++ )
4705  sortkeys[j] = (SCIP_Real) (weights[noncovervars[j]]);
4706  SCIPsortRealInt(sortkeys, noncovervars, nnoncovervars);
4707 
4708  /* calculates lifting coefficient for all variables in N\C */
4709  h = 0;
4710  for( j = 0; j < nnoncovervars; j++ )
4711  {
4712  int liftvar;
4713  SCIP_Longint weight;
4714  SCIP_Real liftcoef;
4715 
4716  liftvar = noncovervars[j];
4717  weight = weights[liftvar];
4718 
4719  while( intervalends[h] < weight )
4720  h++;
4721 
4722  if( h == 0 )
4723  liftcoef = h;
4724  else
4725  {
4726  if( weight <= intervalends[h-1] + rhos[h] )
4727  {
4728  SCIP_Real tmp1;
4729  SCIP_Real tmp2;
4730  tmp1 = (SCIP_Real) (intervalends[h-1] + rhos[h] - weight);
4731  tmp2 = (SCIP_Real) rhos[1];
4732  liftcoef = h - ( tmp1 / tmp2 );
4733  }
4734  else
4735  liftcoef = h;
4736  }
4737 
4738  /* sets lifting coefficient */
4739  assert(liftcoefs[liftvar] == 0.0);
4740  liftcoefs[liftvar] = liftcoef;
4741 
4742  /* updates activity of current valid inequality */
4743  (*cutact) += liftcoef * solvals[liftvar];
4744  }
4745 
4746  /* frees temporary memory */
4747  SCIPfreeBufferArray(scip, &rhos);
4748  SCIPfreeBufferArray(scip, &intervalends);
4749  SCIPfreeBufferArray(scip, &maxweightsums);
4750  SCIPfreeBufferArray(scip, &sortkeys);
4751 
4752  return SCIP_OKAY;
4753 }
4754 
4755 
4756 /** separates lifted minimal cover inequalities using sequential up- and down-lifting and GUB information, if wanted, for
4757  * given knapsack problem
4758 */
4759 static
4761  SCIP* scip, /**< SCIP data structure */
4762  SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
4763  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
4764  SCIP_VAR** vars, /**< variables in knapsack constraint */
4765  int nvars, /**< number of variables in knapsack constraint */
4766  int ntightened, /**< number of variables with tightened upper bound */
4767  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
4768  SCIP_Longint capacity, /**< capacity of knapsack */
4769  SCIP_Real* solvals, /**< solution values of all problem variables */
4770  int* mincovervars, /**< mincover variables */
4771  int* nonmincovervars, /**< nonmincover variables */
4772  int nmincovervars, /**< number of mincover variables */
4773  int nnonmincovervars, /**< number of nonmincover variables */
4774  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
4775  SCIP_GUBSET* gubset, /**< GUB set data structure, NULL if no GUB information should be used */
4776  SCIP_Bool* cutoff, /**< pointer to store whether a cutoff has been detected */
4777  int* ncuts /**< pointer to add up the number of found cuts */
4778  )
4779 {
4780  int* varsC1;
4781  int* varsC2;
4782  int* varsF;
4783  int* varsR;
4784  int nvarsC1;
4785  int nvarsC2;
4786  int nvarsF;
4787  int nvarsR;
4788  SCIP_Real cutact;
4789  int* liftcoefs;
4790  int liftrhs;
4791 
4792  assert( cutoff != NULL );
4793  *cutoff = FALSE;
4794 
4795  /* allocates temporary memory */
4796  SCIP_CALL( SCIPallocBufferArray(scip, &varsC1, nvars) );
4797  SCIP_CALL( SCIPallocBufferArray(scip, &varsC2, nvars) );
4798  SCIP_CALL( SCIPallocBufferArray(scip, &varsF, nvars) );
4799  SCIP_CALL( SCIPallocBufferArray(scip, &varsR, nvars) );
4800  SCIP_CALL( SCIPallocBufferArray(scip, &liftcoefs, nvars) );
4801 
4802  /* gets partition (C_1,C_2) of C, i.e. C_1 & C_2 = C and C_1 cap C_2 = emptyset, with C_1 not empty; chooses partition
4803  * as follows
4804  * C_2 = { j in C : x*_j = 1 } and
4805  * C_1 = C\C_2
4806  */
4807  getPartitionCovervars(scip, solvals, mincovervars, nmincovervars, varsC1, varsC2, &nvarsC1, &nvarsC2);
4808  assert(nvarsC1 + nvarsC2 == nmincovervars);
4809  assert(nmincovervars > 0);
4810  assert(nvarsC1 >= 0); /* nvarsC1 > 0 does not always hold, because relaxed knapsack conss may already be violated */
4811 
4812  /* changes partition (C_1,C_2) of minimal cover C, if |C1| = 1, by moving one variable from C2 to C1 */
4813  if( nvarsC1 < 2 && nvarsC2 > 0)
4814  {
4815  SCIP_CALL( changePartitionCovervars(scip, weights, varsC1, varsC2, &nvarsC1, &nvarsC2) );
4816  assert(nvarsC1 >= 1);
4817  }
4818  assert(nvarsC2 == 0 || nvarsC1 >= 1);
4819 
4820  /* gets partition (F,R) of N\C, i.e. F & R = N\C and F cap R = emptyset; chooses partition as follows
4821  * R = { j in N\C : x*_j = 0 } and
4822  * F = (N\C)\F
4823  */
4824  getPartitionNoncovervars(scip, solvals, nonmincovervars, nnonmincovervars, varsF, varsR, &nvarsF, &nvarsR);
4825  assert(nvarsF + nvarsR == nnonmincovervars);
4826  assert(nvarsC1 + nvarsC2 + nvarsF + nvarsR == nvars - ntightened);
4827 
4828  /* lift cuts without GUB information */
4829  if( gubset == NULL )
4830  {
4831  /* sorts variables in F, C_2, R according to the second level lifting sequence that will be used in the sequential
4832  * lifting procedure
4833  */
4834  SCIP_CALL( getLiftingSequence(scip, solvals, weights, varsF, varsC2, varsR, nvarsF, nvarsC2, nvarsR) );
4835 
4836  /* lifts minimal cover inequality sum_{j in C_1} x_j <= |C_1| - 1 valid for
4837  *
4838  * S^0 = { x in {0,1}^|C_1| : sum_{j in C_1} a_j x_j <= a_0 - sum_{j in C_2} a_j }
4839  *
4840  * to a valid inequality sum_{j in C_1} x_j + sum_{j in N\C_1} alpha_j x_j <= |C_1| - 1 + sum_{j in C_2} alpha_j for
4841  *
4842  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 },
4843  *
4844  * uses sequential up-lifting for the variables in F, sequential down-lifting for the variable in C_2 and sequential
4845  * up-lifting for the variables in R according to the second level lifting sequence
4846  */
4847  SCIP_CALL( sequentialUpAndDownLifting(scip, vars, nvars, ntightened, weights, capacity, solvals, varsC1, varsC2,
4848  varsF, varsR, nvarsC1, nvarsC2, nvarsF, nvarsR, nvarsC1 - 1, liftcoefs, &cutact, &liftrhs) );
4849  }
4850  /* lift cuts with GUB information */
4851  else
4852  {
4853  int* gubconsGC1;
4854  int* gubconsGC2;
4855  int* gubconsGFC1;
4856  int* gubconsGR;
4857  int ngubconsGC1;
4858  int ngubconsGC2;
4859  int ngubconsGFC1;
4860  int ngubconsGR;
4861  int ngubconss;
4862  int nconstightened;
4863  int maxgubvarssize;
4864 
4865  assert(nvars == gubset->nvars);
4866 
4867  ngubconsGC1 = 0;
4868  ngubconsGC2 = 0;
4869  ngubconsGFC1 = 0;
4870  ngubconsGR = 0;
4871  ngubconss = gubset->ngubconss;
4872  nconstightened = 0;
4873  maxgubvarssize = 0;
4874 
4875  /* allocates temporary memory */
4876  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGC1, ngubconss) );
4877  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGC2, ngubconss) );
4878  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGFC1, ngubconss) );
4879  SCIP_CALL( SCIPallocBufferArray(scip, &gubconsGR, ngubconss) );
4880 
4881  /* categorizies GUBs of knapsack GUB partion into GOC1, GNC1, GF, GC2, and GR and computes a lifting sequence of
4882  * the GUBs for the sequential GUB wise lifting procedure
4883  */
4884  SCIP_CALL( getLiftingSequenceGUB(scip, gubset, solvals, weights, varsC1, varsC2, varsF, varsR, nvarsC1,
4885  nvarsC2, nvarsF, nvarsR, gubconsGC1, gubconsGC2, gubconsGFC1, gubconsGR, &ngubconsGC1, &ngubconsGC2,
4886  &ngubconsGFC1, &ngubconsGR, &nconstightened, &maxgubvarssize) );
4887 
4888  /* lifts minimal cover inequality sum_{j in C_1} x_j <= |C_1| - 1 valid for
4889  *
4890  * S^0 = { x in {0,1}^|C_1| : sum_{j in C_1} a_j x_j <= a_0 - sum_{j in C_2} a_j,
4891  * sum_{j in Q_i} x_j <= 1, forall i in I }
4892  *
4893  * to a valid inequality sum_{j in C_1} x_j + sum_{j in N\C_1} alpha_j x_j <= |C_1| - 1 + sum_{j in C_2} alpha_j for
4894  *
4895  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0, sum_{j in Q_i} x_j <= 1, forall i in I },
4896  *
4897  * uses sequential up-lifting for the variables in GUB constraints in gubconsGFC1,
4898  * sequential down-lifting for the variables in GUB constraints in gubconsGC2, and
4899  * sequential up-lifting for the variabels in GUB constraints in gubconsGR.
4900  */
4901  SCIP_CALL( sequentialUpAndDownLiftingGUB(scip, gubset, vars, nconstightened, weights, capacity, solvals, gubconsGC1,
4902  gubconsGC2, gubconsGFC1, gubconsGR, ngubconsGC1, ngubconsGC2, ngubconsGFC1, ngubconsGR,
4903  MIN(nvarsC1 - 1, ngubconsGC1), liftcoefs, &cutact, &liftrhs, maxgubvarssize) );
4904 
4905  /* frees temporary memory */
4906  SCIPfreeBufferArray(scip, &gubconsGR);
4907  SCIPfreeBufferArray(scip, &gubconsGFC1);
4908  SCIPfreeBufferArray(scip, &gubconsGC2);
4909  SCIPfreeBufferArray(scip, &gubconsGC1);
4910  }
4911 
4912  /* checks, if lifting yielded a violated cut */
4913  if( SCIPisEfficacious(scip, (cutact - liftrhs)/sqrt((SCIP_Real)MAX(liftrhs, 1))) )
4914  {
4915  SCIP_ROW* row;
4916  char name[SCIP_MAXSTRLEN];
4917  int j;
4918 
4919  /* creates LP row */
4920  assert( cons == NULL || sepa == NULL );
4921  if ( cons != NULL )
4922  {
4923  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcseq%"SCIP_LONGINT_FORMAT"", SCIPconsGetName(cons), SCIPconshdlrGetNCutsFound(SCIPconsGetHdlr(cons)));
4924  SCIP_CALL( SCIPcreateEmptyRowCons(scip, &row, SCIPconsGetHdlr(cons), name, -SCIPinfinity(scip), (SCIP_Real)liftrhs,
4925  cons != NULL ? SCIPconsIsLocal(cons) : FALSE, FALSE,
4926  cons != NULL ? SCIPconsIsRemovable(cons) : TRUE) );
4927  }
4928  else if ( sepa != NULL )
4929  {
4930  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcseq_%"SCIP_LONGINT_FORMAT"", SCIPsepaGetName(sepa), SCIPsepaGetNCutsFound(sepa));
4931  SCIP_CALL( SCIPcreateEmptyRowSepa(scip, &row, sepa, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
4932  }
4933  else
4934  {
4935  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nn_mcseq_%"SCIP_LONGINT_FORMAT"", *ncuts);
4936  SCIP_CALL( SCIPcreateEmptyRowUnspec(scip, &row, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
4937  }
4938 
4939  /* adds all variables in the knapsack constraint with calculated lifting coefficient to the cut */
4940  SCIP_CALL( SCIPcacheRowExtensions(scip, row) );
4941  assert(nvarsC1 + nvarsC2 + nvarsF + nvarsR == nvars - ntightened);
4942  for( j = 0; j < nvarsC1; j++ )
4943  {
4944  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsC1[j]], 1.0) );
4945  }
4946  for( j = 0; j < nvarsC2; j++ )
4947  {
4948  if( liftcoefs[varsC2[j]] > 0 )
4949  {
4950  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsC2[j]], (SCIP_Real)liftcoefs[varsC2[j]]) );
4951  }
4952  }
4953  for( j = 0; j < nvarsF; j++ )
4954  {
4955  if( liftcoefs[varsF[j]] > 0 )
4956  {
4957  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsF[j]], (SCIP_Real)liftcoefs[varsF[j]]) );
4958  }
4959  }
4960  for( j = 0; j < nvarsR; j++ )
4961  {
4962  if( liftcoefs[varsR[j]] > 0 )
4963  {
4964  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsR[j]], (SCIP_Real)liftcoefs[varsR[j]]) );
4965  }
4966  }
4967  SCIP_CALL( SCIPflushRowExtensions(scip, row) );
4968 
4969  /* checks, if cut is violated enough */
4970  if( SCIPisCutEfficacious(scip, sol, row) )
4971  {
4972  if( cons != NULL )
4973  {
4974  SCIP_CALL( SCIPresetConsAge(scip, cons) );
4975  }
4976  SCIP_CALL( SCIPaddCut(scip, sol, row, FALSE, cutoff) );
4977  (*ncuts)++;
4978  }
4979  SCIP_CALL( SCIPreleaseRow(scip, &row) );
4980  }
4981 
4982  /* frees temporary memory */
4983  SCIPfreeBufferArray(scip, &liftcoefs);
4984  SCIPfreeBufferArray(scip, &varsR);
4985  SCIPfreeBufferArray(scip, &varsF);
4986  SCIPfreeBufferArray(scip, &varsC2);
4987  SCIPfreeBufferArray(scip, &varsC1);
4988 
4989  return SCIP_OKAY;
4990 }
4991 
4992 /** separates lifted extended weight inequalities using sequential up- and down-lifting for given knapsack problem */
4993 static
4995  SCIP* scip, /**< SCIP data structure */
4996  SCIP_CONS* cons, /**< constraint that originates the knapsack problem, or NULL */
4997  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
4998  SCIP_VAR** vars, /**< variables in knapsack constraint */
4999  int nvars, /**< number of variables in knapsack constraint */
5000  int ntightened, /**< number of variables with tightened upper bound */
5001  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5002  SCIP_Longint capacity, /**< capacity of knapsack */
5003  SCIP_Real* solvals, /**< solution values of all problem variables */
5004  int* feassetvars, /**< variables in feasible set */
5005  int* nonfeassetvars, /**< variables not in feasible set */
5006  int nfeassetvars, /**< number of variables in feasible set */
5007  int nnonfeassetvars, /**< number of variables not in feasible set */
5008  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
5009  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
5010  int* ncuts /**< pointer to add up the number of found cuts */
5011  )
5012 {
5013  int* varsT1;
5014  int* varsT2;
5015  int* varsF;
5016  int* varsR;
5017  int* liftcoefs;
5018  SCIP_Real cutact;
5019  int nvarsT1;
5020  int nvarsT2;
5021  int nvarsF;
5022  int nvarsR;
5023  int liftrhs;
5024  int j;
5025 
5026  assert( cutoff != NULL );
5027  *cutoff = FALSE;
5028 
5029  /* allocates temporary memory */
5030  SCIP_CALL( SCIPallocBufferArray(scip, &varsT1, nvars) );
5031  SCIP_CALL( SCIPallocBufferArray(scip, &varsT2, nvars) );
5032  SCIP_CALL( SCIPallocBufferArray(scip, &varsF, nvars) );
5033  SCIP_CALL( SCIPallocBufferArray(scip, &varsR, nvars) );
5034  SCIP_CALL( SCIPallocBufferArray(scip, &liftcoefs, nvars) );
5035 
5036  /* gets partition (T_1,T_2) of T, i.e. T_1 & T_2 = T and T_1 cap T_2 = emptyset, with T_1 not empty; chooses partition
5037  * as follows
5038  * T_2 = { j in T : x*_j = 1 } and
5039  * T_1 = T\T_2
5040  */
5041  getPartitionCovervars(scip, solvals, feassetvars, nfeassetvars, varsT1, varsT2, &nvarsT1, &nvarsT2);
5042  assert(nvarsT1 + nvarsT2 == nfeassetvars);
5043 
5044  /* changes partition (T_1,T_2) of feasible set T, if |T1| = 0, by moving one variable from T2 to T1 */
5045  if( nvarsT1 == 0 && nvarsT2 > 0)
5046  {
5047  SCIP_CALL( changePartitionFeasiblesetvars(scip, weights, varsT1, varsT2, &nvarsT1, &nvarsT2) );
5048  assert(nvarsT1 == 1);
5049  }
5050  assert(nvarsT2 == 0 || nvarsT1 > 0);
5051 
5052  /* gets partition (F,R) of N\T, i.e. F & R = N\T and F cap R = emptyset; chooses partition as follows
5053  * R = { j in N\T : x*_j = 0 } and
5054  * F = (N\T)\F
5055  */
5056  getPartitionNoncovervars(scip, solvals, nonfeassetvars, nnonfeassetvars, varsF, varsR, &nvarsF, &nvarsR);
5057  assert(nvarsF + nvarsR == nnonfeassetvars);
5058  assert(nvarsT1 + nvarsT2 + nvarsF + nvarsR == nvars - ntightened);
5059 
5060  /* sorts variables in F, T_2, and R according to the second level lifting sequence that will be used in the sequential
5061  * lifting procedure (the variable removed last from the initial cover does not have to be lifted first, therefore it
5062  * is included in the sorting routine)
5063  */
5064  SCIP_CALL( getLiftingSequence(scip, solvals, weights, varsF, varsT2, varsR, nvarsF, nvarsT2, nvarsR) );
5065 
5066  /* lifts extended weight inequality sum_{j in T_1} x_j <= |T_1| valid for
5067  *
5068  * S^0 = { x in {0,1}^|T_1| : sum_{j in T_1} a_j x_j <= a_0 - sum_{j in T_2} a_j }
5069  *
5070  * to a valid inequality sum_{j in T_1} x_j + sum_{j in N\T_1} alpha_j x_j <= |T_1| + sum_{j in T_2} alpha_j for
5071  *
5072  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 },
5073  *
5074  * uses sequential up-lifting for the variables in F, sequential down-lifting for the variable in T_2 and sequential
5075  * up-lifting for the variabels in R according to the second level lifting sequence
5076  */
5077  SCIP_CALL( sequentialUpAndDownLifting(scip, vars, nvars, ntightened, weights, capacity, solvals, varsT1, varsT2, varsF, varsR,
5078  nvarsT1, nvarsT2, nvarsF, nvarsR, nvarsT1, liftcoefs, &cutact, &liftrhs) );
5079 
5080  /* checks, if lifting yielded a violated cut */
5081  if( SCIPisEfficacious(scip, (cutact - liftrhs)/sqrt((SCIP_Real)MAX(liftrhs, 1))) )
5082  {
5083  SCIP_ROW* row;
5084  char name[SCIP_MAXSTRLEN];
5085 
5086  /* creates LP row */
5087  assert( cons == NULL || sepa == NULL );
5088  if( cons != NULL )
5089  {
5090  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_ewseq%"SCIP_LONGINT_FORMAT"", SCIPconsGetName(cons), SCIPconshdlrGetNCutsFound(SCIPconsGetHdlr(cons)));
5091  SCIP_CALL( SCIPcreateEmptyRowCons(scip, &row, SCIPconsGetHdlr(cons), name, -SCIPinfinity(scip), (SCIP_Real)liftrhs,
5092  cons != NULL ? SCIPconsIsLocal(cons) : FALSE, FALSE,
5093  cons != NULL ? SCIPconsIsRemovable(cons) : TRUE) );
5094  }
5095  else if ( sepa != NULL )
5096  {
5097  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_ewseq_%"SCIP_LONGINT_FORMAT"", SCIPsepaGetName(sepa), SCIPsepaGetNCutsFound(sepa));
5098  SCIP_CALL( SCIPcreateEmptyRowSepa(scip, &row, sepa, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5099  }
5100  else
5101  {
5102  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nn_ewseq_%"SCIP_LONGINT_FORMAT"", *ncuts);
5103  SCIP_CALL( SCIPcreateEmptyRowUnspec(scip, &row, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5104  }
5105 
5106  /* adds all variables in the knapsack constraint with calculated lifting coefficient to the cut */
5107  SCIP_CALL( SCIPcacheRowExtensions(scip, row) );
5108  assert(nvarsT1 + nvarsT2 + nvarsF + nvarsR == nvars - ntightened);
5109  for( j = 0; j < nvarsT1; j++ )
5110  {
5111  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsT1[j]], 1.0) );
5112  }
5113  for( j = 0; j < nvarsT2; j++ )
5114  {
5115  if( liftcoefs[varsT2[j]] > 0 )
5116  {
5117  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsT2[j]], (SCIP_Real)liftcoefs[varsT2[j]]) );
5118  }
5119  }
5120  for( j = 0; j < nvarsF; j++ )
5121  {
5122  if( liftcoefs[varsF[j]] > 0 )
5123  {
5124  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsF[j]], (SCIP_Real)liftcoefs[varsF[j]]) );
5125  }
5126  }
5127  for( j = 0; j < nvarsR; j++ )
5128  {
5129  if( liftcoefs[varsR[j]] > 0 )
5130  {
5131  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[varsR[j]], (SCIP_Real)liftcoefs[varsR[j]]) );
5132  }
5133  }
5134  SCIP_CALL( SCIPflushRowExtensions(scip, row) );
5135 
5136  /* checks, if cut is violated enough */
5137  if( SCIPisCutEfficacious(scip, sol, row) )
5138  {
5139  if( cons != NULL )
5140  {
5141  SCIP_CALL( SCIPresetConsAge(scip, cons) );
5142  }
5143  SCIP_CALL( SCIPaddCut(scip, sol, row, FALSE, cutoff) );
5144  (*ncuts)++;
5145  }
5146  SCIP_CALL( SCIPreleaseRow(scip, &row) );
5147  }
5148 
5149  /* frees temporary memory */
5150  SCIPfreeBufferArray(scip, &liftcoefs);
5151  SCIPfreeBufferArray(scip, &varsR);
5152  SCIPfreeBufferArray(scip, &varsF);
5153  SCIPfreeBufferArray(scip, &varsT2);
5154  SCIPfreeBufferArray(scip, &varsT1);
5155 
5156  return SCIP_OKAY;
5157 }
5158 
5159 /** separates lifted minimal cover inequalities using superadditive up-lifting for given knapsack problem */
5160 static
5162  SCIP* scip, /**< SCIP data structure */
5163  SCIP_CONS* cons, /**< constraint that originates the knapsack problem, or NULL */
5164  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5165  SCIP_VAR** vars, /**< variables in knapsack constraint */
5166  int nvars, /**< number of variables in knapsack constraint */
5167  int ntightened, /**< number of variables with tightened upper bound */
5168  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5169  SCIP_Longint capacity, /**< capacity of knapsack */
5170  SCIP_Real* solvals, /**< solution values of all problem variables */
5171  int* mincovervars, /**< mincover variables */
5172  int* nonmincovervars, /**< nonmincover variables */
5173  int nmincovervars, /**< number of mincover variables */
5174  int nnonmincovervars, /**< number of nonmincover variables */
5175  SCIP_Longint mincoverweight, /**< weight of minimal cover */
5176  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
5177  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
5178  int* ncuts /**< pointer to add up the number of found cuts */
5179  )
5180 {
5181  SCIP_Real* realliftcoefs;
5182  SCIP_Real cutact;
5183  int liftrhs;
5184 
5185  assert( cutoff != NULL );
5186  *cutoff = FALSE;
5187  cutact = 0.0;
5188 
5189  /* allocates temporary memory */
5190  SCIP_CALL( SCIPallocBufferArray(scip, &realliftcoefs, nvars) );
5191 
5192  /* lifts minimal cover inequality sum_{j in C} x_j <= |C| - 1 valid for
5193  *
5194  * S^0 = { x in {0,1}^|C| : sum_{j in C} a_j x_j <= a_0 }
5195  *
5196  * to a valid inequality sum_{j in C} x_j + sum_{j in N\C} alpha_j x_j <= |C| - 1 for
5197  *
5198  * S = { x in {0,1}^|N| : sum_{j in N} a_j x_j <= a_0 },
5199  *
5200  * uses superadditive up-lifting for the variables in N\C.
5201  */
5202  SCIP_CALL( superadditiveUpLifting(scip, vars, nvars, ntightened, weights, capacity, solvals, mincovervars,
5203  nonmincovervars, nmincovervars, nnonmincovervars, mincoverweight, realliftcoefs, &cutact) );
5204  liftrhs = nmincovervars - 1;
5205 
5206  /* checks, if lifting yielded a violated cut */
5207  if( SCIPisEfficacious(scip, (cutact - liftrhs)/sqrt((SCIP_Real)MAX(liftrhs, 1))) )
5208  {
5209  SCIP_ROW* row;
5210  char name[SCIP_MAXSTRLEN];
5211  int j;
5212 
5213  /* creates LP row */
5214  assert( cons == NULL || sepa == NULL );
5215  if ( cons != NULL )
5216  {
5217  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcsup%"SCIP_LONGINT_FORMAT"", SCIPconsGetName(cons), SCIPconshdlrGetNCutsFound(SCIPconsGetHdlr(cons)));
5218  SCIP_CALL( SCIPcreateEmptyRowCons(scip, &row, SCIPconsGetHdlr(cons), name, -SCIPinfinity(scip), (SCIP_Real)liftrhs,
5219  cons != NULL ? SCIPconsIsLocal(cons) : FALSE, FALSE,
5220  cons != NULL ? SCIPconsIsRemovable(cons) : TRUE) );
5221  }
5222  else if ( sepa != NULL )
5223  {
5224  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_mcsup%"SCIP_LONGINT_FORMAT"", SCIPsepaGetName(sepa), SCIPsepaGetNCutsFound(sepa));
5225  SCIP_CALL( SCIPcreateEmptyRowSepa(scip, &row, sepa, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5226  }
5227  else
5228  {
5229  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nn_mcsup_%"SCIP_LONGINT_FORMAT"", *ncuts);
5230  SCIP_CALL( SCIPcreateEmptyRowUnspec(scip, &row, name, -SCIPinfinity(scip), (SCIP_Real)liftrhs, FALSE, FALSE, TRUE) );
5231  }
5232 
5233  /* adds all variables in the knapsack constraint with calculated lifting coefficient to the cut */
5234  SCIP_CALL( SCIPcacheRowExtensions(scip, row) );
5235  assert(nmincovervars + nnonmincovervars == nvars - ntightened);
5236  for( j = 0; j < nmincovervars; j++ )
5237  {
5238  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[mincovervars[j]], 1.0) );
5239  }
5240  for( j = 0; j < nnonmincovervars; j++ )
5241  {
5242  assert(SCIPisFeasGE(scip, realliftcoefs[nonmincovervars[j]], 0.0));
5243  if( SCIPisFeasGT(scip, realliftcoefs[nonmincovervars[j]], 0.0) )
5244  {
5245  SCIP_CALL( SCIPaddVarToRow(scip, row, vars[nonmincovervars[j]], realliftcoefs[nonmincovervars[j]]) );
5246  }
5247  }
5248  SCIP_CALL( SCIPflushRowExtensions(scip, row) );
5249 
5250  /* checks, if cut is violated enough */
5251  if( SCIPisCutEfficacious(scip, sol, row) )
5252  {
5253  if( cons != NULL )
5254  {
5255  SCIP_CALL( SCIPresetConsAge(scip, cons) );
5256  }
5257  SCIP_CALL( SCIPaddCut(scip, sol, row, FALSE, cutoff) );
5258  (*ncuts)++;
5259  }
5260  SCIP_CALL( SCIPreleaseRow(scip, &row) );
5261  }
5262 
5263  /* frees temporary memory */
5264  SCIPfreeBufferArray(scip, &realliftcoefs);
5265 
5266  return SCIP_OKAY;
5267 }
5268 
5269 /** converts given cover C to a minimal cover by removing variables in the reverse order in which the variables were chosen
5270  * to be in C, i.e. in the order of non-increasing (1 - x*_j)/a_j, if the transformed separation problem was used to find
5271  * C and in the order of non-increasing (1 - x*_j), if the modified transformed separation problem was used to find C;
5272  * note that all variables with x*_j = 1 will be removed last
5273  */
5274 static
5276  SCIP* scip, /**< SCIP data structure */
5277  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5278  SCIP_Longint capacity, /**< capacity of knapsack */
5279  SCIP_Real* solvals, /**< solution values of all problem variables */
5280  int* covervars, /**< pointer to store cover variables */
5281  int* noncovervars, /**< pointer to store noncover variables */
5282  int* ncovervars, /**< pointer to store number of cover variables */
5283  int* nnoncovervars, /**< pointer to store number of noncover variables */
5284  SCIP_Longint* coverweight, /**< pointer to store weight of cover */
5285  SCIP_Bool modtransused /**< TRUE if mod trans sepa prob was used to find cover */
5286  )
5287 {
5288  SORTKEYPAIR** sortkeypairs;
5289  SCIP_Longint minweight;
5290  int nsortkeypairs;
5291  int minweightidx;
5292  int j;
5293  int k;
5294 
5295  assert(scip != NULL);
5296  assert(covervars != NULL);
5297  assert(noncovervars != NULL);
5298  assert(ncovervars != NULL);
5299  assert(*ncovervars > 0);
5300  assert(nnoncovervars != NULL);
5301  assert(*nnoncovervars >= 0);
5302  assert(coverweight != NULL);
5303  assert(*coverweight > 0);
5304  assert(*coverweight > capacity);
5305 
5306  /* allocates temporary memory */
5307  nsortkeypairs = *ncovervars;
5308  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &sortkeypairs, nsortkeypairs) );
5309 
5310  /* sorts C in the reverse order in which the variables were chosen to be in the cover, i.e.
5311  * such that (1 - x*_1)/a_1 >= ... >= (1 - x*_|C|)/a_|C|, if trans separation problem was used to find C
5312  * such that (1 - x*_1) >= ... >= (1 - x*_|C|), if modified trans separation problem was used to find C
5313  * note that all variables with x*_j = 1 are in the end of the sorted C, so they will be removed last from C
5314  */
5315  assert(*ncovervars == nsortkeypairs);
5316  if( modtransused )
5317  {
5318  for( j = 0; j < *ncovervars; j++ )
5319  {
5320  SCIP_CALL( SCIPallocBlockMemory(scip, &(sortkeypairs[j])) ); /*lint !e866 */
5321 
5322  sortkeypairs[j]->key1 = solvals[covervars[j]];
5323  sortkeypairs[j]->key2 = (SCIP_Real) weights[covervars[j]];
5324  }
5325  }
5326  else
5327  {
5328  for( j = 0; j < *ncovervars; j++ )
5329  {
5330  SCIP_CALL( SCIPallocBlockMemory(scip, &(sortkeypairs[j])) ); /*lint !e866 */
5331 
5332  sortkeypairs[j]->key1 = (solvals[covervars[j]] - 1.0) / ((SCIP_Real) weights[covervars[j]]);
5333  sortkeypairs[j]->key2 = (SCIP_Real) (-weights[covervars[j]]);
5334  }
5335  }
5336  SCIPsortPtrInt((void**)sortkeypairs, covervars, compSortkeypairs, *ncovervars);
5337 
5338  /* gets j' with a_j' = min{ a_j : j in C } */
5339  minweightidx = 0;
5340  minweight = weights[covervars[minweightidx]];
5341  for( j = 1; j < *ncovervars; j++ )
5342  {
5343  if( weights[covervars[j]] <= minweight )
5344  {
5345  minweightidx = j;
5346  minweight = weights[covervars[minweightidx]];
5347  }
5348  }
5349  assert(minweightidx >= 0 && minweightidx < *ncovervars);
5350  assert(minweight > 0 && minweight <= *coverweight);
5351 
5352  j = 0;
5353  /* removes variables from C until the remaining variables form a minimal cover */
5354  while( j < *ncovervars && ((*coverweight) - minweight > capacity) )
5355  {
5356  assert(minweightidx >= j);
5357  assert(checkMinweightidx(weights, capacity, covervars, *ncovervars, *coverweight, minweightidx, j));
5358 
5359  /* if sum_{i in C} a_i - a_j <= a_0, j cannot be removed from C */
5360  if( (*coverweight) - weights[covervars[j]] <= capacity )
5361  {
5362  ++j;
5363  continue;
5364  }
5365 
5366  /* adds j to N\C */
5367  noncovervars[*nnoncovervars] = covervars[j];
5368  (*nnoncovervars)++;
5369 
5370  /* removes j from C */
5371  (*coverweight) -= weights[covervars[j]];
5372  for( k = j; k < (*ncovervars) - 1; k++ )
5373  covervars[k] = covervars[k+1];
5374  (*ncovervars)--;
5375 
5376  /* updates j' with a_j' = min{ a_j : j in C } */
5377  if( j == minweightidx )
5378  {
5379  minweightidx = 0;
5380  minweight = weights[covervars[minweightidx]];
5381  for( k = 1; k < *ncovervars; k++ )
5382  {
5383  if( weights[covervars[k]] <= minweight )
5384  {
5385  minweightidx = k;
5386  minweight = weights[covervars[minweightidx]];
5387  }
5388  }
5389  assert(minweight > 0 && minweight <= *coverweight);
5390  assert(minweightidx >= 0 && minweightidx < *ncovervars);
5391  }
5392  else
5393  {
5394  assert(minweightidx > j);
5395  minweightidx--;
5396  }
5397  /* j needs to stay the same */
5398  }
5399  assert((*coverweight) > capacity);
5400  assert((*coverweight) - minweight <= capacity);
5401 
5402  /* frees temporary memory */
5403  for( j = nsortkeypairs-1; j >= 0; j-- )
5404  SCIPfreeBlockMemory(scip, &(sortkeypairs[j])); /*lint !e866 */
5405  SCIPfreeBlockMemoryArray(scip, &sortkeypairs, nsortkeypairs);
5406 
5407  return SCIP_OKAY;
5408 }
5409 
5410 /** converts given initial cover C_init to a feasible set by removing variables in the reverse order in which
5411  * they were chosen to be in C_init:
5412  * non-increasing (1 - x*_j)/a_j, if transformed separation problem was used to find C_init
5413  * non-increasing (1 - x*_j), if modified transformed separation problem was used to find C_init.
5414  * separates lifted extended weight inequalities using sequential up- and down-lifting for this feasible set
5415  * and all subsequent feasible sets.
5416  */
5417 static
5419  SCIP* scip, /**< SCIP data structure */
5420  SCIP_CONS* cons, /**< constraint that originates the knapsack problem */
5421  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5422  SCIP_VAR** vars, /**< variables in knapsack constraint */
5423  int nvars, /**< number of variables in knapsack constraint */
5424  int ntightened, /**< number of variables with tightened upper bound */
5425  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5426  SCIP_Longint capacity, /**< capacity of knapsack */
5427  SCIP_Real* solvals, /**< solution values of all problem variables */
5428  int* covervars, /**< pointer to store cover variables */
5429  int* noncovervars, /**< pointer to store noncover variables */
5430  int* ncovervars, /**< pointer to store number of cover variables */
5431  int* nnoncovervars, /**< pointer to store number of noncover variables */
5432  SCIP_Longint* coverweight, /**< pointer to store weight of cover */
5433  SCIP_Bool modtransused, /**< TRUE if mod trans sepa prob was used to find cover */
5434  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
5435  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
5436  int* ncuts /**< pointer to add up the number of found cuts */
5437  )
5438 {
5439  SCIP_Real* sortkeys;
5440  int j;
5441  int k;
5442 
5443  assert(scip != NULL);
5444  assert(covervars != NULL);
5445  assert(noncovervars != NULL);
5446  assert(ncovervars != NULL);
5447  assert(*ncovervars > 0);
5448  assert(nnoncovervars != NULL);
5449  assert(*nnoncovervars >= 0);
5450  assert(coverweight != NULL);
5451  assert(*coverweight > 0);
5452  assert(*coverweight > capacity);
5453  assert(*ncovervars + *nnoncovervars == nvars - ntightened);
5454  assert(cutoff != NULL);
5455 
5456  *cutoff = FALSE;
5457 
5458  /* allocates temporary memory */
5459  SCIP_CALL( SCIPallocBufferArray(scip, &sortkeys, *ncovervars) );
5460 
5461  /* sorts C in the reverse order in which the variables were chosen to be in the cover, i.e.
5462  * such that (1 - x*_1)/a_1 >= ... >= (1 - x*_|C|)/a_|C|, if trans separation problem was used to find C
5463  * such that (1 - x*_1) >= ... >= (1 - x*_|C|), if modified trans separation problem was used to find C
5464  * note that all variables with x*_j = 1 are in the end of the sorted C, so they will be removed last from C
5465  */
5466  if( modtransused )
5467  {
5468  for( j = 0; j < *ncovervars; j++ )
5469  {
5470  sortkeys[j] = solvals[covervars[j]];
5471  assert(SCIPisFeasGE(scip, sortkeys[j], 0.0));
5472  }
5473  }
5474  else
5475  {
5476  for( j = 0; j < *ncovervars; j++ )
5477  {
5478  sortkeys[j] = (solvals[covervars[j]] - 1.0) / ((SCIP_Real) weights[covervars[j]]);
5479  assert(SCIPisFeasLE(scip, sortkeys[j], 0.0));
5480  }
5481  }
5482  SCIPsortRealInt(sortkeys, covervars, *ncovervars);
5483 
5484  /* removes variables from C_init and separates lifted extended weight inequalities using sequential up- and down-lifting;
5485  * in addition to an extended weight inequality this gives cardinality inequalities */
5486  while( *ncovervars >= 2 )
5487  {
5488  /* adds first element of C_init to N\C_init */
5489  noncovervars[*nnoncovervars] = covervars[0];
5490  (*nnoncovervars)++;
5491 
5492  /* removes first element from C_init */
5493  (*coverweight) -= weights[covervars[0]];
5494  for( k = 0; k < (*ncovervars) - 1; k++ )
5495  covervars[k] = covervars[k+1];
5496  (*ncovervars)--;
5497 
5498  assert(*ncovervars + *nnoncovervars == nvars - ntightened);
5499  if( (*coverweight) <= capacity )
5500  {
5501  SCIP_CALL( separateSequLiftedExtendedWeightInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity, solvals,
5502  covervars, noncovervars, *ncovervars, *nnoncovervars, sol, cutoff, ncuts) );
5503  }
5504 
5505  /* stop if cover is too large */
5506  if ( *ncovervars >= MAXCOVERSIZEITERLEWI )
5507  break;
5508  }
5509 
5510  /* frees temporary memory */
5511  SCIPfreeBufferArray(scip, &sortkeys);
5512 
5513  return SCIP_OKAY;
5514 }
5515 
5516 /** separates different classes of valid inequalities for the 0-1 knapsack problem */
5518  SCIP* scip, /**< SCIP data structure */
5519  SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
5520  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5521  SCIP_VAR** vars, /**< variables in knapsack constraint */
5522  int nvars, /**< number of variables in knapsack constraint */
5523  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
5524  SCIP_Longint capacity, /**< capacity of knapsack */
5525  SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
5526  SCIP_Bool usegubs, /**< should GUB information be used for separation? */
5527  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
5528  int* ncuts /**< pointer to add up the number of found cuts */
5529  )
5530 {
5531  SCIP_Real* solvals;
5532  int* covervars;
5533  int* noncovervars;
5534  SCIP_Bool coverfound;
5535  SCIP_Bool fractional;
5536  SCIP_Bool modtransused;
5537  SCIP_Longint coverweight;
5538  int ncovervars;
5539  int nnoncovervars;
5540  int ntightened;
5541 
5542  assert(scip != NULL);
5543  assert(vars != NULL);
5544  assert(nvars > 0);
5545  assert(weights != NULL);
5546  assert(capacity >= 0);
5547  assert(cutoff != NULL);
5548  assert(ncuts != NULL);
5549 
5550  *cutoff = FALSE;
5551 
5552  /* increase age of constraint (age is reset to zero, if a cut was found) */
5553  if( cons != NULL )
5554  {
5555  SCIP_CALL( SCIPincConsAge(scip, cons) );
5556  }
5557 
5558  /* allocates temporary memory */
5559  SCIP_CALL( SCIPallocBufferArray(scip, &solvals, nvars) );
5560  SCIP_CALL( SCIPallocBufferArray(scip, &covervars, nvars) );
5561  SCIP_CALL( SCIPallocBufferArray(scip, &noncovervars, nvars) );
5562 
5563  /* gets solution values of all problem variables */
5564  SCIP_CALL( SCIPgetSolVals(scip, sol, nvars, vars, solvals) );
5565 
5566 #ifdef SCIP_DEBUG
5567  {
5568  int i;
5569 
5570  SCIPdebugMessage("separate cuts for knapsack constraint originated by cons <%s>:\n",
5571  cons == NULL ? "-" : SCIPconsGetName(cons));
5572  for( i = 0; i < nvars; ++i )
5573  {
5574  SCIPdebugPrintf("%+"SCIP_LONGINT_FORMAT"<%s>(%g)", weights[i], SCIPvarGetName(vars[i]), solvals[i]);
5575  }
5576  SCIPdebugPrintf(" <= %"SCIP_LONGINT_FORMAT"\n", capacity);
5577  }
5578 #endif
5579 
5580  /* LMCI1 (lifted minimal cover inequalities using sequential up- and down-lifting) using GUB information
5581  */
5582  if( usegubs )
5583  {
5584  SCIP_GUBSET* gubset;
5585 
5586  SCIPdebugMessage("separate LMCI1-GUB cuts:\n");
5587 
5588  /* initializes partion of knapsack variables into nonoverlapping GUB constraints */
5589  SCIP_CALL( GUBsetCreate(scip, &gubset, nvars, weights, capacity) );
5590 
5591  /* constructs sophisticated partition of knapsack variables into nonoverlapping GUBs */
5592  SCIP_CALL( GUBsetGetCliquePartition(scip, gubset, vars, solvals) );
5593  assert(gubset->ngubconss <= nvars);
5594 
5595  /* gets a most violated initial cover C_init ( sum_{j in C_init} a_j > a_0 ) by using the
5596  * MODIFIED transformed separation problem and taking into account the following fixing:
5597  * j in C_init, if j in N_1 = {j in N : x*_j = 1} and
5598  * j in N\C_init, if j in N_0 = {j in N : x*_j = 0},
5599  * if one exists
5600  */
5601  modtransused = TRUE;
5602  SCIP_CALL( getCover(scip, vars, nvars, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5603  &nnoncovervars, &coverweight, &coverfound, modtransused, &ntightened, &fractional) );
5604 
5605  assert(!coverfound || !fractional || ncovervars + nnoncovervars == nvars - ntightened);
5606 
5607  /* if x* is not fractional we stop the separation routine */
5608  if( !fractional )
5609  {
5610  SCIPdebugMessage(" LMCI1-GUB terminated by no variable with fractional LP value.\n");
5611 
5612  /* frees memory for GUB set data structure */
5613  SCIP_CALL( GUBsetFree(scip, &gubset) );
5614 
5615  goto TERMINATE;
5616  }
5617 
5618  /* if no cover was found we stop the separation routine for lifted minimal cover inequality */
5619  if( coverfound )
5620  {
5621  /* converts initial cover C_init to a minimal cover C by removing variables in the reverse order in which the
5622  * variables were chosen to be in C_init; note that variables with x*_j = 1 will be removed last
5623  */
5624  SCIP_CALL( makeCoverMinimal(scip, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5625  &nnoncovervars, &coverweight, modtransused) );
5626 
5627  /* only separate with GUB information if we have at least one nontrivial GUB (with more than one variable) */
5628  if( gubset->ngubconss < nvars )
5629  {
5630  /* separates lifted minimal cover inequalities using sequential up- and down-lifting and GUB information */
5631  SCIP_CALL( separateSequLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5632  solvals, covervars, noncovervars, ncovervars, nnoncovervars, sol, gubset, cutoff, ncuts) );
5633  }
5634  else
5635  {
5636  /* separates lifted minimal cover inequalities using sequential up- and down-lifting, but do not use trivial
5637  * GUB information
5638  */
5639  SCIP_CALL( separateSequLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5640  solvals, covervars, noncovervars, ncovervars, nnoncovervars, sol, NULL, cutoff, ncuts) );
5641  }
5642  }
5643 
5644  /* frees memory for GUB set data structure */
5645  SCIP_CALL( GUBsetFree(scip, &gubset) );
5646  }
5647  else
5648  {
5649  /* LMCI1 (lifted minimal cover inequalities using sequential up- and down-lifting)
5650  * (and LMCI2 (lifted minimal cover inequalities using superadditive up-lifting))
5651  */
5652 
5653  /* gets a most violated initial cover C_init ( sum_{j in C_init} a_j > a_0 ) by using the
5654  * MODIFIED transformed separation problem and taking into account the following fixing:
5655  * j in C_init, if j in N_1 = {j in N : x*_j = 1} and
5656  * j in N\C_init, if j in N_0 = {j in N : x*_j = 0},
5657  * if one exists
5658  */
5659  SCIPdebugMessage("separate LMCI1 cuts:\n");
5660  modtransused = TRUE;
5661  SCIP_CALL( getCover(scip, vars, nvars, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5662  &nnoncovervars, &coverweight, &coverfound, modtransused, &ntightened, &fractional) );
5663  assert(!coverfound || !fractional || ncovervars + nnoncovervars == nvars - ntightened);
5664 
5665  /* if x* is not fractional we stop the separation routine */
5666  if( !fractional )
5667  goto TERMINATE;
5668 
5669  /* if no cover was found we stop the separation routine for lifted minimal cover inequality */
5670  if( coverfound )
5671  {
5672  /* converts initial cover C_init to a minimal cover C by removing variables in the reverse order in which the
5673  * variables were chosen to be in C_init; note that variables with x*_j = 1 will be removed last
5674  */
5675  SCIP_CALL( makeCoverMinimal(scip, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5676  &nnoncovervars, &coverweight, modtransused) );
5677 
5678  /* separates lifted minimal cover inequalities using sequential up- and down-lifting */
5679  SCIP_CALL( separateSequLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5680  solvals, covervars, noncovervars, ncovervars, nnoncovervars, sol, NULL, cutoff, ncuts) );
5681 
5682  if( USESUPADDLIFT ) /*lint !e506 !e774*/
5683  {
5684  SCIPdebugMessage("separate LMCI2 cuts:\n");
5685  /* separates lifted minimal cover inequalities using superadditive up-lifting */
5686  SCIP_CALL( separateSupLiftedMinimalCoverInequality(scip, cons, sepa, vars, nvars, ntightened, weights, capacity,
5687  solvals, covervars, noncovervars, ncovervars, nnoncovervars, coverweight, sol, cutoff, ncuts) );
5688  }
5689  }
5690  }
5691 
5692  /* LEWI (lifted extended weight inequalities using sequential up- and down-lifting) */
5693  if ( ! (*cutoff) )
5694  {
5695  /* gets a most violated initial cover C_init ( sum_{j in C_init} a_j > a_0 ) by using the
5696  * transformed separation problem and taking into account the following fixing:
5697  * j in C_init, if j in N_1 = {j in N : x*_j = 1} and
5698  * j in N\C_init, if j in N_0 = {j in N : x*_j = 0},
5699  * if one exists
5700  */
5701  SCIPdebugMessage("separate LEWI cuts:\n");
5702  modtransused = FALSE;
5703  SCIP_CALL( getCover(scip, vars, nvars, weights, capacity, solvals, covervars, noncovervars, &ncovervars,
5704  &nnoncovervars, &coverweight, &coverfound, modtransused, &ntightened, &fractional) );
5705  assert(fractional);
5706  assert(!coverfound || ncovervars + nnoncovervars == nvars - ntightened);
5707 
5708  /* if no cover was found we stop the separation routine */
5709  if( coverfound )
5710  {
5711  /* converts initial cover C_init to a feasible set by removing variables in the reverse order in which
5712  * they were chosen to be in C_init and separates lifted extended weight inequalities using sequential
5713  * up- and down-lifting for this feasible set and all subsequent feasible sets.
5714  */
5715  SCIP_CALL( getFeasibleSet(scip, cons, sepa, vars, nvars, ntightened, weights, capacity, solvals, covervars, noncovervars,
5716  &ncovervars, &nnoncovervars, &coverweight, modtransused, sol, cutoff, ncuts) );
5717  }
5718  }
5719 
5720  TERMINATE:
5721  /* frees temporary memory */
5722  SCIPfreeBufferArray(scip, &noncovervars);
5723  SCIPfreeBufferArray(scip, &covervars);
5724  SCIPfreeBufferArray(scip, &solvals);
5725 
5726  return SCIP_OKAY;
5727 }
5728 
5729 /* relaxes given general linear constraint into a knapsack constraint and separates lifted knapsack cover inequalities */
5731  SCIP* scip, /**< SCIP data structure */
5732  SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
5733  SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
5734  int nknapvars, /**< number of variables in the continuous knapsack constraint */
5735  SCIP_VAR** knapvars, /**< variables in the continuous knapsack constraint */
5736  SCIP_Real* knapvals, /**< coefficient of the variables in the continuous knapsack constraint */
5737  SCIP_Real valscale, /**< -1.0 if lhs of row is used as rhs of c. k. constraint, +1.0 otherwise */
5738  SCIP_Real rhs, /**< right hand side of the continuous knapsack constraint */
5739  SCIP_SOL* sol, /**< primal CIP solution, NULL for current LP solution */
5740  SCIP_Bool* cutoff, /**< pointer to store whether a cutoff was found */
5741  int* ncuts /**< pointer to add up the number of found cuts */
5742  )
5743 {
5744  SCIP_VAR** binvars;
5745  SCIP_VAR** consvars;
5746  SCIP_Real* binvals;
5747  SCIP_Longint* consvals;
5748  SCIP_Longint minact;
5749  SCIP_Longint maxact;
5750  SCIP_Real intscalar;
5751  SCIP_Bool success;
5752  int nbinvars;
5753  int nconsvars;
5754  int i;
5755 
5756  int* tmpindices;
5757  int tmp;
5758  SCIP_CONSHDLR* conshdlr;
5759  SCIP_CONSHDLRDATA* conshdlrdata;
5760  SCIP_Bool noknapsackconshdlr;
5761  SCIP_Bool usegubs;
5762 
5763  assert(nknapvars > 0);
5764  assert(knapvars != NULL);
5765  assert(cutoff != NULL);
5766 
5767  tmpindices = NULL;
5768 
5769  SCIPdebugMessage("separate linear constraint <%s> relaxed to knapsack\n", cons != NULL ? SCIPconsGetName(cons) : "-");
5770  SCIPdebug( if( cons != NULL ) { SCIPdebugPrintCons(scip, cons, NULL); } );
5771 
5772  binvars = SCIPgetVars(scip);
5773 
5774  /* all variables which are of integral type can be potentially of binary type; this can be checked via the method SCIPvarIsBinary(var) */
5775  nbinvars = SCIPgetNVars(scip) - SCIPgetNContVars(scip);
5776 
5777  *cutoff = FALSE;
5778 
5779  if( nbinvars == 0 )
5780  return SCIP_OKAY;
5781 
5782  /* set up data structures */
5783  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nbinvars) );
5784  SCIP_CALL( SCIPallocBufferArray(scip, &consvals, nbinvars) );
5785 
5786  /* get conshdlrdata to use cleared memory */
5787  conshdlr = SCIPfindConshdlr(scip, CONSHDLR_NAME);
5788  if( conshdlr == NULL )
5789  {
5790  noknapsackconshdlr = TRUE;
5791  usegubs = DEFAULT_USEGUBS;
5792 
5793  SCIP_CALL( SCIPallocBufferArray(scip, &binvals, nbinvars) );
5794  BMSclearMemoryArray(binvals, nbinvars);
5795  }
5796  else
5797  {
5798  noknapsackconshdlr = FALSE;
5799  conshdlrdata = SCIPconshdlrGetData(conshdlr);
5800  assert(conshdlrdata != NULL);
5801  usegubs = conshdlrdata->usegubs;
5802 
5803  SCIP_CALL( SCIPallocBufferArray(scip, &tmpindices, nknapvars) );
5804 
5805  assert(conshdlrdata->reals1size > 0);
5806 
5807  /* next if condition should normally not be true, because it means that presolving has created more binary
5808  * variables than binary + integer variables existed at the constraint initialization method, but for example if you would
5809  * transform all integers into their binary representation then it maybe happens
5810  */
5811  if( conshdlrdata->reals1size < nbinvars )
5812  {
5813  int oldsize;
5814  oldsize = conshdlrdata->reals1size;
5815 
5816  while( conshdlrdata->reals1size < nbinvars )
5817  conshdlrdata->reals1size *= 2;
5818  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->reals1, conshdlrdata->reals1size) );
5819  BMSclearMemoryArray(&(conshdlrdata->reals1[oldsize]), conshdlrdata->reals1size - oldsize); /*lint !e866 */
5820  }
5821  binvals = conshdlrdata->reals1;
5822 
5823  /* check for cleared array, all entries have to be zero */
5824 #ifndef NDEBUG
5825  for( tmp = nbinvars - 1; tmp >= 0; --tmp )
5826  {
5827  assert(binvals[tmp] == 0);
5828  }
5829 #endif
5830  }
5831 
5832  tmp = 0;
5833 
5834  /* relax continuous knapsack constraint:
5835  * 1. make all variables binary:
5836  * if x_j is continuous or integer variable substitute:
5837  * - a_j < 0: x_j = lb or x_j = b*z + d with variable lower bound b*z + d with binary variable z
5838  * - a_j > 0: x_j = ub or x_j = b*z + d with variable upper bound b*z + d with binary variable z
5839  * 2. convert coefficients of all variables to positive integers:
5840  * - scale all coefficients a_j to a~_j integral
5841  * - substitute x~_j = 1 - x_j if a~_j < 0
5842  */
5843 
5844  /* replace integer and continuous variables with binary variables */
5845  for( i = 0; i < nknapvars; i++ )
5846  {
5847  SCIP_VAR* var;
5848 
5849  var = knapvars[i];
5850 
5851  if( SCIPvarIsBinary(var) && SCIPvarIsActive(var) )
5852  {
5853  assert(0 <= SCIPvarGetProbindex(var) && SCIPvarGetProbindex(var) < nbinvars);
5854  binvals[SCIPvarGetProbindex(var)] += valscale * knapvals[i];
5855  if( !noknapsackconshdlr )
5856  {
5857  assert(tmpindices != NULL);
5858 
5859  tmpindices[tmp] = SCIPvarGetProbindex(var);
5860  ++tmp;
5861  }
5862  SCIPdebugMessage(" -> binary variable %+.15g<%s>(%.15g)\n", valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var));
5863  }
5864  else if( valscale * knapvals[i] > 0.0 )
5865  {
5866  SCIP_VAR** zvlb;
5867  SCIP_Real* bvlb;
5868  SCIP_Real* dvlb;
5869  SCIP_Real bestlbsol;
5870  int bestlbtype;
5871  int nvlb;
5872  int j;
5873 
5874  /* a_j > 0: substitution with lb or vlb */
5875  nvlb = SCIPvarGetNVlbs(var);
5876  zvlb = SCIPvarGetVlbVars(var);
5877  bvlb = SCIPvarGetVlbCoefs(var);
5878  dvlb = SCIPvarGetVlbConstants(var);
5879 
5880  /* search for lb or vlb with maximal bound value */
5881  bestlbsol = SCIPvarGetLbGlobal(var);
5882  bestlbtype = -1;
5883  for( j = 0; j < nvlb; j++ )
5884  {
5885  /* use only numerical stable vlb with binary variable z */
5886  if( SCIPvarIsBinary(zvlb[j]) && SCIPvarIsActive(zvlb[j]) && REALABS(bvlb[j]) <= MAXABSVBCOEF )
5887  {
5888  SCIP_Real vlbsol;
5889 
5890  if( (bvlb[j] >= 0.0 && SCIPisGT(scip, bvlb[j] * SCIPvarGetLbLocal(zvlb[j]) + dvlb[j], SCIPvarGetUbLocal(var))) ||
5891  (bvlb[j] <= 0.0 && SCIPisGT(scip, bvlb[j] * SCIPvarGetUbLocal(zvlb[j]) + dvlb[j], SCIPvarGetUbLocal(var))) )
5892  {
5893  *cutoff = TRUE;
5894  SCIPdebugMessage("variable bound <%s>[%g,%g] >= %g<%s>[%g,%g] + %g implies local cutoff\n",
5896  bvlb[j], SCIPvarGetName(zvlb[j]), SCIPvarGetLbLocal(zvlb[j]), SCIPvarGetUbLocal(zvlb[j]), dvlb[j]);
5897  goto TERMINATE;
5898  }
5899 
5900  assert(0 <= SCIPvarGetProbindex(zvlb[j]) && SCIPvarGetProbindex(zvlb[j]) < nbinvars);
5901  vlbsol = bvlb[j] * SCIPgetSolVal(scip, sol, zvlb[j]) + dvlb[j];
5902  if( SCIPisGE(scip, vlbsol, bestlbsol) )
5903  {
5904  bestlbsol = vlbsol;
5905  bestlbtype = j;
5906  }
5907  }
5908  }
5909 
5910  /* if no lb or vlb with binary variable was found, we have to abort */
5911  if( SCIPisInfinity(scip, -bestlbsol) )
5912  goto TERMINATE;
5913 
5914  if( bestlbtype == -1 )
5915  {
5916  rhs -= valscale * knapvals[i] * bestlbsol;
5917  SCIPdebugMessage(" -> non-binary variable %+.15g<%s>(%.15g) replaced with lower bound %.15g (rhs=%.15g)\n",
5918  valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var), SCIPvarGetLbGlobal(var), rhs);
5919  }
5920  else
5921  {
5922  assert(0 <= SCIPvarGetProbindex(zvlb[bestlbtype]) && SCIPvarGetProbindex(zvlb[bestlbtype]) < nbinvars);
5923  rhs -= valscale * knapvals[i] * dvlb[bestlbtype];
5924  binvals[SCIPvarGetProbindex(zvlb[bestlbtype])] += valscale * knapvals[i] * bvlb[bestlbtype];
5925 
5926  if( SCIPisInfinity(scip, REALABS(binvals[SCIPvarGetProbindex(zvlb[bestlbtype])])) )
5927  goto TERMINATE;
5928 
5929  if( !noknapsackconshdlr )
5930  {
5931  assert(tmpindices != NULL);
5932 
5933  tmpindices[tmp] = SCIPvarGetProbindex(zvlb[bestlbtype]);
5934  ++tmp;
5935  }
5936  SCIPdebugMessage(" -> non-binary variable %+.15g<%s>(%.15g) replaced with variable lower bound %+.15g<%s>(%.15g) %+.15g (rhs=%.15g)\n",
5937  valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var),
5938  bvlb[bestlbtype], SCIPvarGetName(zvlb[bestlbtype]),
5939  SCIPgetSolVal(scip, sol, zvlb[bestlbtype]), dvlb[bestlbtype], rhs);
5940  }
5941  }
5942  else
5943  {
5944  SCIP_VAR** zvub;
5945  SCIP_Real* bvub;
5946  SCIP_Real* dvub;
5947  SCIP_Real bestubsol;
5948  int bestubtype;
5949  int nvub;
5950  int j;
5951 
5952  assert(valscale * knapvals[i] < 0.0);
5953 
5954  /* a_j < 0: substitution with ub or vub */
5955  nvub = SCIPvarGetNVubs(var);
5956  zvub = SCIPvarGetVubVars(var);
5957  bvub = SCIPvarGetVubCoefs(var);
5958  dvub = SCIPvarGetVubConstants(var);
5959 
5960  /* search for ub or vub with minimal bound value */
5961  bestubsol = SCIPvarGetUbGlobal(var);
5962  bestubtype = -1;
5963  for( j = 0; j < nvub; j++ )
5964  {
5965  /* use only numerical stable vub with active binary variable z */
5966  if( SCIPvarIsBinary(zvub[j]) && SCIPvarIsActive(zvub[j]) && REALABS(bvub[j]) <= MAXABSVBCOEF )
5967  {
5968  SCIP_Real vubsol;
5969 
5970  if( (bvub[j] >= 0.0 && SCIPisLT(scip, bvub[j] * SCIPvarGetUbLocal(zvub[j]) + dvub[j], SCIPvarGetLbLocal(var))) ||
5971  (bvub[j] <= 0.0 && SCIPisLT(scip, bvub[j] * SCIPvarGetLbLocal(zvub[j]) + dvub[j], SCIPvarGetLbLocal(var))) )
5972  {
5973  *cutoff = TRUE;
5974  SCIPdebugMessage("variable bound <%s>[%g,%g] <= %g<%s>[%g,%g] + %g implies local cutoff\n",
5976  bvub[j], SCIPvarGetName(zvub[j]), SCIPvarGetLbLocal(zvub[j]), SCIPvarGetUbLocal(zvub[j]), dvub[j]);
5977  goto TERMINATE;
5978  }
5979 
5980  assert(0 <= SCIPvarGetProbindex(zvub[j]) && SCIPvarGetProbindex(zvub[j]) < nbinvars);
5981  vubsol = bvub[j] * SCIPgetSolVal(scip, sol, zvub[j]) + dvub[j];
5982  if( SCIPisLE(scip, vubsol, bestubsol) )
5983  {
5984  bestubsol = vubsol;
5985  bestubtype = j;
5986  }
5987  }
5988  }
5989 
5990  /* if no ub or vub with binary variable was found, we have to abort */
5991  if( SCIPisInfinity(scip, bestubsol) )
5992  goto TERMINATE;
5993 
5994  if( bestubtype == -1 )
5995  {
5996  rhs -= valscale * knapvals[i] * bestubsol;
5997  SCIPdebugMessage(" -> non-binary variable %+.15g<%s>(%.15g) replaced with upper bound %.15g (rhs=%.15g)\n",
5998  valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var), SCIPvarGetUbGlobal(var), rhs);
5999  }
6000  else
6001  {
6002  assert(0 <= SCIPvarGetProbindex(zvub[bestubtype]) && SCIPvarGetProbindex(zvub[bestubtype]) < nbinvars);
6003  rhs -= valscale * knapvals[i] * dvub[bestubtype];
6004  binvals[SCIPvarGetProbindex(zvub[bestubtype])] += valscale * knapvals[i] * bvub[bestubtype];
6005 
6006  if( SCIPisInfinity(scip, REALABS(binvals[SCIPvarGetProbindex(zvub[bestubtype])])) )
6007  goto TERMINATE;
6008 
6009  if( !noknapsackconshdlr )
6010  {
6011  assert(tmpindices != NULL);
6012 
6013  tmpindices[tmp] = SCIPvarGetProbindex(zvub[bestubtype]);
6014  ++tmp;
6015  }
6016  SCIPdebugMessage(" -> non-binary variable %+.15g<%s>(%.15g) replaced with variable upper bound %+.15g<%s>(%.15g) %+.15g (rhs=%.15g)\n",
6017  valscale * knapvals[i], SCIPvarGetName(var), SCIPgetSolVal(scip, sol, var),
6018  bvub[bestubtype], SCIPvarGetName(zvub[bestubtype]),
6019  SCIPgetSolVal(scip, sol, zvub[bestubtype]), dvub[bestubtype], rhs);
6020  }
6021  }
6022  }
6023 
6024  /* convert coefficients of all (now binary) variables to positive integers:
6025  * - make all coefficients integral
6026  * - make all coefficients positive (substitute negated variable)
6027  */
6028  nconsvars = 0;
6029 
6030  /* calculate scalar which makes all coefficients integral in relative allowed difference in between
6031  * -SCIPepsilon(scip) and KNAPSACKRELAX_MAXDELTA
6032  */
6034  KNAPSACKRELAX_MAXDNOM, KNAPSACKRELAX_MAXSCALE, &intscalar, &success) );
6035  SCIPdebugMessage(" -> intscalar = %.15g\n", intscalar);
6036 
6037  /* if coefficients cannot be made integral, we have to use a scalar of 1.0 and only round fractional coefficients down */
6038  if( !success )
6039  intscalar = 1.0;
6040 
6041  /* make all coefficients integral and positive:
6042  * - scale a~_j = a_j * intscalar
6043  * - substitute x~_j = 1 - x_j if a~_j < 0
6044  */
6045  rhs = rhs*intscalar;
6046 
6047  SCIPdebugMessage(" -> rhs = %.15g\n", rhs);
6048  minact = 0;
6049  maxact = 0;
6050  for( i = 0; i < nbinvars; i++ )
6051  {
6052  SCIP_VAR* var;
6053  SCIP_Longint val;
6054 
6055  val = (SCIP_Longint)SCIPfloor(scip, binvals[i]*intscalar);
6056  if( val == 0 )
6057  continue;
6058 
6059  if( val > 0 )
6060  {
6061  var = binvars[i];
6062  SCIPdebugMessage(" -> positive scaled binary variable %+"SCIP_LONGINT_FORMAT"<%s> (unscaled %.15g): not changed (rhs=%.15g)\n",
6063  val, SCIPvarGetName(var), binvals[i], rhs);
6064  }
6065  else
6066  {
6067  assert(val < 0);
6068 
6069  SCIP_CALL( SCIPgetNegatedVar(scip, binvars[i], &var) );
6070  val = -val;
6071  rhs += val;
6072  SCIPdebugMessage(" -> negative scaled binary variable %+"SCIP_LONGINT_FORMAT"<%s> (unscaled %.15g): substituted by (1 - <%s>) (rhs=%.15g)\n",
6073  -val, SCIPvarGetName(binvars[i]), binvals[i], SCIPvarGetName(var), rhs);
6074  }
6075 
6076  if( SCIPvarGetLbLocal(var) > 0.5 )
6077  minact += val;
6078  if( SCIPvarGetUbLocal(var) > 0.5 )
6079  maxact += val;
6080  consvals[nconsvars] = val;
6081  consvars[nconsvars] = var;
6082  nconsvars++;
6083  }
6084 
6085  if( nconsvars > 0 )
6086  {
6087  SCIP_Longint capacity;
6088 
6089  assert(consvars != NULL);
6090  assert(consvals != NULL);
6091  capacity = (SCIP_Longint)SCIPfeasFloor(scip, rhs);
6092 
6093 #ifdef SCIP_DEBUG
6094  {
6095  SCIP_Real act;
6096 
6097  SCIPdebugMessage(" -> linear constraint <%s> relaxed to knapsack:", cons != NULL ? SCIPconsGetName(cons) : "-");
6098  act = 0.0;
6099  for( i = 0; i < nconsvars; ++i )
6100  {
6101  SCIPdebugPrintf(" %+"SCIP_LONGINT_FORMAT"<%s>(%.15g)", consvals[i], SCIPvarGetName(consvars[i]),
6102  SCIPgetSolVal(scip, sol, consvars[i]));
6103  act += consvals[i] * SCIPgetSolVal(scip, sol, consvars[i]);
6104  }
6105  SCIPdebugPrintf(" <= %"SCIP_LONGINT_FORMAT" (%.15g) [act: %.15g, min: %"SCIP_LONGINT_FORMAT" max: %"SCIP_LONGINT_FORMAT"]\n",
6106  capacity, rhs, act, minact, maxact);
6107  }
6108 #endif
6109 
6110  if( minact > capacity )
6111  {
6112  SCIPdebugMessage("minactivity of knapsack relaxation implies local cutoff\n");
6113  *cutoff = TRUE;
6114  goto TERMINATE;
6115  }
6116 
6117  if( maxact > capacity )
6118  {
6119  /* separate lifted cut from relaxed knapsack constraint */
6120  SCIP_CALL( SCIPseparateKnapsackCuts(scip, cons, sepa, consvars, nconsvars, consvals, capacity, sol, usegubs, cutoff, ncuts) );
6121  }
6122  }
6123 
6124  TERMINATE:
6125  /* free data structures */
6126  if( noknapsackconshdlr)
6127  {
6128  SCIPfreeBufferArray(scip, &binvals);
6129  }
6130  else
6131  {
6132  /* clear binvals */
6133  for( --tmp; tmp >= 0; --tmp)
6134  {
6135  assert(tmpindices != NULL);
6136  binvals[tmpindices[tmp]] = 0;
6137  }
6138  SCIPfreeBufferArray(scip, &tmpindices);
6139  }
6140  SCIPfreeBufferArray(scip, &consvals);
6141  SCIPfreeBufferArray(scip, &consvars);
6142 
6143  return SCIP_OKAY;
6144 }
6145 
6146 /** separates given knapsack constraint */
6147 static
6149  SCIP* scip, /**< SCIP data structure */
6150  SCIP_CONS* cons, /**< knapsack constraint */
6151  SCIP_SOL* sol, /**< primal SCIP solution, NULL for current LP solution */
6152  SCIP_Bool sepacuts, /**< should knapsack cuts be separated? */
6153  SCIP_Bool usegubs, /**< should GUB information be used for separation? */
6154  SCIP_Bool* cutoff, /**< whether a cutoff has been detected */
6155  int* ncuts /**< pointer to add up the number of found cuts */
6156  )
6157 {
6158  SCIP_CONSDATA* consdata;
6159  SCIP_Bool violated;
6160 
6161  assert(ncuts != NULL);
6162  assert(cutoff != NULL);
6163  *cutoff = FALSE;
6164 
6165  consdata = SCIPconsGetData(cons);
6166  assert(consdata != NULL);
6167 
6168  SCIPdebugMessage("separating knapsack constraint <%s>\n", SCIPconsGetName(cons));
6169 
6170  /* check knapsack constraint itself for feasibility */
6171  SCIP_CALL( checkCons(scip, cons, sol, (sol != NULL), FALSE, &violated) );
6172 
6173  if( violated )
6174  {
6175  /* add knapsack constraint as LP row to the LP */
6176  SCIP_CALL( addRelaxation(scip, cons, sol, cutoff) );
6177  (*ncuts)++;
6178  }
6179  else if( sepacuts )
6180  {
6181  SCIP_CALL( SCIPseparateKnapsackCuts(scip, cons, NULL, consdata->vars, consdata->nvars, consdata->weights,
6182  consdata->capacity, sol, usegubs, cutoff, ncuts) );
6183  }
6184 
6185  return SCIP_OKAY;
6186 }
6187 
6188 /** adds coefficient to constraint data */
6189 static
6191  SCIP* scip, /**< SCIP data structure */
6192  SCIP_CONS* cons, /**< knapsack constraint */
6193  SCIP_VAR* var, /**< variable to add to knapsack */
6194  SCIP_Longint weight /**< weight of variable in knapsack */
6195  )
6196 {
6197  SCIP_CONSDATA* consdata;
6198 
6199  consdata = SCIPconsGetData(cons);
6200  assert(consdata != NULL);
6201  assert(SCIPvarIsBinary(var));
6202  assert(weight > 0);
6203 
6204  /* add the new coefficient to the LP row */
6205  if( consdata->row != NULL )
6206  {
6207  SCIP_CALL( SCIPaddVarToRow(scip, consdata->row, var, (SCIP_Real)weight) );
6208  }
6209 
6210  /* check for fixed variable */
6211  if( SCIPvarGetLbGlobal(var) > 0.5 )
6212  {
6213  /* variable is fixed to one: reduce capacity */
6214  consdata->capacity -= weight;
6215  }
6216  else if( SCIPvarGetUbGlobal(var) > 0.5 )
6217  {
6218  SCIP_Bool negated;
6219 
6220  /* get binary representative of variable */
6221  SCIP_CALL( SCIPgetBinvarRepresentative(scip, var, &var, &negated) );
6222 
6223  /* insert coefficient */
6224  SCIP_CALL( consdataEnsureVarsSize(scip, consdata, consdata->nvars+1, SCIPconsIsTransformed(cons)) );
6225  consdata->vars[consdata->nvars] = var;
6226  consdata->weights[consdata->nvars] = weight;
6227  consdata->nvars++;
6228 
6229  /* capture variable */
6230  SCIP_CALL( SCIPcaptureVar(scip, var) );
6231 
6232  /* install the rounding locks of variable */
6233  SCIP_CALL( lockRounding(scip, cons, var) );
6234 
6235  /* catch events */
6236  if( SCIPconsIsTransformed(cons) )
6237  {
6238  SCIP_CONSHDLRDATA* conshdlrdata;
6239 
6240  conshdlrdata = SCIPconshdlrGetData(SCIPconsGetHdlr(cons));
6241  assert(conshdlrdata != NULL);
6242  SCIP_CALL( eventdataCreate(scip, &consdata->eventdatas[consdata->nvars-1], consdata, weight) );
6243  SCIP_CALL( SCIPcatchVarEvent(scip, var,
6246  conshdlrdata->eventhdlr, consdata->eventdatas[consdata->nvars-1],
6247  &consdata->eventdatas[consdata->nvars-1]->filterpos) );
6248 
6249  if( !consdata->existmultaggr && SCIPvarGetStatus(SCIPvarGetProbvar(var)) == SCIP_VARSTATUS_MULTAGGR )
6250  consdata->existmultaggr = TRUE;
6251  }
6252 
6253  /* update weight sums */
6254  consdata->weightsum += weight;
6255  if( SCIPvarGetLbLocal(var) > 0.5 )
6256  consdata->onesweightsum += weight;
6257 
6258  consdata->sorted = FALSE;
6259  consdata->cliquepartitioned = FALSE;
6260  consdata->negcliquepartitioned = FALSE;
6261  consdata->merged = FALSE;
6262  }
6263  consdata->propagated = FALSE;
6264  consdata->presolved = FALSE;
6265  consdata->cliquesadded = FALSE; /* new coefficient might lead to larger cliques */
6266 
6267  return SCIP_OKAY;
6268 }
6269 
6270 /** deletes coefficient at given position from constraint data */
6271 static
6273  SCIP* scip, /**< SCIP data structure */
6274  SCIP_CONS* cons, /**< knapsack constraint */
6275  int pos /**< position of coefficient to delete */
6276  )
6277 {
6278  SCIP_CONSDATA* consdata;
6279  SCIP_VAR* var;
6280 
6281  consdata = SCIPconsGetData(cons);
6282  assert(consdata != NULL);
6283  assert(0 <= pos && pos < consdata->nvars);
6284 
6285  var = consdata->vars[pos];
6286  assert(var != NULL);
6287  assert(SCIPconsIsTransformed(cons) == SCIPvarIsTransformed(var));
6288 
6289  /* delete the coefficient from the LP row */
6290  if( consdata->row != NULL )
6291  {
6292  SCIP_CALL( SCIPaddVarToRow(scip, consdata->row, var, -(SCIP_Real)consdata->weights[pos]) );
6293  }
6294 
6295  /* remove the rounding locks of variable */
6296  SCIP_CALL( unlockRounding(scip, cons, var) );
6297 
6298  /* drop events */
6299  if( SCIPconsIsTransformed(cons) )
6300  {
6301  SCIP_CONSHDLRDATA* conshdlrdata;
6302 
6303  conshdlrdata = SCIPconshdlrGetData(SCIPconsGetHdlr(cons));
6304  assert(conshdlrdata != NULL);
6305  SCIP_CALL( SCIPdropVarEvent(scip, var,
6308  conshdlrdata->eventhdlr, consdata->eventdatas[pos], consdata->eventdatas[pos]->filterpos) );
6309  SCIP_CALL( eventdataFree(scip, &consdata->eventdatas[pos]) );
6310  }
6311 
6312  /* update weight sums */
6313  consdata->weightsum -= consdata->weights[pos];
6314  if( SCIPvarGetLbLocal(var) > 0.5 )
6315  consdata->onesweightsum -= consdata->weights[pos];
6316  assert(consdata->weightsum >= 0);
6317  assert(consdata->onesweightsum >= 0);
6318 
6319  /* move the last variable to the free slot */
6320  consdata->vars[pos] = consdata->vars[consdata->nvars-1];
6321  consdata->weights[pos] = consdata->weights[consdata->nvars-1];
6322  if( consdata->eventdatas != NULL )
6323  consdata->eventdatas[pos] = consdata->eventdatas[consdata->nvars-1];
6324 
6325  /* release variable */
6326  SCIP_CALL( SCIPreleaseVar(scip, &var) );
6327 
6328  consdata->propagated = FALSE;
6329  consdata->presolved = FALSE;
6330  consdata->sorted = (consdata->sorted && pos == consdata->nvars - 1);
6331 
6332  /* try to use old clique partitions */
6333  if( consdata->cliquepartitioned )
6334  {
6335  assert(consdata->cliquepartition != NULL);
6336  /* if the clique number is equal to the number of variables we have only cliques with one element, so we don't
6337  * change the clique number */
6338  if( consdata->cliquepartition[consdata->nvars - 1] != consdata->nvars - 1 )
6339  {
6340  int oldcliqenum;
6341 
6342  oldcliqenum = consdata->cliquepartition[pos];
6343  consdata->cliquepartition[pos] = consdata->cliquepartition[consdata->nvars-1];
6344 
6345  /* the following if and else cases assure that we have increasing clique numbers */
6346  if( consdata->cliquepartition[pos] > pos )
6347  consdata->cliquepartitioned = FALSE; /* recalculate the clique partition after a coefficient was removed */
6348  else
6349  {
6350  int i;
6351  int cliquenumbefore;
6352 
6353  /* if the old clique number was greater than the new one we have to check that before a bigger clique number
6354  * occurs the same as the old one is still in the cliquepartition */
6355  if( oldcliqenum > consdata->cliquepartition[pos] )
6356  {
6357  for( i = 0; i < consdata->nvars; ++i )
6358  if( oldcliqenum == consdata->cliquepartition[i] )
6359  break;
6360  else if( oldcliqenum < consdata->cliquepartition[i] )
6361  {
6362  consdata->cliquepartitioned = FALSE; /* recalculate the clique partition after a coefficient was removed */
6363  break;
6364  }
6365  /* if we reached the end in the for loop, it means we have deleted the last element of the clique with
6366  * the biggest index, so decrease the number of cliques
6367  */
6368  if( i == consdata->nvars )
6369  --(consdata->ncliques);
6370  }
6371  /* if the old clique number was smaller than the new one we have to check the front for an element with
6372  * clique number minus 1 */
6373  else if( oldcliqenum < consdata->cliquepartition[pos] )
6374  {
6375  cliquenumbefore = consdata->cliquepartition[pos] - 1;
6376  for( i = pos - 1; i >= 0 && i >= cliquenumbefore && consdata->cliquepartition[i] < cliquenumbefore; --i ); /*lint !e722*/
6377 
6378  if( i < cliquenumbefore )
6379  consdata->cliquepartitioned = FALSE; /* recalculate the clique partition after a coefficient was removed */
6380  }
6381  /* if we deleted the last element of the clique with biggest index, we have to decrease the clique number */
6382  else if( pos == consdata->nvars - 1)
6383  {
6384  cliquenumbefore = consdata->cliquepartition[pos];
6385  for( i = pos - 1; i >= 0 && i >= cliquenumbefore && consdata->cliquepartition[i] < cliquenumbefore; --i ); /*lint !e722*/
6386 
6387  if( i < cliquenumbefore )
6388  --(consdata->ncliques);
6389  }
6390  /* if the old clique number is equal to the new one the cliquepartition should be ok */
6391  }
6392  }
6393  else
6394  --(consdata->ncliques);
6395  }
6396 
6397  if( consdata->negcliquepartitioned )
6398  {
6399  assert(consdata->negcliquepartition != NULL);
6400  /* if the clique number is equal to the number of variables we have only cliques with one element, so we don't
6401  * change the clique number */
6402  if( consdata->negcliquepartition[consdata->nvars-1] != consdata->nvars - 1 )
6403  {
6404  int oldcliqenum;
6405 
6406  oldcliqenum = consdata->negcliquepartition[pos];
6407  consdata->negcliquepartition[pos] = consdata->negcliquepartition[consdata->nvars-1];
6408 
6409  /* the following if and else cases assure that we have increasing clique numbers */
6410  if( consdata->negcliquepartition[pos] > pos )
6411  consdata->negcliquepartitioned = FALSE; /* recalculate the clique partition after a coefficient was removed */
6412  else
6413  {
6414  int i;
6415  int cliquenumbefore;
6416 
6417  /* if the old clique number was greater than the new one we have to check that, before a bigger clique number
6418  * occurs, the same as the old one occurs */
6419  if( oldcliqenum > consdata->negcliquepartition[pos] )
6420  {
6421  for( i = 0; i < consdata->nvars; ++i )
6422  if( oldcliqenum == consdata->negcliquepartition[i] )
6423  break;
6424  else if( oldcliqenum < consdata->negcliquepartition[i] )
6425  {
6426  consdata->negcliquepartitioned = FALSE; /* recalculate the negated clique partition after a coefficient was removed */
6427  break;
6428  }
6429  /* if we reached the end in the for loop, it means we have deleted the last element of the clique with
6430  * the biggest index, so decrease the number of negated cliques
6431  */
6432  if( i == consdata->nvars )
6433  --(consdata->nnegcliques);
6434  }
6435  /* if the old clique number was smaller than the new one we have to check the front for an element with
6436  * clique number minus 1 */
6437  else if( oldcliqenum < consdata->negcliquepartition[pos] )
6438  {
6439  cliquenumbefore = consdata->negcliquepartition[pos] - 1;
6440  for( i = pos - 1; i >= 0 && i >= cliquenumbefore && consdata->negcliquepartition[i] < cliquenumbefore; --i ); /*lint !e722*/
6441 
6442  if( i < cliquenumbefore )
6443  consdata->negcliquepartitioned = FALSE; /* recalculate the negated clique partition after a coefficient was removed */
6444  }
6445  /* if we deleted the last element of the clique with biggest index, we have to decrease the clique number */
6446  else if( pos == consdata->nvars - 1)
6447  {
6448  cliquenumbefore = consdata->negcliquepartition[pos];
6449  for( i = pos - 1; i >= 0 && i >= cliquenumbefore && consdata->negcliquepartition[i] < cliquenumbefore; --i ); /*lint !e722*/
6450 
6451  if( i < cliquenumbefore )
6452  --(consdata->nnegcliques);
6453  }
6454  /* otherwise if the old clique number is equal to the new one the cliquepartition should be ok */
6455  }
6456  }
6457  else
6458  --(consdata->nnegcliques);
6459  }
6460 
6461  --(consdata->nvars);
6462 
6463  return SCIP_OKAY;
6464 }
6465 
6466 /** removes all items with weight zero from knapsack constraint */
6467 static
6469  SCIP* scip, /**< SCIP data structure */
6470  SCIP_CONS* cons /**< knapsack constraint */
6471  )
6472 {
6473  SCIP_CONSDATA* consdata;
6474  int v;
6475 
6476  consdata = SCIPconsGetData(cons);
6477  assert(consdata != NULL);
6478 
6479  for( v = consdata->nvars-1; v >= 0; --v )
6480  {
6481  if( consdata->weights[v] == 0 )
6482  {
6483  SCIP_CALL( delCoefPos(scip, cons, v) );
6484  }
6485  }
6486 
6487  return SCIP_OKAY;
6488 }
6489 
6490 /* perform deletion of variables in all constraints of the constraint handler */
6491 static
6493  SCIP* scip, /**< SCIP data structure */
6494  SCIP_CONSHDLR* conshdlr, /**< constraint handler */
6495  SCIP_CONS** conss, /**< array of constraints */
6496  int nconss /**< number of constraints */
6497  )
6498 {
6499  SCIP_CONSDATA* consdata;
6500  int i;
6501  int v;
6502 
6503  assert(scip != NULL);
6504  assert(conshdlr != NULL);
6505  assert(conss != NULL);
6506  assert(nconss >= 0);
6507  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
6508 
6509  /* iterate over all constraints */
6510  for( i = 0; i < nconss; i++ )
6511  {
6512  consdata = SCIPconsGetData(conss[i]);
6513 
6514  /* constraint is marked, that some of its variables were deleted */
6515  if( consdata->varsdeleted )
6516  {
6517  /* iterate over all variables of the constraint and delete them from the constraint */
6518  for( v = consdata->nvars - 1; v >= 0; --v )
6519  {
6520  if( SCIPvarIsDeleted(consdata->vars[v]) )
6521  {
6522  SCIP_CALL( delCoefPos(scip, conss[i], v) );
6523  }
6524  }
6525  consdata->varsdeleted = FALSE;
6526  }
6527  }
6528 
6529  return SCIP_OKAY;
6530 }
6531 
6532 /** replaces multiple occurrences of a variable or its negation by a single coefficient */
6533 static
6535  SCIP* scip, /**< SCIP data structure */
6536  SCIP_CONS* cons, /**< knapsack constraint */
6537  SCIP_Bool* cutoff /**< pointer to store whether the node can be cut off */
6538  )
6539 {
6540  SCIP_CONSDATA* consdata;
6541  int v;
6542  int prev;
6543 
6544  assert(scip != NULL);
6545  assert(cons != NULL);
6546  assert(cutoff != NULL);
6547 
6548  consdata = SCIPconsGetData(cons);
6549  assert(consdata != NULL);
6550 
6551  *cutoff = FALSE;
6552 
6553  if( consdata->merged )
6554  return SCIP_OKAY;
6555 
6556  if( consdata->nvars <= 1 )
6557  {
6558  consdata->merged = TRUE;
6559  return SCIP_OKAY;
6560  }
6561 
6562  assert(consdata->vars != NULL || consdata->nvars == 0);
6563 
6564  /* sorting array after indices of variables, that's only for faster merging */
6565  SCIPsortPtrPtrLongIntInt((void**)consdata->vars, (void**)consdata->eventdatas, consdata->weights,
6566  consdata->cliquepartition, consdata->negcliquepartition, SCIPvarCompActiveAndNegated, consdata->nvars);
6567 
6568  /* knapsack-sorting (decreasing weights) now lost */
6569  consdata->sorted = FALSE;
6570 
6571  v = consdata->nvars - 1;
6572  prev = v - 1;
6573  /* loop backwards through the items: deletion only affects rear items */
6574  while( prev >= 0 )
6575  {
6576  SCIP_VAR* var1;
6577  SCIP_VAR* var2;
6578  SCIP_Bool negated1;
6579  SCIP_Bool negated2;
6580 
6581  negated1 = FALSE;
6582  negated2 = FALSE;
6583 
6584  var1 = consdata->vars[v];
6585  assert(SCIPvarIsBinary(var1));
6586  assert(SCIPvarIsActive(var1) || SCIPvarGetStatus(var1) == SCIP_VARSTATUS_NEGATED);
6588  {
6589  var1 = SCIPvarGetNegatedVar(var1);
6590  negated1 = TRUE;
6591  }
6592  assert(var1 != NULL);
6593 
6594  var2 = consdata->vars[prev];
6595  assert(SCIPvarIsBinary(var2));
6596  assert(SCIPvarIsActive(var2) || SCIPvarGetStatus(var2) == SCIP_VARSTATUS_NEGATED);
6598  {
6599  var2 = SCIPvarGetNegatedVar(var2);
6600  negated2 = TRUE;
6601  }
6602  assert(var2 != NULL);
6603 
6604  if( var1 == var2 )
6605  {
6606  /* both variables are either active or negated */
6607  if( negated1 == negated2 )
6608  {
6609  /* variables var1 and var2 are equal: add weight of var1 to var2, and delete var1 */
6610  consdataChgWeight(consdata, prev, consdata->weights[v] + consdata->weights[prev]);
6611  SCIP_CALL( delCoefPos(scip, cons, v) );
6612  }
6613  /* variables var1 and var2 are opposite: subtract smaller weight from larger weight, reduce capacity,
6614  * and delete item of smaller weight
6615  */
6616  else if( consdata->weights[v] == consdata->weights[prev] )
6617  {
6618  /* both variables eliminate themselves: w*x + w*(1-x) == w */
6619  consdata->capacity -= consdata->weights[v];
6620  SCIP_CALL( delCoefPos(scip, cons, v) ); /* this does not affect var2, because var2 stands before var1 */
6621  SCIP_CALL( delCoefPos(scip, cons, prev) );
6622 
6623  --prev;
6624  }
6625  else if( consdata->weights[v] < consdata->weights[prev] )
6626  {
6627  consdata->capacity -= consdata->weights[v];
6628  consdataChgWeight(consdata, prev, consdata->weights[prev] - consdata->weights[v]);
6629  assert(consdata->weights[prev] > 0);
6630  SCIP_CALL( delCoefPos(scip, cons, v) ); /* this does not affect var2, because var2 stands before var1 */
6631  }
6632  else
6633  {
6634  consdata->capacity -= consdata->weights[prev];
6635  consdataChgWeight(consdata, v, consdata->weights[v] - consdata->weights[prev]);
6636  assert(consdata->weights[v] > 0);
6637  SCIP_CALL( delCoefPos(scip, cons, prev) ); /* attention: normally we lose our order */
6638  /* restore order iff necessary */
6639  if( consdata->nvars != v ) /* otherwise the order still stands */
6640  {
6641  assert(prev == 0 || ((prev > 0) && (SCIPvarIsActive(consdata->vars[prev - 1]) || SCIPvarGetStatus(consdata->vars[prev - 1]) == SCIP_VARSTATUS_NEGATED)) );
6642  /* either that was the last pair or both, the negated and "normal" variable in front doesn't match var1, so the order is irrelevant */
6643  if( prev == 0 || (var1 != consdata->vars[prev - 1] && var1 != SCIPvarGetNegatedVar(consdata->vars[prev - 1])) )
6644  --prev;
6645  else /* we need to let v at the same position*/
6646  {
6647  consdata->cliquesadded = FALSE; /* reduced capacity might lead to larger cliques */
6648  /* don't decrease v, the same variable may exist up front */
6649  --prev;
6650  continue;
6651  }
6652  }
6653  }
6654  consdata->cliquesadded = FALSE; /* reduced capacity might lead to larger cliques */
6655  }
6656  v = prev;
6657  --prev;
6658  }
6659 
6660  consdata->merged = TRUE;
6661 
6662  /* check infeasibility */
6663  if( consdata->onesweightsum > consdata->capacity )
6664  {
6665  SCIPdebugMessage("merge multiples detected cutoff.\n");
6666  *cutoff = TRUE;
6667  return SCIP_OKAY;
6668  }
6669 
6670  return SCIP_OKAY;
6671 }
6672 
6673 /** in case the knapsack constraint is independent of every else, solve the knapsack problem (exactly) and apply the
6674  * fixings (dual reductions)
6675  */
6676 static
6678  SCIP* scip, /**< SCIP data structure */
6679  SCIP_CONS* cons, /**< knapsack constraint */
6680  int* nfixedvars, /**< pointer to count number of fixings */
6681  int* ndelconss, /**< pointer to count number of deleted constraints */
6682  SCIP_Bool* deleted /**< pointer to store if the constraint is deleted */
6683  )
6684 {
6685  SCIP_CONSDATA* consdata;
6686  SCIP_VAR** vars;
6687  SCIP_Real* profits;
6688  int* solitems;
6689  int* nonsolitems;
6690  int* items;
6691  SCIP_Real solval;
6692  SCIP_Bool infeasible;
6693  SCIP_Bool tightened;
6694  SCIP_Bool applicable;
6695  int nsolitems;
6696  int nnonsolitems;
6697  int nvars;
6698  int v;
6699 
6700  assert(!SCIPconsIsModifiable(cons));
6701 
6702  /* constraints for which the check flag is set to FALSE, did not contribute to the lock numbers; therefore, we cannot
6703  * use the locks to decide for a dual reduction using this constraint; for example after a restart the cuts which are
6704  * added to the problems have the check flag set to FALSE
6705  */
6706  if( !SCIPconsIsChecked(cons) )
6707  return SCIP_OKAY;
6708 
6709  consdata = SCIPconsGetData(cons);
6710  assert(consdata != NULL);
6711 
6712  nvars = consdata->nvars;
6713  vars = consdata->vars;
6714 
6715  SCIP_CALL( SCIPallocBufferArray(scip, &profits, nvars) );
6716  SCIP_CALL( SCIPallocBufferArray(scip, &items, nvars) );
6717  SCIP_CALL( SCIPallocBufferArray(scip, &solitems, nvars) );
6718  SCIP_CALL( SCIPallocBufferArray(scip, &nonsolitems, nvars) );
6719 
6720  applicable = TRUE;
6721 
6722  /* check if we can apply the dual reduction; this can be done if the knapsack has the only locks on this constraint;
6723  * collect object values which are the profits of the knapsack problem
6724  */
6725  for( v = 0; v < nvars; ++v )
6726  {
6727  SCIP_VAR* var;
6728  SCIP_Bool negated;
6729 
6730  var = vars[v];
6731  assert(var != NULL);
6732 
6733  /* the variable should not be (globally) fixed */
6734  assert(SCIPvarGetLbGlobal(var) < 0.5 && SCIPvarGetUbGlobal(var) > 0.5);
6735 
6736  if( SCIPvarGetNLocksDown(var) > 0 || SCIPvarGetNLocksUp(var) > 1 )
6737  {
6738  applicable = FALSE;
6739  break;
6740  }
6741 
6742  negated = FALSE;
6743 
6744  /* get the active variable */
6745  SCIP_CALL( SCIPvarGetProbvarBinary(&var, &negated) );
6746  assert(SCIPvarIsActive(var));
6747 
6748  if( negated )
6749  profits[v] = SCIPvarGetObj(var);
6750  else
6751  profits[v] = -SCIPvarGetObj(var);
6752 
6753  SCIPdebugMessage("variable <%s> -> item size %"SCIP_LONGINT_FORMAT", profit <%g>\n",
6754  SCIPvarGetName(vars[v]), consdata->weights[v], profits[v]);
6755  items[v] = v;
6756  }
6757 
6758  if( applicable )
6759  {
6760  SCIP_Bool success;
6761 
6762  SCIPdebugMessage("the knapsack constraint <%s> is independent to rest of the problem\n", SCIPconsGetName(cons));
6763  SCIPdebugPrintCons(scip, cons, NULL);
6764 
6765  /* solve knapsack problem exactly */
6766  SCIP_CALL( SCIPsolveKnapsackExactly(scip, consdata->nvars, consdata->weights, profits, consdata->capacity,
6767  items, solitems, nonsolitems, &nsolitems, &nnonsolitems, &solval, &success) );
6768 
6769  if( success )
6770  {
6771  SCIP_VAR* var;
6772 
6773  /* apply solution of the knapsack as dual reductions */
6774  for( v = 0; v < nsolitems; ++v )
6775  {
6776  var = vars[solitems[v]];
6777  assert(var != NULL);
6778 
6779  SCIPdebugMessage("variable <%s> only locked up in knapsack constraints: dual presolve <%s>[%.15g,%.15g] >= 1.0\n",
6781  SCIP_CALL( SCIPtightenVarLb(scip, var, 1.0, TRUE, &infeasible, &tightened) );
6782  assert(!infeasible);
6783  assert(tightened);
6784  (*nfixedvars)++;
6785  }
6786 
6787  for( v = 0; v < nnonsolitems; ++v )
6788  {
6789  var = vars[nonsolitems[v]];
6790  assert(var != NULL);
6791 
6792  SCIPdebugMessage("variable <%s> has no down locks: dual presolve <%s>[%.15g,%.15g] <= 0.0\n",
6794  SCIP_CALL( SCIPtightenVarUb(scip, var, 0.0, TRUE, &infeasible, &tightened) );
6795  assert(!infeasible);
6796  assert(tightened);
6797  (*nfixedvars)++;
6798  }
6799 
6800  SCIP_CALL( SCIPdelCons(scip, cons) );
6801  (*ndelconss)++;
6802  (*deleted) = TRUE;
6803  }
6804  }
6805 
6806  SCIPfreeBufferArray(scip, &nonsolitems);
6807  SCIPfreeBufferArray(scip, &solitems);
6808  SCIPfreeBufferArray(scip, &items);
6809  SCIPfreeBufferArray(scip, &profits);
6810 
6811  return SCIP_OKAY;
6812 }
6813 
6814 /** check if the knapsack constraint is parallel to objective function; if so update the cutoff bound and avoid that the
6815  * constraint enters the LP by setting the initial and separated flag to FALSE
6816  */
6817 static
6819  SCIP* scip, /**< SCIP data structure */
6820  SCIP_CONS* cons, /**< knapsack constraint */
6821  SCIP_CONSHDLRDATA* conshdlrdata /**< knapsack constraint handler data */
6822  )
6823 {
6824  SCIP_CONSDATA* consdata;
6825  SCIP_VAR** vars;
6826  SCIP_VAR* var;
6827  SCIP_Real offset;
6828  SCIP_Real scale;
6829  SCIP_Real objval;
6830  SCIP_Bool applicable;
6831  SCIP_Bool negated;
6832  int nobjvars;
6833  int nvars;
6834  int v;
6835 
6836  assert(scip != NULL);
6837  assert(cons != NULL);
6838  assert(conshdlrdata != NULL);
6839 
6840  consdata = SCIPconsGetData(cons);
6841  assert(consdata != NULL);
6842 
6843  nvars = consdata->nvars;
6844  nobjvars = SCIPgetNObjVars(scip);
6845 
6846  /* check if the knapsack constraints has the same number of variables as the objective function and if the initial
6847  * and/or separated flag is set to FALSE
6848  */
6849  if( nvars != nobjvars || (!SCIPconsIsInitial(cons) && !SCIPconsIsSeparated(cons)) )
6850  return SCIP_OKAY;
6851 
6852  vars = consdata->vars;
6853  applicable = TRUE;
6854  offset = 0.0;
6855  scale = 1.0;
6856 
6857  for( v = 0; v < nvars && applicable; ++v )
6858  {
6859  negated = FALSE;
6860  var = vars[v];
6861  assert(vars != NULL);
6862 
6863  if( SCIPvarIsNegated(var) )
6864  {
6865  negated = TRUE;
6866  var = SCIPvarGetNegatedVar(var);
6867  assert(var != NULL);
6868  }
6869 
6870  objval = SCIPvarGetObj(var);
6871 
6872  /* if a variable has a zero objective coefficient the knapsack constraint is not parallel to objective function */
6873  if( SCIPisZero(scip, objval) )
6874  applicable = FALSE;
6875  else
6876  {
6877  SCIP_Real weight;
6878 
6879  weight = (SCIP_Real)consdata->weights[v];
6880 
6881  if( negated )
6882  {
6883  if( v == 0 )
6884  {
6885  /* the first variable defines the scale */
6886  scale = weight / -objval;
6887 
6888  offset += weight;
6889  }
6890  else if( SCIPisEQ(scip, -objval * scale, weight) )
6891  offset += weight;
6892  else
6893  applicable = FALSE;
6894  }
6895  else if( v == 0 )
6896  {
6897  /* the first variable define the scale */
6898  scale = weight / objval;
6899  }
6900  else if( !SCIPisEQ(scip, objval * scale, weight) )
6901  applicable = FALSE;
6902  }
6903  }
6904 
6905  if( applicable )
6906  {
6907  if( SCIPisPositive(scip, scale) && conshdlrdata->detectcutoffbound )
6908  {
6909  SCIP_Real cutoffbound;
6910 
6911  /* avoid that the knapsack constraint enters the LP since it is parallel to the objective function */
6912  SCIP_CALL( SCIPsetConsInitial(scip, cons, FALSE) );
6913  SCIP_CALL( SCIPsetConsSeparated(scip, cons, FALSE) );
6914 
6915  cutoffbound = (consdata->capacity - offset) / scale;
6916 
6917  /* increase the cutoff bound value by an epsilon to ensue that solution with the value of the cutoff bound are
6918  * still excepted
6919  */
6920  cutoffbound += SCIPcutoffbounddelta(scip);
6921 
6922  SCIPdebugMessage("constraint <%s> is parallel to objective function and provids a cutoff bound <%g>\n",
6923  SCIPconsGetName(cons), cutoffbound);
6924 
6925  if( cutoffbound < SCIPgetCutoffbound(scip) )
6926  {
6927  SCIPdebugMessage("update cutoff bound <%g>\n", cutoffbound);
6928 
6929  SCIP_CALL( SCIPupdateCutoffbound(scip, cutoffbound) );
6930  }
6931  else
6932  {
6933  /* in case the cutoff bound is worse then currently known one we avoid additionaly enforcement and
6934  * propagation
6935  */
6936  SCIP_CALL( SCIPsetConsEnforced(scip, cons, FALSE) );
6937  SCIP_CALL( SCIPsetConsPropagated(scip, cons, FALSE) );
6938  }
6939  }
6940  else if( SCIPisNegative(scip, scale) && conshdlrdata->detectlowerbound )
6941  {
6942  SCIP_Real lowerbound;
6943 
6944  /* avoid that the knapsack constraint enters the LP since it is parallel to the objective function */
6945  SCIP_CALL( SCIPsetConsInitial(scip, cons, FALSE) );
6946  SCIP_CALL( SCIPsetConsSeparated(scip, cons, FALSE) );
6947 
6948  lowerbound = (consdata->capacity - offset) / scale;
6949 
6950  SCIPdebugMessage("constraint <%s> is parallel to objective function and provids a lower bound <%g>\n",
6951  SCIPconsGetName(cons), lowerbound);
6952 
6953  SCIP_CALL( SCIPupdateLocalLowerbound(scip, lowerbound) );
6954  }
6955  }
6956 
6957  return SCIP_OKAY;
6958 }
6959 
6960 /** sort the variables and weights w.r.t. the clique partition; thereby ensure the current order of the variables when a
6961  * weight of one variable is greater or equal another weight and both variables are in the same cliques */
6962 static
6964  SCIP* scip, /**< SCIP data structure */
6965  SCIP_CONSDATA* consdata, /**< knapsack constraint data */
6966  SCIP_VAR** vars, /**< array for sorted variables */
6967  SCIP_Longint* weights, /**< array for sorted weights */
6968  int* cliquestartposs, /**< starting position array for each clique */
6969  SCIP_Bool usenegatedclique /**< should negated or normal clique partition be used */
6970  )
6971 {
6972  SCIP_VAR** origvars;
6973  int norigvars;
6974  SCIP_Longint* origweights;
6975  int* cliquepartition;
6976  int ncliques;
6977 
6978  SCIP_VAR*** varpointers;
6979  SCIP_Longint** weightpointers;
6980  int* cliquecount;
6981 
6982  int nextpos;
6983  int c;
6984  int v;
6985 
6986  assert(scip != NULL);
6987  assert(consdata != NULL);
6988  assert(vars != NULL);
6989  assert(weights != NULL);
6990  assert(cliquestartposs != NULL);
6991 
6992  origweights = consdata->weights;
6993  origvars = consdata->vars;
6994  norigvars = consdata->nvars;
6995 
6996  assert(origvars != NULL || norigvars == 0);
6997  assert(origweights != NULL || norigvars == 0);
6998 
6999  if( norigvars == 0 )
7000  return SCIP_OKAY;
7001 
7002  if( usenegatedclique )
7003  {
7004  assert(consdata->negcliquepartitioned);
7005 
7006  cliquepartition = consdata->negcliquepartition;
7007  ncliques = consdata->nnegcliques;
7008  }
7009  else
7010  {
7011  assert(consdata->cliquepartitioned);
7012 
7013  cliquepartition = consdata->cliquepartition;
7014  ncliques = consdata->ncliques;
7015  }
7016 
7017  assert(cliquepartition != NULL);
7018  assert(ncliques > 0);
7019 
7020  /* we first count all clique items and alloc temporary memory for a bucket sort */
7021  SCIP_CALL( SCIPallocBufferArray(scip, &cliquecount, ncliques) );
7022  BMSclearMemoryArray(cliquecount, ncliques);
7023 
7024  /* first we count for each clique the number of elements */
7025  for( v = norigvars - 1; v >= 0; --v )
7026  {
7027  assert(0 <= cliquepartition[v] && cliquepartition[v] < ncliques);
7028  ++(cliquecount[cliquepartition[v]]);
7029  }
7030 
7031  /*@todo: maybe it is better to put largest cliques up front */
7032 
7033 #ifndef NDEBUG
7034  BMSclearMemoryArray(vars, norigvars);
7035  BMSclearMemoryArray(weights, norigvars);
7036 #endif
7037  SCIP_CALL( SCIPallocBufferArray(scip, &varpointers, ncliques) );
7038  SCIP_CALL( SCIPallocBufferArray(scip, &weightpointers, ncliques) );
7039 
7040  nextpos = 0;
7041  /* now we initialize all start pointers for each clique, so they will be ordered */
7042  for( c = 0; c < ncliques; ++c )
7043  {
7044  /* to reach the goal that all variables of each clique will be standing next to each other we will initialize the
7045  * starting pointers for each clique by adding the number of each clique to the last clique starting pointer
7046  * e.g. clique1 has 4 elements and clique2 has 3 elements the the starting pointer for clique1 will be the pointer
7047  * to vars[0], the starting pointer to clique2 will be the pointer to vars[4] and to clique3 it will be
7048  * vars[7]
7049  *
7050  */
7051  varpointers[c] = (SCIP_VAR**) (vars + nextpos);
7052  cliquestartposs[c] = nextpos;
7053  weightpointers[c] = (SCIP_Longint*) (weights + nextpos);
7054  assert(cliquecount[c] > 0);
7055  nextpos += cliquecount[c];
7056  assert(nextpos > 0);
7057  }
7058  assert(nextpos == norigvars);
7059  cliquestartposs[c] = nextpos;
7060 
7061  /* now we copy all variable and weights to the right order */
7062  for( v = 0; v < norigvars; ++v )
7063  {
7064  *(varpointers[cliquepartition[v]]) = origvars[v]; /*lint !e613*/
7065  ++(varpointers[cliquepartition[v]]);
7066  *(weightpointers[cliquepartition[v]]) = origweights[v]; /*lint !e613*/
7067  ++(weightpointers[cliquepartition[v]]);
7068  }
7069 #ifndef NDEBUG
7070  for( v = 0; v < norigvars; ++v )
7071  {
7072  assert(vars[v] != NULL);
7073  assert(weights[v] > 0);
7074  }
7075 #endif
7076 
7077  /* free temporary memory */
7078  SCIPfreeBufferArray(scip, &weightpointers);
7079  SCIPfreeBufferArray(scip, &varpointers);
7080  SCIPfreeBufferArray(scip, &cliquecount);
7081 
7082  return SCIP_OKAY;
7083 }
7084 
7085 /** propagation method for knapsack constraints */
7086 static
7088  SCIP* scip, /**< SCIP data structure */
7089  SCIP_CONS* cons, /**< knapsack constraint */
7090  SCIP_Bool* cutoff, /**< pointer to store whether the node can be cut off */
7091  SCIP_Bool* redundant, /**< pointer to store whether constraint is redundant */
7092  int* nfixedvars, /**< pointer to count number of fixings */
7093  SCIP_Bool usenegatedclique /**< should negated clique information be used */
7094  )
7095 {
7096  SCIP_CONSDATA* consdata;
7097  SCIP_Bool infeasible;
7098  SCIP_Bool tightened;
7099  SCIP_Longint zerosweightsum;
7100  SCIP_Longint* secondmaxweights;
7101  SCIP_Longint minweightsum;
7102 
7103  int nvars;
7104  int i;
7105  int nnegcliques;
7106 
7107  SCIP_VAR** myvars;
7108  SCIP_Longint* myweights;
7109  int* cliquestartposs;
7110  SCIP_Longint localminweightsum;
7111  SCIP_Bool foundmax;
7112  int c;
7113 
7114  assert(scip != NULL);
7115  assert(cons != NULL);
7116  assert(cutoff != NULL);
7117  assert(redundant != NULL);
7118  assert(nfixedvars != NULL);
7119 
7120  consdata = SCIPconsGetData(cons);
7121  assert(consdata != NULL);
7122 
7123  *cutoff = FALSE;
7124  *redundant = FALSE;
7125 
7126  /* check, if constraint is already propagated */
7127  if( consdata->propagated )
7128  return SCIP_OKAY;
7129 
7130  SCIPdebugMessage("propagating knapsack constraint <%s>\n", SCIPconsGetName(cons));
7131 
7132  /* increase age of constraint; age is reset to zero, if a conflict or a propagation was found */
7133  if( !SCIPinRepropagation(scip) )
7134  {
7135  SCIP_CALL( SCIPincConsAge(scip, cons) );
7136  }
7137 
7138  /* we need a merged constraint cause without it the negated clique information could be invalid */
7139  usenegatedclique = usenegatedclique && consdata->merged;
7140  nnegcliques = -1;
7141 
7142  /* init for debugging */
7143  myvars = NULL;
7144  myweights = NULL;
7145  cliquestartposs = NULL;
7146  secondmaxweights = NULL;
7147  do
7148  {
7149  zerosweightsum = 0;
7150  nvars = consdata->nvars;
7151 
7152  minweightsum = 0;
7153  localminweightsum = 0;
7154 
7155  /* make sure, the items are sorted by non-increasing weight */
7156  sortItems(consdata);
7157 
7158  /* (1) compute the minimum weight of the knapsack constraint using negated clique information;
7159  * a negated clique means, that at most one of the clique variables can be zero
7160  * - minweightsum = sum_{negated cliques C} ( sum(wi : i \in C) - W(C) ), where W(C) is the maximal weight of C
7161  *
7162  * if for i \in C (a negated clique) minweightsum - wi + W(C) > capacity => xi = 1
7163  * since replacing i with the element of maximal weight leads to infeasibility
7164  */
7165  if( usenegatedclique && nvars > 0 )
7166  {
7167  /* compute clique partitions */
7168  SCIP_CALL( calcCliquepartition(scip, consdata, FALSE, TRUE) );
7169  nnegcliques = consdata->nnegcliques;
7170 
7171  /* if we have no real negated cliques we can stop here */
7172  if( nnegcliques == nvars )
7173  break;
7174 
7175  /* allocate temporary memory and initialize it */
7176  SCIP_CALL( SCIPduplicateBufferArray(scip, &myvars, consdata->vars, nvars) );
7177  SCIP_CALL( SCIPduplicateBufferArray(scip, &myweights, consdata->weights, nvars) ) ;
7178  SCIP_CALL( SCIPallocBufferArray(scip, &cliquestartposs, nnegcliques + 1) );
7179  SCIP_CALL( SCIPallocBufferArray(scip, &secondmaxweights, nnegcliques) );
7180  BMSclearMemoryArray(secondmaxweights, nnegcliques);
7181 
7182  /* resort variables to avoid quadratic algorithm later on */
7183  SCIP_CALL( stableSort(scip, consdata, myvars, myweights, cliquestartposs, TRUE) );
7184 
7185  c = 0;
7186  foundmax = FALSE;
7187  i = 0;
7188 
7189  while( i < nvars )
7190  {
7191  /* ignore variables of the negated clique which are fixed to one since these are counted in
7192  * consdata->onesweightsum
7193  */
7194 
7195  /* if there are only one variable negated cliques left we can stop */
7196  if( nnegcliques - c == nvars - i )
7197  {
7198  minweightsum += localminweightsum;
7199  localminweightsum = 0;
7200  break;
7201  }
7202 
7203  /* for summing up the minimum active weights due to cliques we have to omit the biggest weights of each
7204  * clique, we can only skip this clique if this variables is not fixed to zero, otherwise we have to fix all
7205  * other clique variables to one
7206  */
7207  if( cliquestartposs[c] == i )
7208  {
7209  assert(myweights[i] > 0);
7210  ++c;
7211  minweightsum += localminweightsum;
7212  localminweightsum = 0;
7213  foundmax = TRUE;
7214 
7215  if( SCIPvarGetLbLocal(myvars[i]) > 0.5 )
7216  foundmax = FALSE;
7217 
7218  if( SCIPvarGetUbLocal(myvars[i]) > 0.5 )
7219  {
7220  ++i;
7221  continue;
7222  }
7223  }
7224 
7225  if( SCIPvarGetLbLocal(myvars[i]) < 0.5 )
7226  {
7227  assert(myweights[i] > 0);
7228 
7229  if( SCIPvarGetUbLocal(myvars[i]) > 0.5 )
7230  {
7231  assert(myweights[i] <= myweights[cliquestartposs[c - 1]]);
7232 
7233  if( !foundmax )
7234  {
7235  foundmax = TRUE;
7236 
7237  /* overwrite cliquestartpos to the position of the first unfixed variable in this clique */
7238  cliquestartposs[c - 1] = i;
7239  ++i;
7240 
7241  continue;
7242  }
7243  /* memorize second max weight for each clique */
7244  if( secondmaxweights[c - 1] == 0 )
7245  secondmaxweights[c - 1] = myweights[i];
7246 
7247  localminweightsum += myweights[i];
7248  }
7249  /* we found a fixed variable to zero so all other variables in this negated clique have to be fixed to one */
7250  else
7251  {
7252  int v;
7253  /* fix all other variables of the negated clique to 1 */
7254  for( v = cliquestartposs[c - 1]; v < cliquestartposs[c]; ++v )
7255  {
7256  if( v != i && SCIPvarGetLbLocal(myvars[v]) < 0.5 )
7257  {
7258  SCIPdebugMessage(" -> fixing variable <%s> to 1, due to negated clique information\n", SCIPvarGetName(myvars[v]));
7259  SCIP_CALL( SCIPinferBinvarCons(scip, myvars[v], TRUE, cons, SCIPvarGetIndex(myvars[i]), &infeasible, &tightened) );
7260 
7261  if( infeasible )
7262  {
7263  assert( SCIPvarGetUbLocal(myvars[v]) < 0.5 );
7264 
7265  /* analyze the infeasibility if conflict analysis is applicable */
7267  {
7268  /* conflict analysis can only be applied in solving stage */
7269  assert(SCIPgetStage(scip) == SCIP_STAGE_SOLVING || SCIPinProbing(scip));
7270 
7271  /* initialize the conflict analysis */
7273 
7274  /* add the two variables which are fixed to zero within a negated clique */
7275  SCIP_CALL( SCIPaddConflictBinvar(scip, myvars[i]) );
7276  SCIP_CALL( SCIPaddConflictBinvar(scip, myvars[v]) );
7277 
7278  /* start the conflict analysis */
7279  SCIP_CALL( SCIPanalyzeConflictCons(scip, cons, NULL) );
7280  }
7281  *cutoff = TRUE;
7282  break;
7283  }
7284  assert(tightened);
7285  ++(*nfixedvars);
7286  SCIP_CALL( SCIPresetConsAge(scip, cons) );
7287  }
7288  }
7289  if( *cutoff )
7290  break;
7291 
7292  /* reset local minweightsum for clique because all fixed to one variables are now counted in consdata->onesweightsum */
7293  localminweightsum = 0;
7294  /* we can jump to the end of this clique */
7295  i = cliquestartposs[c] - 1;
7296  }
7297  }
7298  ++i;
7299  }
7300  /* add last clique minweightsum */
7301  minweightsum += localminweightsum;
7302 
7303  SCIPdebugMessage("knapsack constraint <%s> has minimum weight sum of <%"SCIP_LONGINT_FORMAT">\n",
7304  SCIPconsGetName(cons), minweightsum + consdata->onesweightsum );
7305 
7306  /* check, if weights of fixed variables don't exceeds knapsack capacity */
7307  if( !(*cutoff) && consdata->capacity >= minweightsum + consdata->onesweightsum )
7308  {
7309  SCIP_Longint maxweight;
7310 
7311  c = 0;
7312  maxweight = -1;
7313 
7314  /* check for each negated clique if we can conclude a fixing of a variable to one */
7315  for( i = 0; i < nvars; ++i )
7316  {
7317  /* if there are only one variable negated cliques left we can stop */
7318  if( nnegcliques - c == nvars - i )
7319  break;
7320 
7321  /* we cannot fix the biggest weight */
7322  if( cliquestartposs[c] == i )
7323  {
7324  maxweight = myweights[i];
7325  ++c;
7326  continue;
7327  }
7328 
7329  /* only check variables of negated cliques for which no variable is locally fixed */
7330  if( SCIPvarGetLbLocal(myvars[i]) < 0.5 && SCIPvarGetUbLocal(myvars[i]) > 0.5 )
7331  {
7332  assert(maxweight >= myweights[i]);
7333 
7334  /* if for i \in C (a negated clique) minweightsum - wi + W(C) > capacity => xi = 1
7335  * since replacing i with the element of maximal weight leads to infeasibility */
7336  if( consdata->onesweightsum + minweightsum - myweights[i] + maxweight > consdata->capacity )
7337  {
7338  SCIPdebugMessage(" -> fixing variable <%s> to 1, due to negated clique information\n", SCIPvarGetName(myvars[i]));
7339  SCIP_CALL( SCIPinferBinvarCons(scip, myvars[i], TRUE, cons, -i, &infeasible, &tightened) );
7340  assert(!infeasible);
7341  assert(tightened);
7342  ++(*nfixedvars);
7343  SCIP_CALL( SCIPresetConsAge(scip, cons) );
7344 
7345  /* update minweightsum because now the variable is fixed to one and its weight is counted by
7346  * consdata->onesweightsum
7347  */
7348  minweightsum -= myweights[i];
7349  assert(minweightsum >= 0);
7350  }
7351  }
7352  }
7353  }
7354 
7355  if( *cutoff )
7356  {
7357  SCIPfreeBufferArray(scip, &secondmaxweights);
7358  SCIPfreeBufferArray(scip, &cliquestartposs);
7359  SCIPfreeBufferArray(scip, &myweights);
7360  SCIPfreeBufferArray(scip, &myvars);
7361 
7362  return SCIP_OKAY;
7363  }
7364  }
7365 
7366  /* check, if weights of fixed variables already exceed knapsack capacity */
7367  if( consdata->capacity < minweightsum + consdata->onesweightsum )
7368  {
7369  SCIPdebugMessage(" -> cutoff - fixed weight: %"SCIP_LONGINT_FORMAT", capacity: %"SCIP_LONGINT_FORMAT", minimum weight sum: %"SCIP_LONGINT_FORMAT" \n",
7370  consdata->onesweightsum, consdata->capacity, minweightsum);
7371 
7372  SCIP_CALL( SCIPresetConsAge(scip, cons) );
7373  *cutoff = TRUE;
7374 
7375  /* analyze the cutoff if conflict analysis is applicable */
7377  {
7378  /* start conflict analysis with the fixed-to-one variables, add only as many as need to exceed the capacity */
7379  SCIP_Longint weight;
7380 
7381  weight = minweightsum;
7382 
7384  for( i = 0; i < nvars && weight <= consdata->capacity; i++ )
7385  {
7386  if( SCIPvarGetLbLocal(consdata->vars[i]) > 0.5)
7387  {
7388  SCIP_CALL( SCIPaddConflictBinvar(scip, consdata->vars[i]) );
7389  weight += consdata->weights[i];
7390  }
7391  }
7392 
7393  SCIP_CALL( SCIPanalyzeConflictCons(scip, cons, NULL) );
7394  }
7395 
7396  if( usenegatedclique && nvars > 0 )
7397  {
7398  SCIPfreeBufferArray(scip, &secondmaxweights);
7399  SCIPfreeBufferArray(scip, &cliquestartposs);
7400  SCIPfreeBufferArray(scip, &myweights);
7401  SCIPfreeBufferArray(scip, &myvars);
7402  }
7403  return SCIP_OKAY;
7404  }
7405 
7406  assert(consdata->negcliquepartitioned || minweightsum == 0);
7407 
7408  /* if the sum of all weights of fixed variables to one plus the minimalweightsum (minimal weight which is already
7409  * used in this knapsack due to negated cliques) plus any weight minus the second largest weight in this cliques
7410  * exceeds the capacity the variables have to be fixed to zero (these variables should only be variables in the
7411  * cliques which have maxweights)
7412  */
7413  if( usenegatedclique && nvars > 0 )
7414  {
7415  SCIP_VAR* var;
7416 
7417  assert(nnegcliques == consdata->nnegcliques);
7418  assert(myvars != NULL);
7419  assert(myweights != NULL);
7420  assert(secondmaxweights != NULL);
7421  assert(cliquestartposs != NULL);
7422 
7423  for( c = 0; c < nnegcliques; ++c )
7424  {
7425  if( consdata->onesweightsum + minweightsum + myweights[cliquestartposs[c]] - secondmaxweights[c] > consdata->capacity )
7426  {
7427  assert(myweights[cliquestartposs[c]] >= secondmaxweights[c]);
7428 
7429  var = myvars[cliquestartposs[c]];
7430  if( SCIPvarGetLbLocal(var) < 0.5 && SCIPvarGetUbLocal(var) > 0.5 )
7431  {
7432  SCIPdebugMessage(" -> fixing variable <%s> to 0\n", SCIPvarGetName(var));
7433  SCIP_CALL( SCIPresetConsAge(scip, cons) );
7434  SCIP_CALL( SCIPinferBinvarCons(scip, var, FALSE, cons, cliquestartposs[c], &infeasible, &tightened) );
7435  assert(!infeasible);
7436  assert(tightened);
7437  (*nfixedvars)++;
7438  }
7439  }
7440  }
7441 
7442  SCIPfreeBufferArray(scip, &secondmaxweights);
7443  SCIPfreeBufferArray(scip, &cliquestartposs);
7444  SCIPfreeBufferArray(scip, &myweights);
7445  SCIPfreeBufferArray(scip, &myvars);
7446  }
7447  }
7448  while( FALSE );
7449 
7450  /* check, if weights of fixed variables already exceed knapsack capacity, this can only happen if 'usenegatedclique'
7451  * is FALSE, or 'nnegcliques == nvars', otherwise the stronger condition above should have led to a cutoff
7452  */
7453  if( consdata->capacity < consdata->onesweightsum )
7454  {
7455  SCIPdebugMessage(" -> cutoff - fixed weight: %"SCIP_LONGINT_FORMAT", capacity: %"SCIP_LONGINT_FORMAT" \n",
7456  consdata->onesweightsum, consdata->capacity);
7457 
7458  SCIP_CALL( SCIPresetConsAge(scip, cons) );
7459  *cutoff = TRUE;
7460 
7461  /* analyze the cutoff in SOLVING stage and if conflict analysis is turned on */
7463  {
7464  /* start conflict analysis with the fixed-to-one variables, add only as many as need to exceed the capacity */
7465  SCIP_Longint weight;
7466 
7467  weight = 0;
7468 
7470  for( i = 0; i < nvars && weight <= consdata->capacity; i++ )
7471  {
7472  if( SCIPvarGetLbLocal(consdata->vars[i]) > 0.5)
7473  {
7474  SCIP_CALL( SCIPaddConflictBinvar(scip, consdata->vars[i]) );
7475  weight += consdata->weights[i];
7476  }
7477  }
7478 
7479  SCIP_CALL( SCIPanalyzeConflictCons(scip, cons, NULL) );
7480  }
7481 
7482  return SCIP_OKAY;
7483  }
7484 
7485  /* fix all variables to zero, that don't fit into the knapsack anymore */
7486  for( i = 0; i < nvars; ++i )
7487  {
7488  /* if all weights of fixed variables to one plus any weight exceeds the capacity the variables have to be fixed
7489  * to zero
7490  */
7491  if( SCIPvarGetLbLocal(consdata->vars[i]) < 0.5 )
7492  {
7493  if( SCIPvarGetUbLocal(consdata->vars[i]) > 0.5 )
7494  {
7495  if( consdata->onesweightsum + consdata->weights[i] > consdata->capacity )
7496  {
7497  SCIPdebugMessage(" -> fixing variable <%s> to 0\n", SCIPvarGetName(consdata->vars[i]));
7498  SCIP_CALL( SCIPresetConsAge(scip, cons) );
7499  SCIP_CALL( SCIPinferBinvarCons(scip, consdata->vars[i], FALSE, cons, i, &infeasible, &tightened) );
7500  assert(!infeasible);
7501  assert(tightened);
7502  (*nfixedvars)++;
7503  zerosweightsum += consdata->weights[i];
7504  }
7505  }
7506  else
7507  zerosweightsum += consdata->weights[i];
7508  }
7509  }
7510 
7511  assert(consdata->onesweightsum + zerosweightsum <= consdata->weightsum);
7512 
7513  /* if the remaining (potentially unfixed) variables would fit all into the knapsack, the knapsack is now redundant */
7514  if( !SCIPconsIsModifiable(cons) && consdata->weightsum - zerosweightsum <= consdata->capacity )
7515  {
7516  SCIPdebugMessage(" -> knapsack constraint <%s> is redundant: weightsum=%"SCIP_LONGINT_FORMAT", zerosweightsum=%"SCIP_LONGINT_FORMAT", capacity=%"SCIP_LONGINT_FORMAT"\n",
7517  SCIPconsGetName(cons), consdata->weightsum, zerosweightsum, consdata->capacity);
7518  SCIP_CALL( SCIPdelConsLocal(scip, cons) );
7519  *redundant = TRUE;
7520  }
7521 
7522  /* mark the constraint propagated */
7523  consdata->propagated = TRUE;
7524  return SCIP_OKAY;
7525 }
7526 
7527 /** all but one variable fit into the knapsack constraint, so we can upgrade this constraint to an logicor constraint
7528  * containing all negated variables of this knapsack constraint
7529  */
7530 static
7532  SCIP* scip, /**< SCIP data structure */
7533  SCIP_CONS* cons, /**< knapsack constraint */
7534  int* ndelconss, /**< pointer to store the amount of deleted constraints */
7535  int* naddconss /**< pointer to count number of added constraints */
7536  )
7537 {
7538  SCIP_CONS* newcons;
7539  SCIP_CONSDATA* consdata;
7540 
7541  assert(scip != NULL);
7542  assert(cons != NULL);
7543  assert(ndelconss != NULL);
7544  assert(naddconss != NULL);
7545 
7546  consdata = SCIPconsGetData(cons);
7547  assert(consdata != NULL);
7548  assert(consdata->nvars > 1);
7549 
7550  /* if the knapsack constraint consists only of two variables, we can upgrade it to a set-packing constraint */
7551  if( consdata->nvars == 2 )
7552  {
7553  SCIPdebugMessage("upgrading knapsack constraint <%s> to a set-packing constraint", SCIPconsGetName(cons));
7554 
7555  SCIP_CALL( SCIPcreateConsSetpack(scip, &newcons, SCIPconsGetName(cons), consdata->nvars, consdata->vars,
7559  SCIPconsIsStickingAtNode(cons)) );
7560  }
7561  /* if the knapsack constraint consists of at least three variables, we can upgrade it to a logicor constraint
7562  * containing all negated variables of the knapsack
7563  */
7564  else
7565  {
7566  SCIP_VAR** consvars;
7567 
7568  SCIPdebugMessage("upgrading knapsack constraint <%s> to a logicor constraint", SCIPconsGetName(cons));
7569 
7570  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, consdata->nvars) );
7571  SCIP_CALL( SCIPgetNegatedVars(scip, consdata->nvars, consdata->vars, consvars) );
7572 
7573  SCIP_CALL( SCIPcreateConsLogicor(scip, &newcons, SCIPconsGetName(cons), consdata->nvars, consvars,
7577  SCIPconsIsStickingAtNode(cons)) );
7578 
7579  SCIPfreeBufferArray(scip, &consvars);
7580  }
7581 
7582  SCIP_CALL( SCIPaddCons(scip, newcons) );
7583  SCIP_CALL( SCIPreleaseCons(scip, &newcons) );
7584  ++(*naddconss);
7585 
7586  SCIP_CALL( SCIPdelCons(scip, cons) );
7587  ++(*ndelconss);
7588 
7589  return SCIP_OKAY;
7590 }
7591 
7592 /** delete redundant variables
7593  *
7594  * i.e. 5x1 + 5x2 + 5x3 + 2x4 + 1x5 <= 13 => x4, x5 always fits into the knapsack, so we can delete them
7595  *
7596  * i.e. 5x1 + 5x2 + 5x3 + 2x4 + 1x5 <= 8 and we have the cliqueinformation (x1,x2,x3) is a clique
7597  * => x4, x5 always fits into the knapsack, so we can delete them
7598  *
7599  * i.e. 5x1 + 5x2 + 5x3 + 1x4 + 1x5 <= 6 and we have the cliqueinformation (x1,x2,x3) is a clique and (x4,x5) too
7600  * => we create the set partitioning constraint x4 + x5 <= 1 and delete them in this knapsack
7601  */
7602 static
7604  SCIP* scip, /**< SCIP data structure */
7605  SCIP_CONS* cons, /**< knapsack constraint */
7606  SCIP_Longint frontsum, /**< sum of front items which fit if we try to take from the first till the last */
7607  int splitpos, /**< split position till when all front items are fitting, splitpos is the
7608  * first which did not fit */
7609  int* nchgcoefs, /**< pointer to store the amount of changed coefficients */
7610  int* nchgsides, /**< pointer to store the amount of changed sides */
7611  int* naddconss /**< pointer to count number of added constraints */
7612  )
7613 {
7614  SCIP_CONSHDLRDATA* conshdlrdata;
7615  SCIP_CONSDATA* consdata;
7616  SCIP_VAR** vars;
7617  SCIP_Longint* weights;
7618  SCIP_Longint capacity;
7619  SCIP_Longint gcd;
7620  int nvars;
7621  int w;
7622 
7623  assert(scip != NULL);
7624  assert(cons != NULL);
7625  assert(nchgcoefs != NULL);
7626  assert(nchgsides != NULL);
7627  assert(naddconss != NULL);
7628 
7629  consdata = SCIPconsGetData(cons);
7630  assert(consdata != NULL);
7631  assert(0 < frontsum && frontsum < consdata->weightsum);
7632  assert(0 < splitpos && splitpos < consdata->nvars);
7633  assert(consdata->sorted);
7634 
7635  conshdlrdata = SCIPconshdlrGetData(SCIPconsGetHdlr(cons));
7636  assert(conshdlrdata != NULL);
7637 
7638  vars = consdata->vars;
7639  weights = consdata->weights;
7640  nvars = consdata->nvars;
7641  capacity = consdata->capacity;
7642 
7643  /* if there are no variables rear to splitpos, the constraint has no redundant variables */
7644  if( consdata->nvars - 1 == splitpos )
7645  return SCIP_OKAY;
7646 
7647  assert(frontsum + weights[splitpos] > capacity);
7648 
7649  /* detect redundant variables */
7650  if( consdata->weightsum - weights[splitpos] <= capacity )
7651  {
7652  /* all rear items are redundant, because leaving one item in front and incl. of splitpos out the rear itmes always
7653  * fit
7654  */
7655  SCIPdebugMessage("Found redundant variables in constraint <%s>.\n", SCIPconsGetName(cons));
7656 
7657  /* delete items and update capacity */
7658  for( w = nvars - 1; w > splitpos; --w )
7659  {
7660  consdata->capacity -= weights[w];
7661  SCIP_CALL( delCoefPos(scip, cons, w) );
7662  }
7663  assert(w == splitpos);
7664 
7665  ++(*nchgsides);
7666  *nchgcoefs += (nvars - splitpos);
7667 
7668  /* division by greatest common divisor */
7669  gcd = weights[w];
7670  for( ; w >= 0 && gcd > 1; --w )
7671  {
7672  gcd = SCIPcalcGreComDiv(gcd, weights[w]);
7673  }
7674 
7675  /* normalize if possible */
7676  if( gcd > 1 )
7677  {
7678  for( w = splitpos; w >= 0; --w )
7679  {
7680  consdataChgWeight(consdata, w, weights[w]/gcd);
7681  }
7682  (*nchgcoefs) += nvars;
7683 
7684  consdata->capacity /= gcd;
7685  ++(*nchgsides);
7686  }
7687 
7688  /* weight should be still sorted, because the reduction preserves this */
7689 #ifndef NDEBUG
7690  for( w = consdata->nvars - 1; w > 0; --w )
7691  assert(weights[w] <= weights[w - 1]);
7692 #endif
7693 
7694  consdata->sorted = TRUE;
7695  }
7696  /* rear items can only be redundant, when the sum is smaller to the weight at splitpos and all rear items would
7697  * always fit into the knapsack, therefor the item directly after splitpos needs to be smaller than the one at
7698  * splitpos and needs to fit into the knapsack
7699  */
7700  else if( conshdlrdata->disaggregation && frontsum + weights[splitpos + 1] <= capacity )
7701  {
7702  int* clqpart;
7703  int nclq;
7704  int len;
7705 
7706  len = nvars - (splitpos + 1);
7707  /* allocate temporary memory */
7708  SCIP_CALL( SCIPallocBufferArray(scip, &clqpart, len) );
7709 
7710  /* calculate clique partition */
7711  SCIP_CALL( SCIPcalcCliquePartition(scip, &(consdata->vars[splitpos+1]), len, clqpart, &nclq) );
7712 
7713  /* check if we found at least one clique */
7714  if( nclq < len )
7715  {
7716  SCIP_Longint maxactduetoclq;
7717  int cliquenum;
7718 
7719  maxactduetoclq = 0;
7720  cliquenum = 0;
7721 
7722  /* calculate maximum activity due to cliques */
7723  for( w = 0; w < len; ++w )
7724  {
7725  assert(clqpart[w] >= 0 && clqpart[w] <= w);
7726  if( clqpart[w] == cliquenum )
7727  {
7728  maxactduetoclq += weights[w + splitpos + 1];
7729  ++cliquenum;
7730  }
7731  }
7732 
7733  /* all rear items are redundant due to clique information, if maxactduetoclq is smaller than the weight before,
7734  * so delete them and create for all clique the corresponding clique constraints and update the capacity
7735  */
7736  if( frontsum + maxactduetoclq <= capacity )
7737  {
7738  SCIP_VAR** clqvars;
7739  int nclqvars;
7740  int c;
7741 
7742  assert(maxactduetoclq < weights[splitpos]);
7743 
7744  SCIPdebugMessage("Found redundant variables in constraint <%s> due to clique information.\n", SCIPconsGetName(cons));
7745 
7746  /* allocate temporary memory */
7747  SCIP_CALL( SCIPallocBufferArray(scip, &clqvars, len - nclq + 1) );
7748 
7749  for( c = 0; c < nclq; ++c )
7750  {
7751  nclqvars = 0;
7752  for( w = 0; w < len; ++w )
7753  {
7754  if( clqpart[w] == c )
7755  {
7756  clqvars[nclqvars] = vars[w + splitpos + 1];
7757  ++nclqvars;
7758  }
7759  }
7760 
7761  /* we found a real clique so extract this constraint, because we do not know who this information generated so */
7762  if( nclqvars > 1 )
7763  {
7764  SCIP_CONS* cliquecons;
7765  char name[SCIP_MAXSTRLEN];
7766 
7767  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_clq_%"SCIP_LONGINT_FORMAT"_%d", SCIPconsGetName(cons), capacity, c);
7768  SCIP_CALL( SCIPcreateConsSetpack(scip, &cliquecons, name, nclqvars, clqvars,
7772  SCIPconsIsStickingAtNode(cons)) );
7773  SCIPdebugMessage(" -> adding clique constraint: ");
7774  SCIPdebugPrintCons(scip, cliquecons, NULL);
7775  SCIP_CALL( SCIPaddCons(scip, cliquecons) );
7776  SCIP_CALL( SCIPreleaseCons(scip, &cliquecons) );
7777  ++(*naddconss);
7778  }
7779  }
7780 
7781  /* delete items and update capacity */
7782  for( w = nvars - 1; w > splitpos; --w )
7783  {
7784  SCIP_CALL( delCoefPos(scip, cons, w) );
7785  ++(*nchgcoefs);
7786  }
7787  consdata->capacity -= maxactduetoclq;
7788  assert(frontsum <= consdata->capacity);
7789  ++(*nchgsides);
7790 
7791  assert(w == splitpos);
7792 
7793  /* renew weights pointer */
7794  weights = consdata->weights;
7795 
7796  /* division by greatest common divisor */
7797  gcd = weights[w];
7798  for( ; w >= 0 && gcd > 1; --w )
7799  {
7800  gcd = SCIPcalcGreComDiv(gcd, weights[w]);
7801  }
7802 
7803  /* normalize if possible */
7804  if( gcd > 1 )
7805  {
7806  for( w = splitpos; w >= 0; --w )
7807  {
7808  consdataChgWeight(consdata, w, weights[w]/gcd);
7809  }
7810  (*nchgcoefs) += nvars;
7811 
7812  consdata->capacity /= gcd;
7813  ++(*nchgsides);
7814  }
7815 
7816  /* free temporary memory */
7817  SCIPfreeBufferArray(scip, &clqvars);
7818 
7819  /* weight should be still sorted, because the reduction preserves this */
7820 #ifndef NDEBUG
7821  for( w = consdata->nvars - 1; w > 0; --w )
7822  assert(weights[w] <= weights[w - 1]);
7823 #endif
7824  consdata->sorted = TRUE;
7825  }
7826  }
7827 
7828  /* free temporary memory */
7829  SCIPfreeBufferArray(scip, &clqpart);
7830  }
7831 
7832  return SCIP_OKAY;
7833 }
7834 
7835 /* detect redundant variables which always fits into the knapsack
7836  *
7837  * i.e. 5x1 + 5x2 + 5x3 + 2x4 + 1x5 <= 13 => x4, x5 always fits into the knapsack, so we can delete them
7838  *
7839  * i.e. 5x1 + 5x2 + 5x3 + 2x4 + 1x5 <= 8 and we have the cliqueinformation (x1,x2,x3) is a clique
7840  * => x4, x5 always fits into the knapsack, so we can delete them
7841  *
7842  * i.e. 5x1 + 5x2 + 5x3 + 1x4 + 1x5 <= 6 and we have the cliqueinformation (x1,x2,x3) is a clique and (x4,x5) too
7843  * => we create the set partitioning constraint x4 + x5 <= 1 and delete them in this knapsack
7844  */
7845 static
7847  SCIP* scip, /**< SCIP data structure */
7848  SCIP_CONS* cons, /**< knapsack constraint */
7849  int* ndelconss, /**< pointer to store the amount of deleted constraints */
7850  int* nchgcoefs, /**< pointer to store the amount of changed coefficients */
7851  int* nchgsides, /**< pointer to store the amount of changed sides */
7852  int* naddconss /**< pointer to count number of added constraints */
7853  )
7854 {
7855  SCIP_CONSDATA* consdata;
7856  SCIP_VAR** vars;
7857  SCIP_Longint* weights;
7858  SCIP_Longint capacity;
7859  SCIP_Longint sum;
7860  int noldchgcoefs;
7861  int nvars;
7862  int v;
7863  int w;
7864 
7865  assert(scip != NULL);
7866  assert(cons != NULL);
7867  assert(ndelconss != NULL);
7868  assert(nchgcoefs != NULL);
7869  assert(nchgsides != NULL);
7870  assert(naddconss != NULL);
7871 
7872  consdata = SCIPconsGetData(cons);
7873  assert(consdata != NULL);
7874  assert(consdata->nvars >= 2);
7875  assert(consdata->weightsum > consdata->capacity);
7876 
7877  noldchgcoefs = *nchgcoefs;
7878  vars = consdata->vars;
7879  weights = consdata->weights;
7880  nvars = consdata->nvars;
7881  capacity = consdata->capacity;
7882  sum = 0;
7883 
7884  /* search for maximal fitting items */
7885  for( v = 0; v < nvars && sum + weights[v] <= capacity; ++v )
7886  sum += weights[v];
7887 
7888  assert(v < nvars);
7889 
7890  /* all but one variable fit into the knapsack, so we can upgrade this constraint to a logicor */
7891  if( v == nvars - 1 )
7892  {
7893  SCIP_CALL( upgradeCons(scip, cons, ndelconss, naddconss) );
7894  assert(SCIPconsIsDeleted(cons));
7895 
7896  return SCIP_OKAY;
7897  }
7898 
7899  if( v < nvars - 1 )
7900  {
7901  /* try to delete variables */
7902  SCIP_CALL( deleteRedundantVars(scip, cons, sum, v, nchgcoefs, nchgsides, naddconss) );
7903  assert(consdata->nvars > 1);
7904 
7905  /* all but one variable fit into the knapsack, so we can upgrade this constraint to a logicor */
7906  if( v == consdata->nvars - 1 )
7907  {
7908  SCIP_CALL( upgradeCons(scip, cons, ndelconss, naddconss) );
7909  assert(SCIPconsIsDeleted(cons));
7910  }
7911 
7912  return SCIP_OKAY;
7913  }
7914 
7915  /* if we already found some redundant variables, stop here */
7916  if( *nchgcoefs > noldchgcoefs )
7917  return SCIP_OKAY;
7918 
7919  assert(vars == consdata->vars);
7920  assert(weights == consdata->weights);
7921  assert(nvars == consdata->nvars);
7922  assert(capacity == consdata->capacity);
7923 
7924  /* calculate clique partition */
7925  SCIP_CALL( calcCliquepartition(scip, consdata, TRUE, FALSE) );
7926 
7927  /* check for real existing cliques */
7928  if( consdata->cliquepartition[v] < v )
7929  {
7930  SCIP_CONSHDLRDATA* conshdlrdata;
7931  SCIP_Longint sumfront;
7932  SCIP_Longint maxactduetoclqfront;
7933  int* clqpart;
7934  int cliquenum;
7935 
7936  conshdlrdata = SCIPconshdlrGetData(SCIPconsGetHdlr(cons));
7937  assert(conshdlrdata != NULL);
7938 
7939  sumfront = 0;
7940  maxactduetoclqfront = 0;
7941 
7942  clqpart = consdata->cliquepartition;
7943  cliquenum = 0;
7944 
7945  /* calculate maximal activity due to cliques */
7946  for( w = 0; w < nvars; ++w )
7947  {
7948  assert(clqpart[w] >= 0 && clqpart[w] <= w);
7949  if( clqpart[w] == cliquenum )
7950  {
7951  if( maxactduetoclqfront + weights[w] <= capacity )
7952  {
7953  maxactduetoclqfront += weights[w];
7954  ++cliquenum;
7955  }
7956  else
7957  break;
7958  }
7959  sumfront += weights[w];
7960  }
7961  assert(w >= v);
7962 
7963  /* if all items fit, then delete the whole constraint but create clique constraints which led to this
7964  * information
7965  */
7966  if( conshdlrdata->disaggregation && w == nvars )
7967  {
7968  SCIP_VAR** clqvars;
7969  int nclqvars;
7970  int c;
7971  int ncliques;
7972 
7973  assert(maxactduetoclqfront <= capacity);
7974 
7975  SCIPdebugMessage("Found redundant constraint <%s> due to clique information.\n", SCIPconsGetName(cons));
7976 
7977  ncliques = consdata->ncliques;
7978 
7979  /* allocate temporary memory */
7980  SCIP_CALL( SCIPallocBufferArray(scip, &clqvars, nvars - ncliques + 1) );
7981 
7982  for( c = 0; c < ncliques; ++c )
7983  {
7984  nclqvars = 0;
7985  for( w = 0; w < nvars; ++w )
7986  {
7987  if( clqpart[w] == c )
7988  {
7989  clqvars[nclqvars] = vars[w];
7990  ++nclqvars;
7991  }
7992  }
7993 
7994  /* we found a real clique so extract this constraint, because we do not know who this information generated so */
7995  if( nclqvars > 1 )
7996  {
7997  SCIP_CONS* cliquecons;
7998  char name[SCIP_MAXSTRLEN];
7999 
8000  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_clq_%"SCIP_LONGINT_FORMAT"_%d", SCIPconsGetName(cons), capacity, c);
8001  SCIP_CALL( SCIPcreateConsSetpack(scip, &cliquecons, name, nclqvars, clqvars,
8005  SCIPconsIsStickingAtNode(cons)) );
8006  SCIPdebugMessage(" -> adding clique constraint: ");
8007  SCIPdebugPrintCons(scip, cliquecons, NULL);
8008  SCIP_CALL( SCIPaddCons(scip, cliquecons) );
8009  SCIP_CALL( SCIPreleaseCons(scip, &cliquecons) );
8010  ++(*naddconss);
8011  }
8012  }
8013 
8014  /* delete old constraint */
8015  SCIP_CALL( SCIPdelConsLocal(scip, cons) );
8016  ++(*ndelconss);
8017 
8018  SCIPfreeBufferArray(scip, &clqvars);
8019 
8020  return SCIP_OKAY;
8021  }
8022 
8023  if( w > v && w < nvars - 1 )
8024  {
8025  /* try to delete variables */
8026  SCIP_CALL( deleteRedundantVars(scip, cons, sumfront, w, nchgcoefs, nchgsides, naddconss) );
8027  }
8028  }
8029 
8030  return SCIP_OKAY;
8031 }
8032 
8033 /** divides weights by their greatest common divisor and divides capacity by the same value, rounding down the result */
8034 static
8036  SCIP_CONS* cons, /**< knapsack constraint */
8037  int* nchgcoefs, /**< pointer to count total number of changed coefficients */
8038  int* nchgsides /**< pointer to count number of side changes */
8039  )
8040 {
8041  SCIP_CONSDATA* consdata;
8042  SCIP_Longint gcd;
8043  int i;
8044 
8045  assert(nchgcoefs != NULL);
8046  assert(nchgsides != NULL);
8047  assert(!SCIPconsIsModifiable(cons));
8048 
8049  consdata = SCIPconsGetData(cons);
8050  assert(consdata != NULL);
8051  assert(consdata->row == NULL); /* we are in presolve, so no LP row exists */
8052  assert(consdata->onesweightsum == 0); /* all fixed variables should have been removed */
8053  assert(consdata->weightsum > consdata->capacity); /* otherwise, the constraint is redundant */
8054  assert(consdata->nvars >= 1);
8055 
8056  /* sort items, because we can stop earlier if the smaller weights are evaluated first */
8057  sortItems(consdata);
8058 
8059  gcd = consdata->weights[consdata->nvars-1];
8060  for( i = consdata->nvars-2; i >= 0 && gcd >= 2; --i )
8061  {
8062  assert(SCIPvarGetLbLocal(consdata->vars[i]) < 0.5);
8063  assert(SCIPvarGetUbLocal(consdata->vars[i]) > 0.5); /* all fixed variables should have been removed */
8064 
8065  gcd = SCIPcalcGreComDiv(gcd, consdata->weights[i]);
8066  }
8067 
8068  if( gcd >= 2 )
8069  {
8070  SCIPdebugMessage("knapsack constraint <%s>: dividing weights by %"SCIP_LONGINT_FORMAT"\n", SCIPconsGetName(cons), gcd);
8071 
8072  for( i = 0; i < consdata->nvars; ++i )
8073  consdataChgWeight(consdata, i, consdata->weights[i]/gcd);
8074  consdata->capacity /= gcd;
8075  (*nchgcoefs) += consdata->nvars;
8076  (*nchgsides)++;
8077 
8078  /* weight should be still sorted, because the reduction preserves this */
8079 #ifndef NDEBUG
8080  for( i = consdata->nvars - 1; i > 0; --i )
8081  assert(consdata->weights[i] <= consdata->weights[i - 1]);
8082 #endif
8083  consdata->sorted = TRUE;
8084  }
8085 }
8086 
8087 /** dual weights tightening for knapsack constraints
8088  *
8089  * 1. a) check if all two pairs exceed the capacity, then we can upgrade this constraint to a set-packing constraint
8090  * b) check if all but the smallest weight fit into the knapsack, then we can upgrade this constraint to a logicor
8091  * constraint
8092  *
8093  * 2. check if besides big coefficients, that fit only by itself, for a certain amount of variables all combination of
8094  * these are a minimal cover, then might reduce the weights and the capacity, e.g.
8095  *
8096  * +219y1 + 180y2 + 74x1 + 70x2 + 63x3 + 62x4 + 53x5 <= 219 <=> 3y1 + 3y2 + x1 + x2 + x3 + x4 + x5 <= 3
8097  *
8098  * 3. use the duality between a^Tx <= capacity <=> a^T~x >= weightsum - capacity to tighten weights, e.g.
8099  *
8100  * 11x1 + 10x2 + 7x3 + 7x4 + 5x5 <= 27 <=> 10~x1 + 10~x2 + 7~x3 + 7~x4 + 5~x5 >= 13
8101  *
8102  * the above constraint can be changed to 8~x1 + 8~x2 + 6.5~x3 + 6.5~x4 + 5~x5 >= 13
8103  *
8104  * 16~x1 + 16~x2 + 13~x3 + 13~x4 + 10~x5 >= 26 <=> 16x1 + 16x2 + 13x3 + 13x4 + 10x5 <= 42
8105  */
8106 static
8108  SCIP* scip, /**< SCIP data structure */
8109  SCIP_CONS* cons, /**< knapsack constraint */
8110  int* ndelconss, /**< pointer to store the amount of deleted constraints */
8111  int* nchgcoefs, /**< pointer to store the amount of changed coefficients */
8112  int* nchgsides, /**< pointer to store the amount of changed sides */
8113  int* naddconss /**< pointer to count number of added constraints */
8114  )
8115 {
8116  SCIP_CONSDATA* consdata;
8117  SCIP_Longint* weights;
8118  SCIP_Longint dualcapacity;
8119  SCIP_Longint reductionsum;
8120  SCIP_Longint capacity;
8121  SCIP_Longint exceedsum;
8122  int oldnchgcoefs;
8123  int nvars;
8124  int vbig;
8125  int v;
8126  int w;
8127 #ifndef NDEBUG
8128  int oldnchgsides = *nchgsides;
8129 #endif
8130 
8131  assert(scip != NULL);
8132  assert(cons != NULL);
8133  assert(ndelconss != NULL);
8134  assert(nchgcoefs != NULL);
8135  assert(nchgsides != NULL);
8136  assert(naddconss != NULL);
8137 
8138  consdata = SCIPconsGetData(cons);
8139  assert(consdata != NULL);
8140  assert(consdata->weightsum > consdata->capacity);
8141  assert(consdata->nvars >= 2);
8142  assert(consdata->sorted);
8143 
8144  /* constraint should be merged */
8145  assert(consdata->merged);
8146 
8147  nvars = consdata->nvars;
8148  weights = consdata->weights;
8149  capacity = consdata->capacity;
8150 
8151  oldnchgcoefs = *nchgcoefs;
8152 
8153  /* case 1. */
8154  if( weights[nvars - 1] + weights[nvars - 2] > capacity )
8155  {
8156  SCIP_CONS* newcons;
8157 
8158  /* two variable are enough to exceed the constraint, so we can update it to a set-packing
8159  *
8160  * e.g. 5x1 + 4x2 + 3x3 <= 5 <=> x1 + x2 + x3 <= 1
8161  */
8162  SCIPdebugMessage("upgrading knapsack constraint <%s> to a set-packing constraint", SCIPconsGetName(cons));
8163 
8164  SCIP_CALL( SCIPcreateConsSetpack(scip, &newcons, SCIPconsGetName(cons), consdata->nvars, consdata->vars,
8168  SCIPconsIsStickingAtNode(cons)) );
8169 
8170  SCIP_CALL( SCIPaddCons(scip, newcons) );
8171  SCIP_CALL( SCIPreleaseCons(scip, &newcons) );
8172  ++(*naddconss);
8173 
8174  SCIP_CALL( SCIPdelCons(scip, cons) );
8175  ++(*ndelconss);
8176 
8177  return SCIP_OKAY;
8178  }
8179 
8180  /* all but one variable fit into the knapsack, so we can upgrade this constraint to a logicor */
8181  if( consdata->weightsum - weights[nvars - 1] <= consdata->capacity )
8182  {
8183  SCIP_CALL( upgradeCons(scip, cons, ndelconss, naddconss) );
8184  assert(SCIPconsIsDeleted(cons));
8185 
8186  return SCIP_OKAY;
8187  }
8188 
8189  /* early termination, if the pair with biggest coeffcients together does not exceed the dualcapacity */
8190  /* @todo might be changed/removed when improving the coeffcients tightening */
8191  if( consdata->weightsum - capacity > weights[0] + weights[1] )
8192  return SCIP_OKAY;
8193 
8194  /* case 2. */
8195 
8196  v = 0;
8197 
8198  /* @todo generalize the following algorithm for several parts of the knapsack
8199  *
8200  * the following is done without looking at the dualcapacity; it is enough to check whether for a certain amount of
8201  * variables each combination is a minimal cover, some examples
8202  *
8203  * +74x1 + 70x2 + 63x3 + 62x4 + 53x5 <= 219 <=> 74~x1 + 70~x2 + 63~x3 + 62~x4 + 53~x5 >= 103
8204  * <=> ~x1 + ~x2 + ~x3 + ~x4 + ~x5 >= 2
8205  * <=> x1 + x2 + x3 + x4 + x5 <= 3
8206  *
8207  * +219y1 + 180y_2 +74x1 + 70x2 + 63x3 + 62x4 + 53x5 <= 219 <=> 3y1 + 3y2 + x1 + x2 + x3 + x4 + x5 <= 3
8208  *
8209  */
8210 
8211  /* determine big weights that fit only by itself */
8212  while( v < nvars && weights[v] + weights[nvars - 1] > capacity )
8213  ++v;
8214 
8215  vbig = v;
8216  assert(vbig < nvars - 1);
8217  exceedsum = 0;
8218 
8219  /* determine the amount needed to exceed the capacity */
8220  while( v < nvars && exceedsum <= capacity )
8221  {
8222  exceedsum += weights[v];
8223  ++v;
8224  }
8225 
8226  /* if we exceeded the capacity we might reduce the weights */
8227  if( exceedsum > capacity )
8228  {
8229  assert(vbig > 0 || v < nvars);
8230 
8231  /* all small weights were needed to exceed the capacity */
8232  if( v == nvars )
8233  {
8234  SCIP_Longint newweight = (SCIP_Longint)nvars - vbig - 1;
8235  assert(newweight > 0);
8236 
8237  /* reduce big weights */
8238  for( v = 0; v < vbig; ++v )
8239  {
8240  if( weights[v] > newweight )
8241  {
8242  consdataChgWeight(consdata, v, newweight);
8243  ++(*nchgcoefs);
8244  }
8245  }
8246 
8247  /* reduce small weights */
8248  for( ; v < nvars; ++v )
8249  {
8250  if( weights[v] > 1 )
8251  {
8252  consdataChgWeight(consdata, v, 1LL);
8253  ++(*nchgcoefs);
8254  }
8255  }
8256 
8257  consdata->capacity = newweight;
8258 
8259  /* weight should be still sorted, because the reduction preserves this */
8260 #ifndef NDEBUG
8261  for( v = nvars - 1; v > 0; --v )
8262  assert(weights[v] <= weights[v-1]);
8263 #endif
8264  consdata->sorted = TRUE;
8265 
8266  return SCIP_OKAY;
8267  }
8268  /* a certain amount of small variables exceed the capacity, so check if this holds for all combinations of the
8269  * small weights
8270  */
8271  else
8272  {
8273  SCIP_Longint exceedsumback = 0;
8274  int nexceed = v - vbig;
8275 
8276  assert(nexceed > 1);
8277 
8278  /* determine weightsum of the same amount as before but of the smallest weight */
8279  for( w = nvars - 1; w >= nvars - nexceed; --w )
8280  exceedsumback += weights[w];
8281 
8282  assert(w >= 0);
8283 
8284  /* if the same amount but with the smallest possible weights also exceed the capacity, it holds for all
8285  * combinations of all small weights
8286  */
8287  if( exceedsumback > capacity )
8288  {
8289  SCIP_Longint newweight = nexceed - 1;
8290 
8291  /* taking out the smallest element needs to fit */
8292  assert(exceedsumback - weights[nvars - 1] <= capacity);
8293 
8294  /* reduce big weights */
8295  for( v = 0; v < vbig; ++v )
8296  {
8297  if( weights[v] > newweight )
8298  {
8299  consdataChgWeight(consdata, v, newweight);
8300  ++(*nchgcoefs);
8301  }
8302  }
8303 
8304  /* reduce small weights */
8305  for( ; v < nvars; ++v )
8306  {
8307  if( weights[v] > 1 )
8308  {
8309  consdataChgWeight(consdata, v, 1LL);
8310  ++(*nchgcoefs);
8311  }
8312  }
8313 
8314  consdata->capacity = newweight;
8315 
8316  /* weight should be still sorted, because the reduction preserves this */
8317 #ifndef NDEBUG
8318  for( v = nvars - 1; v > 0; --v )
8319  assert(weights[v] <= weights[v-1]);
8320 #endif
8321  consdata->sorted = TRUE;
8322 
8323  return SCIP_OKAY;
8324  }
8325  }
8326  }
8327  else
8328  {
8329  /* if the follwoing assert fails we have either a redundant constraint or a set-packing constraint, this should
8330  * not happen here
8331  */
8332  assert(vbig > 0 && vbig < nvars);
8333 
8334  /* either choose a big coefficients or all other variables
8335  *
8336  * 973x1 + 189x2 + 189x3 + 145x4 + 110x5 + 104x6 + 93x7 + 71x8 + 68x9 + 10x10 <= 979
8337  *
8338  * either choose x1, or all other variables (weightsum of x2 to x10 is 979 above), so we can tighten this
8339  * constraint to
8340  *
8341  * 9x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 <= 9
8342  */
8343 
8344  if( weights[vbig - 1] > (SCIP_Longint)nvars - vbig || weights[vbig] > 1 )
8345  {
8346  SCIP_Longint newweight = (SCIP_Longint)nvars - vbig;
8347 #ifndef NDEBUG
8348  SCIP_Longint resweightsum = consdata->weightsum;
8349 
8350  for( v = 0; v < vbig; ++v )
8351  resweightsum -= weights[v];
8352 
8353  assert(exceedsum == resweightsum);
8354 #endif
8355  assert(newweight > 0);
8356 
8357  /* reduce big weights */
8358  for( v = 0; v < vbig; ++v )
8359  {
8360  if( weights[v] > newweight )
8361  {
8362  consdataChgWeight(consdata, v, newweight);
8363  ++(*nchgcoefs);
8364  }
8365  }
8366 
8367  /* reduce small weights */
8368  for( ; v < nvars; ++v )
8369  {
8370  if( weights[v] > 1 )
8371  {
8372  consdataChgWeight(consdata, v, 1LL);
8373  ++(*nchgcoefs);
8374  }
8375  }
8376 
8377  consdata->capacity = newweight;
8378 
8379  /* weight should be still sorted, because the reduction preserves this */
8380 #ifndef NDEBUG
8381  for( v = nvars - 1; v > 0; --v )
8382  assert(weights[v] <= weights[v-1]);
8383 #endif
8384  consdata->sorted = TRUE;
8385 
8386  return SCIP_OKAY;
8387  }
8388  }
8389 
8390  /* case 3. */
8391 
8392  dualcapacity = consdata->weightsum - capacity;
8393  reductionsum = 0;
8394  v = 0;
8395 
8396  /* reduce big weights
8397  *
8398  * e.g. 11x0 + 11x1 + 10x2 + 10x3 <= 32 <=> 11~x0 + 11~x1 + 10~x2 + 10~x3 >= 10
8399  * <=> 10~x0 + 10~x1 + 10~x2 + 10~x3 >= 10
8400  * <=> x0 + x1 + x2 + x3 <= 3
8401  */
8402  while( weights[v] > dualcapacity )
8403  {
8404  reductionsum += (weights[v] - dualcapacity);
8405  consdataChgWeight(consdata, v, dualcapacity);
8406  ++v;
8407  assert(v < nvars);
8408  }
8409  (*nchgcoefs) += v;
8410 
8411  /* skip weights equal to the dualcapacity, because we cannot change them */
8412  while( v < nvars && weights[v] == dualcapacity )
8413  ++v;
8414 
8415  /* one negated variable is enough to fulfill the constraint, so we can update it to a logicor
8416  *
8417  * e.g. 10x1 + 10x2 + 10x3 <= 20 <=> 10~x1 + 10~x2 + 10~x3 >= 10 <=> ~x1 + ~x2 + ~x3 >= 1
8418  */
8419  if( v == nvars )
8420  {
8421  SCIP_CALL( upgradeCons(scip, cons, ndelconss, naddconss) );
8422  assert(SCIPconsIsDeleted(cons));
8423 
8424  return SCIP_OKAY;
8425  }
8426  else if( v < nvars - 1 )
8427  {
8428  /* @todo generalize the following algorithm for more than two variables */
8429 
8430  if( weights[nvars - 1] + weights[nvars - 2] >= dualcapacity )
8431  {
8432  /* we have a dual-knapsack constraint were we either need to choose one variable out of a subset (big
8433  * coefficients) of all or two variables of the rest
8434  *
8435  * e.g. 9x1 + 9x2 + 6x3 + 4x4 <= 19 <=> 9~x1 + 9~x2 + 6~x3 + 4~x4 >= 9
8436  * <=> 2~x1 + 2~x2 + ~x3 + ~x4 >= 2
8437  * <=> 2x1 + 2x2 + x3 + x4 <= 4
8438  *
8439  * 3x1 + 3x2 + 2x3 + 2x4 + 2x5 + 2x6 + x7 <= 12 <=> 3~x1 + 3~x2 + 2~x3 + 2~x4 + 2~x5 + 2~x6 + ~x7 >= 3
8440  * <=> 2~x1 + 2~x2 + ~x3 + ~x4 + ~x5 + ~x6 + ~x7 >= 2
8441  * <=> 2 x1 + 2 x2 + x3 + x4 + x5 + x6 + x7 <= 7
8442  *
8443  */
8444  if( v > 0 && weights[nvars - 2] > 1 )
8445  {
8446  int ncoefchg = 0;
8447 
8448  /* reduce all bigger weights */
8449  for( w = 0; w < v; ++w )
8450  {
8451  if( weights[w] > 2 )
8452  {
8453  consdataChgWeight(consdata, w, 2LL);
8454  ++ncoefchg;
8455  }
8456  else
8457  {
8458  assert(weights[0] == 2);
8459  assert(weights[v - 1] == 2);
8460  break;
8461  }
8462  }
8463 
8464  /* reduce all smaller weights */
8465  for( w = v; w < nvars; ++w )
8466  {
8467  if( weights[w] > 1 )
8468  {
8469  consdataChgWeight(consdata, w, 1LL);
8470  ++ncoefchg;
8471  }
8472  }
8473  assert(ncoefchg > 0);
8474 
8475  (*nchgcoefs) += ncoefchg;
8476 
8477  /* correct the capacity */
8478  consdata->capacity = (-2 + v * 2 + nvars - v); /*lint !e647*/
8479  assert(consdata->capacity > 0);
8480  assert(weights[0] <= consdata->capacity);
8481  assert(consdata->weightsum > consdata->capacity);
8482  /* reset the reductionsum */
8483  reductionsum = 0;
8484  }
8485  else if( v == 0 )
8486  {
8487  assert(weights[nvars - 2] == 1);
8488  }
8489  }
8490  else
8491  {
8492  SCIP_Longint minweight = weights[nvars - 1];
8493  SCIP_Longint newweight = dualcapacity - minweight;
8494  SCIP_Longint restsumweights = 0;
8495  SCIP_Longint sumcoef;
8496  SCIP_Bool sumcoefcase = FALSE;
8497  int startv = v;
8498  int end;
8499  int k;
8500 
8501  assert(weights[nvars - 1] + weights[nvars - 2] <= capacity);
8502 
8503  /* reduce big weights of pairs that exceed the dualcapacity
8504  *
8505  * e.g. 9x1 + 9x2 + 6x3 + 4x4 + 4x5 + 4x6 <= 27 <=> 9~x1 + 9~x2 + 6~x3 + 4~x4 + 4~x5 + 4~x6 >= 9
8506  * <=> 9~x1 + 9~x2 + 5~x3 + 4~x4 + 4~x5 + 4~x6 >= 9
8507  * <=> 9x1 + 9x2 + 5x3 + 4x4 + 4x5 + 4x6 <= 27
8508  */
8509  while( weights[v] > newweight )
8510  {
8511  reductionsum += (weights[v] - newweight);
8512  consdataChgWeight(consdata, v, newweight);
8513  ++v;
8514  assert(v < nvars);
8515  }
8516  (*nchgcoefs) += (v - startv);
8517 
8518  /* skip equal weights */
8519  while( weights[v] == newweight )
8520  ++v;
8521 
8522  if( v > 0 )
8523  {
8524  for( w = v; w < nvars; ++w )
8525  restsumweights += weights[w];
8526  }
8527  else
8528  restsumweights = consdata->weightsum;
8529 
8530  if( restsumweights < dualcapacity )
8531  {
8532  /* we found redundant variables, which does not influence the feasibility of any integral solution, e.g.
8533  *
8534  * +61x1 + 61x2 + 61x3 + 61x4 + 61x5 + 61x6 + 35x7 + 10x8 <= 350 <=>
8535  * +61~x1 + 61~x2 + 61~x3 + 61~x4 + 61~x5 + 61~x6 + 35~x7 + 10~x8 >= 61
8536  */
8537  if( startv == v )
8538  {
8539  /* remove redundant variables */
8540  for( w = nvars - 1; w >= v; --w )
8541  {
8542  SCIP_CALL( delCoefPos(scip, cons, v) );
8543  ++(*nchgcoefs);
8544  }
8545 
8546 #ifndef NDEBUG
8547  /* each coefficients should exceed the dualcapacity by itself */
8548  for( ; w >= 0; --w )
8549  assert(weights[w] == dualcapacity);
8550 #endif
8551  /* for performance reasons we do not update the capacity(, i.e. reduce it by reductionsum) and directly
8552  * upgrade this constraint
8553  */
8554  SCIP_CALL( upgradeCons(scip, cons, ndelconss, naddconss) );
8555  assert(SCIPconsIsDeleted(cons));
8556 
8557  return SCIP_OKAY;
8558  }
8559 
8560  /* special case where we have three different coefficient types
8561  *
8562  * e.g. 9x1 + 9x2 + 6x3 + 6x4 + 4x5 + 4x6 <= 29 <=> 9~x1 + 9~x2 + 6~x3 + 6~x4 + 4~x5 + 4~x6 >= 9
8563  * <=> 9~x1 + 9~x2 + 5~x3 + 5~x4 + 4~x5 + 4~x6 >= 9
8564  * <=> 3~x1 + 3~x2 + 2~x3 + 2~x4 + ~x5 + ~x6 >= 3
8565  * <=> 3x1 + 3x2 + 2x3 + 2x4 + x5 + x6 <= 9
8566  */
8567  if( weights[v] > 1 || (weights[startv] > (SCIP_Longint)nvars - v) || (startv > 0 && weights[0] == (SCIP_Longint)nvars - v + 1) )
8568  {
8569  SCIP_Longint newcap;
8570 
8571  /* adjust smallest coefficients, which all together do not exceed the dualcapacity */
8572  for( w = nvars - 1; w >= v; --w )
8573  {
8574  if( weights[w] > 1 )
8575  {
8576  consdataChgWeight(consdata, w, 1LL);
8577  ++(*nchgcoefs);
8578  }
8579  }
8580 
8581  /* adjust middle sized coefficients, which when choosing also one small coefficients exceed the
8582  * dualcapacity
8583  */
8584  newweight = (SCIP_Longint)nvars - v;
8585  assert(newweight > 1);
8586  for( ; w >= startv; --w )
8587  {
8588  if( weights[w] > newweight )
8589  {
8590  consdataChgWeight(consdata, w, newweight);
8591  ++(*nchgcoefs);
8592  }
8593  else
8594  assert(weights[w] == newweight);
8595  }
8596 
8597  /* adjust big sized coefficients, where each of them exceeds the dualcapacity by itself */
8598  ++newweight;
8599  assert(newweight > 2);
8600  for( ; w >= 0; --w )
8601  {
8602  if( weights[w] > newweight )
8603  {
8604  consdataChgWeight(consdata, w, newweight);
8605  ++(*nchgcoefs);
8606  }
8607  else
8608  assert(weights[w] == newweight);
8609  }
8610 
8611  /* update the capacity */
8612  newcap = ((SCIP_Longint)startv - 1) * newweight + ((SCIP_Longint)v - startv) * (newweight - 1) + ((SCIP_Longint)nvars - v);
8613  if( consdata->capacity > newcap )
8614  {
8615  consdata->capacity = newcap;
8616  ++(*nchgsides);
8617  }
8618  else
8619  assert(consdata->capacity == newcap);
8620  }
8621  assert(weights[v] == 1 && (weights[startv] == (SCIP_Longint)nvars - v) && (startv == 0 || weights[0] == (SCIP_Longint)nvars - v + 1));
8622 
8623  /* the new dualcapacity should still be equal to the (nvars - v + 1) */
8624  assert(consdata->weightsum - consdata->capacity == (SCIP_Longint)nvars - v + 1);
8625 
8626  /* weight should be still sorted, because the reduction preserves this */
8627 #ifndef NDEBUG
8628  for( w = nvars - 1; w > 0; --w )
8629  assert(weights[w] <= weights[w - 1]);
8630 #endif
8631  consdata->sorted = TRUE;
8632 
8633  return SCIP_OKAY;
8634  }
8635 
8636  /* check if all rear items have the same weight as the last one, so we cannot tighten the constraint further */
8637  end = nvars - 2;
8638  while( end >= 0 && weights[end] == weights[end + 1] )
8639  {
8640  assert(end >= v);
8641  --end;
8642  }
8643 
8644  if( v >= end )
8645  goto TERMINATE;
8646 
8647  end = nvars - 2;
8648 
8649  /* can we stop early, another special reduction case might exist */
8650  if( 2 * weights[end] > dualcapacity )
8651  {
8652  restsumweights = 0;
8653 
8654  /* determine capacity of the small items */
8655  for( w = end + 1; w < nvars; ++w )
8656  restsumweights += weights[w];
8657 
8658  if( restsumweights * 2 <= dualcapacity )
8659  {
8660  /* check for further posssible reductions in the middle */
8661  while( v < end && restsumweights + weights[v] >= dualcapacity )
8662  ++v;
8663 
8664  if( v >= end )
8665  goto TERMINATE;
8666 
8667  /* dualcapacity is even, we can set the middle weights to dualcapacity/2 */
8668  if( (dualcapacity & 1) == 0 )
8669  {
8670  newweight = dualcapacity / 2;
8671  startv = v;
8672 
8673  /* set all middle coefficients */
8674  for( ; v <= end; ++v )
8675  {
8676  if( weights[v] > newweight )
8677  {
8678  reductionsum += (weights[v] - newweight);
8679  consdataChgWeight(consdata, v, newweight);
8680  ++(*nchgcoefs);
8681  }
8682  }
8683  }
8684  /* dualcapacity is odd, we can set the middle weights to dualcapacity but therefor need to multiply all
8685  * other coefficients by 2
8686  */
8687  else
8688  {
8689  /* correct the reductionsum */
8690  reductionsum *= 2;
8691 
8692  /* multiply big coefficients by 2 */
8693  for( w = 0; w < v; ++w )
8694  {
8695  consdataChgWeight(consdata, w, weights[w] * 2);
8696  }
8697 
8698  newweight = dualcapacity;
8699  /* set all middle coefficients */
8700  for( ; v <= end; ++v )
8701  {
8702  reductionsum += (2 * weights[v] - newweight);
8703  consdataChgWeight(consdata, v, newweight);
8704  }
8705 
8706  /* multiply small coefficients by 2 */
8707  for( w = end + 1; w < nvars; ++w )
8708  {
8709  consdataChgWeight(consdata, w, weights[w] * 2);
8710  }
8711  (*nchgcoefs) += nvars;
8712 
8713  dualcapacity *= 2;
8714  consdata->capacity *= 2;
8715  ++(*nchgsides);
8716  }
8717  }
8718 
8719  goto TERMINATE;
8720  }
8721 
8722  /* further reductions using the next possible coefficient sum
8723  *
8724  * e.g. 9x1 + 8x2 + 7x3 + 3x4 + x5 <= 19 <=> 9~x1 + 8~x2 + 7~x3 + 3~x4 + ~x5 >= 9
8725  * <=> 9~x1 + 8~x2 + 6~x3 + 3~x4 + ~x5 >= 9
8726  * <=> 9x1 + 8x2 + 6x3 + 3x4 + x5 <= 18
8727  */
8728  /* @todo loop for "k" can be extended, same coefficient when determine next sumcoef can be left out */
8729  for( k = 0; k < 4; ++k )
8730  {
8731  /* determine next minimal coefficient sum */
8732  switch( k )
8733  {
8734  case 0:
8735  sumcoef = weights[nvars - 1] + weights[nvars - 2];
8736  break;
8737  case 1:
8738  assert(nvars >= 3);
8739  sumcoef = weights[nvars - 1] + weights[nvars - 3];
8740  break;
8741  case 2:
8742  assert(nvars >= 4);
8743  if( weights[nvars - 1] + weights[nvars - 4] < weights[nvars - 2] + weights[nvars - 3] )
8744  {
8745  sumcoefcase = TRUE;
8746  sumcoef = weights[nvars - 1] + weights[nvars - 4];
8747  }
8748  else
8749  {
8750  sumcoefcase = FALSE;
8751  sumcoef = weights[nvars - 2] + weights[nvars - 3];
8752  }
8753  break;
8754  case 3:
8755  assert(nvars >= 5);
8756  if( sumcoefcase )
8757  {
8758  sumcoef = MIN(weights[nvars - 1] + weights[nvars - 5], weights[nvars - 2] + weights[nvars - 3]);
8759  }
8760  else
8761  {
8762  sumcoef = MIN(weights[nvars - 1] + weights[nvars - 4], weights[nvars - 1] + weights[nvars - 2] + weights[nvars - 3]);
8763  }
8764  break;
8765  default:
8766  return SCIP_ERROR;
8767  }
8768 
8769  /* tighten next coefficients that, pair with the current small coefficient, exceed the dualcapacity */
8770  minweight = weights[end];
8771  while( minweight <= sumcoef )
8772  {
8773  newweight = dualcapacity - minweight;
8774  startv = v;
8775  assert(v < nvars);
8776 
8777  /* @todo check for further reductions, when two times the minweight exceeds the dualcapacity */
8778  /* shrink big coefficients */
8779  while( weights[v] + minweight > dualcapacity && 2 * minweight <= dualcapacity )
8780  {
8781  reductionsum += (weights[v] - newweight);
8782  consdataChgWeight(consdata, v, newweight);
8783  ++v;
8784  assert(v < nvars);
8785  }
8786  (*nchgcoefs) += (v - startv);
8787 
8788  /* skip unchangable weights */
8789  while( weights[v] + minweight == dualcapacity )
8790  {
8791  assert(v < nvars);
8792  ++v;
8793  }
8794 
8795  --end;
8796  /* skip same end weights */
8797  while( end >= 0 && weights[end] == weights[end + 1] )
8798  --end;
8799 
8800  if( v >= end )
8801  goto TERMINATE;
8802 
8803  minweight = weights[end];
8804  }
8805 
8806  if( v >= end )
8807  goto TERMINATE;
8808 
8809  /* now check if a combination of small coefficients allows us to tighten big coefficients further */
8810  if( sumcoef < minweight )
8811  {
8812  minweight = sumcoef;
8813  newweight = dualcapacity - minweight;
8814  startv = v;
8815  assert(v < nvars);
8816 
8817  /* shrink big coefficients */
8818  while( weights[v] + minweight > dualcapacity && 2 * minweight <= dualcapacity )
8819  {
8820  reductionsum += (weights[v] - newweight);
8821  consdataChgWeight(consdata, v, newweight);
8822  ++v;
8823  assert(v < nvars);
8824  }
8825  (*nchgcoefs) += (v - startv);
8826 
8827  /* skip unchangable weights */
8828  while( weights[v] + minweight == dualcapacity )
8829  {
8830  assert(v < nvars);
8831  ++v;
8832  }
8833  }
8834 
8835  if( v >= end )
8836  goto TERMINATE;
8837 
8838  /* can we stop early, another special reduction case might exist */
8839  if( 2 * weights[end] > dualcapacity )
8840  {
8841  restsumweights = 0;
8842 
8843  /* determine capacity of the small items */
8844  for( w = end + 1; w < nvars; ++w )
8845  restsumweights += weights[w];
8846 
8847  if( restsumweights * 2 <= dualcapacity )
8848  {
8849  /* check for further posssible reductions in the middle */
8850  while( v < end && restsumweights + weights[v] >= dualcapacity )
8851  ++v;
8852 
8853  if( v >= end )
8854  goto TERMINATE;
8855 
8856  /* dualcapacity is even, we can set the middle weights to dualcapacity/2 */
8857  if( (dualcapacity & 1) == 0 )
8858  {
8859  newweight = dualcapacity / 2;
8860  startv = v;
8861 
8862  /* set all middle coefficients */
8863  for( ; v <= end; ++v )
8864  {
8865  if( weights[v] > newweight )
8866  {
8867  reductionsum += (weights[v] - newweight);
8868  consdataChgWeight(consdata, v, newweight);
8869  ++(*nchgcoefs);
8870  }
8871  }
8872  }
8873  /* dualcapacity is odd, we can set the middle weights to dualcapacity but therefor need to multiply all
8874  * other coefficients by 2
8875  */
8876  else
8877  {
8878  /* correct the reductionsum */
8879  reductionsum *= 2;
8880 
8881  /* multiply big coefficients by 2 */
8882  for( w = 0; w < v; ++w )
8883  {
8884  consdataChgWeight(consdata, w, weights[w] * 2);
8885  }
8886 
8887  newweight = dualcapacity;
8888  /* set all middle coefficients */
8889  for( ; v <= end; ++v )
8890  {
8891  reductionsum += (2 * weights[v] - newweight);
8892  consdataChgWeight(consdata, v, newweight);
8893  }
8894 
8895  /* multiply small coefficients by 2 */
8896  for( w = end + 1; w < nvars; ++w )
8897  {
8898  consdataChgWeight(consdata, w, weights[w] * 2);
8899  }
8900  (*nchgcoefs) += nvars;
8901 
8902  dualcapacity *= 2;
8903  consdata->capacity *= 2;
8904  ++(*nchgsides);
8905  }
8906  }
8907 
8908  goto TERMINATE;
8909  }
8910 
8911  /* cannot tighten any further */
8912  if( 2 * sumcoef > dualcapacity )
8913  goto TERMINATE;
8914  }
8915  }
8916  }
8917 
8918 
8919  TERMINATE:
8920  /* correct capacity */
8921  if( reductionsum > 0 )
8922  {
8923  assert(v > 0);
8924 
8925  consdata->capacity -= reductionsum;
8926  ++(*nchgsides);
8927 
8928  assert(consdata->weightsum - dualcapacity == consdata->capacity);
8929  }
8930  assert(weights[0] <= consdata->capacity);
8931 
8932  /* weight should be still sorted, because the reduction preserves this */
8933 #ifndef NDEBUG
8934  for( w = nvars - 1; w > 0; --w )
8935  assert(weights[w] <= weights[w - 1]);
8936 #endif
8937  consdata->sorted = TRUE;
8938 
8939  if( oldnchgcoefs < *nchgcoefs )
8940  {
8941  assert(!SCIPconsIsDeleted(cons));
8942 
8943  /* it might be that we can divide the weights by their greatest common divisor */
8944  normalizeWeights(cons, nchgcoefs, nchgsides);
8945  }
8946  else
8947  {
8948  assert(oldnchgcoefs == *nchgcoefs);
8949  assert(oldnchgsides == *nchgsides);
8950  }
8951 
8952  return SCIP_OKAY;
8953 }
8954 
8955 
8956 /** fixes variables with weights bigger than the capacity and delete redundant constraints, also sort weights */
8957 static
8959  SCIP* scip, /**< SCIP data structure */
8960  SCIP_CONS* cons, /**< knapsack constraint */
8961  int* nfixedvars, /**< pointer to store the amount of fixed variables */
8962  int* ndelconss, /**< pointer to store the amount of deleted constraints */
8963  int* nchgcoefs /**< pointer to store the amount of changed coefficients */
8964  )
8965 {
8966  SCIP_VAR** vars;
8967  SCIP_CONSDATA* consdata;
8968  SCIP_Longint* weights;
8969  SCIP_Longint capacity;
8970  SCIP_Bool infeasible;
8971  SCIP_Bool fixed;
8972  int nvars;
8973  int v;
8974 
8975  assert(scip != NULL);
8976  assert(cons != NULL);
8977  assert(nfixedvars != NULL);
8978  assert(ndelconss != NULL);
8979  assert(nchgcoefs != NULL);
8980 
8981  consdata = SCIPconsGetData(cons);
8982  assert(consdata != NULL);
8983 
8984  nvars = consdata->nvars;
8985 
8986  /* no variables left, then delete constraint */
8987  if( nvars == 0 )
8988  {
8989  assert(consdata->capacity >= 0);
8990 
8991  SCIP_CALL( SCIPdelCons(scip, cons) );
8992  ++(*ndelconss);
8993 
8994  return SCIP_OKAY;
8995  }
8996 
8997  /* sort items */
8998  sortItems(consdata);
8999 
9000  vars = consdata->vars;
9001  weights = consdata->weights;
9002  capacity = consdata->capacity;
9003  v = 0;
9004 
9005  /* check for weights bigger than the capacity */
9006  while( weights[v] > capacity )
9007  {
9008  SCIP_CALL( SCIPfixVar(scip, vars[v], 0.0, &infeasible, &fixed) );
9009  assert(!infeasible);
9010 
9011  if( fixed )
9012  ++(*nfixedvars);
9013 
9014  ++v;
9015  }
9016 
9017  /* if we fixed at least one variable we need to delete them from the constraint */
9018  if( v > 0 )
9019  {
9020  if( v == nvars )
9021  {
9022  SCIP_CALL( SCIPdelCons(scip, cons) );
9023  ++(*ndelconss);
9024 
9025  return SCIP_OKAY;
9026  }
9027 
9028  /* delete all position from back to front */
9029  for( --v; v >= 0; --v )
9030  {
9031  SCIP_CALL( delCoefPos(scip, cons, v) );
9032  ++(*nchgcoefs);
9033  }
9034 
9035  /* sort items again because of deletion */
9036  sortItems(consdata);
9037  assert(vars == consdata->vars);
9038  assert(weights == consdata->weights);
9039  }
9040  assert(consdata->sorted);
9041  assert(weights[0] <= capacity);
9042 
9043  if( !SCIPisHugeValue(scip, (SCIP_Real) capacity) && consdata->weightsum <= capacity )
9044  {
9045  SCIP_CALL( SCIPdelCons(scip, cons) );
9046  ++(*ndelconss);
9047  }
9048 
9049  return SCIP_OKAY;
9050 }
9051 
9052 
9053 /** tries to simplify weights and delete redundant variables in knapsack a^Tx <= capacity
9054  *
9055  * 1. use the duality between a^Tx <= capacity <=> -a^T~x <= capacity - weightsum to tighten weights, e.g.
9056  *
9057  * 11x1 + 10x2 + 7x3 + 5x4 + 5x5 <= 25 <=> -10~x1 - 10~x2 - 7~x3 - 5~x4 - 5~x5 <= -13
9058  *
9059  * the above constraint can be changed to
9060  *
9061  * -8~x1 - 8~x2 - 7~x3 - 5~x4 - 5~x5 <= -12 <=> 8x1 + 8x2 + 7x3 + 5x4 + 5x5 <= 20
9062  *
9063  * 2. if variables in a constraint do not affect the (in-)feasibility of the constraint, we can delte them, e.g.
9064  *
9065  * 7x1 + 6x2 + 5x3 + 5x4 + x5 + x6 <= 20 => x5 and x6 are redundant and can be removed
9066  *
9067  * 3. Tries to use gcd information an all but one weight to change this not-included weight and normalize the
9068  * constraint further, e.g.
9069  *
9070  * 9x1 + 6x2 + 6x3 + 5x4 <= 13 => 9x1 + 6x2 + 6x3 + 6x4 <= 12 => 3x1 + 2x2 + 2x3 + 2x4 <= 4 => 4x1 + 2x2 + 2x3 + 2x4 <= 4
9071  * => 2x1 + x2 + x3 + x4 <= 2
9072  * 9x1 + 6x2 + 6x3 + 7x4 <= 13 => 9x1 + 6x2 + 6x3 + 6x4 <= 12 => see above
9073  */
9074 static
9076  SCIP* scip, /**< SCIP data structure */
9077  SCIP_CONS* cons, /**< knapsack constraint */
9078  int* nfixedvars, /**< pointer to store the amount of fixed variables */
9079  int* ndelconss, /**< pointer to store the amount of deleted constraints */
9080  int* nchgcoefs, /**< pointer to store the amount of changed coefficients */
9081  int* nchgsides, /**< pointer to store the amount of changed sides */
9082  int* naddconss, /**< pointer to count number of added constraints */
9083  SCIP_Bool* cutoff /**< pointer to store whether the node can be cut off */
9084  )
9085 {
9086  SCIP_VAR** vars;
9087  SCIP_CONSDATA* consdata;
9088  SCIP_Longint* weights;
9089  SCIP_Longint restweight;
9090  SCIP_Longint newweight;
9091  SCIP_Longint weight;
9092  SCIP_Longint oldgcd;
9093  SCIP_Longint rest;
9094  SCIP_Longint gcd;
9095  int oldnchgcoefs;
9096  int oldnchgsides;
9097  int candpos;
9098  int candpos2;
9099  int nvars;
9100  int v;
9101 
9102  assert(scip != NULL);
9103  assert(cons != NULL);
9104  assert(nfixedvars != NULL);
9105  assert(ndelconss != NULL);
9106  assert(nchgcoefs != NULL);
9107  assert(nchgsides != NULL);
9108  assert(naddconss != NULL);
9109  assert(cutoff != NULL);
9110  assert(!SCIPconsIsModifiable(cons));
9111 
9112  consdata = SCIPconsGetData(cons);
9113  assert( consdata != NULL );
9114 
9115  *cutoff = FALSE;
9116 
9117  /* remove double enties and also combinations of active and negated variables */
9118  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
9119  assert(consdata->merged);
9120  if( *cutoff )
9121  return SCIP_OKAY;
9122 
9123  assert(consdata->capacity >= 0);
9124 
9125  /* fix variables with big coefficients and remove redundant constraints, sort weights */
9126  SCIP_CALL( prepareCons(scip, cons, nfixedvars, ndelconss, nchgcoefs) );
9127 
9128  if( SCIPconsIsDeleted(cons) )
9129  return SCIP_OKAY;
9130 
9131  if( !SCIPisHugeValue(scip, (SCIP_Real) consdata->capacity) )
9132  {
9133  /* 1. dual weights tightening */
9134  SCIP_CALL( dualWeightsTightening(scip, cons, ndelconss, nchgcoefs, nchgsides, naddconss) );
9135 
9136  if( SCIPconsIsDeleted(cons) )
9137  return SCIP_OKAY;
9138 
9139  /* 2. delete redundant variables */
9140  SCIP_CALL( detectRedundantVars(scip, cons, ndelconss, nchgcoefs, nchgsides, naddconss) );
9141 
9142  if( SCIPconsIsDeleted(cons) )
9143  return SCIP_OKAY;
9144  }
9145 
9146  assert(consdata->sorted);
9147 
9148  vars = consdata->vars;
9149  weights = consdata->weights;
9150  nvars = consdata->nvars;
9151 
9152  /* determine greatest common divisor */
9153  gcd = weights[nvars - 1];
9154  for( v = nvars - 2; v >= 0 && gcd > 1; --v )
9155  {
9156  gcd = SCIPcalcGreComDiv(gcd, weights[v]);
9157  }
9158 
9159  /* divide the constraint by their greatest common divisor */
9160  if( gcd >= 2 )
9161  {
9162  for( v = nvars - 1; v >= 0; --v )
9163  {
9164  consdataChgWeight(consdata, v, weights[v]/gcd);
9165  }
9166  (*nchgcoefs) += nvars;
9167 
9168  consdata->capacity /= gcd;
9169  (*nchgsides)++;
9170 
9171  consdata->sorted = TRUE;
9172  }
9173  assert(consdata->nvars == nvars);
9174  assert(consdata->sorted);
9175 
9176  /* 3. start gcd procedure for all variables */
9177  do
9178  {
9179  oldnchgcoefs = *nchgcoefs;
9180  oldnchgsides = *nchgsides;
9181 
9182  vars = consdata->vars;
9183  weights = consdata->weights;
9184  nvars = consdata->nvars;
9185 
9186  /* stop if we have two coefficients which are one in absolute value */
9187  if( weights[nvars - 1] == 1 && weights[nvars - 2] == 1 )
9188  return SCIP_OKAY;
9189 
9190  gcd = -1;
9191  candpos = -1;
9192  candpos2 = -1;
9193 
9194  /* calculate greatest common divisor over all integer and binary variables and determine the candidate where we might
9195  * change the coefficient
9196  */
9197  for( v = nvars - 1; v >= 0; --v )
9198  {
9199  weight = weights[v];
9200  assert(weight >= 1);
9201 
9202  oldgcd = gcd;
9203 
9204  if( gcd == -1 )
9205  {
9206  gcd = weights[v];
9207  assert(gcd >= 1);
9208  }
9209  else
9210  {
9211  /* calculate greatest common divisor for all variables */
9212  gcd = SCIPcalcGreComDiv(gcd, weight);
9213  }
9214 
9215  /* if the greatest commmon divisor has become 1, we might have found the possible coefficient to change or we
9216  * can terminate
9217  */
9218  if( gcd == 1 )
9219  {
9220  /* found candidate */
9221  if( candpos == -1 )
9222  {
9223  gcd = oldgcd;
9224  candpos = v;
9225 
9226  /* if both first coefficients have a gcd of 1, both are candidates for the coefficient change */
9227  if( v == nvars - 2 )
9228  candpos2 = v + 1;
9229  }
9230  /* two different variables lead to a gcd of one, so we cannot change a coefficient */
9231  else
9232  {
9233  if( candpos == v + 1 && candpos2 == v + 2 )
9234  {
9235  assert(candpos2 == nvars - 1);
9236 
9237  /* take new candidates */
9238  candpos = candpos2;
9239 
9240  /* recalculate gcd from scratch */
9241  gcd = weights[v+1];
9242  assert(gcd >= 1);
9243 
9244  /* calculate greatest common divisor for variables */
9245  gcd = SCIPcalcGreComDiv(gcd, weights[v]);
9246  if( gcd == 1 )
9247  return SCIP_OKAY;
9248  }
9249  else
9250  /* cannot determine a possible coefficient for reduction */
9251  return SCIP_OKAY;
9252  }
9253  }
9254  }
9255  assert(gcd >= 2);
9256 
9257  /* we should have found one coefficient, that led to a gcd of 1, otherwise we could normalize the constraint
9258  * further
9259  */
9260  assert(candpos >= 0 && candpos < nvars);
9261 
9262  /* determine the remainder of the capacity and the gcd */
9263  rest = consdata->capacity % gcd;
9264  assert(rest >= 0);
9265  assert(rest < gcd);
9266 
9267  /* determine the remainder of the coefficient candidate and the gcd */
9268  restweight = weights[candpos] % gcd;
9269  assert(restweight >= 1);
9270  assert(restweight < gcd);
9271 
9272  if( rest > 0 )
9273  {
9274  /* replace old with new capacity */
9275  consdata->capacity -= rest;
9276  ++(*nchgsides);
9277  }
9278 
9279  /* calculate new coefficient */
9280  if( restweight > rest )
9281  newweight = weights[candpos] - restweight + gcd;
9282  else
9283  newweight = weights[candpos] - restweight;
9284 
9285  assert(newweight == 0 || SCIPcalcGreComDiv(gcd, newweight) == gcd);
9286 
9287  SCIPdebugMessage("gcd = %"SCIP_LONGINT_FORMAT", rest = %"SCIP_LONGINT_FORMAT", restweight = %"SCIP_LONGINT_FORMAT"; changing weight of variable <%s> to %"SCIP_LONGINT_FORMAT" and reduced capacity by %"SCIP_LONGINT_FORMAT"\n", gcd, rest, restweight, SCIPvarGetName(vars[candpos]), newweight, rest);
9288 
9289  if( newweight == 0 )
9290  {
9291  /* delete redundant coefficient */
9292  SCIP_CALL( delCoefPos(scip, cons, candpos) );
9293  assert(consdata->nvars == nvars - 1);
9294  --nvars;
9295  }
9296  else
9297  {
9298  /* replace old with new coefficient */
9299  consdataChgWeight(consdata, candpos, newweight);
9300  }
9301  ++(*nchgcoefs);
9302 
9303  assert(consdata->vars == vars);
9304  assert(consdata->nvars == nvars);
9305  assert(consdata->weights == weights);
9306 
9307  /* now constraint can be normalized, dividing it by the gcd */
9308  for( v = nvars - 1; v >= 0; --v )
9309  {
9310  consdataChgWeight(consdata, v, weights[v]/gcd);
9311  }
9312  (*nchgcoefs) += nvars;
9313 
9314  consdata->capacity /= gcd;
9315  ++(*nchgsides);
9316 
9317  SCIPdebugPrintCons(scip, cons, NULL);
9318 
9319  SCIPdebugMessage("we did %d coefficient changes and %d side changes on constraint %s when applying one round of the gcd algorithm\n", *nchgcoefs - oldnchgcoefs, *nchgsides - oldnchgsides, SCIPconsGetName(cons));
9320  }
9321  while( nvars >= 2 );
9322 
9323  return SCIP_OKAY;
9324 }
9325 
9326 /** deletes all fixed variables from knapsack constraint, and replaces variables with binary representatives */
9327 static
9329  SCIP* scip, /**< SCIP data structure */
9330  SCIP_CONS* cons, /**< knapsack constraint */
9331  SCIP_Bool* cutoff /**< pointer to store whether the node can be cut off, or NULL if this
9332  * information is not needed; in this case, we apply all fixings
9333  * instead of stopping after the first infeasible one */
9334  )
9335 {
9336  SCIP_CONSDATA* consdata;
9337  int v;
9338 
9339  assert(scip != NULL);
9340  assert(cons != NULL);
9341 
9342  consdata = SCIPconsGetData(cons);
9343  assert(consdata != NULL);
9344  assert(consdata->nvars == 0 || consdata->vars != NULL);
9345 
9346  if( cutoff != NULL )
9347  *cutoff = FALSE;
9348 
9349  SCIPdebugMessage("apply fixings:\n");
9350  SCIPdebugPrintCons(scip, cons, NULL);
9351 
9352  /* check infeasibility */
9353  if ( consdata->onesweightsum > consdata->capacity )
9354  {
9355  SCIPdebugMessage("apply fixings detected cutoff.\n");
9356 
9357  if( cutoff != NULL )
9358  *cutoff = TRUE;
9359 
9360  return SCIP_OKAY;
9361  }
9362 
9363  /* all multi-aggregations should be resolved */
9364  consdata->existmultaggr = FALSE;
9365 
9366  v = 0;
9367  while( v < consdata->nvars )
9368  {
9369  SCIP_VAR* var;
9370 
9371  var = consdata->vars[v];
9372  assert(SCIPvarIsBinary(var));
9373 
9374  if( SCIPvarGetLbGlobal(var) > 0.5 )
9375  {
9376  assert(SCIPisFeasEQ(scip, SCIPvarGetUbGlobal(var), 1.0));
9377  consdata->capacity -= consdata->weights[v];
9378  SCIP_CALL( delCoefPos(scip, cons, v) );
9379  consdata->cliquesadded = FALSE; /* reduced capacity might lead to larger cliques */
9380  }
9381  else if( SCIPvarGetUbGlobal(var) < 0.5 )
9382  {
9383  assert(SCIPisFeasEQ(scip, SCIPvarGetLbGlobal(var), 0.0));
9384  SCIP_CALL( delCoefPos(scip, cons, v) );
9385  }
9386  else
9387  {
9388  SCIP_VAR* repvar;
9389  SCIP_VAR* negvar;
9390  SCIP_VAR* workvar;
9391  SCIP_Longint weight;
9392  SCIP_Bool negated;
9393 
9394  weight = consdata->weights[v];
9395 
9396  /* get binary representative of variable */
9397  SCIP_CALL( SCIPgetBinvarRepresentative(scip, var, &repvar, &negated) );
9398  assert(repvar != NULL);
9399 
9400  /* check for multi-aggregation */
9401  if( SCIPvarIsNegated(repvar) )
9402  {
9403  workvar = SCIPvarGetNegatedVar(repvar);
9404  assert(workvar != NULL);
9405  negated = TRUE;
9406  }
9407  else
9408  {
9409  workvar = repvar;
9410  negated = FALSE;
9411  }
9412 
9413  /* @todo maybe resolve the problem that the eliminating of the multi-aggregation leads to a non-knapsack
9414  * constraint (converting into a linear constraint), for example the multi-aggregation consist of a non-binary
9415  * variable or due to resolving now their are non-integral coefficients or a non-integral capacity
9416  *
9417  * If repvar is not negated so workvar = repvar, otherwise workvar = 1 - repvar. This means,
9418  * weight * workvar = weight * (a_1*y_1 + ... + a_n*y_n + c)
9419  *
9420  * The explanation for the following block:
9421  * 1a) If repvar is a multi-aggregated variable weight * repvar should be replaced by
9422  * weight * (a_1*y_1 + ... + a_n*y_n + c).
9423  * 1b) If repvar is a negated variable of a multi-aggregated variable weight * repvar should be replaced by
9424  * weight - weight * (a_1*y_1 + ... + a_n*y_n + c), for better further use here we switch the sign of weight
9425  * so now we have the replacement -weight + weight * (a_1*y_1 + ... + a_n*y_n + c).
9426  * 2) For all replacement variable we check:
9427  * 2a) weight * a_i < 0 than we add -weight * a_i * y_i_neg to the constraint and adjust the capacity through
9428  * capacity -= weight * a_i caused by the negation of y_i.
9429  * 2b) weight * a_i >= 0 than we add weight * a_i * y_i to the constraint.
9430  * 3a) If repvar was not negated we need to subtract weight * c from capacity.
9431  * 3b) If repvar was negated we need to subtract weight * (c - 1) from capacity(note we switched the sign of
9432  * weight in this case.
9433  */
9434  if( SCIPvarGetStatus(workvar) == SCIP_VARSTATUS_MULTAGGR )
9435  {
9436  SCIP_VAR** aggrvars;
9437  SCIP_Real* aggrscalars;
9438  SCIP_Real aggrconst;
9439  int naggrvars;
9440  int i;
9441 
9442  SCIP_CALL( SCIPflattenVarAggregationGraph(scip, workvar) );
9443  naggrvars = SCIPvarGetMultaggrNVars(workvar);
9444  aggrvars = SCIPvarGetMultaggrVars(workvar);
9445  aggrscalars = SCIPvarGetMultaggrScalars(workvar);
9446  aggrconst = SCIPvarGetMultaggrConstant(workvar);
9447  assert((aggrvars != NULL && aggrscalars != NULL) || naggrvars == 0);
9448 
9449  if( !SCIPisIntegral(scip, weight * aggrconst) )
9450  {
9451  SCIPerrorMessage("try to resolve a multi-aggregation with a non-integral value for weight*aggrconst = %g\n", weight*aggrconst);
9452  return SCIP_ERROR;
9453  }
9454 
9455  /* if workvar was negated, we have to flip the weight */
9456  if( negated )
9457  weight *= -1;
9458 
9459  for( i = naggrvars - 1; i >= 0; --i )
9460  {
9461  assert(aggrvars != NULL);
9462  assert(aggrscalars != NULL);
9463 
9464  if( !SCIPvarIsBinary(aggrvars[i]) )
9465  {
9466  SCIPerrorMessage("try to resolve a multi-aggregation with a non-binary variable <%s>\n", aggrvars[i]);
9467  return SCIP_ERROR;
9468  }
9469  if( !SCIPisIntegral(scip, weight * aggrscalars[i]) )
9470  {
9471  SCIPerrorMessage("try to resolve a multi-aggregation with a non-integral value for weight*aggrscalars = %g\n", weight*aggrscalars[i]);
9472  return SCIP_ERROR;
9473  }
9474  /* if the new coefficient is smaller than zero, we need to add the negated variable instead and adjust the capacity */
9475  if( SCIPisNegative(scip, weight * aggrscalars[i]) )
9476  {
9477  SCIP_CALL( SCIPgetNegatedVar(scip, aggrvars[i], &negvar));
9478  assert(negvar != NULL);
9479  SCIP_CALL( addCoef(scip, cons, negvar, (SCIP_Longint)(SCIPfloor(scip, -weight * aggrscalars[i] + 0.5))) );
9480  consdata->capacity -= (SCIP_Longint)(SCIPfloor(scip, weight * aggrscalars[i] + 0.5));
9481  }
9482  else
9483  {
9484  SCIP_CALL( addCoef(scip, cons, aggrvars[i], (SCIP_Longint)(SCIPfloor(scip, weight * aggrscalars[i] + 0.5))) );
9485  }
9486  }
9487  /* delete old coefficient */
9488  SCIP_CALL( delCoefPos(scip, cons, v) );
9489 
9490  /* adjust the capacity with the aggregation constant and if necessary the extra weight through the negation */
9491  if( negated )
9492  consdata->capacity -= (SCIP_Longint)SCIPfloor(scip, weight * (aggrconst - 1) + 0.5);
9493  else
9494  consdata->capacity -= (SCIP_Longint)SCIPfloor(scip, weight * aggrconst + 0.5);
9495 
9496  if( consdata->capacity < 0 )
9497  {
9498  if( cutoff != NULL )
9499  {
9500  *cutoff = TRUE;
9501  break;
9502  }
9503  }
9504  }
9505  /* check, if the variable should be replaced with the representative */
9506  else if( repvar != var )
9507  {
9508  /* delete old (aggregated) variable */
9509  SCIP_CALL( delCoefPos(scip, cons, v) );
9510 
9511  /* add representative instead */
9512  SCIP_CALL( addCoef(scip, cons, repvar, weight) );
9513  }
9514  else
9515  ++v;
9516  }
9517  }
9518  assert(consdata->onesweightsum == 0);
9519 
9520  SCIPdebugMessage("after applyFixings, before merging:\n");
9521  SCIPdebugPrintCons(scip, cons, NULL);
9522 
9523  /* if aggregated variables have been replaced, multiple entries of the same variable are possible and we have to
9524  * clean up the constraint
9525  */
9526  if( cutoff != NULL && !(*cutoff) )
9527  {
9528  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
9529  SCIPdebugMessage("after applyFixings and merging:\n");
9530  SCIPdebugPrintCons(scip, cons, NULL);
9531  }
9532 
9533  return SCIP_OKAY;
9534 }
9535 
9536 /** inserts an element into the list of binary zero implications */
9537 static
9539  SCIP* scip, /**< SCIP data structure */
9540  int** liftcands, /**< array of the lifting candidates */
9541  int* nliftcands, /**< number of lifting candidates */
9542  int** firstidxs, /**< array of first zeroitems indices */
9543  SCIP_Longint** zeroweightsums, /**< array of sums of weights of the implied-to-zero items */
9544  int** zeroitems, /**< pointer to zero items array */
9545  int** nextidxs, /**< pointer to array of next zeroitems indeces */
9546  int* zeroitemssize, /**< pointer to size of zero items array */
9547  int* nzeroitems, /**< pointer to length of zero items array */
9548  int probindex, /**< problem index of variable y in implication y == v -> x == 0 */
9549  SCIP_Bool value, /**< value v of variable y in implication */
9550  int knapsackidx, /**< index of variable x in knapsack */
9551  SCIP_Longint knapsackweight, /**< weight of variable x in knapsack */
9552  SCIP_Bool* memlimitreached /**< pointer to store whether the memory limit was reached */
9553  )
9554 {
9555  int nzeros;
9556 
9557  assert(liftcands != NULL);
9558  assert(liftcands[value] != NULL);
9559  assert(nliftcands != NULL);
9560  assert(firstidxs != NULL);
9561  assert(firstidxs[value] != NULL);
9562  assert(zeroweightsums != NULL);
9563  assert(zeroweightsums[value] != NULL);
9564  assert(zeroitems != NULL);
9565  assert(nextidxs != NULL);
9566  assert(zeroitemssize != NULL);
9567  assert(nzeroitems != NULL);
9568  assert(*nzeroitems <= *zeroitemssize);
9569  assert(0 <= probindex && probindex < SCIPgetNVars(scip) - SCIPgetNContVars(scip));
9570  assert(memlimitreached != NULL);
9571 
9572  nzeros = *nzeroitems;
9573 
9574  /* allocate enough memory */
9575  if( nzeros == *zeroitemssize )
9576  {
9577  /* we explicitly construct the complete implication graph where the knapsack variables are involved;
9578  * this can be too huge - abort on memory limit
9579  */
9580  if( *zeroitemssize >= MAX_ZEROITEMS_SIZE )
9581  {
9582  SCIPdebugMessage("memory limit of %d bytes reached in knapsack preprocessing - abort collecting zero items\n",
9583  *zeroitemssize);
9584  *memlimitreached = TRUE;
9585  return SCIP_OKAY;
9586  }
9587  *zeroitemssize *= 2;
9588  *zeroitemssize = MIN(*zeroitemssize, MAX_ZEROITEMS_SIZE);
9589  SCIP_CALL( SCIPreallocBufferArray(scip, zeroitems, *zeroitemssize) );
9590  SCIP_CALL( SCIPreallocBufferArray(scip, nextidxs, *zeroitemssize) );
9591  }
9592  assert(nzeros < *zeroitemssize);
9593 
9594  if( *memlimitreached )
9595  *memlimitreached = FALSE;
9596 
9597  /* insert element */
9598  (*zeroitems)[nzeros] = knapsackidx;
9599  (*nextidxs)[nzeros] = firstidxs[value][probindex];
9600  if( firstidxs[value][probindex] == 0 )
9601  {
9602  liftcands[value][nliftcands[value]] = probindex;
9603  ++nliftcands[value];
9604  }
9605  firstidxs[value][probindex] = nzeros;
9606  ++(*nzeroitems);
9607  zeroweightsums[value][probindex] += knapsackweight;
9608 
9609  return SCIP_OKAY;
9610 }
9611 
9612 #define MAX_CLIQUELENGTH 50
9613 /** applies rule (3) of the weight tightening procedure, which can lift other variables into the knapsack:
9614  * (3) for a clique C let C(xi == v) := C \ {j: xi == v -> xj == 0}),
9615  * let cliqueweightsum(xi == v) := sum(W(C(xi == v)))
9616  * if cliqueweightsum(xi == v) < capacity:
9617  * - fixing variable xi to v would make the knapsack constraint redundant
9618  * - the weight of the variable or its negation (depending on v) can be increased as long as it has the same
9619  * redundancy effect:
9620  * wi' := capacity - cliqueweightsum(xi == v)
9621  * this rule can also be applied to binary variables not in the knapsack!
9622  */
9623 static
9625  SCIP* scip, /**< SCIP data structure */
9626  SCIP_CONS* cons, /**< knapsack constraint */
9627  int* nchgcoefs, /**< pointer to count total number of changed coefficients */
9628  SCIP_Bool* cutoff /**< pointer to store whether the node can be cut off */
9629  )
9630 {
9631  SCIP_CONSDATA* consdata;
9632  SCIP_VAR** binvars;
9633  int nbinvars;
9634  int* liftcands[2]; /* binary variables that have at least one entry in zeroitems */
9635  int* firstidxs[2]; /* first index in zeroitems for each binary variable/value pair, or zero for empty list */
9636  SCIP_Longint* zeroweightsums[2]; /* sums of weights of the implied-to-zero items */
9637  int* zeroitems; /* item number in knapsack that is implied to zero */
9638  int* nextidxs; /* next index in zeroitems for the same binary variable, or zero for end of list */
9639  int zeroitemssize;
9640  int nzeroitems;
9641  SCIP_Bool* zeroiteminserted[2];
9642  SCIP_Bool memlimitreached;
9643  int nliftcands[2];
9644  SCIP_Bool* cliqueused;
9645  SCIP_Bool* itemremoved;
9646  SCIP_Longint maxcliqueweightsum;
9647  SCIP_VAR** addvars;
9648  SCIP_Longint* addweights;
9649  SCIP_Longint addweightsum;
9650  int nvars;
9651  int cliquenum;
9652  int naddvars;
9653  int val;
9654  int i;
9655 
9656  int* tmpindices;
9657  SCIP_Bool* tmpboolindices;
9658  int* tmpindices2;
9659  SCIP_Bool* tmpboolindices2;
9660  int* tmpindices3;
9661  SCIP_Bool* tmpboolindices3;
9662  int tmp;
9663  int tmp2;
9664  int tmp3;
9665  SCIP_CONSHDLR* conshdlr;
9666  SCIP_CONSHDLRDATA* conshdlrdata;
9667 
9668  assert(nchgcoefs != NULL);
9669  assert(!SCIPconsIsModifiable(cons));
9670 
9671  consdata = SCIPconsGetData(cons);
9672  assert(consdata != NULL);
9673  assert(consdata->row == NULL); /* we are in presolve, so no LP row exists */
9674  assert(consdata->weightsum > consdata->capacity); /* otherwise, the constraint is redundant */
9675  assert(consdata->nvars > 0);
9676  assert(consdata->merged);
9677 
9678  nvars = consdata->nvars;
9679 
9680  /* check if the knapsack has too many items/cliques for applying this costly method */
9681  if( (!consdata->cliquepartitioned && nvars > MAX_USECLIQUES_SIZE) || consdata->ncliques > MAX_USECLIQUES_SIZE )
9682  return SCIP_OKAY;
9683 
9684  /* sort items, s.t. the heaviest one is in the first position */
9685  sortItems(consdata);
9686 
9687  if( !consdata->cliquepartitioned && nvars > MAX_USECLIQUES_SIZE )
9688  return SCIP_OKAY;
9689 
9690  /* we have to consider all integral variables since even integer and implicit integer variables can have binary bounds */
9691  nbinvars = SCIPgetNVars(scip) - SCIPgetNContVars(scip);
9692  assert(nbinvars > 0);
9693  binvars = SCIPgetVars(scip);
9694 
9695  /* get conshdlrdata to use cleared memory */
9696  conshdlr = SCIPconsGetHdlr(cons);
9697  assert(conshdlr != NULL);
9698  conshdlrdata = SCIPconshdlrGetData(conshdlr);
9699  assert(conshdlrdata != NULL);
9700 
9701  /* allocate temporary memory for the list of implied to zero variables */
9702  zeroitemssize = MIN(nbinvars, MAX_ZEROITEMS_SIZE); /* initial size of zeroitems buffer */
9703  SCIP_CALL( SCIPallocBufferArray(scip, &liftcands[0], nbinvars) );
9704  SCIP_CALL( SCIPallocBufferArray(scip, &liftcands[1], nbinvars) );
9705 
9706  assert(conshdlrdata->ints1size > 0);
9707  assert(conshdlrdata->ints2size > 0);
9708  assert(conshdlrdata->longints1size > 0);
9709  assert(conshdlrdata->longints2size > 0);
9710 
9711  /* next if conditions should normally not be true, because it means that presolving has created more binary variables
9712  * than binary + integer variables existed at the presolving initialization method, but for example if you would
9713  * transform all integers into their binary representation then it maybe happens
9714  */
9715  if( conshdlrdata->ints1size < nbinvars )
9716  {
9717  int oldsize;
9718  oldsize = conshdlrdata->ints1size;
9719 
9720  while( conshdlrdata->ints1size < nbinvars )
9721  conshdlrdata->ints1size *= 2;
9722  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->ints1, conshdlrdata->ints1size) );
9723  BMSclearMemoryArray(&(conshdlrdata->ints1[oldsize]), conshdlrdata->ints1size - oldsize); /*lint !e866*/
9724  }
9725  if( conshdlrdata->ints2size < nbinvars )
9726  {
9727  int oldsize;
9728  oldsize = conshdlrdata->ints2size;
9729 
9730  while( conshdlrdata->ints2size < nbinvars )
9731  conshdlrdata->ints2size *= 2;
9732  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->ints2, conshdlrdata->ints2size) );
9733  BMSclearMemoryArray(&(conshdlrdata->ints2[oldsize]), conshdlrdata->ints2size - oldsize); /*lint !e866*/
9734  }
9735  if( conshdlrdata->longints1size < nbinvars )
9736  {
9737  int oldsize;
9738  oldsize = conshdlrdata->longints1size;
9739 
9740  while( conshdlrdata->longints1size < nbinvars )
9741  conshdlrdata->longints1size *= 2;
9742  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->longints1, conshdlrdata->longints1size) );
9743  BMSclearMemoryArray(&(conshdlrdata->longints1[oldsize]), conshdlrdata->longints1size - oldsize); /*lint !e866*/
9744  }
9745  if( conshdlrdata->longints2size < nbinvars )
9746  {
9747  int oldsize;
9748  oldsize = conshdlrdata->longints2size;
9749 
9750  while( conshdlrdata->longints2size < nbinvars )
9751  conshdlrdata->longints2size *= 2;
9752  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->longints2, conshdlrdata->longints2size) );
9753  BMSclearMemoryArray(&(conshdlrdata->longints2[oldsize]), conshdlrdata->longints2size - oldsize); /*lint !e866*/
9754  }
9755 
9756  firstidxs[0] = conshdlrdata->ints1;
9757  firstidxs[1] = conshdlrdata->ints2;
9758  zeroweightsums[0] = conshdlrdata->longints1;
9759  zeroweightsums[1] = conshdlrdata->longints2;
9760 
9761  /* check for cleared arrays, all entries are zero */
9762 #ifndef NDEBUG
9763  for( tmp = nbinvars - 1; tmp >= 0; --tmp )
9764  {
9765  assert(firstidxs[0][tmp] == 0);
9766  assert(firstidxs[1][tmp] == 0);
9767  assert(zeroweightsums[0][tmp] == 0);
9768  assert(zeroweightsums[1][tmp] == 0);
9769  }
9770 #endif
9771 
9772  SCIP_CALL( SCIPallocBufferArray(scip, &zeroitems, zeroitemssize) );
9773  SCIP_CALL( SCIPallocBufferArray(scip, &nextidxs, zeroitemssize) );
9774 
9775  zeroitems[0] = -1; /* dummy element */
9776  nextidxs[0] = -1;
9777  nzeroitems = 1;
9778  nliftcands[0] = 0;
9779  nliftcands[1] = 0;
9780 
9781  assert(conshdlrdata->bools1size > 0);
9782  assert(conshdlrdata->bools2size > 0);
9783 
9784  /* next if conditions should normally not be true, because it means that presolving has created more binary variables
9785  * than binary + integer variables existed at the presolving initialization method, but for example if you would
9786  * transform all integers into their binary representation then it maybe happens
9787  */
9788  if( conshdlrdata->bools1size < nbinvars )
9789  {
9790  int oldsize;
9791  oldsize = conshdlrdata->bools1size;
9792 
9793  while( conshdlrdata->bools1size < nbinvars )
9794  conshdlrdata->bools1size *= 2;
9795  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->bools1, conshdlrdata->bools1size) );
9796  BMSclearMemoryArray(&(conshdlrdata->bools1[oldsize]), conshdlrdata->bools1size - oldsize); /*lint !e866*/
9797  }
9798  if( conshdlrdata->bools2size < nbinvars )
9799  {
9800  int oldsize;
9801  oldsize = conshdlrdata->bools2size;
9802 
9803  while( conshdlrdata->bools2size < nbinvars )
9804  conshdlrdata->bools2size *= 2;
9805  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->bools2, conshdlrdata->bools2size) );
9806  BMSclearMemoryArray(&(conshdlrdata->bools2[oldsize]), conshdlrdata->bools2size - oldsize); /*lint !e866*/
9807  }
9808 
9809  zeroiteminserted[0] = conshdlrdata->bools1;
9810  zeroiteminserted[1] = conshdlrdata->bools2;
9811 
9812  /* check for cleared arrays, all entries are zero */
9813 #ifndef NDEBUG
9814  for( tmp = nbinvars - 1; tmp >= 0; --tmp )
9815  {
9816  assert(zeroiteminserted[0][tmp] == 0);
9817  assert(zeroiteminserted[1][tmp] == 0);
9818  }
9819 #endif
9820 
9821  SCIP_CALL( SCIPallocBufferArray(scip, &tmpindices, 2 * nbinvars) );
9822  SCIP_CALL( SCIPallocBufferArray(scip, &tmpboolindices, 2 * nbinvars) );
9823  SCIP_CALL( SCIPallocBufferArray(scip, &tmpindices2, 2 * nbinvars) );
9824  SCIP_CALL( SCIPallocBufferArray(scip, &tmpboolindices2, 2 * nbinvars) );
9825  SCIP_CALL( SCIPallocBufferArray(scip, &tmpindices3, consdata->nvars) );
9826  SCIP_CALL( SCIPallocBufferArray(scip, &tmpboolindices3, consdata->nvars) );
9827  tmp2 = 0;
9828  tmp3 = 0;
9829 
9830  memlimitreached = FALSE;
9831  for( i = 0; i < consdata->nvars && !memlimitreached; ++i )
9832  {
9833  SCIP_VAR* var;
9834  SCIP_Longint weight;
9835  SCIP_Bool value;
9836  int varprobindex;
9837  SCIP_VAR** implvars;
9838  SCIP_BOUNDTYPE* impltypes;
9839  int nimpls;
9840  SCIP_CLIQUE** cliques;
9841  int ncliques;
9842  int j;
9843 
9844  tmp = 0;
9845 
9846  /* get corresponding active problem variable */
9847  var = consdata->vars[i];
9848  weight = consdata->weights[i];
9849  value = TRUE;
9850  SCIP_CALL( SCIPvarGetProbvarBinary(&var, &value) );
9851  varprobindex = SCIPvarGetProbindex(var);
9852  assert(0 <= varprobindex && varprobindex < nbinvars);
9853 
9854  /* update the zeroweightsum */
9855  zeroweightsums[!value][varprobindex] += weight; /*lint !e514*/
9856  tmpboolindices3[tmp3] = !value;
9857  tmpindices3[tmp3] = varprobindex;
9858  ++tmp3;
9859 
9860  /* initialize the arrays of inserted zero items */
9861  /* first add the implications (~x == 1 -> x == 0) */
9862  {
9863  SCIP_Bool implvalue;
9864  int probindex;
9865 
9866  probindex = SCIPvarGetProbindex(var);
9867  assert(0 <= probindex && probindex < nbinvars);
9868 
9869  implvalue = !value;
9870 
9871  /* insert the item into the list of the implied variable/value */
9872  assert( !zeroiteminserted[implvalue][probindex] );
9873 
9874  if( firstidxs[implvalue][probindex] == 0 )
9875  {
9876  tmpboolindices2[tmp2] = implvalue;
9877  tmpindices2[tmp2] = probindex;
9878  ++tmp2;
9879  }
9880  SCIP_CALL( insertZerolist(scip, liftcands, nliftcands, firstidxs, zeroweightsums,
9881  &zeroitems, &nextidxs, &zeroitemssize, &nzeroitems, probindex, implvalue, i, weight,
9882  &memlimitreached) );
9883  zeroiteminserted[implvalue][probindex] = TRUE;
9884  tmpboolindices[tmp] = implvalue;
9885  tmpindices[tmp] = probindex;
9886  ++tmp;
9887  }
9888 
9889  /* get implications of the knapsack item fixed to one: x == 1 -> y == (1-v);
9890  * the negation of these implications (y == v -> x == 0) are the ones that we are interested in
9891  */
9892  nimpls = SCIPvarGetNBinImpls(var, value);
9893  implvars = SCIPvarGetImplVars(var, value);
9894  impltypes = SCIPvarGetImplTypes(var, value);
9895 
9896  for( j = 0; j < nimpls && !memlimitreached; ++j )
9897  {
9898  int probindex;
9899  SCIP_Bool implvalue;
9900 
9901  assert(SCIPvarIsBinary(implvars[j]));
9902  probindex = SCIPvarGetProbindex(implvars[j]);
9903  assert(probindex < nbinvars);
9904 
9905  /* this assert should hold, but if not there is a old continue later on */
9906  assert(probindex >= 0);
9907 
9908  /* consider only implications with active implvar */
9909  if( probindex < 0 )
9910  continue;
9911 
9912  implvalue = (impltypes[j] == SCIP_BOUNDTYPE_UPPER); /* the negation of the implication */
9913 
9914  /* insert the item into the list of the implied variable/value */
9915  if( !zeroiteminserted[implvalue][probindex] )
9916  {
9917  if( firstidxs[implvalue][probindex] == 0 )
9918  {
9919  tmpboolindices2[tmp2] = implvalue;
9920  tmpindices2[tmp2] = probindex;
9921  ++tmp2;
9922  }
9923  SCIP_CALL( insertZerolist(scip, liftcands, nliftcands, firstidxs, zeroweightsums,
9924  &zeroitems, &nextidxs, &zeroitemssize, &nzeroitems, probindex, implvalue, i, weight,
9925  &memlimitreached) );
9926  zeroiteminserted[implvalue][probindex] = TRUE;
9927  tmpboolindices[tmp] = implvalue;
9928  tmpindices[tmp] = probindex;
9929  ++tmp;
9930  }
9931  }
9932 
9933  /* get the cliques where the knapsack item is member of with value 1 */
9934  ncliques = SCIPvarGetNCliques(var, value);
9935  cliques = SCIPvarGetCliques(var, value);
9936  for( j = 0; j < ncliques && !memlimitreached; ++j )
9937  {
9938  SCIP_VAR** cliquevars;
9939  SCIP_Bool* cliquevalues;
9940  int ncliquevars;
9941  int k;
9942 
9943  ncliquevars = SCIPcliqueGetNVars(cliques[j]);
9944 
9945  /* discard big cliques */
9946  if( ncliquevars > MAX_CLIQUELENGTH )
9947  continue;
9948 
9949  cliquevars = SCIPcliqueGetVars(cliques[j]);
9950  cliquevalues = SCIPcliqueGetValues(cliques[j]);
9951 
9952  for( k = ncliquevars - 1; k >= 0; --k )
9953  {
9954  SCIP_Bool implvalue;
9955  int probindex;
9956 
9957  if( var == cliquevars[k] )
9958  continue;
9959 
9960  probindex = SCIPvarGetProbindex(cliquevars[k]);
9961  assert(0 <= probindex && probindex < nbinvars);
9962  implvalue = cliquevalues[k];
9963 
9964  /* insert the item into the list of the clique variable/value */
9965  if( !zeroiteminserted[implvalue][probindex] )
9966  {
9967  if( firstidxs[implvalue][probindex] == 0 )
9968  {
9969  tmpboolindices2[tmp2] = implvalue;
9970  tmpindices2[tmp2] = probindex;
9971  ++tmp2;
9972  }
9973 
9974  SCIP_CALL( insertZerolist(scip, liftcands, nliftcands, firstidxs, zeroweightsums,
9975  &zeroitems, &nextidxs, &zeroitemssize, &nzeroitems, probindex, implvalue, i, weight,
9976  &memlimitreached) );
9977  zeroiteminserted[implvalue][probindex] = TRUE;
9978  tmpboolindices[tmp] = implvalue;
9979  tmpindices[tmp] = probindex;
9980  ++tmp;
9981 
9982  if( memlimitreached )
9983  break;
9984  }
9985  }
9986  }
9987  /* clear zeroiteminserted */
9988  for( --tmp; tmp >= 0; --tmp)
9989  zeroiteminserted[tmpboolindices[tmp]][tmpindices[tmp]] = FALSE;
9990  }
9991  SCIPfreeBufferArray(scip, &tmpboolindices);
9992 
9993  /* calculate the clique partition and the maximal sum of weights using the clique information */
9994  assert(consdata->sorted);
9995  SCIP_CALL( calcCliquepartition(scip, consdata, TRUE, FALSE) );
9996 
9997  assert(conshdlrdata->bools3size > 0);
9998 
9999  /* next if condition should normally not be true, because it means that presolving has created more binary variables
10000  * in one constraint than binary + integer variables existed in the whole problem at the presolving initialization
10001  * method, but for example if you would transform all integers into their binary representation then it maybe happens
10002  */
10003  if( conshdlrdata->bools3size < consdata->nvars )
10004  {
10005  int oldsize;
10006  oldsize = conshdlrdata->bools3size;
10007 
10008  while( conshdlrdata->bools3size < consdata->nvars )
10009  conshdlrdata->bools3size *= 2;
10010  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->bools3, conshdlrdata->bools3size) );
10011  BMSclearMemoryArray(&(conshdlrdata->bools3[oldsize]), conshdlrdata->bools3size - oldsize); /*lint !e866*/
10012  }
10013 
10014  cliqueused = conshdlrdata->bools3;
10015 
10016  /* check for cleared array, all entries are zero */
10017 #ifndef NDEBUG
10018  for( tmp = consdata->nvars - 1; tmp >= 0; --tmp )
10019  assert(cliqueused[tmp] == 0);
10020 #endif
10021 
10022  maxcliqueweightsum = 0;
10023  tmp = 0;
10024 
10025  /* calculates maximal weight of cliques */
10026  for( i = 0; i < consdata->nvars; ++i )
10027  {
10028  cliquenum = consdata->cliquepartition[i];
10029  assert(0 <= cliquenum && cliquenum < consdata->nvars);
10030 
10031  if( !cliqueused[cliquenum] )
10032  {
10033  maxcliqueweightsum += consdata->weights[i];
10034  cliqueused[cliquenum] = TRUE;
10035  tmpindices[tmp] = cliquenum;
10036  ++tmp;
10037  }
10038  }
10039  /* clear cliqueused */
10040  for( --tmp; tmp >= 0; --tmp)
10041  cliqueused[tmp] = FALSE;
10042 
10043  assert(conshdlrdata->bools4size > 0);
10044 
10045  /* next if condition should normally not be true, because it means that presolving has created more binary variables
10046  * in one constraint than binary + integer variables existed in the whole problem at the presolving initialization
10047  * method, but for example if you would transform all integers into their binary representation then it maybe happens
10048  */
10049  if( conshdlrdata->bools4size < consdata->nvars )
10050  {
10051  int oldsize;
10052  oldsize = conshdlrdata->bools4size;
10053 
10054  while( conshdlrdata->bools4size < consdata->nvars )
10055  conshdlrdata->bools4size *= 2;
10056  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->bools4, conshdlrdata->bools4size) );
10057  BMSclearMemoryArray(&conshdlrdata->bools4[oldsize], conshdlrdata->bools4size - oldsize); /*lint !e866*/
10058  }
10059 
10060  itemremoved = conshdlrdata->bools4;
10061 
10062  /* check for cleared array, all entries are zero */
10063 #ifndef NDEBUG
10064  for( tmp = consdata->nvars - 1; tmp >= 0; --tmp )
10065  assert(itemremoved[tmp] == 0);
10066 #endif
10067 
10068  /* for each binary variable xi and each fixing v, calculate the cliqueweightsum and update the weight of the
10069  * variable in the knapsack (this is sequence-dependent because the new or modified weights have to be
10070  * included in subsequent cliqueweightsum calculations)
10071  */
10072  SCIP_CALL( SCIPallocBufferArray(scip, &addvars, 2*nbinvars) );
10073  SCIP_CALL( SCIPallocBufferArray(scip, &addweights, 2*nbinvars) );
10074  naddvars = 0;
10075  addweightsum = 0;
10076  for( val = 0; val < 2 && addweightsum < consdata->capacity; ++val )
10077  {
10078  for( i = 0; i < nliftcands[val] && addweightsum < consdata->capacity; ++i )
10079  {
10080  SCIP_Longint cliqueweightsum;
10081  int probindex;
10082  int idx;
10083  int j;
10084 
10085  tmp = 0;
10086 
10087  probindex = liftcands[val][i];
10088  assert(0 <= probindex && probindex < nbinvars);
10089 
10090  /* ignore empty zero lists and variables that cannot be lifted anyways */
10091  if( firstidxs[val][probindex] == 0
10092  || maxcliqueweightsum - zeroweightsums[val][probindex] + addweightsum >= consdata->capacity )
10093  continue;
10094 
10095  /* mark the items that are implied to zero by setting the current variable to the current value */
10096  for( idx = firstidxs[val][probindex]; idx != 0; idx = nextidxs[idx] )
10097  {
10098  assert(0 < idx && idx < nzeroitems);
10099  assert(0 <= zeroitems[idx] && zeroitems[idx] < consdata->nvars);
10100  itemremoved[zeroitems[idx]] = TRUE;
10101  }
10102 
10103  /* calculate the residual cliqueweight sum */
10104  cliqueweightsum = addweightsum; /* the previously added items are single-element cliques */
10105  for( j = 0; j < consdata->nvars; ++j )
10106  {
10107  cliquenum = consdata->cliquepartition[j];
10108  assert(0 <= cliquenum && cliquenum < consdata->nvars);
10109  if( !itemremoved[j] )
10110  {
10111  if( !cliqueused[cliquenum] )
10112  {
10113  cliqueweightsum += consdata->weights[j];
10114  cliqueused[cliquenum] = TRUE;
10115  tmpindices[tmp] = cliquenum;
10116  ++tmp;
10117  }
10118 
10119  if( cliqueweightsum >= consdata->capacity )
10120  break;
10121  }
10122  }
10123 
10124  /* check if the weight of the variable/value can be increased */
10125  if( cliqueweightsum < consdata->capacity )
10126  {
10127  SCIP_VAR* var;
10128  SCIP_Longint weight;
10129 
10130  /* insert the variable (with value TRUE) in the list of additional items */
10131  assert(naddvars < 2*nbinvars);
10132  var = binvars[probindex];
10133  if( val == FALSE )
10134  {
10135  SCIP_CALL( SCIPgetNegatedVar(scip, var, &var) );
10136  }
10137  weight = consdata->capacity - cliqueweightsum;
10138  addvars[naddvars] = var;
10139  addweights[naddvars] = weight;
10140  addweightsum += weight;
10141  naddvars++;
10142 
10143  SCIPdebugMessage("knapsack constraint <%s>: adding lifted item %"SCIP_LONGINT_FORMAT"<%s>\n",
10144  SCIPconsGetName(cons), weight, SCIPvarGetName(var));
10145  }
10146 
10147  /* clear itemremoved */
10148  for( idx = firstidxs[val][probindex]; idx != 0; idx = nextidxs[idx] )
10149  {
10150  assert(0 < idx && idx < nzeroitems);
10151  assert(0 <= zeroitems[idx] && zeroitems[idx] < consdata->nvars);
10152  itemremoved[zeroitems[idx]] = FALSE;
10153  }
10154  /* clear cliqueused */
10155  for( --tmp; tmp >= 0; --tmp)
10156  cliqueused[tmpindices[tmp]] = FALSE;
10157  }
10158  }
10159  SCIPfreeBufferArray(scip, &tmpindices);
10160 
10161  /* clear part of zeroweightsums */
10162  for( --tmp3; tmp3 >= 0; --tmp3)
10163  zeroweightsums[tmpboolindices3[tmp3]][tmpindices3[tmp3]] = 0;
10164 
10165  /* clear rest of zeroweightsums and firstidxs */
10166  for( --tmp2; tmp2 >= 0; --tmp2)
10167  {
10168  zeroweightsums[tmpboolindices2[tmp2]][tmpindices2[tmp2]] = 0;
10169  firstidxs[tmpboolindices2[tmp2]][tmpindices2[tmp2]] = 0;
10170  }
10171 
10172  SCIPfreeBufferArray(scip, &tmpindices2);
10173  SCIPfreeBufferArray(scip, &tmpindices3);
10174  SCIPfreeBufferArray(scip, &tmpboolindices2);
10175  SCIPfreeBufferArray(scip, &tmpboolindices3);
10176 
10177  /* add all additional item weights */
10178  for( i = 0; i < naddvars; ++i )
10179  {
10180  SCIP_CALL( addCoef(scip, cons, addvars[i], addweights[i]) );
10181  }
10182  *nchgcoefs += naddvars;
10183 
10184  if( naddvars > 0 )
10185  {
10186  /* if new items were added, multiple entries of the same variable are possible and we have to clean up the constraint */
10187  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
10188  }
10189 
10190  /* free temporary memory */
10191  SCIPfreeBufferArray(scip, &addweights);
10192  SCIPfreeBufferArray(scip, &addvars);
10193  SCIPfreeBufferArray(scip, &nextidxs);
10194  SCIPfreeBufferArray(scip, &zeroitems);
10195  SCIPfreeBufferArray(scip, &liftcands[1]);
10196  SCIPfreeBufferArray(scip, &liftcands[0]);
10197 
10198  return SCIP_OKAY;
10199 }
10200 
10201 /** tightens item weights and capacity in presolving:
10202  * given a knapsack sum(wi*xi) <= capacity
10203  * (1) let weightsum := sum(wi)
10204  * if weightsum - wi < capacity:
10205  * - not using item i would make the knapsack constraint redundant
10206  * - wi and capacity can be changed to have the same redundancy effect and the same results for
10207  * fixing xi to zero or one, but with a reduced wi and tightened capacity to tighten the LP relaxation
10208  * - change coefficients:
10209  * wi' := weightsum - capacity
10210  * capacity' := capacity - (wi - wi')
10211  * (2) increase weights from front to back(sortation is necessary) if there is no space left for another weight
10212  * - determine the four(can be adjusted) minimal weightsums of the knapsack, i.e. in increasing order
10213  * weights[nvars - 1], weights[nvars - 2], MIN(weights[nvars - 3], weights[nvars - 1] + weights[nvars - 2]),
10214  * MIN(MAX(weights[nvars - 3], weights[nvars - 1] + weights[nvars - 2]), weights[nvars - 4]), note that there
10215  * can be multiple times the same weight, this can be improved
10216  * - check if summing up a minimal weightsum with a big weight exceeds the capacity, then we can increase the big
10217  * weight, to capacity - lastmininmalweightsum, e.g. :
10218  * 19x1 + 15x2 + 10x3 + 5x4 + 5x5 <= 19
10219  * -> minimal weightsums: 5, 5, 10, 10
10220  * -> 15 + 5 > 19 => increase 15 to 19 - 0 = 19
10221  * -> 10 + 10 > 19 => increase 10 to 19 - 5 = 14, resulting in
10222  * 19x1 + 19x2 + 14x3 + 5x4 + 5x5 <= 19
10223  * (3) let W(C) be the maximal weight of clique C,
10224  * cliqueweightsum := sum(W(C))
10225  * if cliqueweightsum - W(C) < capacity:
10226  * - not using any item of C would make the knapsack constraint redundant
10227  * - weights wi, i in C, and capacity can be changed to have the same redundancy effect and the same results for
10228  * fixing xi, i in C, to zero or one, but with a reduced wi and tightened capacity to tighten the LP relaxation
10229  * - change coefficients:
10230  * delta := capacity - (cliqueweightsum - W(C))
10231  * wi' := max(wi - delta, 0)
10232  * capacity' := capacity - delta
10233  * This rule has to add the used cliques in order to ensure they are enforced - otherwise, the reduction might
10234  * introduce infeasible solutions.
10235  * (4) for a clique C let C(xi == v) := C \ {j: xi == v -> xj == 0}),
10236  * let cliqueweightsum(xi == v) := sum(W(C(xi == v)))
10237  * if cliqueweightsum(xi == v) < capacity:
10238  * - fixing variable xi to v would make the knapsack constraint redundant
10239  * - the weight of the variable or its negation (depending on v) can be increased as long as it has the same
10240  * redundancy effect:
10241  * wi' := capacity - cliqueweightsum(xi == v)
10242  * This rule can also be applied to binary variables not in the knapsack!
10243  * (5) if min{w} + wi > capacity:
10244  * - using item i would force to fix other items to zero
10245  * - wi can be increased to the capacity
10246  */
10247 static
10249  SCIP* scip, /**< SCIP data structure */
10250  SCIP_CONS* cons, /**< knapsack constraint */
10251  int* nchgcoefs, /**< pointer to count total number of changed coefficients */
10252  int* nchgsides, /**< pointer to count number of side changes */
10253  int* naddconss, /**< pointer to count number of added constraints */
10254  int* ndelconss, /**< pointer to count number of deleted constraints */
10255  SCIP_Bool* cutoff /**< pointer to store whether the node can be cut off */
10256  )
10257 {
10258  SCIP_CONSHDLRDATA* conshdlrdata;
10259  SCIP_CONSDATA* consdata;
10260  SCIP_Longint* weights;
10261  SCIP_Longint sumcoef;
10262  SCIP_Longint capacity;
10263  SCIP_Longint newweight;
10264  SCIP_Longint maxweight;
10265  SCIP_Longint minweight;
10266  SCIP_Bool sumcoefcase = FALSE;
10267  int startpos;
10268  int backpos;
10269  int nvars;
10270  int pos;
10271  int k;
10272  int i;
10273 
10274  assert(nchgcoefs != NULL);
10275  assert(nchgsides != NULL);
10276  assert(!SCIPconsIsModifiable(cons));
10277 
10278  conshdlrdata = SCIPconshdlrGetData(SCIPconsGetHdlr(cons));
10279  assert(conshdlrdata != NULL);
10280 
10281  consdata = SCIPconsGetData(cons);
10282  assert(consdata != NULL);
10283  assert(consdata->row == NULL); /* we are in presolve, so no LP row exists */
10284  assert(consdata->onesweightsum == 0); /* all fixed variables should have been removed */
10285  assert(consdata->weightsum > consdata->capacity); /* otherwise, the constraint is redundant */
10286  assert(consdata->nvars > 0);
10287 
10288  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
10289  if( *cutoff )
10290  return SCIP_OKAY;
10291 
10292  /* apply rule (1) */
10293  do
10294  {
10295  assert(consdata->merged);
10296 
10297  /* sort items, s.t. the heaviest one is in the first position */
10298  sortItems(consdata);
10299 
10300  for( i = 0; i < consdata->nvars; ++i )
10301  {
10302  SCIP_Longint weight;
10303 
10304  weight = consdata->weights[i];
10305  if( consdata->weightsum - weight < consdata->capacity )
10306  {
10307  newweight = consdata->weightsum - consdata->capacity;
10308  consdataChgWeight(consdata, i, newweight);
10309  consdata->capacity -= (weight - newweight);
10310  (*nchgcoefs)++;
10311  (*nchgsides)++;
10312  assert(!consdata->sorted);
10313  SCIPdebugMessage("knapsack constraint <%s>: changed weight of <%s> from %"SCIP_LONGINT_FORMAT" to %"SCIP_LONGINT_FORMAT", capacity from %"SCIP_LONGINT_FORMAT" to %"SCIP_LONGINT_FORMAT"\n",
10314  SCIPconsGetName(cons), SCIPvarGetName(consdata->vars[i]), weight, newweight,
10315  consdata->capacity + (weight-newweight), consdata->capacity);
10316  }
10317  else
10318  break;
10319  }
10320  }
10321  while( !consdata->sorted && consdata->weightsum > consdata->capacity );
10322 
10323  /* check for redundancy */
10324  if( consdata->weightsum <= consdata->capacity )
10325  return SCIP_OKAY;
10326 
10327  pos = 0;
10328  while( pos < consdata->nvars && consdata->weights[pos] == consdata->capacity )
10329  ++pos;
10330 
10331  sumcoef = 0;
10332  weights = consdata->weights;
10333  nvars = consdata->nvars;
10334  capacity = consdata->capacity;
10335 
10336  if( pos < nvars && weights[pos] + weights[pos + 1] > capacity )
10337  {
10338  /* further reductions using the next possible coefficient sum
10339  *
10340  * e.g. 19x1 + 15x2 + 10x3 + 5x4 + 5x5 <= 19 <=> 19x1 + 19x2 + 14x3 + 5x4 + 5x5 <= 19
10341  */
10342  /* @todo loop for "k" can be extended, same coefficient when determine next sumcoef can be left out */
10343  for( k = 0; k < 4; ++k )
10344  {
10345  newweight = capacity - sumcoef;
10346 
10347  /* determine next minimal coefficient sum */
10348  switch( k )
10349  {
10350  case 0:
10351  sumcoef = weights[nvars - 1];
10352  backpos = nvars - 1;
10353  break;
10354  case 1:
10355  sumcoef = weights[nvars - 2];
10356  backpos = nvars - 2;
10357  break;
10358  case 2:
10359  if( weights[nvars - 3] < weights[nvars - 1] + weights[nvars - 2] )
10360  {
10361  sumcoefcase = TRUE;
10362  sumcoef = weights[nvars - 3];
10363  backpos = nvars - 3;
10364  }
10365  else
10366  {
10367  sumcoefcase = FALSE;
10368  sumcoef = weights[nvars - 1] + weights[nvars - 2];
10369  backpos = nvars - 2;
10370  }
10371  break;
10372  case 3:
10373  if( sumcoefcase )
10374  {
10375  if( weights[nvars - 4] < weights[nvars - 1] + weights[nvars - 2] )
10376  {
10377  sumcoef = weights[nvars - 4];
10378  backpos = nvars - 4;
10379  }
10380  else
10381  {
10382  sumcoef = weights[nvars - 1] + weights[nvars - 2];
10383  backpos = nvars - 2;
10384  }
10385  }
10386  else
10387  {
10388  sumcoef = weights[nvars - 3];
10389  backpos = nvars - 3;
10390  }
10391  break;
10392  default:
10393  return SCIP_ERROR;
10394  }
10395 
10396  if( backpos <= pos )
10397  break;
10398 
10399  /* tighten next coefficients that, paired with the current small coefficient, exceed the capacity */
10400  maxweight = weights[pos];
10401  startpos = pos;
10402  while( 2 * maxweight > capacity && maxweight + sumcoef > capacity )
10403  {
10404  assert(newweight > weights[pos]);
10405 
10406  SCIPdebugMessage("in constraint <%s> changing weight %lld to %lld\n", SCIPconsGetName(cons), maxweight, newweight);
10407  consdataChgWeight(consdata, pos, newweight);
10408 
10409  ++pos;
10410  assert(pos < nvars);
10411 
10412  maxweight = weights[pos];
10413 
10414  if( backpos <= pos )
10415  break;
10416  }
10417  (*nchgcoefs) += (pos - startpos);
10418 
10419  /* skip unchangable weights */
10420  while( pos < nvars && weights[pos] + sumcoef == capacity )
10421  ++pos;
10422 
10423  /* check special case were there is only one weight left to tighten
10424  *
10425  * e.g. 95x1 + 59x2 + 37x3 + 36x4 <= 95 (37 > 36)
10426  *
10427  * => 95x1 + 59x2 + 59x3 + 36x4 <= 95
10428  *
10429  * 197x1 + 120x2 + 77x3 + 10x4 <= 207 (here we cannot tighten the coefficient further)
10430  */
10431  if( pos + 1 == backpos && weights[pos] > sumcoef &&
10432  ((k == 0) || (k == 1 && weights[nvars - 1] + sumcoef + weights[pos] > capacity)) )
10433  {
10434  newweight = capacity - sumcoef;
10435  assert(newweight > weights[pos]);
10436 
10437  SCIPdebugMessage("in constraint <%s> changing weight %lld to %lld\n", SCIPconsGetName(cons), maxweight, newweight);
10438  consdataChgWeight(consdata, pos, newweight);
10439 
10440  break;
10441  }
10442 
10443  if( backpos <= pos )
10444  break;
10445  }
10446  }
10447 
10448  /* apply rule (2) (don't apply, if the knapsack has too many items for applying this costly method) */
10449  if( conshdlrdata->disaggregation && consdata->nvars - pos <= MAX_USECLIQUES_SIZE && consdata->nvars >= 2 &&
10450  pos > 0 && (SCIP_Longint)consdata->nvars - pos <= consdata->capacity &&
10451  consdata->weights[pos - 1] == consdata->capacity && (pos == consdata->nvars || consdata->weights[pos] == 1) )
10452  {
10453  SCIP_VAR** clqvars;
10454  SCIP_CONS* cliquecons;
10455  char name[SCIP_MAXSTRLEN];
10456  int* clqpart;
10457  int nclqvars;
10458  int nclq;
10459  int len;
10460  int c;
10461  int w;
10462 
10463  assert(!SCIPconsIsDeleted(cons));
10464 
10465  if( pos == consdata->nvars )
10466  {
10467  SCIPdebugMessage("upgrading knapsack constraint <%s> to a set-packing constraint", SCIPconsGetName(cons));
10468 
10469  SCIP_CALL( SCIPcreateConsSetpack(scip, &cliquecons, SCIPconsGetName(cons), pos, consdata->vars,
10473  SCIPconsIsStickingAtNode(cons)) );
10474 
10475  SCIP_CALL( SCIPaddCons(scip, cliquecons) );
10476  SCIP_CALL( SCIPreleaseCons(scip, &cliquecons) );
10477  ++(*naddconss);
10478 
10479  /* delete old constraint */
10480  SCIP_CALL( SCIPdelCons(scip, cons) );
10481  ++(*ndelconss);
10482 
10483  return SCIP_OKAY;
10484  }
10485 
10486  len = consdata->nvars - pos;
10487 
10488  /* allocate temporary memory */
10489  SCIP_CALL( SCIPallocBufferArray(scip, &clqpart, len) );
10490 
10491  /* calculate clique partition */
10492  SCIP_CALL( SCIPcalcCliquePartition(scip, &(consdata->vars[pos]), len, clqpart, &nclq) );
10493  assert(nclq <= len);
10494 
10495 #ifndef NDEBUG
10496  /* clique numbers must be at least as high as the index */
10497  for( w = 0; w < nclq; ++w )
10498  assert(clqpart[w] <= w);
10499 #endif
10500 
10501  SCIPdebugMessage("Disaggregating knapsack constraint <%s> due to clique information.\n", SCIPconsGetName(cons));
10502 
10503  /* allocate temporary memory */
10504  SCIP_CALL( SCIPallocBufferArray(scip, &clqvars, pos + len - nclq + 1) );
10505 
10506  /* copy corresponding variables with big coefficients */
10507  for( w = pos - 1; w >= 0; --w )
10508  clqvars[w] = consdata->vars[w];
10509 
10510  /* create for each clique a set-packing constraint */
10511  for( c = 0; c < nclq; ++c )
10512  {
10513  nclqvars = pos;
10514 
10515  for( w = c; w < len; ++w )
10516  {
10517  if( clqpart[w] == c )
10518  {
10519  assert(nclqvars < pos + len - nclq + 1);
10520  clqvars[nclqvars] = consdata->vars[w + pos];
10521  ++nclqvars;
10522  }
10523  }
10524 
10525  assert(nclqvars > 1);
10526 
10527  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_clq_%"SCIP_LONGINT_FORMAT"_%d", SCIPconsGetName(cons), consdata->capacity, c);
10528  SCIP_CALL( SCIPcreateConsSetpack(scip, &cliquecons, name, nclqvars, clqvars,
10532  SCIPconsIsStickingAtNode(cons)) );
10533  SCIPdebugMessage(" -> adding clique constraint: ");
10534  SCIPdebugPrintCons(scip, cliquecons, NULL);
10535  SCIP_CALL( SCIPaddCons(scip, cliquecons) );
10536  SCIP_CALL( SCIPreleaseCons(scip, &cliquecons) );
10537  ++(*naddconss);
10538  }
10539 
10540  /* delete old constraint */
10541  SCIP_CALL( SCIPdelCons(scip, cons) );
10542  ++(*ndelconss);
10543 
10544  SCIPfreeBufferArray(scip, &clqvars);
10545  SCIPfreeBufferArray(scip, &clqpart);
10546 
10547  return SCIP_OKAY;
10548  }
10549  else if( consdata->nvars <= MAX_USECLIQUES_SIZE || (consdata->cliquepartitioned && consdata->ncliques <= MAX_USECLIQUES_SIZE) )
10550  {
10551  SCIP_Longint* maxcliqueweights;
10552  SCIP_Longint* newweightvals;
10553  int* newweightidxs;
10554  SCIP_Longint cliqueweightsum;
10555 
10556  SCIP_CALL( SCIPallocBufferArray(scip, &maxcliqueweights, consdata->nvars) );
10557  SCIP_CALL( SCIPallocBufferArray(scip, &newweightvals, consdata->nvars) );
10558  SCIP_CALL( SCIPallocBufferArray(scip, &newweightidxs, consdata->nvars) );
10559 
10560  /* repeat as long as changes have been applied */
10561  do
10562  {
10563  int ncliques;
10564  int cliquenum;
10565  SCIP_Bool zeroweights;
10566 
10567  assert(consdata->merged);
10568 
10569  /* sort items, s.t. the heaviest one is in the first position */
10570  sortItems(consdata);
10571 
10572  /* calculate a clique partition */
10573  SCIP_CALL( calcCliquepartition(scip, consdata, TRUE, FALSE) );
10574 
10575  /* if there are only single element cliques, rule (2) is equivalent to rule (1) */
10576  if( consdata->cliquepartition[consdata->nvars - 1] == consdata->nvars - 1 )
10577  break;
10578 
10579  /* calculate the maximal weight of the cliques and store the clique type */
10580  cliqueweightsum = 0;
10581  ncliques = 0;
10582 
10583  for( i = 0; i < consdata->nvars; ++i )
10584  {
10585  SCIP_Longint weight;
10586 
10587  cliquenum = consdata->cliquepartition[i];
10588  assert(0 <= cliquenum && cliquenum <= ncliques);
10589 
10590  weight = consdata->weights[i];
10591  assert(weight > 0);
10592 
10593  if( cliquenum == ncliques )
10594  {
10595  maxcliqueweights[ncliques] = weight;
10596  cliqueweightsum += weight;
10597  ++ncliques;
10598  }
10599 
10600  assert(maxcliqueweights[cliquenum] >= weight);
10601  }
10602 
10603  /* apply rule on every clique */
10604  zeroweights = FALSE;
10605  for( i = 0; i < ncliques; ++i )
10606  {
10607  SCIP_Longint delta;
10608 
10609  delta = consdata->capacity - (cliqueweightsum - maxcliqueweights[i]);
10610  if( delta > 0 )
10611  {
10612  SCIP_Longint newcapacity;
10613 #ifndef NDEBUG
10614  SCIP_Longint newmincliqueweight;
10615 #endif
10616  SCIP_Longint newminweightsuminclique;
10617  SCIP_Bool forceclique;
10618  int nnewweights;
10619  int j;
10620 
10621  SCIPdebugMessage("knapsack constraint <%s>: weights of clique %d (maxweight: %"SCIP_LONGINT_FORMAT") can be tightened: cliqueweightsum=%"SCIP_LONGINT_FORMAT", capacity=%"SCIP_LONGINT_FORMAT" -> delta: %"SCIP_LONGINT_FORMAT"\n",
10622  SCIPconsGetName(cons), i, maxcliqueweights[i], cliqueweightsum, consdata->capacity, delta);
10623  newcapacity = consdata->capacity - delta;
10624  forceclique = FALSE;
10625  nnewweights = 0;
10626 #ifndef NDEBUG
10627  newmincliqueweight = newcapacity + 1;
10628  for( j = 0; j < i; ++j )
10629  assert(consdata->cliquepartition[j] < i); /* no element j < i can be in clique i */
10630 #endif
10631  for( j = i; j < consdata->nvars; ++j )
10632  {
10633  if( consdata->cliquepartition[j] == i )
10634  {
10635  newweight = consdata->weights[j] - delta;
10636  newweight = MAX(newweight, 0);
10637 
10638  /* cache the new weight */
10639  assert(nnewweights < consdata->nvars);
10640  newweightvals[nnewweights] = newweight;
10641  newweightidxs[nnewweights] = j;
10642  nnewweights++;
10643 
10644 #ifndef NDEBUG
10645  assert(newweight <= newmincliqueweight); /* items are sorted by non-increasing weight! */
10646  newmincliqueweight = newweight;
10647 #endif
10648  }
10649  }
10650 
10651  /* check if our clique information results out of this knapsack constraint and if so check if we would loose the clique information */
10652  if( nnewweights > 1 )
10653  {
10654 #ifndef NDEBUG
10655  j = newweightidxs[nnewweights - 2];
10656  assert(0 <= j && j < consdata->nvars);
10657  assert(consdata->cliquepartition[j] == i);
10658  j = newweightidxs[nnewweights - 1];
10659  assert(0 <= j && j < consdata->nvars);
10660  assert(consdata->cliquepartition[j] == i);
10661 #endif
10662 
10663  newminweightsuminclique = newweightvals[nnewweights - 2];
10664  newminweightsuminclique += newweightvals[nnewweights - 1];
10665 
10666  /* check if these new two minimal weights both fit into the knapsack;
10667  * if this is true, we have to add a clique constraint in order to enforce the clique
10668  * (otherwise, the knapsack might have been one of the reasons for the clique, and the weight
10669  * reduction might be infeasible, i.e., allows additional solutions)
10670  */
10671  if( newminweightsuminclique <= newcapacity )
10672  forceclique = TRUE;
10673  }
10674 
10675  /* check if we really want to apply the change */
10676  if( conshdlrdata->disaggregation || !forceclique )
10677  {
10678  SCIPdebugMessage(" -> change capacity from %"SCIP_LONGINT_FORMAT" to %"SCIP_LONGINT_FORMAT" (forceclique:%u)\n",
10679  consdata->capacity, newcapacity, forceclique);
10680  consdata->capacity = newcapacity;
10681  (*nchgsides)++;
10682 
10683  for( k = 0; k < nnewweights; ++k )
10684  {
10685  j = newweightidxs[k];
10686  assert(0 <= j && j < consdata->nvars);
10687  assert(consdata->cliquepartition[j] == i);
10688 
10689  /* apply the weight change */
10690  SCIPdebugMessage(" -> change weight of <%s> from %"SCIP_LONGINT_FORMAT" to %"SCIP_LONGINT_FORMAT"\n",
10691  SCIPvarGetName(consdata->vars[j]), consdata->weights[j], newweightvals[k]);
10692  consdataChgWeight(consdata, j, newweightvals[k]);
10693  (*nchgcoefs)++;
10694  assert(!consdata->sorted);
10695  zeroweights = zeroweights || (newweightvals[k] == 0);
10696  }
10697  /* if before the weight update at least one pair of weights did not fit into the knapsack and now fits,
10698  * we have to make sure, the clique is enforced - the clique might have been constructed partially from
10699  * this constraint, and by reducing the weights, this clique information is not contained anymore in the
10700  * knapsack constraint
10701  */
10702  if( forceclique )
10703  {
10704  SCIP_CONS* cliquecons;
10705  char name[SCIP_MAXSTRLEN];
10706  SCIP_VAR** cliquevars;
10707 
10708  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevars, nnewweights) );
10709  for( k = 0; k < nnewweights; ++k )
10710  cliquevars[k] = consdata->vars[newweightidxs[k]];
10711 
10712  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_clq_%"SCIP_LONGINT_FORMAT"_%d", SCIPconsGetName(cons), consdata->capacity, i);
10713  SCIP_CALL( SCIPcreateConsSetpack(scip, &cliquecons, name, nnewweights, cliquevars,
10717  SCIPconsIsStickingAtNode(cons)) );
10718  SCIPdebugMessage(" -> adding clique constraint: ");
10719  SCIPdebugPrintCons(scip, cliquecons, NULL);
10720  SCIP_CALL( SCIPaddCons(scip, cliquecons) );
10721  SCIP_CALL( SCIPreleaseCons(scip, &cliquecons) );
10722  SCIPfreeBufferArray(scip, &cliquevars);
10723  (*naddconss)++;
10724  }
10725  }
10726  }
10727  }
10728  if( zeroweights )
10729  {
10730  SCIP_CALL( removeZeroWeights(scip, cons) );
10731  }
10732  }
10733  while( !consdata->sorted && consdata->weightsum > consdata->capacity );
10734 
10735  /* free temporary memory */
10736  SCIPfreeBufferArray(scip, &newweightidxs);
10737  SCIPfreeBufferArray(scip, &newweightvals);
10738  SCIPfreeBufferArray(scip, &maxcliqueweights);
10739 
10740  /* check for redundancy */
10741  if( consdata->weightsum <= consdata->capacity )
10742  return SCIP_OKAY;
10743  }
10744 
10745  /* apply rule (3) */
10746  SCIP_CALL( tightenWeightsLift(scip, cons, nchgcoefs, cutoff) );
10747 
10748  /* check for redundancy */
10749  if( consdata->weightsum <= consdata->capacity )
10750  return SCIP_OKAY;
10751 
10752  /* apply rule (4) (all but smallest weight) */
10753  assert(consdata->merged);
10754  sortItems(consdata);
10755  minweight = consdata->weights[consdata->nvars-1];
10756  for( i = 0; i < consdata->nvars-1; ++i )
10757  {
10758  SCIP_Longint weight;
10759 
10760  weight = consdata->weights[i];
10761  assert(weight >= minweight);
10762  if( minweight + weight > consdata->capacity )
10763  {
10764  if( weight < consdata->capacity )
10765  {
10766  SCIPdebugMessage("knapsack constraint <%s>: changed weight of <%s> from %"SCIP_LONGINT_FORMAT" to %"SCIP_LONGINT_FORMAT"\n",
10767  SCIPconsGetName(cons), SCIPvarGetName(consdata->vars[i]), weight, consdata->capacity);
10768  assert(consdata->sorted);
10769  consdataChgWeight(consdata, i, consdata->capacity); /* this does not destroy the weight order! */
10770  assert(i == 0 || consdata->weights[i-1] >= consdata->weights[i]);
10771  consdata->sorted = TRUE;
10772  (*nchgcoefs)++;
10773  }
10774  }
10775  else
10776  break;
10777  }
10778 
10779  /* apply rule (4) (smallest weight) */
10780  if( consdata->nvars >= 2 )
10781  {
10782  SCIP_Longint weight;
10783 
10784  minweight = consdata->weights[consdata->nvars-2];
10785  weight = consdata->weights[consdata->nvars-1];
10786  assert(minweight >= weight);
10787  if( minweight + weight > consdata->capacity && weight < consdata->capacity )
10788  {
10789  SCIPdebugMessage("knapsack constraint <%s>: changed weight of <%s> from %"SCIP_LONGINT_FORMAT" to %"SCIP_LONGINT_FORMAT"\n",
10790  SCIPconsGetName(cons), SCIPvarGetName(consdata->vars[consdata->nvars-1]), weight, consdata->capacity);
10791  assert(consdata->sorted);
10792  consdataChgWeight(consdata, consdata->nvars-1, consdata->capacity); /* this does not destroy the weight order! */
10793  assert(minweight >= consdata->weights[consdata->nvars-1]);
10794  consdata->sorted = TRUE;
10795  (*nchgcoefs)++;
10796  }
10797  }
10798 
10799  return SCIP_OKAY;
10800 }
10801 
10802 /** adds negated cliques of the knapsack constraint to the global clique table */
10803 static
10805  SCIP*const scip, /**< SCIP data structure */
10806  SCIP_CONS*const cons, /**< knapsack constraint */
10807  SCIP_Bool*const cutoff, /**< pointer to store whether the node can be cut off */
10808  int*const nbdchgs /**< pointer to count the number of performed bound changes */
10809  )
10810 {
10811  SCIP_CONSDATA* consdata;
10812  SCIP_VAR** poscliquevars;
10813  SCIP_VAR** cliquevars;
10814  SCIP_Longint* maxweights;
10815  SCIP_Longint* gainweights;
10816  int* gaincliquepartition;
10817  SCIP_Bool* cliqueused;
10818  SCIP_Longint minactduetonegcliques;
10819  SCIP_Longint freecapacity;
10820  SCIP_Longint lastweight;
10821  SCIP_Longint beforelastweight;
10822  int nposcliquevars;
10823  int ncliquevars;
10824  int nvars;
10825  int nnegcliques;
10826  int lastcliqueused;
10827  int thisnbdchgs;
10828  int v;
10829  int w;
10830 
10831  assert(scip != NULL);
10832  assert(cons != NULL);
10833  assert(cutoff != NULL);
10834  assert(nbdchgs != NULL);
10835 
10836  *cutoff = FALSE;
10837 
10838  consdata = SCIPconsGetData(cons);
10839  assert(consdata != NULL);
10840 
10841  nvars = consdata->nvars;
10842 
10843  /* check whether the cliques have already been added */
10844  if( consdata->cliquesadded || nvars == 0 )
10845  return SCIP_OKAY;
10846 
10847  /* make sure, the items are merged */
10848  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
10849  if( *cutoff )
10850  return SCIP_OKAY;
10851 
10852  /* make sure, items are sorted by non-increasing weight */
10853  sortItems(consdata);
10854 
10855  assert(consdata->merged);
10856 
10857  /* calculate a clique partition */
10858  SCIP_CALL( calcCliquepartition(scip, consdata, FALSE, TRUE) );
10859  nnegcliques = consdata->nnegcliques;
10860 
10861  /* if we have no negated cliques, stop */
10862  if( nnegcliques == nvars )
10863  return SCIP_OKAY;
10864 
10865  /* get temporary memory */
10866  SCIP_CALL( SCIPallocBufferArray(scip, &poscliquevars, nvars) );
10867  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevars, nvars) );
10868  SCIP_CALL( SCIPallocBufferArray(scip, &gainweights, nvars) );
10869  BMSclearMemoryArray(gainweights, nvars);
10870  SCIP_CALL( SCIPallocBufferArray(scip, &gaincliquepartition, nvars) );
10871  SCIP_CALL( SCIPallocBufferArray(scip, &maxweights, nnegcliques) );
10872  SCIP_CALL( SCIPallocBufferArray(scip, &cliqueused, nnegcliques) );
10873  BMSclearMemoryArray(cliqueused, nnegcliques);
10874 
10875  nnegcliques = 0;
10876  minactduetonegcliques = 0;
10877 
10878  /* determine maximal weights for all negated cliques and calculate minimal weightsum due to negated cliques */
10879  for( v = 0; v < nvars; ++v )
10880  {
10881  assert(0 <= consdata->negcliquepartition[v] && consdata->negcliquepartition[v] <= nnegcliques);
10882  assert(consdata->weights[v] > 0);
10883 
10884  if( consdata->negcliquepartition[v] == nnegcliques )
10885  {
10886  nnegcliques++;
10887  maxweights[consdata->negcliquepartition[v]] = consdata->weights[v];
10888  }
10889  else
10890  minactduetonegcliques += consdata->weights[v];
10891  }
10892 
10893  nposcliquevars = 0;
10894 
10895  /* add cliques, using negated cliques information */
10896  if( minactduetonegcliques > 0 )
10897  {
10898  /* free capacity is the rest of not used capacity if the smallest amount of weights due to negated cliques are used */
10899  freecapacity = consdata->capacity - minactduetonegcliques;
10900 
10901  SCIPdebugPrintCons(scip, cons, NULL);
10902  SCIPdebugMessage("Try to add negated cliques in knapsack constraint handler for constraint %s; capacity = %"SCIP_LONGINT_FORMAT", minactivity(due to neg. cliques) = %"SCIP_LONGINT_FORMAT", freecapacity = %"SCIP_LONGINT_FORMAT".\n",
10903  SCIPconsGetName(cons), consdata->capacity, minactduetonegcliques, freecapacity);
10904 
10905  /* calculate possible gain by switching chosen items in negated cliques */
10906  for( v = 0; v < nvars; ++v )
10907  {
10908  if( !cliqueused[consdata->negcliquepartition[v]] )
10909  {
10910  cliqueused[consdata->negcliquepartition[v]] = TRUE;
10911  for( w = v + 1; w < nvars; ++w )
10912  {
10913  /* if we would take the biggest weight instead of another what would we gain, take weight[i] instead of
10914  * weight[j] (which are both in a negated clique) */
10915  if( consdata->negcliquepartition[v] == consdata->negcliquepartition[w]
10916  && consdata->weights[v] > consdata->weights[w] )
10917  {
10918  poscliquevars[nposcliquevars] = consdata->vars[w];
10919  gainweights[nposcliquevars] = maxweights[consdata->negcliquepartition[v]] - consdata->weights[w];
10920  gaincliquepartition[nposcliquevars] = consdata->negcliquepartition[v];
10921  ++nposcliquevars;
10922  }
10923  }
10924  }
10925  }
10926 
10927  /* try to create negated cliques */
10928  if( nposcliquevars > 0 )
10929  {
10930  /* sort possible gain per substitution of the clique members */
10931  SCIPsortDownLongPtrInt(gainweights,(void**) poscliquevars, gaincliquepartition, nposcliquevars);
10932 
10933  for( v = 0; v < nposcliquevars; ++v )
10934  {
10935  SCIP_CALL( SCIPgetNegatedVar(scip, poscliquevars[v], &cliquevars[0]) );
10936  ncliquevars = 1;
10937  lastweight = gainweights[v];
10938  beforelastweight = -1;
10939  lastcliqueused = gaincliquepartition[v];
10940  /* clear cliqueused to get an unused array */
10941  BMSclearMemoryArray(cliqueused, nnegcliques);
10942  cliqueused[gaincliquepartition[v]] = TRUE;
10943 
10944  /* taking bigger weights make the knapsack redundant so we will create cliques, only take items which are not
10945  * in the same negated clique and by taking two of them would exceed the free capacity */
10946  for( w = v + 1; w < nposcliquevars && !cliqueused[gaincliquepartition[w]] && gainweights[w] + lastweight > freecapacity; ++w )
10947  {
10948  beforelastweight = lastweight;
10949  lastweight = gainweights[w];
10950  lastcliqueused = gaincliquepartition[w];
10951  cliqueused[gaincliquepartition[w]] = TRUE;
10952  SCIP_CALL( SCIPgetNegatedVar(scip, poscliquevars[w], &cliquevars[ncliquevars]) );
10953  ++ncliquevars;
10954  }
10955 
10956  if( ncliquevars > 1 )
10957  {
10958 #ifdef SCIP_DEBUG
10959  int b;
10960  SCIPdebugMessage("adding new Clique: ");
10961  for( b = 0; b < ncliquevars; ++b )
10962  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
10963  SCIPdebugPrintf("\n");
10964 #endif
10965 
10966  assert(beforelastweight > 0);
10967  /* add the clique to the clique table */
10968  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
10969  if( *cutoff )
10970  goto TERMINATE;
10971  *nbdchgs += thisnbdchgs;
10972 
10973  /* reset last used clique to get slightly different cliques */
10974  cliqueused[lastcliqueused] = FALSE;
10975 
10976  /* try to replace the last item in the clique by a different item to obtain a slightly different clique */
10977  for( ++w; w < nposcliquevars && !cliqueused[gaincliquepartition[w]] && beforelastweight + gainweights[w] > freecapacity; ++w )
10978  {
10979  SCIP_CALL( SCIPgetNegatedVar(scip, poscliquevars[w], &cliquevars[ncliquevars - 1]) );
10980 #ifdef SCIP_DEBUG
10981  {
10982  SCIPdebugMessage("adding new Clique: ");
10983  for( b = 0; b < ncliquevars; ++b )
10984  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
10985  SCIPdebugPrintf("\n");
10986  }
10987 #endif
10988  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
10989  if( *cutoff )
10990  goto TERMINATE;
10991  *nbdchgs += thisnbdchgs;
10992  }
10993  }
10994  }
10995  }
10996  }
10997 
10998  TERMINATE:
10999  /* free temporary memory */
11000  SCIPfreeBufferArray(scip, &cliqueused);
11001  SCIPfreeBufferArray(scip, &gaincliquepartition);
11002  SCIPfreeBufferArray(scip, &maxweights);
11003  SCIPfreeBufferArray(scip, &gainweights);
11004  SCIPfreeBufferArray(scip, &cliquevars);
11005  SCIPfreeBufferArray(scip, &poscliquevars);
11006 
11007  return SCIP_OKAY;
11008 }
11009 
11010 /** adds cliques of the knapsack constraint to the global clique table */
11011 static
11013  SCIP*const scip, /**< SCIP data structure */
11014  SCIP_CONS*const cons, /**< knapsack constraint */
11015  SCIP_Bool*const cutoff, /**< pointer to store whether the node can be cut off */
11016  int*const nbdchgs /**< pointer to count the number of performed bound changes */
11017  )
11018 {
11019  SCIP_CONSDATA* consdata;
11020  SCIP_VAR** cliquevars;
11021  int ncliquevars;
11022  int i;
11023  SCIP_Longint minactduetonegcliques;
11024  SCIP_Longint freecapacity;
11025  SCIP_Longint lastweight;
11026  SCIP_Longint beforelastweight;
11027  int nnegcliques;
11028  int cliquenum;
11029  int thisnbdchgs;
11030  SCIP_VAR** poscliquevars;
11031  SCIP_Longint* gainweights;
11032  int nposcliquevars;
11033  SCIP_Longint* secondmaxweights;
11034  int nvars;
11035 
11036  assert(scip != NULL);
11037  assert(cons != NULL);
11038  assert(cutoff != NULL);
11039  assert(nbdchgs != NULL);
11040 
11041  *cutoff = FALSE;
11042 
11043  consdata = SCIPconsGetData(cons);
11044  assert(consdata != NULL);
11045 
11046  nvars = consdata->nvars;
11047 
11048  /* check whether the cliques have already been added */
11049  if( consdata->cliquesadded || nvars == 0 )
11050  return SCIP_OKAY;
11051 
11052  /* make sure, the items are merged */
11053  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
11054  if( *cutoff )
11055  return SCIP_OKAY;
11056 
11057  /* make sure, the items are sorted by non-increasing weight */
11058  sortItems(consdata);
11059 
11060  assert(consdata->merged);
11061 
11062  /* calculate a clique partition */
11063  SCIP_CALL( calcCliquepartition(scip, consdata, FALSE, TRUE) );
11064  nnegcliques = consdata->nnegcliques;
11065  assert(nnegcliques <= nvars);
11066 
11067  /* get temporary memory */
11068  SCIP_CALL( SCIPallocBufferArray(scip, &poscliquevars, nvars) );
11069  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevars, nvars) );
11070  SCIP_CALL( SCIPallocBufferArray(scip, &gainweights, nvars) );
11071  BMSclearMemoryArray(gainweights, nvars);
11072  SCIP_CALL( SCIPallocBufferArray(scip, &secondmaxweights, nnegcliques) );
11073  BMSclearMemoryArray(secondmaxweights, nnegcliques);
11074 
11075  minactduetonegcliques = 0;
11076 
11077  /* calculate minimal activity due to negated cliques, and determine second maximal weight in each clique */
11078  if( nnegcliques < nvars )
11079  {
11080  nnegcliques = 0;
11081 
11082  for( i = 0; i < nvars; ++i )
11083  {
11084  SCIP_Longint weight;
11085 
11086  cliquenum = consdata->negcliquepartition[i];
11087  assert(0 <= cliquenum && cliquenum <= nnegcliques);
11088 
11089  weight = consdata->weights[i];
11090  assert(weight > 0);
11091 
11092  if( cliquenum == nnegcliques )
11093  nnegcliques++;
11094  else
11095  {
11096  minactduetonegcliques += weight;
11097  if( secondmaxweights[cliquenum] == 0 )
11098  secondmaxweights[cliquenum] = weight;
11099  }
11100  }
11101  }
11102 
11103  /* add cliques, using negated cliques information */
11104  if( minactduetonegcliques > 0 )
11105  {
11106  /* free capacity is the rest of not used capacity if the smallest amount of weights due to negated cliques are used */
11107  freecapacity = consdata->capacity - minactduetonegcliques;
11108 
11109  SCIPdebugPrintCons(scip, cons, NULL);
11110  SCIPdebugMessage("Try to add cliques in knapsack constraint handler for constraint %s; capacity = %"SCIP_LONGINT_FORMAT", minactivity(due to neg. cliques) = %"SCIP_LONGINT_FORMAT", freecapacity = %"SCIP_LONGINT_FORMAT".\n",
11111  SCIPconsGetName(cons), consdata->capacity, minactduetonegcliques, freecapacity);
11112 
11113  /* create negated cliques out of negated cliques, if we do not take the smallest weight of a cliques ... */
11114  SCIP_CALL( addNegatedCliques(scip, cons, cutoff, nbdchgs ) );
11115 
11116  nposcliquevars = 0;
11117 
11118  for( i = nvars - 1; i >= 0; --i )
11119  {
11120  /* if we would take the biggest weight instead of the second biggest */
11121  cliquenum = consdata->negcliquepartition[i];
11122  if( consdata->weights[i] > secondmaxweights[cliquenum] )
11123  {
11124  poscliquevars[nposcliquevars] = consdata->vars[i];
11125  gainweights[nposcliquevars] = consdata->weights[i] - secondmaxweights[cliquenum];
11126  ++nposcliquevars;
11127  }
11128  }
11129 
11130  if( nposcliquevars > 0 )
11131  {
11132  /* sort possible gain per substitution of the clique members */
11133  SCIPsortDownLongPtr(gainweights,(void**) poscliquevars, nposcliquevars);
11134 
11135  cliquevars[0] = poscliquevars[0];
11136  ncliquevars = 1;
11137  lastweight = gainweights[0];
11138  beforelastweight = 0;
11139 
11140  /* taking bigger weights make the knapsack redundant so we will create cliques */
11141  for( i = 1; i < nposcliquevars && gainweights[i] + lastweight > freecapacity; ++i )
11142  {
11143  beforelastweight = lastweight;
11144  lastweight = gainweights[i];
11145  cliquevars[ncliquevars] = poscliquevars[i];
11146  ++ncliquevars;
11147  }
11148 
11149  if( ncliquevars > 1 )
11150  {
11151 #ifdef SCIP_DEBUG
11152  int b;
11153  SCIPdebugMessage("adding new Clique: ");
11154  for( b = 0; b < ncliquevars; ++b )
11155  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
11156  SCIPdebugPrintf("\n");
11157 #endif
11158 
11159  assert(beforelastweight > 0);
11160  /* add the clique to the clique table */
11161  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
11162  if( *cutoff )
11163  goto TERMINATE;
11164  *nbdchgs += thisnbdchgs;
11165 
11166  /* try to replace the last item in the clique by a different item to obtain a slightly different clique */
11167  for( ++i; i < nposcliquevars && beforelastweight + gainweights[i] > freecapacity; ++i )
11168  {
11169  cliquevars[ncliquevars - 1] = poscliquevars[i];
11170 #ifdef SCIP_DEBUG
11171  {
11172  SCIPdebugMessage("adding new Clique: ");
11173  for( b = 0; b < ncliquevars; ++b )
11174  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
11175  SCIPdebugPrintf("\n");
11176  }
11177 #endif
11178  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
11179  if( *cutoff )
11180  goto TERMINATE;
11181  *nbdchgs += thisnbdchgs;
11182  }
11183  }
11184  }
11185  }
11186 
11187  /* build a largest clique by using the items with the maximal weights */
11188  cliquevars[0] = consdata->vars[0];
11189  for( i = 1; i < nvars && consdata->weights[i-1] + consdata->weights[i] > consdata->capacity; ++i )
11190  cliquevars[i] = consdata->vars[i];
11191 
11192  ncliquevars = i;
11193 
11194  if( ncliquevars >= 2 )
11195  {
11196  SCIP_Longint cliqueminweight;
11197 #ifdef SCIP_DEBUG
11198  int b;
11199  SCIPdebugMessage("adding new Clique: ");
11200  for( b = 0; b < ncliquevars; ++b )
11201  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
11202  SCIPdebugPrintf("\n");
11203 #endif
11204 
11205  /* add the clique to the clique table */
11206  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
11207  if( *cutoff )
11208  goto TERMINATE;
11209  *nbdchgs += thisnbdchgs;
11210 
11211  /* try to replace the last item in the clique by a different item to obtain a slightly different clique */
11212  cliqueminweight = consdata->weights[ncliquevars-2];
11213  for( i = ncliquevars; i < nvars && cliqueminweight + consdata->weights[i] > consdata->capacity; ++i )
11214  {
11215  cliquevars[ncliquevars - 1] = consdata->vars[i];
11216 #ifdef SCIP_DEBUG
11217  {
11218  SCIPdebugMessage("adding new Clique: ");
11219  for( b = 0; b < ncliquevars; ++b )
11220  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
11221  SCIPdebugPrintf("\n");
11222  }
11223 #endif
11224  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
11225  if( *cutoff )
11226  goto TERMINATE;
11227  *nbdchgs += thisnbdchgs;
11228  }
11229  }
11230 
11231  TERMINATE:
11232  /* free temporary memory and mark the constraint */
11233  SCIPfreeBufferArray(scip, &secondmaxweights);
11234  SCIPfreeBufferArray(scip, &gainweights);
11235  SCIPfreeBufferArray(scip, &cliquevars);
11236  SCIPfreeBufferArray(scip, &poscliquevars);
11237  consdata->cliquesadded = TRUE;
11238 
11239  return SCIP_OKAY;
11240 }
11241 
11242 
11243 /** gets the key of the given element */
11244 static
11245 SCIP_DECL_HASHGETKEY(hashGetKeyKnapsackcons)
11246 { /*lint --e{715}*/
11247  /* the key is the element itself */
11248  return elem;
11249 }
11250 
11251 /** returns TRUE iff both keys are equal; two constraints are equal if they have the same variables and the
11252  * same coefficients
11253  */
11254 static
11255 SCIP_DECL_HASHKEYEQ(hashKeyEqKnapsackcons)
11256 {
11257 #ifndef NDEBUG
11258  SCIP* scip;
11259 #endif
11260  SCIP_CONSDATA* consdata1;
11261  SCIP_CONSDATA* consdata2;
11262  int i;
11263 
11264  consdata1 = SCIPconsGetData((SCIP_CONS*)key1);
11265  consdata2 = SCIPconsGetData((SCIP_CONS*)key2);
11266  assert(consdata1->sorted);
11267  assert(consdata2->sorted);
11268 #ifndef NDEBUG
11269  scip = (SCIP*)userptr;
11270  assert(scip != NULL);
11271 #endif
11272 
11273  /* checks trivial case */
11274  if( consdata1->nvars != consdata2->nvars )
11275  return FALSE;
11276 
11277  for( i = consdata1->nvars - 1; i >= 0; --i )
11278  {
11279  /* tests if variables are equal */
11280  if( consdata1->vars[i] != consdata2->vars[i] )
11281  {
11282  assert(SCIPvarCompare(consdata1->vars[i], consdata2->vars[i]) == 1 ||
11283  SCIPvarCompare(consdata1->vars[i], consdata2->vars[i]) == -1);
11284  return FALSE;
11285  }
11286  assert(SCIPvarCompare(consdata1->vars[i], consdata2->vars[i]) == 0);
11287 
11288  /* tests if weights are equal too */
11289  if( consdata1->weights[i] != consdata2->weights[i] )
11290  return FALSE;
11291  }
11292 
11293  return TRUE;
11294 }
11295 
11296 /** returns the hash value of the key */
11297 static
11298 SCIP_DECL_HASHKEYVAL(hashKeyValKnapsackcons)
11299 {
11300 #ifndef NDEBUG
11301  SCIP* scip;
11302 #endif
11303  SCIP_CONSDATA* consdata;
11304  unsigned int hashval;
11305  int minidx;
11306  int mididx;
11307  int maxidx;
11308  int maxabsval;
11309 
11310  consdata = SCIPconsGetData((SCIP_CONS*)key);
11311  assert(consdata != NULL);
11312  assert(consdata->nvars > 0);
11313 
11314 #ifndef NDEBUG
11315  scip = (SCIP*)userptr;
11316  assert(scip != NULL);
11317 #endif
11318 
11319  /* sorts the constraints */
11320  sortItems(consdata);
11321 
11322  minidx = SCIPvarGetIndex(consdata->vars[0]);
11323  mididx = SCIPvarGetIndex(consdata->vars[consdata->nvars / 2]);
11324  maxidx = SCIPvarGetIndex(consdata->vars[consdata->nvars - 1]);
11325  assert(minidx >= 0 && mididx >= 0 && maxidx >= 0);
11326 
11327  if( consdata->weights[0] > INT_MAX )
11328  maxabsval = 0;
11329  else
11330  maxabsval = (int) consdata->weights[0];
11331 
11332  /* hash value depends on vectors of variable indices */
11333  hashval = (consdata->nvars << 29) + (minidx << 22) + (mididx << 11) + maxidx + maxabsval; /*lint !e701*/
11334 
11335  return hashval;
11336 }
11337 
11338 /** compares each constraint with all other constraints for possible redundancy and removes or changes constraint
11339  * accordingly; in contrast to preprocessConstraintPairs(), it uses a hash table
11340  */
11341 static
11343  SCIP* scip, /**< SCIP data structure */
11344  BMS_BLKMEM* blkmem, /**< block memory */
11345  SCIP_CONS** conss, /**< constraint set */
11346  int nconss, /**< number of constraints in constraint set */
11347  SCIP_Bool* cutoff, /**< pointer to store whether the problem is infeasible */
11348  int* ndelconss /**< pointer to count number of deleted constraints */
11349  )
11350 {
11351  SCIP_HASHTABLE* hashtable;
11352  int hashtablesize;
11353  int c;
11354 
11355  assert(scip != NULL);
11356  assert(blkmem != NULL);
11357  assert(conss != NULL);
11358  assert(ndelconss != NULL);
11359 
11360  /* create a hash table for the constraint set */
11361  hashtablesize = SCIPcalcHashtableSize(10*nconss);
11362  hashtablesize = MAX(hashtablesize, HASHSIZE_KNAPSACKCONS);
11363  SCIP_CALL( SCIPhashtableCreate(&hashtable, blkmem, hashtablesize,
11364  hashGetKeyKnapsackcons, hashKeyEqKnapsackcons, hashKeyValKnapsackcons, (void*) scip) );
11365 
11366  /* check all constraints in the given set for redundancy */
11367  for( c = nconss - 1; c >= 0; --c )
11368  {
11369  SCIP_CONS* cons0;
11370  SCIP_CONS* cons1;
11371  SCIP_CONSDATA* consdata0;
11372 
11373  cons0 = conss[c];
11374 
11375  if( !SCIPconsIsActive(cons0) || SCIPconsIsModifiable(cons0) )
11376  continue;
11377 
11378  consdata0 = SCIPconsGetData(cons0);
11379  assert(consdata0 != NULL);
11380  if( consdata0->nvars == 0 )
11381  {
11382  if( consdata0->capacity < 0 )
11383  {
11384  *cutoff = TRUE;
11385  return SCIP_OKAY;
11386  }
11387  else
11388  {
11389  SCIP_CALL( SCIPdelCons(scip, cons0) );
11390  ++(*ndelconss);
11391  continue;
11392  }
11393  }
11394 
11395  /* get constraint from current hash table with same variables and same weights as cons0 */
11396  cons1 = (SCIP_CONS*)(SCIPhashtableRetrieve(hashtable, (void*)cons0));
11397 
11398  if( cons1 != NULL )
11399  {
11400  SCIP_CONS* consstay;
11401  SCIP_CONS* consdel;
11402  SCIP_CONSDATA* consdata1;
11403 
11404  assert(SCIPconsIsActive(cons1));
11405  assert(!SCIPconsIsModifiable(cons1));
11406 
11407  /* constraint found: create a new constraint with same coefficients and best left and right hand side;
11408  * delete old constraints afterwards
11409  */
11410  consdata1 = SCIPconsGetData(cons1);
11411 
11412  assert(consdata1 != NULL);
11413  assert(consdata0->nvars > 0 && consdata0->nvars == consdata1->nvars);
11414 
11415  assert(consdata0->sorted && consdata1->sorted);
11416  assert(consdata0->vars[0] == consdata1->vars[0]);
11417  assert(consdata0->weights[0] == consdata1->weights[0]);
11418 
11419  SCIPdebugMessage("knapsack constraints <%s> and <%s> with equal coefficients\n",
11420  SCIPconsGetName(cons0), SCIPconsGetName(cons1));
11421 
11422  /* check which constraint has to stay; */
11423  if( consdata0->capacity < consdata1->capacity )
11424  {
11425  consstay = cons0;
11426  consdel = cons1;
11427 
11428  /* exchange consdel with consstay in hashtable */
11429  SCIP_CALL( SCIPhashtableRemove(hashtable, (void*) consdel) );
11430  SCIP_CALL( SCIPhashtableInsert(hashtable, (void*) consstay) );
11431  }
11432  else
11433  {
11434  consstay = cons1;
11435  consdel = cons0;
11436  }
11437 
11438  /* update flags of constraint which caused the redundancy s.t. nonredundant information doesn't get lost */
11439  SCIP_CALL( SCIPupdateConsFlags(scip, consstay, consdel) );
11440 
11441  /* delete consdel */
11442  SCIP_CALL( SCIPdelCons(scip, consdel) );
11443  ++(*ndelconss);
11444 
11445  assert(SCIPconsIsActive(consstay));
11446  }
11447  else
11448  {
11449  /* no such constraint in current hash table: insert cons0 into hash table */
11450  SCIP_CALL( SCIPhashtableInsert(hashtable, (void*) cons0) );
11451  }
11452  }
11453 
11454  /* free hash table */
11455  SCIPhashtableFree(&hashtable);
11456 
11457  return SCIP_OKAY;
11458 }
11459 
11460 
11461 /** compares constraint with all prior constraints for possible redundancy or aggregation,
11462  * and removes or changes constraint accordingly
11463  */
11464 static
11466  SCIP* scip, /**< SCIP data structure */
11467  SCIP_CONS** conss, /**< constraint set */
11468  int firstchange, /**< first constraint that changed since last pair preprocessing round */
11469  int chkind, /**< index of constraint to check against all prior indices upto startind */
11470  int* ndelconss /**< pointer to count number of deleted constraints */
11471  )
11472 {
11473  SCIP_CONS* cons0;
11474  SCIP_CONSDATA* consdata0;
11475  int c;
11476 
11477  assert(scip != NULL);
11478  assert(conss != NULL);
11479  assert(firstchange <= chkind);
11480  assert(ndelconss != NULL);
11481 
11482  /* get the constraint to be checked against all prior constraints */
11483  cons0 = conss[chkind];
11484  assert(cons0 != NULL);
11485  assert(SCIPconsIsActive(cons0));
11486  assert(!SCIPconsIsModifiable(cons0));
11487 
11488  consdata0 = SCIPconsGetData(cons0);
11489  assert(consdata0 != NULL);
11490  assert(consdata0->nvars >= 1);
11491  assert(consdata0->merged);
11492 
11493  /* sort the constraint */
11494  sortItems(consdata0);
11495 
11496  /* check constraint against all prior constraints */
11497  for( c = (consdata0->presolved ? firstchange : 0); c < chkind; ++c )
11498  {
11499  SCIP_CONS* cons1;
11500  SCIP_CONSDATA* consdata1;
11501  SCIP_Bool iscons0incons1contained;
11502  SCIP_Bool iscons1incons0contained;
11503  SCIP_Real quotient;
11504  int v;
11505  int v0;
11506  int v1;
11507 
11508  cons1 = conss[c];
11509  assert(cons1 != NULL);
11510  if( !SCIPconsIsActive(cons1) || SCIPconsIsModifiable(cons1) )
11511  continue;
11512 
11513  consdata1 = SCIPconsGetData(cons1);
11514  assert(consdata1 != NULL);
11515 
11516  /* if both constraints didn't change since last pair processing, we can ignore the pair */
11517  if( consdata0->presolved && consdata0->presolved )
11518  continue;
11519 
11520  assert(consdata1->nvars >= 1);
11521  assert(consdata1->merged);
11522 
11523  /* sort the constraint */
11524  sortItems(consdata1);
11525 
11526  quotient = ((SCIP_Real) consdata0->capacity) / ((SCIP_Real) consdata1->capacity);
11527 
11528  if( consdata0->nvars > consdata1->nvars )
11529  {
11530  iscons0incons1contained = FALSE;
11531  iscons1incons0contained = TRUE;
11532  v = consdata1->nvars - 1;
11533  }
11534  else if( consdata0->nvars < consdata1->nvars )
11535  {
11536  iscons0incons1contained = TRUE;
11537  iscons1incons0contained = FALSE;
11538  v = consdata0->nvars - 1;
11539  }
11540  else
11541  {
11542  iscons0incons1contained = TRUE;
11543  iscons1incons0contained = TRUE;
11544  v = consdata0->nvars - 1;
11545  }
11546 
11547  SCIPdebugMessage("preprocess knapsack constraint pair <%s> and <%s>\n", SCIPconsGetName(cons0), SCIPconsGetName(cons1));
11548 
11549  /* check consdata0 against consdata1:
11550  * 1. if all variables var_i of cons1 are in cons0 and for each of these variables
11551  * (consdata0->weights[i] / quotient) >= consdata1->weights[i] cons1 is redundant
11552  * 2. if all variables var_i of cons0 are in cons1 and for each of these variables
11553  * (consdata0->weights[i] / quotient) <= consdata1->weights[i] cons0 is redundant
11554  */
11555  v0 = consdata0->nvars - 1;
11556  v1 = consdata1->nvars - 1;
11557 
11558  while( v >= 0 )
11559  {
11560  assert(iscons0incons1contained || iscons1incons0contained);
11561 
11562  /* now there are more variables in cons1 left */
11563  if( v1 > v0 )
11564  {
11565  iscons1incons0contained = FALSE;
11566  if( !iscons0incons1contained )
11567  break;
11568  }
11569  /* now there are more variables in cons0 left */
11570  else if( v1 < v0 )
11571  {
11572  iscons0incons1contained = FALSE;
11573  if( !iscons1incons0contained )
11574  break;
11575  }
11576 
11577  assert(v == v0 || v == v1);
11578  assert(v0 >= 0);
11579  assert(v1 >= 0);
11580 
11581  /* both variables are the same */
11582  if( consdata0->vars[v0] == consdata1->vars[v1] )
11583  {
11584  /* if cons1 is possible contained in cons0 (consdata0->weights[v0] / quotient) must be greater equals consdata1->weights[v1] */
11585  if( iscons1incons0contained && SCIPisLT(scip, ((SCIP_Real) consdata0->weights[v0]) / quotient, (SCIP_Real) consdata1->weights[v1]) )
11586  {
11587  iscons1incons0contained = FALSE;
11588  if( !iscons0incons1contained )
11589  break;
11590  }
11591  /* if cons0 is possible contained in cons1 (consdata0->weight[v0] / quotient) must be less equals consdata1->weight[v1] */
11592  else if( iscons0incons1contained && SCIPisGT(scip, ((SCIP_Real) consdata0->weights[v0]) / quotient, (SCIP_Real) consdata1->weights[v1]) )
11593  {
11594  iscons0incons1contained = FALSE;
11595  if( !iscons1incons0contained )
11596  break;
11597  }
11598  --v0;
11599  --v1;
11600  --v;
11601  }
11602  else
11603  {
11604  /* both constraints have a variables which is not part of the other constraint, so stop */
11605  if( iscons0incons1contained && iscons1incons0contained )
11606  {
11607  iscons0incons1contained = FALSE;
11608  iscons1incons0contained = FALSE;
11609  break;
11610  }
11611  assert(iscons0incons1contained ? (v1 >= v0) : iscons1incons0contained);
11612  assert(iscons1incons0contained ? (v1 <= v0) : iscons0incons1contained);
11613  /* continue to the next variable */
11614  if( iscons0incons1contained )
11615  --v1;
11616  else
11617  --v0;
11618  }
11619  }
11620  /* neither one constraint was contained in another or we checked all variables of one constraint against the
11621  * other
11622  */
11623  assert(!iscons1incons0contained || !iscons0incons1contained || v0 == -1 || v1 == -1);
11624 
11625  if( iscons1incons0contained )
11626  {
11627  SCIPdebugMessage("knapsack constraint <%s> is redundant\n", SCIPconsGetName(cons1));
11628  SCIPdebugPrintCons(scip, cons1, NULL);
11629 
11630  /* update flags of constraint which caused the redundancy s.t. nonredundant information doesn't get lost */
11631  SCIP_CALL( SCIPupdateConsFlags(scip, cons0, cons1) );
11632 
11633  SCIP_CALL( SCIPdelCons(scip, cons1) );
11634  ++(*ndelconss);
11635  }
11636  else if( iscons0incons1contained )
11637  {
11638  SCIPdebugMessage("knapsack constraint <%s> is redundant\n", SCIPconsGetName(cons0));
11639  SCIPdebugPrintCons(scip, cons0, NULL);
11640 
11641  /* update flags of constraint which caused the redundancy s.t. nonredundant information doesn't get lost */
11642  SCIP_CALL( SCIPupdateConsFlags(scip, cons1, cons0) );
11643 
11644  SCIP_CALL( SCIPdelCons(scip, cons0) );
11645  ++(*ndelconss);
11646  break;
11647  }
11648  }
11649 
11650  return SCIP_OKAY;
11651 }
11652 
11653 /*
11654  * Linear constraint upgrading
11655  */
11656 
11657 /** creates and captures a knapsack constraint out of a linear inequality */
11658 static
11660  SCIP* scip, /**< SCIP data structure */
11661  SCIP_CONS** cons, /**< pointer to hold the created constraint */
11662  const char* name, /**< name of constraint */
11663  int nvars, /**< number of variables in the constraint */
11664  SCIP_VAR** vars, /**< array with variables of constraint entries */
11665  SCIP_Real* vals, /**< array with inequality coefficients */
11666  SCIP_Real lhs, /**< left hand side of inequality */
11667  SCIP_Real rhs, /**< right hand side of inequality */
11668  SCIP_Bool initial, /**< should the LP relaxation of constraint be in the initial LP?
11669  * Usually set to TRUE. Set to FALSE for 'lazy constraints'. */
11670  SCIP_Bool separate, /**< should the constraint be separated during LP processing?
11671  * Usually set to TRUE. */
11672  SCIP_Bool enforce, /**< should the constraint be enforced during node processing?
11673  * TRUE for model constraints, FALSE for additional, redundant constraints. */
11674  SCIP_Bool check, /**< should the constraint be checked for feasibility?
11675  * TRUE for model constraints, FALSE for additional, redundant constraints. */
11676  SCIP_Bool propagate, /**< should the constraint be propagated during node processing?
11677  * Usually set to TRUE. */
11678  SCIP_Bool local, /**< is constraint only valid locally?
11679  * Usually set to FALSE. Has to be set to TRUE, e.g., for branching constraints. */
11680  SCIP_Bool modifiable, /**< is constraint modifiable (subject to column generation)?
11681  * Usually set to FALSE. In column generation applications, set to TRUE if pricing
11682  * adds coefficients to this constraint. */
11683  SCIP_Bool dynamic, /**< is constraint subject to aging?
11684  * Usually set to FALSE. Set to TRUE for own cuts which
11685  * are separated as constraints. */
11686  SCIP_Bool removable, /**< should the relaxation be removed from the LP due to aging or cleanup?
11687  * Usually set to FALSE. Set to TRUE for 'lazy constraints' and 'user cuts'. */
11688  SCIP_Bool stickingatnode /**< should the constraint always be kept at the node where it was added, even
11689  * if it may be moved to a more global node?
11690  * Usually set to FALSE. Set to TRUE to for constraints that represent node data. */
11691  )
11692 {
11693  SCIP_VAR** transvars;
11694  SCIP_Longint* weights;
11695  SCIP_Longint capacity;
11696  SCIP_Longint weight;
11697  int mult;
11698  int v;
11699 
11700  assert(nvars == 0 || vars != NULL);
11701  assert(nvars == 0 || vals != NULL);
11702  assert(SCIPisInfinity(scip, -lhs) != SCIPisInfinity(scip, rhs));
11703 
11704  /* get temporary memory */
11705  SCIP_CALL( SCIPallocBufferArray(scip, &transvars, nvars) );
11706  SCIP_CALL( SCIPallocBufferArray(scip, &weights, nvars) );
11707 
11708  /* if the right hand side is non-infinite, we have to negate all variables with negative coefficient;
11709  * otherwise, we have to negate all variables with positive coefficient and multiply the row with -1
11710  */
11711  if( SCIPisInfinity(scip, rhs) )
11712  {
11713  mult = -1;
11714  capacity = (SCIP_Longint)SCIPfeasFloor(scip, -lhs);
11715  }
11716  else
11717  {
11718  mult = +1;
11719  capacity = (SCIP_Longint)SCIPfeasFloor(scip, rhs);
11720  }
11721 
11722  /* negate positive or negative variables */
11723  for( v = 0; v < nvars; ++v )
11724  {
11725  assert(SCIPisFeasIntegral(scip, vals[v]));
11726  weight = mult * (SCIP_Longint)SCIPfeasFloor(scip, vals[v]);
11727  if( weight > 0 )
11728  {
11729  transvars[v] = vars[v];
11730  weights[v] = weight;
11731  }
11732  else
11733  {
11734  SCIP_CALL( SCIPgetNegatedVar(scip, vars[v], &transvars[v]) );
11735  weights[v] = -weight;
11736  capacity -= weight;
11737  }
11738  assert(transvars[v] != NULL);
11739  }
11740 
11741  /* create the constraint */
11742  SCIP_CALL( SCIPcreateConsKnapsack(scip, cons, name, nvars, transvars, weights, capacity,
11743  initial, separate, enforce, check, propagate, local, modifiable, dynamic, removable, stickingatnode) );
11744 
11745  /* free temporary memory */
11746  SCIPfreeBufferArray(scip, &weights);
11747  SCIPfreeBufferArray(scip, &transvars);
11748 
11749  return SCIP_OKAY;
11750 }
11751 
11752 /** tries to upgrade a linear constraint into a knapsack constraint */
11753 static
11754 SCIP_DECL_LINCONSUPGD(linconsUpgdKnapsack)
11755 { /*lint --e{715}*/
11756  SCIP_Bool upgrade;
11757 
11758  assert(upgdcons != NULL);
11759 
11760  /* check, if linear constraint can be upgraded to a knapsack constraint
11761  * - all variables must be binary
11762  * - all coefficients must be integral
11763  * - exactly one of the sides must be infinite
11764  */
11765  upgrade = (nposbin + nnegbin + nposimplbin + nnegimplbin == nvars)
11766  && (ncoeffspone + ncoeffsnone + ncoeffspint + ncoeffsnint == nvars)
11767  && (SCIPisInfinity(scip, -lhs) != SCIPisInfinity(scip, rhs));
11768 
11769  if( upgrade )
11770  {
11771  SCIPdebugMessage("upgrading constraint <%s> to knapsack constraint\n", SCIPconsGetName(cons));
11772 
11773  /* create the knapsack constraint (an automatically upgraded constraint is always unmodifiable) */
11774  assert(!SCIPconsIsModifiable(cons));
11775  SCIP_CALL( createNormalizedKnapsack(scip, upgdcons, SCIPconsGetName(cons), nvars, vars, vals, lhs, rhs,
11780  }
11781 
11782  return SCIP_OKAY;
11783 }
11784 
11785 
11786 /*
11787  * Callback methods of constraint handler
11788  */
11789 
11790 /** copy method for constraint handler plugins (called when SCIP copies plugins) */
11791 static
11792 SCIP_DECL_CONSHDLRCOPY(conshdlrCopyKnapsack)
11793 { /*lint --e{715}*/
11794  assert(scip != NULL);
11795  assert(conshdlr != NULL);
11796  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
11797 
11798  /* call inclusion method of constraint handler */
11800 
11801  *valid = TRUE;
11802 
11803  return SCIP_OKAY;
11804 }
11805 
11806 /** destructor of constraint handler to free constraint handler data (called when SCIP is exiting) */
11807 static
11808 SCIP_DECL_CONSFREE(consFreeKnapsack)
11809 { /*lint --e{715}*/
11810  SCIP_CONSHDLRDATA* conshdlrdata;
11811 
11812  /* free constraint handler data */
11813  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11814  assert(conshdlrdata != NULL);
11815 
11816  SCIPfreeMemory(scip, &conshdlrdata);
11817 
11818  SCIPconshdlrSetData(conshdlr, NULL);
11819 
11820  return SCIP_OKAY;
11821 }
11822 
11823 
11824 /** initialization method of constraint handler (called after problem was transformed) */
11825 static
11826 SCIP_DECL_CONSINIT(consInitKnapsack)
11827 { /*lint --e{715}*/
11828  SCIP_CONSHDLRDATA* conshdlrdata;
11829  int nvars;
11830 
11831  assert( scip != NULL );
11832  assert( conshdlr != NULL );
11833 
11834  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11835  assert(conshdlrdata != NULL);
11836 
11837  /* all variables which are of integral type can be binary; this can be checked via the method SCIPvarIsBinary(var) */
11838  nvars = SCIPgetNVars(scip) - SCIPgetNContVars(scip);
11839 
11840  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->reals1, nvars) );
11841  BMSclearMemoryArray(conshdlrdata->reals1, nvars);
11842  conshdlrdata->reals1size = nvars;
11843 
11844  return SCIP_OKAY;
11845 }
11846 
11847 /** deinitialization method of constraint handler (called before transformed problem is freed) */
11848 static
11849 SCIP_DECL_CONSEXIT(consExitKnapsack)
11850 { /*lint --e{715}*/
11851  SCIP_CONSHDLRDATA* conshdlrdata;
11852 
11853  assert( scip != NULL );
11854  assert( conshdlr != NULL );
11855 
11856  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11857  assert(conshdlrdata != NULL);
11858 
11859  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->reals1);
11860  conshdlrdata->reals1size = 0;
11861 
11862  return SCIP_OKAY;
11863 }
11864 
11865 
11866 /** presolving initialization method of constraint handler (called when presolving is about to begin) */
11867 static
11868 SCIP_DECL_CONSINITPRE(consInitpreKnapsack)
11869 { /*lint --e{715}*/
11870  SCIP_CONSHDLRDATA* conshdlrdata;
11871  int nvars;
11872 
11873  assert(scip != NULL);
11874  assert(conshdlr != NULL);
11875  assert(nconss == 0 || conss != NULL);
11876 
11877  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11878  assert(conshdlrdata != NULL);
11879 
11880  /* all variables which are of integral type can be binary; this can be checked via the method SCIPvarIsBinary(var) */
11881  nvars = SCIPgetNVars(scip) - SCIPgetNContVars(scip);
11882 
11883  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->ints1, nvars) );
11884  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->ints2, nvars) );
11885  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->longints1, nvars) );
11886  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->longints2, nvars) );
11887  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->bools1, nvars) );
11888  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->bools2, nvars) );
11889  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->bools3, nvars) );
11890  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->bools4, nvars) );
11891 
11892  BMSclearMemoryArray(conshdlrdata->ints1, nvars);
11893  BMSclearMemoryArray(conshdlrdata->ints2, nvars);
11894  BMSclearMemoryArray(conshdlrdata->longints1, nvars);
11895  BMSclearMemoryArray(conshdlrdata->longints2, nvars);
11896  BMSclearMemoryArray(conshdlrdata->bools1, nvars);
11897  BMSclearMemoryArray(conshdlrdata->bools2, nvars);
11898  BMSclearMemoryArray(conshdlrdata->bools3, nvars);
11899  BMSclearMemoryArray(conshdlrdata->bools4, nvars);
11900 
11901  conshdlrdata->ints1size = nvars;
11902  conshdlrdata->ints2size = nvars;
11903  conshdlrdata->longints1size = nvars;
11904  conshdlrdata->longints2size = nvars;
11905  conshdlrdata->bools1size = nvars;
11906  conshdlrdata->bools2size = nvars;
11907  conshdlrdata->bools3size = nvars;
11908  conshdlrdata->bools4size = nvars;
11909 
11910  return SCIP_OKAY;
11911 }
11912 
11913 
11914 /** presolving deinitialization method of constraint handler (called after presolving has been finished) */
11915 static
11916 SCIP_DECL_CONSEXITPRE(consExitpreKnapsack)
11917 { /*lint --e{715}*/
11918  SCIP_CONSHDLRDATA* conshdlrdata;
11919  int c;
11920 
11921  assert(scip != NULL);
11922  assert(conshdlr != NULL);
11923 
11924  for( c = 0; c < nconss; ++c )
11925  {
11926  if( !SCIPconsIsDeleted(conss[c]) )
11927  {
11928  /* since we are not allowed to detect infeasibility in the exitpre stage, we dont give an infeasible pointer */
11929  SCIP_CALL( applyFixings(scip, conss[c], NULL) );
11930  }
11931  }
11932 
11933  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11934  assert(conshdlrdata != NULL);
11935 
11936  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->ints1);
11937  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->ints2);
11938  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->longints1);
11939  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->longints2);
11940  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->bools1);
11941  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->bools2);
11942  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->bools3);
11943  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->bools4);
11944 
11945  conshdlrdata->ints1size = 0;
11946  conshdlrdata->ints2size = 0;
11947  conshdlrdata->longints1size = 0;
11948  conshdlrdata->longints2size = 0;
11949  conshdlrdata->bools1size = 0;
11950  conshdlrdata->bools2size = 0;
11951  conshdlrdata->bools3size = 0;
11952  conshdlrdata->bools4size = 0;
11953 
11954  return SCIP_OKAY;
11955 }
11956 
11957 
11958 /** solving process deinitialization method of constraint handler (called before branch and bound process data is freed) */
11959 static
11960 SCIP_DECL_CONSEXITSOL(consExitsolKnapsack)
11961 { /*lint --e{715}*/
11962  SCIP_CONSDATA* consdata;
11963  int c;
11964 
11965  assert( scip != NULL );
11966 
11967  /* release the rows of all constraints */
11968  for( c = 0; c < nconss; ++c )
11969  {
11970  consdata = SCIPconsGetData(conss[c]);
11971  assert(consdata != NULL);
11972 
11973  if( consdata->row != NULL )
11974  {
11975  SCIP_CALL( SCIPreleaseRow(scip, &consdata->row) );
11976  }
11977  }
11978 
11979  return SCIP_OKAY;
11980 }
11981 
11982 /** frees specific constraint data */
11983 static
11984 SCIP_DECL_CONSDELETE(consDeleteKnapsack)
11985 { /*lint --e{715}*/
11986  SCIP_CONSHDLRDATA* conshdlrdata;
11987 
11988  assert(conshdlr != NULL);
11989  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
11990 
11991  /* get event handler */
11992  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11993  assert(conshdlrdata != NULL);
11994  assert(conshdlrdata->eventhdlr != NULL);
11995 
11996  /* free knapsack constraint */
11997  SCIP_CALL( consdataFree(scip, consdata, conshdlrdata->eventhdlr) );
11998 
11999  return SCIP_OKAY;
12000 }
12001 
12002 /** transforms constraint data into data belonging to the transformed problem */
12003 static
12004 SCIP_DECL_CONSTRANS(consTransKnapsack)
12005 { /*lint --e{715}*/
12006  SCIP_CONSHDLRDATA* conshdlrdata;
12007  SCIP_CONSDATA* sourcedata;
12008  SCIP_CONSDATA* targetdata;
12009 
12010  assert(conshdlr != NULL);
12011  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
12012  assert(SCIPgetStage(scip) == SCIP_STAGE_TRANSFORMING);
12013  assert(sourcecons != NULL);
12014  assert(targetcons != NULL);
12015 
12016  sourcedata = SCIPconsGetData(sourcecons);
12017  assert(sourcedata != NULL);
12018  assert(sourcedata->row == NULL); /* in original problem, there cannot be LP rows */
12019 
12020  /* get event handler */
12021  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12022  assert(conshdlrdata != NULL);
12023  assert(conshdlrdata->eventhdlr != NULL);
12024 
12025  /* create target constraint data */
12026  SCIP_CALL( consdataCreate(scip, &targetdata, conshdlrdata->eventhdlr,
12027  sourcedata->nvars, sourcedata->vars, sourcedata->weights, sourcedata->capacity) );
12028 
12029  /* create target constraint */
12030  SCIP_CALL( SCIPcreateCons(scip, targetcons, SCIPconsGetName(sourcecons), conshdlr, targetdata,
12031  SCIPconsIsInitial(sourcecons), SCIPconsIsSeparated(sourcecons), SCIPconsIsEnforced(sourcecons),
12032  SCIPconsIsChecked(sourcecons), SCIPconsIsPropagated(sourcecons),
12033  SCIPconsIsLocal(sourcecons), SCIPconsIsModifiable(sourcecons),
12034  SCIPconsIsDynamic(sourcecons), SCIPconsIsRemovable(sourcecons), SCIPconsIsStickingAtNode(sourcecons)) );
12035 
12036  return SCIP_OKAY;
12037 }
12038 
12039 /** LP initialization method of constraint handler (called before the initial LP relaxation at a node is solved) */
12040 static
12041 SCIP_DECL_CONSINITLP(consInitlpKnapsack)
12042 { /*lint --e{715}*/
12043  SCIP_Bool cutoff;
12044  int i;
12045 
12046  for( i = 0; i < nconss; i++ )
12047  {
12048  assert(SCIPconsIsInitial(conss[i]));
12049  SCIP_CALL( addRelaxation(scip, conss[i], NULL, &cutoff) );
12050  /* ignore cutoff: cannot return status */
12051  }
12052 
12053  return SCIP_OKAY;
12054 }
12055 
12056 /** separation method of constraint handler for LP solutions */
12057 static
12058 SCIP_DECL_CONSSEPALP(consSepalpKnapsack)
12059 { /*lint --e{715}*/
12060  SCIP_CONSHDLRDATA* conshdlrdata;
12061  SCIP_Bool sepacardinality;
12062  SCIP_Bool cutoff;
12063 
12064  SCIP_Real loclowerbound;
12065  SCIP_Real glblowerbound;
12066  SCIP_Real cutoffbound;
12067  SCIP_Real maxbound;
12068 
12069  int depth;
12070  int nrounds;
12071  int sepafreq;
12072  int sepacardfreq;
12073  int ncuts;
12074  int maxsepacuts;
12075  int i;
12076 
12077  *result = SCIP_DIDNOTRUN;
12078 
12079  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12080  assert(conshdlrdata != NULL);
12081 
12082  depth = SCIPgetDepth(scip);
12083  nrounds = SCIPgetNSepaRounds(scip);
12084 
12085  SCIPdebugMessage("knapsack separation of %d/%d constraints, round %d (max %d/%d)\n",
12086  nusefulconss, nconss, nrounds, conshdlrdata->maxroundsroot, conshdlrdata->maxrounds);
12087 
12088  /* only call the separator a given number of times at each node */
12089  if( (depth == 0 && conshdlrdata->maxroundsroot >= 0 && nrounds >= conshdlrdata->maxroundsroot)
12090  || (depth > 0 && conshdlrdata->maxrounds >= 0 && nrounds >= conshdlrdata->maxrounds) )
12091  return SCIP_OKAY;
12092 
12093  /* check, if we should additionally separate knapsack cuts */
12094  sepafreq = SCIPconshdlrGetSepaFreq(conshdlr);
12095  sepacardfreq = sepafreq * conshdlrdata->sepacardfreq;
12096  sepacardinality = (conshdlrdata->sepacardfreq >= 0)
12097  && ((sepacardfreq == 0 && depth == 0) || (sepacardfreq >= 1 && (depth % sepacardfreq == 0)));
12098 
12099  /* check dual bound to see if we want to produce knapsack cuts at this node */
12100  loclowerbound = SCIPgetLocalLowerbound(scip);
12101  glblowerbound = SCIPgetLowerbound(scip);
12102  cutoffbound = SCIPgetCutoffbound(scip);
12103  maxbound = glblowerbound + conshdlrdata->maxcardbounddist * (cutoffbound - glblowerbound);
12104  sepacardinality = sepacardinality && SCIPisLE(scip, loclowerbound, maxbound);
12105  sepacardinality = sepacardinality && (SCIPgetNLPBranchCands(scip) > 0);
12106 
12107  /* get the maximal number of cuts allowed in a separation round */
12108  maxsepacuts = (depth == 0 ? conshdlrdata->maxsepacutsroot : conshdlrdata->maxsepacuts);
12109 
12110  *result = SCIP_DIDNOTFIND;
12111  ncuts = 0;
12112  cutoff = FALSE;
12113 
12114  /* separate useful constraints */
12115  for( i = 0; i < nusefulconss && ncuts < maxsepacuts && !SCIPisStopped(scip); i++ )
12116  {
12117  SCIP_CALL( separateCons(scip, conss[i], NULL, sepacardinality, conshdlrdata->usegubs, &cutoff, &ncuts) );
12118  }
12119 
12120  /* adjust return value */
12121  if ( cutoff )
12122  *result = SCIP_CUTOFF;
12123  else if ( ncuts > 0 )
12124  *result = SCIP_SEPARATED;
12125 
12126  return SCIP_OKAY;
12127 }
12128 
12129 
12130 /** separation method of constraint handler for arbitrary primal solutions */
12131 static
12132 SCIP_DECL_CONSSEPASOL(consSepasolKnapsack)
12133 { /*lint --e{715}*/
12134  SCIP_CONSHDLRDATA* conshdlrdata;
12135  SCIP_Bool sepacardinality;
12136  SCIP_Bool cutoff;
12137 
12138  int depth;
12139  int nrounds;
12140  int sepafreq;
12141  int sepacardfreq;
12142  int ncuts;
12143  int maxsepacuts;
12144  int i;
12145 
12146  *result = SCIP_DIDNOTRUN;
12147 
12148  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12149  assert(conshdlrdata != NULL);
12150 
12151  depth = SCIPgetDepth(scip);
12152  nrounds = SCIPgetNSepaRounds(scip);
12153 
12154  SCIPdebugMessage("knapsack separation of %d/%d constraints, round %d (max %d/%d)\n",
12155  nusefulconss, nconss, nrounds, conshdlrdata->maxroundsroot, conshdlrdata->maxrounds);
12156 
12157  /* only call the separator a given number of times at each node */
12158  if( (depth == 0 && conshdlrdata->maxroundsroot >= 0 && nrounds >= conshdlrdata->maxroundsroot)
12159  || (depth > 0 && conshdlrdata->maxrounds >= 0 && nrounds >= conshdlrdata->maxrounds) )
12160  return SCIP_OKAY;
12161 
12162  /* check, if we should additionally separate knapsack cuts */
12163  sepafreq = SCIPconshdlrGetSepaFreq(conshdlr);
12164  sepacardfreq = sepafreq * conshdlrdata->sepacardfreq;
12165  sepacardinality = (conshdlrdata->sepacardfreq >= 0)
12166  && ((sepacardfreq == 0 && depth == 0) || (sepacardfreq >= 1 && (depth % sepacardfreq == 0)));
12167 
12168  /* get the maximal number of cuts allowed in a separation round */
12169  maxsepacuts = (depth == 0 ? conshdlrdata->maxsepacutsroot : conshdlrdata->maxsepacuts);
12170 
12171  *result = SCIP_DIDNOTFIND;
12172  ncuts = 0;
12173  cutoff = FALSE;
12174 
12175  /* separate useful constraints */
12176  for( i = 0; i < nusefulconss && ncuts < maxsepacuts && !SCIPisStopped(scip); i++ )
12177  {
12178  SCIP_CALL( separateCons(scip, conss[i], sol, sepacardinality, conshdlrdata->usegubs, &cutoff, &ncuts) );
12179  }
12180 
12181  /* adjust return value */
12182  if ( cutoff )
12183  *result = SCIP_CUTOFF;
12184  else if( ncuts > 0 )
12185  *result = SCIP_SEPARATED;
12186 
12187  return SCIP_OKAY;
12188 }
12189 
12190 
12191 /** constraint enforcing method of constraint handler for LP solutions */
12192 static
12193 SCIP_DECL_CONSENFOLP(consEnfolpKnapsack)
12194 { /*lint --e{715}*/
12195  SCIP_CONSHDLRDATA* conshdlrdata;
12196  SCIP_Bool violated;
12197  SCIP_Bool cutoff = FALSE;
12198  int maxncuts;
12199  int ncuts = 0;
12200  int i;
12201 
12202  *result = SCIP_FEASIBLE;
12203 
12204  SCIPdebugMessage("knapsack enforcement of %d/%d constraints\n", nusefulconss, nconss);
12205 
12206  /* get maximal number of cuts per round */
12207  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12208  assert(conshdlrdata != NULL);
12209  maxncuts = (SCIPgetDepth(scip) == 0 ? conshdlrdata->maxsepacutsroot : conshdlrdata->maxsepacuts);
12210 
12211  /* search for violated useful knapsack constraints */
12212  for( i = 0; i < nusefulconss && ncuts < maxncuts && ! cutoff; i++ )
12213  {
12214  SCIP_CALL( checkCons(scip, conss[i], NULL, FALSE, FALSE, &violated) );
12215  if( violated )
12216  {
12217  /* add knapsack constraint as LP row to the LP */
12218  SCIP_CALL( addRelaxation(scip, conss[i], NULL, &cutoff) );
12219  ncuts++;
12220  }
12221  }
12222 
12223  /* as long as no violations were found, search for violated obsolete knapsack constraints */
12224  for( i = nusefulconss; i < nconss && ncuts == 0 && ! cutoff; i++ )
12225  {
12226  SCIP_CALL( checkCons(scip, conss[i], NULL, FALSE, FALSE, &violated) );
12227  if( violated )
12228  {
12229  /* add knapsack constraint as LP row to the LP */
12230  SCIP_CALL( addRelaxation(scip, conss[i], NULL, &cutoff) );
12231  ncuts++;
12232  }
12233  }
12234 
12235  /* adjust the result code */
12236  if ( cutoff )
12237  *result = SCIP_CUTOFF;
12238  else if ( ncuts > 0 )
12239  *result = SCIP_SEPARATED;
12240 
12241  return SCIP_OKAY;
12242 }
12243 
12244 /** constraint enforcing method of constraint handler for pseudo solutions */
12245 static
12246 SCIP_DECL_CONSENFOPS(consEnfopsKnapsack)
12247 { /*lint --e{715}*/
12248  SCIP_Bool violated;
12249  int i;
12250 
12251  for( i = 0; i < nconss; i++ )
12252  {
12253  SCIP_CALL( checkCons(scip, conss[i], NULL, TRUE, FALSE, &violated) );
12254  if( violated )
12255  {
12256  *result = SCIP_INFEASIBLE;
12257  return SCIP_OKAY;
12258  }
12259  }
12260  *result = SCIP_FEASIBLE;
12261 
12262  return SCIP_OKAY;
12263 }
12264 
12265 /** feasibility check method of constraint handler for integral solutions */
12266 static
12267 SCIP_DECL_CONSCHECK(consCheckKnapsack)
12268 { /*lint --e{715}*/
12269  SCIP_Bool violated;
12270  int i;
12271 
12272  for( i = 0; i < nconss; i++ )
12273  {
12274  SCIP_CALL( checkCons(scip, conss[i], sol, checklprows, printreason, &violated) );
12275  if( violated )
12276  {
12277  *result = SCIP_INFEASIBLE;
12278  return SCIP_OKAY;
12279  }
12280  }
12281  *result = SCIP_FEASIBLE;
12282 
12283  return SCIP_OKAY;
12284 }
12285 
12286 /** domain propagation method of constraint handler */
12287 static
12288 SCIP_DECL_CONSPROP(consPropKnapsack)
12289 { /*lint --e{715}*/
12290  SCIP_CONSHDLRDATA* conshdlrdata;
12291  SCIP_Bool cutoff;
12292  SCIP_Bool redundant;
12293  SCIP_Bool inpresolve;
12294  int nfixedvars;
12295  int i;
12296 
12297  cutoff = FALSE;
12298  nfixedvars = 0;
12299 
12300  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12301  assert(conshdlrdata != NULL);
12302 
12303  inpresolve = (SCIPgetStage(scip) < SCIP_STAGE_INITSOLVE);
12304 
12305  /* process useful constraints */
12306  for( i = 0; i < nusefulconss && !cutoff; i++ )
12307  {
12308  /* do not propagate constraints with multi-aggregated variables, which should only happen in probing mode,
12309  * otherwise the multi-aggregation should be resolved
12310  */
12311  if( inpresolve && SCIPconsGetData(conss[i])->existmultaggr )
12312  continue;
12313 #ifndef NDEBUG
12314  else if( !inpresolve )
12315  assert(!(SCIPconsGetData(conss[i])->existmultaggr));
12316 #endif
12317 
12318  SCIP_CALL( propagateCons(scip, conss[i], &cutoff, &redundant, &nfixedvars, conshdlrdata->negatedclique) );
12319  }
12320 
12321  /* adjust result code */
12322  if( cutoff )
12323  *result = SCIP_CUTOFF;
12324  else if( nfixedvars > 0 )
12325  *result = SCIP_REDUCEDDOM;
12326  else
12327  *result = SCIP_DIDNOTFIND;
12328 
12329  return SCIP_OKAY;
12330 }
12331 
12332 /** presolving method of constraint handler */
12333 static
12334 SCIP_DECL_CONSPRESOL(consPresolKnapsack)
12335 { /*lint --e{715}*/
12336  SCIP_CONSHDLRDATA* conshdlrdata;
12337  SCIP_CONSDATA* consdata;
12338  SCIP_CONS* cons;
12339  SCIP_Bool cutoff;
12340  SCIP_Bool redundant;
12341  SCIP_Bool success;
12342  int oldnfixedvars;
12343  int oldnchgbds;
12344  int oldndelconss;
12345  int oldnaddconss;
12346  int oldnchgcoefs;
12347  int oldnchgsides;
12348  int firstchange;
12349  int c;
12350 
12351  /* remember old preprocessing counters */
12352  cutoff = FALSE;
12353  oldnfixedvars = *nfixedvars;
12354  oldnchgbds = *nchgbds;
12355  oldndelconss = *ndelconss;
12356  oldnaddconss = *naddconss;
12357  oldnchgcoefs = *nchgcoefs;
12358  oldnchgsides = *nchgsides;
12359  firstchange = INT_MAX;
12360 
12361  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12362  assert(conshdlrdata != NULL);
12363 
12364  for( c = 0; c < nconss && !SCIPisStopped(scip); c++ )
12365  {
12366  int thisnfixedvars;
12367  int thisnchgbds;
12368 
12369  cons = conss[c];
12370  consdata = SCIPconsGetData(cons);
12371  assert(consdata != NULL);
12372 
12373  /* force presolving the constraint in the initial round */
12374  if( nrounds == 0 )
12375  consdata->presolved = FALSE;
12376  else if( consdata->presolved )
12377  continue;
12378 
12379  SCIPdebugMessage("presolving knapsack constraint <%s>\n", SCIPconsGetName(cons));
12380  SCIPdebugPrintCons(scip, cons, NULL);
12381  consdata->presolved = TRUE;
12382 
12383  if( nrounds == 0 || nnewfixedvars > 0 || nnewaggrvars > 0 || nnewchgbds > 0
12384  || *nfixedvars > oldnfixedvars || *nchgbds > oldnchgbds )
12385  {
12386  SCIP_CALL( applyFixings(scip, cons, &cutoff) );
12387  if( cutoff )
12388  break;
12389  }
12390 
12391  thisnfixedvars = *nfixedvars;
12392  thisnchgbds = *nchgbds;
12393 
12394  /* merge constraint, so propagation works better */
12395  SCIP_CALL( mergeMultiples(scip, cons, &cutoff) );
12396  if( cutoff )
12397  return SCIP_OKAY;
12398 
12399  /* add cliques in the knapsack to the clique table */
12400  SCIP_CALL( addCliques(scip, cons, &cutoff, nchgbds) );
12401  if( cutoff )
12402  break;
12403 
12404  /* propagate constraint */
12405  SCIP_CALL( propagateCons(scip, cons, &cutoff, &redundant, nfixedvars, TRUE) );
12406  if( cutoff )
12407  break;
12408  if( redundant )
12409  {
12410  (*ndelconss)++;
12411  continue;
12412  }
12413 
12414  /* remove again all fixed variables, if further fixings were found */
12415  if( *nfixedvars > thisnfixedvars || *nchgbds > thisnchgbds )
12416  {
12417  SCIP_CALL( applyFixings(scip, cons, &cutoff) );
12418  if( cutoff )
12419  break;
12420 
12421  thisnfixedvars = *nfixedvars;
12422  }
12423 
12424  if( !SCIPconsIsModifiable(cons) )
12425  {
12426  /* check again for redundancy (applyFixings() might have decreased weightsum due to fixed-to-zero vars) */
12427  if( consdata->weightsum <= consdata->capacity )
12428  {
12429  SCIPdebugMessage(" -> knapsack constraint <%s> is redundant: weightsum=%"SCIP_LONGINT_FORMAT", capacity=%"SCIP_LONGINT_FORMAT"\n",
12430  SCIPconsGetName(cons), consdata->weightsum, consdata->capacity);
12431  SCIP_CALL( SCIPdelConsLocal(scip, cons) );
12432  continue;
12433  }
12434 
12435  /* divide weights by their greatest common divisor */
12436  normalizeWeights(cons, nchgcoefs, nchgsides);
12437 
12438  /* try to simplify inequalities */
12439  if( conshdlrdata->simplifyinequalities )
12440  {
12441  SCIP_CALL( simplifyInequalities(scip, cons, nfixedvars, ndelconss, nchgcoefs, nchgsides, naddconss, &cutoff) );
12442  if( cutoff )
12443  break;
12444 
12445  if( SCIPconsIsDeleted(cons) )
12446  continue;
12447 
12448  /* remove again all fixed variables, if further fixings were found */
12449  if( *nfixedvars > thisnfixedvars )
12450  {
12451  SCIP_CALL( applyFixings(scip, cons, &cutoff) );
12452  if( cutoff )
12453  break;
12454  }
12455  }
12456 
12457  /* tighten capacity and weights */
12458  SCIP_CALL( tightenWeights(scip, cons, nchgcoefs, nchgsides, naddconss, ndelconss, &cutoff) );
12459  if( cutoff )
12460  break;
12461 
12462  if( SCIPconsIsActive(cons) )
12463  {
12464  if( conshdlrdata->dualpresolving )
12465  {
12466  /* in case the knapsack constraints is independent of everything else, solve the knapsack and apply the
12467  * dual reduction
12468  */
12469  SCIP_CALL( dualPresolving(scip, cons, nchgbds, ndelconss, &redundant) );
12470  if( redundant )
12471  continue;
12472  }
12473 
12474  /* check if knapsack constraint is parallel to objective function */
12475  SCIP_CALL( checkParallelObjective(scip, cons, conshdlrdata) );
12476  }
12477  }
12478  /* remember the first changed constraint to begin the next aggregation round with */
12479  if( firstchange == INT_MAX && !consdata->presolved )
12480  firstchange = c;
12481  }
12482 
12483  /* preprocess pairs of knapsack constraints */
12484  if( !cutoff && conshdlrdata->presolusehashing )
12485  {
12486  /* detect redundant constraints; fast version with hash table instead of pairwise comparison */
12487  SCIP_CALL( detectRedundantConstraints(scip, SCIPblkmem(scip), conss, nconss, &cutoff, ndelconss) );
12488  }
12489 
12490  if( (*ndelconss != oldndelconss) || (*nchgsides != oldnchgsides) || (*nchgcoefs != oldnchgcoefs) || (*naddconss != oldnaddconss) )
12491  success = TRUE;
12492  else
12493  success = FALSE;
12494 
12495  if( !cutoff && firstchange < nconss && conshdlrdata->presolpairwise )
12496  {
12497  SCIP_Longint npaircomparisons;
12498 
12499  npaircomparisons = 0;
12500  oldndelconss = *ndelconss;
12501  oldnchgsides = *nchgsides;
12502  oldnchgcoefs = *nchgcoefs;
12503 
12504  for( c = firstchange; c < nconss && !cutoff && !SCIPisStopped(scip); ++c )
12505  {
12506  cons = conss[c];
12507  if( !SCIPconsIsActive(cons) || SCIPconsIsModifiable(cons) )
12508  continue;
12509 
12510  npaircomparisons += ((!SCIPconsGetData(cons)->presolved) ? (SCIP_Longint) c : ((SCIP_Longint) c - (SCIP_Longint) firstchange));
12511 
12512  SCIP_CALL( preprocessConstraintPairs(scip, conss, firstchange, c, ndelconss) );
12513 
12514  if( npaircomparisons > NMINCOMPARISONS )
12515  {
12516  if( (*ndelconss != oldndelconss) || (*nchgsides != oldnchgsides) || (*nchgcoefs != oldnchgcoefs) )
12517  success = TRUE;
12518  if( ((SCIP_Real) (*ndelconss - oldndelconss) + ((SCIP_Real) (*nchgsides - oldnchgsides))/2.0 +
12519  ((SCIP_Real) (*nchgcoefs - oldnchgcoefs))/10.0) / ((SCIP_Real) npaircomparisons) < MINGAINPERNMINCOMPARISONS )
12520  break;
12521  oldndelconss = *ndelconss;
12522  oldnchgsides = *nchgsides;
12523  oldnchgcoefs = *nchgcoefs;
12524  npaircomparisons = 0;
12525  }
12526  }
12527  }
12528 
12529  if( cutoff )
12530  *result = SCIP_CUTOFF;
12531  else if( success || *nfixedvars > oldnfixedvars || *nchgbds > oldnchgbds )
12532  *result = SCIP_SUCCESS;
12533  else
12534  *result = SCIP_DIDNOTFIND;
12535 
12536  return SCIP_OKAY;
12537 }
12538 
12539 /** propagation conflict resolving method of constraint handler */
12540 static
12541 SCIP_DECL_CONSRESPROP(consRespropKnapsack)
12542 { /*lint --e{715}*/
12543  SCIP_CONSDATA* consdata;
12544  SCIP_Longint capsum;
12545  int i;
12546 
12547  assert(result != NULL);
12548 
12549  consdata = SCIPconsGetData(cons);
12550  assert(consdata != NULL);
12551 
12552  /* check if we fixed a binary variable to one (due to negated clique) */
12553  if( inferinfo >= 0 && SCIPvarGetLbLocal(infervar) > 0.5 )
12554  {
12555  for( i = 0; i < consdata->nvars; ++i )
12556  {
12557  if( SCIPvarGetIndex(consdata->vars[i]) == inferinfo )
12558  {
12559  assert( SCIPvarGetUbAtIndex(consdata->vars[i], bdchgidx, FALSE) < 0.5 );
12560  SCIP_CALL( SCIPaddConflictBinvar(scip, consdata->vars[i]) );
12561  break;
12562  }
12563  }
12564  assert(i < consdata->nvars);
12565  }
12566  else
12567  {
12568  /* according to negated cliques the minweightsum and all variables which are fixed to one which led to a fixing of
12569  * another negated clique variable to one, the inferinfo was chosen to be the negative of the position in the
12570  * knapsack constraint, see one above call of SCIPinferBinvarCons
12571  */
12572  if( inferinfo < 0 )
12573  capsum = 0;
12574  else
12575  {
12576  /* locate the inference variable and calculate the capacity that has to be used up to conclude infervar == 0;
12577  * inferinfo stores the position of the inference variable (but maybe the variables were resorted)
12578  */
12579  if( inferinfo < consdata->nvars && consdata->vars[inferinfo] == infervar )
12580  capsum = consdata->weights[inferinfo];
12581  else
12582  {
12583  for( i = 0; i < consdata->nvars && consdata->vars[i] != infervar; ++i )
12584  {}
12585  assert(i < consdata->nvars);
12586  capsum = consdata->weights[i];
12587  }
12588  }
12589 
12590  /* add fixed-to-one variables up to the point, that their weight plus the weight of the conflict variable exceeds
12591  * the capacity
12592  */
12593  if( capsum <= consdata->capacity )
12594  {
12595  for( i = 0; i < consdata->nvars; i++ )
12596  {
12597  if( SCIPvarGetLbAtIndex(consdata->vars[i], bdchgidx, FALSE) > 0.5 )
12598  {
12599  SCIP_CALL( SCIPaddConflictBinvar(scip, consdata->vars[i]) );
12600  capsum += consdata->weights[i];
12601  if( capsum > consdata->capacity )
12602  break;
12603  }
12604  }
12605  }
12606  }
12607 
12608  /* NOTE: It might be the case that capsum < consdata->capacity. This is due the fact that the fixing of the variable
12609  * to zero can included negated clique information. A negated clique means, that at most one of the clique
12610  * variables can be zero. These information can be used to compute a minimum activity of the constraint and
12611  * used to fix variables to zero.
12612  *
12613  * Even if capsum < consdata->capacity we still reported a complete reason since the minimum activity is based
12614  * on global variable bounds. It might even be the case that we reported to many variables which are fixed to
12615  * one.
12616  */
12617  *result = SCIP_SUCCESS;
12618 
12619  return SCIP_OKAY;
12620 }
12621 
12622 /** variable rounding lock method of constraint handler */
12623 static
12624 SCIP_DECL_CONSLOCK(consLockKnapsack)
12625 { /*lint --e{715}*/
12626  SCIP_CONSDATA* consdata;
12627  int i;
12628 
12629  consdata = SCIPconsGetData(cons);
12630  assert(consdata != NULL);
12631 
12632  for( i = 0; i < consdata->nvars; i++)
12633  {
12634  SCIP_CALL( SCIPaddVarLocks(scip, consdata->vars[i], nlocksneg, nlockspos) );
12635  }
12636 
12637  return SCIP_OKAY;
12638 }
12639 
12640 
12641 /** variable deletion method of constraint handler */
12642 static
12643 SCIP_DECL_CONSDELVARS(consDelvarsKnapsack)
12644 {
12645  assert(scip != NULL);
12646  assert(conshdlr != NULL);
12647  assert(conss != NULL || nconss == 0);
12648 
12649  if( nconss > 0 )
12650  {
12651  SCIP_CALL( performVarDeletions(scip, conshdlr, conss, nconss) );
12652  }
12653 
12654  return SCIP_OKAY;
12655 }
12656 
12657 /** constraint display method of constraint handler */
12658 static
12659 SCIP_DECL_CONSPRINT(consPrintKnapsack)
12660 { /*lint --e{715}*/
12661  SCIP_CONSDATA* consdata;
12662  int i;
12663 
12664  assert( scip != NULL );
12665  assert( conshdlr != NULL );
12666  assert( cons != NULL );
12667 
12668  consdata = SCIPconsGetData(cons);
12669  assert(consdata != NULL);
12670 
12671  for( i = 0; i < consdata->nvars; ++i )
12672  {
12673  if( i > 0 )
12674  SCIPinfoMessage(scip, file, " ");
12675  SCIPinfoMessage(scip, file, "%+"SCIP_LONGINT_FORMAT, consdata->weights[i]);
12676  SCIP_CALL( SCIPwriteVarName(scip, file, consdata->vars[i], TRUE) );
12677  }
12678  SCIPinfoMessage(scip, file, " <= %"SCIP_LONGINT_FORMAT"", consdata->capacity);
12679 
12680  return SCIP_OKAY;
12681 }
12682 
12683 /** constraint copying method of constraint handler */
12684 static
12685 SCIP_DECL_CONSCOPY(consCopyKnapsack)
12686 { /*lint --e{715}*/
12687  SCIP_VAR** sourcevars;
12688  SCIP_Longint* weights;
12689  SCIP_Real* coefs;
12690  const char* consname;
12691  int nvars;
12692  int v;
12693 
12694  /* get variables and coefficients of the source constraint */
12695  sourcevars = SCIPgetVarsKnapsack(sourcescip, sourcecons);
12696  nvars = SCIPgetNVarsKnapsack(sourcescip, sourcecons);
12697  weights = SCIPgetWeightsKnapsack(sourcescip, sourcecons);
12698 
12699  SCIP_CALL( SCIPallocBufferArray(scip, &coefs, nvars) );
12700  for( v = 0; v < nvars; ++v )
12701  coefs[v] = (SCIP_Real) weights[v];
12702 
12703  if( name != NULL )
12704  consname = name;
12705  else
12706  consname = SCIPconsGetName(sourcecons);
12707 
12708  /* copy the logic using the linear constraint copy method */
12709  SCIP_CALL( SCIPcopyConsLinear(scip, cons, sourcescip, consname, nvars, sourcevars, coefs,
12710  -SCIPinfinity(scip), (SCIP_Real) SCIPgetCapacityKnapsack(sourcescip, sourcecons), varmap, consmap,
12711  initial, separate, enforce, check, propagate, local, modifiable, dynamic, removable, stickingatnode, global, valid) );
12712  assert(cons != NULL);
12713 
12714  SCIPfreeBufferArray(scip, &coefs);
12715 
12716  return SCIP_OKAY;
12717 }
12718 
12719 /** constraint parsing method of constraint handler */
12720 static
12721 SCIP_DECL_CONSPARSE(consParseKnapsack)
12722 { /*lint --e{715}*/
12723  SCIP_VAR* var;
12724  SCIP_Longint weight;
12725  SCIP_VAR** vars;
12726  SCIP_Longint* weights;
12727  SCIP_Longint capacity;
12728  char* endptr;
12729  int nread;
12730  int nvars;
12731  int varssize;
12732 
12733  assert(scip != NULL);
12734  assert(success != NULL);
12735  assert(str != NULL);
12736  assert(name != NULL);
12737  assert(cons != NULL);
12738 
12739  *success = TRUE;
12740 
12741  nvars = 0;
12742  varssize = 5;
12743  SCIP_CALL( SCIPallocBufferArray(scip, &vars, varssize) );
12744  SCIP_CALL( SCIPallocBufferArray(scip, &weights, varssize) );
12745 
12746  while( *str != '\0' )
12747  {
12748  /* try to parse coefficient, and stop if not successful (probably reached <=) */
12749  if( sscanf(str, "%"SCIP_LONGINT_FORMAT"%n", &weight, &nread) < 1 )
12750  break;
12751 
12752  str += nread;
12753 
12754  /* skip whitespace */
12755  while( isspace((int)*str) )
12756  ++str;
12757 
12758  /* parse variable name */
12759  SCIP_CALL( SCIPparseVarName(scip, str, &var, &endptr) );
12760  if( var == NULL )
12761  {
12762  SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, "unknown variable name at '%s'\n", str);
12763  *success = FALSE;
12764  break;
12765  }
12766 
12767  str = endptr;
12768 
12769  /* store weight and variable */
12770  if( varssize <= nvars )
12771  {
12772  varssize = SCIPcalcMemGrowSize(scip, varssize+1);
12773  SCIP_CALL( SCIPreallocBufferArray(scip, &vars, varssize) );
12774  SCIP_CALL( SCIPreallocBufferArray(scip, &weights, varssize) );
12775  }
12776 
12777  vars[nvars] = var;
12778  weights[nvars] = weight;
12779  ++nvars;
12780 
12781  /* skip whitespace */
12782  while( isspace((int)*str) )
12783  ++str;
12784  }
12785 
12786  if( *success )
12787  {
12788  if( strncmp(str, "<= ", 3) != 0 )
12789  {
12790  SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, "expected '<= ' at begin of '%s'\n", str);
12791  *success = FALSE;
12792  }
12793  else
12794  {
12795  str += 3;
12796  }
12797  }
12798 
12799  if( *success )
12800  {
12801  if( sscanf(str, "%"SCIP_LONGINT_FORMAT, &capacity) != 1 )
12802  {
12803  SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, "error parsing capacity from '%s'\n", str);
12804  *success = FALSE;
12805  }
12806  else
12807  {
12808  SCIP_CALL( SCIPcreateConsKnapsack(scip, cons, name, nvars, vars, weights, capacity,
12809  initial, separate, enforce, check, propagate, local, modifiable, dynamic, removable, stickingatnode) );
12810  }
12811  }
12812 
12813  SCIPfreeBufferArray(scip, &vars);
12814  SCIPfreeBufferArray(scip, &weights);
12815 
12816  return SCIP_OKAY;
12817 }
12818 
12819 /** constraint method of constraint handler which returns the variables (if possible) */
12820 static
12821 SCIP_DECL_CONSGETVARS(consGetVarsKnapsack)
12822 { /*lint --e{715}*/
12823  SCIP_CONSDATA* consdata;
12824 
12825  consdata = SCIPconsGetData(cons);
12826  assert(consdata != NULL);
12827 
12828  if( varssize < consdata->nvars )
12829  (*success) = FALSE;
12830  else
12831  {
12832  assert(vars != NULL);
12833 
12834  BMScopyMemoryArray(vars, consdata->vars, consdata->nvars);
12835  (*success) = TRUE;
12836  }
12837 
12838  return SCIP_OKAY;
12839 }
12840 
12841 /** constraint method of constraint handler which returns the number of variables (if possible) */
12842 static
12843 SCIP_DECL_CONSGETNVARS(consGetNVarsKnapsack)
12844 { /*lint --e{715}*/
12845  SCIP_CONSDATA* consdata;
12846 
12847  consdata = SCIPconsGetData(cons);
12848  assert(consdata != NULL);
12849 
12850  (*nvars) = consdata->nvars;
12851  (*success) = TRUE;
12852 
12853  return SCIP_OKAY;
12854 }
12855 
12856 /*
12857  * Event handler
12858  */
12859 
12860 /** execution method of bound change event handler */
12861 static
12862 SCIP_DECL_EVENTEXEC(eventExecKnapsack)
12863 { /*lint --e{715}*/
12864  assert(eventdata != NULL);
12865  assert(eventdata->consdata != NULL);
12866 
12867  switch( SCIPeventGetType(event) )
12868  {
12869  SCIP_CONSHDLR* conshdlr;
12870  SCIP_CONSHDLRDATA* conshdlrdata;
12871 
12873  eventdata->consdata->onesweightsum += eventdata->weight;
12874  eventdata->consdata->propagated = FALSE;
12875  eventdata->consdata->presolved = FALSE;
12876  break;
12878  eventdata->consdata->onesweightsum -= eventdata->weight;
12879 
12880  conshdlr = SCIPfindConshdlr(scip, CONSHDLR_NAME);
12881  assert(conshdlr != NULL);
12882  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12883  assert(conshdlrdata != NULL);
12884 
12885  if( conshdlrdata->negatedclique )
12886  {
12887  /* if a variable fixed to 1 is unfixed, it is possible, that it can be fixed to 1 again */
12888  eventdata->consdata->propagated = FALSE;
12889  }
12890 
12891  break;
12893  /* if a variable fixed to 0 is unfixed, it is possible, that it can be fixed to 0 again */
12894  eventdata->consdata->propagated = FALSE;
12895  break;
12896  case SCIP_EVENTTYPE_VARFIXED: /* the variable should be removed from the constraint in presolving */
12897  if( !eventdata->consdata->existmultaggr )
12898  {
12899  SCIP_VAR* var = SCIPeventGetVar(event);
12900  assert(var != NULL);
12901 
12903  eventdata->consdata->existmultaggr = TRUE;
12904  }
12905  /*lint -fallthrough*/
12906  case SCIP_EVENTTYPE_IMPLADDED: /* further preprocessing might be possible due to additional implications */
12907  eventdata->consdata->presolved = FALSE;
12908  break;
12910  eventdata->consdata->varsdeleted = TRUE;
12911  break;
12912  default:
12913  SCIPerrorMessage("invalid event type %x\n", SCIPeventGetType(event));
12914  return SCIP_INVALIDDATA;
12915  }
12916 
12917  return SCIP_OKAY;
12918 }
12919 
12920 
12921 /*
12922  * constraint specific interface methods
12923  */
12924 
12925 /** creates the handler for knapsack constraints and includes it in SCIP */
12927  SCIP* scip /**< SCIP data structure */
12928  )
12929 {
12930  SCIP_EVENTHDLRDATA* eventhdlrdata;
12931  SCIP_CONSHDLRDATA* conshdlrdata;
12932  SCIP_CONSHDLR* conshdlr;
12933 
12934  /* create knapsack constraint handler data */
12935  SCIP_CALL( SCIPallocMemory(scip, &conshdlrdata) );
12936 
12937  /* include event handler for bound change events */
12938  eventhdlrdata = NULL;
12939  conshdlrdata->eventhdlr = NULL;
12940  SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &(conshdlrdata->eventhdlr), EVENTHDLR_NAME, EVENTHDLR_DESC,
12941  eventExecKnapsack, eventhdlrdata) );
12942 
12943  /* get event handler for bound change events */
12944  if( conshdlrdata->eventhdlr == NULL )
12945  {
12946  SCIPerrorMessage("event handler for knapsack constraints not found\n");
12947  return SCIP_PLUGINNOTFOUND;
12948  }
12949 
12950  /* include constraint handler */
12953  consEnfolpKnapsack, consEnfopsKnapsack, consCheckKnapsack, consLockKnapsack,
12954  conshdlrdata) );
12955 
12956  assert(conshdlr != NULL);
12957 
12958  /* set non-fundamental callbacks via specific setter functions */
12959  SCIP_CALL( SCIPsetConshdlrCopy(scip, conshdlr, conshdlrCopyKnapsack, consCopyKnapsack) );
12960  SCIP_CALL( SCIPsetConshdlrDelete(scip, conshdlr, consDeleteKnapsack) );
12961  SCIP_CALL( SCIPsetConshdlrDelvars(scip, conshdlr, consDelvarsKnapsack) );
12962  SCIP_CALL( SCIPsetConshdlrExit(scip, conshdlr, consExitKnapsack) );
12963  SCIP_CALL( SCIPsetConshdlrExitpre(scip, conshdlr, consExitpreKnapsack) );
12964  SCIP_CALL( SCIPsetConshdlrExitsol(scip, conshdlr, consExitsolKnapsack) );
12965  SCIP_CALL( SCIPsetConshdlrFree(scip, conshdlr, consFreeKnapsack) );
12966  SCIP_CALL( SCIPsetConshdlrGetVars(scip, conshdlr, consGetVarsKnapsack) );
12967  SCIP_CALL( SCIPsetConshdlrGetNVars(scip, conshdlr, consGetNVarsKnapsack) );
12968  SCIP_CALL( SCIPsetConshdlrInit(scip, conshdlr, consInitKnapsack) );
12969  SCIP_CALL( SCIPsetConshdlrInitpre(scip, conshdlr, consInitpreKnapsack) );
12970  SCIP_CALL( SCIPsetConshdlrInitlp(scip, conshdlr, consInitlpKnapsack) );
12971  SCIP_CALL( SCIPsetConshdlrParse(scip, conshdlr, consParseKnapsack) );
12972  SCIP_CALL( SCIPsetConshdlrPresol(scip, conshdlr, consPresolKnapsack,CONSHDLR_MAXPREROUNDS, CONSHDLR_DELAYPRESOL) );
12973  SCIP_CALL( SCIPsetConshdlrPrint(scip, conshdlr, consPrintKnapsack) );
12974  SCIP_CALL( SCIPsetConshdlrProp(scip, conshdlr, consPropKnapsack, CONSHDLR_PROPFREQ, CONSHDLR_DELAYPROP,
12976  SCIP_CALL( SCIPsetConshdlrResprop(scip, conshdlr, consRespropKnapsack) );
12977  SCIP_CALL( SCIPsetConshdlrSepa(scip, conshdlr, consSepalpKnapsack, consSepasolKnapsack, CONSHDLR_SEPAFREQ,
12979  SCIP_CALL( SCIPsetConshdlrTrans(scip, conshdlr, consTransKnapsack) );
12980 
12981  if( SCIPfindConshdlr(scip,"linear") != NULL )
12982  {
12983  /* include the linear constraint to knapsack constraint upgrade in the linear constraint handler */
12985  }
12986 
12987  /* add knapsack constraint handler parameters */
12988  SCIP_CALL( SCIPaddIntParam(scip,
12989  "constraints/"CONSHDLR_NAME"/sepacardfreq",
12990  "multiplier on separation frequency, how often knapsack cuts are separated (-1: never, 0: only at root)",
12991  &conshdlrdata->sepacardfreq, TRUE, DEFAULT_SEPACARDFREQ, -1, INT_MAX, NULL, NULL) );
12993  "constraints/"CONSHDLR_NAME"/maxcardbounddist",
12994  "maximal relative distance from current node's dual bound to primal bound compared to best node's dual bound for separating knapsack cuts",
12995  &conshdlrdata->maxcardbounddist, TRUE, DEFAULT_MAXCARDBOUNDDIST, 0.0, 1.0, NULL, NULL) );
12996  SCIP_CALL( SCIPaddIntParam(scip,
12997  "constraints/"CONSHDLR_NAME"/maxrounds",
12998  "maximal number of separation rounds per node (-1: unlimited)",
12999  &conshdlrdata->maxrounds, FALSE, DEFAULT_MAXROUNDS, -1, INT_MAX, NULL, NULL) );
13000  SCIP_CALL( SCIPaddIntParam(scip,
13001  "constraints/"CONSHDLR_NAME"/maxroundsroot",
13002  "maximal number of separation rounds per node in the root node (-1: unlimited)",
13003  &conshdlrdata->maxroundsroot, FALSE, DEFAULT_MAXROUNDSROOT, -1, INT_MAX, NULL, NULL) );
13004  SCIP_CALL( SCIPaddIntParam(scip,
13005  "constraints/"CONSHDLR_NAME"/maxsepacuts",
13006  "maximal number of cuts separated per separation round",
13007  &conshdlrdata->maxsepacuts, FALSE, DEFAULT_MAXSEPACUTS, 0, INT_MAX, NULL, NULL) );
13008  SCIP_CALL( SCIPaddIntParam(scip,
13009  "constraints/"CONSHDLR_NAME"/maxsepacutsroot",
13010  "maximal number of cuts separated per separation round in the root node",
13011  &conshdlrdata->maxsepacutsroot, FALSE, DEFAULT_MAXSEPACUTSROOT, 0, INT_MAX, NULL, NULL) );
13013  "constraints/"CONSHDLR_NAME"/disaggregation",
13014  "should disaggregation of knapsack constraints be allowed in preprocessing?",
13015  &conshdlrdata->disaggregation, TRUE, DEFAULT_DISAGGREGATION, NULL, NULL) );
13017  "constraints/"CONSHDLR_NAME"/simplifyinequalities",
13018  "should presolving try to simplify knapsacks",
13019  &conshdlrdata->simplifyinequalities, TRUE, DEFAULT_SIMPLIFYINEQUALITIES, NULL, NULL) );
13021  "constraints/"CONSHDLR_NAME"/negatedclique",
13022  "should negated clique information be used in solving process",
13023  &conshdlrdata->negatedclique, TRUE, DEFAULT_NEGATEDCLIQUE, NULL, NULL) );
13025  "constraints/"CONSHDLR_NAME"/presolpairwise",
13026  "should pairwise constraint comparison be performed in presolving?",
13027  &conshdlrdata->presolpairwise, TRUE, DEFAULT_PRESOLPAIRWISE, NULL, NULL) );
13029  "constraints/"CONSHDLR_NAME"/presolusehashing",
13030  "should hash table be used for detecting redundant constraints in advance",
13031  &conshdlrdata->presolusehashing, TRUE, DEFAULT_PRESOLUSEHASHING, NULL, NULL) );
13033  "constraints/"CONSHDLR_NAME"/dualpresolving",
13034  "should dual presolving steps be performed?",
13035  &conshdlrdata->dualpresolving, TRUE, DEFAULT_DUALPRESOLVING, NULL, NULL) );
13037  "constraints/"CONSHDLR_NAME"/usegubs",
13038  "should GUB information be used for separation?",
13039  &conshdlrdata->usegubs, TRUE, DEFAULT_USEGUBS, NULL, NULL) );
13041  "constraints/"CONSHDLR_NAME"/detectcutoffbound",
13042  "should presolving try to detect constraints parallel to the objective function defining an upper bound and prevent these constraints from entering the LP?",
13043  &conshdlrdata->detectcutoffbound, TRUE, DEFAULT_DETECTCUTOFFBOUND, NULL, NULL) );
13045  "constraints/"CONSHDLR_NAME"/detectlowerbound",
13046  "should presolving try to detect constraints parallel to the objective function defining a lower bound and prevent these constraints from entering the LP?",
13047  &conshdlrdata->detectlowerbound, TRUE, DEFAULT_DETECTLOWERBOUND, NULL, NULL) );
13048 
13049  return SCIP_OKAY;
13050 }
13051 
13052 /** creates and captures a knapsack constraint
13053  *
13054  * @note the constraint gets captured, hence at one point you have to release it using the method SCIPreleaseCons()
13055  */
13057  SCIP* scip, /**< SCIP data structure */
13058  SCIP_CONS** cons, /**< pointer to hold the created constraint */
13059  const char* name, /**< name of constraint */
13060  int nvars, /**< number of items in the knapsack */
13061  SCIP_VAR** vars, /**< array with item variables */
13062  SCIP_Longint* weights, /**< array with item weights */
13063  SCIP_Longint capacity, /**< capacity of knapsack */
13064  SCIP_Bool initial, /**< should the LP relaxation of constraint be in the initial LP?
13065  * Usually set to TRUE. Set to FALSE for 'lazy constraints'. */
13066  SCIP_Bool separate, /**< should the constraint be separated during LP processing?
13067  * Usually set to TRUE. */
13068  SCIP_Bool enforce, /**< should the constraint be enforced during node processing?
13069  * TRUE for model constraints, FALSE for additional, redundant constraints. */
13070  SCIP_Bool check, /**< should the constraint be checked for feasibility?
13071  * TRUE for model constraints, FALSE for additional, redundant constraints. */
13072  SCIP_Bool propagate, /**< should the constraint be propagated during node processing?
13073  * Usually set to TRUE. */
13074  SCIP_Bool local, /**< is constraint only valid locally?
13075  * Usually set to FALSE. Has to be set to TRUE, e.g., for branching constraints. */
13076  SCIP_Bool modifiable, /**< is constraint modifiable (subject to column generation)?
13077  * Usually set to FALSE. In column generation applications, set to TRUE if pricing
13078  * adds coefficients to this constraint. */
13079  SCIP_Bool dynamic, /**< is constraint subject to aging?
13080  * Usually set to FALSE. Set to TRUE for own cuts which
13081  * are separated as constraints. */
13082  SCIP_Bool removable, /**< should the relaxation be removed from the LP due to aging or cleanup?
13083  * Usually set to FALSE. Set to TRUE for 'lazy constraints' and 'user cuts'. */
13084  SCIP_Bool stickingatnode /**< should the constraint always be kept at the node where it was added, even
13085  * if it may be moved to a more global node?
13086  * Usually set to FALSE. Set to TRUE to for constraints that represent node data. */
13087  )
13088 {
13089  SCIP_CONSHDLRDATA* conshdlrdata;
13090  SCIP_CONSHDLR* conshdlr;
13091  SCIP_CONSDATA* consdata;
13092 
13093  /* find the knapsack constraint handler */
13094  conshdlr = SCIPfindConshdlr(scip, CONSHDLR_NAME);
13095  if( conshdlr == NULL )
13096  {
13097  SCIPerrorMessage("knapsack constraint handler not found\n");
13098  return SCIP_PLUGINNOTFOUND;
13099  }
13100 
13101  /* get event handler */
13102  conshdlrdata = SCIPconshdlrGetData(conshdlr);
13103  assert(conshdlrdata != NULL);
13104  assert(conshdlrdata->eventhdlr != NULL);
13105 
13106  /* create constraint data */
13107  SCIP_CALL( consdataCreate(scip, &consdata, conshdlrdata->eventhdlr, nvars, vars, weights, capacity) );
13108 
13109  /* create constraint */
13110  SCIP_CALL( SCIPcreateCons(scip, cons, name, conshdlr, consdata, initial, separate, enforce, check, propagate,
13111  local, modifiable, dynamic, removable, stickingatnode) );
13112 
13113  return SCIP_OKAY;
13114 }
13115 
13116 /** creates and captures a knapsack constraint
13117  * in its most basic version, i. e., all constraint flags are set to their basic value as explained for the
13118  * method SCIPcreateConsKnapsack(); all flags can be set via SCIPsetConsFLAGNAME-methods in scip.h
13119  *
13120  * @see SCIPcreateConsKnapsack() for information about the basic constraint flag configuration
13121  *
13122  * @note the constraint gets captured, hence at one point you have to release it using the method SCIPreleaseCons()
13123  */
13125  SCIP* scip, /**< SCIP data structure */
13126  SCIP_CONS** cons, /**< pointer to hold the created constraint */
13127  const char* name, /**< name of constraint */
13128  int nvars, /**< number of items in the knapsack */
13129  SCIP_VAR** vars, /**< array with item variables */
13130  SCIP_Longint* weights, /**< array with item weights */
13131  SCIP_Longint capacity /**< capacity of knapsack */
13132  )
13133 {
13134  assert(scip != NULL);
13135 
13136  SCIP_CALL( SCIPcreateConsKnapsack(scip, cons, name, nvars, vars, weights, capacity,
13137  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
13138 
13139  return SCIP_OKAY;
13140 }
13141 
13142 /** adds new item to knapsack constraint */
13144  SCIP* scip, /**< SCIP data structure */
13145  SCIP_CONS* cons, /**< constraint data */
13146  SCIP_VAR* var, /**< item variable */
13147  SCIP_Longint weight /**< item weight */
13148  )
13149 {
13150  assert(var != NULL);
13151 
13152  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13153  {
13154  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13155  return SCIP_INVALIDDATA;
13156  }
13157 
13158  SCIP_CALL( addCoef(scip, cons, var, weight) );
13159 
13160  return SCIP_OKAY;
13161 }
13162 
13163 /** gets the capacity of the knapsack constraint */
13165  SCIP* scip, /**< SCIP data structure */
13166  SCIP_CONS* cons /**< constraint data */
13167  )
13168 {
13169  SCIP_CONSDATA* consdata;
13170 
13171  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13172  {
13173  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13174  SCIPABORT();
13175  return 0; /*lint !e527*/
13176  }
13177 
13178  consdata = SCIPconsGetData(cons);
13179  assert(consdata != NULL);
13180 
13181  return consdata->capacity;
13182 }
13183 
13184 /** changes capacity of the knapsack constraint
13185  *
13186  * @note This method can only be called during problem creation stage (SCIP_STAGE_PROBLEM)
13187  */
13189  SCIP* scip, /**< SCIP data structure */
13190  SCIP_CONS* cons, /**< constraint data */
13191  SCIP_Longint capacity /**< new capacity of knapsack */
13192  )
13193 {
13194  SCIP_CONSDATA* consdata;
13195 
13196  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13197  {
13198  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13199  return SCIP_INVALIDDATA;
13200  }
13201 
13202  if( SCIPgetStage(scip) != SCIP_STAGE_PROBLEM )
13203  {
13204  SCIPerrorMessage("method can only be called during problem creation stage\n");
13205  return SCIP_INVALIDDATA;
13206  }
13207 
13208  consdata = SCIPconsGetData(cons);
13209  assert(consdata != NULL);
13210 
13211  consdata->capacity = capacity;
13212 
13213  return SCIP_OKAY;
13214 }
13215 
13216 /** gets the number of items in the knapsack constraint */
13218  SCIP* scip, /**< SCIP data structure */
13219  SCIP_CONS* cons /**< constraint data */
13220  )
13221 {
13222  SCIP_CONSDATA* consdata;
13223 
13224  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13225  {
13226  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13227  SCIPABORT();
13228  return -1; /*lint !e527*/
13229  }
13230 
13231  consdata = SCIPconsGetData(cons);
13232  assert(consdata != NULL);
13233 
13234  return consdata->nvars;
13235 }
13236 
13237 /** gets the array of variables in the knapsack constraint; the user must not modify this array! */
13239  SCIP* scip, /**< SCIP data structure */
13240  SCIP_CONS* cons /**< constraint data */
13241  )
13242 {
13243  SCIP_CONSDATA* consdata;
13244 
13245  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13246  {
13247  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13248  SCIPABORT();
13249  return NULL; /*lint !e527*/
13250  }
13251 
13252  consdata = SCIPconsGetData(cons);
13253  assert(consdata != NULL);
13254 
13255  return consdata->vars;
13256 }
13257 
13258 /** gets the array of weights in the knapsack constraint; the user must not modify this array! */
13260  SCIP* scip, /**< SCIP data structure */
13261  SCIP_CONS* cons /**< constraint data */
13262  )
13263 {
13264  SCIP_CONSDATA* consdata;
13265 
13266  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13267  {
13268  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13269  SCIPABORT();
13270  return NULL; /*lint !e527*/
13271  }
13272 
13273  consdata = SCIPconsGetData(cons);
13274  assert(consdata != NULL);
13275 
13276  return consdata->weights;
13277 }
13278 
13279 /** gets the dual solution of the knapsack constraint in the current LP */
13281  SCIP* scip, /**< SCIP data structure */
13282  SCIP_CONS* cons /**< constraint data */
13283  )
13284 {
13285  SCIP_CONSDATA* consdata;
13286 
13287  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13288  {
13289  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13290  SCIPABORT();
13291  return SCIP_INVALID; /*lint !e527*/
13292  }
13293 
13294  consdata = SCIPconsGetData(cons);
13295  assert(consdata != NULL);
13296 
13297  if( consdata->row != NULL )
13298  return SCIProwGetDualsol(consdata->row);
13299  else
13300  return 0.0;
13301 }
13302 
13303 /** gets the dual Farkas value of the knapsack constraint in the current infeasible LP */
13305  SCIP* scip, /**< SCIP data structure */
13306  SCIP_CONS* cons /**< constraint data */
13307  )
13308 {
13309  SCIP_CONSDATA* consdata;
13310 
13311  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13312  {
13313  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13314  SCIPABORT();
13315  return SCIP_INVALID; /*lint !e527*/
13316  }
13317 
13318  consdata = SCIPconsGetData(cons);
13319  assert(consdata != NULL);
13320 
13321  if( consdata->row != NULL )
13322  return SCIProwGetDualfarkas(consdata->row);
13323  else
13324  return 0.0;
13325 }
13326 
13327 /** returns the linear relaxation of the given knapsack constraint; may return NULL if no LP row was yet created;
13328  * the user must not modify the row!
13329  */
13331  SCIP* scip, /**< SCIP data structure */
13332  SCIP_CONS* cons /**< constraint data */
13333  )
13334 {
13335  SCIP_CONSDATA* consdata;
13336 
13337  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13338  {
13339  SCIPerrorMessage("constraint is not a knapsack\n");
13340  SCIPABORT();
13341  return NULL; /*lint !e527*/
13342  }
13343 
13344  consdata = SCIPconsGetData(cons);
13345  assert(consdata != NULL);
13346 
13347  return consdata->row;
13348 }
13349 
13350