1 | /* $Id: CouenneCutGenerator.hpp 946 2013-04-15 22:20:38Z stefan $ |
---|
2 | * |
---|
3 | * Name: CouenneCutGenerator.hpp |
---|
4 | * Author: Pietro Belotti |
---|
5 | * Purpose: a convexification cut generator for MINLP problems |
---|
6 | * |
---|
7 | * (C) Carnegie-Mellon University, 2006-09. |
---|
8 | * This file is licensed under the Eclipse Public License (EPL) |
---|
9 | */ |
---|
10 | |
---|
11 | #ifndef COUENNE_CUT_GENERATOR_HPP |
---|
12 | #define COUENNE_CUT_GENERATOR_HPP |
---|
13 | |
---|
14 | //#include "BonRegisteredOptions.hpp" |
---|
15 | |
---|
16 | #include "BonOaDecBase.hpp" |
---|
17 | #include "CglConfig.h" |
---|
18 | #include "CglCutGenerator.hpp" |
---|
19 | #include "OsiRowCut.hpp" |
---|
20 | #include "BonAuxInfos.hpp" |
---|
21 | #include "BonBabInfos.hpp" |
---|
22 | #include "OsiSolverInterface.hpp" |
---|
23 | #include "CouenneConfig.h" |
---|
24 | #include "CouenneJournalist.hpp" |
---|
25 | #include "CouenneTypes.hpp" |
---|
26 | |
---|
27 | namespace Ipopt { |
---|
28 | template <class T> class SmartPtr; |
---|
29 | class OptionsList; |
---|
30 | class Journalist; |
---|
31 | } |
---|
32 | |
---|
33 | namespace Bonmin { |
---|
34 | class RegisteredOptions; |
---|
35 | class BabInfo; |
---|
36 | class OsiTMINLPInterface; |
---|
37 | class BabSetupBase; |
---|
38 | } |
---|
39 | |
---|
40 | struct ASL; |
---|
41 | |
---|
42 | namespace Couenne { |
---|
43 | |
---|
44 | class CouenneProblem; |
---|
45 | class funtriplet; |
---|
46 | |
---|
47 | /// Cut Generator for linear convexifications |
---|
48 | |
---|
49 | class CouenneCutGenerator: public CglCutGenerator { |
---|
50 | |
---|
51 | protected: |
---|
52 | |
---|
53 | /// True if no convexification cuts have been generated yet for this |
---|
54 | /// problem |
---|
55 | mutable bool firstcall_; |
---|
56 | |
---|
57 | /// True if we should add the violated cuts only, false if all of |
---|
58 | /// them should be added |
---|
59 | mutable bool addviolated_; |
---|
60 | |
---|
61 | /// what kind of sampling should be performed? |
---|
62 | enum conv_type convtype_; |
---|
63 | |
---|
64 | /// how many cuts should be added for each function? |
---|
65 | int nSamples_; |
---|
66 | |
---|
67 | /// pointer to symbolic repr. of constraint, variables, and bounds |
---|
68 | CouenneProblem *problem_; |
---|
69 | |
---|
70 | /// number of cuts generated at the first call |
---|
71 | mutable int nrootcuts_; |
---|
72 | |
---|
73 | /// total number of cuts generated |
---|
74 | mutable int ntotalcuts_; |
---|
75 | |
---|
76 | /// separation time (includes generation of problem) |
---|
77 | mutable double septime_; |
---|
78 | |
---|
79 | /// Record obj value at final point of CouenneConv. |
---|
80 | mutable double objValue_; |
---|
81 | |
---|
82 | /// nonlinear solver interface as used within Bonmin (used at first |
---|
83 | /// Couenne pass of each b&b node |
---|
84 | Bonmin::OsiTMINLPInterface *nlp_; |
---|
85 | |
---|
86 | /// pointer to the Bab object (used to retrieve the current primal |
---|
87 | /// bound through bestObj()) |
---|
88 | Bonmin::Bab *BabPtr_; |
---|
89 | |
---|
90 | /// signal infeasibility of current node (found through bound tightening) |
---|
91 | mutable bool infeasNode_; |
---|
92 | |
---|
93 | /// SmartPointer to the Journalist |
---|
94 | JnlstPtr jnlst_; |
---|
95 | |
---|
96 | /// Time spent at the root node |
---|
97 | mutable double rootTime_; |
---|
98 | |
---|
99 | /// Check all generated LPs through an independent call to |
---|
100 | /// OsiClpSolverInterface::initialSolve() |
---|
101 | bool check_lp_; |
---|
102 | |
---|
103 | /// Take advantage of OsiClpSolverInterface::tightenBounds (), known |
---|
104 | /// to have caused some problems some time ago |
---|
105 | bool enable_lp_implied_bounds_; |
---|
106 | |
---|
107 | /// Running count of printed info lines |
---|
108 | mutable int lastPrintLine; |
---|
109 | |
---|
110 | public: |
---|
111 | |
---|
112 | /// constructor |
---|
113 | CouenneCutGenerator (Bonmin::OsiTMINLPInterface * = NULL, |
---|
114 | Bonmin::BabSetupBase *base = NULL, |
---|
115 | CouenneProblem * = NULL, |
---|
116 | struct ASL * = NULL); |
---|
117 | |
---|
118 | /// copy constructor |
---|
119 | CouenneCutGenerator (const CouenneCutGenerator &); |
---|
120 | |
---|
121 | /// destructor |
---|
122 | ~CouenneCutGenerator (); |
---|
123 | |
---|
124 | /// clone method (necessary for the abstract CglCutGenerator class) |
---|
125 | CouenneCutGenerator *clone () const |
---|
126 | {return new CouenneCutGenerator (*this);} |
---|
127 | |
---|
128 | /// return pointer to symbolic problem |
---|
129 | inline CouenneProblem *Problem () const |
---|
130 | {return problem_;} |
---|
131 | |
---|
132 | /// return pointer to symbolic problem |
---|
133 | inline void setProblem (CouenneProblem *p) |
---|
134 | {problem_ = p;} |
---|
135 | |
---|
136 | /// total number of variables (original + auxiliary) |
---|
137 | int getnvars () const; |
---|
138 | |
---|
139 | /// has generateCuts been called yet? |
---|
140 | inline bool isFirst () const |
---|
141 | {return firstcall_;} |
---|
142 | |
---|
143 | /// should we add the violated cuts only (true), or all of them (false)? |
---|
144 | inline bool addViolated () const |
---|
145 | {return addviolated_;} |
---|
146 | |
---|
147 | /// get convexification type (see CouenneTypes.h) |
---|
148 | inline enum conv_type ConvType () const |
---|
149 | {return convtype_;} |
---|
150 | |
---|
151 | /// get number of convexification samples |
---|
152 | inline int nSamples () const |
---|
153 | {return nSamples_;} |
---|
154 | |
---|
155 | /// the main CglCutGenerator |
---|
156 | void generateCuts (const OsiSolverInterface &, |
---|
157 | OsiCuts &, |
---|
158 | const CglTreeInfo = CglTreeInfo ()) |
---|
159 | #if CGL_VERSION_MAJOR == 0 && CGL_VERSION_MINOR <= 57 |
---|
160 | const |
---|
161 | #endif |
---|
162 | ; |
---|
163 | |
---|
164 | /// create cut and check violation. Insert and return status |
---|
165 | int createCut (OsiCuts &, // cutset to insert |
---|
166 | CouNumber, // lb |
---|
167 | CouNumber, // ub |
---|
168 | // index, coeff (index -1: "don't care") |
---|
169 | int, CouNumber, // of first term |
---|
170 | int=-1, CouNumber=0., // of second term |
---|
171 | int=-1, CouNumber=0., // of third term |
---|
172 | bool = false) const; // is it a global cut? No, by default |
---|
173 | |
---|
174 | /// create cut and check violation. Other version with only one bound |
---|
175 | int createCut (OsiCuts &, // cutset to insert |
---|
176 | CouNumber, // rhs |
---|
177 | int, // sign: -1: <=, 0: =, +1: >= |
---|
178 | // index, coeff (index -1: "don't care") |
---|
179 | int, CouNumber, // of first term |
---|
180 | int=-1, CouNumber=0., // of second term |
---|
181 | int=-1, CouNumber=0., // of third term |
---|
182 | bool = false) const; // is it a global cut? No, by default |
---|
183 | |
---|
184 | /// Add general linear envelope to convex function, given its |
---|
185 | /// variables' indices, the (univariate) function and its first |
---|
186 | /// derivative |
---|
187 | void addEnvelope (OsiCuts &, |
---|
188 | int, |
---|
189 | unary_function, unary_function, |
---|
190 | int, int, |
---|
191 | CouNumber, CouNumber, CouNumber, |
---|
192 | t_chg_bounds * = NULL, |
---|
193 | bool = false) const; |
---|
194 | |
---|
195 | /// Add general linear envelope to convex function, given its |
---|
196 | /// variables' indices, the (univariate) function and its first |
---|
197 | /// derivative |
---|
198 | void addEnvelope (OsiCuts &, |
---|
199 | int, |
---|
200 | funtriplet *, |
---|
201 | int, int, |
---|
202 | CouNumber, CouNumber, CouNumber, |
---|
203 | t_chg_bounds * = NULL, |
---|
204 | bool = false) const; |
---|
205 | |
---|
206 | /// Add half-plane through (x1,y1) and (x2,y2) -- resp. 4th, 5th, |
---|
207 | /// 6th, and 7th argument |
---|
208 | int addSegment (OsiCuts &, int, int, |
---|
209 | CouNumber, CouNumber, |
---|
210 | CouNumber, CouNumber, int) const; |
---|
211 | |
---|
212 | /// add tangent at given poing (x,w) with given slope |
---|
213 | int addTangent (OsiCuts &, int, int, |
---|
214 | CouNumber, CouNumber, |
---|
215 | CouNumber, int) const; |
---|
216 | |
---|
217 | /// Method to set the Bab pointer |
---|
218 | void setBabPtr (Bonmin::Bab *p) |
---|
219 | {BabPtr_ = p;} |
---|
220 | |
---|
221 | /// Get statistics |
---|
222 | void getStats (int &nrc, int &ntc, double &st) { |
---|
223 | nrc = nrootcuts_; |
---|
224 | ntc = ntotalcuts_; |
---|
225 | st = septime_; |
---|
226 | } |
---|
227 | |
---|
228 | /// Allow to get and set the infeasNode_ flag (used only in generateCuts()) |
---|
229 | bool &infeasNode () const |
---|
230 | {return infeasNode_;} |
---|
231 | |
---|
232 | /// generate OsiRowCuts for current convexification |
---|
233 | void genRowCuts (const OsiSolverInterface &, OsiCuts &cs, |
---|
234 | int, int *, t_chg_bounds * = NULL) const; |
---|
235 | |
---|
236 | /// generate OsiColCuts for improved (implied and propagated) bounds |
---|
237 | void genColCuts (const OsiSolverInterface &, OsiCuts &, int, int *) const; |
---|
238 | |
---|
239 | /// Add list of options to be read from file |
---|
240 | static void registerOptions (Ipopt::SmartPtr <Bonmin::RegisteredOptions> roptions); |
---|
241 | |
---|
242 | /// print node, depth, LB/UB/LP info |
---|
243 | void printLineInfo() const; |
---|
244 | |
---|
245 | /// Provide Journalist |
---|
246 | inline ConstJnlstPtr Jnlst() const |
---|
247 | {return ConstPtr (jnlst_);} |
---|
248 | |
---|
249 | void setJnlst (JnlstPtr jnlst__) |
---|
250 | { jnlst_ = jnlst__; } |
---|
251 | |
---|
252 | /// Time spent at root node |
---|
253 | double &rootTime () |
---|
254 | {return rootTime_;} |
---|
255 | |
---|
256 | /// return check_lp flag (used in CouenneSolverInterface) |
---|
257 | bool check_lp () const |
---|
258 | {return check_lp_;} |
---|
259 | |
---|
260 | /// returns value of enable_lp_implied_bounds_ |
---|
261 | bool enableLpImpliedBounds () const |
---|
262 | {return enable_lp_implied_bounds_;} |
---|
263 | }; |
---|
264 | |
---|
265 | |
---|
266 | /// translate sparse to dense vector (should be replaced) |
---|
267 | void sparse2dense (int ncols, t_chg_bounds *chg_bds, int *&changed, int &nchanged); |
---|
268 | |
---|
269 | } |
---|
270 | |
---|
271 | #endif |
---|