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