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