PFWeightedDim.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @file PFWeightedDim.h
  3. * @brief Parameterized Function: weights for each dimension (Interface + Implementation)
  4. * @author Erik Rodner, Alexander Freytag
  5. */
  6. #ifndef _NICE_PFWEIGHTEDDIMINCLUDE
  7. #define _NICE_PFWEIGHTEDDIMINCLUDE
  8. // STL includes
  9. #include <math.h>
  10. // NICE-core includes
  11. #include <core/vector/VectorT.h>
  12. // NICE-core includes
  13. #include "ParameterizedFunction.h"
  14. namespace NICE {
  15. /**
  16. * @class PFWeightedDim
  17. * @brief Parameterized Function: weights for each dimension
  18. * @author Erik Rodner, Alexander Freytag
  19. */
  20. class PFWeightedDim : public ParameterizedFunction
  21. {
  22. protected:
  23. double upperBound;
  24. double lowerBound;
  25. public:
  26. PFWeightedDim( uint dimension,
  27. double lB = -std::numeric_limits<double>::max(),
  28. double uB = std::numeric_limits<double>::max() ) :
  29. ParameterizedFunction(dimension)
  30. {
  31. upperBound = uB;
  32. lowerBound = lB;
  33. if ( uB < 1.0 )
  34. m_parameters.set(uB);
  35. else
  36. m_parameters.set(1.0);
  37. };
  38. ~PFWeightedDim(){};
  39. double f ( uint index, double x ) const { return m_parameters[index] * m_parameters[index] * x; }
  40. bool isOrderPreserving() const { return true; };
  41. Vector getParameterUpperBounds() const { return NICE::Vector(m_parameters.size(), upperBound); };
  42. Vector getParameterLowerBounds() const { return NICE::Vector(m_parameters.size(), lowerBound); };
  43. void setParameterLowerBounds(const NICE::Vector & _newLowerBounds) { if (_newLowerBounds.size() > 0) lowerBound = _newLowerBounds(0);};
  44. void setParameterUpperBounds(const NICE::Vector & _newUpperBounds) { if (_newUpperBounds.size() > 0) upperBound = _newUpperBounds(0);};
  45. /** Persistent interface */
  46. virtual void restore ( std::istream & is, int format = 0 )
  47. {
  48. if (is.good())
  49. {
  50. is.precision (std::numeric_limits<double>::digits10 + 1);
  51. std::string tmp;
  52. bool b_endOfBlock ( false ) ;
  53. while ( !b_endOfBlock )
  54. {
  55. is >> tmp; // start of block
  56. if ( this->isEndTag( tmp, "PFWeightedDim" ) )
  57. {
  58. b_endOfBlock = true;
  59. continue;
  60. }
  61. tmp = this->removeStartTag ( tmp );
  62. if ( tmp.compare("upperBound") == 0 )
  63. {
  64. is >> upperBound;
  65. is >> tmp; // end of block
  66. tmp = this->removeEndTag ( tmp );
  67. }
  68. else if ( tmp.compare("lowerBound") == 0 )
  69. {
  70. is >> lowerBound;
  71. is >> tmp; // end of block
  72. tmp = this->removeEndTag ( tmp );
  73. }
  74. else if ( tmp.compare("ParameterizedFunction") == 0 )
  75. {
  76. // restore parent object
  77. ParameterizedFunction::restore(is);
  78. }
  79. else
  80. {
  81. std::cerr << "WARNING -- unexpected PFWeightedDim object -- " << tmp << " -- for restoration... aborting" << std::endl;
  82. throw;
  83. }
  84. }
  85. }
  86. else
  87. {
  88. std::cerr << "PFWeightedDim::restore -- InStream not initialized - restoring not possible!" << std::endl;
  89. }
  90. };
  91. virtual void store ( std::ostream & os, int format = 0 ) const
  92. {
  93. if (os.good())
  94. {
  95. // show starting point
  96. os << this->createStartTag( "PFWeightedDim" ) << std::endl;
  97. os.precision (std::numeric_limits<double>::digits10 + 1);
  98. os << this->createStartTag( "upperBound" ) << std::endl;
  99. os << upperBound << std::endl;
  100. os << this->createEndTag( "upperBound" ) << std::endl;
  101. os << this->createStartTag( "lowerBound" ) << std::endl;
  102. os << lowerBound << std::endl;
  103. os << this->createEndTag( "lowerBound" ) << std::endl;
  104. // store parent object
  105. ParameterizedFunction::store(os);
  106. // done
  107. os << this->createEndTag( "PFWeightedDim" ) << std::endl;
  108. }
  109. };
  110. virtual void clear () {};
  111. virtual std::string sayYourName() const {return "weightedDim";};
  112. };
  113. }
  114. #endif