Ipopt Documentation  
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 // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
6 
7 #ifndef __IPAMPLTNLP_HPP__
8 #define __IPAMPLTNLP_HPP__
9 
10 #include "IpUtils.hpp"
11 #include "IpTNLP.hpp"
12 #include "IpJournalist.hpp"
13 #include "IpOptionsList.hpp"
14 
15 #include <map>
16 #include <string>
17 
18 /* non Ipopt forward declaration */
19 struct ASL_pfgh;
20 struct SufDecl;
21 struct SufDesc;
22 
23 namespace Ipopt
24 {
25 
26 class IPOPTAMPLINTERFACELIB_EXPORT AmplSuffixHandler: public ReferencedObject
27 {
28 public:
30 
32 
34  {
36  Number_Type
37  };
38 
40  {
44  Problem_Source
45  };
46 
48  std::string suffix_string,
49  Suffix_Source source,
50  Suffix_Type type
51  )
52  {
53  suffix_ids_.push_back(suffix_string);
54  suffix_types_.push_back(type);
55  suffix_sources_.push_back(source);
56  // suffix_values_.push_back();
57  }
58 
60  std::string suffix_string,
61  Suffix_Source source
62  ) const;
63 
65  std::string suffix_string,
66  Suffix_Source source
67  ) const;
68 
69  std::vector<Index> GetIntegerSuffixValues(
70  Index n,
71  std::string suffix_string,
72  Suffix_Source source
73  ) const;
74 
75  std::vector<Number> GetNumberSuffixValues(
76  Index n,
77  std::string suffix_string,
78  Suffix_Source source
79  ) const;
80 
81 private:
92 
93  //AmplSuffixHandler();
96  const AmplSuffixHandler&
97  );
98 
100  void operator=(
101  const AmplSuffixHandler&
102  );
104 
105  mutable ASL_pfgh* asl_;
106 
107  SufDecl* suftab_;
108 
109  std::vector<std::string> suffix_ids_;
110  std::vector<Suffix_Type> suffix_types_;
111  std::vector<Suffix_Source> suffix_sources_;
112 
115  ASL_pfgh* asl
116  );
117 
119  // void RetrieveSuffixesFromAmpl(ASL_pfgh* asl);
120  friend class AmplTNLP;
121 };
122 
126 class IPOPTAMPLINTERFACELIB_EXPORT AmplOptionsList: public ReferencedObject
127 {
128 public:
130  {
135  HaltOnError_Option
136  };
137 
139  class IPOPTAMPLINTERFACELIB_EXPORT AmplOption: public ReferencedObject
140  {
141  public:
143  const std::string ipopt_option_name,
144  AmplOptionType type,
145  const std::string description);
146 
148  {
149  delete[] description_;
150  }
151 
152  const std::string& IpoptOptionName() const
153  {
154  return ipopt_option_name_;
155  }
157  {
158  return type_;
159  }
160  char* Description() const
161  {
162  return description_;
163  }
164  private:
175 
177 
180  const AmplOption&);
181 
183  void operator=(
184  const AmplOption&);
186 
187  const std::string ipopt_option_name_;
190  };
191 
193  {
194  public:
196  const std::string ipopt_name,
197  SmartPtr<OptionsList> options,
199  void** nerror = NULL
200  )
201  : ipopt_name_(ipopt_name),
202  options_(options),
203  jnlst_(jnlst),
204  nerror_(nerror)
205  {
206  }
207  const std::string& IpoptName() const
208  {
209  return ipopt_name_;
210  }
212  {
213  return options_;
214  }
216  {
217  return jnlst_;
218  }
219  void** NError()
220  {
221  return nerror_;
222  }
223  private:
224  const std::string ipopt_name_;
227  void** nerror_;
228  };
229 
230 public:
233  : keywds_(NULL),
234  nkeywds_(0)
235  { }
236 
239 
242  const std::string ampl_option_name,
243  const std::string ipopt_option_name,
245  const std::string description
246  )
247  {
248  SmartPtr<AmplOption> new_option = new AmplOption(ipopt_option_name, type, description);
249  ampl_options_map_[ampl_option_name] = ConstPtr(new_option);
250  }
251 
254  {
255  return (Index) ampl_options_map_.size();
256  }
257 
259  void* Keywords(
260  const SmartPtr<OptionsList>& options,
262  void** nerror
263  );
264 
265 private:
276 
277  //AmplOptionsList();
280  const AmplOptionsList&
281  );
282 
284  void operator=(
285  const AmplOptionsList&
286  );
288 
290  std::string source,
291  std::string& dest
292  ) const;
293 
296  );
297 
300  );
301 
303  std::map<std::string, SmartPtr<const AmplOption> > ampl_options_map_;
304  // AW: I think it should be with const like in the following line
305  // but with const the AIX compiler fails
306  // std::map<const std::string, SmartPtr<const AmplOption> > ampl_options_map_;
307 
309  void* keywds_;
310 
313 };
314 
316 class IPOPTAMPLINTERFACELIB_EXPORT AmplTNLP: public TNLP
317 {
318 public:
321 
323  const SmartPtr<const Journalist>& jnlst,
324  const SmartPtr<OptionsList> options,
325  char**& argv,
326  SmartPtr<AmplSuffixHandler> suffix_handler = NULL,
327  bool allow_discrete = false,
328  SmartPtr<AmplOptionsList> ampl_options_list = NULL,
329  const char* ampl_option_string = NULL,
330  const char* ampl_invokation_string = NULL,
331  const char* ampl_banner_string = NULL,
332  std::string* nl_file_content = NULL
333  );
334 
336  virtual ~AmplTNLP();
338 
340  DECLARE_STD_EXCEPTION(NONPOSITIVE_SCALING_FACTOR);
341 
344  virtual bool get_nlp_info(
345  Index& n,
346  Index& m,
347  Index& nnz_jac_g,
348  Index& nnz_h_lag,
349  IndexStyleEnum& index_style
350  );
351 
352  virtual bool get_var_con_metadata(
353  Index n,
354  StringMetaDataMapType& var_string_md,
355  IntegerMetaDataMapType& var_integer_md,
356  NumericMetaDataMapType& var_numeric_md,
357  Index m,
358  StringMetaDataMapType& con_string_md,
359  IntegerMetaDataMapType& con_integer_md,
360  NumericMetaDataMapType& con_numeric_md
361  );
362 
364  virtual bool get_bounds_info(
365  Index n,
366  Number* x_l,
367  Number* x_u,
368  Index m,
369  Number* g_l,
370  Number* g_u
371  );
372 
374  Index m,
375  LinearityType* const_types
376  );
377 
378  virtual bool get_starting_point(
379  Index n,
380  bool init_x,
381  Number* x,
382  bool init_z,
383  Number* z_L,
384  Number* z_U,
385  Index m,
386  bool init_lambda,
387  Number* lambda
388  );
389 
390  virtual bool eval_f(
391  Index n,
392  const Number* x,
393  bool new_x,
394  Number& obj_value
395  );
396 
397  virtual bool eval_grad_f(
398  Index n,
399  const Number* x,
400  bool new_x,
401  Number* grad_f
402  );
403 
404  virtual bool eval_g(
405  Index n,
406  const Number* x,
407  bool new_x,
408  Index m,
409  Number* g
410  );
411 
412  virtual bool eval_jac_g(
413  Index n,
414  const Number* x,
415  bool new_x,
416  Index m,
417  Index nele_jac,
418  Index* iRow,
419  Index* jCol,
420  Number* values
421  );
422 
423  virtual bool eval_h(
424  Index n,
425  const Number* x,
426  bool new_x,
427  Number obj_factor,
428  Index m,
429  const Number* lambda,
430  bool new_lambda,
431  Index nele_hess,
432  Index* iRow,
433  Index* jCol,
434  Number* values
435  );
436 
438  Number& obj_scaling,
439  bool& use_x_scaling,
440  Index n,
441  Number* x_scaling,
442  bool& use_g_scaling,
443  Index m,
444  Number* g_scaling
445  );
447 
450  virtual void finalize_solution(
451  SolverReturn status,
452  Index n,
453  const Number* x,
454  const Number* z_L,
455  const Number* z_U,
456  Index m,
457  const Number* g,
458  const Number* lambda,
459  Number obj_value,
460  const IpoptData* ip_data,
462  );
464 
469  Index num_nonlin_vars,
470  Index* pos_nonlin_vars
471  );
473 
476 
477  ASL_pfgh* AmplSolverObject()
478  {
479  return asl_;
480  }
481 
488  const std::string& message
489  ) const;
490 
497  Index& nlvb_,
498  Index& nlvbi_,
499  Index& nlvc_,
500  Index& nlvci_,
501  Index& nlvo_,
502  Index& nlvoi_,
503  Index& nbv_,
504  Index& niv_
505  ) const;
507 
517  Index obj_no
518  );
519 
526  std::string tag,
527  std::vector<std::string> meta_data)
528  {
529  var_string_md_[tag] = meta_data;
530  }
531 
533  std::string tag,
534  std::vector<Index> meta_data)
535  {
536  var_integer_md_[tag] = meta_data;
537  }
538 
540  std::string tag,
541  std::vector<Number> meta_data)
542  {
543  var_numeric_md_[tag] = meta_data;
544  }
545 
547  std::string tag,
548  std::vector<std::string> meta_data)
549  {
550  con_string_md_[tag] = meta_data;
551  }
552 
554  std::string tag,
555  std::vector<Index> meta_data)
556  {
557  con_integer_md_[tag] = meta_data;
558  }
559 
561  std::string tag,
562  std::vector<Number> meta_data)
563  {
564  con_numeric_md_[tag] = meta_data;
565  }
567 
570  {
571  return suffix_handler_;
572  }
573 
574 private:
585 
587 
590  const AmplTNLP&
591  );
592 
594  void operator=(
595  const AmplTNLP&
596  );
598 
599 protected:
602 
604  ASL_pfgh* asl_;
605 
607  double obj_sign_;
608 
611 
613  /* the rest of the problem size data is available easily through the ampl variables */
615 
624 
627 
641 
643  void* Oinfo_ptr_;
644 
646  void* nerror_;
647 
650 
653  const Number* x,
654  Number& obj_val
655  );
656 
659  const Number* x,
660  Index m,
661  Number* g = NULL
662  );
663 
668  bool new_x,
669  Index n,
670  const Number* x
671  );
672 
678  char* get_options(
679  const SmartPtr<OptionsList>& options,
680  SmartPtr<AmplOptionsList>& ampl_options_list,
681  const char* ampl_option_string,
682  const char* ampl_invokation_string,
683  const char* ampl_banner_string,
684  char**& argv
685  );
686 
688  bool nerror_ok(
689  void* nerror
690  );
691 
693  void call_hesset();
694 
702 };
703 
704 } // namespace Ipopt
705 
706 #endif
Ampl Option class containing name, type and description for an AMPL option.
Definition: AmplTNLP.hpp:140
AmplOptionType Type() const
Definition: AmplTNLP.hpp:156
AmplOption()
Default Constructor.
AmplOption(const std::string ipopt_option_name, AmplOptionType type, const std::string description)
const std::string ipopt_option_name_
Definition: AmplTNLP.hpp:187
const std::string & IpoptOptionName() const
Definition: AmplTNLP.hpp:152
void operator=(const AmplOption &)
Default Assignment Operator.
const AmplOptionType type_
Definition: AmplTNLP.hpp:188
AmplOption(const AmplOption &)
Copy Constructor.
const std::string & IpoptName() const
Definition: AmplTNLP.hpp:207
const SmartPtr< const Journalist > jnlst_
Definition: AmplTNLP.hpp:226
const SmartPtr< OptionsList > options_
Definition: AmplTNLP.hpp:225
PrivatInfo(const std::string ipopt_name, SmartPtr< OptionsList > options, SmartPtr< const Journalist > jnlst, void **nerror=NULL)
Definition: AmplTNLP.hpp:195
const SmartPtr< OptionsList > & Options() const
Definition: AmplTNLP.hpp:211
const SmartPtr< const Journalist > & Jnlst() const
Definition: AmplTNLP.hpp:215
Class for storing a number of AMPL options that should be registered to the AMPL Solver library inter...
Definition: AmplTNLP.hpp:127
void PrintLatex(SmartPtr< const Journalist > jnlst)
AmplOptionsList(const AmplOptionsList &)
Default Constructor.
void AddAmplOption(const std::string ampl_option_name, const std::string ipopt_option_name, AmplOptionsList::AmplOptionType type, const std::string description)
Adding a new AMPL Option.
Definition: AmplTNLP.hpp:241
@ WS_Option
this is for AMPL's internal wantsol callback
Definition: AmplTNLP.hpp:134
Index NumberOfAmplOptions()
Number of AMPL Options.
Definition: AmplTNLP.hpp:253
void * keywds_
pointer to the keywords
Definition: AmplTNLP.hpp:309
void PrintDoxygen(SmartPtr< const Journalist > jnlst)
void * Keywords(const SmartPtr< OptionsList > &options, SmartPtr< const Journalist > jnlst, void **nerror)
ASL keywords list for the stored options.
void MakeValidLatexString(std::string source, std::string &dest) const
Index nkeywds_
Number of entries stored in keywds_.
Definition: AmplTNLP.hpp:312
~AmplOptionsList()
Destructor.
void operator=(const AmplOptionsList &)
Default Assignment Operator.
std::map< std::string, SmartPtr< const AmplOption > > ampl_options_map_
map for storing registered AMPL options
Definition: AmplTNLP.hpp:303
AmplOptionsList()
Default Constructor.
Definition: AmplTNLP.hpp:232
std::vector< Suffix_Source > suffix_sources_
Definition: AmplTNLP.hpp:111
std::vector< std::string > suffix_ids_
Definition: AmplTNLP.hpp:109
void operator=(const AmplSuffixHandler &)
Default Assignment Operator.
void PrepareAmplForSuffixes(ASL_pfgh *asl)
Method called by AmplTNLP to prepare the asl for the suffixes.
const Index * GetIntegerSuffixValues(std::string suffix_string, Suffix_Source source) const
const Number * GetNumberSuffixValues(std::string suffix_string, Suffix_Source source) const
void AddAvailableSuffix(std::string suffix_string, Suffix_Source source, Suffix_Type type)
Definition: AmplTNLP.hpp:47
std::vector< Suffix_Type > suffix_types_
Definition: AmplTNLP.hpp:110
AmplSuffixHandler(const AmplSuffixHandler &)
Default Constructor.
std::vector< Index > GetIntegerSuffixValues(Index n, std::string suffix_string, Suffix_Source source) const
std::vector< Number > GetNumberSuffixValues(Index n, std::string suffix_string, Suffix_Source source) const
Ampl Interface, implemented as a TNLP.
Definition: AmplTNLP.hpp:317
void set_numeric_metadata_for_var(std::string tag, std::vector< Number > meta_data)
Definition: AmplTNLP.hpp:539
void * Oinfo_ptr_
Pointer to the Oinfo structure.
Definition: AmplTNLP.hpp:643
void set_string_metadata_for_var(std::string tag, std::vector< std::string > meta_data)
Definition: AmplTNLP.hpp:525
virtual bool eval_jac_g(Index n, const Number *x, bool new_x, Index m, Index nele_jac, Index *iRow, Index *jCol, Number *values)
Method to request either the sparsity structure or the values of the Jacobian of the constraints.
AmplTNLP(const SmartPtr< const Journalist > &jnlst, const SmartPtr< OptionsList > options, char **&argv, SmartPtr< AmplSuffixHandler > suffix_handler=NULL, bool allow_discrete=false, SmartPtr< AmplOptionsList > ampl_options_list=NULL, const char *ampl_option_string=NULL, const char *ampl_invokation_string=NULL, const char *ampl_banner_string=NULL, std::string *nl_file_content=NULL)
Constructor.
SmartPtr< AmplSuffixHandler > suffix_handler_
Suffix Handler.
Definition: AmplTNLP.hpp:649
StringMetaDataMapType con_string_md_
Definition: AmplTNLP.hpp:699
Number * z_L_sol_
Definition: AmplTNLP.hpp:618
virtual Index get_number_of_nonlinear_variables()
Return the number of variables that appear nonlinearly in the objective function or in at least one c...
void set_numeric_metadata_for_con(std::string tag, std::vector< Number > meta_data)
Definition: AmplTNLP.hpp:560
AmplTNLP(const AmplTNLP &)
Copy Constructor.
virtual bool get_nlp_info(Index &n, Index &m, Index &nnz_jac_g, Index &nnz_h_lag, IndexStyleEnum &index_style)
Method to request the initial information about the problem.
Index nz_h_full_
number of nonzeros in the full_x Hessian
Definition: AmplTNLP.hpp:612
IntegerMetaDataMapType var_integer_md_
Definition: AmplTNLP.hpp:697
DECLARE_STD_EXCEPTION(NONPOSITIVE_SCALING_FACTOR)
Exceptions.
StringMetaDataMapType var_string_md_
meta data to pass on to TNLP
Definition: AmplTNLP.hpp:696
bool hesset_called_
whether we have called hesset
Definition: AmplTNLP.hpp:637
bool apply_new_x(bool new_x, Index n, const Number *x)
Internal function to update the internal and ampl state if the x value changes.
AmplTNLP()
Default Constructor.
SmartPtr< const Journalist > jnlst_
Journalist.
Definition: AmplTNLP.hpp:601
virtual void finalize_solution(SolverReturn status, Index n, const Number *x, const Number *z_L, const Number *z_U, Index m, const Number *g, const Number *lambda, Number obj_value, const IpoptData *ip_data, IpoptCalculatedQuantities *ip_cq)
This method is called when the algorithm has finished (successfully or not) so the TNLP can digest th...
Number * x_sol_
Definition: AmplTNLP.hpp:617
virtual bool get_scaling_parameters(Number &obj_scaling, bool &use_x_scaling, Index n, Number *x_scaling, bool &use_g_scaling, Index m, Number *g_scaling)
Method to request scaling parameters.
virtual bool eval_g(Index n, const Number *x, bool new_x, Index m, Number *g)
Method to request the constraint values.
virtual bool eval_grad_f(Index n, const Number *x, bool new_x, Number *grad_f)
Method to request the gradient of the objective function.
NumericMetaDataMapType var_numeric_md_
Definition: AmplTNLP.hpp:698
virtual bool get_list_of_nonlinear_variables(Index num_nonlin_vars, Index *pos_nonlin_vars)
Return the indices of all nonlinear variables.
ASL_pfgh * AmplSolverObject()
Return the ampl solver object (ASL*)
Definition: AmplTNLP.hpp:477
void set_integer_metadata_for_con(std::string tag, std::vector< Index > meta_data)
Definition: AmplTNLP.hpp:553
virtual bool get_starting_point(Index n, bool init_x, Number *x, bool init_z, Number *z_L, Number *z_U, Index m, bool init_lambda, Number *lambda)
Method to request the starting point before iterating.
virtual bool get_constraints_linearity(Index m, LinearityType *const_types)
Method to request the constraints linearity.
NumericMetaDataMapType con_numeric_md_
Definition: AmplTNLP.hpp:701
bool internal_conval(const Number *x, Index m, Number *g=NULL)
Make the constraint call to ampl.
void get_discrete_info(Index &nlvb_, Index &nlvbi_, Index &nlvc_, Index &nlvci_, Index &nlvo_, Index &nlvoi_, Index &nbv_, Index &niv_) const
Give the number of binary and integer variables.
virtual bool eval_h(Index n, const Number *x, bool new_x, Number obj_factor, Index m, const Number *lambda, bool new_lambda, Index nele_hess, Index *iRow, Index *jCol, Number *values)
Method to request either the sparsity structure or the values of the Hessian of the Lagrangian.
virtual ~AmplTNLP()
Default destructor.
void write_solution_file(const std::string &message) const
Write the solution file.
virtual bool eval_f(Index n, const Number *x, bool new_x, Number &obj_value)
Method to request the value of the objective function.
virtual bool get_bounds_info(Index n, Number *x_l, Number *x_u, Index m, Number *g_l, Number *g_u)
returns bounds of the nlp.
bool conval_called_with_current_x_
whether the constraint values have been calculated with the current x set to false in apply_new_x,...
Definition: AmplTNLP.hpp:635
double obj_sign_
Sign of the objective fn (1 for min, -1 for max)
Definition: AmplTNLP.hpp:607
Number * lambda_sol_
Definition: AmplTNLP.hpp:621
void call_hesset()
calls hesset ASL function
Number * g_sol_
Definition: AmplTNLP.hpp:620
void * nerror_
nerror flag passed to ampl calls - set to NULL to halt on error
Definition: AmplTNLP.hpp:646
Number * z_U_sol_
Definition: AmplTNLP.hpp:619
bool objval_called_with_current_x_
whether the objective value has been calculated with the current x
Definition: AmplTNLP.hpp:631
virtual bool get_var_con_metadata(Index n, StringMetaDataMapType &var_string_md, IntegerMetaDataMapType &var_integer_md, NumericMetaDataMapType &var_numeric_md, Index m, StringMetaDataMapType &con_string_md, IntegerMetaDataMapType &con_integer_md, NumericMetaDataMapType &con_numeric_md)
Method to request meta data for the variables and the constraints.
bool internal_objval(const Number *x, Number &obj_val)
Make the objective call to ampl.
void set_string_metadata_for_con(std::string tag, std::vector< std::string > meta_data)
Definition: AmplTNLP.hpp:546
char * get_options(const SmartPtr< OptionsList > &options, SmartPtr< AmplOptionsList > &ampl_options_list, const char *ampl_option_string, const char *ampl_invokation_string, const char *ampl_banner_string, char **&argv)
Method for obtaining the name of the NL file and the options set from AMPL.
bool set_active_objective_called_
whether set_active_objective has been called
Definition: AmplTNLP.hpp:639
void set_integer_metadata_for_var(std::string tag, std::vector< Index > meta_data)
Definition: AmplTNLP.hpp:532
SmartPtr< AmplSuffixHandler > get_suffix_handler()
Method for returning the suffix handler.
Definition: AmplTNLP.hpp:569
void operator=(const AmplTNLP &)
Default Assignment Operator.
IntegerMetaDataMapType con_integer_md_
Definition: AmplTNLP.hpp:700
void set_active_objective(Index obj_no)
A method for setting the index of the objective function to be considered.
bool nerror_ok(void *nerror)
whether the ampl nerror code is ok
ASL_pfgh * asl_
pointer to the main ASL structure
Definition: AmplTNLP.hpp:604
Class for all IPOPT specific calculated quantities.
Class to organize all the data required by the algorithm.
Definition: IpIpoptData.hpp:98
Storing the reference count of all the smart pointers that currently reference it.
Template class for Smart Pointers.
Definition: IpSmartPtr.hpp:172
Base class for all NLP's that use standard triplet matrix form and dense vectors.
Definition: IpTNLP.hpp:48
std::map< std::string, std::vector< Index > > IntegerMetaDataMapType
Definition: IpTNLP.hpp:105
std::map< std::string, std::vector< Number > > NumericMetaDataMapType
Definition: IpTNLP.hpp:106
LinearityType
Linearity-types of variables and constraints.
Definition: IpTNLP.hpp:53
std::map< std::string, std::vector< std::string > > StringMetaDataMapType
Definition: IpTNLP.hpp:104
This file contains a base class for all exceptions and a set of macros to help with exceptions.
SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:665
SolverReturn
enum for the return from the optimize algorithm
Definition: IpAlgTypes.hpp:20
int Index
Type of all indices of vectors, matrices etc.
Definition: IpTypes.hpp:17
double Number
Type of all numbers.
Definition: IpTypes.hpp:15