exprUnary.cpp
Go to the documentation of this file.
1 /* $Id: exprUnary.cpp 1157 2015-05-05 21:24:02Z stefan $
2  *
3  * Name: exprUnary.cpp
4  * Author: Pietro Belotti
5  * Purpose: methods of the unary expression class
6  *
7  * (C) Carnegie-Mellon University, 2006-2009.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 #include "CouenneProblem.hpp"
12 #include "CouenneTypes.hpp"
13 #include "CouenneExprUnary.hpp"
14 #include "CouenneExprVar.hpp"
15 #include "CouenneExprAux.hpp"
16 #include "CouenneExprClone.hpp"
17 
18 using namespace Couenne;
19 
20 // print unary expression
21 void exprUnary::print (std::ostream &out,
22  bool descend) const {
23 
24  if (printPos () == PRE) out << printOp ();
25  out << "(";
26  argument_ -> print (out, descend);
27  out << ")";
28  if (printPos () == POST) out << printOp ();
29 }
30 
31 
34 
35  int c0 = code (),
36  c1 = e1. code ();
37 
38  if (c0 < c1) return -1;
39  else if (c0 > c1) return 1;
40  else // have to compare arguments
41  return argument_ -> compare (*(e1.argument_));
42 }
43 
44 
45 // Create standard formulation of this expression, by:
46 //
47 // - creating auxiliary w variables and corresponding expressions
48 // - returning linear counterpart as new constraint (to replace
49 // current one)
51 
52  exprAux *subst;
53 
54  if ((subst = argument_ -> standardize (p))) {
55 
56  if ((subst -> Type () == AUX) ||
57  (subst -> Type () == VAR))
58  argument_ = new exprClone (subst);
59  else argument_ = subst;
60  }
61 
62  return (addAux ? (p -> addAuxiliary (this)) : new exprAux (this, p -> domain ()));
63 }
64 
65 
68 
69  if (argument_ -> Type () == VAR) {
70  if (argument_ -> Index () == x -> Index ()) {
71  delete argument_;
72  argument_ = new exprClone (w);
73  }
74  } else argument_ -> replace (x, w);
75 }
76 
77 
80 
81  // only check if argument is, *at this point in the algorithm*,
82  // constant -- due to branching rules, for instance. If so, check if
83  // the corresponding evaluated expression is integer.
84 
85  CouNumber al, au;
86  argument_ -> getBounds (al, au);
87 
88  if ((al > -COUENNE_INFINITY) && // Funny: if al=-(au=-1.7e308) returns true...
89  (au < COUENNE_INFINITY) &&
90  fabs (al - au) < COUENNE_EPS) { // argument is constant
91 
92  register CouNumber fval = (F ()) (al);
93 
94  // check if f(lb=ub) is integer
95  if (fabs (COUENNE_round (fval) - fval) < COUENNE_EPS)
96  return true;
97  }
98 
99  return false;
100 }
101 
102 
103 // simplify unary operators
105 
106  register expression *subst;
107 
108  // Simplify argument g(x) of this expression f(g(x))
109  if ((subst = argument_ -> simplify ())) {
110 
111  delete argument_;
112  argument_ = subst;
113 
114  // g(x) is a constant k, therefore return f (k)
115  if (subst -> Type () == CONST) {
116 
117  expression *ret = new exprConst (operator () ());
118  argument_ = NULL;
119  delete subst;
120 
121  return ret;
122  }
123  }
124 
125  return NULL;
126 }
#define COUENNE_round(x)
virtual void print(std::ostream &out=std::cout, bool=false) const
print this expression to iostream
Definition: exprUnary.cpp:21
virtual enum expr_type code()
type of operator
virtual bool isInteger()
is this expression integer?
Definition: exprUnary.cpp:79
expression * simplify()
simplification
Definition: exprUnary.cpp:104
virtual int compare(exprUnary &)
compare two unary functions
Definition: exprUnary.cpp:33
virtual exprAux * standardize(CouenneProblem *, bool addAux=true)
reduce expression in standard form, creating additional aux variables (and constraints) ...
Definition: exprUnary.cpp:50
expression * argument_
single argument taken by this expression
virtual unary_function F()
the operator itself (e.g. sin, log...)
expression class for unary functions (sin, log, etc.)
constant-type operator
Long e1
Definition: OSdtoa.cpp:1815
virtual int Index() const
Return index of variable (only valid for exprVar and exprAux)
virtual void replace(exprVar *, exprVar *)
replace variable with other
Definition: exprUnary.cpp:67
virtual enum pos printPos() const
print position (PRE, INSIDE, POST)
Class for MINLP problems with symbolic information.
expression clone (points to another expression)
#define COUENNE_EPS
virtual std::string printOp() const
print operator
double CouNumber
main number type in Couenne
virtual void getBounds(expression *&, expression *&)
Get lower and upper bound of an expression (if any)
Definition: expression.cpp:32
#define COUENNE_INFINITY
Auxiliary variable.
variable-type operator
Expression base class.
void fint fint fint real fint real real real real real real real real * w
virtual enum nodeType Type() const
node type
void fint fint fint real fint real * x