AbstractBudgetMaintenanceStrategy.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Abstract Budget maintenance strategy
6  *
7  * \par
8  * This holds the interface for any budget maintenance strategy.
9  *
10  *
11  *
12  *
13  * \author Aydin Demircioglu
14  * \date 2014
15  *
16  *
17  * \par Copyright 1995-2015 Shark Development Team
18  *
19  * <BR><HR>
20  * This file is part of Shark.
21  * <http://image.diku.dk/shark/>
22  *
23  * Shark is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU Lesser General Public License as published
25  * by the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * Shark is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31  * GNU Lesser General Public License for more details.
32  *
33  * You should have received a copy of the GNU Lesser General Public License
34  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
35  *
36  */
37 //===========================================================================
38 
39 
40 #ifndef SHARK_MODELS_ABSTRACTBUDGETMAINTENANCESTRATEGY_H
41 #define SHARK_MODELS_ABSTRACTBUDGETMAINTENANCESTRATEGY_H
42 
43 #include <shark/Data/Dataset.h>
44 #include <shark/Data/DataView.h>
45 #include <shark/Models/Converter.h>
48 
49 
50 namespace shark
51 {
52 
53 ///
54 /// \brief This is the abstract interface for any budget maintenance strategy.
55 ///
56 /// To allow for easy exchange of budget maintenance strategies, each of
57 /// them should derive from this class. The only function it defines is addToModel,
58 /// which, when implemented, will add a given supportvector and given alphas
59 /// to the provided model by applying the respective budget maintenance strategy.
60 /// (Note that not all merging strategies need the alphas, but some do)
61 ///
62 template<class InputType>
64 {
65 
66 public:
69  typedef typename DataType::element_type ElementType;
70 
72  { }
73 
74 
75  /// this is the main interface, which adds a given supportvector with
76  /// given alpha coefficients to the model.
77  ///
78  /// @param[in,out] model the model the strategy will work with
79  /// @param[in] alpha alphas for the new budget vector
80  /// @param[in] supportVector the vector to add to the model by applying the maintenance strategy
81  ///
82  virtual void addToModel(ModelType& model, InputType const& alpha, ElementType const& supportVector) = 0;
83 
84 
85 
86  /// this will find the vector with the smallest alpha, measured in 2-norm
87  /// in the given model. now there is a special case: if there is somewhere a zero
88  /// coefficient, then obviously this is the smallest element. in this case we
89  /// just proceed as usual. the caller must decide what to do with such a vector.
90  /// \par note: if the model is completely empty, we will give back infinity and index 0.
91  /// this is again for the caller to handle. for safety, we put an assert in there.
92  ///
93  /// @param[in] model the model we want to search
94  /// @param[out] minIndex the index of the vector with smallest coefficient
95  /// @param[out] minAlpha the 2-norm of the alpha coefficient of the found vector
96  ///
97  static void findSmallestVector(ModelType const& model, size_t &minIndex, double &minAlpha)
98  {
99  // we do not have it, so we remove the vector with the
100  // smallest 'influcence', measured by the smallest alpha
101 
102  minAlpha = std::numeric_limits<double>::infinity();
103  minIndex = 0;
104 
105  for(size_t j = 0; j < model.alpha().size1(); j++)
106  {
107  double currentNorm = blas::norm_2(row(model.alpha(), j));
108 
109  if(currentNorm < minAlpha)
110  {
111  minAlpha = blas::norm_2(row(model.alpha(), j));
112  minIndex = j;
113  }
114  }
115 
116  SHARK_ASSERT(minAlpha != std::numeric_limits<double>::infinity());
117  }
118 
119 
120  /// return the class name
121  std::string name() const
122  { return "AbstractBudgetMaintenanceStrategy"; }
123 };
124 
125 
126 }
127 #endif