source: trunk/Couenne/src/expression/CouenneExprAux.hpp @ 968

Last change on this file since 968 was 968, checked in by pbelotti, 7 years ago

fixed reformulation bug for auxs with different sign but equal rhs (ouch)

  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/* $Id: CouenneExprAux.hpp 968 2013-06-24 21:42:26Z pbelotti $
2 *
3 * Name:    exprAux.hpp
4 * Author:  Pietro Belotti
5 * Purpose: definition of the auxiliary variable class (used in
6 *          standardization and convexification)
7 *
8 * (C) Carnegie-Mellon University, 2006-10.
9 * This file is licensed under the Eclipse Public License (EPL)
10 */
11
12#ifndef COUENNE_EXPRAUX_HPP
13#define COUENNE_EXPRAUX_HPP
14
15#include <iostream>
16
17#include "CouenneExprVar.hpp"
18
19namespace Couenne {
20
21class CouenneCutGenerator;
22
23/** Auxiliary variable
24 *
25 *  It is associated with an expression which depends, in general, on
26 *  original and/or other auxiliary variables. It is used for AMPL's
27 *  defined variables (aka common expressions) and to reformulate
28 *  nonlinear constraints/objectives.
29 */
30
31class exprAux: public exprVar {
32
33 public:
34
35  /// integrality type of an auxiliary variable: unset, continuous, integer
36  enum intType {Unset=-1, Continuous, Integer};
37
38 protected:
39
40  /// The expression associated with this auxiliary variable
41  expression *image_;
42
43  /// lower bound, a function of the associated expression and the
44  /// bounds on the variables in the expression
45  expression *lb_;
46
47  /// upper bound, a function of the associated expression and the
48  /// bounds on the variables in the expression
49  expression *ub_;
50
51  /// used in rank-based branching variable choice: original variables
52  /// have rank 1; auxiliary w=f(x) has rank r(w) = r(x)+1; finally,
53  /// auxiliary w=f(x1,x2...,xk) has rank r(w) = 1+max{r(xi):i=1..k}.
54  int rank_;
55
56  /// number of appearances of this aux in the formulation. The more
57  /// times it occurs in the formulation, the more implication its
58  /// branching has on other variables
59  int multiplicity_;
60
61  /// is this variable integer?
62  enum intType integer_;
63
64  /// True if this variable replaces the lhs of a constraint, i.e., if
65  /// it is a top level variable in the DAG of the problem
66  bool top_level_;
67
68  /// "sign" of the defining constraint
69  enum auxSign sign_;
70
71 public:
72
73  /// Node type
74  inline enum nodeType Type () const
75  {return AUX;}
76
77  /// Constructor
78  exprAux (expression *, int, int, intType = Unset, Domain * = NULL, enum auxSign = expression::AUX_EQ);
79
80  /// Constructor to be used with standardize ([...], false)
81  exprAux (expression *, Domain * = NULL, enum auxSign = expression::AUX_EQ);
82
83  /// Destructor
84  virtual ~exprAux ();
85
86  /// Copy constructor
87  exprAux (const exprAux &, Domain *d = NULL);
88
89  /// Cloning method
90  virtual inline exprVar *clone (Domain *d = NULL) const
91  {return new exprAux (*this, d);}
92
93  inline expression *Lb () {return lb_;} ///< get lower bound expression
94  inline expression *Ub () {return ub_;} ///< get upper bound expression
95
96  /// Print expression
97  virtual void print (std::ostream & = std::cout, 
98                      bool = false) const;
99
100  /// The expression associated with this auxiliary variable
101  inline expression *Image () const
102  {return image_;}
103
104  /// Sets expression associated with this auxiliary variable
105  inline void Image (expression *image)
106  {image_ = image;}
107
108  /// Null function for evaluating the expression
109  inline CouNumber operator () ()
110  {return domain_ -> x (varIndex_);}
111
112  /// fill in the set with all indices of variables appearing in the
113  /// expression
114  int DepList (std::set <int> &deplist, 
115               enum dig_type type = ORIG_ONLY);
116
117  /// simplify
118  expression *simplify ();
119
120  /// Get a measure of "how linear" the expression is (see CouenneTypes.h)
121  inline int Linearity ()
122  {return LINEAR;}
123    /*return image_ -> Linearity ();*/
124
125  /// Get lower and upper bound of an expression (if any)
126  //virtual void getBounds (expression *&lb, expression *&ub);
127
128  /// set bounds depending on both branching rules and propagated
129  /// bounds. To be used after standardization
130  void crossBounds ();
131
132  /// generate cuts for expression associated with this auxiliary
133  void generateCuts (//const OsiSolverInterface &,
134                     OsiCuts &, const CouenneCutGenerator *, 
135                     t_chg_bounds * = NULL, int = -1, 
136                     CouNumber = -COUENNE_INFINITY, 
137                     CouNumber =  COUENNE_INFINITY);
138
139  /// used in rank-based branching variable choice
140  virtual inline int rank ()
141    {return rank_;} 
142
143  /// is this expression defined as integer?
144  virtual inline bool isDefinedInteger () {
145
146    return ((integer_ == Integer) || 
147            ((integer_ == Unset) && 
148            ((integer_ = (image_ -> isInteger ()) ? 
149              Integer : Continuous) == Integer)));
150  }
151
152  /// is this expression integer?
153  virtual inline bool isInteger () {
154
155    if (isDefinedInteger ()) 
156      return true;
157
158    register CouNumber l = lb ();
159    return ((l == ub ()) && (COUENNE_round (l) == l));
160    //CouNumber l = (*(Lb ())) ();
161    //return (::isInteger (l) && (fabs (l - (*(Ub ())) ()) < COUENNE_EPS));
162  }
163
164  /// Set this variable as integer
165  virtual inline void setInteger (bool value)
166  {integer_ = value ? Integer : Continuous;}
167
168  /// Tell this variable appears once more
169  inline void increaseMult () {++multiplicity_;}
170
171  /// Tell this variable appears once less (standardized within
172  /// exprSum, for instance)
173  inline void decreaseMult () {--multiplicity_;}
174
175  /// Disable this auxiliary variable
176  inline void zeroMult () {multiplicity_ = 0;}
177
178  /// How many times this variable appears
179  inline int Multiplicity () {return multiplicity_;}
180
181  /// link this variable to a domain
182  inline void linkDomain (Domain *d) {
183    domain_ = d; 
184    if (lb_) lb_ -> linkDomain (d);
185    if (ub_) ub_ -> linkDomain (d);
186  }
187
188  /// return top_level_
189  bool &top_level () 
190  {return top_level_;}
191
192  /// return proper object to handle expression associated with this
193  /// variable (NULL if this is not an auxiliary)
194  CouenneObject *properObject (CouenneCutGenerator *c,
195                              CouenneProblem *p, 
196                              Bonmin::BabSetupBase *base, 
197                              JnlstPtr jnlst);
198
199  /// return its sign in the definition constraint
200  virtual inline enum auxSign sign () const 
201  {return sign_;}
202};
203
204
205/** Structure for comparing expressions
206 *
207 *  Used in compare() method for same-class expressions
208 */
209
210struct compExpr {
211  inline bool operator () (exprAux* e0, exprAux* e1) const
212  {return ((e0 -> sign () < e1 -> sign ()) || (e0 -> Image () -> compare (*(e1 -> Image ())) < 0));}
213};
214
215
216/// allow to draw function within intervals and cuts introduced
217void draw_cuts (OsiCuts &, const CouenneCutGenerator *, 
218                int, expression *, expression *);
219
220}
221
222#endif
Note: See TracBrowser for help on using the repository browser.