/home/coin/SVN-release/Ipopt-3.2.1/Ipopt/src/Apps/AmplSolver/AmplTNLP.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2004, 2006 International Business Machines and others.
00002 // All Rights Reserved.
00003 // This code is published under the Common Public License.
00004 //
00005 // $Id: AmplTNLP.hpp 759 2006-07-07 03:07:08Z andreasw $
00006 //
00007 // Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13
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 <map>
00018 #include <string>
00019 
00020 /* non Ipopt forward declaration */
00021 struct ASL_pfgh;
00022 struct SufDecl;
00023 struct SufDesc;
00024 
00025 namespace Ipopt
00026 {
00027   /* forward declarations */
00028   class AmplSuffixHandler;
00029 
00032   class AmplOptionsList : public ReferencedObject
00033   {
00034   public:
00035     enum AmplOptionType {
00036       String_Option,
00037       Number_Option,
00038       Integer_Option,
00039       WS_Option,  /* this is for AMPL's internal wantsol callback */
00040       HaltOnError_Option /* this is for our setting of the nerror_ member */
00041     };
00042 
00045   class AmplOption : public ReferencedObject
00046     {
00047     public:
00048       AmplOption(const std::string ipopt_option_name,
00049                  AmplOptionType type,
00050                  const std::string description)
00051           :
00052           ipopt_option_name_(ipopt_option_name),
00053           type_(type)
00054       {
00055         description_ = new char[description.size()+1];
00056         strcpy(description_, description.c_str());
00057       }
00058 
00059       ~AmplOption()
00060       {
00061         delete [] description_;
00062       }
00063 
00064       const std::string& IpoptOptionName() const
00065       {
00066         return ipopt_option_name_;
00067       }
00068       AmplOptionType Type() const
00069       {
00070         return type_;
00071       }
00072       char* Description() const
00073       {
00074         return description_;
00075       }
00076     private:
00086       AmplOption();
00087 
00089       AmplOption(const AmplOption&);
00090 
00092       void operator=(const AmplOption&);
00094 
00095       const std::string ipopt_option_name_;
00096       const AmplOptionType type_;
00097       char* description_;
00098     };
00099 
00100     class PrivatInfo
00101     {
00102     public:
00103       PrivatInfo(const std::string ipopt_name,
00104                  SmartPtr<OptionsList> options,
00105                  SmartPtr<const Journalist> jnlst,
00106                  void** nerror = NULL)
00107           :
00108           ipopt_name_(ipopt_name),
00109           options_(options),
00110           jnlst_(jnlst),
00111           nerror_(nerror)
00112       {}
00113       const std::string& IpoptName() const
00114       {
00115         return ipopt_name_;
00116       }
00117       const SmartPtr<OptionsList>& Options() const
00118       {
00119         return options_;
00120       }
00121       const SmartPtr<const Journalist>& Jnlst() const
00122       {
00123         return jnlst_;
00124       }
00125       void** NError()
00126       {
00127         return nerror_;
00128       }
00129     private:
00130       const std::string ipopt_name_;
00131       const SmartPtr<OptionsList> options_;
00132       const SmartPtr<const Journalist> jnlst_;
00133       void** nerror_;
00134     };
00135 
00136   public:
00138     AmplOptionsList()
00139         :
00140         keywds_(NULL),
00141         nkeywds_(0)
00142     {}
00143 
00145     ~AmplOptionsList();
00146 
00148     void AddAmplOption(const std::string ampl_option_name,
00149                        const std::string ipopt_option_name,
00150                        AmplOptionsList::AmplOptionType type,
00151                        const std::string description)
00152     {
00153       SmartPtr<AmplOption> new_option =
00154         new AmplOption(ipopt_option_name, type, description);
00155       ampl_options_map_[ampl_option_name] = ConstPtr(new_option);
00156     }
00157 
00159     Index NumberOfAmplOptions()
00160     {
00161       return ampl_options_map_.size();
00162     }
00163 
00165     void* Keywords(const SmartPtr<OptionsList>& options,
00166                    SmartPtr<const Journalist> jnlst,
00167                    void** nerror);
00168 
00169   private:
00179     //AmplOptionsList();
00180 
00182     AmplOptionsList(const AmplOptionsList&);
00183 
00185     void operator=(const AmplOptionsList&);
00187 
00189     std::map<std::string, SmartPtr<const AmplOption> > ampl_options_map_;
00190     // AW: I think it should be with const like in the following line
00191     //     but with const the AIX compiler fails
00192     // std::map<const std::string, SmartPtr<const AmplOption> > ampl_options_map_;
00193 
00195     void* keywds_;
00196 
00198     Index nkeywds_;
00199   };
00200 
00204   class AmplTNLP : public TNLP
00205   {
00206   public:
00210     AmplTNLP(const SmartPtr<const Journalist>& jnlst,
00211              const SmartPtr<OptionsList> options,
00212              char**& argv, SmartPtr<AmplSuffixHandler>
00213              suffix_handler = NULL, bool allow_discrete = false,
00214              SmartPtr<AmplOptionsList> ampl_options_list = NULL,
00215              const char* ampl_option_string = NULL,
00216              const char* ampl_invokation_string = NULL,
00217              const char* ampl_banner_string = NULL,
00218              std::string* nl_file_content = NULL);
00219 
00221     virtual ~AmplTNLP();
00223 
00225     DECLARE_STD_EXCEPTION(NONPOSITIVE_SCALING_FACTOR);
00226 
00232     virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
00233                               Index& nnz_h_lag, IndexStyleEnum& index_style);
00234 
00236     virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
00237                                  Index m, Number* g_l, Number* g_u);
00238 
00241     virtual bool get_starting_point(Index n, bool init_x, Number* x,
00242                                     bool init_z, Number* z_L, Number* z_U,
00243                                     Index m, bool init_lambda, Number* lambda);
00244 
00246     virtual bool eval_f(Index n, const Number* x, bool new_x,
00247                         Number& obj_value);
00248 
00251     virtual bool eval_grad_f(Index n, const Number* x, bool new_x,
00252                              Number* grad_f);
00253 
00255     virtual bool eval_g(Index n, const Number* x, bool new_x,
00256                         Index m, Number* g);
00257 
00261     virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
00262                             Index m, Index nele_jac, Index* iRow,
00263                             Index *jCol, Number* values);
00264 
00268     virtual bool eval_h(Index n, const Number* x, bool new_x,
00269                         Number obj_factor, Index m, const Number* lambda,
00270                         bool new_lambda, Index nele_hess, Index* iRow,
00271                         Index* jCol, Number* values);
00272 
00275     virtual bool get_scaling_parameters(Number& obj_scaling,
00276                                         bool& use_x_scaling, Index n,
00277                                         Number* x_scaling,
00278                                         bool& use_g_scaling, Index m,
00279                                         Number* g_scaling);
00281 
00284     virtual void finalize_solution(SolverReturn status,
00285                                    Index n, const Number* x, const Number* z_L, const Number* z_U,
00286                                    Index m, const Number* g, const Number* lambda,
00287                                    Number obj_value);
00289 
00292     virtual Index get_number_of_nonlinear_variables();
00293     virtual bool get_list_of_nonlinear_variables(Index num_nonlin_vars,
00294         Index* pos_nonlin_vars);
00296 
00297 
00301     ASL_pfgh* AmplSolverObject()
00302     {
00303       return asl_;
00304     }
00305 
00309     void write_solution_file(const std::string& message) const;
00310 
00316     void get_discrete_info(Index& nlvb_,
00317                            Index& nlvbi_,
00318                            Index& nlvc_,
00319                            Index& nlvci_,
00320                            Index& nlvo_,
00321                            Index& nlvoi_,
00322                            Index& nbv_,
00323                            Index& niv_) const;
00325 
00326   private:
00336     AmplTNLP();
00337 
00339     AmplTNLP(const AmplTNLP&);
00340 
00342     void operator=(const AmplTNLP&);
00344 
00346     SmartPtr<const Journalist> jnlst_;
00347 
00349     ASL_pfgh* asl_;
00350 
00352     double obj_sign_;
00353 
00356     Index nz_h_full_; // number of nonzeros in the full_x hessian
00357     /* the rest of the problem size data is available easily through the ampl variables */
00359 
00363     Number* non_const_x_;
00364 
00366     Number* x_sol_;
00367     Number* z_L_sol_;
00368     Number* z_U_sol_;
00369     Number* g_sol_;
00370     Number* lambda_sol_;
00371     Number obj_sol_;
00373 
00379     bool objval_called_with_current_x_;
00383     bool conval_called_with_current_x_;
00385 
00387     void* Oinfo_ptr_;
00388 
00390     void* nerror_;
00391 
00393     SmartPtr<AmplSuffixHandler> suffix_handler_;
00394 
00396     bool internal_objval(Number& obj_val);
00397 
00399     bool internal_conval(Index m, Number* g=NULL);
00400 
00403     bool apply_new_x(bool new_x, Index n, const Number* x);
00404 
00408     char* get_options(const SmartPtr<OptionsList>& options,
00409                       SmartPtr<AmplOptionsList>& ampl_options_list,
00410                       const char* ampl_option_string,
00411                       const char* ampl_invokation_string,
00412                       const char* ampl_banner_string, char**& argv);
00413 
00415     bool nerror_ok(void* nerror);
00416   };
00417 
00418 
00419   class AmplSuffixHandler : public ReferencedObject
00420   {
00421   public:
00422     AmplSuffixHandler();
00423 
00424     ~AmplSuffixHandler();
00425 
00426     enum Suffix_Type
00427     {
00428       Index_Type,
00429       Number_Type
00430     };
00431 
00432     enum Suffix_Source
00433     {
00434       Variable_Source,
00435       Constraint_Source,
00436       Objective_Source,
00437       Problem_Source
00438     };
00439 
00440     void AddAvailableSuffix(std::string suffix_string, Suffix_Source source, Suffix_Type type)
00441     {
00442       suffix_ids_.push_back(suffix_string);
00443       suffix_types_.push_back(type);
00444       suffix_sources_.push_back(source);
00445       //      suffix_values_.push_back();
00446     }
00447 
00448     const Index* GetIntegerSuffixValues(std::string suffix_string, Suffix_Source source) const;
00449 
00450     const Number* GetNumberSuffixValues(std::string suffix_string, Suffix_Source source) const;
00451 
00452   private:
00462     //AmplSuffixHandler();
00463 
00465     AmplSuffixHandler(const AmplSuffixHandler&);
00466 
00468     void operator=(const AmplSuffixHandler&);
00470 
00471     mutable ASL_pfgh* asl_;
00472 
00473     SufDecl* suftab_;
00474 
00475     std::vector<std::string> suffix_ids_;
00476     std::vector<Suffix_Type> suffix_types_;
00477     std::vector<Suffix_Source> suffix_sources_;
00478 
00480     void PrepareAmplForSuffixes(ASL_pfgh* asl);
00481 
00483     //    void RetrieveSuffixesFromAmpl(ASL_pfgh* asl);
00484 
00485     friend class AmplTNLP;
00486   };
00487 
00488 
00489 } // namespace Ipopt
00490 
00491 #endif

Generated on Thu May 15 22:17:57 2008 by  doxygen 1.4.7