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 
27 {
28 public:
30 
32 
34  {
36  Number_Type
37  };
38 
40  {
44  Problem_Source
45  };
46 
48  const 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 
59  const int* GetIntegerSuffixValues(
60  const std::string& suffix_string,
61  Suffix_Source source
62  ) const;
63 
64  const Number* GetNumberSuffixValues(
65  const std::string& suffix_string,
66  Suffix_Source source
67  ) const;
68 
69  std::vector<int> GetIntegerSuffixValues(
70  Index n,
71  const std::string& suffix_string,
72  Suffix_Source source
73  ) const;
74 
75  std::vector<Number> GetNumberSuffixValues(
76  Index n,
77  const std::string& suffix_string,
78  Suffix_Source source
79  ) const;
80 
81 private:
91 
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 
114  void PrepareAmplForSuffixes(
115  ASL_pfgh* asl
116  );
117 
119  // void RetrieveSuffixesFromAmpl(ASL_pfgh* asl);
120  friend class AmplTNLP;
121 };
122 
127 {
128 public:
130  {
135  HaltOnError_Option
136  };
137 
140  {
141  public:
142  AmplOption(
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:
174 
176  AmplOption();
177 
179  AmplOption(
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 
238  ~AmplOptionsList();
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:
275 
277  //AmplOptionsList();
280  const AmplOptionsList&
281  );
282 
284  void operator=(
285  const AmplOptionsList&
286  );
288 
289  void MakeValidLatexString(
290  std::string source,
291  std::string& dest
292  ) const;
293 
294  void PrintLatex(
296  );
297 
298  void PrintDoxygen(
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 
317 {
318 public:
320 
324  AmplTNLP(
325  const SmartPtr<const Journalist>& jnlst,
326  const SmartPtr<RegisteredOptions> regoptions,
327  const SmartPtr<OptionsList> options,
328  const char* const* argv,
329  SmartPtr<AmplSuffixHandler> suffix_handler = NULL,
330  bool allow_discrete = false,
331  SmartPtr<AmplOptionsList> ampl_options_list = NULL,
332  const char* ampl_option_string = NULL,
333  const char* ampl_invokation_string = NULL,
334  const char* ampl_banner_string = NULL,
335  std::string* nl_file_content = NULL
336  );
337 
343  AmplTNLP(
344  const SmartPtr<const Journalist>& jnlst,
345  const SmartPtr<OptionsList> options,
346  char**& argv,
347  SmartPtr<AmplSuffixHandler> suffix_handler = NULL,
348  bool allow_discrete = false,
349  SmartPtr<AmplOptionsList> ampl_options_list = NULL,
350  const char* ampl_option_string = NULL,
351  const char* ampl_invokation_string = NULL,
352  const char* ampl_banner_string = NULL,
353  std::string* nl_file_content = NULL
354  );
355 
357  virtual ~AmplTNLP();
359 
361  DECLARE_STD_EXCEPTION(NONPOSITIVE_SCALING_FACTOR);
362 
364  virtual bool get_nlp_info(
366  Index& n,
367  Index& m,
368  Index& nnz_jac_g,
369  Index& nnz_h_lag,
370  IndexStyleEnum& index_style
371  );
372 
373  virtual bool get_var_con_metadata(
374  Index n,
375  StringMetaDataMapType& var_string_md,
376  IntegerMetaDataMapType& var_integer_md,
377  NumericMetaDataMapType& var_numeric_md,
378  Index m,
379  StringMetaDataMapType& con_string_md,
380  IntegerMetaDataMapType& con_integer_md,
381  NumericMetaDataMapType& con_numeric_md
382  );
383 
385  virtual bool get_bounds_info(
386  Index n,
387  Number* x_l,
388  Number* x_u,
389  Index m,
390  Number* g_l,
391  Number* g_u
392  );
393 
394  virtual bool get_constraints_linearity(
395  Index m,
396  LinearityType* const_types
397  );
398 
399  virtual bool get_starting_point(
400  Index n,
401  bool init_x,
402  Number* x,
403  bool init_z,
404  Number* z_L,
405  Number* z_U,
406  Index m,
407  bool init_lambda,
408  Number* lambda
409  );
410 
411  virtual bool eval_f(
412  Index n,
413  const Number* x,
414  bool new_x,
415  Number& obj_value
416  );
417 
418  virtual bool eval_grad_f(
419  Index n,
420  const Number* x,
421  bool new_x,
422  Number* grad_f
423  );
424 
425  virtual bool eval_g(
426  Index n,
427  const Number* x,
428  bool new_x,
429  Index m,
430  Number* g
431  );
432 
433  virtual bool eval_jac_g(
434  Index n,
435  const Number* x,
436  bool new_x,
437  Index m,
438  Index nele_jac,
439  Index* iRow,
440  Index* jCol,
441  Number* values
442  );
443 
444  virtual bool eval_h(
445  Index n,
446  const Number* x,
447  bool new_x,
448  Number obj_factor,
449  Index m,
450  const Number* lambda,
451  bool new_lambda,
452  Index nele_hess,
453  Index* iRow,
454  Index* jCol,
455  Number* values
456  );
457 
458  virtual bool get_scaling_parameters(
459  Number& obj_scaling,
460  bool& use_x_scaling,
461  Index n,
462  Number* x_scaling,
463  bool& use_g_scaling,
464  Index m,
465  Number* g_scaling
466  );
468 
470  virtual void finalize_solution(
472  SolverReturn status,
473  Index n,
474  const Number* x,
475  const Number* z_L,
476  const Number* z_U,
477  Index m,
478  const Number* g,
479  const Number* lambda,
480  Number obj_value,
481  const IpoptData* ip_data,
483  );
485 
487  virtual Index get_number_of_nonlinear_variables();
489  virtual bool get_list_of_nonlinear_variables(
490  Index num_nonlin_vars,
491  Index* pos_nonlin_vars
492  );
494 
496 
498  ASL_pfgh* AmplSolverObject()
499  {
500  return asl_;
501  }
502 
507  // ToDo Maybe this should be at a different place, or collect the numbers itself?
508  void write_solution_file(
509  const std::string& message
510  ) const;
511 
517  void get_discrete_info(
518  Index& nlvb_,
519  Index& nlvbi_,
520  Index& nlvc_,
521  Index& nlvci_,
522  Index& nlvo_,
523  Index& nlvoi_,
524  Index& nbv_,
525  Index& niv_
526  ) const;
528 
537  void set_active_objective(
538  Index obj_no
539  );
540 
545  void set_string_metadata_for_var(
547  const std::string& tag,
548  const std::vector<std::string>& meta_data
549  )
550  {
551  var_string_md_[tag] = meta_data;
552  }
553 
555  const std::string& tag,
556  const std::vector<Index>& meta_data
557  )
558  {
559  var_integer_md_[tag] = meta_data;
560  }
561 
563  const std::string& tag,
564  const std::vector<Number>& meta_data
565  )
566  {
567  var_numeric_md_[tag] = meta_data;
568  }
569 
571  const std::string& tag,
572  const std::vector<std::string>& meta_data
573  )
574  {
575  con_string_md_[tag] = meta_data;
576  }
577 
579  const std::string& tag,
580  const std::vector<Index>& meta_data
581  )
582  {
583  con_integer_md_[tag] = meta_data;
584  }
585 
587  const std::string& tag,
588  const std::vector<Number>& meta_data
589  )
590  {
591  con_numeric_md_[tag] = meta_data;
592  }
594 
597  {
598  return suffix_handler_;
599  }
600 
601 private:
611 
613  AmplTNLP();
614 
616  AmplTNLP(
617  const AmplTNLP&
618  );
619 
621  void operator=(
622  const AmplTNLP&
623  );
625 
626  void gutsOfConstructor(
627  const SmartPtr<RegisteredOptions> regoptions,
628  const SmartPtr<OptionsList> options,
629  const char* const* argv,
630  bool allow_discrete /* = false */,
631  SmartPtr<AmplOptionsList> ampl_options_list /* = NULL */,
632  const char* ampl_option_string /* = NULL */,
633  const char* ampl_invokation_string /* = NULL */,
634  const char* ampl_banner_string /* = NULL */,
635  std::string* nl_file_content /* = NULL */
636  );
637 
638 protected:
641 
643  ASL_pfgh* asl_;
644 
647 
649 
652  /* the rest of the problem size data is available easily through the ampl variables */
654 
655  Number* x_sol_;
663 
665 
680 
682  void* Oinfo_ptr_;
683 
685  void* nerror_;
686 
689 
691  bool internal_objval(
692  const Number* x,
693  Number& obj_val
694  );
695 
697  bool internal_conval(
698  const Number* x,
699  Index m,
700  Number* g = NULL
701  );
702 
706  bool apply_new_x(
707  bool new_x,
708  Index n,
709  const Number* x
710  );
711 
717  char* get_options(
718  const SmartPtr<RegisteredOptions> regoptions,
719  const SmartPtr<OptionsList>& options,
720  SmartPtr<AmplOptionsList>& ampl_options_list,
721  const char* ampl_option_string,
722  const char* ampl_invokation_string,
723  const char* ampl_banner_string,
724  const char* const* argv
725  );
726 
733  char* get_options(
734  const SmartPtr<OptionsList>& options,
735  SmartPtr<AmplOptionsList>& ampl_options_list,
736  const char* ampl_option_string,
737  const char* ampl_invokation_string,
738  const char* ampl_banner_string,
739  // cppcheck-suppress constParameter
740  char**& argv
741  )
742  {
743  return get_options(NULL, options, ampl_options_list, ampl_option_string, ampl_invokation_string, ampl_banner_string, argv);
744  }
745 
747  bool nerror_ok(
748  void* nerror
749  );
750 
752  void call_hesset();
753 
761 };
762 
763 } // namespace Ipopt
764 
765 #endif
const std::string ipopt_option_name_
Definition: AmplTNLP.hpp:187
std::map< std::string, std::vector< Number > > NumericMetaDataMapType
Definition: IpTNLP.hpp:72
StringMetaDataMapType con_string_md_
Definition: AmplTNLP.hpp:758
SmartPtr< AmplSuffixHandler > get_suffix_handler()
Method for returning the suffix handler.
Definition: AmplTNLP.hpp:596
AmplOptionType Type() const
Definition: AmplTNLP.hpp:156
Class for all IPOPT specific calculated quantities.
const std::string & IpoptOptionName() const
Definition: AmplTNLP.hpp:152
void * nerror_
nerror flag passed to ampl calls - set to NULL to halt on error
Definition: AmplTNLP.hpp:685
this is for AMPL&#39;s internal wantsol callback
Definition: AmplTNLP.hpp:134
Index NumberOfAmplOptions()
Number of AMPL Options.
Definition: AmplTNLP.hpp:253
void * Oinfo_ptr_
Pointer to the Oinfo structure.
Definition: AmplTNLP.hpp:682
DECLARE_STD_EXCEPTION(FATAL_ERROR_IN_LINEAR_SOLVER)
void set_numeric_metadata_for_con(const std::string &tag, const std::vector< Number > &meta_data)
Definition: AmplTNLP.hpp:586
bool set_active_objective_called_
whether set_active_objective has been called
Definition: AmplTNLP.hpp:678
ASL_pfgh * asl_
pointer to the main ASL structure
Definition: AmplTNLP.hpp:643
bool hesset_called_
whether we have called hesset
Definition: AmplTNLP.hpp:676
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:674
ipindex Index
Type of all indices of vectors, matrices etc.
Definition: IpTypes.hpp:20
IntegerMetaDataMapType con_integer_md_
Definition: AmplTNLP.hpp:759
LinearityType
Linearity-types of variables and constraints.
Definition: IpTNLP.hpp:52
This file contains a base class for all exceptions and a set of macros to help with exceptions...
const SmartPtr< OptionsList > & Options() const
Definition: AmplTNLP.hpp:211
bool objval_called_with_current_x_
whether the objective value has been calculated with the current x
Definition: AmplTNLP.hpp:670
const std::string & IpoptName() const
Definition: AmplTNLP.hpp:207
Number * lambda_sol_
Definition: AmplTNLP.hpp:660
Template class for Smart Pointers.
Definition: IpSmartPtr.hpp:164
Storing the reference count of all the smart pointers that currently reference it.
Number obj_sign_
Sign of the objective fn (1 for min, -1 for max)
Definition: AmplTNLP.hpp:646
void set_string_metadata_for_con(const std::string &tag, const std::vector< std::string > &meta_data)
Definition: AmplTNLP.hpp:570
Ampl Option class containing name, type and description for an AMPL option.
Definition: AmplTNLP.hpp:139
const SmartPtr< const Journalist > jnlst_
Definition: AmplTNLP.hpp:226
SolverReturn
enum for the return from the optimize algorithm
Definition: IpAlgTypes.hpp:19
Index nkeywds_
Number of entries stored in keywds_.
Definition: AmplTNLP.hpp:312
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
ASL_pfgh * AmplSolverObject()
Return the ampl solver object (ASL*)
Definition: AmplTNLP.hpp:498
PrivatInfo(const std::string &ipopt_name, SmartPtr< OptionsList > options, SmartPtr< const Journalist > jnlst, void **nerror=NULL)
Definition: AmplTNLP.hpp:195
IntegerMetaDataMapType var_integer_md_
Definition: AmplTNLP.hpp:756
std::vector< std::string > suffix_ids_
Definition: AmplTNLP.hpp:109
std::vector< Suffix_Type > suffix_types_
Definition: AmplTNLP.hpp:110
Index nz_h_full_
number of nonzeros in the full_x Hessian
Definition: AmplTNLP.hpp:651
Class to organize all the data required by the algorithm.
Definition: IpIpoptData.hpp:97
ipnumber Number
Type of all numbers.
Definition: IpTypes.hpp:17
Number * z_U_sol_
Definition: AmplTNLP.hpp:658
SmartPtr< const Journalist > jnlst_
Journalist.
Definition: AmplTNLP.hpp:640
void AddAvailableSuffix(const std::string &suffix_string, Suffix_Source source, Suffix_Type type)
Definition: AmplTNLP.hpp:47
std::map< std::string, std::vector< Index > > IntegerMetaDataMapType
Definition: IpTNLP.hpp:71
void set_integer_metadata_for_con(const std::string &tag, const std::vector< Index > &meta_data)
Definition: AmplTNLP.hpp:578
std::map< std::string, std::vector< std::string > > StringMetaDataMapType
Definition: IpTNLP.hpp:70
SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:663
Ampl Interface, implemented as a TNLP.
Definition: AmplTNLP.hpp:316
void set_integer_metadata_for_var(const std::string &tag, const std::vector< Index > &meta_data)
Definition: AmplTNLP.hpp:554
void * keywds_
pointer to the keywords
Definition: AmplTNLP.hpp:309
SmartPtr< AmplSuffixHandler > suffix_handler_
Suffix Handler.
Definition: AmplTNLP.hpp:688
void set_numeric_metadata_for_var(const std::string &tag, const std::vector< Number > &meta_data)
Definition: AmplTNLP.hpp:562
Number * g_sol_
Definition: AmplTNLP.hpp:659
Class for storing a number of AMPL options that should be registered to the AMPL Solver library inter...
Definition: AmplTNLP.hpp:126
AmplOptionsList()
Default Constructor.
Definition: AmplTNLP.hpp:232
NumericMetaDataMapType con_numeric_md_
Definition: AmplTNLP.hpp:760
std::vector< Suffix_Source > suffix_sources_
Definition: AmplTNLP.hpp:111
const SmartPtr< const Journalist > & Jnlst() const
Definition: AmplTNLP.hpp:215
Base class for all NLP&#39;s that use standard triplet matrix form and dense vectors. ...
Definition: IpTNLP.hpp:47
const AmplOptionType type_
Definition: AmplTNLP.hpp:188
IPOPT_DEPRECATED 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.
Definition: AmplTNLP.hpp:733
#define IPOPT_DEPRECATED
macro to declare symbols as deprecated
Definition: IpTypes.h:25
Number * z_L_sol_
Definition: AmplTNLP.hpp:657
NumericMetaDataMapType var_numeric_md_
Definition: AmplTNLP.hpp:757
const SmartPtr< OptionsList > options_
Definition: AmplTNLP.hpp:225
#define IPOPTAMPLINTERFACELIB_EXPORT
Definition: config.h:91
StringMetaDataMapType var_string_md_
meta data to pass on to TNLP
Definition: AmplTNLP.hpp:755
std::map< std::string, SmartPtr< const AmplOption > > ampl_options_map_
map for storing registered AMPL options
Definition: AmplTNLP.hpp:303