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 */
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** eventdata; /**< event data 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 eventdata 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 */
214 enum GUBVarstatus
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 };
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 };
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;
259 /*
260  * Local methods
261  */
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
316 void sortItems(
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->eventdata != 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->eventdata,
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->eventdata[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;
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->eventdata != NULL);
486 
487  for( i = 0; i < consdata->nvars; i++)
488  {
489  SCIP_CALL( eventdataCreate(scip, &consdata->eventdata[i], consdata, consdata->weights[i]) );
490  SCIP_CALL( SCIPcatchVarEvent(scip, consdata->vars[i],
493  eventhdlr, consdata->eventdata[i], &consdata->eventdata[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;
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->eventdata != NULL);
513 
514  for( i = 0; i < consdata->nvars; i++)
515  {
516  SCIP_CALL( SCIPdropVarEvent(scip, consdata->vars[i],
519  eventhdlr, consdata->eventdata[i], consdata->eventdata[i]->filterpos) );
520  SCIP_CALL( eventdataFree(scip, &consdata->eventdata[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->eventdata, 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->eventdata == 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)->eventdata = 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)->eventdata, (*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)->eventdata != NULL )
705  {
706  SCIP_CALL( dropEvents(scip, *consdata, eventhdlr) );
707  SCIPfreeBlockMemoryArray(scip, &(*consdata)->eventdata, (*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
741 void consdataChgWeight(
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;
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->eventdata != NULL )
760  {
761  assert(consdata->eventdata[item] != NULL);
762  assert(consdata->eventdata[item]->weight == oldweight);
763  consdata->eventdata[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;
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) )) ) /*lint !e571*/
1243  {
1244  SCIPdebugMessage("Too much memory (%lu) would be consumed.\n", (unsigned long) (((size_t)nmyitems) * ((size_t)intcap) * sizeof(*optvalues))); /*lint !e571*/
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);
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
1875 void GUBsetSwapVars(
1876  SCIP* scip, /**< SCIP data structure */
1877  SCIP_GUBSET* gubset, /**< GUB set data structure */
1878  int var1, /**< first variable to be swapped */
1879  int var2 /**< second variable to be swapped */
1880  )
1881 {
1882  int gubcons;
1883  int var1idx;
1884  GUBVARSTATUS var1status;
1885  int var2idx;
1886  GUBVARSTATUS var2status;
1887 
1888  assert(scip != NULL);
1889  assert(gubset != NULL);
1890 
1891  gubcons = gubset->gubconssidx[var1];
1892  assert(gubcons == gubset->gubconssidx[var2]);
1893 
1894  /* nothing to be done if both variables are the same */
1895  if( var1 == var2 )
1896  return;
1897 
1898  /* swap index and status of variables in GUB constraint */
1899  var1idx = gubset->gubvarsidx[var1];
1900  var1status = gubset->gubconss[gubcons]->gubvarsstatus[var1idx];
1901  var2idx = gubset->gubvarsidx[var2];
1902  var2status = gubset->gubconss[gubcons]->gubvarsstatus[var2idx];
1903 
1904  gubset->gubvarsidx[var1] = var2idx;
1905  gubset->gubconss[gubcons]->gubvars[var1idx] = var2;
1906  gubset->gubconss[gubcons]->gubvarsstatus[var1idx] = var2status;
1907 
1908  gubset->gubvarsidx[var2] = var1idx;
1909  gubset->gubconss[gubcons]->gubvars[var2idx] = var1;
1910  gubset->gubconss[gubcons]->gubvarsstatus[var2idx] = var1status;
1911 }
1912 
1913 /** initializes partition of knapsack variables into nonoverlapping trivial GUB constraints (GUB with one variable) */
1914 static
1916  SCIP* scip, /**< SCIP data structure */
1917  SCIP_GUBSET** gubset, /**< pointer to store GUB set data structure */
1918  int nvars, /**< number of variables in the knapsack constraint */
1919  SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
1920  SCIP_Longint capacity /**< capacity of knapsack */
1921  )
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  )
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);
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->eventdata[consdata->nvars-1], consdata, weight) );
6243  SCIP_CALL( SCIPcatchVarEvent(scip, var,
6246  conshdlrdata->eventhdlr, consdata->eventdata[consdata->nvars-1],
6247  &consdata->eventdata[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->eventdata[pos], consdata->eventdata[pos]->filterpos) );
6309  SCIP_CALL( eventdataFree(scip, &consdata->eventdata[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->eventdata != NULL )
6323  consdata->eventdata[pos] = consdata->eventdata[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;
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->eventdata, 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  )
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 
7634  conshdlrdata = SCIPconshdlrGetData(SCIPconsGetHdlr(cons));
7635  assert(conshdlrdata != NULL);
7636 
7637  vars = consdata->vars;
7638  weights = consdata->weights;
7639  nvars = consdata->nvars;
7640  capacity = consdata->capacity;
7641 
7642  /* weight should still be sorted, because the reduction preserves this, but corresponding variables with equal
7643  * weight must not be sorted by their index
7644  */
7645 #ifndef NDEBUG
7646  for( w = nvars - 1; w > 0; --w )
7647  assert(weights[w] <= weights[w-1]);
7648 #endif
7649 
7650  /* if there are no variables rear to splitpos, the constraint has no redundant variables */
7651  if( consdata->nvars - 1 == splitpos )
7652  return SCIP_OKAY;
7653 
7654  assert(frontsum + weights[splitpos] > capacity);
7655 
7656  /* detect redundant variables */
7657  if( consdata->weightsum - weights[splitpos] <= capacity )
7658  {
7659  /* all rear items are redundant, because leaving one item in front and incl. of splitpos out the rear itmes always
7660  * fit
7661  */
7662  SCIPdebugMessage("Found redundant variables in constraint <%s>.\n", SCIPconsGetName(cons));
7663 
7664  /* delete items and update capacity */
7665  for( w = nvars - 1; w > splitpos; --w )
7666  {
7667  consdata->capacity -= weights[w];
7668  SCIP_CALL( delCoefPos(scip, cons, w) );
7669  }
7670  assert(w == splitpos);
7671 
7672  ++(*nchgsides);
7673  *nchgcoefs += (nvars - splitpos);
7674 
7675  /* division by greatest common divisor */
7676  gcd = weights[w];
7677  for( ; w >= 0 && gcd > 1; --w )
7678  {
7679  gcd = SCIPcalcGreComDiv(gcd, weights[w]);
7680  }
7681 
7682  /* normalize if possible */
7683  if( gcd > 1 )
7684  {
7685  for( w = splitpos; w >= 0; --w )
7686  {
7687  consdataChgWeight(consdata, w, weights[w]/gcd);
7688  }
7689  (*nchgcoefs) += nvars;
7690 
7691  consdata->capacity /= gcd;
7692  ++(*nchgsides);
7693  }
7694 
7695  /* weight should still be sorted, because the reduction preserves this, but corresponding variables with equal
7696  * weight must not be sorted by their index
7697  */
7698 #ifndef NDEBUG
7699  for( w = consdata->nvars - 1; w > 0; --w )
7700  assert(weights[w] <= weights[w - 1]);
7701 #endif
7702  }
7703  /* rear items can only be redundant, when the sum is smaller to the weight at splitpos and all rear items would
7704  * always fit into the knapsack, therefor the item directly after splitpos needs to be smaller than the one at
7705  * splitpos and needs to fit into the knapsack
7706  */
7707  else if( conshdlrdata->disaggregation && frontsum + weights[splitpos + 1] <= capacity )
7708  {
7709  int* clqpart;
7710  int nclq;
7711  int len;
7712 
7713  len = nvars - (splitpos + 1);
7714  /* allocate temporary memory */
7715  SCIP_CALL( SCIPallocBufferArray(scip, &clqpart, len) );
7716 
7717  /* calculate clique partition */
7718  SCIP_CALL( SCIPcalcCliquePartition(scip, &(consdata->vars[splitpos+1]), len, clqpart, &nclq) );
7719 
7720  /* check if we found at least one clique */
7721  if( nclq < len )
7722  {
7723  SCIP_Longint maxactduetoclq;
7724  int cliquenum;
7725 
7726  maxactduetoclq = 0;
7727  cliquenum = 0;
7728 
7729  /* calculate maximum activity due to cliques */
7730  for( w = 0; w < len; ++w )
7731  {
7732  assert(clqpart[w] >= 0 && clqpart[w] <= w);
7733  if( clqpart[w] == cliquenum )
7734  {
7735  maxactduetoclq += weights[w + splitpos + 1];
7736  ++cliquenum;
7737  }
7738  }
7739 
7740  /* all rear items are redundant due to clique information, if maxactduetoclq is smaller than the weight before,
7741  * so delete them and create for all clique the corresponding clique constraints and update the capacity
7742  */
7743  if( frontsum + maxactduetoclq <= capacity )
7744  {
7745  SCIP_VAR** clqvars;
7746  int nclqvars;
7747  int c;
7748 
7749  assert(maxactduetoclq < weights[splitpos]);
7750 
7751  SCIPdebugMessage("Found redundant variables in constraint <%s> due to clique information.\n", SCIPconsGetName(cons));
7752 
7753  /* allocate temporary memory */
7754  SCIP_CALL( SCIPallocBufferArray(scip, &clqvars, len - nclq + 1) );
7755 
7756  for( c = 0; c < nclq; ++c )
7757  {
7758  nclqvars = 0;
7759  for( w = 0; w < len; ++w )
7760  {
7761  if( clqpart[w] == c )
7762  {
7763  clqvars[nclqvars] = vars[w + splitpos + 1];
7764  ++nclqvars;
7765  }
7766  }
7767 
7768  /* we found a real clique so extract this constraint, because we do not know who this information generated so */
7769  if( nclqvars > 1 )
7770  {
7771  SCIP_CONS* cliquecons;
7772  char name[SCIP_MAXSTRLEN];
7773 
7774  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_clq_%"SCIP_LONGINT_FORMAT"_%d", SCIPconsGetName(cons), capacity, c);
7775  SCIP_CALL( SCIPcreateConsSetpack(scip, &cliquecons, name, nclqvars, clqvars,
7779  SCIPconsIsStickingAtNode(cons)) );
7780  SCIPdebugMessage(" -> adding clique constraint: ");
7781  SCIPdebugPrintCons(scip, cliquecons, NULL);
7782  SCIP_CALL( SCIPaddCons(scip, cliquecons) );
7783  SCIP_CALL( SCIPreleaseCons(scip, &cliquecons) );
7784  ++(*naddconss);
7785  }
7786  }
7787 
7788  /* delete items and update capacity */
7789  for( w = nvars - 1; w > splitpos; --w )
7790  {
7791  SCIP_CALL( delCoefPos(scip, cons, w) );
7792  ++(*nchgcoefs);
7793  }
7794  consdata->capacity -= maxactduetoclq;
7795  assert(frontsum <= consdata->capacity);
7796  ++(*nchgsides);
7797 
7798  assert(w == splitpos);
7799 
7800  /* renew weights pointer */
7801  weights = consdata->weights;
7802 
7803  /* division by greatest common divisor */
7804  gcd = weights[w];
7805  for( ; w >= 0 && gcd > 1; --w )
7806  {
7807  gcd = SCIPcalcGreComDiv(gcd, weights[w]);
7808  }
7809 
7810  /* normalize if possible */
7811  if( gcd > 1 )
7812  {
7813  for( w = splitpos; w >= 0; --w )
7814  {
7815  consdataChgWeight(consdata, w, weights[w]/gcd);
7816  }
7817  (*nchgcoefs) += nvars;
7818 
7819  consdata->capacity /= gcd;
7820  ++(*nchgsides);
7821  }
7822 
7823  /* free temporary memory */
7824  SCIPfreeBufferArray(scip, &clqvars);
7825 
7826  /* weight should still be sorted, because the reduction preserves this, but corresponding variables with equal
7827  * weight must not be sorted by their index
7828  */
7829 #ifndef NDEBUG
7830  for( w = consdata->nvars - 1; w > 0; --w )
7831  assert(weights[w] <= weights[w - 1]);
7832 #endif
7833  }
7834  }
7835 
7836  /* free temporary memory */
7837  SCIPfreeBufferArray(scip, &clqpart);
7838  }
7839 
7840  return SCIP_OKAY;
7841 }
7842 
7843 /* detect redundant variables which always fits into the knapsack
7844  *
7845  * i.e. 5x1 + 5x2 + 5x3 + 2x4 + 1x5 <= 13 => x4, x5 always fits into the knapsack, so we can delete them
7846  *
7847  * i.e. 5x1 + 5x2 + 5x3 + 2x4 + 1x5 <= 8 and we have the cliqueinformation (x1,x2,x3) is a clique
7848  * => x4, x5 always fits into the knapsack, so we can delete them
7849  *
7850  * i.e. 5x1 + 5x2 + 5x3 + 1x4 + 1x5 <= 6 and we have the cliqueinformation (x1,x2,x3) is a clique and (x4,x5) too
7851  * => we create the set partitioning constraint x4 + x5 <= 1 and delete them in this knapsack
7852  */
7853 static
7855  SCIP* scip, /**< SCIP data structure */
7856  SCIP_CONS* cons, /**< knapsack constraint */
7857  int* ndelconss, /**< pointer to store the amount of deleted constraints */
7858  int* nchgcoefs, /**< pointer to store the amount of changed coefficients */
7859  int* nchgsides, /**< pointer to store the amount of changed sides */
7860  int* naddconss /**< pointer to count number of added constraints */
7861  )
7862 {
7863  SCIP_CONSDATA* consdata;
7864  SCIP_VAR** vars;
7865  SCIP_Longint* weights;
7866  SCIP_Longint capacity;
7867  SCIP_Longint sum;
7868  int noldchgcoefs;
7869  int nvars;
7870  int v;
7871  int w;
7872 
7873  assert(scip != NULL);
7874  assert(cons != NULL);
7875  assert(ndelconss != NULL);
7876  assert(nchgcoefs != NULL);
7877  assert(nchgsides != NULL);
7878  assert(naddconss != NULL);
7879 
7880  consdata = SCIPconsGetData(cons);
7881  assert(consdata != NULL);
7882  assert(consdata->nvars >= 2);
7883  assert(consdata->weightsum > consdata->capacity);
7884 
7885  noldchgcoefs = *nchgcoefs;
7886  vars = consdata->vars;
7887  weights = consdata->weights;
7888  nvars = consdata->nvars;
7889  capacity = consdata->capacity;
7890  sum = 0;
7891 
7892  /* search for maximal fitting items */
7893  for( v = 0; v < nvars && sum + weights[v] <= capacity; ++v )
7894  sum += weights[v];
7895 
7896  assert(v < nvars);
7897 
7898  /* all but one variable fit into the knapsack, so we can upgrade this constraint to a logicor */
7899  if( v == nvars - 1 )
7900  {
7901  SCIP_CALL( upgradeCons(scip, cons, ndelconss, naddconss) );
7902  assert(SCIPconsIsDeleted(cons));
7903 
7904  return SCIP_OKAY;
7905  }
7906 
7907  if( v < nvars - 1 )
7908  {
7909  /* try to delete variables */
7910  SCIP_CALL( deleteRedundantVars(scip, cons, sum, v, nchgcoefs, nchgsides, naddconss) );
7911  assert(consdata->nvars > 1);
7912 
7913  /* all but one variable fit into the knapsack, so we can upgrade this constraint to a logicor */
7914  if( v == consdata->nvars - 1 )
7915  {
7916  SCIP_CALL( upgradeCons(scip, cons, ndelconss, naddconss) );
7917  assert(SCIPconsIsDeleted(cons));
7918  }
7919 
7920  return SCIP_OKAY;
7921  }
7922 
7923  /* if we already found some redundant variables, stop here */
7924  if( *nchgcoefs > noldchgcoefs )
7925  return SCIP_OKAY;
7926 
7927  assert(vars == consdata->vars);
7928  assert(weights == consdata->weights);
7929  assert(nvars == consdata->nvars);
7930  assert(capacity == consdata->capacity);
7931 
7932  /* calculate clique partition */
7933  SCIP_CALL( calcCliquepartition(scip, consdata, TRUE, FALSE) );
7934 
7935  /* check for real existing cliques */
7936  if( consdata->cliquepartition[v] < v )
7937  {
7938  SCIP_CONSHDLRDATA* conshdlrdata;
7939  SCIP_Longint sumfront;
7940  SCIP_Longint maxactduetoclqfront;
7941  int* clqpart;
7942  int cliquenum;
7943 
7944  conshdlrdata = SCIPconshdlrGetData(SCIPconsGetHdlr(cons));
7945  assert(conshdlrdata != NULL);
7946 
7947  sumfront = 0;
7948  maxactduetoclqfront = 0;
7949 
7950  clqpart = consdata->cliquepartition;
7951  cliquenum = 0;
7952 
7953  /* calculate maximal activity due to cliques */
7954  for( w = 0; w < nvars; ++w )
7955  {
7956  assert(clqpart[w] >= 0 && clqpart[w] <= w);
7957  if( clqpart[w] == cliquenum )
7958  {
7959  if( maxactduetoclqfront + weights[w] <= capacity )
7960  {
7961  maxactduetoclqfront += weights[w];
7962  ++cliquenum;
7963  }
7964  else
7965  break;
7966  }
7967  sumfront += weights[w];
7968  }
7969  assert(w >= v);
7970 
7971  /* if all items fit, then delete the whole constraint but create clique constraints which led to this
7972  * information
7973  */
7974  if( conshdlrdata->disaggregation && w == nvars )
7975  {
7976  SCIP_VAR** clqvars;
7977  int nclqvars;
7978  int c;
7979  int ncliques;
7980 
7981  assert(maxactduetoclqfront <= capacity);
7982 
7983  SCIPdebugMessage("Found redundant constraint <%s> due to clique information.\n", SCIPconsGetName(cons));
7984 
7985  ncliques = consdata->ncliques;
7986 
7987  /* allocate temporary memory */
7988  SCIP_CALL( SCIPallocBufferArray(scip, &clqvars, nvars - ncliques + 1) );
7989 
7990  for( c = 0; c < ncliques; ++c )
7991  {
7992  nclqvars = 0;
7993  for( w = 0; w < nvars; ++w )
7994  {
7995  if( clqpart[w] == c )
7996  {
7997  clqvars[nclqvars] = vars[w];
7998  ++nclqvars;
7999  }
8000  }
8001 
8002  /* we found a real clique so extract this constraint, because we do not know who this information generated so */
8003  if( nclqvars > 1 )
8004  {
8005  SCIP_CONS* cliquecons;
8006  char name[SCIP_MAXSTRLEN];
8007 
8008  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_clq_%"SCIP_LONGINT_FORMAT"_%d", SCIPconsGetName(cons), capacity, c);
8009  SCIP_CALL( SCIPcreateConsSetpack(scip, &cliquecons, name, nclqvars, clqvars,
8013  SCIPconsIsStickingAtNode(cons)) );
8014  SCIPdebugMessage(" -> adding clique constraint: ");
8015  SCIPdebugPrintCons(scip, cliquecons, NULL);
8016  SCIP_CALL( SCIPaddCons(scip, cliquecons) );
8017  SCIP_CALL( SCIPreleaseCons(scip, &cliquecons) );
8018  ++(*naddconss);
8019  }
8020  }
8021 
8022  /* delete old constraint */
8023  SCIP_CALL( SCIPdelConsLocal(scip, cons) );
8024  ++(*ndelconss);
8025 
8026  SCIPfreeBufferArray(scip, &clqvars);
8027 
8028  return SCIP_OKAY;
8029  }
8030 
8031  if( w > v && w < nvars - 1 )
8032  {
8033  /* try to delete variables */
8034  SCIP_CALL( deleteRedundantVars(scip, cons, sumfront, w, nchgcoefs, nchgsides, naddconss) );
8035  }
8036  }
8037 
8038  return SCIP_OKAY;
8039 }
8040 
8041 /** divides weights by their greatest common divisor and divides capacity by the same value, rounding down the result */
8042 static
8043 void normalizeWeights(
8044  SCIP_CONS* cons, /**< knapsack constraint */
8045  int* nchgcoefs, /**< pointer to count total number of changed coefficients */
8046  int* nchgsides /**< pointer to count number of side changes */
8047  )
8048 {
8049  SCIP_CONSDATA* consdata;
8051  int i;
8052 
8053  assert(nchgcoefs != NULL);
8054  assert(nchgsides != NULL);
8055  assert(!SCIPconsIsModifiable(cons));
8056 
8057  consdata = SCIPconsGetData(cons);
8058  assert(consdata != NULL);
8059  assert(consdata->row == NULL); /* we are in presolve, so no LP row exists */
8060  assert(consdata->onesweightsum == 0); /* all fixed variables should have been removed */
8061  assert(consdata->weightsum > consdata->capacity); /* otherwise, the constraint is redundant */
8062  assert(consdata->nvars >= 1);
8063 
8064  /* sort items, because we can stop earlier if the smaller weights are evaluated first */
8065  sortItems(consdata);
8066 
8067  gcd = consdata->weights[consdata->nvars-1];
8068  for( i = consdata->nvars-2; i >= 0 && gcd >= 2; --i )
8069  {
8070  assert(SCIPvarGetLbLocal(consdata->vars[i]) < 0.5);
8071  assert(SCIPvarGetUbLocal(consdata->vars[i]) > 0.5); /* all fixed variables should have been removed */
8072 
8073  gcd = SCIPcalcGreComDiv(gcd, consdata->weights[i]);
8074  }
8075 
8076  if( gcd >= 2 )
8077  {
8078  SCIPdebugMessage("knapsack constraint <%s>: dividing weights by %"SCIP_LONGINT_FORMAT"\n", SCIPconsGetName(cons), gcd);
8079 
8080  for( i = 0; i < consdata->nvars; ++i )
8081  consdataChgWeight(consdata, i, consdata->weights[i]/gcd);
8082  consdata->capacity /= gcd;
8083  (*nchgcoefs) += consdata->nvars;
8084  (*nchgsides)++;
8085 
8086  /* weight should still be sorted, because the reduction preserves this */
8087 #ifndef NDEBUG
8088  for( i = consdata->nvars - 1; i > 0; --i )
8089  assert(consdata->weights[i] <= consdata->weights[i - 1]);
8090 #endif
8091  consdata->sorted = TRUE;
8092  }
8093 }
8094 
8095 /** dual weights tightening for knapsack constraints
8096  *
8097  * 1. a) check if all two pairs exceed the capacity, then we can upgrade this constraint to a set-packing constraint
8098  * b) check if all but the smallest weight fit into the knapsack, then we can upgrade this constraint to a logicor
8099  * constraint
8100  *
8101  * 2. check if besides big coefficients, that fit only by itself, for a certain amount of variables all combination of
8102  * these are a minimal cover, then might reduce the weights and the capacity, e.g.
8103  *
8104  * +219y1 + 180y2 + 74x1 + 70x2 + 63x3 + 62x4 + 53x5 <= 219 <=> 3y1 + 3y2 + x1 + x2 + x3 + x4 + x5 <= 3
8105  *
8106  * 3. use the duality between a^Tx <= capacity <=> a^T~x >= weightsum - capacity to tighten weights, e.g.
8107  *
8108  * 11x1 + 10x2 + 7x3 + 7x4 + 5x5 <= 27 <=> 10~x1 + 10~x2 + 7~x3 + 7~x4 + 5~x5 >= 13
8109  *
8110  * the above constraint can be changed to 8~x1 + 8~x2 + 6.5~x3 + 6.5~x4 + 5~x5 >= 13
8111  *
8112  * 16~x1 + 16~x2 + 13~x3 + 13~x4 + 10~x5 >= 26 <=> 16x1 + 16x2 + 13x3 + 13x4 + 10x5 <= 42
8113  */
8114 static
8116  SCIP* scip, /**< SCIP data structure */
8117  SCIP_CONS* cons, /**< knapsack constraint */
8118  int* ndelconss, /**< pointer to store the amount of deleted constraints */
8119  int* nchgcoefs, /**< pointer to store the amount of changed coefficients */
8120  int* nchgsides, /**< pointer to store the amount of changed sides */
8121  int* naddconss /**< pointer to count number of added constraints */
8122  )
8123 {
8124  SCIP_CONSDATA* consdata;
8125  SCIP_Longint* weights;
8126  SCIP_Longint dualcapacity;
8127  SCIP_Longint reductionsum;
8128  SCIP_Longint capacity;
8129  SCIP_Longint exceedsum;
8130  int oldnchgcoefs;
8131  int nvars;
8132  int vbig;
8133  int v;
8134  int w;
8135 #ifndef NDEBUG
8136  int oldnchgsides = *nchgsides;
8137 #endif
8138 
8139  assert(scip != NULL);
8140  assert(cons != NULL);
8141  assert(ndelconss != NULL);
8142  assert(nchgcoefs != NULL);
8143  assert(nchgsides != NULL);
8144  assert(naddconss != NULL);
8145 
8146  consdata = SCIPconsGetData(cons);
8147  assert(consdata != NULL);
8148  assert(consdata->weightsum > consdata->capacity);
8149  assert(consdata->nvars >= 2);
8150  assert(consdata->sorted);
8151 
8152  /* constraint should be merged */
8153  assert(consdata->merged);
8154 
8155  nvars = consdata->nvars;
8156  weights = consdata->weights;
8157  capacity = consdata->capacity;
8158 
8159  oldnchgcoefs = *nchgcoefs;
8160 
8161  /* case 1. */
8162  if( weights[nvars - 1] + weights[nvars - 2] > capacity )
8163  {
8164  SCIP_CONS* newcons;
8165 
8166  /* two variable are enough to exceed the constraint, so we can update it to a set-packing
8167  *
8168  * e.g. 5x1 + 4x2 + 3x3 <= 5 <=> x1 + x2 + x3 <= 1
8169  */
8170  SCIPdebugMessage("upgrading knapsack constraint <%s> to a set-packing constraint", SCIPconsGetName(cons));
8171 
8172  SCIP_CALL( SCIPcreateConsSetpack(scip, &newcons, SCIPconsGetName(cons), consdata->nvars, consdata->vars,
8176  SCIPconsIsStickingAtNode(cons)) );
8177 
8178  SCIP_CALL( SCIPaddCons(scip, newcons) );
8179  SCIP_CALL( SCIPreleaseCons(scip, &newcons) );
8180  ++(*naddconss);
8181 
8182  SCIP_CALL( SCIPdelCons(scip, cons) );
8183  ++(*ndelconss);
8184 
8185  return SCIP_OKAY;
8186  }
8187 
8188  /* all but one variable fit into the knapsack, so we can upgrade this constraint to a logicor */
8189  if( consdata->weightsum - weights[nvars - 1] <= consdata->capacity )
8190  {
8191  SCIP_CALL( upgradeCons(scip, cons, ndelconss, naddconss) );
8192  assert(SCIPconsIsDeleted(cons));
8193 
8194  return SCIP_OKAY;
8195  }
8196 
8197  /* early termination, if the pair with biggest coeffcients together does not exceed the dualcapacity */
8198  /* @todo might be changed/removed when improving the coeffcients tightening */
8199  if( consdata->weightsum - capacity > weights[0] + weights[1] )
8200  return SCIP_OKAY;
8201 
8202  /* case 2. */
8203 
8204  v = 0;
8205 
8206  /* @todo generalize the following algorithm for several parts of the knapsack
8207  *
8208  * the following is done without looking at the dualcapacity; it is enough to check whether for a certain amount of
8209  * variables each combination is a minimal cover, some examples
8210  *
8211  * +74x1 + 70x2 + 63x3 + 62x4 + 53x5 <= 219 <=> 74~x1 + 70~x2 + 63~x3 + 62~x4 + 53~x5 >= 103
8212  * <=> ~x1 + ~x2 + ~x3 + ~x4 + ~x5 >= 2
8213  * <=> x1 + x2 + x3 + x4 + x5 <= 3
8214  *
8215  * +219y1 + 180y_2 +74x1 + 70x2 + 63x3 + 62x4 + 53x5 <= 219 <=> 3y1 + 3y2 + x1 + x2 + x3 + x4 + x5 <= 3
8216  *
8217  */
8218 
8219  /* determine big weights that fit only by itself */
8220  while( v < nvars && weights[v] + weights[nvars - 1] > capacity )
8221  ++v;
8222 
8223  vbig = v;
8224  assert(vbig < nvars - 1);
8225  exceedsum = 0;
8226 
8227  /* determine the amount needed to exceed the capacity */
8228  while( v < nvars && exceedsum <= capacity )
8229  {
8230  exceedsum += weights[v];
8231  ++v;
8232  }
8233 
8234  /* if we exceeded the capacity we might reduce the weights */
8235  if( exceedsum > capacity )
8236  {
8237  assert(vbig > 0 || v < nvars);
8238 
8239  /* all small weights were needed to exceed the capacity */
8240  if( v == nvars )
8241  {
8242  SCIP_Longint newweight = (SCIP_Longint)nvars - vbig - 1;
8243  assert(newweight > 0);
8244 
8245  /* reduce big weights */
8246  for( v = 0; v < vbig; ++v )
8247  {
8248  if( weights[v] > newweight )
8249  {
8250  consdataChgWeight(consdata, v, newweight);
8251  ++(*nchgcoefs);
8252  }
8253  }
8254 
8255  /* reduce small weights */
8256  for( ; v < nvars; ++v )
8257  {
8258  if( weights[v] > 1 )
8259  {
8260  consdataChgWeight(consdata, v, 1LL);
8261  ++(*nchgcoefs);
8262  }
8263  }
8264 
8265  consdata->capacity = newweight;
8266 
8267  /* weight should still be sorted, because the reduction preserves this, but corresponding variables with equal
8268  * weight must not be sorted by their index
8269  */
8270 #ifndef NDEBUG
8271  for( v = nvars - 1; v > 0; --v )
8272  assert(weights[v] <= weights[v-1]);
8273 #endif
8274 
8275  return SCIP_OKAY;
8276  }
8277  /* a certain amount of small variables exceed the capacity, so check if this holds for all combinations of the
8278  * small weights
8279  */
8280  else
8281  {
8282  SCIP_Longint exceedsumback = 0;
8283  int nexceed = v - vbig;
8284 
8285  assert(nexceed > 1);
8286 
8287  /* determine weightsum of the same amount as before but of the smallest weight */
8288  for( w = nvars - 1; w >= nvars - nexceed; --w )
8289  exceedsumback += weights[w];
8290 
8291  assert(w >= 0);
8292 
8293  /* if the same amount but with the smallest possible weights also exceed the capacity, it holds for all
8294  * combinations of all small weights
8295  */
8296  if( exceedsumback > capacity )
8297  {
8298  SCIP_Longint newweight = nexceed - 1;
8299 
8300  /* taking out the smallest element needs to fit */
8301  assert(exceedsumback - weights[nvars - 1] <= capacity);
8302 
8303  /* reduce big weights */
8304  for( v = 0; v < vbig; ++v )
8305  {
8306  if( weights[v] > newweight )
8307  {
8308  consdataChgWeight(consdata, v, newweight);
8309  ++(*nchgcoefs);
8310  }
8311  }
8312 
8313  /* reduce small weights */
8314  for( ; v < nvars; ++v )
8315  {
8316  if( weights[v] > 1 )
8317  {
8318  consdataChgWeight(consdata, v, 1LL);
8319  ++(*nchgcoefs);
8320  }
8321  }
8322 
8323  consdata->capacity = newweight;
8324 
8325  /* weight should still be sorted, because the reduction preserves this, but corresponding variables with equal
8326  * weight must not be sorted by their index
8327  */
8328 #ifndef NDEBUG
8329  for( v = nvars - 1; v > 0; --v )
8330  assert(weights[v] <= weights[v-1]);
8331 #endif
8332  return SCIP_OKAY;
8333  }
8334  }
8335  }
8336  else
8337  {
8338  /* if the follwoing assert fails we have either a redundant constraint or a set-packing constraint, this should
8339  * not happen here
8340  */
8341  assert(vbig > 0 && vbig < nvars);
8342 
8343  /* either choose a big coefficients or all other variables
8344  *
8345  * 973x1 + 189x2 + 189x3 + 145x4 + 110x5 + 104x6 + 93x7 + 71x8 + 68x9 + 10x10 <= 979
8346  *
8347  * either choose x1, or all other variables (weightsum of x2 to x10 is 979 above), so we can tighten this
8348  * constraint to
8349  *
8350  * 9x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 <= 9
8351  */
8352 
8353  if( weights[vbig - 1] > (SCIP_Longint)nvars - vbig || weights[vbig] > 1 )
8354  {
8355  SCIP_Longint newweight = (SCIP_Longint)nvars - vbig;
8356 #ifndef NDEBUG
8357  SCIP_Longint resweightsum = consdata->weightsum;
8358 
8359  for( v = 0; v < vbig; ++v )
8360  resweightsum -= weights[v];
8361 
8362  assert(exceedsum == resweightsum);
8363 #endif
8364  assert(newweight > 0);
8365 
8366  /* reduce big weights */
8367  for( v = 0; v < vbig; ++v )
8368  {
8369  if( weights[v] > newweight )
8370  {
8371  consdataChgWeight(consdata, v, newweight);
8372  ++(*nchgcoefs);
8373  }
8374  }
8375 
8376  /* reduce small weights */
8377  for( ; v < nvars; ++v )
8378  {
8379  if( weights[v] > 1 )
8380  {
8381  consdataChgWeight(consdata, v, 1LL);
8382  ++(*nchgcoefs);
8383  }
8384  }
8385 
8386  consdata->capacity = newweight;
8387 
8388  /* weight should still be sorted, because the reduction preserves this, but corresponding variables with equal
8389  * weight must not be sorted by their index
8390  */
8391 #ifndef NDEBUG
8392  for( v = nvars - 1; v > 0; --v )
8393  assert(weights[v] <= weights[v-1]);
8394 #endif
8395  return SCIP_OKAY;
8396  }
8397  }
8398 
8399  /* case 3. */
8400 
8401  dualcapacity = consdata->weightsum - capacity;
8402  reductionsum = 0;
8403  v = 0;
8404 
8405  /* reduce big weights
8406  *
8407  * e.g. 11x0 + 11x1 + 10x2 + 10x3 <= 32 <=> 11~x0 + 11~x1 + 10~x2 + 10~x3 >= 10
8408  * <=> 10~x0 + 10~x1 + 10~x2 + 10~x3 >= 10
8409  * <=> x0 + x1 + x2 + x3 <= 3
8410  */
8411  while( weights[v] > dualcapacity )
8412  {
8413  reductionsum += (weights[v] - dualcapacity);
8414  consdataChgWeight(consdata, v, dualcapacity);
8415  ++v;
8416  assert(v < nvars);
8417  }
8418  (*nchgcoefs) += v;
8419 
8420  /* skip weights equal to the dualcapacity, because we cannot change them */
8421  while( v < nvars && weights[v] == dualcapacity )
8422  ++v;
8423 
8424  /* one negated variable is enough to fulfill the constraint, so we can update it to a logicor
8425  *
8426  * e.g. 10x1 + 10x2 + 10x3 <= 20 <=> 10~x1 + 10~x2 + 10~x3 >= 10 <=> ~x1 + ~x2 + ~x3 >= 1
8427  */
8428  if( v == nvars )
8429  {
8430  SCIP_CALL( upgradeCons(scip, cons, ndelconss, naddconss) );
8431  assert(SCIPconsIsDeleted(cons));
8432 
8433  return SCIP_OKAY;
8434  }
8435  else if( v < nvars - 1 )
8436  {
8437  /* @todo generalize the following algorithm for more than two variables */
8438 
8439  if( weights[nvars - 1] + weights[nvars - 2] >= dualcapacity )
8440  {
8441  /* we have a dual-knapsack constraint were we either need to choose one variable out of a subset (big
8442  * coefficients) of all or two variables of the rest
8443  *
8444  * e.g. 9x1 + 9x2 + 6x3 + 4x4 <= 19 <=> 9~x1 + 9~x2 + 6~x3 + 4~x4 >= 9
8445  * <=> 2~x1 + 2~x2 + ~x3 + ~x4 >= 2
8446  * <=> 2x1 + 2x2 + x3 + x4 <= 4
8447  *
8448  * 3x1 + 3x2 + 2x3 + 2x4 + 2x5 + 2x6 + x7 <= 12 <=> 3~x1 + 3~x2 + 2~x3 + 2~x4 + 2~x5 + 2~x6 + ~x7 >= 3
8449  * <=> 2~x1 + 2~x2 + ~x3 + ~x4 + ~x5 + ~x6 + ~x7 >= 2
8450  * <=> 2 x1 + 2 x2 + x3 + x4 + x5 + x6 + x7 <= 7
8451  *
8452  */
8453  if( v > 0 && weights[nvars - 2] > 1 )
8454  {
8455  int ncoefchg = 0;
8456 
8457  /* reduce all bigger weights */
8458  for( w = 0; w < v; ++w )
8459  {
8460  if( weights[w] > 2 )
8461  {
8462  consdataChgWeight(consdata, w, 2LL);
8463  ++ncoefchg;
8464  }
8465  else
8466  {
8467  assert(weights[0] == 2);
8468  assert(weights[v - 1] == 2);
8469  break;
8470  }
8471  }
8472 
8473  /* reduce all smaller weights */
8474  for( w = v; w < nvars; ++w )
8475  {
8476  if( weights[w] > 1 )
8477  {
8478  consdataChgWeight(consdata, w, 1LL);
8479  ++ncoefchg;
8480  }
8481  }
8482  assert(ncoefchg > 0);
8483 
8484  (*nchgcoefs) += ncoefchg;
8485 
8486  /* correct the capacity */
8487  consdata->capacity = (-2 + v * 2 + nvars - v); /*lint !e647*/
8488  assert(consdata->capacity > 0);
8489  assert(weights[0] <= consdata->capacity);
8490  assert(consdata->weightsum > consdata->capacity);
8491  /* reset the reductionsum */
8492  reductionsum = 0;
8493  }
8494  else if( v == 0 )
8495  {
8496  assert(weights[nvars - 2] == 1);
8497  }
8498  }
8499  else
8500  {
8501  SCIP_Longint minweight = weights[nvars - 1];
8502  SCIP_Longint newweight = dualcapacity - minweight;
8503  SCIP_Longint restsumweights = 0;
8504  SCIP_Longint sumcoef;
8505  SCIP_Bool sumcoefcase = FALSE;
8506  int startv = v;
8507  int end;
8508  int k;
8509 
8510  assert(weights[nvars - 1] + weights[nvars - 2] <= capacity);
8511 
8512  /* reduce big weights of pairs that exceed the dualcapacity
8513  *
8514  * e.g. 9x1 + 9x2 + 6x3 + 4x4 + 4x5 + 4x6 <= 27 <=> 9~x1 + 9~x2 + 6~x3 + 4~x4 + 4~x5 + 4~x6 >= 9
8515  * <=> 9~x1 + 9~x2 + 5~x3 + 4~x4 + 4~x5 + 4~x6 >= 9
8516  * <=> 9x1 + 9x2 + 5x3 + 4x4 + 4x5 + 4x6 <= 27
8517  */
8518  while( weights[v] > newweight )
8519  {
8520  reductionsum += (weights[v] - newweight);
8521  consdataChgWeight(consdata, v, newweight);
8522  ++v;
8523  assert(v < nvars);
8524  }
8525  (*nchgcoefs) += (v - startv);
8526 
8527  /* skip equal weights */
8528  while( weights[v] == newweight )
8529  ++v;
8530 
8531  if( v > 0 )
8532  {
8533  for( w = v; w < nvars; ++w )
8534  restsumweights += weights[w];
8535  }
8536  else
8537  restsumweights = consdata->weightsum;
8538 
8539  if( restsumweights < dualcapacity )
8540  {
8541  /* we found redundant variables, which does not influence the feasibility of any integral solution, e.g.
8542  *
8543  * +61x1 + 61x2 + 61x3 + 61x4 + 61x5 + 61x6 + 35x7 + 10x8 <= 350 <=>
8544  * +61~x1 + 61~x2 + 61~x3 + 61~x4 + 61~x5 + 61~x6 + 35~x7 + 10~x8 >= 61
8545  */
8546  if( startv == v )
8547  {
8548  /* remove redundant variables */
8549  for( w = nvars - 1; w >= v; --w )
8550  {
8551  SCIP_CALL( delCoefPos(scip, cons, v) );
8552  ++(*nchgcoefs);
8553  }
8554 
8555 #ifndef NDEBUG
8556  /* each coefficients should exceed the dualcapacity by itself */
8557  for( ; w >= 0; --w )
8558  assert(weights[w] == dualcapacity);
8559 #endif
8560  /* for performance reasons we do not update the capacity(, i.e. reduce it by reductionsum) and directly
8561  * upgrade this constraint
8562  */
8563  SCIP_CALL( upgradeCons(scip, cons, ndelconss, naddconss) );
8564  assert(SCIPconsIsDeleted(cons));
8565 
8566  return SCIP_OKAY;
8567  }
8568 
8569  /* special case where we have three different coefficient types
8570  *
8571  * e.g. 9x1 + 9x2 + 6x3 + 6x4 + 4x5 + 4x6 <= 29 <=> 9~x1 + 9~x2 + 6~x3 + 6~x4 + 4~x5 + 4~x6 >= 9
8572  * <=> 9~x1 + 9~x2 + 5~x3 + 5~x4 + 4~x5 + 4~x6 >= 9
8573  * <=> 3~x1 + 3~x2 + 2~x3 + 2~x4 + ~x5 + ~x6 >= 3
8574  * <=> 3x1 + 3x2 + 2x3 + 2x4 + x5 + x6 <= 9
8575  */
8576  if( weights[v] > 1 || (weights[startv] > (SCIP_Longint)nvars - v) || (startv > 0 && weights[0] == (SCIP_Longint)nvars - v + 1) )
8577  {
8578  SCIP_Longint newcap;
8579 
8580  /* adjust smallest coefficients, which all together do not exceed the dualcapacity */
8581  for( w = nvars - 1; w >= v; --w )
8582  {
8583  if( weights[w] > 1 )
8584  {
8585  consdataChgWeight(consdata, w, 1LL);
8586  ++(*nchgcoefs);
8587  }
8588  }
8589 
8590  /* adjust middle sized coefficients, which when choosing also one small coefficients exceed the
8591  * dualcapacity
8592  */
8593  newweight = (SCIP_Longint)nvars - v;
8594  assert(newweight > 1);
8595  for( ; w >= startv; --w )
8596  {
8597  if( weights[w] > newweight )
8598  {
8599  consdataChgWeight(consdata, w, newweight);
8600  ++(*nchgcoefs);
8601  }
8602  else
8603  assert(weights[w] == newweight);
8604  }
8605 
8606  /* adjust big sized coefficients, where each of them exceeds the dualcapacity by itself */
8607  ++newweight;
8608  assert(newweight > 2);
8609  for( ; w >= 0; --w )
8610  {
8611  if( weights[w] > newweight )
8612  {
8613  consdataChgWeight(consdata, w, newweight);
8614  ++(*nchgcoefs);
8615  }
8616  else
8617  assert(weights[w] == newweight);
8618  }
8619 
8620  /* update the capacity */
8621  newcap = ((SCIP_Longint)startv - 1) * newweight + ((SCIP_Longint)v - startv) * (newweight - 1) + ((SCIP_Longint)nvars - v);
8622  if( consdata->capacity > newcap )
8623  {
8624  consdata->capacity = newcap;
8625  ++(*nchgsides);
8626  }
8627  else
8628  assert(consdata->capacity == newcap);
8629  }
8630  assert(weights[v] == 1 && (weights[startv] == (SCIP_Longint)nvars - v) && (startv == 0 || weights[0] == (SCIP_Longint)nvars - v + 1));
8631 
8632  /* the new dualcapacity should still be equal to the (nvars - v + 1) */
8633  assert(consdata->weightsum - consdata->capacity == (SCIP_Longint)nvars - v + 1);
8634 
8635  /* weight should still be sorted, because the reduction preserves this, but corresponding variables with equal
8636  * weight must not be sorted by their index
8637  */
8638 #ifndef NDEBUG
8639  for( w = nvars - 1; w > 0; --w )
8640  assert(weights[w] <= weights[w - 1]);
8641 #endif
8642  return SCIP_OKAY;
8643  }
8644 
8645  /* check if all rear items have the same weight as the last one, so we cannot tighten the constraint further */
8646  end = nvars - 2;
8647  while( end >= 0 && weights[end] == weights[end + 1] )
8648  {
8649  assert(end >= v);
8650  --end;
8651  }
8652 
8653  if( v >= end )
8654  goto TERMINATE;
8655 
8656  end = nvars - 2;
8657 
8658  /* can we stop early, another special reduction case might exist */
8659  if( 2 * weights[end] > dualcapacity )
8660  {
8661  restsumweights = 0;
8662 
8663  /* determine capacity of the small items */
8664  for( w = end + 1; w < nvars; ++w )
8665  restsumweights += weights[w];
8666 
8667  if( restsumweights * 2 <= dualcapacity )
8668  {
8669  /* check for further posssible reductions in the middle */
8670  while( v < end && restsumweights + weights[v] >= dualcapacity )
8671  ++v;
8672 
8673  if( v >= end )
8674  goto TERMINATE;
8675 
8676  /* dualcapacity is even, we can set the middle weights to dualcapacity/2 */
8677  if( (dualcapacity & 1) == 0 )
8678  {
8679  newweight = dualcapacity / 2;
8680  startv = v;
8681 
8682  /* set all middle coefficients */
8683  for( ; v <= end; ++v )
8684  {
8685  if( weights[v] > newweight )
8686  {
8687  reductionsum += (weights[v] - newweight);
8688  consdataChgWeight(consdata, v, newweight);
8689  ++(*nchgcoefs);
8690  }
8691  }
8692  }
8693  /* dualcapacity is odd, we can set the middle weights to dualcapacity but therefor need to multiply all
8694  * other coefficients by 2
8695  */
8696  else
8697  {
8698  /* correct the reductionsum */
8699  reductionsum *= 2;
8700 
8701  /* multiply big coefficients by 2 */
8702  for( w = 0; w < v; ++w )
8703  {
8704  consdataChgWeight(consdata, w, weights[w] * 2);
8705  }
8706 
8707  newweight = dualcapacity;
8708  /* set all middle coefficients */
8709  for( ; v <= end; ++v )
8710  {
8711  reductionsum += (2 * weights[v] - newweight);
8712  consdataChgWeight(consdata, v, newweight);
8713  }
8714 
8715  /* multiply small coefficients by 2 */
8716  for( w = end + 1; w < nvars; ++w )
8717  {
8718  consdataChgWeight(consdata, w, weights[w] * 2);
8719  }
8720  (*nchgcoefs) += nvars;
8721 
8722  dualcapacity *= 2;
8723  consdata->capacity *= 2;
8724  ++(*nchgsides);
8725  }
8726  }
8727 
8728  goto TERMINATE;
8729  }
8730 
8731  /* further reductions using the next possible coefficient sum
8732  *
8733  * e.g. 9x1 + 8x2 + 7x3 + 3x4 + x5 <= 19 <=> 9~x1 + 8~x2 + 7~x3 + 3~x4 + ~x5 >= 9
8734  * <=> 9~x1 + 8~x2 + 6~x3 + 3~x4 + ~x5 >= 9
8735  * <=> 9x1 + 8x2 + 6x3 + 3x4 + x5 <= 18
8736  */
8737  /* @todo loop for "k" can be extended, same coefficient when determine next sumcoef can be left out */
8738  for( k = 0; k < 4; ++k )
8739  {
8740  /* determine next minimal coefficient sum */
8741  switch( k )
8742  {
8743  case 0:
8744  sumcoef = weights[nvars - 1] + weights[nvars - 2];
8745  break;
8746  case 1:
8747  assert(nvars >= 3);
8748  sumcoef = weights[nvars - 1] + weights[nvars - 3];
8749  break;
8750  case 2:
8751  assert(nvars >= 4);
8752  if( weights[nvars - 1] + weights[nvars - 4] < weights[nvars - 2] + weights[nvars - 3] )
8753  {
8754  sumcoefcase = TRUE;
8755  sumcoef = weights[nvars - 1] + weights[nvars - 4];
8756  }
8757  else
8758  {
8759  sumcoefcase = FALSE;
8760  sumcoef = weights[nvars - 2] + weights[nvars - 3];
8761  }
8762  break;
8763  case 3:
8764  assert(nvars >= 5);
8765  if( sumcoefcase )
8766  {
8767  sumcoef = MIN(weights[nvars - 1] + weights[nvars - 5], weights[nvars - 2] + weights[nvars - 3]);
8768  }
8769  else
8770  {
8771  sumcoef = MIN(weights[nvars - 1] + weights[nvars - 4], weights[nvars - 1] + weights[nvars - 2] + weights[nvars - 3]);
8772  }
8773  break;
8774  default:
8775  return SCIP_ERROR;
8776  }
8777 
8778  /* tighten next coefficients that, pair with the current small coefficient, exceed the dualcapacity */
8779  minweight = weights[end];
8780  while( minweight <= sumcoef )
8781  {
8782  newweight = dualcapacity - minweight;
8783  startv = v;
8784  assert(v < nvars);
8785 
8786  /* @todo check for further reductions, when two times the minweight exceeds the dualcapacity */
8787  /* shrink big coefficients */
8788  while( weights[v] + minweight > dualcapacity && 2 * minweight <= dualcapacity )
8789  {
8790  reductionsum += (weights[v] - newweight);
8791  consdataChgWeight(consdata, v, newweight);
8792  ++v;
8793  assert(v < nvars);
8794  }
8795  (*nchgcoefs) += (v - startv);
8796 
8797  /* skip unchangable weights */
8798  while( weights[v] + minweight == dualcapacity )
8799  {
8800  assert(v < nvars);
8801  ++v;
8802  }
8803 
8804  --end;
8805  /* skip same end weights */
8806  while( end >= 0 && weights[end] == weights[end + 1] )
8807  --end;
8808 
8809  if( v >= end )
8810  goto TERMINATE;
8811 
8812  minweight = weights[end];
8813  }
8814 
8815  if( v >= end )
8816  goto TERMINATE;
8817 
8818  /* now check if a combination of small coefficients allows us to tighten big coefficients further */
8819  if( sumcoef < minweight )
8820  {
8821  minweight = sumcoef;
8822  newweight = dualcapacity - minweight;
8823  startv = v;
8824  assert(v < nvars);
8825 
8826  /* shrink big coefficients */
8827  while( weights[v] + minweight > dualcapacity && 2 * minweight <= dualcapacity )
8828  {
8829  reductionsum += (weights[v] - newweight);
8830  consdataChgWeight(consdata, v, newweight);
8831  ++v;
8832  assert(v < nvars);
8833  }
8834  (*nchgcoefs) += (v - startv);
8835 
8836  /* skip unchangable weights */
8837  while( weights[v] + minweight == dualcapacity )
8838  {
8839  assert(v < nvars);
8840  ++v;
8841  }
8842  }
8843 
8844  if( v >= end )
8845  goto TERMINATE;
8846 
8847  /* can we stop early, another special reduction case might exist */
8848  if( 2 * weights[end] > dualcapacity )
8849  {
8850  restsumweights = 0;
8851 
8852  /* determine capacity of the small items */
8853  for( w = end + 1; w < nvars; ++w )
8854  restsumweights += weights[w];
8855 
8856  if( restsumweights * 2 <= dualcapacity )
8857  {
8858  /* check for further posssible reductions in the middle */
8859  while( v < end && restsumweights + weights[v] >= dualcapacity )
8860  ++v;
8861 
8862  if( v >= end )
8863  goto TERMINATE;
8864 
8865  /* dualcapacity is even, we can set the middle weights to dualcapacity/2 */
8866  if( (dualcapacity & 1) == 0 )
8867  {
8868  newweight = dualcapacity / 2;
8869  startv = v;
8870 
8871  /* set all middle coefficients */
8872  for( ; v <= end; ++v )
8873  {
8874  if( weights[v] > newweight )
8875  {
8876  reductionsum += (weights[v] - newweight);
8877  consdataChgWeight(consdata, v, newweight);
8878  ++(*nchgcoefs);
8879  }
8880  }
8881  }
8882  /* dualcapacity is odd, we can set the middle weights to dualcapacity but therefor need to multiply all
8883  * other coefficients by 2
8884  */
8885  else
8886  {
8887  /* correct the reductionsum */
8888  reductionsum *= 2;
8889 
8890  /* multiply big coefficients by 2 */
8891  for( w = 0; w < v; ++w )
8892  {
8893  consdataChgWeight(consdata, w, weights[w] * 2);
8894  }
8895 
8896  newweight = dualcapacity;
8897  /* set all middle coefficients */
8898  for( ; v <= end; ++v )
8899  {
8900  reductionsum += (2 * weights[v] - newweight);
8901  consdataChgWeight(consdata, v, newweight);
8902  }
8903 
8904  /* multiply small coefficients by 2 */
8905  for( w = end + 1; w < nvars; ++w )
8906  {
8907  consdataChgWeight(consdata, w, weights[w] * 2);
8908  }
8909  (*nchgcoefs) += nvars;
8910 
8911  dualcapacity *= 2;
8912  consdata->capacity *= 2;
8913  ++(*nchgsides);
8914  }
8915  }
8916 
8917  goto TERMINATE;
8918  }
8919 
8920  /* cannot tighten any further */
8921  if( 2 * sumcoef > dualcapacity )
8922  goto TERMINATE;
8923  }
8924  }
8925  }
8926 
8927 
8928  TERMINATE:
8929  /* correct capacity */
8930  if( reductionsum > 0 )
8931  {
8932  assert(v > 0);
8933 
8934  consdata->capacity -= reductionsum;
8935  ++(*nchgsides);
8936 
8937  assert(consdata->weightsum - dualcapacity == consdata->capacity);
8938  }
8939  assert(weights[0] <= consdata->capacity);
8940 
8941  /* weight should still be sorted, because the reduction preserves this, but corresponding variables with equal
8942  * weight must not be sorted by their index
8943  */
8944 #ifndef NDEBUG
8945  for( w = nvars - 1; w > 0; --w )
8946  assert(weights[w] <= weights[w - 1]);
8947 #endif
8948 
8949  if( oldnchgcoefs < *nchgcoefs )
8950  {
8951  assert(!SCIPconsIsDeleted(cons));
8952 
8953  /* it might be that we can divide the weights by their greatest common divisor */
8954  normalizeWeights(cons, nchgcoefs, nchgsides);
8955  }
8956  else
8957  {
8958  assert(oldnchgcoefs == *nchgcoefs);
8959  assert(oldnchgsides == *nchgsides);
8960  }
8961 
8962  return SCIP_OKAY;
8963 }
8964 
8965 
8966 /** fixes variables with weights bigger than the capacity and delete redundant constraints, also sort weights */
8967 static
8969  SCIP* scip, /**< SCIP data structure */
8970  SCIP_CONS* cons, /**< knapsack constraint */
8971  int* nfixedvars, /**< pointer to store the amount of fixed variables */
8972  int* ndelconss, /**< pointer to store the amount of deleted constraints */
8973  int* nchgcoefs /**< pointer to store the amount of changed coefficients */
8974  )
8976  SCIP_VAR** vars;
8977  SCIP_CONSDATA* consdata;
8978  SCIP_Longint* weights;
8979  SCIP_Longint capacity;
8980  SCIP_Bool infeasible;
8981  SCIP_Bool fixed;
8982  int nvars;
8983  int v;
8984 
8985  assert(scip != NULL);
8986  assert(cons != NULL);
8987  assert(nfixedvars != NULL);
8988  assert(ndelconss != NULL);
8989  assert(nchgcoefs != NULL);
8990 
8991  consdata = SCIPconsGetData(cons);
8992  assert(consdata != NULL);
8993 
8994  nvars = consdata->nvars;
8995 
8996  /* no variables left, then delete constraint */
8997  if( nvars == 0 )
8998  {
8999  assert(consdata->capacity >= 0);
9000 
9001  SCIP_CALL( SCIPdelCons(scip, cons) );
9002  ++(*ndelconss);
9003 
9004  return SCIP_OKAY;
9005  }
9006 
9007  /* sort items */
9008  sortItems(consdata);
9009 
9010  vars = consdata->vars;
9011  weights = consdata->weights;
9012  capacity = consdata->capacity;
9013  v = 0;
9014 
9015  /* check for weights bigger than the capacity */
9016  while( weights[v] > capacity )
9017  {
9018  SCIP_CALL( SCIPfixVar(scip, vars[v], 0.0, &infeasible, &fixed) );
9019  assert(!infeasible);
9020 
9021  if( fixed )
9022  ++(*nfixedvars);
9023 
9024  ++v;
9025  }
9026 
9027  /* if we fixed at least one variable we need to delete them from the constraint */
9028  if( v > 0 )
9029  {
9030  if( v == nvars )
9031  {
9032  SCIP_CALL( SCIPdelCons(scip, cons) );
9033  ++(*ndelconss);
9034 
9035  return SCIP_OKAY;
9036  }
9037 
9038  /* delete all position from back to front */
9039  for( --v; v >= 0; --v )
9040  {
9041  SCIP_CALL( delCoefPos(scip, cons, v) );
9042  ++(*nchgcoefs);
9043  }
9044 
9045  /* sort items again because of deletion */
9046  sortItems(consdata);
9047  assert(vars == consdata->vars);
9048  assert(weights == consdata->weights);
9049  }
9050  assert(consdata->sorted);
9051  assert(weights[0] <= capacity);
9052 
9053  if( !SCIPisHugeValue(scip, (SCIP_Real) capacity) && consdata->weightsum <= capacity )
9054  {
9055  SCIP_CALL( SCIPdelCons(scip, cons) );
9056  ++(*ndelconss);
9057  }
9058 
9059  return SCIP_OKAY;
9060 }
9061 
9062 
9063 /** tries to simplify weights and delete redundant variables in knapsack a^Tx <= capacity
9064  *
9065  * 1. use the duality between a^Tx <= capacity <=> -a^T~x <= capacity - weightsum to tighten weights, e.g.
9066  *
9067  * 11x1 + 10x2 + 7x3 + 5x4 + 5x5 <= 25 <=> -10~x1 - 10~x2 - 7~x3 - 5~x4 - 5~x5 <= -13
9068  *
9069  * the above constraint can be changed to
9070  *
9071  * -8~x1 - 8~x2 - 7~x3 - 5~x4 - 5~x5 <= -12 <=> 8x1 + 8x2 + 7x3 + 5x4 + 5x5 <= 20
9072  *
9073  * 2. if variables in a constraint do not affect the (in-)feasibility of the constraint, we can delte them, e.g.
9074  *
9075  * 7x1 + 6x2 + 5x3 + 5x4 + x5 + x6 <= 20 => x5 and x6 are redundant and can be removed
9076  *
9077  * 3. Tries to use gcd information an all but one weight to change this not-included weight and normalize the
9078  * constraint further, e.g.
9079  *
9080  * 9x1 + 6x2 + 6x3 + 5x4 <= 13 => 9x1 + 6x2 + 6x3 + 6x4 <= 12 => 3x1 + 2x2 + 2x3 + 2x4 <= 4 => 4x1 + 2x2 + 2x3 + 2x4 <= 4
9081  * => 2x1 + x2 + x3 + x4 <= 2
9082  * 9x1 + 6x2 + 6x3 + 7x4 <= 13 => 9x1 + 6x2 + 6x3 + 6x4 <= 12 => see above
9083  */
9084 static
9086  SCIP* scip, /**< SCIP data structure */
9087  SCIP_CONS* cons, /**< knapsack constraint */
9088  int* nfixedvars, /**< pointer to store the amount of fixed variables */
9089  int* ndelconss, /**< pointer to store the amount of deleted constraints */
9090  int* nchgcoefs, /**< pointer to store the amount of changed coefficients */
9091  int* nchgsides, /**< pointer to store the amount of changed sides */
9092  int* naddconss, /**< pointer to count number of added constraints */
9093  SCIP_Bool* cutoff /**< pointer to store whether the node can be cut off */
9094  )
9095 {
9096  SCIP_VAR** vars;
9097  SCIP_CONSDATA* consdata;
9098  SCIP_Longint* weights;
9099  SCIP_Longint restweight;
9100  SCIP_Longint newweight;
9101  SCIP_Longint weight;
9102  SCIP_Longint oldgcd;
9103  SCIP_Longint rest;
9104  SCIP_Longint gcd;
9105  int oldnchgcoefs;
9106  int oldnchgsides;
9107  int candpos;
9108  int candpos2;
9109  int nvars;
9110  int v;
9111 
9112  assert(scip != NULL);
9113  assert(cons != NULL);
9114  assert(nfixedvars != NULL);
9115  assert(ndelconss != NULL);
9116  assert(nchgcoefs != NULL);
9117  assert(nchgsides != NULL);
9118  assert(naddconss != NULL);
9119  assert(cutoff != NULL);
9120  assert(!SCIPconsIsModifiable(cons));
9121 
9122  consdata = SCIPconsGetData(cons);
9123  assert( consdata != NULL );
9124 
9125  *cutoff = FALSE;
9126 
9127  /* remove double enties and also combinations of active and negated variables */
9128  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
9129  assert(consdata->merged);
9130  if( *cutoff )
9131  return SCIP_OKAY;
9132 
9133  assert(consdata->capacity >= 0);
9134 
9135  /* fix variables with big coefficients and remove redundant constraints, sort weights */
9136  SCIP_CALL( prepareCons(scip, cons, nfixedvars, ndelconss, nchgcoefs) );
9137 
9138  if( SCIPconsIsDeleted(cons) )
9139  return SCIP_OKAY;
9140 
9141  if( !SCIPisHugeValue(scip, (SCIP_Real) consdata->capacity) )
9142  {
9143  /* 1. dual weights tightening */
9144  SCIP_CALL( dualWeightsTightening(scip, cons, ndelconss, nchgcoefs, nchgsides, naddconss) );
9145 
9146  if( SCIPconsIsDeleted(cons) )
9147  return SCIP_OKAY;
9148 
9149  /* 2. delete redundant variables */
9150  SCIP_CALL( detectRedundantVars(scip, cons, ndelconss, nchgcoefs, nchgsides, naddconss) );
9151 
9152  if( SCIPconsIsDeleted(cons) )
9153  return SCIP_OKAY;
9154  }
9155 
9156  vars = consdata->vars;
9157  weights = consdata->weights;
9158  nvars = consdata->nvars;
9159 
9160 #ifndef NDEBUG
9161  /* constraint might not be sorted, but the weights are already sorted */
9162  for( v = nvars - 1; v > 0; --v )
9163  assert(weights[v] <= weights[v-1]);
9164 #endif
9165 
9166  /* determine greatest common divisor */
9167  gcd = weights[nvars - 1];
9168  for( v = nvars - 2; v >= 0 && gcd > 1; --v )
9169  {
9170  gcd = SCIPcalcGreComDiv(gcd, weights[v]);
9171  }
9172 
9173  /* divide the constraint by their greatest common divisor */
9174  if( gcd >= 2 )
9175  {
9176  for( v = nvars - 1; v >= 0; --v )
9177  {
9178  consdataChgWeight(consdata, v, weights[v]/gcd);
9179  }
9180  (*nchgcoefs) += nvars;
9181 
9182  consdata->capacity /= gcd;
9183  (*nchgsides)++;
9184  }
9185  assert(consdata->nvars == nvars);
9186 
9187  /* weight should still be sorted, because the reduction preserves this, but corresponding variables with equal weight
9188  * must not be sorted by their index
9189  */
9190 #ifndef NDEBUG
9191  for( v = nvars - 1; v > 0; --v )
9192  assert(weights[v] <= weights[v-1]);
9193 #endif
9194 
9195  /* 3. start gcd procedure for all variables */
9196  do
9197  {
9198  oldnchgcoefs = *nchgcoefs;
9199  oldnchgsides = *nchgsides;
9200 
9201  vars = consdata->vars;
9202  weights = consdata->weights;
9203  nvars = consdata->nvars;
9204 
9205  /* stop if we have two coefficients which are one in absolute value */
9206  if( weights[nvars - 1] == 1 && weights[nvars - 2] == 1 )
9207  return SCIP_OKAY;
9208 
9209  gcd = -1;
9210  candpos = -1;
9211  candpos2 = -1;
9212 
9213  /* calculate greatest common divisor over all integer and binary variables and determine the candidate where we might
9214  * change the coefficient
9215  */
9216  for( v = nvars - 1; v >= 0; --v )
9217  {
9218  weight = weights[v];
9219  assert(weight >= 1);
9220 
9221  oldgcd = gcd;
9222 
9223  if( gcd == -1 )
9224  {
9225  gcd = weights[v];
9226  assert(gcd >= 1);
9227  }
9228  else
9229  {
9230  /* calculate greatest common divisor for all variables */
9231  gcd = SCIPcalcGreComDiv(gcd, weight);
9232  }
9233 
9234  /* if the greatest commmon divisor has become 1, we might have found the possible coefficient to change or we
9235  * can terminate
9236  */
9237  if( gcd == 1 )
9238  {
9239  /* found candidate */
9240  if( candpos == -1 )
9241  {
9242  gcd = oldgcd;
9243  candpos = v;
9244 
9245  /* if both first coefficients have a gcd of 1, both are candidates for the coefficient change */
9246  if( v == nvars - 2 )
9247  candpos2 = v + 1;
9248  }
9249  /* two different variables lead to a gcd of one, so we cannot change a coefficient */
9250  else
9251  {
9252  if( candpos == v + 1 && candpos2 == v + 2 )
9253  {
9254  assert(candpos2 == nvars - 1);
9255 
9256  /* take new candidates */
9257  candpos = candpos2;
9258 
9259  /* recalculate gcd from scratch */
9260  gcd = weights[v+1];
9261  assert(gcd >= 1);
9262 
9263  /* calculate greatest common divisor for variables */
9264  gcd = SCIPcalcGreComDiv(gcd, weights[v]);
9265  if( gcd == 1 )
9266  return SCIP_OKAY;
9267  }
9268  else
9269  /* cannot determine a possible coefficient for reduction */
9270  return SCIP_OKAY;
9271  }
9272  }
9273  }
9274  assert(gcd >= 2);
9275 
9276  /* we should have found one coefficient, that led to a gcd of 1, otherwise we could normalize the constraint
9277  * further
9278  */
9279  assert(candpos >= 0 && candpos < nvars);
9280 
9281  /* determine the remainder of the capacity and the gcd */
9282  rest = consdata->capacity % gcd;
9283  assert(rest >= 0);
9284  assert(rest < gcd);
9285 
9286  /* determine the remainder of the coefficient candidate and the gcd */
9287  restweight = weights[candpos] % gcd;
9288  assert(restweight >= 1);
9289  assert(restweight < gcd);
9290 
9291  if( rest > 0 )
9292  {
9293  /* replace old with new capacity */
9294  consdata->capacity -= rest;
9295  ++(*nchgsides);
9296  }
9297 
9298  /* calculate new coefficient */
9299  if( restweight > rest )
9300  newweight = weights[candpos] - restweight + gcd;
9301  else
9302  newweight = weights[candpos] - restweight;
9303 
9304  assert(newweight == 0 || SCIPcalcGreComDiv(gcd, newweight) == gcd);
9305 
9306  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);
9307 
9308  if( newweight == 0 )
9309  {
9310  /* delete redundant coefficient */
9311  SCIP_CALL( delCoefPos(scip, cons, candpos) );
9312  assert(consdata->nvars == nvars - 1);
9313  --nvars;
9314  }
9315  else
9316  {
9317  /* replace old with new coefficient */
9318  consdataChgWeight(consdata, candpos, newweight);
9319  }
9320  ++(*nchgcoefs);
9321 
9322  assert(consdata->vars == vars);
9323  assert(consdata->nvars == nvars);
9324  assert(consdata->weights == weights);
9325 
9326  /* now constraint can be normalized, dividing it by the gcd */
9327  for( v = nvars - 1; v >= 0; --v )
9328  {
9329  consdataChgWeight(consdata, v, weights[v]/gcd);
9330  }
9331  (*nchgcoefs) += nvars;
9332 
9333  consdata->capacity /= gcd;
9334  ++(*nchgsides);
9335 
9336  SCIPdebugPrintCons(scip, cons, NULL);
9337 
9338  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));
9339  }
9340  while( nvars >= 2 );
9341 
9342  return SCIP_OKAY;
9343 }
9344 
9345 /** deletes all fixed variables from knapsack constraint, and replaces variables with binary representatives */
9346 static
9348  SCIP* scip, /**< SCIP data structure */
9349  SCIP_CONS* cons, /**< knapsack constraint */
9350  SCIP_Bool* cutoff /**< pointer to store whether the node can be cut off, or NULL if this
9351  * information is not needed; in this case, we apply all fixings
9352  * instead of stopping after the first infeasible one */
9353  )
9355  SCIP_CONSDATA* consdata;
9356  int v;
9357 
9358  assert(scip != NULL);
9359  assert(cons != NULL);
9360 
9361  consdata = SCIPconsGetData(cons);
9362  assert(consdata != NULL);
9363  assert(consdata->nvars == 0 || consdata->vars != NULL);
9364 
9365  if( cutoff != NULL )
9366  *cutoff = FALSE;
9367 
9368  SCIPdebugMessage("apply fixings:\n");
9369  SCIPdebugPrintCons(scip, cons, NULL);
9370 
9371  /* check infeasibility */
9372  if ( consdata->onesweightsum > consdata->capacity )
9373  {
9374  SCIPdebugMessage("apply fixings detected cutoff.\n");
9375 
9376  if( cutoff != NULL )
9377  *cutoff = TRUE;
9378 
9379  return SCIP_OKAY;
9380  }
9381 
9382  /* all multi-aggregations should be resolved */
9383  consdata->existmultaggr = FALSE;
9384 
9385  v = 0;
9386  while( v < consdata->nvars )
9387  {
9388  SCIP_VAR* var;
9389 
9390  var = consdata->vars[v];
9391  assert(SCIPvarIsBinary(var));
9392 
9393  if( SCIPvarGetLbGlobal(var) > 0.5 )
9394  {
9395  assert(SCIPisFeasEQ(scip, SCIPvarGetUbGlobal(var), 1.0));
9396  consdata->capacity -= consdata->weights[v];
9397  SCIP_CALL( delCoefPos(scip, cons, v) );
9398  consdata->cliquesadded = FALSE; /* reduced capacity might lead to larger cliques */
9399  }
9400  else if( SCIPvarGetUbGlobal(var) < 0.5 )
9401  {
9402  assert(SCIPisFeasEQ(scip, SCIPvarGetLbGlobal(var), 0.0));
9403  SCIP_CALL( delCoefPos(scip, cons, v) );
9404  }
9405  else
9406  {
9407  SCIP_VAR* repvar;
9408  SCIP_VAR* negvar;
9409  SCIP_VAR* workvar;
9410  SCIP_Longint weight;
9411  SCIP_Bool negated;
9412 
9413  weight = consdata->weights[v];
9414 
9415  /* get binary representative of variable */
9416  SCIP_CALL( SCIPgetBinvarRepresentative(scip, var, &repvar, &negated) );
9417  assert(repvar != NULL);
9418 
9419  /* check for multi-aggregation */
9420  if( SCIPvarIsNegated(repvar) )
9421  {
9422  workvar = SCIPvarGetNegatedVar(repvar);
9423  assert(workvar != NULL);
9424  negated = TRUE;
9425  }
9426  else
9427  {
9428  workvar = repvar;
9429  negated = FALSE;
9430  }
9431 
9432  /* @todo maybe resolve the problem that the eliminating of the multi-aggregation leads to a non-knapsack
9433  * constraint (converting into a linear constraint), for example the multi-aggregation consist of a non-binary
9434  * variable or due to resolving now their are non-integral coefficients or a non-integral capacity
9435  *
9436  * If repvar is not negated so workvar = repvar, otherwise workvar = 1 - repvar. This means,
9437  * weight * workvar = weight * (a_1*y_1 + ... + a_n*y_n + c)
9438  *
9439  * The explanation for the following block:
9440  * 1a) If repvar is a multi-aggregated variable weight * repvar should be replaced by
9441  * weight * (a_1*y_1 + ... + a_n*y_n + c).
9442  * 1b) If repvar is a negated variable of a multi-aggregated variable weight * repvar should be replaced by
9443  * weight - weight * (a_1*y_1 + ... + a_n*y_n + c), for better further use here we switch the sign of weight
9444  * so now we have the replacement -weight + weight * (a_1*y_1 + ... + a_n*y_n + c).
9445  * 2) For all replacement variable we check:
9446  * 2a) weight * a_i < 0 than we add -weight * a_i * y_i_neg to the constraint and adjust the capacity through
9447  * capacity -= weight * a_i caused by the negation of y_i.
9448  * 2b) weight * a_i >= 0 than we add weight * a_i * y_i to the constraint.
9449  * 3a) If repvar was not negated we need to subtract weight * c from capacity.
9450  * 3b) If repvar was negated we need to subtract weight * (c - 1) from capacity(note we switched the sign of
9451  * weight in this case.
9452  */
9453  if( SCIPvarGetStatus(workvar) == SCIP_VARSTATUS_MULTAGGR )
9454  {
9455  SCIP_VAR** aggrvars;
9456  SCIP_Real* aggrscalars;
9457  SCIP_Real aggrconst;
9458  int naggrvars;
9459  int i;
9460 
9461  SCIP_CALL( SCIPflattenVarAggregationGraph(scip, workvar) );
9462  naggrvars = SCIPvarGetMultaggrNVars(workvar);
9463  aggrvars = SCIPvarGetMultaggrVars(workvar);
9464  aggrscalars = SCIPvarGetMultaggrScalars(workvar);
9465  aggrconst = SCIPvarGetMultaggrConstant(workvar);
9466  assert((aggrvars != NULL && aggrscalars != NULL) || naggrvars == 0);
9467 
9468  if( !SCIPisIntegral(scip, weight * aggrconst) )
9469  {
9470  SCIPerrorMessage("try to resolve a multi-aggregation with a non-integral value for weight*aggrconst = %g\n", weight*aggrconst);
9471  return SCIP_ERROR;
9472  }
9473 
9474  /* if workvar was negated, we have to flip the weight */
9475  if( negated )
9476  weight *= -1;
9477 
9478  for( i = naggrvars - 1; i >= 0; --i )
9479  {
9480  assert(aggrvars != NULL);
9481  assert(aggrscalars != NULL);
9482 
9483  if( !SCIPvarIsBinary(aggrvars[i]) )
9484  {
9485  SCIPerrorMessage("try to resolve a multi-aggregation with a non-binary variable <%s>\n", aggrvars[i]);
9486  return SCIP_ERROR;
9487  }
9488  if( !SCIPisIntegral(scip, weight * aggrscalars[i]) )
9489  {
9490  SCIPerrorMessage("try to resolve a multi-aggregation with a non-integral value for weight*aggrscalars = %g\n", weight*aggrscalars[i]);
9491  return SCIP_ERROR;
9492  }
9493  /* if the new coefficient is smaller than zero, we need to add the negated variable instead and adjust the capacity */
9494  if( SCIPisNegative(scip, weight * aggrscalars[i]) )
9495  {
9496  SCIP_CALL( SCIPgetNegatedVar(scip, aggrvars[i], &negvar));
9497  assert(negvar != NULL);
9498  SCIP_CALL( addCoef(scip, cons, negvar, (SCIP_Longint)(SCIPfloor(scip, -weight * aggrscalars[i] + 0.5))) );
9499  consdata->capacity -= (SCIP_Longint)(SCIPfloor(scip, weight * aggrscalars[i] + 0.5));
9500  }
9501  else
9502  {
9503  SCIP_CALL( addCoef(scip, cons, aggrvars[i], (SCIP_Longint)(SCIPfloor(scip, weight * aggrscalars[i] + 0.5))) );
9504  }
9505  }
9506  /* delete old coefficient */
9507  SCIP_CALL( delCoefPos(scip, cons, v) );
9508 
9509  /* adjust the capacity with the aggregation constant and if necessary the extra weight through the negation */
9510  if( negated )
9511  consdata->capacity -= (SCIP_Longint)SCIPfloor(scip, weight * (aggrconst - 1) + 0.5);
9512  else
9513  consdata->capacity -= (SCIP_Longint)SCIPfloor(scip, weight * aggrconst + 0.5);
9514 
9515  if( consdata->capacity < 0 )
9516  {
9517  if( cutoff != NULL )
9518  {
9519  *cutoff = TRUE;
9520  break;
9521  }
9522  }
9523  }
9524  /* check, if the variable should be replaced with the representative */
9525  else if( repvar != var )
9526  {
9527  /* delete old (aggregated) variable */
9528  SCIP_CALL( delCoefPos(scip, cons, v) );
9529 
9530  /* add representative instead */
9531  SCIP_CALL( addCoef(scip, cons, repvar, weight) );
9532  }
9533  else
9534  ++v;
9535  }
9536  }
9537  assert(consdata->onesweightsum == 0);
9538 
9539  SCIPdebugMessage("after applyFixings, before merging:\n");
9540  SCIPdebugPrintCons(scip, cons, NULL);
9541 
9542  /* if aggregated variables have been replaced, multiple entries of the same variable are possible and we have to
9543  * clean up the constraint
9544  */
9545  if( cutoff != NULL && !(*cutoff) )
9546  {
9547  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
9548  SCIPdebugMessage("after applyFixings and merging:\n");
9549  SCIPdebugPrintCons(scip, cons, NULL);
9550  }
9551 
9552  return SCIP_OKAY;
9553 }
9554 
9555 /** inserts an element into the list of binary zero implications */
9556 static
9558  SCIP* scip, /**< SCIP data structure */
9559  int** liftcands, /**< array of the lifting candidates */
9560  int* nliftcands, /**< number of lifting candidates */
9561  int** firstidxs, /**< array of first zeroitems indices */
9562  SCIP_Longint** zeroweightsums, /**< array of sums of weights of the implied-to-zero items */
9563  int** zeroitems, /**< pointer to zero items array */
9564  int** nextidxs, /**< pointer to array of next zeroitems indeces */
9565  int* zeroitemssize, /**< pointer to size of zero items array */
9566  int* nzeroitems, /**< pointer to length of zero items array */
9567  int probindex, /**< problem index of variable y in implication y == v -> x == 0 */
9568  SCIP_Bool value, /**< value v of variable y in implication */
9569  int knapsackidx, /**< index of variable x in knapsack */
9570  SCIP_Longint knapsackweight, /**< weight of variable x in knapsack */
9571  SCIP_Bool* memlimitreached /**< pointer to store whether the memory limit was reached */
9572  )
9573 {
9574  int nzeros;
9575 
9576  assert(liftcands != NULL);
9577  assert(liftcands[value] != NULL);
9578  assert(nliftcands != NULL);
9579  assert(firstidxs != NULL);
9580  assert(firstidxs[value] != NULL);
9581  assert(zeroweightsums != NULL);
9582  assert(zeroweightsums[value] != NULL);
9583  assert(zeroitems != NULL);
9584  assert(nextidxs != NULL);
9585  assert(zeroitemssize != NULL);
9586  assert(nzeroitems != NULL);
9587  assert(*nzeroitems <= *zeroitemssize);
9588  assert(0 <= probindex && probindex < SCIPgetNVars(scip) - SCIPgetNContVars(scip));
9589  assert(memlimitreached != NULL);
9590 
9591  nzeros = *nzeroitems;
9592 
9593  /* allocate enough memory */
9594  if( nzeros == *zeroitemssize )
9595  {
9596  /* we explicitly construct the complete implication graph where the knapsack variables are involved;
9597  * this can be too huge - abort on memory limit
9598  */
9599  if( *zeroitemssize >= MAX_ZEROITEMS_SIZE )
9600  {
9601  SCIPdebugMessage("memory limit of %d bytes reached in knapsack preprocessing - abort collecting zero items\n",
9602  *zeroitemssize);
9603  *memlimitreached = TRUE;
9604  return SCIP_OKAY;
9605  }
9606  *zeroitemssize *= 2;
9607  *zeroitemssize = MIN(*zeroitemssize, MAX_ZEROITEMS_SIZE);
9608  SCIP_CALL( SCIPreallocBufferArray(scip, zeroitems, *zeroitemssize) );
9609  SCIP_CALL( SCIPreallocBufferArray(scip, nextidxs, *zeroitemssize) );
9610  }
9611  assert(nzeros < *zeroitemssize);
9612 
9613  if( *memlimitreached )
9614  *memlimitreached = FALSE;
9615 
9616  /* insert element */
9617  (*zeroitems)[nzeros] = knapsackidx;
9618  (*nextidxs)[nzeros] = firstidxs[value][probindex];
9619  if( firstidxs[value][probindex] == 0 )
9620  {
9621  liftcands[value][nliftcands[value]] = probindex;
9622  ++nliftcands[value];
9623  }
9624  firstidxs[value][probindex] = nzeros;
9625  ++(*nzeroitems);
9626  zeroweightsums[value][probindex] += knapsackweight;
9627 
9628  return SCIP_OKAY;
9629 }
9630 
9631 #define MAX_CLIQUELENGTH 50
9632 /** applies rule (3) of the weight tightening procedure, which can lift other variables into the knapsack:
9633  * (3) for a clique C let C(xi == v) := C \ {j: xi == v -> xj == 0}),
9634  * let cliqueweightsum(xi == v) := sum(W(C(xi == v)))
9635  * if cliqueweightsum(xi == v) < capacity:
9636  * - fixing variable xi to v would make the knapsack constraint redundant
9637  * - the weight of the variable or its negation (depending on v) can be increased as long as it has the same
9638  * redundancy effect:
9639  * wi' := capacity - cliqueweightsum(xi == v)
9640  * this rule can also be applied to binary variables not in the knapsack!
9641  */
9642 static
9644  SCIP* scip, /**< SCIP data structure */
9645  SCIP_CONS* cons, /**< knapsack constraint */
9646  int* nchgcoefs, /**< pointer to count total number of changed coefficients */
9647  SCIP_Bool* cutoff /**< pointer to store whether the node can be cut off */
9648  )
9649 {
9650  SCIP_CONSDATA* consdata;
9651  SCIP_VAR** binvars;
9652  int nbinvars;
9653  int* liftcands[2]; /* binary variables that have at least one entry in zeroitems */
9654  int* firstidxs[2]; /* first index in zeroitems for each binary variable/value pair, or zero for empty list */
9655  SCIP_Longint* zeroweightsums[2]; /* sums of weights of the implied-to-zero items */
9656  int* zeroitems; /* item number in knapsack that is implied to zero */
9657  int* nextidxs; /* next index in zeroitems for the same binary variable, or zero for end of list */
9658  int zeroitemssize;
9659  int nzeroitems;
9660  SCIP_Bool* zeroiteminserted[2];
9661  SCIP_Bool memlimitreached;
9662  int nliftcands[2];
9663  SCIP_Bool* cliqueused;
9664  SCIP_Bool* itemremoved;
9665  SCIP_Longint maxcliqueweightsum;
9666  SCIP_VAR** addvars;
9667  SCIP_Longint* addweights;
9668  SCIP_Longint addweightsum;
9669  int nvars;
9670  int cliquenum;
9671  int naddvars;
9672  int val;
9673  int i;
9674 
9675  int* tmpindices;
9676  SCIP_Bool* tmpboolindices;
9677  int* tmpindices2;
9678  SCIP_Bool* tmpboolindices2;
9679  int* tmpindices3;
9680  SCIP_Bool* tmpboolindices3;
9681  int tmp;
9682  int tmp2;
9683  int tmp3;
9684  SCIP_CONSHDLR* conshdlr;
9685  SCIP_CONSHDLRDATA* conshdlrdata;
9686 
9687  assert(nchgcoefs != NULL);
9688  assert(!SCIPconsIsModifiable(cons));
9689 
9690  consdata = SCIPconsGetData(cons);
9691  assert(consdata != NULL);
9692  assert(consdata->row == NULL); /* we are in presolve, so no LP row exists */
9693  assert(consdata->weightsum > consdata->capacity); /* otherwise, the constraint is redundant */
9694  assert(consdata->nvars > 0);
9695  assert(consdata->merged);
9696 
9697  nvars = consdata->nvars;
9698 
9699  /* check if the knapsack has too many items/cliques for applying this costly method */
9700  if( (!consdata->cliquepartitioned && nvars > MAX_USECLIQUES_SIZE) || consdata->ncliques > MAX_USECLIQUES_SIZE )
9701  return SCIP_OKAY;
9702 
9703  /* sort items, s.t. the heaviest one is in the first position */
9704  sortItems(consdata);
9705 
9706  if( !consdata->cliquepartitioned && nvars > MAX_USECLIQUES_SIZE )
9707  return SCIP_OKAY;
9708 
9709  /* we have to consider all integral variables since even integer and implicit integer variables can have binary bounds */
9710  nbinvars = SCIPgetNVars(scip) - SCIPgetNContVars(scip);
9711  assert(nbinvars > 0);
9712  binvars = SCIPgetVars(scip);
9713 
9714  /* get conshdlrdata to use cleared memory */
9715  conshdlr = SCIPconsGetHdlr(cons);
9716  assert(conshdlr != NULL);
9717  conshdlrdata = SCIPconshdlrGetData(conshdlr);
9718  assert(conshdlrdata != NULL);
9719 
9720  /* allocate temporary memory for the list of implied to zero variables */
9721  zeroitemssize = MIN(nbinvars, MAX_ZEROITEMS_SIZE); /* initial size of zeroitems buffer */
9722  SCIP_CALL( SCIPallocBufferArray(scip, &liftcands[0], nbinvars) );
9723  SCIP_CALL( SCIPallocBufferArray(scip, &liftcands[1], nbinvars) );
9724 
9725  assert(conshdlrdata->ints1size > 0);
9726  assert(conshdlrdata->ints2size > 0);
9727  assert(conshdlrdata->longints1size > 0);
9728  assert(conshdlrdata->longints2size > 0);
9729 
9730  /* next if conditions should normally not be true, because it means that presolving has created more binary variables
9731  * than binary + integer variables existed at the presolving initialization method, but for example if you would
9732  * transform all integers into their binary representation then it maybe happens
9733  */
9734  if( conshdlrdata->ints1size < nbinvars )
9735  {
9736  int oldsize;
9737  oldsize = conshdlrdata->ints1size;
9738 
9739  while( conshdlrdata->ints1size < nbinvars )
9740  conshdlrdata->ints1size *= 2;
9741  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->ints1, conshdlrdata->ints1size) );
9742  BMSclearMemoryArray(&(conshdlrdata->ints1[oldsize]), conshdlrdata->ints1size - oldsize); /*lint !e866*/
9743  }
9744  if( conshdlrdata->ints2size < nbinvars )
9745  {
9746  int oldsize;
9747  oldsize = conshdlrdata->ints2size;
9748 
9749  while( conshdlrdata->ints2size < nbinvars )
9750  conshdlrdata->ints2size *= 2;
9751  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->ints2, conshdlrdata->ints2size) );
9752  BMSclearMemoryArray(&(conshdlrdata->ints2[oldsize]), conshdlrdata->ints2size - oldsize); /*lint !e866*/
9753  }
9754  if( conshdlrdata->longints1size < nbinvars )
9755  {
9756  int oldsize;
9757  oldsize = conshdlrdata->longints1size;
9758 
9759  while( conshdlrdata->longints1size < nbinvars )
9760  conshdlrdata->longints1size *= 2;
9761  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->longints1, conshdlrdata->longints1size) );
9762  BMSclearMemoryArray(&(conshdlrdata->longints1[oldsize]), conshdlrdata->longints1size - oldsize); /*lint !e866*/
9763  }
9764  if( conshdlrdata->longints2size < nbinvars )
9765  {
9766  int oldsize;
9767  oldsize = conshdlrdata->longints2size;
9768 
9769  while( conshdlrdata->longints2size < nbinvars )
9770  conshdlrdata->longints2size *= 2;
9771  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->longints2, conshdlrdata->longints2size) );
9772  BMSclearMemoryArray(&(conshdlrdata->longints2[oldsize]), conshdlrdata->longints2size - oldsize); /*lint !e866*/
9773  }
9774 
9775  firstidxs[0] = conshdlrdata->ints1;
9776  firstidxs[1] = conshdlrdata->ints2;
9777  zeroweightsums[0] = conshdlrdata->longints1;
9778  zeroweightsums[1] = conshdlrdata->longints2;
9779 
9780  /* check for cleared arrays, all entries are zero */
9781 #ifndef NDEBUG
9782  for( tmp = nbinvars - 1; tmp >= 0; --tmp )
9783  {
9784  assert(firstidxs[0][tmp] == 0);
9785  assert(firstidxs[1][tmp] == 0);
9786  assert(zeroweightsums[0][tmp] == 0);
9787  assert(zeroweightsums[1][tmp] == 0);
9788  }
9789 #endif
9790 
9791  SCIP_CALL( SCIPallocBufferArray(scip, &zeroitems, zeroitemssize) );
9792  SCIP_CALL( SCIPallocBufferArray(scip, &nextidxs, zeroitemssize) );
9793 
9794  zeroitems[0] = -1; /* dummy element */
9795  nextidxs[0] = -1;
9796  nzeroitems = 1;
9797  nliftcands[0] = 0;
9798  nliftcands[1] = 0;
9799 
9800  assert(conshdlrdata->bools1size > 0);
9801  assert(conshdlrdata->bools2size > 0);
9802 
9803  /* next if conditions should normally not be true, because it means that presolving has created more binary variables
9804  * than binary + integer variables existed at the presolving initialization method, but for example if you would
9805  * transform all integers into their binary representation then it maybe happens
9806  */
9807  if( conshdlrdata->bools1size < nbinvars )
9808  {
9809  int oldsize;
9810  oldsize = conshdlrdata->bools1size;
9811 
9812  while( conshdlrdata->bools1size < nbinvars )
9813  conshdlrdata->bools1size *= 2;
9814  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->bools1, conshdlrdata->bools1size) );
9815  BMSclearMemoryArray(&(conshdlrdata->bools1[oldsize]), conshdlrdata->bools1size - oldsize); /*lint !e866*/
9816  }
9817  if( conshdlrdata->bools2size < nbinvars )
9818  {
9819  int oldsize;
9820  oldsize = conshdlrdata->bools2size;
9821 
9822  while( conshdlrdata->bools2size < nbinvars )
9823  conshdlrdata->bools2size *= 2;
9824  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->bools2, conshdlrdata->bools2size) );
9825  BMSclearMemoryArray(&(conshdlrdata->bools2[oldsize]), conshdlrdata->bools2size - oldsize); /*lint !e866*/
9826  }
9827 
9828  zeroiteminserted[0] = conshdlrdata->bools1;
9829  zeroiteminserted[1] = conshdlrdata->bools2;
9830 
9831  /* check for cleared arrays, all entries are zero */
9832 #ifndef NDEBUG
9833  for( tmp = nbinvars - 1; tmp >= 0; --tmp )
9834  {
9835  assert(zeroiteminserted[0][tmp] == 0);
9836  assert(zeroiteminserted[1][tmp] == 0);
9837  }
9838 #endif
9839 
9840  SCIP_CALL( SCIPallocBufferArray(scip, &tmpindices, 2 * nbinvars) );
9841  SCIP_CALL( SCIPallocBufferArray(scip, &tmpboolindices, 2 * nbinvars) );
9842  SCIP_CALL( SCIPallocBufferArray(scip, &tmpindices2, 2 * nbinvars) );
9843  SCIP_CALL( SCIPallocBufferArray(scip, &tmpboolindices2, 2 * nbinvars) );
9844  SCIP_CALL( SCIPallocBufferArray(scip, &tmpindices3, consdata->nvars) );
9845  SCIP_CALL( SCIPallocBufferArray(scip, &tmpboolindices3, consdata->nvars) );
9846  tmp2 = 0;
9847  tmp3 = 0;
9848 
9849  memlimitreached = FALSE;
9850  for( i = 0; i < consdata->nvars && !memlimitreached; ++i )
9851  {
9852  SCIP_VAR* var;
9853  SCIP_Longint weight;
9854  SCIP_Bool value;
9855  int varprobindex;
9856  SCIP_VAR** implvars;
9857  SCIP_BOUNDTYPE* impltypes;
9858  int nimpls;
9859  SCIP_CLIQUE** cliques;
9860  int ncliques;
9861  int j;
9862 
9863  tmp = 0;
9864 
9865  /* get corresponding active problem variable */
9866  var = consdata->vars[i];
9867  weight = consdata->weights[i];
9868  value = TRUE;
9869  SCIP_CALL( SCIPvarGetProbvarBinary(&var, &value) );
9870  varprobindex = SCIPvarGetProbindex(var);
9871  assert(0 <= varprobindex && varprobindex < nbinvars);
9872 
9873  /* update the zeroweightsum */
9874  zeroweightsums[!value][varprobindex] += weight; /*lint !e514*/
9875  tmpboolindices3[tmp3] = !value;
9876  tmpindices3[tmp3] = varprobindex;
9877  ++tmp3;
9878 
9879  /* initialize the arrays of inserted zero items */
9880  /* first add the implications (~x == 1 -> x == 0) */
9881  {
9882  SCIP_Bool implvalue;
9883  int probindex;
9884 
9885  probindex = SCIPvarGetProbindex(var);
9886  assert(0 <= probindex && probindex < nbinvars);
9887 
9888  implvalue = !value;
9889 
9890  /* insert the item into the list of the implied variable/value */
9891  assert( !zeroiteminserted[implvalue][probindex] );
9892 
9893  if( firstidxs[implvalue][probindex] == 0 )
9894  {
9895  tmpboolindices2[tmp2] = implvalue;
9896  tmpindices2[tmp2] = probindex;
9897  ++tmp2;
9898  }
9899  SCIP_CALL( insertZerolist(scip, liftcands, nliftcands, firstidxs, zeroweightsums,
9900  &zeroitems, &nextidxs, &zeroitemssize, &nzeroitems, probindex, implvalue, i, weight,
9901  &memlimitreached) );
9902  zeroiteminserted[implvalue][probindex] = TRUE;
9903  tmpboolindices[tmp] = implvalue;
9904  tmpindices[tmp] = probindex;
9905  ++tmp;
9906  }
9907 
9908  /* get implications of the knapsack item fixed to one: x == 1 -> y == (1-v);
9909  * the negation of these implications (y == v -> x == 0) are the ones that we are interested in
9910  */
9911  nimpls = SCIPvarGetNBinImpls(var, value);
9912  implvars = SCIPvarGetImplVars(var, value);
9913  impltypes = SCIPvarGetImplTypes(var, value);
9914 
9915  for( j = 0; j < nimpls && !memlimitreached; ++j )
9916  {
9917  int probindex;
9918  SCIP_Bool implvalue;
9919 
9920  assert(SCIPvarIsBinary(implvars[j]));
9921  probindex = SCIPvarGetProbindex(implvars[j]);
9922  assert(probindex < nbinvars);
9923 
9924  /* this assert should hold, but if not there is a old continue later on */
9925  assert(probindex >= 0);
9926 
9927  /* consider only implications with active implvar */
9928  if( probindex < 0 )
9929  continue;
9930 
9931  implvalue = (impltypes[j] == SCIP_BOUNDTYPE_UPPER); /* the negation of the implication */
9932 
9933  /* insert the item into the list of the implied variable/value */
9934  if( !zeroiteminserted[implvalue][probindex] )
9935  {
9936  if( firstidxs[implvalue][probindex] == 0 )
9937  {
9938  tmpboolindices2[tmp2] = implvalue;
9939  tmpindices2[tmp2] = probindex;
9940  ++tmp2;
9941  }
9942  SCIP_CALL( insertZerolist(scip, liftcands, nliftcands, firstidxs, zeroweightsums,
9943  &zeroitems, &nextidxs, &zeroitemssize, &nzeroitems, probindex, implvalue, i, weight,
9944  &memlimitreached) );
9945  zeroiteminserted[implvalue][probindex] = TRUE;
9946  tmpboolindices[tmp] = implvalue;
9947  tmpindices[tmp] = probindex;
9948  ++tmp;
9949  }
9950  }
9951 
9952  /* get the cliques where the knapsack item is member of with value 1 */
9953  ncliques = SCIPvarGetNCliques(var, value);
9954  cliques = SCIPvarGetCliques(var, value);
9955  for( j = 0; j < ncliques && !memlimitreached; ++j )
9956  {
9957  SCIP_VAR** cliquevars;
9958  SCIP_Bool* cliquevalues;
9959  int ncliquevars;
9960  int k;
9961 
9962  ncliquevars = SCIPcliqueGetNVars(cliques[j]);
9963 
9964  /* discard big cliques */
9965  if( ncliquevars > MAX_CLIQUELENGTH )
9966  continue;
9967 
9968  cliquevars = SCIPcliqueGetVars(cliques[j]);
9969  cliquevalues = SCIPcliqueGetValues(cliques[j]);
9970 
9971  for( k = ncliquevars - 1; k >= 0; --k )
9972  {
9973  SCIP_Bool implvalue;
9974  int probindex;
9975 
9976  if( var == cliquevars[k] )
9977  continue;
9978 
9979  probindex = SCIPvarGetProbindex(cliquevars[k]);
9980  assert(0 <= probindex && probindex < nbinvars);
9981  implvalue = cliquevalues[k];
9982 
9983  /* insert the item into the list of the clique variable/value */
9984  if( !zeroiteminserted[implvalue][probindex] )
9985  {
9986  if( firstidxs[implvalue][probindex] == 0 )
9987  {
9988  tmpboolindices2[tmp2] = implvalue;
9989  tmpindices2[tmp2] = probindex;
9990  ++tmp2;
9991  }
9992 
9993  SCIP_CALL( insertZerolist(scip, liftcands, nliftcands, firstidxs, zeroweightsums,
9994  &zeroitems, &nextidxs, &zeroitemssize, &nzeroitems, probindex, implvalue, i, weight,
9995  &memlimitreached) );
9996  zeroiteminserted[implvalue][probindex] = TRUE;
9997  tmpboolindices[tmp] = implvalue;
9998  tmpindices[tmp] = probindex;
9999  ++tmp;
10000 
10001  if( memlimitreached )
10002  break;
10003  }
10004  }
10005  }
10006  /* clear zeroiteminserted */
10007  for( --tmp; tmp >= 0; --tmp)
10008  zeroiteminserted[tmpboolindices[tmp]][tmpindices[tmp]] = FALSE;
10009  }
10010  SCIPfreeBufferArray(scip, &tmpboolindices);
10011 
10012  /* calculate the clique partition and the maximal sum of weights using the clique information */
10013  assert(consdata->sorted);
10014  SCIP_CALL( calcCliquepartition(scip, consdata, TRUE, FALSE) );
10015 
10016  assert(conshdlrdata->bools3size > 0);
10017 
10018  /* next if condition should normally not be true, because it means that presolving has created more binary variables
10019  * in one constraint than binary + integer variables existed in the whole problem at the presolving initialization
10020  * method, but for example if you would transform all integers into their binary representation then it maybe happens
10021  */
10022  if( conshdlrdata->bools3size < consdata->nvars )
10023  {
10024  int oldsize;
10025  oldsize = conshdlrdata->bools3size;
10026 
10027  while( conshdlrdata->bools3size < consdata->nvars )
10028  conshdlrdata->bools3size *= 2;
10029  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->bools3, conshdlrdata->bools3size) );
10030  BMSclearMemoryArray(&(conshdlrdata->bools3[oldsize]), conshdlrdata->bools3size - oldsize); /*lint !e866*/
10031  }
10032 
10033  cliqueused = conshdlrdata->bools3;
10034 
10035  /* check for cleared array, all entries are zero */
10036 #ifndef NDEBUG
10037  for( tmp = consdata->nvars - 1; tmp >= 0; --tmp )
10038  assert(cliqueused[tmp] == 0);
10039 #endif
10040 
10041  maxcliqueweightsum = 0;
10042  tmp = 0;
10043 
10044  /* calculates maximal weight of cliques */
10045  for( i = 0; i < consdata->nvars; ++i )
10046  {
10047  cliquenum = consdata->cliquepartition[i];
10048  assert(0 <= cliquenum && cliquenum < consdata->nvars);
10049 
10050  if( !cliqueused[cliquenum] )
10051  {
10052  maxcliqueweightsum += consdata->weights[i];
10053  cliqueused[cliquenum] = TRUE;
10054  tmpindices[tmp] = cliquenum;
10055  ++tmp;
10056  }
10057  }
10058  /* clear cliqueused */
10059  for( --tmp; tmp >= 0; --tmp)
10060  cliqueused[tmp] = FALSE;
10061 
10062  assert(conshdlrdata->bools4size > 0);
10063 
10064  /* next if condition should normally not be true, because it means that presolving has created more binary variables
10065  * in one constraint than binary + integer variables existed in the whole problem at the presolving initialization
10066  * method, but for example if you would transform all integers into their binary representation then it maybe happens
10067  */
10068  if( conshdlrdata->bools4size < consdata->nvars )
10069  {
10070  int oldsize;
10071  oldsize = conshdlrdata->bools4size;
10072 
10073  while( conshdlrdata->bools4size < consdata->nvars )
10074  conshdlrdata->bools4size *= 2;
10075  SCIP_CALL( SCIPreallocMemoryArray(scip, &conshdlrdata->bools4, conshdlrdata->bools4size) );
10076  BMSclearMemoryArray(&conshdlrdata->bools4[oldsize], conshdlrdata->bools4size - oldsize); /*lint !e866*/
10077  }
10078 
10079  itemremoved = conshdlrdata->bools4;
10080 
10081  /* check for cleared array, all entries are zero */
10082 #ifndef NDEBUG
10083  for( tmp = consdata->nvars - 1; tmp >= 0; --tmp )
10084  assert(itemremoved[tmp] == 0);
10085 #endif
10086 
10087  /* for each binary variable xi and each fixing v, calculate the cliqueweightsum and update the weight of the
10088  * variable in the knapsack (this is sequence-dependent because the new or modified weights have to be
10089  * included in subsequent cliqueweightsum calculations)
10090  */
10091  SCIP_CALL( SCIPallocBufferArray(scip, &addvars, 2*nbinvars) );
10092  SCIP_CALL( SCIPallocBufferArray(scip, &addweights, 2*nbinvars) );
10093  naddvars = 0;
10094  addweightsum = 0;
10095  for( val = 0; val < 2 && addweightsum < consdata->capacity; ++val )
10096  {
10097  for( i = 0; i < nliftcands[val] && addweightsum < consdata->capacity; ++i )
10098  {
10099  SCIP_Longint cliqueweightsum;
10100  int probindex;
10101  int idx;
10102  int j;
10103 
10104  tmp = 0;
10105 
10106  probindex = liftcands[val][i];
10107  assert(0 <= probindex && probindex < nbinvars);
10108 
10109  /* ignore empty zero lists and variables that cannot be lifted anyways */
10110  if( firstidxs[val][probindex] == 0
10111  || maxcliqueweightsum - zeroweightsums[val][probindex] + addweightsum >= consdata->capacity )
10112  continue;
10113 
10114  /* mark the items that are implied to zero by setting the current variable to the current value */
10115  for( idx = firstidxs[val][probindex]; idx != 0; idx = nextidxs[idx] )
10116  {
10117  assert(0 < idx && idx < nzeroitems);
10118  assert(0 <= zeroitems[idx] && zeroitems[idx] < consdata->nvars);
10119  itemremoved[zeroitems[idx]] = TRUE;
10120  }
10121 
10122  /* calculate the residual cliqueweight sum */
10123  cliqueweightsum = addweightsum; /* the previously added items are single-element cliques */
10124  for( j = 0; j < consdata->nvars; ++j )
10125  {
10126  cliquenum = consdata->cliquepartition[j];
10127  assert(0 <= cliquenum && cliquenum < consdata->nvars);
10128  if( !itemremoved[j] )
10129  {
10130  if( !cliqueused[cliquenum] )
10131  {
10132  cliqueweightsum += consdata->weights[j];
10133  cliqueused[cliquenum] = TRUE;
10134  tmpindices[tmp] = cliquenum;
10135  ++tmp;
10136  }
10137 
10138  if( cliqueweightsum >= consdata->capacity )
10139  break;
10140  }
10141  }
10142 
10143  /* check if the weight of the variable/value can be increased */
10144  if( cliqueweightsum < consdata->capacity )
10145  {
10146  SCIP_VAR* var;
10147  SCIP_Longint weight;
10148 
10149  /* insert the variable (with value TRUE) in the list of additional items */
10150  assert(naddvars < 2*nbinvars);
10151  var = binvars[probindex];
10152  if( val == FALSE )
10153  {
10154  SCIP_CALL( SCIPgetNegatedVar(scip, var, &var) );
10155  }
10156  weight = consdata->capacity - cliqueweightsum;
10157  addvars[naddvars] = var;
10158  addweights[naddvars] = weight;
10159  addweightsum += weight;
10160  naddvars++;
10161 
10162  SCIPdebugMessage("knapsack constraint <%s>: adding lifted item %"SCIP_LONGINT_FORMAT"<%s>\n",
10163  SCIPconsGetName(cons), weight, SCIPvarGetName(var));
10164  }
10165 
10166  /* clear itemremoved */
10167  for( idx = firstidxs[val][probindex]; idx != 0; idx = nextidxs[idx] )
10168  {
10169  assert(0 < idx && idx < nzeroitems);
10170  assert(0 <= zeroitems[idx] && zeroitems[idx] < consdata->nvars);
10171  itemremoved[zeroitems[idx]] = FALSE;
10172  }
10173  /* clear cliqueused */
10174  for( --tmp; tmp >= 0; --tmp)
10175  cliqueused[tmpindices[tmp]] = FALSE;
10176  }
10177  }
10178  SCIPfreeBufferArray(scip, &tmpindices);
10179 
10180  /* clear part of zeroweightsums */
10181  for( --tmp3; tmp3 >= 0; --tmp3)
10182  zeroweightsums[tmpboolindices3[tmp3]][tmpindices3[tmp3]] = 0;
10183 
10184  /* clear rest of zeroweightsums and firstidxs */
10185  for( --tmp2; tmp2 >= 0; --tmp2)
10186  {
10187  zeroweightsums[tmpboolindices2[tmp2]][tmpindices2[tmp2]] = 0;
10188  firstidxs[tmpboolindices2[tmp2]][tmpindices2[tmp2]] = 0;
10189  }
10190 
10191  SCIPfreeBufferArray(scip, &tmpindices2);
10192  SCIPfreeBufferArray(scip, &tmpindices3);
10193  SCIPfreeBufferArray(scip, &tmpboolindices2);
10194  SCIPfreeBufferArray(scip, &tmpboolindices3);
10195 
10196  /* add all additional item weights */
10197  for( i = 0; i < naddvars; ++i )
10198  {
10199  SCIP_CALL( addCoef(scip, cons, addvars[i], addweights[i]) );
10200  }
10201  *nchgcoefs += naddvars;
10202 
10203  if( naddvars > 0 )
10204  {
10205  /* if new items were added, multiple entries of the same variable are possible and we have to clean up the constraint */
10206  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
10207  }
10208 
10209  /* free temporary memory */
10210  SCIPfreeBufferArray(scip, &addweights);
10211  SCIPfreeBufferArray(scip, &addvars);
10212  SCIPfreeBufferArray(scip, &nextidxs);
10213  SCIPfreeBufferArray(scip, &zeroitems);
10214  SCIPfreeBufferArray(scip, &liftcands[1]);
10215  SCIPfreeBufferArray(scip, &liftcands[0]);
10216 
10217  return SCIP_OKAY;
10218 }
10219 
10220 /** tightens item weights and capacity in presolving:
10221  * given a knapsack sum(wi*xi) <= capacity
10222  * (1) let weightsum := sum(wi)
10223  * if weightsum - wi < capacity:
10224  * - not using item i would make the knapsack constraint redundant
10225  * - wi and capacity can be changed to have the same redundancy effect and the same results for
10226  * fixing xi to zero or one, but with a reduced wi and tightened capacity to tighten the LP relaxation
10227  * - change coefficients:
10228  * wi' := weightsum - capacity
10229  * capacity' := capacity - (wi - wi')
10230  * (2) increase weights from front to back(sortation is necessary) if there is no space left for another weight
10231  * - determine the four(can be adjusted) minimal weightsums of the knapsack, i.e. in increasing order
10232  * weights[nvars - 1], weights[nvars - 2], MIN(weights[nvars - 3], weights[nvars - 1] + weights[nvars - 2]),
10233  * MIN(MAX(weights[nvars - 3], weights[nvars - 1] + weights[nvars - 2]), weights[nvars - 4]), note that there
10234  * can be multiple times the same weight, this can be improved
10235  * - check if summing up a minimal weightsum with a big weight exceeds the capacity, then we can increase the big
10236  * weight, to capacity - lastmininmalweightsum, e.g. :
10237  * 19x1 + 15x2 + 10x3 + 5x4 + 5x5 <= 19
10238  * -> minimal weightsums: 5, 5, 10, 10
10239  * -> 15 + 5 > 19 => increase 15 to 19 - 0 = 19
10240  * -> 10 + 10 > 19 => increase 10 to 19 - 5 = 14, resulting in
10241  * 19x1 + 19x2 + 14x3 + 5x4 + 5x5 <= 19
10242  * (3) let W(C) be the maximal weight of clique C,
10243  * cliqueweightsum := sum(W(C))
10244  * if cliqueweightsum - W(C) < capacity:
10245  * - not using any item of C would make the knapsack constraint redundant
10246  * - weights wi, i in C, and capacity can be changed to have the same redundancy effect and the same results for
10247  * fixing xi, i in C, to zero or one, but with a reduced wi and tightened capacity to tighten the LP relaxation
10248  * - change coefficients:
10249  * delta := capacity - (cliqueweightsum - W(C))
10250  * wi' := max(wi - delta, 0)
10251  * capacity' := capacity - delta
10252  * This rule has to add the used cliques in order to ensure they are enforced - otherwise, the reduction might
10253  * introduce infeasible solutions.
10254  * (4) for a clique C let C(xi == v) := C \ {j: xi == v -> xj == 0}),
10255  * let cliqueweightsum(xi == v) := sum(W(C(xi == v)))
10256  * if cliqueweightsum(xi == v) < capacity:
10257  * - fixing variable xi to v would make the knapsack constraint redundant
10258  * - the weight of the variable or its negation (depending on v) can be increased as long as it has the same
10259  * redundancy effect:
10260  * wi' := capacity - cliqueweightsum(xi == v)
10261  * This rule can also be applied to binary variables not in the knapsack!
10262  * (5) if min{w} + wi > capacity:
10263  * - using item i would force to fix other items to zero
10264  * - wi can be increased to the capacity
10265  */
10266 static
10268  SCIP* scip, /**< SCIP data structure */
10269  SCIP_CONS* cons, /**< knapsack constraint */
10270  int* nchgcoefs, /**< pointer to count total number of changed coefficients */
10271  int* nchgsides, /**< pointer to count number of side changes */
10272  int* naddconss, /**< pointer to count number of added constraints */
10273  int* ndelconss, /**< pointer to count number of deleted constraints */
10274  SCIP_Bool* cutoff /**< pointer to store whether the node can be cut off */
10275  )
10276 {
10277  SCIP_CONSHDLRDATA* conshdlrdata;
10278  SCIP_CONSDATA* consdata;
10279  SCIP_Longint* weights;
10280  SCIP_Longint sumcoef;
10281  SCIP_Longint capacity;
10282  SCIP_Longint newweight;
10283  SCIP_Longint maxweight;
10284  SCIP_Longint minweight;
10285  SCIP_Bool sumcoefcase = FALSE;
10286  int startpos;
10287  int backpos;
10288  int nvars;
10289  int pos;
10290  int k;
10291  int i;
10292 
10293  assert(nchgcoefs != NULL);
10294  assert(nchgsides != NULL);
10295  assert(!SCIPconsIsModifiable(cons));
10296 
10297  conshdlrdata = SCIPconshdlrGetData(SCIPconsGetHdlr(cons));
10298  assert(conshdlrdata != NULL);
10299 
10300  consdata = SCIPconsGetData(cons);
10301  assert(consdata != NULL);
10302  assert(consdata->row == NULL); /* we are in presolve, so no LP row exists */
10303  assert(consdata->onesweightsum == 0); /* all fixed variables should have been removed */
10304  assert(consdata->weightsum > consdata->capacity); /* otherwise, the constraint is redundant */
10305  assert(consdata->nvars > 0);
10306 
10307  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
10308  if( *cutoff )
10309  return SCIP_OKAY;
10310 
10311  /* apply rule (1) */
10312  do
10313  {
10314  assert(consdata->merged);
10315 
10316  /* sort items, s.t. the heaviest one is in the first position */
10317  sortItems(consdata);
10318 
10319  for( i = 0; i < consdata->nvars; ++i )
10320  {
10321  SCIP_Longint weight;
10322 
10323  weight = consdata->weights[i];
10324  if( consdata->weightsum - weight < consdata->capacity )
10325  {
10326  newweight = consdata->weightsum - consdata->capacity;
10327  consdataChgWeight(consdata, i, newweight);
10328  consdata->capacity -= (weight - newweight);
10329  (*nchgcoefs)++;
10330  (*nchgsides)++;
10331  assert(!consdata->sorted);
10332  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",
10333  SCIPconsGetName(cons), SCIPvarGetName(consdata->vars[i]), weight, newweight,
10334  consdata->capacity + (weight-newweight), consdata->capacity);
10335  }
10336  else
10337  break;
10338  }
10339  }
10340  while( !consdata->sorted && consdata->weightsum > consdata->capacity );
10341 
10342  /* check for redundancy */
10343  if( consdata->weightsum <= consdata->capacity )
10344  return SCIP_OKAY;
10345 
10346  pos = 0;
10347  while( pos < consdata->nvars && consdata->weights[pos] == consdata->capacity )
10348  ++pos;
10349 
10350  sumcoef = 0;
10351  weights = consdata->weights;
10352  nvars = consdata->nvars;
10353  capacity = consdata->capacity;
10354 
10355  if( pos < nvars && weights[pos] + weights[pos + 1] > capacity )
10356  {
10357  /* further reductions using the next possible coefficient sum
10358  *
10359  * e.g. 19x1 + 15x2 + 10x3 + 5x4 + 5x5 <= 19 <=> 19x1 + 19x2 + 14x3 + 5x4 + 5x5 <= 19
10360  */
10361  /* @todo loop for "k" can be extended, same coefficient when determine next sumcoef can be left out */
10362  for( k = 0; k < 4; ++k )
10363  {
10364  newweight = capacity - sumcoef;
10365 
10366  /* determine next minimal coefficient sum */
10367  switch( k )
10368  {
10369  case 0:
10370  sumcoef = weights[nvars - 1];
10371  backpos = nvars - 1;
10372  break;
10373  case 1:
10374  sumcoef = weights[nvars - 2];
10375  backpos = nvars - 2;
10376  break;
10377  case 2:
10378  if( weights[nvars - 3] < weights[nvars - 1] + weights[nvars - 2] )
10379  {
10380  sumcoefcase = TRUE;
10381  sumcoef = weights[nvars - 3];
10382  backpos = nvars - 3;
10383  }
10384  else
10385  {
10386  sumcoefcase = FALSE;
10387  sumcoef = weights[nvars - 1] + weights[nvars - 2];
10388  backpos = nvars - 2;
10389  }
10390  break;
10391  case 3:
10392  if( sumcoefcase )
10393  {
10394  if( weights[nvars - 4] < weights[nvars - 1] + weights[nvars - 2] )
10395  {
10396  sumcoef = weights[nvars - 4];
10397  backpos = nvars - 4;
10398  }
10399  else
10400  {
10401  sumcoef = weights[nvars - 1] + weights[nvars - 2];
10402  backpos = nvars - 2;
10403  }
10404  }
10405  else
10406  {
10407  sumcoef = weights[nvars - 3];
10408  backpos = nvars - 3;
10409  }
10410  break;
10411  default:
10412  return SCIP_ERROR;
10413  }
10414 
10415  if( backpos <= pos )
10416  break;
10417 
10418  /* tighten next coefficients that, paired with the current small coefficient, exceed the capacity */
10419  maxweight = weights[pos];
10420  startpos = pos;
10421  while( 2 * maxweight > capacity && maxweight + sumcoef > capacity )
10422  {
10423  assert(newweight > weights[pos]);
10424 
10425  SCIPdebugMessage("in constraint <%s> changing weight %lld to %lld\n", SCIPconsGetName(cons), maxweight, newweight);
10426  consdataChgWeight(consdata, pos, newweight);
10427 
10428  ++pos;
10429  assert(pos < nvars);
10430 
10431  maxweight = weights[pos];
10432 
10433  if( backpos <= pos )
10434  break;
10435  }
10436  (*nchgcoefs) += (pos - startpos);
10437 
10438  /* skip unchangable weights */
10439  while( pos < nvars && weights[pos] + sumcoef == capacity )
10440  ++pos;
10441 
10442  /* check special case were there is only one weight left to tighten
10443  *
10444  * e.g. 95x1 + 59x2 + 37x3 + 36x4 <= 95 (37 > 36)
10445  *
10446  * => 95x1 + 59x2 + 59x3 + 36x4 <= 95
10447  *
10448  * 197x1 + 120x2 + 77x3 + 10x4 <= 207 (here we cannot tighten the coefficient further)
10449  */
10450  if( pos + 1 == backpos && weights[pos] > sumcoef &&
10451  ((k == 0) || (k == 1 && weights[nvars - 1] + sumcoef + weights[pos] > capacity)) )
10452  {
10453  newweight = capacity - sumcoef;
10454  assert(newweight > weights[pos]);
10455 
10456  SCIPdebugMessage("in constraint <%s> changing weight %lld to %lld\n", SCIPconsGetName(cons), maxweight, newweight);
10457  consdataChgWeight(consdata, pos, newweight);
10458 
10459  break;
10460  }
10461 
10462  if( backpos <= pos )
10463  break;
10464  }
10465  }
10466 
10467  /* apply rule (2) (don't apply, if the knapsack has too many items for applying this costly method) */
10468  if( conshdlrdata->disaggregation && consdata->nvars - pos <= MAX_USECLIQUES_SIZE && consdata->nvars >= 2 &&
10469  pos > 0 && (SCIP_Longint)consdata->nvars - pos <= consdata->capacity &&
10470  consdata->weights[pos - 1] == consdata->capacity && (pos == consdata->nvars || consdata->weights[pos] == 1) )
10471  {
10472  SCIP_VAR** clqvars;
10473  SCIP_CONS* cliquecons;
10474  char name[SCIP_MAXSTRLEN];
10475  int* clqpart;
10476  int nclqvars;
10477  int nclq;
10478  int len;
10479  int c;
10480  int w;
10481 
10482  assert(!SCIPconsIsDeleted(cons));
10483 
10484  if( pos == consdata->nvars )
10485  {
10486  SCIPdebugMessage("upgrading knapsack constraint <%s> to a set-packing constraint", SCIPconsGetName(cons));
10487 
10488  SCIP_CALL( SCIPcreateConsSetpack(scip, &cliquecons, SCIPconsGetName(cons), pos, consdata->vars,
10492  SCIPconsIsStickingAtNode(cons)) );
10493 
10494  SCIP_CALL( SCIPaddCons(scip, cliquecons) );
10495  SCIP_CALL( SCIPreleaseCons(scip, &cliquecons) );
10496  ++(*naddconss);
10497 
10498  /* delete old constraint */
10499  SCIP_CALL( SCIPdelCons(scip, cons) );
10500  ++(*ndelconss);
10501 
10502  return SCIP_OKAY;
10503  }
10504 
10505  len = consdata->nvars - pos;
10506 
10507  /* allocate temporary memory */
10508  SCIP_CALL( SCIPallocBufferArray(scip, &clqpart, len) );
10509 
10510  /* calculate clique partition */
10511  SCIP_CALL( SCIPcalcCliquePartition(scip, &(consdata->vars[pos]), len, clqpart, &nclq) );
10512  assert(nclq <= len);
10513 
10514 #ifndef NDEBUG
10515  /* clique numbers must be at least as high as the index */
10516  for( w = 0; w < nclq; ++w )
10517  assert(clqpart[w] <= w);
10518 #endif
10519 
10520  SCIPdebugMessage("Disaggregating knapsack constraint <%s> due to clique information.\n", SCIPconsGetName(cons));
10521 
10522  /* allocate temporary memory */
10523  SCIP_CALL( SCIPallocBufferArray(scip, &clqvars, pos + len - nclq + 1) );
10524 
10525  /* copy corresponding variables with big coefficients */
10526  for( w = pos - 1; w >= 0; --w )
10527  clqvars[w] = consdata->vars[w];
10528 
10529  /* create for each clique a set-packing constraint */
10530  for( c = 0; c < nclq; ++c )
10531  {
10532  nclqvars = pos;
10533 
10534  for( w = c; w < len; ++w )
10535  {
10536  if( clqpart[w] == c )
10537  {
10538  assert(nclqvars < pos + len - nclq + 1);
10539  clqvars[nclqvars] = consdata->vars[w + pos];
10540  ++nclqvars;
10541  }
10542  }
10543 
10544  assert(nclqvars > 1);
10545 
10546  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_clq_%"SCIP_LONGINT_FORMAT"_%d", SCIPconsGetName(cons), consdata->capacity, c);
10547  SCIP_CALL( SCIPcreateConsSetpack(scip, &cliquecons, name, nclqvars, clqvars,
10551  SCIPconsIsStickingAtNode(cons)) );
10552  SCIPdebugMessage(" -> adding clique constraint: ");
10553  SCIPdebugPrintCons(scip, cliquecons, NULL);
10554  SCIP_CALL( SCIPaddCons(scip, cliquecons) );
10555  SCIP_CALL( SCIPreleaseCons(scip, &cliquecons) );
10556  ++(*naddconss);
10557  }
10558 
10559  /* delete old constraint */
10560  SCIP_CALL( SCIPdelCons(scip, cons) );
10561  ++(*ndelconss);
10562 
10563  SCIPfreeBufferArray(scip, &clqvars);
10564  SCIPfreeBufferArray(scip, &clqpart);
10565 
10566  return SCIP_OKAY;
10567  }
10568  else if( consdata->nvars <= MAX_USECLIQUES_SIZE || (consdata->cliquepartitioned && consdata->ncliques <= MAX_USECLIQUES_SIZE) )
10569  {
10570  SCIP_Longint* maxcliqueweights;
10571  SCIP_Longint* newweightvals;
10572  int* newweightidxs;
10573  SCIP_Longint cliqueweightsum;
10574 
10575  SCIP_CALL( SCIPallocBufferArray(scip, &maxcliqueweights, consdata->nvars) );
10576  SCIP_CALL( SCIPallocBufferArray(scip, &newweightvals, consdata->nvars) );
10577  SCIP_CALL( SCIPallocBufferArray(scip, &newweightidxs, consdata->nvars) );
10578 
10579  /* repeat as long as changes have been applied */
10580  do
10581  {
10582  int ncliques;
10583  int cliquenum;
10584  SCIP_Bool zeroweights;
10585 
10586  assert(consdata->merged);
10587 
10588  /* sort items, s.t. the heaviest one is in the first position */
10589  sortItems(consdata);
10590 
10591  /* calculate a clique partition */
10592  SCIP_CALL( calcCliquepartition(scip, consdata, TRUE, FALSE) );
10593 
10594  /* if there are only single element cliques, rule (2) is equivalent to rule (1) */
10595  if( consdata->cliquepartition[consdata->nvars - 1] == consdata->nvars - 1 )
10596  break;
10597 
10598  /* calculate the maximal weight of the cliques and store the clique type */
10599  cliqueweightsum = 0;
10600  ncliques = 0;
10601 
10602  for( i = 0; i < consdata->nvars; ++i )
10603  {
10604  SCIP_Longint weight;
10605 
10606  cliquenum = consdata->cliquepartition[i];
10607  assert(0 <= cliquenum && cliquenum <= ncliques);
10608 
10609  weight = consdata->weights[i];
10610  assert(weight > 0);
10611 
10612  if( cliquenum == ncliques )
10613  {
10614  maxcliqueweights[ncliques] = weight;
10615  cliqueweightsum += weight;
10616  ++ncliques;
10617  }
10618 
10619  assert(maxcliqueweights[cliquenum] >= weight);
10620  }
10621 
10622  /* apply rule on every clique */
10623  zeroweights = FALSE;
10624  for( i = 0; i < ncliques; ++i )
10625  {
10626  SCIP_Longint delta;
10627 
10628  delta = consdata->capacity - (cliqueweightsum - maxcliqueweights[i]);
10629  if( delta > 0 )
10630  {
10631  SCIP_Longint newcapacity;
10632 #ifndef NDEBUG
10633  SCIP_Longint newmincliqueweight;
10634 #endif
10635  SCIP_Longint newminweightsuminclique;
10636  SCIP_Bool forceclique;
10637  int nnewweights;
10638  int j;
10639 
10640  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",
10641  SCIPconsGetName(cons), i, maxcliqueweights[i], cliqueweightsum, consdata->capacity, delta);
10642  newcapacity = consdata->capacity - delta;
10643  forceclique = FALSE;
10644  nnewweights = 0;
10645 #ifndef NDEBUG
10646  newmincliqueweight = newcapacity + 1;
10647  for( j = 0; j < i; ++j )
10648  assert(consdata->cliquepartition[j] < i); /* no element j < i can be in clique i */
10649 #endif
10650  for( j = i; j < consdata->nvars; ++j )
10651  {
10652  if( consdata->cliquepartition[j] == i )
10653  {
10654  newweight = consdata->weights[j] - delta;
10655  newweight = MAX(newweight, 0);
10656 
10657  /* cache the new weight */
10658  assert(nnewweights < consdata->nvars);
10659  newweightvals[nnewweights] = newweight;
10660  newweightidxs[nnewweights] = j;
10661  nnewweights++;
10662 
10663 #ifndef NDEBUG
10664  assert(newweight <= newmincliqueweight); /* items are sorted by non-increasing weight! */
10665  newmincliqueweight = newweight;
10666 #endif
10667  }
10668  }
10669 
10670  /* check if our clique information results out of this knapsack constraint and if so check if we would loose the clique information */
10671  if( nnewweights > 1 )
10672  {
10673 #ifndef NDEBUG
10674  j = newweightidxs[nnewweights - 2];
10675  assert(0 <= j && j < consdata->nvars);
10676  assert(consdata->cliquepartition[j] == i);
10677  j = newweightidxs[nnewweights - 1];
10678  assert(0 <= j && j < consdata->nvars);
10679  assert(consdata->cliquepartition[j] == i);
10680 #endif
10681 
10682  newminweightsuminclique = newweightvals[nnewweights - 2];
10683  newminweightsuminclique += newweightvals[nnewweights - 1];
10684 
10685  /* check if these new two minimal weights both fit into the knapsack;
10686  * if this is true, we have to add a clique constraint in order to enforce the clique
10687  * (otherwise, the knapsack might have been one of the reasons for the clique, and the weight
10688  * reduction might be infeasible, i.e., allows additional solutions)
10689  */
10690  if( newminweightsuminclique <= newcapacity )
10691  forceclique = TRUE;
10692  }
10693 
10694  /* check if we really want to apply the change */
10695  if( conshdlrdata->disaggregation || !forceclique )
10696  {
10697  SCIPdebugMessage(" -> change capacity from %"SCIP_LONGINT_FORMAT" to %"SCIP_LONGINT_FORMAT" (forceclique:%u)\n",
10698  consdata->capacity, newcapacity, forceclique);
10699  consdata->capacity = newcapacity;
10700  (*nchgsides)++;
10701 
10702  for( k = 0; k < nnewweights; ++k )
10703  {
10704  j = newweightidxs[k];
10705  assert(0 <= j && j < consdata->nvars);
10706  assert(consdata->cliquepartition[j] == i);
10707 
10708  /* apply the weight change */
10709  SCIPdebugMessage(" -> change weight of <%s> from %"SCIP_LONGINT_FORMAT" to %"SCIP_LONGINT_FORMAT"\n",
10710  SCIPvarGetName(consdata->vars[j]), consdata->weights[j], newweightvals[k]);
10711  consdataChgWeight(consdata, j, newweightvals[k]);
10712  (*nchgcoefs)++;
10713  assert(!consdata->sorted);
10714  zeroweights = zeroweights || (newweightvals[k] == 0);
10715  }
10716  /* if before the weight update at least one pair of weights did not fit into the knapsack and now fits,
10717  * we have to make sure, the clique is enforced - the clique might have been constructed partially from
10718  * this constraint, and by reducing the weights, this clique information is not contained anymore in the
10719  * knapsack constraint
10720  */
10721  if( forceclique )
10722  {
10723  SCIP_CONS* cliquecons;
10724  char name[SCIP_MAXSTRLEN];
10725  SCIP_VAR** cliquevars;
10726 
10727  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevars, nnewweights) );
10728  for( k = 0; k < nnewweights; ++k )
10729  cliquevars[k] = consdata->vars[newweightidxs[k]];
10730 
10731  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_clq_%"SCIP_LONGINT_FORMAT"_%d", SCIPconsGetName(cons), consdata->capacity, i);
10732  SCIP_CALL( SCIPcreateConsSetpack(scip, &cliquecons, name, nnewweights, cliquevars,
10736  SCIPconsIsStickingAtNode(cons)) );
10737  SCIPdebugMessage(" -> adding clique constraint: ");
10738  SCIPdebugPrintCons(scip, cliquecons, NULL);
10739  SCIP_CALL( SCIPaddCons(scip, cliquecons) );
10740  SCIP_CALL( SCIPreleaseCons(scip, &cliquecons) );
10741  SCIPfreeBufferArray(scip, &cliquevars);
10742  (*naddconss)++;
10743  }
10744  }
10745  }
10746  }
10747  if( zeroweights )
10748  {
10749  SCIP_CALL( removeZeroWeights(scip, cons) );
10750  }
10751  }
10752  while( !consdata->sorted && consdata->weightsum > consdata->capacity );
10753 
10754  /* free temporary memory */
10755  SCIPfreeBufferArray(scip, &newweightidxs);
10756  SCIPfreeBufferArray(scip, &newweightvals);
10757  SCIPfreeBufferArray(scip, &maxcliqueweights);
10758 
10759  /* check for redundancy */
10760  if( consdata->weightsum <= consdata->capacity )
10761  return SCIP_OKAY;
10762  }
10763 
10764  /* apply rule (3) */
10765  SCIP_CALL( tightenWeightsLift(scip, cons, nchgcoefs, cutoff) );
10766 
10767  /* check for redundancy */
10768  if( consdata->weightsum <= consdata->capacity )
10769  return SCIP_OKAY;
10770 
10771  /* apply rule (4) (all but smallest weight) */
10772  assert(consdata->merged);
10773  sortItems(consdata);
10774  minweight = consdata->weights[consdata->nvars-1];
10775  for( i = 0; i < consdata->nvars-1; ++i )
10776  {
10777  SCIP_Longint weight;
10778 
10779  weight = consdata->weights[i];
10780  assert(weight >= minweight);
10781  if( minweight + weight > consdata->capacity )
10782  {
10783  if( weight < consdata->capacity )
10784  {
10785  SCIPdebugMessage("knapsack constraint <%s>: changed weight of <%s> from %"SCIP_LONGINT_FORMAT" to %"SCIP_LONGINT_FORMAT"\n",
10786  SCIPconsGetName(cons), SCIPvarGetName(consdata->vars[i]), weight, consdata->capacity);
10787  assert(consdata->sorted);
10788  consdataChgWeight(consdata, i, consdata->capacity); /* this does not destroy the weight order! */
10789  assert(i == 0 || consdata->weights[i-1] >= consdata->weights[i]);
10790  consdata->sorted = TRUE;
10791  (*nchgcoefs)++;
10792  }
10793  }
10794  else
10795  break;
10796  }
10797 
10798  /* apply rule (4) (smallest weight) */
10799  if( consdata->nvars >= 2 )
10800  {
10801  SCIP_Longint weight;
10802 
10803  minweight = consdata->weights[consdata->nvars-2];
10804  weight = consdata->weights[consdata->nvars-1];
10805  assert(minweight >= weight);
10806  if( minweight + weight > consdata->capacity && weight < consdata->capacity )
10807  {
10808  SCIPdebugMessage("knapsack constraint <%s>: changed weight of <%s> from %"SCIP_LONGINT_FORMAT" to %"SCIP_LONGINT_FORMAT"\n",
10809  SCIPconsGetName(cons), SCIPvarGetName(consdata->vars[consdata->nvars-1]), weight, consdata->capacity);
10810  assert(consdata->sorted);
10811  consdataChgWeight(consdata, consdata->nvars-1, consdata->capacity); /* this does not destroy the weight order! */
10812  assert(minweight >= consdata->weights[consdata->nvars-1]);
10813  consdata->sorted = TRUE;
10814  (*nchgcoefs)++;
10815  }
10816  }
10817 
10818  return SCIP_OKAY;
10819 }
10820 
10821 /** adds negated cliques of the knapsack constraint to the global clique table */
10822 static
10824  SCIP*const scip, /**< SCIP data structure */
10825  SCIP_CONS*const cons, /**< knapsack constraint */
10826  SCIP_Bool*const cutoff, /**< pointer to store whether the node can be cut off */
10827  int*const nbdchgs /**< pointer to count the number of performed bound changes */
10828  )
10829 {
10830  SCIP_CONSDATA* consdata;
10831  SCIP_VAR** poscliquevars;
10832  SCIP_VAR** cliquevars;
10833  SCIP_Longint* maxweights;
10834  SCIP_Longint* gainweights;
10835  int* gaincliquepartition;
10836  SCIP_Bool* cliqueused;
10837  SCIP_Longint minactduetonegcliques;
10838  SCIP_Longint freecapacity;
10839  SCIP_Longint lastweight;
10840  SCIP_Longint beforelastweight;
10841  int nposcliquevars;
10842  int ncliquevars;
10843  int nvars;
10844  int nnegcliques;
10845  int lastcliqueused;
10846  int thisnbdchgs;
10847  int v;
10848  int w;
10849 
10850  assert(scip != NULL);
10851  assert(cons != NULL);
10852  assert(cutoff != NULL);
10853  assert(nbdchgs != NULL);
10854 
10855  *cutoff = FALSE;
10856 
10857  consdata = SCIPconsGetData(cons);
10858  assert(consdata != NULL);
10859 
10860  nvars = consdata->nvars;
10861 
10862  /* check whether the cliques have already been added */
10863  if( consdata->cliquesadded || nvars == 0 )
10864  return SCIP_OKAY;
10865 
10866  /* make sure, the items are merged */
10867  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
10868  if( *cutoff )
10869  return SCIP_OKAY;
10870 
10871  /* make sure, items are sorted by non-increasing weight */
10872  sortItems(consdata);
10873 
10874  assert(consdata->merged);
10875 
10876  /* calculate a clique partition */
10877  SCIP_CALL( calcCliquepartition(scip, consdata, FALSE, TRUE) );
10878  nnegcliques = consdata->nnegcliques;
10879 
10880  /* if we have no negated cliques, stop */
10881  if( nnegcliques == nvars )
10882  return SCIP_OKAY;
10883 
10884  /* get temporary memory */
10885  SCIP_CALL( SCIPallocBufferArray(scip, &poscliquevars, nvars) );
10886  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevars, nvars) );
10887  SCIP_CALL( SCIPallocBufferArray(scip, &gainweights, nvars) );
10888  BMSclearMemoryArray(gainweights, nvars);
10889  SCIP_CALL( SCIPallocBufferArray(scip, &gaincliquepartition, nvars) );
10890  SCIP_CALL( SCIPallocBufferArray(scip, &maxweights, nnegcliques) );
10891  SCIP_CALL( SCIPallocBufferArray(scip, &cliqueused, nnegcliques) );
10892  BMSclearMemoryArray(cliqueused, nnegcliques);
10893 
10894  nnegcliques = 0;
10895  minactduetonegcliques = 0;
10896 
10897  /* determine maximal weights for all negated cliques and calculate minimal weightsum due to negated cliques */
10898  for( v = 0; v < nvars; ++v )
10899  {
10900  assert(0 <= consdata->negcliquepartition[v] && consdata->negcliquepartition[v] <= nnegcliques);
10901  assert(consdata->weights[v] > 0);
10902 
10903  if( consdata->negcliquepartition[v] == nnegcliques )
10904  {
10905  nnegcliques++;
10906  maxweights[consdata->negcliquepartition[v]] = consdata->weights[v];
10907  }
10908  else
10909  minactduetonegcliques += consdata->weights[v];
10910  }
10911 
10912  nposcliquevars = 0;
10913 
10914  /* add cliques, using negated cliques information */
10915  if( minactduetonegcliques > 0 )
10916  {
10917  /* free capacity is the rest of not used capacity if the smallest amount of weights due to negated cliques are used */
10918  freecapacity = consdata->capacity - minactduetonegcliques;
10919 
10920  SCIPdebugPrintCons(scip, cons, NULL);
10921  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",
10922  SCIPconsGetName(cons), consdata->capacity, minactduetonegcliques, freecapacity);
10923 
10924  /* calculate possible gain by switching chosen items in negated cliques */
10925  for( v = 0; v < nvars; ++v )
10926  {
10927  if( !cliqueused[consdata->negcliquepartition[v]] )
10928  {
10929  cliqueused[consdata->negcliquepartition[v]] = TRUE;
10930  for( w = v + 1; w < nvars; ++w )
10931  {
10932  /* if we would take the biggest weight instead of another what would we gain, take weight[i] instead of
10933  * weight[j] (which are both in a negated clique) */
10934  if( consdata->negcliquepartition[v] == consdata->negcliquepartition[w]
10935  && consdata->weights[v] > consdata->weights[w] )
10936  {
10937  poscliquevars[nposcliquevars] = consdata->vars[w];
10938  gainweights[nposcliquevars] = maxweights[consdata->negcliquepartition[v]] - consdata->weights[w];
10939  gaincliquepartition[nposcliquevars] = consdata->negcliquepartition[v];
10940  ++nposcliquevars;
10941  }
10942  }
10943  }
10944  }
10945 
10946  /* try to create negated cliques */
10947  if( nposcliquevars > 0 )
10948  {
10949  /* sort possible gain per substitution of the clique members */
10950  SCIPsortDownLongPtrInt(gainweights,(void**) poscliquevars, gaincliquepartition, nposcliquevars);
10951 
10952  for( v = 0; v < nposcliquevars; ++v )
10953  {
10954  SCIP_CALL( SCIPgetNegatedVar(scip, poscliquevars[v], &cliquevars[0]) );
10955  ncliquevars = 1;
10956  lastweight = gainweights[v];
10957  beforelastweight = -1;
10958  lastcliqueused = gaincliquepartition[v];
10959  /* clear cliqueused to get an unused array */
10960  BMSclearMemoryArray(cliqueused, nnegcliques);
10961  cliqueused[gaincliquepartition[v]] = TRUE;
10962 
10963  /* taking bigger weights make the knapsack redundant so we will create cliques, only take items which are not
10964  * in the same negated clique and by taking two of them would exceed the free capacity */
10965  for( w = v + 1; w < nposcliquevars && !cliqueused[gaincliquepartition[w]] && gainweights[w] + lastweight > freecapacity; ++w )
10966  {
10967  beforelastweight = lastweight;
10968  lastweight = gainweights[w];
10969  lastcliqueused = gaincliquepartition[w];
10970  cliqueused[gaincliquepartition[w]] = TRUE;
10971  SCIP_CALL( SCIPgetNegatedVar(scip, poscliquevars[w], &cliquevars[ncliquevars]) );
10972  ++ncliquevars;
10973  }
10974 
10975  if( ncliquevars > 1 )
10976  {
10977 #ifdef SCIP_DEBUG
10978  int b;
10979  SCIPdebugMessage("adding new Clique: ");
10980  for( b = 0; b < ncliquevars; ++b )
10981  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
10982  SCIPdebugPrintf("\n");
10983 #endif
10984 
10985  assert(beforelastweight > 0);
10986  /* add the clique to the clique table */
10987  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
10988  if( *cutoff )
10989  goto TERMINATE;
10990  *nbdchgs += thisnbdchgs;
10991 
10992  /* reset last used clique to get slightly different cliques */
10993  cliqueused[lastcliqueused] = FALSE;
10994 
10995  /* try to replace the last item in the clique by a different item to obtain a slightly different clique */
10996  for( ++w; w < nposcliquevars && !cliqueused[gaincliquepartition[w]] && beforelastweight + gainweights[w] > freecapacity; ++w )
10997  {
10998  SCIP_CALL( SCIPgetNegatedVar(scip, poscliquevars[w], &cliquevars[ncliquevars - 1]) );
10999 #ifdef SCIP_DEBUG
11000  {
11001  SCIPdebugMessage("adding new Clique: ");
11002  for( b = 0; b < ncliquevars; ++b )
11003  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
11004  SCIPdebugPrintf("\n");
11005  }
11006 #endif
11007  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
11008  if( *cutoff )
11009  goto TERMINATE;
11010  *nbdchgs += thisnbdchgs;
11011  }
11012  }
11013  }
11014  }
11015  }
11016 
11017  TERMINATE:
11018  /* free temporary memory */
11019  SCIPfreeBufferArray(scip, &cliqueused);
11020  SCIPfreeBufferArray(scip, &gaincliquepartition);
11021  SCIPfreeBufferArray(scip, &maxweights);
11022  SCIPfreeBufferArray(scip, &gainweights);
11023  SCIPfreeBufferArray(scip, &cliquevars);
11024  SCIPfreeBufferArray(scip, &poscliquevars);
11025 
11026  return SCIP_OKAY;
11027 }
11028 
11029 /** adds cliques of the knapsack constraint to the global clique table */
11030 static
11032  SCIP*const scip, /**< SCIP data structure */
11033  SCIP_CONS*const cons, /**< knapsack constraint */
11034  SCIP_Bool*const cutoff, /**< pointer to store whether the node can be cut off */
11035  int*const nbdchgs /**< pointer to count the number of performed bound changes */
11036  )
11037 {
11038  SCIP_CONSDATA* consdata;
11039  SCIP_VAR** cliquevars;
11040  int ncliquevars;
11041  int i;
11042  SCIP_Longint minactduetonegcliques;
11043  SCIP_Longint freecapacity;
11044  SCIP_Longint lastweight;
11045  SCIP_Longint beforelastweight;
11046  int nnegcliques;
11047  int cliquenum;
11048  int thisnbdchgs;
11049  SCIP_VAR** poscliquevars;
11050  SCIP_Longint* gainweights;
11051  int nposcliquevars;
11052  SCIP_Longint* secondmaxweights;
11053  int nvars;
11054 
11055  assert(scip != NULL);
11056  assert(cons != NULL);
11057  assert(cutoff != NULL);
11058  assert(nbdchgs != NULL);
11059 
11060  *cutoff = FALSE;
11061 
11062  consdata = SCIPconsGetData(cons);
11063  assert(consdata != NULL);
11064 
11065  nvars = consdata->nvars;
11066 
11067  /* check whether the cliques have already been added */
11068  if( consdata->cliquesadded || nvars == 0 )
11069  return SCIP_OKAY;
11070 
11071  /* make sure, the items are merged */
11072  SCIP_CALL( mergeMultiples(scip, cons, cutoff) );
11073  if( *cutoff )
11074  return SCIP_OKAY;
11075 
11076  /* make sure, the items are sorted by non-increasing weight */
11077  sortItems(consdata);
11078 
11079  assert(consdata->merged);
11080 
11081  /* calculate a clique partition */
11082  SCIP_CALL( calcCliquepartition(scip, consdata, FALSE, TRUE) );
11083  nnegcliques = consdata->nnegcliques;
11084  assert(nnegcliques <= nvars);
11085 
11086  /* get temporary memory */
11087  SCIP_CALL( SCIPallocBufferArray(scip, &poscliquevars, nvars) );
11088  SCIP_CALL( SCIPallocBufferArray(scip, &cliquevars, nvars) );
11089  SCIP_CALL( SCIPallocBufferArray(scip, &gainweights, nvars) );
11090  BMSclearMemoryArray(gainweights, nvars);
11091  SCIP_CALL( SCIPallocBufferArray(scip, &secondmaxweights, nnegcliques) );
11092  BMSclearMemoryArray(secondmaxweights, nnegcliques);
11093 
11094  minactduetonegcliques = 0;
11095 
11096  /* calculate minimal activity due to negated cliques, and determine second maximal weight in each clique */
11097  if( nnegcliques < nvars )
11098  {
11099  nnegcliques = 0;
11100 
11101  for( i = 0; i < nvars; ++i )
11102  {
11103  SCIP_Longint weight;
11104 
11105  cliquenum = consdata->negcliquepartition[i];
11106  assert(0 <= cliquenum && cliquenum <= nnegcliques);
11107 
11108  weight = consdata->weights[i];
11109  assert(weight > 0);
11110 
11111  if( cliquenum == nnegcliques )
11112  nnegcliques++;
11113  else
11114  {
11115  minactduetonegcliques += weight;
11116  if( secondmaxweights[cliquenum] == 0 )
11117  secondmaxweights[cliquenum] = weight;
11118  }
11119  }
11120  }
11121 
11122  /* add cliques, using negated cliques information */
11123  if( minactduetonegcliques > 0 )
11124  {
11125  /* free capacity is the rest of not used capacity if the smallest amount of weights due to negated cliques are used */
11126  freecapacity = consdata->capacity - minactduetonegcliques;
11127 
11128  SCIPdebugPrintCons(scip, cons, NULL);
11129  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",
11130  SCIPconsGetName(cons), consdata->capacity, minactduetonegcliques, freecapacity);
11131 
11132  /* create negated cliques out of negated cliques, if we do not take the smallest weight of a cliques ... */
11133  SCIP_CALL( addNegatedCliques(scip, cons, cutoff, nbdchgs ) );
11134 
11135  nposcliquevars = 0;
11136 
11137  for( i = nvars - 1; i >= 0; --i )
11138  {
11139  /* if we would take the biggest weight instead of the second biggest */
11140  cliquenum = consdata->negcliquepartition[i];
11141  if( consdata->weights[i] > secondmaxweights[cliquenum] )
11142  {
11143  poscliquevars[nposcliquevars] = consdata->vars[i];
11144  gainweights[nposcliquevars] = consdata->weights[i] - secondmaxweights[cliquenum];
11145  ++nposcliquevars;
11146  }
11147  }
11148 
11149  if( nposcliquevars > 0 )
11150  {
11151  /* sort possible gain per substitution of the clique members */
11152  SCIPsortDownLongPtr(gainweights,(void**) poscliquevars, nposcliquevars);
11153 
11154  cliquevars[0] = poscliquevars[0];
11155  ncliquevars = 1;
11156  lastweight = gainweights[0];
11157  beforelastweight = 0;
11158 
11159  /* taking bigger weights make the knapsack redundant so we will create cliques */
11160  for( i = 1; i < nposcliquevars && gainweights[i] + lastweight > freecapacity; ++i )
11161  {
11162  beforelastweight = lastweight;
11163  lastweight = gainweights[i];
11164  cliquevars[ncliquevars] = poscliquevars[i];
11165  ++ncliquevars;
11166  }
11167 
11168  if( ncliquevars > 1 )
11169  {
11170 #ifdef SCIP_DEBUG
11171  int b;
11172  SCIPdebugMessage("adding new Clique: ");
11173  for( b = 0; b < ncliquevars; ++b )
11174  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
11175  SCIPdebugPrintf("\n");
11176 #endif
11177 
11178  assert(beforelastweight > 0);
11179  /* add the clique to the clique table */
11180  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
11181  if( *cutoff )
11182  goto TERMINATE;
11183  *nbdchgs += thisnbdchgs;
11184 
11185  /* try to replace the last item in the clique by a different item to obtain a slightly different clique */
11186  for( ++i; i < nposcliquevars && beforelastweight + gainweights[i] > freecapacity; ++i )
11187  {
11188  cliquevars[ncliquevars - 1] = poscliquevars[i];
11189 #ifdef SCIP_DEBUG
11190  {
11191  SCIPdebugMessage("adding new Clique: ");
11192  for( b = 0; b < ncliquevars; ++b )
11193  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
11194  SCIPdebugPrintf("\n");
11195  }
11196 #endif
11197  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
11198  if( *cutoff )
11199  goto TERMINATE;
11200  *nbdchgs += thisnbdchgs;
11201  }
11202  }
11203  }
11204  }
11205 
11206  /* build a largest clique by using the items with the maximal weights */
11207  cliquevars[0] = consdata->vars[0];
11208  for( i = 1; i < nvars && consdata->weights[i-1] + consdata->weights[i] > consdata->capacity; ++i )
11209  cliquevars[i] = consdata->vars[i];
11210 
11211  ncliquevars = i;
11212 
11213  if( ncliquevars >= 2 )
11214  {
11215  SCIP_Longint cliqueminweight;
11216 #ifdef SCIP_DEBUG
11217  int b;
11218  SCIPdebugMessage("adding new Clique: ");
11219  for( b = 0; b < ncliquevars; ++b )
11220  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
11221  SCIPdebugPrintf("\n");
11222 #endif
11223 
11224  /* add the clique to the clique table */
11225  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
11226  if( *cutoff )
11227  goto TERMINATE;
11228  *nbdchgs += thisnbdchgs;
11229 
11230  /* try to replace the last item in the clique by a different item to obtain a slightly different clique */
11231  cliqueminweight = consdata->weights[ncliquevars-2];
11232  for( i = ncliquevars; i < nvars && cliqueminweight + consdata->weights[i] > consdata->capacity; ++i )
11233  {
11234  cliquevars[ncliquevars - 1] = consdata->vars[i];
11235 #ifdef SCIP_DEBUG
11236  {
11237  SCIPdebugMessage("adding new Clique: ");
11238  for( b = 0; b < ncliquevars; ++b )
11239  SCIPdebugPrintf("%s ", SCIPvarGetName(cliquevars[b]));
11240  SCIPdebugPrintf("\n");
11241  }
11242 #endif
11243  SCIP_CALL( SCIPaddClique(scip, cliquevars, NULL, ncliquevars, cutoff, &thisnbdchgs) );
11244  if( *cutoff )
11245  goto TERMINATE;
11246  *nbdchgs += thisnbdchgs;
11247  }
11248  }
11249 
11250  TERMINATE:
11251  /* free temporary memory and mark the constraint */
11252  SCIPfreeBufferArray(scip, &secondmaxweights);
11253  SCIPfreeBufferArray(scip, &gainweights);
11254  SCIPfreeBufferArray(scip, &cliquevars);
11255  SCIPfreeBufferArray(scip, &poscliquevars);
11256  consdata->cliquesadded = TRUE;
11257 
11258  return SCIP_OKAY;
11259 }
11260 
11261 
11262 /** gets the key of the given element */
11263 static
11264 SCIP_DECL_HASHGETKEY(hashGetKeyKnapsackcons)
11265 { /*lint --e{715}*/
11266  /* the key is the element itself */
11267  return elem;
11268 }
11269 
11270 /** returns TRUE iff both keys are equal; two constraints are equal if they have the same variables and the
11271  * same coefficients
11272  */
11273 static
11274 SCIP_DECL_HASHKEYEQ(hashKeyEqKnapsackcons)
11275 {
11276 #ifndef NDEBUG
11277  SCIP* scip;
11278 #endif
11279  SCIP_CONSDATA* consdata1;
11280  SCIP_CONSDATA* consdata2;
11281  int i;
11282 
11283  consdata1 = SCIPconsGetData((SCIP_CONS*)key1);
11284  consdata2 = SCIPconsGetData((SCIP_CONS*)key2);
11285  assert(consdata1->sorted);
11286  assert(consdata2->sorted);
11287 #ifndef NDEBUG
11288  scip = (SCIP*)userptr;
11289  assert(scip != NULL);
11290 #endif
11291 
11292  /* checks trivial case */
11293  if( consdata1->nvars != consdata2->nvars )
11294  return FALSE;
11295 
11296  for( i = consdata1->nvars - 1; i >= 0; --i )
11297  {
11298  /* tests if variables are equal */
11299  if( consdata1->vars[i] != consdata2->vars[i] )
11300  {
11301  assert(SCIPvarCompare(consdata1->vars[i], consdata2->vars[i]) == 1 ||
11302  SCIPvarCompare(consdata1->vars[i], consdata2->vars[i]) == -1);
11303  return FALSE;
11304  }
11305  assert(SCIPvarCompare(consdata1->vars[i], consdata2->vars[i]) == 0);
11306 
11307  /* tests if weights are equal too */
11308  if( consdata1->weights[i] != consdata2->weights[i] )
11309  return FALSE;
11310  }
11311 
11312  return TRUE;
11313 }
11314 
11315 /** returns the hash value of the key */
11316 static
11317 SCIP_DECL_HASHKEYVAL(hashKeyValKnapsackcons)
11318 {
11319 #ifndef NDEBUG
11320  SCIP* scip;
11321 #endif
11322  SCIP_CONSDATA* consdata;
11323  unsigned int hashval;
11324  int minidx;
11325  int mididx;
11326  int maxidx;
11327  int maxabsval;
11328 
11329  consdata = SCIPconsGetData((SCIP_CONS*)key);
11330  assert(consdata != NULL);
11331  assert(consdata->nvars > 0);
11332 
11333 #ifndef NDEBUG
11334  scip = (SCIP*)userptr;
11335  assert(scip != NULL);
11336 #endif
11337 
11338  /* sorts the constraints */
11339  sortItems(consdata);
11340 
11341  minidx = SCIPvarGetIndex(consdata->vars[0]);
11342  mididx = SCIPvarGetIndex(consdata->vars[consdata->nvars / 2]);
11343  maxidx = SCIPvarGetIndex(consdata->vars[consdata->nvars - 1]);
11344  assert(minidx >= 0 && mididx >= 0 && maxidx >= 0);
11345 
11346  if( consdata->weights[0] > INT_MAX )
11347  maxabsval = 0;
11348  else
11349  maxabsval = (int) consdata->weights[0];
11350 
11351  /* hash value depends on vectors of variable indices */
11352  hashval = (consdata->nvars << 29) + (minidx << 22) + (mididx << 11) + maxidx + maxabsval; /*lint !e701*/
11353 
11354  return hashval;
11355 }
11356 
11357 /** compares each constraint with all other constraints for possible redundancy and removes or changes constraint
11358  * accordingly; in contrast to preprocessConstraintPairs(), it uses a hash table
11359  */
11360 static
11362  SCIP* scip, /**< SCIP data structure */
11363  BMS_BLKMEM* blkmem, /**< block memory */
11364  SCIP_CONS** conss, /**< constraint set */
11365  int nconss, /**< number of constraints in constraint set */
11366  SCIP_Bool* cutoff, /**< pointer to store whether the problem is infeasible */
11367  int* ndelconss /**< pointer to count number of deleted constraints */
11368  )
11369 {
11370  SCIP_HASHTABLE* hashtable;
11371  int hashtablesize;
11372  int c;
11373 
11374  assert(scip != NULL);
11375  assert(blkmem != NULL);
11376  assert(conss != NULL);
11377  assert(ndelconss != NULL);
11378 
11379  /* create a hash table for the constraint set */
11380  hashtablesize = SCIPcalcHashtableSize(10*nconss);
11381  hashtablesize = MAX(hashtablesize, HASHSIZE_KNAPSACKCONS);
11382  SCIP_CALL( SCIPhashtableCreate(&hashtable, blkmem, hashtablesize,
11383  hashGetKeyKnapsackcons, hashKeyEqKnapsackcons, hashKeyValKnapsackcons, (void*) scip) );
11384 
11385  /* check all constraints in the given set for redundancy */
11386  for( c = nconss - 1; c >= 0; --c )
11387  {
11388  SCIP_CONS* cons0;
11389  SCIP_CONS* cons1;
11390  SCIP_CONSDATA* consdata0;
11391 
11392  cons0 = conss[c];
11393 
11394  if( !SCIPconsIsActive(cons0) || SCIPconsIsModifiable(cons0) )
11395  continue;
11396 
11397  consdata0 = SCIPconsGetData(cons0);
11398  assert(consdata0 != NULL);
11399  if( consdata0->nvars == 0 )
11400  {
11401  if( consdata0->capacity < 0 )
11402  {
11403  *cutoff = TRUE;
11404  return SCIP_OKAY;
11405  }
11406  else
11407  {
11408  SCIP_CALL( SCIPdelCons(scip, cons0) );
11409  ++(*ndelconss);
11410  continue;
11411  }
11412  }
11413 
11414  /* get constraint from current hash table with same variables and same weights as cons0 */
11415  cons1 = (SCIP_CONS*)(SCIPhashtableRetrieve(hashtable, (void*)cons0));
11416 
11417  if( cons1 != NULL )
11418  {
11419  SCIP_CONS* consstay;
11420  SCIP_CONS* consdel;
11421  SCIP_CONSDATA* consdata1;
11422 
11423  assert(SCIPconsIsActive(cons1));
11424  assert(!SCIPconsIsModifiable(cons1));
11425 
11426  /* constraint found: create a new constraint with same coefficients and best left and right hand side;
11427  * delete old constraints afterwards
11428  */
11429  consdata1 = SCIPconsGetData(cons1);
11430 
11431  assert(consdata1 != NULL);
11432  assert(consdata0->nvars > 0 && consdata0->nvars == consdata1->nvars);
11433 
11434  assert(consdata0->sorted && consdata1->sorted);
11435  assert(consdata0->vars[0] == consdata1->vars[0]);
11436  assert(consdata0->weights[0] == consdata1->weights[0]);
11437 
11438  SCIPdebugMessage("knapsack constraints <%s> and <%s> with equal coefficients\n",
11439  SCIPconsGetName(cons0), SCIPconsGetName(cons1));
11440 
11441  /* check which constraint has to stay; */
11442  if( consdata0->capacity < consdata1->capacity )
11443  {
11444  consstay = cons0;
11445  consdel = cons1;
11446 
11447  /* exchange consdel with consstay in hashtable */
11448  SCIP_CALL( SCIPhashtableRemove(hashtable, (void*) consdel) );
11449  SCIP_CALL( SCIPhashtableInsert(hashtable, (void*) consstay) );
11450  }
11451  else
11452  {
11453  consstay = cons1;
11454  consdel = cons0;
11455  }
11456 
11457  /* update flags of constraint which caused the redundancy s.t. nonredundant information doesn't get lost */
11458  SCIP_CALL( SCIPupdateConsFlags(scip, consstay, consdel) );
11459 
11460  /* delete consdel */
11461  SCIP_CALL( SCIPdelCons(scip, consdel) );
11462  ++(*ndelconss);
11463 
11464  assert(SCIPconsIsActive(consstay));
11465  }
11466  else
11467  {
11468  /* no such constraint in current hash table: insert cons0 into hash table */
11469  SCIP_CALL( SCIPhashtableInsert(hashtable, (void*) cons0) );
11470  }
11471  }
11472 
11473  /* free hash table */
11474  SCIPhashtableFree(&hashtable);
11475 
11476  return SCIP_OKAY;
11477 }
11478 
11479 
11480 /** compares constraint with all prior constraints for possible redundancy or aggregation,
11481  * and removes or changes constraint accordingly
11482  */
11483 static
11485  SCIP* scip, /**< SCIP data structure */
11486  SCIP_CONS** conss, /**< constraint set */
11487  int firstchange, /**< first constraint that changed since last pair preprocessing round */
11488  int chkind, /**< index of constraint to check against all prior indices upto startind */
11489  int* ndelconss /**< pointer to count number of deleted constraints */
11490  )
11492  SCIP_CONS* cons0;
11493  SCIP_CONSDATA* consdata0;
11494  int c;
11495 
11496  assert(scip != NULL);
11497  assert(conss != NULL);
11498  assert(firstchange <= chkind);
11499  assert(ndelconss != NULL);
11500 
11501  /* get the constraint to be checked against all prior constraints */
11502  cons0 = conss[chkind];
11503  assert(cons0 != NULL);
11504  assert(SCIPconsIsActive(cons0));
11505  assert(!SCIPconsIsModifiable(cons0));
11506 
11507  consdata0 = SCIPconsGetData(cons0);
11508  assert(consdata0 != NULL);
11509  assert(consdata0->nvars >= 1);
11510  assert(consdata0->merged);
11511 
11512  /* sort the constraint */
11513  sortItems(consdata0);
11514 
11515  /* check constraint against all prior constraints */
11516  for( c = (consdata0->presolved ? firstchange : 0); c < chkind; ++c )
11517  {
11518  SCIP_CONS* cons1;
11519  SCIP_CONSDATA* consdata1;
11520  SCIP_Bool iscons0incons1contained;
11521  SCIP_Bool iscons1incons0contained;
11522  SCIP_Real quotient;
11523  int v;
11524  int v0;
11525  int v1;
11526 
11527  cons1 = conss[c];
11528  assert(cons1 != NULL);
11529  if( !SCIPconsIsActive(cons1) || SCIPconsIsModifiable(cons1) )
11530  continue;
11531 
11532  consdata1 = SCIPconsGetData(cons1);
11533  assert(consdata1 != NULL);
11534 
11535  /* if both constraints didn't change since last pair processing, we can ignore the pair */
11536  if( consdata0->presolved && consdata1->presolved )
11537  continue;
11538 
11539  assert(consdata1->nvars >= 1);
11540  assert(consdata1->merged);
11541 
11542  /* sort the constraint */
11543  sortItems(consdata1);
11544 
11545  quotient = ((SCIP_Real) consdata0->capacity) / ((SCIP_Real) consdata1->capacity);
11546 
11547  if( consdata0->nvars > consdata1->nvars )
11548  {
11549  iscons0incons1contained = FALSE;
11550  iscons1incons0contained = TRUE;
11551  v = consdata1->nvars - 1;
11552  }
11553  else if( consdata0->nvars < consdata1->nvars )
11554  {
11555  iscons0incons1contained = TRUE;
11556  iscons1incons0contained = FALSE;
11557  v = consdata0->nvars - 1;
11558  }
11559  else
11560  {
11561  iscons0incons1contained = TRUE;
11562  iscons1incons0contained = TRUE;
11563  v = consdata0->nvars - 1;
11564  }
11565 
11566  SCIPdebugMessage("preprocess knapsack constraint pair <%s> and <%s>\n", SCIPconsGetName(cons0), SCIPconsGetName(cons1));
11567 
11568  /* check consdata0 against consdata1:
11569  * 1. if all variables var_i of cons1 are in cons0 and for each of these variables
11570  * (consdata0->weights[i] / quotient) >= consdata1->weights[i] cons1 is redundant
11571  * 2. if all variables var_i of cons0 are in cons1 and for each of these variables
11572  * (consdata0->weights[i] / quotient) <= consdata1->weights[i] cons0 is redundant
11573  */
11574  v0 = consdata0->nvars - 1;
11575  v1 = consdata1->nvars - 1;
11576 
11577  while( v >= 0 )
11578  {
11579  assert(iscons0incons1contained || iscons1incons0contained);
11580 
11581  /* now there are more variables in cons1 left */
11582  if( v1 > v0 )
11583  {
11584  iscons1incons0contained = FALSE;
11585  if( !iscons0incons1contained )
11586  break;
11587  }
11588  /* now there are more variables in cons0 left */
11589  else if( v1 < v0 )
11590  {
11591  iscons0incons1contained = FALSE;
11592  if( !iscons1incons0contained )
11593  break;
11594  }
11595 
11596  assert(v == v0 || v == v1);
11597  assert(v0 >= 0);
11598  assert(v1 >= 0);
11599 
11600  /* both variables are the same */
11601  if( consdata0->vars[v0] == consdata1->vars[v1] )
11602  {
11603  /* if cons1 is possible contained in cons0 (consdata0->weights[v0] / quotient) must be greater equals consdata1->weights[v1] */
11604  if( iscons1incons0contained && SCIPisLT(scip, ((SCIP_Real) consdata0->weights[v0]) / quotient, (SCIP_Real) consdata1->weights[v1]) )
11605  {
11606  iscons1incons0contained = FALSE;
11607  if( !iscons0incons1contained )
11608  break;
11609  }
11610  /* if cons0 is possible contained in cons1 (consdata0->weight[v0] / quotient) must be less equals consdata1->weight[v1] */
11611  else if( iscons0incons1contained && SCIPisGT(scip, ((SCIP_Real) consdata0->weights[v0]) / quotient, (SCIP_Real) consdata1->weights[v1]) )
11612  {
11613  iscons0incons1contained = FALSE;
11614  if( !iscons1incons0contained )
11615  break;
11616  }
11617  --v0;
11618  --v1;
11619  --v;
11620  }
11621  else
11622  {
11623  /* both constraints have a variables which is not part of the other constraint, so stop */
11624  if( iscons0incons1contained && iscons1incons0contained )
11625  {
11626  iscons0incons1contained = FALSE;
11627  iscons1incons0contained = FALSE;
11628  break;
11629  }
11630  assert(iscons0incons1contained ? (v1 >= v0) : iscons1incons0contained);
11631  assert(iscons1incons0contained ? (v1 <= v0) : iscons0incons1contained);
11632  /* continue to the next variable */
11633  if( iscons0incons1contained )
11634  --v1;
11635  else
11636  --v0;
11637  }
11638  }
11639  /* neither one constraint was contained in another or we checked all variables of one constraint against the
11640  * other
11641  */
11642  assert(!iscons1incons0contained || !iscons0incons1contained || v0 == -1 || v1 == -1);
11643 
11644  if( iscons1incons0contained )
11645  {
11646  SCIPdebugMessage("knapsack constraint <%s> is redundant\n", SCIPconsGetName(cons1));
11647  SCIPdebugPrintCons(scip, cons1, NULL);
11648 
11649  /* update flags of constraint which caused the redundancy s.t. nonredundant information doesn't get lost */
11650  SCIP_CALL( SCIPupdateConsFlags(scip, cons0, cons1) );
11651 
11652  SCIP_CALL( SCIPdelCons(scip, cons1) );
11653  ++(*ndelconss);
11654  }
11655  else if( iscons0incons1contained )
11656  {
11657  SCIPdebugMessage("knapsack constraint <%s> is redundant\n", SCIPconsGetName(cons0));
11658  SCIPdebugPrintCons(scip, cons0, NULL);
11659 
11660  /* update flags of constraint which caused the redundancy s.t. nonredundant information doesn't get lost */
11661  SCIP_CALL( SCIPupdateConsFlags(scip, cons1, cons0) );
11662 
11663  SCIP_CALL( SCIPdelCons(scip, cons0) );
11664  ++(*ndelconss);
11665  break;
11666  }
11667  }
11668 
11669  return SCIP_OKAY;
11670 }
11671 
11672 /*
11673  * Linear constraint upgrading
11674  */
11675 
11676 /** creates and captures a knapsack constraint out of a linear inequality */
11677 static
11679  SCIP* scip, /**< SCIP data structure */
11680  SCIP_CONS** cons, /**< pointer to hold the created constraint */
11681  const char* name, /**< name of constraint */
11682  int nvars, /**< number of variables in the constraint */
11683  SCIP_VAR** vars, /**< array with variables of constraint entries */
11684  SCIP_Real* vals, /**< array with inequality coefficients */
11685  SCIP_Real lhs, /**< left hand side of inequality */
11686  SCIP_Real rhs, /**< right hand side of inequality */
11687  SCIP_Bool initial, /**< should the LP relaxation of constraint be in the initial LP?
11688  * Usually set to TRUE. Set to FALSE for 'lazy constraints'. */
11689  SCIP_Bool separate, /**< should the constraint be separated during LP processing?
11690  * Usually set to TRUE. */
11691  SCIP_Bool enforce, /**< should the constraint be enforced during node processing?
11692  * TRUE for model constraints, FALSE for additional, redundant constraints. */
11693  SCIP_Bool check, /**< should the constraint be checked for feasibility?
11694  * TRUE for model constraints, FALSE for additional, redundant constraints. */
11695  SCIP_Bool propagate, /**< should the constraint be propagated during node processing?
11696  * Usually set to TRUE. */
11697  SCIP_Bool local, /**< is constraint only valid locally?
11698  * Usually set to FALSE. Has to be set to TRUE, e.g., for branching constraints. */
11699  SCIP_Bool modifiable, /**< is constraint modifiable (subject to column generation)?
11700  * Usually set to FALSE. In column generation applications, set to TRUE if pricing
11701  * adds coefficients to this constraint. */
11702  SCIP_Bool dynamic, /**< is constraint subject to aging?
11703  * Usually set to FALSE. Set to TRUE for own cuts which
11704  * are separated as constraints. */
11705  SCIP_Bool removable, /**< should the relaxation be removed from the LP due to aging or cleanup?
11706  * Usually set to FALSE. Set to TRUE for 'lazy constraints' and 'user cuts'. */
11707  SCIP_Bool stickingatnode /**< should the constraint always be kept at the node where it was added, even
11708  * if it may be moved to a more global node?
11709  * Usually set to FALSE. Set to TRUE to for constraints that represent node data. */
11710  )
11711 {
11712  SCIP_VAR** transvars;
11713  SCIP_Longint* weights;
11714  SCIP_Longint capacity;
11715  SCIP_Longint weight;
11716  int mult;
11717  int v;
11718 
11719  assert(nvars == 0 || vars != NULL);
11720  assert(nvars == 0 || vals != NULL);
11721  assert(SCIPisInfinity(scip, -lhs) != SCIPisInfinity(scip, rhs));
11722 
11723  /* get temporary memory */
11724  SCIP_CALL( SCIPallocBufferArray(scip, &transvars, nvars) );
11725  SCIP_CALL( SCIPallocBufferArray(scip, &weights, nvars) );
11726 
11727  /* if the right hand side is non-infinite, we have to negate all variables with negative coefficient;
11728  * otherwise, we have to negate all variables with positive coefficient and multiply the row with -1
11729  */
11730  if( SCIPisInfinity(scip, rhs) )
11731  {
11732  mult = -1;
11733  capacity = (SCIP_Longint)SCIPfeasFloor(scip, -lhs);
11734  }
11735  else
11736  {
11737  mult = +1;
11738  capacity = (SCIP_Longint)SCIPfeasFloor(scip, rhs);
11739  }
11740 
11741  /* negate positive or negative variables */
11742  for( v = 0; v < nvars; ++v )
11743  {
11744  assert(SCIPisFeasIntegral(scip, vals[v]));
11745  weight = mult * (SCIP_Longint)SCIPfeasFloor(scip, vals[v]);
11746  if( weight > 0 )
11747  {
11748  transvars[v] = vars[v];
11749  weights[v] = weight;
11750  }
11751  else
11752  {
11753  SCIP_CALL( SCIPgetNegatedVar(scip, vars[v], &transvars[v]) );
11754  weights[v] = -weight;
11755  capacity -= weight;
11756  }
11757  assert(transvars[v] != NULL);
11758  }
11759 
11760  /* create the constraint */
11761  SCIP_CALL( SCIPcreateConsKnapsack(scip, cons, name, nvars, transvars, weights, capacity,
11762  initial, separate, enforce, check, propagate, local, modifiable, dynamic, removable, stickingatnode) );
11763 
11764  /* free temporary memory */
11765  SCIPfreeBufferArray(scip, &weights);
11766  SCIPfreeBufferArray(scip, &transvars);
11767 
11768  return SCIP_OKAY;
11769 }
11770 
11771 /** tries to upgrade a linear constraint into a knapsack constraint */
11772 static
11773 SCIP_DECL_LINCONSUPGD(linconsUpgdKnapsack)
11774 { /*lint --e{715}*/
11775  SCIP_Bool upgrade;
11776 
11777  assert(upgdcons != NULL);
11778 
11779  /* check, if linear constraint can be upgraded to a knapsack constraint
11780  * - all variables must be binary
11781  * - all coefficients must be integral
11782  * - exactly one of the sides must be infinite
11783  */
11784  upgrade = (nposbin + nnegbin + nposimplbin + nnegimplbin == nvars)
11785  && (ncoeffspone + ncoeffsnone + ncoeffspint + ncoeffsnint == nvars)
11786  && (SCIPisInfinity(scip, -lhs) != SCIPisInfinity(scip, rhs));
11787 
11788  if( upgrade )
11789  {
11790  SCIPdebugMessage("upgrading constraint <%s> to knapsack constraint\n", SCIPconsGetName(cons));
11791 
11792  /* create the knapsack constraint (an automatically upgraded constraint is always unmodifiable) */
11793  assert(!SCIPconsIsModifiable(cons));
11794  SCIP_CALL( createNormalizedKnapsack(scip, upgdcons, SCIPconsGetName(cons), nvars, vars, vals, lhs, rhs,
11799  }
11800 
11801  return SCIP_OKAY;
11802 }
11803 
11804 
11805 /*
11806  * Callback methods of constraint handler
11807  */
11808 
11809 /** copy method for constraint handler plugins (called when SCIP copies plugins) */
11810 static
11811 SCIP_DECL_CONSHDLRCOPY(conshdlrCopyKnapsack)
11812 { /*lint --e{715}*/
11813  assert(scip != NULL);
11814  assert(conshdlr != NULL);
11815  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
11816 
11817  /* call inclusion method of constraint handler */
11819 
11820  *valid = TRUE;
11821 
11822  return SCIP_OKAY;
11823 }
11824 
11825 /** destructor of constraint handler to free constraint handler data (called when SCIP is exiting) */
11826 static
11827 SCIP_DECL_CONSFREE(consFreeKnapsack)
11828 { /*lint --e{715}*/
11829  SCIP_CONSHDLRDATA* conshdlrdata;
11830 
11831  /* free constraint handler data */
11832  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11833  assert(conshdlrdata != NULL);
11835  SCIPfreeMemory(scip, &conshdlrdata);
11836 
11837  SCIPconshdlrSetData(conshdlr, NULL);
11838 
11839  return SCIP_OKAY;
11840 }
11841 
11842 
11843 /** initialization method of constraint handler (called after problem was transformed) */
11844 static
11845 SCIP_DECL_CONSINIT(consInitKnapsack)
11846 { /*lint --e{715}*/
11847  SCIP_CONSHDLRDATA* conshdlrdata;
11848  int nvars;
11849 
11850  assert( scip != NULL );
11851  assert( conshdlr != NULL );
11853  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11854  assert(conshdlrdata != NULL);
11855 
11856  /* all variables which are of integral type can be binary; this can be checked via the method SCIPvarIsBinary(var) */
11857  nvars = SCIPgetNVars(scip) - SCIPgetNContVars(scip);
11858 
11859  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->reals1, nvars) );
11860  BMSclearMemoryArray(conshdlrdata->reals1, nvars);
11861  conshdlrdata->reals1size = nvars;
11862 
11863  return SCIP_OKAY;
11864 }
11865 
11866 /** deinitialization method of constraint handler (called before transformed problem is freed) */
11867 static
11868 SCIP_DECL_CONSEXIT(consExitKnapsack)
11869 { /*lint --e{715}*/
11870  SCIP_CONSHDLRDATA* conshdlrdata;
11871 
11872  assert( scip != NULL );
11873  assert( conshdlr != NULL );
11874 
11875  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11876  assert(conshdlrdata != NULL);
11877 
11878  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->reals1);
11879  conshdlrdata->reals1size = 0;
11880 
11881  return SCIP_OKAY;
11882 }
11883 
11884 
11885 /** presolving initialization method of constraint handler (called when presolving is about to begin) */
11886 static
11887 SCIP_DECL_CONSINITPRE(consInitpreKnapsack)
11888 { /*lint --e{715}*/
11889  SCIP_CONSHDLRDATA* conshdlrdata;
11890  int nvars;
11891 
11892  assert(scip != NULL);
11893  assert(conshdlr != NULL);
11894  assert(nconss == 0 || conss != NULL);
11895 
11896  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11897  assert(conshdlrdata != NULL);
11898 
11899  /* all variables which are of integral type can be binary; this can be checked via the method SCIPvarIsBinary(var) */
11900  nvars = SCIPgetNVars(scip) - SCIPgetNContVars(scip);
11901 
11902  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->ints1, nvars) );
11903  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->ints2, nvars) );
11904  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->longints1, nvars) );
11905  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->longints2, nvars) );
11906  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->bools1, nvars) );
11907  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->bools2, nvars) );
11908  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->bools3, nvars) );
11909  SCIP_CALL( SCIPallocMemoryArray(scip, &conshdlrdata->bools4, nvars) );
11910 
11911  BMSclearMemoryArray(conshdlrdata->ints1, nvars);
11912  BMSclearMemoryArray(conshdlrdata->ints2, nvars);
11913  BMSclearMemoryArray(conshdlrdata->longints1, nvars);
11914  BMSclearMemoryArray(conshdlrdata->longints2, nvars);
11915  BMSclearMemoryArray(conshdlrdata->bools1, nvars);
11916  BMSclearMemoryArray(conshdlrdata->bools2, nvars);
11917  BMSclearMemoryArray(conshdlrdata->bools3, nvars);
11918  BMSclearMemoryArray(conshdlrdata->bools4, nvars);
11919 
11920  conshdlrdata->ints1size = nvars;
11921  conshdlrdata->ints2size = nvars;
11922  conshdlrdata->longints1size = nvars;
11923  conshdlrdata->longints2size = nvars;
11924  conshdlrdata->bools1size = nvars;
11925  conshdlrdata->bools2size = nvars;
11926  conshdlrdata->bools3size = nvars;
11927  conshdlrdata->bools4size = nvars;
11928 
11929  return SCIP_OKAY;
11930 }
11931 
11932 
11933 /** presolving deinitialization method of constraint handler (called after presolving has been finished) */
11934 static
11935 SCIP_DECL_CONSEXITPRE(consExitpreKnapsack)
11936 { /*lint --e{715}*/
11937  SCIP_CONSHDLRDATA* conshdlrdata;
11938  int c;
11939 
11940  assert(scip != NULL);
11941  assert(conshdlr != NULL);
11943  for( c = 0; c < nconss; ++c )
11944  {
11945  if( !SCIPconsIsDeleted(conss[c]) )
11946  {
11947  /* since we are not allowed to detect infeasibility in the exitpre stage, we dont give an infeasible pointer */
11948  SCIP_CALL( applyFixings(scip, conss[c], NULL) );
11949  }
11950  }
11951 
11952  conshdlrdata = SCIPconshdlrGetData(conshdlr);
11953  assert(conshdlrdata != NULL);
11954 
11955  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->ints1);
11956  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->ints2);
11957  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->longints1);
11958  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->longints2);
11959  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->bools1);
11960  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->bools2);
11961  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->bools3);
11962  SCIPfreeMemoryArrayNull(scip, &conshdlrdata->bools4);
11963 
11964  conshdlrdata->ints1size = 0;
11965  conshdlrdata->ints2size = 0;
11966  conshdlrdata->longints1size = 0;
11967  conshdlrdata->longints2size = 0;
11968  conshdlrdata->bools1size = 0;
11969  conshdlrdata->bools2size = 0;
11970  conshdlrdata->bools3size = 0;
11971  conshdlrdata->bools4size = 0;
11972 
11973  return SCIP_OKAY;
11974 }
11975 
11976 
11977 /** solving process deinitialization method of constraint handler (called before branch and bound process data is freed) */
11978 static
11979 SCIP_DECL_CONSEXITSOL(consExitsolKnapsack)
11980 { /*lint --e{715}*/
11981  SCIP_CONSDATA* consdata;
11982  int c;
11983 
11984  assert( scip != NULL );
11985 
11986  /* release the rows of all constraints */
11987  for( c = 0; c < nconss; ++c )
11988  {
11989  consdata = SCIPconsGetData(conss[c]);
11990  assert(consdata != NULL);
11991 
11992  if( consdata->row != NULL )
11993  {
11994  SCIP_CALL( SCIPreleaseRow(scip, &consdata->row) );
11995  }
11996  }
11997 
11998  return SCIP_OKAY;
11999 }
12000 
12001 /** frees specific constraint data */
12002 static
12003 SCIP_DECL_CONSDELETE(consDeleteKnapsack)
12004 { /*lint --e{715}*/
12005  SCIP_CONSHDLRDATA* conshdlrdata;
12006 
12007  assert(conshdlr != NULL);
12008  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
12009 
12010  /* get event handler */
12011  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12012  assert(conshdlrdata != NULL);
12013  assert(conshdlrdata->eventhdlr != NULL);
12014 
12015  /* free knapsack constraint */
12016  SCIP_CALL( consdataFree(scip, consdata, conshdlrdata->eventhdlr) );
12017 
12018  return SCIP_OKAY;
12019 }
12020 
12021 /** transforms constraint data into data belonging to the transformed problem */
12022 static
12023 SCIP_DECL_CONSTRANS(consTransKnapsack)
12024 { /*lint --e{715}*/
12025  SCIP_CONSHDLRDATA* conshdlrdata;
12026  SCIP_CONSDATA* sourcedata;
12027  SCIP_CONSDATA* targetdata;
12028 
12029  assert(conshdlr != NULL);
12030  assert(strcmp(SCIPconshdlrGetName(conshdlr), CONSHDLR_NAME) == 0);
12031  assert(SCIPgetStage(scip) == SCIP_STAGE_TRANSFORMING);
12032  assert(sourcecons != NULL);
12033  assert(targetcons != NULL);
12034 
12035  sourcedata = SCIPconsGetData(sourcecons);
12036  assert(sourcedata != NULL);
12037  assert(sourcedata->row == NULL); /* in original problem, there cannot be LP rows */
12038 
12039  /* get event handler */
12040  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12041  assert(conshdlrdata != NULL);
12042  assert(conshdlrdata->eventhdlr != NULL);
12043 
12044  /* create target constraint data */
12045  SCIP_CALL( consdataCreate(scip, &targetdata, conshdlrdata->eventhdlr,
12046  sourcedata->nvars, sourcedata->vars, sourcedata->weights, sourcedata->capacity) );
12047 
12048  /* create target constraint */
12049  SCIP_CALL( SCIPcreateCons(scip, targetcons, SCIPconsGetName(sourcecons), conshdlr, targetdata,
12050  SCIPconsIsInitial(sourcecons), SCIPconsIsSeparated(sourcecons), SCIPconsIsEnforced(sourcecons),
12051  SCIPconsIsChecked(sourcecons), SCIPconsIsPropagated(sourcecons),
12052  SCIPconsIsLocal(sourcecons), SCIPconsIsModifiable(sourcecons),
12053  SCIPconsIsDynamic(sourcecons), SCIPconsIsRemovable(sourcecons), SCIPconsIsStickingAtNode(sourcecons)) );
12054 
12055  return SCIP_OKAY;
12056 }
12057 
12058 /** LP initialization method of constraint handler (called before the initial LP relaxation at a node is solved) */
12059 static
12060 SCIP_DECL_CONSINITLP(consInitlpKnapsack)
12061 { /*lint --e{715}*/
12062  SCIP_Bool cutoff;
12063  int i;
12064 
12065  for( i = 0; i < nconss; i++ )
12066  {
12067  assert(SCIPconsIsInitial(conss[i]));
12068  SCIP_CALL( addRelaxation(scip, conss[i], NULL, &cutoff) );
12069  /* ignore cutoff: cannot return status */
12070  }
12071 
12072  return SCIP_OKAY;
12073 }
12074 
12075 /** separation method of constraint handler for LP solutions */
12076 static
12077 SCIP_DECL_CONSSEPALP(consSepalpKnapsack)
12078 { /*lint --e{715}*/
12079  SCIP_CONSHDLRDATA* conshdlrdata;
12080  SCIP_Bool sepacardinality;
12081  SCIP_Bool cutoff;
12082 
12083  SCIP_Real loclowerbound;
12084  SCIP_Real glblowerbound;
12085  SCIP_Real cutoffbound;
12086  SCIP_Real maxbound;
12087 
12088  int depth;
12089  int nrounds;
12090  int sepafreq;
12091  int sepacardfreq;
12092  int ncuts;
12093  int maxsepacuts;
12094  int i;
12095 
12096  *result = SCIP_DIDNOTRUN;
12097 
12098  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12099  assert(conshdlrdata != NULL);
12100 
12101  depth = SCIPgetDepth(scip);
12102  nrounds = SCIPgetNSepaRounds(scip);
12103 
12104  SCIPdebugMessage("knapsack separation of %d/%d constraints, round %d (max %d/%d)\n",
12105  nusefulconss, nconss, nrounds, conshdlrdata->maxroundsroot, conshdlrdata->maxrounds);
12106 
12107  /* only call the separator a given number of times at each node */
12108  if( (depth == 0 && conshdlrdata->maxroundsroot >= 0 && nrounds >= conshdlrdata->maxroundsroot)
12109  || (depth > 0 && conshdlrdata->maxrounds >= 0 && nrounds >= conshdlrdata->maxrounds) )
12110  return SCIP_OKAY;
12111 
12112  /* check, if we should additionally separate knapsack cuts */
12113  sepafreq = SCIPconshdlrGetSepaFreq(conshdlr);
12114  sepacardfreq = sepafreq * conshdlrdata->sepacardfreq;
12115  sepacardinality = (conshdlrdata->sepacardfreq >= 0)
12116  && ((sepacardfreq == 0 && depth == 0) || (sepacardfreq >= 1 && (depth % sepacardfreq == 0)));
12117 
12118  /* check dual bound to see if we want to produce knapsack cuts at this node */
12119  loclowerbound = SCIPgetLocalLowerbound(scip);
12120  glblowerbound = SCIPgetLowerbound(scip);
12121  cutoffbound = SCIPgetCutoffbound(scip);
12122  maxbound = glblowerbound + conshdlrdata->maxcardbounddist * (cutoffbound - glblowerbound);
12123  sepacardinality = sepacardinality && SCIPisLE(scip, loclowerbound, maxbound);
12124  sepacardinality = sepacardinality && (SCIPgetNLPBranchCands(scip) > 0);
12125 
12126  /* get the maximal number of cuts allowed in a separation round */
12127  maxsepacuts = (depth == 0 ? conshdlrdata->maxsepacutsroot : conshdlrdata->maxsepacuts);
12128 
12129  *result = SCIP_DIDNOTFIND;
12130  ncuts = 0;
12131  cutoff = FALSE;
12132 
12133  /* separate useful constraints */
12134  for( i = 0; i < nusefulconss && ncuts < maxsepacuts && !SCIPisStopped(scip); i++ )
12135  {
12136  SCIP_CALL( separateCons(scip, conss[i], NULL, sepacardinality, conshdlrdata->usegubs, &cutoff, &ncuts) );
12137  }
12138 
12139  /* adjust return value */
12140  if ( cutoff )
12141  *result = SCIP_CUTOFF;
12142  else if ( ncuts > 0 )
12143  *result = SCIP_SEPARATED;
12144 
12145  return SCIP_OKAY;
12146 }
12147 
12148 
12149 /** separation method of constraint handler for arbitrary primal solutions */
12150 static
12151 SCIP_DECL_CONSSEPASOL(consSepasolKnapsack)
12152 { /*lint --e{715}*/
12153  SCIP_CONSHDLRDATA* conshdlrdata;
12154  SCIP_Bool sepacardinality;
12155  SCIP_Bool cutoff;
12156 
12157  int depth;
12158  int nrounds;
12159  int sepafreq;
12160  int sepacardfreq;
12161  int ncuts;
12162  int maxsepacuts;
12163  int i;
12164 
12165  *result = SCIP_DIDNOTRUN;
12166 
12167  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12168  assert(conshdlrdata != NULL);
12169 
12170  depth = SCIPgetDepth(scip);
12171  nrounds = SCIPgetNSepaRounds(scip);
12172 
12173  SCIPdebugMessage("knapsack separation of %d/%d constraints, round %d (max %d/%d)\n",
12174  nusefulconss, nconss, nrounds, conshdlrdata->maxroundsroot, conshdlrdata->maxrounds);
12175 
12176  /* only call the separator a given number of times at each node */
12177  if( (depth == 0 && conshdlrdata->maxroundsroot >= 0 && nrounds >= conshdlrdata->maxroundsroot)
12178  || (depth > 0 && conshdlrdata->maxrounds >= 0 && nrounds >= conshdlrdata->maxrounds) )
12179  return SCIP_OKAY;
12180 
12181  /* check, if we should additionally separate knapsack cuts */
12182  sepafreq = SCIPconshdlrGetSepaFreq(conshdlr);
12183  sepacardfreq = sepafreq * conshdlrdata->sepacardfreq;
12184  sepacardinality = (conshdlrdata->sepacardfreq >= 0)
12185  && ((sepacardfreq == 0 && depth == 0) || (sepacardfreq >= 1 && (depth % sepacardfreq == 0)));
12186 
12187  /* get the maximal number of cuts allowed in a separation round */
12188  maxsepacuts = (depth == 0 ? conshdlrdata->maxsepacutsroot : conshdlrdata->maxsepacuts);
12189 
12190  *result = SCIP_DIDNOTFIND;
12191  ncuts = 0;
12192  cutoff = FALSE;
12193 
12194  /* separate useful constraints */
12195  for( i = 0; i < nusefulconss && ncuts < maxsepacuts && !SCIPisStopped(scip); i++ )
12196  {
12197  SCIP_CALL( separateCons(scip, conss[i], sol, sepacardinality, conshdlrdata->usegubs, &cutoff, &ncuts) );
12198  }
12199 
12200  /* adjust return value */
12201  if ( cutoff )
12202  *result = SCIP_CUTOFF;
12203  else if( ncuts > 0 )
12204  *result = SCIP_SEPARATED;
12205 
12206  return SCIP_OKAY;
12207 }
12208 
12209 
12210 /** constraint enforcing method of constraint handler for LP solutions */
12211 static
12212 SCIP_DECL_CONSENFOLP(consEnfolpKnapsack)
12213 { /*lint --e{715}*/
12214  SCIP_CONSHDLRDATA* conshdlrdata;
12215  SCIP_Bool violated;
12216  SCIP_Bool cutoff = FALSE;
12217  int maxncuts;
12218  int ncuts = 0;
12219  int i;
12220 
12221  *result = SCIP_FEASIBLE;
12222 
12223  SCIPdebugMessage("knapsack enforcement of %d/%d constraints\n", nusefulconss, nconss);
12224 
12225  /* get maximal number of cuts per round */
12226  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12227  assert(conshdlrdata != NULL);
12228  maxncuts = (SCIPgetDepth(scip) == 0 ? conshdlrdata->maxsepacutsroot : conshdlrdata->maxsepacuts);
12229 
12230  /* search for violated useful knapsack constraints */
12231  for( i = 0; i < nusefulconss && ncuts < maxncuts && ! cutoff; i++ )
12232  {
12233  SCIP_CALL( checkCons(scip, conss[i], NULL, FALSE, FALSE, &violated) );
12234  if( violated )
12235  {
12236  /* add knapsack constraint as LP row to the LP */
12237  SCIP_CALL( addRelaxation(scip, conss[i], NULL, &cutoff) );
12238  ncuts++;
12239  }
12240  }
12241 
12242  /* as long as no violations were found, search for violated obsolete knapsack constraints */
12243  for( i = nusefulconss; i < nconss && ncuts == 0 && ! cutoff; i++ )
12244  {
12245  SCIP_CALL( checkCons(scip, conss[i], NULL, FALSE, FALSE, &violated) );
12246  if( violated )
12247  {
12248  /* add knapsack constraint as LP row to the LP */
12249  SCIP_CALL( addRelaxation(scip, conss[i], NULL, &cutoff) );
12250  ncuts++;
12251  }
12252  }
12253 
12254  /* adjust the result code */
12255  if ( cutoff )
12256  *result = SCIP_CUTOFF;
12257  else if ( ncuts > 0 )
12258  *result = SCIP_SEPARATED;
12259 
12260  return SCIP_OKAY;
12261 }
12262 
12263 /** constraint enforcing method of constraint handler for pseudo solutions */
12264 static
12265 SCIP_DECL_CONSENFOPS(consEnfopsKnapsack)
12266 { /*lint --e{715}*/
12267  SCIP_Bool violated;
12268  int i;
12269 
12270  for( i = 0; i < nconss; i++ )
12271  {
12272  SCIP_CALL( checkCons(scip, conss[i], NULL, TRUE, FALSE, &violated) );
12273  if( violated )
12274  {
12275  *result = SCIP_INFEASIBLE;
12276  return SCIP_OKAY;
12277  }
12278  }
12279  *result = SCIP_FEASIBLE;
12280 
12281  return SCIP_OKAY;
12282 }
12283 
12284 /** feasibility check method of constraint handler for integral solutions */
12285 static
12286 SCIP_DECL_CONSCHECK(consCheckKnapsack)
12287 { /*lint --e{715}*/
12288  SCIP_Bool violated;
12289  int i;
12290 
12291  for( i = 0; i < nconss; i++ )
12292  {
12293  SCIP_CALL( checkCons(scip, conss[i], sol, checklprows, printreason, &violated) );
12294  if( violated )
12295  {
12296  *result = SCIP_INFEASIBLE;
12297  return SCIP_OKAY;
12298  }
12299  }
12300  *result = SCIP_FEASIBLE;
12301 
12302  return SCIP_OKAY;
12303 }
12304 
12305 /** domain propagation method of constraint handler */
12306 static
12307 SCIP_DECL_CONSPROP(consPropKnapsack)
12308 { /*lint --e{715}*/
12309  SCIP_CONSHDLRDATA* conshdlrdata;
12310  SCIP_Bool cutoff;
12311  SCIP_Bool redundant;
12312  SCIP_Bool inpresolve;
12313  int nfixedvars;
12314  int i;
12315 
12316  cutoff = FALSE;
12317  nfixedvars = 0;
12318 
12319  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12320  assert(conshdlrdata != NULL);
12321 
12322  inpresolve = (SCIPgetStage(scip) < SCIP_STAGE_INITSOLVE);
12323 
12324  /* process useful constraints */
12325  for( i = 0; i < nusefulconss && !cutoff; i++ )
12326  {
12327  /* do not propagate constraints with multi-aggregated variables, which should only happen in probing mode,
12328  * otherwise the multi-aggregation should be resolved
12329  */
12330  if( inpresolve && SCIPconsGetData(conss[i])->existmultaggr )
12331  continue;
12332 #ifndef NDEBUG
12333  else if( !inpresolve )
12334  assert(!(SCIPconsGetData(conss[i])->existmultaggr));
12335 #endif
12336 
12337  SCIP_CALL( propagateCons(scip, conss[i], &cutoff, &redundant, &nfixedvars, conshdlrdata->negatedclique) );
12338  }
12339 
12340  /* adjust result code */
12341  if( cutoff )
12342  *result = SCIP_CUTOFF;
12343  else if( nfixedvars > 0 )
12344  *result = SCIP_REDUCEDDOM;
12345  else
12346  *result = SCIP_DIDNOTFIND;
12347 
12348  return SCIP_OKAY;
12349 }
12350 
12351 /** presolving method of constraint handler */
12352 static
12353 SCIP_DECL_CONSPRESOL(consPresolKnapsack)
12354 { /*lint --e{715}*/
12355  SCIP_CONSHDLRDATA* conshdlrdata;
12356  SCIP_CONSDATA* consdata;
12357  SCIP_CONS* cons;
12358  SCIP_Bool cutoff;
12359  SCIP_Bool redundant;
12360  SCIP_Bool success;
12361  int oldnfixedvars;
12362  int oldnchgbds;
12363  int oldndelconss;
12364  int oldnaddconss;
12365  int oldnchgcoefs;
12366  int oldnchgsides;
12367  int firstchange;
12368  int c;
12369 
12370  /* remember old preprocessing counters */
12371  cutoff = FALSE;
12372  oldnfixedvars = *nfixedvars;
12373  oldnchgbds = *nchgbds;
12374  oldndelconss = *ndelconss;
12375  oldnaddconss = *naddconss;
12376  oldnchgcoefs = *nchgcoefs;
12377  oldnchgsides = *nchgsides;
12378  firstchange = INT_MAX;
12379 
12380  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12381  assert(conshdlrdata != NULL);
12382 
12383  for( c = 0; c < nconss && !SCIPisStopped(scip); c++ )
12384  {
12385  int thisnfixedvars;
12386  int thisnchgbds;
12387 
12388  cons = conss[c];
12389  consdata = SCIPconsGetData(cons);
12390  assert(consdata != NULL);
12391 
12392  /* force presolving the constraint in the initial round */
12393  if( nrounds == 0 )
12394  consdata->presolved = FALSE;
12395  else if( consdata->presolved )
12396  continue;
12397 
12398  SCIPdebugMessage("presolving knapsack constraint <%s>\n", SCIPconsGetName(cons));
12399  SCIPdebugPrintCons(scip, cons, NULL);
12400  consdata->presolved = TRUE;
12401 
12402  if( nrounds == 0 || nnewfixedvars > 0 || nnewaggrvars > 0 || nnewchgbds > 0
12403  || *nfixedvars > oldnfixedvars || *nchgbds > oldnchgbds )
12404  {
12405  SCIP_CALL( applyFixings(scip, cons, &cutoff) );
12406  if( cutoff )
12407  break;
12408  }
12409 
12410  thisnfixedvars = *nfixedvars;
12411  thisnchgbds = *nchgbds;
12412 
12413  /* merge constraint, so propagation works better */
12414  SCIP_CALL( mergeMultiples(scip, cons, &cutoff) );
12415  if( cutoff )
12416  break;
12417 
12418  /* add cliques in the knapsack to the clique table */
12419  SCIP_CALL( addCliques(scip, cons, &cutoff, nchgbds) );
12420  if( cutoff )
12421  break;
12422 
12423  /* propagate constraint */
12424  SCIP_CALL( propagateCons(scip, cons, &cutoff, &redundant, nfixedvars, TRUE) );
12425  if( cutoff )
12426  break;
12427  if( redundant )
12428  {
12429  (*ndelconss)++;
12430  continue;
12431  }
12432 
12433  /* remove again all fixed variables, if further fixings were found */
12434  if( *nfixedvars > thisnfixedvars || *nchgbds > thisnchgbds )
12435  {
12436  SCIP_CALL( applyFixings(scip, cons, &cutoff) );
12437  if( cutoff )
12438  break;
12439 
12440  thisnfixedvars = *nfixedvars;
12441  }
12442 
12443  if( !SCIPconsIsModifiable(cons) )
12444  {
12445  /* check again for redundancy (applyFixings() might have decreased weightsum due to fixed-to-zero vars) */
12446  if( consdata->weightsum <= consdata->capacity )
12447  {
12448  SCIPdebugMessage(" -> knapsack constraint <%s> is redundant: weightsum=%"SCIP_LONGINT_FORMAT", capacity=%"SCIP_LONGINT_FORMAT"\n",
12449  SCIPconsGetName(cons), consdata->weightsum, consdata->capacity);
12450  SCIP_CALL( SCIPdelConsLocal(scip, cons) );
12451  continue;
12452  }
12453 
12454  /* divide weights by their greatest common divisor */
12455  normalizeWeights(cons, nchgcoefs, nchgsides);
12456 
12457  /* try to simplify inequalities */
12458  if( conshdlrdata->simplifyinequalities )
12459  {
12460  SCIP_CALL( simplifyInequalities(scip, cons, nfixedvars, ndelconss, nchgcoefs, nchgsides, naddconss, &cutoff) );
12461  if( cutoff )
12462  break;
12463 
12464  if( SCIPconsIsDeleted(cons) )
12465  continue;
12466 
12467  /* remove again all fixed variables, if further fixings were found */
12468  if( *nfixedvars > thisnfixedvars )
12469  {
12470  SCIP_CALL( applyFixings(scip, cons, &cutoff) );
12471  if( cutoff )
12472  break;
12473  }
12474  }
12475 
12476  /* tighten capacity and weights */
12477  SCIP_CALL( tightenWeights(scip, cons, nchgcoefs, nchgsides, naddconss, ndelconss, &cutoff) );
12478  if( cutoff )
12479  break;
12480 
12481  if( SCIPconsIsActive(cons) )
12482  {
12483  if( conshdlrdata->dualpresolving )
12484  {
12485  /* in case the knapsack constraints is independent of everything else, solve the knapsack and apply the
12486  * dual reduction
12487  */
12488  SCIP_CALL( dualPresolving(scip, cons, nchgbds, ndelconss, &redundant) );
12489  if( redundant )
12490  continue;
12491  }
12492 
12493  /* check if knapsack constraint is parallel to objective function */
12494  SCIP_CALL( checkParallelObjective(scip, cons, conshdlrdata) );
12495  }
12496  }
12497  /* remember the first changed constraint to begin the next aggregation round with */
12498  if( firstchange == INT_MAX && !consdata->presolved )
12499  firstchange = c;
12500  }
12501 
12502  /* preprocess pairs of knapsack constraints */
12503  if( !cutoff && conshdlrdata->presolusehashing )
12504  {
12505  /* detect redundant constraints; fast version with hash table instead of pairwise comparison */
12506  SCIP_CALL( detectRedundantConstraints(scip, SCIPblkmem(scip), conss, nconss, &cutoff, ndelconss) );
12507  }
12508 
12509  if( (*ndelconss != oldndelconss) || (*nchgsides != oldnchgsides) || (*nchgcoefs != oldnchgcoefs) || (*naddconss != oldnaddconss) )
12510  success = TRUE;
12511  else
12512  success = FALSE;
12513 
12514  if( !cutoff && firstchange < nconss && conshdlrdata->presolpairwise )
12515  {
12516  SCIP_Longint npaircomparisons;
12517 
12518  npaircomparisons = 0;
12519  oldndelconss = *ndelconss;
12520  oldnchgsides = *nchgsides;
12521  oldnchgcoefs = *nchgcoefs;
12522 
12523  for( c = firstchange; c < nconss && !cutoff && !SCIPisStopped(scip); ++c )
12524  {
12525  cons = conss[c];
12526  if( !SCIPconsIsActive(cons) || SCIPconsIsModifiable(cons) )
12527  continue;
12528 
12529  npaircomparisons += ((!SCIPconsGetData(cons)->presolved) ? (SCIP_Longint) c : ((SCIP_Longint) c - (SCIP_Longint) firstchange));
12530 
12531  SCIP_CALL( preprocessConstraintPairs(scip, conss, firstchange, c, ndelconss) );
12532 
12533  if( npaircomparisons > NMINCOMPARISONS )
12534  {
12535  if( (*ndelconss != oldndelconss) || (*nchgsides != oldnchgsides) || (*nchgcoefs != oldnchgcoefs) )
12536  success = TRUE;
12537  if( ((SCIP_Real) (*ndelconss - oldndelconss) + ((SCIP_Real) (*nchgsides - oldnchgsides))/2.0 +
12538  ((SCIP_Real) (*nchgcoefs - oldnchgcoefs))/10.0) / ((SCIP_Real) npaircomparisons) < MINGAINPERNMINCOMPARISONS )
12539  break;
12540  oldndelconss = *ndelconss;
12541  oldnchgsides = *nchgsides;
12542  oldnchgcoefs = *nchgcoefs;
12543  npaircomparisons = 0;
12544  }
12545  }
12546  }
12547 
12548  if( cutoff )
12549  *result = SCIP_CUTOFF;
12550  else if( success || *nfixedvars > oldnfixedvars || *nchgbds > oldnchgbds )
12551  *result = SCIP_SUCCESS;
12552  else
12553  *result = SCIP_DIDNOTFIND;
12554 
12555  return SCIP_OKAY;
12556 }
12557 
12558 /** propagation conflict resolving method of constraint handler */
12559 static
12560 SCIP_DECL_CONSRESPROP(consRespropKnapsack)
12561 { /*lint --e{715}*/
12562  SCIP_CONSDATA* consdata;
12563  SCIP_Longint capsum;
12564  int i;
12565 
12566  assert(result != NULL);
12568  consdata = SCIPconsGetData(cons);
12569  assert(consdata != NULL);
12570 
12571  /* check if we fixed a binary variable to one (due to negated clique) */
12572  if( inferinfo >= 0 && SCIPvarGetLbLocal(infervar) > 0.5 )
12573  {
12574  for( i = 0; i < consdata->nvars; ++i )
12575  {
12576  if( SCIPvarGetIndex(consdata->vars[i]) == inferinfo )
12577  {
12578  assert( SCIPvarGetUbAtIndex(consdata->vars[i], bdchgidx, FALSE) < 0.5 );
12579  SCIP_CALL( SCIPaddConflictBinvar(scip, consdata->vars[i]) );
12580  break;
12581  }
12582  }
12583  assert(i < consdata->nvars);
12584  }
12585  else
12586  {
12587  /* according to negated cliques the minweightsum and all variables which are fixed to one which led to a fixing of
12588  * another negated clique variable to one, the inferinfo was chosen to be the negative of the position in the
12589  * knapsack constraint, see one above call of SCIPinferBinvarCons
12590  */
12591  if( inferinfo < 0 )
12592  capsum = 0;
12593  else
12594  {
12595  /* locate the inference variable and calculate the capacity that has to be used up to conclude infervar == 0;
12596  * inferinfo stores the position of the inference variable (but maybe the variables were resorted)
12597  */
12598  if( inferinfo < consdata->nvars && consdata->vars[inferinfo] == infervar )
12599  capsum = consdata->weights[inferinfo];
12600  else
12601  {
12602  for( i = 0; i < consdata->nvars && consdata->vars[i] != infervar; ++i )
12603  {}
12604  assert(i < consdata->nvars);
12605  capsum = consdata->weights[i];
12606  }
12607  }
12608 
12609  /* add fixed-to-one variables up to the point, that their weight plus the weight of the conflict variable exceeds
12610  * the capacity
12611  */
12612  if( capsum <= consdata->capacity )
12613  {
12614  for( i = 0; i < consdata->nvars; i++ )
12615  {
12616  if( SCIPvarGetLbAtIndex(consdata->vars[i], bdchgidx, FALSE) > 0.5 )
12617  {
12618  SCIP_CALL( SCIPaddConflictBinvar(scip, consdata->vars[i]) );
12619  capsum += consdata->weights[i];
12620  if( capsum > consdata->capacity )
12621  break;
12622  }
12623  }
12624  }
12625  }
12626 
12627  /* NOTE: It might be the case that capsum < consdata->capacity. This is due the fact that the fixing of the variable
12628  * to zero can included negated clique information. A negated clique means, that at most one of the clique
12629  * variables can be zero. These information can be used to compute a minimum activity of the constraint and
12630  * used to fix variables to zero.
12631  *
12632  * Even if capsum < consdata->capacity we still reported a complete reason since the minimum activity is based
12633  * on global variable bounds. It might even be the case that we reported to many variables which are fixed to
12634  * one.
12635  */
12636  *result = SCIP_SUCCESS;
12637 
12638  return SCIP_OKAY;
12639 }
12640 
12641 /** variable rounding lock method of constraint handler */
12642 static
12643 SCIP_DECL_CONSLOCK(consLockKnapsack)
12644 { /*lint --e{715}*/
12645  SCIP_CONSDATA* consdata;
12646  int i;
12647 
12648  consdata = SCIPconsGetData(cons);
12649  assert(consdata != NULL);
12651  for( i = 0; i < consdata->nvars; i++)
12652  {
12653  SCIP_CALL( SCIPaddVarLocks(scip, consdata->vars[i], nlocksneg, nlockspos) );
12654  }
12655 
12656  return SCIP_OKAY;
12657 }
12658 
12659 
12660 /** variable deletion method of constraint handler */
12661 static
12662 SCIP_DECL_CONSDELVARS(consDelvarsKnapsack)
12663 {
12664  assert(scip != NULL);
12665  assert(conshdlr != NULL);
12666  assert(conss != NULL || nconss == 0);
12667 
12668  if( nconss > 0 )
12669  {
12670  SCIP_CALL( performVarDeletions(scip, conshdlr, conss, nconss) );
12671  }
12672 
12673  return SCIP_OKAY;
12674 }
12675 
12676 /** constraint display method of constraint handler */
12677 static
12678 SCIP_DECL_CONSPRINT(consPrintKnapsack)
12679 { /*lint --e{715}*/
12680  SCIP_CONSDATA* consdata;
12681  int i;
12682 
12683  assert( scip != NULL );
12684  assert( conshdlr != NULL );
12685  assert( cons != NULL );
12686 
12687  consdata = SCIPconsGetData(cons);
12688  assert(consdata != NULL);
12689 
12690  for( i = 0; i < consdata->nvars; ++i )
12691  {
12692  if( i > 0 )
12693  SCIPinfoMessage(scip, file, " ");
12694  SCIPinfoMessage(scip, file, "%+"SCIP_LONGINT_FORMAT, consdata->weights[i]);
12695  SCIP_CALL( SCIPwriteVarName(scip, file, consdata->vars[i], TRUE) );
12696  }
12697  SCIPinfoMessage(scip, file, " <= %"SCIP_LONGINT_FORMAT"", consdata->capacity);
12698 
12699  return SCIP_OKAY;
12700 }
12701 
12702 /** constraint copying method of constraint handler */
12703 static
12704 SCIP_DECL_CONSCOPY(consCopyKnapsack)
12705 { /*lint --e{715}*/
12706  SCIP_VAR** sourcevars;
12707  SCIP_Longint* weights;
12708  SCIP_Real* coefs;
12709  const char* consname;
12710  int nvars;
12711  int v;
12712 
12713  /* get variables and coefficients of the source constraint */
12714  sourcevars = SCIPgetVarsKnapsack(sourcescip, sourcecons);
12715  nvars = SCIPgetNVarsKnapsack(sourcescip, sourcecons);
12716  weights = SCIPgetWeightsKnapsack(sourcescip, sourcecons);
12717 
12718  SCIP_CALL( SCIPallocBufferArray(scip, &coefs, nvars) );
12719  for( v = 0; v < nvars; ++v )
12720  coefs[v] = (SCIP_Real) weights[v];
12721 
12722  if( name != NULL )
12723  consname = name;
12724  else
12725  consname = SCIPconsGetName(sourcecons);
12726 
12727  /* copy the logic using the linear constraint copy method */
12728  SCIP_CALL( SCIPcopyConsLinear(scip, cons, sourcescip, consname, nvars, sourcevars, coefs,
12729  -SCIPinfinity(scip), (SCIP_Real) SCIPgetCapacityKnapsack(sourcescip, sourcecons), varmap, consmap,
12730  initial, separate, enforce, check, propagate, local, modifiable, dynamic, removable, stickingatnode, global, valid) );
12731  assert(cons != NULL);
12732 
12733  SCIPfreeBufferArray(scip, &coefs);
12734 
12735  return SCIP_OKAY;
12736 }
12737 
12738 /** constraint parsing method of constraint handler */
12739 static
12740 SCIP_DECL_CONSPARSE(consParseKnapsack)
12741 { /*lint --e{715}*/
12742  SCIP_VAR* var;
12743  SCIP_Longint weight;
12744  SCIP_VAR** vars;
12745  SCIP_Longint* weights;
12746  SCIP_Longint capacity;
12747  char* endptr;
12748  int nread;
12749  int nvars;
12750  int varssize;
12751 
12752  assert(scip != NULL);
12753  assert(success != NULL);
12754  assert(str != NULL);
12755  assert(name != NULL);
12756  assert(cons != NULL);
12757 
12758  *success = TRUE;
12759 
12760  nvars = 0;
12761  varssize = 5;
12762  SCIP_CALL( SCIPallocBufferArray(scip, &vars, varssize) );
12763  SCIP_CALL( SCIPallocBufferArray(scip, &weights, varssize) );
12764 
12765  while( *str != '\0' )
12766  {
12767  /* try to parse coefficient, and stop if not successful (probably reached <=) */
12768  if( sscanf(str, "%"SCIP_LONGINT_FORMAT"%n", &weight, &nread) < 1 )
12769  break;
12770 
12771  str += nread;
12772 
12773  /* skip whitespace */
12774  while( isspace((int)*str) )
12775  ++str;
12776 
12777  /* parse variable name */
12778  SCIP_CALL( SCIPparseVarName(scip, str, &var, &endptr) );
12779  if( var == NULL )
12780  {
12781  SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, "unknown variable name at '%s'\n", str);
12782  *success = FALSE;
12783  break;
12784  }
12785 
12786  str = endptr;
12787 
12788  /* store weight and variable */
12789  if( varssize <= nvars )
12790  {
12791  varssize = SCIPcalcMemGrowSize(scip, varssize+1);
12792  SCIP_CALL( SCIPreallocBufferArray(scip, &vars, varssize) );
12793  SCIP_CALL( SCIPreallocBufferArray(scip, &weights, varssize) );
12794  }
12795 
12796  vars[nvars] = var;
12797  weights[nvars] = weight;
12798  ++nvars;
12799 
12800  /* skip whitespace */
12801  while( isspace((int)*str) )
12802  ++str;
12803  }
12804 
12805  if( *success )
12806  {
12807  if( strncmp(str, "<= ", 3) != 0 )
12808  {
12809  SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, "expected '<= ' at begin of '%s'\n", str);
12810  *success = FALSE;
12811  }
12812  else
12813  {
12814  str += 3;
12815  }
12816  }
12817 
12818  if( *success )
12819  {
12820  if( sscanf(str, "%"SCIP_LONGINT_FORMAT, &capacity) != 1 )
12821  {
12822  SCIPverbMessage(scip, SCIP_VERBLEVEL_MINIMAL, NULL, "error parsing capacity from '%s'\n", str);
12823  *success = FALSE;
12824  }
12825  else
12826  {
12827  SCIP_CALL( SCIPcreateConsKnapsack(scip, cons, name, nvars, vars, weights, capacity,
12828  initial, separate, enforce, check, propagate, local, modifiable, dynamic, removable, stickingatnode) );
12829  }
12830  }
12831 
12832  SCIPfreeBufferArray(scip, &vars);
12833  SCIPfreeBufferArray(scip, &weights);
12834 
12835  return SCIP_OKAY;
12836 }
12837 
12838 /** constraint method of constraint handler which returns the variables (if possible) */
12839 static
12840 SCIP_DECL_CONSGETVARS(consGetVarsKnapsack)
12841 { /*lint --e{715}*/
12842  SCIP_CONSDATA* consdata;
12843 
12844  consdata = SCIPconsGetData(cons);
12845  assert(consdata != NULL);
12846 
12847  if( varssize < consdata->nvars )
12848  (*success) = FALSE;
12849  else
12850  {
12851  assert(vars != NULL);
12852 
12853  BMScopyMemoryArray(vars, consdata->vars, consdata->nvars);
12854  (*success) = TRUE;
12855  }
12856 
12857  return SCIP_OKAY;
12858 }
12859 
12860 /** constraint method of constraint handler which returns the number of variables (if possible) */
12861 static
12862 SCIP_DECL_CONSGETNVARS(consGetNVarsKnapsack)
12863 { /*lint --e{715}*/
12864  SCIP_CONSDATA* consdata;
12865 
12866  consdata = SCIPconsGetData(cons);
12867  assert(consdata != NULL);
12868 
12869  (*nvars) = consdata->nvars;
12870  (*success) = TRUE;
12871 
12872  return SCIP_OKAY;
12873 }
12874 
12875 /*
12876  * Event handler
12877  */
12878 
12879 /** execution method of bound change event handler */
12880 static
12881 SCIP_DECL_EVENTEXEC(eventExecKnapsack)
12882 { /*lint --e{715}*/
12883  assert(eventdata != NULL);
12884  assert(eventdata->consdata != NULL);
12885 
12886  switch( SCIPeventGetType(event) )
12887  {
12888  SCIP_CONSHDLR* conshdlr;
12889  SCIP_CONSHDLRDATA* conshdlrdata;
12890 
12892  eventdata->consdata->onesweightsum += eventdata->weight;
12893  eventdata->consdata->propagated = FALSE;
12894  eventdata->consdata->presolved = FALSE;
12895  break;
12897  eventdata->consdata->onesweightsum -= eventdata->weight;
12898 
12899  conshdlr = SCIPfindConshdlr(scip, CONSHDLR_NAME);
12900  assert(conshdlr != NULL);
12901  conshdlrdata = SCIPconshdlrGetData(conshdlr);
12902  assert(conshdlrdata != NULL);
12903 
12904  if( conshdlrdata->negatedclique )
12905  {
12906  /* if a variable fixed to 1 is unfixed, it is possible, that it can be fixed to 1 again */
12907  eventdata->consdata->propagated = FALSE;
12908  }
12909 
12910  break;
12912  /* if a variable fixed to 0 is unfixed, it is possible, that it can be fixed to 0 again */
12913  eventdata->consdata->propagated = FALSE;
12914  break;
12915  case SCIP_EVENTTYPE_VARFIXED: /* the variable should be removed from the constraint in presolving */
12916  if( !eventdata->consdata->existmultaggr )
12917  {
12918  SCIP_VAR* var = SCIPeventGetVar(event);
12919  assert(var != NULL);
12920 
12922  eventdata->consdata->existmultaggr = TRUE;
12923  }
12924  /*lint -fallthrough*/
12925  case SCIP_EVENTTYPE_IMPLADDED: /* further preprocessing might be possible due to additional implications */
12926  eventdata->consdata->presolved = FALSE;
12927  break;
12929  eventdata->consdata->varsdeleted = TRUE;
12930  break;
12931  default:
12932  SCIPerrorMessage("invalid event type %x\n", SCIPeventGetType(event));
12933  return SCIP_INVALIDDATA;
12934  }
12935 
12936  return SCIP_OKAY;
12937 }
12938 
12939 
12940 /*
12941  * constraint specific interface methods
12942  */
12943 
12944 /** creates the handler for knapsack constraints and includes it in SCIP */
12946  SCIP* scip /**< SCIP data structure */
12947  )
12948 {
12949  SCIP_EVENTHDLRDATA* eventhdlrdata;
12950  SCIP_CONSHDLRDATA* conshdlrdata;
12951  SCIP_CONSHDLR* conshdlr;
12953  /* create knapsack constraint handler data */
12954  SCIP_CALL( SCIPallocMemory(scip, &conshdlrdata) );
12955 
12956  /* include event handler for bound change events */
12957  eventhdlrdata = NULL;
12958  conshdlrdata->eventhdlr = NULL;
12959  SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &(conshdlrdata->eventhdlr), EVENTHDLR_NAME, EVENTHDLR_DESC,
12960  eventExecKnapsack, eventhdlrdata) );
12961 
12962  /* get event handler for bound change events */
12963  if( conshdlrdata->eventhdlr == NULL )
12964  {
12965  SCIPerrorMessage("event handler for knapsack constraints not found\n");
12966  return SCIP_PLUGINNOTFOUND;
12967  }
12968 
12969  /* include constraint handler */
12972  consEnfolpKnapsack, consEnfopsKnapsack, consCheckKnapsack, consLockKnapsack,
12973  conshdlrdata) );
12974 
12975  assert(conshdlr != NULL);
12976 
12977  /* set non-fundamental callbacks via specific setter functions */
12978  SCIP_CALL( SCIPsetConshdlrCopy(scip, conshdlr, conshdlrCopyKnapsack, consCopyKnapsack) );
12979  SCIP_CALL( SCIPsetConshdlrDelete(scip, conshdlr, consDeleteKnapsack) );
12980  SCIP_CALL( SCIPsetConshdlrDelvars(scip, conshdlr, consDelvarsKnapsack) );
12981  SCIP_CALL( SCIPsetConshdlrExit(scip, conshdlr, consExitKnapsack) );
12982  SCIP_CALL( SCIPsetConshdlrExitpre(scip, conshdlr, consExitpreKnapsack) );
12983  SCIP_CALL( SCIPsetConshdlrExitsol(scip, conshdlr, consExitsolKnapsack) );
12984  SCIP_CALL( SCIPsetConshdlrFree(scip, conshdlr, consFreeKnapsack) );
12985  SCIP_CALL( SCIPsetConshdlrGetVars(scip, conshdlr, consGetVarsKnapsack) );
12986  SCIP_CALL( SCIPsetConshdlrGetNVars(scip, conshdlr, consGetNVarsKnapsack) );
12987  SCIP_CALL( SCIPsetConshdlrInit(scip, conshdlr, consInitKnapsack) );
12988  SCIP_CALL( SCIPsetConshdlrInitpre(scip, conshdlr, consInitpreKnapsack) );
12989  SCIP_CALL( SCIPsetConshdlrInitlp(scip, conshdlr, consInitlpKnapsack) );
12990  SCIP_CALL( SCIPsetConshdlrParse(scip, conshdlr, consParseKnapsack) );
12991  SCIP_CALL( SCIPsetConshdlrPresol(scip, conshdlr, consPresolKnapsack,CONSHDLR_MAXPREROUNDS, CONSHDLR_DELAYPRESOL) );
12992  SCIP_CALL( SCIPsetConshdlrPrint(scip, conshdlr, consPrintKnapsack) );
12993  SCIP_CALL( SCIPsetConshdlrProp(scip, conshdlr, consPropKnapsack, CONSHDLR_PROPFREQ, CONSHDLR_DELAYPROP,
12995  SCIP_CALL( SCIPsetConshdlrResprop(scip, conshdlr, consRespropKnapsack) );
12996  SCIP_CALL( SCIPsetConshdlrSepa(scip, conshdlr, consSepalpKnapsack, consSepasolKnapsack, CONSHDLR_SEPAFREQ,
12998  SCIP_CALL( SCIPsetConshdlrTrans(scip, conshdlr, consTransKnapsack) );
12999 
13000  if( SCIPfindConshdlr(scip,"linear") != NULL )
13001  {
13002  /* include the linear constraint to knapsack constraint upgrade in the linear constraint handler */
13004  }
13005 
13006  /* add knapsack constraint handler parameters */
13007  SCIP_CALL( SCIPaddIntParam(scip,
13008  "constraints/"CONSHDLR_NAME"/sepacardfreq",
13009  "multiplier on separation frequency, how often knapsack cuts are separated (-1: never, 0: only at root)",
13010  &conshdlrdata->sepacardfreq, TRUE, DEFAULT_SEPACARDFREQ, -1, INT_MAX, NULL, NULL) );
13012  "constraints/"CONSHDLR_NAME"/maxcardbounddist",
13013  "maximal relative distance from current node's dual bound to primal bound compared to best node's dual bound for separating knapsack cuts",
13014  &conshdlrdata->maxcardbounddist, TRUE, DEFAULT_MAXCARDBOUNDDIST, 0.0, 1.0, NULL, NULL) );
13015  SCIP_CALL( SCIPaddIntParam(scip,
13016  "constraints/"CONSHDLR_NAME"/maxrounds",
13017  "maximal number of separation rounds per node (-1: unlimited)",
13018  &conshdlrdata->maxrounds, FALSE, DEFAULT_MAXROUNDS, -1, INT_MAX, NULL, NULL) );
13019  SCIP_CALL( SCIPaddIntParam(scip,
13020  "constraints/"CONSHDLR_NAME"/maxroundsroot",
13021  "maximal number of separation rounds per node in the root node (-1: unlimited)",
13022  &conshdlrdata->maxroundsroot, FALSE, DEFAULT_MAXROUNDSROOT, -1, INT_MAX, NULL, NULL) );
13023  SCIP_CALL( SCIPaddIntParam(scip,
13024  "constraints/"CONSHDLR_NAME"/maxsepacuts",
13025  "maximal number of cuts separated per separation round",
13026  &conshdlrdata->maxsepacuts, FALSE, DEFAULT_MAXSEPACUTS, 0, INT_MAX, NULL, NULL) );
13027  SCIP_CALL( SCIPaddIntParam(scip,
13028  "constraints/"CONSHDLR_NAME"/maxsepacutsroot",
13029  "maximal number of cuts separated per separation round in the root node",
13030  &conshdlrdata->maxsepacutsroot, FALSE, DEFAULT_MAXSEPACUTSROOT, 0, INT_MAX, NULL, NULL) );
13032  "constraints/"CONSHDLR_NAME"/disaggregation",
13033  "should disaggregation of knapsack constraints be allowed in preprocessing?",
13034  &conshdlrdata->disaggregation, TRUE, DEFAULT_DISAGGREGATION, NULL, NULL) );
13036  "constraints/"CONSHDLR_NAME"/simplifyinequalities",
13037  "should presolving try to simplify knapsacks",
13038  &conshdlrdata->simplifyinequalities, TRUE, DEFAULT_SIMPLIFYINEQUALITIES, NULL, NULL) );
13040  "constraints/"CONSHDLR_NAME"/negatedclique",
13041  "should negated clique information be used in solving process",
13042  &conshdlrdata->negatedclique, TRUE, DEFAULT_NEGATEDCLIQUE, NULL, NULL) );
13044  "constraints/"CONSHDLR_NAME"/presolpairwise",
13045  "should pairwise constraint comparison be performed in presolving?",
13046  &conshdlrdata->presolpairwise, TRUE, DEFAULT_PRESOLPAIRWISE, NULL, NULL) );
13048  "constraints/"CONSHDLR_NAME"/presolusehashing",
13049  "should hash table be used for detecting redundant constraints in advance",
13050  &conshdlrdata->presolusehashing, TRUE, DEFAULT_PRESOLUSEHASHING, NULL, NULL) );
13052  "constraints/"CONSHDLR_NAME"/dualpresolving",
13053  "should dual presolving steps be performed?",
13054  &conshdlrdata->dualpresolving, TRUE, DEFAULT_DUALPRESOLVING, NULL, NULL) );
13056  "constraints/"CONSHDLR_NAME"/usegubs",
13057  "should GUB information be used for separation?",
13058  &conshdlrdata->usegubs, TRUE, DEFAULT_USEGUBS, NULL, NULL) );
13060  "constraints/"CONSHDLR_NAME"/detectcutoffbound",
13061  "should presolving try to detect constraints parallel to the objective function defining an upper bound and prevent these constraints from entering the LP?",
13062  &conshdlrdata->detectcutoffbound, TRUE, DEFAULT_DETECTCUTOFFBOUND, NULL, NULL) );
13064  "constraints/"CONSHDLR_NAME"/detectlowerbound",
13065  "should presolving try to detect constraints parallel to the objective function defining a lower bound and prevent these constraints from entering the LP?",
13066  &conshdlrdata->detectlowerbound, TRUE, DEFAULT_DETECTLOWERBOUND, NULL, NULL) );
13067 
13068  return SCIP_OKAY;
13069 }
13070 
13071 /** creates and captures a knapsack constraint
13072  *
13073  * @note the constraint gets captured, hence at one point you have to release it using the method SCIPreleaseCons()
13074  */
13076  SCIP* scip, /**< SCIP data structure */
13077  SCIP_CONS** cons, /**< pointer to hold the created constraint */
13078  const char* name, /**< name of constraint */
13079  int nvars, /**< number of items in the knapsack */
13080  SCIP_VAR** vars, /**< array with item variables */
13081  SCIP_Longint* weights, /**< array with item weights */
13082  SCIP_Longint capacity, /**< capacity of knapsack */
13083  SCIP_Bool initial, /**< should the LP relaxation of constraint be in the initial LP?
13084  * Usually set to TRUE. Set to FALSE for 'lazy constraints'. */
13085  SCIP_Bool separate, /**< should the constraint be separated during LP processing?
13086  * Usually set to TRUE. */
13087  SCIP_Bool enforce, /**< should the constraint be enforced during node processing?
13088  * TRUE for model constraints, FALSE for additional, redundant constraints. */
13089  SCIP_Bool check, /**< should the constraint be checked for feasibility?
13090  * TRUE for model constraints, FALSE for additional, redundant constraints. */
13091  SCIP_Bool propagate, /**< should the constraint be propagated during node processing?
13092  * Usually set to TRUE. */
13093  SCIP_Bool local, /**< is constraint only valid locally?
13094  * Usually set to FALSE. Has to be set to TRUE, e.g., for branching constraints. */
13095  SCIP_Bool modifiable, /**< is constraint modifiable (subject to column generation)?
13096  * Usually set to FALSE. In column generation applications, set to TRUE if pricing
13097  * adds coefficients to this constraint. */
13098  SCIP_Bool dynamic, /**< is constraint subject to aging?
13099  * Usually set to FALSE. Set to TRUE for own cuts which
13100  * are separated as constraints. */
13101  SCIP_Bool removable, /**< should the relaxation be removed from the LP due to aging or cleanup?
13102  * Usually set to FALSE. Set to TRUE for 'lazy constraints' and 'user cuts'. */
13103  SCIP_Bool stickingatnode /**< should the constraint always be kept at the node where it was added, even
13104  * if it may be moved to a more global node?
13105  * Usually set to FALSE. Set to TRUE to for constraints that represent node data. */
13106  )
13107 {
13108  SCIP_CONSHDLRDATA* conshdlrdata;
13109  SCIP_CONSHDLR* conshdlr;
13110  SCIP_CONSDATA* consdata;
13111 
13112  /* find the knapsack constraint handler */
13113  conshdlr = SCIPfindConshdlr(scip, CONSHDLR_NAME);
13114  if( conshdlr == NULL )
13115  {
13116  SCIPerrorMessage("knapsack constraint handler not found\n");
13117  return SCIP_PLUGINNOTFOUND;
13118  }
13119 
13120  /* get event handler */
13121  conshdlrdata = SCIPconshdlrGetData(conshdlr);
13122  assert(conshdlrdata != NULL);
13123  assert(conshdlrdata->eventhdlr != NULL);
13124 
13125  /* create constraint data */
13126  SCIP_CALL( consdataCreate(scip, &consdata, conshdlrdata->eventhdlr, nvars, vars, weights, capacity) );
13127 
13128  /* create constraint */
13129  SCIP_CALL( SCIPcreateCons(scip, cons, name, conshdlr, consdata, initial, separate, enforce, check, propagate,
13130  local, modifiable, dynamic, removable, stickingatnode) );
13131 
13132  return SCIP_OKAY;
13133 }
13134 
13135 /** creates and captures a knapsack constraint
13136  * in its most basic version, i. e., all constraint flags are set to their basic value as explained for the
13137  * method SCIPcreateConsKnapsack(); all flags can be set via SCIPsetConsFLAGNAME-methods in scip.h
13138  *
13139  * @see SCIPcreateConsKnapsack() for information about the basic constraint flag configuration
13140  *
13141  * @note the constraint gets captured, hence at one point you have to release it using the method SCIPreleaseCons()
13142  */
13144  SCIP* scip, /**< SCIP data structure */
13145  SCIP_CONS** cons, /**< pointer to hold the created constraint */
13146  const char* name, /**< name of constraint */
13147  int nvars, /**< number of items in the knapsack */
13148  SCIP_VAR** vars, /**< array with item variables */
13149  SCIP_Longint* weights, /**< array with item weights */
13150  SCIP_Longint capacity /**< capacity of knapsack */
13151  )
13152 {
13153  assert(scip != NULL);
13154 
13155  SCIP_CALL( SCIPcreateConsKnapsack(scip, cons, name, nvars, vars, weights, capacity,
13156  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
13157 
13158  return SCIP_OKAY;
13159 }
13160 
13161 /** adds new item to knapsack constraint */
13163  SCIP* scip, /**< SCIP data structure */
13164  SCIP_CONS* cons, /**< constraint data */
13165  SCIP_VAR* var, /**< item variable */
13166  SCIP_Longint weight /**< item weight */
13167  )
13168 {
13169  assert(var != NULL);
13170 
13171  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13172  {
13173  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13174  return SCIP_INVALIDDATA;
13175  }
13176 
13177  SCIP_CALL( addCoef(scip, cons, var, weight) );
13178 
13179  return SCIP_OKAY;
13180 }
13181 
13182 /** gets the capacity of the knapsack constraint */
13184  SCIP* scip, /**< SCIP data structure */
13185  SCIP_CONS* cons /**< constraint data */
13186  )
13187 {
13188  SCIP_CONSDATA* consdata;
13189 
13191  {
13192  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13193  SCIPABORT();
13194  return 0; /*lint !e527*/
13195  }
13196 
13197  consdata = SCIPconsGetData(cons);
13198  assert(consdata != NULL);
13199 
13200  return consdata->capacity;
13201 }
13202 
13203 /** changes capacity of the knapsack constraint
13204  *
13205  * @note This method can only be called during problem creation stage (SCIP_STAGE_PROBLEM)
13206  */
13208  SCIP* scip, /**< SCIP data structure */
13209  SCIP_CONS* cons, /**< constraint data */
13210  SCIP_Longint capacity /**< new capacity of knapsack */
13211  )
13212 {
13213  SCIP_CONSDATA* consdata;
13215  if( strcmp(SCIPconshdlrGetName(SCIPconsGetHdlr(cons)), CONSHDLR_NAME) != 0 )
13216  {
13217  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13218  return SCIP_INVALIDDATA;
13219  }
13220 
13221  if( SCIPgetStage(scip) != SCIP_STAGE_PROBLEM )
13222  {
13223  SCIPerrorMessage("method can only be called during problem creation stage\n");
13224  return SCIP_INVALIDDATA;
13225  }
13226 
13227  consdata = SCIPconsGetData(cons);
13228  assert(consdata != NULL);
13229 
13230  consdata->capacity = capacity;
13231 
13232  return SCIP_OKAY;
13233 }
13234 
13235 /** gets the number of items in the knapsack constraint */
13237  SCIP* scip, /**< SCIP data structure */
13238  SCIP_CONS* cons /**< constraint data */
13239  )
13240 {
13241  SCIP_CONSDATA* consdata;
13242 
13244  {
13245  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13246  SCIPABORT();
13247  return -1; /*lint !e527*/
13248  }
13249 
13250  consdata = SCIPconsGetData(cons);
13251  assert(consdata != NULL);
13252 
13253  return consdata->nvars;
13254 }
13255 
13256 /** gets the array of variables in the knapsack constraint; the user must not modify this array! */
13258  SCIP* scip, /**< SCIP data structure */
13259  SCIP_CONS* cons /**< constraint data */
13260  )
13261 {
13262  SCIP_CONSDATA* consdata;
13263 
13265  {
13266  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13267  SCIPABORT();
13268  return NULL; /*lint !e527*/
13269  }
13270 
13271  consdata = SCIPconsGetData(cons);
13272  assert(consdata != NULL);
13273 
13274  return consdata->vars;
13275 }
13276 
13277 /** gets the array of weights in the knapsack constraint; the user must not modify this array! */
13279  SCIP* scip, /**< SCIP data structure */
13280  SCIP_CONS* cons /**< constraint data */
13281  )
13282 {
13283  SCIP_CONSDATA* consdata;
13284 
13286  {
13287  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13288  SCIPABORT();
13289  return NULL; /*lint !e527*/
13290  }
13291 
13292  consdata = SCIPconsGetData(cons);
13293  assert(consdata != NULL);
13294 
13295  return consdata->weights;
13296 }
13297 
13298 /** gets the dual solution of the knapsack constraint in the current LP */
13300  SCIP* scip, /**< SCIP data structure */
13301  SCIP_CONS* cons /**< constraint data */
13302  )
13303 {
13304  SCIP_CONSDATA* consdata;
13305 
13307  {
13308  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13309  SCIPABORT();
13310  return SCIP_INVALID; /*lint !e527*/
13311  }
13312 
13313  consdata = SCIPconsGetData(cons);
13314  assert(consdata != NULL);
13315 
13316  if( consdata->row != NULL )
13317  return SCIProwGetDualsol(consdata->row);
13318  else
13319  return 0.0;
13320 }
13321 
13322 /** gets the dual Farkas value of the knapsack constraint in the current infeasible LP */
13324  SCIP* scip, /**< SCIP data structure */
13325  SCIP_CONS* cons /**< constraint data */
13326  )
13327 {
13328  SCIP_CONSDATA* consdata;
13329 
13331  {
13332  SCIPerrorMessage("constraint is not a knapsack constraint\n");
13333  SCIPABORT();
13334  return SCIP_INVALID; /*lint !e527*/
13335  }
13336 
13337  consdata = SCIPconsGetData(cons);
13338  assert(consdata != NULL);
13339 
13340  if( consdata->row != NULL )
13341  return SCIProwGetDualfarkas(consdata->row);
13342  else
13343  return 0.0;
13344 }
13345 
13346 /** returns the linear relaxation of the given knapsack constraint; may return NULL if no LP row was yet created;
13347  * the user must not modify the row!
13348  */
13350  SCIP* scip, /**< SCIP data structure */
13351  SCIP_CONS* cons /**< constraint data */
13352  )
13353 {
13354  SCIP_CONSDATA* consdata;
13355 
13357  {
13358  SCIPerrorMessage("constraint is not a knapsack\n");
13359  SCIPABORT();
13360  return NULL; /*lint !e527*/
13361  }
13362 
13363  consdata = SCIPconsGetData(cons);
13364  assert(consdata != NULL);
13365 
13366  return consdata->row;
13367 }
13368 
SCIP_VAR ** SCIPvarGetImplVars(SCIP_VAR *var, SCIP_Bool varfixing)
Definition: var.c:16679
int SCIPgetNLPBranchCands(SCIP *scip)
Definition: scip.c:30035
SCIP_RETCODE SCIPfixVar(SCIP *scip, SCIP_VAR *var, SCIP_Real fixedval, SCIP_Bool *infeasible, SCIP_Bool *fixed)
Definition: scip.c:20784
void SCIPsortRealInt(SCIP_Real *realarray, int *intarray, int len)
#define KNAPSACKRELAX_MAXSCALE
Definition: cons_knapsack.c:71
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38254
void SCIPsortPtrInt(void **ptrarray, int *intarray, SCIP_DECL_SORTPTRCOMP((*ptrcomp)), int len)
enum SCIP_BoundType SCIP_BOUNDTYPE
Definition: type_lp.h:50
int * gubconssidx
SCIP_Real SCIPvarGetMultaggrConstant(SCIP_VAR *var)
Definition: var.c:16183
#define DEFAULT_DETECTCUTOFFBOUND
Definition: cons_knapsack.c:97
SCIP_RETCODE SCIPwriteVarName(SCIP *scip, FILE *file, SCIP_VAR *var, SCIP_Bool type)
Definition: scip.c:14102
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
Definition: scip.c:38397
static SCIP_RETCODE GUBsetFree(SCIP *scip, SCIP_GUBSET **gubset)
int SCIPgetNVars(SCIP *scip)
Definition: scip.c:10071
SCIP_CONSHDLR * SCIPfindConshdlr(SCIP *scip, const char *name)
Definition: scip.c:5600
static SCIP_RETCODE performVarDeletions(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_CONS **conss, int nconss)
static SCIP_RETCODE mergeMultiples(SCIP *scip, SCIP_CONS *cons, SCIP_Bool *cutoff)
SCIP_RETCODE SCIPsetConshdlrTrans(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSTRANS((*constrans)))
Definition: scip.c:5332
GUBVarstatus
SCIP_RETCODE SCIPresetConsAge(SCIP *scip, SCIP_CONS *cons)
Definition: scip.c:23286
static SCIP_RETCODE getCover(SCIP *scip, SCIP_VAR **vars, int nvars, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Real *solvals, int *covervars, int *noncovervars, int *ncovervars, int *nnoncovervars, SCIP_Longint *coverweight, SCIP_Bool *found, SCIP_Bool modtransused, int *ntightened, SCIP_Bool *fractional)
SCIP_VAR ** SCIPcliqueGetVars(SCIP_CLIQUE *clique)
Definition: implics.c:2372
const char * SCIPvarGetName(SCIP_VAR *var)
Definition: var.c:15788
static SCIP_DECL_EVENTEXEC(eventExecKnapsack)
int SCIPvarGetNBinImpls(SCIP_VAR *var, SCIP_Bool varfixing)
Definition: var.c:16662
#define CONSHDLR_DESC
Definition: cons_knapsack.c:40
#define CONSHDLR_PROP_TIMING
Definition: cons_knapsack.c:55
#define SCIP_EVENTTYPE_VARFIXED
Definition: type_event.h:48
static SCIP_RETCODE GUBsetCalcCliquePartition(SCIP *const scip, SCIP_VAR **const vars, int const nvars, int *const cliquepartition, int *const ncliques, SCIP_Real *solvals)
SCIP_Real SCIPgetDualfarkasKnapsack(SCIP *scip, SCIP_CONS *cons)
static SCIP_DECL_CONSRESPROP(consRespropKnapsack)
static SCIP_RETCODE GUBsetMoveVar(SCIP *scip, SCIP_GUBSET *gubset, SCIP_VAR **vars, int var, int oldgubcons, int newgubcons)
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
Definition: scip.c:38360
static SCIP_RETCODE addNegatedCliques(SCIP *const scip, SCIP_CONS *const cons, SCIP_Bool *const cutoff, int *const nbdchgs)
SCIP_RETCODE SCIPhashtableInsert(SCIP_HASHTABLE *hashtable, void *element)
Definition: misc.c:1374
#define SCIPfreeBlockMemoryArray(scip, ptr, num)
Definition: scip.h:19206
static SCIP_DECL_CONSINIT(consInitKnapsack)
static SCIP_DECL_HASHKEYVAL(hashKeyValKnapsackcons)
SCIP_RETCODE SCIPdelCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip.c:10913
SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
Definition: var.c:15968
void SCIPsortDownLongPtrPtrIntInt(SCIP_Longint *longarray, void **ptrarray1, void **ptrarray2, int *intarray1, int *intarray2, int len)
static SCIP_RETCODE separateCons(SCIP *scip, SCIP_CONS *cons, SCIP_SOL *sol, SCIP_Bool sepacuts, SCIP_Bool usegubs, SCIP_Bool *cutoff, int *ncuts)
SCIP_Bool SCIPisFeasLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38667
GUBCONSSTATUS * gubconsstatus
SCIP_VAR ** SCIPgetVars(SCIP *scip)
Definition: scip.c:10026
#define SCIP_MAXSTRLEN
Definition: def.h:196
SCIP_CLIQUE ** SCIPvarGetCliques(SCIP_VAR *var, SCIP_Bool varfixing)
Definition: var.c:16748
SCIP_RETCODE SCIPsetConshdlrResprop(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSRESPROP((*consresprop)))
Definition: scip.c:5378
SCIP_Bool SCIPisEfficacious(SCIP *scip, SCIP_Real efficacy)
Definition: scip.c:28166
SCIP_RETCODE SCIPsetConshdlrSepa(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSSEPALP((*conssepalp)), SCIP_DECL_CONSSEPASOL((*conssepasol)), int sepafreq, int sepapriority, SCIP_Bool delaysepa)
Definition: scip.c:4990
#define CONSHDLR_ENFOPRIORITY
Definition: cons_knapsack.c:42
#define NULL
Definition: lpi_spx.cpp:129
SCIP_Real SCIPvarGetLbLocal(SCIP_VAR *var)
Definition: var.c:16426
SCIP_Bool SCIPisStopped(SCIP *scip)
Definition: scip.c:1111
SCIP_Real SCIPfeasFloor(SCIP *scip, SCIP_Real val)
Definition: scip.c:38803
SCIP_VAR * SCIPeventGetVar(SCIP_EVENT *event)
Definition: event.c:940
SCIP_Real * SCIPvarGetVubConstants(SCIP_VAR *var)
Definition: var.c:16636
SCIP_Bool SCIPconsIsModifiable(SCIP_CONS *cons)
Definition: cons.c:7786
SCIP_VAR ** SCIPvarGetVlbVars(SCIP_VAR *var)
Definition: var.c:16574
static SCIP_RETCODE getLiftingSequenceGUB(SCIP *scip, SCIP_GUBSET *gubset, SCIP_Real *solvals, SCIP_Longint *weights, int *varsC1, int *varsC2, int *varsF, int *varsR, int nvarsC1, int nvarsC2, int nvarsF, int nvarsR, int *gubconsGC1, int *gubconsGC2, int *gubconsGFC1, int *gubconsGR, int *ngubconsGC1, int *ngubconsGC2, int *ngubconsGFC1, int *ngubconsGR, int *ngubconscapexceed, int *maxgubvarssize)
static SCIP_RETCODE separateSequLiftedMinimalCoverInequality(SCIP *scip, SCIP_CONS *cons, SCIP_SEPA *sepa, SCIP_VAR **vars, int nvars, int ntightened, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Real *solvals, int *mincovervars, int *nonmincovervars, int nmincovervars, int nnonmincovervars, SCIP_SOL *sol, SCIP_GUBSET *gubset, SCIP_Bool *cutoff, int *ncuts)
#define LINCONSUPGD_PRIORITY
Definition: cons_knapsack.c:60
SCIP_Bool SCIPconsIsActive(SCIP_CONS *cons)
Definition: cons.c:7618
SCIP_RETCODE SCIPsetConshdlrCopy(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSHDLRCOPY((*conshdlrcopy)), SCIP_DECL_CONSCOPY((*conscopy)))
Definition: scip.c:5078
SCIP_Real SCIPcutoffbounddelta(SCIP *scip)
Definition: scip.c:37789
SCIP_RETCODE SCIPupdateCutoffbound(SCIP *scip, SCIP_Real cutoffbound)
Definition: scip.c:35253
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition: var.c:16380
SCIP_Bool SCIPisCutEfficacious(SCIP *scip, SCIP_SOL *sol, SCIP_ROW *cut)
Definition: scip.c:28148
SCIP_Longint SCIPgetCapacityKnapsack(SCIP *scip, SCIP_CONS *cons)
static SCIP_RETCODE addCoef(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Longint weight)
static SCIP_DECL_CONSINITLP(consInitlpKnapsack)
SCIP_RETCODE SCIPseparateRelaxedKnapsack(SCIP *scip, SCIP_CONS *cons, SCIP_SEPA *sepa, int nknapvars, SCIP_VAR **knapvars, SCIP_Real *knapvals, SCIP_Real valscale, SCIP_Real rhs, SCIP_SOL *sol, SCIP_Bool *cutoff, int *ncuts)
SCIP_RETCODE SCIPupdateLocalLowerbound(SCIP *scip, SCIP_Real newbound)
Definition: scip.c:11674
SCIP_GUBCONS ** gubconss
SCIP_RETCODE SCIPgetNegatedVars(SCIP *scip, int nvars, SCIP_VAR **vars, SCIP_VAR **negvars)
Definition: scip.c:15438
static SCIP_DECL_CONSCHECK(consCheckKnapsack)
struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
Definition: type_event.h:129
int SCIPvarGetNVubs(SCIP_VAR *var)
Definition: var.c:16604
SCIP_Bool SCIPisConflictAnalysisApplicable(SCIP *scip)
Definition: scip.c:22044
static SCIP_RETCODE checkParallelObjective(SCIP *scip, SCIP_CONS *cons, SCIP_CONSHDLRDATA *conshdlrdata)
static SCIP_RETCODE separateSupLiftedMinimalCoverInequality(SCIP *scip, SCIP_CONS *cons, SCIP_SEPA *sepa, SCIP_VAR **vars, int nvars, int ntightened, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Real *solvals, int *mincovervars, int *nonmincovervars, int nmincovervars, int nnonmincovervars, SCIP_Longint mincoverweight, SCIP_SOL *sol, SCIP_Bool *cutoff, int *ncuts)
GUBConsstatus
static SCIP_RETCODE dualWeightsTightening(SCIP *scip, SCIP_CONS *cons, int *ndelconss, int *nchgcoefs, int *nchgsides, int *naddconss)
static SCIP_RETCODE consdataFree(SCIP *scip, SCIP_CONSDATA **consdata, SCIP_EVENTHDLR *eventhdlr)
SCIP_RETCODE SCIPsetConshdlrGetVars(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSGETVARS((*consgetvars)))
Definition: scip.c:5562
#define CONSHDLR_SEPAFREQ
Definition: cons_knapsack.c:44
static SCIP_RETCODE propagateCons(SCIP *scip, SCIP_CONS *cons, SCIP_Bool *cutoff, SCIP_Bool *redundant, int *nfixedvars, SCIP_Bool usenegatedclique)
SCIP_RETCODE SCIPcreateConsSetpack(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
Definition: cons_setppc.c:8952
SCIP_Bool SCIPconsIsTransformed(SCIP_CONS *cons)
Definition: cons.c:7846
#define FALSE
Definition: def.h:52
#define MAX_CLIQUELENGTH
#define CONSHDLR_MAXPREROUNDS
Definition: cons_knapsack.c:49
SCIP_RETCODE SCIPincludeEventhdlrBasic(SCIP *scip, SCIP_EVENTHDLR **eventhdlrptr, const char *name, const char *desc, SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip.c:7209
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:7579
#define TRUE
Definition: def.h:51
#define SCIPdebug(x)
Definition: pub_message.h:74
static SCIP_RETCODE insertZerolist(SCIP *scip, int **liftcands, int *nliftcands, int **firstidxs, SCIP_Longint **zeroweightsums, int **zeroitems, int **nextidxs, int *zeroitemssize, int *nzeroitems, int probindex, SCIP_Bool value, int knapsackidx, SCIP_Longint knapsackweight, SCIP_Bool *memlimitreached)
SCIP_CONSHDLR * SCIPconsGetHdlr(SCIP_CONS *cons)
Definition: cons.c:7577
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
static SCIP_RETCODE separateSequLiftedExtendedWeightInequality(SCIP *scip, SCIP_CONS *cons, SCIP_SEPA *sepa, SCIP_VAR **vars, int nvars, int ntightened, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Real *solvals, int *feassetvars, int *nonfeassetvars, int nfeassetvars, int nnonfeassetvars, SCIP_SOL *sol, SCIP_Bool *cutoff, int *ncuts)
#define CONSHDLR_EAGERFREQ
Definition: cons_knapsack.c:46
SCIP_RETCODE SCIPsetConshdlrParse(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSPARSE((*consparse)))
Definition: scip.c:5539
static void consdataChgWeight(SCIP_CONSDATA *consdata, int item, SCIP_Longint newweight)
SCIP_Longint * SCIPgetWeightsKnapsack(SCIP *scip, SCIP_CONS *cons)
SCIP_RETCODE SCIPsetConshdlrPresol(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSPRESOL((*conspresol)), int maxprerounds, SCIP_Bool delaypresol)
Definition: scip.c:5271
static void getPartitionNoncovervars(SCIP *scip, SCIP_Real *solvals, int *noncovervars, int nnoncovervars, int *varsF, int *varsR, int *nvarsF, int *nvarsR)
SCIP_Bool SCIPisFeasIntegral(SCIP *scip, SCIP_Real val)
Definition: scip.c:38779
SCIP_Real SCIPgetCutoffbound(SCIP *scip)
Definition: scip.c:35229
#define SCIPfreeBuffer(scip, ptr)
Definition: scip.h:19219
SCIP_RETCODE SCIPaddClique(SCIP *scip, SCIP_VAR **vars, SCIP_Bool *values, int nvars, SCIP_Bool *infeasible, int *nbdchgs)
Definition: scip.c:19756
#define SCIPallocBlockMemoryArray(scip, ptr, num)
Definition: scip.h:19185
SCIP_RETCODE SCIPseparateKnapsackCuts(SCIP *scip, SCIP_CONS *cons, SCIP_SEPA *sepa, SCIP_VAR **vars, int nvars, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_SOL *sol, SCIP_Bool usegubs, SCIP_Bool *cutoff, int *ncuts)
SCIP_Real SCIPvarGetLbAtIndex(SCIP_VAR *var, SCIP_BDCHGIDX *bdchgidx, SCIP_Bool after)
Definition: var.c:15141
#define EVENTHDLR_NAME
Definition: cons_knapsack.c:57
#define SCIPdebugMessage
Definition: pub_message.h:77
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:31775
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip.h:19214
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
Definition: var.c:16227
SCIP_RETCODE SCIPcreateConsBasicKnapsack(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Longint *weights, SCIP_Longint capacity)
void SCIPsortDownRealInt(SCIP_Real *realarray, int *intarray, int len)
#define SCIP_LONGINT_MAX
Definition: def.h:108
SCIP_RETCODE SCIPincludeConshdlrKnapsack(SCIP *scip)
int SCIPgetNContVars(SCIP *scip)
Definition: scip.c:10251
SCIP_RETCODE SCIPsetConshdlrProp(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSPROP((*consprop)), int propfreq, SCIP_Bool delayprop, SCIP_PROPTIMING timingmask)
Definition: scip.c:5036
SCIP_RETCODE SCIPsetConsPropagated(SCIP *scip, SCIP_CONS *cons, SCIP_Bool propagate)
Definition: scip.c:22847
SCIP_Bool SCIPconsIsLocal(SCIP_CONS *cons)
Definition: cons.c:7776
SCIP_Bool SCIPconsIsSeparated(SCIP_CONS *cons)
Definition: cons.c:7716
SCIP_RETCODE SCIPprintCons(SCIP *scip, SCIP_CONS *cons, FILE *file)
Definition: scip.c:23934
SCIP_RETCODE SCIPaddCut(SCIP *scip, SCIP_SOL *sol, SCIP_ROW *cut, SCIP_Bool forcecut, SCIP_Bool *infeasible)
Definition: scip.c:28256
Constraint handler for the set partitioning / packing / covering constraints .
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip.c:22651
#define SCIPdebugPrintCons(x, y, z)
Definition: pub_message.h:83
const char * SCIPsepaGetName(SCIP_SEPA *sepa)
Definition: sepa.c:633
SCIP_VAR ** SCIPgetVarsKnapsack(SCIP *scip, SCIP_CONS *cons)
#define SCIPreallocBufferArray(scip, ptr, num)
Definition: scip.h:19215
SCIP_Real SCIPgetDualsolKnapsack(SCIP *scip, SCIP_CONS *cons)
SCIP_RETCODE SCIPsolveKnapsackApproximately(SCIP *scip, int nitems, SCIP_Longint *weights, SCIP_Real *profits, SCIP_Longint capacity, int *items, int *solitems, int *nonsolitems, int *nsolitems, int *nnonsolitems, SCIP_Real *solval)
int SCIPvarCompare(SCIP_VAR *var1, SCIP_VAR *var2)
Definition: var.c:11177
#define DEFAULT_PRESOLUSEHASHING
Definition: cons_knapsack.c:88
static SCIP_RETCODE GUBsetGetCliquePartition(SCIP *scip, SCIP_GUBSET *gubset, SCIP_VAR **vars, SCIP_Real *solvals)
SCIP_RETCODE SCIPsetConshdlrFree(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSFREE((*consfree)))
Definition: scip.c:5103
SCIP_Real SCIPgetLowerbound(SCIP *scip)
Definition: scip.c:35061
SCIP_Bool SCIPconsIsInitial(SCIP_CONS *cons)
Definition: cons.c:7706
SCIP_RETCODE SCIPhashtableCreate(SCIP_HASHTABLE **hashtable, BMS_BLKMEM *blkmem, int tablesize, SCIP_DECL_HASHGETKEY((*hashgetkey)), SCIP_DECL_HASHKEYEQ((*hashkeyeq)), SCIP_DECL_HASHKEYVAL((*hashkeyval)), void *userptr)
Definition: misc.c:1287
SCIP_RETCODE SCIPanalyzeConflictCons(SCIP *scip, SCIP_CONS *cons, SCIP_Bool *success)
Definition: scip.c:22424
#define SCIP_EVENTTYPE_LBCHANGED
Definition: type_event.h:95
static SCIP_DECL_CONSDELETE(consDeleteKnapsack)
SCIP_RETCODE SCIPsetConshdlrInit(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSINIT((*consinit)))
Definition: scip.c:5127
SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:3388
SCIP_RETCODE SCIPcreateEmptyRowUnspec(SCIP *scip, SCIP_ROW **row, const char *name, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool removable)
Definition: scip.c:25334
SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
Definition: var.c:15907
#define DEFAULT_SEPACARDFREQ
Definition: cons_knapsack.c:73
SCIP_RETCODE SCIPaddIntParam(SCIP *scip, const char *name, const char *desc, int *valueptr, SCIP_Bool isadvanced, int defaultvalue, int minvalue, int maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:3414
SCIP_RETCODE SCIPparseVarName(SCIP *scip, const char *str, SCIP_VAR **var, char **endptr)
Definition: scip.c:14405
#define SCIP_EVENTTYPE_LBRELAXED
Definition: type_event.h:56
static SCIP_RETCODE GUBconsFree(SCIP *scip, SCIP_GUBCONS **gubcons)
static SCIP_DECL_HASHGETKEY(hashGetKeyKnapsackcons)
static SCIP_RETCODE GUBconsDelVar(SCIP *scip, SCIP_GUBCONS *gubcons, int var, int gubvarsidx)
SCIP_CONSHDLRDATA * SCIPconshdlrGetData(SCIP_CONSHDLR *conshdlr)
Definition: cons.c:3893
static SCIP_DECL_CONSPARSE(consParseKnapsack)
static SCIP_RETCODE stableSort(SCIP *scip, SCIP_CONSDATA *consdata, SCIP_VAR **vars, SCIP_Longint *weights, int *cliquestartposs, SCIP_Bool usenegatedclique)
#define DEFAULT_MAXROUNDS
Definition: cons_knapsack.c:74
#define DEFAULT_DISAGGREGATION
Definition: cons_knapsack.c:81
SCIP_RETCODE SCIPcatchVarEvent(SCIP *scip, SCIP_VAR *var, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip.c:33378
static SCIP_RETCODE checkCons(SCIP *scip, SCIP_CONS *cons, SCIP_SOL *sol, SCIP_Bool checklprows, SCIP_Bool printreason, SCIP_Bool *violated)
SCIP_RETCODE SCIPgetBinvarRepresentative(SCIP *scip, SCIP_VAR *var, SCIP_VAR **repvar, SCIP_Bool *negated)
Definition: scip.c:15475
static SCIP_RETCODE applyFixings(SCIP *scip, SCIP_CONS *cons, SCIP_Bool *cutoff)
#define CONSHDLR_SEPAPRIORITY
Definition: cons_knapsack.c:41
SCIP_RETCODE SCIPcreateConsKnapsack(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
Constraint handler for knapsack constraints of the form , x binary and .
SCIP_RETCODE SCIPinitConflictAnalysis(SCIP *scip)
Definition: scip.c:22066
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38273
SCIP_Bool SCIPvarIsTransformed(SCIP_VAR *var)
Definition: var.c:15930
SCIP_RETCODE SCIPincConsAge(SCIP *scip, SCIP_CONS *cons)
Definition: scip.c:23258
SCIP_RETCODE SCIPvarsGetProbvarBinary(SCIP_VAR ***vars, SCIP_Bool **negatedarr, int nvars)
Definition: var.c:11512
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip.h:19183
void SCIPsortDownPtrInt(void **ptrarray, int *intarray, SCIP_DECL_SORTPTRCOMP((*ptrcomp)), int len)
SCIP_Bool SCIPisFeasPositive(SCIP *scip, SCIP_Real val)
Definition: scip.c:38755
static SCIP_DECL_HASHKEYEQ(hashKeyEqKnapsackcons)
SCIP_VAR ** SCIPvarGetVubVars(SCIP_VAR *var)
Definition: var.c:16616
static SCIP_DECL_CONSEXITSOL(consExitsolKnapsack)
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
Definition: scip.c:38421
enum GUBVarstatus GUBVARSTATUS
SCIP_Bool SCIPconsIsStickingAtNode(SCIP_CONS *cons)
Definition: cons.c:7816
#define SCIPallocMemory(scip, ptr)
Definition: scip.h:19159
int SCIPgetNSepaRounds(SCIP *scip)
Definition: scip.c:34672
static void getPartitionCovervars(SCIP *scip, SCIP_Real *solvals, int *covervars, int ncovervars, int *varsC1, int *varsC2, int *nvarsC1, int *nvarsC2)
#define SCIPallocBuffer(scip, ptr)
Definition: scip.h:19213
static SCIP_RETCODE simplifyInequalities(SCIP *scip, SCIP_CONS *cons, int *nfixedvars, int *ndelconss, int *nchgcoefs, int *nchgsides, int *naddconss, SCIP_Bool *cutoff)
#define SCIPerrorMessage
Definition: pub_message.h:45
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip.h:19221
#define SCIPdebugPrintf
Definition: pub_message.h:80
SCIP_RETCODE SCIPunlockVarCons(SCIP *scip, SCIP_VAR *var, SCIP_CONS *cons, SCIP_Bool lockdown, SCIP_Bool lockup)
Definition: scip.c:17713
void SCIPsortIntInt(int *intarray1, int *intarray2, int len)
SCIP_RETCODE SCIPupdateConsFlags(SCIP *scip, SCIP_CONS *cons0, SCIP_CONS *cons1)
Definition: scip.c:23000
static SCIP_DECL_CONSDELVARS(consDelvarsKnapsack)
static void GUBsetSwapVars(SCIP *scip, SCIP_GUBSET *gubset, int var1, int var2)
#define DEFAULT_PRESOLPAIRWISE
Definition: cons_knapsack.c:91
Constraint handler for logicor constraints (equivalent to set covering, but algorithms are suited fo...
const char * SCIPconshdlrGetName(SCIP_CONSHDLR *conshdlr)
Definition: cons.c:3873
int SCIPcalcHashtableSize(int minsize)
Definition: misc.c:964
SCIP_Real SCIPvarGetUbAtIndex(SCIP_VAR *var, SCIP_BDCHGIDX *bdchgidx, SCIP_Bool after)
Definition: var.c:15233
SCIP_Real SCIPepsilon(SCIP *scip)
Definition: scip.c:37705
SCIPInterval sqrt(const SCIPInterval &x)
static SCIP_RETCODE sequentialUpAndDownLiftingGUB(SCIP *scip, SCIP_GUBSET *gubset, SCIP_VAR **vars, int ngubconscapexceed, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Real *solvals, int *gubconsGC1, int *gubconsGC2, int *gubconsGFC1, int *gubconsGR, int ngubconsGC1, int ngubconsGC2, int ngubconsGFC1, int ngubconsGR, int alpha0, int *liftcoefs, SCIP_Real *cutact, int *liftrhs, int maxgubvarssize)
SCIP_Bool SCIPisLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38292
#define DEFAULT_DUALPRESOLVING
Definition: cons_knapsack.c:96
static SCIP_DECL_CONSINITPRE(consInitpreKnapsack)
SCIP_RETCODE SCIPgetNegatedVar(SCIP *scip, SCIP_VAR *var, SCIP_VAR **negvar)
Definition: scip.c:15406
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip.c:37940
const char * SCIPconsGetName(SCIP_CONS *cons)
Definition: cons.c:7557
SCIP_Bool SCIPisFeasEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38648
SCIP_Bool SCIPvarIsNegated(SCIP_VAR *var)
Definition: var.c:15943
SCIP_Bool SCIProwIsInLP(SCIP_ROW *row)
Definition: lp.c:18848
#define SCIP_EVENTTYPE_IMPLADDED
Definition: type_event.h:63
SCIP_Real SCIPfloor(SCIP *scip, SCIP_Real val)
Definition: scip.c:38470
SCIP_Bool SCIPvarIsDeleted(SCIP_VAR *var)
Definition: var.c:16009
struct SCIP_EventData SCIP_EVENTDATA
Definition: type_event.h:146
SCIP_RETCODE SCIPinferBinvarCons(SCIP *scip, SCIP_VAR *var, SCIP_Bool fixedval, SCIP_CONS *infercons, int inferinfo, SCIP_Bool *infeasible, SCIP_Bool *tightened)
Definition: scip.c:18783
SCIP_RETCODE SCIPsetConshdlrExitpre(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSEXITPRE((*consexitpre)))
Definition: scip.c:5247
static SCIP_DECL_SORTPTRCOMP(compSortkeypairs)
#define SCIPreallocBlockMemoryArray(scip, ptr, oldnum, newnum)
Definition: scip.h:19189
static SCIP_Longint safeAddMinweightsGUB(SCIP_Longint val1, SCIP_Longint val2)
struct sortkeypair SORTKEYPAIR
SCIP_BOUNDTYPE * SCIPvarGetImplTypes(SCIP_VAR *var, SCIP_Bool varfixing)
Definition: var.c:16694
static SCIP_DECL_CONSPRINT(consPrintKnapsack)
#define REALABS(x)
Definition: def.h:146
SCIP_RETCODE SCIPvarGetProbvarBinary(SCIP_VAR **var, SCIP_Bool *negated)
Definition: var.c:11544
#define DEFAULT_NEGATEDCLIQUE
Definition: cons_knapsack.c:83
#define SCIP_EVENTTYPE_UBRELAXED
Definition: type_event.h:58
SCIP_VAR ** SCIPvarGetMultaggrVars(SCIP_VAR *var)
Definition: var.c:16159
int SCIPvarGetNLocksUp(SCIP_VAR *var)
Definition: var.c:3214
SCIP_RETCODE SCIPgetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:31809
SCIP_Real SCIPinfinity(SCIP *scip)
Definition: scip.c:38349
#define SCIP_CALL(x)
Definition: def.h:258
SCIP_RETCODE SCIPsolveKnapsackExactly(SCIP *scip, int nitems, SCIP_Longint *weights, SCIP_Real *profits, SCIP_Longint capacity, int *items, int *solitems, int *nonsolitems, int *nsolitems, int *nnonsolitems, SCIP_Real *solval, SCIP_Bool *success)
#define SCIP_EVENTTYPE_LBTIGHTENED
Definition: type_event.h:55
SCIP_RETCODE SCIPincludeConshdlrBasic(SCIP *scip, SCIP_CONSHDLR **conshdlrptr, const char *name, const char *desc, int enfopriority, int chckpriority, int eagerfreq, SCIP_Bool needscons, SCIP_DECL_CONSENFOLP((*consenfolp)), SCIP_DECL_CONSENFOPS((*consenfops)), SCIP_DECL_CONSCHECK((*conscheck)), SCIP_DECL_CONSLOCK((*conslock)), SCIP_CONSHDLRDATA *conshdlrdata)
Definition: scip.c:4936
SCIP_Bool SCIPvarsHaveCommonClique(SCIP_VAR *var1, SCIP_Bool value1, SCIP_VAR *var2, SCIP_Bool value2, SCIP_Bool regardimplics)
Definition: var.c:10713
static SCIP_DECL_CONSENFOPS(consEnfopsKnapsack)
#define MAXNCLIQUEVARSCOMP
SCIP_RETCODE SCIPhashtableRemove(SCIP_HASHTABLE *hashtable, void *element)
Definition: misc.c:1526
GUBVARSTATUS * gubvarsstatus
static SCIP_RETCODE addRelaxation(SCIP *scip, SCIP_CONS *cons, SCIP_SOL *sol, SCIP_Bool *cutoff)
SCIP_Bool SCIPinProbing(SCIP *scip)
Definition: scip.c:29454
#define KNAPSACKRELAX_MAXDNOM
Definition: cons_knapsack.c:70
#define DEFAULT_MAXSEPACUTSROOT
Definition: cons_knapsack.c:77
SCIP_RETCODE SCIPcreateEmptyRowSepa(SCIP *scip, SCIP_ROW **row, SCIP_SEPA *sepa, const char *name, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool removable)
Definition: scip.c:25305
SCIP_Bool SCIPisFeasGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38705
static SCIP_DECL_CONSFREE(consFreeKnapsack)
void SCIPsortPtrPtrIntInt(void **ptrarray1, void **ptrarray2, int *intarray1, int *intarray2, SCIP_DECL_SORTPTRCOMP((*ptrcomp)), int len)
SCIP_RETCODE SCIPaddCoefKnapsack(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Longint weight)
static SCIP_RETCODE createRelaxation(SCIP *scip, SCIP_CONS *cons)
struct SCIP_ConsData SCIP_CONSDATA
Definition: type_cons.h:49
SCIP_CONSDATA * SCIPconsGetData(SCIP_CONS *cons)
Definition: cons.c:7587
static SCIP_Bool checkSolveKnapsack(SCIP *scip, int nitems, SCIP_Longint *transweights, SCIP_Real *transprofits, int *items, SCIP_Longint *weights, SCIP_Real *solvals, SCIP_Bool modtransused)
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition: scip.c:15130
SCIP_Bool SCIPisHugeValue(SCIP *scip, SCIP_Real val)
Definition: scip.c:38372
static SCIP_DECL_CONSEXIT(consExitKnapsack)
static SCIP_RETCODE getLiftingSequence(SCIP *scip, SCIP_Real *solvals, SCIP_Longint *weights, int *varsF, int *varsC2, int *varsR, int nvarsF, int nvarsC2, int nvarsR)
SCIP_RETCODE SCIPdelConsLocal(SCIP *scip, SCIP_CONS *cons)
Definition: scip.c:11453
void SCIPsortDownRealLongRealInt(SCIP_Real *realarray1, SCIP_Longint *longarray, SCIP_Real *realarray3, int *intarray, int len)
SCIP_RETCODE SCIPgetTransformedVars(SCIP *scip, int nvars, SCIP_VAR **vars, SCIP_VAR **transvars)
Definition: scip.c:15360
SCIP_Bool SCIPisGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38311
static SCIP_DECL_CONSPRESOL(consPresolKnapsack)
SCIP_VAR * SCIPvarGetNegatedVar(SCIP_VAR *var)
Definition: var.c:16195
static SCIP_RETCODE consdataEnsureVarsSize(SCIP *scip, SCIP_CONSDATA *consdata, int num, SCIP_Bool transformed)
static SCIP_RETCODE delCoefPos(SCIP *scip, SCIP_CONS *cons, int pos)
int SCIPconshdlrGetSepaFreq(SCIP_CONSHDLR *conshdlr)
Definition: cons.c:4702
SCIP_RETCODE SCIPsetConshdlrDelvars(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSDELVARS((*consdelvars)))
Definition: scip.c:5493
SCIP_RETCODE SCIPtightenVarLb(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound, SCIP_Bool force, SCIP_Bool *infeasible, SCIP_Bool *tightened)
Definition: scip.c:18371
SCIP_RETCODE SCIPcaptureVar(SCIP *scip, SCIP_VAR *var)
Definition: scip.c:15097
SCIP_Real SCIPvarGetUbLocal(SCIP_VAR *var)
Definition: var.c:16436
SCIP_Bool SCIPisTransformed(SCIP *scip)
Definition: scip.c:983
public data structures and miscellaneous methods
SCIP_RETCODE SCIPsetConshdlrInitpre(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSINITPRE((*consinitpre)))
Definition: scip.c:5223
SCIP_RETCODE SCIPcreateCons(SCIP *scip, SCIP_CONS **cons, const char *name, SCIP_CONSHDLR *conshdlr, SCIP_CONSDATA *consdata, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
Definition: scip.c:22476
static SCIP_RETCODE enlargeMinweights(SCIP *scip, SCIP_Longint **minweightsptr, int *minweightslen, int *minweightssize, int newlen)
static SCIP_RETCODE addCliques(SCIP *const scip, SCIP_CONS *const cons, SCIP_Bool *const cutoff, int *const nbdchgs)
SCIP_RETCODE SCIPsetConsEnforced(SCIP *scip, SCIP_CONS *cons, SCIP_Bool enforce)
Definition: scip.c:22797
int SCIPvarGetIndex(SCIP_VAR *var)
Definition: var.c:16062
#define SCIP_Bool
Definition: def.h:49
#define MINGAINPERNMINCOMPARISONS
Definition: cons_knapsack.c:93
static SCIP_DECL_CONSPROP(consPropKnapsack)
#define MAXABSVBCOEF
Definition: cons_knapsack.c:85
static SCIP_RETCODE superadditiveUpLifting(SCIP *scip, SCIP_VAR **vars, int nvars, int ntightened, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Real *solvals, int *covervars, int *noncovervars, int ncovervars, int nnoncovervars, SCIP_Longint coverweight, SCIP_Real *liftcoefs, SCIP_Real *cutact)
SCIP_RETCODE SCIPcalcCliquePartition(SCIP *const scip, SCIP_VAR **const vars, int const nvars, int *const cliquepartition, int *const ncliques)
Definition: scip.c:19876
#define CONSHDLR_DELAYSEPA
Definition: cons_knapsack.c:50
#define DEFAULT_MAXSEPACUTS
Definition: cons_knapsack.c:76
SCIP_Real * SCIPvarGetVubCoefs(SCIP_VAR *var)
Definition: var.c:16626
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip.c:790
SCIP_Bool SCIPconsIsDynamic(SCIP_CONS *cons)
Definition: cons.c:7796
#define DEFAULT_MAXCARDBOUNDDIST
Definition: cons_knapsack.c:78
SCIP_RETCODE SCIPcacheRowExtensions(SCIP *scip, SCIP_ROW *row)
Definition: scip.c:25487
static SCIP_DECL_CONSEXITPRE(consExitpreKnapsack)
#define MAX(x, y)
Definition: tclique_def.h:75
SCIP_Real SCIPgetLocalLowerbound(SCIP *scip)
Definition: scip.c:11563
SCIP_RETCODE SCIPflattenVarAggregationGraph(SCIP *scip, SCIP_VAR *var)
Definition: scip.c:15570
SCIP_Real * SCIPvarGetVlbConstants(SCIP_VAR *var)
Definition: var.c:16594
SCIP_Bool * SCIPcliqueGetValues(SCIP_CLIQUE *clique)
Definition: implics.c:2384
SCIP_Bool SCIPconsIsRemovable(SCIP_CONS *cons)
Definition: cons.c:7806
static SCIP_RETCODE consdataCreate(SCIP *scip, SCIP_CONSDATA **consdata, SCIP_EVENTHDLR *eventhdlr, int nvars, SCIP_VAR **vars, SCIP_Longint *weights, SCIP_Longint capacity)
#define DEFAULT_USEGUBS
static SCIP_RETCODE tightenWeights(SCIP *scip, SCIP_CONS *cons, int *nchgcoefs, int *nchgsides, int *naddconss, int *ndelconss, SCIP_Bool *cutoff)
SCIP_Bool SCIPconsIsDeleted(SCIP_CONS *cons)
Definition: cons.c:7676
static SCIP_RETCODE GUBsetCreate(SCIP *scip, SCIP_GUBSET **gubset, int nvars, SCIP_Longint *weights, SCIP_Longint capacity)
#define KNAPSACKRELAX_MAXDELTA
Definition: cons_knapsack.c:69
#define MAX_ZEROITEMS_SIZE
Definition: cons_knapsack.c:67
static SCIP_RETCODE catchEvents(SCIP *scip, SCIP_CONSDATA *consdata, SCIP_EVENTHDLR *eventhdlr)
#define EVENTHDLR_DESC
Definition: cons_knapsack.c:58
#define CONSHDLR_NAME
Definition: cons_knapsack.c:39
int SCIPvarGetMultaggrNVars(SCIP_VAR *var)
Definition: var.c:16147
#define SCIPfreeMemoryArray(scip, ptr)
Definition: scip.h:19178
SCIP_RETCODE SCIPsetConshdlrInitlp(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSINITLP((*consinitlp)))
Definition: scip.c:5355
#define SCIPreallocMemoryArray(scip, ptr, newnum)
Definition: scip.h:19167
int SCIPvarGetNVlbs(SCIP_VAR *var)
Definition: var.c:16562
#define BMScopyMemoryArray(ptr, source, num)
Definition: memory.h:102
static void sortItems(SCIP_CONSDATA *consdata)
#define CONSHDLR_DELAYPROP
Definition: cons_knapsack.c:51
SCIP_Real * SCIPvarGetMultaggrScalars(SCIP_VAR *var)
Definition: var.c:16171
static void normalizeWeights(SCIP_CONS *cons, int *nchgcoefs, int *nchgsides)
#define SCIPallocMemoryArray(scip, ptr, num)
Definition: scip.h:19161
Constraint handler for linear constraints in their most general form, .
#define IDX(j, d)
void * SCIPhashtableRetrieve(SCIP_HASHTABLE *hashtable, void *key)
Definition: misc.c:1434
int * gubvarsidx
static SCIP_RETCODE prepareCons(SCIP *scip, SCIP_CONS *cons, int *nfixedvars, int *ndelconss, int *nchgcoefs)
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip.h:19204
static SCIP_RETCODE deleteRedundantVars(SCIP *scip, SCIP_CONS *cons, SCIP_Longint frontsum, int splitpos, int *nchgcoefs, int *nchgsides, int *naddconss)
int SCIPgetDepth(SCIP *scip)
Definition: scip.c:34833
static SCIP_RETCODE changePartitionCovervars(SCIP *scip, SCIP_Longint *weights, int *varsC1, int *varsC2, int *nvarsC1, int *nvarsC2)
#define CONSHDLR_DELAYPRESOL
Definition: cons_knapsack.c:52
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip.c:1256
static SCIP_RETCODE dropEvents(SCIP *scip, SCIP_CONSDATA *consdata, SCIP_EVENTHDLR *eventhdlr)
static SCIP_RETCODE dualPresolving(SCIP *scip, SCIP_CONS *cons, int *nfixedvars, int *ndelconss, SCIP_Bool *deleted)
SCIP_Bool SCIPisGE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38330
void SCIPhashtableFree(SCIP_HASHTABLE **hashtable)
Definition: misc.c:1317
SCIP_RETCODE SCIPdropVarEvent(SCIP *scip, SCIP_VAR *var, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip.c:33424
SCIP_RETCODE SCIPsetConshdlrDelete(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSDELETE((*consdelete)))
Definition: scip.c:5309
static SCIP_RETCODE upgradeCons(SCIP *scip, SCIP_CONS *cons, int *ndelconss, int *naddconss)
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip.c:10850
static SCIP_DECL_CONSSEPALP(consSepalpKnapsack)
static SCIP_RETCODE sequentialUpAndDownLifting(SCIP *scip, SCIP_VAR **vars, int nvars, int ntightened, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Real *solvals, int *varsM1, int *varsM2, int *varsF, int *varsR, int nvarsM1, int nvarsM2, int nvarsF, int nvarsR, int alpha0, int *liftcoefs, SCIP_Real *cutact, int *liftrhs)
static SCIP_RETCODE createNormalizedKnapsack(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *vals, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
static SCIP_DECL_LINCONSUPGD(linconsUpgdKnapsack)
SCIP_Real * SCIPvarGetVlbCoefs(SCIP_VAR *var)
Definition: var.c:16584
static SCIP_DECL_CONSCOPY(consCopyKnapsack)
#define SCIPfreeMemory(scip, ptr)
Definition: scip.h:19176
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:16370
static SCIP_RETCODE GUBconsCreate(SCIP *scip, SCIP_GUBCONS **gubcons)
static SCIP_RETCODE GUBsetCheck(SCIP *scip, SCIP_GUBSET *gubset, SCIP_VAR **vars)
SCIP_RETCODE SCIPchgCapacityKnapsack(SCIP *scip, SCIP_CONS *cons, SCIP_Longint capacity)
SCIP_RETCODE SCIPsetConshdlrPrint(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSPRINT((*consprint)))
Definition: scip.c:5516
SCIP_Real SCIProwGetDualsol(SCIP_ROW *row)
Definition: lp.c:18657
static SCIP_DECL_CONSLOCK(consLockKnapsack)
static SCIP_DECL_CONSGETNVARS(consGetNVarsKnapsack)
SCIP_RETCODE SCIPflushRowExtensions(SCIP *scip, SCIP_ROW *row)
Definition: scip.c:25510
SCIP_RETCODE SCIPsetConsSeparated(SCIP *scip, SCIP_CONS *cons, SCIP_Bool separate)
Definition: scip.c:22772
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip.c:1239
static SCIP_RETCODE detectRedundantConstraints(SCIP *scip, BMS_BLKMEM *blkmem, SCIP_CONS **conss, int nconss, SCIP_Bool *cutoff, int *ndelconss)
SCIP_Longint SCIPconshdlrGetNCutsFound(SCIP_CONSHDLR *conshdlr)
Definition: cons.c:4472
int SCIPgetNVarsKnapsack(SCIP *scip, SCIP_CONS *cons)
SCIP_Bool SCIPisIntegral(SCIP *scip, SCIP_Real val)
Definition: scip.c:38433
static SCIP_Bool checkMinweightidx(SCIP_Longint *weights, SCIP_Longint capacity, int *covervars, int ncovervars, SCIP_Longint coverweight, int minweightidx, int j)
#define SCIPduplicateBufferArray(scip, ptr, source, num)
Definition: scip.h:19217
SCIP_RETCODE SCIPsetConshdlrExitsol(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSEXITSOL((*consexitsol)))
Definition: scip.c:5199
#define DEFAULT_DETECTLOWERBOUND
#define HASHSIZE_KNAPSACKCONS
Definition: cons_knapsack.c:89
enum GUBConsstatus GUBCONSSTATUS
#define MAX_USECLIQUES_SIZE
Definition: cons_knapsack.c:66
SCIP_Bool SCIPisFeasLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38686
SCIP_RETCODE SCIPlockVarCons(SCIP *scip, SCIP_VAR *var, SCIP_CONS *cons, SCIP_Bool lockdown, SCIP_Bool lockup)
Definition: scip.c:17642
int SCIPvarGetProbindex(SCIP_VAR *var)
Definition: var.c:16072
static SCIP_DECL_CONSGETVARS(consGetVarsKnapsack)
static SCIP_RETCODE GUBconsAddVar(SCIP *scip, SCIP_GUBCONS *gubcons, int var)
#define SCIP_Real
Definition: def.h:123
SCIP_Bool SCIPconsIsChecked(SCIP_CONS *cons)
Definition: cons.c:7736
SCIP_RETCODE SCIPsetConsInitial(SCIP *scip, SCIP_CONS *cons, SCIP_Bool initial)
Definition: scip.c:22747
static SCIP_RETCODE unlockRounding(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var)
SCIP_RETCODE SCIPtightenVarUb(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound, SCIP_Bool force, SCIP_Bool *infeasible, SCIP_Bool *tightened)
Definition: scip.c:18477
int SCIPvarGetNCliques(SCIP_VAR *var, SCIP_Bool varfixing)
Definition: var.c:16736
static SCIP_RETCODE calcCliquepartition(SCIP *scip, SCIP_CONSDATA *consdata, SCIP_Bool normalclique, SCIP_Bool negatedclique)
#define MIN(x, y)
Definition: memory.c:59
SCIP_RETCODE SCIPcreateEmptyRowCons(SCIP *scip, SCIP_ROW **row, SCIP_CONSHDLR *conshdlr, const char *name, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool removable)
Definition: scip.c:25276
#define GUBCONSGROWVALUE
SCIP_RETCODE SCIPprintRow(SCIP *scip, SCIP_ROW *row, FILE *file)
Definition: scip.c:26010
static SCIP_DECL_CONSSEPASOL(consSepasolKnapsack)
SCIP_Bool SCIPisFeasGE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:38724
#define SCIPduplicateBlockMemoryArray(scip, ptr, source, num)
Definition: scip.h:19198
SCIP_RETCODE SCIPcalcNegatedCliquePartition(SCIP *const scip, SCIP_VAR **const vars, int const nvars, int *const cliquepartition, int *const ncliques)
Definition: scip.c:20025
#define SCIP_INVALID
Definition: def.h:142
static SCIP_DECL_CONSENFOLP(consEnfolpKnapsack)
static SCIP_RETCODE removeZeroWeights(SCIP *scip, SCIP_CONS *cons)
#define DEFAULT_MAXROUNDSROOT
Definition: cons_knapsack.c:75
SCIP_RETCODE SCIPreleaseRow(SCIP *scip, SCIP_ROW **row)
Definition: scip.c:25414
#define CONSHDLR_PROPFREQ
Definition: cons_knapsack.c:45
#define CONSHDLR_NEEDSCONS
Definition: cons_knapsack.c:53
#define SCIP_Longint
Definition: def.h:107
SCIP_RETCODE SCIPcalcIntegralScalar(SCIP_Real *vals, int nvals, SCIP_Real mindelta, SCIP_Real maxdelta, SCIP_Longint maxdnom, SCIP_Real maxscale, SCIP_Real *intscalar, SCIP_Bool *success)
Definition: misc.c:6887
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:917
static SCIP_RETCODE makeCoverMinimal(SCIP *scip, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Real *solvals, int *covervars, int *noncovervars, int *ncovervars, int *nnoncovervars, SCIP_Longint *coverweight, SCIP_Bool modtransused)
SCIP_RETCODE SCIPsetConshdlrExit(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSEXIT((*consexit)))
Definition: scip.c:5151
SCIP_ROW * SCIPgetRowKnapsack(SCIP *scip, SCIP_CONS *cons)
SCIP_RETCODE SCIPaddVarLocks(SCIP *scip, SCIP_VAR *var, int nlocksdown, int nlocksup)
Definition: scip.c:17590
SCIP_RETCODE SCIPincludeLinconsUpgrade(SCIP *scip, SCIP_DECL_LINCONSUPGD((*linconsupgd)), int priority, const char *conshdlrname)
void SCIPsortPtrPtrLongIntInt(void **ptrarray1, void **ptrarray2, SCIP_Longint *longarray, int *intarray1, int *intarray2, SCIP_DECL_SORTPTRCOMP((*ptrcomp)), int len)
struct SCIP_ConshdlrData SCIP_CONSHDLRDATA
Definition: type_cons.h:48
SCIP_Bool SCIPvarIsActive(SCIP_VAR *var)
Definition: var.c:16052
int SCIPgetNObjVars(SCIP *scip)
Definition: scip.c:10299
int SCIPcliqueGetNVars(SCIP_CLIQUE *clique)
Definition: implics.c:2362
SCIP_RETCODE SCIPaddConflictBinvar(SCIP *scip, SCIP_VAR *var)
Definition: scip.c:22285
void SCIPsortDownRealIntLong(SCIP_Real *realarray, int *intarray, SCIP_Longint *longarray, int len)
static SCIP_RETCODE getFeasibleSet(SCIP *scip, SCIP_CONS *cons, SCIP_SEPA *sepa, SCIP_VAR **vars, int nvars, int ntightened, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Real *solvals, int *covervars, int *noncovervars, int *ncovervars, int *nnoncovervars, SCIP_Longint *coverweight, SCIP_Bool modtransused, SCIP_SOL *sol, SCIP_Bool *cutoff, int *ncuts)
static SCIP_RETCODE preprocessConstraintPairs(SCIP *scip, SCIP_CONS **conss, int firstchange, int chkind, int *ndelconss)
static SCIP_RETCODE eventdataCreate(SCIP *scip, SCIP_EVENTDATA **eventdata, SCIP_CONSDATA *consdata, SCIP_Longint weight)
static SCIP_RETCODE lockRounding(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var)
int SCIPcalcMemGrowSize(SCIP *scip, int num)
Definition: scip.c:38001
#define BMSclearMemoryArray(ptr, num)
Definition: memory.h:98
static SCIP_RETCODE eventdataFree(SCIP *scip, SCIP_EVENTDATA **eventdata)
static SCIP_RETCODE detectRedundantVars(SCIP *scip, SCIP_CONS *cons, int *ndelconss, int *nchgcoefs, int *nchgsides, int *naddconss)
#define USESUPADDLIFT
Definition: cons_knapsack.c:86
struct BMS_BlkMem BMS_BLKMEM
Definition: memory.h:371
#define NMINCOMPARISONS
Definition: cons_knapsack.c:92
SCIP_Real SCIProwGetDualfarkas(SCIP_ROW *row)
Definition: lp.c:18670
static SCIP_DECL_CONSTRANS(consTransKnapsack)
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
Definition: scip.c:38409
SCIP_Longint SCIPsepaGetNCutsFound(SCIP_SEPA *sepa)
Definition: sepa.c:780
SCIP_RETCODE SCIPaddRealParam(SCIP *scip, const char *name, const char *desc, SCIP_Real *valueptr, SCIP_Bool isadvanced, SCIP_Real defaultvalue, SCIP_Real minvalue, SCIP_Real maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:3470
SCIP_RETCODE SCIPsetConshdlrGetNVars(SCIP *scip, SCIP_CONSHDLR *conshdlr, SCIP_DECL_CONSGETNVARS((*consgetnvars)))
Definition: scip.c:5585
SCIP_RETCODE SCIPcreateConsLogicor(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
#define SCIPfreeMemoryArrayNull(scip, ptr)
Definition: scip.h:19179
SCIP_VAR * SCIPvarGetProbvar(SCIP_VAR *var)
Definition: var.c:11452
SCIP_RETCODE SCIPaddVarToRow(SCIP *scip, SCIP_ROW *row, SCIP_VAR *var, SCIP_Real val)
Definition: scip.c:25540
#define SCIPABORT()
Definition: def.h:230
static SCIP_DECL_CONSHDLRCOPY(conshdlrCopyKnapsack)
#define MAXCOVERSIZEITERLEWI
SCIP_Bool SCIPconsIsEnforced(SCIP_CONS *cons)
Definition: cons.c:7726
SCIP_Longint SCIPcalcGreComDiv(SCIP_Longint val1, SCIP_Longint val2)
Definition: misc.c:6595
static SCIP_RETCODE changePartitionFeasiblesetvars(SCIP *scip, SCIP_Longint *weights, int *varsC1, int *varsC2, int *nvarsC1, int *nvarsC2)
static SCIP_RETCODE tightenWeightsLift(SCIP *scip, SCIP_CONS *cons, int *nchgcoefs, SCIP_Bool *cutoff)
SCIP_Bool SCIPconsIsPropagated(SCIP_CONS *cons)
Definition: cons.c:7756
SCIP_RETCODE SCIPcopyConsLinear(SCIP *scip, SCIP_CONS **cons, SCIP *sourcescip, const char *name, int nvars, SCIP_VAR **sourcevars, SCIP_Real *sourcecoefs, SCIP_Real lhs, SCIP_Real rhs, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode, SCIP_Bool global, SCIP_Bool *valid)
void SCIPconshdlrSetData(SCIP_CONSHDLR *conshdlr, SCIP_CONSHDLRDATA *conshdlrdata)
Definition: cons.c:3903
void SCIPsortDownLongPtr(SCIP_Longint *longarray, void **ptrarray, int len)
int SCIPvarGetNLocksDown(SCIP_VAR *var)
Definition: var.c:3159
#define CONSHDLR_CHECKPRIORITY
Definition: cons_knapsack.c:43
SCIP_Bool SCIPinRepropagation(SCIP *scip)
Definition: scip.c:33573
#define SCIP_EVENTTYPE_VARDELETED
Definition: type_event.h:47
#define DEFAULT_SIMPLIFYINEQUALITIES
Definition: cons_knapsack.c:82
static void computeMinweightsGUB(SCIP_Longint *minweights, SCIP_Longint *finished, SCIP_Longint *unfinished, int minweightslen)
void SCIPsortDownLongPtrInt(SCIP_Longint *longarray, void **ptrarray, int *intarray, int len)