source: trunk/yat/classifier/SVindex.cc @ 1487

Last change on this file since 1487 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

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