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