MyNLP.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (C) 2004, 2006 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: MyNLP.hpp 1861 2010-12-21 21:34:47Z andreasw $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2004-11-05
  8. #ifndef __MYNLP_HPP__
  9. #define __MYNLP_HPP__
  10. #include "IpTNLP.hpp"
  11. using namespace Ipopt;
  12. /** C++ Example NLP for interfacing a problem with IPOPT.
  13. * MyNLP implements a C++ example showing how to interface with IPOPT
  14. * through the TNLP interface. This example is designed to go along with
  15. * the tutorial document (see Examples/CppTutorial/).
  16. * This class implements the following NLP.
  17. *
  18. * min_x f(x) = -(x2-2)^2
  19. * s.t.
  20. * 0 = x1^2 + x2 - 1
  21. * -1 <= x1 <= 1
  22. *
  23. */
  24. class MyNLP : public TNLP
  25. {
  26. public:
  27. /** default constructor */
  28. MyNLP();
  29. /** default destructor */
  30. virtual ~MyNLP();
  31. /**@name Overloaded from TNLP */
  32. //@{
  33. /** Method to return some info about the nlp */
  34. virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
  35. Index& nnz_h_lag, IndexStyleEnum& index_style);
  36. /** Method to return the bounds for my problem */
  37. virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
  38. Index m, Number* g_l, Number* g_u);
  39. /** Method to return the starting point for the algorithm */
  40. virtual bool get_starting_point(Index n, bool init_x, Number* x,
  41. bool init_z, Number* z_L, Number* z_U,
  42. Index m, bool init_lambda,
  43. Number* lambda);
  44. /** Method to return the objective value */
  45. virtual bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value);
  46. /** Method to return the gradient of the objective */
  47. virtual bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f);
  48. /** Method to return the constraint residuals */
  49. virtual bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g);
  50. /** Method to return:
  51. * 1) The structure of the jacobian (if "values" is NULL)
  52. * 2) The values of the jacobian (if "values" is not NULL)
  53. */
  54. virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
  55. Index m, Index nele_jac, Index* iRow, Index *jCol,
  56. Number* values);
  57. /** Method to return:
  58. * 1) The structure of the hessian of the lagrangian (if "values" is NULL)
  59. * 2) The values of the hessian of the lagrangian (if "values" is not NULL)
  60. */
  61. virtual bool eval_h(Index n, const Number* x, bool new_x,
  62. Number obj_factor, Index m, const Number* lambda,
  63. bool new_lambda, Index nele_hess, Index* iRow,
  64. Index* jCol, Number* values);
  65. //@}
  66. /** @name Solution Methods */
  67. //@{
  68. /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
  69. virtual void finalize_solution(SolverReturn status,
  70. Index n, const Number* x, const Number* z_L, const Number* z_U,
  71. Index m, const Number* g, const Number* lambda,
  72. Number obj_value,
  73. const IpoptData* ip_data,
  74. IpoptCalculatedQuantities* ip_cq);
  75. //@}
  76. private:
  77. /**@name Methods to block default compiler methods.
  78. * The compiler automatically generates the following three methods.
  79. * Since the default compiler implementation is generally not what
  80. * you want (for all but the most simple classes), we usually
  81. * put the declarations of these methods in the private section
  82. * and never implement them. This prevents the compiler from
  83. * implementing an incorrect "default" behavior without us
  84. * knowing. (See Scott Meyers book, "Effective C++")
  85. *
  86. */
  87. //@{
  88. // MyNLP();
  89. MyNLP(const MyNLP&);
  90. MyNLP& operator=(const MyNLP&);
  91. //@}
  92. };
  93. #endif