Scippy

SCIP

Solving Constraint Integer Programs

cons_nonlinear.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2020 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file cons_nonlinear.c
17  * @ingroup DEFPLUGINS_CONS
18  * @brief constraint handler for nonlinear constraints \f$\textrm{lhs} \leq \sum_{i=1}^n a_ix_i + \sum_{j=1}^m c_jf_j(x) \leq \textrm{rhs}\f$
19  * @author Stefan Vigerske
20  * @author Ingmar Vierhaus (consparse)
21  */
22 
23 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
24 
25 #include "blockmemshell/memory.h"
26 #include <ctype.h>
27 #include "lpi/lpi.h"
28 #include "lpi/type_lpi.h"
29 #include "nlpi/exprinterpret.h"
30 #include "nlpi/nlpi_ipopt.h"
31 #include "nlpi/pub_expr.h"
33 #include "nlpi/type_nlpi.h"
34 #include "scip/cons_linear.h"
35 #include "scip/cons_nonlinear.h"
36 #define SCIP_PRIVATE_ROWPREP
37 #include "scip/cons_quadratic.h" /* for SCIP_ROWPREP */
38 #include "scip/debug.h"
39 #include "scip/heur_subnlp.h"
40 #include "scip/heur_trysol.h"
41 #include "scip/intervalarith.h"
42 #include "scip/pub_cons.h"
43 #include "scip/pub_event.h"
44 #include "scip/pub_heur.h"
45 #include "scip/pub_lp.h"
46 #include "scip/pub_message.h"
47 #include "scip/pub_misc.h"
48 #include "scip/pub_misc_sort.h"
49 #include "scip/pub_nlp.h"
50 #include "scip/pub_sol.h"
51 #include "scip/pub_tree.h"
52 #include "scip/pub_var.h"
53 #include "scip/scip_branch.h"
54 #include "scip/scip_cons.h"
55 #include "scip/scip_copy.h"
56 #include "scip/scip_cut.h"
57 #include "scip/scip_event.h"
58 #include "scip/scip_expr.h"
59 #include "scip/scip_general.h"
60 #include "scip/scip_heur.h"
61 #include "scip/scip_lp.h"
62 #include "scip/scip_mem.h"
63 #include "scip/scip_message.h"
64 #include "scip/scip_nlp.h"
65 #include "scip/scip_numerics.h"
66 #include "scip/scip_param.h"
67 #include "scip/scip_prob.h"
68 #include "scip/scip_probing.h"
69 #include "scip/scip_sepa.h"
70 #include "scip/scip_sol.h"
71 #include "scip/scip_solvingstats.h"
72 #include "scip/scip_tree.h"
73 #include "scip/scip_var.h"
74 #include <string.h>
75 
76 /* Inform compiler that this code accesses the floating-point environment, so that
77  * certain optimizations should be omitted (http://www.cplusplus.com/reference/cfenv/FENV_ACCESS/).
78  * Not supported by Clang (gives warning) and GCC (silently), at the moment.
79  */
80 #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
81 #pragma fenv_access (on)
82 #elif defined __GNUC__
83 #pragma STDC FENV_ACCESS ON
84 #endif
85 
86 /* constraint handler properties */
87 #define CONSHDLR_NAME "nonlinear"
88 #define CONSHDLR_DESC "constraint handler for nonlinear constraints"
89 #define CONSHDLR_SEPAPRIORITY 10 /**< priority of the constraint handler for separation */
90 #define CONSHDLR_ENFOPRIORITY -60 /**< priority of the constraint handler for constraint enforcing */
91 #define CONSHDLR_CHECKPRIORITY -4000010 /**< priority of the constraint handler for checking feasibility */
92 #define CONSHDLR_SEPAFREQ 1 /**< frequency for separating cuts; zero means to separate only in the root node */
93 #define CONSHDLR_PROPFREQ 1 /**< frequency for propagating domains; zero means only preprocessing propagation */
94 #define CONSHDLR_EAGERFREQ 100 /**< frequency for using all instead of only the useful constraints in separation,
95  * propagation and enforcement, -1 for no eager evaluations, 0 for first only */
96 #define CONSHDLR_MAXPREROUNDS -1 /**< maximal number of presolving rounds the constraint handler participates in (-1: no limit) */
97 #define CONSHDLR_DELAYSEPA FALSE /**< should separation method be delayed, if other separators found cuts? */
98 #define CONSHDLR_DELAYPROP FALSE /**< should propagation method be delayed, if other propagators found reductions? */
99 #define CONSHDLR_NEEDSCONS TRUE /**< should the constraint handler be skipped, if no constraints are available? */
101 #define CONSHDLR_PROP_TIMING SCIP_PROPTIMING_BEFORELP /**< propagation timing mask of the constraint handler */
102 #define CONSHDLR_PRESOLTIMING SCIP_PRESOLTIMING_ALWAYS /**< presolving timing of the constraint handler (fast, medium, or exhaustive) */
104 #define INTERVALINFTY 1E+43 /**< value for infinity in interval operations */
105 #define BOUNDTIGHTENING_MINSTRENGTH 0.05/**< minimal required bound tightening strength in expression graph domain tightening for propagating bound change */
106 #define INITLPMAXVARVAL 1000.0 /**< maximal absolute value of variable for still generating a linearization cut at that point in initlp */
108 /*
109  * Data structures
110  */
111 
112 /** event data for linear variable bound change events */
113 struct LinVarEventData
114 {
115  SCIP_CONSHDLRDATA* conshdlrdata; /**< the constraint handler data */
116  SCIP_CONS* cons; /**< the constraint */
117  int varidx; /**< the index of the linear variable which bound change is catched */
118  int filterpos; /**< position of eventdata in SCIP's event filter */
119 };
120 typedef struct LinVarEventData LINVAREVENTDATA;
122 /** constraint data for nonlinear constraints */
123 struct SCIP_ConsData
124 {
125  SCIP_Real lhs; /**< left hand side of constraint */
126  SCIP_Real rhs; /**< right hand side of constraint */
127 
128  int nlinvars; /**< number of linear variables */
129  int linvarssize; /**< length of linear variable arrays */
130  SCIP_VAR** linvars; /**< linear variables */
131  SCIP_Real* lincoefs; /**< coefficients of linear variables */
132  LINVAREVENTDATA** lineventdata; /**< eventdata for bound change of linear variable */
133 
134  int nexprtrees; /**< number of expression trees */
135  SCIP_Real* nonlincoefs; /**< coefficients of expression trees */
136  SCIP_EXPRTREE** exprtrees; /**< nonlinear part of constraint */
137  SCIP_EXPRCURV* curvatures; /**< curvature of each expression tree (taking nonlincoefs into account) */
138  SCIP_EXPRGRAPHNODE* exprgraphnode; /**< node in expression graph corresponding to expression tree of this constraint */
139  SCIP_EXPRCURV curvature; /**< curvature of complete nonlinear part, if checked */
140 
141  SCIP_NLROW* nlrow; /**< a nonlinear row representation of this constraint */
142 
143  unsigned int linvarssorted:1; /**< are the linear variables already sorted? */
144  unsigned int linvarsmerged:1; /**< are equal linear variables already merged? */
145 
146  unsigned int iscurvchecked:1; /**< is nonlinear function checked on convexity or concavity ? */
147  unsigned int isremovedfixingslin:1; /**< did we removed fixed/aggr/multiaggr variables in linear part? */
148  unsigned int ispresolved:1; /**< did we checked for possibilities of upgrading or implicit integer variables? */
149  unsigned int forcebackprop:1; /**< should we force to run the backward propagation on our subgraph in the exprgraph? */
150 
151  SCIP_Real minlinactivity; /**< sum of minimal activities of all linear terms with finite minimal activity */
152  SCIP_Real maxlinactivity; /**< sum of maximal activities of all linear terms with finite maximal activity */
153  int minlinactivityinf; /**< number of linear terms with infinite minimal activity */
154  int maxlinactivityinf; /**< number of linear terms with infinity maximal activity */
155  SCIP_Real activity; /**< activity of constraint function w.r.t. current solution */
156  SCIP_Real lhsviol; /**< violation of lower bound by current solution (used temporarily inside constraint handler) */
157  SCIP_Real rhsviol; /**< violation of lower bound by current solution (used temporarily inside constraint handler) */
158 
159  int linvar_maydecrease; /**< index of a variable in linvars that may be decreased without making any other constraint infeasible, or -1 if none */
160  int linvar_mayincrease; /**< index of a variable in linvars that may be increased without making any other constraint infeasible, or -1 if none */
161 
162  SCIP_Real lincoefsmin; /**< maximal absolute value of coefficients in linear part, only available in solving stage */
163  SCIP_Real lincoefsmax; /**< minimal absolute value of coefficients in linear part, only available in solving stage */
164  unsigned int ncuts; /**< number of cuts created for this constraint so far */
165 };
166 
167 /** nonlinear constraint update method */
168 struct SCIP_NlConsUpgrade
169 {
170  SCIP_DECL_NONLINCONSUPGD((*nlconsupgd)); /**< method to call for upgrading nonlinear constraint */
171  SCIP_DECL_EXPRGRAPHNODEREFORM((*nodereform));/**< method to call for reformulating an expression graph node */
172  int priority; /**< priority of upgrading method */
173  SCIP_Bool active; /**< is upgrading enabled */
174 };
177 /** constraint handler data */
178 struct SCIP_ConshdlrData
179 {
180  SCIP_EXPRINT* exprinterpreter; /**< expression interpreter to compute gradients */
181 
182  SCIP_Real cutmaxrange; /**< maximal range (maximal coef / minimal coef) of a cut in order to be added to LP */
183  SCIP_Bool linfeasshift; /**< whether to make solutions in check feasible if possible */
184  SCIP_Bool assumeconvex; /**< whether functions in inequalities should be assumed to be convex */
185  int maxproprounds; /**< limit on number of propagation rounds for a single constraint within one round of SCIP propagation */
186  SCIP_Bool reformulate; /**< whether to reformulate expression graph */
187  int maxexpansionexponent;/**< maximal exponent where still expanding non-monomial polynomials in expression simplification */
188  SCIP_Real sepanlpmincont; /**< minimal required fraction of continuous variables in problem to use solution of NLP relaxation in root for separation */
189  SCIP_Bool enfocutsremovable; /**< are cuts added during enforcement removable from the LP in the same node? */
190 
191  SCIP_HEUR* subnlpheur; /**< a pointer to the subNLP heuristic, if available */
192  SCIP_HEUR* trysolheur; /**< a pointer to the TRYSOL heuristic, if available */
193  SCIP_EVENTHDLR* linvareventhdlr; /**< our handler for linear variable bound change events */
194  SCIP_EVENTHDLR* nonlinvareventhdlr; /**< our handler for nonlinear variable bound change events */
195  int newsoleventfilterpos;/**< filter position of new solution event handler, if catched */
196 
197  SCIP_NLCONSUPGRADE** nlconsupgrades; /**< nonlinear constraint upgrade methods for specializing nonlinear constraints */
198  int nlconsupgradessize; /**< size of nlconsupgrade array */
199  int nnlconsupgrades; /**< number of nonlinear constraint upgrade methods */
200 
201  SCIP_EXPRGRAPH* exprgraph; /**< expression graph */
202  SCIP* scip; /**< SCIP pointer for use in expression graph callbacks */
203  unsigned int isremovedfixings:1; /**< have fixed variables been removed in the expression graph? */
204  unsigned int ispropagated:1; /**< have current bounds of linear variables in constraints and variables in expression graph been propagated? */
205  unsigned int isreformulated:1; /**< has expression graph been reformulated? */
206  unsigned int sepanlp:1; /**< has a linearization in the NLP relaxation been added? */
207  int naddedreformconss; /**< number of constraints added via reformulation */
208  SCIP_NODE* lastenfonode; /**< the node for which enforcement was called the last time (and some constraint was violated) */
209  int nenforounds; /**< counter on number of enforcement rounds for the current node */
210 };
211 
212 /*
213  * Local methods
214  */
215 
216 /** translate from one value of infinity to another
217  *
218  * if val is >= infty1, then give infty2, else give val
219  */
220 #define infty2infty(infty1, infty2, val) ((val) >= (infty1) ? (infty2) : (val))
222 /* catches variable bound change events on a linear variable in a nonlinear constraint */
223 static
225  SCIP* scip, /**< SCIP data structure */
226  SCIP_CONS* cons, /**< constraint for which to catch bound change events */
227  int linvarpos /**< position of variable in linear variables array */
228  )
229 {
231  SCIP_CONSDATA* consdata;
232  LINVAREVENTDATA* eventdata;
233  SCIP_EVENTTYPE eventtype;
234 
235  assert(scip != NULL);
236  assert(cons != NULL);
237  assert(SCIPconsIsEnabled(cons));
238  assert(SCIPconsIsTransformed(cons));
239 
240  assert(SCIPconsGetHdlr(cons) != NULL);
241  conshdlrdata = SCIPconshdlrGetData(SCIPconsGetHdlr(cons));
242  assert(conshdlrdata != NULL);
243  assert(conshdlrdata->linvareventhdlr != NULL);
244 
245  consdata = SCIPconsGetData(cons);
246  assert(consdata != NULL);
247 
248  assert(linvarpos >= 0);
249  assert(linvarpos < consdata->nlinvars);
250 
251  SCIP_CALL( SCIPallocBlockMemory(scip, &eventdata) );
252 
253  eventtype = SCIP_EVENTTYPE_VARFIXED;
254  if( !SCIPisInfinity(scip, consdata->rhs) )
255  {
256  /* if right hand side is finite, then a tightening in the lower bound of coef*linvar is of interest */
257  if( consdata->lincoefs[linvarpos] > 0.0 )
258  eventtype |= SCIP_EVENTTYPE_LBCHANGED;
259  else
260  eventtype |= SCIP_EVENTTYPE_UBCHANGED;
261  }
262  if( !SCIPisInfinity(scip, -consdata->lhs) )
263  {
264  /* if left hand side is finite, then a tightening in the upper bound of coef*linvar is of interest */
265  if( consdata->lincoefs[linvarpos] > 0.0 )
266  eventtype |= SCIP_EVENTTYPE_UBCHANGED;
267  else
268  eventtype |= SCIP_EVENTTYPE_LBCHANGED;
269  }
270 
271  eventdata->conshdlrdata = conshdlrdata;
272  eventdata->cons = cons;
273  eventdata->varidx = linvarpos;
274  SCIP_CALL( SCIPcatchVarEvent(scip, consdata->linvars[linvarpos], eventtype, conshdlrdata->linvareventhdlr, (SCIP_EVENTDATA*)eventdata, &eventdata->filterpos) );
275 
276  /* ensure lineventdata array is existing */
277  if( consdata->lineventdata == NULL )
278  {
279  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &consdata->lineventdata, consdata->linvarssize) );
280  }
281 
282  consdata->lineventdata[linvarpos] = eventdata;
283 
284  /* since bound changes were not catched before, a possibly stored linear activity may have become outdated, so set to invalid */
285  consdata->minlinactivity = SCIP_INVALID;
286  consdata->maxlinactivity = SCIP_INVALID;
287 
288  /* mark constraint for propagation */
289  SCIP_CALL( SCIPmarkConsPropagate(scip, cons) );
290 
291  return SCIP_OKAY;
292 }
293 
294 /* drops variable bound change events on a linear variable in a nonlinear constraint */
295 static
297  SCIP* scip, /**< SCIP data structure */
298  SCIP_CONS* cons, /**< constraint for which to catch bound change events */
299  int linvarpos /**< position of variable in linear variables array */
300  )
301 {
303  SCIP_CONSDATA* consdata;
304  SCIP_EVENTTYPE eventtype;
305 
306  assert(scip != NULL);
307  assert(cons != NULL);
308  assert(SCIPconsIsTransformed(cons));
309 
310  assert(SCIPconsGetHdlr(cons) != NULL);
312  assert(conshdlrdata != NULL);
313  assert(conshdlrdata->linvareventhdlr != NULL);
314 
315  consdata = SCIPconsGetData(cons);
316  assert(consdata != NULL);
317 
318  assert(linvarpos >= 0);
319  assert(linvarpos < consdata->nlinvars);
320  assert(consdata->lineventdata != NULL);
321  assert(consdata->lineventdata[linvarpos] != NULL);
322  assert(consdata->lineventdata[linvarpos]->cons == cons);
323  assert(consdata->lineventdata[linvarpos]->varidx == linvarpos);
324  assert(consdata->lineventdata[linvarpos]->filterpos >= 0);
325 
326  eventtype = SCIP_EVENTTYPE_VARFIXED;
327  if( !SCIPisInfinity(scip, consdata->rhs) )
328  {
329  /* if right hand side is finite, then a tightening in the lower bound of coef*linvar is of interest */
330  if( consdata->lincoefs[linvarpos] > 0.0 )
331  eventtype |= SCIP_EVENTTYPE_LBCHANGED;
332  else
333  eventtype |= SCIP_EVENTTYPE_UBCHANGED;
334  }
335  if( !SCIPisInfinity(scip, -consdata->lhs) )
336  {
337  /* if left hand side is finite, then a tightening in the upper bound of coef*linvar is of interest */
338  if( consdata->lincoefs[linvarpos] > 0.0 )
339  eventtype |= SCIP_EVENTTYPE_UBCHANGED;
340  else
341  eventtype |= SCIP_EVENTTYPE_LBCHANGED;
342  }
343 
344  SCIP_CALL( SCIPdropVarEvent(scip, consdata->linvars[linvarpos], eventtype, conshdlrdata->linvareventhdlr, (SCIP_EVENTDATA*)consdata->lineventdata[linvarpos], consdata->lineventdata[linvarpos]->filterpos) );
345 
346  SCIPfreeBlockMemory(scip, &consdata->lineventdata[linvarpos]); /*lint !e866*/
347 
348  return SCIP_OKAY;
349 }
350 
351 /** locks a linear variable in a constraint */
352 static
354  SCIP* scip, /**< SCIP data structure */
355  SCIP_CONS* cons, /**< constraint where to lock a variable */
356  SCIP_VAR* var, /**< variable to lock */
357  SCIP_Real coef /**< coefficient of variable in constraint */
358  )
359 {
360  SCIP_CONSDATA* consdata;
361 
362  assert(scip != NULL);
363  assert(cons != NULL);
364  assert(var != NULL);
365  assert(coef != 0.0);
366 
367  consdata = SCIPconsGetData(cons);
368  assert(consdata != NULL);
369 
370  if( coef > 0.0 )
371  {
372  SCIP_CALL( SCIPlockVarCons(scip, var, cons, !SCIPisInfinity(scip, -consdata->lhs), !SCIPisInfinity(scip, consdata->rhs)) );
373  }
374  else
375  {
376  SCIP_CALL( SCIPlockVarCons(scip, var, cons, !SCIPisInfinity(scip, consdata->rhs), !SCIPisInfinity(scip, -consdata->lhs)) );
377  }
378 
379  return SCIP_OKAY;
380 }
381 
382 /** unlocks a linear variable in a constraint */
383 static
385  SCIP* scip, /**< SCIP data structure */
386  SCIP_CONS* cons, /**< constraint where to unlock a variable */
387  SCIP_VAR* var, /**< variable to unlock */
388  SCIP_Real coef /**< coefficient of variable in constraint */
389  )
390 {
391  SCIP_CONSDATA* consdata;
392 
393  assert(scip != NULL);
394  assert(cons != NULL);
396  assert(var != NULL);
397  assert(coef != 0.0);
398 
399  consdata = SCIPconsGetData(cons);
400  assert(consdata != NULL);
401 
402  if( coef > 0.0 )
403  {
404  SCIP_CALL( SCIPunlockVarCons(scip, var, cons, !SCIPisInfinity(scip, -consdata->lhs), !SCIPisInfinity(scip, consdata->rhs)) );
405  }
406  else
407  {
408  SCIP_CALL( SCIPunlockVarCons(scip, var, cons, !SCIPisInfinity(scip, consdata->rhs), !SCIPisInfinity(scip, -consdata->lhs)) );
409  }
410 
411  return SCIP_OKAY;
412 }
413 
414 /** computes the minimal and maximal activity for the linear part in a constraint data
415  * only sums up terms that contribute finite values
416  * gives the number of terms that contribute infinite values
417  * only computes those activities where the corresponding side of the constraint is finite
418  */
419 static
421  SCIP* scip, /**< SCIP data structure */
422  SCIP_CONSDATA* consdata /**< constraint data */
423  )
424 { /*lint --e{666}*/
425  SCIP_ROUNDMODE prevroundmode;
426  int i;
427  SCIP_Real bnd;
428 
429  assert(scip != NULL);
430  assert(consdata != NULL);
431 
432  if( consdata->minlinactivity != SCIP_INVALID && consdata->maxlinactivity != SCIP_INVALID ) /*lint !e777*/
433  {
434  /* activities should be uptodate */
435  assert(consdata->minlinactivityinf >= 0);
436  assert(consdata->maxlinactivityinf >= 0);
437  return;
438  }
439 
440  consdata->minlinactivityinf = 0;
441  consdata->maxlinactivityinf = 0;
442 
443  /* if lhs is -infinite, then we do not compute a maximal activity, so we set it to infinity
444  * if rhs is infinite, then we do not compute a minimal activity, so we set it to -infinity
445  */
446  consdata->minlinactivity = SCIPisInfinity(scip, consdata->rhs) ? -INTERVALINFTY : 0.0;
447  consdata->maxlinactivity = SCIPisInfinity(scip, -consdata->lhs) ? INTERVALINFTY : 0.0;
448 
449  if( consdata->nlinvars == 0 )
450  return;
451 
452  /* if the activities computed here should be still uptodate after boundchanges,
453  * variable events need to be catched */
454  assert(consdata->lineventdata != NULL);
455 
456  prevroundmode = SCIPintervalGetRoundingMode();
457 
458  if( !SCIPisInfinity(scip, consdata->rhs) )
459  {
460  /* compute minimal activity only if there is a finite right hand side */
462 
463  for( i = 0; i < consdata->nlinvars; ++i )
464  {
465  assert(SCIPvarGetLbLocal(consdata->linvars[i]) <= SCIPvarGetUbLocal(consdata->linvars[i]));
466  assert(consdata->lineventdata[i] != NULL);
467  if( consdata->lincoefs[i] >= 0.0 )
468  {
469  bnd = SCIPvarGetLbLocal(consdata->linvars[i]);
470  if( SCIPisInfinity(scip, -bnd) )
471  {
472  ++consdata->minlinactivityinf;
473  continue;
474  }
475  assert(!SCIPisInfinity(scip, bnd)); /* do not like variables that are fixed at +infinity */
476  }
477  else
478  {
479  bnd = SCIPvarGetUbLocal(consdata->linvars[i]);
480  if( SCIPisInfinity(scip, bnd) )
481  {
482  ++consdata->minlinactivityinf;
483  continue;
484  }
485  assert(!SCIPisInfinity(scip, -bnd)); /* do not like variables that are fixed at -infinity */
486  }
487  consdata->minlinactivity += consdata->lincoefs[i] * bnd;
488  }
489  }
490 
491  if( !SCIPisInfinity(scip, -consdata->lhs) )
492  {
493  /* compute maximal activity only if there is a finite left hand side */
495 
496  for( i = 0; i < consdata->nlinvars; ++i )
497  {
498  assert(consdata->lineventdata[i] != NULL);
499  assert(SCIPvarGetLbLocal(consdata->linvars[i]) <= SCIPvarGetUbLocal(consdata->linvars[i]));
500  if( consdata->lincoefs[i] >= 0.0 )
501  {
502  bnd = SCIPvarGetUbLocal(consdata->linvars[i]);
503  if( SCIPisInfinity(scip, bnd) )
504  {
505  ++consdata->maxlinactivityinf;
506  continue;
507  }
508  assert(!SCIPisInfinity(scip, -bnd)); /* do not like variables that are fixed at -infinity */
509  }
510  else
511  {
512  bnd = SCIPvarGetLbLocal(consdata->linvars[i]);
513  if( SCIPisInfinity(scip, -bnd) )
514  {
515  ++consdata->maxlinactivityinf;
516  continue;
517  }
518  assert(!SCIPisInfinity(scip, bnd)); /* do not like variables that are fixed at +infinity */
519  }
520  consdata->maxlinactivity += consdata->lincoefs[i] * bnd;
521  }
522  }
523  assert(consdata->minlinactivity <= consdata->maxlinactivity || consdata->minlinactivityinf > 0 || consdata->maxlinactivityinf > 0);
524 
525  SCIPintervalSetRoundingMode(prevroundmode);
526 }
527 
528 /** update the linear activities after a change in the lower bound of a variable */
529 static
531  SCIP* scip, /**< SCIP data structure */
532  SCIP_CONSDATA* consdata, /**< constraint data */
533  SCIP_Real coef, /**< coefficient of variable in constraint */
534  SCIP_Real oldbnd, /**< previous lower bound of variable */
535  SCIP_Real newbnd /**< new lower bound of variable */
536  )
537 {
538  SCIP_ROUNDMODE prevroundmode;
539 
540  assert(scip != NULL);
541  assert(consdata != NULL);
542  /* we can't deal with lower bounds at infinity */
543  assert(!SCIPisInfinity(scip, oldbnd));
544  assert(!SCIPisInfinity(scip, newbnd));
545 
546  /* assume lhs <= a*x + y <= rhs, then the following boundchanges can be deduced:
547  * a > 0: y <= rhs - a*lb(x), y >= lhs - a*ub(x)
548  * a < 0: y <= rhs - a*ub(x), y >= lhs - a*lb(x)
549  */
550 
551  if( coef > 0.0 )
552  {
553  /* we should only be called if rhs is finite */
554  assert(!SCIPisInfinity(scip, consdata->rhs));
555 
556  /* we have no min activities computed so far, so cannot update */
557  if( consdata->minlinactivity == SCIP_INVALID ) /*lint !e777*/
558  return;
559 
560  assert(consdata->minlinactivity > -INTERVALINFTY);
561 
562  prevroundmode = SCIPintervalGetRoundingMode();
564 
565  /* update min activity */
566  if( SCIPisInfinity(scip, -oldbnd) )
567  {
568  --consdata->minlinactivityinf;
569  assert(consdata->minlinactivityinf >= 0);
570  }
571  else
572  {
573  consdata->minlinactivity += SCIPintervalNegateReal(coef) * oldbnd;
574  }
575 
576  if( SCIPisInfinity(scip, -newbnd) )
577  {
578  ++consdata->minlinactivityinf;
579  }
580  else
581  {
582  consdata->minlinactivity += coef * newbnd;
583  }
584 
585  SCIPintervalSetRoundingMode(prevroundmode);
586  }
587  else
588  {
589  /* we should only be called if lhs is finite */
590  assert(!SCIPisInfinity(scip, -consdata->lhs));
591 
592  /* we have no max activities computed so far, so cannot update */
593  if( consdata->maxlinactivity == SCIP_INVALID ) /*lint !e777*/
594  return;
595 
596  assert(consdata->maxlinactivity < INTERVALINFTY);
597 
598  prevroundmode = SCIPintervalGetRoundingMode();
600 
601  /* update max activity */
602  if( SCIPisInfinity(scip, -oldbnd) )
603  {
604  --consdata->maxlinactivityinf;
605  assert(consdata->maxlinactivityinf >= 0);
606  }
607  else
608  {
609  consdata->maxlinactivity += SCIPintervalNegateReal(coef) * oldbnd;
610  }
611 
612  if( SCIPisInfinity(scip, -newbnd) )
613  {
614  ++consdata->maxlinactivityinf;
615  }
616  else
617  {
618  consdata->maxlinactivity += coef * newbnd;
619  }
620 
621  SCIPintervalSetRoundingMode(prevroundmode);
622  }
623 }
624 
625 /** update the linear activities after a change in the upper bound of a variable */
626 static
628  SCIP* scip, /**< SCIP data structure */
629  SCIP_CONSDATA* consdata, /**< constraint data */
630  SCIP_Real coef, /**< coefficient of variable in constraint */
631  SCIP_Real oldbnd, /**< previous lower bound of variable */
632  SCIP_Real newbnd /**< new lower bound of variable */
633  )
634 {
635  SCIP_ROUNDMODE prevroundmode;
636 
637  assert(scip != NULL);
638  assert(consdata != NULL);
639  /* we can't deal with upper bounds at -infinity */
640  assert(!SCIPisInfinity(scip, -oldbnd));
641  assert(!SCIPisInfinity(scip, -newbnd));
642 
643  /* assume lhs <= a*x + y <= rhs, then the following boundchanges can be deduced:
644  * a > 0: y <= rhs - a*lb(x), y >= lhs - a*ub(x)
645  * a < 0: y <= rhs - a*ub(x), y >= lhs - a*lb(x)
646  */
647  if( coef > 0.0 )
648  {
649  /* we should only be called if lhs is finite */
650  assert(!SCIPisInfinity(scip, -consdata->lhs));
651 
652  /* we have no max activities computed so far, so cannot update */
653  if( consdata->maxlinactivity == SCIP_INVALID ) /*lint !e777*/
654  return;
655 
656  assert(consdata->maxlinactivity < INTERVALINFTY);
657 
658  prevroundmode = SCIPintervalGetRoundingMode();
660 
661  /* update max activity */
662  if( SCIPisInfinity(scip, oldbnd) )
663  {
664  --consdata->maxlinactivityinf;
665  assert(consdata->maxlinactivityinf >= 0);
666  }
667  else
668  {
669  consdata->maxlinactivity += SCIPintervalNegateReal(coef) * oldbnd;
670  }
671 
672  if( SCIPisInfinity(scip, newbnd) )
673  {
674  ++consdata->maxlinactivityinf;
675  }
676  else
677  {
678  consdata->maxlinactivity += coef * newbnd;
679  }
680 
681  SCIPintervalSetRoundingMode(prevroundmode);
682  }
683  else
684  {
685  /* we should only be called if rhs is finite */
686  assert(!SCIPisInfinity(scip, consdata->rhs));
687 
688  /* we have no min activities computed so far, so cannot update */
689  if( consdata->minlinactivity == SCIP_INVALID ) /*lint !e777*/
690  return;
691 
692  assert(consdata->minlinactivity > -INTERVALINFTY);
693 
694  prevroundmode = SCIPintervalGetRoundingMode();
696 
697  /* update min activity */
698  if( SCIPisInfinity(scip, oldbnd) )
699  {
700  --consdata->minlinactivityinf;
701  assert(consdata->minlinactivityinf >= 0);
702  }
703  else
704  {
705  consdata->minlinactivity += SCIPintervalNegateReal(coef) * oldbnd;
706  }
707 
708  if( SCIPisInfinity(scip, newbnd) )
709  {
710  ++consdata->minlinactivityinf;
711  }
712  else
713  {
714  consdata->minlinactivity += coef * newbnd;
715  }
716 
717  SCIPintervalSetRoundingMode(prevroundmode);
718  }
719 }
720 
721 /** processes variable fixing or bound change event */
722 static
723 SCIP_DECL_EVENTEXEC(processLinearVarEvent)
724 {
725  SCIP_CONS* cons;
726  SCIP_CONSDATA* consdata;
727  SCIP_EVENTTYPE eventtype;
728  int varidx;
729 
730  assert(scip != NULL);
731  assert(event != NULL);
732  assert(eventdata != NULL);
733  assert(eventhdlr != NULL);
734 
735  cons = ((LINVAREVENTDATA*)eventdata)->cons;
736  assert(cons != NULL);
737 
738  consdata = SCIPconsGetData(cons);
739  assert(consdata != NULL);
740 
741  varidx = ((LINVAREVENTDATA*)eventdata)->varidx;
742  assert(varidx >= 0);
743  assert(varidx < consdata->nlinvars);
744 
745  eventtype = SCIPeventGetType(event);
746 
747  if( eventtype & SCIP_EVENTTYPE_VARFIXED )
748  {
749  consdata->isremovedfixingslin = FALSE;
750  }
751 
752  if( eventtype & SCIP_EVENTTYPE_BOUNDCHANGED )
753  {
754  /* update activity bounds for linear terms */
755  if( eventtype & SCIP_EVENTTYPE_LBCHANGED )
756  consdataUpdateLinearActivityLbChange(scip, consdata, consdata->lincoefs[varidx], SCIPeventGetOldbound(event), SCIPeventGetNewbound(event));
757  else
758  consdataUpdateLinearActivityUbChange(scip, consdata, consdata->lincoefs[varidx], SCIPeventGetOldbound(event), SCIPeventGetNewbound(event));
759 
760  if( eventtype & SCIP_EVENTTYPE_BOUNDTIGHTENED )
761  {
762  assert(((LINVAREVENTDATA*)eventdata)->conshdlrdata != NULL);
763  ((LINVAREVENTDATA*)eventdata)->conshdlrdata->ispropagated = FALSE;
764 
765  /* mark constraint for propagation */
766  SCIP_CALL( SCIPmarkConsPropagate(scip, cons) );
767  }
768  }
769 
770  return SCIP_OKAY;
771 }
772 
773 /** processes bound change events for variables in expression graph */
774 static
775 SCIP_DECL_EVENTEXEC(processNonlinearVarEvent)
776 {
778  SCIP_EVENTTYPE eventtype;
779 
780  assert(scip != NULL);
781  assert(event != NULL);
782  assert(eventdata != NULL);
783  assert(eventhdlr != NULL);
784 
785  conshdlrdata = (SCIP_CONSHDLRDATA*)SCIPeventhdlrGetData(eventhdlr);
786  assert(conshdlrdata != NULL);
787  assert(conshdlrdata->exprgraph != NULL);
788 
789  eventtype = SCIPeventGetType(event);
790  assert( eventtype & (SCIP_EVENTTYPE_BOUNDCHANGED | SCIP_EVENTTYPE_VARFIXED) );
791 
792  if( eventtype & SCIP_EVENTTYPE_BOUNDCHANGED )
793  {
794  SCIP_Real newbd;
795 
796  SCIPdebugMsg(scip, "changed %s bound on expression graph variable <%s> from %g to %g\n",
797  (eventtype & SCIP_EVENTTYPE_LBCHANGED) ? "lower" : "upper",
799 
800  if( eventtype & SCIP_EVENTTYPE_BOUNDTIGHTENED )
801  conshdlrdata->ispropagated = FALSE;
802  /* @todo a global bound tightening may yield in convex/concave curvatures, maybe the iscurvcheck flag should be reset? */
803 
804  /* update variable bound in expression graph, with epsilon added */
805  if( eventtype & SCIP_EVENTTYPE_LBCHANGED )
806  {
807  newbd = -infty2infty(SCIPinfinity(scip), INTERVALINFTY, -SCIPeventGetNewbound(event)); /*lint !e666*/
808  /* if newbd in [0,eps], then relax to 0.0, otherwise relax by -epsilon
809  * this is to ensure that an original positive variable does not get negative by this, which may have an adverse effect on convexity recoginition, for example */
810  if( newbd >= 0.0 && newbd <= SCIPepsilon(scip) )
811  newbd = 0.0;
812  else
813  newbd -= SCIPepsilon(scip);
814  SCIPexprgraphSetVarNodeLb(conshdlrdata->exprgraph, (SCIP_EXPRGRAPHNODE*)eventdata, newbd);
815  }
816  else
817  {
818  newbd = +infty2infty(SCIPinfinity(scip), INTERVALINFTY, SCIPeventGetNewbound(event)); /*lint !e666*/
819  /* if newbd in [-eps,0], then relax to 0.0, otherwise relax by +epsilon */
820  if( newbd >= -SCIPepsilon(scip) && newbd <= 0.0 )
821  newbd = 0.0;
822  else
823  newbd += SCIPepsilon(scip);
824  SCIPexprgraphSetVarNodeUb(conshdlrdata->exprgraph, (SCIP_EXPRGRAPHNODE*)eventdata, newbd);
825  }
826  }
827  else
828  {
829  assert(eventtype & SCIP_EVENTTYPE_VARFIXED);
830  conshdlrdata->isremovedfixings = FALSE;
831  }
832 
833  return SCIP_OKAY;
834 }
835 
836 /** callback method for variable addition in expression graph */
837 static
838 SCIP_DECL_EXPRGRAPHVARADDED( exprgraphVarAdded )
839 {
841  SCIP_INTERVAL varbounds;
842  SCIP_VAR* var_;
843 
844  assert(exprgraph != NULL);
845  assert(var != NULL);
846  assert(varnode != NULL);
847 
848  var_ = (SCIP_VAR*)var;
849 
850  conshdlrdata = (SCIP_CONSHDLRDATA*)userdata;
851  assert(conshdlrdata != NULL);
852  assert(conshdlrdata->exprgraph == exprgraph);
853 
854  /* catch variable bound change events */
855  SCIP_CALL( SCIPcatchVarEvent(conshdlrdata->scip, (SCIP_VAR*)var, SCIP_EVENTTYPE_BOUNDCHANGED | SCIP_EVENTTYPE_VARFIXED, conshdlrdata->nonlinvareventhdlr, (SCIP_EVENTDATA*)varnode, NULL) );
856  SCIPdebugMessage("catch boundchange events on new expression graph variable <%s>\n", SCIPvarGetName(var_));
857 
858  /* set current bounds in expression graph */
859  SCIPintervalSetBounds(&varbounds,
860  -infty2infty(SCIPinfinity(conshdlrdata->scip), INTERVALINFTY, -MIN(SCIPvarGetLbLocal(var_), SCIPvarGetUbLocal(var_))), /*lint !e666*/
861  +infty2infty(SCIPinfinity(conshdlrdata->scip), INTERVALINFTY, MAX(SCIPvarGetLbLocal(var_), SCIPvarGetUbLocal(var_))) /*lint !e666*/
862  );
863  SCIPexprgraphSetVarNodeBounds(exprgraph, varnode, varbounds);
864 
865  SCIP_CALL( SCIPaddVarLocksType(conshdlrdata->scip, var_, SCIP_LOCKTYPE_MODEL, 1, 1) );
866  SCIPdebugMessage("increased up- and downlocks of variable <%s>\n", SCIPvarGetName(var_));
867 
868  SCIP_CALL( SCIPcaptureVar(conshdlrdata->scip, var_) );
869  SCIPdebugMessage("capture variable <%s>\n", SCIPvarGetName(var_));
870 
871  conshdlrdata->isremovedfixings &= SCIPvarIsActive(var_);
872  conshdlrdata->ispropagated = FALSE;
873 
874  return SCIP_OKAY;
875 }
876 
877 /** callback method for variable removal in expression graph */
878 static
879 SCIP_DECL_EXPRGRAPHVARREMOVE( exprgraphVarRemove )
880 {
882  SCIP_VAR* var_;
883 
884  assert(exprgraph != NULL);
885  assert(var != NULL);
886  assert(varnode != NULL);
887 
888  var_ = (SCIP_VAR*)var;
889 
890  conshdlrdata = (SCIP_CONSHDLRDATA*)userdata;
891  assert(conshdlrdata != NULL);
892  assert(conshdlrdata->exprgraph == exprgraph);
893 
894  SCIP_CALL( SCIPdropVarEvent(conshdlrdata->scip, var_, SCIP_EVENTTYPE_BOUNDCHANGED | SCIP_EVENTTYPE_VARFIXED, conshdlrdata->nonlinvareventhdlr, (SCIP_EVENTDATA*)varnode, -1) );
895  SCIPdebugMessage("drop boundchange events on expression graph variable <%s>\n", SCIPvarGetName(var_));
896 
897  SCIP_CALL( SCIPaddVarLocksType(conshdlrdata->scip, var_, SCIP_LOCKTYPE_MODEL, -1, -1) );
898  SCIPdebugMessage("decreased up- and downlocks of variable <%s>\n", SCIPvarGetName(var_));
899 
900  SCIPdebugMessage("release variable <%s>\n", SCIPvarGetName(var_));
901  SCIP_CALL( SCIPreleaseVar(conshdlrdata->scip, &var_) );
902 
903  return SCIP_OKAY;
904 }
905 
906 /* adds expression trees to constraint */
907 static
909  SCIP* scip, /**< SCIP data structure */
910  SCIP_CONSDATA* consdata, /**< nonlinear constraint data */
911  int nexprtrees, /**< number of expression trees */
912  SCIP_EXPRTREE** exprtrees, /**< expression trees */
913  SCIP_Real* coefs, /**< coefficients of expression trees, or NULL if all 1.0 */
914  SCIP_Bool copytrees /**< whether trees should be copied or ownership should be assumed */
915  )
916 {
917  int i;
918 
919  assert(scip != NULL);
920  assert(consdata != NULL);
921  assert(consdata->exprtrees != NULL || consdata->nexprtrees == 0);
922 
923  if( nexprtrees == 0 )
924  return SCIP_OKAY;
925 
926  /* invalidate activity information */
927  consdata->activity = SCIP_INVALID;
928 
929  /* invalidate nonlinear row */
930  if( consdata->nlrow != NULL )
931  {
932  SCIP_CALL( SCIPreleaseNlRow(scip, &consdata->nlrow) );
933  }
934 
935  consdata->ispresolved = FALSE;
936  consdata->curvature = SCIP_EXPRCURV_UNKNOWN;
937  consdata->iscurvchecked = FALSE;
938 
939  if( consdata->nexprtrees == 0 )
940  {
941  assert(consdata->exprtrees == NULL);
942  assert(consdata->nonlincoefs == NULL);
943  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &consdata->exprtrees, nexprtrees) );
944  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &consdata->nonlincoefs, nexprtrees) );
945  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &consdata->curvatures, nexprtrees) );
946  }
947  else
948  {
949  assert(consdata->exprtrees != NULL);
950  assert(consdata->nonlincoefs != NULL);
951  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->exprtrees, consdata->nexprtrees, consdata->nexprtrees + nexprtrees) );
952  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->nonlincoefs, consdata->nexprtrees, consdata->nexprtrees + nexprtrees) );
953  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->curvatures, consdata->nexprtrees, consdata->nexprtrees + nexprtrees) );
954  }
955 
956  for( i = 0; i < nexprtrees; ++i )
957  {
958  assert(exprtrees[i] != NULL);
959  /* the expression tree need to have SCIP_VAR*'s stored */
960  assert(SCIPexprtreeGetNVars(exprtrees[i]) == 0 || SCIPexprtreeGetVars(exprtrees[i]) != NULL);
961 
962  if( copytrees )
963  {
964  SCIP_CALL( SCIPexprtreeCopy(SCIPblkmem(scip), &consdata->exprtrees[consdata->nexprtrees + i], exprtrees[i]) );
965  }
966  else
967  {
968  consdata->exprtrees[consdata->nexprtrees + i] = exprtrees[i];
969  }
970 
971  consdata->nonlincoefs[consdata->nexprtrees + i] = (coefs != NULL ? coefs[i] : 1.0);
972  consdata->curvatures[consdata->nexprtrees + i] = SCIP_EXPRCURV_UNKNOWN;
973  }
974  consdata->nexprtrees += nexprtrees;
975 
976  return SCIP_OKAY;
977 }
978 
979 /* sets expression trees of constraints, clears previously ones */
980 static
982  SCIP* scip, /**< SCIP data structure */
983  SCIP_CONSDATA* consdata, /**< nonlinear constraint data */
984  int nexprtrees, /**< number of expression trees */
985  SCIP_EXPRTREE** exprtrees, /**< expression trees */
986  SCIP_Real* coefs, /**< coefficients of expression trees, or NULL if all 1.0 */
987  SCIP_Bool copytrees /**< whether trees should be copied or ownership should be assumed */
988  )
989 {
990  int i;
991 
992  assert(scip != NULL);
993  assert(consdata != NULL);
994  assert(consdata->exprtrees != NULL || consdata->nexprtrees == 0);
995 
996  /* clear existing expression trees */
997  if( consdata->nexprtrees > 0 )
998  {
999  for( i = 0; i < consdata->nexprtrees; ++i )
1000  {
1001  assert(consdata->exprtrees[i] != NULL);
1002  SCIP_CALL( SCIPexprtreeFree(&consdata->exprtrees[i]) );
1003  }
1004 
1005  /* invalidate activity information */
1006  consdata->activity = SCIP_INVALID;
1007 
1008  /* invalidate nonlinear row */
1009  if( consdata->nlrow != NULL )
1010  {
1011  SCIP_CALL( SCIPreleaseNlRow(scip, &consdata->nlrow) );
1012  }
1013 
1014  consdata->ispresolved = FALSE;
1015  consdata->curvature = SCIP_EXPRCURV_LINEAR;
1016  consdata->iscurvchecked = TRUE;
1017 
1018  SCIPfreeBlockMemoryArray(scip, &consdata->exprtrees, consdata->nexprtrees);
1019  SCIPfreeBlockMemoryArray(scip, &consdata->nonlincoefs, consdata->nexprtrees);
1020  SCIPfreeBlockMemoryArray(scip, &consdata->curvatures, consdata->nexprtrees);
1021  consdata->nexprtrees = 0;
1022  }
1023 
1024  SCIP_CALL( consdataAddExprtrees(scip, consdata, nexprtrees, exprtrees, coefs, copytrees) );
1025 
1026  return SCIP_OKAY;
1027 }
1028 
1029 /** ensures, that linear vars and coefs arrays can store at least num entries */
1030 static
1032  SCIP* scip, /**< SCIP data structure */
1033  SCIP_CONSDATA* consdata, /**< nonlinear constraint data */
1034  int num /**< minimum number of entries to store */
1035  )
1036 {
1037  assert(scip != NULL);
1038  assert(consdata != NULL);
1039  assert(consdata->nlinvars <= consdata->linvarssize);
1040 
1041  if( num > consdata->linvarssize )
1042  {
1043  int newsize;
1044 
1045  newsize = SCIPcalcMemGrowSize(scip, num);
1046  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->linvars, consdata->linvarssize, newsize) );
1047  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->lincoefs, consdata->linvarssize, newsize) );
1048  if( consdata->lineventdata != NULL )
1049  {
1050  SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->lineventdata, consdata->linvarssize, newsize) );
1051  }
1052  consdata->linvarssize = newsize;
1053  }
1054  assert(num <= consdata->linvarssize);
1055 
1056  return SCIP_OKAY;
1057 }
1058 
1059 /** creates constraint data structure */
1060 static
1062  SCIP* scip, /**< SCIP data structure */
1063  SCIP_CONSDATA** consdata, /**< a buffer to store pointer to new constraint data */
1064  SCIP_Real lhs, /**< left hand side of constraint */
1065  SCIP_Real rhs, /**< right hand side of constraint */
1066  int nlinvars, /**< number of linear variables */
1067  SCIP_VAR** linvars, /**< array of linear variables */
1068  SCIP_Real* lincoefs, /**< array of coefficients of linear variables */
1069  int nexprtrees, /**< number of expression trees */
1070  SCIP_EXPRTREE** exprtrees, /**< expression trees */
1071  SCIP_Real* nonlincoefs, /**< coefficients of expression trees */
1072  SCIP_Bool capturevars /**< whether we should capture variables */
1073  )
1074 {
1075  int i;
1076 
1077  assert(scip != NULL);
1078  assert(consdata != NULL);
1079 
1080  assert(nlinvars == 0 || linvars != NULL);
1081  assert(nlinvars == 0 || lincoefs != NULL);
1082  assert(nexprtrees == 0 || exprtrees != NULL);
1083  assert(nexprtrees == 0 || nonlincoefs != NULL);
1084 
1085  SCIP_CALL( SCIPallocBlockMemory(scip, consdata) );
1086  BMSclearMemory(*consdata);
1087 
1088  (*consdata)->minlinactivity = SCIP_INVALID;
1089  (*consdata)->maxlinactivity = SCIP_INVALID;
1090  (*consdata)->minlinactivityinf = -1;
1091  (*consdata)->maxlinactivityinf = -1;
1092 
1093  (*consdata)->lhs = lhs;
1094  (*consdata)->rhs = rhs;
1095 
1096  if( nlinvars > 0 )
1097  {
1098  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->linvars, linvars, nlinvars) );
1099  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->lincoefs, lincoefs, nlinvars) );
1100  (*consdata)->nlinvars = nlinvars;
1101  (*consdata)->linvarssize = nlinvars;
1102 
1103  if( capturevars )
1104  for( i = 0; i < nlinvars; ++i )
1105  {
1106  SCIP_CALL( SCIPcaptureVar(scip, linvars[i]) );
1107  }
1108  }
1109  else
1110  {
1111  (*consdata)->linvarssorted = TRUE;
1112  (*consdata)->linvarsmerged = TRUE;
1113  }
1114 
1115  SCIP_CALL( consdataSetExprtrees(scip, *consdata, nexprtrees, exprtrees, nonlincoefs, TRUE) );
1116 
1117  (*consdata)->linvar_maydecrease = -1;
1118  (*consdata)->linvar_mayincrease = -1;
1119 
1120  (*consdata)->activity = SCIP_INVALID;
1121  (*consdata)->lhsviol = SCIPisInfinity(scip, -lhs) ? 0.0 : SCIP_INVALID;
1122  (*consdata)->rhsviol = SCIPisInfinity(scip, rhs) ? 0.0 : SCIP_INVALID;
1123 
1124  return SCIP_OKAY;
1125 }
1126 
1127 /** creates empty constraint data structure */
1128 static
1130  SCIP* scip, /**< SCIP data structure */
1131  SCIP_CONSDATA** consdata /**< a buffer to store pointer to new constraint data */
1132  )
1133 {
1134  assert(scip != NULL);
1135  assert(consdata != NULL);
1136 
1137  SCIP_CALL( SCIPallocBlockMemory(scip, consdata) );
1138  BMSclearMemory(*consdata);
1139 
1140  (*consdata)->lhs = -SCIPinfinity(scip);
1141  (*consdata)->rhs = SCIPinfinity(scip);
1142 
1143  (*consdata)->linvarssorted = TRUE;
1144  (*consdata)->linvarsmerged = TRUE;
1145 
1146  (*consdata)->isremovedfixingslin = TRUE;
1147 
1148  (*consdata)->linvar_maydecrease = -1;
1149  (*consdata)->linvar_mayincrease = -1;
1150 
1151  (*consdata)->minlinactivity = SCIP_INVALID;
1152  (*consdata)->maxlinactivity = SCIP_INVALID;
1153  (*consdata)->minlinactivityinf = -1;
1154  (*consdata)->maxlinactivityinf = -1;
1155 
1156  (*consdata)->curvature = SCIP_EXPRCURV_LINEAR;
1157  (*consdata)->iscurvchecked = TRUE;
1158 
1159  (*consdata)->ncuts = 0;
1160 
1161  return SCIP_OKAY;
1162 }
1163 
1164 /** frees constraint data structure */
1165 static
1167  SCIP* scip, /**< SCIP data structure */
1168  SCIP_CONSDATA** consdata /**< pointer to constraint data to free */
1169  )
1170 {
1171  int i;
1172 
1173  assert(scip != NULL);
1174  assert(consdata != NULL);
1175  assert(*consdata != NULL);
1176 
1177  /* release linear variables and free linear part */
1178  if( (*consdata)->linvarssize > 0 )
1179  {
1180  for( i = 0; i < (*consdata)->nlinvars; ++i )
1181  {
1182  assert((*consdata)->lineventdata == NULL || (*consdata)->lineventdata[i] == NULL); /* variable events should have been dropped earlier */
1183  SCIP_CALL( SCIPreleaseVar(scip, &(*consdata)->linvars[i]) );
1184  }
1185  SCIPfreeBlockMemoryArray(scip, &(*consdata)->linvars, (*consdata)->linvarssize);
1186  SCIPfreeBlockMemoryArray(scip, &(*consdata)->lincoefs, (*consdata)->linvarssize);
1187  SCIPfreeBlockMemoryArrayNull(scip, &(*consdata)->lineventdata, (*consdata)->linvarssize);
1188  }
1189  assert((*consdata)->linvars == NULL);
1190  assert((*consdata)->lincoefs == NULL);
1191  assert((*consdata)->lineventdata == NULL);
1192 
1193  if( (*consdata)->nexprtrees > 0 )
1194  {
1195  assert((*consdata)->exprtrees != NULL);
1196  assert((*consdata)->nonlincoefs != NULL);
1197  assert((*consdata)->curvatures != NULL);
1198  for( i = 0; i < (*consdata)->nexprtrees; ++i )
1199  {
1200  assert((*consdata)->exprtrees[i] != NULL);
1201  SCIP_CALL( SCIPexprtreeFree(&(*consdata)->exprtrees[i]) );
1202  assert((*consdata)->exprtrees[i] == NULL);
1203  }
1204  SCIPfreeBlockMemoryArray(scip, &(*consdata)->exprtrees, (*consdata)->nexprtrees);
1205  SCIPfreeBlockMemoryArray(scip, &(*consdata)->nonlincoefs, (*consdata)->nexprtrees);
1206  SCIPfreeBlockMemoryArray(scip, &(*consdata)->curvatures, (*consdata)->nexprtrees);
1207  }
1208  assert((*consdata)->exprtrees == NULL);
1209  assert((*consdata)->nonlincoefs == NULL);
1210  assert((*consdata)->curvatures == NULL);
1211 
1212  /* free nonlinear row representation */
1213  if( (*consdata)->nlrow != NULL )
1214  {
1215  SCIP_CALL( SCIPreleaseNlRow(scip, &(*consdata)->nlrow) );
1216  }
1217 
1218  SCIPfreeBlockMemory(scip, consdata);
1219  *consdata = NULL;
1220 
1221  return SCIP_OKAY;
1222 }
1223 
1224 /** sorts linear part of constraint data */
1225 static
1227  SCIP_CONSDATA* consdata /**< nonlinear constraint data */
1228  )
1229 {
1230  assert(consdata != NULL);
1231 
1232  if( consdata->linvarssorted )
1233  return;
1234 
1235  if( consdata->nlinvars <= 1 )
1236  {
1237  consdata->linvarssorted = TRUE;
1238  return;
1239  }
1240 
1241  if( consdata->lineventdata == NULL )
1242  {
1243  SCIPsortPtrReal((void**)consdata->linvars, consdata->lincoefs, SCIPvarComp, consdata->nlinvars);
1244  }
1245  else
1246  {
1247  int i;
1248 
1249  SCIPsortPtrPtrReal((void**)consdata->linvars, (void**)consdata->lineventdata, consdata->lincoefs, SCIPvarComp, consdata->nlinvars);
1250 
1251  /* update variable indices in event data */
1252  for( i = 0; i < consdata->nlinvars; ++i )
1253  if( consdata->lineventdata[i] != NULL )
1254  consdata->lineventdata[i]->varidx = i;
1255  }
1256 
1257  consdata->linvarssorted = TRUE;
1258 }
1259 
1260 /* this function is currently not needed, but also to nice to be deleted, so it is only deactivated */
1261 #ifdef SCIP_DISABLED_CODE
1262 /** returns the position of variable in the linear coefficients array of a constraint, or -1 if not found */
1263 static
1264 int consdataFindLinearVar(
1265  SCIP_CONSDATA* consdata, /**< nonlinear constraint data */
1266  SCIP_VAR* var /**< variable to search for */
1267  )
1268 {
1269  int pos;
1270 
1271  assert(consdata != NULL);
1272  assert(var != NULL);
1273 
1274  if( consdata->nlinvars == 0 )
1275  return -1;
1276 
1277  consdataSortLinearVars(consdata);
1278 
1279  if( !SCIPsortedvecFindPtr((void**)consdata->linvars, SCIPvarComp, (void*)var, consdata->nlinvars, &pos) )
1280  pos = -1;
1281 
1282  return pos;
1283 }
1284 #endif
1285 
1286 /** moves a linear variable from one position to another */
1287 static
1289  SCIP_CONSDATA* consdata, /**< constraint data */
1290  int oldpos, /**< position of variable that shall be moved */
1291  int newpos /**< new position of variable */
1292  )
1293 {
1294  assert(consdata != NULL);
1295  assert(oldpos >= 0);
1296  assert(oldpos < consdata->nlinvars);
1297  assert(newpos >= 0);
1298  assert(newpos < consdata->linvarssize);
1299 
1300  if( newpos == oldpos )
1301  return;
1302 
1303  consdata->linvars [newpos] = consdata->linvars [oldpos];
1304  consdata->lincoefs[newpos] = consdata->lincoefs[oldpos];
1305 
1306  if( consdata->lineventdata != NULL )
1307  {
1308  assert(newpos >= consdata->nlinvars || consdata->lineventdata[newpos] == NULL);
1309 
1310  consdata->lineventdata[newpos] = consdata->lineventdata[oldpos];
1311  consdata->lineventdata[newpos]->varidx = newpos;
1312 
1313  consdata->lineventdata[oldpos] = NULL;
1314  }
1315 
1316  consdata->linvarssorted = FALSE;
1317 }
1318 
1319 /** adds linear coefficient in nonlinear constraint */
1320 static
1322  SCIP* scip, /**< SCIP data structure */
1323  SCIP_CONS* cons, /**< nonlinear constraint */
1324  SCIP_VAR* var, /**< variable of constraint entry */
1325  SCIP_Real coef /**< coefficient of constraint entry */
1326  )
1327 {
1328  SCIP_CONSDATA* consdata;
1329  SCIP_Bool transformed;
1330 
1331  assert(scip != NULL);
1332  assert(cons != NULL);
1333  assert(var != NULL);
1334 
1335  /* ignore coefficient if it is nearly zero */
1336  if( SCIPisZero(scip, coef) )
1337  return SCIP_OKAY;
1338 
1339  consdata = SCIPconsGetData(cons);
1340  assert(consdata != NULL);
1341 
1342  /* are we in the transformed problem? */
1343  transformed = SCIPconsIsTransformed(cons);
1344 
1345  /* always use transformed variables in transformed constraints */
1346  if( transformed )
1347  {
1348  SCIP_CALL( SCIPgetTransformedVar(scip, var, &var) );
1349  }
1350  assert(var != NULL);
1351  assert(transformed == SCIPvarIsTransformed(var));
1352 
1353  SCIP_CALL( consdataEnsureLinearVarsSize(scip, consdata, consdata->nlinvars+1) );
1354  consdata->linvars [consdata->nlinvars] = var;
1355  consdata->lincoefs[consdata->nlinvars] = coef;
1356 
1357  ++consdata->nlinvars;
1358 
1359  /* catch variable events */
1360  if( SCIPconsIsEnabled(cons) )
1361  {
1362  /* catch bound change events of variable */
1363  SCIP_CALL( catchLinearVarEvents(scip, cons, consdata->nlinvars-1) );
1364  }
1365 
1366  /* invalidate activity information */
1367  consdata->activity = SCIP_INVALID;
1368  consdata->minlinactivity = SCIP_INVALID;
1369  consdata->maxlinactivity = SCIP_INVALID;
1370  consdata->minlinactivityinf = -1;
1371  consdata->maxlinactivityinf = -1;
1372 
1373  /* invalidate nonlinear row */
1374  if( consdata->nlrow != NULL )
1375  {
1376  SCIP_CALL( SCIPreleaseNlRow(scip, &consdata->nlrow) );
1377  }
1378 
1379  /* install rounding locks for new variable */
1380  SCIP_CALL( lockLinearVariable(scip, cons, var, coef) );
1381 
1382  /* capture new variable */
1383  SCIP_CALL( SCIPcaptureVar(scip, var) );
1384 
1385  consdata->ispresolved = FALSE;
1386  consdata->isremovedfixingslin = consdata->isremovedfixingslin && SCIPvarIsActive(var);
1387  if( consdata->nlinvars == 1 )
1388  consdata->linvarssorted = TRUE;
1389  else
1390  consdata->linvarssorted = consdata->linvarssorted &&
1391  (SCIPvarCompare(consdata->linvars[consdata->nlinvars-2], consdata->linvars[consdata->nlinvars-1]) == -1);
1392  /* always set too FALSE since the new linear variable should be checked if already existing as quad var term */
1393  consdata->linvarsmerged = FALSE;
1394 
1395  return SCIP_OKAY;
1396 }
1397 
1398 /** deletes linear coefficient at given position from nonlinear constraint data */
1399 static
1401  SCIP* scip, /**< SCIP data structure */
1402  SCIP_CONS* cons, /**< nonlinear constraint */
1403  int pos /**< position of coefficient to delete */
1404  )
1405 {
1406  SCIP_CONSDATA* consdata;
1407  SCIP_VAR* var;
1408  SCIP_Real coef;
1409 
1410  assert(scip != NULL);
1411  assert(cons != NULL);
1412 
1413  consdata = SCIPconsGetData(cons);
1414  assert(consdata != NULL);
1415  assert(0 <= pos && pos < consdata->nlinvars);
1416 
1417  var = consdata->linvars[pos];
1418  coef = consdata->lincoefs[pos];
1419  assert(var != NULL);
1420 
1421  /* remove rounding locks for deleted variable */
1422  SCIP_CALL( unlockLinearVariable(scip, cons, var, coef) );
1423 
1424  /* if constraint is enabled, drop the events on the variable */
1425  if( SCIPconsIsEnabled(cons) )
1426  {
1427  /* drop bound change events of variable */
1428  SCIP_CALL( dropLinearVarEvents(scip, cons, pos) );
1429  }
1430 
1431  /* release variable */
1432  SCIP_CALL( SCIPreleaseVar(scip, &consdata->linvars[pos]) );
1433 
1434  /* move the last variable to the free slot */
1435  consdataMoveLinearVar(consdata, consdata->nlinvars-1, pos);
1436 
1437  --consdata->nlinvars;
1438 
1439  /* invalidate activity */
1440  consdata->activity = SCIP_INVALID;
1441  consdata->minlinactivity = SCIP_INVALID;
1442  consdata->maxlinactivity = SCIP_INVALID;
1443  consdata->minlinactivityinf = -1;
1444  consdata->maxlinactivityinf = -1;
1445 
1446  /* invalidate nonlinear row */
1447  if( consdata->nlrow != NULL )
1448  {
1449  SCIP_CALL( SCIPreleaseNlRow(scip, &consdata->nlrow) );
1450  }
1451 
1452  consdata->ispresolved = FALSE;
1453 
1454  return SCIP_OKAY;
1455 }
1456 
1457 /** changes linear coefficient value at given position of nonlinear constraint */
1458 static
1460  SCIP* scip, /**< SCIP data structure */
1461  SCIP_CONS* cons, /**< nonlinear constraint */
1462  int pos, /**< position of linear coefficient to change */
1463  SCIP_Real newcoef /**< new value of linear coefficient */
1464  )
1465 {
1466  SCIP_CONSDATA* consdata;
1467  SCIP_VAR* var;
1468  SCIP_Real coef;
1469  SCIP_Bool locked;
1470  int i;
1471 
1472  assert(scip != NULL);
1473  assert(cons != NULL);
1474  assert(!SCIPisZero(scip, newcoef));
1475 
1476  consdata = SCIPconsGetData(cons);
1477  assert(consdata != NULL);
1478  assert(0 <= pos);
1479  assert(pos < consdata->nlinvars);
1480  assert(!SCIPisZero(scip, newcoef));
1481 
1482  var = consdata->linvars[pos];
1483  coef = consdata->lincoefs[pos];
1484  assert(var != NULL);
1485  assert(SCIPconsIsTransformed(cons) == SCIPvarIsTransformed(var));
1486 
1487  /* invalidate activity */
1488  consdata->activity = SCIP_INVALID;
1489  consdata->minlinactivity = SCIP_INVALID;
1490  consdata->maxlinactivity = SCIP_INVALID;
1491  consdata->minlinactivityinf = -1;
1492  consdata->maxlinactivityinf = -1;
1493 
1494  /* invalidate nonlinear row */
1495  if( consdata->nlrow != NULL )
1496  {
1497  SCIP_CALL( SCIPreleaseNlRow(scip, &consdata->nlrow) );
1498  }
1499 
1500  locked = FALSE;
1501  for( i = 0; i < NLOCKTYPES && !locked; i++ )
1502  locked = SCIPconsIsLockedType(cons, (SCIP_LOCKTYPE) i);
1503 
1504  /* if necessary, remove the rounding locks and event catching of the variable */
1505  if( newcoef * coef < 0.0 )
1506  {
1507  if( locked )
1508  {
1509  assert(SCIPconsIsTransformed(cons));
1510 
1511  /* remove rounding locks for variable with old coefficient */
1512  SCIP_CALL( unlockLinearVariable(scip, cons, var, coef) );
1513  }
1514 
1515  if( SCIPconsIsEnabled(cons) )
1516  {
1517  /* drop bound change events of variable */
1518  SCIP_CALL( dropLinearVarEvents(scip, cons, pos) );
1519  }
1520  }
1521 
1522  /* change the coefficient */
1523  consdata->lincoefs[pos] = newcoef;
1524 
1525  /* if necessary, install the rounding locks and event catching of the variable again */
1526  if( newcoef * coef < 0.0 )
1527  {
1528  if( locked )
1529  {
1530  /* install rounding locks for variable with new coefficient */
1531  SCIP_CALL( lockLinearVariable(scip, cons, var, newcoef) );
1532  }
1533 
1534  if( SCIPconsIsEnabled(cons) )
1535  {
1536  /* catch bound change events of variable */
1537  SCIP_CALL( catchLinearVarEvents(scip, cons, pos) );
1538  }
1539  }
1540 
1541  consdata->ispresolved = FALSE;
1542 
1543  return SCIP_OKAY;
1544 }
1545 
1546 /** changes side of constraint and allow to change between finite and infinite
1547  *
1548  * takes care of updating events and locks of linear variables
1549  */
1550 static
1552  SCIP* scip, /**< SCIP data structure */
1553  SCIP_CONS* cons, /**< constraint */
1554  SCIP_SIDETYPE side, /**< which side to change */
1555  SCIP_Real sideval /**< new value for side */
1556  )
1557 {
1558  SCIP_CONSDATA* consdata;
1559  int i;
1560 
1561  assert(scip != NULL);
1562  assert(cons != NULL);
1563  assert(!SCIPisInfinity(scip, side == SCIP_SIDETYPE_LEFT ? sideval : -sideval));
1564 
1565  consdata = SCIPconsGetData(cons);
1566  assert(consdata != NULL);
1567 
1568  /* if remaining finite or remaining infinite, then can just update the value */
1569  if( side == SCIP_SIDETYPE_LEFT )
1570  {
1571  if( SCIPisInfinity(scip, -consdata->lhs) == SCIPisInfinity(scip, -sideval) )
1572  {
1573  consdata->lhs = sideval;
1574  return SCIP_OKAY;
1575  }
1576  }
1577  else
1578  {
1579  if( SCIPisInfinity(scip, consdata->rhs) == SCIPisInfinity(scip, sideval) )
1580  {
1581  consdata->rhs = sideval;
1582  return SCIP_OKAY;
1583  }
1584  }
1585 
1586  /* catched boundchange events and locks for linear variables depends on whether side is finite, so first drop all */
1587  for( i = 0; i < consdata->nlinvars; ++i )
1588  {
1589  int j;
1590  if( SCIPconsIsEnabled(cons) )
1591  {
1592  SCIP_CALL( dropLinearVarEvents(scip, cons, i) );
1593  }
1594 
1595  for( j = 0; j < NLOCKTYPES; j++ )
1596  {
1597  if( SCIPconsIsLockedType(cons, (SCIP_LOCKTYPE) j) )
1598  {
1599  assert(SCIPconsIsTransformed(cons));
1600  SCIP_CALL( unlockLinearVariable(scip, cons, consdata->linvars[i], consdata->lincoefs[i]) );
1601  break;
1602  }
1603  }
1604  }
1605 
1606  if( side == SCIP_SIDETYPE_LEFT )
1607  consdata->lhs = sideval;
1608  else
1609  consdata->rhs = sideval;
1610 
1611  /* catch boundchange events and locks on variables again */
1612  for( i = 0; i < consdata->nlinvars; ++i )
1613  {
1614  int j;
1615  if( SCIPconsIsEnabled(cons) )
1616  {
1617  SCIP_CALL( catchLinearVarEvents(scip, cons, i) );
1618  }
1619 
1620  for( j = 0; j < NLOCKTYPES; j++ )
1621  {
1622  if( SCIPconsIsLockedType(cons, (SCIP_LOCKTYPE) j) )
1623  {
1624  SCIP_CALL( lockLinearVariable(scip, cons, consdata->linvars[i], consdata->lincoefs[i]) );
1625  break;
1626  }
1627  }
1628  }
1629 
1630  return SCIP_OKAY;
1631 }
1632 
1633 /* merges entries with same linear variable into one entry and cleans up entries with coefficient 0.0 */
1634 static
1636  SCIP* scip, /**< SCIP data structure */
1637  SCIP_CONS* cons /**< nonlinear constraint */
1638  )
1639 {
1640  SCIP_CONSDATA* consdata;
1641  SCIP_Real newcoef;
1642  int i;
1643  int j;
1644 
1645  assert(scip != NULL);
1646  assert(cons != NULL);
1647 
1648  consdata = SCIPconsGetData(cons);
1649 
1650  if( consdata->linvarsmerged )
1651  return SCIP_OKAY;
1652 
1653  if( consdata->nlinvars == 0 )
1654  {
1655  consdata->linvarsmerged = TRUE;
1656  return SCIP_OKAY;
1657  }
1658 
1659  i = 0;
1660  while( i < consdata->nlinvars )
1661  {
1662  /* make sure linear variables are sorted (do this in every round, since we may move variables around) */
1663  consdataSortLinearVars(consdata);
1664 
1665  /* sum up coefficients that correspond to variable i */
1666  newcoef = consdata->lincoefs[i];
1667  for( j = i+1; j < consdata->nlinvars && consdata->linvars[i] == consdata->linvars[j]; ++j )
1668  newcoef += consdata->lincoefs[j];
1669  /* delete the additional variables in backward order */
1670  for( j = j-1; j > i; --j )
1671  {
1672  SCIP_CALL( delLinearCoefPos(scip, cons, j) );
1673  }
1674 
1675  /* delete also entry at position i, if it became zero (or was zero before) */
1676  if( SCIPisZero(scip, newcoef) )
1677  {
1678  SCIP_CALL( delLinearCoefPos(scip, cons, i) );
1679  }
1680  else
1681  {
1682  SCIP_CALL( chgLinearCoefPos(scip, cons, i, newcoef) );
1683  ++i;
1684  }
1685  }
1686 
1687  consdata->linvarsmerged = TRUE;
1688 
1689  return SCIP_OKAY;
1690 }
1691 
1692 /** removes fixes (or aggregated) linear variables from a nonlinear constraint */
1693 static
1695  SCIP* scip, /**< SCIP data structure */
1696  SCIP_CONS* cons /**< nonlinearconstraint */
1697  )
1698 {
1699  SCIP_CONSDATA* consdata;
1700  SCIP_Real coef;
1701  SCIP_Real offset;
1702  SCIP_VAR* var;
1703  int i;
1704  int j;
1705 
1706  assert(scip != NULL);
1707  assert(cons != NULL);
1708 
1709  consdata = SCIPconsGetData(cons);
1710 
1711  if( !consdata->isremovedfixingslin )
1712  {
1713  i = 0;
1714  while( i < consdata->nlinvars )
1715  {
1716  var = consdata->linvars[i];
1717 
1718  if( SCIPvarIsActive(var) )
1719  {
1720  ++i;
1721  continue;
1722  }
1723 
1724  coef = consdata->lincoefs[i];
1725  offset = 0.0;
1726 
1727  SCIP_CALL( SCIPgetProbvarSum(scip, &var, &coef, &offset) );
1728 
1729  SCIPdebugMsg(scip, " linear term %g*<%s> is replaced by %g * <%s> + %g\n", consdata->lincoefs[i], SCIPvarGetName(consdata->linvars[i]), coef, SCIPvarGetName(var), offset);
1730 
1731  /* delete previous variable (this will move another variable to position i) */
1732  SCIP_CALL( delLinearCoefPos(scip, cons, i) );
1733 
1734  /* put constant part into bounds */
1735  if( offset != 0.0 )
1736  {
1737  if( !SCIPisInfinity(scip, -consdata->lhs) )
1738  {
1739  SCIP_CALL( chgSideNonlinear(scip, cons, SCIP_SIDETYPE_LEFT, consdata->lhs - offset) );
1740  }
1741  if( !SCIPisInfinity(scip, consdata->rhs) )
1742  {
1743  SCIP_CALL( chgSideNonlinear(scip, cons, SCIP_SIDETYPE_RIGHT, consdata->rhs - offset) );
1744  }
1745  }
1746 
1747  /* nothing left to do if variable had been fixed */
1748  if( coef == 0.0 )
1749  continue;
1750 
1751  /* if GetProbvar gave a linear variable, just add it
1752  * if it's a multilinear variable, add it's disaggregated variables */
1753  if( SCIPvarIsActive(var) )
1754  {
1755  SCIP_CALL( addLinearCoef(scip, cons, var, coef) );
1756  }
1757  else
1758  {
1759  int naggrs;
1760  SCIP_VAR** aggrvars;
1761  SCIP_Real* aggrscalars;
1762  SCIP_Real aggrconstant;
1763 
1764  assert(SCIPvarGetStatus(var) == SCIP_VARSTATUS_MULTAGGR);
1765 
1766  naggrs = SCIPvarGetMultaggrNVars(var);
1767  aggrvars = SCIPvarGetMultaggrVars(var);
1768  aggrscalars = SCIPvarGetMultaggrScalars(var);
1769  aggrconstant = SCIPvarGetMultaggrConstant(var);
1770 
1771  SCIP_CALL( consdataEnsureLinearVarsSize(scip, consdata, consdata->nlinvars + naggrs) );
1772 
1773  for( j = 0; j < naggrs; ++j )
1774  {
1775  SCIP_CALL( addLinearCoef(scip, cons, aggrvars[j], coef * aggrscalars[j]) );
1776  }
1777 
1778  if( aggrconstant != 0.0 )
1779  {
1780  if( !SCIPisInfinity(scip, -consdata->lhs) )
1781  {
1782  consdata->lhs -= coef * aggrconstant;
1783  assert(!SCIPisInfinity(scip, REALABS(consdata->lhs)));
1784  }
1785  if( !SCIPisInfinity(scip, consdata->rhs) )
1786  {
1787  consdata->rhs -= coef * aggrconstant;
1788  assert(!SCIPisInfinity(scip, REALABS(consdata->rhs)));
1789  }
1790  }
1791  }
1792  }
1793 
1794  SCIP_CALL( mergeAndCleanLinearVars(scip, cons) );
1795 
1796  consdata->isremovedfixingslin = TRUE;
1797  }
1798 
1799  SCIPdebugMsg(scip, "removed fixations of linear variables from <%s>\n -> ", SCIPconsGetName(cons));
1800  SCIPdebugPrintCons(scip, cons, NULL);
1801 
1802 #ifndef NDEBUG
1803  for( i = 0; i < consdata->nlinvars; ++i )
1804  assert(SCIPvarIsActive(consdata->linvars[i]));
1805 #endif
1806 
1807  return SCIP_OKAY;
1808 }
1809 
1810 /** removes fixed variables from expression graph */
1811 static
1813  SCIP* scip, /**< SCIP data structure */
1814  SCIP_CONSHDLR* conshdlr /**< constraint handler */
1815  )
1816 {
1818  SCIP_VAR* var;
1819  SCIP_VAR** vars;
1820  SCIP_Real* coefs;
1821  int nvars;
1822  int varssize;
1823  SCIP_Real constant;
1824  int i;
1825  int requsize;
1826  SCIPdebug( int j );
1827 
1828  conshdlrdata = SCIPconshdlrGetData(conshdlr);
1829  assert(conshdlrdata != NULL);
1830  assert(conshdlrdata->exprgraph != NULL);
1831 
1832  if( conshdlrdata->isremovedfixings )
1833  return SCIP_OKAY;
1834 
1835  varssize = 5;
1836  SCIP_CALL( SCIPallocBufferArray(scip, &vars, varssize) );
1837  SCIP_CALL( SCIPallocBufferArray(scip, &coefs, varssize) );
1838 
1839  i = 0;
1840  while( i < SCIPexprgraphGetNVars(conshdlrdata->exprgraph) )
1841  {
1842  var = (SCIP_VAR*)SCIPexprgraphGetVars(conshdlrdata->exprgraph)[i];
1843  if( SCIPvarIsActive(var) )
1844  {
1845  ++i;
1846  continue;
1847  }
1848 
1849  vars[0] = var;
1850  coefs[0] = 1.0;
1851  constant = 0.0;
1852  nvars = 1;
1853  SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, coefs, &nvars, varssize, &constant, &requsize, TRUE) );
1854 
1855  if( requsize > varssize )
1856  {
1857  SCIP_CALL( SCIPreallocBufferArray(scip, &vars, requsize) );
1858  SCIP_CALL( SCIPreallocBufferArray(scip, &coefs, requsize) );
1859  varssize = requsize;
1860 
1861  SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, coefs, &nvars, varssize, &constant, &requsize, TRUE) );
1862  }
1863 
1864 #ifdef SCIP_DEBUG
1865  SCIPdebugMsg(scip, "replace fixed variable <%s> by %g", SCIPvarGetName(var), constant);
1866  for( j = 0; j < nvars; ++j )
1867  {
1868  SCIPdebugMsgPrint(scip, " %+g <%s>", coefs[j], SCIPvarGetName(vars[j]));
1869  }
1870  SCIPdebugMsgPrint(scip, "\n");
1871 #endif
1872 
1873  SCIP_CALL( SCIPexprgraphReplaceVarByLinearSum(conshdlrdata->exprgraph, var, nvars, coefs, (void**)vars, constant) );
1874 
1875  i = 0;
1876  }
1877 
1878  SCIPfreeBufferArray(scip, &coefs);
1879  SCIPfreeBufferArray(scip, &vars);
1880 
1881  conshdlrdata->isremovedfixings = TRUE;
1882 
1883  return SCIP_OKAY;
1884 }
1885 
1886 /** moves constant and linear part from expression graph node into constraint sides and linear part
1887  * frees expression graph node if expression is constant or linear */
1888 static
1890  SCIP* scip, /**< SCIP data structure */
1891  SCIP_CONSHDLR* conshdlr, /**< constraint handler */
1892  SCIP_CONS* cons, /**< nonlinear constraint */
1893  SCIP_Bool* infeasible /**< pointer to store whether the problem is infeasible or not */
1894  )
1895 {
1897  SCIP_CONSDATA* consdata;
1898  SCIP_VAR** linvars;
1899  SCIP_Real* lincoefs;
1900  SCIP_Real constant;
1901  int linvarssize;
1902  int nlinvars;
1903  int i;
1904 
1905  assert(scip != NULL);
1906  assert(conshdlr != NULL);
1907  assert(cons != NULL);
1908 
1909  consdata = SCIPconsGetData(cons);
1910  assert(consdata != NULL);
1911 
1912  *infeasible = FALSE;
1913 
1914  if( consdata->exprgraphnode == NULL )
1915  return SCIP_OKAY;
1916 
1917  conshdlrdata = SCIPconshdlrGetData(conshdlr);
1918  assert(conshdlrdata != NULL);
1919  assert(conshdlrdata->exprgraph != NULL);
1920 
1921  /* number of children of expression graph node is a good upper estimate on number of linear variables */
1922  linvarssize = MAX(SCIPexprgraphGetNodeNChildren(consdata->exprgraphnode), 1); /*lint !e666*/
1923  SCIP_CALL( SCIPallocBufferArray(scip, &linvars, linvarssize) );
1924  SCIP_CALL( SCIPallocBufferArray(scip, &lincoefs, linvarssize) );
1925 
1926  /* get linear and constant part from expression graph node
1927  * releases expression graph node if not uses otherwise */
1928  SCIP_CALL( SCIPexprgraphNodeSplitOffLinear(conshdlrdata->exprgraph, &consdata->exprgraphnode, linvarssize, &nlinvars, (void**)linvars, lincoefs, &constant) );
1929 
1930  if( SCIPisInfinity(scip, constant) )
1931  {
1932  if( !SCIPisInfinity(scip, -consdata->lhs) )
1933  {
1934  /* setting constraint lhs to -infinity; this may change linear variable locks and events */
1935  for( i = 0; i < consdata->nlinvars; ++i )
1936  {
1937  SCIP_Bool locked = FALSE;
1938  int j;
1939 
1940  for( j = 0; j < NLOCKTYPES && !locked; j++ )
1941  locked = SCIPconsIsLockedType(cons, (SCIP_LOCKTYPE) j);
1942 
1943  if( locked )
1944  {
1945  SCIP_CALL( unlockLinearVariable(scip, cons, consdata->linvars[i], consdata->lincoefs[i]) );
1946  }
1947  if( SCIPconsIsEnabled(cons) )
1948  {
1949  SCIP_CALL( dropLinearVarEvents(scip, cons, i) );
1950  }
1951  }
1952 
1953  consdata->lhs = -SCIPinfinity(scip);
1954 
1955  for( i = 0; i < consdata->nlinvars; ++i )
1956  {
1957  SCIP_Bool locked = FALSE;
1958  int j;
1959 
1960  for( j = 0; j < NLOCKTYPES && !locked; j++ )
1961  locked = SCIPconsIsLockedType(cons, (SCIP_LOCKTYPE) j);
1962 
1963  if( SCIPconsIsEnabled(cons) )
1964  {
1965  SCIP_CALL( catchLinearVarEvents(scip, cons, i) );
1966  }
1967  if( locked )
1968  {
1969  SCIP_CALL( lockLinearVariable(scip, cons, consdata->linvars[i], consdata->lincoefs[i]) );
1970  }
1971  }
1972  }
1973 
1974  if( !SCIPisInfinity(scip, consdata->rhs) )
1975  {
1976  *infeasible = TRUE;
1977  goto TERMINATE;
1978  }
1979  }
1980  else if( SCIPisInfinity(scip, -constant) )
1981  {
1982  if( !SCIPisInfinity(scip, consdata->rhs) )
1983  {
1984  /* setting constraint rhs to infinity; this may change linear variable locks and events */
1985  for( i = 0; i < consdata->nlinvars; ++i )
1986  {
1987  SCIP_Bool locked = FALSE;
1988  int j;
1989 
1990  for( j = 0; j < NLOCKTYPES && !locked; j++ )
1991  locked = SCIPconsIsLockedType(cons, (SCIP_LOCKTYPE) j);
1992 
1993  if( locked )
1994  {
1995  SCIP_CALL( unlockLinearVariable(scip, cons, consdata->linvars[i], consdata->lincoefs[i]) );
1996  }
1997  if( SCIPconsIsEnabled(cons) )
1998  {
1999  SCIP_CALL( dropLinearVarEvents(scip, cons, i) );
2000  }
2001  }
2002 
2003  consdata->rhs = SCIPinfinity(scip);
2004 
2005  for( i = 0; i < consdata->nlinvars; ++i )
2006  {
2007  SCIP_Bool locked = FALSE;
2008  int j;
2009 
2010  for( j = 0; j < NLOCKTYPES && !locked; j++ )
2011  locked = SCIPconsIsLockedType(cons, (SCIP_LOCKTYPE) j);
2012 
2013  if( SCIPconsIsEnabled(cons) )
2014  {
2015  SCIP_CALL( catchLinearVarEvents(scip, cons, i) );
2016  }
2017  if( locked )
2018  {
2019  SCIP_CALL( lockLinearVariable(scip, cons, consdata->linvars[i], consdata->lincoefs[i]) );
2020  }
2021  }
2022  }
2023  if( !SCIPisInfinity(scip, -consdata->lhs) )
2024  {
2025  *infeasible = TRUE;
2026  goto TERMINATE;
2027  }
2028  }
2029  else if( constant != 0.0 )
2030  {
2031  if( !SCIPisInfinity(scip, -consdata->lhs) )
2032  {
2033  consdata->lhs -= constant;
2034  assert(!SCIPisInfinity(scip, REALABS(consdata->lhs)));
2035  }
2036  if( !SCIPisInfinity(scip, consdata->rhs) )
2037  {
2038  consdata->rhs -= constant;
2039  assert(!SCIPisInfinity(scip, REALABS(consdata->rhs)));
2040  }
2041  }
2042 
2043 TERMINATE:
2044  for( i = 0; i < nlinvars; ++i )
2045  {
2046  SCIP_CALL( addLinearCoef(scip, cons, linvars[i], lincoefs[i]) );
2047  }
2048 
2049  SCIPfreeBufferArray(scip, &lincoefs);
2050  SCIPfreeBufferArray(scip, &linvars);
2051 
2052  /* @todo linear variables that are also children of exprgraphnode could be moved into the expression graph for certain nonlinear operators (quadratic, polynomial), since that may allow better bound tightening */
2053 
2054  return SCIP_OKAY;
2055 }
2056 
2057 /** create a nonlinear row representation of the constraint and stores them in consdata */
2058 static
2060  SCIP* scip, /**< SCIP data structure */
2061  SCIP_CONS* cons /**< nonlinear constraint */
2062  )
2063 {
2064  SCIP_CONSDATA* consdata;
2065 
2066  assert(scip != NULL);
2067  assert(cons != NULL);
2068 
2069  consdata = SCIPconsGetData(cons);
2070  assert(consdata != NULL);
2071 
2072  if( consdata->nlrow != NULL )
2073  {
2074  SCIP_CALL( SCIPreleaseNlRow(scip, &consdata->nlrow) );
2075  }
2076 
2077  if( consdata->nexprtrees == 0 )
2078  {
2079  SCIP_CALL( SCIPcreateNlRow(scip, &consdata->nlrow, SCIPconsGetName(cons), 0.0,
2080  consdata->nlinvars, consdata->linvars, consdata->lincoefs,
2081  0, NULL, 0, NULL,
2082  NULL, consdata->lhs, consdata->rhs,
2083  consdata->curvature) );
2084  }
2085  else if( consdata->nexprtrees == 1 && consdata->nonlincoefs[0] == 1.0 )
2086  {
2087  assert(consdata->exprtrees[0] != NULL);
2088  SCIP_CALL( SCIPcreateNlRow(scip, &consdata->nlrow, SCIPconsGetName(cons), 0.0,
2089  consdata->nlinvars, consdata->linvars, consdata->lincoefs,
2090  0, NULL, 0, NULL,
2091  consdata->exprtrees[0], consdata->lhs, consdata->rhs,
2092  consdata->curvature) );
2093  }
2094  else
2095  {
2096  /* since expression trees may share variable, we cannot easily sum them up,
2097  * but we can request a single expression tree from the expression graph
2098  */
2100  SCIP_EXPRTREE* exprtree;
2101 
2102  assert(consdata->exprgraphnode != NULL); /* since nexprtrees > 0 */
2103  conshdlrdata = SCIPconshdlrGetData(SCIPconsGetHdlr(cons));
2104  assert(conshdlrdata != NULL);
2105 
2106  SCIP_CALL( SCIPexprgraphGetTree(conshdlrdata->exprgraph, consdata->exprgraphnode, &exprtree) );
2107  SCIP_CALL( SCIPcreateNlRow(scip, &consdata->nlrow, SCIPconsGetName(cons), 0.0,
2108  consdata->nlinvars, consdata->linvars, consdata->lincoefs,
2109  0, NULL, 0, NULL,
2110  exprtree, consdata->lhs, consdata->rhs,
2111  consdata->curvature) );
2112  SCIP_CALL( SCIPexprtreeFree(&exprtree) );
2113  }
2114 
2115  return SCIP_OKAY;
2116 }
2117 
2118 /** tries to automatically convert a nonlinear constraint (or a part of it) into a more specific and more specialized constraint */
2119 static
2121  SCIP* scip, /**< SCIP data structure */
2122  SCIP_CONSHDLR* conshdlr, /**< constraint handler data structure */
2123  SCIP_CONS* cons, /**< source constraint to try to convert */
2124  SCIP_Bool* upgraded, /**< buffer to store whether constraint was upgraded */
2125  int* nupgdconss, /**< buffer to increase if constraint was upgraded */
2126  int* naddconss /**< buffer to increase with number of additional constraints created during upgrade */
2127  )
2128 {
2130  SCIP_CONS** upgdconss;
2131  int upgdconsssize;
2132  int nupgdconss_;
2133  int i;
2134 
2135  assert(scip != NULL);
2136  assert(conshdlr != NULL);
2137  assert(cons != NULL);
2138  assert(!SCIPconsIsModifiable(cons));
2139  assert(upgraded != NULL);
2140  assert(nupgdconss != NULL);
2141  assert(naddconss != NULL);
2142 
2143  *upgraded = FALSE;
2144 
2145  nupgdconss_ = 0;
2146 
2147  conshdlrdata = SCIPconshdlrGetData(conshdlr);
2148  assert(conshdlrdata != NULL);
2149 
2150  /* if there are no upgrade methods, we can stop */
2151  if( conshdlrdata->nnlconsupgrades == 0 )
2152  return SCIP_OKAY;
2153 
2154  /* set debug solution in expression graph and evaluate nodes, so reformulation methods can compute debug solution values for new auxiliary variables */
2155 #ifdef WITH_DEBUG_SOLUTION
2156  if( SCIPdebugIsMainscip(scip) )
2157  {
2158  SCIP_Real* varvals;
2159 
2160  SCIP_CALL( SCIPallocBufferArray(scip, &varvals, SCIPexprgraphGetNVars(conshdlrdata->exprgraph)) );
2161 
2162  for( i = 0; i < SCIPexprgraphGetNVars(conshdlrdata->exprgraph); ++i )
2163  SCIP_CALL( SCIPdebugGetSolVal(scip, (SCIP_VAR*)SCIPexprgraphGetVars(conshdlrdata->exprgraph)[i], &varvals[i]) );
2164 
2165  SCIP_CALL( SCIPexprgraphEval(conshdlrdata->exprgraph, varvals) );
2166 
2167  SCIPfreeBufferArray(scip, &varvals);
2168  }
2169 #endif
2170 
2171  upgdconsssize = 2;
2172  SCIP_CALL( SCIPallocBufferArray(scip, &upgdconss, upgdconsssize) );
2173 
2174  /* call the upgrading methods */
2175 
2176  SCIPdebugMsg(scip, "upgrading nonlinear constraint <%s> (up to %d upgrade methods):\n",
2177  SCIPconsGetName(cons), conshdlrdata->nnlconsupgrades);
2178  SCIPdebugPrintCons(scip, cons, NULL);
2179 
2180  /* try all upgrading methods in priority order in case the upgrading step is enable */
2181  for( i = 0; i < conshdlrdata->nnlconsupgrades; ++i )
2182  {
2183  if( !conshdlrdata->nlconsupgrades[i]->active )
2184  continue;
2185  if( conshdlrdata->nlconsupgrades[i]->nlconsupgd == NULL )
2186  continue;
2187 
2188  SCIP_CALL( conshdlrdata->nlconsupgrades[i]->nlconsupgd(scip, cons, &nupgdconss_, upgdconss, upgdconsssize) );
2189 
2190  while( nupgdconss_ < 0 )
2191  {
2192  /* upgrade function requires more memory: resize upgdconss and call again */
2193  assert(-nupgdconss_ > upgdconsssize);
2194  upgdconsssize = -nupgdconss_;
2195  SCIP_CALL( SCIPreallocBufferArray(scip, &upgdconss, -nupgdconss_) );
2196 
2197  SCIP_CALL( conshdlrdata->nlconsupgrades[i]->nlconsupgd(scip, cons, &nupgdconss_, upgdconss, upgdconsssize) );
2198 
2199  assert(nupgdconss_ != 0);
2200  }
2201 
2202  if( nupgdconss_ > 0 )
2203  {
2204  /* got upgrade */
2205  SCIP_CONSDATA* consdata;
2206  int j;
2207 
2208  SCIPdebugMsg(scip, " -> upgraded to %d constraints:\n", nupgdconss_);
2209 
2210  /* add the upgraded constraints to the problem and forget them */
2211  for( j = 0; j < nupgdconss_; ++j )
2212  {
2213  SCIPdebugMsgPrint(scip, "\t");
2214  SCIPdebugPrintCons(scip, upgdconss[j], NULL);
2215 
2216  SCIP_CALL( SCIPaddCons(scip, upgdconss[j]) ); /*lint !e613*/
2217  SCIP_CALL( SCIPreleaseCons(scip, &upgdconss[j]) ); /*lint !e613*/
2218  }
2219 
2220  /* count the first upgrade constraint as constraint upgrade and the remaining ones as added constraints */
2221  *nupgdconss += 1;
2222  *naddconss += nupgdconss_ - 1;
2223  *upgraded = TRUE;
2224 
2225  /* delete upgraded constraint */
2226  SCIPdebugMsg(scip, "delete constraint <%s> after upgrade\n", SCIPconsGetName(cons));
2227  SCIP_CALL( SCIPdelCons(scip, cons) );
2228 
2229  /* make sure node is disabled now, so that reformulation within this presolve round does not work on it */
2230  consdata = SCIPconsGetData(cons);
2231  assert(consdata != NULL);
2232  if( consdata->exprgraphnode != NULL )
2233  SCIPexprgraphDisableNode(conshdlrdata->exprgraph, consdata->exprgraphnode);
2234 
2235  break;
2236  }
2237  }
2238 
2239  SCIPfreeBufferArray(scip, &upgdconss);
2240 
2241  return SCIP_OKAY;
2242 }
2243 
2244 /** checks a nonlinear constraint for convexity and/or concavity */
2245 static
2247  SCIP* scip, /**< SCIP data structure */
2248  SCIP_CONS* cons, /**< nonlinear constraint */
2249  SCIP_Bool assumeconvex /**< whether to assume convexity in inequalities */
2250  )
2251 {
2252  SCIP_CONSDATA* consdata;
2253  SCIP_INTERVAL* varbounds;
2254  int varboundssize;
2255  SCIP_VAR* var;
2256  int i;
2257  int j;
2258 
2259  assert(scip != NULL);
2260  assert(cons != NULL);
2261 
2262  consdata = SCIPconsGetData(cons);
2263  assert(consdata != NULL);
2264 
2265  if( consdata->iscurvchecked )
2266  return SCIP_OKAY;
2267 
2268  SCIPdebugMsg(scip, "Checking curvature of constraint <%s>\n", SCIPconsGetName(cons));
2269 
2270  consdata->curvature = SCIP_EXPRCURV_LINEAR;
2271  consdata->iscurvchecked = TRUE;
2272 
2273  varbounds = NULL;
2274  varboundssize = 0;
2275 
2276  for( i = 0; i < consdata->nexprtrees; ++i )
2277  {
2278  assert(consdata->exprtrees[i] != NULL);
2279  assert(SCIPexprtreeGetNVars(consdata->exprtrees[i]) > 0 );
2280 
2281  if( assumeconvex )
2282  {
2283  /* for constraints a*f(x) <= rhs, we assume that it is convex */
2284  if( SCIPisInfinity(scip, -consdata->lhs) )
2285  consdata->curvatures[i] = SCIP_EXPRCURV_CONVEX;
2286 
2287  /* for constraints lhs <= a*f(x), we assume that it is concave */
2288  if( SCIPisInfinity(scip, consdata->rhs) )
2289  consdata->curvatures[i] = SCIP_EXPRCURV_CONCAVE;
2290  }
2291  else
2292  {
2293  if( varboundssize == 0 )
2294  {
2295  SCIP_CALL( SCIPallocBufferArray(scip, &varbounds, SCIPexprtreeGetNVars(consdata->exprtrees[i])) );
2296  varboundssize = SCIPexprtreeGetNVars(consdata->exprtrees[i]);
2297  }
2298  else if( varboundssize < SCIPexprtreeGetNVars(consdata->exprtrees[i]) )
2299  {
2300  SCIP_CALL( SCIPreallocBufferArray(scip, &varbounds, SCIPexprtreeGetNVars(consdata->exprtrees[i])) );
2301  varboundssize = SCIPexprtreeGetNVars(consdata->exprtrees[i]);
2302  }
2303  assert(varbounds != NULL);
2304 
2305  for( j = 0; j < SCIPexprtreeGetNVars(consdata->exprtrees[i]); ++j )
2306  {
2307  var = SCIPexprtreeGetVars(consdata->exprtrees[i])[j];
2308  SCIPintervalSetBounds(&varbounds[j],
2309  -infty2infty(SCIPinfinity(scip), INTERVALINFTY, -MIN(SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var))), /*lint !e666*/
2310  +infty2infty(SCIPinfinity(scip), INTERVALINFTY, MAX(SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var))) ); /*lint !e666*/
2311  }
2312 
2313  SCIP_CALL( SCIPexprtreeCheckCurvature(consdata->exprtrees[i], INTERVALINFTY, varbounds, &consdata->curvatures[i], NULL) );
2314  consdata->curvatures[i] = SCIPexprcurvMultiply(consdata->nonlincoefs[i], consdata->curvatures[i]);
2315 
2316  if( consdata->curvatures[i] == SCIP_EXPRCURV_UNKNOWN && SCIPconshdlrGetData(SCIPconsGetHdlr(cons))->isreformulated && SCIPexprGetOperator(SCIPexprtreeGetRoot(consdata->exprtrees[i])) != SCIP_EXPR_USER )
2317  {
2318  SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "indefinite expression tree in constraint <%s>\n", SCIPconsGetName(cons));
2319  SCIPdebug( SCIP_CALL( SCIPexprtreePrintWithNames(consdata->exprtrees[i], SCIPgetMessagehdlr(scip), NULL) ) );
2320  SCIPdebugMsgPrint(scip, "\n");
2321  }
2322  }
2323 
2324  /* @todo implement some more expensive checks */
2325 
2326  consdata->curvature = SCIPexprcurvAdd(consdata->curvature, consdata->curvatures[i]);
2327 
2328  SCIPdebugMsg(scip, "-> tree %d with coef %g is %s -> nonlinear part is %s\n", i, consdata->nonlincoefs[i], SCIPexprcurvGetName(consdata->curvatures[i]), SCIPexprcurvGetName(consdata->curvature));
2329  }
2330 
2331  SCIPfreeBufferArrayNull(scip, &varbounds);
2332 
2333  return SCIP_OKAY;
2334 } /*lint !e715*/
2335 
2336 /* replaces a node by another node in expression graph
2337  * moves all parents of node to replacement
2338  * replaces all exprgraphnode's in constraints that use node by replacement, except for the last "update constraints" many, because they may use node on purpose
2339  * node may be freed, if captured only by given constraints
2340  */
2341 static
2343  SCIP_EXPRGRAPH* exprgraph, /**< expression graph */
2344  SCIP_EXPRGRAPHNODE** node, /**< pointer to node to be replaced in expression graph */
2345  SCIP_EXPRGRAPHNODE* replacement, /**< node which takes node's place */
2346  SCIP_CONSHDLR* conshdlr, /**< constraint handler */
2347  int nskipupdatecons /**< number of last update constraints to be skipped in check */
2348  )
2349 {
2350  SCIP_CONSDATA* consdata;
2351  int c;
2352 
2353  assert(exprgraph != NULL);
2354  assert(node != NULL);
2355  assert(*node != NULL);
2356  assert(replacement != NULL);
2357  assert(conshdlr != NULL);
2358  assert(nskipupdatecons <= SCIPconshdlrGetNUpdateConss(conshdlr));
2359 
2360  SCIP_CALL( SCIPexprgraphMoveNodeParents(exprgraph, node, replacement) );
2361 
2362  /* node was not captured by any constraint */
2363  if( *node == NULL )
2364  return SCIP_OKAY;
2365 
2366  /* if node still exists, then because it is captured by some constraint (it should not have parents anymore)
2367  * thus, look into all active constraints and also those that have been just added during reformulation (delayed activation)
2368  * and replace their exprgraphnode by replacement
2369  * @todo may be expensive when this is done more often
2370  */
2371  assert(*node == NULL || SCIPexprgraphGetNodeNParents(*node) == 0);
2372  for( c = 0; c < SCIPconshdlrGetNActiveConss(conshdlr) + SCIPconshdlrGetNUpdateConss(conshdlr) - nskipupdatecons; ++c )
2373  {
2374  SCIP_CONS* cons;
2375 
2376  if( c < SCIPconshdlrGetNActiveConss(conshdlr) )
2377  cons = SCIPconshdlrGetConss(conshdlr)[c];
2378  else
2379  cons = SCIPconshdlrGetUpdateConss(conshdlr)[c-SCIPconshdlrGetNActiveConss(conshdlr)];
2380 
2381  assert(cons != NULL); /*lint !e613*/
2382 
2383  consdata = SCIPconsGetData(cons); /*lint !e613*/
2384  assert(consdata != NULL);
2385 
2386  if( consdata->exprgraphnode == *node )
2387  {
2388  SCIP_CALL( SCIPexprgraphReleaseNode(exprgraph, &consdata->exprgraphnode) );
2389  consdata->exprgraphnode = replacement;
2390  SCIPexprgraphCaptureNode(replacement);
2391 
2392  /* since we change the node, also the constraint changes, so ensure that it is presolved again */
2393  consdata->ispresolved = FALSE;
2394  }
2395  }
2396  *node = NULL;
2397 
2398  return SCIP_OKAY;
2399 }
2400 
2401 /** creates a new auxiliary variable and a new auxiliary nonlinear constraint connecting the var and a given node
2402  * node is replaced by new new auxiliary variables node in all parents of node in expression graph and in all constraints that use node
2403  */
2404 static
2406  SCIP* scip, /**< SCIP data structure */
2407  SCIP_EXPRGRAPH* exprgraph, /**< expression graph */
2408  SCIP_EXPRGRAPHNODE* node, /**< expression graph node */
2409  SCIP_CONSHDLR* conshdlr, /**< constraint handler */
2410  int* naddcons, /**< counter to increase when constraint is added */
2411  SCIP_Bool donotmultaggr /**< whether to mark auxiliary variable as not to multiaggregate */
2412  )
2413 {
2414  char name[SCIP_MAXSTRLEN];
2415  SCIP_VAR* auxvar;
2416  SCIP_CONS* auxcons;
2417  SCIP_EXPRGRAPHNODE* auxvarnode;
2418  SCIP_INTERVAL bounds;
2419  SCIP_Real minusone;
2420  SCIP_Bool cutoff;
2421 
2422  assert(scip != NULL);
2423  assert(exprgraph != NULL);
2424  assert(node != NULL);
2425  assert(naddcons != NULL);
2426  assert(SCIPexprgraphGetNodeDepth(node) >= 1); /* do not turn vars or consts into new vars */
2427 
2428  bounds = SCIPexprgraphGetNodeBounds(node);
2429  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nlreform%d", *naddcons);
2430 
2431  SCIPdebugMsg(scip, "add auxiliary variable and constraint %s for node %p(%d,%d)\n", name, (void*)node, SCIPexprgraphGetNodeDepth(node), SCIPexprgraphGetNodePosition(node));
2432 
2433  SCIP_CALL( SCIPcreateVar(scip, &auxvar, name, SCIPintervalGetInf(bounds), SCIPintervalGetSup(bounds), 0.0,
2435  SCIP_CALL( SCIPaddVar(scip, auxvar) );
2436  SCIP_CALL( SCIPexprgraphAddVars(exprgraph, 1, (void**)&auxvar, &auxvarnode) );
2437 #ifdef WITH_DEBUG_SOLUTION
2438  if( SCIPdebugIsMainscip(scip) )
2439  {
2440  /* store debug sol value of node as value for auxvar in debug solution and as value for auxvarnode */
2442  SCIP_CALL( SCIPdebugAddSolVal(scip, auxvar, SCIPexprgraphGetNodeVal(node)) );
2443  }
2444 #endif
2445 
2446  if( donotmultaggr )
2447  {
2448  SCIP_CALL( SCIPmarkDoNotMultaggrVar(scip, auxvar) );
2449  }
2450 
2451  /* set also bounds of auxvarnode to bounds, so it is available for new parent nodes (currently node->parents)
2452  * when updating their curvature information; avoid having to run domain propagation through exprgraph
2453  */
2454  SCIPexprgraphTightenNodeBounds(exprgraph, auxvarnode, bounds, BOUNDTIGHTENING_MINSTRENGTH, INTERVALINFTY, &cutoff);
2455  assert(!cutoff); /* we tightenend bounds from [-inf,+inf] to bounds, this should not be infeasible */
2456 
2457  /* add new constraint auxvar == node */
2458  minusone = -1.0;
2459  SCIP_CALL( SCIPcreateConsNonlinear2(scip, &auxcons, name, 1, &auxvar, &minusone, node, 0.0, 0.0, TRUE, TRUE, TRUE, TRUE, TRUE,
2460  FALSE, FALSE, FALSE, FALSE, FALSE) );
2461  SCIP_CALL( SCIPaddCons(scip, auxcons) );
2462 
2463  /* move parents of node in expression graph to auxvarnode
2464  * replace node by auxvarnode in constraints that use node */
2465  SCIP_CALL( reformReplaceNode(exprgraph, &node, auxvarnode, conshdlr, 1) );
2466 
2467  SCIP_CALL( SCIPreleaseCons(scip, &auxcons) );
2468  SCIP_CALL( SCIPreleaseVar(scip, &auxvar) );
2469 
2470  ++*naddcons;
2471 
2472  return SCIP_OKAY;
2473 }
2474 
2475 /** ensures that all children of a node have at least a given curvature by adding auxiliary variables */
2476 static
2478  SCIP* scip, /**< SCIP data structure */
2479  SCIP_EXPRGRAPH* exprgraph, /**< expression graph */
2480  SCIP_EXPRGRAPHNODE* node, /**< expression graph node */
2481  SCIP_EXPRCURV mincurv, /**< minimal desired curvature */
2482  SCIP_CONSHDLR* conshdlr, /**< constraint handler */
2483  int* naddcons /**< counter to increase when constraint is added */
2484  )
2485 {
2486  SCIP_EXPRGRAPHNODE* child;
2487  SCIP_Bool needupdate;
2488 
2489  int i;
2490  assert(scip != NULL);
2491  assert(exprgraph != NULL);
2492  assert(node != NULL);
2493  assert(naddcons != NULL);
2494  assert(SCIPexprgraphGetNodeDepth(node) >= 1); /* do not turn vars or consts into new vars */
2495  assert(mincurv != SCIP_EXPRCURV_UNKNOWN); /* this is trivial and makes no sense */
2496 
2497  needupdate = FALSE; /* whether we need to update curvature of node */
2498 
2499  for( i = 0; i < SCIPexprgraphGetNodeNChildren(node); ++i )
2500  {
2501  child = SCIPexprgraphGetNodeChildren(node)[i];
2502  assert(child != NULL);
2503 
2504  if( (SCIPexprgraphGetNodeCurvature(child) & mincurv) != mincurv )
2505  {
2506  SCIPdebugMsg(scip, "add auxiliary variable for child %p(%d,%d) with curvature %s\n",
2508 
2509  SCIP_CALL( reformNode2Var(scip, exprgraph, child, conshdlr, naddcons, FALSE) );
2510  needupdate = TRUE;
2511 
2512  /* i'th child of node should now be a variable */
2513  assert(SCIPexprgraphGetNodeChildren(node)[i] != child);
2515  }
2516 
2517  assert(SCIPexprgraphGetNodeCurvature(SCIPexprgraphGetNodeChildren(node)[i]) & mincurv);
2518  }
2519 
2520  if( needupdate )
2521  {
2524  }
2525 
2526  return SCIP_OKAY;
2527 }
2528 
2529 /** reformulates a monomial by adding auxiliary variables and constraints for bilinear terms */
2530 static
2532  SCIP* scip, /**< SCIP data structure */
2533  SCIP_EXPRGRAPH* exprgraph, /**< expression graph */
2534  int nfactors, /**< number of factors */
2535  SCIP_EXPRGRAPHNODE** factors, /**< factors */
2536  SCIP_Real* exponents, /**< exponents, or NULL if all 1.0 */
2537  SCIP_EXPRGRAPHNODE** resultnode, /**< buffer to store node which represents the reformulated monomial */
2538  SCIP_Bool createauxcons, /**< whether to create auxiliary var/cons */
2539  int mindepth, /**< minimal depth of new nodes in expression graph, or -1 */
2540  int* naddcons /**< buffer to increase by number of added cons */
2541  )
2542 {
2543  char name[SCIP_MAXSTRLEN];
2544  SCIP_VAR* auxvar;
2545  SCIP_CONS* auxcons;
2546  SCIP_Real minusone;
2547 
2548  assert(scip != NULL);
2549  assert(exprgraph != NULL);
2550  assert(nfactors > 0);
2551  assert(factors != NULL);
2552  assert(resultnode != NULL);
2553  assert(naddcons != NULL);
2554 
2555  /* factors are just one node */
2556  if( nfactors == 1 && (exponents == NULL || exponents[0] == 1.0) )
2557  {
2558  *resultnode = factors[0];
2559  return SCIP_OKAY;
2560  }
2561 
2562  /* only one factor, but with exponent < 0.0 and factor has mixed sign, e.g., x^(-3)
2563  * reformulate as auxvar * factor^(-exponent) = 1 and return the node for auxvar in resultnode
2564  */
2565  if( nfactors == 1 && exponents[0] < 0.0 && SCIPexprgraphGetNodeBounds(factors[0]).inf < 0.0 && SCIPexprgraphGetNodeBounds(factors[0]).sup > 0.0 ) /*lint !e613*/
2566  {
2567  SCIP_EXPRGRAPHNODE* auxnode;
2568  SCIP_EXPRGRAPHNODE* reformfactors[2];
2569  SCIP_Real reformexp[2];
2570 
2571  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nlreform%d", *naddcons);
2572  SCIPdebugMsg(scip, "add auxiliary variable and constraint %s\n", name);
2573 
2574  SCIP_CALL( SCIPcreateVar(scip, &auxvar, name, -SCIPinfinity(scip), SCIPinfinity(scip), 0.0,
2576  SCIP_CALL( SCIPaddVar(scip, auxvar) );
2577  SCIP_CALL( SCIPexprgraphAddVars(exprgraph, 1, (void**)&auxvar, resultnode) );
2578 
2579 #ifdef WITH_DEBUG_SOLUTION
2580  /* store debug sol value of node as value for auxvar in debug solution and as value for resultnode */
2581  if( SCIPdebugIsMainscip(scip) )
2582  {
2583  SCIP_Real debugval;
2584  debugval = pow(SCIPexprgraphGetNodeVal(factors[0]), exponents[0]);
2585  SCIPexprgraphSetVarNodeValue(*resultnode, debugval);
2586  SCIP_CALL( SCIPdebugAddSolVal(scip, auxvar, debugval) );
2587  }
2588 #endif
2589 
2590  /* increase naddcons before next call to reformMonomial, to avoid duplicate variable names, which is bad for debugging */
2591  ++*naddcons;
2592 
2593  /* add reformulation for resultnode(=auxvar) * factor^(-exponent) = 1.0
2594  * if exponent != -1.0, then factor^(-exponent) should be moved into extra variable
2595  * finally one should get an EXPR_MUL node */
2596  reformfactors[0] = *resultnode;
2597  reformfactors[1] = factors[0];
2598  reformexp[0] = 1.0;
2599  reformexp[1] = -exponents[0]; /*lint !e613*/
2600  SCIP_CALL( reformMonomial(scip, exprgraph, 2, reformfactors, reformexp, &auxnode, FALSE, mindepth, naddcons) );
2601 
2602  SCIP_CALL( SCIPcreateConsNonlinear2(scip, &auxcons, name, 0, NULL, NULL, auxnode, 1.0, 1.0,
2603  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
2604  SCIP_CALL( SCIPaddCons(scip, auxcons) );
2605 
2606  SCIP_CALL( SCIPreleaseCons(scip, &auxcons) );
2607  SCIP_CALL( SCIPreleaseVar(scip, &auxvar) );
2608 
2609  return SCIP_OKAY;
2610  }
2611 
2612  /* only one factor, but with exponent != 1.0 */
2613  if( nfactors == 1 )
2614  {
2615  /* create some power expression node, if not existing already */
2616  SCIP_EXPRGRAPHNODE* expnode;
2617  SCIP_EXPRGRAPHNODE* parent;
2618  int p;
2619 
2620  assert(exponents != NULL);
2621 
2622  /* check if there is already a node for factors[0]^exponents[0] */
2623  expnode = NULL;
2624  for( p = 0; p < SCIPexprgraphGetNodeNParents(factors[0]); ++p)
2625  {
2626  parent = SCIPexprgraphGetNodeParents(factors[0])[p];
2627  if( SCIPisIntegral(scip, exponents[0]) &&
2629  SCIPexprgraphGetNodeIntPowerExponent(parent) == (int)SCIPround(scip, exponents[0]) )
2630  {
2631  expnode = parent;
2632  break;
2633  }
2635  SCIPisEQ(scip, SCIPexprgraphGetNodeRealPowerExponent(parent), exponents[0]) )
2636  {
2637  expnode = parent;
2638  }
2639  }
2640  if( expnode == NULL )
2641  {
2642  if( SCIPisIntegral(scip, exponents[0]) )
2643  SCIP_CALL( SCIPexprgraphCreateNode(SCIPblkmem(scip), &expnode, SCIP_EXPR_INTPOWER, (int)SCIPround(scip, exponents[0])) );
2644  else
2645  SCIP_CALL( SCIPexprgraphCreateNode(SCIPblkmem(scip), &expnode, SCIP_EXPR_REALPOWER, exponents[0]) );
2646 
2647  SCIP_CALL( SCIPexprgraphAddNode(exprgraph, expnode, mindepth, 1, &factors[0]) );
2650  }
2651 
2652  if( createauxcons )
2653  {
2654  /* @todo if there was already a node for factors[0]^exponents[0], then there may have been also been already an auxiliary variable and constraint (-> ex7_3_4) */
2655  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nlreform%d", *naddcons);
2656  SCIPdebugMsg(scip, "add auxiliary variable and constraint %s\n", name);
2657 
2658  SCIP_CALL( SCIPcreateVar(scip, &auxvar, name, -SCIPinfinity(scip), SCIPinfinity(scip), 0.0,
2660  SCIP_CALL( SCIPaddVar(scip, auxvar) );
2661  SCIP_CALL( SCIPexprgraphAddVars(exprgraph, 1, (void**)&auxvar, resultnode) );
2662 
2663 #ifdef WITH_DEBUG_SOLUTION
2664  if( SCIPdebugIsMainscip(scip) )
2665  {
2667  SCIP_CALL( SCIPdebugAddSolVal(scip, auxvar, SCIPexprgraphGetNodeVal(expnode)) );
2668  }
2669 #endif
2670 
2671  /* add new constraint resultnode(=auxvar) = expnode */
2672  minusone = -1.0;
2673  SCIP_CALL( SCIPcreateConsNonlinear2(scip, &auxcons, name, 1, &auxvar, &minusone, expnode, 0.0, 0.0, TRUE, TRUE, TRUE, TRUE, TRUE,
2674  FALSE, FALSE, FALSE, FALSE, FALSE) );
2675  SCIP_CALL( SCIPaddCons(scip, auxcons) );
2676 
2677  SCIP_CALL( SCIPreleaseCons(scip, &auxcons) );
2678  SCIP_CALL( SCIPreleaseVar(scip, &auxvar) );
2679 
2680  ++*naddcons;
2681  }
2682  else
2683  {
2684  *resultnode = expnode;
2685  }
2686 
2687  return SCIP_OKAY;
2688  }
2689 
2690  if( nfactors == 2 && exponents != NULL && exponents[0] != 1.0 && exponents[0] == exponents[1] ) /*lint !e777*/
2691  {
2692  /* factor0^exponent * factor1^exponent with exponent != 1.0, reform as (factor0*factor1)^exponent */
2693  SCIP_EXPRGRAPHNODE* productnode;
2694 
2695  /* create node for factor0*factor1 */
2696  SCIP_CALL( reformMonomial(scip, exprgraph, 2, factors, NULL, &productnode, TRUE, mindepth, naddcons) );
2697 
2698  /* create node for productnode^exponents[0] by just calling this method again */
2699  SCIP_CALL( reformMonomial(scip, exprgraph, 1, &productnode, &exponents[0], resultnode, createauxcons, mindepth, naddcons) );
2700 
2701  return SCIP_OKAY;
2702  }
2703 
2704  if( nfactors == 2 && exponents != NULL && exponents[0] == -exponents[1] ) /*lint !e777*/
2705  {
2706  /* factor0^exponent * factor1^(-exponent), reform as (factor0/factor1)^exponent or (factor1/factor0)^(-exponent) */
2707  SCIP_EXPRGRAPHNODE* auxvarnode;
2708  SCIP_EXPRGRAPHNODE* auxconsnode;
2709  SCIP_EXPRGRAPHNODE* leftright[2];
2710  SCIP_Real absexp;
2711 
2712  /* create variable and constraint for factor0 = auxvar * factor1 (if exponent > 0) or factor1 = auxvar * factor0 (if exponent < 0) */
2713 
2714  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nlreform%d", *naddcons);
2715  SCIPdebugMsg(scip, "add auxiliary variable and constraint %s\n", name);
2716 
2717  SCIP_CALL( SCIPcreateVar(scip, &auxvar, name, -SCIPinfinity(scip), SCIPinfinity(scip), 0.0,
2719  SCIP_CALL( SCIPaddVar(scip, auxvar) );
2720  SCIP_CALL( SCIPexprgraphAddVars(exprgraph, 1, (void**)&auxvar, &auxvarnode) );
2721 
2722 #ifdef WITH_DEBUG_SOLUTION
2723  /* store debug sol value of node as value for auxvar in debug solution and as value for resultnode */
2724  if( SCIPdebugIsMainscip(scip) )
2725  {
2726  SCIP_Real debugval;
2727  if( exponents[0] > 0.0 )
2728  debugval = SCIPexprgraphGetNodeVal(factors[0]) / SCIPexprgraphGetNodeVal(factors[1]);
2729  else
2730  debugval = SCIPexprgraphGetNodeVal(factors[1]) / SCIPexprgraphGetNodeVal(factors[0]);
2731  SCIPexprgraphSetVarNodeValue(auxvarnode, debugval);
2732  SCIP_CALL( SCIPdebugAddSolVal(scip, auxvar, debugval) );
2733  }
2734 #endif
2735 
2736  /* add new constraint resultnode(= auxvar) * factor1 - factor0 == 0 (exponent > 0) or auxvar * factor0 - factor1 == 0 (exponent < 0) */
2737  leftright[0] = auxvarnode;
2738  leftright[1] = exponents[0] > 0.0 ? factors[1] : factors[0];
2739 
2741  SCIP_CALL( SCIPexprgraphAddNode(exprgraph, auxconsnode, -1, 2, leftright) );
2742 
2743  leftright[0] = auxconsnode;
2744  leftright[1] = exponents[0] > 0.0 ? factors[0] : factors[1];
2745 
2747  SCIP_CALL( SCIPexprgraphAddNode(exprgraph, auxconsnode, -1, 2, leftright) );
2748 
2749  SCIP_CALL( SCIPcreateConsNonlinear2(scip, &auxcons, name, 0, NULL, NULL, auxconsnode, 0.0, 0.0,
2750  TRUE, TRUE, TRUE, TRUE, TRUE,
2751  FALSE, FALSE, FALSE, FALSE, FALSE) );
2752  SCIP_CALL( SCIPaddCons(scip, auxcons) );
2753 
2754  SCIP_CALL( SCIPreleaseCons(scip, &auxcons) );
2755  SCIP_CALL( SCIPreleaseVar(scip, &auxvar) );
2756 
2757  ++*naddcons;
2758 
2759  /* create node for auxvarnode^abs(exponents[0]) by just calling this method again */
2760  absexp = fabs(exponents[0]);
2761  SCIP_CALL( reformMonomial(scip, exprgraph, 1, &auxvarnode, &absexp, resultnode, createauxcons, mindepth, naddcons) );
2762 
2763  return SCIP_OKAY;
2764  }
2765 
2766  /* @todo if nfactors > 2, assemble groups of factors with same exponent and replace these by a single variable first */
2767 
2768  {
2769  /* at least two factors */
2770  /* create auxvar for left half (recursively) and auxvar for right half (recursively) and maybe new auxvar for product */
2771  /* @todo it may be enough to replace single factors in a monomial to get it convex or concave, see Westerlund et.al. */
2772 
2773  SCIP_EXPRGRAPHNODE* productnode;
2774  SCIP_EXPRGRAPHNODE* leftright[2]; /* {left, right} */
2775  SCIP_EXPRGRAPHNODE* parent;
2776  int half;
2777  int p;
2778 
2779  half = nfactors / 2;
2780  assert(half > 0);
2781  assert(half < nfactors);
2782 
2783  SCIP_CALL( reformMonomial(scip, exprgraph, half, factors, exponents, &leftright[0], TRUE, mindepth, naddcons) );
2784  SCIP_CALL( reformMonomial(scip, exprgraph, nfactors-half, &factors[half], exponents != NULL ? &exponents[half] : NULL, &leftright[1], TRUE, mindepth, naddcons) ); /*lint !e826*/
2785 
2786  /* check if there is already a node for left * right */
2787  productnode = NULL;
2788  for( p = 0; p < SCIPexprgraphGetNodeNParents(leftright[0]); ++p)
2789  {
2790  parent = SCIPexprgraphGetNodeParents(leftright[0])[p];
2792  continue;
2793 
2794  assert(SCIPexprgraphGetNodeNChildren(parent) == 2);
2795  if( (SCIPexprgraphGetNodeChildren(parent)[0] == leftright[0] && SCIPexprgraphGetNodeChildren(parent)[1] == leftright[1]) ||
2796  ( SCIPexprgraphGetNodeChildren(parent)[0] == leftright[1] && SCIPexprgraphGetNodeChildren(parent)[1] == leftright[0]) )
2797  {
2798  productnode = parent;
2799  break;
2800  }
2801  }
2802  if( productnode == NULL )
2803  {
2804  /* create node for left * right */
2805  SCIP_CALL( SCIPexprgraphCreateNode(SCIPblkmem(scip), &productnode, SCIP_EXPR_MUL, NULL) );
2806  SCIP_CALL( SCIPexprgraphAddNode(exprgraph, productnode, mindepth, 2, leftright) );
2809  }
2810 
2811  if( createauxcons )
2812  {
2813  /* @todo if there was already a node for factors[0]^exponents[0], then there may have been also been already an auxiliary variable and constraint (-> ex7_3_4) */
2814  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nlreform%d", *naddcons);
2815  SCIPdebugMsg(scip, "add auxiliary variable and constraint %s\n", name);
2816 
2817  SCIP_CALL( SCIPcreateVar(scip, &auxvar, name, -SCIPinfinity(scip), SCIPinfinity(scip), 0.0,
2818  SCIP_VARTYPE_CONTINUOUS, TRUE, TRUE, NULL, NULL, NULL, NULL, NULL) );
2819  SCIP_CALL( SCIPaddVar(scip, auxvar) );
2820  SCIP_CALL( SCIPexprgraphAddVars(exprgraph, 1, (void**)&auxvar, resultnode) );
2821 
2822 #ifdef WITH_DEBUG_SOLUTION
2823  if( SCIPdebugIsMainscip(scip) )
2824  {
2825  SCIPexprgraphSetVarNodeValue(*resultnode, SCIPexprgraphGetNodeVal(productnode));
2826  SCIP_CALL( SCIPdebugAddSolVal(scip, auxvar, SCIPexprgraphGetNodeVal(productnode)) );
2827  }
2828 #endif
2829 
2830  /* add new constraint resultnode(= auxvar) == left * right */
2831  minusone = -1.0;
2832  SCIP_CALL( SCIPcreateConsNonlinear2(scip, &auxcons, name, 1, &auxvar, &minusone, productnode, 0.0, 0.0, TRUE, TRUE, TRUE, TRUE, TRUE,
2833  FALSE, FALSE, FALSE, FALSE, FALSE) );
2834  SCIP_CALL( SCIPaddCons(scip, auxcons) );
2835 
2836  SCIP_CALL( SCIPreleaseCons(scip, &auxcons) );
2837  SCIP_CALL( SCIPreleaseVar(scip, &auxvar) );
2838 
2839  ++*naddcons;
2840  }
2841  else
2842  {
2843  *resultnode = productnode;
2844  }
2845  }
2846 
2847  return SCIP_OKAY;
2848 }
2849 
2850 /** reformulates expression graph into a form so that for each node under- and overestimators could be computed
2851  * similar to factorable reformulation in other global solvers, but sometimes does not split up complex operands (like quadratic)
2852  */
2853 static
2855  SCIP* scip, /**< SCIP data structure */
2856  SCIP_CONSHDLR* conshdlr, /**< constraint handler */
2857  SCIP_CONS** conss, /**< constraints */
2858  int nconss, /**< number of constraints */
2859  int* naddcons /**< buffer to increase by the number of added constraints */
2860  )
2861 {
2863  SCIP_CONSDATA* consdata;
2864  SCIP_EXPRGRAPH* exprgraph;
2865  SCIP_EXPRGRAPHNODE* node;
2866  SCIP_EXPRGRAPHNODE** children;
2867  SCIP_EXPRGRAPHNODE* reformnode;
2868  SCIP_Bool havenonlinparent;
2869  SCIP_Bool domainerror;
2870  int nchildren;
2871  int c;
2872  int d;
2873  int i;
2874  int u;
2875 #ifndef NDEBUG
2876  int j;
2877 #endif
2878 
2879  assert(scip != NULL);
2880  assert(conshdlr != NULL);
2881  assert(conss != NULL || nconss == 0);
2882  assert(naddcons != NULL);
2883  assert(SCIPgetStage(scip) == SCIP_STAGE_PRESOLVING);
2884  assert(!SCIPinProbing(scip));
2885 
2886  conshdlrdata = SCIPconshdlrGetData(conshdlr);
2887  assert(conshdlrdata != NULL);
2888 
2889  if( conshdlrdata->isreformulated )
2890  {
2891  SCIPdebugMsg(scip, "skip reformulation, already done\n");
2892  return SCIP_OKAY;
2893  }
2894 
2895  exprgraph = conshdlrdata->exprgraph;
2896 
2897  /* make sure current variable bounds are variable nodes of exprgraph */
2898  SCIP_CALL( SCIPexprgraphPropagateVarBounds(exprgraph, INTERVALINFTY, FALSE, &domainerror) );
2899  assert(!domainerror); /* should have been found by domain propagation */
2900 
2901  /* set debug solution in expression graph and evaluate nodes, so we can compute debug solution values for auxiliary variables */
2902 #ifdef WITH_DEBUG_SOLUTION
2903  if( SCIPdebugIsMainscip(scip) )
2904  {
2905  SCIP_Real* varvals;
2906 
2907  SCIP_CALL( SCIPallocBufferArray(scip, &varvals, SCIPexprgraphGetNVars(exprgraph)) );
2908 
2909  for( i = 0; i < SCIPexprgraphGetNVars(exprgraph); ++i )
2910  SCIP_CALL( SCIPdebugGetSolVal(scip, (SCIP_VAR*)SCIPexprgraphGetVars(exprgraph)[i], &varvals[i]) );
2911 
2912  SCIP_CALL( SCIPexprgraphEval(exprgraph, varvals) );
2913 
2914  SCIPfreeBufferArray(scip, &varvals);
2915  }
2916 #endif
2917 
2918  for( d = 1; d < SCIPexprgraphGetDepth(exprgraph); ++d )
2919  {
2920  i = 0;
2921  while( i < SCIPexprgraphGetNNodes(exprgraph)[d] )
2922  {
2923  node = SCIPexprgraphGetNodes(exprgraph)[d][i];
2924  assert(node != NULL);
2925 
2926  /* skip disabled nodes, they should be removed soon */
2927  if( !SCIPexprgraphIsNodeEnabled(node) )
2928  {
2929  ++i;
2930  continue;
2931  }
2932 
2933  /* make sure bounds and curvature of node are uptodate */
2936 
2937  /* try external reformulation methods */
2938  for( u = 0; u < conshdlrdata->nnlconsupgrades; ++u )
2939  {
2940  if( conshdlrdata->nlconsupgrades[u]->nodereform != NULL && conshdlrdata->nlconsupgrades[u]->active )
2941  {
2942  int nupdateconsbefore = SCIPconshdlrGetNUpdateConss(conshdlr);
2943  SCIP_CALL( conshdlrdata->nlconsupgrades[u]->nodereform(scip, exprgraph, node, naddcons, &reformnode) );
2944  if( reformnode == NULL )
2945  continue;
2946 
2947  SCIPdebugMsg(scip, "external nodereform reformulated node %p(%d,%d), replace by %p\n",
2948  (void*)node, SCIPexprgraphGetNodeDepth(node), SCIPexprgraphGetNodePosition(node), (void*)reformnode);
2949 
2950  /* do not replace node in nonlinear update constraints that may have been added by reformulation */
2951  SCIP_CALL( reformReplaceNode(exprgraph, &node, reformnode, conshdlr, SCIPconshdlrGetNUpdateConss(conshdlr) - nupdateconsbefore) );
2954 
2955  break;
2956  }
2957  }
2958  /* if node has been reformulated, continue with next node without increasing i */
2959  if( u < conshdlrdata->nnlconsupgrades )
2960  continue;
2961 
2962  /* leave nodes that are known to be convex/concave/linear as they are */
2964  {
2965  SCIPdebugMsg(scip, "skip reformulating node %p(%d,%d) = ", (void*)node, SCIPexprgraphGetNodeDepth(node), SCIPexprgraphGetNodePosition(node));
2968  ++i;
2969  continue;
2970  }
2971 
2972  /* get flag whether node has a nonlinear parent
2973  * we want to know whether the current node will be at the top of the tree after the next simplification run
2974  * due to the tricky reformulation of polynomials below, this may not be the case yet
2975  */
2976  havenonlinparent = SCIPexprgraphHasNodeNonlinearAncestor(node);
2977 
2978  /* take action */
2980  SCIPdebugMsg(scip, "think about reformulating %s node %p(%d,%d) = ", SCIPexpropGetName(SCIPexprgraphGetNodeOperator(node)), (void*)node, SCIPexprgraphGetNodeDepth(node), SCIPexprgraphGetNodePosition(node));
2982  SCIPdebugMsgPrint(scip, "\n");
2983 
2984  children = SCIPexprgraphGetNodeChildren(node);
2985  nchildren = SCIPexprgraphGetNodeNChildren(node);
2986  assert(children != NULL || nchildren == 0);
2987 
2988 #ifndef NDEBUG
2989  /* at this place, all children nodes should have a known curvature, except if they only appear only linearly in constraints
2990  * the latter for cases where constraints with unknown curvature are upgraded to other constraint handler that can handle these (quadratic, signpower,...)
2991  */
2992  for( j = 0; j < nchildren; ++j )
2993  {
2994  assert(children[j] != NULL); /*lint !e613*/
2995  if( havenonlinparent ||
3000  assert(SCIPexprgraphGetNodeCurvature(children[j]) != SCIP_EXPRCURV_UNKNOWN || SCIPexprgraphGetNodeOperator(children[j]) == SCIP_EXPR_USER); /*lint !e613*/
3001  }
3002 #endif
3003 
3004  switch( SCIPexprgraphGetNodeOperator(node) )
3005  {
3006  case SCIP_EXPR_VARIDX:
3007  case SCIP_EXPR_PARAM:
3008  case SCIP_EXPR_CONST:
3009  SCIPerrorMessage("node with operator %d cannot have unknown curvature\n", SCIPexprgraphGetNodeOperator(node));
3010  SCIPABORT();
3011  break; /*lint !e527*/
3012 
3013  /* linear operands */
3014  case SCIP_EXPR_PLUS:
3015  case SCIP_EXPR_MINUS:
3016  case SCIP_EXPR_SUM:
3017  case SCIP_EXPR_LINEAR:
3018  /* children have conflicting curvature, we can handle such sums in cons_nonlinear
3019  * thus, turn node into variable, if it has nonlinear parents */
3020  if( havenonlinparent )
3021  {
3022  SCIP_CALL( reformNode2Var(scip, exprgraph, node, conshdlr, naddcons, FALSE) );
3023  assert(node != NULL);
3024  assert(SCIPexprgraphGetNodeNParents(node) == 0); /* node should now be at top of graph */
3025  }
3026  ++i;
3027  break;
3028 
3029  /* quadratic operands */
3030  case SCIP_EXPR_MUL:
3031  case SCIP_EXPR_QUADRATIC:
3032  {
3033  SCIP_EXPRGRAPHNODE* child;
3034  SCIP_Bool needupdate;
3035 
3036  /* ensure all children are linear, so next simplifier run makes sure all children will be variables (by distributing the product)
3037  * however, that will not work for user-expressions, so we should also ensure that they are none (@todo as they are linear, they could actually be replaced by a regular linear expression)
3038  */
3039  SCIPdebugMessage("ensure children are linear\n");
3040  SCIP_CALL( reformEnsureChildrenMinCurvature(scip, exprgraph, node, SCIP_EXPRCURV_LINEAR, conshdlr, naddcons) );
3041 
3042  needupdate = FALSE; /* whether we need to update curvature of node */
3043  for( c = 0; c < SCIPexprgraphGetNodeNChildren(node); ++c )
3044  {
3045  child = SCIPexprgraphGetNodeChildren(node)[c];
3046  assert(child != NULL);
3047 
3049  {
3050  SCIPdebugMessage("add auxiliary variable for child %p(%d,%d) with curvature %s operator %s\n",
3052 
3053  SCIP_CALL( reformNode2Var(scip, exprgraph, child, conshdlr, naddcons, FALSE) );
3054  needupdate = TRUE;
3055 
3056  /* c'th child of node should now be a variable */
3057  assert(SCIPexprgraphGetNodeChildren(node)[c] != child);
3059  }
3060  }
3061  if( needupdate )
3062  {
3065  }
3066 
3068  {
3069  /* if curvature is now known then we are done */
3070  ++i;
3071  break;
3072  }
3073 
3074  /* if we have nonlinear parents or a sibling, then add auxiliary variable for this node, so an upgrade to cons_quadratic should take place
3075  * we assume that siblings are non-linear and non-quadratic, which should be the case if simplifier was run, and also if this node was created during reformulating a polynomial
3076  * @todo we could also add auxvars for the sibling nodes, e.g., if there is only one
3077  * @todo if sibling nodes are quadratic (or even linear) due to reformulation, then we do not need to reform here... (-> nvs16)
3078  * maybe this step should not be done here at all if havenonlinparent is FALSE? e.g., move into upgrade from quadratic?
3079  */
3080  if( havenonlinparent || SCIPexprgraphHasNodeSibling(node) )
3081  {
3082  SCIP_CALL( reformNode2Var(scip, exprgraph, node, conshdlr, naddcons, FALSE) );
3083  assert(node != NULL);
3084  assert(SCIPexprgraphGetNodeNParents(node) == 0); /* node should now be at top of graph, so it can be upgraded by cons_quadratic */
3085  break;
3086  }
3087 
3088  ++i;
3089  break;
3090  }
3091 
3092  case SCIP_EXPR_DIV:
3093  {
3094  /* reformulate as bilinear term
3095  * note that in the reformulation, a zero in the denominator forces the nominator to be zero too, but the auxiliary can be arbitrary
3096  */
3097  SCIP_EXPRGRAPHNODE* auxvarnode;
3098  SCIP_EXPRGRAPHNODE* auxnode;
3099  SCIP_EXPRGRAPHNODE* auxchildren[3];
3100  SCIP_Real lincoefs[3];
3101  SCIP_QUADELEM quadelem;
3102  SCIP_VAR* auxvar;
3103  SCIP_CONS* auxcons;
3104  char name[SCIP_MAXSTRLEN];
3105  SCIP_INTERVAL bounds;
3106 
3107  assert(children != NULL);
3108  assert(nchildren == 2);
3109 
3110  bounds = SCIPexprgraphGetNodeBounds(node);
3111  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "nlreform%d", *naddcons);
3112 
3113  SCIPdebugMsg(scip, "add auxiliary variable %s for division in node %p(%d,%d)\n", name, (void*)node, SCIPexprgraphGetNodeDepth(node), SCIPexprgraphGetNodePosition(node));
3114 
3115  SCIP_CALL( SCIPcreateVar(scip, &auxvar, name, SCIPintervalGetInf(bounds), SCIPintervalGetSup(bounds), 0.0,
3117  SCIP_CALL( SCIPaddVar(scip, auxvar) );
3118  SCIP_CALL( SCIPexprgraphAddVars(exprgraph, 1, (void**)&auxvar, &auxvarnode) );
3119 
3120 #ifdef WITH_DEBUG_SOLUTION
3121  if( SCIPdebugIsMainscip(scip) )
3122  {
3123  SCIP_Real debugval;
3124  debugval = SCIPexprgraphGetNodeVal(children[0]) / SCIPexprgraphGetNodeVal(children[1]);
3125  SCIPexprgraphSetVarNodeValue(auxvarnode, debugval);
3126  SCIP_CALL( SCIPdebugAddSolVal(scip, auxvar, debugval) );
3127  }
3128 #endif
3129 
3130  /* add new constraint auxvar * child[1] - child[0] == 0 */
3131  auxchildren[0] = children[0]; /*lint !e613*/
3132  auxchildren[1] = children[1]; /*lint !e613*/
3133  auxchildren[2] = auxvarnode;
3134 
3135  lincoefs[0] = -1.0;
3136  lincoefs[1] = 0.0;
3137  lincoefs[2] = 0.0;
3138 
3139  quadelem.idx1 = 1;
3140  quadelem.idx2 = 2;
3141  quadelem.coef = 1.0;
3142 
3143  SCIP_CALL( SCIPexprgraphCreateNodeQuadratic(SCIPblkmem(scip), &auxnode, 3, lincoefs, 1, &quadelem, 0.0) );
3144  SCIP_CALL( SCIPexprgraphAddNode(exprgraph, auxnode, -1, 3, auxchildren) );
3145 
3146  SCIP_CALL( SCIPcreateConsNonlinear2(scip, &auxcons, name, 0, NULL, NULL, auxnode, 0.0, 0.0,
3147  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
3148  SCIP_CALL( SCIPaddCons(scip, auxcons) );
3149 
3150  /* replace node by auxvarnode in graph and constraints that use it */
3151  SCIP_CALL( reformReplaceNode(exprgraph, &node, auxvarnode, conshdlr, 1) );
3152 
3153  SCIP_CALL( SCIPreleaseCons(scip, &auxcons) );
3154  SCIP_CALL( SCIPreleaseVar(scip, &auxvar) );
3155 
3156  ++*naddcons;
3157 
3158  /* do not increase i, since node was removed and not necessarily replaced here */
3159  break;
3160  }
3161 
3162  case SCIP_EXPR_MIN:
3163  {
3164  /* make sure that both children are concave, because min of concave functions is concave */
3165  SCIP_CALL( reformEnsureChildrenMinCurvature(scip, exprgraph, node, SCIP_EXPRCURV_CONCAVE, conshdlr, naddcons) );
3167  ++i;
3168  break;
3169  }
3170 
3171  case SCIP_EXPR_MAX:
3172  {
3173  /* make sure that both children are convex, because max of convex functions is convex */
3174  SCIP_CALL( reformEnsureChildrenMinCurvature(scip, exprgraph, node, SCIP_EXPRCURV_CONVEX, conshdlr, naddcons) );
3176  ++i;
3177  break;
3178  }
3179 
3180  case SCIP_EXPR_INTPOWER:
3181  {
3182  assert(nchildren == 1);
3183 
3184  /* for an intpower with mixed sign in the base and negative exponent, we reformulate similar as for EXPR_DIV */
3185  if( SCIPexprgraphGetNodeIntPowerExponent(node) < 0 && SCIPintervalGetInf(SCIPexprgraphGetNodeBounds(children[0])) < 0.0 && SCIPintervalGetSup(SCIPexprgraphGetNodeBounds(children[0])) > 0.0 ) /*lint !e613*/
3186  {
3187  SCIP_EXPRGRAPHNODE* auxvarnode;
3188  SCIP_Real exponent;
3189  int naddconsbefore = *naddcons;
3190 
3191  /* if we have something like x^(-3) with mixed sign for x, then add auxvar and reform as auxvar*x^3 = 1 via reformMonomial */
3193  SCIP_CALL( reformMonomial(scip, exprgraph, 1, children, &exponent, &auxvarnode, TRUE, SCIPexprgraphGetNodeDepth(node), naddcons) );
3194 
3195  /* replace node by auxvarnode */
3196  SCIP_CALL( reformReplaceNode(exprgraph, &node, auxvarnode, conshdlr, *naddcons - naddconsbefore) );
3197  break;
3198  }
3199 
3200  /* otherwise, we continue as for other univariate operands */
3201  } /*lint -fallthrough*/
3202 
3203  /* univariate operands where the child does not have bounds and curvature from which we can deduce curvature of this node,
3204  * but where we can do more if the child is linear
3205  * thus, turn child into auxiliary variable
3206  */
3207  case SCIP_EXPR_SQUARE:
3208  case SCIP_EXPR_SQRT:
3209  case SCIP_EXPR_EXP:
3210  case SCIP_EXPR_LOG:
3211  case SCIP_EXPR_ABS:
3212  case SCIP_EXPR_REALPOWER:
3213  case SCIP_EXPR_SIGNPOWER:
3214  {
3215  assert(nchildren == 1);
3216 
3217  SCIP_CALL( reformEnsureChildrenMinCurvature(scip, exprgraph, node, SCIP_EXPRCURV_LINEAR, conshdlr, naddcons) );
3218 
3220  {
3221  /* the only case where f(x) for a linear term x is indefinite here is if f is intpower or signpower and x has mixed sign */
3223  assert(SCIPintervalGetInf(SCIPexprgraphGetNodeBounds(children[0])) < 0.0); /*lint !e613*/
3224  assert(SCIPintervalGetSup(SCIPexprgraphGetNodeBounds(children[0])) > 0.0); /*lint !e613*/
3225  }
3226 
3227  /* update curvature of node */
3230 
3232  {
3233  /* if intpower and signpower with positive exponent and a mixed sign in the child bounds still does not give a curvature,
3234  * we can do more if we make this node the root of a nonlinear constraints expression node, so it can be upgraded by cons_signpower
3235  * of course, this is only required if the node is still intermediate
3236  *
3237  * an intpower with negative exponent should have been handled above
3238  * for signpower, we assume the exponent is > 1.0
3239  */
3243  if( havenonlinparent )
3244  {
3245  SCIP_CALL( reformNode2Var(scip, exprgraph, node, conshdlr, naddcons, FALSE) );
3246  assert(node != NULL); /* it should be used by some auxiliary constraint now */
3247  assert(SCIPexprgraphGetNodeNParents(node) == 0); /* node should now be at top of graph (and used by new auxiliary constraint) */
3248  }
3249  }
3250  ++i;
3251 
3252  break;
3253  }
3254 
3255  case SCIP_EXPR_SIN:
3256  case SCIP_EXPR_COS:
3257  case SCIP_EXPR_TAN:
3258  case SCIP_EXPR_SIGN:
3259  /* case SCIP_EXPR_ERF : */
3260  /* case SCIP_EXPR_ERFI : */
3261  {
3262  SCIPerrorMessage("no support for trigonometric or sign operands yet\n");
3263  return SCIP_ERROR;
3264  }
3265 
3266  case SCIP_EXPR_PRODUCT:
3267  {
3268  int naddconsbefore;
3269  /* ensure all children are linear */
3270  SCIP_CALL( reformEnsureChildrenMinCurvature(scip, exprgraph, node, SCIP_EXPRCURV_LINEAR, conshdlr, naddcons) );
3272  {
3273  ++i;
3274  break;
3275  }
3276 
3277  /* if curvature is still unknown (quite likely), then turn into a cascade of bilinear terms
3278  * if node has parents, then ensure that it has a known curvature, otherwise we are also fine with a node that is a product of two (aux)variables */
3279  naddconsbefore = *naddcons;
3280  SCIP_CALL( reformMonomial(scip, exprgraph, nchildren, children, NULL, &reformnode, havenonlinparent, SCIPexprgraphGetNodeDepth(node), naddcons) );
3281 
3282  /* replace node by reformnode in graph and in all constraints that use it */
3283  SCIP_CALL( reformReplaceNode(exprgraph, &node, reformnode, conshdlr, *naddcons - naddconsbefore) );
3284 
3285  /* do not increase i, since node was removed and not necessarily replaced here */
3286  break;
3287  }
3288 
3289  case SCIP_EXPR_POLYNOMIAL:
3290  {
3291  /* if polynomial has several monomials, replace by a sum of nodes each having a single monomial and one that has all linear and quadratic monomials
3292  * if polynomial has only a single monomial, then reformulate that one
3293  */
3294  SCIP_EXPRDATA_MONOMIAL** monomials;
3295  SCIP_EXPRDATA_MONOMIAL* monomial;
3296  int nmonomials;
3297  SCIP_Real* exponents;
3298  SCIP_Real coef;
3299  int* childidxs;
3300  int nfactors;
3301  int f;
3302  SCIP_INTERVAL childbounds;
3303  SCIP_EXPRCURV childcurv;
3304  SCIP_Bool modified;
3305 
3306  monomials = SCIPexprgraphGetNodePolynomialMonomials(node);
3307  nmonomials = SCIPexprgraphGetNodePolynomialNMonomials(node);
3308  assert(nmonomials >= 1); /* constant polynomials should have been simplified away */
3309 
3310  if( nmonomials > 1 )
3311  {
3312  SCIP_EXPRGRAPHNODE* sumnode;
3313  SCIP_Real constant;
3314  int nquadelems;
3315  SCIP_QUADELEM* quadelems;
3316  SCIP_Real* lincoefs;
3317  int nmonomialnodes;
3318  SCIP_EXPRGRAPHNODE** childrennew;
3319  SCIP_EXPRGRAPHNODE** monomialnodes;
3320  SCIP_Bool foundlincoefs;
3321  int m;
3322 
3323  /* @todo if a monomial is a factor of another monomial, then we could (and should?) replace it there by the node we create for it here -> ex7_2_1
3324  * @todo factorizing the polynomial could be beneficial
3325  */
3326 
3327  /* constant part of polynomials, to add to first monomialnode, if any, or quadratic or linear part */
3328  constant = SCIPexprgraphGetNodePolynomialConstant(node);
3329 
3330  /* coefficients from linear monomials */
3331  foundlincoefs = FALSE;
3332 
3333  /* quadratic elements */
3334  nquadelems = 0;
3335 
3336  /* expression graph nodes representing single higher-degree monomials, and single node with linear and/or quadratic monomials */
3337  nmonomialnodes = 0;
3338  SCIP_CALL( SCIPallocBufferArray(scip, &monomialnodes, nmonomials) );
3339 
3340  /* allocate memory */
3341  SCIP_CALL( SCIPallocClearBufferArray(scip, &lincoefs, nchildren) );
3342  SCIP_CALL( SCIPallocBufferArray(scip, &quadelems, nmonomials) );
3343  SCIP_CALL( SCIPallocBufferArray(scip, &childrennew, nchildren) );
3344 
3345  for( m = 0; m < nmonomials; ++m )
3346  {
3347  monomial = monomials[m];
3348  assert(monomial != NULL);
3349 
3350  coef = SCIPexprGetMonomialCoef(monomial);
3351  exponents = SCIPexprGetMonomialExponents(monomial);
3352  childidxs = SCIPexprGetMonomialChildIndices(monomial);
3353  nfactors = SCIPexprGetMonomialNFactors(monomial);
3354  assert(nfactors >= 1); /* constant monomials should have been simplified away */
3355  assert(coef != 0.0); /* zero-monomials should have been simplified away */
3356 
3357  if( nfactors == 1 && exponents[0] == 1.0 )
3358  {
3359  /* linear monomial */
3360  foundlincoefs = TRUE;
3361  assert(0 <= childidxs[0] && childidxs[0] < nchildren);
3362  assert(lincoefs[childidxs[0]] == 0.0); /* monomials should have been merged */
3363  lincoefs[childidxs[0]] = coef;
3364  }
3365  else if( nfactors == 1 && exponents[0] == 2.0 )
3366  {
3367  /* square monomial */
3368  quadelems[nquadelems].idx1 = childidxs[0];
3369  quadelems[nquadelems].idx2 = childidxs[0];
3370  quadelems[nquadelems].coef = coef;
3371  ++nquadelems;
3372  }
3373  else if( nfactors == 2 && exponents[0] == 1.0 && exponents[1] == 1.0 )
3374  {
3375  /* bilinear monomial */
3376  if( childidxs[0] < childidxs[1] )
3377  {
3378  quadelems[nquadelems].idx1 = childidxs[0];
3379  quadelems[nquadelems].idx2 = childidxs[1];
3380  }
3381  else
3382  {
3383  quadelems[nquadelems].idx1 = childidxs[1];
3384  quadelems[nquadelems].idx2 = childidxs[0];
3385  }
3386  quadelems[nquadelems].coef = coef;
3387  ++nquadelems;
3388  }
3389  else
3390  {
3391  /* general monomial -> pass into separate expression graph node */
3392  SCIP_EXPRDATA_MONOMIAL* monomialnew;
3393 
3394  /* create new node for this monomial, children will be those associated with factors */
3395  SCIP_CALL( SCIPexprCreateMonomial(SCIPblkmem(scip), &monomialnew, coef, nfactors, NULL, exponents) );
3396  SCIP_CALL( SCIPexprgraphCreateNodePolynomial(SCIPblkmem(scip), &monomialnodes[nmonomialnodes], 1, &monomialnew, constant, FALSE) );
3397  constant = 0.0;
3398 
3399  assert(nfactors <= nchildren);
3400  for( f = 0; f < nfactors; ++f )
3401  childrennew[f] = children[childidxs[f]]; /*lint !e613*/
3402 
3403  /* add new node to same depth as this node, so we will reformulate it during this run
3404  * no need to refresh bounds/curvature here, since that will be done when we reach this node next */
3405  SCIP_CALL( SCIPexprgraphAddNode(exprgraph, monomialnodes[nmonomialnodes], SCIPexprgraphGetNodeDepth(node), nfactors, childrennew) );
3406 
3407  ++nmonomialnodes;
3408  }
3409  }
3410  /* should have had at least one linear, quadratic, or general monomial */
3411  assert(foundlincoefs || nquadelems > 0 || nmonomialnodes > 0);
3412 
3413  if( nquadelems > 0 )
3414  {
3415  /* create and add additional node for quadratic and linear part, simplifier should take care of removing unused children later */
3416  SCIP_CALL( SCIPexprgraphCreateNodeQuadratic(SCIPblkmem(scip), &monomialnodes[nmonomialnodes], nchildren, foundlincoefs ? lincoefs : NULL, nquadelems, quadelems, constant) );
3417  constant = 0.0;
3418  SCIP_CALL( SCIPexprgraphAddNode(exprgraph, monomialnodes[nmonomialnodes], SCIPexprgraphGetNodeDepth(node), nchildren, children) );
3419  ++nmonomialnodes;
3420  }
3421  else if( foundlincoefs )
3422  {
3423  /* create additional node for linear part, simplifier should take care of removing unused children later */
3424  SCIP_CALL( SCIPexprgraphCreateNodeLinear(SCIPblkmem(scip), &monomialnodes[nmonomialnodes], nchildren, lincoefs, constant) );
3425  constant = 0.0;
3426  SCIP_CALL( SCIPexprgraphAddNode(exprgraph, monomialnodes[nmonomialnodes], SCIPexprgraphGetNodeDepth(node), nchildren, children) );
3427  ++nmonomialnodes;
3428  }
3429  assert(constant == 0.0); /* the constant should have been used somewhere */
3430 
3431  /* release memory */
3432  SCIPfreeBufferArray(scip, &childrennew);
3433  SCIPfreeBufferArray(scip, &quadelems);
3434  SCIPfreeBufferArray(scip, &lincoefs);
3435 
3436  assert(nmonomialnodes > 0);
3437  if( nmonomialnodes > 1 )
3438  {
3439  /* add node for sum of monomials to expression graph */
3440  SCIP_CALL( SCIPexprgraphCreateNode(SCIPblkmem(scip), &sumnode, nmonomialnodes == 2 ? SCIP_EXPR_PLUS : SCIP_EXPR_SUM) );
3441  SCIP_CALL( SCIPexprgraphAddNode(exprgraph, sumnode, -1, nmonomialnodes, monomialnodes) );
3442  }
3443  else
3444  {
3445  /* if only one monomial, then because polynomial was linear or quadratic... */
3446  assert(SCIPexprgraphGetNodeOperator(monomialnodes[0]) == SCIP_EXPR_LINEAR || SCIPexprgraphGetNodeOperator(monomialnodes[0]) == SCIP_EXPR_QUADRATIC);
3447  sumnode = monomialnodes[0];
3448  }
3449  SCIPfreeBufferArray(scip, &monomialnodes);
3450 
3451  /* replace node by sumnode, and we are done */
3452  SCIP_CALL( reformReplaceNode(exprgraph, &node, sumnode, conshdlr, 0) );
3453 
3454  SCIPdebugMsg(scip, "splitup polynomial into sum of %d nodes\n", nmonomialnodes);
3455 
3456  break;
3457  }
3458 
3459  /* reformulate a monomial such that it becomes convex or concave, if necessary */
3460 
3461  monomial = monomials[0];
3462  assert(monomial != NULL);
3463 
3464  coef = SCIPexprGetMonomialCoef(monomial);
3465  exponents = SCIPexprGetMonomialExponents(monomial);
3466  childidxs = SCIPexprGetMonomialChildIndices(monomial);
3467  nfactors = SCIPexprGetMonomialNFactors(monomial);
3468  assert(nfactors >= 1); /* constant monomials should have been simplified away */
3469  assert(coef != 0.0); /* zero-monomials should have been simplified away */
3470  assert(children != NULL);
3471 
3472  /* check if we make monomial convex or concave by making a child linear */
3473  modified = FALSE;
3474  if( nfactors == 1 )
3475  {
3476  /* ensure that the child of an univariate monomial is linear if its current (bounds,curvature) yields an unknown curvature for the monomial
3477  * and with linear child it had a known curvature (rules out x^a, a negative, x not linear) */
3478  childcurv = SCIPexprgraphGetNodeCurvature(children[childidxs[0]]); /*lint !e613*/
3479  childbounds = SCIPexprgraphGetNodeBounds(children[childidxs[0]]); /*lint !e613*/
3480  assert(SCIPexprcurvPower(childbounds, childcurv, exponents[0]) == SCIP_EXPRCURV_UNKNOWN); /* this is exactly the curvature of the node, which is unknown */
3481 
3482  /* if monomial were convex or concave if child were linear, then make child linear */
3483  if( SCIPexprcurvPower(childbounds, SCIP_EXPRCURV_LINEAR, exponents[0]) != SCIP_EXPRCURV_UNKNOWN )
3484  {
3485  assert(childcurv != SCIP_EXPRCURV_LINEAR);
3486  SCIPdebugMsg(scip, "reform child %d (univar. monomial) with curv %s into var\n", childidxs[0], SCIPexprcurvGetName(childcurv));
3487  SCIP_CALL( reformNode2Var(scip, exprgraph, children[childidxs[0]], conshdlr, naddcons, FALSE) ); /*lint !e613*/
3488  modified = TRUE;
3489  }
3490  }
3491  else
3492  {
3493  /* check if the conditions on the exponents allow for a convex or concave monomial, assuming that the children are linear
3494  * if one of these conditions is fulfilled but a children curvature does not fit, then make these children linear
3495  */
3496  int nnegative;
3497  int npositive;
3498  SCIP_Real sum;
3499  SCIP_Bool expcurvpos;
3500  SCIP_Bool expcurvneg;
3501  SCIP_EXPRCURV desiredcurv;
3502 
3503  nnegative = 0; /* number of negative exponents */
3504  npositive = 0; /* number of positive exponents */
3505  sum = 0.0; /* sum of exponents */
3506  expcurvpos = TRUE; /* whether exp_j * f_j''(x) >= 0 for all factors (assuming f_j >= 0) */
3507  expcurvneg = TRUE; /* whether exp_j * f_j''(x) <= 0 for all factors (assuming f_j >= 0) */
3508 
3509  /* ensure that none of the children have unknown curvature */
3510  for( c = 0; c < SCIPexprgraphGetNodeNChildren(node); ++c )
3511  {
3512  childcurv = SCIPexprgraphGetNodeCurvature(children[c]); /*lint !e613*/
3513  if( childcurv == SCIP_EXPRCURV_UNKNOWN )
3514  {
3515  SCIPdebugMessage("reform child %d with unknown curvature into var\n", c);
3516  SCIP_CALL( reformNode2Var(scip, exprgraph, children[c], conshdlr, naddcons, FALSE) ); /*lint !e613*/
3517  modified = TRUE;
3518  }
3519  }
3520  if( modified )
3521  {
3522  /* refresh curvature information in node, since we changed children */
3525 
3526  modified = FALSE;
3527  }
3528 
3529  for( f = 0; f < nfactors; ++f )
3530  {
3531  childcurv = SCIPexprgraphGetNodeCurvature(children[childidxs[f]]); /*lint !e613*/
3532  assert(childcurv != SCIP_EXPRCURV_UNKNOWN);
3533  childbounds = SCIPexprgraphGetNodeBounds(children[childidxs[f]]); /*lint !e613*/
3534  if( childbounds.inf < 0.0 && childbounds.sup > 0.0 )
3535  break;
3536 
3537  if( exponents[f] < 0.0 )
3538  ++nnegative;
3539  else
3540  ++npositive;
3541  sum += exponents[f];
3542 
3543  /* negate curvature if factor is negative */
3544  if( childbounds.inf < 0.0 )
3545  childcurv = SCIPexprcurvNegate(childcurv);
3546 
3547  /* check if exp_j * checkcurv is convex (>= 0) and/or concave */
3548  childcurv = SCIPexprcurvMultiply(exponents[f], childcurv);
3549  if( !(childcurv & SCIP_EXPRCURV_CONVEX) )
3550  expcurvpos = FALSE;
3551  if( !(childcurv & SCIP_EXPRCURV_CONCAVE) )
3552  expcurvneg = FALSE;
3553  }
3554 
3555  /* if some child can be both positive and negative, then nothing we can do here to get the monomial convex or concave
3556  * otherwise (i.e., f == nfactors), look further */
3557  desiredcurv = SCIP_EXPRCURV_UNKNOWN;
3558  if( f == nfactors )
3559  {
3560  /* if all factors are linear, then a product f_j^exp_j with f_j >= 0 is convex if
3561  * - all exponents are negative, or
3562  * - all except one exponent j* are negative and exp_j* >= 1 - sum_{j!=j*}exp_j, but the latter is equivalent to sum_j exp_j >= 1
3563  * further, the product is concave if
3564  * - all exponents are positive and the sum of exponents is <= 1.0
3565  *
3566  * if factors are nonlinear, then we require additionally, that for convexity
3567  * - each factor is convex if exp_j >= 0, or concave if exp_j <= 0, i.e., exp_j*f_j'' >= 0
3568  * and for concavity, we require that
3569  * - all factors are concave, i.e., exp_j*f_j'' <= 0
3570  */
3571 
3572  if( nnegative == nfactors || (nnegative == nfactors-1 && SCIPisGE(scip, sum, 1.0)) )
3573  {
3574  /* if exponents are such that we can be convex, but children curvature does not fit, make some children linear */
3575  SCIPdebugMsg(scip, "%d-variate monomial is convex (modulo sign), child curv fits = %u\n", nfactors, expcurvpos);
3576  /* since current node curvature is set to unknown, there must be such a child, since otherwise the node curvature had to be convex */
3577  assert(!expcurvpos);
3578  desiredcurv = SCIP_EXPRCURV_CONVEX;
3579  }
3580  else if( npositive == nfactors && SCIPisLE(scip, sum, 1.0) )
3581  {
3582  /* if exponents are such that we can be concave, but children curvature does not fit, make some children linear */
3583  SCIPdebugMsg(scip, "%d-variate monomial is concave (modulo sign), child curv fits = %u\n", nfactors, expcurvneg);
3584  /* since current node curvature is set to unknown, there must be such a child, since otherwise the node curvature had to be concave */
3585  assert(!expcurvneg);
3586  desiredcurv = SCIP_EXPRCURV_CONCAVE;
3587  }
3588  else
3589  {
3590  /* exponents are such that monomial is neither convex nor concave even if children were linear
3591  * thus, reformulate monomial below
3592  */
3593  }
3594  }
3595 
3596  if( desiredcurv != SCIP_EXPRCURV_UNKNOWN )
3597  {
3598  for( f = 0; f < nfactors; ++f )
3599  {
3600  childcurv = SCIPexprgraphGetNodeCurvature(children[childidxs[f]]); /*lint !e613*/
3601  assert(childcurv != SCIP_EXPRCURV_UNKNOWN);
3602  childbounds = SCIPexprgraphGetNodeBounds(children[childidxs[f]]); /*lint !e613*/
3603  assert(childbounds.inf >= 0.0 || childbounds.sup <= 0.0);
3604 
3605  /* negate curvature if factor is negative */
3606  if( childbounds.inf < 0.0 )
3607  childcurv = SCIPexprcurvNegate(childcurv);
3608 
3609  /* check if exp_j * checkcurv is convex (>= 0) and/or concave */
3610  childcurv = SCIPexprcurvMultiply(SCIPexprGetMonomialExponents(monomial)[f], childcurv);
3611  if( (desiredcurv == SCIP_EXPRCURV_CONVEX && !(childcurv & SCIP_EXPRCURV_CONVEX )) ||
3612  (desiredcurv == SCIP_EXPRCURV_CONCAVE && !(childcurv & SCIP_EXPRCURV_CONCAVE)) )
3613  {
3614  SCIPdebugMsg(scip, "reform child %d (factor %d) with curv %s into var\n",
3615  childidxs[f], f, SCIPexprcurvGetName(SCIPexprgraphGetNodeCurvature(children[childidxs[f]]))); /*lint !e613*/
3616  SCIP_CALL( reformNode2Var(scip, exprgraph, children[childidxs[f]], conshdlr, naddcons, FALSE) ); /*lint !e613*/
3617  modified = TRUE;
3618  }
3619  }
3620  }
3621  }
3622 
3623  if( modified )
3624  {
3625  /* refresh curvature information in node, since we changed children, it should be convex or concave now */
3629 
3630  /* we are done and can proceed with the next node */
3631  ++i;
3632  break;
3633  }
3634 
3635  /* monomial can only have unknown curvature here, if it has several factors
3636  * or is of form x^a with x both negative and positive and a an odd or negative integer (-> INTPOWER expression)
3637  */
3638  assert(nfactors > 1 ||
3639  (SCIPexprgraphGetNodeBounds(children[childidxs[0]]).inf < 0.0 && SCIPexprgraphGetNodeBounds(children[childidxs[0]]).sup > 0.0 &&
3640  SCIPisIntegral(scip, exponents[0]) && (exponents[0] < 0.0 || ((int)SCIPround(scip, exponents[0]) % 2 != 0)))
3641  ); /*lint !e613*/
3642 
3643  /* bilinear monomials should not come up here, since simplifier should have turned them into quadratic expression nodes */
3644  assert(!(nfactors == 2 && exponents[0] == 1.0 && exponents[1] == 1.0));
3645 
3646  /* reform monomial if it is a product, or we need it to be on the top of the graph, or if it of the form x^a with a < 0.0 (and thus x having mixed sign, see assert above)
3647  * thus, in the case x^a with a an odd positive integer we assume that cons_signpower will do something */
3648  if( nfactors > 1 || havenonlinparent || exponents[0] < 0.0 )
3649  {
3650  SCIP_EXPRGRAPHNODE* auxnode;
3651  SCIP_EXPRGRAPHNODE** factors;
3652  int naddconsbefore = *naddcons;
3653 
3654  if( nfactors > 1 )
3655  {
3656  SCIP_CALL( SCIPallocBufferArray(scip, &factors, nfactors) );
3657  for( f = 0; f < nfactors; ++f )
3658  factors[f] = children[childidxs[f]]; /*lint !e613*/
3659  }
3660  else
3661  factors = &children[childidxs[0]]; /*lint !e613*/
3662 
3663  SCIPdebugMsg(scip, "reform monomial node, create auxvar = %u\n", havenonlinparent);
3664  /* get new auxnode for monomial
3665  * if node has parents and monomial is of indefinite form x^a, then also create auxvar for it, since otherwise we create a auxnode with unknown curvature
3666  * note, that the case x^a with positive and odd a will still give an indefinite node (without parents), where we assume that signpower will pick it up at some point
3667  */
3668  SCIP_CALL( reformMonomial(scip, exprgraph, nfactors, factors, exponents, &auxnode, havenonlinparent, SCIPexprgraphGetNodeDepth(node), naddcons) );
3669 
3670  if( nfactors > 1 )
3671  {
3672  SCIPfreeBufferArray(scip, &factors);
3673  }
3674 
3675  /* create node for monomialcoef * auxnode + monomialconstant, if not identical to auxnode */
3676  if( SCIPexprgraphGetNodePolynomialConstant(node) != 0.0 || coef != 1.0 )
3677  {
3678  SCIP_EXPRGRAPHNODE* replnode;
3679 
3681  SCIP_CALL( SCIPexprgraphAddNode(exprgraph, replnode, -1, 1, &auxnode) );
3682  auxnode = replnode;
3683  }
3684 
3685  /* replace node by auxnode and refresh its curvature */
3686  SCIP_CALL( reformReplaceNode(exprgraph, &node, auxnode, conshdlr, *naddcons - naddconsbefore) );
3689 
3690  break;
3691  }
3692  else
3693  {
3694  SCIPdebugMsg(scip, "no reformulation of monomial node, assume signpower will take care of it\n");
3695  }
3696 
3697  ++i;
3698  break;
3699  }
3700 
3701  case SCIP_EXPR_USER:
3702  {
3703  /* ensure all children are linear */
3704  SCIP_CALL( reformEnsureChildrenMinCurvature( scip, exprgraph, node, SCIP_EXPRCURV_LINEAR, conshdlr, naddcons ) );
3705 
3706  /* unknown curvature can be handled by user estimator callback or interval gradient */
3707  /*
3708  if( SCIPexprgraphGetNodeCurvature( node ) == SCIP_EXPRCURV_UNKNOWN )
3709  {
3710  SCIPerrorMessage("user expression with unknown curvature not supported\n");
3711  return SCIP_ERROR;
3712  }
3713  */
3714 
3715  ++i;
3716  break;
3717  }
3718 
3719  case SCIP_EXPR_LAST:
3720  SCIPABORT();
3721  break;
3722  }
3723  }
3724  }
3725 
3726  /* for constraints with concave f(g(x)) with linear g:R^n -> R, n>1, reformulate to get a univariate concave function, since this is easier to underestimate
3727  * @todo this does not work yet for sums of functions other than polynomials
3728  */
3729  for( c = 0; c < nconss; ++c )
3730  {
3731  SCIP_EXPRGRAPHNODE* multivarnode;
3732  SCIP_EXPRCURV curv;
3733 
3734  assert(conss[c] != NULL); /*lint !e613*/
3735 
3736  /* skip constraints that are to be deleted */
3737  if( SCIPconsIsDeleted(conss[c]) ) /*lint !e613*/
3738  continue;
3739 
3740  consdata = SCIPconsGetData(conss[c]); /*lint !e613*/
3741  assert(consdata != NULL);
3742 
3743  if( consdata->exprgraphnode == NULL )
3744  continue;
3745 
3746  /* after reformulation, force a round of backpropagation in expression graph for all constraints,
3747  * since new variables (nlreform*) may now be used in existing constraints and we want domain restrictions
3748  * of operators propagated for these variables
3749  */
3750  consdata->forcebackprop = TRUE;
3751 
3752  if( SCIPexprgraphGetNodeOperator(consdata->exprgraphnode) == SCIP_EXPR_POLYNOMIAL )
3753  {
3754  SCIP_EXPRDATA_MONOMIAL* monomial;
3755  int m;
3756  int f;
3757 
3758  for( m = 0; m < SCIPexprgraphGetNodePolynomialNMonomials(consdata->exprgraphnode); ++m )
3759  {
3760  SCIP_CALL( SCIPexprgraphGetNodePolynomialMonomialCurvature(consdata->exprgraphnode, m, INTERVALINFTY, &curv) );
3761 
3762  monomial = SCIPexprgraphGetNodePolynomialMonomials(consdata->exprgraphnode)[m];
3763  assert(monomial != NULL);
3764 
3765  /* if nothing concave, then continue */
3766  if( (SCIPisInfinity(scip, consdata->rhs) || curv != SCIP_EXPRCURV_CONCAVE) &&
3767  ( SCIPisInfinity(scip, -consdata->lhs) || curv != SCIP_EXPRCURV_CONVEX) )
3768  continue;
3769 
3770  for( f = 0; f < SCIPexprGetMonomialNFactors(monomial); ++f )
3771  {
3772  multivarnode = SCIPexprgraphGetNodeChildren(consdata->exprgraphnode)[SCIPexprGetMonomialChildIndices(monomial)[f]];
3773 
3774  /* search for a descendant of node that has > 1 children
3775  * after simplifier run, there should be no constant expressions left
3776  */
3777  while( SCIPexprgraphGetNodeNChildren(multivarnode) == 1 )
3778  multivarnode = SCIPexprgraphGetNodeChildren(multivarnode)[0];
3779 
3780  /* if node expression is obviously univariate, then continue */
3781  if( SCIPexprgraphGetNodeNChildren(multivarnode) == 0 )
3782  {
3784  continue;
3785  }
3786 
3787  /* if multivarnode is a linear expression, then replace this by an auxiliary variable/node
3788  * mark auxiliary variable as not to multiaggregate, so SCIP cannot undo what we just did
3789  */
3791  {
3792  SCIPdebugMsg(scip, "replace linear multivariate node %p(%d,%d) in expression of cons <%s> by auxvar\n",
3793  (void*)multivarnode, SCIPexprgraphGetNodeDepth(multivarnode), SCIPexprgraphGetNodePosition(multivarnode), SCIPconsGetName(conss[c])); /*lint !e613*/
3794  SCIPdebugPrintCons(scip, conss[c], NULL); /*lint !e613*/
3795  SCIP_CALL( reformNode2Var(scip, exprgraph, multivarnode, conshdlr, naddcons, TRUE) );
3796  }
3797  }
3798  }
3799  }
3800  else
3801  {
3802  curv = SCIPexprgraphGetNodeCurvature(consdata->exprgraphnode);
3803 
3804  /* if nothing concave, then continue */
3805  if( (SCIPisInfinity(scip, consdata->rhs) || curv != SCIP_EXPRCURV_CONCAVE) &&
3806  ( SCIPisInfinity(scip, -consdata->lhs) || curv != SCIP_EXPRCURV_CONVEX) )
3807  continue;
3808 
3809  /* search for a descendant of node that has > 1 children
3810  * after simplifier run, there should be no constant expressions left
3811  */
3812  multivarnode = consdata->exprgraphnode;
3813  while( SCIPexprgraphGetNodeNChildren(multivarnode) == 1 )
3814  multivarnode = SCIPexprgraphGetNodeChildren(multivarnode)[0];
3815 
3816  /* if node expression is obviously univariate, then continue */
3817  if( SCIPexprgraphGetNodeNChildren(multivarnode) == 0 )
3818  {
3820  continue;
3821  }
3822 
3823  /* if node itself is multivariate, then continue */
3824  if( multivarnode == consdata->exprgraphnode )
3825  continue;
3826 
3827  /* if multivarnode is a linear expression, then replace this by an auxiliary variable/node
3828  * mark auxiliary variable as not to multiaggregate, so SCIP cannot undo what we just did
3829  */
3831  {
3832  SCIPdebugMsg(scip, "replace linear multivariate node %p(%d,%d) in expression of cons <%s> by auxvar\n",
3833  (void*)multivarnode, SCIPexprgraphGetNodeDepth(multivarnode), SCIPexprgraphGetNodePosition(multivarnode), SCIPconsGetName(conss[c])); /*lint !e613*/
3834  SCIPdebugPrintCons(scip, conss[c], NULL); /*lint !e613*/
3835  SCIP_CALL( reformNode2Var(scip, exprgraph, multivarnode, conshdlr, naddcons, TRUE) );
3836  }
3837  }
3838  }
3839 
3840  conshdlrdata->isreformulated = TRUE;
3841 
3842  return SCIP_OKAY;
3843 }
3844 
3845 /** computes activity and violation of a constraint
3846  *
3847  * During presolving and if the constraint is active, it is assumes that SCIPexprgraphEval has been called for sol before.
3848  *
3849  * If a solution is found to violate the variable bounds, then violation calculation is stopped and solviolbounds is set to TRUE.
3850  */
3851 static
3853  SCIP* scip, /**< SCIP data structure */
3854  SCIP_CONSHDLR* conshdlr, /**< constraint handler */
3855  SCIP_CONS* cons, /**< nonlinear constraint */
3856  SCIP_SOL* sol, /**< solution or NULL if LP solution should be used */
3857  SCIP_Bool* solviolbounds /**< buffer to indicate whether solution is found to violate variable bounds by more than feastol */
3858  )
3859 { /*lint --e{666}*/
3861  SCIP_CONSDATA* consdata;
3862  SCIP_VAR* var;
3863  SCIP_Real varval;
3864  int i;
3865 
3866  assert(scip != NULL);
3867  assert(conshdlr != NULL);
3868  assert(cons != NULL);
3869  assert(solviolbounds != NULL);
3870 
3871  conshdlrdata = SCIPconshdlrGetData(conshdlr);
3872  assert(conshdlrdata != NULL);
3873  assert(conshdlrdata->exprinterpreter != NULL);
3874 
3875  consdata = SCIPconsGetData(cons);
3876  assert(consdata != NULL);
3877 
3878  consdata->activity = 0.0;
3879  consdata->lhsviol = 0.0;
3880  consdata->rhsviol = 0.0;
3881  varval = 0.0;
3882  *solviolbounds = FALSE;
3883 
3884  for( i = 0; i < consdata->nlinvars; ++i )
3885  {
3886  SCIP_Real activity;
3887 
3888  var = consdata->linvars[i];
3889  varval = SCIPgetSolVal(scip, sol, var);
3890 
3891  /* project onto local box, in case the LP solution is slightly outside the bounds (which is not our job to enforce) */
3892  if( sol == NULL )
3893  {
3894  /* with non-initial columns, this might fail because variables can shortly be a column variable before entering the LP and have value 0.0 in this case */
3895  if( (!SCIPisInfinity(scip, -SCIPvarGetLbLocal(var)) && !SCIPisFeasGE(scip, varval, SCIPvarGetLbLocal(var))) ||
3896  (!SCIPisInfinity(scip, SCIPvarGetUbLocal(var)) && !SCIPisFeasLE(scip, varval, SCIPvarGetUbLocal(var))) )
3897  {
3898  *solviolbounds = TRUE;
3899  return SCIP_OKAY;
3900  }
3901  varval = MAX(SCIPvarGetLbLocal(var), MIN(SCIPvarGetUbLocal(var), varval));
3902  }
3903  activity = consdata->lincoefs[i] * varval;
3904 
3905  /* the contribution of a variable with |varval| = +inf is +inf when activity > 0.0, -inf when activity < 0.0, and
3906  * 0.0 otherwise
3907  */
3908  if( SCIPisInfinity(scip, REALABS(varval)) )
3909  {
3910  if( activity > 0.0 && !SCIPisInfinity(scip, consdata->rhs) )
3911  {
3912  consdata->activity = SCIPinfinity(scip);
3913  consdata->rhsviol = SCIPinfinity(scip);
3914  return SCIP_OKAY;
3915  }
3916 
3917  if( activity < 0.0 && !SCIPisInfinity(scip, -consdata->lhs) )
3918  {
3919  consdata->activity = -SCIPinfinity(scip);
3920  consdata->lhsviol = SCIPinfinity(scip);
3921  return SCIP_OKAY;
3922  }
3923  }
3924 
3925  consdata->activity += activity;
3926  }
3927 
3928  for( i = 0; i < consdata->nexprtrees; ++i )
3929  {
3930  SCIP_Real activity;
3931  SCIP_Real val;
3932  int nvars;
3933 
3934  /* compile expression tree, if not done before */
3935  if( SCIPexprtreeGetInterpreterData(consdata->exprtrees[i]) == NULL )
3936  {
3937  SCIP_CALL( SCIPexprintCompile(conshdlrdata->exprinterpreter, consdata->exprtrees[i]) );
3938  }
3939 
3940  nvars = SCIPexprtreeGetNVars(consdata->exprtrees[i]);
3941 
3942  if( nvars == 1 )
3943  {
3944  /* in the not so unusual case that an expression has only one variable, we do not need to extra allocate memory */
3945  var = SCIPexprtreeGetVars(consdata->exprtrees[i])[0];
3946  varval = SCIPgetSolVal(scip, sol, var);
3947 
3948  /* project onto local box, in case the LP solution is slightly outside the bounds (and then cannot be evaluated) */
3949  if( sol == NULL )
3950  {
3951  /* with non-initial columns, this might fail because variables can shortly be a column variable before entering the LP and have value 0.0 in this case */
3952  if( (!SCIPisInfinity(scip, -SCIPvarGetLbLocal(var)) && !SCIPisFeasGE(scip, varval, SCIPvarGetLbLocal(var))) ||
3953  (!SCIPisInfinity(scip, SCIPvarGetUbLocal(var)) && !SCIPisFeasLE(scip, varval, SCIPvarGetUbLocal(var))) )
3954  {
3955  *solviolbounds = TRUE;
3956  return SCIP_OKAY;
3957  }
3958  varval = MAX(SCIPvarGetLbLocal(var), MIN(SCIPvarGetUbLocal(var), varval));
3959  }
3960 
3961  /* coverity[callee_ptr_arith] */
3962  SCIP_CALL( SCIPexprintEval(conshdlrdata->exprinterpreter, consdata->exprtrees[i], &varval, &val) );
3963  }
3964  else
3965  {
3966  SCIP_Real* x;
3967  int j;
3968 
3969  SCIP_CALL( SCIPallocBufferArray(scip, &x, nvars) );
3970 
3971  for( j = 0; j < nvars; ++j )
3972  {
3973  var = SCIPexprtreeGetVars(consdata->exprtrees[i])[j];
3974  varval = SCIPgetSolVal(scip, sol, var);
3975 
3976  /* project onto local box, in case the LP solution is slightly outside the bounds (and then cannot be evaluated) */
3977  if( sol == NULL )
3978  {
3979  /* with non-initial columns, this might fail because variables can shortly be a column variable before entering the LP and have value 0.0 in this case */
3980  if( (!SCIPisInfinity(scip, -SCIPvarGetLbLocal(var)) && !SCIPisFeasGE(scip, varval, SCIPvarGetLbLocal(var))) ||
3981  (!SCIPisInfinity(scip, SCIPvarGetUbLocal(var)) && !SCIPisFeasLE(scip, varval, SCIPvarGetUbLocal(var))) )
3982  {
3983  *solviolbounds = TRUE;
3984  SCIPfreeBufferArray(scip, &x);
3985  return SCIP_OKAY;
3986  }
3987  varval = MAX(SCIPvarGetLbLocal(var), MIN(SCIPvarGetUbLocal(var), varval));
3988  }
3989 
3990  x[j] = varval;
3991  }
3992 
3993  SCIP_CALL( SCIPexprintEval(conshdlrdata->exprinterpreter, consdata->exprtrees[i], x, &val) );
3994 
3995  SCIPfreeBufferArray(scip, &x);
3996  }
3997 
3998  /* set the activity to infinity if a function evaluation was not valid (e.g., sqrt(-1) ) */
3999  if( !SCIPisFinite(val) )
4000  {
4001  consdata->activity = SCIPinfinity(scip);
4002  if( !SCIPisInfinity(scip, -consdata->lhs) )
4003  consdata->lhsviol = SCIPinfinity(scip);
4004  if( !SCIPisInfinity(scip, consdata->rhs) )
4005  consdata->rhsviol = SCIPinfinity(scip);
4006  return SCIP_OKAY;
4007  }
4008 
4009  /* the contribution of an expression with |val| = +inf is +inf when its coefficient is > 0.0, -inf when its coefficient is < 0.0, and
4010  * 0.0 otherwise
4011  */
4012  activity = consdata->nonlincoefs[i] * val;
4013  if( SCIPisInfinity(scip, REALABS(val)) )
4014  {
4015  if( activity > 0.0 && !SCIPisInfinity(scip, consdata->rhs) )
4016  {
4017  consdata->activity = SCIPinfinity(scip);
4018  consdata->rhsviol = SCIPinfinity(scip);
4019  return SCIP_OKAY;
4020  }
4021 
4022  if( activity < 0.0 && !SCIPisInfinity(scip, -consdata->lhs) )
4023  {
4024  consdata->activity = -SCIPinfinity(scip);
4025  consdata->lhsviol = SCIPinfinity(scip);
4026  return SCIP_OKAY;
4027  }
4028  }
4029 
4030  consdata->activity += activity;
4031  }
4032 
4033  if( consdata->nexprtrees == 0 && consdata->exprgraphnode != NULL )
4034  {
4035  SCIP_Real val;
4036 
4038 
4039  val = SCIPexprgraphGetNodeVal(consdata->exprgraphnode);
4040  assert(val != SCIP_INVALID); /*lint !e777*/
4041 
4042  /* set the activity to infinity if a function evaluation was not valid (e.g., sqrt(-1) ) */
4043  if( !SCIPisFinite(val) )
4044  {
4045  consdata->activity = SCIPinfinity(scip);
4046  if( !SCIPisInfinity(scip, -consdata->lhs) )
4047  consdata->lhsviol = SCIPinfinity(scip);
4048  if( !SCIPisInfinity(scip, consdata->rhs) )
4049  consdata->rhsviol = SCIPinfinity(scip);
4050  return SCIP_OKAY;
4051  }
4052 
4053  if( SCIPisInfinity(scip, val) && !SCIPisInfinity(scip, consdata->rhs) )
4054  {
4055  consdata->activity = SCIPinfinity(scip);
4056  consdata->rhsviol = SCIPinfinity(scip);
4057  return SCIP_OKAY;
4058  }
4059  else if( SCIPisInfinity(scip, -val) && !SCIPisInfinity(scip, -consdata->lhs) )
4060  {
4061  consdata->activity = -SCIPinfinity(scip);
4062  consdata->lhsviol = SCIPinfinity(scip);
4063  return SCIP_OKAY;
4064  }
4065 
4066  consdata->activity += val;
4067  }
4068 
4069  if( !SCIPisInfinity(scip, -consdata->lhs) && SCIPisGT(scip, consdata->lhs - consdata->activity, SCIPfeastol(scip)) )
4070  consdata->lhsviol = consdata->lhs - consdata->activity;
4071  else
4072  consdata->lhsviol = 0.0;
4073 
4074  if( !SCIPisInfinity(scip, consdata->rhs) && SCIPisGT(scip, consdata->activity - consdata->rhs, SCIPfeastol(scip)) )
4075  consdata->rhsviol = consdata->activity - consdata->rhs;
4076  else
4077  consdata->rhsviol = 0.0;
4078 
4079  /* update absolute and relative violation of the solution */
4080  if( sol != NULL )
4081  {
4082  SCIP_Real absviol;
4083  SCIP_Real relviol;
4084  SCIP_Real lhsrelviol;
4085  SCIP_Real rhsrelviol;
4086 
4087  absviol = MAX(consdata->lhsviol, consdata->rhsviol);
4088 
4089  lhsrelviol = SCIPrelDiff(consdata->lhs, consdata->activity);
4090  rhsrelviol = SCIPrelDiff(consdata->activity, consdata->rhs);
4091  relviol = MAX(lhsrelviol, rhsrelviol);
4092 
4093  SCIPupdateSolConsViolation(scip, sol, absviol, relviol);
4094  }
4095 
4096  return SCIP_OKAY;
4097 }
4098 
4099 /** computes violation of a set of constraints
4100  *
4101  * If the solution is found to violate bounds of some variable in some constraint, then violation computation is stopped and solviolbounds is set to TRUE.
4102  */
4103 static
4105  SCIP* scip, /**< SCIP data structure */
4106  SCIP_CONSHDLR* conshdlr, /**< constraint handler */
4107  SCIP_CONS** conss, /**< constraints */
4108  int nconss, /**< number of constraints */
4109  SCIP_SOL* sol, /**< solution or NULL if LP solution should be used */
4110  SCIP_Bool* solviolbounds, /**< buffer to indicate whether solution violates bounds of some variable by more than feastol */
4111  SCIP_CONS** maxviolcon /**< buffer to store constraint with largest violation, or NULL if solution is feasible */
4112  )
4113 {
4114  SCIP_CONSDATA* consdata;
4115  SCIP_Real viol;
4116  SCIP_Real maxviol;
4117  int c;
4118 
4119  assert(scip != NULL);
4120  assert(conshdlr != NULL);
4121  assert(conss != NULL || nconss == 0);
4122  assert(maxviolcon != NULL);
4123  assert(solviolbounds != NULL);
4124 
4126  {
4128  SCIP_Real* varvals;
4129 
4130  conshdlrdata = SCIPconshdlrGetData(conshdlr);
4131  assert(conshdlrdata != NULL);
4132  assert(conshdlrdata->exprgraph != NULL);
4133 
4134  SCIP_CALL( SCIPallocBufferArray(scip, &varvals, SCIPexprgraphGetNVars(conshdlrdata->exprgraph)) );
4135  SCIP_CALL( SCIPgetSolVals(scip, sol, SCIPexprgraphGetNVars(conshdlrdata->exprgraph), (SCIP_VAR**)SCIPexprgraphGetVars(conshdlrdata->exprgraph), varvals) );
4136 
4137  SCIP_CALL( SCIPexprgraphEval(conshdlrdata->exprgraph, varvals) );
4138 
4139  SCIPfreeBufferArray(scip, &varvals);
4140  }
4141 
4142  *maxviolcon = NULL;
4143 
4144  maxviol = 0.0;
4145 
4146  for( c = 0; c < nconss; ++c )
4147  {
4148  assert(conss != NULL);
4149  assert(conss[c] != NULL);
4150 
4151  SCIP_CALL( computeViolation(scip, conshdlr, conss[c], sol, solviolbounds) );
4152 
4153  /* stop if solution violates bounds */
4154  if( *solviolbounds )
4155  break;
4156 
4157  consdata = SCIPconsGetData(conss[c]);
4158  assert(consdata != NULL);
4159 
4160  viol = MAX(consdata->lhsviol, consdata->rhsviol);
4161  if( viol > maxviol && SCIPisGT(scip, viol, SCIPfeastol(scip)) )
4162  {
4163  maxviol = viol;
4164  *maxviolcon = conss[c];
4165  }
4166 
4167  /* SCIPdebugMsg(scip, "constraint <%s> violated by (%g, %g), activity = %g\n", SCIPconsGetName(conss[c]), consdata->lhsviol, consdata->rhsviol, consdata->activity); */
4168  }
4169 
4170  return SCIP_OKAY;
4171 }
4172 
4173 /** adds linearization of a constraints expression tree in reference point to a row */
4174 static
4176  SCIP* scip, /**< SCIP data structure */
4177  SCIP_EXPRINT* exprint, /**< expression interpreter */
4178  SCIP_CONS* cons, /**< constraint */
4179  int exprtreeidx, /**< for which tree a linearization should be added */
4180  SCIP_Real* x, /**< value of expression tree variables where to generate cut */
4181  SCIP_Bool newx, /**< whether the last evaluation of the expression with the expression interpreter was not at x */
4182  SCIP_ROWPREP* rowprep, /**< rowprep where to add linearization */
4183  SCIP_Bool* success /**< buffer to store whether a linearization was succefully added to the row */
4184  )
4185 {
4186  SCIP_CONSDATA* consdata;
4187  SCIP_EXPRTREE* exprtree;
4188  SCIP_Real treecoef;
4189  SCIP_Real val;
4190  SCIP_Real* grad;
4191  SCIP_Real constant = 0.0;
4192  SCIP_Bool perturbedx;
4193  int nvars;
4194  int i;
4195 
4196  assert(scip != NULL);
4197  assert(cons != NULL);
4198  assert(x != NULL);
4199  assert(rowprep != NULL);
4200  assert(success != NULL);
4201 
4202  consdata = SCIPconsGetData(cons);
4203  assert(consdata != NULL);
4204  assert(exprtreeidx >= 0);
4205  assert(exprtreeidx < consdata->nexprtrees);
4206  assert(consdata->exprtrees != NULL);
4207 
4208  exprtree = consdata->exprtrees[exprtreeidx];
4209  assert(exprtree != NULL);
4210  assert(newx || SCIPexprtreeGetInterpreterData(exprtree) != NULL);
4211 
4212  treecoef = consdata->nonlincoefs[exprtreeidx];
4213 
4214  *success = FALSE;
4215 
4216  /* compile expression if evaluated the first time; can only happen if newx is FALSE */
4217  if( newx && SCIPexprtreeGetInterpreterData(exprtree) == NULL )
4218  {
4219  SCIP_CALL( SCIPexprintCompile(exprint, exprtree) );
4220  }
4221 
4222  nvars = SCIPexprtreeGetNVars(exprtree);
4223  SCIP_CALL( SCIPallocBufferArray(scip, &grad, nvars) );
4224 
4225  perturbedx = FALSE;
4226  do
4227  {
4228  /* get value and gradient */
4229  SCIP_CALL( SCIPexprintGrad(exprint, exprtree, x, newx, &val, grad) );
4230  if( SCIPisFinite(val) && !SCIPisInfinity(scip, REALABS(val)) )
4231  {
4232  val *= treecoef;
4233  /* check gradient entries and compute constant f(refx) - grad * refx */
4234  constant = val;
4235  for( i = 0; i < nvars; ++i )
4236  {
4237  if( !SCIPisFinite(grad[i]) || SCIPisInfinity(scip, grad[i]) || SCIPisInfinity(scip, -grad[i]) )
4238  break;
4239 
4240  grad[i] *= treecoef;
4241  constant -= grad[i] * x[i];
4242 
4243  /* try to perturb x if the constant is too large */
4244  if( SCIPisInfinity(scip, REALABS(constant)) )
4245  break;
4246 
4247  /* coefficients smaller than epsilon are rounded to 0.0 when added to row, this can be wrong if variable value is very large (bad numerics)
4248  * in this case, set gradient to 0.0 here, but modify constant so that cut is still valid (if possible)
4249  * i.e., estimate grad[i]*x >= grad[i] * bound(x) or grad[i]*x <= grad[i] * bound(x), depending on whether we compute an underestimator (convex) or an overestimator (concave)
4250  * if required bound of x is not finite, then give up
4251  */
4252  if( grad[i] != 0.0 && SCIPisZero(scip, grad[i]) )
4253  {
4254  SCIP_VAR* var;
4255  SCIP_Real xbnd;
4256 
4257  var = SCIPexprtreeGetVars(exprtree)[i];
4258  if( consdata->curvatures[exprtreeidx] & SCIP_EXPRCURV_CONVEX )
4259  {
4260  xbnd = grad[i] > 0.0 ? SCIPvarGetLbGlobal(var) : SCIPvarGetUbGlobal(var);
4261  }
4262  else
4263  {
4264  assert(consdata->curvatures[exprtreeidx] & SCIP_EXPRCURV_CONCAVE);
4265  xbnd = grad[i] > 0.0 ? SCIPvarGetUbGlobal(var) : SCIPvarGetLbGlobal(var);
4266  }
4267  if( !SCIPisInfinity(scip, REALABS(xbnd)) )
4268  {
4269  SCIPdebugMsg(scip, "var <%s> [%g,%g] has tiny gradient %g, replace coefficient by constant %g\n",
4270  SCIPvarGetName(var), SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var), grad[i], grad[i] * xbnd);
4271  constant += grad[i] * xbnd;
4272  grad[i] = 0.0;
4273  }
4274  else
4275  {
4276  *success = FALSE;
4277  SCIPdebugMsg(scip, "skipping linearization, var <%s> [%g,%g] has tiny gradient %g but no finite bound in this direction\n",
4278  SCIPvarGetName(var), SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var), grad[i]);
4279  SCIPfreeBufferArray(scip, &grad);
4280  return SCIP_OKAY;
4281  }
4282  }
4283  }
4284 
4285  if( i == nvars )
4286  break;
4287  }
4288 
4289  SCIPdebugMsg(scip, "got nonfinite value in evaluation or gradient of <%s>: ", SCIPconsGetName(cons));
4290  if( !perturbedx )
4291  {
4292  SCIP_Real lb;
4293  SCIP_Real ub;
4294 
4295  SCIPdebugMsgPrint(scip, "perturbing reference point and trying again\n");
4296  for( i = 0; i < nvars; ++i )
4297  {
4298  lb = SCIPvarGetLbGlobal(SCIPexprtreeGetVars(exprtree)[i]);
4299  ub = SCIPvarGetUbGlobal(SCIPexprtreeGetVars(exprtree)[i]);
4300  if( SCIPisEQ(scip, x[i], lb) )
4301  x[i] += MIN(0.9*(ub-lb), i*SCIPfeastol(scip)); /*lint !e666*/
4302  else if( SCIPisEQ(scip, x[i], ub) )
4303  x[i] -= MIN(0.9*(ub-lb), i*SCIPfeastol(scip)); /*lint !e666*/
4304  else
4305  x[i] += MIN3(0.9*(ub-x[i]), 0.9*(x[i]-lb), i*SCIPfeastol(scip)) * (i%2 != 0 ? -1.0 : 1.0); /*lint !e666*/
4306  }
4307  newx = TRUE;
4308  perturbedx = TRUE;
4309  }
4310  else
4311  {
4312  SCIPdebugMsgPrint(scip, "skipping linearization\n");
4313  SCIPfreeBufferArray(scip, &grad);
4314  return SCIP_OKAY;
4315  }
4316  }
4317  while( TRUE ); /*lint !e506*/
4318 
4319  /* add linearization to SCIP row */
4320  SCIPaddRowprepConstant(rowprep, constant);
4321  SCIP_CALL( SCIPaddRowprepTerms(scip, rowprep, nvars, SCIPexprtreeGetVars(exprtree), grad) );
4322 
4323  *success = TRUE;
4324 
4325  SCIPfreeBufferArray(scip, &grad);
4326 
4327  SCIPdebugMsg(scip, "added linearization for tree %d of constraint <%s>\n", exprtreeidx, SCIPconsGetName(cons));
4328  SCIPdebug( SCIPprintRowprep(scip, rowprep, NULL) );
4329 
4330  return SCIP_OKAY;
4331 }
4332 
4333 /** adds secant of a constraints univariate expression tree in reference point to a row */
4334 static
4336  SCIP* scip, /**< SCIP data structure */
4337  SCIP_CONS* cons, /**< constraint */
4338  int exprtreeidx, /**< for which tree a secant should be added */
4339  SCIP_ROWPREP* rowprep, /**< rowprep where to add estimator */
4340  SCIP_Bool* success /**< buffer to store whether a secant was succefully added to the row */
4341  )
4342 {
4343  SCIP_CONSDATA* consdata;
4344  SCIP_EXPRTREE* exprtree;
4345  SCIP_Real treecoef;
4346  SCIP_VAR* var;
4347  SCIP_Real xlb;
4348  SCIP_Real xub;
4349  SCIP_Real vallb;
4350  SCIP_Real valub;
4351  SCIP_Real slope;
4352  SCIP_Real constant;
4353 
4354  assert(scip != NULL);
4355  assert(cons != NULL);
4356  assert(rowprep != NULL);
4357  assert(success != NULL);
4358 
4359  consdata = SCIPconsGetData(cons);
4360  assert(consdata != NULL);
4361  assert(exprtreeidx >= 0);
4362  assert(exprtreeidx < consdata->nexprtrees);
4363  assert(consdata->exprtrees != NULL);
4364 
4365  exprtree = consdata->exprtrees[exprtreeidx];
4366  assert(exprtree != NULL);
4367  assert(SCIPexprtreeGetNVars(exprtree) == 1);
4368 
4369  treecoef = consdata->nonlincoefs[exprtreeidx];
4370 
4371  *success = FALSE;
4372 
4373  var = SCIPexprtreeGetVars(exprtree)[0];
4374  xlb = SCIPvarGetLbLocal(var);
4375  xub = SCIPvarGetUbLocal(var);
4376 
4377  /* if variable is unbounded, then cannot really compute secant */
4378  if( SCIPisInfinity(scip, -xlb) || SCIPisInfinity(scip, xub) )
4379  {
4380  SCIPdebugMsg(scip, "skip secant for tree %d of constraint <%s> since variable is unbounded\n", exprtreeidx, SCIPconsGetName(cons));
4381  return SCIP_OKAY;
4382  }
4383  assert(SCIPisLE(scip, xlb, xub));
4384 
4385  /* coverity[callee_ptr_arith] */
4386  SCIP_CALL( SCIPexprtreeEval(exprtree, &xlb, &vallb) );
4387  if( !SCIPisFinite(vallb) || SCIPisInfinity(scip, REALABS(vallb)) )
4388  {
4389  SCIPdebugMsg(scip, "skip secant for tree %d of constraint <%s> since function cannot be evaluated in lower bound\n", exprtreeidx, SCIPconsGetName(cons));
4390  return SCIP_OKAY;
4391  }
4392  vallb *= treecoef;
4393 
4394  /* coverity[callee_ptr_arith] */
4395  SCIP_CALL( SCIPexprtreeEval(exprtree, &xub, &valub) );
4396  if( !SCIPisFinite(valub) || SCIPisInfinity(scip, REALABS(valub)) )
4397  {
4398  SCIPdebugMsg(scip, "skip secant for tree %d of constraint <%s> since function cannot be evaluated in upper bound\n", exprtreeidx, SCIPconsGetName(cons));
4399  return SCIP_OKAY;
4400  }
4401  valub *= treecoef;
4402 
4403  if( SCIPisEQ(scip, xlb, xub) )
4404  {
4405  slope = 0.0;
4406  /* choose most conservative value for the cut */
4407  if( rowprep->sidetype == SCIP_SIDETYPE_LEFT )
4408  constant = MAX(vallb, valub);
4409  else
4410  constant = MIN(vallb, valub);
4411  }
4412  else
4413  {
4414  slope = (valub - vallb) / (xub - xlb);
4415  constant = vallb - slope * xlb;
4416  }
4417 
4418  /* add secant to SCIP row */
4419  SCIPaddRowprepConstant(rowprep, constant);
4420  SCIP_CALL( SCIPaddRowprepTerm(scip, rowprep, var, slope) );
4421 
4422  *success = TRUE;
4423 
4424  SCIPdebugMsg(scip, "added secant for tree %d of constraint <%s>, slope = %g\n", exprtreeidx, SCIPconsGetName(cons), slope);
4425  SCIPdebug( SCIPprintRowprep(scip, rowprep, NULL) );
4426 
4427  return SCIP_OKAY;
4428 }
4429 
4430 /** adds estimator of a constraints bivariate expression tree to a row
4431  * a reference point is given to decide which hyperplane to choose
4432  */
4433 static
4435  SCIP* scip, /**< SCIP data structure */
4436  SCIP_CONS* cons, /**< constraint */
4437  int exprtreeidx, /**< for which tree a secant should be added */
4438  SCIP_Real* ref, /**< reference values of expression tree variables where to generate cut */
4439  SCIP_ROWPREP* rowprep, /**< rowprep where to add estimator */
4440  SCIP_Bool* success /**< buffer to store whether a secant was succefully added to the row */
4441  )
4442 {
4443  SCIP_CONSDATA* consdata;
4444  SCIP_EXPRTREE* exprtree;
4445  SCIP_Real treecoef;
4446  SCIP_VAR* x;
4447  SCIP_VAR* y;
4448  SCIP_Real xlb;
4449  SCIP_Real xub;
4450  SCIP_Real ylb;
4451  SCIP_Real yub;
4452 
4453  SCIP_Real coefx;
4454  SCIP_Real coefy;
4455  SCIP_Real constant;
4456 
4457  SCIP_Real p1[2];
4458  SCIP_Real p2[2];
4459  SCIP_Real p3[2];
4460  SCIP_Real p4[2];
4461  SCIP_Real p1val, p2val, p3val, p4val;
4462 
4463  assert(scip != NULL);
4464  assert(cons != NULL);
4465  assert(ref != NULL);
4466  assert(rowprep != NULL);
4467  assert(success != NULL);
4468 
4469  consdata = SCIPconsGetData(cons);
4470  assert(consdata != NULL);
4471  assert(exprtreeidx >= 0);
4472  assert(exprtreeidx < consdata->nexprtrees);
4473  assert(consdata->exprtrees != NULL);
4474 
4475  exprtree = consdata->exprtrees[exprtreeidx];
4476  assert(exprtree != NULL);
4477  assert(SCIPexprtreeGetNVars(exprtree) == 2);
4478 
4479  treecoef = consdata->nonlincoefs[exprtreeidx];
4480 
4481  *success = FALSE;
4482 
4483  x = SCIPexprtreeGetVars(exprtree)[0];
4484  y = SCIPexprtreeGetVars(exprtree)[1];
4485  xlb = SCIPvarGetLbLocal(x);
4486  xub = SCIPvarGetUbLocal(x);
4487  ylb = SCIPvarGetLbLocal(y);
4488  yub = SCIPvarGetUbLocal(y);
4489 
4490  if( SCIPisInfinity(scip, -xlb) || SCIPisInfinity(scip, xub) || SCIPisInfinity(scip, -ylb) || SCIPisInfinity(scip, yub) )
4491  {
4492  SCIPdebugMsg(scip, "skip bivariate secant since <%s> or <%s> is unbounded\n", SCIPvarGetName(x), SCIPvarGetName(y));
4493  return SCIP_OKAY;
4494  }
4495 
4496  /* reference point should not be outside of bounds */
4497  assert(SCIPisFeasLE(scip, xlb, ref[0]));
4498  assert(SCIPisFeasGE(scip, xub, ref[0]));
4499  ref[0] = MIN(xub, MAX(xlb, ref[0]));
4500  assert(SCIPisFeasLE(scip, ylb, ref[1]));
4501  assert(SCIPisFeasGE(scip, yub, ref[1]));
4502  ref[1] = MIN(yub, MAX(ylb, ref[1]));
4503 
4504  /* lower left */
4505  p1[0] = xlb;
4506  p1[1] = ylb;
4507 
4508  /* lower right */
4509  p2[0] = xub;
4510  p2[1] = ylb;
4511 
4512  /* upper right */
4513  p3[0] = xub;
4514  p3[1] = yub;
4515 
4516  /* upper left */
4517  p4[0] = xlb;
4518  p4[1] = yub;
4519 
4520  if( SCIPisEQ(scip, xlb, xub) && SCIPisEQ(scip, ylb, yub) )
4521  {
4522  SCIP_CALL( SCIPexprtreeEval(exprtree, p1, &p1val) );
4523 
4524  if( !SCIPisFinite(p1val) || SCIPisInfinity(scip, REALABS(p1val)) )
4525  {
4526  SCIPdebugMsg(scip, "skip secant for tree %d of constraint <%s> since function cannot be evaluated\n", exprtreeidx, SCIPconsGetName(cons));
4527  return SCIP_OKAY;
4528  }
4529 
4530  p1val *= treecoef;
4531 
4532  coefx = 0.0;
4533  coefy = 0.0;
4534  constant = p1val;
4535  }
4536  else if( SCIPisEQ(scip, xlb, xub) )
4537  {
4538  /* secant between p1 and p4: p1val + [(p4val - p1val) / (yub - ylb)] * (y - ylb) */
4539  assert(!SCIPisEQ(scip, ylb, yub));
4540 
4541  SCIP_CALL( SCIPexprtreeEval(exprtree, p1, &p1val) );
4542  SCIP_CALL( SCIPexprtreeEval(exprtree, p4, &p4val) );
4543  if( !SCIPisFinite(p1val) || SCIPisInfinity(scip, REALABS(p1val)) || !SCIPisFinite(p4val) || SCIPisInfinity(scip, REALABS(p4val)) )
4544  {
4545  SCIPdebugMsg(scip, "skip secant for tree %d of constraint <%s> since function cannot be evaluated\n", exprtreeidx, SCIPconsGetName(cons));
4546  return SCIP_OKAY;
4547  }
4548  p1val *= treecoef;
4549  p4val *= treecoef;
4550 
4551  coefx = 0.0;
4552  coefy = (p4val - p1val) / (yub - ylb);
4553  constant = p1val - coefy * ylb;
4554  }
4555  else if( SCIPisEQ(scip, ylb, yub) )
4556  {
4557  /* secant between p1 and p2: p1val + [(p2val - p1val) / (xub - xlb)] * (x - xlb) */
4558  assert(!SCIPisEQ(scip, xlb, xub));
4559 
4560  SCIP_CALL( SCIPexprtreeEval(exprtree, p1, &p1val) );
4561  SCIP_CALL( SCIPexprtreeEval(exprtree, p2, &p2val) );
4562  if( !SCIPisFinite(p1val) || SCIPisInfinity(scip, REALABS(p1val)) || !SCIPisFinite(p2val) || SCIPisInfinity(scip, REALABS(p2val)) )
4563  {
4564  SCIPdebugMsg(scip, "skip secant for tree %d of constraint <%s> since function cannot be evaluated\n", exprtreeidx, SCIPconsGetName(cons));
4565  return SCIP_OKAY;
4566  }
4567 
4568  p1val *= treecoef;
4569  p2val *= treecoef;
4570 
4571  coefx = (p2val - p1val) / (xub - xlb);
4572  coefy = 0.0;
4573  constant = p1val - coefx * xlb;
4574  }
4575  else
4576  {
4577  SCIP_Real alpha, beta, gamma_, delta;
4578  SCIP_Bool tryother;
4579  SCIP_Bool doover;
4580 
4581  /* if function is convex, then we want an overestimator, otherwise we want an underestimator */
4582  assert(consdata->curvatures[exprtreeidx] == SCIP_EXPRCURV_CONVEX || consdata->curvatures[exprtreeidx] == SCIP_EXPRCURV_CONCAVE);
4583  doover = (consdata->curvatures[exprtreeidx] & SCIP_EXPRCURV_CONVEX); /*lint !e641*/
4584 
4585  SCIP_CALL( SCIPexprtreeEval(exprtree, p1, &p1val) );
4586  SCIP_CALL( SCIPexprtreeEval(exprtree, p2, &p2val) );
4587  SCIP_CALL( SCIPexprtreeEval(exprtree, p3, &p3val) );
4588  SCIP_CALL( SCIPexprtreeEval(exprtree, p4, &p4val) );
4589  if( !SCIPisFinite(p1val) || SCIPisInfinity(scip, REALABS(p1val)) || !SCIPisFinite(p2val) || SCIPisInfinity(scip, REALABS(p2val)) ||
4590  ! SCIPisFinite(p3val) || SCIPisInfinity(scip, REALABS(p3val)) || !SCIPisFinite(p4val) || SCIPisInfinity(scip, REALABS(p4val)) )
4591  {
4592  SCIPdebugMsg(scip, "skip secant for tree %d of constraint <%s> since function cannot be evaluated\n", exprtreeidx, SCIPconsGetName(cons));
4593  return SCIP_OKAY;
4594  }
4595  p1val *= treecoef;
4596  p2val *= treecoef;
4597  p3val *= treecoef;
4598  p4val *= treecoef;
4599 
4600  /* if we want an underestimator, flip f(x,y), i.e., do as if we compute an overestimator for -f(x,y) */
4601  if( !doover )
4602  {
4603  p1val = -p1val;
4604  p2val = -p2val;
4605  p3val = -p3val;
4606  p4val = -p4val;
4607  }
4608 
4609  SCIPdebugMsg(scip, "p1 = (%g, %g), f(p1) = %g\n", p1[0], p1[1], p1val);
4610  SCIPdebugMsg(scip, "p2 = (%g, %g), f(p2) = %g\n", p2[0], p2[1], p2val);
4611  SCIPdebugMsg(scip, "p3 = (%g, %g), f(p3) = %g\n", p3[0], p3[1], p3val);
4612  SCIPdebugMsg(scip, "p4 = (%g, %g), f(p4) = %g\n", p4[0], p4[1], p4val);
4613 
4614  /* Compute coefficients alpha, beta, gamma (>0), delta such that
4615  * alpha*x + beta*y + gamma*z = delta
4616  * is satisfied by at least three of the corner points (p1,f(p1)), ..., (p4,f(p4)) and
4617  * the fourth corner point lies below this hyperplane.
4618  * Since we assume that f is convex, we then know that all points (x,y,f(x,y)) are below this hyperplane, i.e.,
4619  * alpha*x + beta*y - delta <= -gamma * f(x,y),
4620  * or, equivalently,
4621  * -alpha/gamma*x - beta/gamma*y + delta/gamma >= f(x,y).
4622  */
4623 
4624  tryother = FALSE;
4625  if( ref[1] <= ylb + (yub - ylb)/(xub - xlb) * (ref[0] - xlb) )
4626  {
4627  SCIP_CALL( SCIPcomputeHyperplaneThreePoints(scip, p1[0], p1[1], p1val, p2[0], p2[1], p2val, p3[0], p3[1], p3val,
4628  &alpha, &beta, &gamma_, &delta) );
4629 
4630  assert(SCIPisRelEQ(scip, alpha * p1[0] + beta * p1[1] - delta, -gamma_ * p1val));
4631  assert(SCIPisRelEQ(scip, alpha * p2[0] + beta * p2[1] - delta, -gamma_ * p2val));
4632  assert(SCIPisRelEQ(scip, alpha * p3[0] + beta * p3[1] - delta, -gamma_ * p3val));
4633 
4634  /* if hyperplane through p1,p2,p3 does not overestimate f(p4), then it must be the other variant */
4635  if( alpha * p4[0] + beta * p4[1] + gamma_ * p4val > delta )
4636  tryother = TRUE;
4637  else if( (!SCIPisZero(scip, alpha) && SCIPisZero(scip, alpha/gamma_)) ||
4638  ( !SCIPisZero(scip, beta) && SCIPisZero(scip, beta /gamma_)) )
4639  {
4640  /* if numerically bad, take alternative hyperplane */
4641  SCIP_CALL( SCIPcomputeHyperplaneThreePoints(scip, p1[0], p1[1], p1val, p3[0], p3[1], p3val, p4[0], p4[1],
4642  p4val, &alpha, &beta, &gamma_, &delta) );
4643 
4644  assert(SCIPisRelEQ(scip, alpha * p1[0] + beta * p1[1] - delta, -gamma_ * p1val));
4645  assert(SCIPisRelEQ(scip, alpha * p3[0] + beta * p3[1] - delta, -gamma_ * p3val));
4646  assert(SCIPisRelEQ(scip, alpha * p4[0] + beta * p4[1] - delta, -gamma_ * p4val));
4647 
4648  /* if hyperplane through p1,p3,p4 does not overestimate f(p2), then it must be the other variant */
4649  if( alpha * p2[0] + beta * p2[1] + gamma_ * p2val > delta )
4650  tryother = TRUE;
4651  }
4652  }
4653  else
4654  {
4655  SCIP_CALL( SCIPcomputeHyperplaneThreePoints(scip, p1[0], p1[1], p1val, p3[0], p3[1], p3val, p4[0], p4[1], p4val,
4656  &alpha, &beta, &gamma_, &delta) );
4657 
4658  assert(SCIPisRelEQ(scip, alpha * p1[0] + beta * p1[1] - delta, -gamma_ * p1val));
4659  assert(SCIPisRelEQ(scip, alpha * p3[0] + beta * p3[1] - delta, -gamma_ * p3val));
4660  assert(SCIPisRelEQ(scip, alpha * p4[0] + beta * p4[1] - delta, -gamma_ * p4val));
4661 
4662  /* if hyperplane through p1,p3,p4 does not overestimate f(p2), then it must be the other variant */
4663  if( alpha * p2[0] + beta * p2[1] + gamma_ * p2val > delta )
4664  tryother = TRUE;
4665  else if( (!SCIPisZero(scip, alpha) && SCIPisZero(scip, alpha/gamma_)) ||
4666  ( !SCIPisZero(scip, beta) && SCIPisZero(scip, beta /gamma_)) )
4667  {
4668  /* if numerically bad, take alternative */
4669  SCIP_CALL( SCIPcomputeHyperplaneThreePoints(scip, p1[0], p1[1], p1val, p2[0], p2[1], p2val, p3[0], p3[1],
4670  p3val, &alpha, &beta, &gamma_, &delta) );
4671 
4672  assert(SCIPisRelEQ(scip, alpha * p1[0] + beta * p1[1] - delta, -gamma_ * p1val));
4673  assert(SCIPisRelEQ(scip, alpha * p2[0] + beta * p2[1] - delta, -gamma_ * p2val));
4674  assert(SCIPisRelEQ(scip, alpha * p3[0] + beta * p3[1] - delta, -gamma_ * p3val));
4675 
4676  /* if hyperplane through p1,p2,p3 does not overestimate f(p4), then it must be the other variant */
4677  if( alpha * p4[0] + beta * p4[1] + gamma_ * p4val > delta )
4678  tryother = TRUE;
4679  }
4680  }
4681 
4682  if( tryother )
4683  {
4684  if( ref[1] <= yub + (ylb - yub)/(xub - xlb) * (ref[0] - xlb) )
4685  {
4686  SCIP_CALL( SCIPcomputeHyperplaneThreePoints(scip, p1[0], p1[1], p1val, p2[0], p2[1], p2val, p4[0], p4[1],
4687  p4val, &alpha, &beta, &gamma_, &delta) );
4688 
4689  /* hyperplane should be above (p3,f(p3)) and other points should lie on hyperplane */
4690  assert(SCIPisRelEQ(scip, alpha * p1[0] + beta * p1[1] - delta, -gamma_ * p1val));
4691  assert(SCIPisRelEQ(scip, alpha * p2[0] + beta * p2[1] - delta, -gamma_ * p2val));
4692  assert(SCIPisRelLE(scip, alpha * p3[0] + beta * p3[1] - delta, -gamma_ * p3val));
4693  assert(SCIPisRelEQ(scip, alpha * p4[0] + beta * p4[1] - delta, -gamma_ * p4val));
4694 
4695  if( (!SCIPisZero(scip, alpha) && SCIPisZero(scip, alpha/gamma_)) ||
4696  ( !SCIPisZero(scip, beta) && SCIPisZero(scip, beta /gamma_)) )
4697  {
4698  /* if numerically bad, take alternative */
4699  SCIP_CALL( SCIPcomputeHyperplaneThreePoints(scip, p2[0], p2[1], p2val, p3[0], p3[1], p3val, p4[0], p4[1],
4700  p4val, &alpha, &beta, &gamma_, &delta) );
4701 
4702  /* hyperplane should be above (p1,f(p1)) and other points should lie on hyperplane */
4703  assert(SCIPisRelLE(scip, alpha * p1[0] + beta * p1[1] - delta, -gamma_ * p1val));
4704  assert(SCIPisRelEQ(scip, alpha * p2[0] + beta * p2[1] - delta, -gamma_ * p2val));
4705  assert(SCIPisRelEQ(scip, alpha * p3[0] + beta * p3[1] - delta, -gamma_ * p3val));
4706  assert(SCIPisRelEQ(scip, alpha * p4[0] + beta * p4[1] - delta, -gamma_ * p4val));
4707  }
4708  }
4709  else
4710  {
4711  SCIP_CALL( SCIPcomputeHyperplaneThreePoints(scip, p2[0], p2[1], p2val, p3[0], p3[1], p3val, p4[0], p4[1],
4712  p4val, &alpha, &beta, &gamma_, &delta) );
4713 
4714  /* hyperplane should be above (p1,f(p1)) and other points should lie on hyperplane */
4715  assert(SCIPisRelLE(scip, alpha * p1[0] + beta * p1[1] - delta, -gamma_ * p1val));
4716  assert(SCIPisRelEQ(scip, alpha * p2[0] + beta * p2[1] - delta, -gamma_ * p2val));
4717  assert(SCIPisRelEQ(scip, alpha * p3[0] + beta * p3[1] - delta, -gamma_ * p3val));
4718  assert(SCIPisRelEQ(scip, alpha * p4[0] + beta * p4[1] - delta, -gamma_ * p4val));
4719 
4720  if( (!SCIPisZero(scip, alpha) && SCIPisZero(scip, alpha/gamma_)) ||
4721  ( !SCIPisZero(scip, beta) && SCIPisZero(scip, beta /gamma_)) )
4722  {
4723  /* if numerically bad, take alternative */
4724  SCIP_CALL( SCIPcomputeHyperplaneThreePoints(scip, p1[0], p1[1], p1val, p2[0], p2[1], p2val, p4[0], p4[1],
4725  p4val, &alpha, &beta, &gamma_, &delta) );
4726 
4727  /* hyperplane should be above (p3,f(p3)) and other points should lie on hyperplane */
4728  assert(SCIPisRelEQ(scip, alpha * p1[0] + beta * p1[1] - delta, -gamma_ * p1val));
4729  assert(SCIPisRelEQ(scip, alpha * p2[0] + beta * p2[1] - delta, -gamma_ * p2val));
4730  assert(SCIPisRelLE(scip, alpha * p3[0] + beta * p3[1] - delta, -gamma_ * p3val));
4731  assert(SCIPisRelEQ(scip, alpha * p4[0] + beta * p4[1] - delta, -gamma_ * p4val));
4732  }
4733  }
4734  }
4735 
4736  SCIPdebugMsg(scip, "alpha = %g, beta = %g, gamma = %g, delta = %g\n", alpha, beta, gamma_, delta);
4737 
4738  /* check if bad luck: should not happen if xlb != xub and ylb != yub and numerics are fine */
4739  if( SCIPisZero(scip, gamma_) )
4740  return SCIP_OKAY;
4741  assert(!SCIPisNegative(scip, gamma_));
4742 
4743  /* flip hyperplane */
4744  if( !doover )
4745  gamma_ = -gamma_;
4746 
4747  coefx = -alpha / gamma_;
4748  coefy = -beta / gamma_;
4749  constant = delta / gamma_;
4750 
4751  /* if we loose coefficients because division by gamma makes them < SCIPepsilon(scip), then better not generate a cut here */
4752  if( (!SCIPisZero(scip, alpha) && SCIPisZero(scip, coefx)) ||
4753  ( !SCIPisZero(scip, beta) && SCIPisZero(scip, coefy)) )
4754  {
4755  SCIPdebugMsg(scip, "skip bivar secant for <%s> tree %d due to bad numerics\n", SCIPconsGetName(cons), exprtreeidx);
4756  return SCIP_OKAY;
4757  }
4758  }
4759 
4760  /* add hyperplane coefs to SCIP row */
4761  SCIPaddRowprepConstant(rowprep, constant);
4762  SCIP_CALL( SCIPaddRowprepTerm(scip, rowprep, x, coefx) );
4763  SCIP_CALL( SCIPaddRowprepTerm(scip, rowprep, y, coefy) );
4764 
4765  *success = TRUE;
4766 
4767  SCIPdebugMsg(scip, "added bivariate secant for tree %d of constraint <%s>\n", exprtreeidx, SCIPconsGetName(cons));
4768  SCIPdebug( SCIPprintRowprep(scip, rowprep, NULL) );
4769 
4770  return SCIP_OKAY;
4771 }
4772 
4773 /** internal method using an auxiliary LPI, see addConcaveEstimatorMultivariate() */
4774 static
4775 SCIP_RETCODE _addConcaveEstimatorMultivariate(
4776  SCIP* scip, /**< SCIP data structure */
4777  SCIP_LPI* lpi, /**< auxiliary LPI */
4778  SCIP_CONS* cons, /**< constraint */
4779  int exprtreeidx, /**< for which tree a secant should be added */
4780  SCIP_Real* ref, /**< reference values of expression tree variables where to generate cut */
4781  SCIP_ROWPREP* rowprep, /**< rowprep where to add estimator */
4782  SCIP_VAR** vars, /**< variables of the constraint */
4783  SCIP_EXPRTREE* exprtree, /**< expression tree of constraint */
4784  int nvars, /**< number of variables */
4785  SCIP_Bool doupper, /**< should an upper estimator be computed */
4786  SCIP_Bool* success /**< buffer to store whether a secant was succefully added to the row */
4787  )
4788 {
4789  SCIP_CONSDATA* consdata;
4790  SCIP_Real* val;
4791  SCIP_Real* obj;
4792  SCIP_Real* lb;
4793  SCIP_Real* ub;
4794  SCIP_Real* corner;
4795  SCIP_Real* lhs;
4796  SCIP_Real* rhs;
4797  int* beg;
4798  int* ind;
4799  SCIP_Real lpobj;
4800  int ncols;
4801  int nrows;
4802  int nnonz;
4803  SCIP_Real funcval;
4804  SCIP_Real treecoef;
4805 
4806  int i;
4807  int j;
4808  int idx;
4809 
4810  SCIP_RETCODE lpret;
4811 
4812  assert(lpi != NULL);
4813  assert(nvars <= 10);
4814 
4815  consdata = SCIPconsGetData(cons);
4816  treecoef = consdata->nonlincoefs[exprtreeidx];
4817 
4818  /* columns are cut coefficients plus constant */
4819  ncols = nvars + 1;
4820  SCIP_CALL( SCIPallocBufferArray(scip, &obj, ncols) );
4821  SCIP_CALL( SCIPallocBufferArray(scip, &lb, ncols) );
4822  SCIP_CALL( SCIPallocBufferArray(scip, &ub, ncols) );
4823  corner = lb; /* will not use lb and corner simultaneously, so can share memory */
4824 
4825  /* one row for each corner of domain, i.e., 2^nvars many */
4826  nrows = (int)(1u << nvars);
4827  SCIP_CALL( SCIPallocBufferArray(scip, &lhs, nrows) );
4828  SCIP_CALL( SCIPallocBufferArray(scip, &rhs, nrows) );
4829 
4830  /* the coefficients matrix will have at most ncols * nrows many nonzeros */
4831  nnonz = nrows * ncols;
4832  SCIP_CALL( SCIPallocBufferArray(scip, &beg, nrows+1) );
4833  SCIP_CALL( SCIPallocBufferArray(scip, &ind, nnonz) );
4834  SCIP_CALL( SCIPallocBufferArray(scip, &val, nnonz) );
4835 
4836  /* setup LP data */
4837  idx = 0;
4838  for( i = 0; i < nrows; ++i )
4839  {
4840  /* assemble corner point */
4841  SCIPdebugMsg(scip, "f(");
4842  for( j = 0; j < nvars; ++j )
4843  {
4844  /* if j'th bit of row index i is set, then take upper bound on var j, otherwise lower bound var j
4845  * we check this by shifting i for j positions to the right and checking whether the j'th bit is set */
4846  if( ((unsigned int)i >> j) & 0x1 )
4847  corner[j] = SCIPvarGetUbLocal(vars[j]);
4848  else
4849  corner[j] = SCIPvarGetLbLocal(vars[j]);
4850  SCIPdebugMsgPrint(scip, "%g, ", corner[j]);
4851  assert(!SCIPisInfinity(scip, REALABS(corner[j])));
4852  }
4853 
4854  /* evaluate function in current corner */
4855  SCIP_CALL( SCIPexprtreeEval(exprtree, corner, &funcval) );
4856  SCIPdebugMsgPrint(scip, ") = %g\n", funcval);
4857 
4858  if( !SCIPisFinite(funcval) || SCIPisInfinity(scip, REALABS(funcval)) )
4859  {
4860  SCIPdebugMsg(scip, "cannot compute underestimator for concave because constaint <%s> cannot be evaluated\n", SCIPconsGetName(cons));
4861  goto TERMINATE;
4862  }
4863 
4864  funcval *= treecoef;
4865 
4866  if( !doupper )
4867  {
4868  lhs[i] = -SCIPlpiInfinity(lpi);
4869  rhs[i] = funcval;
4870  }
4871  else
4872  {
4873  lhs[i] = funcval;
4874  rhs[i] = SCIPlpiInfinity(lpi);
4875  }
4876 
4877  /* add nonzeros of corner to matrix */
4878  beg[i] = idx;
4879  for( j = 0; j < nvars; ++j )
4880  {
4881  if( corner[j] != 0.0 )
4882  {
4883  ind[idx] = j;
4884  val[idx] = corner[j];
4885  ++idx;
4886  }
4887  }
4888 
4889  /* coefficient for constant is 1.0 */
4890  val[idx] = 1.0;
4891  ind[idx] = nvars;
4892  ++idx;
4893  }
4894  nnonz = idx;
4895  beg[nrows] = nnonz;
4896 
4897  for( j = 0; j < ncols; ++j )
4898  {
4899  lb[j] = -SCIPlpiInfinity(lpi);
4900  ub[j] = SCIPlpiInfinity(lpi);
4901  }
4902 
4903  /* objective coefficients are reference points, and an additional 1.0 for the constant */
4904  BMScopyMemoryArray(obj, ref, nvars);
4905  obj[nvars] = 1.0;
4906 
4907  /* get function value in reference point, so we can use this as a cutoff */
4908  SCIP_CALL( SCIPexprtreeEval(exprtree, ref, &funcval) );
4909  funcval *= treecoef;
4910 
4911  SCIP_CALL( SCIPlpiAddCols(lpi, ncols, obj, lb, ub, NULL, 0, NULL, NULL, NULL) );
4912  SCIP_CALL( SCIPlpiAddRows(lpi, nrows, lhs, rhs, NULL, nnonz, beg, ind, val) );
4913 
4914  /* make use of this convenient features, since for us nrows >> ncols */
4915  /*SCIP_CALL( SCIPlpiSetRealpar(lpi, SCIP_LPPAR_ROWREPSWITCH, 5.0) ); */
4916  /* get accurate coefficients */
4918  SCIP_CALL( SCIPlpiSetRealpar(lpi, SCIP_LPPAR_OBJLIM, funcval) );
4919  SCIP_CALL( SCIPlpiSetIntpar(lpi, SCIP_LPPAR_LPITLIM, 10 * nvars) );
4922 
4923  /* SCIPdebug( SCIP_CALL( SCIPlpiSetIntpar(lpi, SCIP_LPPAR_LPINFO, 1) ) ); */
4924 
4925  lpret = SCIPlpiSolveDual(lpi);
4926  if( lpret != SCIP_OKAY )
4927  {
4928  SCIPwarningMessage(scip, "solving auxiliary LP for underestimator of concave function returned %d\n", lpret);
4929  goto TERMINATE;
4930  }
4931 
4932  if( !SCIPlpiIsPrimalFeasible(lpi) )
4933  {
4934  SCIPdebugMsg(scip, "failed to find feasible solution for auxiliary LP for underestimator of concave function, iterlimexc = %u, cutoff = %u, unbounded = %u\n", SCIPlpiIsIterlimExc(lpi), SCIPlpiIsObjlimExc(lpi), SCIPlpiIsPrimalUnbounded(lpi));
4935  goto TERMINATE;
4936  }
4937  /* should be either solved to optimality, or the objective or iteration limit be hit */
4938  assert(SCIPlpiIsOptimal(lpi) || SCIPlpiIsObjlimExc(lpi) || SCIPlpiIsIterlimExc(lpi));
4939 
4940  /* setup row coefficient, reuse obj array to store LP sol values */
4941  SCIP_CALL( SCIPlpiGetSol(lpi, &lpobj, obj, NULL, NULL, NULL) );
4942 
4943  /* check that computed hyperplane is on right side of function in refpoint
4944  * if numerics is very bad (e.g., st_e32), then even this can happen */
4945  if( (!doupper && SCIPisFeasGT(scip, lpobj, funcval)) || (doupper && SCIPisFeasGT(scip, funcval, lpobj)) )
4946  {
4947  SCIPwarningMessage(scip, "computed cut does not underestimate concave function in refpoint\n");
4948  goto TERMINATE;
4949  }
4950  assert( doupper || SCIPisFeasLE(scip, lpobj, funcval) );
4951  assert(!doupper || SCIPisFeasLE(scip, funcval, lpobj) );
4952 
4953  /* add estimator to rowprep */
4954  SCIPaddRowprepConstant(rowprep, obj[nvars]);
4955  SCIP_CALL( SCIPaddRowprepTerms(scip, rowprep, nvars, vars, obj) );
4956 
4957  *success = TRUE;
4958 
4959 TERMINATE:
4960  SCIPfreeBufferArray(scip, &val);
4961  SCIPfreeBufferArray(scip, &ind);
4962  SCIPfreeBufferArray(scip, &beg);
4963  SCIPfreeBufferArray(scip, &rhs);
4964  SCIPfreeBufferArray(scip, &lhs);
4965  SCIPfreeBufferArray(scip, &ub);
4966  SCIPfreeBufferArray(scip, &lb);
4967  SCIPfreeBufferArray(scip, &obj);
4968 
4969  return SCIP_OKAY;
4970 }
4971 
4972 
4973 
4974 /** adds estimator of a constraints multivariate expression tree to a row
4975  * Given concave function f(x) and reference point ref.
4976  * Let (v_i: i=1,...,n) be corner points of current domain of x.
4977  * Find (coef,constant) such that <coef,v_i> + constant <= f(v_i) (cut validity) and
4978  * such that <coef, ref> + constant is maximized (cut efficacy).
4979  * Then <coef, x> + constant <= f(x) for all x in current domain.
4980  *
4981  * Similar to compute an overestimator for a convex function f(x).
4982  * Find (coef,constant) such that <coef,v_i> + constant >= f(v_i) and
4983  * such that <coef, ref> + constant is minimized.
4984  * Then <coef, x> + constant >= f(x) for all x in current domain.
4985  */
4986 static
4988  SCIP* scip, /**< SCIP data structure */
4989  SCIP_CONS* cons, /**< constraint */
4990  int exprtreeidx, /**< for which tree a secant should be added */
4991  SCIP_Real* ref, /**< reference values of expression tree variables where to generate cut */
4992  SCIP_ROWPREP* rowprep, /**< rowprep where to add estimator */
4993  SCIP_Bool* success /**< buffer to store whether a secant was succefully added to the row */
4994  )
4995 {
4996  SCIP_VAR** vars;
4997  SCIP_CONSDATA* consdata;
4998  SCIP_EXPRTREE* exprtree;
4999  SCIP_LPI* lpi;
5000  int nvars;
5001  int j;
5002  SCIP_Bool doupper;
5003 
5004  SCIP_RETCODE retcode;
5005 
5006  static SCIP_Bool warned_highdim_concave = FALSE;
5007 
5008  assert(scip != NULL);
5009  assert(cons != NULL);
5010  assert(ref != NULL);
5011  assert(rowprep != NULL);
5012  assert(success != NULL);
5013 
5014  consdata = SCIPconsGetData(cons);
5015  assert(consdata != NULL);
5016  assert(exprtreeidx >= 0);
5017  assert(exprtreeidx < consdata->nexprtrees);
5018  assert(consdata->exprtrees != NULL);
5019 
5020  exprtree = consdata->exprtrees[exprtreeidx];
5021  assert(exprtree != NULL);
5022 
5023  nvars = SCIPexprtreeGetNVars(exprtree);
5024  assert(nvars >= 2);
5025 
5026  *success = FALSE;
5027 
5028  /* size of LP is exponential in number of variables of tree, so do only for small trees */
5029  if( nvars > 10 )
5030  {
5031  if( !warned_highdim_concave )
5032  {
5033  SCIPwarningMessage(scip, "concave function in constraint <%s> too high-dimensional to compute underestimator\n", SCIPconsGetName(cons));
5034  warned_highdim_concave = TRUE;
5035  }
5036  return SCIP_OKAY;
5037  }
5038 
5039  vars = SCIPexprtreeGetVars(exprtree);
5040 
5041  /* check whether bounds are finite
5042  * make sure reference point is strictly within bounds
5043  * otherwise we can easily get an unbounded LP below, e.g., with instances like ex6_2_* from GlobalLib
5044  */
5045  for( j = 0; j < nvars; ++j )
5046  {
5047  if( SCIPisInfinity(scip, -SCIPvarGetLbLocal(vars[j])) || SCIPisInfinity(scip, SCIPvarGetUbLocal(vars[j])) )
5048  {
5049  SCIPdebugMsg(scip, "cannot compute underestimator for concave because variable <%s> is unbounded\n", SCIPvarGetName(vars[j]));
5050  return SCIP_OKAY;
5051  }
5052  assert(SCIPisFeasLE(scip, SCIPvarGetLbLocal(vars[j]), ref[j]));
5053  assert(SCIPisFeasGE(scip, SCIPvarGetUbLocal(vars[j]), ref[j]));
5054  ref[j] = MIN(SCIPvarGetUbLocal(vars[j]), MAX(SCIPvarGetLbLocal(vars[j]), ref[j])); /*lint !e666*/
5055  }
5056 
5057  /* create empty auxiliary LP and decide its objective sense */
5058  assert(consdata->curvatures[exprtreeidx] == SCIP_EXPRCURV_CONVEX || consdata->curvatures[exprtreeidx] == SCIP_EXPRCURV_CONCAVE);
5059  doupper = (consdata->curvatures[exprtreeidx] & SCIP_EXPRCURV_CONVEX); /*lint !e641*/
5060  SCIP_CALL( SCIPlpiCreate(&lpi, SCIPgetMessagehdlr(scip), "concaveunderest", doupper ? SCIP_OBJSEN_MINIMIZE : SCIP_OBJSEN_MAXIMIZE) );
5061  if( lpi == NULL )
5062  {
5063  SCIPerrorMessage("failed to create auxiliary LP\n");
5064  return SCIP_ERROR;
5065  }
5066 
5067  /* capture the retcode, free the LPI afterwards */
5068  retcode = _addConcaveEstimatorMultivariate(scip, lpi, cons, exprtreeidx, ref, rowprep, vars, exprtree, nvars, doupper, success);
5069 
5070  assert(lpi != NULL);
5071  SCIP_CALL( SCIPlpiFree(&lpi) );
5072 
5073  return retcode;
5074 }
5075 
5076 /** Computes the linear coeffs and the constant in a linear expression
5077  * both scaled by a given scalar value.
5078  * The coeffs of the variables will be stored in the given array at
5079  * their variable index.
5080  * The constant of the given linear expression will be added to the given
5081  * buffer.
5082  */
5083 static
5085  SCIP_EXPR* expr, /**< the linear expression */
5086  SCIP_Real scalar, /**< the scalar value, i.e. the coeff of the given expression */
5087  SCIP_Real* varcoeffs, /**< buffer array to store the computed coefficients */
5088  SCIP_Real* constant /**< buffer to hold the constant value of the given expression */
5089  )
5090 {
5091  switch( SCIPexprGetOperator( expr ) )
5092  {
5093  case SCIP_EXPR_VARIDX: /* set coeff for this variable to current scalar */
5094  {
5095  /* TODO: can a linear expression contain the same variable twice?
5096  * if yes varcoeffs need to be initialized to zero before calling this function
5097  * and coeff must not be overridden but summed up instead. */
5098  varcoeffs[SCIPexprGetOpIndex( expr )] = scalar;
5099  return SCIP_OKAY;
5100  }
5101 
5102  case SCIP_EXPR_CONST:
5103  {
5104  /* constant value increases */
5105  *constant += scalar * SCIPexprGetOpReal( expr );
5106  return SCIP_OKAY;
5107  }
5108 
5109  case SCIP_EXPR_MUL: /* need to find the constant part of the muliplication and then recurse */
5110  {
5111  SCIP_EXPR** children;
5112  children = SCIPexprGetChildren( expr );
5113 
5114  /* first part is constant */
5115  if( SCIPexprGetOperator( children[0] ) == SCIP_EXPR_CONST )
5116  {
5117  SCIP_CALL( getCoeffsAndConstantFromLinearExpr( children[1], scalar * SCIPexprGetOpReal( children[0] ), varcoeffs, constant ) );
5118  return SCIP_OKAY;
5119  }
5120 
5121  /* second part is constant */
5122  if( SCIPexprGetOperator( children[1] ) == SCIP_EXPR_CONST )
5123  {
5124  SCIP_CALL( getCoeffsAndConstantFromLinearExpr( children[0], scalar * SCIPexprGetOpReal( children[1] ), varcoeffs, constant ) );
5125  return SCIP_OKAY;
5126  }
5127 
5128  /* nonlinear -> break out to error case */
5129  break;
5130  }
5131 
5132  case SCIP_EXPR_PLUS: /* just recurse */
5133  {
5134  SCIP_EXPR** children;
5135  children = SCIPexprGetChildren( expr );
5136  SCIP_CALL( getCoeffsAndConstantFromLinearExpr( children[0], scalar, varcoeffs, constant ) );
5137  SCIP_CALL( getCoeffsAndConstantFromLinearExpr( children[1], scalar, varcoeffs, constant ) );
5138  return SCIP_OKAY;
5139  }
5140 
5141  case SCIP_EXPR_MINUS: /* recursion on second child is called with negated scalar */
5142  {
5143  SCIP_EXPR** children;
5144  children = SCIPexprGetChildren( expr );
5145  SCIP_CALL( getCoeffsAndConstantFromLinearExpr( children[0], scalar, varcoeffs, constant ) );
5146  SCIP_CALL( getCoeffsAndConstantFromLinearExpr( children[1], -scalar, varcoeffs, constant ) );
5147  return SCIP_OKAY;
5148  }
5149 
5150  case SCIP_EXPR_SUM: /* just recurse */
5151  {
5152  SCIP_EXPR** children;
5153  int nchildren;
5154  int c;
5155 
5156  children = SCIPexprGetChildren(expr);
5157  nchildren = SCIPexprGetNChildren(expr);
5158 
5159  for( c = 0; c < nchildren; ++c )
5160  {
5161  SCIP_CALL( getCoeffsAndConstantFromLinearExpr( children[c], scalar, varcoeffs, constant ) );
5162  }
5163 
5164  return SCIP_OKAY;
5165  }
5166 
5167  case SCIP_EXPR_LINEAR: /* add scaled constant and recurse on children with their coeff multiplied into scalar */
5168  {
5169  SCIP_Real* childCoeffs;
5170  SCIP_EXPR** children;
5171  int i;
5172 
5173  *constant += scalar * SCIPexprGetLinearConstant( expr );
5174 
5175  children = SCIPexprGetChildren( expr );
5176  childCoeffs = SCIPexprGetLinearCoefs( expr );
5177 
5178  for( i = 0; i < SCIPexprGetNChildren( expr ); ++i )
5179  {
5180  SCIP_CALL( getCoeffsAndConstantFromLinearExpr( children[i], scalar * childCoeffs[i], varcoeffs, constant ) );
5181  }
5182 
5183  return SCIP_OKAY;
5184  }
5185 
5186  default:
5187  break;
5188  } /*lint !e788*/
5189 
5190  SCIPerrorMessage( "Cannot extract linear coefficients from expressions with operator %d %s\n", SCIPexprGetOperator( expr ), SCIPexpropGetName(SCIPexprGetOperator( expr )));
5191  SCIPABORT();
5192  return SCIP_ERROR; /*lint !e527*/
5193 }
5194 
5195 /** adds estimator from user callback of a constraints user expression tree to a row
5196  */
5197 static
5199  SCIP* scip, /**< SCIP data structure */
5200  SCIP_CONS* cons, /**< constraint */
5201  int exprtreeidx, /**< for which tree an estimator should be added */
5202  SCIP_Real* x, /**< value of expression tree variables where to generate cut */
5203  SCIP_Bool overestimate, /**< whether to compute an overestimator instead of an underestimator */
5204  SCIP_ROWPREP* rowprep, /**< rowprep where to add estimator */
5205  SCIP_Bool* success /**< buffer to store whether an estimator was succefully added to the rowprep */
5206  )
5207 {
5208  SCIP_CONSDATA* consdata;
5209  SCIP_EXPRTREE* exprtree;
5210  SCIP_EXPR** children;
5211  SCIP_VAR** vars;
5212  SCIP_Real* params;
5213  SCIP_INTERVAL* varbounds;
5214 
5215  SCIP_INTERVAL* childbounds;
5216  SCIP_Real* childvals;
5217  SCIP_Real* childcoeffs;
5218 
5219  SCIP_Real constant;
5220  SCIP_Real treecoef;
5221  int nvars;
5222  int nchildren;
5223  int i;
5224 
5225  consdata = SCIPconsGetData( cons );
5226  assert( consdata != NULL );
5227  assert( exprtreeidx >= 0 );
5228  assert( exprtreeidx < consdata->nexprtrees );
5229  assert( consdata->exprtrees != NULL );
5230  assert( rowprep != NULL );
5231  assert( success != NULL );
5232 
5233  exprtree = consdata->exprtrees[exprtreeidx];
5234  assert( exprtree != NULL );
5235  assert( SCIPexprGetOperator(SCIPexprtreeGetRoot(exprtree)) == SCIP_EXPR_USER );
5236 
5237  /* if user did not implement estimator callback, then we cannot do anything */
5239  {
5240  *success = FALSE;
5241  return SCIP_OKAY;
5242  }
5243 
5244  params = SCIPexprtreeGetParamVals( exprtree );
5245  nvars = SCIPexprtreeGetNVars( exprtree );
5246  vars = SCIPexprtreeGetVars( exprtree );
5247  nchildren = SCIPexprGetNChildren( SCIPexprtreeGetRoot( exprtree ) );
5248  children = SCIPexprGetChildren( SCIPexprtreeGetRoot( exprtree ) );
5249 
5250  /* Get bounds of variables */
5251  SCIP_CALL( SCIPallocBufferArray( scip, &varbounds, nchildren ) );
5252 
5253  for( i = 0; i < nvars; ++i )
5254  {
5255  double lb = SCIPvarGetLbLocal( vars[i] );
5256  double ub = SCIPvarGetUbLocal( vars[i] );
5257  SCIPintervalSetBounds( &varbounds[i],
5258  -infty2infty( SCIPinfinity( scip ), INTERVALINFTY, -MIN( lb, ub ) ),
5259  +infty2infty( SCIPinfinity( scip ), INTERVALINFTY, MAX( lb, ub ) ) );
5260  }
5261 
5262  /* Compute bounds and solution value for the user expressions children */
5263  SCIP_CALL( SCIPallocBufferArray( scip, &childcoeffs, nchildren ) );
5264  SCIP_CALL( SCIPallocBufferArray( scip, &childbounds, nchildren ) );
5265  SCIP_CALL( SCIPallocBufferArray( scip, &childvals, nchildren ) );
5266 
5267  for( i = 0; i < nchildren; ++i )
5268  {
5269  SCIP_CALL( SCIPexprEval( children[i], x, params, &childvals[i] ) );
5270  SCIP_CALL( SCIPexprEvalInt( children[i], INTERVALINFTY, varbounds, params, &childbounds[i] ) );
5271  }
5272 
5273  /* varbounds not needed any longer */
5274  SCIPfreeBufferArray( scip, &varbounds );
5275 
5276  /* call estimator for user expressions to compute coeffs and constant for the user expressions children */
5277  SCIP_CALL( SCIPexprEstimateUser( SCIPexprtreeGetRoot( exprtree ), INTERVALINFTY, childvals, childbounds, overestimate, childcoeffs, &constant, success ) );
5278 
5279  if( *success )
5280  {
5281  SCIP_Real* varcoeffs;
5282  SCIP_CALL( SCIPallocBufferArray( scip, &varcoeffs, nvars ) );
5283 
5284  treecoef = consdata->nonlincoefs[exprtreeidx];
5285  constant *= treecoef;
5286 
5287  for( i = 0; i < nchildren; ++i )
5288  {
5289  SCIP_CALL( getCoeffsAndConstantFromLinearExpr( children[i], childcoeffs[i]*treecoef, varcoeffs, &constant ) );
5290  }
5291 
5292  SCIPaddRowprepConstant(rowprep, constant);
5293  SCIP_CALL( SCIPaddRowprepTerms(scip, rowprep, nvars, vars, varcoeffs) );
5294 
5295  SCIPfreeBufferArray( scip, &varcoeffs );
5296  }
5297 
5298  SCIPfreeBufferArray( scip, &childcoeffs );
5299  SCIPfreeBufferArray( scip, &childbounds );
5300  SCIPfreeBufferArray( scip, &childvals );
5301 
5302  return SCIP_OKAY;
5303 }
5304 
5305 /** adds estimator from interval gradient of a constraints univariate expression tree to a row
5306  * a reference point is used to decide in which corner to generate the cut
5307  */
5308 static
5310  SCIP* scip, /**< SCIP data structure */
5311  SCIP_EXPRINT* exprint, /**< expression interpreter */
5312  SCIP_CONS* cons, /**< constraint */
5313  int exprtreeidx, /**< for which tree a secant should be added */
5314  SCIP_Real* x, /**< value of expression tree variables where to generate cut */
5315  SCIP_Bool newx, /**< whether the last evaluation of the expression with the expression interpreter was not at x */
5316  SCIP_Bool overestimate, /**< whether to compute an overestimator instead of an underestimator */
5317  SCIP_ROWPREP* rowprep, /**< rowprep where to add estimator */
5318  SCIP_Bool* success /**< buffer to store whether an estimator was succefully added to the rowprep */
5319  )
5320 {
5321  SCIP_CONSDATA* consdata;
5322  SCIP_EXPRTREE* exprtree;
5323  SCIP_Real treecoef;
5324  SCIP_Real* coefs;
5325  SCIP_Real constant;
5326  SCIP_Real val;
5327  SCIP_Real lb;
5328  SCIP_Real ub;
5329  SCIP_INTERVAL* box;
5330  SCIP_INTERVAL* intgrad;
5331  SCIP_INTERVAL intval;
5332  SCIP_VAR** vars;
5333  int nvars;
5334  int i;
5335 
5336  assert(scip != NULL);
5337  assert(cons != NULL);
5338  assert(x != NULL);
5339  assert(rowprep != NULL);
5340  assert(success != NULL);
5341 
5342  consdata = SCIPconsGetData(cons);
5343  assert(consdata != NULL);
5344  assert(exprtreeidx >= 0);
5345  assert(exprtreeidx < consdata->nexprtrees);
5346  assert(consdata->exprtrees != NULL);
5347 
5348  exprtree = consdata->exprtrees[exprtreeidx];
5349  assert(exprtree != NULL);
5350  assert(newx || SCIPexprtreeGetInterpreterData(exprtree) != NULL);
5351 
5352  *success = FALSE;
5353 
5354  /* skip interval gradient if expression interpreter cannot compute interval gradients */
5356  return SCIP_OKAY;
5357 
5358  nvars = SCIPexprtreeGetNVars(exprtree);
5359  vars = SCIPexprtreeGetVars(exprtree);
5360 
5361  box = NULL;
5362  intgrad = NULL;
5363  coefs = NULL;
5364 
5365  SCIP_CALL( SCIPallocBufferArray(scip, &box, nvars) );
5366 
5367  /* move reference point to bounds, setup box */
5368  for( i = 0; i < nvars; ++i )
5369  {
5370  lb = SCIPvarGetLbLocal(vars[i]);
5371  ub = SCIPvarGetUbLocal(vars[i]);
5372  if( SCIPisInfinity(scip, -lb) )
5373  {
5374  if( SCIPisInfinity(scip, ub) )
5375  {
5376  SCIPdebugMsg(scip, "skip interval gradient estimator for constraint <%s> because variable <%s> is still unbounded.\n", SCIPconsGetName(cons), SCIPvarGetName(vars[i]));
5377  goto INTGRADESTIMATOR_CLEANUP;
5378  }
5379  x[i] = ub;
5380  }
5381  else
5382  {
5383  if( SCIPisInfinity(scip, ub) )
5384  x[i] = lb;
5385  else
5386  x[i] = (2.0*x[i] < lb+ub) ? lb : ub;
5387  }
5388  SCIPintervalSetBounds(&box[i],
5389  -infty2infty(SCIPinfinity(scip), INTERVALINFTY, -MIN(lb, ub)),
5390  +infty2infty(SCIPinfinity(scip), INTERVALINFTY, MAX(lb, ub)));
5391  }
5392 
5393  /* compile expression if evaluated the first time; can only happen if newx is FALSE */
5394  if( newx && SCIPexprtreeGetInterpreterData(exprtree) == NULL )
5395  {
5396  SCIP_CALL( SCIPexprintCompile(exprint, exprtree) );
5397  }
5398 
5399  /* evaluate in reference point */
5400  SCIP_CALL( SCIPexprintEval(exprint, exprtree, x, &val) );
5401  if( !SCIPisFinite(val) )
5402  {
5403  SCIPdebugMsg(scip, "Got nonfinite function value from evaluation of constraint %s tree %d. skipping interval gradient estimator.\n", SCIPconsGetName(cons), exprtreeidx);
5404  goto INTGRADESTIMATOR_CLEANUP;
5405  }
5406 
5407  treecoef = consdata->nonlincoefs[exprtreeidx];
5408  val *= treecoef;
5409  constant = val;
5410 
5411  /* compute interval gradient */
5412  SCIP_CALL( SCIPallocBufferArray(scip, &intgrad, nvars) );
5413  SCIP_CALL( SCIPexprintGradInt(exprint, exprtree, INTERVALINFTY, box, TRUE, &intval, intgrad) );
5414  SCIPintervalMulScalar(INTERVALINFTY, &intval, intval, treecoef);
5415 
5416  /* printf("nvars %d side %d xref = %g x = [%g,%g] intval = [%g,%g] intgrad = [%g,%g]\n", nvars, side, x[0],
5417  box[0].inf, box[0].sup, intval.inf, intval.sup, intgrad[0].inf, intgrad[0].sup); */
5418 
5419  /* compute coefficients and constant */
5420  SCIP_CALL( SCIPallocBufferArray(scip, &coefs, nvars) );
5421  for( i = 0; i < nvars; ++i )
5422  {
5423  val = x[i];
5424  lb = SCIPintervalGetInf(box[i]);
5425  ub = SCIPintervalGetSup(box[i]);
5426 
5427  SCIPintervalMulScalar(INTERVALINFTY, &intgrad[i], intgrad[i], treecoef);
5428 
5429  if( SCIPisEQ(scip, lb, ub) )
5430  coefs[i] = 0.0;
5431  else if( (overestimate && val == ub) || /*lint !e777*/
5432  (!overestimate && val == lb) ) /*lint !e777*/
5433  coefs[i] = SCIPintervalGetInf(intgrad[i]);
5434  else
5435  coefs[i] = SCIPintervalGetSup(intgrad[i]);
5436 
5437  if( SCIPisZero(scip, coefs[i]) )
5438  continue;
5439 
5440  if( SCIPisInfinity(scip, -coefs[i]) || SCIPisInfinity(scip, coefs[i]) )
5441  {
5442  SCIPdebugMsg(scip, "skip intgrad estimator because of infinite interval bound\n");
5443  goto INTGRADESTIMATOR_CLEANUP;
5444  }
5445 
5446  constant -= coefs[i] * val;
5447  }
5448 
5449  /* add interval gradient estimator to row */
5450  SCIPaddRowprepConstant(rowprep, constant);
5451  SCIP_CALL( SCIPaddRowprepTerms(scip, rowprep, nvars, vars, coefs) );
5452 
5453  INTGRADESTIMATOR_CLEANUP:
5454  SCIPfreeBufferArrayNull(scip, &coefs);
5455  SCIPfreeBufferArrayNull(scip, &intgrad);
5456  SCIPfreeBufferArrayNull(scip, &box);
5457 
5458  return SCIP_OKAY;
5459 }
5460 
5461 /** generates a cut based on linearization (if convex), secant (if concave), or intervalgradient (if indefinite)
5462  */
5463 static
5465  SCIP* scip, /**< SCIP data structure */
5466  SCIP_EXPRINT* exprint, /**< expression interpreter */
5467  SCIP_CONS* cons, /**< constraint */
5468  SCIP_Real** ref, /**< reference point for each exprtree, or NULL if sol should be used */
5469  SCIP_SOL* sol, /**< reference solution where cut should be generated, or NULL if LP solution should be used */
5470  SCIP_Bool newsol, /**< whether the last evaluation of the expression with the expression interpreter was not at sol */
5471  SCIP_SIDETYPE side, /**< for which side a cut should be generated */
5472  SCIP_ROW** row, /**< storage for cut */
5473  SCIP_Real minviol, /**< minimal absolute violation we try to achieve */
5474  SCIP_Real maxrange, /**< maximal range allowed */
5475  SCIP_Bool assumeconvex /**< whether to assume convexity in inequalities */
5476  )
5477 {
5478  SCIP_ROWPREP* rowprep;
5479  SCIP_CONSDATA* consdata;
5480  SCIP_Bool success;
5481  SCIP_Real* x;
5482  int i;
5483 
5484  assert(scip != NULL);
5485  assert(cons != NULL);
5486  assert(row != NULL);
5487 
5488  SCIPdebugMsg(scip, "constructing cut for %s hand side of constraint <%s>\n", side == SCIP_SIDETYPE_LEFT ? "left" : "right", SCIPconsGetName(cons));
5489 
5490  SCIP_CALL( checkCurvature(scip, cons, assumeconvex) );
5491 
5492  consdata = SCIPconsGetData(cons);
5493  assert(consdata != NULL);
5494 
5495  if( consdata->nexprtrees == 0 )
5496  {
5497  char rowname[SCIP_MAXSTRLEN];
5498  (void) SCIPsnprintf(rowname, SCIP_MAXSTRLEN, "%s_%u", SCIPconsGetName(cons), ++(consdata->ncuts));
5499 
5500  /* if we are actually linear, add the constraint as row to the LP */
5501  SCIP_CALL( SCIPcreateEmptyRowCons(scip, row, cons, rowname, consdata->lhs, consdata->rhs, SCIPconsIsLocal(cons), FALSE , TRUE) );
5502  SCIP_CALL( SCIPaddVarsToRow(scip, *row, consdata->nlinvars, consdata->linvars, consdata->lincoefs) );
5503  return SCIP_OKAY;
5504  }
5505 
5506  SCIP_CALL( SCIPcreateRowprep(scip, &rowprep, side,
5507  !(side == SCIP_SIDETYPE_LEFT && (consdata->curvature & SCIP_EXPRCURV_CONCAVE)) &&
5508  !(side == SCIP_SIDETYPE_RIGHT && (consdata->curvature & SCIP_EXPRCURV_CONVEX ))) ); /*lint --e{845}*/
5509  SCIPaddRowprepSide(rowprep, side == SCIP_SIDETYPE_LEFT ? consdata->lhs : consdata->rhs);
5510  (void) SCIPsnprintf(rowprep->name, SCIP_MAXSTRLEN, "%s_%u", SCIPconsGetName(cons), ++(consdata->ncuts));
5511 
5512  if( ref == NULL )
5513  {
5514  SCIP_CALL( SCIPallocBufferArray(scip, &x, SCIPexprtreeGetNVars(consdata->exprtrees[0])) );
5515  }
5516 
5517  success = TRUE;
5518  for( i = 0; i < consdata->nexprtrees; ++i )
5519  {
5520  if( ref == NULL )
5521  {
5522  SCIP_CALL( SCIPreallocBufferArray(scip, &x, SCIPexprtreeGetNVars(consdata->exprtrees[i])) ); /*lint !e644*/
5523  SCIP_CALL( SCIPgetSolVals(scip, sol, SCIPexprtreeGetNVars(consdata->exprtrees[i]), SCIPexprtreeGetVars(consdata->exprtrees[i]), x) );
5524  }
5525  else
5526  {
5527  x = ref[i];
5528  }
5529 
5530  if( (side == SCIP_SIDETYPE_LEFT && (consdata->curvatures[i] &