fun_record.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # ifndef CPPAD_CPPAD_IPOPT_SRC_FUN_RECORD_HPP
  2. # define CPPAD_CPPAD_IPOPT_SRC_FUN_RECORD_HPP
  3. /* --------------------------------------------------------------------------
  4. CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
  5. CppAD is distributed under the terms of the
  6. Eclipse Public License Version 2.0.
  7. This Source Code may also be made available under the following
  8. Secondary License when the conditions for such availability set forth
  9. in the Eclipse Public License, Version 2.0 are satisfied:
  10. GNU General Public License, Version 2.0 or later.
  11. ---------------------------------------------------------------------------- */
  12. # include "cppad_ipopt_nlp.hpp"
  13. // ---------------------------------------------------------------------------
  14. namespace cppad_ipopt {
  15. // ---------------------------------------------------------------------------
  16. /*!
  17. \{
  18. \file fun_record.hpp
  19. \brief Records operation sequence for r_k (u)
  20. */
  21. /*!
  22. Records operation sequence for \f$ r_k (u) \f$ at \f$u = [ J \circ n ] (x)\f$.
  23. \tparam NumVector
  24. is the type of the argumen x. It can either be
  25. <tt>Ipopt::Number*</tt> or
  26. <tt>CppAD::vector<Ipopt::Number></tt>; i.e., <tt>NumberVector</tt>.
  27. \param fg_info
  28. Given a value \f$ u \in {\bf R}^{q[k]} \f$,
  29. fg_info returns the value \f$ r_k (u) \in {\bf R}^{p[k]} \f$.
  30. using the syntax
  31. \verbatim
  32. fg_info->eval_r(k, u);
  33. \endverbatim
  34. No other use is made of fg_info.
  35. \param k
  36. is a value less that K specifying
  37. the index value for k in the evaluation <tt>eval_r</tt>.
  38. \param p
  39. <tt>p[k]</tt> is dimension of the range space for \f$ r_k (u) \f$; i.e.,
  40. \f$ r_k (u) \in {\bf R}^{p(k)} \f$.
  41. \param q
  42. <tt>q[k]</tt> is dimension of the domain space for \f$ r_k (u) \f$; i.e.,
  43. \f$ u \in {\bf R}^{q(k)} \f$.
  44. \param n
  45. is the length of the vector x.
  46. \param x
  47. the length of x is equal to n and the point
  48. \f[
  49. u = [ J \circ n ] (x)
  50. \f]
  51. is the point at which the operation sequence for \f$ r_k \f$ is recorded.
  52. \param J
  53. is a vector with length <tt>q[k]</tt> that projects from \f$ {\bf R}^n \f$
  54. to \f$ {\bf R}^{q[k]} \f$
  55. by selecting an ordered subset of the possible indices
  56. \f$ \{ 0 , \ldots , n-1 \} \f$.
  57. Hence, <tt>0 <= J[j] < n</tt> for <tt>j = 0 , ... , q[k]-1</tt>.
  58. \param r_fun
  59. is the vector of AD function objects which has size size greater than k.
  60. Only the function object <tt>r_fun[k]</tt> is referenced.
  61. The input value of this function object does not matter.
  62. On output it is a recording of the function \f$ r_k (u) \f$
  63. at the value of \f$ u \f$ specified by x and J.
  64. */
  65. template <class NumVector>
  66. void fun_record(
  67. cppad_ipopt_fg_info* fg_info ,
  68. size_t k ,
  69. const SizeVector& p ,
  70. const SizeVector& q ,
  71. size_t n ,
  72. const NumVector& x ,
  73. const SizeVector& J ,
  74. CppAD::vector< CppAD::ADFun<Ipopt::Number> >& r_fun )
  75. { size_t j;
  76. // extract u from x
  77. ADVector u(q[k]);
  78. for(j = 0; j < q[k]; j++)
  79. { // when NDEBUG is not defined, this error should be caught
  80. // during the cppad_ipopt_nlp constructor.
  81. CPPAD_ASSERT_UNKNOWN( J[j] < n );
  82. u[j] = x[ J[j] ];
  83. }
  84. // start the recording
  85. CppAD::Independent(u);
  86. // record the evaulation of r_k (u)
  87. ADVector r_k = fg_info->eval_r(k, u);
  88. CPPAD_ASSERT_KNOWN( r_k.size() == p[k] ,
  89. "cppad_ipopt_nlp: eval_r return value size not equal to p[k]."
  90. );
  91. // stop the recording and store operation sequence in
  92. r_fun[k].Dependent(u, r_k);
  93. }
  94. // ---------------------------------------------------------------------------
  95. } // end namespace cppad_ipopt
  96. // ---------------------------------------------------------------------------
  97. # endif