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  bool checkinterrupt = false
337  );
338 
344  AmplTNLP(
345  const SmartPtr<const Journalist>& jnlst,
346  const SmartPtr<OptionsList> options,
347  char**& argv,
348  SmartPtr<AmplSuffixHandler> suffix_handler = NULL,
349  bool allow_discrete = false,
350  SmartPtr<AmplOptionsList> ampl_options_list = NULL,
351  const char* ampl_option_string = NULL,
352  const char* ampl_invokation_string = NULL,
353  const char* ampl_banner_string = NULL,
354  std::string* nl_file_content = NULL
355  );
356 
358  virtual ~AmplTNLP();
360 
362  DECLARE_STD_EXCEPTION(NONPOSITIVE_SCALING_FACTOR);
363 
365  virtual bool get_nlp_info(
367  Index& n,
368  Index& m,
369  Index& nnz_jac_g,
370  Index& nnz_h_lag,
371  IndexStyleEnum& index_style
372  );
373 
374  virtual bool get_var_con_metadata(
375  Index n,
376  StringMetaDataMapType& var_string_md,
377  IntegerMetaDataMapType& var_integer_md,
378  NumericMetaDataMapType& var_numeric_md,
379  Index m,
380  StringMetaDataMapType& con_string_md,
381  IntegerMetaDataMapType& con_integer_md,
382  NumericMetaDataMapType& con_numeric_md
383  );
384 
386  virtual bool get_bounds_info(
387  Index n,
388  Number* x_l,
389  Number* x_u,
390  Index m,
391  Number* g_l,
392  Number* g_u
393  );
394 
395  virtual bool get_constraints_linearity(
396  Index m,
397  LinearityType* const_types
398  );
399 
400  virtual bool get_starting_point(
401  Index n,
402  bool init_x,
403  Number* x,
404  bool init_z,
405  Number* z_L,
406  Number* z_U,
407  Index m,
408  bool init_lambda,
409  Number* lambda
410  );
411 
412  virtual bool eval_f(
413  Index n,
414  const Number* x,
415  bool new_x,
416  Number& obj_value
417  );
418 
419  virtual bool eval_grad_f(
420  Index n,
421  const Number* x,
422  bool new_x,
423  Number* grad_f
424  );
425 
426  virtual bool eval_g(
427  Index n,
428  const Number* x,
429  bool new_x,
430  Index m,
431  Number* g
432  );
433 
434  virtual bool eval_jac_g(
435  Index n,
436  const Number* x,
437  bool new_x,
438  Index m,
439  Index nele_jac,
440  Index* iRow,
441  Index* jCol,
442  Number* values
443  );
444 
445  virtual bool eval_h(
446  Index n,
447  const Number* x,
448  bool new_x,
449  Number obj_factor,
450  Index m,
451  const Number* lambda,
452  bool new_lambda,
453  Index nele_hess,
454  Index* iRow,
455  Index* jCol,
456  Number* values
457  );
458 
459  virtual bool get_scaling_parameters(
460  Number& obj_scaling,
461  bool& use_x_scaling,
462  Index n,
463  Number* x_scaling,
464  bool& use_g_scaling,
465  Index m,
466  Number* g_scaling
467  );
469 
471  virtual bool intermediate_callback(
473  AlgorithmMode mode,
474  Index iter,
475  Number obj_value,
476  Number inf_pr,
477  Number inf_du,
478  Number mu,
479  Number d_norm,
480  Number regularization_size,
481  Number alpha_du,
482  Number alpha_pr,
483  Index ls_trials,
484  const IpoptData* ip_data,
486  );
487 
488  virtual void finalize_solution(
489  SolverReturn status,
490  Index n,
491  const Number* x,
492  const Number* z_L,
493  const Number* z_U,
494  Index m,
495  const Number* g,
496  const Number* lambda,
497  Number obj_value,
498  const IpoptData* ip_data,
500  );
502 
504  virtual Index get_number_of_nonlinear_variables();
506  virtual bool get_list_of_nonlinear_variables(
507  Index num_nonlin_vars,
508  Index* pos_nonlin_vars
509  );
511 
513 
515  ASL_pfgh* AmplSolverObject()
516  {
517  return asl_;
518  }
519 
524  // ToDo Maybe this should be at a different place, or collect the numbers itself?
525  void write_solution_file(
526  const std::string& message
527  ) const;
528 
534  void get_discrete_info(
535  Index& nlvb_,
536  Index& nlvbi_,
537  Index& nlvc_,
538  Index& nlvci_,
539  Index& nlvo_,
540  Index& nlvoi_,
541  Index& nbv_,
542  Index& niv_
543  ) const;
545 
554  void set_active_objective(
555  Index obj_no
556  );
557 
562  void set_string_metadata_for_var(
564  const std::string& tag,
565  const std::vector<std::string>& meta_data
566  )
567  {
568  var_string_md_[tag] = meta_data;
569  }
570 
572  const std::string& tag,
573  const std::vector<Index>& meta_data
574  )
575  {
576  var_integer_md_[tag] = meta_data;
577  }
578 
580  const std::string& tag,
581  const std::vector<Number>& meta_data
582  )
583  {
584  var_numeric_md_[tag] = meta_data;
585  }
586 
588  const std::string& tag,
589  const std::vector<std::string>& meta_data
590  )
591  {
592  con_string_md_[tag] = meta_data;
593  }
594 
596  const std::string& tag,
597  const std::vector<Index>& meta_data
598  )
599  {
600  con_integer_md_[tag] = meta_data;
601  }
602 
604  const std::string& tag,
605  const std::vector<Number>& meta_data
606  )
607  {
608  con_numeric_md_[tag] = meta_data;
609  }
611 
614  {
615  return suffix_handler_;
616  }
617 
618 private:
628 
630  AmplTNLP();
631 
633  AmplTNLP(
634  const AmplTNLP&
635  );
636 
638  void operator=(
639  const AmplTNLP&
640  );
642 
643  void gutsOfConstructor(
644  const SmartPtr<RegisteredOptions> regoptions,
645  const SmartPtr<OptionsList> options,
646  const char* const* argv,
647  bool allow_discrete /* = false */,
648  SmartPtr<AmplOptionsList> ampl_options_list /* = NULL */,
649  const char* ampl_option_string /* = NULL */,
650  const char* ampl_invokation_string /* = NULL */,
651  const char* ampl_banner_string /* = NULL */,
652  std::string* nl_file_content /* = NULL */
653  );
654 
655 protected:
658 
660  ASL_pfgh* asl_;
661 
664 
666 
669  /* the rest of the problem size data is available easily through the ampl variables */
671 
672  Number* x_sol_;
680 
682 
697 
699  void* Oinfo_ptr_;
700 
702  void* nerror_;
703 
706 
711 
713  bool internal_objval(
714  const Number* x,
715  Number& obj_val
716  );
717 
719  bool internal_conval(
720  const Number* x,
721  Index m,
722  Number* g = NULL
723  );
724 
728  bool apply_new_x(
729  bool new_x,
730  Index n,
731  const Number* x
732  );
733 
739  char* get_options(
740  const SmartPtr<RegisteredOptions> regoptions,
741  const SmartPtr<OptionsList>& options,
742  SmartPtr<AmplOptionsList>& ampl_options_list,
743  const char* ampl_option_string,
744  const char* ampl_invokation_string,
745  const char* ampl_banner_string,
746  const char* const* argv
747  );
748 
755  char* get_options(
756  const SmartPtr<OptionsList>& options,
757  SmartPtr<AmplOptionsList>& ampl_options_list,
758  const char* ampl_option_string,
759  const char* ampl_invokation_string,
760  const char* ampl_banner_string,
761  // cppcheck-suppress constParameter
762  char**& argv
763  )
764  {
765  return get_options(NULL, options, ampl_options_list, ampl_option_string, ampl_invokation_string, ampl_banner_string, argv);
766  }
767 
769  bool nerror_ok(
770  void* nerror
771  );
772 
774  void call_hesset();
775 
783 };
784 
785 } // namespace Ipopt
786 
787 #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:780
SmartPtr< AmplSuffixHandler > get_suffix_handler()
Method for returning the suffix handler.
Definition: AmplTNLP.hpp:613
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:702
this is for AMPL&#39;s internal wantsol callback
Definition: AmplTNLP.hpp:134
Index NumberOfAmplOptions()
Number of AMPL Options.
Definition: AmplTNLP.hpp:253
bool interrupted_
pointer to flag to check whether to interrupt signal was set
Definition: AmplTNLP.hpp:710
void * Oinfo_ptr_
Pointer to the Oinfo structure.
Definition: AmplTNLP.hpp:699
AlgorithmMode
enum to indicate the mode in which the algorithm is
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:603
bool set_active_objective_called_
whether set_active_objective has been called
Definition: AmplTNLP.hpp:695
ASL_pfgh * asl_
pointer to the main ASL structure
Definition: AmplTNLP.hpp:660
bool hesset_called_
whether we have called hesset
Definition: AmplTNLP.hpp:693
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:691
ipindex Index
Type of all indices of vectors, matrices etc.
Definition: IpTypes.hpp:20
IntegerMetaDataMapType con_integer_md_
Definition: AmplTNLP.hpp:781
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:687
const std::string & IpoptName() const
Definition: AmplTNLP.hpp:207
Number * lambda_sol_
Definition: AmplTNLP.hpp:677
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:663
void set_string_metadata_for_con(const std::string &tag, const std::vector< std::string > &meta_data)
Definition: AmplTNLP.hpp:587
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:515
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:778
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:668
Class to organize all the data required by the algorithm.
bool checkinterrupt_
whether to register handler for interrupt signals
Definition: AmplTNLP.hpp:708
ipnumber Number
Type of all numbers.
Definition: IpTypes.hpp:17
Number * z_U_sol_
Definition: AmplTNLP.hpp:675
SmartPtr< const Journalist > jnlst_
Journalist.
Definition: AmplTNLP.hpp:657
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:595
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:664
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:571
void * keywds_
pointer to the keywords
Definition: AmplTNLP.hpp:309
SmartPtr< AmplSuffixHandler > suffix_handler_
Suffix Handler.
Definition: AmplTNLP.hpp:705
void set_numeric_metadata_for_var(const std::string &tag, const std::vector< Number > &meta_data)
Definition: AmplTNLP.hpp:579
Number * g_sol_
Definition: AmplTNLP.hpp:676
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:782
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:755
#define IPOPT_DEPRECATED
macro to declare symbols as deprecated
Definition: IpTypes.h:25
Number * z_L_sol_
Definition: AmplTNLP.hpp:674
NumericMetaDataMapType var_numeric_md_
Definition: AmplTNLP.hpp:779
const SmartPtr< OptionsList > options_
Definition: AmplTNLP.hpp:225
#define IPOPTAMPLINTERFACELIB_EXPORT
Definition: config.h:85
StringMetaDataMapType var_string_md_
meta data to pass on to TNLP
Definition: AmplTNLP.hpp:777
std::map< std::string, SmartPtr< const AmplOption > > ampl_options_map_
map for storing registered AMPL options
Definition: AmplTNLP.hpp:303