DimWrapperCostFunction.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // DimWrapperCostFunction.cpp: implementation of the DimWrapperCostFunction class.
  4. //
  5. // Written By: Matthias Wacker
  6. //
  7. //////////////////////////////////////////////////////////////////////
  8. #include <iostream>
  9. #include "DimWrapperCostFunction.h"
  10. DimWrapperCostFunction::DimWrapperCostFunction() : SuperClass()
  11. {
  12. m_pOrigCostFunc=NULL;
  13. m_numOfParameters=0;
  14. }
  15. DimWrapperCostFunction::DimWrapperCostFunction(const DimWrapperCostFunction &func) : SuperClass(func)
  16. {
  17. m_pOrigCostFunc= func.m_pOrigCostFunc;
  18. m_selMatTransposed = func.m_selMatTransposed;
  19. m_fixedValues = func.m_fixedValues;
  20. }
  21. DimWrapperCostFunction::~DimWrapperCostFunction()
  22. {
  23. // nothing dynamically allocated..
  24. }
  25. DimWrapperCostFunction::DimWrapperCostFunction(CostFunction *orig, const matrix_type &selectionVector,
  26. const matrix_type &fixedValues)
  27. {
  28. if(orig == NULL)
  29. {
  30. std::cerr << "DimWrapperCostFunction::DimWrapperCostFunction NULL pointer error" << std::endl;
  31. exit(1);
  32. }
  33. m_pOrigCostFunc = orig;
  34. changeSelection(selectionVector,fixedValues);
  35. }
  36. void DimWrapperCostFunction::changeSelection(const matrix_type & selectionVector, const matrix_type &fixedValues)
  37. {
  38. // BUILD MATRIX
  39. //count nonzero values:
  40. m_fixedValues=fixedValues;
  41. int count=0;
  42. int dim = selectionVector.rows();
  43. std::vector<int> indices;
  44. for(int i=0; i < dim ; ++i)
  45. {
  46. if (selectionVector[i][0] != 0.0 )
  47. {
  48. count++;
  49. indices.push_back(i);
  50. m_fixedValues[i][0]= 0.0; // resets fixed values to zero for active params
  51. }
  52. }
  53. m_numOfParameters = count;
  54. m_selMatTransposed = matrix_type(dim,count);
  55. for(int i =0; i < m_selMatTransposed.rows(); ++i)
  56. {
  57. for(int j =0; j < m_selMatTransposed.cols(); ++j)
  58. {
  59. if( indices[j]==i )
  60. {
  61. m_selMatTransposed[i][j]=1.0;
  62. }
  63. else
  64. {
  65. m_selMatTransposed[i][j]=0.0;
  66. }
  67. }
  68. }
  69. }
  70. DimWrapperCostFunction &DimWrapperCostFunction::operator=(const DimWrapperCostFunction &func)
  71. {
  72. /*
  73. avoid self-copy
  74. */
  75. if(this != &func)
  76. {
  77. /*
  78. =operator of SuperClass
  79. */
  80. SuperClass::operator=(func);
  81. /*
  82. own values:
  83. */
  84. m_pOrigCostFunc= func.m_pOrigCostFunc;
  85. m_selMatTransposed = func.m_selMatTransposed;
  86. m_fixedValues = func.m_fixedValues;
  87. }
  88. return *this;
  89. }
  90. void DimWrapperCostFunction::init()
  91. {
  92. m_pOrigCostFunc->init();
  93. }
  94. double DimWrapperCostFunction::evaluate(const matrix_type &x)
  95. {
  96. return m_pOrigCostFunc->evaluate(m_selMatTransposed * x + m_fixedValues);
  97. }
  98. opt::matrix_type DimWrapperCostFunction::getFullParamsFromSubParams(const opt::matrix_type &x)
  99. {
  100. return m_selMatTransposed * x + m_fixedValues;
  101. }
  102. /*bool DimWrapperCostFunction::setX0(const opt::matrix_type &x0)
  103. {
  104. cout<<"dimWrapperCF - setX0"<<endl;
  105. if(x0.rows() == static_cast<int>(m_numOfParameters) && x0.cols() == 1)
  106. {
  107. cout<<"set m_x_0"<<endl;
  108. m_x_0 = x0;
  109. return true;
  110. }
  111. else
  112. return false;
  113. }
  114. bool DimWrapperCostFunction::setH0(const opt::matrix_type &H0)
  115. {
  116. cout<<"dimWrapperCF - setH0"<<endl;
  117. if(H0.rows() == static_cast<int>(m_numOfParameters) && H0.cols() == 1)
  118. {
  119. m_h_0= H0;
  120. return true;
  121. }
  122. else
  123. return false;
  124. }
  125. double DimWrapperCostFunction::evaluateSub(double lambda)
  126. {
  127. cout<<"dimWrapperCF - evalSub"<<endl;
  128. double eval= evaluate(m_x_0 + m_h_0 * lambda);
  129. return eval;
  130. }*/