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-2017 Shark Development Team
20 *
21 * <BR><HR>
22 * This file is part of Shark.
23 * <https://shark-ml.github.io/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
46#include <shark/Data/Dataset.h>
47#include <shark/Data/DataView.h>
48
50
51
52namespace shark
53{
54
55///
56/// \brief Budget maintenance strategy that removes a vector
57///
58/// This is an budget strategy that simply removes one of the
59/// budget vectors. Depending on the flavor, this can be e.g.
60/// a random one, the smallest one (w.r.t. to 2-norm of the alphas)
61///
62template<class InputType>
64{
67 typedef typename DataType::element_type ElementType;
68
69public:
70
71 /// the flavors of the remove strategy
73
74
75 /// constructor.
76 /// @param[in] flavor enum that decides on the method a vector is removed.
78 : m_flavor(flavor)
79 {
80 SHARK_RUNTIME_CHECK(flavor < 2, "Invalid flavor");
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 = random::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 }
123
124 // replace vector and alpha
125 model.basis().element(index) = supportVector.input;
126 row(model.alpha(), index) = alpha;
127
128 // we need to clear out the last vector, as it is just a buffer
129 row (model.alpha(), model.basis().numberOfElements() -1).clear();
130 }
131
132
133 /// class name
134 std::string name() const
135 { return "RemoveBudgetMaintenanceStrategy"; }
136
137protected:
138 /// flavor for removing a vector
139 size_t m_flavor;
140};
141
142}
143#endif