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