Constraints.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // Constraints.h: interface of the Constaints class.
  4. //
  5. // Written By: Matthias Wacker
  6. //
  7. //////////////////////////////////////////////////////////////////////
  8. #ifndef _CONSTRAINTS_H_
  9. #define _CONSTRAINTS_H_
  10. #include "core/optimization/blackbox/Definitions_core_opt.h"
  11. namespace opt=OPTIMIZATION;
  12. /*!
  13. class Abstract base class for all constraints.
  14. */
  15. class Constraints
  16. {
  17. public:
  18. typedef OPTIMIZATION::matrix_type matrix_type;
  19. /*!
  20. default constructor
  21. */
  22. Constraints();
  23. /*!
  24. Constructor.
  25. \param numOfParameters
  26. \param numOfConstraints
  27. */
  28. Constraints(unsigned int numOfParameters, unsigned int numOfConstraints);
  29. /*
  30. Copy constructor
  31. \param con the constraint to copy
  32. */
  33. Constraints(const Constraints &con);
  34. /*!
  35. Destructor.
  36. */
  37. virtual ~Constraints();
  38. /*!
  39. get Number of Parameters
  40. \return the number of parameters
  41. */
  42. inline unsigned int getNumOfParameters(){return m_numOfParameters;};
  43. /*!
  44. get Number of Constraints
  45. \return the number of Constraints
  46. */
  47. inline unsigned int getNumOfConstraints(){return m_numOfConstraints;};
  48. /*!
  49. do the constraints provide analytic gradient computation?
  50. \return true if they do
  51. */
  52. inline bool hasAnalyticGradient(){return m_hasAnalyticGradient;};
  53. /*!
  54. get the analytic gradient
  55. \param x =^ position
  56. \return matrix_type =[gradConstraint(i,j)]_{i,j=(0,0)}^{(m_numOfParameters,m_numOfConstraints)}
  57. where
  58. (i,j) indicates the i-th component
  59. of the j-th Constraint
  60. */
  61. virtual const matrix_type getAnalyticGradient(const matrix_type &x);
  62. /*!
  63. do the constraints provide analytic hessian computation?
  64. \return true if they do
  65. */
  66. inline bool hasAnalyticHessian(){return m_hasAnalyticHessian;};
  67. /*!
  68. get the analytic hessian(s)
  69. \param x =^ position
  70. \return matrix_type =[hessianConstraint(i,j)]_{i,j=(0,0)}^{(m_numOfParameters,
  71. m_numOfParameters * m_numOfConstraints)}
  72. where
  73. (i,j) indicates the (i,[j%m_numOfConstraints])-th component
  74. of the Hessian of the ground(j/m_numOfConstraints)-th Constraint
  75. where gound means cuttung the number after '.'
  76. example:
  77. 2 dimensional parameters
  78. 3 constraints
  79. return= [H_1(i,j), H_2(i,j), H_3(i,j)],
  80. where H_v are matrices of 2x2:
  81. [h_1_1,1 h_1_1,2 h_2_1,1 h_2_1,2 h_3_1,1 h_3_1,2
  82. h_1_2,1 h_1_2,2 h_2_2,1 h_2_2,2 h_3_2,1 h_3_2,2 ]
  83. */
  84. virtual const matrix_type getAnalyticHessian(const matrix_type & x);
  85. /*!
  86. weight handling: setWeightVector
  87. Set a weight vector for normalization of the constraint ranges. The value of the i-th constraint,
  88. gets multiplied with the i-th value of the weight vector.
  89. The weights have te be non-negative, but can be 0 to disable the i-th constraint
  90. The default is [1,1, ... , 1] what weights the constraints equally.
  91. \param weight
  92. \returns true, if everything went fine
  93. */
  94. bool setWeightVector(const matrix_type weights);
  95. /*!
  96. get the weight vector
  97. */
  98. inline matrix_type getWeightVector(){return m_weights;};
  99. /*!
  100. Initialization for the cost function
  101. */
  102. virtual void init() = 0;
  103. /*!
  104. evaluate the constraints
  105. \param x =^ position
  106. \return matrix_type containing the i-th constraint result in the i-th component
  107. */
  108. virtual matrix_type evaluate(const matrix_type & x) = 0;
  109. /*!
  110. set an x0 for 1dim line search
  111. \param x0 affine part of affine linear parameter space
  112. */
  113. bool setX0(const matrix_type &x0);
  114. /*!
  115. set an h0 for 1dim line search
  116. \param h0 linear part of affine linear parameter space
  117. */
  118. bool setH0(const matrix_type &H0);
  119. /*!
  120. evaluate 1dimension sub function fsub(lambda) = f(x0 + lambda * h0)
  121. \param lambda
  122. */
  123. matrix_type evaluateSub(double lambda);
  124. protected:
  125. /*!
  126. number of parameters
  127. */
  128. unsigned int m_numOfParameters;
  129. /*!
  130. number of constraints
  131. */
  132. unsigned int m_numOfConstraints;
  133. /*!
  134. has analytic gradient ?
  135. */
  136. bool m_hasAnalyticGradient;
  137. /*!
  138. has analytic hessian ?
  139. */
  140. bool m_hasAnalyticHessian;
  141. /*!
  142. internal weight vector
  143. */
  144. matrix_type m_weights;
  145. /*!
  146. x_0
  147. */
  148. matrix_type m_x_0;
  149. /*!
  150. direction h_0
  151. */
  152. matrix_type m_h_0;
  153. };
  154. #endif