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