Scippy

SCIP

Solving Constraint Integer Programs

cons_sos1.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2018 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scip.zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file cons_sos1.c
17  * @brief constraint handler for SOS type 1 constraints
18  * @author Tobias Fischer
19  * @author Marc Pfetsch
20  *
21  * A specially ordered set of type 1 (SOS1) is a sequence of variables such that at most one
22  * variable is nonzero. The special case of two variables arises, for instance, from equilibrium or
23  * complementary conditions like \f$x \cdot y = 0\f$. Note that it is in principle allowed that a
24  * variables appears twice, but it then can be fixed to 0.
25  *
26  * This implementation of this constraint handler is based on classical ideas, see e.g.@n
27  * "Special Facilities in General Mathematical Programming System for
28  * Non-Convex Problems Using Ordered Sets of Variables"@n
29  * E. Beale and J. Tomlin, Proc. 5th IFORS Conference, 447-454 (1970)
30  *
31  *
32  * The order of the variables is determined as follows:
33  *
34  * - If the constraint is created with SCIPcreateConsSOS1() and weights are given, the weights
35  * determine the order (decreasing weights). Additional variables can be added with
36  * SCIPaddVarSOS1(), which adds a variable with given weight.
37  *
38  * - If an empty constraint is created and then variables are added with SCIPaddVarSOS1(), weights
39  * are needed and stored.
40  *
41  * - All other calls ignore the weights, i.e., if a nonempty constraint is created or variables are
42  * added with SCIPappendVarSOS1().
43  *
44  * The validity of the SOS1 constraints can be enforced by different branching rules:
45  *
46  * - If classical SOS branching is used, branching is performed on only one SOS1 constraint.
47  * Depending on the parameters, there are two ways to choose this branching constraint. Either
48  * the constraint with the most number of nonzeros or the one with the largest nonzero-variable
49  * weight. The later version allows the user to specify an order for the branching importance of
50  * the constraints. Constraint branching can also be turned off.
51  *
52  * - Another way is to branch on the neighborhood of a single variable @p i, i.e., in one branch
53  * \f$x_i\f$ is fixed to zero and in the other its neighbors from the conflict graph.
54  *
55  * - If bipartite branching is used, then we branch using complete bipartite subgraphs of the
56  * conflict graph, i.e., in one branch fix the variables from the first bipartite partition and
57  * the variables from the second bipartite partition in the other.
58  *
59  * - In addition to variable domain fixings, it is sometimes also possible to add new SOS1
60  * constraints to the branching nodes. This results in a nonstatic conflict graph, which may
61  * change dynamically with every branching node.
62  *
63  *
64  * @todo Possibly allow to generate local cuts via strengthened local cuts (would need to modified coefficients of rows).
65  *
66  * @todo Check whether we can avoid turning off multi-aggregation (it is sometimes possible to fix a multi-aggregated
67  * variable to 0 by fixing the aggregating variables to 0).
68  */
69 
70 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
71 
72 #include "blockmemshell/memory.h"
73 #include "scip/cons_linear.h"
74 #include "scip/cons_setppc.h"
75 #include "scip/cons_sos1.h"
76 #include "scip/misc.h"
77 #include "scip/pub_cons.h"
78 #include "scip/pub_event.h"
79 #include "scip/pub_heur.h"
80 #include "scip/pub_lp.h"
81 #include "scip/pub_message.h"
82 #include "scip/pub_misc.h"
83 #include "scip/pub_misc_sort.h"
84 #include "scip/pub_tree.h"
85 #include "scip/pub_var.h"
86 #include "scip/scip_branch.h"
87 #include "scip/scip_conflict.h"
88 #include "scip/scip_cons.h"
89 #include "scip/scip_copy.h"
90 #include "scip/scip_cut.h"
92 #include "scip/scip_event.h"
93 #include "scip/scip_general.h"
94 #include "scip/scip_lp.h"
95 #include "scip/scip_mem.h"
96 #include "scip/scip_message.h"
97 #include "scip/scip_numerics.h"
98 #include "scip/scip_param.h"
99 #include "scip/scip_prob.h"
100 #include "scip/scip_probing.h"
101 #include "scip/scip_sol.h"
102 #include "scip/scip_solvingstats.h"
103 #include "scip/scip_tree.h"
104 #include "scip/scip_var.h"
105 #include "tclique/tclique.h"
106 #include <ctype.h>
107 #include <stdlib.h>
108 #include <string.h>
109 
110 
111 /* constraint handler properties */
112 #define CONSHDLR_NAME "SOS1"
113 #define CONSHDLR_DESC "SOS1 constraint handler"
114 #define CONSHDLR_SEPAPRIORITY 1000 /**< priority of the constraint handler for separation */
115 #define CONSHDLR_ENFOPRIORITY 100 /**< priority of the constraint handler for constraint enforcing */
116 #define CONSHDLR_CHECKPRIORITY -10 /**< priority of the constraint handler for checking feasibility */
117 #define CONSHDLR_SEPAFREQ 10 /**< frequency for separating cuts; zero means to separate only in the root node */
118 #define CONSHDLR_PROPFREQ 1 /**< frequency for propagating domains; zero means only preprocessing propagation */
119 #define CONSHDLR_EAGERFREQ 100 /**< frequency for using all instead of only the useful constraints in separation,
120  * propagation and enforcement, -1 for no eager evaluations, 0 for first only */
121 #define CONSHDLR_MAXPREROUNDS -1 /**< maximal number of presolving rounds the constraint handler participates in (-1: no limit) */
122 #define CONSHDLR_DELAYSEPA FALSE /**< should separation method be delayed, if other separators found cuts? */
123 #define CONSHDLR_DELAYPROP FALSE /**< should propagation method be delayed, if other propagators found reductions? */
124 #define CONSHDLR_NEEDSCONS TRUE /**< should the constraint handler be skipped, if no constraints are available? */
125 #define CONSHDLR_PROP_TIMING SCIP_PROPTIMING_BEFORELP
126 #define CONSHDLR_PRESOLTIMING SCIP_PRESOLTIMING_MEDIUM
128 /* adjacency matrix */
129 #define DEFAULT_MAXSOSADJACENCY 10000 /**< do not create an adjacency matrix if number of SOS1 variables is larger than predefined value
130  * (-1: no limit) */
131 
132 /* presolving */
133 #define DEFAULT_MAXEXTENSIONS 1 /**< maximal number of extensions that will be computed for each SOS1 constraint */
134 #define DEFAULT_MAXTIGHTENBDS 5 /**< maximal number of bound tightening rounds per presolving round (-1: no limit) */
135 #define DEFAULT_PERFIMPLANALYSIS FALSE /**< if TRUE then perform implication graph analysis (might add additional SOS1 constraints) */
136 #define DEFAULT_DEPTHIMPLANALYSIS -1 /**< number of recursive calls of implication graph analysis (-1: no limit) */
138 /* propagation */
139 #define DEFAULT_CONFLICTPROP TRUE /**< whether to use conflict graph propagation */
140 #define DEFAULT_IMPLPROP TRUE /**< whether to use implication graph propagation */
141 #define DEFAULT_SOSCONSPROP FALSE /**< whether to use SOS1 constraint propagation */
143 /* branching rules */
144 #define DEFAULT_BRANCHSTRATEGIES "nbs" /**< possible branching strategies (see parameter DEFAULT_BRANCHINGRULE) */
145 #define DEFAULT_BRANCHINGRULE 'n' /**< which branching rule should be applied ? ('n': neighborhood, 'b': bipartite, 's': SOS1/clique)
146  * (note: in some cases an automatic switching to SOS1 branching is possible) */
147 #define DEFAULT_AUTOSOS1BRANCH TRUE /**< if TRUE then automatically switch to SOS1 branching if the SOS1 constraints do not overlap */
148 #define DEFAULT_FIXNONZERO FALSE /**< if neighborhood branching is used, then fix the branching variable (if positive in sign) to the value of the
149  * feasibility tolerance */
150 #define DEFAULT_ADDCOMPS FALSE /**< if TRUE then add complementarity constraints to the branching nodes (can be used in combination with
151  * neighborhood or bipartite branching) */
152 #define DEFAULT_MAXADDCOMPS -1 /**< maximal number of complementarity constraints added per branching node (-1: no limit) */
153 #define DEFAULT_ADDCOMPSDEPTH 30 /**< only add complementarity constraints to branching nodes for predefined depth (-1: no limit) */
154 #define DEFAULT_ADDCOMPSFEAS -0.6 /**< minimal feasibility value for complementarity constraints in order to be added to the branching node */
155 #define DEFAULT_ADDBDSFEAS 1.0 /**< minimal feasibility value for bound inequalities in order to be added to the branching node */
156 #define DEFAULT_ADDEXTENDEDBDS TRUE /**< should added complementarity constraints be extended to SOS1 constraints to get tighter bound inequalities */
158 /* selection rules */
159 #define DEFAULT_NSTRONGROUNDS 0 /**< maximal number of strong branching rounds to perform for each node (-1: auto)
160  * (only available for neighborhood and bipartite branching) */
161 #define DEFAULT_NSTRONGITER 10000 /**< maximal number LP iterations to perform for each strong branching round (-2: auto, -1: no limit) */
162 
163 /* separation */
164 #define DEFAULT_BOUNDCUTSFROMSOS1 FALSE /**< if TRUE separate bound inequalities from SOS1 constraints */
165 #define DEFAULT_BOUNDCUTSFROMGRAPH TRUE /**< if TRUE separate bound inequalities from the conflict graph */
166 #define DEFAULT_AUTOCUTSFROMSOS1 TRUE /**< if TRUE then automatically switch to separating from SOS1 constraints if the SOS1 constraints do not overlap */
167 #define DEFAULT_BOUNDCUTSFREQ 10 /**< frequency for separating bound cuts; zero means to separate only in the root node */
168 #define DEFAULT_BOUNDCUTSDEPTH 40 /**< node depth of separating bound cuts (-1: no limit) */
169 #define DEFAULT_MAXBOUNDCUTS 50 /**< maximal number of bound cuts separated per branching node */
170 #define DEFAULT_MAXBOUNDCUTSROOT 150 /**< maximal number of bound cuts separated per iteration in the root node */
171 #define DEFAULT_STRTHENBOUNDCUTS TRUE /**< if TRUE then bound cuts are strengthened in case bound variables are available */
172 #define DEFAULT_IMPLCUTSFREQ 0 /**< frequency for separating implied bound cuts; zero means to separate only in the root node */
173 #define DEFAULT_IMPLCUTSDEPTH 40 /**< node depth of separating implied bound cuts (-1: no limit) */
174 #define DEFAULT_MAXIMPLCUTS 50 /**< maximal number of implied bound cuts separated per branching node */
175 #define DEFAULT_MAXIMPLCUTSROOT 150 /**< maximal number of implied bound cuts separated per iteration in the root node */
177 /* event handler properties */
178 #define EVENTHDLR_NAME "SOS1"
179 #define EVENTHDLR_DESC "bound change event handler for SOS1 constraints"
181 /* defines */
182 #define DIVINGCUTOFFVALUE 1e6
183 
185 /** constraint data for SOS1 constraints */
186 struct SCIP_ConsData
187 {
188  int nvars; /**< number of variables in the constraint */
189  int maxvars; /**< maximal number of variables (= size of storage) */
190  int nfixednonzeros; /**< number of variables fixed to be nonzero */
191  SCIP_Bool local; /**< TRUE if constraint is only valid locally */
192  SCIP_VAR** vars; /**< variables in constraint */
193  SCIP_ROW* rowlb; /**< row corresponding to lower bounds, or NULL if not yet created */
194  SCIP_ROW* rowub; /**< row corresponding to upper bounds, or NULL if not yet created */
195  SCIP_Real* weights; /**< weights determining the order (ascending), or NULL if not used */
196 };
197 
198 
199 /** node data of a given node in the conflict graph */
200 struct SCIP_NodeData
201 {
202  SCIP_VAR* var; /**< variable belonging to node */
203  SCIP_VAR* lbboundvar; /**< bound variable @p z from constraint \f$x \geq \mu \cdot z\f$ (or NULL if not existent) */
204  SCIP_VAR* ubboundvar; /**< bound variable @p z from constraint \f$x \leq \mu \cdot z\f$ (or NULL if not existent) */
205  SCIP_Real lbboundcoef; /**< value \f$\mu\f$ from constraint \f$x \geq \mu z \f$ (0.0 if not existent) */
206  SCIP_Real ubboundcoef; /**< value \f$\mu\f$ from constraint \f$x \leq \mu z \f$ (0.0 if not existent) */
207  SCIP_Bool lbboundcomp; /**< TRUE if the nodes from the connected component of the conflict graph the given node belongs to
208  * all have the same lower bound variable */
209  SCIP_Bool ubboundcomp; /**< TRUE if the nodes from the connected component of the conflict graph the given node belongs to
210  * all have the same lower bound variable */
211 };
212 typedef struct SCIP_NodeData SCIP_NODEDATA;
213 
214 
215 /** successor data of a given nodes successor in the implication graph */
216 struct SCIP_SuccData
217 {
218  SCIP_Real lbimpl; /**< lower bound implication */
219  SCIP_Real ubimpl; /**< upper bound implication */
220 };
221 typedef struct SCIP_SuccData SCIP_SUCCDATA;
222 
223 
224 /** tclique data for bound cut generation */
225 struct TCLIQUE_Data
226 {
227  SCIP* scip; /**< SCIP data structure */
228  SCIP_CONSHDLR* conshdlr; /**< SOS1 constraint handler */
229  SCIP_DIGRAPH* conflictgraph; /**< conflict graph */
230  SCIP_SOL* sol; /**< LP solution to be separated (or NULL) */
231  SCIP_Real scaleval; /**< factor for scaling weights */
232  SCIP_Bool cutoff; /**< whether a cutoff occurred */
233  int ncuts; /**< number of bound cuts found in this iteration */
234  int nboundcuts; /**< number of bound cuts found so far */
235  int maxboundcuts; /**< maximal number of clique cuts separated per separation round (-1: no limit) */
236  SCIP_Bool strthenboundcuts; /**< if TRUE then bound cuts are strengthened in case bound variables are available */
237 };
240 /** SOS1 constraint handler data */
241 struct SCIP_ConshdlrData
242 {
243  /* conflict graph */
244  SCIP_DIGRAPH* conflictgraph; /**< conflict graph */
245  SCIP_DIGRAPH* localconflicts; /**< local conflicts */
246  SCIP_Bool isconflocal; /**< if TRUE then local conflicts are present and conflict graph has to be updated for each node */
247  SCIP_HASHMAP* varhash; /**< hash map from variable to node in the conflict graph */
248  int nsos1vars; /**< number of problem variables that are part of the SOS1 conflict graph */
249  /* adjacency matrix */
250  int maxsosadjacency; /**< do not create an adjacency matrix if number of SOS1 variables is larger than predefined
251  * value (-1: no limit) */
252  /* implication graph */
253  SCIP_DIGRAPH* implgraph; /**< implication graph (@p j is successor of @p i if and only if \f$ x_i\not = 0 \Rightarrow x_j\not = 0\f$) */
254  int nimplnodes; /**< number of nodes in the implication graph */
255  /* tclique graph */
256  TCLIQUE_GRAPH* tcliquegraph; /**< tclique graph data structure */
257  TCLIQUE_DATA* tcliquedata; /**< tclique data */
258  /* event handler */
259  SCIP_EVENTHDLR* eventhdlr; /**< event handler for bound change events */
260  SCIP_VAR** fixnonzerovars; /**< stack of variables fixed to nonzero marked by event handler */
261  int maxnfixnonzerovars; /**< size of stack fixnonzerovars */
262  int nfixnonzerovars; /**< number of variables fixed to nonzero marked by event handler */
263  /* presolving */
264  int cntextsos1; /**< counts number of extended SOS1 constraints */
265  int maxextensions; /**< maximal number of extensions that will be computed for each SOS1 constraint */
266  int maxtightenbds; /**< maximal number of bound tightening rounds per presolving round (-1: no limit) */
267  SCIP_Bool perfimplanalysis; /**< if TRUE then perform implication graph analysis (might add additional SOS1 constraints) */
268  int depthimplanalysis; /**< number of recursive calls of implication graph analysis (-1: no limit) */
269  /* propagation */
270  SCIP_Bool conflictprop; /**< whether to use conflict graph propagation */
271  SCIP_Bool implprop; /**< whether to use implication graph propagation */
272  SCIP_Bool sosconsprop; /**< whether to use SOS1 constraint propagation */
273  /* branching */
274  char branchingrule; /**< which branching rule should be applied ? ('n': neighborhood, 'b': bipartite, 's': SOS1/clique)
275  * (note: in some cases an automatic switching to SOS1 branching is possible) */
276  SCIP_Bool autosos1branch; /**< if TRUE then automatically switch to SOS1 branching if the SOS1 constraints do not overlap */
277  SCIP_Bool fixnonzero; /**< if neighborhood branching is used, then fix the branching variable (if positive in sign) to the value of the
278  * feasibility tolerance */
279  SCIP_Bool addcomps; /**< if TRUE then add complementarity constraints to the branching nodes additionally to domain fixings
280  * (can be used in combination with neighborhood or bipartite branching) */
281  int maxaddcomps; /**< maximal number of complementarity cons. and cor. bound ineq. added per branching node (-1: no limit) */
282  int addcompsdepth; /**< only add complementarity constraints to branching nodes for predefined depth (-1: no limit) */
283  SCIP_Real addcompsfeas; /**< minimal feasibility value for complementarity constraints in order to be added to the branching node */
284  SCIP_Real addbdsfeas; /**< minimal feasibility value for bound inequalities in order to be added to the branching node */
285  SCIP_Bool addextendedbds; /**< should added complementarity constraints be extended to SOS1 constraints to get tighter bound inequalities */
286  SCIP_Bool branchsos; /**< Branch on SOS condition in enforcing? This value can only be set to false if all SOS1 variables are binary */
287  SCIP_Bool branchnonzeros; /**< Branch on SOS cons. with most number of nonzeros? */
288  SCIP_Bool branchweight; /**< Branch on SOS cons. with highest nonzero-variable weight for branching - needs branchnonzeros to be false */
289  SCIP_Bool switchsos1branch; /**< whether to switch to SOS1 branching */
290  /* selection rules */
291  int nstrongrounds; /**< maximal number of strong branching rounds to perform for each node (-1: auto)
292  * (only available for neighborhood and bipartite branching) */
293  int nstrongiter; /**< maximal number LP iterations to perform for each strong branching round (-2: auto, -1: no limit) */
294  /* separation */
295  SCIP_Bool boundcutsfromsos1; /**< if TRUE separate bound inequalities from SOS1 constraints */
296  SCIP_Bool boundcutsfromgraph; /**< if TRUE separate bound inequalities from the conflict graph */
297  SCIP_Bool autocutsfromsos1; /**< if TRUE then automatically switch to separating SOS1 constraints if the SOS1 constraints do not overlap */
298  SCIP_Bool switchcutsfromsos1; /**< whether to switch to separate bound inequalities from SOS1 constraints */
299  int boundcutsfreq; /**< frequency for separating bound cuts; zero means to separate only in the root node */
300  int boundcutsdepth; /**< node depth of separating bound cuts (-1: no limit) */
301  int maxboundcuts; /**< maximal number of bound cuts separated per branching node */
302  int maxboundcutsroot; /**< maximal number of bound cuts separated per iteration in the root node */
303  int nboundcuts; /**< number of bound cuts found so far */
304  SCIP_Bool strthenboundcuts; /**< if TRUE then bound cuts are strengthened in case bound variables are available */
305  int implcutsfreq; /**< frequency for separating implied bound cuts; zero means to separate only in the root node */
306  int implcutsdepth; /**< node depth of separating implied bound cuts (-1: no limit) */
307  int maximplcuts; /**< maximal number of implied bound cuts separated per branching node */
308  int maximplcutsroot; /**< maximal number of implied bound cuts separated per iteration in the root node */
309 };
310 
311 
312 
313 /*
314  * local methods
315  */
316 
317 /** returns whether two vertices are adjacent in the conflict graph */
318 static
320  SCIP_Bool** adjacencymatrix, /**< adjacency matrix of conflict graph (lower half) (or NULL if an adjacencymatrix is not at hand) */
321  SCIP_DIGRAPH* conflictgraph, /**< conflict graph (or NULL if an adjacencymatrix is at hand) */
322  int vertex1, /**< first vertex */
323  int vertex2 /**< second vertex */
324  )
325 {
326  assert( adjacencymatrix != NULL || conflictgraph != NULL );
327 
328  /* we do not allow self-loops */
329  if ( vertex1 == vertex2 )
330  return FALSE;
331 
332  /* for debugging */
333  if ( adjacencymatrix == NULL )
334  {
335  int succvertex;
336  int* succ;
337  int nsucc1;
338  int nsucc2;
339  int j;
340 
341  nsucc1 = SCIPdigraphGetNSuccessors(conflictgraph, vertex1);
342  nsucc2 = SCIPdigraphGetNSuccessors(conflictgraph, vertex2);
343 
344  if ( nsucc1 < 1 || nsucc2 < 1 )
345  return FALSE;
346 
347  if ( nsucc1 > nsucc2 )
348  {
349  SCIPswapInts(&vertex1, &vertex2);
350  SCIPswapInts(&nsucc1, &nsucc2);
351  }
352 
353  succ = SCIPdigraphGetSuccessors(conflictgraph, vertex1);
354  SCIPsortInt(succ, nsucc1);
355 
356  for (j = 0; j < nsucc1; ++j)
357  {
358  succvertex = succ[j];
359  if ( succvertex == vertex2 )
360  return TRUE;
361  else if ( succvertex > vertex2 )
362  return FALSE;
363  }
364  }
365  else
366  {
367  if ( vertex1 < vertex2 )
368  return adjacencymatrix[vertex2][vertex1];
369  else
370  return adjacencymatrix[vertex1][vertex2];
371  }
372 
373  return FALSE;
374 }
375 
376 
377 /** checks whether a variable violates an SOS1 constraint w.r.t. sol together with at least one other variable */
378 static
380  SCIP* scip, /**< SCIP data structure */
381  SCIP_DIGRAPH* conflictgraph, /**< conflict graph (or NULL if an adjacencymatrix is at hand) */
382  int node, /**< node of variable in the conflict graph */
383  SCIP_SOL* sol /**< solution, or NULL to use current node's solution */
384  )
385 {
386  SCIP_Real solval;
387  SCIP_VAR* var;
388 
389  assert( scip != NULL );
390  assert( conflictgraph != NULL );
391  assert( node >= 0 );
392 
393  var = SCIPnodeGetVarSOS1(conflictgraph, node);
394  assert( var != NULL );
395  solval = SCIPgetSolVal(scip, sol, var);
396 
397  /* check whether variable is nonzero w.r.t. sol and the bounds have not been fixed to zero by propagation */
398  if ( ! SCIPisFeasZero(scip, solval) && ( ! SCIPisFeasZero(scip, SCIPvarGetLbLocal(var)) || ! SCIPisFeasZero(scip, SCIPvarGetUbLocal(var)) ) )
399  {
400  int* succ;
401  int nsucc;
402  int s;
403 
404  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, node);
405  succ = SCIPdigraphGetSuccessors(conflictgraph, node);
406 
407  /* check whether a neighbor variable is nonzero w.r.t. sol */
408  for (s = 0; s < nsucc; ++s)
409  {
410  var = SCIPnodeGetVarSOS1(conflictgraph, succ[s]);
411  assert( var != NULL );
412  solval = SCIPgetSolVal(scip, sol, var);
413  if ( ! SCIPisFeasZero(scip, solval) && ( ! SCIPisFeasZero(scip, SCIPvarGetLbLocal(var)) || ! SCIPisFeasZero(scip, SCIPvarGetUbLocal(var)) ) )
414  return TRUE;
415  }
416  }
417 
418  return FALSE;
419 }
420 
421 
422 /** returns solution value of imaginary binary big-M variable of a given node from the conflict graph */
423 static
425  SCIP* scip, /**< SCIP pointer */
426  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
427  SCIP_SOL* sol, /**< primal solution, or NULL for current LP/pseudo solution */
428  int node /**< node of the conflict graph */
429  )
430 {
432  SCIP_VAR* var;
433  SCIP_Real val;
434 
435  assert( scip != NULL );
436  assert( conflictgraph != NULL );
437  assert( node >= 0 && node < SCIPdigraphGetNNodes(conflictgraph) );
438 
439  var = SCIPnodeGetVarSOS1(conflictgraph, node);
440  val = SCIPgetSolVal(scip, sol, var);
441 
442  if ( SCIPisFeasNegative(scip, val) )
443  {
444  bound = SCIPvarGetLbLocal(var);
445  assert( SCIPisFeasNegative(scip, bound) );
446 
447  if ( SCIPisInfinity(scip, -val) )
448  return 1.0;
449  else if ( SCIPisInfinity(scip, -bound) )
450  return 0.0;
451  else
452  return (val/bound);
453  }
454  else if ( SCIPisFeasPositive(scip, val) )
455  {
456  bound = SCIPvarGetUbLocal(var);
457  assert( SCIPisFeasPositive(scip, bound) );
458  assert( SCIPisFeasPositive(scip, val) );
459 
460  if ( SCIPisInfinity(scip, val) )
461  return 1.0;
462  else if ( SCIPisInfinity(scip, bound) )
463  return 0.0;
464  else
465  return (val/bound);
466  }
467  else
468  return 0.0;
469 }
470 
471 
472 /** gets (variable) lower bound value of current LP relaxation solution for a given node from the conflict graph */
473 static
475  SCIP* scip, /**< SCIP pointer */
476  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
477  SCIP_SOL* sol, /**< primal solution, or NULL for current LP/pseudo solution */
478  int node /**< node of the conflict graph */
479  )
480 {
481  SCIP_NODEDATA* nodedata;
482 
483  assert( scip != NULL );
484  assert( conflictgraph != NULL );
485  assert( node >= 0 && node < SCIPdigraphGetNNodes(conflictgraph) );
486 
487  /* get node data */
488  nodedata = (SCIP_NODEDATA*)SCIPdigraphGetNodeData(conflictgraph, node);
489  assert( nodedata != NULL );
490 
491  /* if variable is not involved in a variable upper bound constraint */
492  if ( nodedata->lbboundvar == NULL || ! nodedata->lbboundcomp )
493  return SCIPvarGetLbLocal(nodedata->var);
494 
495  return nodedata->lbboundcoef * SCIPgetSolVal(scip, sol, nodedata->lbboundvar);
496 }
497 
498 
499 /** gets (variable) upper bound value of current LP relaxation solution for a given node from the conflict graph */
500 static
502  SCIP* scip, /**< SCIP pointer */
503  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
504  SCIP_SOL* sol, /**< primal solution, or NULL for current LP/pseudo solution */
505  int node /**< node of the conflict graph */
506  )
507 {
508  SCIP_NODEDATA* nodedata;
509 
510  assert( scip != NULL );
511  assert( conflictgraph != NULL );
512  assert( node >= 0 && node < SCIPdigraphGetNNodes(conflictgraph) );
513 
514  /* get node data */
515  nodedata = (SCIP_NODEDATA*)SCIPdigraphGetNodeData(conflictgraph, node);
516  assert( nodedata != NULL );
517 
518  /* if variable is not involved in a variable upper bound constraint */
519  if ( nodedata->ubboundvar == NULL || ! nodedata->ubboundcomp )
520  return SCIPvarGetUbLocal(nodedata->var);
521 
522  return nodedata->ubboundcoef * SCIPgetSolVal(scip, sol, nodedata->ubboundvar);
523 }
524 
525 
526 /** returns whether variable is part of the SOS1 conflict graph */
527 static
529  SCIP_CONSHDLRDATA* conshdlrdata, /**< SOS1 constraint handler */
530  SCIP_VAR* var /**< variable */
531  )
532 {
533  assert( conshdlrdata != NULL );
534  assert( var != NULL );
535 
536  if ( conshdlrdata->varhash == NULL || ! SCIPhashmapExists(conshdlrdata->varhash, var) )
537  return FALSE;
538 
539  return TRUE;
540 }
541 
542 
543 /** returns SOS1 index of variable or -1 if variable is not part of the SOS1 conflict graph */
544 static
545 int varGetNodeSOS1(
546  SCIP_CONSHDLRDATA* conshdlrdata, /**< SOS1 constraint handler */
547  SCIP_VAR* var /**< variable */
548  )
549 {
550  assert( conshdlrdata != NULL );
551  assert( var != NULL );
552  assert( conshdlrdata->varhash != NULL );
553 
554  if ( ! SCIPhashmapExists(conshdlrdata->varhash, var) )
555  return -1;
556 
557  return (int) (size_t) SCIPhashmapGetImage(conshdlrdata->varhash, var);
558 }
559 
560 
561 /** fix variable in given node to 0 or add constraint if variable is multi-aggregated
562  *
563  * @todo Try to handle multi-aggregated variables as in fixVariableZero() below.
564  */
565 static
567  SCIP* scip, /**< SCIP pointer */
568  SCIP_VAR* var, /**< variable to be fixed to 0*/
569  SCIP_NODE* node, /**< node */
570  SCIP_Bool* infeasible /**< if fixing is infeasible */
571  )
572 {
573  /* if variable cannot be nonzero */
574  *infeasible = FALSE;
576  {
577  *infeasible = TRUE;
578  return SCIP_OKAY;
579  }
580 
581  /* if variable is multi-aggregated */
583  {
584  SCIP_CONS* cons;
585  SCIP_Real val;
586 
587  val = 1.0;
588 
589  if ( ! SCIPisFeasZero(scip, SCIPvarGetLbLocal(var)) || ! SCIPisFeasZero(scip, SCIPvarGetUbLocal(var)) )
590  {
591  SCIPdebugMsg(scip, "creating constraint to force multi-aggregated variable <%s> to 0.\n", SCIPvarGetName(var));
592  /* we have to insert a local constraint var = 0 */
593  SCIP_CALL( SCIPcreateConsLinear(scip, &cons, "branch", 1, &var, &val, 0.0, 0.0, TRUE, TRUE, TRUE, TRUE, TRUE,
594  TRUE, FALSE, FALSE, FALSE, FALSE) );
595  SCIP_CALL( SCIPaddConsNode(scip, node, cons, NULL) );
596  SCIP_CALL( SCIPreleaseCons(scip, &cons) );
597  }
598  }
599  else
600  {
601  if ( ! SCIPisFeasZero(scip, SCIPvarGetLbLocal(var)) )
602  SCIP_CALL( SCIPchgVarLbNode(scip, node, var, 0.0) );
603  if ( ! SCIPisFeasZero(scip, SCIPvarGetUbLocal(var)) )
604  SCIP_CALL( SCIPchgVarUbNode(scip, node, var, 0.0) );
605  }
606 
607  return SCIP_OKAY;
608 }
609 
610 
611 /** try to fix variable to 0
612  *
613  * Try to treat fixing by special consideration of multiaggregated variables. For a multi-aggregation
614  * \f[
615  * x = \sum_{i=1}^n \alpha_i x_i + c,
616  * \f]
617  * we can express the fixing \f$x = 0\f$ by fixing all \f$x_i\f$ to 0 if \f$c = 0\f$ and the lower bounds of \f$x_i\f$
618  * are nonnegative if \f$\alpha_i > 0\f$ or the upper bounds are nonpositive if \f$\alpha_i < 0\f$.
619  */
620 static
622  SCIP* scip, /**< SCIP pointer */
623  SCIP_VAR* var, /**< variable to be fixed to 0*/
624  SCIP_Bool* infeasible, /**< if fixing is infeasible */
625  SCIP_Bool* tightened /**< if fixing was performed */
626  )
627 {
628  assert( scip != NULL );
629  assert( var != NULL );
630  assert( infeasible != NULL );
631  assert( tightened != NULL );
632 
633  *infeasible = FALSE;
634  *tightened = FALSE;
635 
637  {
638  SCIP_Real aggrconst;
639 
640  /* if constant is 0 */
641  aggrconst = SCIPvarGetMultaggrConstant(var);
642  if ( SCIPisZero(scip, aggrconst) )
643  {
644  SCIP_VAR** aggrvars;
645  SCIP_Real* aggrvals;
646  SCIP_Bool allnonnegative = TRUE;
647  int naggrvars;
648  int i;
649 
651 
652  /* check whether all variables are "nonnegative" */
653  naggrvars = SCIPvarGetMultaggrNVars(var);
654  aggrvars = SCIPvarGetMultaggrVars(var);
655  aggrvals = SCIPvarGetMultaggrScalars(var);
656  for (i = 0; i < naggrvars; ++i)
657  {
658  if ( (SCIPisPositive(scip, aggrvals[i]) && SCIPisNegative(scip, SCIPvarGetLbLocal(aggrvars[i]))) ||
659  (SCIPisNegative(scip, aggrvals[i]) && SCIPisPositive(scip, SCIPvarGetUbLocal(aggrvars[i]))) )
660  {
661  allnonnegative = FALSE;
662  break;
663  }
664  }
665 
666  if ( allnonnegative )
667  {
668  /* all variables are nonnegative -> fix variables */
669  for (i = 0; i < naggrvars; ++i)
670  {
671  SCIP_Bool fixed;
672  SCIP_CALL( SCIPfixVar(scip, aggrvars[i], 0.0, infeasible, &fixed) );
673  if ( *infeasible )
674  return SCIP_OKAY;
675  *tightened = *tightened || fixed;
676  }
677  }
678  }
679  }
680  else
681  {
682  SCIP_CALL( SCIPfixVar(scip, var, 0.0, infeasible, tightened) );
683  }
684 
685  return SCIP_OKAY;
686 }
687 
688 
689 /** fix variable in local node to 0, and return whether the operation was feasible
690  *
691  * @note We do not add a linear constraint if the variable is multi-aggregated as in
692  * fixVariableZeroNode(), since this would be too time consuming.
693  */
694 static
696  SCIP* scip, /**< SCIP pointer */
697  SCIP_VAR* var, /**< variable to be fixed to 0*/
698  SCIP_CONS* cons, /**< constraint */
699  int inferinfo, /**< info for reverse prop. */
700  SCIP_Bool* infeasible, /**< if fixing is infeasible */
701  SCIP_Bool* tightened, /**< if fixing was performed */
702  SCIP_Bool* success /**< whether fixing was successful, i.e., variable is not multi-aggregated */
703  )
704 {
705  *infeasible = FALSE;
706  *tightened = FALSE;
707  *success = FALSE;
708 
709  /* if variable cannot be nonzero */
711  {
712  *infeasible = TRUE;
713  return SCIP_OKAY;
714  }
715 
716  /* directly fix variable if it is not multi-aggregated */
718  {
719  SCIP_Bool tighten;
720 
721  /* fix lower bound */
722  SCIP_CALL( SCIPinferVarLbCons(scip, var, 0.0, cons, inferinfo, FALSE, infeasible, &tighten) );
723  *tightened = *tightened || tighten;
724 
725  /* fix upper bound */
726  SCIP_CALL( SCIPinferVarUbCons(scip, var, 0.0, cons, inferinfo, FALSE, infeasible, &tighten) );
727  *tightened = *tightened || tighten;
728 
729  *success = TRUE;
730  }
731 
732  return SCIP_OKAY;
733 }
734 
735 
736 /** add lock on variable */
737 static
739  SCIP* scip, /**< SCIP data structure */
740  SCIP_CONS* cons, /**< constraint */
741  SCIP_VAR* var /**< variable */
742  )
743 {
744  assert( scip != NULL );
745  assert( cons != NULL );
746  assert( var != NULL );
747 
748  /* rounding down == bad if lb < 0, rounding up == bad if ub > 0 */
750 
751  return SCIP_OKAY;
752 }
753 
754 
755 /** remove lock on variable */
756 static
758  SCIP* scip, /**< SCIP data structure */
759  SCIP_CONS* cons, /**< constraint */
760  SCIP_VAR* var /**< variable */
761  )
762 {
763  assert( scip != NULL );
764  assert( cons != NULL );
765  assert( var != NULL );
766 
767  /* rounding down == bad if lb < 0, rounding up == bad if ub > 0 */
769 
770  return SCIP_OKAY;
771 }
772 
773 
774 /** ensures that the vars and weights array can store at least num entries */
775 static
777  SCIP* scip, /**< SCIP data structure */
778  SCIP_CONSDATA* consdata, /**< constraint data */
779  int num, /**< minimum number of entries to store */
780  SCIP_Bool reserveWeights /**< whether the weights array is handled */
781  )
782 {
783  assert( consdata != NULL );
784  assert( consdata->nvars <= consdata->maxvars );
785 
786  if ( num > consdata->maxvars )
787  {
788  int newsize;
789 
790  newsize = SCIPcalcMemGrowSize(scip, num);
791  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->vars, consdata->maxvars, newsize) );
792  if ( reserveWeights )
793  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->weights, consdata->maxvars, newsize) );
794  consdata->maxvars = newsize;
795  }
796  assert( num <= consdata->maxvars );
797 
798  return SCIP_OKAY;
799 }
800 
801 
802 /** handle new variable */
803 static
805  SCIP* scip, /**< SCIP data structure */
806  SCIP_CONS* cons, /**< constraint */
807  SCIP_CONSDATA* consdata, /**< constraint data */
808  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
809  SCIP_VAR* var, /**< variable */
810  SCIP_Bool transformed /**< whether original variable was transformed */
811  )
812 {
813  SCIP_DIGRAPH* conflictgraph;
814  int node;
815 
816  assert( scip != NULL );
817  assert( cons != NULL );
818  assert( consdata != NULL );
819  assert( conshdlrdata != NULL );
820  assert( var != NULL );
821 
822  /* if we are in transformed problem, catch the variable's events */
823  if ( transformed )
824  {
825  assert( conshdlrdata->eventhdlr != NULL );
826 
827  /* catch bound change events of variable */
828  SCIP_CALL( SCIPcatchVarEvent(scip, var, SCIP_EVENTTYPE_BOUNDCHANGED, conshdlrdata->eventhdlr,
829  (SCIP_EVENTDATA*)cons, NULL) ); /*lint !e740*/
830 
831  /* if the variable if fixed to nonzero */
832  assert( consdata->nfixednonzeros >= 0 );
834  ++consdata->nfixednonzeros;
835  }
836 
837  /* install the rounding locks for the new variable */
838  SCIP_CALL( lockVariableSOS1(scip, cons, var) );
839 
840  /* branching on multiaggregated variables does not seem to work well, so avoid it */
841  SCIP_CALL( SCIPmarkDoNotMultaggrVar(scip, var) );
842 
843  /* add the new coefficient to the upper bound LP row, if necessary */
844  if ( consdata->rowub != NULL && ! SCIPisInfinity(scip, SCIPvarGetUbGlobal(var)) && ! SCIPisZero(scip, SCIPvarGetUbGlobal(var)) )
845  {
846  SCIP_CALL( SCIPaddVarToRow(scip, consdata->rowub, var, 1.0/SCIPvarGetUbGlobal(var)) );
847  }
848 
849  /* add the new coefficient to the lower bound LP row, if necessary */
850  if ( consdata->rowlb != NULL && ! SCIPisInfinity(scip, SCIPvarGetLbGlobal(var)) && ! SCIPisZero(scip, SCIPvarGetLbGlobal(var)) )
851  {
852  SCIP_CALL( SCIPaddVarToRow(scip, consdata->rowlb, var, 1.0/SCIPvarGetLbGlobal(var)) );
853  }
854 
855  /* return if the conflict graph has not been created yet */
856  conflictgraph = conshdlrdata->conflictgraph;
857  if ( conflictgraph == NULL )
858  return SCIP_OKAY;
859 
860  /* get node of variable in the conflict graph (or -1) */
861  node = varGetNodeSOS1(conshdlrdata, var);
862  assert( node < conshdlrdata->nsos1vars );
863 
864  /* if the variable is not already a node of the conflict graph */
865  if ( node < 0 )
866  {
867  /* variable does not appear in the conflict graph: switch to SOS1 branching rule, which does not make use of a conflict graph
868  * @todo: maybe recompute the conflict graph, implication graph and varhash instead */
869  SCIPdebugMsg(scip, "Switched to SOS1 branching rule, since conflict graph could be infeasible.\n");
870  conshdlrdata->switchsos1branch = TRUE;
871  return SCIP_OKAY;
872  }
873 
874  /* if the constraint is local, then there is no need to act, since local constraints are handled by the local conflict graph in the
875  * function enforceConflictgraph() */
876  if ( ! consdata->local )
877  {
878  SCIP_VAR** vars;
879  int nvars;
880  int v;
881 
882  vars = consdata->vars;
883  nvars = consdata->nvars;
884 
885  for (v = 0; v < nvars; ++v)
886  {
887  int nodev;
888 
889  if ( var == vars[v] )
890  continue;
891 
892  /* get node of variable in the conflict graph (or -1) */
893  nodev = varGetNodeSOS1(conshdlrdata, vars[v]);
894  assert( nodev < conshdlrdata->nsos1vars );
895 
896  /* if the variable is already a node of the conflict graph */
897  if ( nodev >= 0 )
898  {
899  int nsucc;
900  int nsuccv;
901 
902  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, node);
903  nsuccv = SCIPdigraphGetNSuccessors(conflictgraph, nodev);
904 
905  /* add arcs if not existent */
906  SCIP_CALL( SCIPdigraphAddArcSafe(conflictgraph, nodev, node, NULL) );
907  SCIP_CALL( SCIPdigraphAddArcSafe(conflictgraph, node, nodev, NULL) );
908 
909  /* in case of new arcs: sort successors in ascending order */
910  if ( nsucc < SCIPdigraphGetNSuccessors(conflictgraph, node) )
911  {
912  SCIPdebugMsg(scip, "Added new conflict graph arc from variable %s to variable %s.\n", SCIPvarGetName(var), SCIPvarGetName(vars[v]));
913  SCIPsortInt(SCIPdigraphGetSuccessors(conflictgraph, node), SCIPdigraphGetNSuccessors(conflictgraph, node));
914  }
915 
916  if ( nsuccv < SCIPdigraphGetNSuccessors(conflictgraph, nodev) )
917  {
918  SCIPdebugMsg(scip, "Added new conflict graph arc from variable %s to variable %s.\n", SCIPvarGetName(vars[v]), SCIPvarGetName(var));
919  SCIPsortInt(SCIPdigraphGetSuccessors(conflictgraph, nodev), SCIPdigraphGetNSuccessors(conflictgraph, nodev));
920  }
921  }
922  else
923  {
924  /* variable does not appear in the conflict graph: switch to SOS1 branching rule, which does not make use of a conflict graph
925  * @todo: maybe recompute the conflict graph, implication graph and varhash instead */
926  SCIPdebugMsg(scip, "Switched to SOS1 branching rule, since conflict graph could be infeasible.\n");
927  conshdlrdata->switchsos1branch = TRUE;
928  return SCIP_OKAY;
929  }
930  }
931  }
932 
933  return SCIP_OKAY;
934 }
935 
936 
937 /** adds a variable to an SOS1 constraint, at position given by weight - ascending order */
938 static
940  SCIP* scip, /**< SCIP data structure */
941  SCIP_CONS* cons, /**< constraint */
942  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
943  SCIP_VAR* var, /**< variable to add to the constraint */
944  SCIP_Real weight /**< weight to determine position */
945  )
946 {
947  SCIP_CONSDATA* consdata;
948  SCIP_Bool transformed;
949  int pos;
950  int j;
951 
952  assert( var != NULL );
953  assert( cons != NULL );
954  assert( conshdlrdata != NULL );
955 
956  consdata = SCIPconsGetData(cons);
957  assert( consdata != NULL );
958 
959  if ( consdata->weights == NULL && consdata->maxvars > 0 )
960  {
961  SCIPerrorMessage("cannot add variable to SOS1 constraint <%s> that does not contain weights.\n", SCIPconsGetName(cons));
962  return SCIP_INVALIDCALL;
963  }
964 
965  /* are we in the transformed problem? */
966  transformed = SCIPconsIsTransformed(cons);
967 
968  /* always use transformed variables in transformed constraints */
969  if ( transformed )
970  {
971  SCIP_CALL( SCIPgetTransformedVar(scip, var, &var) );
972  }
973  assert( var != NULL );
974  assert( transformed == SCIPvarIsTransformed(var) );
975 
976  SCIP_CALL( consdataEnsurevarsSizeSOS1(scip, consdata, consdata->nvars + 1, TRUE) );
977  assert( consdata->weights != NULL );
978  assert( consdata->maxvars >= consdata->nvars+1 );
979 
980  /* find variable position */
981  for (pos = 0; pos < consdata->nvars; ++pos)
982  {
983  if ( consdata->weights[pos] > weight )
984  break;
985  }
986  assert( 0 <= pos && pos <= consdata->nvars );
987 
988  /* move other variables, if necessary */
989  for (j = consdata->nvars; j > pos; --j)
990  {
991  consdata->vars[j] = consdata->vars[j-1];
992  consdata->weights[j] = consdata->weights[j-1];
993  }
994 
995  /* insert variable */
996  consdata->vars[pos] = var;
997  consdata->weights[pos] = weight;
998  ++consdata->nvars;
999 
1000  /* handle the new variable */
1001  SCIP_CALL( handleNewVariableSOS1(scip, cons, consdata, conshdlrdata, var, transformed) );
1002 
1003  return SCIP_OKAY;
1004 }
1005 
1006 
1007 /** appends a variable to an SOS1 constraint */
1008 static
1010  SCIP* scip, /**< SCIP data structure */
1011  SCIP_CONS* cons, /**< constraint */
1012  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
1013  SCIP_VAR* var /**< variable to add to the constraint */
1014  )
1016  SCIP_CONSDATA* consdata;
1017  SCIP_Bool transformed;
1018 
1019  assert( var != NULL );
1020  assert( cons != NULL );
1021  assert( conshdlrdata != NULL );
1022 
1023  consdata = SCIPconsGetData(cons);
1024  assert( consdata != NULL );
1025 
1026  /* are we in the transformed problem? */
1027  transformed = SCIPconsIsTransformed(cons);
1028 
1029  /* always use transformed variables in transformed constraints */
1030  if ( transformed )
1031  {
1032  SCIP_CALL( SCIPgetTransformedVar(scip, var, &var) );
1033  }
1034  assert( var != NULL );
1035  assert( transformed == SCIPvarIsTransformed(var) );
1036 
1037  SCIP_CALL( consdataEnsurevarsSizeSOS1(scip, consdata, consdata->nvars + 1, FALSE) );
1038 
1039  /* insert variable */
1040  consdata->vars[consdata->nvars] = var;
1041  assert( consdata->weights != NULL || consdata->nvars > 0 );
1042  if ( consdata->weights != NULL && consdata->nvars > 0 )
1043  consdata->weights[consdata->nvars] = consdata->weights[consdata->nvars-1] + 1.0;
1044  ++consdata->nvars;
1045 
1046  /* handle the new variable */
1047  SCIP_CALL( handleNewVariableSOS1(scip, cons, consdata, conshdlrdata, var, transformed) );
1048 
1049  return SCIP_OKAY;
1050 }
1051 
1052 
1053 /** deletes a variable of an SOS1 constraint */
1054 static
1056  SCIP* scip, /**< SCIP data structure */
1057  SCIP_CONS* cons, /**< constraint */
1058  SCIP_CONSDATA* consdata, /**< constraint data */
1059  SCIP_EVENTHDLR* eventhdlr, /**< corresponding event handler */
1060  int pos /**< position of variable in array */
1061  )
1062 {
1063  int j;
1064 
1065  assert( 0 <= pos && pos < consdata->nvars );
1066 
1067  /* remove lock of variable */
1068  SCIP_CALL( unlockVariableSOS1(scip, cons, consdata->vars[pos]) );
1069 
1070  /* drop events on variable */
1071  SCIP_CALL( SCIPdropVarEvent(scip, consdata->vars[pos], SCIP_EVENTTYPE_BOUNDCHANGED, eventhdlr, (SCIP_EVENTDATA*)cons, -1) ); /*lint !e740*/
1072 
1073  /* delete variable - need to copy since order is important */
1074  for (j = pos; j < consdata->nvars-1; ++j)
1075  {
1076  consdata->vars[j] = consdata->vars[j+1]; /*lint !e679*/
1077  if ( consdata->weights != NULL )
1078  consdata->weights[j] = consdata->weights[j+1]; /*lint !e679*/
1079  }
1080  --consdata->nvars;
1081 
1082  return SCIP_OKAY;
1083 }
1084 
1085 
1086 /* ----------------------------- presolving --------------------------------------*/
1087 
1088 /** extends a given clique of the conflict graph
1089  *
1090  * Implementation of the Bron-Kerbosch Algorithm from the paper:
1091  * Algorithm 457: Finding all Cliques of an Undirected Graph, Bron & Kerbosch, Commun. ACM, 1973
1092  */
1093 static
1095  SCIP* scip, /**< SCIP pointer */
1096  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
1097  SCIP_Bool** adjacencymatrix, /**< adjacencymatrix of the conflict graph (only lower half filled) */
1098  SCIP_DIGRAPH* vertexcliquegraph, /**< graph that contains the information which cliques contain a given vertex
1099  * vertices of variables = 0, ..., nsos1vars-1; vertices of cliques = nsos1vars, ..., nsos1vars+ncliques-1*/
1100  int nsos1vars, /**< number of SOS1 variables */
1101  int nconss, /**< number of SOS1 constraints */
1102  SCIP_CONS* cons, /**< constraint to be extended */
1103  SCIP_VAR** vars, /**< variables of extended clique */
1104  SCIP_Real* weights, /**< weights of extended clique */
1105  SCIP_Bool firstcall, /**< whether this is the first call of extension operator */
1106  SCIP_Bool usebacktrack, /**< whether backtracking is needed for the computation */
1107  int** cliques, /**< all cliques found so far */
1108  int* ncliques, /**< number of clique found so far */
1109  int* cliquesizes, /**< number of variables of current clique */
1110  int* newclique, /**< clique we want to extended*/
1111  int* workingset, /**< set of vertices that already served as extension and set of candidates that probably will lead to an extension */
1112  int nworkingset, /**< length of array workingset */
1113  int nexts, /**< number of vertices that already served as extension */
1114  int pos, /**< position of potential candidate */
1115  int* maxextensions, /**< maximal number of extensions */
1116  int* naddconss, /**< number of added constraints */
1117  SCIP_Bool* success /**< pointer to store if at least one new clique was found */
1118  )
1119 {
1120  int* workingsetnew = NULL;
1121  int nextsnew;
1122  int nworkingsetnew;
1123  int mincands;
1124  int btriter = 0; /* backtrack iterator */
1125  int selvertex;
1126  int selpos = -1;
1127  int fixvertex = -1;
1128  int i;
1129  int j;
1130 
1131  assert( scip != NULL );
1132  assert( conshdlrdata != NULL );
1133  assert( adjacencymatrix != NULL );
1134  assert( vertexcliquegraph != NULL );
1135  assert( cons != NULL );
1136  assert( cliques != NULL );
1137  assert( cliquesizes != NULL );
1138  assert( newclique != NULL );
1139  assert( workingset != NULL );
1140  assert( maxextensions != NULL );
1141  assert( naddconss != NULL );
1142  assert( success != NULL );
1143 
1144  if ( firstcall )
1145  *success = FALSE;
1146 
1147  mincands = nworkingset;
1148  if ( mincands < 1 )
1149  return SCIP_OKAY;
1150 
1151  /* allocate buffer array */
1152  SCIP_CALL( SCIPallocBufferArray(scip, &workingsetnew, nworkingset) );
1153 
1154 #ifdef SCIP_DEBUG
1155  for (i = 0; i < nexts; ++i)
1156  {
1157  int vertex = workingset[i];
1158  for (j = nexts; j < nworkingset; ++j)
1159  {
1160  assert( isConnectedSOS1(adjacencymatrix, NULL, vertex, workingset[j]) );
1161  }
1162  }
1163 #endif
1164 
1165  /* determine candidate with minimum number of disconnections */
1166  for (i = 0; i < nworkingset; ++i)
1167  {
1168  int vertex;
1169  int cnt = 0;
1170 
1171  vertex = workingset[i];
1172 
1173  /* count disconnections */
1174  for (j = nexts; j < nworkingset && cnt < mincands; ++j)
1175  {
1176  if ( vertex != workingset[j] && ! isConnectedSOS1(adjacencymatrix, NULL, vertex, workingset[j]) )
1177  {
1178  cnt++;
1179 
1180  /* save position of potential candidate */
1181  pos = j;
1182  }
1183  }
1184 
1185  /* check whether a new minimum was found */
1186  if ( cnt < mincands )
1187  {
1188  fixvertex = vertex;
1189  mincands = cnt;
1190  if ( i < nexts )
1191  {
1192  assert( pos >= 0 );
1193  selpos = pos;
1194  }
1195  else
1196  {
1197  selpos = i;
1198 
1199  /* preincrement */
1200  btriter = 1;
1201  }
1202  }
1203  }
1204 
1205  /* If fixed point is initially chosen from candidates then number of disconnections will be preincreased by one. */
1206 
1207  /* backtrackcycle */
1208  for (btriter = mincands + btriter; btriter >= 1; --btriter)
1209  {
1210  assert( selpos >= 0);
1211  assert( fixvertex >= 0);
1212 
1213  /* interchange */
1214  selvertex = workingset[selpos];
1215  workingset[selpos] = workingset[nexts];
1216  workingset[nexts] = selvertex;
1217 
1218  /* create new workingset */
1219  nextsnew = 0;
1220  for (j = 0 ; j < nexts; ++j)
1221  {
1222  if ( isConnectedSOS1(adjacencymatrix, NULL, selvertex, workingset[j]) )
1223  workingsetnew[nextsnew++] = workingset[j];
1224  }
1225  nworkingsetnew = nextsnew;
1226  for (j = nexts + 1; j < nworkingset; ++j)
1227  {
1228  if ( isConnectedSOS1(adjacencymatrix, NULL, selvertex, workingset[j]) )
1229  workingsetnew[nworkingsetnew++] = workingset[j];
1230  }
1231 
1232  newclique[cliquesizes[*ncliques]++] = selvertex;
1233 
1234  /* if we found a new clique */
1235  if ( nworkingsetnew == 0 )
1236  {
1237  char consname[SCIP_MAXSTRLEN];
1238  SCIP_CONSDATA* consdata;
1239  SCIP_CONS* newcons;
1240  int cliqueind;
1241 
1242  cliqueind = nsos1vars + *ncliques; /* index of clique in the vertex-clique graph */
1243 
1244  /* save new clique */
1245  assert( cliquesizes[*ncliques] >= 0 && cliquesizes[*ncliques] <= nsos1vars );
1246  assert( *ncliques < MAX(1, conshdlrdata->maxextensions) * nconss );
1247  SCIP_CALL( SCIPallocBufferArray(scip, &(cliques[*ncliques]), cliquesizes[*ncliques]) );/*lint !e866*/
1248  for (j = 0 ; j < cliquesizes[*ncliques]; ++j)
1249  {
1250  vars[j] = SCIPnodeGetVarSOS1(conshdlrdata->conflictgraph, newclique[j]);
1251  weights[j] = j+1;
1252  cliques[*ncliques][j] = newclique[j];
1253  }
1254 
1255  SCIPsortInt(cliques[*ncliques], cliquesizes[*ncliques]);
1256 
1257  /* create new constraint */
1258  (void) SCIPsnprintf(consname, SCIP_MAXSTRLEN, "extsos1_%" SCIP_LONGINT_FORMAT, conshdlrdata->cntextsos1, conshdlrdata->cntextsos1);
1259 
1260  SCIP_CALL( SCIPcreateConsSOS1(scip, &newcons, consname, cliquesizes[*ncliques], vars, weights,
1264  SCIPconsIsDynamic(cons),
1266 
1267  consdata = SCIPconsGetData(newcons);
1268 
1269  /* add directed edges to the vertex-clique graph */
1270  for (j = 0; j < consdata->nvars; ++j)
1271  {
1272  /* add arc from clique vertex to clique (needed in presolRoundConssSOS1() to delete redundand cliques) */
1273  SCIP_CALL( SCIPdigraphAddArcSafe(vertexcliquegraph, cliques[*ncliques][j], cliqueind, NULL) );
1274  }
1275 
1276  SCIP_CALL( SCIPaddCons(scip, newcons) );
1277  SCIP_CALL( SCIPreleaseCons(scip, &newcons) );
1278 
1279  ++(*naddconss);
1280  ++(conshdlrdata->cntextsos1);
1281  ++(*ncliques);
1282  cliquesizes[*ncliques] = cliquesizes[*ncliques-1]; /* cliquesizes[*ncliques] = size of newclique */
1283 
1284  *success = TRUE;
1285 
1286  --(*maxextensions);
1287 
1288  if ( *maxextensions <= 0 )
1289  {
1290  SCIPfreeBufferArray(scip, &workingsetnew);
1291  return SCIP_OKAY;
1292  }
1293  }
1294  else if ( nextsnew < nworkingsetnew ) /* else if the number of of candidates equals zero */
1295  {
1296  /* if backtracking is used, it is necessary to keep the memory for 'workingsetnew' */
1297  if ( usebacktrack )
1298  {
1299  SCIP_CALL( extensionOperatorSOS1(scip, conshdlrdata, adjacencymatrix, vertexcliquegraph, nsos1vars, nconss, cons, vars, weights, FALSE, usebacktrack,
1300  cliques, ncliques, cliquesizes, newclique, workingsetnew, nworkingsetnew, nextsnew, pos, maxextensions, naddconss, success) );
1301  if ( *maxextensions <= 0 )
1302  {
1303  SCIPfreeBufferArrayNull(scip, &workingsetnew);
1304  return SCIP_OKAY;
1305  }
1306  }
1307  else
1308  {
1309  int w;
1310 
1311  assert( nworkingset >= nworkingsetnew );
1312  for (w = 0; w < nworkingsetnew; ++w)
1313  workingset[w] = workingsetnew[w];
1314  nworkingset = nworkingsetnew;
1315 
1316  SCIPfreeBufferArrayNull(scip, &workingsetnew);
1317 
1318  SCIP_CALL( extensionOperatorSOS1(scip, conshdlrdata, adjacencymatrix, vertexcliquegraph, nsos1vars, nconss, cons, vars, weights, FALSE, usebacktrack,
1319  cliques, ncliques, cliquesizes, newclique, workingset, nworkingset, nextsnew, pos, maxextensions, naddconss, success) );
1320  assert( *maxextensions <= 0 );
1321  return SCIP_OKAY;
1322  }
1323  }
1324  assert( workingsetnew != NULL );
1325  assert( workingset != NULL );
1326 
1327  /* remove selvertex from clique */
1328  --cliquesizes[*ncliques];
1329 
1330  /* add selvertex to the set of vertices that already served as extension */
1331  ++nexts;
1332 
1333  if ( btriter > 1 )
1334  {
1335  /* select a candidate that is not connected to the fixed vertex */
1336  for (j = nexts; j < nworkingset; ++j)
1337  {
1338  assert( fixvertex != workingset[j] );
1339  if ( ! isConnectedSOS1(adjacencymatrix, NULL, fixvertex, workingset[j]) )
1340  {
1341  selpos = j;
1342  break;
1343  }
1344  }
1345  }
1346  }
1347 
1348  SCIPfreeBufferArrayNull(scip, &workingsetnew);
1349 
1350  return SCIP_OKAY;
1351 }
1352 
1353 
1354 /** generates conflict graph that is induced by the variables of a linear constraint */
1355 static
1357  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
1358  SCIP_DIGRAPH* conflictgraphlin, /**< conflict graph of linear constraint (nodes: 1, ..., nlinvars) */
1359  SCIP_DIGRAPH* conflictgraphorig, /**< original conflict graph (nodes: 1, ..., nsos1vars) */
1360  SCIP_VAR** linvars, /**< linear variables in linear constraint */
1361  int nlinvars, /**< number of linear variables in linear constraint */
1362  int* posinlinvars /**< posinlinvars[i] = position (index) of SOS1 variable i in linear constraint,
1363  * posinlinvars[i]= -1 if @p i is not a SOS1 variable or not a variable of the linear constraint */
1364  )
1365 {
1366  int indexinsosvars;
1367  int indexinlinvars;
1368  int* succ;
1369  int nsucc;
1370  int v;
1371  int s;
1372 
1373  assert( conflictgraphlin != NULL );
1374  assert( conflictgraphorig != NULL );
1375  assert( linvars != NULL );
1376  assert( posinlinvars != NULL );
1377 
1378  for (v = 1; v < nlinvars; ++v) /* we start with v = 1, since "indexinlinvars < v" (see below) is never fulfilled for v = 0 */
1379  {
1380  indexinsosvars = varGetNodeSOS1(conshdlrdata, linvars[v]);
1381 
1382  /* if linvars[v] is contained in at least one SOS1 constraint */
1383  if ( indexinsosvars >= 0 )
1384  {
1385  succ = SCIPdigraphGetSuccessors(conflictgraphorig, indexinsosvars);
1386  nsucc = SCIPdigraphGetNSuccessors(conflictgraphorig, indexinsosvars);
1387 
1388  for (s = 0; s < nsucc; ++s)
1389  {
1390  assert( succ[s] >= 0 );
1391  indexinlinvars = posinlinvars[succ[s]];
1392  assert( indexinlinvars < nlinvars );
1393 
1394  if ( indexinlinvars >= 0 && indexinlinvars < v )
1395  {
1396  SCIP_CALL( SCIPdigraphAddArcSafe(conflictgraphlin, v, indexinlinvars, NULL) );
1397  SCIP_CALL( SCIPdigraphAddArcSafe(conflictgraphlin, indexinlinvars, v, NULL) );
1398  }
1399  }
1400  }
1401  }
1402 
1403  return SCIP_OKAY;
1404 }
1405 
1406 
1407 /** determine the common successors of the vertices from the considered clique */
1408 static
1410  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
1411  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
1412  int* clique, /**< current clique */
1413  SCIP_VAR** vars, /**< clique variables */
1414  int nvars, /**< number of clique variables */
1415  int* comsucc, /**< pointer to store common successors of clique vertices (size = nvars) */
1416  int* ncomsucc /**< pointer to store number common successors of clique vertices */
1417  )
1418 {
1419  int nsucc;
1420  int* succ;
1421  int ind;
1422  int k = 0;
1423  int v;
1424  int i;
1425  int j;
1426 
1427  assert( conflictgraph != NULL );
1428  assert( clique != NULL );
1429  assert( vars != NULL );
1430  assert( comsucc != NULL );
1431  assert( ncomsucc != NULL );
1432 
1433  *ncomsucc = 0;
1434 
1435  /* determine the common successors of the vertices from the considered clique */
1436 
1437  /* determine successors of variable var[0] that are not in the clique */
1438  assert(vars[0] != NULL );
1439  ind = varGetNodeSOS1(conshdlrdata, vars[0]);
1440 
1441  if( ind == -1 )
1442  return SCIP_INVALIDDATA;
1443 
1444  assert( ind < SCIPdigraphGetNNodes(conflictgraph) );
1445  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, ind);
1446  succ = SCIPdigraphGetSuccessors(conflictgraph, ind);
1447 
1448  for (j = 0; j < nvars; ++j)
1449  {
1450  for (i = k; i < nsucc; ++i)
1451  {
1452  if ( succ[i] > clique[j] )
1453  {
1454  k = i;
1455  break;
1456  }
1457  else if ( succ[i] == clique[j] )
1458  {
1459  k = i + 1;
1460  break;
1461  }
1462  else
1463  comsucc[(*ncomsucc)++] = succ[i];
1464  }
1465  }
1466 
1467  /* for all variables except the first one */
1468  for (v = 1; v < nvars; ++v)
1469  {
1470  int ncomsuccsave = 0;
1471  k = 0;
1472 
1473  assert(vars[v] != NULL );
1474  ind = varGetNodeSOS1(conshdlrdata, vars[v]);
1475  assert( ind >= 0 && ind < SCIPdigraphGetNNodes(conflictgraph) );
1476 
1477  if ( ind >= 0 )
1478  {
1479  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, ind);
1480  succ = SCIPdigraphGetSuccessors(conflictgraph, ind);
1481 
1482  /* determine successors that are in comsucc */
1483  for (j = 0; j < *ncomsucc; ++j)
1484  {
1485  for (i = k; i < nsucc; ++i)
1486  {
1487  if ( succ[i] > comsucc[j] )
1488  {
1489  k = i;
1490  break;
1491  }
1492  else if ( succ[i] == comsucc[j] )
1493  {
1494  comsucc[ncomsuccsave++] = succ[i];
1495  k = i + 1;
1496  break;
1497  }
1498  }
1499  }
1500  *ncomsucc = ncomsuccsave;
1501  }
1502  }
1503 
1504  return SCIP_OKAY;
1505 }
1506 
1507 
1508 /** get nodes whose corresponding SOS1 variables are nonzero if an SOS1 variable of a given node is nonzero */
1509 static
1511  SCIP* scip, /**< SCIP pointer */
1512  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
1513  SCIP_VAR** vars, /**< problem and SOS1 variables */
1514  SCIP_DIGRAPH* implgraph, /**< implication graph (@p j is successor of @p i if and only if \f$ x_i\not = 0 \Rightarrow x_j\not = 0\f$) */
1515  SCIP_HASHMAP* implhash, /**< hash map from variable to node in implication graph */
1516  SCIP_Bool* implnodes, /**< implnodes[i] = TRUE if the SOS1 variable corresponding to node i in the implication graph is implied to be nonzero */
1517  int node /**< node of the implication graph */
1518  )
1519 {
1520  SCIP_SUCCDATA** succdatas;
1521  int sos1node;
1522  int* succ;
1523  int nsucc;
1524  int s;
1525 
1526  assert( scip != NULL );
1527  assert( implgraph != NULL );
1528  assert( implnodes != NULL );
1529  assert( node >= 0 );
1530  assert( vars[node] != NULL );
1531  assert( (int) (size_t) SCIPhashmapGetImage(implhash, vars[node]) == node );
1532 
1533  /* get node of variable in the conflict graph (-1 if variable is no SOS1 variable) */
1534  sos1node = varGetNodeSOS1(conshdlrdata, vars[node]);
1535  if ( sos1node < 0 )
1536  return SCIP_OKAY;
1537 
1538  succdatas = (SCIP_SUCCDATA**) SCIPdigraphGetSuccessorsData(implgraph, node);
1539  nsucc = SCIPdigraphGetNSuccessors(implgraph, node);
1540  succ = SCIPdigraphGetSuccessors(implgraph, node);
1541 
1542  for (s = 0; s < nsucc; ++s)
1543  {
1544  SCIP_SUCCDATA* data;
1545  int succnode;
1546  succnode = succ[s];
1547  data = succdatas[s];
1548  sos1node = varGetNodeSOS1(conshdlrdata, vars[succnode]);
1549 
1550  /* if node is SOS1 and the corresponding variable is implied to be nonzero */
1551  assert( succdatas[s] != NULL );
1552  if ( sos1node >= 0 && ! implnodes[sos1node] && ( SCIPisFeasPositive(scip, data->lbimpl) || SCIPisFeasNegative(scip, data->ubimpl) ) )
1553  {
1554  assert( sos1node == succnode );
1555  implnodes[sos1node] = TRUE;
1556  SCIP_CALL( getSOS1Implications(scip, conshdlrdata, vars, implgraph, implhash, implnodes, succnode) );
1557  }
1558  }
1559 
1560  return SCIP_OKAY;
1561 }
1562 
1563 
1564 /** perform one presolving round for a single SOS1 constraint
1565  *
1566  * We perform the following presolving steps.
1567  *
1568  * - If the bounds of some variable force it to be nonzero, we can
1569  * fix all other variables to zero and remove the SOS1 constraints
1570  * that contain it.
1571  * - If a variable is fixed to zero, we can remove the variable.
1572  * - If a variable appears twice, it can be fixed to 0.
1573  * - We substitute appregated variables.
1574  */
1575 static
1577  SCIP* scip, /**< SCIP pointer */
1578  SCIP_CONS* cons, /**< constraint */
1579  SCIP_CONSDATA* consdata, /**< constraint data */
1580  SCIP_EVENTHDLR* eventhdlr, /**< event handler */
1581  SCIP_Bool* substituted, /**< whether a variable was substituted */
1582  SCIP_Bool* cutoff, /**< whether a cutoff happened */
1583  SCIP_Bool* success, /**< whether we performed a successful reduction */
1584  int* ndelconss, /**< number of deleted constraints */
1585  int* nupgdconss, /**< number of upgraded constraints */
1586  int* nfixedvars, /**< number of fixed variables */
1587  int* nremovedvars /**< number of variables removed */
1588  )
1589 {
1590  SCIP_VAR** vars;
1591  SCIP_Bool allvarsbinary;
1592  SCIP_Bool infeasible;
1593  SCIP_Bool fixed;
1594  int nfixednonzeros;
1595  int lastFixedNonzero;
1596  int j;
1597 
1598  assert( scip != NULL );
1599  assert( cons != NULL );
1600  assert( consdata != NULL );
1601  assert( eventhdlr != NULL );
1602  assert( cutoff != NULL );
1603  assert( success != NULL );
1604  assert( ndelconss != NULL );
1605  assert( nfixedvars != NULL );
1606  assert( nremovedvars != NULL );
1607 
1608  *substituted = FALSE;
1609  *cutoff = FALSE;
1610  *success = FALSE;
1611 
1612  SCIPdebugMsg(scip, "Presolving SOS1 constraint <%s>.\n", SCIPconsGetName(cons) );
1613 
1614  j = 0;
1615  nfixednonzeros = 0;
1616  lastFixedNonzero = -1;
1617  allvarsbinary = TRUE;
1618  vars = consdata->vars;
1619 
1620  /* check for variables fixed to 0 and bounds that fix a variable to be nonzero */
1621  while ( j < consdata->nvars )
1622  {
1623  int l;
1624  SCIP_VAR* var;
1625  SCIP_Real lb;
1626  SCIP_Real ub;
1627  SCIP_Real scalar;
1628  SCIP_Real constant;
1629 
1630  scalar = 1.0;
1631  constant = 0.0;
1632 
1633  /* check for aggregation: if the constant is zero the variable is zero iff the aggregated
1634  * variable is 0 */
1635  var = vars[j];
1636  SCIP_CALL( SCIPgetProbvarSum(scip, &var, &scalar, &constant) );
1637 
1638  /* if constant is zero and we get a different variable, substitute variable */
1639  if ( SCIPisZero(scip, constant) && ! SCIPisZero(scip, scalar) && var != vars[j] )
1640  {
1641  SCIPdebugMsg(scip, "substituted variable <%s> by <%s>.\n", SCIPvarGetName(vars[j]), SCIPvarGetName(var));
1642  SCIP_CALL( SCIPdropVarEvent(scip, consdata->vars[j], SCIP_EVENTTYPE_BOUNDCHANGED, eventhdlr, (SCIP_EVENTDATA*)cons, -1) ); /*lint !e740*/
1643  SCIP_CALL( SCIPcatchVarEvent(scip, var, SCIP_EVENTTYPE_BOUNDCHANGED, eventhdlr, (SCIP_EVENTDATA*)cons, NULL) ); /*lint !e740*/
1644 
1645  /* change the rounding locks */
1646  SCIP_CALL( unlockVariableSOS1(scip, cons, consdata->vars[j]) );
1647  SCIP_CALL( lockVariableSOS1(scip, cons, var) );
1648 
1649  vars[j] = var;
1650  *substituted = TRUE;
1651  }
1652 
1653  /* check whether the variable appears again later */
1654  for (l = j+1; l < consdata->nvars; ++l)
1655  {
1656  /* if variable appeared before, we can fix it to 0 and remove it */
1657  if ( vars[j] == vars[l] )
1658  {
1659  SCIPdebugMsg(scip, "variable <%s> appears twice in constraint, fixing it to 0.\n", SCIPvarGetName(vars[j]));
1660  SCIP_CALL( SCIPfixVar(scip, vars[j], 0.0, &infeasible, &fixed) );
1661 
1662  if ( infeasible )
1663  {
1664  *cutoff = TRUE;
1665  return SCIP_OKAY;
1666  }
1667  if ( fixed )
1668  ++(*nfixedvars);
1669  }
1670  }
1671 
1672  /* get bounds */
1673  lb = SCIPvarGetLbLocal(vars[j]);
1674  ub = SCIPvarGetUbLocal(vars[j]);
1675 
1676  /* if the variable if fixed to nonzero */
1677  if ( SCIPisFeasPositive(scip, lb) || SCIPisFeasNegative(scip, ub) )
1678  {
1679  ++nfixednonzeros;
1680  lastFixedNonzero = j;
1681  }
1682 
1683  /* if the variable is fixed to 0 */
1684  if ( SCIPisFeasZero(scip, lb) && SCIPisFeasZero(scip, ub) )
1685  {
1686  SCIPdebugMsg(scip, "deleting variable <%s> fixed to 0.\n", SCIPvarGetName(vars[j]));
1687  SCIP_CALL( deleteVarSOS1(scip, cons, consdata, eventhdlr, j) );
1688  ++(*nremovedvars);
1689  }
1690  else
1691  {
1692  /* check whether all variables are binary */
1693  if ( ! SCIPvarIsBinary(vars[j]) )
1694  allvarsbinary = FALSE;
1695 
1696  ++j;
1697  }
1698  }
1699 
1700  /* if the number of variables is less than 2 */
1701  if ( consdata->nvars < 2 )
1702  {
1703  SCIPdebugMsg(scip, "Deleting SOS1 constraint <%s> with < 2 variables.\n", SCIPconsGetName(cons));
1704 
1705  /* delete constraint */
1706  assert( ! SCIPconsIsModifiable(cons) );
1707  SCIP_CALL( SCIPdelCons(scip, cons) );
1708  ++(*ndelconss);
1709  *success = TRUE;
1710  return SCIP_OKAY;
1711  }
1712 
1713  /* if more than one variable are fixed to be nonzero, we are infeasible */
1714  if ( nfixednonzeros > 1 )
1715  {
1716  SCIPdebugMsg(scip, "The problem is infeasible: more than one variable has bounds that keep it from being 0.\n");
1717  assert( lastFixedNonzero >= 0 );
1718  *cutoff = TRUE;
1719  return SCIP_OKAY;
1720  }
1721 
1722  /* if there is exactly one fixed nonzero variable */
1723  if ( nfixednonzeros == 1 )
1724  {
1725  assert( lastFixedNonzero >= 0 );
1726 
1727  /* fix all other variables to zero */
1728  for (j = 0; j < consdata->nvars; ++j)
1729  {
1730  if ( j != lastFixedNonzero )
1731  {
1732  SCIP_CALL( fixVariableZero(scip, vars[j], &infeasible, &fixed) );
1733  if ( infeasible )
1734  {
1735  *cutoff = TRUE;
1736  return SCIP_OKAY;
1737  }
1738  if ( fixed )
1739  ++(*nfixedvars);
1740  }
1741  }
1742 
1743  SCIPdebugMsg(scip, "Deleting redundant SOS1 constraint <%s> with one variable.\n", SCIPconsGetName(cons));
1744 
1745  /* delete original constraint */
1746  assert( ! SCIPconsIsModifiable(cons) );
1747  SCIP_CALL( SCIPdelCons(scip, cons) );
1748  ++(*ndelconss);
1749  *success = TRUE;
1750  }
1751  /* note: there is no need to update consdata->nfixednonzeros, since the constraint is deleted as soon nfixednonzeros > 0. */
1752  else
1753  {
1754  /* if all variables are binary create a set packing constraint */
1755  if ( allvarsbinary && SCIPfindConshdlr(scip, "setppc") != NULL )
1756  {
1757  SCIP_CONS* setpackcons;
1758 
1759  /* create, add, and release the logicor constraint */
1760  SCIP_CALL( SCIPcreateConsSetpack(scip, &setpackcons, SCIPconsGetName(cons), consdata->nvars, consdata->vars,
1764  SCIP_CALL( SCIPaddCons(scip, setpackcons) );
1765  SCIP_CALL( SCIPreleaseCons(scip, &setpackcons) );
1766 
1767  SCIPdebugMsg(scip, "Upgrading SOS1 constraint <%s> to set packing constraint.\n", SCIPconsGetName(cons));
1768 
1769  /* remove the SOS1 constraint globally */
1770  assert( ! SCIPconsIsModifiable(cons) );
1771  SCIP_CALL( SCIPdelCons(scip, cons) );
1772  ++(*nupgdconss);
1773  *success = TRUE;
1774  }
1775  }
1776 
1777  return SCIP_OKAY;
1778 }
1779 
1780 
1781 
1782 /** perform one presolving round for all SOS1 constraints
1783  *
1784  * We perform the following presolving steps.
1785  *
1786  * - If the bounds of some variable force it to be nonzero, we can
1787  * fix all other variables to zero and remove the SOS1 constraints
1788  * that contain it.
1789  * - If a variable is fixed to zero, we can remove the variable.
1790  * - If a variable appears twice, it can be fixed to 0.
1791  * - We substitute appregated variables.
1792  * - Remove redundant SOS1 constraints
1793  *
1794  * If the adjacency matrix of the conflict graph is present, then
1795  * we perform the following additional presolving steps
1796  *
1797  * - Search for larger SOS1 constraints in the conflict graph
1798  */
1799 static
1801  SCIP* scip, /**< SCIP pointer */
1802  SCIP_EVENTHDLR* eventhdlr, /**< event handler */
1803  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
1804  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
1805  SCIP_Bool** adjacencymatrix, /**< adjacency matrix of conflict graph (or NULL) */
1806  SCIP_CONS** conss, /**< SOS1 constraints */
1807  int nconss, /**< number of SOS1 constraints */
1808  int nsos1vars, /**< number of SOS1 variables */
1809  int* naddconss, /**< number of added constraints */
1810  int* ndelconss, /**< number of deleted constraints */
1811  int* nupgdconss, /**< number of upgraded constraints */
1812  int* nfixedvars, /**< number of fixed variables */
1813  int* nremovedvars, /**< number of variables removed */
1814  SCIP_RESULT* result /**< result */
1815  )
1816 {
1817  SCIP_DIGRAPH* vertexcliquegraph;
1818  SCIP_VAR** consvars;
1819  SCIP_Real* consweights;
1820  int** cliques = NULL;
1821  int ncliques = 0;
1822  int* cliquesizes = NULL;
1823  int* newclique = NULL;
1824  int* indconss = NULL;
1825  int* lengthconss = NULL;
1826  int* comsucc = NULL;
1827  int csize;
1828  int iter;
1829  int c;
1830 
1831  assert( scip != NULL );
1832  assert( eventhdlr != NULL );
1833  assert( conshdlrdata != NULL );
1834  assert( conflictgraph != NULL );
1835  assert( conss != NULL );
1836  assert( naddconss != NULL );
1837  assert( ndelconss != NULL );
1838  assert( nupgdconss != NULL );
1839  assert( nfixedvars != NULL );
1840  assert( nremovedvars != NULL );
1841  assert( result != NULL );
1842 
1843  /* create digraph whose nodes represent variables and cliques in the conflict graph */
1844  csize = MAX(1, conshdlrdata->maxextensions) * nconss;
1845  SCIP_CALL( SCIPcreateDigraph(scip, &vertexcliquegraph, nsos1vars + csize) );
1846 
1847  /* allocate buffer arrays */
1848  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nsos1vars) );
1849  SCIP_CALL( SCIPallocBufferArray(scip, &consweights, nsos1vars) );
1850  SCIP_CALL( SCIPallocBufferArray(scip, &cliquesizes, csize) );
1851  SCIP_CALL( SCIPallocBufferArray(scip, &newclique, nsos1vars) );
1852  SCIP_CALL( SCIPallocBufferArray(scip, &indconss, csize) );
1853  SCIP_CALL( SCIPallocBufferArray(scip, &lengthconss, csize) );
1854  SCIP_CALL( SCIPallocBufferArray(scip, &comsucc, MAX(nsos1vars, csize)) );
1855  SCIP_CALL( SCIPallocBufferArray(scip, &cliques, csize) );
1856 
1857  /* get constraint indices and sort them in descending order of their lengths */
1858  for (c = 0; c < nconss; ++c)
1859  {
1860  SCIP_CONSDATA* consdata;
1861 
1862  consdata = SCIPconsGetData(conss[c]);
1863  assert( consdata != NULL );
1864 
1865  indconss[c] = c;
1866  lengthconss[c] = consdata->nvars;
1867  }
1868  SCIPsortDownIntInt(lengthconss, indconss, nconss);
1869 
1870  /* check each constraint */
1871  for (iter = 0; iter < nconss; ++iter)
1872  {
1873  SCIP_CONSDATA* consdata;
1874  SCIP_CONS* cons;
1875  SCIP_Bool substituted;
1876  SCIP_Bool success;
1877  SCIP_Bool cutoff;
1878  int savennupgdconss;
1879  int savendelconss;
1880 
1881  SCIP_VAR** vars;
1882  int nvars;
1883 
1884  c = indconss[iter];
1885 
1886  assert( conss != NULL );
1887  assert( conss[c] != NULL );
1888  cons = conss[c];
1889  consdata = SCIPconsGetData(cons);
1890 
1891  assert( consdata != NULL );
1892  assert( consdata->nvars >= 0 );
1893  assert( consdata->nvars <= consdata->maxvars );
1894  assert( ! SCIPconsIsModifiable(cons) );
1895  assert( ncliques < csize );
1896 
1897  savendelconss = *ndelconss;
1898  savennupgdconss = *nupgdconss;
1899 
1900  /* perform one presolving round for SOS1 constraint */
1901  SCIP_CALL( presolRoundConsSOS1(scip, cons, consdata, eventhdlr, &substituted, &cutoff, &success, ndelconss, nupgdconss, nfixedvars, nremovedvars) );
1902 
1903  if ( cutoff )
1904  {
1905  *result = SCIP_CUTOFF;
1906  break;
1907  }
1908 
1909  if ( *ndelconss > savendelconss || *nupgdconss > savennupgdconss || substituted )
1910  {
1911  *result = SCIP_SUCCESS;
1912  continue;
1913  }
1914 
1915  if ( success )
1916  *result = SCIP_SUCCESS;
1917 
1918  /* get number of variables of constraint */
1919  nvars = consdata->nvars;
1920 
1921  /* get variables of constraint */
1922  vars = consdata->vars;
1923 
1924  if ( nvars > 1 && conshdlrdata->maxextensions != 0 )
1925  {
1926  SCIP_Bool extended = FALSE;
1927  int cliquesize = 0;
1928  int ncomsucc = 0;
1929  int varprobind;
1930  int j;
1931 
1932  /* get clique and size of clique */
1933  for (j = 0; j < nvars; ++j)
1934  {
1935  varprobind = varGetNodeSOS1(conshdlrdata, vars[j]);
1936 
1937  if ( varprobind >= 0 )
1938  newclique[cliquesize++] = varprobind;
1939  }
1940 
1941  if ( cliquesize > 1 )
1942  {
1943  cliquesizes[ncliques] = cliquesize;
1944 
1945  /* sort clique vertices */
1946  SCIPsortInt(newclique, cliquesizes[ncliques]);
1947 
1948  /* check if clique is contained in an already known clique */
1949  if ( ncliques > 0 )
1950  {
1951  int* succ;
1952  int nsucc;
1953  int v;
1954 
1955  varprobind = newclique[0];
1956  ncomsucc = SCIPdigraphGetNSuccessors(vertexcliquegraph, varprobind);
1957  succ = SCIPdigraphGetSuccessors(vertexcliquegraph, varprobind);
1958 
1959  /* get all (already processed) cliques that contain 'varpropind' */
1960  for (j = 0; j < ncomsucc; ++j)
1961  {
1962  /* successors should have been sorted in a former step of the algorithm */
1963  assert( j == 0 || succ[j] > succ[j-1] );
1964  comsucc[j] = succ[j];
1965  }
1966 
1967  /* loop through remaining nodes of clique (case v = 0 already processed) */
1968  for (v = 1; v < cliquesize && ncomsucc > 0; ++v)
1969  {
1970  varprobind = newclique[v];
1971 
1972  /* get all (already processed) cliques that contain 'varpropind' */
1973  nsucc = SCIPdigraphGetNSuccessors(vertexcliquegraph, varprobind);
1974  succ = SCIPdigraphGetSuccessors(vertexcliquegraph, varprobind);
1975  assert( succ != NULL || nsucc == 0 );
1976 
1977  if ( nsucc < 1 )
1978  {
1979  ncomsucc = 0;
1980  break;
1981  }
1982 
1983  /* get intersection with comsucc */
1984  SCIP_CALL( SCIPcomputeArraysIntersection(comsucc, ncomsucc, succ, nsucc, comsucc, &ncomsucc) );
1985  }
1986  }
1987 
1988  /* if constraint is redundand then delete it */
1989  if ( ncomsucc > 0 )
1990  {
1991  assert( ! SCIPconsIsModifiable(cons) );
1992  SCIP_CALL( SCIPdelCons(scip, cons) );
1993  ++(*ndelconss);
1994  *result = SCIP_SUCCESS;
1995  continue;
1996  }
1997 
1998  if ( conshdlrdata->maxextensions != 0 && adjacencymatrix != NULL )
1999  {
2000  int maxextensions;
2001  ncomsucc = 0;
2002 
2003  /* determine the common successors of the vertices from the considered clique */
2004  SCIP_CALL( cliqueGetCommonSuccessorsSOS1(conshdlrdata, conflictgraph, newclique, vars, nvars, comsucc, &ncomsucc) );
2005 
2006  /* find extensions for the clique */
2007  maxextensions = conshdlrdata->maxextensions;
2008  extended = FALSE;
2009  SCIP_CALL( extensionOperatorSOS1(scip, conshdlrdata, adjacencymatrix, vertexcliquegraph, nsos1vars, nconss, cons, consvars, consweights,
2010  TRUE, (maxextensions <= 1) ? FALSE : TRUE, cliques, &ncliques, cliquesizes, newclique, comsucc, ncomsucc, 0, -1, &maxextensions,
2011  naddconss, &extended) );
2012  }
2013 
2014  /* if an extension was found for the current clique then free the old SOS1 constraint */
2015  if ( extended )
2016  {
2017  assert( ! SCIPconsIsModifiable(cons) );
2018  SCIP_CALL( SCIPdelCons(scip, cons) );
2019  ++(*ndelconss);
2020  *result = SCIP_SUCCESS;
2021  }
2022  else /* if we keep the constraint */
2023  {
2024  int cliqueind;
2025 
2026  cliqueind = nsos1vars + ncliques; /* index of clique in vertex-clique graph */
2027 
2028  /* add directed edges to the vertex-clique graph */
2029  assert( cliquesize >= 0 && cliquesize <= nsos1vars );
2030  assert( ncliques < csize );
2031  SCIP_CALL( SCIPallocBufferArray(scip, &cliques[ncliques], cliquesize) );/*lint !e866*/
2032  for (j = 0; j < cliquesize; ++j)
2033  {
2034  cliques[ncliques][j] = newclique[j];
2035  SCIP_CALL( SCIPdigraphAddArcSafe(vertexcliquegraph, cliques[ncliques][j], cliqueind, NULL) );
2036  }
2037 
2038  /* update number of maximal cliques */
2039  ++ncliques;
2040  }
2041  }
2042  }
2043  }
2044 
2045  /* free buffer arrays */
2046  for (c = ncliques-1; c >= 0; --c)
2047  SCIPfreeBufferArrayNull(scip, &cliques[c]);
2048  SCIPfreeBufferArrayNull(scip, &cliques);
2049  SCIPfreeBufferArrayNull(scip, &comsucc);
2050  SCIPfreeBufferArrayNull(scip, &lengthconss);
2051  SCIPfreeBufferArrayNull(scip, &indconss);
2052  SCIPfreeBufferArrayNull(scip, &newclique);
2053  SCIPfreeBufferArrayNull(scip, &cliquesizes);
2054  SCIPfreeBufferArrayNull(scip, &consweights);
2055  SCIPfreeBufferArrayNull(scip, &consvars);
2056  SCIPdigraphFree(&vertexcliquegraph);
2057 
2058  return SCIP_OKAY;
2059 }
2060 
2061 
2062 /** performs implication graph analysis
2063  *
2064  * Tentatively fixes a variable to nonzeero and extracts consequences from it:
2065  * - adds (possibly new) complementarity constraints to the problem if variables are implied to be zero
2066  * - returns that the subproblem is infeasible if the domain of a variable turns out to be empty
2067  */
2068 static
2070  SCIP* scip, /**< SCIP pointer */
2071  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
2072  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
2073  SCIP_VAR** totalvars, /**< problem and SOS1 variables */
2074  SCIP_DIGRAPH* implgraph, /**< implication graph (@p j is successor of @p i if and only if \f$ x_i\not = 0 \Rightarrow x_j\not = 0\f$) */
2075  SCIP_HASHMAP* implhash, /**< hash map from variable to node in implication graph */
2076  SCIP_Bool** adjacencymatrix, /**< adjacencymatrix of the conflict graph (only lower half filled) */
2077  int givennode, /**< node of the conflict graph */
2078  int nonznode, /**< node of the conflict graph that is implied to be nonzero if given node is nonzero */
2079  SCIP_Real* impllbs, /**< current lower variable bounds if given node is nonzero (update possible) */
2080  SCIP_Real* implubs, /**< current upper variable bounds if given node is nonzero (update possible) */
2081  SCIP_Bool* implnodes, /**< indicates which variables are currently implied to be nonzero if given node is nonzero (update possible) */
2082  int* naddconss, /**< pointer to store number of added SOS1 constraints */
2083  int* probingdepth, /**< pointer to store current probing depth */
2084  SCIP_Bool* infeasible /**< pointer to store whether the subproblem gets infeasible if variable to 'nonznode' is nonzero */
2085  )
2086 {
2087  SCIP_SUCCDATA** succdatas;
2088  int succnode;
2089  int* succ;
2090  int nsucc;
2091  int s;
2092 
2093  assert( nonznode >= 0 && nonznode < SCIPdigraphGetNNodes(conflictgraph) );
2094 
2095  /* check probing depth */
2096  if ( conshdlrdata->depthimplanalysis >= 0 && *probingdepth >= conshdlrdata->depthimplanalysis )
2097  return SCIP_OKAY;
2098  ++(*probingdepth);
2099 
2100  /* get successors of 'nonznode' in the conflict graph */
2101  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, nonznode);
2102  succ = SCIPdigraphGetSuccessors(conflictgraph, nonznode);
2103 
2104  /* loop through neighbors of 'nonznode' in the conflict graph; these variables are implied to be zero */
2105  for (s = 0; s < nsucc; ++s)
2106  {
2107  succnode = succ[s];
2108 
2109  /* if the current variable domain of the successor node does not contain the value zero then return that the problem is infeasible
2110  * else if 'succnode' is not already complementary to 'givennode' then add a new complementarity constraint */
2111  if ( givennode == succnode || SCIPisFeasPositive(scip, impllbs[succnode]) || SCIPisFeasNegative(scip, implubs[succnode]) )
2112  {
2113  *infeasible = TRUE;
2114  return SCIP_OKAY;
2115  }
2116  else if ( ! isConnectedSOS1(adjacencymatrix, NULL, givennode, succnode) )
2117  {
2118  char namesos[SCIP_MAXSTRLEN];
2119  SCIP_CONS* soscons = NULL;
2120  SCIP_VAR* var1;
2121  SCIP_VAR* var2;
2122 
2123  /* update implied bounds of succnode */
2124  impllbs[succnode] = 0;
2125  implubs[succnode] = 0;
2126 
2127  /* add arcs to the conflict graph */
2128  SCIP_CALL( SCIPdigraphAddArcSafe(conflictgraph, givennode, succnode, NULL) );
2129  SCIP_CALL( SCIPdigraphAddArcSafe(conflictgraph, succnode, givennode, NULL) );
2130 
2131  /* resort successors */
2132  SCIPsortInt(SCIPdigraphGetSuccessors(conflictgraph, givennode), SCIPdigraphGetNSuccessors(conflictgraph, givennode));
2133  SCIPsortInt(SCIPdigraphGetSuccessors(conflictgraph, succnode), SCIPdigraphGetNSuccessors(conflictgraph, succnode));
2134 
2135  /* update adjacencymatrix */
2136  if ( givennode > succnode )
2137  adjacencymatrix[givennode][succnode] = 1;
2138  else
2139  adjacencymatrix[succnode][givennode] = 1;
2140 
2141  var1 = SCIPnodeGetVarSOS1(conflictgraph, givennode);
2142  var2 = SCIPnodeGetVarSOS1(conflictgraph, succnode);
2143 
2144  /* create SOS1 constraint */
2145  assert( SCIPgetDepth(scip) == 0 );
2146  (void) SCIPsnprintf(namesos, SCIP_MAXSTRLEN, "presolved_sos1_%s_%s", SCIPvarGetName(var1), SCIPvarGetName(var2) );
2147  SCIP_CALL( SCIPcreateConsSOS1(scip, &soscons, namesos, 0, NULL, NULL, TRUE, TRUE, TRUE, FALSE, TRUE,
2148  FALSE, FALSE, FALSE, FALSE) );
2149 
2150  /* add variables to SOS1 constraint */
2151  SCIP_CALL( addVarSOS1(scip, soscons, conshdlrdata, var1, 1.0) );
2152  SCIP_CALL( addVarSOS1(scip, soscons, conshdlrdata, var2, 2.0) );
2153 
2154  /* add constraint */
2155  SCIP_CALL( SCIPaddCons(scip, soscons) );
2156 
2157  /* release constraint */
2158  SCIP_CALL( SCIPreleaseCons(scip, &soscons) );
2159 
2160  ++(*naddconss);
2161  }
2162  }
2163 
2164  /* by construction: nodes of SOS1 variables are equal for conflict graph and implication graph */
2165  assert( nonznode == (int) (size_t) SCIPhashmapGetImage(implhash, SCIPnodeGetVarSOS1(conflictgraph, nonznode)) );
2166  succdatas = (SCIP_SUCCDATA**) SCIPdigraphGetSuccessorsData(implgraph, nonznode);
2167  nsucc = SCIPdigraphGetNSuccessors(implgraph, nonznode);
2168  succ = SCIPdigraphGetSuccessors(implgraph, nonznode);
2169 
2170  /* go further in implication graph */
2171  for (s = 0; s < nsucc; ++s)
2172  {
2173  SCIP_SUCCDATA* data;
2174  int oldprobingdepth;
2175 
2176  succnode = succ[s];
2177  data = succdatas[s];
2178  oldprobingdepth = *probingdepth;
2179 
2180  /* if current lower bound is smaller than implied lower bound */
2181  if ( SCIPisFeasLT(scip, impllbs[succnode], data->lbimpl) )
2182  {
2183  impllbs[succnode] = data->lbimpl;
2184 
2185  /* if node is SOS1 and implied to be nonzero for the first time, then this recursively may imply further bound changes */
2186  if ( varGetNodeSOS1(conshdlrdata, totalvars[succnode]) >= 0 && ! implnodes[succnode] && SCIPisFeasPositive(scip, data->lbimpl) )
2187  {
2188  /* by construction: nodes of SOS1 variables are equal for conflict graph and implication graph */
2189  assert( succnode == (int) (size_t) SCIPhashmapGetImage(implhash, SCIPnodeGetVarSOS1(conflictgraph, succnode)) );
2190  implnodes[succnode] = TRUE; /* in order to avoid cycling */
2191  SCIP_CALL( performImplicationGraphAnalysis(scip, conshdlrdata, conflictgraph, totalvars, implgraph, implhash, adjacencymatrix, givennode, succnode, impllbs, implubs, implnodes, naddconss, probingdepth, infeasible) );
2192  *probingdepth = oldprobingdepth;
2193 
2194  /* return if the subproblem is known to be infeasible */
2195  if ( *infeasible )
2196  return SCIP_OKAY;
2197  }
2198  }
2199 
2200  /* if current upper bound is larger than implied upper bound */
2201  if ( SCIPisFeasGT(scip, implubs[succnode], data->ubimpl) )
2202  {
2203  implubs[succnode] = data->ubimpl;
2204 
2205  /* if node is SOS1 and implied to be nonzero for the first time, then this recursively may imply further bound changes */
2206  if ( varGetNodeSOS1(conshdlrdata, totalvars[succnode]) >= 0 && ! implnodes[succnode] && SCIPisFeasNegative(scip, data->ubimpl) )
2207  {
2208  /* by construction: nodes of SOS1 variables are equal for conflict graph and implication graph */
2209  assert( succnode == (int) (size_t) SCIPhashmapGetImage(implhash, SCIPnodeGetVarSOS1(conflictgraph, succnode)) );
2210  implnodes[succnode] = TRUE; /* in order to avoid cycling */
2211  SCIP_CALL( performImplicationGraphAnalysis(scip, conshdlrdata, conflictgraph, totalvars, implgraph, implhash, adjacencymatrix, givennode, succnode, impllbs, implubs, implnodes, naddconss, probingdepth, infeasible) );
2212  *probingdepth = oldprobingdepth;
2213 
2214  /* return if the subproblem is known to be infeasible */
2215  if ( *infeasible )
2216  return SCIP_OKAY;
2217  }
2218  }
2219  }
2220 
2221  return SCIP_OKAY;
2222 }
2223 
2224 
2225 /** returns whether node is implied to be zero; this information is taken from the input array 'implnodes' */
2226 static
2228  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
2229  SCIP_Bool* implnodes, /**< implnodes[i] = TRUE if the SOS1 variable corresponding to node i in the implication graph is implied to be nonzero */
2230  int node /**< node of the conflict graph (or -1) */
2231  )
2232 {
2233  int* succ;
2234  int nsucc;
2235  int s;
2236 
2237  if ( node < 0 )
2238  return FALSE;
2239 
2240  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, node);
2241  succ = SCIPdigraphGetSuccessors(conflictgraph, node);
2242 
2243  /* check whether any successor is implied to be nonzero */
2244  for (s = 0; s < nsucc; ++s)
2245  {
2246  if ( implnodes[succ[s]] )
2247  return TRUE;
2248  }
2249 
2250  return FALSE;
2251 }
2252 
2253 
2254 /** updates arc data of implication graph */
2255 static
2257  SCIP* scip, /**< SCIP pointer */
2258  SCIP_DIGRAPH* implgraph, /**< implication graph */
2259  SCIP_HASHMAP* implhash, /**< hash map from variable to node in implication graph */
2260  SCIP_VAR** totalvars, /**< problem and SOS1 variables */
2261  SCIP_VAR* varv, /**< variable that is assumed to be nonzero */
2262  SCIP_VAR* varw, /**< implication variable */
2263  SCIP_Real lb, /**< old lower bound of \f$x_w\f$ */
2264  SCIP_Real ub, /**< old upper bound of \f$x_w\f$ */
2265  SCIP_Real newbound, /**< new bound of \f$x_w\f$ */
2266  SCIP_Bool lower, /**< whether to consider lower bound implication (otherwise upper bound) */
2267  int* nchgbds, /**< pointer to store number of changed bounds */
2268  SCIP_Bool* update, /**< pointer to store whether implication graph has been updated */
2269  SCIP_Bool* infeasible /**< pointer to store whether an infeasibility has been detected */
2270  )
2271 {
2272  SCIP_SUCCDATA** succdatas;
2273  SCIP_SUCCDATA* data = NULL;
2274  int nsucc;
2275  int* succ;
2276  int indv;
2277  int indw;
2278  int s;
2279 
2280  assert( scip != NULL );
2281  assert( implgraph != NULL );
2282  assert( implhash != NULL );
2283  assert( totalvars != NULL );
2284  assert( varv != NULL );
2285  assert( varw != NULL );
2286 
2287  /* if x_v != 0 turns out to be infeasible then fix x_v = 0 */
2288  if ( ( lower && SCIPisFeasLT(scip, ub, newbound) ) || ( ! lower && SCIPisFeasGT(scip, lb, newbound) ) )
2289  {
2290  SCIP_Bool infeasible1;
2291  SCIP_Bool infeasible2;
2292  SCIP_Bool tightened1;
2293  SCIP_Bool tightened2;
2294 
2295  SCIP_CALL( SCIPtightenVarLb(scip, varv, 0.0, FALSE, &infeasible1, &tightened1) );
2296  SCIP_CALL( SCIPtightenVarUb(scip, varv, 0.0, FALSE, &infeasible2, &tightened2) );
2297 
2298  if ( infeasible1 || infeasible2 )
2299  {
2300  SCIPdebugMsg(scip, "detected infeasibility while trying to fix variable <%s> to zero\n", SCIPvarGetName(varv));
2301  *infeasible = TRUE;
2302  }
2303 
2304  if ( tightened1 || tightened2 )
2305  {
2306  SCIPdebugMsg(scip, "fixed variable %s from lb = %f and ub = %f to 0.0 \n", SCIPvarGetName(varv), lb, ub);
2307  ++(*nchgbds);
2308  }
2309  }
2310 
2311  /* get successor information */
2312  indv = (int) (size_t) SCIPhashmapGetImage(implhash, varv); /* get index of x_v in implication graph */
2313  assert( (int) (size_t) SCIPhashmapGetImage(implhash, totalvars[indv]) == indv );
2314  succdatas = (SCIP_SUCCDATA**) SCIPdigraphGetSuccessorsData(implgraph, indv);
2315  nsucc = SCIPdigraphGetNSuccessors(implgraph, indv);
2316  succ = SCIPdigraphGetSuccessors(implgraph, indv);
2317 
2318  /* search for nodew in existing successors. If this is the case then check whether the lower implication bound may be updated ... */
2319  indw = (int) (size_t) SCIPhashmapGetImage(implhash, varw);
2320  assert( (int) (size_t) SCIPhashmapGetImage(implhash, totalvars[indw]) == indw );
2321  for (s = 0; s < nsucc; ++s)
2322  {
2323  if ( succ[s] == indw )
2324  {
2325  data = succdatas[s];
2326  assert( data != NULL );
2327  if ( lower && SCIPisFeasLT(scip, data->lbimpl, newbound) )
2328  {
2329  if ( SCIPvarIsIntegral(varw) )
2330  data->lbimpl = SCIPceil(scip, newbound);
2331  else
2332  data->lbimpl = newbound;
2333 
2334  *update = TRUE;
2335  SCIPdebugMsg(scip, "updated to implication %s != 0 -> %s >= %f\n", SCIPvarGetName(varv), SCIPvarGetName(varw), newbound);
2336  }
2337  else if ( ! lower && SCIPisFeasGT(scip, data->ubimpl, newbound) )
2338  {
2339  if ( SCIPvarIsIntegral(varw) )
2340  data->ubimpl = SCIPfloor(scip, newbound);
2341  else
2342  data->ubimpl = newbound;
2343 
2344  *update = TRUE;
2345  SCIPdebugMsg(scip, "updated to implication %s != 0 -> %s >= %f\n", SCIPvarGetName(varv), SCIPvarGetName(varw), newbound);
2346  }
2347  break;
2348  }
2349  }
2350 
2351  /* ..., otherwise if there does not exist an arc between indv and indw already, then create one and add implication */
2352  if ( s == nsucc )
2353  {
2354  assert( data == NULL );
2355  SCIP_CALL( SCIPallocBlockMemory(scip, &data) );
2356  if ( lower )
2357  {
2358  data->lbimpl = newbound;
2359  data->ubimpl = ub;
2360  SCIPdebugMsg(scip, "add implication %s != 0 -> %s >= %f\n", SCIPvarGetName(varv), SCIPvarGetName(varw), newbound);
2361  }
2362  else
2363  {
2364  data->lbimpl = lb;
2365  data->ubimpl = newbound;
2366  SCIPdebugMsg(scip, "add implication %s != 0 -> %s <= %f\n", SCIPvarGetName(varv), SCIPvarGetName(varw), newbound);
2367  }
2368  SCIP_CALL( SCIPdigraphAddArc(implgraph, indv, indw, (void*)data) );
2369  *update = TRUE;
2370  }
2371 
2372  return SCIP_OKAY;
2373 }
2374 
2375 
2376 /** updates implication graph
2377  *
2378  * Assume the variable from the input is nonzero. If this implies that some other variable is also nonzero, then
2379  * store this information in an implication graph
2380  */
2381 static
2383  SCIP* scip, /**< SCIP pointer */
2384  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
2385  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
2386  SCIP_Bool** adjacencymatrix, /**< adjacency matrix of conflict graph (lower half) */
2387  SCIP_DIGRAPH* implgraph, /**< implication graph (@p j is successor of @p i if and only if \f$ x_i\not = 0 \Rightarrow x_j\not = 0\f$) */
2388  SCIP_HASHMAP* implhash, /**< hash map from variable to node in implication graph */
2389  SCIP_Bool* implnodes, /**< implnodes[i] = TRUE if the SOS1 variable corresponding to node i in the implication graph is implied to be nonzero */
2390  SCIP_VAR** totalvars, /**< problem and SOS1 variables */
2391  int** cliquecovers, /**< clique covers of linear constraint */
2392  int* cliquecoversizes, /**< size of clique covers */
2393  int* varincover, /**< array with varincover[i] = cover of SOS1 index @p i */
2394  SCIP_VAR** vars, /**< variables to be checked */
2395  SCIP_Real* coefs, /**< coefficients of variables in linear constraint */
2396  int nvars, /**< number of variables to be checked */
2397  SCIP_Real* bounds, /**< bounds of variables */
2398  SCIP_VAR* var, /**< variable that is assumed to be nonzero */
2399  SCIP_Real bound, /**< bound of variable */
2400  SCIP_Real boundnonzero, /**< bound of variable if it is known to be nonzero if infinity values are not summarized */
2401  int ninftynonzero, /**< number of times infinity/-infinity has to be summarized to boundnonzero */
2402  SCIP_Bool lower, /**< TRUE if lower bounds are consideres; FALSE for upper bounds */
2403  int* nchgbds, /**< pointer to store number of changed bounds */
2404  SCIP_Bool* update, /**< pointer to store whether implication graph has been updated */
2405  SCIP_Bool* infeasible /**< pointer to store whether an infeasibility has been detected */
2406  )
2407 {
2408  int nodev;
2409  int w;
2410 
2411  assert( update != NULL );
2412 
2413  /* update implication graph if possible */
2414  *update = FALSE;
2415  *infeasible = FALSE;
2416  nodev = varGetNodeSOS1(conshdlrdata, var); /* possibly -1 if var is not involved in an SOS1 constraint */
2417 
2418  /* if nodev is an index of an SOS1 variable and at least one lower bound of a variable that is not x_v is infinity */
2419  if ( nodev < 0 || SCIPisInfinity(scip, REALABS(bound)) || ninftynonzero > 1 )
2420  return SCIP_OKAY;
2421 
2422  /* for every variable x_w: compute upper bound of a_w * x_w if x_v is known to be nonzero */
2423  for (w = 0; w < nvars; ++w)
2424  {
2425  int newninftynonzero;
2426  SCIP_Bool implinfty = FALSE;
2427  int nodew;
2428 
2429  /* get node of x_w in conflict graph: nodew = -1 if it is no SOS1 variable */
2430  nodew = varGetNodeSOS1(conshdlrdata, vars[w]);
2431 
2432  newninftynonzero = ninftynonzero;
2433 
2434  /* variable should not be fixed to be already zero (note x_v is fixed to be nonzero by assumption) */
2435  if ( nodew < 0 || ( nodev != nodew && ! isConnectedSOS1(adjacencymatrix, NULL, nodev, nodew) && ! isImpliedZero(conflictgraph, implnodes, nodew) ) )
2436  {
2437  SCIP_Real implbound;
2438  SCIP_Bool implcoverw;
2439  int nodecliq;
2440  int indcliq;
2441  int ind;
2442  int j;
2443 
2444  /* boundnonzero is the bound of x_v if x_v is nonzero we use this information to get a bound of x_w if x_v is
2445  * nonzero; therefore, we have to perform some recomputations */
2446  implbound = boundnonzero - bound;
2447  ind = varincover[w];
2448  assert( cliquecoversizes[ind] > 0 );
2449 
2450  implcoverw = FALSE;
2451  for (j = 0; j < cliquecoversizes[ind]; ++j)
2452  {
2453  indcliq = cliquecovers[ind][j];
2454  assert( 0 <= indcliq && indcliq < nvars );
2455 
2456  nodecliq = varGetNodeSOS1(conshdlrdata, vars[indcliq]); /* possibly -1 if variable is not involved in an SOS1 constraint */
2457 
2458  /* if nodecliq is not a member of an SOS1 constraint or the variable corresponding to nodecliq is not implied to be zero if x_v != 0 */
2459  if ( nodecliq < 0 || (! isConnectedSOS1(adjacencymatrix, NULL, nodev, nodecliq) && ! isImpliedZero(conflictgraph, implnodes, nodecliq) ) )
2460  {
2461  if ( indcliq == w )
2462  {
2463  if ( !SCIPisInfinity(scip, REALABS(bounds[w])) && !SCIPisInfinity(scip, REALABS(implbound + bounds[w])) )
2464  implbound += bounds[w];
2465  else
2466  --newninftynonzero;
2467  implcoverw = TRUE;
2468  }
2469  else if ( implcoverw )
2470  {
2471  if ( SCIPisInfinity(scip, REALABS(bounds[indcliq])) || SCIPisInfinity(scip, REALABS(implbound - bounds[indcliq])) )
2472  implinfty = TRUE;
2473  else
2474  implbound -= bounds[indcliq];
2475  break;
2476  }
2477  else
2478  {
2479  if ( SCIPisInfinity(scip, REALABS(bounds[indcliq])) )
2480  implinfty = TRUE;
2481  break;
2482  }
2483  }
2484  }
2485 
2486  /* check whether x_v != 0 implies a bound change of x_w */
2487  if ( ! implinfty && newninftynonzero == 0 )
2488  {
2489  SCIP_Real newbound;
2490  SCIP_Real coef;
2491  SCIP_Real lb;
2492  SCIP_Real ub;
2493 
2494  lb = SCIPvarGetLbLocal(vars[w]);
2495  ub = SCIPvarGetUbLocal(vars[w]);
2496  coef = coefs[w];
2497 
2498  if ( SCIPisFeasZero(scip, coef) )
2499  continue;
2500 
2501  newbound = implbound / coef;
2502 
2503  if ( SCIPisInfinity(scip, newbound) )
2504  continue;
2505 
2506  /* check if an implication can be added/updated or assumption x_v != 0 is infeasible */
2507  if ( lower )
2508  {
2509  if ( SCIPisFeasPositive(scip, coef) && SCIPisFeasLT(scip, lb, newbound) )
2510  {
2511  SCIP_CALL( updateArcData(scip, implgraph, implhash, totalvars, var, vars[w], lb, ub, newbound, TRUE, nchgbds, update, infeasible) );
2512  }
2513  else if ( SCIPisFeasNegative(scip, coef) && SCIPisFeasGT(scip, ub, newbound) )
2514  {
2515  SCIP_CALL( updateArcData(scip, implgraph, implhash, totalvars, var, vars[w], lb, ub, newbound, FALSE, nchgbds, update, infeasible) );
2516  }
2517  }
2518  else
2519  {
2520  if ( SCIPisFeasPositive(scip, coef) && SCIPisFeasGT(scip, ub, newbound) )
2521  {
2522  SCIP_CALL( updateArcData(scip, implgraph, implhash, totalvars, var, vars[w], lb, ub, newbound, FALSE, nchgbds, update, infeasible) );
2523  }
2524  else if ( SCIPisFeasNegative(scip, coef) && SCIPisFeasLT(scip, lb, newbound) )
2525  {
2526  SCIP_CALL( updateArcData(scip, implgraph, implhash, totalvars, var, vars[w], lb, ub, newbound, TRUE, nchgbds, update, infeasible) );
2527  }
2528  }
2529  }
2530  }
2531  }
2532 
2533  return SCIP_OKAY;
2534 }
2535 
2536 
2537 /** search new disjoint clique that covers given node
2538  *
2539  * For a given vertex @p v search for a clique of the conflict graph induced by the variables of a linear constraint that
2540  * - covers @p v and
2541  * - has an an empty intersection with already computed clique cover.
2542  */
2543 static
2545  SCIP* scip, /**< SCIP pointer */
2546  SCIP_DIGRAPH* conflictgraphroot, /**< conflict graph of the root node (nodes: 1, ..., @p nsos1vars) */
2547  SCIP_DIGRAPH* conflictgraphlin, /**< conflict graph of linear constraint (nodes: 1, ..., @p nlinvars) */
2548  SCIP_VAR** linvars, /**< variables in linear constraint */
2549  SCIP_Bool* coveredvars, /**< states which variables of the linear constraint are currently covered by a clique */
2550  int* clique, /**< array to store new clique in cover */
2551  int* cliquesize, /**< pointer to store the size of @p clique */
2552  int v, /**< position of variable in linear constraint that should be covered */
2553  SCIP_Bool considersolvals /**< TRUE if largest auxiliary bigM values of variables should be prefered */
2554  )
2555 {
2556  int nsucc;
2557  int s;
2558 
2559  assert( conflictgraphlin != NULL );
2560  assert( linvars != NULL );
2561  assert( coveredvars != NULL );
2562  assert( clique != NULL );
2563  assert( cliquesize != NULL );
2564 
2565  assert( ! coveredvars[v] ); /* we should produce a new clique */
2566 
2567  /* add index 'v' to the clique cover */
2568  clique[0] = v;
2569  *cliquesize = 1;
2570 
2571  nsucc = SCIPdigraphGetNSuccessors(conflictgraphlin, v);
2572  if ( nsucc > 0 )
2573  {
2574  int* extensions;
2575  int nextensions = 0;
2576  int nextensionsnew;
2577  int succnode;
2578  int* succ;
2579 
2580  /* allocate buffer array */
2581  SCIP_CALL( SCIPallocBufferArray(scip, &extensions, nsucc) );
2582 
2583  succ = SCIPdigraphGetSuccessors(conflictgraphlin, v);
2584 
2585  /* compute possible extensions for the clique cover */
2586  for (s = 0; s < nsucc; ++s)
2587  {
2588  succnode = succ[s];
2589  if ( ! coveredvars[succnode] )
2590  extensions[nextensions++] = succ[s];
2591  }
2592 
2593  /* while there exist possible extensions for the clique cover */
2594  while ( nextensions > 0 )
2595  {
2596  int bestindex = -1;
2597 
2598  if ( considersolvals )
2599  {
2600  SCIP_Real bestbigMval;
2601  SCIP_Real bigMval;
2602 
2603  bestbigMval = -SCIPinfinity(scip);
2604 
2605  /* search for the extension with the largest absolute value of its LP relaxation solution value */
2606  for (s = 0; s < nextensions; ++s)
2607  {
2608  bigMval = nodeGetSolvalBinaryBigMSOS1(scip, conflictgraphroot, NULL, extensions[s]);
2609  if ( SCIPisFeasLT(scip, bestbigMval, bigMval) )
2610  {
2611  bestbigMval = bigMval;
2612  bestindex = extensions[s];
2613  }
2614  }
2615  }
2616  else
2617  bestindex = extensions[0];
2618 
2619  assert( bestindex != -1 );
2620 
2621  /* add bestindex to the clique cover */
2622  clique[(*cliquesize)++] = bestindex;
2623 
2624  /* compute new 'extensions' array */
2625  nextensionsnew = 0;
2626  for (s = 0; s < nextensions; ++s)
2627  {
2628  if ( s != bestindex && isConnectedSOS1(NULL, conflictgraphlin, bestindex, extensions[s]) )
2629  extensions[nextensionsnew++] = extensions[s];
2630  }
2631  nextensions = nextensionsnew;
2632  }
2633 
2634  /* free buffer array */
2635  SCIPfreeBufferArray(scip, &extensions);
2636  }
2637 
2638  /* mark covered indices */
2639  for (s = 0; s < *cliquesize; ++s)
2640  {
2641  int ind;
2642 
2643  ind = clique[s];
2644  assert( 0 <= ind );
2645  assert( ! coveredvars[ind] );
2646  coveredvars[ind] = TRUE;
2647  }
2648 
2649  return SCIP_OKAY;
2650 }
2651 
2652 
2653 /** try to tighten upper and lower bounds for variables */
2654 static
2656  SCIP* scip, /**< SCIP pointer */
2657  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
2658  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
2659  SCIP_DIGRAPH* implgraph, /**< implication graph (@p j is successor of @p i if and only if \f$ x_i\not = 0 \f$ implies a new lower/upper bound for \f$ x_j\f$) */
2660  SCIP_HASHMAP* implhash, /**< hash map from variable to node in implication graph */
2661  SCIP_Bool** adjacencymatrix, /**< adjacencymatrix of conflict graph */
2662  SCIP_VAR** totalvars, /**< problem and SOS1 vars */
2663  int ntotalvars, /**< number of problem and SOS1 variables*/
2664  int nsos1vars, /**< number of SOS1 variables */
2665  int* nchgbds, /**< pointer to store number of changed bounds */
2666  SCIP_Bool* implupdate, /**< pointer to store whether the implication graph has been updated in this function call */
2667  SCIP_Bool* cutoff /**< pointer to store if current nodes LP is infeasible */
2668  )
2669 {
2670  SCIP_CONSHDLR* conshdlrlinear;
2671  SCIP_CONS** linearconss;
2672  int nlinearconss;
2673 
2674  SCIP_Bool* implnodes = NULL; /* implnodes[i] = TRUE if the SOS1 variable corresponding to node i in the implication graph is implied to be nonzero */
2675  SCIP_Bool* coveredvars = NULL; /* coveredvars[i] = TRUE if variable with index i is covered by the clique cover */
2676  int* varindincons = NULL; /* varindincons[i] = position of SOS1 index i in linear constraint (-1 if x_i is not involved in linear constraint) */
2677 
2678  SCIP_VAR** trafolinvars = NULL; /* variables of transformed linear constraints without (multi)aggregated variables */
2679  int ntrafolinvars = 0;
2680  SCIP_Real* trafolinvals = NULL;
2681  SCIP_Real* trafoubs = NULL;
2682  SCIP_Real* trafolbs = NULL;
2683  SCIP_Real traforhs;
2684  SCIP_Real trafolhs;
2685 
2686  SCIP_VAR** sos1linvars = NULL; /* variables that are not contained in linear constraint, but are in conflict with a variable from the linear constraint */
2687  int nsos1linvars;
2688  int c;
2689 
2690  assert( scip != NULL );
2691  assert( conflictgraph != NULL );
2692  assert( adjacencymatrix != NULL );
2693  assert( nchgbds != NULL );
2694  assert( cutoff != NULL );
2695 
2696  *cutoff = FALSE;
2697  *implupdate = FALSE;
2698 
2699  /* get constraint handler data of linear constraints */
2700  conshdlrlinear = SCIPfindConshdlr(scip, "linear");
2701  if ( conshdlrlinear == NULL )
2702  return SCIP_OKAY;
2703 
2704  /* get linear constraints and number of linear constraints */
2705  nlinearconss = SCIPconshdlrGetNConss(conshdlrlinear);
2706  linearconss = SCIPconshdlrGetConss(conshdlrlinear);
2707 
2708  /* allocate buffer arrays */
2709  SCIP_CALL( SCIPallocBufferArray(scip, &sos1linvars, nsos1vars) );
2710  SCIP_CALL( SCIPallocBufferArray(scip, &implnodes, nsos1vars) );
2711  SCIP_CALL( SCIPallocBufferArray(scip, &varindincons, nsos1vars) );
2712  SCIP_CALL( SCIPallocBufferArray(scip, &coveredvars, ntotalvars) );
2713  SCIP_CALL( SCIPallocBufferArray(scip, &trafoubs, ntotalvars) );
2714  SCIP_CALL( SCIPallocBufferArray(scip, &trafolbs, ntotalvars) );
2715 
2716  /* for every linear constraint and every SOS1 variable */
2717  for (c = 0; c < nlinearconss + nsos1vars && ! (*cutoff); ++c)
2718  {
2719  SCIP_DIGRAPH* conflictgraphlin;
2720  int** cliquecovers = NULL; /* clique covers of indices of variables in linear constraint */
2721  int* cliquecoversizes = NULL; /* size of each cover */
2722  int ncliquecovers;
2723  SCIP_Real* cliquecovervals = NULL;
2724  int* varincover = NULL; /* varincover[i] = cover of SOS1 index i */
2725 
2726  int v;
2727  int i;
2728  int j;
2729 
2730  /* get transformed linear constraints (without aggregated variables) */
2731  if ( c < nlinearconss )
2732  {
2733  SCIP_VAR** origlinvars;
2734  int noriglinvars;
2735  SCIP_Real* origlinvals;
2736  SCIP_Real origrhs;
2737  SCIP_Real origlhs;
2738  SCIP_Real constant;
2739  int requiredsize;
2740 
2741  /* get data of linear constraint */
2742  noriglinvars = SCIPgetNVarsLinear(scip, linearconss[c]);
2743  origlinvars = SCIPgetVarsLinear(scip, linearconss[c]);
2744  origlinvals = SCIPgetValsLinear(scip, linearconss[c]);
2745  origrhs = SCIPgetRhsLinear(scip, linearconss[c]);
2746  origlhs = SCIPgetLhsLinear(scip, linearconss[c]);
2747 
2748  if ( noriglinvars < 1 )
2749  continue;
2750  assert( origlinvars != NULL );
2751  assert( origlinvals != NULL );
2752 
2753  /* copy variables and coefficients of linear constraint */
2754  SCIP_CALL( SCIPduplicateBufferArray(scip, &trafolinvars, origlinvars, noriglinvars) );
2755  SCIP_CALL( SCIPduplicateBufferArray(scip, &trafolinvals, origlinvals, noriglinvars) );
2756  ntrafolinvars = noriglinvars;
2757 
2758  /* transform linear constraint */
2759  constant = 0.0;
2760  SCIP_CALL( SCIPgetProbvarLinearSum(scip, trafolinvars, trafolinvals, &ntrafolinvars, noriglinvars, &constant, &requiredsize, TRUE) );
2761  if( requiredsize > ntrafolinvars )
2762  {
2763  SCIP_CALL( SCIPreallocBufferArray(scip, &trafolinvars, requiredsize) );
2764  SCIP_CALL( SCIPreallocBufferArray(scip, &trafolinvals, requiredsize) );
2765 
2766  SCIP_CALL( SCIPgetProbvarLinearSum(scip, trafolinvars, trafolinvals, &ntrafolinvars, requiredsize, &constant, &requiredsize, TRUE) );
2767  assert( requiredsize <= ntrafolinvars );
2768  }
2769  trafolhs = origlhs - constant;
2770  traforhs = origrhs - constant;
2771  }
2772  else
2773  {
2774  SCIP_VAR* var;
2775 
2776  var = SCIPnodeGetVarSOS1(conflictgraph, c-nlinearconss);
2777 
2779  {
2780  SCIP_Real constant;
2781 
2782  SCIP_CALL( SCIPallocBufferArray(scip, &trafolinvars, 2) );
2783  SCIP_CALL( SCIPallocBufferArray(scip, &trafolinvals, 2) );
2784 
2785  constant = SCIPvarGetAggrConstant(var);
2786  trafolinvars[0] = SCIPvarGetAggrVar(var);
2787  trafolinvals[0] = SCIPvarGetAggrScalar(var);
2788  trafolinvars[1] = var;
2789  trafolinvals[1] = -1.0;
2790  trafolhs = -constant;
2791  traforhs = -constant;
2792  ntrafolinvars = 2;
2793  }
2794  else if ( SCIPvarGetStatus(var) == SCIP_VARSTATUS_MULTAGGR )
2795  {
2796  SCIP_Real* scalars;
2797  SCIP_VAR** agrvars;
2798  SCIP_Real constant;
2799  int nagrvars;
2800 
2801  nagrvars = SCIPvarGetMultaggrNVars(var);
2802 
2803  SCIP_CALL( SCIPallocBufferArray(scip, &trafolinvars, nagrvars+1) );
2804  SCIP_CALL( SCIPallocBufferArray(scip, &trafolinvals, nagrvars+1) );
2805 
2806  agrvars = SCIPvarGetMultaggrVars(var);
2807  scalars = SCIPvarGetMultaggrScalars(var);
2808  constant = SCIPvarGetMultaggrConstant(var);
2809 
2810  for (v = 0; v < nagrvars; ++v)
2811  {
2812  trafolinvars[v] = agrvars[v];
2813  trafolinvals[v] = scalars[v];
2814  }
2815  trafolinvars[nagrvars] = var;
2816  trafolinvals[nagrvars] = -1.0;
2817  trafolhs = -constant;
2818  traforhs = -constant;
2819  ntrafolinvars = nagrvars + 1;
2820  }
2821  else if ( SCIPvarGetStatus(var) == SCIP_VARSTATUS_NEGATED )
2822  {
2823  SCIP_VAR* negvar;
2824  SCIP_Real negcons;
2825 
2826  /* get negation variable and negation offset */
2827  negvar = SCIPvarGetNegationVar(var);
2828  negcons = SCIPvarGetNegationConstant(var);
2829 
2830  SCIP_CALL( SCIPallocBufferArray(scip, &trafolinvars, 2) );
2831  SCIP_CALL( SCIPallocBufferArray(scip, &trafolinvals, 2) );
2832 
2833  trafolinvars[0] = negvar;
2834  trafolinvars[1] = var;
2835  trafolinvals[0] = 1.0;
2836  trafolinvals[1] = 1.0;
2837  trafolhs = negcons;
2838  traforhs = negcons;
2839  ntrafolinvars = 2;
2840  }
2841  else
2842  continue;
2843  }
2844 
2845  if ( ntrafolinvars == 0 )
2846  {
2847  SCIPfreeBufferArray(scip, &trafolinvars);
2848  SCIPfreeBufferArray(scip, &trafolinvals);
2849  continue;
2850  }
2851 
2852  /* compute lower and upper bounds of each term a_i * x_i of transformed constraint */
2853  for (v = 0; v < ntrafolinvars; ++v)
2854  {
2855  SCIP_Real lb = SCIPvarGetLbLocal(trafolinvars[v]);
2856  SCIP_Real ub = SCIPvarGetUbLocal(trafolinvars[v]);
2857 
2858  if ( trafolinvals[v] < 0.0 )
2859  {
2860  SCIP_Real temp;
2861 
2862  temp = lb;
2863  lb = ub;
2864  ub = temp;
2865  }
2866 
2867  assert(!SCIPisInfinity(scip, REALABS(trafolinvals[v])));
2868 
2869  if ( SCIPisInfinity(scip, REALABS(lb)) || SCIPisInfinity(scip, REALABS(lb * trafolinvals[v])) )
2870  trafolbs[v] = -SCIPinfinity(scip);
2871  else
2872  trafolbs[v] = lb * trafolinvals[v];
2873 
2874  if ( SCIPisInfinity(scip, REALABS(ub)) || SCIPisInfinity(scip, REALABS(ub * trafolinvals[v])) )
2875  trafoubs[v] = SCIPinfinity(scip);
2876  else
2877  trafoubs[v] = ub * trafolinvals[v];
2878  }
2879 
2880  /* initialization: mark all the SOS1 variables as 'not a member of the linear constraint' */
2881  for (v = 0; v < nsos1vars; ++v)
2882  varindincons[v] = -1;
2883 
2884  /* save position of SOS1 variables in linear constraint */
2885  for (v = 0; v < ntrafolinvars; ++v)
2886  {
2887  int node;
2888 
2889  node = varGetNodeSOS1(conshdlrdata, trafolinvars[v]);
2890 
2891  if ( node >= 0 )
2892  varindincons[node] = v;
2893  }
2894 
2895  /* create conflict graph of linear constraint */
2896  SCIP_CALL( SCIPcreateDigraph(scip, &conflictgraphlin, ntrafolinvars) );
2897  SCIP_CALL( genConflictgraphLinearCons(conshdlrdata, conflictgraphlin, conflictgraph, trafolinvars, ntrafolinvars, varindincons) );
2898 
2899  /* mark all the variables as 'not covered by some clique cover' */
2900  for (i = 0; i < ntrafolinvars; ++i)
2901  coveredvars[i] = FALSE;
2902 
2903  /* allocate buffer array */
2904  SCIP_CALL( SCIPallocBufferArray(scip, &cliquecovervals, ntrafolinvars) );
2905  SCIP_CALL( SCIPallocBufferArray(scip, &cliquecoversizes, ntrafolinvars) );
2906  SCIP_CALL( SCIPallocBufferArray(scip, &cliquecovers, ntrafolinvars) );
2907 
2908  /* compute distinct cliques that cover all the variables of the linear constraint */
2909  ncliquecovers = 0;
2910  for (v = 0; v < ntrafolinvars; ++v)
2911  {
2912  /* if variable is not already covered by an already known clique cover */
2913  if ( ! coveredvars[v] )
2914  {
2915  SCIP_CALL( SCIPallocBufferArray(scip, &(cliquecovers[ncliquecovers]), ntrafolinvars) ); /*lint !e866*/
2916  SCIP_CALL( computeVarsCoverSOS1(scip, conflictgraph, conflictgraphlin, trafolinvars, coveredvars, cliquecovers[ncliquecovers], &(cliquecoversizes[ncliquecovers]), v, FALSE) );
2917  ++ncliquecovers;
2918  }
2919  }
2920 
2921  /* free conflictgraph */
2922  SCIPdigraphFree(&conflictgraphlin);
2923 
2924  /* compute variables that are not contained in transformed linear constraint, but are in conflict with a variable from the transformed linear constraint */
2925  nsos1linvars = 0;
2926  for (v = 0; v < ntrafolinvars; ++v)
2927  {
2928  int nodev;
2929 
2930  nodev = varGetNodeSOS1(conshdlrdata, trafolinvars[v]);
2931 
2932  /* if variable is an SOS1 variable */
2933  if ( nodev >= 0 )
2934  {
2935  int succnode;
2936  int nsucc;
2937  int* succ;
2938  int s;
2939 
2940  succ = SCIPdigraphGetSuccessors(conflictgraph, nodev);
2941  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, nodev);
2942 
2943  for (s = 0; s < nsucc; ++s)
2944  {
2945  succnode = succ[s];
2946 
2947  /* if variable is not a member of linear constraint and not already listed in the array sos1linvars */
2948  if ( varindincons[succnode] == -1 )
2949  {
2950  sos1linvars[nsos1linvars] = SCIPnodeGetVarSOS1(conflictgraph, succnode);
2951  varindincons[succnode] = -2; /* mark variable as listed in array sos1linvars */
2952  ++nsos1linvars;
2953  }
2954  }
2955  }
2956  }
2957 
2958  /* try to tighten lower bounds */
2959 
2960  /* sort each cliquecover array in ascending order of the lower bounds of a_i * x_i; fill vector varincover */
2961  SCIP_CALL( SCIPallocBufferArray(scip, &varincover, ntrafolinvars) );
2962  for (i = 0; i < ncliquecovers; ++i)
2963  {
2964  for (j = 0; j < cliquecoversizes[i]; ++j)
2965  {
2966  int ind = cliquecovers[i][j];
2967 
2968  varincover[ind] = i;
2969  cliquecovervals[j] = trafoubs[ind];
2970  }
2971  SCIPsortDownRealInt(cliquecovervals, cliquecovers[i], cliquecoversizes[i]);
2972  }
2973 
2974  /* for every variable in transformed constraint: try lower bound tightening */
2975  for (v = 0; v < ntrafolinvars + nsos1linvars; ++v)
2976  {
2977  SCIP_Real newboundnonzero; /* new bound of a_v * x_v if we assume that x_v != 0 */
2978  SCIP_Real newboundnores; /* new bound of a_v * x_v if we assume that x_v = 0 is possible */
2979  SCIP_Real newbound; /* resulting new bound of x_v */
2980  SCIP_VAR* var;
2981  SCIP_Real trafoubv;
2982  SCIP_Real linval;
2983  SCIP_Real ub;
2984  SCIP_Real lb;
2985  SCIP_Bool tightened;
2986  SCIP_Bool infeasible;
2987  SCIP_Bool inftynores = FALSE;
2988  SCIP_Bool update;
2989  int ninftynonzero = 0;
2990  int nodev;
2991  int w;
2992 
2993  if ( v < ntrafolinvars )
2994  {
2995  var = trafolinvars[v];
2996  trafoubv = trafoubs[v];
2997  }
2998  else
2999  {
3000  assert( v >= ntrafolinvars );
3001  var = sos1linvars[v-ntrafolinvars];/*lint !e679*/
3002  trafoubv = 0.0;
3003  }
3004 
3005  ub = SCIPvarGetUbLocal(var);
3006  lb = SCIPvarGetLbLocal(var);
3007 
3008  if ( SCIPisInfinity(scip, -trafolhs) || SCIPisZero(scip, ub - lb) )
3009  continue;
3010 
3011  newboundnonzero = trafolhs;
3012  newboundnores = trafolhs;
3013  nodev = varGetNodeSOS1(conshdlrdata, var); /* possibly -1 if var is not involved in an SOS1 constraint */
3014  assert( nodev < nsos1vars );
3015 
3016  /* determine incidence vector of implication variables */
3017  for (w = 0; w < nsos1vars; ++w)
3018  implnodes[w] = FALSE;
3019  SCIP_CALL( getSOS1Implications(scip, conshdlrdata, totalvars, implgraph, implhash, implnodes, (int) (size_t) SCIPhashmapGetImage(implhash, var)) );
3020 
3021  /* compute new bound */
3022  for (i = 0; i < ncliquecovers; ++i)
3023  {
3024  int indcliq;
3025  int nodecliq;
3026 
3027  assert( cliquecoversizes[i] > 0 );
3028 
3029  indcliq = cliquecovers[i][0];
3030  assert( 0 <= indcliq && indcliq < ntrafolinvars );
3031 
3032  /* determine maximum without index v (note that the array 'cliquecovers' is sorted by the values of trafoub in non-increasing order) */
3033  if ( v != indcliq )
3034  {
3035  if ( SCIPisInfinity(scip, trafoubs[indcliq]) || SCIPisInfinity(scip, REALABS(newboundnores - trafoubs[indcliq])) )
3036  inftynores = TRUE;
3037  else
3038  newboundnores -= trafoubs[indcliq];
3039  }
3040  else if ( cliquecoversizes[i] > 1 )
3041  {
3042  assert( 0 <= cliquecovers[i][1] && cliquecovers[i][1] < ntrafolinvars );
3043  if ( SCIPisInfinity(scip, trafoubs[cliquecovers[i][1]]) || SCIPisInfinity(scip, REALABS(newboundnores - trafoubs[cliquecovers[i][1]])) )
3044  inftynores = TRUE;
3045  else
3046  newboundnores -= trafoubs[cliquecovers[i][1]];/*lint --e{679}*/
3047  }
3048 
3049  /* determine maximum without index v and if x_v is nonzero (note that the array 'cliquecovers' is sorted by the values of trafoub in non-increasing order) */
3050  for (j = 0; j < cliquecoversizes[i]; ++j)
3051  {
3052  indcliq = cliquecovers[i][j];
3053  assert( 0 <= indcliq && indcliq < ntrafolinvars );
3054 
3055  nodecliq = varGetNodeSOS1(conshdlrdata, trafolinvars[indcliq]); /* possibly -1 if variable is not involved in an SOS1 constraint */
3056  assert( nodecliq < nsos1vars );
3057 
3058  if ( v != indcliq )
3059  {
3060  /* if nodev or nodecliq are not a member of an SOS1 constraint or the variable corresponding to nodecliq is not implied to be zero if x_v != 0 */
3061  if ( nodev < 0 || nodecliq < 0 || (! isConnectedSOS1(adjacencymatrix, NULL, nodev, nodecliq) && ! isImpliedZero(conflictgraph, implnodes, nodecliq) ) )
3062  {
3063  if ( SCIPisInfinity(scip, trafoubs[indcliq]) || SCIPisInfinity(scip, REALABS(newboundnonzero - trafoubs[indcliq])) )
3064  ++ninftynonzero;
3065  else
3066  newboundnonzero -= trafoubs[indcliq];
3067  break; /* break since we are only interested in the maximum upper bound among the variables in the clique cover;
3068  * the variables in the clique cover form an SOS1 constraint, thus only one of them can be nonzero */
3069  }
3070  }
3071  }
3072  }
3073  assert( ninftynonzero == 0 || inftynores );
3074 
3075  /* if computed upper bound is not infinity and variable is contained in linear constraint */
3076  if ( ninftynonzero == 0 && v < ntrafolinvars )
3077  {
3078  linval = trafolinvals[v];
3079 
3080  if ( SCIPisFeasZero(scip, linval) )
3081  continue;
3082 
3083  /* compute new bound */
3084  if ( SCIPisFeasPositive(scip, newboundnores) && ! inftynores )
3085  newbound = newboundnonzero;
3086  else
3087  newbound = MIN(0, newboundnonzero);
3088  newbound /= linval;
3089 
3090  if ( SCIPisInfinity(scip, newbound) )
3091  continue;
3092 
3093  /* check if new bound is tighter than the old one or problem is infeasible */
3094  if ( SCIPisFeasPositive(scip, linval) && SCIPisFeasLT(scip, lb, newbound) )
3095  {
3096  if ( SCIPisFeasLT(scip, ub, newbound) )
3097  {
3098  *cutoff = TRUE;
3099  break;
3100  }
3101 
3102  if ( SCIPvarIsIntegral(var) )
3103  newbound = SCIPceil(scip, newbound);
3104 
3105  SCIP_CALL( SCIPtightenVarLb(scip, var, newbound, FALSE, &infeasible, &tightened) );
3106  assert( ! infeasible );
3107 
3108  if ( tightened )
3109  {
3110  SCIPdebugMsg(scip, "changed lower bound of variable %s from %f to %f \n", SCIPvarGetName(var), lb, newbound);
3111  ++(*nchgbds);
3112  }
3113  }
3114  else if ( SCIPisFeasNegative(scip, linval) && SCIPisFeasGT(scip, ub, newbound) )
3115  {
3116  /* if assumption a_i * x_i != 0 was not correct */
3117  if ( SCIPisFeasGT(scip, SCIPvarGetLbLocal(var), newbound) )
3118  {
3119  *cutoff = TRUE;
3120  break;
3121  }
3122 
3123  if ( SCIPvarIsIntegral(var) )
3124  newbound = SCIPfloor(scip, newbound);
3125 
3126  SCIP_CALL( SCIPtightenVarUb(scip, var, newbound, FALSE, &infeasible, &tightened) );
3127  assert( ! infeasible );
3128 
3129  if ( tightened )
3130  {
3131  SCIPdebugMsg(scip, "changed upper bound of variable %s from %f to %f \n", SCIPvarGetName(var), ub, newbound);
3132  ++(*nchgbds);
3133  }
3134  }
3135  }
3136 
3137  /* update implication graph if possible */
3138  SCIP_CALL( updateImplicationGraphSOS1(scip, conshdlrdata, conflictgraph, adjacencymatrix, implgraph, implhash, implnodes, totalvars, cliquecovers, cliquecoversizes, varincover,
3139  trafolinvars, trafolinvals, ntrafolinvars, trafoubs, var, trafoubv, newboundnonzero, ninftynonzero, TRUE, nchgbds, &update, &infeasible) );
3140  if ( infeasible )
3141  *cutoff = TRUE;
3142  else if ( update )
3143  *implupdate = TRUE;
3144  }
3145 
3146  if ( *cutoff == TRUE )
3147  {
3148  /* free memory */
3149  SCIPfreeBufferArrayNull(scip, &varincover);
3150  for (j = ncliquecovers-1; j >= 0; --j)
3151  SCIPfreeBufferArrayNull(scip, &cliquecovers[j]);
3152  SCIPfreeBufferArrayNull(scip, &cliquecovers);
3153  SCIPfreeBufferArrayNull(scip, &cliquecoversizes);
3154  SCIPfreeBufferArrayNull(scip, &cliquecovervals);
3155  SCIPfreeBufferArrayNull(scip, &trafolinvals);
3156  SCIPfreeBufferArrayNull(scip, &trafolinvars);
3157  break;
3158  }
3159 
3160  /* try to tighten upper bounds */
3161 
3162  /* sort each cliquecover array in ascending order of the lower bounds of a_i * x_i; fill vector varincover */
3163  for (i = 0; i < ncliquecovers; ++i)
3164  {
3165  for (j = 0; j < cliquecoversizes[i]; ++j)
3166  {
3167  int ind = cliquecovers[i][j];
3168 
3169  varincover[ind] = i;
3170  cliquecovervals[j] = trafolbs[ind];
3171  }
3172  SCIPsortRealInt(cliquecovervals, cliquecovers[i], cliquecoversizes[i]);
3173  }
3174 
3175  /* for every variable that is in transformed constraint or every variable that is in conflict with some variable from trans. cons.:
3176  try upper bound tightening */
3177  for (v = 0; v < ntrafolinvars + nsos1linvars; ++v)
3178  {
3179  SCIP_Real newboundnonzero; /* new bound of a_v*x_v if we assume that x_v != 0 */
3180  SCIP_Real newboundnores; /* new bound of a_v*x_v if there are no restrictions */
3181  SCIP_Real newbound; /* resulting new bound of x_v */
3182  SCIP_VAR* var;
3183  SCIP_Real linval;
3184  SCIP_Real trafolbv;
3185  SCIP_Real lb;
3186  SCIP_Real ub;
3187  SCIP_Bool tightened;
3188  SCIP_Bool infeasible;
3189  SCIP_Bool inftynores = FALSE;
3190  SCIP_Bool update;
3191  int ninftynonzero = 0;
3192  int nodev;
3193  int w;
3194 
3195  if ( v < ntrafolinvars )
3196  {
3197  var = trafolinvars[v];
3198  trafolbv = trafolbs[v];
3199  }
3200  else
3201  {
3202  assert( v-ntrafolinvars >= 0 );
3203  var = sos1linvars[v-ntrafolinvars];/*lint !e679*/
3204  trafolbv = 0.0; /* since variable is not a member of linear constraint */
3205  }
3206  lb = SCIPvarGetLbLocal(var);
3207  ub = SCIPvarGetUbLocal(var);
3208  if ( SCIPisInfinity(scip, traforhs) || SCIPisEQ(scip, lb, ub) )
3209  continue;
3210 
3211  newboundnonzero = traforhs;
3212  newboundnores = traforhs;
3213  nodev = varGetNodeSOS1(conshdlrdata, var); /* possibly -1 if var is not involved in an SOS1 constraint */
3214  assert( nodev < nsos1vars );
3215 
3216  /* determine incidence vector of implication variables (i.e., which SOS1 variables are nonzero if x_v is nonzero) */
3217  for (w = 0; w < nsos1vars; ++w)
3218  implnodes[w] = FALSE;
3219  SCIP_CALL( getSOS1Implications(scip, conshdlrdata, totalvars, implgraph, implhash, implnodes, (int) (size_t) SCIPhashmapGetImage(implhash, var)) );
3220 
3221  /* compute new bound */
3222  for (i = 0; i < ncliquecovers; ++i)
3223  {
3224  int indcliq;
3225  int nodecliq;
3226 
3227  assert( cliquecoversizes[i] > 0 );
3228 
3229  indcliq = cliquecovers[i][0];
3230  assert( 0 <= indcliq && indcliq < ntrafolinvars );
3231 
3232  /* determine minimum without index v (note that the array 'cliquecovers' is sorted by the values of trafolb in increasing order) */
3233  if ( v != indcliq )
3234  {
3235  /* if bound would be infinity */
3236  if ( SCIPisInfinity(scip, -trafolbs[indcliq]) || SCIPisInfinity(scip, REALABS(newboundnores - trafolbs[indcliq])) )
3237  inftynores = TRUE;
3238  else
3239  newboundnores -= trafolbs[indcliq];
3240  }
3241  else if ( cliquecoversizes[i] > 1 )
3242  {
3243  assert( 0 <= cliquecovers[i][1] && cliquecovers[i][1] < ntrafolinvars );
3244  if ( SCIPisInfinity(scip, -trafolbs[cliquecovers[i][1]]) || SCIPisInfinity(scip, REALABS(newboundnores - trafolbs[cliquecovers[i][1]])) )
3245  inftynores = TRUE;
3246  else
3247  newboundnores -= trafolbs[cliquecovers[i][1]]; /*lint --e{679}*/
3248  }
3249 
3250  /* determine minimum without index v and if x_v is nonzero (note that the array 'cliquecovers' is sorted by the values of trafolb in increasing order) */
3251  for (j = 0; j < cliquecoversizes[i]; ++j)
3252  {
3253  indcliq = cliquecovers[i][j];
3254  assert( 0 <= indcliq && indcliq < ntrafolinvars );
3255 
3256  nodecliq = varGetNodeSOS1(conshdlrdata, trafolinvars[indcliq]); /* possibly -1 if variable is not involved in an SOS1 constraint */
3257  assert( nodecliq < nsos1vars );
3258 
3259  if ( v != indcliq )
3260  {
3261  /* if nodev or nodecliq are not a member of an SOS1 constraint or the variable corresponding to nodecliq is not implied to be zero if x_v != 0 */
3262  if ( nodev < 0 || nodecliq < 0 || (! isConnectedSOS1(adjacencymatrix, NULL, nodev, nodecliq) && ! isImpliedZero(conflictgraph, implnodes, nodecliq) ) )
3263  {
3264  /* if bound would be infinity */
3265  if ( SCIPisInfinity(scip, -trafolbs[indcliq]) || SCIPisInfinity(scip, REALABS(newboundnonzero - trafolbs[indcliq])) )
3266  ++ninftynonzero;
3267  else
3268  newboundnonzero -= trafolbs[indcliq];
3269  break; /* break since we are only interested in the minimum lower bound among the variables in the clique cover;
3270  * the variables in the clique cover form an SOS1 constraint, thus only one of them can be nonzero */
3271  }
3272  }
3273  }
3274  }
3275  assert( ninftynonzero == 0 || inftynores );
3276 
3277  /* if computed bound is not infinity and variable is contained in linear constraint */
3278  if ( ninftynonzero == 0 && v < ntrafolinvars )
3279  {
3280  linval = trafolinvals[v];
3281 
3282  if ( SCIPisFeasZero(scip, linval) )
3283  continue;
3284 
3285  /* compute new bound */
3286  if ( SCIPisFeasNegative(scip, newboundnores) && ! inftynores )
3287  newbound = newboundnonzero;
3288  else
3289  newbound = MAX(0, newboundnonzero);
3290  newbound /= linval;
3291 
3292  if ( SCIPisInfinity(scip, newbound) )
3293  continue;
3294 
3295  /* check if new bound is tighter than the old one or problem is infeasible */
3296  if ( SCIPisFeasPositive(scip, linval) && SCIPisFeasGT(scip, ub, newbound) )
3297  {
3298  /* if new upper bound is smaller than the lower bound, we are infeasible */
3299  if ( SCIPisFeasGT(scip, lb, newbound) )
3300  {
3301  *cutoff = TRUE;
3302  break;
3303  }
3304 
3305  if ( SCIPvarIsIntegral(var) )
3306  newbound = SCIPfloor(scip, newbound);
3307 
3308  SCIP_CALL( SCIPtightenVarUb(scip, var, newbound, FALSE, &infeasible, &tightened) );
3309  assert( ! infeasible );
3310 
3311  if ( tightened )
3312  {
3313  SCIPdebugMsg(scip, "changed upper bound of variable %s from %f to %f \n", SCIPvarGetName(var), ub, newbound);
3314  ++(*nchgbds);
3315  }
3316  }
3317  else if ( SCIPisFeasNegative(scip, linval) && SCIPisFeasLT(scip, lb, newbound) )
3318  {
3319  /* if assumption a_i * x_i != 0 was not correct */
3320  if ( SCIPisFeasLT(scip, ub, newbound) )
3321  {
3322  *cutoff = TRUE;
3323  break;
3324  }
3325 
3326  if ( SCIPvarIsIntegral(var) )
3327  newbound = SCIPceil(scip, newbound);
3328 
3329  SCIP_CALL( SCIPtightenVarLb(scip, var, newbound, FALSE, &infeasible, &tightened) );
3330  assert( ! infeasible );
3331 
3332  if ( tightened )
3333  {
3334  SCIPdebugMsg(scip, "changed lower bound of variable %s from %f to %f \n", SCIPvarGetName(var), lb, newbound);
3335  ++(*nchgbds);
3336  }
3337  }
3338  }
3339 
3340  /* update implication graph if possible */
3341  SCIP_CALL( updateImplicationGraphSOS1(scip, conshdlrdata, conflictgraph, adjacencymatrix, implgraph, implhash, implnodes, totalvars, cliquecovers, cliquecoversizes, varincover,
3342  trafolinvars, trafolinvals, ntrafolinvars, trafolbs, var, trafolbv, newboundnonzero, ninftynonzero, FALSE, nchgbds, &update, &infeasible) );
3343  if ( infeasible )
3344  *cutoff = TRUE;
3345  else if ( update )
3346  *implupdate = TRUE;
3347  }
3348 
3349  /* free memory */
3350  SCIPfreeBufferArrayNull(scip, &varincover);
3351  for (j = ncliquecovers-1; j >= 0; --j)
3352  SCIPfreeBufferArrayNull(scip, &cliquecovers[j]);
3353  SCIPfreeBufferArrayNull(scip, &cliquecovers);
3354  SCIPfreeBufferArrayNull(scip, &cliquecoversizes);
3355  SCIPfreeBufferArrayNull(scip, &cliquecovervals);
3356  SCIPfreeBufferArrayNull(scip, &trafolinvals);
3357  SCIPfreeBufferArrayNull(scip, &trafolinvars);
3358 
3359  if ( *cutoff == TRUE )
3360  break;
3361  } /* end for every linear constraint */
3362 
3363  /* free buffer arrays */
3364  SCIPfreeBufferArrayNull(scip, &sos1linvars);
3365  SCIPfreeBufferArrayNull(scip, &trafolbs);
3366  SCIPfreeBufferArrayNull(scip, &trafoubs);
3367  SCIPfreeBufferArrayNull(scip, &coveredvars);
3368  SCIPfreeBufferArrayNull(scip, &varindincons);
3369  SCIPfreeBufferArrayNull(scip, &implnodes);
3370 
3371  return SCIP_OKAY;
3372 }
3373 
3374 
3375 /** perform one presolving round for variables
3376  *
3377  * We perform the following presolving steps:
3378  * - Tighten the bounds of the variables
3379  * - Update conflict graph based on bound implications of the variables
3380  */
3381 static
3383  SCIP* scip, /**< SCIP pointer */
3384  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
3385  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
3386  SCIP_Bool** adjacencymatrix, /**< adjacencymatrix of conflict graph */
3387  int nsos1vars, /**< number of SOS1 variables */
3388  int* nfixedvars, /**< pointer to store number of fixed variables */
3389  int* nchgbds, /**< pointer to store number of changed bounds */
3390  int* naddconss, /**< pointer to store number of addded constraints */
3391  SCIP_RESULT* result /**< result */
3392  )
3393 {
3394  SCIP_DIGRAPH* implgraph;
3395  SCIP_HASHMAP* implhash;
3396 
3397  SCIP_Bool cutoff = FALSE;
3398  SCIP_Bool updateconfl;
3399 
3400  SCIP_VAR** totalvars;
3401  SCIP_VAR** probvars;
3402  int ntotalvars = 0;
3403  int nprobvars;
3404  int i;
3405  int j;
3406 
3407  /* determine totalvars (union of SOS1 and problem variables) */
3408  probvars = SCIPgetVars(scip);
3409  nprobvars = SCIPgetNVars(scip);
3410  SCIP_CALL( SCIPhashmapCreate(&implhash, SCIPblkmem(scip), nsos1vars + nprobvars) );
3411  SCIP_CALL( SCIPallocBufferArray(scip, &totalvars, nsos1vars + nprobvars) );
3412 
3413  for (i = 0; i < nsos1vars; ++i)
3414  {
3415  SCIP_VAR* var;
3416  var = SCIPnodeGetVarSOS1(conflictgraph, i);
3417 
3418  /* insert node number to hash map */
3419  assert( ! SCIPhashmapExists(implhash, var) );
3420  SCIP_CALL( SCIPhashmapInsert(implhash, var, (void*) (size_t) ntotalvars) );/*lint !e571*/
3421  assert( ntotalvars == (int) (size_t) SCIPhashmapGetImage(implhash, var) );
3422  totalvars[ntotalvars++] = var;
3423  }
3424 
3425  for (i = 0; i < nprobvars; ++i)
3426  {
3427  SCIP_VAR* var;
3428  var = probvars[i];
3429 
3430  /* insert node number to hash map if not existent */
3431  if ( ! SCIPhashmapExists(implhash, var) )
3432  {
3433  SCIP_CALL( SCIPhashmapInsert(implhash, var, (void*) (size_t) ntotalvars) );/*lint !e571*/
3434  assert( ntotalvars == (int) (size_t) SCIPhashmapGetImage(implhash, var) );
3435  totalvars[ntotalvars++] = var;
3436  }
3437  }
3438 
3439  /* create implication graph */
3440  SCIP_CALL( SCIPcreateDigraph(scip, &implgraph, ntotalvars) );
3441 
3442  /* try to tighten the lower and upper bounds of the variables */
3443  updateconfl = FALSE;
3444  for (j = 0; (j < conshdlrdata->maxtightenbds || conshdlrdata->maxtightenbds == -1 ) && ! cutoff; ++j)
3445  {
3446  SCIP_Bool implupdate;
3447  int nchgbdssave;
3448 
3449  nchgbdssave = *nchgbds;
3450 
3451  assert( ntotalvars > 0 );
3452  SCIP_CALL( tightenVarsBoundsSOS1(scip, conshdlrdata, conflictgraph, implgraph, implhash, adjacencymatrix, totalvars, ntotalvars, nsos1vars, nchgbds, &implupdate, &cutoff) );
3453  if ( *nchgbds > nchgbdssave )
3454  {
3455  *result = SCIP_SUCCESS;
3456  if ( implupdate )
3457  updateconfl = TRUE;
3458  }
3459  else if ( implupdate )
3460  updateconfl = TRUE;
3461  else
3462  break;
3463  }
3464 
3465  /* perform implication graph analysis */
3466  if ( updateconfl && conshdlrdata->perfimplanalysis && ! cutoff )
3467  {
3468  SCIP_Real* implubs;
3469  SCIP_Real* impllbs;
3470  SCIP_Bool* implnodes;
3471  SCIP_Bool infeasible;
3472  SCIP_Bool fixed;
3473  int naddconsssave;
3474  int probingdepth;
3475 
3476  /* allocate buffer arrays */
3477  SCIP_CALL( SCIPallocBufferArray(scip, &implnodes, nsos1vars) );
3478  SCIP_CALL( SCIPallocBufferArray(scip, &impllbs, ntotalvars) );
3479  SCIP_CALL( SCIPallocBufferArray(scip, &implubs, ntotalvars) );
3480 
3481  naddconsssave = *naddconss;
3482  for (i = 0; i < nsos1vars; ++i)
3483  {
3484  /* initialize data for implication graph analysis */
3485  infeasible = FALSE;
3486  probingdepth = 0;
3487  for (j = 0; j < nsos1vars; ++j)
3488  implnodes[j] = FALSE;
3489  for (j = 0; j < ntotalvars; ++j)
3490  {
3491  impllbs[j] = SCIPvarGetLbLocal(totalvars[j]);
3492  implubs[j] = SCIPvarGetUbLocal(totalvars[j]);
3493  }
3494 
3495  /* try to update the conflict graph based on the information of the implication graph */
3496  SCIP_CALL( performImplicationGraphAnalysis(scip, conshdlrdata, conflictgraph, totalvars, implgraph, implhash, adjacencymatrix, i, i, impllbs, implubs, implnodes, naddconss, &probingdepth, &infeasible) );
3497 
3498  /* if the subproblem turned out to be infeasible then fix variable to zero */
3499  if ( infeasible )
3500  {
3501  SCIP_CALL( SCIPfixVar(scip, totalvars[i], 0.0, &infeasible, &fixed) );
3502 
3503  if ( fixed )
3504  {
3505  SCIPdebugMsg(scip, "fixed variable %s with lower bound %f and upper bound %f to zero\n",
3506  SCIPvarGetName(totalvars[i]), SCIPvarGetLbLocal(totalvars[i]), SCIPvarGetUbLocal(totalvars[i]));
3507  ++(*nfixedvars);
3508  }
3509 
3510  if ( infeasible )
3511  cutoff = TRUE;
3512  }
3513  }
3514 
3515  if ( *naddconss > naddconsssave )
3516  *result = SCIP_SUCCESS;
3517 
3518  /* free buffer arrays */
3519  SCIPfreeBufferArrayNull(scip, &implubs);
3520  SCIPfreeBufferArrayNull(scip, &impllbs);
3521  SCIPfreeBufferArrayNull(scip, &implnodes);
3522  }
3523 
3524  /* if an infeasibility has been detected */
3525  if ( cutoff )
3526  {
3527  SCIPdebugMsg(scip, "cutoff \n");
3528  *result = SCIP_CUTOFF;
3529  }
3530 
3531  /* free memory */;
3532  for (j = ntotalvars-1; j >= 0; --j)
3533  {
3534  SCIP_SUCCDATA** succdatas;
3535  int nsucc;
3536  int s;
3537 
3538  succdatas = (SCIP_SUCCDATA**) SCIPdigraphGetSuccessorsData(implgraph, j);
3539  nsucc = SCIPdigraphGetNSuccessors(implgraph, j);
3540 
3541  for (s = nsucc-1; s >= 0; --s)
3542  SCIPfreeBlockMemory(scip, &succdatas[s]);/*lint !e866*/
3543  }
3544  SCIPdigraphFree(&implgraph);
3545  SCIPfreeBufferArrayNull(scip, &totalvars);
3546  SCIPhashmapFree(&implhash);
3547 
3548  return SCIP_OKAY;
3549 }
3550 
3551 
3552 /* ----------------------------- propagation -------------------------------------*/
3553 
3554 /** propagate variables of SOS1 constraint */
3555 static
3557  SCIP* scip, /**< SCIP pointer */
3558  SCIP_CONS* cons, /**< constraint */
3559  SCIP_CONSDATA* consdata, /**< constraint data */
3560  SCIP_Bool* cutoff, /**< whether a cutoff happened */
3561  int* ngen /**< number of domain changes */
3562  )
3563 {
3564  assert( scip != NULL );
3565  assert( cons != NULL );
3566  assert( consdata != NULL );
3567  assert( cutoff != NULL );
3568  assert( ngen != NULL );
3569 
3570  *cutoff = FALSE;
3571 
3572  /* if more than one variable is fixed to be nonzero */
3573  if ( consdata->nfixednonzeros > 1 )
3574  {
3575  SCIPdebugMsg(scip, "the node is infeasible, more than 1 variable is fixed to be nonzero.\n");
3576  SCIP_CALL( SCIPresetConsAge(scip, cons) );
3577  *cutoff = TRUE;
3578  return SCIP_OKAY;
3579  }
3580 
3581  /* if exactly one variable is fixed to be nonzero */
3582  if ( consdata->nfixednonzeros == 1 )
3583  {
3584  SCIP_VAR** vars;
3585  SCIP_Bool infeasible;
3586  SCIP_Bool tightened;
3587  SCIP_Bool success;
3588  SCIP_Bool allVarFixed;
3589  int firstFixedNonzero;
3590  int nvars;
3591  int j;
3592 
3593  firstFixedNonzero = -1;
3594  nvars = consdata->nvars;
3595  vars = consdata->vars;
3596  assert( vars != NULL );
3597 
3598  /* search nonzero variable - is needed for propinfo */
3599  for (j = 0; j < nvars; ++j)
3600  {
3601  if ( SCIPisFeasPositive(scip, SCIPvarGetLbLocal(vars[j])) || SCIPisFeasNegative(scip, SCIPvarGetUbLocal(vars[j])) )
3602  {
3603  firstFixedNonzero = j;
3604  break;
3605  }
3606  }
3607  assert( firstFixedNonzero >= 0 );
3608 
3609  SCIPdebugMsg(scip, "variable <%s> is fixed nonzero, fixing other variables to 0.\n", SCIPvarGetName(vars[firstFixedNonzero]));
3610 
3611  /* fix variables before firstFixedNonzero to 0 */
3612  allVarFixed = TRUE;
3613  for (j = 0; j < firstFixedNonzero; ++j)
3614  {
3615  /* fix variable */
3616  SCIP_CALL( inferVariableZero(scip, vars[j], cons, firstFixedNonzero, &infeasible, &tightened, &success) );
3617  assert( ! infeasible );
3618  allVarFixed = allVarFixed && success;
3619  if ( tightened )
3620  ++(*ngen);
3621  }
3622 
3623  /* fix variables after firstFixedNonzero to 0 */
3624  for (j = firstFixedNonzero+1; j < nvars; ++j)
3625  {
3626  /* fix variable */
3627  SCIP_CALL( inferVariableZero(scip, vars[j], cons, firstFixedNonzero, &infeasible, &tightened, &success) );
3628  assert( ! infeasible ); /* there should be no variables after firstFixedNonzero that are fixed to be nonzero */
3629  allVarFixed = allVarFixed && success;
3630  if ( tightened )
3631  ++(*ngen);
3632  }
3633 
3634  /* reset constraint age counter */
3635  if ( *ngen > 0 )
3636  {
3637  SCIP_CALL( SCIPresetConsAge(scip, cons) );
3638  }
3639 
3640  /* delete constraint locally */
3641  if ( allVarFixed )
3642  {
3643  assert( !SCIPconsIsModifiable(cons) );
3644  SCIP_CALL( SCIPdelConsLocal(scip, cons) );
3645  }
3646  }
3647 
3648  return SCIP_OKAY;
3649 }
3650 
3651 
3652 /** propagate a variable that is known to be nonzero */
3653 static
3655  SCIP* scip, /**< SCIP pointer */
3656  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
3657  SCIP_DIGRAPH* implgraph, /**< implication graph */
3658  SCIP_CONS* cons, /**< some arbitrary SOS1 constraint */
3659  int node, /**< conflict graph node of variable that is known to be nonzero */
3660  SCIP_Bool implprop, /**< whether implication graph propagation shall be applied */
3661  SCIP_Bool* cutoff, /**< whether a cutoff happened */
3662  int* ngen /**< number of domain changes */
3663  )
3664 {
3665  int inferinfo;
3666  int* succ;
3667  int nsucc;
3668  int s;
3669 
3670  assert( scip != NULL );
3671  assert( conflictgraph != NULL );
3672  assert( cutoff != NULL );
3673  assert( ngen != NULL );
3674  assert( node >= 0 );
3675 
3676  *cutoff = FALSE;
3677  inferinfo = -node - 1;
3678 
3679  /* by assumption zero is outside the domain of variable */
3680  assert( SCIPisFeasPositive(scip, SCIPvarGetLbLocal(SCIPnodeGetVarSOS1(conflictgraph, node))) || SCIPisFeasNegative(scip, SCIPvarGetUbLocal(SCIPnodeGetVarSOS1(conflictgraph, node))) );
3681 
3682  /* apply conflict graph propagation (fix all neighbors in the conflict graph to zero) */
3683  succ = SCIPdigraphGetSuccessors(conflictgraph, node);
3684  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, node);
3685  for (s = 0; s < nsucc; ++s)
3686  {
3687  SCIP_VAR* succvar;
3688  SCIP_Real lb;
3689  SCIP_Real ub;
3690 
3691  succvar = SCIPnodeGetVarSOS1(conflictgraph, succ[s]);
3692  lb = SCIPvarGetLbLocal(succvar);
3693  ub = SCIPvarGetUbLocal(succvar);
3694 
3695  if ( ! SCIPisFeasZero(scip, lb) || ! SCIPisFeasZero(scip, ub) )
3696  {
3697  SCIP_Bool infeasible;
3698  SCIP_Bool tightened;
3699  SCIP_Bool success;
3700 
3701  /* fix variable if it is not multi-aggregated */
3702  SCIP_CALL( inferVariableZero(scip, succvar, cons, inferinfo, &infeasible, &tightened, &success) );
3703 
3704  if ( infeasible )
3705  {
3706  /* variable cannot be nonzero */
3707  *cutoff = TRUE;
3708  return SCIP_OKAY;
3709  }
3710  if ( tightened )
3711  ++(*ngen);
3712  assert( success || SCIPvarGetStatus(succvar) == SCIP_VARSTATUS_MULTAGGR );
3713  }
3714  }
3715 
3716  /* apply implication graph propagation */
3717  if ( implprop && implgraph != NULL )
3718  {
3719  SCIP_SUCCDATA** succdatas;
3720 
3721 #ifndef NDEBUG
3722  SCIP_NODEDATA* nodedbgdata;
3723  nodedbgdata = (SCIP_NODEDATA*) SCIPdigraphGetNodeData(implgraph, node);
3724  assert( SCIPvarCompare(nodedbgdata->var, SCIPnodeGetVarSOS1(conflictgraph, node)) == 0 );
3725 #endif
3726 
3727  /* get successor datas */
3728  succdatas = (SCIP_SUCCDATA**) SCIPdigraphGetSuccessorsData(implgraph, node);
3729 
3730  if ( succdatas != NULL )
3731  {
3732  succ = SCIPdigraphGetSuccessors(implgraph, node);
3733  nsucc = SCIPdigraphGetNSuccessors(implgraph, node);
3734  for (s = 0; s < nsucc; ++s)
3735  {
3736  SCIP_SUCCDATA* succdata;
3737  SCIP_NODEDATA* nodedata;
3738  SCIP_VAR* var;
3739 
3740  nodedata = (SCIP_NODEDATA*) SCIPdigraphGetNodeData(implgraph, succ[s]);
3741  assert( nodedata != NULL );
3742  succdata = succdatas[s];
3743  assert( succdata != NULL );
3744  var = nodedata->var;
3745  assert( var != NULL );
3746 
3747  /* tighten variable if it is not multi-aggregated */
3749  {
3750  /* check for lower bound implication */
3751  if ( SCIPisFeasLT(scip, SCIPvarGetLbLocal(var), succdata->lbimpl) )
3752  {
3753  SCIP_Bool infeasible;
3754  SCIP_Bool tightened;
3755 
3756  SCIP_CALL( SCIPinferVarLbCons(scip, var, succdata->lbimpl, cons, inferinfo, FALSE, &infeasible, &tightened) );
3757  if ( infeasible )
3758  {
3759  *cutoff = TRUE;
3760  return SCIP_OKAY;
3761  }
3762  if ( tightened )
3763  ++(*ngen);
3764  }
3765 
3766  /* check for upper bound implication */
3767  if ( SCIPisFeasGT(scip, SCIPvarGetUbLocal(var), succdata->ubimpl) )
3768  {
3769  SCIP_Bool infeasible;
3770  SCIP_Bool tightened;
3771 
3772  SCIP_CALL( SCIPinferVarUbCons(scip, var, succdata->ubimpl, cons, inferinfo, FALSE, &infeasible, &tightened) );
3773  if ( infeasible )
3774  {
3775  *cutoff = TRUE;
3776  return SCIP_OKAY;
3777  }
3778  if ( tightened )
3779  ++(*ngen);
3780  }
3781  }
3782  }
3783  }
3784  }
3785 
3786  return SCIP_OKAY;
3787 }
3788 
3789 
3790 /** initialize implication graph
3791  *
3792  * @p j is successor of @p i if and only if \f$ x_i\not = 0 \Rightarrow x_j\not = 0\f$
3793  *
3794  * @note By construction the implication graph is globally valid.
3795  */
3796 static
3798  SCIP* scip, /**< SCIP pointer */
3799  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
3800  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
3801  int nsos1vars, /**< number of SOS1 variables */
3802  int maxrounds, /**< maximal number of propagation rounds for generating implications */
3803  int* nchgbds, /**< pointer to store number of bound changes */
3804  SCIP_Bool* cutoff, /**< pointer to store whether a cutoff occurred */
3805  SCIP_Bool* success /**< whether initialization was successful */
3806  )
3807 {
3808  SCIP_HASHMAP* implhash = NULL;
3809  SCIP_Bool** adjacencymatrix = NULL;
3810  SCIP_Bool* implnodes = NULL;
3811  SCIP_VAR** implvars = NULL;
3812  SCIP_VAR** probvars;
3813  int nimplnodes;
3814  int nprobvars;
3815  int i;
3816  int j;
3817 
3818  assert( scip != NULL );
3819  assert( conshdlrdata != NULL );
3820  assert( conflictgraph != NULL );
3821  assert( conshdlrdata->implgraph == NULL );
3822  assert( conshdlrdata->nimplnodes == 0 );
3823  assert( cutoff != NULL );
3824  assert( nchgbds != NULL );
3825 
3826  *nchgbds = 0;
3827  *cutoff = FALSE;
3828 
3829  /* we do not create the adjacency matrix of the conflict graph if the number of SOS1 variables is larger than a predefined value */
3830  if ( conshdlrdata->maxsosadjacency != -1 && nsos1vars > conshdlrdata->maxsosadjacency )
3831  {
3832  *success = FALSE;
3833  SCIPdebugMsg(scip, "Implication graph was not created since number of SOS1 variables (%d) is larger than %d.\n", nsos1vars, conshdlrdata->maxsosadjacency);
3834 
3835  return SCIP_OKAY;
3836  }
3837  *success = TRUE;
3838 
3839  /* only add globally valid implications to implication graph */
3840  assert ( SCIPgetDepth(scip) == 0 );
3841 
3842  probvars = SCIPgetVars(scip);
3843  nprobvars = SCIPgetNVars(scip);
3844  nimplnodes = 0;
3845 
3846  /* create implication graph */
3847  SCIP_CALL( SCIPdigraphCreate(&conshdlrdata->implgraph, SCIPblkmem(scip), nsos1vars + nprobvars) );
3848 
3849  /* create hashmap */
3850  SCIP_CALL( SCIPhashmapCreate(&implhash, SCIPblkmem(scip), nsos1vars + nprobvars) );
3851 
3852  /* determine implvars (union of SOS1 and problem variables)
3853  * Note: For separation of implied bound cuts it is important that SOS1 variables are enumerated first
3854  */
3855  SCIP_CALL( SCIPallocBufferArray(scip, &implvars, nsos1vars + nprobvars) );
3856  for (i = 0; i < nsos1vars; ++i)
3857  {
3858  SCIP_VAR* var;
3859  var = SCIPnodeGetVarSOS1(conflictgraph, i);
3860 
3861  /* insert node number to hash map */
3862  assert( ! SCIPhashmapExists(implhash, var) );
3863  SCIP_CALL( SCIPhashmapInsert(implhash, var, (void*) (size_t) nimplnodes) );/*lint !e571*/
3864  assert( nimplnodes == (int) (size_t) SCIPhashmapGetImage(implhash, var) );
3865  implvars[nimplnodes++] = var;
3866  }
3867 
3868  for (i = 0; i < nprobvars; ++i)
3869  {
3870  SCIP_VAR* var;
3871  var = probvars[i];
3872 
3873  /* insert node number to hash map if not existent */
3874  if ( ! SCIPhashmapExists(implhash, var) )
3875  {
3876  SCIP_CALL( SCIPhashmapInsert(implhash, var, (void*) (size_t) nimplnodes) );/*lint !e571*/
3877  assert( nimplnodes == (int) (size_t) SCIPhashmapGetImage(implhash, var) );
3878  implvars[nimplnodes++] = var;
3879  }
3880  }
3881  conshdlrdata->nimplnodes = nimplnodes;
3882 
3883  /* add variables to nodes of implication graph */
3884  for (i = 0; i < nimplnodes; ++i)
3885  {
3886  SCIP_NODEDATA* nodedata = NULL;
3887 
3888  /* create node data */
3889  SCIP_CALL( SCIPallocBlockMemory(scip, &nodedata) );
3890  nodedata->var = implvars[i];
3891 
3892  /* set node data */
3893  SCIPdigraphSetNodeData(conshdlrdata->implgraph, (void*) nodedata, i);
3894  }
3895 
3896  /* allocate buffer arrays */
3897  SCIP_CALL( SCIPallocBufferArray(scip, &implnodes, nsos1vars) );
3898  SCIP_CALL( SCIPallocBufferArray(scip, &adjacencymatrix, nsos1vars) );
3899 
3900  for (i = 0; i < nsos1vars; ++i)
3901  SCIP_CALL( SCIPallocBufferArray(scip, &adjacencymatrix[i], i+1) ); /*lint !e866*/
3902 
3903  /* create adjacency matrix */
3904  for (i = 0; i < nsos1vars; ++i)
3905  {
3906  for (j = 0; j < i+1; ++j)
3907  adjacencymatrix[i][j] = 0;
3908  }
3909 
3910  for (i = 0; i < nsos1vars; ++i)
3911  {
3912  int* succ;
3913  int nsucc;
3914  succ = SCIPdigraphGetSuccessors(conflictgraph, i);
3915  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, i);
3916 
3917  for (j = 0; j < nsucc; ++j)
3918  {
3919  if ( i > succ[j] )
3920  adjacencymatrix[i][succ[j]] = 1;
3921  }
3922  }
3923 
3924  assert( SCIPgetDepth(scip) == 0 );
3925 
3926  /* compute SOS1 implications from linear constraints and tighten bounds of variables */
3927  for (j = 0; (j < maxrounds || maxrounds == -1 ); ++j)
3928  {
3929  SCIP_Bool implupdate;
3930  int nchgbdssave;
3931 
3932  nchgbdssave = *nchgbds;
3933 
3934  assert( nimplnodes > 0 );
3935  SCIP_CALL( tightenVarsBoundsSOS1(scip, conshdlrdata, conflictgraph, conshdlrdata->implgraph, implhash, adjacencymatrix, implvars, nimplnodes, nsos1vars, nchgbds, &implupdate, cutoff) );
3936  if ( *cutoff || ( ! implupdate && ! ( *nchgbds > nchgbdssave ) ) )
3937  break;
3938  }
3939 
3940  /* free memory */
3941  for (i = nsos1vars-1; i >= 0; --i)
3942  SCIPfreeBufferArrayNull(scip, &adjacencymatrix[i]);
3943  SCIPfreeBufferArrayNull(scip, &adjacencymatrix);
3944  SCIPfreeBufferArrayNull(scip, &implnodes);
3945  SCIPfreeBufferArrayNull(scip, &implvars);
3946  SCIPhashmapFree(&implhash);
3947 
3948 #ifdef SCIP_DEBUG
3949  /* evaluate results */
3950  if ( cutoff )
3951  {
3952  SCIPdebugMsg(scip, "cutoff \n");
3953  }
3954  else if ( *nchgbds > 0 )
3955  {
3956  SCIPdebugMsg(scip, "found %d bound changes\n", *nchgbds);
3957  }
3958 #endif
3959 
3960  assert( conshdlrdata->implgraph != NULL );
3961 
3962  return SCIP_OKAY;
3963 }
3964 
3965 
3966 /** deinitialize implication graph */
3967 static
3969  SCIP* scip, /**< SCIP pointer */
3970  SCIP_CONSHDLRDATA* conshdlrdata /**< constraint handler data */
3971  )
3972 {
3973  int j;
3975  assert( scip != NULL );
3976  assert( conshdlrdata != NULL );
3977 
3978  /* free whole memory of implication graph */
3979  if ( conshdlrdata->implgraph == NULL )
3980  {
3981  assert( conshdlrdata->nimplnodes == 0 );
3982  return SCIP_OKAY;
3983  }
3984 
3985  /* free arc data */
3986  for (j = conshdlrdata->nimplnodes-1; j >= 0; --j)
3987  {
3988  SCIP_SUCCDATA** succdatas;
3989  int nsucc;
3990  int s;
3991 
3992  succdatas = (SCIP_SUCCDATA**) SCIPdigraphGetSuccessorsData(conshdlrdata->implgraph, j);
3993  nsucc = SCIPdigraphGetNSuccessors(conshdlrdata->implgraph, j);
3994 
3995  for (s = nsucc-1; s >= 0; --s)
3996  {
3997  assert( succdatas[s] != NULL );
3998  SCIPfreeBlockMemory(scip, &succdatas[s]);/*lint !e866*/
3999  }
4000  }
4001 
4002  /* free node data */
4003  for (j = conshdlrdata->nimplnodes-1; j >= 0; --j)
4004  {
4005  SCIP_NODEDATA* nodedata;
4006  nodedata = (SCIP_NODEDATA*)SCIPdigraphGetNodeData(conshdlrdata->implgraph, j);
4007  assert( nodedata != NULL );
4008  SCIPfreeBlockMemory(scip, &nodedata);
4009  SCIPdigraphSetNodeData(conshdlrdata->implgraph, NULL, j);
4010  }
4011 
4012  /* free implication graph */
4013  SCIPdigraphFree(&conshdlrdata->implgraph);
4014  conshdlrdata->nimplnodes = 0;
4015 
4016  return SCIP_OKAY;
4017 }
4018 
4019 
4020 /* ----------------------------- branching -------------------------------------*/
4021 
4022 /** get the vertices whose neighbor set covers a subset of the neighbor set of a given other vertex.
4023  *
4024  * This function can be used to compute sets of variables to branch on.
4025  */
4026 static
4028  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
4029  SCIP_Bool* verticesarefixed, /**< array that indicates which variables are currently fixed to zero */
4030  int vertex, /**< vertex (-1 if not needed) */
4031  int* neightocover, /**< neighbors of given vertex to be covered (or NULL if all neighbors shall be covered) */
4032  int nneightocover, /**< number of entries of neightocover (or 0 if all neighbors shall be covered )*/
4033  int* coververtices, /**< array to store the vertices whose neighbor set covers the neighbor set of the given vertex */
4034  int* ncoververtices /**< pointer to store size of coververtices */
4035  )
4036 {
4037  int* succ1;
4038  int nsucc1;
4039  int s;
4040 
4041  assert( conflictgraph != NULL );
4042  assert( verticesarefixed != NULL );
4043  assert( coververtices != NULL );
4044  assert( ncoververtices != NULL );
4045 
4046  *ncoververtices = 0;
4047 
4048  /* if all the neighbors shall be covered */
4049  if ( neightocover == NULL )
4050  {
4051  assert( nneightocover == 0 );
4052  nsucc1 = SCIPdigraphGetNSuccessors(conflictgraph, vertex);
4053  succ1 = SCIPdigraphGetSuccessors(conflictgraph, vertex);
4054  }
4055  else
4056  {
4057  nsucc1 = nneightocover;
4058  succ1 = neightocover;
4059  }
4060 
4061  /* determine all the successors of the first unfixed successor */
4062  for (s = 0; s < nsucc1; ++s)
4063  {
4064  int succvertex1 = succ1[s];
4065 
4066  if ( ! verticesarefixed[succvertex1] )
4067  {
4068  int succvertex2;
4069  int* succ2;
4070  int nsucc2;
4071  int j;
4072 
4073  nsucc2 = SCIPdigraphGetNSuccessors(conflictgraph, succvertex1);
4074  succ2 = SCIPdigraphGetSuccessors(conflictgraph, succvertex1);
4075 
4076  /* for the first unfixed vertex */
4077  if ( *ncoververtices == 0 )
4078  {
4079  for (j = 0; j < nsucc2; ++j)
4080  {
4081  succvertex2 = succ2[j];
4082  if ( ! verticesarefixed[succvertex2] )
4083  coververtices[(*ncoververtices)++] = succvertex2;
4084  }
4085  }
4086  else
4087  {
4088  int vv = 0;
4089  int k = 0;
4090  int v;
4091 
4092  /* determine all the successors that are in the set "coververtices" */
4093  for (v = 0; v < *ncoververtices; ++v)
4094  {
4095  assert( vv <= v );
4096  for (j = k; j < nsucc2; ++j)
4097  {
4098  succvertex2 = succ2[j];
4099  if ( succvertex2 > coververtices[v] )
4100  {
4101  /* coververtices[v] does not appear in succ2 list, go to next vertex in coververtices */
4102  k = j;
4103  break;
4104  }
4105  else if ( succvertex2 == coververtices[v] )
4106  {
4107  /* vertices are equal, copy to free position vv */
4108  coververtices[vv++] = succvertex2;
4109  k = j + 1;
4110  break;
4111  }
4112  }
4113  }
4114  /* store new size of coververtices */
4115  *ncoververtices = vv;
4116  }
4117  }
4118  }
4119 
4120 #ifdef SCIP_DEBUG
4121  /* check sorting */
4122  for (s = 0; s < *ncoververtices; ++s)
4123  {
4124  assert( *ncoververtices <= 1 || coververtices[*ncoververtices - 1] > coververtices[*ncoververtices - 2] );
4125  }
4126 #endif
4127 
4128  return SCIP_OKAY;
4129 }
4130 
4131 
4132 /** get vertices of variables that will be fixed to zero for each node */
4133 static
4135  SCIP* scip, /**< SCIP pointer */
4136  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
4137  SCIP_SOL* sol, /**< solution to be enforced (NULL for LP solution) */
4138  SCIP_Bool* verticesarefixed, /**< vector that indicates which variables are currently fixed to zero */
4139  SCIP_Bool bipbranch, /**< TRUE if bipartite branching method should be used */
4140  int branchvertex, /**< branching vertex */
4141  int* fixingsnode1, /**< vertices of variables that will be fixed to zero for the first node */
4142  int* nfixingsnode1, /**< pointer to store number of fixed variables for the first node */
4143  int* fixingsnode2, /**< vertices of variables that will be fixed to zero for the second node */
4144  int* nfixingsnode2 /**< pointer to store number of fixed variables for the second node */
4145  )
4146 {
4147  SCIP_Bool takeallsucc; /* whether to set fixingsnode1 = neighbors of 'branchvertex' in the conflict graph */
4148  int* succ;
4149  int nsucc;
4150  int j;
4151 
4152  assert( scip != NULL );
4153  assert( conflictgraph != NULL );
4154  assert( verticesarefixed != NULL );
4155  assert( ! verticesarefixed[branchvertex] );
4156  assert( fixingsnode1 != NULL );
4157  assert( fixingsnode2 != NULL );
4158  assert( nfixingsnode1 != NULL );
4159  assert( nfixingsnode2 != NULL );
4160 
4161  *nfixingsnode1 = 0;
4162  *nfixingsnode2 = 0;
4163  takeallsucc = TRUE;
4164 
4165  /* get successors and number of successors of branching vertex */
4166  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, branchvertex);
4167  succ = SCIPdigraphGetSuccessors(conflictgraph, branchvertex);
4168 
4169  /* if bipartite branching method is turned on */
4170  if ( bipbranch )
4171  {
4172  SCIP_Real solval;
4173  int cnt = 0;
4174 
4175  /* get all the neighbors of the variable with index 'branchvertex' whose solution value is nonzero */
4176  for (j = 0; j < nsucc; ++j)
4177  {
4178  if ( ! SCIPisFeasZero(scip, SCIPgetSolVal(scip, sol, SCIPnodeGetVarSOS1(conflictgraph, succ[j]))) )
4179  {
4180  assert( ! verticesarefixed[succ[j]] );
4181  fixingsnode1[(*nfixingsnode1)++] = succ[j];
4182  }
4183  }
4184 
4185  /* if one of the sets fixingsnode1 or fixingsnode2 contains only one variable with a nonzero LP value we perform standard neighborhood branching */
4186  if ( *nfixingsnode1 > 0 )
4187  {
4188  /* get the vertices whose neighbor set cover the selected subset of the neighbors of the given branching vertex */
4189  SCIP_CALL( getCoverVertices(conflictgraph, verticesarefixed, branchvertex, fixingsnode1, *nfixingsnode1, fixingsnode2, nfixingsnode2) );
4190 
4191  /* determine the intersection of the neighbors of branchvertex with the intersection of all the neighbors of fixingsnode2 */
4192  SCIP_CALL( getCoverVertices(conflictgraph, verticesarefixed, branchvertex, fixingsnode2, *nfixingsnode2, fixingsnode1, nfixingsnode1) );
4193 
4194  for (j = 0; j < *nfixingsnode2; ++j)
4195  {
4196  solval = SCIPgetSolVal(scip, sol, SCIPnodeGetVarSOS1(conflictgraph, fixingsnode2[j]));
4197  if( ! SCIPisFeasZero(scip, solval) )
4198  ++cnt;
4199  }
4200 
4201  /* we decide whether to use all successors if one partition of complete bipartite subgraph has only one node */
4202  if ( cnt >= 2 )
4203  {
4204  cnt = 0;
4205  for (j = 0; j < *nfixingsnode1; ++j)
4206  {
4207  solval = SCIPgetSolVal(scip, sol, SCIPnodeGetVarSOS1(conflictgraph, fixingsnode1[j]));
4208  if( ! SCIPisFeasZero(scip, solval) )
4209  ++cnt;
4210  }
4211 
4212  if ( cnt >= 2 )
4213  takeallsucc = FALSE;
4214  }
4215  }
4216  }
4217 
4218  if ( takeallsucc )
4219  {
4220  /* get all the unfixed neighbors of the branching vertex */
4221  *nfixingsnode1 = 0;
4222  for (j = 0; j < nsucc; ++j)
4223  {
4224  if ( ! verticesarefixed[succ[j]] )
4225  fixingsnode1[(*nfixingsnode1)++] = succ[j];
4226  }
4227 
4228  if ( bipbranch )
4229  {
4230  /* get the vertices whose neighbor set covers the neighbor set of a given branching vertex */
4231  SCIP_CALL( getCoverVertices(conflictgraph, verticesarefixed, branchvertex, fixingsnode1, *nfixingsnode1, fixingsnode2, nfixingsnode2) );
4232  }
4233  else
4234  {
4235  /* use neighborhood branching, i.e, for the second node only the branching vertex can be fixed */
4236  fixingsnode2[0] = branchvertex;
4237  *nfixingsnode2 = 1;
4238  }
4239  }
4240 
4241  return SCIP_OKAY;
4242 }
4243 
4244 
4245 /** gets branching priorities for SOS1 variables and applies 'most infeasible selection' rule to determine a vertex for the next branching decision */
4246 static
4248  SCIP* scip, /**< SCIP pointer */
4249  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
4250  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
4251  SCIP_SOL* sol, /**< solution to be enforced (NULL for LP solution) */
4252  int nsos1vars, /**< number of SOS1 variables */
4253  SCIP_Bool* verticesarefixed, /**< vector that indicates which variables are currently fixed to zero */
4254  SCIP_Bool bipbranch, /**< TRUE if bipartite branching method should be used */
4255  int* fixingsnode1, /**< vertices of variables that will be fixed to zero for the first node (size = nsos1vars) */
4256  int* fixingsnode2, /**< vertices of variables that will be fixed to zero for the second node (size = nsos1vars) */
4257  SCIP_Real* branchpriors, /**< pointer to store branching priorities (size = nsos1vars) or NULL if not needed */
4258  int* vertexbestprior, /**< pointer to store vertex with the best branching priority or NULL if not needed */
4259  SCIP_Bool* relsolfeas /**< pointer to store if LP relaxation solution is feasible */
4260  )
4261 {
4262  SCIP_Real bestprior;
4263  int i;
4264 
4265  assert( scip != NULL );
4266  assert( conshdlrdata != NULL );
4267  assert( conflictgraph != NULL );
4268  assert( verticesarefixed != NULL );
4269  assert( fixingsnode1 != NULL );
4270  assert( fixingsnode2 != NULL );
4271  assert( relsolfeas != NULL );
4272 
4273  bestprior = -SCIPinfinity(scip);
4274 
4275  for (i = 0; i < nsos1vars; ++i)
4276  {
4277  SCIP_Real prior;
4278  SCIP_Real solval;
4279  SCIP_Real sum1;
4280  SCIP_Real sum2;
4281  int nfixingsnode1;
4282  int nfixingsnode2;
4283  int nsucc;
4284  int j;
4285 
4286  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, i);
4287 
4288  if ( nsucc == 0 || SCIPisFeasZero(scip, SCIPgetSolVal(scip, sol, SCIPnodeGetVarSOS1(conflictgraph, i))) || verticesarefixed[i] )
4289  prior = -SCIPinfinity(scip);
4290  else
4291  {
4292  SCIP_Bool iszero1 = TRUE;
4293  SCIP_Bool iszero2 = TRUE;
4294 
4295  /* get vertices of variables that will be fixed to zero for each strong branching execution */
4296  assert( ! verticesarefixed[i] );
4297  SCIP_CALL( getBranchingVerticesSOS1(scip, conflictgraph, sol, verticesarefixed, bipbranch, i, fixingsnode1, &nfixingsnode1, fixingsnode2, &nfixingsnode2) );
4298 
4299  sum1 = 0.0;
4300  for (j = 0; j < nfixingsnode1; ++j)
4301  {
4302  solval = SCIPgetSolVal(scip, sol, SCIPnodeGetVarSOS1(conflictgraph, fixingsnode1[j]));
4303  if ( ! SCIPisFeasZero(scip, solval) )
4304  {
4305  sum1 += REALABS( solval );
4306  iszero1 = FALSE;
4307  }
4308  }
4309 
4310  sum2 = 0.0;
4311  for (j = 0; j < nfixingsnode2; ++j)
4312  {
4313  solval = SCIPgetSolVal(scip, sol, SCIPnodeGetVarSOS1(conflictgraph, fixingsnode2[j]));
4314  if ( ! SCIPisFeasZero(scip, solval) )
4315  {
4316  sum2 += REALABS( solval );
4317  iszero2 = FALSE;
4318  }
4319  }
4320 
4321  if ( iszero1 || iszero2 )
4322  prior = -SCIPinfinity(scip);
4323  else
4324  prior = sum1 * sum2;
4325  }
4326 
4327  if ( branchpriors != NULL )
4328  branchpriors[i] = prior;
4329  if ( bestprior < prior )
4330  {
4331  bestprior = prior;
4332 
4333  if ( vertexbestprior != NULL )
4334  *vertexbestprior = i;
4335  }
4336  }
4337 
4338  if ( SCIPisInfinity(scip, -bestprior) )
4339  *relsolfeas = TRUE;
4340  else
4341  *relsolfeas = FALSE;
4342 
4343  return SCIP_OKAY;
4344 }
4345 
4346 
4347 /** performs strong branching with given domain fixings */
4348 static
4350  SCIP* scip, /**< SCIP pointer */
4351  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
4352  int* fixingsexec, /**< vertices of variables to be fixed to zero for this strong branching execution */
4353  int nfixingsexec, /**< number of vertices of variables to be fixed to zero for this strong branching execution */
4354  int* fixingsop, /**< vertices of variables to be fixed to zero for the opposite strong branching execution */
4355  int nfixingsop, /**< number of vertices of variables to be fixed to zero for the opposite strong branching execution */
4356  int inititer, /**< maximal number of LP iterations to perform */
4357  SCIP_Bool fixnonzero, /**< shall opposite variable (if positive in sign) fixed to the feasibility tolerance
4358  * (only possible if nfixingsop = 1) */
4359  int* domainfixings, /**< vertices that can be used to reduce the domain (should have size equal to number of variables) */
4360  int* ndomainfixings, /**< pointer to store number of vertices that can be used to reduce the domain, could be filled by earlier calls */
4361  SCIP_Bool* infeasible, /**< pointer to store whether branch is infeasible */
4362  SCIP_Real* objval, /**< pointer to store objective value of LP with fixed variables (SCIP_INVALID if reddomain = TRUE or lperror = TRUE) */
4363  SCIP_Bool* lperror /**< pointer to store whether an unresolved LP error or a strange solution status occurred */
4364  )
4365 {
4366  SCIP_LPSOLSTAT solstat;
4367  int i;
4368 
4369  assert( scip != NULL );
4370  assert( conflictgraph != NULL );
4371  assert( fixingsexec != NULL );
4372  assert( nfixingsop > 0 );
4373  assert( fixingsop != NULL );
4374  assert( nfixingsop > 0 );
4375  assert( inititer >= -1 );
4376  assert( domainfixings != NULL );
4377  assert( ndomainfixings != NULL );
4378  assert( *ndomainfixings >= 0 );
4379  assert( infeasible != NULL );
4380  assert( objval != NULL );
4381  assert( lperror != NULL );
4382 
4383  *objval = SCIP_INVALID; /* for debugging */
4384  *lperror = FALSE;
4385  *infeasible = FALSE;
4386 
4387  /* start probing */
4388  SCIP_CALL( SCIPstartProbing(scip) );
4389 
4390  /* perform domain fixings */
4391  if ( fixnonzero && nfixingsop == 1 )
4392  {
4393  SCIP_VAR* var;
4394  SCIP_Real lb;
4395  SCIP_Real ub;
4396 
4397  var = SCIPnodeGetVarSOS1(conflictgraph, fixingsop[0]);
4398  lb = SCIPvarGetLbLocal(var);
4399  ub = SCIPvarGetUbLocal(var);
4400 
4402  {
4403  if ( SCIPisZero(scip, lb) )
4404  {
4405  /* fix variable to some very small, but positive number or to 1.0 if variable is integral */
4406  if (SCIPvarIsIntegral(var) )
4407  {
4408  SCIP_CALL( SCIPchgVarLbProbing(scip, var, 1.0) );
4409  }
4410  else
4411  {
4412  SCIP_CALL( SCIPchgVarLbProbing(scip, var, 1.5 * SCIPfeastol(scip)) );
4413  }
4414  }
4415  else if ( SCIPisZero(scip, ub) )
4416  {
4417  /* fix variable to some negative number with small absolute value or to -1.0 if variable is integral */
4418  if (SCIPvarIsIntegral(var) )
4419  {
4420  SCIP_CALL( SCIPchgVarUbProbing(scip, var, -1.0) );
4421  }
4422  else
4423  {
4424  SCIP_CALL( SCIPchgVarUbProbing(scip, var, -1.5 * SCIPfeastol(scip)) );
4425  }
4426  }
4427  }
4428  }
4429 
4430  /* injects variable fixings into current probing node */
4431  for (i = 0; i < nfixingsexec && ! *infeasible; ++i)
4432  {
4433  SCIP_VAR* var;
4434 
4435  var = SCIPnodeGetVarSOS1(conflictgraph, fixingsexec[i]);
4436  if ( SCIPisFeasGT(scip, SCIPvarGetLbLocal(var), 0.0) || SCIPisFeasLT(scip, SCIPvarGetUbLocal(var), 0.0) )
4437  *infeasible = TRUE;
4438  else
4439  {
4440  SCIP_CALL( SCIPfixVarProbing(scip, var, 0.0) );
4441  }
4442  }
4443 
4444  /* apply domain propagation */
4445  if ( ! *infeasible )
4446  {
4447  SCIP_CALL( SCIPpropagateProbing(scip, 0, infeasible, NULL) );
4448  }
4449 
4450  if ( *infeasible )
4451  solstat = SCIP_LPSOLSTAT_INFEASIBLE;
4452  else
4453  {
4454  /* solve the probing LP */
4455  SCIP_CALL( SCIPsolveProbingLP(scip, inititer, lperror, NULL) );
4456  if ( *lperror )
4457  {
4458  SCIP_CALL( SCIPendProbing(scip) );
4459  return SCIP_OKAY;
4460  }
4461 
4462  /* get solution status */
4463  solstat = SCIPgetLPSolstat(scip);
4464  }
4465 
4466  /* if objective limit was reached, then the domain can be reduced */
4467  if ( solstat == SCIP_LPSOLSTAT_OBJLIMIT || solstat == SCIP_LPSOLSTAT_INFEASIBLE )
4468  {
4469  *infeasible = TRUE;
4470 
4471  for (i = 0; i < nfixingsop; ++i)
4472  domainfixings[(*ndomainfixings)++] = fixingsop[i];
4473  }
4474  else if ( solstat == SCIP_LPSOLSTAT_OPTIMAL || solstat == SCIP_LPSOLSTAT_TIMELIMIT || solstat == SCIP_LPSOLSTAT_ITERLIMIT )
4475  {
4476  /* get objective value of probing LP */
4477  *objval = SCIPgetLPObjval(scip);
4478  }
4479  else
4480  *lperror = TRUE;
4481 
4482  /* end probing */
4483  SCIP_CALL( SCIPendProbing(scip) );
4484 
4485  return SCIP_OKAY;
4486 }
4487 
4488 
4489 /** apply strong branching to determine the vertex for the next branching decision */
4490 static
4492  SCIP* scip, /**< SCIP pointer */
4493  SCIP_CONSHDLRDATA* conshdlrdata, /**< SOS1 constraint handler data */
4494  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
4495  SCIP_SOL* sol, /**< solution to be enforced (NULL for LP solution) */
4496  int nsos1vars, /**< number of SOS1 variables */
4497  SCIP_Real lpobjval, /**< current LP relaxation solution */
4498  SCIP_Bool bipbranch, /**< TRUE if bipartite branching method should be used */
4499  int nstrongrounds, /**< number of strong branching rounds */
4500  SCIP_Bool* verticesarefixed, /**< vector that indicates which variables are currently fixed to zero */
4501  int* fixingsnode1, /**< pointer to store vertices of variables that will be fixed to zero for the first node (size = nsos1vars) */
4502  int* fixingsnode2, /**< pointer to store vertices of variables that will be fixed to zero for the second node (size = nsos1vars) */
4503  int* vertexbestprior, /**< pointer to store vertex with the best strong branching priority */
4504  SCIP_Real* bestobjval1, /**< pointer to store LP objective for left child node of branching decision with best priority */
4505  SCIP_Real* bestobjval2, /**< pointer to store LP objective for right child node of branching decision with best priority */
4506  SCIP_RESULT* result /**< pointer to store result of strong branching */
4507  )
4508 {
4509  SCIP_Real* branchpriors = NULL;
4510  int* indsos1vars = NULL;
4511  int* domainfixings = NULL;
4512  int ndomainfixings;
4513  int nfixingsnode1;
4514  int nfixingsnode2;
4515 
4516  SCIP_Bool relsolfeas;
4517  SCIP_Real bestscore;
4518  int lastscorechange;
4519  int maxfailures;
4520 
4521  SCIP_Longint nlpiterations;
4522  SCIP_Longint nlps;
4523  int inititer;
4524  int j;
4525  int i;
4526 
4527  assert( scip != NULL );
4528  assert( conshdlrdata != NULL );
4529  assert( conflictgraph != NULL );
4530  assert( verticesarefixed != NULL );
4531  assert( fixingsnode1 != NULL );
4532  assert( fixingsnode2 != NULL );
4533  assert( vertexbestprior != NULL );
4534  assert( result != NULL );
4535 
4536  /* allocate buffer arrays */
4537  SCIP_CALL( SCIPallocBufferArray(scip, &branchpriors, nsos1vars) );
4538 
4539  /* get branching priorities */
4540  SCIP_CALL( getBranchingPrioritiesSOS1(scip, conshdlrdata, conflictgraph, sol, nsos1vars, verticesarefixed,
4541  bipbranch, fixingsnode1, fixingsnode2, branchpriors, NULL, &relsolfeas) );
4542 
4543  /* if LP relaxation solution is feasible */
4544  if ( relsolfeas )
4545  {
4546  SCIPdebugMsg(scip, "all the SOS1 constraints are feasible.\n");
4547  *result = SCIP_FEASIBLE;
4548 
4549  /* free memory */
4550  SCIPfreeBufferArrayNull(scip, &branchpriors);
4551 
4552  return SCIP_OKAY;
4553  }
4554 
4555  /* allocate buffer arrays */
4556  SCIP_CALL( SCIPallocBufferArray(scip, &indsos1vars, nsos1vars) );
4557  SCIP_CALL( SCIPallocBufferArray(scip, &domainfixings, nsos1vars) );
4558 
4559  /* sort branching priorities (descending order) */
4560  for (j = 0; j < nsos1vars; ++j)
4561  indsos1vars[j] = j;
4562  SCIPsortDownRealInt(branchpriors, indsos1vars, nsos1vars);
4563 
4564  /* determine the number of LP iterations to perform in each strong branch */
4565  nlpiterations = SCIPgetNDualResolveLPIterations(scip);
4566  nlps = SCIPgetNDualResolveLPs(scip);
4567  if ( nlps == 0 )
4568  {
4569  nlpiterations = SCIPgetNNodeInitLPIterations(scip);
4570  nlps = SCIPgetNNodeInitLPs(scip);
4571  if ( nlps == 0 )
4572  {
4573  nlpiterations = 1000;
4574  nlps = 1;
4575  }
4576  }
4577  assert(nlps >= 1);
4578 
4579  /* compute number of LP iterations performed per strong branching iteration */
4580  if ( conshdlrdata->nstrongiter == -2 )
4581  {
4582  inititer = (int)(2*nlpiterations / nlps);
4583  inititer = (int)((SCIP_Real)inititer * (1.0 + 20.0/SCIPgetNNodes(scip)));
4584  inititer = MAX(inititer, 10);
4585  inititer = MIN(inititer, 500);
4586  }
4587  else
4588  inititer = conshdlrdata->nstrongiter;
4589 
4590  /* get current LP relaxation solution */
4591  lpobjval = SCIPgetLPObjval(scip);
4592 
4593  /* determine branching variable by strong branching or reduce domain */
4594  ndomainfixings = 0;
4595  lastscorechange = -1;
4596  *vertexbestprior = indsos1vars[0]; /* for the case that nstrongrounds = 0 */
4597  bestscore = -SCIPinfinity(scip);
4598  *bestobjval1 = -SCIPinfinity(scip);
4599  *bestobjval2 = -SCIPinfinity(scip);
4600  maxfailures = nstrongrounds;
4601 
4602  /* for each strong branching round */
4603  for (j = 0; j < nstrongrounds; ++j)
4604  {
4605  int testvertex;
4606 
4607  /* get branching vertex for the current strong branching iteration */
4608  testvertex = indsos1vars[j];
4609 
4610  /* if variable with index 'vertex' does not violate any complementarity in its neighborhood for the current LP relaxation solution */
4611  if ( SCIPisPositive(scip, branchpriors[j]) )
4612  {
4613  SCIP_Bool infeasible1;
4614  SCIP_Bool infeasible2;
4615  SCIP_Bool lperror;
4616  SCIP_Real objval1;
4617  SCIP_Real objval2;
4618  SCIP_Real score;
4619 
4620  /* get vertices of variables that will be fixed to zero for each strong branching execution */
4621  assert( ! verticesarefixed[testvertex] );
4622  SCIP_CALL( getBranchingVerticesSOS1(scip, conflictgraph, sol, verticesarefixed, bipbranch, testvertex, fixingsnode1, &nfixingsnode1, fixingsnode2, &nfixingsnode2) );
4623 
4624  /* get information for first strong branching execution */
4625  SCIP_CALL( performStrongbranchSOS1(scip, conflictgraph, fixingsnode1, nfixingsnode1, fixingsnode2, nfixingsnode2,
4626  inititer, conshdlrdata->fixnonzero, domainfixings, &ndomainfixings, &infeasible1, &objval1, &lperror) );
4627  if ( lperror )
4628  continue;
4629 
4630  /* get information for second strong branching execution */
4631  SCIP_CALL( performStrongbranchSOS1(scip, conflictgraph, fixingsnode2, nfixingsnode2, fixingsnode1, nfixingsnode1,
4632  inititer, FALSE, domainfixings, &ndomainfixings, &infeasible2, &objval2, &lperror) );
4633  if ( lperror )
4634  continue;
4635 
4636  /* if both subproblems are infeasible */
4637  if ( infeasible1 && infeasible2 )
4638  {
4639  SCIPdebugMsg(scip, "detected cutoff.\n");
4640 
4641  /* update result */
4642  *result = SCIP_CUTOFF;
4643 
4644  /* free memory */
4645  SCIPfreeBufferArrayNull(scip, &domainfixings);
4646  SCIPfreeBufferArrayNull(scip, &indsos1vars);
4647  SCIPfreeBufferArrayNull(scip, &branchpriors);
4648 
4649  return SCIP_OKAY;
4650  }
4651  else if ( ! infeasible1 && ! infeasible2 ) /* both subproblems are feasible */
4652  {
4653  /* if domain has not been reduced in this for-loop */
4654  if ( ndomainfixings == 0 )
4655  {
4656  score = MAX( REALABS( objval1 - lpobjval ), SCIPfeastol(scip) ) * MAX( REALABS( objval2 - lpobjval ), SCIPfeastol(scip) );/*lint !e666*/
4657 
4658  if ( SCIPisPositive(scip, score - bestscore) )
4659  {
4660  bestscore = score;
4661  *vertexbestprior = testvertex;
4662  *bestobjval1 = objval1;
4663  *bestobjval2 = objval2;
4664 
4665  lastscorechange = j;
4666  }
4667  else if ( j - lastscorechange > maxfailures )
4668  break;
4669  }
4670  }
4671  }
4672  }
4673 
4674  /* if variable fixings have been detected by probing, then reduce domain */
4675  if ( ndomainfixings > 0 )
4676  {
4677  SCIP_NODE* node = SCIPgetCurrentNode(scip);
4678  SCIP_Bool infeasible;
4679 
4680  for (i = 0; i < ndomainfixings; ++i)
4681  {
4682  SCIP_CALL( fixVariableZeroNode(scip, SCIPnodeGetVarSOS1(conflictgraph, domainfixings[i]), node, &infeasible) );
4683  assert( ! infeasible );
4684  }
4685 
4686  SCIPdebugMsg(scip, "found %d domain fixings.\n", ndomainfixings);
4687 
4688  /* update result */
4689  *result = SCIP_REDUCEDDOM;
4690  }
4691 
4692  /* free buffer arrays */
4693  SCIPfreeBufferArrayNull(scip, &domainfixings);
4694  SCIPfreeBufferArrayNull(scip, &indsos1vars);
4695  SCIPfreeBufferArrayNull(scip, &branchpriors);
4696 
4697  return SCIP_OKAY;
4698 }
4699 
4700 
4701 /** for two given vertices @p v1 and @p v2 search for a clique in the conflict graph that contains these vertices. From
4702  * this clique, we create a bound constraint.
4703  */
4704 static
4706  SCIP* scip, /**< SCIP pointer */
4707  SCIP_DIGRAPH* conflictgraph, /**< conflict graph */
4708  SCIP_SOL* sol, /**< solution to be enforced (NULL for LP solution) */
4709  int v1, /**< first vertex that shall be contained in bound constraint */
4710  int v2, /**< second vertex that shall be contained in bound constraint */
4711  SCIP_VAR* boundvar, /**< bound variable of @p v1 and @p v2 (or NULL if not existent) */
4712  SCIP_Bool extend, /**< should @p v1 and @p v2 be greedily extended to a clique of larger size */
4713  SCIP_CONS* cons, /**< bound constraint */
4714  SCIP_Real* feas /**< feasibility value of bound constraint */
4715  )
4716 {
4717  SCIP_NODEDATA* nodedata;
4718  SCIP_Bool addv2 = TRUE;
4719  SCIP_Real solval;
4720  SCIP_VAR* var;
4721  SCIP_Real coef = 0.0;
4722  int nsucc;
4723  int s;
4724 
4725  int* extensions = NULL;
4726  int nextensions = 0;
4727  int nextensionsnew;
4728  int* succ;
4729 
4730  assert( scip != NULL );
4731  assert( conflictgraph != NULL );
4732  assert( cons != NULL );
4733  assert( feas != NULL );
4734 
4735  *feas = 0.0;
4736 
4737  /* add index 'v1' to the clique */
4738  nodedata = (SCIP_NODEDATA*)SCIPdigraphGetNodeData(conflictgraph, v1);
4739  var = nodedata->var;
4740  assert( boundvar == NULL || SCIPvarCompare(boundvar, nodedata->ubboundvar) == 0 );
4741  solval = SCIPgetSolVal(scip, sol, var);
4742 
4743  /* if 'v1' and 'v2' have the same bound variable then the bound cut can be strengthened */
4744  if ( boundvar == NULL )
4745  {
4746  if ( SCIPisFeasPositive(scip, solval) )
4747  {
4748  SCIP_Real ub;
4749  ub = SCIPvarGetUbLocal(var);
4750  assert( SCIPisFeasPositive(scip, ub));
4751 
4752  if ( ! SCIPisInfinity(scip, ub) )
4753  coef = 1.0/ub;
4754  }
4755  else if ( SCIPisFeasNegative(scip, solval) )
4756  {
4757  SCIP_Real lb;
4758  lb = SCIPvarGetLbLocal(var);
4759  assert( SCIPisFeasNegative(scip, lb) );
4760  if ( ! SCIPisInfinity(scip, -lb) )
4761  coef = 1.0/lb;
4762  }
4763  }
4764  else if ( boundvar == nodedata->ubboundvar )
4765  {
4766  if ( SCIPisFeasPositive(scip, solval) )
4767  {
4768  SCIP_Real ub;
4769 
4770  ub = nodedata->ubboundcoef;
4771  assert( SCIPisFeasPositive(scip, ub) );
4772  if ( ! SCIPisInfinity(scip, ub) )
4773  coef = 1.0/ub;
4774  }
4775  else if ( SCIPisFeasNegative(scip, solval) )
4776  {
4777  SCIP_Real lb;
4778 
4779  lb = nodedata->lbboundcoef;
4780  assert( SCIPisFeasPositive(scip, lb) );
4781  if ( ! SCIPisInfinity(scip, lb) )
4782  coef = 1.0/lb;
4783  }
4784  }
4785 
4786  if ( ! SCIPisZero(scip, coef) )
4787  {
4788  *feas += coef * solval;
4789  SCIP_CALL( SCIPaddCoefLinear(scip, cons, var, coef) );
4790  }
4791 
4792  /* if clique shall be greedily extended to a clique of larger size */
4793  if ( extend )
4794  {
4795  /* get successors */
4796  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, v1);
4797  succ = SCIPdigraphGetSuccessors(conflictgraph, v1);
4798  assert( nsucc > 0 );
4799 
4800  /* allocate buffer array */
4801  SCIP_CALL( SCIPallocBufferArray(scip, &extensions, nsucc) );
4802 
4803  /* get possible extensions for the clique cover */
4804  for (s = 0; s < nsucc; ++s)
4805  extensions[s] = succ[s];
4806  nextensions = nsucc;
4807  }
4808  else
4809  nextensions = 1;
4810 
4811  /* while there exist possible extensions for the clique cover */
4812  while ( nextensions > 0 )
4813  {
4814  SCIP_Real bestbigMval;
4815  SCIP_Real bigMval;
4816  int bestindex = -1;
4817  int ext;
4818 
4819  bestbigMval = -SCIPinfinity(scip);
4820 
4821  /* if v2 has not been added to clique already */
4822  if ( addv2 )
4823  {
4824  bestindex = v2;
4825  addv2 = FALSE;
4826  }
4827  else /* search for the extension with the largest absolute value of its LP relaxation solution value */
4828  {
4829  assert( extensions != NULL );
4830  for (s = 0; s < nextensions; ++s)
4831  {
4832  ext = extensions[s];
4833  bigMval = nodeGetSolvalBinaryBigMSOS1(scip, conflictgraph, sol, ext);
4834  if ( SCIPisFeasLT(scip, bestbigMval, bigMval) )
4835  {
4836  bestbigMval = bigMval;
4837  bestindex = ext;
4838  }
4839  }
4840  }
4841  assert( bestindex != -1 );
4842 
4843  /* add bestindex variable to the constraint */
4844  nodedata = (SCIP_NODEDATA*)SCIPdigraphGetNodeData(conflictgraph, bestindex);
4845  var = nodedata->var;
4846  solval = SCIPgetSolVal(scip, sol, var);
4847  coef = 0.0;
4848  if ( boundvar == NULL )
4849  {
4850  if ( SCIPisFeasPositive(scip, solval) )
4851  {
4852  SCIP_Real ub;
4853  ub = SCIPvarGetUbLocal(var);
4854  assert( SCIPisFeasPositive(scip, ub));
4855 
4856  if ( ! SCIPisInfinity(scip, ub) )
4857  coef = 1.0/ub;
4858  }
4859  else if ( SCIPisFeasNegative(scip, solval) )
4860  {
4861  SCIP_Real lb;
4862  lb = SCIPvarGetLbLocal(var);
4863  assert( SCIPisFeasNegative(scip, lb) );
4864  if ( ! SCIPisInfinity(scip, -lb) )
4865  coef = 1.0/lb;
4866  }
4867  }
4868  else if ( boundvar == nodedata->ubboundvar )
4869  {
4870  if ( SCIPisFeasPositive(scip, solval) )
4871  {
4872  SCIP_Real ub;
4873 
4874  ub = nodedata->ubboundcoef;
4875  assert( SCIPisFeasPositive(scip, ub) );
4876  if ( ! SCIPisInfinity(scip, ub) )
4877  coef = 1.0/ub;
4878  }
4879  else if ( SCIPisFeasNegative(scip, solval) )
4880  {
4881  SCIP_Real lb;
4882 
4883  lb = nodedata->lbboundcoef;
4884  assert( SCIPisFeasPositive(scip, lb) );
4885  if ( ! SCIPisInfinity(scip, -lb) )
4886  coef = 1.0/lb;
4887  }
4888  }
4889  if ( ! SCIPisZero(scip, coef) )
4890  {
4891  *feas += coef * solval;
4892  SCIP_CALL( SCIPaddCoefLinear(scip, cons, var, coef) );
4893  }
4894 
4895  if ( extend )
4896  {
4897  assert( extensions != NULL );
4898  /* compute new 'extensions' array */
4899  nextensionsnew = 0;
4900  for (s = 0; s < nextensions; ++s)
4901  {
4902  if ( s != bestindex && isConnectedSOS1(NULL, conflictgraph, bestindex, extensions[s]) )
4903  extensions[nextensionsnew++] = extensions[s];
4904  }
4905  nextensions = nextensionsnew;
4906  }
4907  else
4908  nextensions = 0;
4909  }
4910 
4911  /* free buffer array */
4912  if ( extend )
4913  SCIPfreeBufferArray(scip, &extensions);
4914 
4915  /* subtract rhs of constraint from feasibility value or add bound variable if existent */
4916  if ( boundvar == NULL )
4917  *feas -= 1.0;
4918  else
4919  {
4920  SCIP_CALL( SCIPaddCoefLinear(scip, cons, boundvar, -1.0) );
4921  *feas -= SCIPgetSolVal(scip, sol, boundvar);
4922  }
4923 
4924  return SCIP_OKAY;
4925 }
4926 
4927 
4928 /** tries to add feasible complementarity constraints to a given child branching node.
4929  *
4930  * @note In this function the conflict graph is updated to the conflict graph of the considered child branching node.
4931  */
4932 static
4934  SCIP* scip, /**< SCIP pointer */
4935  SCIP_NODE* node, /**< branching node */
4936  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
4937  SCIP_DIGRAPH* conflictgraph, /**< conflict graph of the current node */
4938  SCIP_DIGRAPH* localconflicts, /**< local conflicts (updates to local conflicts of child node) */
4939  SCIP_SOL* sol, /**< solution to be enforced (NULL for LP solution) */
4940  int nsos1vars, /**< number of SOS1 variables */
4941  SCIP_Bool* verticesarefixed, /**< vector that indicates which variables are currently fixed to zerox */
4942  int* fixingsnode1, /**< vertices of variables that will be fixed to zero for the branching node in the input of this function */
4943  int nfixingsnode1, /**< number of entries of array nfixingsnode1 */
4944  int* fixingsnode2, /**< vertices of variables that will be fixed to zero for the other branching node */
4945  int nfixingsnode2, /**< number of entries of array nfixingsnode2 */
4946  int* naddedconss, /**< pointer to store the number of added SOS1 constraints */
4947  SCIP_Bool onlyviolsos1 /**< should only SOS1 constraints be added that are violated by the LP solution */
4948  )
4949 {
4950  assert( scip != NULL );
4951  assert( node != NULL );
4952  assert( conshdlrdata != NULL );
4953  assert( conflictgraph != NULL );
4954  assert( verticesarefixed != NULL );
4955  assert( fixingsnode1 != NULL );
4956  assert( fixingsnode2 != NULL );
4957  assert( naddedconss != NULL );
4958 
4959  *naddedconss = 0;
4960 
4961  if ( nfixingsnode2 > 1 )
4962  {
4963  int* fixingsnode21; /* first partition of fixingsnode2 */
4964  int* fixingsnode22; /* second partition of fixingsnode2 */
4965  int nfixingsnode21;
4966  int nfixingsnode22;
4967 
4968  int* coverarray; /* vertices, not in fixingsnode1 that cover all the vertices in array fixingsnode22 */
4969  int ncoverarray;
4970 
4971  SCIP_Bool* mark;
4972  int* succarray;
4973  int nsuccarray;
4974  int* succ;
4975  int nsucc;
4976 
4977  int i;
4978  int s;
4979 
4980  /* allocate buffer arrays */
4981  SCIP_CALL( SCIPallocBufferArray(scip, &succarray, nsos1vars) );
4982  SCIP_CALL( SCIPallocBufferArray(scip, &mark, nsos1vars) );
4983  SCIP_CALL( SCIPallocBufferArray(scip, &fixingsnode21, nfixingsnode2) );
4984  SCIP_CALL( SCIPallocBufferArray(scip, &fixingsnode22, nfixingsnode2) );
4985 
4986  /* mark all the unfixed vertices with FALSE */
4987  for (i = 0; i < nsos1vars; ++i)
4988  mark[i] = (verticesarefixed[i]);
4989 
4990  /* mark all the vertices that are in the set fixingsnode1 */
4991  for (i = 0; i < nfixingsnode1; ++i)
4992  {
4993  assert( nfixingsnode1 <= 1 || (fixingsnode1[nfixingsnode1 - 1] > fixingsnode1[nfixingsnode1 - 2]) ); /* test: vertices are sorted */
4994  mark[fixingsnode1[i]] = TRUE;
4995  }
4996 
4997  /* mark all the vertices that are in the set fixingsnode2 */
4998  for (i = 0; i < nfixingsnode2; ++i)
4999  {
5000  assert( nfixingsnode2 <= 1 || (fixingsnode2[nfixingsnode2 - 1] > fixingsnode2[nfixingsnode2 - 2]) ); /* test: vertices are sorted */
5001  mark[fixingsnode2[i]] = TRUE;
5002  }
5003 
5004  /* compute the set of vertices that have a neighbor in the set fixingsnode2, but are not in the set fixingsnode1 or fixingsnode2 and are not already fixed */
5005  nsuccarray = 0;
5006  for (i = 0; i < nfixingsnode2; ++i)
5007  {
5008  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, fixingsnode2[i]);
5009  succ = SCIPdigraphGetSuccessors(conflictgraph, fixingsnode2[i]);
5010 
5011  for (s = 0; s < nsucc; ++s)
5012  {
5013  int succnode = succ[s];
5014 
5015  if ( ! mark[succnode] )
5016  {
5017  mark[succnode] = TRUE;
5018  succarray[nsuccarray++] = succnode;
5019  }
5020  }
5021  }
5022 
5023  /* allocate buffer array */
5024  SCIP_CALL( SCIPallocBufferArray(scip, &coverarray, nsos1vars) );
5025 
5026  /* mark all the vertices with FALSE */
5027  for (i = 0; i < nsos1vars; ++i)
5028  mark[i] = FALSE;
5029 
5030  /* mark all the vertices that are in the set fixingsnode2 */
5031  for (i = 0; i < nfixingsnode2; ++i)
5032  mark[fixingsnode2[i]] = TRUE;
5033 
5034  /* for every node in succarray */
5035  for (i = 0; i < nsuccarray; ++i)
5036  {
5037  SCIP_Real solval1;
5038  SCIP_VAR* var1;
5039  int vertex1;
5040  int j;
5041 
5042  vertex1 = succarray[i];
5043  var1 = SCIPnodeGetVarSOS1(conflictgraph, vertex1);
5044  solval1 = SCIPgetSolVal(scip, sol, var1);
5045 
5046  /* we only add complementarity constraints if they are violated by the current LP solution */
5047  if ( ! onlyviolsos1 || ! SCIPisFeasZero(scip, solval1) )
5048  {
5049  /* compute first partition of fixingsnode2 that is the intersection of the neighbors of 'vertex1' with the set fixingsnode2 */
5050  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, vertex1);
5051  succ = SCIPdigraphGetSuccessors(conflictgraph, vertex1);
5052  nfixingsnode21 = 0;
5053 
5054  for (s = 0; s < nsucc; ++s)
5055  {
5056  if ( mark[succ[s]] )
5057  {
5058  fixingsnode21[nfixingsnode21++] = succ[s];
5059  assert( nfixingsnode21 == 1 || (fixingsnode21[nfixingsnode21 - 1] > fixingsnode21[nfixingsnode21 - 2]) ); /* test: successor vertices are sorted */
5060  }
5061  }
5062 
5063  /* if variable can be fixed to zero */
5064  if ( nfixingsnode21 == nfixingsnode2 )
5065  {
5066  SCIP_Bool infeasible;
5067 
5068  SCIP_CALL( fixVariableZeroNode(scip, var1, node, &infeasible) );
5069  assert( ! infeasible );
5070  continue;
5071  }
5072 
5073  /* compute second partition of fixingsnode2 (that is fixingsnode2 \setminus fixingsnode21 ) */
5074  SCIP_CALL( SCIPcomputeArraysSetminus(fixingsnode2, nfixingsnode2, fixingsnode21, nfixingsnode21, fixingsnode22, &nfixingsnode22) );
5075  assert ( nfixingsnode22 + nfixingsnode21 == nfixingsnode2 );
5076 
5077  /* compute cover set (that are all the vertices not in fixingsnode1 and fixingsnode21, whose neighborhood covers all the vertices of fixingsnode22) */
5078  SCIP_CALL( getCoverVertices(conflictgraph, verticesarefixed, -1, fixingsnode22, nfixingsnode22, coverarray, &ncoverarray) );
5079  SCIP_CALL( SCIPcomputeArraysSetminus(coverarray, ncoverarray, fixingsnode1, nfixingsnode1, coverarray, &ncoverarray) );
5080  SCIP_CALL( SCIPcomputeArraysSetminus(coverarray, ncoverarray, fixingsnode21, nfixingsnode21, coverarray, &ncoverarray) );
5081 
5082  for (j = 0; j < ncoverarray; ++j)
5083  {
5084  int vertex2;
5085 
5086  vertex2 = coverarray[j];
5087  assert( vertex2 != vertex1 );
5088 
5089  /* prevent double enumeration */
5090  if ( vertex2 < vertex1 )
5091  {
5092  SCIP_VAR* var2;
5093  SCIP_Real solval2;
5094 
5095  var2 = SCIPnodeGetVarSOS1(conflictgraph, vertex2);
5096  solval2 = SCIPgetSolVal(scip, sol, var2);
5097 
5098  if ( onlyviolsos1 && ( SCIPisFeasZero(scip, solval1) || SCIPisFeasZero(scip, solval2) ) )
5099  continue;
5100 
5101  if ( ! isConnectedSOS1(NULL, conflictgraph, vertex1, vertex2) )
5102  {
5103  char name[SCIP_MAXSTRLEN];
5104  SCIP_CONS* conssos1 = NULL;
5105  SCIP_Bool takebound = FALSE;
5106  SCIP_Real feas;
5107 
5108  SCIP_NODEDATA* nodedata;
5109  SCIP_Real lbboundcoef1;
5110  SCIP_Real lbboundcoef2;
5111  SCIP_Real ubboundcoef1;
5112  SCIP_Real ubboundcoef2;
5113  SCIP_VAR* boundvar1;
5114  SCIP_VAR* boundvar2;
5115 
5116  /* get bound variables if available */
5117  nodedata = (SCIP_NODEDATA*)SCIPdigraphGetNodeData(conflictgraph, vertex1);
5118  assert( nodedata != NULL );
5119  boundvar1 = nodedata->ubboundvar;
5120  lbboundcoef1 = nodedata->lbboundcoef;
5121  ubboundcoef1 = nodedata->ubboundcoef;
5122  nodedata = (SCIP_NODEDATA*)SCIPdigraphGetNodeData(conflictgraph, vertex2);
5123  assert( nodedata != NULL );
5124  boundvar2 = nodedata->ubboundvar;
5125  lbboundcoef2 = nodedata->lbboundcoef;
5126  ubboundcoef2 = nodedata->ubboundcoef;
5127 
5128  if ( boundvar1 != NULL && boundvar2 != NULL && SCIPvarCompare(boundvar1, boundvar2) == 0 )
5129  takebound = TRUE;
5130 
5131  /* add new arc to local conflicts in order to generate tighter bound inequalities */
5132  if ( conshdlrdata->addextendedbds )
5133  {
5134  if ( localconflicts == NULL )
5135  {
5136  SCIP_CALL( SCIPdigraphCreate(&conshdlrdata->localconflicts, SCIPblkmem(scip), nsos1vars) );
5137  localconflicts = conshdlrdata->localconflicts;
5138  }
5139  SCIP_CALL( SCIPdigraphAddArc(localconflicts, vertex1, vertex2, NULL) );
5140  SCIP_CALL( SCIPdigraphAddArc(localconflicts, vertex2, vertex1, NULL) );
5141  SCIP_CALL( SCIPdigraphAddArc(conflictgraph, vertex1, vertex2, NULL) );
5142  SCIP_CALL( SCIPdigraphAddArc(conflictgraph, vertex2, vertex1, NULL) );
5143 
5144  /* can sort successors in place - do not use arcdata */
5145  SCIPsortInt(SCIPdigraphGetSuccessors(localconflicts, vertex1), SCIPdigraphGetNSuccessors(localconflicts, vertex1));
5146  SCIPsortInt(SCIPdigraphGetSuccessors(localconflicts, vertex2), SCIPdigraphGetNSuccessors(localconflicts, vertex2));
5147  SCIPsortInt(SCIPdigraphGetSuccessors(conflictgraph, vertex1), SCIPdigraphGetNSuccessors(conflictgraph, vertex1));
5148  SCIPsortInt(SCIPdigraphGetSuccessors(conflictgraph, vertex2), SCIPdigraphGetNSuccessors(conflictgraph, vertex2));
5149 
5150  /* mark conflictgraph as not local such that the new arcs are deleted after currents node processing */
5151  conshdlrdata->isconflocal = TRUE;
5152  }
5153 
5154  /* measure feasibility of complementarity between var1 and var2 */
5155  if ( ! takebound )
5156  {
5157  feas = -1.0;
5158  if ( SCIPisFeasPositive(scip, solval1) )
5159  {
5160  assert( SCIPisFeasPositive(scip, SCIPvarGetUbLocal(var1)));
5161  if ( ! SCIPisInfinity(scip, SCIPvarGetUbLocal(var1)) )
5162  feas += solval1/SCIPvarGetUbLocal(var1);
5163  }
5164  else if ( SCIPisFeasNegative(scip, solval1) )
5165  {
5166  assert( SCIPisFeasPositive(scip, SCIPvarGetLbLocal(var1)));
5167  if ( ! SCIPisInfinity(scip, -SCIPvarGetLbLocal(var1)) )
5168  feas += solval1/SCIPvarGetLbLocal(var1);
5169  }
5170 
5171  if ( SCIPisFeasPositive(scip, solval2) )
5172  {
5173  assert( SCIPisFeasPositive(scip, SCIPvarGetUbLocal(var2)));
5174  if ( ! SCIPisInfinity(scip, SCIPvarGetUbLocal(var2)) )
5175  feas += solval2/SCIPvarGetUbLocal(var2);
5176  }
5177  else if ( SCIPisFeasNegative(scip, solval2) )
5178  {
5179  assert( SCIPisFeasPositive(scip, SCIPvarGetLbLocal(var2)));
5180  if ( ! SCIPisInfinity(scip, -SCIPvarGetLbLocal(var2)) )
5181  feas += solval2/SCIPvarGetLbLocal(var2);
5182  }
5183  }
5184  else
5185  {
5186  feas = -SCIPgetSolVal(scip, sol, boundvar1);
5187  if ( SCIPisFeasPositive(scip, solval1) )
5188  {
5189  assert( SCIPisFeasPositive(scip, ubboundcoef1));
5190  if ( ! SCIPisInfinity(scip, ubboundcoef1) )
5191  feas += solval1/ubboundcoef1;
5192  }
5193  else if ( SCIPisFeasNegative(scip, solval1) )
5194  {
5195  assert( SCIPisFeasPositive(scip, lbboundcoef1));
5196  if ( ! SCIPisInfinity(scip, -lbboundcoef1) )
5197  feas += solval1/lbboundcoef1;
5198  }
5199 
5200  if ( SCIPisFeasPositive(scip, solval2) )
5201  {
5202  assert( SCIPisFeasPositive(scip, ubboundcoef2));
5203  if ( ! SCIPisInfinity(scip, ubboundcoef2) )
5204  feas += solval2/ubboundcoef2;
5205  }
5206  else if ( SCIPisFeasNegative(scip, solval2) )
5207  {
5208  assert( SCIPisFeasPositive(scip, lbboundcoef2));
5209  if ( ! SCIPisInfinity(scip, -lbboundcoef2) )
5210  feas += solval2/lbboundcoef2;
5211  }
5212  assert( ! SCIPisFeasNegative(scip, solval2) );
5213  }
5214 
5215  if ( SCIPisGT(scip, feas, conshdlrdata->addcompsfeas) )
5216  {
5217  /* create SOS1 constraint */
5218  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "sos1_branchnode_%i_no_%i", SCIPnodeGetNumber(node), *naddedconss);
5219  SCIP_CALL( SCIPcreateConsSOS1(scip, &conssos1, name, 0, NULL, NULL, TRUE, TRUE, TRUE, FALSE, TRUE,
5220  TRUE, FALSE, FALSE, FALSE) );
5221 
5222  /* add variables to SOS1 constraint */
5223  SCIP_CALL( addVarSOS1(scip, conssos1, conshdlrdata, var1, 1.0) );
5224  SCIP_CALL( addVarSOS1(scip, conssos1, conshdlrdata, var2, 2.0) );
5225 
5226  /* add SOS1 constraint to the branching node */
5227  SCIP_CALL( SCIPaddConsNode(scip, node, conssos1, NULL) );
5228  ++(*naddedconss);
5229 
5230  /* release constraint */
5231  SCIP_CALL( SCIPreleaseCons(scip, &conssos1) );
5232  }
5233 
5234  /* add bound inequality*/
5235  if ( ! SCIPisFeasZero(scip, solval1) && ! SCIPisFeasZero(scip, solval2) )
5236  {
5237  /* possibly create linear constraint of the form x_i/u_i + x_j/u_j <= t if a bound variable t with x_i <= u_i * t and x_j <= u_j * t exists.
5238  * Otherwise try to create a constraint of the form x_i/u_i + x_j/u_j <= 1. Try the same for the lower bounds. */
5239  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "boundcons_branchnode_%i_no_%i", SCIPnodeGetNumber(node), *naddedconss);
5240  if ( takebound )
5241  {
5242  /* create constraint with right hand side = 0.0 */
5243  SCIP_CALL( SCIPcreateConsLinear(scip, &conssos1, name, 0, NULL, NULL, -SCIPinfinity(scip), 0.0, TRUE, FALSE, TRUE, FALSE, FALSE,
5244  TRUE, FALSE, FALSE, FALSE, FALSE) );
5245 
5246  /* add variables */
5247  SCIP_CALL( getBoundConsFromVertices(scip, conflictgraph, sol, vertex1, vertex2, boundvar1, conshdlrdata->addextendedbds, conssos1, &feas) );
5248  }
5249  else
5250  {
5251  /* create constraint with right hand side = 1.0 */
5252  SCIP_CALL( SCIPcreateConsLinear(scip, &conssos1, name, 0, NULL, NULL, -SCIPinfinity(scip), 1.0, TRUE, FALSE, TRUE, FALSE, FALSE,
5253  TRUE, FALSE, FALSE, FALSE, FALSE) );
5254 
5255  /* add variables */
5256  SCIP_CALL( getBoundConsFromVertices(scip, conflictgraph, sol, vertex1, vertex2, NULL, conshdlrdata->addextendedbds, conssos1, &feas) );
5257  }
5258 
5259  /* add linear constraint to the branching node if usefull */
5260  if ( SCIPisGT(scip, feas, conshdlrdata->addbdsfeas ) )
5261  {
5262  SCIP_CALL( SCIPaddConsNode(scip, node, conssos1, NULL) );
5263  ++(*naddedconss);
5264  }
5265 
5266  /* release constraint */
5267  SCIP_CALL( SCIPreleaseCons(scip, &conssos1) );
5268  }
5269 
5270  /* break if number of added constraints exceeds a predefined value */
5271  if ( conshdlrdata->maxaddcomps >= 0 && *naddedconss > conshdlrdata->maxaddcomps )
5272  break;
5273  }
5274  }
5275  }
5276  }
5277 
5278  /* break if number of added constraints exceeds a predefined value */
5279  if ( conshdlrdata->maxaddcomps >= 0 && *naddedconss > conshdlrdata->maxaddcomps )
5280  break;
5281  }
5282 
5283  /* free buffer array */
5284  SCIPfreeBufferArray(scip, &coverarray);
5285  SCIPfreeBufferArray(scip, &fixingsnode22);
5286  SCIPfreeBufferArray(scip, &fixingsnode21);
5287  SCIPfreeBufferArray(scip, &mark);
5288  SCIPfreeBufferArray(scip, &succarray);
5289  }
5290 
5291  return SCIP_OKAY;
5292 }
5293 
5294 
5295 /** resets local conflict graph to the conflict graph of the root node */
5296 static
5298  SCIP_DIGRAPH* conflictgraph, /**< conflict graph of root node */
5299  SCIP_DIGRAPH* localconflicts, /**< local conflicts that should be removed from conflict graph */
5300  int nsos1vars /**< number of SOS1 variables */
5301  )
5302 {
5303  int j;
5304 
5305  for (j = 0; j < nsos1vars; ++j)
5306  {
5307  int nsuccloc;
5308 
5309  nsuccloc = SCIPdigraphGetNSuccessors(localconflicts, j);
5310  if ( nsuccloc > 0 )
5311  {
5312  int* succloc;
5313  int* succ;
5314  int nsucc;
5315  int k = 0;
5316 
5317  succloc = SCIPdigraphGetSuccessors(localconflicts, j);
5318  succ = SCIPdigraphGetSuccessors(conflictgraph, j);
5319  nsucc = SCIPdigraphGetNSuccessors(conflictgraph, j);
5320 
5321  /* reset number of successors */
5322  SCIP_CALL( SCIPcomputeArraysSetminus(succ, nsucc, succloc, nsuccloc, succ, &k) );
5323  SCIP_CALL( SCIPdigraphSetNSuccessors(conflictgraph, j, k) );
5324  SCIP_CALL( SCIPdigraphSetNSuccessors(localconflicts, j, 0) );
5325  }
5326  }
5327 
5328  return SCIP_OKAY;
5329 }
5330 
5331 
5332 /** Conflict graph enforcement method
5333  *
5334  * The conflict graph can be enforced by different branching rules:
5335  *
5336  * - Branch on the neighborhood of a single variable @p i, i.e., in one branch \f$x_i\f$ is fixed to zero and in the
5337  * other its neighbors from the conflict graph.
5338  *
5339  * - Branch on complete bipartite subgraphs of the conflict graph, i.e., in one branch fix the variables from the first
5340  * bipartite partition and the variables from the second bipartite partition in the other.
5341  *
5342  * - In addition to variable domain fixings, it is sometimes also possible to add new SOS1 constraints to the branching
5343  * nodes. This results in a nonstatic conflict graph, which may change dynamically with every branching node.
5344  *
5345  * We make use of different selection rules that define on which system of SOS1 variables to branch next:
5346  *
5347  * - Most infeasible branching: Branch on the system of SOS1 variables with largest violation.
5348  *
5349  * - Strong branching: Here, the LP-relaxation is partially solved for each branching decision among a candidate list.
5350  * Then the decision with best progress is chosen.
5351  */
5352 static
5354  SCIP* scip, /**< SCIP pointer */
5355  SCIP_CONSHDLRDATA* conshdlrdata, /**< constraint handler data */
5356  SCIP_CONSHDLR* conshdlr, /**< constraint handler */
5357  int nconss, /**< number of constraints */
5358  SCIP_CONS** conss, /**< SOS1 constraints */
5359  SCIP_SOL* sol, /**< solution to be enforced (NULL for LP solution) */
5360  SCIP_RESULT* result /**< result */
5361  )
5362 {
5363  SCIP_DIGRAPH* conflictgraph;
5364  int nsos1vars;
5365 
5366  SCIP_Bool* verticesarefixed = NULL;
5367  int* fixingsnode1 = NULL;
5368  int* fixingsnode2 = NULL;
5369  int nfixingsnode1;
5370  int nfixingsnode2;
5371 
5372  SCIP_Real bestobjval1 = -SCIPinfinity(scip);
5373  SCIP_Real bestobjval2 = -SCIPinfinity(scip);
5374  SCIP_Real lpobjval = -SCIPinfinity(scip);
5375 
5376  SCIP_Bool infeasible;
5377  SCIP_Bool bipbranch = FALSE;
5378  int nstrongrounds;
5379 
5380  int branchvertex;
5381  SCIP_NODE* node1;
5382  SCIP_NODE* node2;
5383  SCIP_Real nodeselest;
5384  SCIP_Real objest;
5385 
5386  int i;
5387  int j;
5388  int c;
5389 
5390  assert( scip != NULL );
5391  assert( conshdlrdata != NULL );
5392  assert( conshdlr != NULL );
5393  assert( conss != NULL );
5394  assert( result != NULL );
5395 
5396  SCIPdebugMsg(scip, "Enforcing SOS1 conflict graph <%s>.\n", SCIPconshdlrGetName(conshdlr) );
5397  *result = SCIP_DIDNOTRUN;
5398 
5399  /* get number of SOS1 variables */
5400  nsos1vars = conshdlrdata->nsos1vars;
5401 
5402  /* get conflict graph */
5403  conflictgraph = conshdlrdata->conflictgraph;
5404  assert( ! conshdlrdata->isconflocal ); /* conflictgraph should be the one of the root node */
5405 
5406  /* check each constraint and update conflict graph if necessary */
5407  for (c = 0; c < nconss; ++c)
5408  {
5409  SCIP_CONSDATA* consdata;
5410  SCIP_CONS* cons;
5411  SCIP_Bool cutoff;int ngen;
5412 
5413  cons = conss[c];
5414  assert( cons != NULL );
5415  consdata = SCIPconsGetData(cons);
5416  assert( consdata != NULL );
5417 
5418  /* do nothing if there are not enough variables - this is usually eliminated by preprocessing */
5419  if ( consdata->nvars < 2 )
5420  continue;
5421 
5422  /* first perform propagation (it might happen that standard propagation is turned off) */
5423  ngen = 0;
5424  SCIP_CALL( propConsSOS1(scip, cons, consdata, &cutoff, &ngen) );
5425  SCIPdebugMsg(scip, "propagating <%s> in enforcing (cutoff: %u, domain reductions: %d).\n", SCIPconsGetName(cons), cutoff, ngen);
5426  if ( cutoff )
5427  {
5428  *result = SCIP_CUTOFF;
5429  break;
5430  }
5431  if ( ngen > 0 )
5432  {
5433  *result = SCIP_REDUCEDDOM;
5434  break;
5435  }
5436  assert( ngen == 0 );
5437 
5438  /* add local conflicts to conflict graph and save them in 'localconflicts' */
5439  if ( consdata->local )
5440  {
5441  SCIP_VAR** vars;
5442  int nvars;
5443  int indi;
5444  int indj;
5445 
5446  if ( conshdlrdata->localconflicts == NULL )
5447  {
5448  SCIP_CALL( SCIPdigraphCreate(&conshdlrdata->localconflicts, SCIPblkmem(scip), nsos1vars ) );
5449  }
5450 
5451  vars = consdata->vars;
5452  nvars = consdata->nvars;
5453  for (i = 0; i < nvars-1; ++i)
5454  {
5455  SCIP_VAR* var;
5456 
5457  var = vars[i];
5458  indi = varGetNodeSOS1(conshdlrdata, var);
5459 
5460  if( indi == -1 )
5461  return SCIP_INVALIDDATA;
5462 
5463  if ( ! SCIPisFeasZero(scip, SCIPvarGetUbLocal(var)) || ! SCIPisFeasZero(scip, SCIPvarGetLbLocal(var)) )
5464  {
5465  for (j = i+1; j < nvars; ++j)
5466  {
5467  var = vars[j];
5468  indj = varGetNodeSOS1(conshdlrdata, var);
5469 
5470  if( indj == -1 )
5471  return SCIP_INVALIDDATA;
5472 
5473  if ( ! SCIPisFeasZero(scip, SCIPvarGetUbLocal(var)) || ! SCIPisFeasZero(scip, SCIPvarGetLbLocal(var)) )
5474  {
5475  if ( ! isConnectedSOS1(NULL, conflictgraph, indi, indj) )
5476  {
5477  SCIP_CALL( SCIPdigraphAddArcSafe(conflictgraph, indi, indj, NULL) );
5478  SCIP_CALL( SCIPdigraphAddArcSafe(conflictgraph, indj, indi, NULL) );
5479 
5480  SCIP_CALL( SCIPdigraphAddArcSafe(conshdlrdata->localconflicts, indi, indj, NULL) );
5481  SCIP_CALL( SCIPdigraphAddArcSafe(conshdlrdata->localconflicts, indj, indi, NULL) );
5482 
5483  conshdlrdata->isconflocal = TRUE;
5484  }
5485  }
5486  }
5487  }
5488  }
5489  }
5490  }
5491 
5492  /* sort successor list of conflict graph if necessary */
5493  if ( conshdlrdata->isconflocal )
5494  {
5495  for (j = 0; j < nsos1vars; ++j)
5496  {
5497  int nsuccloc;
5498 
5499  nsuccloc = SCIPdigraphGetNSuccessors(conshdlrdata->localconflicts, j);
5500  if ( nsuccloc > 0 )
5501  {
5502  SCIPsortInt(SCIPdigraphGetSuccessors(conflictgraph, j), SCIPdigraphGetNSuccessors(conflictgraph, j));
5503  SCIPsortInt(SCIPdigraphGetSuccessors(conshdlrdata->localconflicts, j), nsuccloc);
5504  }
5505  }
5506  }
5507 
5508  if ( *result == SCIP_CUTOFF || *result == SCIP_REDUCEDDOM )
5509  {
5510  /* remove local conflicts from conflict graph */
5511  if ( conshdlrdata->isconflocal )
5512  {
5513  SCIP_CALL( resetConflictgraphSOS1(conflictgraph, conshdlrdata->localconflicts, nsos1vars) );
5514  conshdlrdata->isconflocal = FALSE;
5515  }
5516  return SCIP_OKAY;
5517  }
5518 
5519  /* detect fixed variables */
5520  SCIP_CALL( SCIPallocBufferArray(scip, &verticesarefixed, nsos1vars) );
5521  for (j = 0; j < nsos1vars; ++j)
5522  {
5523  SCIP_VAR* var;
5524  SCIP_Real ub;
5525  SCIP_Real lb;
5526 
5527  var = SCIPnodeGetVarSOS1(conflictgraph, j);
5528  ub = SCIPvarGetUbLocal(var);
5529  lb = SCIPvarGetLbLocal(var);
5530  if ( SCIPisFeasZero(scip, ub) && SCIPisFeasZero(scip, lb) )
5531  verticesarefixed[j] = TRUE;
5532  else
5533  verticesarefixed[j] = FALSE;
5534  }
5535 
5536  /* should bipartite branching be used? */
5537  if ( conshdlrdata->branchingrule == 'b' )
5538  bipbranch = TRUE;
5539 
5540  /* determine number of strong branching iterations */
5541  if ( conshdlrdata->nstrongrounds >= 0 )
5542  nstrongrounds = MIN(conshdlrdata->nstrongrounds, nsos1vars);
5543  else
5544  {
5545  /* determine number depending on depth, based on heuristical considerations */
5546  if ( SCIPgetDepth(scip) <= 10 )
5547  nstrongrounds = MAX(10, (int)SCIPfloor(scip, pow(log((SCIP_Real)nsos1vars), 1.0)));/*lint !e666*/
5548  else if ( SCIPgetDepth(scip) <= 20 )
5549  nstrongrounds = MAX(5, (int)SCIPfloor(scip, pow(log((SCIP_Real)nsos1vars), 0.7)));/*lint !e666*/
5550  else
5551  nstrongrounds = 0;
5552  nstrongrounds = MIN(nsos1vars, nstrongrounds);
5553  }
5554 
5555  /* allocate buffer arrays */
5556  SCIP_CALL( SCIPallocBufferArray(scip, &fixingsnode1, nsos1vars) );
5557  if ( bipbranch )
5558  SCIP_CALL( SCIPallocBufferArray(scip, &fixingsnode2, nsos1vars) );
5559  else
5560  SCIP_CALL( SCIPallocBufferArray(scip, &fixingsnode2, 1) );
5561 
5562  /* if strongbranching is turned off: use most infeasible branching */
5563  if ( nstrongrounds == 0 )
5564  {
5565  SCIP_Bool relsolfeas;
5566 
5567  /* get branching vertex using most infeasible branching */
5568  SCIP_CALL( getBranchingPrioritiesSOS1(scip, conshdlrdata, conflictgraph, sol, nsos1vars, verticesarefixed, bipbranch, fixingsnode1, fixingsnode2, NULL, &branchvertex, &relsolfeas) );
5569 
5570  /* if LP relaxation solution is feasible */
5571  if ( relsolfeas )
5572  {
5573  SCIPdebugMsg(scip, "all the SOS1 constraints are feasible.\n");
5574 
5575  /* update result */
5576  *result = SCIP_FEASIBLE;
5577 
5578  /* remove local conflicts from conflict graph */
5579  if ( conshdlrdata->isconflocal )
5580  {
5581  SCIP_CALL( resetConflictgraphSOS1(conflictgraph, conshdlrdata->localconflicts, nsos1vars) );
5582  conshdlrdata->isconflocal = FALSE;
5583  }
5584 
5585  /* free memory */
5586  SCIPfreeBufferArrayNull(scip, &fixingsnode2);
5587  SCIPfreeBufferArrayNull(scip, &fixingsnode1);
5588  SCIPfreeBufferArrayNull(scip, &verticesarefixed);
5589 
5590  return SCIP_OKAY;
5591  }
5592  }
5593  else
5594  {
5595  /* get branching vertex using strong branching */
5596  SCIP_CALL( getBranchingDecisionStrongbranchSOS1(scip, conshdlrdata, conflictgraph, sol, nsos1vars, lpobjval, bipbranch, nstrongrounds, verticesarefixed,
5597  fixingsnode1, fixingsnode2, &branchvertex, &bestobjval1, &bestobjval2, result) );
5598 
5599  if ( *result == SCIP_CUTOFF || *result == SCIP_FEASIBLE || *result == SCIP_REDUCEDDOM )
5600  {
5601  /* remove local conflicts from conflict graph */
5602  if ( conshdlrdata->isconflocal )
5603  {
5604  SCIP_CALL( resetConflictgraphSOS1(conflictgraph, conshdlrdata->localconflicts, nsos1vars) );
5605  conshdlrdata->isconflocal = FALSE;
5606  }
5607 
5608  /* free memory */
5609  SCIPfreeBufferArrayNull(scip, &fixingsnode2);
5610  SCIPfreeBufferArrayNull(scip, &fixingsnode1);
5611  SCIPfreeBufferArrayNull(scip, &verticesarefixed);
5612 
5613  return SCIP_OKAY;
5614  }
5615  }
5616 
5617  /* if we shouldleave branching decision to branching rules */
5618  if ( ! conshdlrdata->branchsos )
5619  {
5620  /* remove local conflicts from conflict graph */
5621  if ( conshdlrdata->isconflocal )
5622  {
5623  SCIP_CALL( resetConflictgraphSOS1(conflictgraph, conshdlrdata->localconflicts, nsos1vars) );
5624  conshdlrdata->isconflocal = FALSE;
5625  }
5626 
5627  if ( SCIPvarIsBinary(SCIPnodeGetVarSOS1(conflictgraph, branchvertex)) )
5628  {
5629  *result = SCIP_INFEASIBLE;
5630  return SCIP_OKAY;
5631  }
5632  else
5633  {
5634  SCIPerrorMessage("Incompatible parameter setting: branchsos can only be set to false if all SOS1 variables are binary.\n");
5635  return SCIP_PARAMETERWRONGVAL;
5636  }
5637  }
5638 
5639  /* create branching nodes */
5640 
5641  /* get vertices of variables that will be fixed to zero for each node */
5642  assert( branchvertex >= 0 && branchvertex < nsos1vars );
5643  assert( ! verticesarefixed[branchvertex] );
5644  SCIP_CALL( getBranchingVerticesSOS1(scip, conflictgraph, sol, verticesarefixed, bipbranch, branchvertex, fixingsnode1, &nfixingsnode1, fixingsnode2, &nfixingsnode2) );
5645 
5646  /* calculate node selection and objective estimate for node 1 */
5647  nodeselest = 0.0;
5648  objest = 0.0;
5649  for (j = 0; j < nfixingsnode1; ++j)
5650  {
5651  SCIP_VAR* var;
5652 
5653  var = SCIPnodeGetVarSOS1(conflictgraph, fixingsnode1[j]);
5654  nodeselest += SCIPcalcNodeselPriority(scip, var, SCIP_BRANCHDIR_DOWNWARDS, 0.0);
5655  objest += SCIPcalcChildEstimate(scip, var, 0.0);
5656  }
5657  /* take the average of the individual estimates */
5658  objest = objest/((SCIP_Real) nfixingsnode1);
5659 
5660  /* create node 1 */
5661  SCIP_CALL( SCIPcreateChild(scip, &node1, nodeselest, objest) );
5662 
5663  /* fix variables for the first node */
5664  if ( conshdlrdata->fixnonzero && nfixingsnode2 == 1 )
5665  {
5666  SCIP_VAR* var;
5667  SCIP_Real lb;
5668  SCIP_Real ub;
5669 
5670  var = SCIPnodeGetVarSOS1(conflictgraph, fixingsnode2[0]);
5671  lb = SCIPvarGetLbLocal(var);
5672  ub = SCIPvarGetUbLocal(var);
5673 
5675  {
5676  if ( SCIPisZero(scip, lb) )
5677  {
5678  /* fix variable to some very small, but positive number or to 1.0 if variable is integral */
5679  if (SCIPvarIsIntegral(var) )
5680  {
5681  SCIP_CALL( SCIPchgVarLbNode(scip, node1, var, 1.0) );
5682  }
5683  else
5684  {
5685  SCIP_CALL( SCIPchgVarLbNode(scip, node1, var, 1.5 * SCIPfeastol(scip)) );
5686  }
5687  }
5688  else if ( SCIPisZero(scip, ub) )
5689  {
5690  if (SCIPvarIsIntegral(var) )
5691  {
5692  /* fix variable to some negative number with small absolute value to -1.0 if variable is integral */
5693  SCIP_CALL( SCIPchgVarUbNode(scip, node1, var, -1.0) );
5694  }
5695  else
5696  {
5697  /* fix variable to some negative number with small absolute value to -1.0 if variable is integral */
5698  SCIP_CALL( SCIPchgVarUbNode(scip, node1, var, -1.5 * SCIPfeastol(scip)) );
5699  }
5700  }
5701  }
5702  }
5703  for (j = 0; j < nfixingsnode1; ++j)
5704  {
5705  /* fix variable to zero */
5706  SCIP_CALL( fixVariableZeroNode(scip, SCIPnodeGetVarSOS1(conflictgraph, fixingsnode1[j]), node1, &infeasible) );
5707  assert( ! infeasible );
5708  }
5709 
5710  /* calculate node selection and objective estimate for node 2 */
5711  nodeselest = 0.0;
5712  objest = 0.0;
5713  for (j = 0; j < nfixingsnode2; ++j)
5714  {
5715  SCIP_VAR* var;
5716 
5717  var = SCIPnodeGetVarSOS1(conflictgraph, fixingsnode2[j]);
5718  nodeselest += SCIPcalcNodeselPriority(scip, var, SCIP_BRANCHDIR_DOWNWARDS, 0.0);
5719  objest += SCIPcalcChildEstimate(scip, var, 0.0);
5720  }
5721 
5722  /* take the average of the individual estimates */
5723  objest = objest/((SCIP_Real) nfixingsnode2);
5724 
5725  /* create node 2 */
5726  SCIP_CALL( SCIPcreateChild(scip, &node2, nodeselest, objest) );
5727 
5728  /* fix variables to zero */
5729  for (j = 0; j < nfixingsnode2; ++j)
5730  {
5731  SCIP_CALL( fixVariableZeroNode(scip, SCIPnodeGetVarSOS1(conflictgraph, fixingsnode2[j]), node2, &infeasible) );
5732  assert( ! infeasible );
5733  }
5734 
5735  /* add complementarity constraints to the branching nodes */
5736  if ( conshdlrdata->addcomps && ( conshdlrdata->addcompsdepth == -1 || conshdlrdata->addcompsdepth >= SCIPgetDepth(scip) ) )
5737  {
5738  int naddedconss;
5739 
5740  assert( ! conshdlrdata->fixnonzero );
5741 
5742  /* add complementarity constraints to the left branching node */
5743  SCIP_CALL( addBranchingComplementaritiesSOS1(scip, node1, conshdlrdata, conflictgraph, conshdlrdata->localconflicts, sol,
5744  nsos1vars, verticesarefixed, fixingsnode1, nfixingsnode1, fixingsnode2, nfixingsnode2, &naddedconss, TRUE) );
5745 
5746  if ( naddedconss == 0 )
5747  {
5748  /* add complementarity constraints to the right branching node */
5749  SCIP_CALL( addBranchingComplementaritiesSOS1(scip, node2, conshdlrdata, conflictgraph, conshdlrdata->localconflicts, sol,
5750  nsos1vars, verticesarefixed, fixingsnode2, nfixingsnode2, fixingsnode1, nfixingsnode1, &naddedconss, TRUE) );
5751  }
5752  }
5753 
5754  /* sets node's lower bound to the best known value */
5755  if ( nstrongrounds > 0 )
5756  {
5757  SCIP_CALL( SCIPupdateNodeLowerbound(scip, node1, MAX(lpobjval, bestobjval1) ) );
5758  SCIP_CALL( SCIPupdateNodeLowerbound(scip, node2, MAX(lpobjval, bestobjval2) ) );
5759  }
5760 
5761  /* remove local conflicts from conflict graph */
5762  if ( conshdlrdata->isconflocal )
5763  {
5764  SCIP_CALL( resetConflictgraphSOS1(conflictgraph, conshdlrdata->localconflicts, nsos1vars) );
5765  conshdlrdata->isconflocal = FALSE;
5766  }
5767 
5768  /* free buffer arrays */
5769  SCIPfreeBufferArrayNull(scip, &fixingsnode2);
5770  SCIPfreeBufferArrayNull(scip, &fixingsnode1);
5771  SCIPfreeBufferArrayNull(scip, &verticesarefixed );
5772  *result = SCIP_BRANCHED;
5773 
5774  return SCIP_OKAY;
5775 }
5776 
5777 
5778 /** SOS1 branching enforcement method
5779  *
5780  * We check whether the current solution is feasible, i.e., contains at most one nonzero
5781  * variable. If not, we branch along the lines indicated by Beale and Tomlin:
5782  *
5783  * We first compute \f$W = \sum_{j=1}^n |x_i|\f$ and \f$w = \sum_{j=1}^n j\, |x_i|\f$. Then we
5784  * search for the index \f$k\f$ that satisfies
5785  * \f[
5786  * k \leq \frac{w}{W} < k+1.
5787  * \f]
5788  * The branches are then
5789  * \f[
5790  * x_1 = 0, \ldots, x_k = 0 \qquad \mbox{and}\qquad x_{k+1} = 0, \ldots, x_n = 0.
5791  * \f]
5792  *
5793  * If the constraint contains two variables, the branching of course simplifies.
5794  *
5795  * Depending on the parameters (@c branchnonzeros, @c branchweight) there are three ways to choose
5796  * the branching constraint.
5797  *
5798  * <TABLE>
5799  * <TR><TD>@c branchnonzeros</TD><TD>@c branchweight</TD><TD>constraint chosen</TD></TR>
5800  * <TR><TD>@c true </TD><TD> ? </TD><TD>most number of nonzeros</TD></TR>
5801  * <TR><TD>@c false </TD><TD> @c true </TD><TD>maximal weight corresponding to nonzero variable</TD></TR>
5802  * <TR><TD>@c false </TD><TD> @c true </TD><TD>largest sum of variable values</TD></TR>
5803  * </TABLE>
5804  *
5805  * @c branchnonzeros = @c false, @c branchweight = @c true allows the user to specify an order for
5806  * the branching importance of the constraints (setting the weights accordingly).
5807  *
5808  * Constraint branching can also be turned off using parameter @c branchsos.
5809  */
5810 static
5812  SCIP* scip, /**< SCIP pointer */
5813  SCIP_CONSHDLR* conshdlr, /**< constraint handler */
5814  int nconss, /**< number of constraints */
5815  SCIP_CONS** conss, /**< indicator constraints */
5816  SCIP_SOL* sol, /**< solution to be enforced (NULL for LP solution) */
5817  SCIP_RESULT* result /**< result */
5818  )
5819 {
5820  SCIP_CONSHDLRDATA* conshdlrdata;
5821  SCIP_CONSDATA* consdata;
5822  SCIP_NODE* node1;
5823  SCIP_NODE* node2;
5825  SCIP_Real maxWeight;
5826  SCIP_VAR** vars;
5827  int nvars;
5828  int c;
5829 
5830  assert( scip != NULL );
5831  assert( conshdlr != NULL );
5832  assert( conss != NULL );
5833  assert( result != NULL );
5834 
5835  maxWeight = -SCIP_REAL_MAX;
5836  branchCons = NULL;
5837 
5838  SCIPdebugMsg(scip, "Enforcing SOS1 constraints <%s>.\n", SCIPconshdlrGetName(conshdlr) );
5839  *result = SCIP_FEASIBLE;
5840 
5841  /* get constraint handler data */
5842  conshdlrdata = SCIPconshdlrGetData(conshdlr);
5843  assert( conshdlrdata != NULL );
5844 
5845  /* check each constraint */
5846  for (c = 0; c < nconss; ++c)
5847  {
5848  SCIP_CONS* cons;
5849  SCIP_Bool cutoff;
5850  SCIP_Real weight;
5851  int ngen;
5852  int cnt;
5853  int j;
5854 
5855  cons = conss[c];
5856  assert( cons != NULL );
5857  consdata = SCIPconsGetData(cons);
5858  assert( consdata != NULL );
5859 
5860  ngen = 0;
5861  cnt = 0;
5862  nvars = consdata->nvars;
5863  vars = consdata->vars;
5864 
5865  /* do nothing if there are not enough variables - this is usually eliminated by preprocessing */
5866  if ( nvars < 2 )
5867  continue;
5868 
5869  /* first perform propagation (it might happen that standard propagation is turned off) */
5870  SCIP_CALL( propConsSOS1(scip, cons, consdata, &cutoff, &ngen) );
5871  SCIPdebugMsg(scip, "propagating <%s> in enforcing (cutoff: %u, domain reductions: %d).\n", SCIPconsGetName(cons), cutoff, ngen);
5872  if ( cutoff )
5873  {
5874  *result = SCIP_CUTOFF;
5875  return SCIP_OKAY;
5876  }
5877  if ( ngen > 0 )
5878  {
5879  *result = SCIP_REDUCEDDOM;
5880  return SCIP_OKAY;
5881  }
5882  assert( ngen == 0 );
5883 
5884  /* check constraint */
5885  weight = 0.0;
5886  for (j = 0; j < nvars; ++j)
5887  {
5888  SCIP_Real val = REALABS(SCIPgetSolVal(scip, sol, vars[j]));
5889 
5890  if ( ! SCIPisFeasZero(scip, val) )
5891  {
5892  if ( conshdlrdata->branchnonzeros )
5893  weight += 1.0;
5894  else
5895  {
5896  if ( conshdlrdata->branchweight )
5897  {
5898  /* choose maximum nonzero-variable weight */
5899  if ( consdata->weights[j] > weight )
5900  weight = consdata->weights[j];
5901  }
5902  else
5903  weight += val;
5904  }
5905  ++cnt;
5906  }
5907  }
5908  /* if constraint is violated */
5909  if ( cnt > 1 && weight > maxWeight )
5910  {
5911  maxWeight = weight;
5912  branchCons = cons;
5913  }
5914  }
5915 
5916  /* if all constraints are feasible */
5917  if ( branchCons == NULL )
5918  {
5919  SCIPdebugMsg(scip, "All SOS1 constraints are feasible.\n");
5920  return SCIP_OKAY;
5921  }
5922 
5923  /* if we should leave branching decision to branching rules */
5924  if ( ! conshdlrdata->branchsos )
5925  {
5926  int j;
5927 
5928  consdata = SCIPconsGetData(branchCons);
5929  for (j = 0; j < consdata->nvars; ++j)
5930  {
5931  if ( ! SCIPvarIsBinary(consdata->vars[j]) )
5932  break;
5933  }
5934 
5935  if ( j == consdata->nvars )
5936  {
5937  *result =