1 | // (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2004 |
---|
2 | // All Rights Reserved. |
---|
3 | // This code is published under the Common Public License. |
---|
4 | // |
---|
5 | // Authors : |
---|
6 | // Carl D. Laird, Carnegie Mellon University, |
---|
7 | // Andreas Waechter, International Business Machines Corporation |
---|
8 | // Pierre Bonami, Carnegie Mellon University, |
---|
9 | // |
---|
10 | // Date : 12/01/2004 |
---|
11 | |
---|
12 | #ifndef __IPAMPLTMINLP_HPP__ |
---|
13 | #define __IPAMPLTMINLP_HPP__ |
---|
14 | |
---|
15 | #include "TMINLP.hpp" |
---|
16 | #include "IpSmartPtr.hpp" |
---|
17 | #include "CoinPackedMatrix.hpp" |
---|
18 | #include "OsiCuts.hpp" |
---|
19 | |
---|
20 | namespace Ipopt |
---|
21 | { |
---|
22 | |
---|
23 | |
---|
24 | // Declarations, so that we don't have to include the Ipopt AMPL headers |
---|
25 | class AmplTNLP; |
---|
26 | class AmplSuffixHandler; |
---|
27 | class AmplOptionsList; |
---|
28 | |
---|
29 | /** Ampl MINLP Interface. |
---|
30 | * Ampl MINLP Interface, implemented as a TMINLP. |
---|
31 | * This interface creates a AmplTNLP and also retrieves |
---|
32 | * the information about the binary and integer variables |
---|
33 | */ |
---|
34 | class AmplTMINLP : public TMINLP |
---|
35 | { |
---|
36 | public: |
---|
37 | /**@name Constructors/Destructors */ |
---|
38 | //@{ |
---|
39 | /** Constructor */ |
---|
40 | AmplTMINLP(const SmartPtr<const Journalist>& jnlst, |
---|
41 | const SmartPtr<OptionsList> options, |
---|
42 | char**& argv, |
---|
43 | AmplSuffixHandler* suffix_handler = NULL, |
---|
44 | const std::string& appName = "bonmin", |
---|
45 | std::string* nl_file_content = NULL); |
---|
46 | |
---|
47 | virtual void Initialize(const SmartPtr<const Journalist>& jnlst, |
---|
48 | const SmartPtr<OptionsList> options, |
---|
49 | char**& argv, |
---|
50 | AmplSuffixHandler* suffix_handler =NULL, |
---|
51 | const std::string& appName = "bonmin", |
---|
52 | std::string* nl_file_content = NULL); |
---|
53 | |
---|
54 | /** read the branching priorities from ampl suffixes.*/ |
---|
55 | void read_priorities(); |
---|
56 | |
---|
57 | /** read the sos constraints from ampl suffixes */ |
---|
58 | void read_sos(); |
---|
59 | |
---|
60 | AmplTMINLP(); |
---|
61 | |
---|
62 | virtual AmplTMINLP * createEmpty() |
---|
63 | { |
---|
64 | AmplTMINLP * tminlp = new AmplTMINLP; |
---|
65 | return tminlp; |
---|
66 | } |
---|
67 | |
---|
68 | /** Default destructor */ |
---|
69 | virtual ~AmplTMINLP(); |
---|
70 | //@} |
---|
71 | |
---|
72 | /**@name methods to gather information about the NLP. These |
---|
73 | * methods are overloaded from TMINLP. See TMINLP for their more |
---|
74 | * detailed documentation. */ |
---|
75 | //@{ |
---|
76 | /** returns dimensions of the nlp. Overloaded from TMINLP */ |
---|
77 | virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, |
---|
78 | Index& nnz_h_lag, |
---|
79 | TNLP::IndexStyleEnum& index_style); |
---|
80 | |
---|
81 | /** returns the vector of variable types */ |
---|
82 | virtual bool get_var_types(Index n, VariableType* var_types); |
---|
83 | /** return the vector of constraints types*/ |
---|
84 | virtual bool get_constraints_types(Index n, ConstraintType* const_types); |
---|
85 | /** returns bounds of the nlp. Overloaded from TMINLP */ |
---|
86 | virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u, |
---|
87 | Index m, Number* g_l, Number* g_u); |
---|
88 | |
---|
89 | /** provides a starting point for the nlp variables. Overloaded |
---|
90 | from TMINLP */ |
---|
91 | virtual bool get_starting_point(Index n, bool init_x, Number* x, |
---|
92 | Index m, bool init_lambda, Number* lambda); |
---|
93 | |
---|
94 | /** evaluates the objective value for the nlp. Overloaded from TMINLP */ |
---|
95 | virtual bool eval_f(Index n, const Number* x, bool new_x, |
---|
96 | Number& obj_value); |
---|
97 | |
---|
98 | /** evaluates the gradient of the objective for the |
---|
99 | nlp. Overloaded from TMINLP */ |
---|
100 | virtual bool eval_grad_f(Index n, const Number* x, bool new_x, |
---|
101 | Number* grad_f); |
---|
102 | |
---|
103 | /** evaluates the constraint residuals for the nlp. Overloaded from TMINLP */ |
---|
104 | virtual bool eval_g(Index n, const Number* x, bool new_x, |
---|
105 | Index m, Number* g); |
---|
106 | |
---|
107 | /** specifies the jacobian structure (if values is NULL) and |
---|
108 | * evaluates the jacobian values (if values is not NULL) for the |
---|
109 | * nlp. Overloaded from TMINLP */ |
---|
110 | virtual bool eval_jac_g(Index n, const Number* x, bool new_x, |
---|
111 | Index m, Index nele_jac, Index* iRow, |
---|
112 | Index *jCol, Number* values); |
---|
113 | |
---|
114 | /** specifies the structure of the hessian of the lagrangian (if |
---|
115 | * values is NULL) and evaluates the values (if values is not |
---|
116 | * NULL). Overloaded from TMINLP */ |
---|
117 | virtual bool eval_h(Index n, const Number* x, bool new_x, |
---|
118 | Number obj_factor, Index m, const Number* lambda, |
---|
119 | bool new_lambda, Index nele_hess, Index* iRow, |
---|
120 | Index* jCol, Number* values); |
---|
121 | //@} |
---|
122 | |
---|
123 | /** @name Solution Methods */ |
---|
124 | //@{ |
---|
125 | virtual void finalize_solution(SolverReturn status, |
---|
126 | Index n, const Number* x, const Number* z_L, const Number* z_U, |
---|
127 | Index m, const Number* g, const Number* lambda, |
---|
128 | Number obj_value); |
---|
129 | |
---|
130 | void write_solution(const std::string & message, const Number *x_sol, const Number * lambda_sol); |
---|
131 | //@} |
---|
132 | |
---|
133 | /** Write the solution file. This is a wrapper for AMPL's |
---|
134 | * write_sol. TODO Maybe this should be at a different place, or |
---|
135 | * collect the numbers itself? */ |
---|
136 | void write_solution_file(const std::string& message) const; |
---|
137 | //@} |
---|
138 | |
---|
139 | |
---|
140 | virtual const BranchingInfo * branchingInfo() const |
---|
141 | { |
---|
142 | return &branch_; |
---|
143 | } |
---|
144 | |
---|
145 | virtual const SosInfo * sosConstraints() const |
---|
146 | { |
---|
147 | return &sos_; |
---|
148 | } |
---|
149 | |
---|
150 | /** @name User callbacks */ |
---|
151 | //@{ |
---|
152 | /** Additional application specific options.*/ |
---|
153 | virtual void fillApplicationOptions(AmplOptionsList* amplOptList) |
---|
154 | {} |
---|
155 | //@} |
---|
156 | |
---|
157 | |
---|
158 | /** This methods gives the linear part of the objective function */ |
---|
159 | virtual void getLinearPartOfObjective(double * obj); |
---|
160 | /** Journlist */ |
---|
161 | SmartPtr<const Journalist> jnlst_; |
---|
162 | |
---|
163 | /** Sign of the objective fn (1 for min, -1 for max) */ |
---|
164 | double obj_sign_; |
---|
165 | |
---|
166 | /**@name Problem Size Data*/ |
---|
167 | //@{ |
---|
168 | Index nz_h_full_; // number of nonzeros in the full_x hessian |
---|
169 | /* the rest of the problem size data is available easily through the ampl variables */ |
---|
170 | //@} |
---|
171 | |
---|
172 | /**@name Internal copies of data */ |
---|
173 | //@{ |
---|
174 | /** A non-const copy of x - this is kept up-to-date in apply_new_x */ |
---|
175 | Number* non_const_x_; |
---|
176 | |
---|
177 | /** Solution Vectors */ |
---|
178 | Number* x_sol_; |
---|
179 | Number* z_L_sol_; |
---|
180 | Number* z_U_sol_; |
---|
181 | Number* g_sol_; |
---|
182 | Number* lambda_sol_; |
---|
183 | Number obj_sol_; |
---|
184 | //@} |
---|
185 | |
---|
186 | /**@name Flags to track internal state */ |
---|
187 | //@{ |
---|
188 | /** true when the objective value has been calculated with the |
---|
189 | * current x, set to false in apply_new_x, and set to true in |
---|
190 | * internal_objval */ |
---|
191 | bool objval_called_with_current_x_; |
---|
192 | /** true when the constraint values have been calculated with the |
---|
193 | * current x, set to false in apply_new_x, and set to true in |
---|
194 | * internal_conval */ |
---|
195 | bool conval_called_with_current_x_; |
---|
196 | //@} |
---|
197 | |
---|
198 | |
---|
199 | /** Make the objective call to ampl */ |
---|
200 | bool internal_objval(Number& obj_val); |
---|
201 | |
---|
202 | /** Make the constraint call to ampl*/ |
---|
203 | bool internal_conval(Index m, Number* g=NULL); |
---|
204 | |
---|
205 | /** Internal function to update the internal and ampl state if the |
---|
206 | x value changes */ |
---|
207 | void apply_new_x(bool new_x, Index n, const Number* x); |
---|
208 | |
---|
209 | /** Method to add the extra option for ampl */ |
---|
210 | void fillAmplOptionList(AmplOptionsList* amplOptList); |
---|
211 | |
---|
212 | /**@name Default Compiler Generated Methods |
---|
213 | * (Hidden to avoid implicit creation/calling). |
---|
214 | * These methods are not implemented and |
---|
215 | * we do not want the compiler to implement |
---|
216 | * them for us, so we declare them private |
---|
217 | * and do not define them. This ensures that |
---|
218 | * they will not be implicitly created/called. */ |
---|
219 | //@{ |
---|
220 | |
---|
221 | /** Copy Constructor */ |
---|
222 | AmplTMINLP(const AmplTMINLP&); |
---|
223 | |
---|
224 | /** Overloaded Equals Operator */ |
---|
225 | void operator=(const AmplTMINLP&); |
---|
226 | //@} |
---|
227 | |
---|
228 | /** pointer to the internal AmplTNLP */ |
---|
229 | AmplTNLP* ampl_tnlp_; |
---|
230 | |
---|
231 | /** Storage of branching priorities information.*/ |
---|
232 | BranchingInfo branch_; |
---|
233 | /** Storage of sos constraints */ |
---|
234 | SosInfo sos_; |
---|
235 | /** Store a suffix handler (since Ipopt does not want to give access to his >:( ).*/ |
---|
236 | SmartPtr<AmplSuffixHandler> suffix_handler_; |
---|
237 | }; |
---|
238 | } // namespace Ipopt |
---|
239 | |
---|
240 | #endif |
---|