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