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 
59  const Index* GetIntegerSuffixValues(
60  std::string suffix_string,
61  Suffix_Source source
62  ) const;
63 
64  const Number* GetNumberSuffixValues(
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:
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 
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:
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 
316 class IPOPTAMPLINTERFACELIB_EXPORT AmplTNLP: public TNLP
317 {
318 public:
320 
322  AmplTNLP(
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 
343  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 
373  virtual bool get_constraints_linearity(
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 
437  virtual bool get_scaling_parameters(
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 
449  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 
466  virtual Index get_number_of_nonlinear_variables();
468  virtual bool get_list_of_nonlinear_variables(
469  Index num_nonlin_vars,
470  Index* pos_nonlin_vars
471  );
473 
475 
477  ASL_pfgh* AmplSolverObject()
478  {
479  return asl_;
480  }
481 
487  void write_solution_file(
488  const std::string& message
489  ) const;
490 
496  void get_discrete_info(
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 
516  void set_active_objective(
517  Index obj_no
518  );
519 
524  void set_string_metadata_for_var(
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:
584 
586  AmplTNLP();
587 
589  AmplTNLP(
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 
610 
613  /* the rest of the problem size data is available easily through the ampl variables */
615 
616  Number* x_sol_;
624 
626 
641 
643  void* Oinfo_ptr_;
644 
646  void* nerror_;
647 
650 
652  bool internal_objval(
653  const Number* x,
654  Number& obj_val
655  );
656 
658  bool internal_conval(
659  const Number* x,
660  Index m,
661  Number* g = NULL
662  );
663 
667  bool apply_new_x(
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
const std::string ipopt_option_name_
Definition: AmplTNLP.hpp:187
std::map< std::string, std::vector< Number > > NumericMetaDataMapType
Definition: IpTNLP.hpp:106
StringMetaDataMapType con_string_md_
Definition: AmplTNLP.hpp:699
SmartPtr< AmplSuffixHandler > get_suffix_handler()
Method for returning the suffix handler.
Definition: AmplTNLP.hpp:569
PrivatInfo(const std::string ipopt_name, SmartPtr< OptionsList > options, SmartPtr< const Journalist > jnlst, void **nerror=NULL)
Definition: AmplTNLP.hpp:195
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:646
this is for AMPL&#39;s internal wantsol callback
Definition: AmplTNLP.hpp:134
Index NumberOfAmplOptions()
Number of AMPL Options.
Definition: AmplTNLP.hpp:253
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 AddAvailableSuffix(std::string suffix_string, Suffix_Source source, Suffix_Type type)
Definition: AmplTNLP.hpp:47
DECLARE_STD_EXCEPTION(FATAL_ERROR_IN_LINEAR_SOLVER)
double Number
Type of all numbers.
Definition: IpTypes.hpp:15
bool set_active_objective_called_
whether set_active_objective has been called
Definition: AmplTNLP.hpp:639
void set_integer_metadata_for_con(std::string tag, std::vector< Index > meta_data)
Definition: AmplTNLP.hpp:553
ASL_pfgh * asl_
pointer to the main ASL structure
Definition: AmplTNLP.hpp:604
bool hesset_called_
whether we have called hesset
Definition: AmplTNLP.hpp:637
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
void set_integer_metadata_for_var(std::string tag, std::vector< Index > meta_data)
Definition: AmplTNLP.hpp:532
IntegerMetaDataMapType con_integer_md_
Definition: AmplTNLP.hpp:700
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:631
const std::string & IpoptName() const
Definition: AmplTNLP.hpp:207
void set_string_metadata_for_con(std::string tag, std::vector< std::string > meta_data)
Definition: AmplTNLP.hpp:546
Number * lambda_sol_
Definition: AmplTNLP.hpp:621
Template class for Smart Pointers.
Definition: IpSmartPtr.hpp:171
Storing the reference count of all the smart pointers that currently reference it.
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
ASL_pfgh * AmplSolverObject()
Return the ampl solver object (ASL*)
Definition: AmplTNLP.hpp:477
IntegerMetaDataMapType var_integer_md_
Definition: AmplTNLP.hpp:697
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:612
Class to organize all the data required by the algorithm.
Definition: IpIpoptData.hpp:97
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
Number * z_U_sol_
Definition: AmplTNLP.hpp:619
int Index
Type of all indices of vectors, matrices etc.
Definition: IpTypes.hpp:17
SmartPtr< const Journalist > jnlst_
Journalist.
Definition: AmplTNLP.hpp:601
void set_numeric_metadata_for_con(std::string tag, std::vector< Number > meta_data)
Definition: AmplTNLP.hpp:560
std::map< std::string, std::vector< Index > > IntegerMetaDataMapType
Definition: IpTNLP.hpp:105
std::map< std::string, std::vector< std::string > > StringMetaDataMapType
Definition: IpTNLP.hpp:104
SmartPtr< const U > ConstPtr(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:665
Ampl Interface, implemented as a TNLP.
Definition: AmplTNLP.hpp:316
void * keywds_
pointer to the keywords
Definition: AmplTNLP.hpp:309
SmartPtr< AmplSuffixHandler > suffix_handler_
Suffix Handler.
Definition: AmplTNLP.hpp:649
Number * g_sol_
Definition: AmplTNLP.hpp:620
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:701
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
Number * z_L_sol_
Definition: AmplTNLP.hpp:618
NumericMetaDataMapType var_numeric_md_
Definition: AmplTNLP.hpp:698
const SmartPtr< OptionsList > options_
Definition: AmplTNLP.hpp:225
StringMetaDataMapType var_string_md_
meta data to pass on to TNLP
Definition: AmplTNLP.hpp:696
std::map< std::string, SmartPtr< const AmplOption > > ampl_options_map_
map for storing registered AMPL options
Definition: AmplTNLP.hpp:303