00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __IPAMPLTNLP_HPP__
00010 #define __IPAMPLTNLP_HPP__
00011
00012 #include "IpUtils.hpp"
00013 #include "IpTNLP.hpp"
00014 #include "IpJournalist.hpp"
00015 #include "IpOptionsList.hpp"
00016
00017 #include "IpoptConfig.h"
00018 #ifdef HAVE_CSTRING
00019 # include <cstring>
00020 #else
00021 # ifdef HAVE_STRING_H
00022 # include <string.h>
00023 # else
00024 # error "don't have header file for string"
00025 # endif
00026 #endif
00027
00028 #include <map>
00029 #include <string>
00030
00031
00032 struct ASL_pfgh;
00033 struct SufDecl;
00034 struct SufDesc;
00035
00036 namespace Ipopt
00037 {
00038 class AmplSuffixHandler : public ReferencedObject
00039 {
00040 public:
00041 AmplSuffixHandler();
00042
00043 ~AmplSuffixHandler();
00044
00045 enum Suffix_Type
00046 {
00047 Index_Type,
00048 Number_Type
00049 };
00050
00051 enum Suffix_Source
00052 {
00053 Variable_Source,
00054 Constraint_Source,
00055 Objective_Source,
00056 Problem_Source
00057 };
00058
00059 void AddAvailableSuffix(std::string suffix_string, Suffix_Source source, Suffix_Type type)
00060 {
00061 suffix_ids_.push_back(suffix_string);
00062 suffix_types_.push_back(type);
00063 suffix_sources_.push_back(source);
00064
00065 }
00066
00067 const Index* GetIntegerSuffixValues(std::string suffix_string, Suffix_Source source) const;
00068
00069 const Number* GetNumberSuffixValues(std::string suffix_string, Suffix_Source source) const;
00070
00071 private:
00081
00082
00084 AmplSuffixHandler(const AmplSuffixHandler&);
00085
00087 void operator=(const AmplSuffixHandler&);
00089
00090 mutable ASL_pfgh* asl_;
00091
00092 SufDecl* suftab_;
00093
00094 std::vector<std::string> suffix_ids_;
00095 std::vector<Suffix_Type> suffix_types_;
00096 std::vector<Suffix_Source> suffix_sources_;
00097
00099 void PrepareAmplForSuffixes(ASL_pfgh* asl);
00100
00102
00103
00104 friend class AmplTNLP;
00105 };
00106
00109 class AmplOptionsList : public ReferencedObject
00110 {
00111 public:
00112 enum AmplOptionType {
00113 String_Option,
00114 Number_Option,
00115 Integer_Option,
00116 WS_Option,
00117 HaltOnError_Option
00118 };
00119
00122 class AmplOption : public ReferencedObject
00123 {
00124 public:
00125 AmplOption(const std::string ipopt_option_name,
00126 AmplOptionType type,
00127 const std::string description)
00128 :
00129 ipopt_option_name_(ipopt_option_name),
00130 type_(type)
00131 {
00132 description_ = new char[description.size()+1];
00133 strcpy(description_, description.c_str());
00134 }
00135
00136 ~AmplOption()
00137 {
00138 delete [] description_;
00139 }
00140
00141 const std::string& IpoptOptionName() const
00142 {
00143 return ipopt_option_name_;
00144 }
00145 AmplOptionType Type() const
00146 {
00147 return type_;
00148 }
00149 char* Description() const
00150 {
00151 return description_;
00152 }
00153 private:
00163 AmplOption();
00164
00166 AmplOption(const AmplOption&);
00167
00169 void operator=(const AmplOption&);
00171
00172 const std::string ipopt_option_name_;
00173 const AmplOptionType type_;
00174 char* description_;
00175 };
00176
00177 class PrivatInfo
00178 {
00179 public:
00180 PrivatInfo(const std::string ipopt_name,
00181 SmartPtr<OptionsList> options,
00182 SmartPtr<const Journalist> jnlst,
00183 void** nerror = NULL)
00184 :
00185 ipopt_name_(ipopt_name),
00186 options_(options),
00187 jnlst_(jnlst),
00188 nerror_(nerror)
00189 {}
00190 const std::string& IpoptName() const
00191 {
00192 return ipopt_name_;
00193 }
00194 const SmartPtr<OptionsList>& Options() const
00195 {
00196 return options_;
00197 }
00198 const SmartPtr<const Journalist>& Jnlst() const
00199 {
00200 return jnlst_;
00201 }
00202 void** NError()
00203 {
00204 return nerror_;
00205 }
00206 private:
00207 const std::string ipopt_name_;
00208 const SmartPtr<OptionsList> options_;
00209 const SmartPtr<const Journalist> jnlst_;
00210 void** nerror_;
00211 };
00212
00213 public:
00215 AmplOptionsList()
00216 :
00217 keywds_(NULL),
00218 nkeywds_(0)
00219 {}
00220
00222 ~AmplOptionsList();
00223
00225 void AddAmplOption(const std::string ampl_option_name,
00226 const std::string ipopt_option_name,
00227 AmplOptionsList::AmplOptionType type,
00228 const std::string description)
00229 {
00230 SmartPtr<AmplOption> new_option =
00231 new AmplOption(ipopt_option_name, type, description);
00232 ampl_options_map_[ampl_option_name] = ConstPtr(new_option);
00233 }
00234
00236 Index NumberOfAmplOptions()
00237 {
00238 return (Index)ampl_options_map_.size();
00239 }
00240
00242 void* Keywords(const SmartPtr<OptionsList>& options,
00243 SmartPtr<const Journalist> jnlst,
00244 void** nerror);
00245
00246 private:
00256
00257
00259 AmplOptionsList(const AmplOptionsList&);
00260
00262 void operator=(const AmplOptionsList&);
00264
00266 std::map<std::string, SmartPtr<const AmplOption> > ampl_options_map_;
00267
00268
00269
00270
00272 void* keywds_;
00273
00275 Index nkeywds_;
00276 };
00277
00281 class AmplTNLP : public TNLP
00282 {
00283 public:
00287 AmplTNLP(const SmartPtr<const Journalist>& jnlst,
00288 const SmartPtr<OptionsList> options,
00289 char**& argv, SmartPtr<AmplSuffixHandler>
00290 suffix_handler = NULL, bool allow_discrete = false,
00291 SmartPtr<AmplOptionsList> ampl_options_list = NULL,
00292 const char* ampl_option_string = NULL,
00293 const char* ampl_invokation_string = NULL,
00294 const char* ampl_banner_string = NULL,
00295 std::string* nl_file_content = NULL);
00296
00298 virtual ~AmplTNLP();
00300
00302 DECLARE_STD_EXCEPTION(NONPOSITIVE_SCALING_FACTOR);
00303
00309 virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
00310 Index& nnz_h_lag, IndexStyleEnum& index_style);
00311
00313 virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
00314 Index m, Number* g_l, Number* g_u);
00315
00319 virtual bool get_constraints_linearity(Index m,
00320 LinearityType* const_types);
00321
00324 virtual bool get_starting_point(Index n, bool init_x, Number* x,
00325 bool init_z, Number* z_L, Number* z_U,
00326 Index m, bool init_lambda, Number* lambda);
00327
00329 virtual bool eval_f(Index n, const Number* x, bool new_x,
00330 Number& obj_value);
00331
00334 virtual bool eval_grad_f(Index n, const Number* x, bool new_x,
00335 Number* grad_f);
00336
00338 virtual bool eval_g(Index n, const Number* x, bool new_x,
00339 Index m, Number* g);
00340
00344 virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
00345 Index m, Index nele_jac, Index* iRow,
00346 Index *jCol, Number* values);
00347
00351 virtual bool eval_h(Index n, const Number* x, bool new_x,
00352 Number obj_factor, Index m, const Number* lambda,
00353 bool new_lambda, Index nele_hess, Index* iRow,
00354 Index* jCol, Number* values);
00355
00358 virtual bool get_scaling_parameters(Number& obj_scaling,
00359 bool& use_x_scaling, Index n,
00360 Number* x_scaling,
00361 bool& use_g_scaling, Index m,
00362 Number* g_scaling);
00364
00367 virtual void finalize_solution(SolverReturn status,
00368 Index n, const Number* x, const Number* z_L, const Number* z_U,
00369 Index m, const Number* g, const Number* lambda,
00370 Number obj_value,
00371 const IpoptData* ip_data,
00372 IpoptCalculatedQuantities* ip_cq);
00374
00377 virtual Index get_number_of_nonlinear_variables();
00378 virtual bool get_list_of_nonlinear_variables(Index num_nonlin_vars,
00379 Index* pos_nonlin_vars);
00381
00382
00386 ASL_pfgh* AmplSolverObject()
00387 {
00388 return asl_;
00389 }
00390
00394 void write_solution_file(const std::string& message) const;
00395
00401 void get_discrete_info(Index& nlvb_,
00402 Index& nlvbi_,
00403 Index& nlvc_,
00404 Index& nlvci_,
00405 Index& nlvo_,
00406 Index& nlvoi_,
00407 Index& nbv_,
00408 Index& niv_) const;
00410
00416 void set_active_objective(Index obj_no);
00417
00418 private:
00428 AmplTNLP();
00429
00431 AmplTNLP(const AmplTNLP&);
00432
00434 void operator=(const AmplTNLP&);
00436
00438 SmartPtr<const Journalist> jnlst_;
00439
00441 ASL_pfgh* asl_;
00442
00444 double obj_sign_;
00445
00448 Index nz_h_full_;
00449
00451
00455 Number* x_sol_;
00456 Number* z_L_sol_;
00457 Number* z_U_sol_;
00458 Number* g_sol_;
00459 Number* lambda_sol_;
00460 Number obj_sol_;
00462
00468 bool objval_called_with_current_x_;
00472 bool conval_called_with_current_x_;
00474 bool hesset_called_;
00476 bool set_active_objective_called_;
00478
00480 void* Oinfo_ptr_;
00481
00483 void* nerror_;
00484
00486 SmartPtr<AmplSuffixHandler> suffix_handler_;
00487
00489 bool internal_objval(const Number* x, Number& obj_val);
00490
00492 bool internal_conval(const Number* x, Index m, Number* g=NULL);
00493
00496 bool apply_new_x(bool new_x, Index n, const Number* x);
00497
00501 char* get_options(const SmartPtr<OptionsList>& options,
00502 SmartPtr<AmplOptionsList>& ampl_options_list,
00503 const char* ampl_option_string,
00504 const char* ampl_invokation_string,
00505 const char* ampl_banner_string, char**& argv);
00506
00508 bool nerror_ok(void* nerror);
00509
00511 void call_hesset();
00512 };
00513
00514
00515
00516 }
00517
00518 #endif