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

Last change on this file since 1706 was 1592, checked in by Peter, 13 years ago

cleaning up some includes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1// $Id: SVindex.cc 1592 2008-10-20 14:02:03Z peter $
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/utility/Vector.h"
26
27#include <algorithm>
28#include <cassert>
29#include <cctype>
30#include <cmath>
31#include <limits>
32#include <utility>
33#include <vector>
34
35namespace theplu {
36namespace yat {
37namespace classifier { 
38
39  SVindex::SVindex(void)
40    : nof_sv_(0), vec_(std::vector<size_t>(0))
41  {
42  }
43
44  SVindex::SVindex(const size_t n)
45    : nof_sv_(0), vec_(std::vector<size_t>(n))
46  {
47    for (size_t i=0; i<vec_.size(); i++)
48      vec_[i]=i;
49  }
50
51  size_t SVindex::index_first(void) const
52  {
53    assert(index_first_<size());
54    return index_first_;
55  }
56
57  size_t SVindex::index_second(void) const
58  {
59    assert(index_second_<size());
60    return index_second_;
61  }
62
63  void SVindex::init(const utility::Vector& alpha, const double tol)
64  {
65    nof_sv_=0;
66    size_t nof_nsv=0;
67    vec_.resize(alpha.size());
68    for (size_t i=0; i<alpha.size(); i++) 
69      if (alpha(i)<tol){
70        nof_nsv++;
71        vec_[vec_.size()-nof_nsv]=i;
72      }
73      else{
74        vec_[nof_sv_]=i;
75        nof_sv_++;
76      }
77    assert(nof_sv_+nof_nsv==vec_.size());
78
79  }
80
81  size_t SVindex::nof_sv(void) const
82  {
83    return nof_sv_;
84  }
85
86  void SVindex::nsv_first(void)
87  {
88    // if already nsv, do nothing
89    if ( !(index_first_<nof_sv()) )
90      return;
91   
92    if(index_second_==nof_sv_-1)
93      index_second_=index_first_;
94    vec_[index_first_]=vec_[nof_sv_-1];
95    vec_[nof_sv_-1]=value_first_;
96    index_first_=nof_sv_-1;
97   
98    nof_sv_--;
99  }
100
101  void SVindex::nsv_second(void)
102  {
103    // if already nsv, do nothing
104    if ( !(index_second_<nof_sv()) )
105      return;
106
107    if(index_first_==nof_sv_-1)
108      index_first_=index_second_;
109    vec_[index_second_]=vec_[nof_sv_-1];
110    vec_[nof_sv_-1]=value_second_;
111    index_second_ = nof_sv_-1;
112   
113    nof_sv_--;
114  }
115
116
117  void SVindex::shuffle(void)
118  {
119    random::random_shuffle(vec_.begin()+nof_sv_, vec_.end()); 
120  }
121
122  size_t SVindex::size(void) const
123  {
124    return vec_.size();
125  }
126
127  void SVindex::sv_first(void)
128  {
129    // if already sv, do nothing
130    if (index_first_<nof_sv())
131      return;
132
133    // swap elements
134    if(index_second_==nof_sv_){
135      index_second_=index_first_;
136    }
137    vec_[index_first_]=vec_[nof_sv_];
138    vec_[nof_sv_]=value_first_;
139    index_first_ = nof_sv_;
140
141    nof_sv_++;
142
143  }
144
145  void SVindex::sv_second(void)
146  {
147    // if already sv, do nothing
148    if (index_second_<nof_sv())
149      return;
150
151    // swap elements
152    if(index_first_==nof_sv_){
153      index_first_=index_second_;
154    }
155
156    vec_[index_second_]=vec_[nof_sv_];
157    vec_[nof_sv_]=value_second_;
158    index_second_=nof_sv_;
159
160    nof_sv_++;
161  }
162
163  void SVindex::update_first(const size_t i)
164  {
165    assert(i<size());
166    index_first_=i;
167    value_first_=vec_[i];
168  }
169
170  void SVindex::update_second(const size_t i)
171  {
172    assert(i<size());
173    index_second_=i;
174    value_second_=vec_[i];
175  }
176
177  size_t SVindex::value_first(void) const
178  {
179    assert(value_first_<size());
180    return value_first_;
181  }
182
183  size_t SVindex::value_second(void) const
184  {
185    assert(value_second_<size());
186    return value_second_;
187  }
188
189  size_t SVindex::operator()(size_t i) const
190  {
191    assert(i<size());
192    assert(vec_[i]<size());
193    return vec_[i];
194  }
195
196}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.