Debian Codegraph @

.deprecated/learning/classifier.h

raw
1 /*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2013-2014, Willow Garage, Inc.
5 * Copyright (c) 2014-2016, Open Source Robotics Foundation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of Open Source Robotics Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /** \author Jia Pan */
37
38 #ifndef FCL_LEARNING_CLASSIFIER_H
39 #define FCL_LEARNING_CLASSIFIER_H
40
41 #include <vector>
42
43 #include "fcl/common/data_types.h"
44
45 namespace fcl
46 {
47
48 template<typename S, std::size_t N>
49 struct Item
50 {
51 VectorN<S, N> q;
52 bool label;
53 S w;
54
55 Item(const VectorN<S, N>& q_, bool label_, S w_ = 1);
56
57 Item();
58
59 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(S, N)
60 };
61
62 template<typename S, std::size_t N>
63 struct Scaler
64 {
65 VectorN<S, N> v_min, v_max;
66 Scaler();
67
68 Scaler(const VectorN<S, N>& v_min_, const VectorN<S, N>& v_max_);
69
70 VectorN<S, N> scale(const VectorN<S, N>& v) const;
71
72 VectorN<S, N> unscale(const VectorN<S, N>& v) const;
73
74 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(S, N)
75 };
76
77 template<typename S>
78 struct PredictResult
79 {
80 bool label;
81 S prob;
82
83 PredictResult();
84 PredictResult(bool label_, S prob_ = 1);
85 };
86
87 template<typename S, std::size_t N>
88 class SVMClassifier
89 {
90 public:
91
92 ~SVMClassifier();
93
94 virtual PredictResult<S> predict(const VectorN<S, N>& q) const = 0;
95 virtual std::vector<PredictResult<S>> predict(
96 const std::vector<VectorN<S, N> >& qs) const = 0;
97
98 virtual std::vector<Item<S, N> > getSupportVectors() const = 0;
99 virtual void setScaler(const Scaler<S, N>& scaler) = 0;
100
101 virtual void learn(const std::vector<Item<S, N> >& data) = 0;
102
103 S error_rate(const std::vector<Item<S, N> >& data) const;
104 };
105
106 template<typename S, std::size_t N>
107 Scaler<S, N> computeScaler(const std::vector<Item<S, N> >& data);
108
109 template<typename S, std::size_t N>
110 Scaler<S, N> computeScaler(const std::vector<VectorN<S, N> >& data);
111
112 //============================================================================//
113 // //
114 // Implementations //
115 // //
116 //============================================================================//
117
118 //==============================================================================
119 template<typename S, std::size_t N>
120 Item<S, N>::Item(const VectorN<S, N>& q_, bool label_, S w_)
121 : q(q_), label(label_), w(w_)
122 {
123 // Do nothing
124 }
125
126 //==============================================================================
127 template<typename S, std::size_t N>
128 Item<S, N>::Item()
129 {
130 // Do nothing
131 }
132
133 //==============================================================================
134 template<typename S, std::size_t N>
135 Scaler<S, N>::Scaler()
136 {
137 // default no scale
138 for(std::size_t i = 0; i < N; ++i)
139 {
140 v_min[i] = 0;
141 v_max[i] = 1;
142 }
143 }
144
145 //==============================================================================
146 template<typename S, std::size_t N>
147 Scaler<S, N>::Scaler(const VectorN<S, N>& v_min_, const VectorN<S, N>& v_max_)
148 : v_min(v_min_), v_max(v_max_)
149 {
150 // Do nothing
151 }
152
153 //==============================================================================
154 template<typename S, std::size_t N>
155 VectorN<S, N> Scaler<S, N>::scale(const VectorN<S, N>& v) const
156 {
157 VectorN<S, N> res;
158 for(std::size_t i = 0; i < N; ++i)
159 res[i] = (v[i] - v_min[i]) / (v_max[i] - v_min[i]);
160 return res;
161 }
162
163 //==============================================================================
164 template<typename S, std::size_t N>
165 VectorN<S, N> Scaler<S, N>::unscale(const VectorN<S, N>& v) const
166 {
167 VectorN<S, N> res;
168 for(std::size_t i = 0; i < N; ++i)
169 res[i] = v[i] * (v_max[i] - v_min[i]) + v_min[i];
170 return res;
171 }
172
173 //==============================================================================
174 template<typename S>
175 PredictResult<S>::PredictResult()
176 {
177 // Do nothing
178 }
179
180 //==============================================================================
181 template<typename S>
182 PredictResult<S>::PredictResult(bool label_, S prob_)
183 : label(label_), prob(prob_)
184 {
185 // Do nothing
186 }
187
188 //==============================================================================
189 template<typename S, std::size_t N>
190 SVMClassifier<S, N>::~SVMClassifier()
191 {
192 // Do nothing
193 }
194
195 //==============================================================================
196 template<typename S, std::size_t N>
197 S SVMClassifier<S, N>::error_rate(const std::vector<Item<S, N> >& data) const
198 {
199 std::size_t num = data.size();
200
201 std::size_t error_num = 0;
202 for(std::size_t i = 0; i < data.size(); ++i)
203 {
204 PredictResult<S> res = predict(data[i].q);
205 if(res.label != data[i].label)
206 error_num++;
207 }
208
209 return error_num / (S)num;
210 }
211
212 //==============================================================================
213 template<typename S, std::size_t N>
214 Scaler<S, N> computeScaler(const std::vector<Item<S, N> >& data)
215 {
216 VectorN<S, N> lower_bound, upper_bound;
217 for(std::size_t j = 0; j < N; ++j)
218 {
219 lower_bound[j] = std::numeric_limits<S>::max();
220 upper_bound[j] = -std::numeric_limits<S>::max();
221 }
222
223 for(std::size_t i = 0; i < data.size(); ++i)
224 {
225 for(std::size_t j = 0; j < N; ++j)
226 {
227 if(data[i].q[j] < lower_bound[j]) lower_bound[j] = data[i].q[j];
228 if(data[i].q[j] > upper_bound[j]) upper_bound[j] = data[i].q[j];
229 }
230 }
231
232 return Scaler<S, N>(lower_bound, upper_bound);
233 }
234
235 //==============================================================================
236 template<typename S, std::size_t N>
237 Scaler<S, N> computeScaler(const std::vector<VectorN<S, N> >& data)
238 {
239 VectorN<S, N> lower_bound, upper_bound;
240 for(std::size_t j = 0; j < N; ++j)
241 {
242 lower_bound[j] = std::numeric_limits<S>::max();
243 upper_bound[j] = -std::numeric_limits<S>::max();
244 }
245
246 for(std::size_t i = 0; i < data.size(); ++i)
247 {
248 for(std::size_t j = 0; j < N; ++j)
249 {
250 if(data[i][j] < lower_bound[j]) lower_bound[j] = data[i][j];
251 if(data[i][j] > upper_bound[j]) upper_bound[j] = data[i][j];
252 }
253 }
254
255 return Scaler<S, N>(lower_bound, upper_bound);
256 }
257
258 } // namespace fcl
259
260 #endif