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 std::vector<Index> GetIntegerSuffixValues(Index n, std::string suffix_string, Suffix_Source source) const;
00072
00073 std::vector<Number> GetNumberSuffixValues(Index n, std::string suffix_string, Suffix_Source source) const;
00074
00075 private:
00085
00086
00088 AmplSuffixHandler(const AmplSuffixHandler&);
00089
00091 void operator=(const AmplSuffixHandler&);
00093
00094 mutable ASL_pfgh* asl_;
00095
00096 SufDecl* suftab_;
00097
00098 std::vector<std::string> suffix_ids_;
00099 std::vector<Suffix_Type> suffix_types_;
00100 std::vector<Suffix_Source> suffix_sources_;
00101
00103 void PrepareAmplForSuffixes(ASL_pfgh* asl);
00104
00106
00107
00108 friend class AmplTNLP;
00109 };
00110
00113 class AmplOptionsList : public ReferencedObject
00114 {
00115 public:
00116 enum AmplOptionType {
00117 String_Option,
00118 Number_Option,
00119 Integer_Option,
00120 WS_Option,
00121 HaltOnError_Option
00122 };
00123
00126 class AmplOption : public ReferencedObject
00127 {
00128 public:
00129 AmplOption(const std::string ipopt_option_name,
00130 AmplOptionType type,
00131 const std::string description)
00132 :
00133 ipopt_option_name_(ipopt_option_name),
00134 type_(type)
00135 {
00136 description_ = new char[description.size()+1];
00137 strcpy(description_, description.c_str());
00138 }
00139
00140 ~AmplOption()
00141 {
00142 delete [] description_;
00143 }
00144
00145 const std::string& IpoptOptionName() const
00146 {
00147 return ipopt_option_name_;
00148 }
00149 AmplOptionType Type() const
00150 {
00151 return type_;
00152 }
00153 char* Description() const
00154 {
00155 return description_;
00156 }
00157 private:
00167 AmplOption();
00168
00170 AmplOption(const AmplOption&);
00171
00173 void operator=(const AmplOption&);
00175
00176 const std::string ipopt_option_name_;
00177 const AmplOptionType type_;
00178 char* description_;
00179 };
00180
00181 class PrivatInfo
00182 {
00183 public:
00184 PrivatInfo(const std::string ipopt_name,
00185 SmartPtr<OptionsList> options,
00186 SmartPtr<const Journalist> jnlst,
00187 void** nerror = NULL)
00188 :
00189 ipopt_name_(ipopt_name),
00190 options_(options),
00191 jnlst_(jnlst),
00192 nerror_(nerror)
00193 {}
00194 const std::string& IpoptName() const
00195 {
00196 return ipopt_name_;
00197 }
00198 const SmartPtr<OptionsList>& Options() const
00199 {
00200 return options_;
00201 }
00202 const SmartPtr<const Journalist>& Jnlst() const
00203 {
00204 return jnlst_;
00205 }
00206 void** NError()
00207 {
00208 return nerror_;
00209 }
00210 private:
00211 const std::string ipopt_name_;
00212 const SmartPtr<OptionsList> options_;
00213 const SmartPtr<const Journalist> jnlst_;
00214 void** nerror_;
00215 };
00216
00217 public:
00219 AmplOptionsList()
00220 :
00221 keywds_(NULL),
00222 nkeywds_(0)
00223 {}
00224
00226 ~AmplOptionsList();
00227
00229 void AddAmplOption(const std::string ampl_option_name,
00230 const std::string ipopt_option_name,
00231 AmplOptionsList::AmplOptionType type,
00232 const std::string description)
00233 {
00234 SmartPtr<AmplOption> new_option =
00235 new AmplOption(ipopt_option_name, type, description);
00236 ampl_options_map_[ampl_option_name] = ConstPtr(new_option);
00237 }
00238
00240 Index NumberOfAmplOptions()
00241 {
00242 return (Index)ampl_options_map_.size();
00243 }
00244
00246 void* Keywords(const SmartPtr<OptionsList>& options,
00247 SmartPtr<const Journalist> jnlst,
00248 void** nerror);
00249
00250 private:
00260
00261
00263 AmplOptionsList(const AmplOptionsList&);
00264
00266 void operator=(const AmplOptionsList&);
00268
00270 std::map<std::string, SmartPtr<const AmplOption> > ampl_options_map_;
00271
00272
00273
00274
00276 void* keywds_;
00277
00279 Index nkeywds_;
00280 };
00281
00285 class AmplTNLP : public TNLP
00286 {
00287 public:
00291 AmplTNLP(const SmartPtr<const Journalist>& jnlst,
00292 const SmartPtr<OptionsList> options,
00293 char**& argv, SmartPtr<AmplSuffixHandler>
00294 suffix_handler = NULL, bool allow_discrete = false,
00295 SmartPtr<AmplOptionsList> ampl_options_list = NULL,
00296 const char* ampl_option_string = NULL,
00297 const char* ampl_invokation_string = NULL,
00298 const char* ampl_banner_string = NULL,
00299 std::string* nl_file_content = NULL);
00300
00302 virtual ~AmplTNLP();
00304
00306 DECLARE_STD_EXCEPTION(NONPOSITIVE_SCALING_FACTOR);
00307
00313 virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
00314 Index& nnz_h_lag, IndexStyleEnum& index_style);
00315
00318 virtual bool get_var_con_metadata(Index n,
00319 StringMetaDataMapType& var_string_md,
00320 IntegerMetaDataMapType& var_integer_md,
00321 NumericMetaDataMapType& var_numeric_md,
00322 Index m,
00323 StringMetaDataMapType& con_string_md,
00324 IntegerMetaDataMapType& con_integer_md,
00325 NumericMetaDataMapType& con_numeric_md);
00326
00328 virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
00329 Index m, Number* g_l, Number* g_u);
00330
00334 virtual bool get_constraints_linearity(Index m,
00335 LinearityType* const_types);
00336
00339 virtual bool get_starting_point(Index n, bool init_x, Number* x,
00340 bool init_z, Number* z_L, Number* z_U,
00341 Index m, bool init_lambda, Number* lambda);
00342
00344 virtual bool eval_f(Index n, const Number* x, bool new_x,
00345 Number& obj_value);
00346
00349 virtual bool eval_grad_f(Index n, const Number* x, bool new_x,
00350 Number* grad_f);
00351
00353 virtual bool eval_g(Index n, const Number* x, bool new_x,
00354 Index m, Number* g);
00355
00359 virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
00360 Index m, Index nele_jac, Index* iRow,
00361 Index *jCol, Number* values);
00362
00366 virtual bool eval_h(Index n, const Number* x, bool new_x,
00367 Number obj_factor, Index m, const Number* lambda,
00368 bool new_lambda, Index nele_hess, Index* iRow,
00369 Index* jCol, Number* values);
00370
00373 virtual bool get_scaling_parameters(Number& obj_scaling,
00374 bool& use_x_scaling, Index n,
00375 Number* x_scaling,
00376 bool& use_g_scaling, Index m,
00377 Number* g_scaling);
00379
00382 virtual void finalize_solution(SolverReturn status,
00383 Index n, const Number* x, const Number* z_L, const Number* z_U,
00384 Index m, const Number* g, const Number* lambda,
00385 Number obj_value,
00386 const IpoptData* ip_data,
00387 IpoptCalculatedQuantities* ip_cq);
00389
00392 virtual Index get_number_of_nonlinear_variables();
00393 virtual bool get_list_of_nonlinear_variables(Index num_nonlin_vars,
00394 Index* pos_nonlin_vars);
00396
00397
00401 ASL_pfgh* AmplSolverObject()
00402 {
00403 return asl_;
00404 }
00405
00409 void write_solution_file(const std::string& message) const;
00410
00416 void get_discrete_info(Index& nlvb_,
00417 Index& nlvbi_,
00418 Index& nlvc_,
00419 Index& nlvci_,
00420 Index& nlvo_,
00421 Index& nlvoi_,
00422 Index& nbv_,
00423 Index& niv_) const;
00425
00431 void set_active_objective(Index obj_no);
00432
00438 void set_string_metadata_for_var(std::string tag, std::vector<std::string> meta_data)
00439 {
00440 var_string_md_[tag] = meta_data;
00441 }
00442
00443 void set_integer_metadata_for_var(std::string tag, std::vector<Index> meta_data)
00444 {
00445 var_integer_md_[tag] = meta_data;
00446 }
00447
00448 void set_numeric_metadata_for_var(std::string tag, std::vector<Number> meta_data)
00449 {
00450 var_numeric_md_[tag] = meta_data;
00451 }
00452
00453 void set_string_metadata_for_con(std::string tag, std::vector<std::string> meta_data)
00454 {
00455 con_string_md_[tag] = meta_data;
00456 }
00457
00458 void set_integer_metadata_for_con(std::string tag, std::vector<Index> meta_data)
00459 {
00460 con_integer_md_[tag] = meta_data;
00461 }
00462
00463 void set_numeric_metadata_for_con(std::string tag, std::vector<Number> meta_data)
00464 {
00465 con_numeric_md_[tag] = meta_data;
00466 }
00468
00470 SmartPtr<AmplSuffixHandler> get_suffix_handler()
00471 {
00472 return suffix_handler_;
00473 }
00474
00475 private:
00485 AmplTNLP();
00486
00488 AmplTNLP(const AmplTNLP&);
00489
00491 void operator=(const AmplTNLP&);
00493
00495 SmartPtr<const Journalist> jnlst_;
00496
00498 ASL_pfgh* asl_;
00499
00501 double obj_sign_;
00502
00505 Index nz_h_full_;
00506
00508
00512 Number* x_sol_;
00513 Number* z_L_sol_;
00514 Number* z_U_sol_;
00515 Number* g_sol_;
00516 Number* lambda_sol_;
00517 Number obj_sol_;
00519
00525 bool objval_called_with_current_x_;
00529 bool conval_called_with_current_x_;
00531 bool hesset_called_;
00533 bool set_active_objective_called_;
00535
00537 void* Oinfo_ptr_;
00538
00540 void* nerror_;
00541
00543 SmartPtr<AmplSuffixHandler> suffix_handler_;
00544
00546 bool internal_objval(const Number* x, Number& obj_val);
00547
00549 bool internal_conval(const Number* x, Index m, Number* g=NULL);
00550
00553 bool apply_new_x(bool new_x, Index n, const Number* x);
00554
00558 char* get_options(const SmartPtr<OptionsList>& options,
00559 SmartPtr<AmplOptionsList>& ampl_options_list,
00560 const char* ampl_option_string,
00561 const char* ampl_invokation_string,
00562 const char* ampl_banner_string, char**& argv);
00563
00565 bool nerror_ok(void* nerror);
00566
00568 void call_hesset();
00569
00571 StringMetaDataMapType var_string_md_;
00572 IntegerMetaDataMapType var_integer_md_;
00573 NumericMetaDataMapType var_numeric_md_;
00574 StringMetaDataMapType con_string_md_;
00575 IntegerMetaDataMapType con_integer_md_;
00576 NumericMetaDataMapType con_numeric_md_;
00577 };
00578
00579
00580
00581 }
00582
00583 #endif