1 | // $Id: SVindex.cc 1121 2008-02-22 15:29:56Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 2 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "SVindex.h" |
---|
25 | #include "DataLookup2D.h" |
---|
26 | #include "yat/random/random.h" |
---|
27 | #include "yat/statistics/Averager.h" |
---|
28 | #include "yat/utility/Vector.h" |
---|
29 | |
---|
30 | #include <algorithm> |
---|
31 | #include <cassert> |
---|
32 | #include <cctype> |
---|
33 | #include <cmath> |
---|
34 | #include <limits> |
---|
35 | #include <utility> |
---|
36 | #include <vector> |
---|
37 | |
---|
38 | namespace theplu { |
---|
39 | namespace yat { |
---|
40 | namespace classifier { |
---|
41 | |
---|
42 | SVindex::SVindex(void) |
---|
43 | : nof_sv_(0), vec_(std::vector<size_t>(0)) |
---|
44 | { |
---|
45 | } |
---|
46 | |
---|
47 | SVindex::SVindex(const size_t n) |
---|
48 | : nof_sv_(0), vec_(std::vector<size_t>(n)) |
---|
49 | { |
---|
50 | for (size_t i=0; i<vec_.size(); i++) |
---|
51 | vec_[i]=i; |
---|
52 | } |
---|
53 | |
---|
54 | size_t SVindex::index_first(void) const |
---|
55 | { |
---|
56 | assert(index_first_<size()); |
---|
57 | return index_first_; |
---|
58 | } |
---|
59 | |
---|
60 | size_t SVindex::index_second(void) const |
---|
61 | { |
---|
62 | assert(index_second_<size()); |
---|
63 | return index_second_; |
---|
64 | } |
---|
65 | |
---|
66 | void SVindex::init(const utility::Vector& alpha, const double tol) |
---|
67 | { |
---|
68 | nof_sv_=0; |
---|
69 | size_t nof_nsv=0; |
---|
70 | vec_.resize(alpha.size()); |
---|
71 | for (size_t i=0; i<alpha.size(); i++) |
---|
72 | if (alpha(i)<tol){ |
---|
73 | nof_nsv++; |
---|
74 | vec_[vec_.size()-nof_nsv]=i; |
---|
75 | } |
---|
76 | else{ |
---|
77 | vec_[nof_sv_]=i; |
---|
78 | nof_sv_++; |
---|
79 | } |
---|
80 | assert(nof_sv_+nof_nsv==vec_.size()); |
---|
81 | |
---|
82 | } |
---|
83 | |
---|
84 | size_t SVindex::nof_sv(void) const |
---|
85 | { |
---|
86 | return nof_sv_; |
---|
87 | } |
---|
88 | |
---|
89 | void SVindex::nsv_first(void) |
---|
90 | { |
---|
91 | // if already nsv, do nothing |
---|
92 | if ( !(index_first_<nof_sv()) ) |
---|
93 | return; |
---|
94 | |
---|
95 | if(index_second_==nof_sv_-1) |
---|
96 | index_second_=index_first_; |
---|
97 | vec_[index_first_]=vec_[nof_sv_-1]; |
---|
98 | vec_[nof_sv_-1]=value_first_; |
---|
99 | index_first_=nof_sv_-1; |
---|
100 | |
---|
101 | nof_sv_--; |
---|
102 | } |
---|
103 | |
---|
104 | void SVindex::nsv_second(void) |
---|
105 | { |
---|
106 | // if already nsv, do nothing |
---|
107 | if ( !(index_second_<nof_sv()) ) |
---|
108 | return; |
---|
109 | |
---|
110 | if(index_first_==nof_sv_-1) |
---|
111 | index_first_=index_second_; |
---|
112 | vec_[index_second_]=vec_[nof_sv_-1]; |
---|
113 | vec_[nof_sv_-1]=value_second_; |
---|
114 | index_second_ = nof_sv_-1; |
---|
115 | |
---|
116 | nof_sv_--; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | void SVindex::shuffle(void) |
---|
121 | { |
---|
122 | random::random_shuffle(vec_.begin()+nof_sv_, vec_.end()); |
---|
123 | } |
---|
124 | |
---|
125 | size_t SVindex::size(void) const |
---|
126 | { |
---|
127 | return vec_.size(); |
---|
128 | } |
---|
129 | |
---|
130 | void SVindex::sv_first(void) |
---|
131 | { |
---|
132 | // if already sv, do nothing |
---|
133 | if (index_first_<nof_sv()) |
---|
134 | return; |
---|
135 | |
---|
136 | // swap elements |
---|
137 | if(index_second_==nof_sv_){ |
---|
138 | index_second_=index_first_; |
---|
139 | } |
---|
140 | vec_[index_first_]=vec_[nof_sv_]; |
---|
141 | vec_[nof_sv_]=value_first_; |
---|
142 | index_first_ = nof_sv_; |
---|
143 | |
---|
144 | nof_sv_++; |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | void SVindex::sv_second(void) |
---|
149 | { |
---|
150 | // if already sv, do nothing |
---|
151 | if (index_second_<nof_sv()) |
---|
152 | return; |
---|
153 | |
---|
154 | // swap elements |
---|
155 | if(index_first_==nof_sv_){ |
---|
156 | index_first_=index_second_; |
---|
157 | } |
---|
158 | |
---|
159 | vec_[index_second_]=vec_[nof_sv_]; |
---|
160 | vec_[nof_sv_]=value_second_; |
---|
161 | index_second_=nof_sv_; |
---|
162 | |
---|
163 | nof_sv_++; |
---|
164 | } |
---|
165 | |
---|
166 | void SVindex::update_first(const size_t i) |
---|
167 | { |
---|
168 | assert(i<size()); |
---|
169 | index_first_=i; |
---|
170 | value_first_=vec_[i]; |
---|
171 | } |
---|
172 | |
---|
173 | void SVindex::update_second(const size_t i) |
---|
174 | { |
---|
175 | assert(i<size()); |
---|
176 | index_second_=i; |
---|
177 | value_second_=vec_[i]; |
---|
178 | } |
---|
179 | |
---|
180 | size_t SVindex::value_first(void) const |
---|
181 | { |
---|
182 | assert(value_first_<size()); |
---|
183 | return value_first_; |
---|
184 | } |
---|
185 | |
---|
186 | size_t SVindex::value_second(void) const |
---|
187 | { |
---|
188 | assert(value_second_<size()); |
---|
189 | return value_second_; |
---|
190 | } |
---|
191 | |
---|
192 | size_t SVindex::operator()(size_t i) const |
---|
193 | { |
---|
194 | assert(i<size()); |
---|
195 | assert(vec_[i]<size()); |
---|
196 | return vec_[i]; |
---|
197 | } |
---|
198 | |
---|
199 | }}} // of namespace classifier, yat, and theplu |
---|