DimWrapperCostFunction.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. using namespace OPTIMIZATION;
  11. DimWrapperCostFunction::DimWrapperCostFunction() : SuperClass()
  12. {
  13. m_pOrigCostFunc=NULL;
  14. m_numOfParameters=0;
  15. }
  16. DimWrapperCostFunction::DimWrapperCostFunction(const DimWrapperCostFunction &func) : SuperClass(func)
  17. {
  18. m_pOrigCostFunc= func.m_pOrigCostFunc;
  19. m_selMatTransposed = func.m_selMatTransposed;
  20. m_fixedValues = func.m_fixedValues;
  21. }
  22. DimWrapperCostFunction::~DimWrapperCostFunction()
  23. {
  24. // nothing dynamically allocated..
  25. }
  26. DimWrapperCostFunction::DimWrapperCostFunction(CostFunction *orig, const matrix_type &selectionVector,
  27. const matrix_type &fixedValues)
  28. {
  29. if(orig == NULL)
  30. {
  31. std::cerr << "DimWrapperCostFunction::DimWrapperCostFunction NULL pointer error" << std::endl;
  32. exit(1);
  33. }
  34. m_pOrigCostFunc = orig;
  35. changeSelection(selectionVector,fixedValues);
  36. }
  37. void DimWrapperCostFunction::changeSelection(const matrix_type & selectionVector, const matrix_type &fixedValues)
  38. {
  39. // BUILD MATRIX
  40. //count nonzero values:
  41. m_fixedValues=fixedValues;
  42. int count=0;
  43. int dim = selectionVector.rows();
  44. std::vector<int> indices;
  45. for(int i=0; i < dim ; ++i)
  46. {
  47. if (selectionVector(i,0) != 0.0 )
  48. {
  49. count++;
  50. indices.push_back(i);
  51. m_fixedValues(i,0)= 0.0; // resets fixed values to zero for active params
  52. }
  53. }
  54. m_numOfParameters = count;
  55. m_selMatTransposed = matrix_type(dim,count);
  56. for(int i =0; i < (int)m_selMatTransposed.rows(); ++i)
  57. {
  58. for(int j =0; j < (int)m_selMatTransposed.cols(); ++j)
  59. {
  60. if( indices[j]==i )
  61. {
  62. m_selMatTransposed(i,j)=1.0;
  63. }
  64. else
  65. {
  66. m_selMatTransposed(i,j)=0.0;
  67. }
  68. }
  69. }
  70. }
  71. DimWrapperCostFunction &DimWrapperCostFunction::operator=(const DimWrapperCostFunction &func)
  72. {
  73. /*
  74. avoid self-copy
  75. */
  76. if(this != &func)
  77. {
  78. /*
  79. =operator of SuperClass
  80. */
  81. SuperClass::operator=(func);
  82. /*
  83. own values:
  84. */
  85. m_pOrigCostFunc= func.m_pOrigCostFunc;
  86. m_selMatTransposed = func.m_selMatTransposed;
  87. m_fixedValues = func.m_fixedValues;
  88. }
  89. return *this;
  90. }
  91. void DimWrapperCostFunction::init()
  92. {
  93. m_pOrigCostFunc->init();
  94. }
  95. double DimWrapperCostFunction::evaluate(const matrix_type &x)
  96. {
  97. return m_pOrigCostFunc->evaluate(m_selMatTransposed * x + m_fixedValues);
  98. }
  99. OPTIMIZATION::matrix_type DimWrapperCostFunction::getFullParamsFromSubParams(const OPTIMIZATION::matrix_type &x)
  100. {
  101. return m_selMatTransposed * x + m_fixedValues;
  102. }