RemoveBudgetMaintenanceStrategy.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Remove budget maintenance strategy.
6  *
7  * \par
8  * This is an budget strategy that simply removes one of the
9  * budget vectors. Depending on the flavor, this can be e.g.
10  * a random one, the smallest one (w.r.t. to 2-norm of the alphas)
11  *
12  *
13  *
14  *
15  * \author Aydin Demircioglu
16  * \date 2014
17  *
18  *
19  * \par Copyright 1995-2015 Shark Development Team
20  *
21  * <BR><HR>
22  * This file is part of Shark.
23  * <http://image.diku.dk/shark/>
24  *
25  * Shark is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU Lesser General Public License as published
27  * by the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * Shark is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33  * GNU Lesser General Public License for more details.
34  *
35  * You should have received a copy of the GNU Lesser General Public License
36  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
37  *
38  */
39 //===========================================================================
40 
41 
42 #ifndef SHARK_MODELS_REMOVEBUDGETMAINTENANCESTRATEGY_H
43 #define SHARK_MODELS_REMOVEBUDGETMAINTENANCESTRATEGY_H
44 
45 #include <shark/Models/Converter.h>
47 #include <shark/Data/Dataset.h>
48 #include <shark/Data/DataView.h>
49 
51 
52 
53 namespace shark
54 {
55 
56 ///
57 /// \brief Budget maintenance strategy that removes a vector
58 ///
59 /// This is an budget strategy that simply removes one of the
60 /// budget vectors. Depending on the flavor, this can be e.g.
61 /// a random one, the smallest one (w.r.t. to 2-norm of the alphas)
62 ///
63 template<class InputType>
65 {
68  typedef typename DataType::element_type ElementType;
69 
70 public:
71 
72  /// the flavors of the remove strategy
74 
75 
76  /// constructor.
77  /// @param[in] flavor enum that decides on the method a vector is removed.
78  RemoveBudgetMaintenanceStrategy(size_t flavor = SMALLEST)
79  : m_flavor(flavor)
80  {
81  }
82 
83 
84  /// add a vector to the model.
85  /// this will add the given vector to the model and remove another one depending on the flavor.
86  ///
87  /// @param[in,out] model the model the strategy will work with
88  /// @param[in] alpha alphas for the new budget vector
89  /// @param[in] supportVector the vector to add to the model by applying the maintenance strategy
90  ///
91  virtual void addToModel(ModelType& model, InputType const& alpha, ElementType const& supportVector)
92  {
93 
94  // first we check: if the budget is not full, we do not need to do remove anything
95  std::size_t index = 0;
96  double minAlpha = 0;
97  this->findSmallestVector(model, index, minAlpha);
98 
99  if(minAlpha == 0.0f)
100  {
101  // replace vector and alpha
102  model.basis().element(index) = supportVector.input;
103  row(model.alpha(), index) = alpha;
104  return;
105  }
106 
107  // else depending on the flavor we do something
108  switch(m_flavor)
109  {
110  case RANDOM:
111  {
112  // though we have found the smallest one, we want to remove
113  // a random element.
114  index = Rng::discrete(0, model.basis().numberOfElements() - 1);
115  break;
116  }
117  case SMALLEST:
118  {
119  // we already have found the smallest alpha, so nothing to do
120  break;
121  }
122  default:
123  // throw some error
124  throw(SHARKEXCEPTION("RemoveBudgetMaintenanceStrategy: Unknown flavor!"));
125  }
126 
127  // replace vector and alpha
128  model.basis().element(index) = supportVector.input;
129  row(model.alpha(), index) = alpha;
130 
131  // we need to clear out the last vector, as it is just a buffer
132  row (model.alpha(), model.basis().numberOfElements() -1).clear();
133  }
134 
135 
136  /// class name
137  std::string name() const
138  { return "RemoveBudgetMaintenanceStrategy"; }
139 
140 protected:
141  /// flavor for removing a vector
142  size_t m_flavor;
143 };
144 
145 }
146 #endif