AmplTNLP.hpp
Go to the documentation of this file.
1 // Copyright (C) 2004, 2009 International Business Machines and others.
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // $Id: AmplTNLP.hpp 2005 2011-06-06 12:55:16Z stefan $
6 //
7 // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
8 
9 #ifndef __IPAMPLTNLP_HPP__
10 #define __IPAMPLTNLP_HPP__
11 
12 #include "IpUtils.hpp"
13 #include "IpTNLP.hpp"
14 #include "IpJournalist.hpp"
15 #include "IpOptionsList.hpp"
16 
17 #include <map>
18 #include <string>
19 
20 /* non Ipopt forward declaration */
21 struct ASL_pfgh;
22 struct SufDecl;
23 struct SufDesc;
24 
25 namespace Ipopt
26 {
28  {
29  public:
31 
33 
35  {
38  };
39 
41  {
46  };
47 
48  void AddAvailableSuffix(std::string suffix_string, Suffix_Source source, Suffix_Type type)
49  {
50  suffix_ids_.push_back(suffix_string);
51  suffix_types_.push_back(type);
52  suffix_sources_.push_back(source);
53  // suffix_values_.push_back();
54  }
55 
56  const Index* GetIntegerSuffixValues(std::string suffix_string, Suffix_Source source) const;
57 
58  const Number* GetNumberSuffixValues(std::string suffix_string, Suffix_Source source) const;
59 
60  std::vector<Index> GetIntegerSuffixValues(Index n, std::string suffix_string, Suffix_Source source) const;
61 
62  std::vector<Number> GetNumberSuffixValues(Index n, std::string suffix_string, Suffix_Source source) const;
63 
64  private:
74  //AmplSuffixHandler();
75 
78 
80  void operator=(const AmplSuffixHandler&);
82 
83  mutable ASL_pfgh* asl_;
84 
85  SufDecl* suftab_;
86 
87  std::vector<std::string> suffix_ids_;
88  std::vector<Suffix_Type> suffix_types_;
89  std::vector<Suffix_Source> suffix_sources_;
90 
92  void PrepareAmplForSuffixes(ASL_pfgh* asl);
93 
95  // void RetrieveSuffixesFromAmpl(ASL_pfgh* asl);
96 
97  friend class AmplTNLP;
98  };
99 
103  {
104  public:
109  WS_Option, /* this is for AMPL's internal wantsol callback */
110  HaltOnError_Option /* this is for our setting of the nerror_ member */
111  };
112 
116  {
117  public:
118  AmplOption(const std::string ipopt_option_name,
119  AmplOptionType type,
120  const std::string description);
121 
123  {
124  delete [] description_;
125  }
126 
127  const std::string& IpoptOptionName() const
128  {
129  return ipopt_option_name_;
130  }
132  {
133  return type_;
134  }
135  char* Description() const
136  {
137  return description_;
138  }
139  private:
149  AmplOption();
150 
152  AmplOption(const AmplOption&);
153 
155  void operator=(const AmplOption&);
157 
158  const std::string ipopt_option_name_;
161  };
162 
164  {
165  public:
166  PrivatInfo(const std::string ipopt_name,
167  SmartPtr<OptionsList> options,
169  void** nerror = NULL)
170  :
171  ipopt_name_(ipopt_name),
172  options_(options),
173  jnlst_(jnlst),
174  nerror_(nerror)
175  {}
176  const std::string& IpoptName() const
177  {
178  return ipopt_name_;
179  }
181  {
182  return options_;
183  }
185  {
186  return jnlst_;
187  }
188  void** NError()
189  {
190  return nerror_;
191  }
192  private:
193  const std::string ipopt_name_;
196  void** nerror_;
197  };
198 
199  public:
202  :
203  keywds_(NULL),
204  nkeywds_(0)
205  {}
206 
209 
211  void AddAmplOption(const std::string ampl_option_name,
212  const std::string ipopt_option_name,
214  const std::string description)
215  {
216  SmartPtr<AmplOption> new_option =
217  new AmplOption(ipopt_option_name, type, description);
218  ampl_options_map_[ampl_option_name] = ConstPtr(new_option);
219  }
220 
223  {
224  return (Index)ampl_options_map_.size();
225  }
226 
228  void* Keywords(const SmartPtr<OptionsList>& options,
230  void** nerror);
231 
232  private:
242  //AmplOptionsList();
243 
246 
248  void operator=(const AmplOptionsList&);
250 
252  std::map<std::string, SmartPtr<const AmplOption> > ampl_options_map_;
253  // AW: I think it should be with const like in the following line
254  // but with const the AIX compiler fails
255  // std::map<const std::string, SmartPtr<const AmplOption> > ampl_options_map_;
256 
258  void* keywds_;
259 
262  };
263 
267  class AmplTNLP : public TNLP
268  {
269  public:
274  const SmartPtr<OptionsList> options,
275  char**& argv, SmartPtr<AmplSuffixHandler>
276  suffix_handler = NULL, bool allow_discrete = false,
277  SmartPtr<AmplOptionsList> ampl_options_list = NULL,
278  const char* ampl_option_string = NULL,
279  const char* ampl_invokation_string = NULL,
280  const char* ampl_banner_string = NULL,
281  std::string* nl_file_content = NULL);
282 
284  virtual ~AmplTNLP();
286 
288  DECLARE_STD_EXCEPTION(NONPOSITIVE_SCALING_FACTOR);
289 
295  virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
296  Index& nnz_h_lag, IndexStyleEnum& index_style);
297 
300  virtual bool get_var_con_metadata(Index n,
301  StringMetaDataMapType& var_string_md,
302  IntegerMetaDataMapType& var_integer_md,
303  NumericMetaDataMapType& var_numeric_md,
304  Index m,
305  StringMetaDataMapType& con_string_md,
306  IntegerMetaDataMapType& con_integer_md,
307  NumericMetaDataMapType& con_numeric_md);
308 
310  virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
311  Index m, Number* g_l, Number* g_u);
312 
316  virtual bool get_constraints_linearity(Index m,
317  LinearityType* const_types);
318 
321  virtual bool get_starting_point(Index n, bool init_x, Number* x,
322  bool init_z, Number* z_L, Number* z_U,
323  Index m, bool init_lambda, Number* lambda);
324 
326  virtual bool eval_f(Index n, const Number* x, bool new_x,
327  Number& obj_value);
328 
331  virtual bool eval_grad_f(Index n, const Number* x, bool new_x,
332  Number* grad_f);
333 
335  virtual bool eval_g(Index n, const Number* x, bool new_x,
336  Index m, Number* g);
337 
341  virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
342  Index m, Index nele_jac, Index* iRow,
343  Index *jCol, Number* values);
344 
348  virtual bool eval_h(Index n, const Number* x, bool new_x,
349  Number obj_factor, Index m, const Number* lambda,
350  bool new_lambda, Index nele_hess, Index* iRow,
351  Index* jCol, Number* values);
352 
356  bool& use_x_scaling, Index n,
357  Number* x_scaling,
358  bool& use_g_scaling, Index m,
359  Number* g_scaling);
361 
364  virtual void finalize_solution(SolverReturn status,
365  Index n, const Number* x, const Number* z_L, const Number* z_U,
366  Index m, const Number* g, const Number* lambda,
367  Number obj_value,
368  const IpoptData* ip_data,
371 
375  virtual bool get_list_of_nonlinear_variables(Index num_nonlin_vars,
376  Index* pos_nonlin_vars);
378 
379 
383  ASL_pfgh* AmplSolverObject()
384  {
385  return asl_;
386  }
387 
391  void write_solution_file(const std::string& message) const;
392 
398  void get_discrete_info(Index& nlvb_,
399  Index& nlvbi_,
400  Index& nlvc_,
401  Index& nlvci_,
402  Index& nlvo_,
403  Index& nlvoi_,
404  Index& nbv_,
405  Index& niv_) const;
407 
413  void set_active_objective(Index obj_no);
414 
420  void set_string_metadata_for_var(std::string tag, std::vector<std::string> meta_data)
421  {
422  var_string_md_[tag] = meta_data;
423  }
424 
425  void set_integer_metadata_for_var(std::string tag, std::vector<Index> meta_data)
426  {
427  var_integer_md_[tag] = meta_data;
428  }
429 
430  void set_numeric_metadata_for_var(std::string tag, std::vector<Number> meta_data)
431  {
432  var_numeric_md_[tag] = meta_data;
433  }
434 
435  void set_string_metadata_for_con(std::string tag, std::vector<std::string> meta_data)
436  {
437  con_string_md_[tag] = meta_data;
438  }
439 
440  void set_integer_metadata_for_con(std::string tag, std::vector<Index> meta_data)
441  {
442  con_integer_md_[tag] = meta_data;
443  }
444 
445  void set_numeric_metadata_for_con(std::string tag, std::vector<Number> meta_data)
446  {
447  con_numeric_md_[tag] = meta_data;
448  }
450 
453  {
454  return suffix_handler_;
455  }
456 
457  private:
467  AmplTNLP();
468 
470  AmplTNLP(const AmplTNLP&);
471 
473  void operator=(const AmplTNLP&);
475 
478 
480  ASL_pfgh* asl_;
481 
483  double obj_sign_;
484 
487  Index nz_h_full_; // number of nonzeros in the full_x hessian
488  /* the rest of the problem size data is available easily through the ampl variables */
490 
501 
517 
519  void* Oinfo_ptr_;
520 
522  void* nerror_;
523 
526 
528  bool internal_objval(const Number* x, Number& obj_val);
529 
531  bool internal_conval(const Number* x, Index m, Number* g=NULL);
532 
535  bool apply_new_x(bool new_x, Index n, const Number* x);
536 
540  char* get_options(const SmartPtr<OptionsList>& options,
541  SmartPtr<AmplOptionsList>& ampl_options_list,
542  const char* ampl_option_string,
543  const char* ampl_invokation_string,
544  const char* ampl_banner_string, char**& argv);
545 
547  bool nerror_ok(void* nerror);
548 
550  void call_hesset();
551 
559  };
560 
561 
562 
563 } // namespace Ipopt
564 
565 #endif