PFMKL.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * @file PFMKL.h
  3. * @brief Parameterized Function: weights for Multiple Kernel Learning approach (Interface + Implementation)
  4. * @author Alexander Freytag
  5. */
  6. #ifndef _NICE_PFMULTIPLEKERNELLEARNINGINCLUDE
  7. #define _NICE_PFMULTIPLEKERNELLEARNINGINCLUDE
  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 PFMKL
  17. * @brief Parameterized Function: weights for Multiple Kernel Learning approach
  18. * @author Alexander Freytag
  19. */
  20. class PFMKL : public ParameterizedFunction
  21. {
  22. protected:
  23. double upperBound;
  24. double lowerBound;
  25. std::set<int> steps;
  26. public:
  27. PFMKL( const std::set<int> & _steps,
  28. double lB = -std::numeric_limits<double>::max(),
  29. double uB = std::numeric_limits<double>::max() ) :
  30. ParameterizedFunction(_steps.size()+1)
  31. {
  32. upperBound = uB;
  33. lowerBound = std::max( lB, 0.0 );
  34. if ( uB < 1.0 )
  35. m_parameters.set(uB);
  36. else
  37. m_parameters.set(1.0);
  38. steps = _steps;
  39. };
  40. ~PFMKL(){};
  41. double f ( uint index, double x ) const
  42. {
  43. int dummyCnt ( 0 );
  44. for (std::set<int>::const_iterator it = steps.begin(); it != steps.end(); it++, dummyCnt++)
  45. {
  46. if ( (int)index < *it)
  47. return x * m_parameters[dummyCnt];
  48. }
  49. //default value, should never be reached
  50. return 0.0;
  51. }
  52. bool isOrderPreserving() const { return true; };
  53. Vector getParameterUpperBounds() const { return NICE::Vector(m_parameters.size(), upperBound); };
  54. Vector getParameterLowerBounds() const { return NICE::Vector(m_parameters.size(), lowerBound); };
  55. void setParameterLowerBounds(const NICE::Vector & _newLowerBounds) { if (_newLowerBounds.size() > 0) lowerBound = _newLowerBounds(0);};
  56. void setParameterUpperBounds(const NICE::Vector & _newUpperBounds) { if (_newUpperBounds.size() > 0) upperBound = _newUpperBounds(0);};
  57. /** Persistent interface */
  58. virtual void restore ( std::istream & is, int format = 0 )
  59. {
  60. if (is.good())
  61. {
  62. is.precision (std::numeric_limits<double>::digits10 + 1);
  63. std::string tmp;
  64. bool b_endOfBlock ( false ) ;
  65. while ( !b_endOfBlock )
  66. {
  67. is >> tmp; // start of block
  68. if ( this->isEndTag( tmp, "PFMKL" ) )
  69. {
  70. b_endOfBlock = true;
  71. continue;
  72. }
  73. tmp = this->removeStartTag ( tmp );
  74. if ( tmp.compare("upperBound") == 0 )
  75. {
  76. is >> upperBound;
  77. is >> tmp; // end of block
  78. tmp = this->removeEndTag ( tmp );
  79. }
  80. else if ( tmp.compare("lowerBound") == 0 )
  81. {
  82. is >> lowerBound;
  83. is >> tmp; // end of block
  84. tmp = this->removeEndTag ( tmp );
  85. }
  86. else if ( tmp.compare("steps") == 0 )
  87. {
  88. is >> tmp; // start of block
  89. int numberOfSteps;
  90. if ( ! this->isStartTag( tmp, "numberOfSteps" ) )
  91. {
  92. std::cerr << "Attempt to restore PFMKL, but found no information about numberOfSteps elements. Aborting..." << std::endl;
  93. throw;
  94. }
  95. else
  96. {
  97. is >> numberOfSteps;
  98. is >> tmp; // end of block
  99. tmp = this->removeEndTag ( tmp );
  100. }
  101. is >> tmp; // start of block
  102. if ( ! this->isStartTag( tmp, "stepInfo" ) )
  103. {
  104. std::cerr << "Attempt to restore PFMKL, but found no stepInfo. Aborting..." << std::endl;
  105. throw;
  106. }
  107. else
  108. {
  109. steps.clear();
  110. for ( int tmpCnt = 0; tmpCnt < numberOfSteps; tmpCnt++)
  111. {
  112. int tmpStep;
  113. is >> tmpStep;
  114. steps.insert ( tmpStep );
  115. }
  116. is >> tmp; // end of block
  117. tmp = this->removeEndTag ( tmp );
  118. }
  119. is >> tmp; // end of block
  120. tmp = this->removeEndTag ( tmp );
  121. is >> tmp; // end of block
  122. tmp = this->removeEndTag ( tmp );
  123. }
  124. else if ( tmp.compare("ParameterizedFunction") == 0 )
  125. {
  126. // restore parent object
  127. ParameterizedFunction::restore(is);
  128. }
  129. else
  130. {
  131. std::cerr << "WARNING -- unexpected PFMKL object -- " << tmp << " -- for restoration... aborting" << std::endl;
  132. throw;
  133. }
  134. }
  135. // restore parent object
  136. ParameterizedFunction::restore(is);
  137. }
  138. else
  139. {
  140. std::cerr << "PFMKL::restore -- InStream not initialized - restoring not possible!" << std::endl;
  141. }
  142. };
  143. virtual void store ( std::ostream & os, int format = 0 ) const
  144. {
  145. if (os.good())
  146. {
  147. // show starting point
  148. os << this->createStartTag( "PFMKL" ) << std::endl;
  149. os.precision (std::numeric_limits<double>::digits10 + 1);
  150. os << this->createStartTag( "upperBound" ) << std::endl;
  151. os << upperBound << std::endl;
  152. os << this->createEndTag( "upperBound" ) << std::endl;
  153. os << this->createStartTag( "lowerBound" ) << std::endl;
  154. os << lowerBound << std::endl;
  155. os << this->createEndTag( "lowerBound" ) << std::endl;
  156. os << this->createStartTag( "steps" ) << std::endl;
  157. os << this->createStartTag( "numberOfSteps" ) << std::endl;
  158. os << steps.size() << std::endl;
  159. os << this->createEndTag( "numberOfSteps" ) << std::endl;
  160. os << this->createStartTag( "stepInfo" ) << std::endl;;
  161. for ( std::set<int>::const_iterator mySetIt = steps.begin(); mySetIt != steps.end(); mySetIt++)
  162. os << *mySetIt << " ";
  163. os << std::endl;
  164. os << this->createEndTag( "stepInfo" ) << std::endl;
  165. os << this->createEndTag( "steps" ) << std::endl;
  166. // store parent object
  167. ParameterizedFunction::store(os);
  168. // done
  169. os << this->createEndTag( "PFMKL" ) << std::endl;
  170. }
  171. };
  172. virtual void clear () {};
  173. virtual std::string sayYourName() const {return "MKL weighting";};
  174. };
  175. }
  176. #endif