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

Go to the documentation of this file.
00001 // Copyright (C) 2004, 2007 International Business Machines and others.
00002 // All Rights Reserved.
00003 // This code is published under the Common Public License.
00004 //
00005 // $Id: AmplTNLP.hpp 1010 2007-06-21 15:54:36Z 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 "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 /* non Ipopt forward declaration */
00032 struct ASL_pfgh;
00033 struct SufDecl;
00034 struct SufDesc;
00035 
00036 namespace Ipopt
00037 {
00038   /* forward declarations */
00039   class AmplSuffixHandler;
00040 
00043   class AmplOptionsList : public ReferencedObject
00044   {
00045   public:
00046     enum AmplOptionType {
00047       String_Option,
00048       Number_Option,
00049       Integer_Option,
00050       WS_Option,  /* this is for AMPL's internal wantsol callback */
00051       HaltOnError_Option /* this is for our setting of the nerror_ member */
00052     };
00053 
00056   class AmplOption : public ReferencedObject
00057     {
00058     public:
00059       AmplOption(const std::string ipopt_option_name,
00060                  AmplOptionType type,
00061                  const std::string description)
00062           :
00063           ipopt_option_name_(ipopt_option_name),
00064           type_(type)
00065       {
00066         description_ = new char[description.size()+1];
00067         strcpy(description_, description.c_str());
00068       }
00069 
00070       ~AmplOption()
00071       {
00072         delete [] description_;
00073       }
00074 
00075       const std::string& IpoptOptionName() const
00076       {
00077         return ipopt_option_name_;
00078       }
00079       AmplOptionType Type() const
00080       {
00081         return type_;
00082       }
00083       char* Description() const
00084       {
00085         return description_;
00086       }
00087     private:
00097       AmplOption();
00098 
00100       AmplOption(const AmplOption&);
00101 
00103       void operator=(const AmplOption&);
00105 
00106       const std::string ipopt_option_name_;
00107       const AmplOptionType type_;
00108       char* description_;
00109     };
00110 
00111     class PrivatInfo
00112     {
00113     public:
00114       PrivatInfo(const std::string ipopt_name,
00115                  SmartPtr<OptionsList> options,
00116                  SmartPtr<const Journalist> jnlst,
00117                  void** nerror = NULL)
00118           :
00119           ipopt_name_(ipopt_name),
00120           options_(options),
00121           jnlst_(jnlst),
00122           nerror_(nerror)
00123       {}
00124       const std::string& IpoptName() const
00125       {
00126         return ipopt_name_;
00127       }
00128       const SmartPtr<OptionsList>& Options() const
00129       {
00130         return options_;
00131       }
00132       const SmartPtr<const Journalist>& Jnlst() const
00133       {
00134         return jnlst_;
00135       }
00136       void** NError()
00137       {
00138         return nerror_;
00139       }
00140     private:
00141       const std::string ipopt_name_;
00142       const SmartPtr<OptionsList> options_;
00143       const SmartPtr<const Journalist> jnlst_;
00144       void** nerror_;
00145     };
00146 
00147   public:
00149     AmplOptionsList()
00150         :
00151         keywds_(NULL),
00152         nkeywds_(0)
00153     {}
00154 
00156     ~AmplOptionsList();
00157 
00159     void AddAmplOption(const std::string ampl_option_name,
00160                        const std::string ipopt_option_name,
00161                        AmplOptionsList::AmplOptionType type,
00162                        const std::string description)
00163     {
00164       SmartPtr<AmplOption> new_option =
00165         new AmplOption(ipopt_option_name, type, description);
00166       ampl_options_map_[ampl_option_name] = ConstPtr(new_option);
00167     }
00168 
00170     Index NumberOfAmplOptions()
00171     {
00172       return ampl_options_map_.size();
00173     }
00174 
00176     void* Keywords(const SmartPtr<OptionsList>& options,
00177                    SmartPtr<const Journalist> jnlst,
00178                    void** nerror);
00179 
00180   private:
00190     //AmplOptionsList();
00191 
00193     AmplOptionsList(const AmplOptionsList&);
00194 
00196     void operator=(const AmplOptionsList&);
00198 
00200     std::map<std::string, SmartPtr<const AmplOption> > ampl_options_map_;
00201     // AW: I think it should be with const like in the following line
00202     //     but with const the AIX compiler fails
00203     // std::map<const std::string, SmartPtr<const AmplOption> > ampl_options_map_;
00204 
00206     void* keywds_;
00207 
00209     Index nkeywds_;
00210   };
00211 
00215   class AmplTNLP : public TNLP
00216   {
00217   public:
00221     AmplTNLP(const SmartPtr<const Journalist>& jnlst,
00222              const SmartPtr<OptionsList> options,
00223              char**& argv, SmartPtr<AmplSuffixHandler>
00224              suffix_handler = NULL, bool allow_discrete = false,
00225              SmartPtr<AmplOptionsList> ampl_options_list = NULL,
00226              const char* ampl_option_string = NULL,
00227              const char* ampl_invokation_string = NULL,
00228              const char* ampl_banner_string = NULL,
00229              std::string* nl_file_content = NULL);
00230 
00232     virtual ~AmplTNLP();
00234 
00236     DECLARE_STD_EXCEPTION(NONPOSITIVE_SCALING_FACTOR);
00237 
00243     virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
00244                               Index& nnz_h_lag, IndexStyleEnum& index_style);
00245 
00247     virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
00248                                  Index m, Number* g_l, Number* g_u);
00249 
00253     virtual bool get_constraints_linearity(Index m,
00254                                            LinearityType* const_types);
00255 
00258     virtual bool get_starting_point(Index n, bool init_x, Number* x,
00259                                     bool init_z, Number* z_L, Number* z_U,
00260                                     Index m, bool init_lambda, Number* lambda);
00261 
00263     virtual bool eval_f(Index n, const Number* x, bool new_x,
00264                         Number& obj_value);
00265 
00268     virtual bool eval_grad_f(Index n, const Number* x, bool new_x,
00269                              Number* grad_f);
00270 
00272     virtual bool eval_g(Index n, const Number* x, bool new_x,
00273                         Index m, Number* g);
00274 
00278     virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
00279                             Index m, Index nele_jac, Index* iRow,
00280                             Index *jCol, Number* values);
00281 
00285     virtual bool eval_h(Index n, const Number* x, bool new_x,
00286                         Number obj_factor, Index m, const Number* lambda,
00287                         bool new_lambda, Index nele_hess, Index* iRow,
00288                         Index* jCol, Number* values);
00289 
00292     virtual bool get_scaling_parameters(Number& obj_scaling,
00293                                         bool& use_x_scaling, Index n,
00294                                         Number* x_scaling,
00295                                         bool& use_g_scaling, Index m,
00296                                         Number* g_scaling);
00298 
00301     virtual void finalize_solution(SolverReturn status,
00302                                    Index n, const Number* x, const Number* z_L, const Number* z_U,
00303                                    Index m, const Number* g, const Number* lambda,
00304                                    Number obj_value,
00305                                    const IpoptData* ip_data,
00306                                    IpoptCalculatedQuantities* ip_cq);
00308 
00311     virtual Index get_number_of_nonlinear_variables();
00312     virtual bool get_list_of_nonlinear_variables(Index num_nonlin_vars,
00313         Index* pos_nonlin_vars);
00315 
00316 
00320     ASL_pfgh* AmplSolverObject()
00321     {
00322       return asl_;
00323     }
00324 
00328     void write_solution_file(const std::string& message) const;
00329 
00335     void get_discrete_info(Index& nlvb_,
00336                            Index& nlvbi_,
00337                            Index& nlvc_,
00338                            Index& nlvci_,
00339                            Index& nlvo_,
00340                            Index& nlvoi_,
00341                            Index& nbv_,
00342                            Index& niv_) const;
00344 
00350     void set_active_objective(Index obj_no);
00351 
00352   private:
00362     AmplTNLP();
00363 
00365     AmplTNLP(const AmplTNLP&);
00366 
00368     void operator=(const AmplTNLP&);
00370 
00372     SmartPtr<const Journalist> jnlst_;
00373 
00375     ASL_pfgh* asl_;
00376 
00378     double obj_sign_;
00379 
00382     Index nz_h_full_; // number of nonzeros in the full_x hessian
00383     /* the rest of the problem size data is available easily through the ampl variables */
00385 
00389     Number* x_sol_;
00390     Number* z_L_sol_;
00391     Number* z_U_sol_;
00392     Number* g_sol_;
00393     Number* lambda_sol_;
00394     Number obj_sol_;
00396 
00402     bool objval_called_with_current_x_;
00406     bool conval_called_with_current_x_;
00408     bool hesset_called_;
00410     bool set_active_objective_called_;
00412 
00414     void* Oinfo_ptr_;
00415 
00417     void* nerror_;
00418 
00420     SmartPtr<AmplSuffixHandler> suffix_handler_;
00421 
00423     bool internal_objval(const Number* x, Number& obj_val);
00424 
00426     bool internal_conval(const Number* x, Index m, Number* g=NULL);
00427 
00430     bool apply_new_x(bool new_x, Index n, const Number* x);
00431 
00435     char* get_options(const SmartPtr<OptionsList>& options,
00436                       SmartPtr<AmplOptionsList>& ampl_options_list,
00437                       const char* ampl_option_string,
00438                       const char* ampl_invokation_string,
00439                       const char* ampl_banner_string, char**& argv);
00440 
00442     bool nerror_ok(void* nerror);
00443 
00445     void call_hesset();
00446   };
00447 
00448 
00449   class AmplSuffixHandler : public ReferencedObject
00450   {
00451   public:
00452     AmplSuffixHandler();
00453 
00454     ~AmplSuffixHandler();
00455 
00456     enum Suffix_Type
00457     {
00458       Index_Type,
00459       Number_Type
00460     };
00461 
00462     enum Suffix_Source
00463     {
00464       Variable_Source,
00465       Constraint_Source,
00466       Objective_Source,
00467       Problem_Source
00468     };
00469 
00470     void AddAvailableSuffix(std::string suffix_string, Suffix_Source source, Suffix_Type type)
00471     {
00472       suffix_ids_.push_back(suffix_string);
00473       suffix_types_.push_back(type);
00474       suffix_sources_.push_back(source);
00475       //      suffix_values_.push_back();
00476     }
00477 
00478     const Index* GetIntegerSuffixValues(std::string suffix_string, Suffix_Source source) const;
00479 
00480     const Number* GetNumberSuffixValues(std::string suffix_string, Suffix_Source source) const;
00481 
00482   private:
00492     //AmplSuffixHandler();
00493 
00495     AmplSuffixHandler(const AmplSuffixHandler&);
00496 
00498     void operator=(const AmplSuffixHandler&);
00500 
00501     mutable ASL_pfgh* asl_;
00502 
00503     SufDecl* suftab_;
00504 
00505     std::vector<std::string> suffix_ids_;
00506     std::vector<Suffix_Type> suffix_types_;
00507     std::vector<Suffix_Source> suffix_sources_;
00508 
00510     void PrepareAmplForSuffixes(ASL_pfgh* asl);
00511 
00513     //    void RetrieveSuffixesFromAmpl(ASL_pfgh* asl);
00514 
00515     friend class AmplTNLP;
00516   };
00517 
00518 
00519 } // namespace Ipopt
00520 
00521 #endif

Generated on Thu May 15 22:27:52 2008 by  doxygen 1.4.7