expression.cpp
Go to the documentation of this file.
1 /* $Id: expression.cpp 1081 2014-10-30 20:07:03Z pbelotti $
2  *
3  * Name: expression.cpp
4  * Author: Pietro Belotti
5  * Purpose: methods of the expression class
6  *
7  * (C) Carnegie-Mellon University, 2006-09.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 #include <iostream>
12 
13 #include "CouenneCutGenerator.hpp"
14 #include "CouenneProblem.hpp"
15 
16 #include "CouenneTypes.hpp"
17 #include "CouenneExpression.hpp"
18 #include "CouenneExprClone.hpp"
19 #include "CouenneExprAux.hpp"
20 #include "CouenneExprOp.hpp"
21 #include "CouenneExprUnary.hpp"
22 #include "CouenneExprStore.hpp"
23 
24 using namespace Couenne;
25 
26 // empty differentiation method
28 {return new exprConst (0.);}
29 
30 
31 // Get lower and upper bound of a generic expression
33 
34  lb = new exprConst (- COIN_DBL_MAX);
35  ub = new exprConst ( COIN_DBL_MAX);
36 }
37 
38 
41 
42  expression *le, *ue;
43  getBounds (le, ue);
44 
45  lb = (*le) ();
46  ub = (*ue) ();
47 
48  delete le;
49  delete ue;
50 }
51 
52 
53 // generate one cut for a constant
54 void exprConst::generateCuts (expression *w, //const OsiSolverInterface &si,
55  OsiCuts &cs, const CouenneCutGenerator *cg,
56  t_chg_bounds *chg, int,
58  if (cg -> isFirst ())
59  cg -> createCut (cs, value_, 0, w -> Index (), 1.);
60 }
61 
62 
65 
66  register int c0 = code (),
67  c1 = e1. code ();
68 
69  if (c0 < c1) return -1;
70  else if (c0 > c1) return 1;
71 
72  // same code, check arguments
73 
74  if (c0 >= COU_EXPRUNARY) { // both are exprUnary's. COU_EXPRUNARY > COU_EXPROP, so if this is run then the next is not
75 
76  exprUnary *ne0 = dynamic_cast <exprUnary *> (const_cast <expression *> (this->Original()));
77  exprUnary *ne1 = dynamic_cast <exprUnary *> (const_cast <expression *> (e1.Original()));
78 
79  return ne0 -> compare (*ne1);
80  }
81 
82  if (c0 >= COU_EXPROP) { // both are exprOp's
83 
84  exprOp *ne0 = dynamic_cast <exprOp *> (const_cast <expression *> (this->Original()));
85  exprOp *ne1 = dynamic_cast <exprOp *> (const_cast <expression *> (e1.Original()));
86 
87  return ne0 -> compare (*ne1);
88  }
89 
90  // expressions are both variables or constants
91 
92  {
93  register int
94  i0 = Index (),
95  i1 = e1. Index ();
96 
97  if (i0 < i1) return -1;
98  if (i0 > i1) return 1;
99  if (i0 >= 0) return 0; // same variables
100  }
101 
102  // both are numbers
103  {
104  register CouNumber
105  v0 = Value (),
106  v1 = e1. Value ();
107 
108  if (v0 < v1) return -1;
109  if (v0 > v1) return 1;
110  }
111 
112  return 0;
113 }
114 
115 
118 
119  //expression *copy = getOriginal (e.copy_);
120 
121  //if ((copy -> Type () == VAR) ||
122  // (copy -> Type () == AUX))
123 
124  // if this is a variable, don't copy, just refer to the same
125  // object (otherwise many useless copies are created)
126 
127  // copy_ = copy;// -> clone (d);
128  //else copy_ = copy -> clone (d);
129 
130  copy_ = e.copy_ -> Original () -> clone (d); // !!! REMOVE " -> Original ()"
131  //else copy_ = e.copy_ -> clone (d);
132 
133  value_ = e.value_;
134 }
135 
136 
139 {return compare (const_cast <expression &> (*(c. Original ())));}
140 
141 
143 void exprCopy::replace (exprVar *orig, exprVar *aux) {
144 
145  if (!aux)
146  aux = orig;
147 
148  enum nodeType copyType = copy_ -> Type ();
149 
150  if ((copyType == VAR) ||
151  (copyType == AUX)) {
152 
153  if (copy_ -> Index () == orig -> Index ()) {
154  if (copy_ -> isaCopy ()) // !!! REMOVE
155  delete copy_; // !!! REMOVE
156  copy_ = aux;
157  }
158 
159  } else copy_ -> replace (orig, aux);
160 
161  //copy_ -> replace (orig, aux);
162 
163  /*if ((copy_ -> Index () == orig -> Index ()) && (copy_ -> Type () != AUX)) {
164  delete copy_;
165  copy_ = new exprClone (aux);
166  }*/
167 }
168 
169 
172 int expression::dependsOn (int *ind, int n, enum dig_type type) {
173 
174  std::set <int>
175  indlist (ind, ind + n),
176  deplist;
177  //intersectn;
178 
179  /*printf (":::::: indlist = {");
180  for (std::set <int>::iterator i=indlist.begin (); i != indlist.end(); ++i)
181  printf ("%d ", *i);
182  printf ("} -- deplist = {");*/
183 
184  DepList (deplist, type);
185 
186  /*for (std::set <int>::iterator i=deplist.begin (); i != deplist.end(); ++i)
187  printf ("%d ", *i);
188  printf ("} -- intersection: \n");*/
189 
190  for (std::set <int>::iterator
191  i = deplist.begin (),
192  j = indlist.begin ();
193  (i != deplist.end ()) &&
194  (j != indlist.end ());) {
195 
196  if (*i == *j) return 1;
197 
198  if (*i > *j) ++j;
199  else ++i;
200  }
201 
202  return 0;
203 }
204 
205 
208 
209  if (((copy_ -> Type () == VAR) ||
210  (copy_ -> Type () == AUX)) &&
211  (copy_ -> Original () != p -> Var (copy_ -> Index ()))) {
212 
213  /*printf ("exprCopy::realign replaces %x with %x (",
214  copy_ -> Original (), p -> Var (copy_ -> Index ()));
215  copy_ -> Original () -> print (); printf (" --> ");
216  p -> Var (copy_ -> Index ()) -> print (); printf (")\n");*/
217 
218  expression *trash = copy_;
219 
220  copy_ = p -> Var (copy_ -> Index ());
221  //delete trash; // Otherwise ticket 13
222  } else copy_ -> realign (p);
223 }
224 
226 void exprCopy::print (std::ostream &out, bool descend) const
227 {copy_ -> Original () -> print (out, descend);}
228 //{out << "["; copy_ -> print (out, descend); out << "]<" << copy_ << ">"; } // Must go
229 
230 
232 void exprClone::print (std::ostream &out, bool descend) const
233 {copy_ -> Original () -> print (out, descend);}
234 //{out << "{"; copy_ -> print (out, descend); out << "}<" << copy_ << ">"; } // Must go
235 
236 
238 void exprStore::print (std::ostream &out, bool descend) const
239 {copy_ -> Original () -> print (out, descend);}
240 //{out << "<"; copy_ -> print (out, descend); out << "><" << copy_ << ">"; }
241 
242 
243 // /// redirect variables to proper variable vector
244 // void exprClone::realign (const CouenneProblem *p) {
245 
246 // if (((copy_ -> Type () == VAR) ||
247 // (copy_ -> Type () == AUX)) &&
248 // (copy_ -> Original () != p -> Var (copy_ -> Index ()))) {
249 
250 // expression *trash = copy_;
251 
252 // copy_ = p -> Var (copy_ -> Index ());
253 // printf ("deleting "); trash -> print (); printf ("\n");
254 // delete trash;
255 // } else {printf ("not deleted "); print (); printf ("\n");}
256 // }
257 
258 
261  CouNumber& left, CouNumber& right) const
262 {
263  assert(isBijective());
264  CouNumber inv = inverse(vardep);
265  CouNumber curr = (*varind) ();
266  if (curr > inv) {
267  left = inv;
268  right = curr;
269  }
270  else {
271  left = curr;
272  right = inv;
273  }
274 }
Cut Generator for linear convexifications.
void replace(exprVar *, exprVar *)
replace occurrence of a variable with another variable
Definition: expression.cpp:143
virtual int dependsOn(int *ind, int n, enum dig_type type=STOP_AT_AUX)
dependence on variable set: return cardinality of subset of the set of indices in first argument whic...
Definition: expression.cpp:172
CouNumber value_
saved value to be used by exprStore expressions
virtual int compare(expression &)
compare expressions
Definition: expression.cpp:64
const expression * Original() const
If this is an exprClone of a exprClone of an expr???, point to the original expr??? instead of an exprClone – improves computing efficiency.
virtual void closestFeasible(expression *varind, expression *vardep, CouNumber &left, CouNumber &right) const
closest feasible points in function in both directions
Definition: expression.cpp:260
status of lower/upper bound of a variable, to be checked/modified in bound tightening ...
virtual expression * clone(Domain *d=NULL) const
Cloning method.
virtual void print(std::ostream &out=std::cout, bool descend=false) const
Printing.
Definition: expression.cpp:238
virtual bool isBijective() const
indicating if function is monotonically increasing
virtual void print(std::ostream &out=std::cout, bool descend=false) const
I/O.
Definition: expression.cpp:226
virtual expression * differentiate(int)
differentiation
Definition: expression.cpp:27
void generateCuts(expression *, OsiCuts &, const CouenneCutGenerator *, t_chg_bounds *=NULL, int=-1, CouNumber=-COUENNE_INFINITY, CouNumber=COUENNE_INFINITY)
generate convexification cut for constraint w = this
Definition: expression.cpp:54
exprCopy(expression *copy)
Empty constructor - used in cloning method of exprClone.
bool isaCopy() const
return true if this is a copy of something, i.e.
expression class for unary functions (sin, log, etc.)
CouNumber inv(register CouNumber arg)
the operator itself
constant-type operator
Long e1
Definition: OSdtoa.cpp:1815
static char * j
Definition: OSdtoa.cpp:3622
void fint fint fint real fint real real real real real real real real real * e
int Index() const
Get variable index in problem.
virtual CouNumber inverse(expression *vardep) const
compute the inverse function
virtual int Index() const
Return index of variable (only valid for exprVar and exprAux)
virtual void print(std::ostream &out=std::cout, bool descend=false) const
Printing.
Definition: expression.cpp:232
virtual enum expr_type code()
return integer for comparing expressions (used to recognize common expression)
Class for MINLP problems with symbolic information.
CouNumber value_
the value of this constant
expression * copy_
the expression this object is a (reference) copy of
double CouNumber
main number type in Couenne
general n-ary operator-type expression: requires argument list.
virtual void getBounds(expression *&, expression *&)
Get lower and upper bound of an expression (if any)
Definition: expression.cpp:32
nodeType
type of a node in an expression tree
enum nodeType Type() const
node type
virtual int DepList(std::set< int > &deplist, enum dig_type type=ORIG_ONLY)
fill std::set with indices of variables on which this expression depends.
dig_type
type of digging when filling the dependence list
variable-type operator
void realign(const CouenneProblem *p)
redirect variables to proper variable vector
Definition: expression.cpp:207
Expression base class.
void fint fint fint real fint real real real real real real real real * w
void fint * n
virtual CouNumber Value() const
value (empty)
real c
virtual const expression * Original() const
If this is an exprClone of a exprClone of an expr???, point to the original expr??? instead of an exprClone – improve computing efficiency.
Define a dynamic point+bounds, with a way to save and restore previous points+bounds through a LIFO s...