Constraints.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // Constraints.cpp: implementation of the CostFunction class.
  4. //
  5. // Written By: Matthias Wacker
  6. //
  7. //////////////////////////////////////////////////////////////////////
  8. #include "optimization/Constraints.h"
  9. using namespace opt;
  10. Constraints::Constraints(): m_numOfParameters(0),
  11. m_numOfConstraints(0),
  12. m_hasAnalyticGradient(false),
  13. m_hasAnalyticHessian(false)
  14. {
  15. }
  16. /*
  17. constructor
  18. */
  19. Constraints::Constraints(unsigned int numOfParameters, unsigned int numOfConstraints) :
  20. m_numOfParameters(numOfParameters),
  21. m_numOfConstraints(numOfConstraints),
  22. m_hasAnalyticGradient(false),
  23. m_hasAnalyticHessian(false)
  24. {
  25. m_weights = matrix_type(numOfConstraints,1);
  26. for(int i=0; i < static_cast<int>(numOfConstraints); ++i)
  27. {
  28. m_weights(i,0) = 1;
  29. }
  30. }
  31. /*
  32. copy constructor
  33. */
  34. Constraints::Constraints(const Constraints &con)
  35. {
  36. m_numOfParameters = con.m_numOfParameters;
  37. m_numOfConstraints = con.m_numOfConstraints;
  38. m_hasAnalyticGradient = con.m_hasAnalyticGradient;
  39. m_hasAnalyticHessian = con.m_hasAnalyticHessian;
  40. m_weights = con.m_weights;
  41. }
  42. /*
  43. destructor
  44. */
  45. Constraints::~Constraints()
  46. {
  47. }
  48. /*
  49. return the AnalyticGradient
  50. */
  51. const matrix_type Constraints::getAnalyticGradient(const matrix_type &x)
  52. {
  53. matrix_type tmp(m_numOfParameters, m_numOfConstraints);
  54. return tmp;
  55. }
  56. /*
  57. return the AnalyticGradient
  58. */
  59. const matrix_type Constraints::getAnalyticHessian(const matrix_type &x)
  60. {
  61. matrix_type tmp(m_numOfParameters, m_numOfConstraints * m_numOfParameters);
  62. return tmp;
  63. }
  64. bool Constraints::setWeightVector(const matrix_type weights)
  65. {
  66. /*
  67. dimensions have to match:
  68. */
  69. if(static_cast<unsigned int >(weights.rows()) != m_numOfConstraints || weights.cols() != 1)
  70. {
  71. return false;
  72. }
  73. /*
  74. weights have to be nonnegative
  75. */
  76. for(unsigned int i= 0; i < m_numOfConstraints; i++)
  77. {
  78. if(weights(i,0) < 0)
  79. {
  80. return false;
  81. }
  82. }
  83. /*
  84. everything is fine so far, copy weights and return true
  85. */
  86. m_weights = weights;
  87. return true;
  88. }
  89. matrix_type Constraints::evaluateSub(double lambda)
  90. {
  91. return evaluate(m_x_0 + m_h_0 * lambda);
  92. }
  93. bool Constraints::setX0(const matrix_type &x0)
  94. {
  95. if(x0.rows() == static_cast<int>(m_numOfParameters) && x0.cols() == 1)
  96. {
  97. m_x_0 = x0;
  98. return true;
  99. }
  100. else
  101. return false;
  102. }
  103. bool Constraints::setH0(const matrix_type &H0)
  104. {
  105. if(H0.rows() == static_cast<int>(m_numOfParameters) && H0.cols() == 1)
  106. {
  107. m_h_0 = H0;
  108. return true;
  109. }
  110. else
  111. return false;
  112. }