Debian Codegraph @

Bonmin/src/Interfaces/Ampl/BonAmplTMINLP.hpp

raw
1 // (C) Copyright International Business Machines Corporation and
2 // Carnegie Mellon University 2004, 2007
3 //
4 // All Rights Reserved.
5 // This code is published under the Eclipse Public License.
6 //
7 // Authors :
8 // Carl D. Laird, Carnegie Mellon University,
9 // Andreas Waechter, International Business Machines Corporation
10 // Pierre Bonami, Carnegie Mellon University,
11 //
12 // Date : 12/01/2004
13
14 #ifndef __IPAMPLTMINLP_HPP__
15 #define __IPAMPLTMINLP_HPP__
16
17 #include "BonTMINLP.hpp"
18 #include "IpSmartPtr.hpp"
19 #include "CoinPackedMatrix.hpp"
20 #include "OsiCuts.hpp"
21 #include "BonRegisteredOptions.hpp"
22 #include "BonTypes.hpp"
23
24 /* non Ipopt forward declaration */
25 struct ASL_pfgh;
26 struct SufDecl;
27 struct SufDesc;
28
29
30 // Declarations, so that we don't have to include the Ipopt AMPL headers
31 namespace Ipopt
32 {
33 class AmplSuffixHandler;
34 class AmplOptionsList;
35 class AmplTNLP;
36 }
37
38 namespace Bonmin
39 {
40
41 /** Ampl MINLP Interface.
42 * Ampl MINLP Interface, implemented as a TMINLP.
43 * This interface creates a AmplTNLP and also retrieves
44 * the information about the binary and integer variables
45 */
46 class AmplTMINLP : public TMINLP
47 {
48 public:
49 /**@name Constructors/Destructors */
50 //@{
51 /** Constructor */
52 AmplTMINLP(const Ipopt::SmartPtr<const Ipopt::Journalist>& jnlst,
53 const Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
54 const Ipopt::SmartPtr<Ipopt::OptionsList> options,
55 char**& argv,
56 Ipopt::AmplSuffixHandler* suffix_handler = NULL,
57 const std::string& appName = "bonmin",
58 std::string* nl_file_content = NULL);
59
60 virtual void Initialize(const Ipopt::SmartPtr<const Ipopt::Journalist>& jnlst,
61 const Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
62 const Ipopt::SmartPtr<Ipopt::OptionsList> options,
63 char**& argv,
64 Ipopt::AmplSuffixHandler* suffix_handler =NULL,
65 const std::string& appName = "bonmin",
66 std::string* nl_file_content = NULL);
67
68 /** read the branching priorities from ampl suffixes.*/
69 void read_priorities();
70
71 /** read the sos constraints from ampl suffixes */
72 void read_sos();
73
74 /** Read suffixes which indicate which constraints are convex.*/
75 void read_convexities();
76
77 /** Read suffixes used to apply perspective in OA to some of the constraints.*/
78 void read_onoff();
79
80 /** Read suffixes on objective functions for upper bounding*/
81 void read_obj_suffixes();
82
83 /** Default constructor.*/
84 AmplTMINLP();
85
86 virtual AmplTMINLP * createEmpty()
87 {
88 AmplTMINLP * tminlp = new AmplTMINLP;
89 return tminlp;
90 }
91
92 /** destructor */
93 virtual ~AmplTMINLP();
94 //@}
95
96 /** Return the ampl solver object (ASL*) */
97 const ASL_pfgh* AmplSolverObject() const;
98
99
100 /**@name methods to gather information about the NLP. These
101 * methods are overloaded from TMINLP. See TMINLP for their more
102 * detailed documentation. */
103 //@{
104 /** returns dimensions of the nlp. Overloaded from TMINLP */
105 virtual bool get_nlp_info(Ipopt::Index& n, Ipopt::Index& m, Ipopt::Index& nnz_jac_g,
106 Ipopt::Index& nnz_h_lag,
107 Ipopt::TNLP::IndexStyleEnum& index_style);
108
109 /** returns the vector of variable types */
110 virtual bool get_variables_types(Ipopt::Index n, VariableType* var_types);
111
112 /** return the variables linearity (linear or not)*/
113 virtual bool get_variables_linearity(Ipopt::Index n, Ipopt::TNLP::LinearityType * var_types);
114
115 /** Returns the constraint linearity.
116 * array should be alocated with length at least n.*/
117 virtual bool get_constraints_linearity(Ipopt::Index m,
118 Ipopt::TNLP::LinearityType* const_types);
119
120 /** returns bounds of the nlp. Overloaded from TMINLP */
121 virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number* x_l, Ipopt::Number* x_u,
122 Ipopt::Index m, Ipopt::Number* g_l, Ipopt::Number* g_u);
123
124 /** provides a starting point for the nlp variables. Overloaded
125 from TMINLP */
126 virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number* x,
127 bool init_z, Ipopt::Number* z_L, Ipopt::Number* z_U,
128 Ipopt::Index m, bool init_lambda, Ipopt::Number* lambda);
129
130 /** evaluates the objective value for the nlp. Overloaded from TMINLP */
131 virtual bool eval_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
132 Ipopt::Number& obj_value);
133
134 /** evaluates the gradient of the objective for the
135 nlp. Overloaded from TMINLP */
136 virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
137 Ipopt::Number* grad_f);
138
139 /** evaluates the constraint residuals for the nlp. Overloaded from TMINLP */
140 virtual bool eval_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
141 Ipopt::Index m, Ipopt::Number* g);
142
143 /** specifies the jacobian structure (if values is NULL) and
144 * evaluates the jacobian values (if values is not NULL) for the
145 * nlp. Overloaded from TMINLP */
146 virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
147 Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index* iRow,
148 Ipopt::Index *jCol, Ipopt::Number* values);
149
150 /** specifies the structure of the hessian of the lagrangian (if
151 * values is NULL) and evaluates the values (if values is not
152 * NULL). Overloaded from TMINLP */
153 virtual bool eval_h(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
154 Ipopt::Number obj_factor, Ipopt::Index m, const Ipopt::Number* lambda,
155 bool new_lambda, Ipopt::Index nele_hess, Ipopt::Index* iRow,
156 Ipopt::Index* jCol, Ipopt::Number* values);
157
158 /** compute the value of a single constraint */
159 virtual bool eval_gi(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
160 Ipopt::Index i, Ipopt::Number& gi);
161 /** compute the structure or values of the gradient for one
162 constraint */
163 virtual bool eval_grad_gi(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
164 Ipopt::Index i, Ipopt::Index& nele_grad_gi, Ipopt::Index* jCol,
165 Ipopt::Number* values);
166 //@}
167
168 /** @name Solution Methods */
169 //@{
170 /** Called after optimizing to return results to ampl.
171 * Status code is put into solve_result_num according to the table below.
172 * <table>
173 * <tr> <td> <b> <center> Code </center> </b> </td> <td> <b> <center> Status </center> </b> </td> </tr>
174 * <tr> <td> 3 </td> <td> Integer optimal </td> </tr>
175 * <tr> <td> 220 </td> <td> problem is proven infeasible. </td> </tr>
176 * <tr> <td> 421 </td> <td> limit reached or user interrupt with integer feasible solution found. </td> </tr>
177 * <tr> <td> 410 </td> <td> limit reached or user interrupt without any integer feasible solution. </td> </tr>
178 * <tr> <td> 500 </td> <td> error. </td> </tr>
179 * <caption> Status codes for optimization. </caption>
180 * </table>
181 * */
182 virtual void finalize_solution(TMINLP::SolverReturn status,
183 Ipopt::Index n, const Ipopt::Number* x, Ipopt::Number obj_value);
184
185 /** Write the solution using ampl's write_sol (called by finalize_solution).*/
186 void write_solution(const std::string & message, const Ipopt::Number *x_sol);
187 //@}
188
189 //@}
190
191
192 virtual const BranchingInfo * branchingInfo() const
193 {
194 return &branch_;
195 }
196
197 virtual const SosInfo * sosConstraints() const
198 {
199 return &sos_;
200 }
201
202 virtual const PerturbInfo* perturbInfo() const
203 {
204 return &perturb_info_;
205 }
206
207 /** @name User callbacks */
208 //@{
209 /** Additional application specific options.*/
210 virtual void fillApplicationOptions(Ipopt::AmplOptionsList* amplOptList)
211 {}
212 //@}
213
214
215 /** This methods gives the linear part of the objective function */
216 virtual void getLinearPartOfObjective(double * obj);
217
218
219 /** Do we have an alternate objective for upper bounding?*/
220 virtual bool hasUpperBoundingObjective()
221 {
222 return upperBoundingObj_ != -1;
223 }
224
225 /** This method to returns the value of an alternative objective function for
226 upper bounding (if one has been declared by using the prefix UBObj).*/
227 virtual bool eval_upper_bound_f(Ipopt::Index n, const Ipopt::Number* x,
228 Ipopt::Number& obj_value);
229
230 /** Get accest to constraint convexities.*/
231 virtual bool get_constraint_convexities(int m, TMINLP::Convexity * constraints_convexities)const
232 {
233 if (constraintsConvexities_ != NULL) {
234 CoinCopyN(constraintsConvexities_, m, constraints_convexities);
235 }
236 else {
237 CoinFillN(constraints_convexities, m, TMINLP::Convex);
238 }
239 return true;
240 }
241 /** Get dimension information on nonconvex constraints.*/
242 virtual bool get_number_nonconvex(int & number_non_conv, int & number_concave) const
243 {
244 number_non_conv = numberNonConvex_;
245 number_concave = numberSimpleConcave_;
246 return true;
247 }
248 /** Get array describing the constraints marked nonconvex in the model.*/
249 virtual bool get_constraint_convexities(int number_non_conv, MarkedNonConvex * non_convexes) const
250 {
251 assert(number_non_conv == numberNonConvex_);
252 CoinCopyN( nonConvexConstraintsAndRelaxations_, number_non_conv, non_convexes);
253 return true;
254 }
255 /** Fill array containing indices of simple concave constraints.*/
256 virtual bool get_simple_concave_constraints(int number_concave, SimpleConcaveConstraint * simple_concave) const
257 {
258 assert(number_concave == numberSimpleConcave_);
259 CoinCopyN(simpleConcaves_, numberSimpleConcave_, simple_concave);
260 return true;
261 }
262
263 /** Say if problem has a linear objective (for OA) */
264 virtual bool hasLinearObjective()
265 {
266 return hasLinearObjective_;
267 }
268
269 /** Access array describing onoff constraint.*/
270 virtual const int * get_const_xtra_id() const{
271 return c_extra_id_();
272 }
273 private:
274 /**@name Default Compiler Generated Methods
275 * (Hidden to avoid implicit creation/calling).
276 * These methods are not implemented and
277 * we do not want the compiler to implement
278 * them for us, so we declare them private
279 * and do not define them. This ensures that
280 * they will not be implicitly created/called. */
281 //@{
282
283 /** Copy Constructor */
284 AmplTMINLP(const AmplTMINLP&);
285
286 /** Overloaded Equals Operator */
287 void operator=(const AmplTMINLP&);
288 //@}
289 /** Name of application.*/
290 std::string appName_;
291
292 /** Index of the objective to use for upper bounding*/
293 int upperBoundingObj_;
294 /** pointer to the internal AmplTNLP */
295 Ipopt::AmplTNLP* ampl_tnlp_;
296 /** Journalist */
297 Ipopt::SmartPtr<const Ipopt::Journalist> jnlst_;
298
299 /** Storage of branching priorities information.*/
300 BranchingInfo branch_;
301 /** Storage of sos constraints */
302 SosInfo sos_;
303 /** Storage for perturbation radii */
304 PerturbInfo perturb_info_;
305 /** Store a suffix handler.*/
306 Ipopt::SmartPtr<Ipopt::AmplSuffixHandler> suffix_handler_;
307
308 /** Store constraints types.*/
309 TMINLP::Convexity * constraintsConvexities_;
310
311 /** Store onoff information.*/
312 vector<int> c_extra_id_;
313
314 /** Ipopt::Number of nonConvex constraints.*/
315 int numberNonConvex_;
316 /** Store marked non-convex constraints and their relaxations.*/
317 MarkedNonConvex * nonConvexConstraintsAndRelaxations_;
318 /** Ipopt::Number of simpleConcave constraints.*/
319 int numberSimpleConcave_;
320 /** Store simple concave constraints descriptions.*/
321 SimpleConcaveConstraint * simpleConcaves_;
322
323 /** Flag to indicate if objective function is linear */
324 bool hasLinearObjective_;
325
326 /** Flag to say if AMPL solution file should be written.*/
327 int writeAmplSolFile_;
328 };
329 } // namespace Ipopt
330
331 #endif
332