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

Last change on this file since 1004 was 1004, checked in by Peter, 14 years ago

adding a wrapper around std::random_shuffle that uses the RNG class. Changed all calls to random_shuffle to use the added function. Acctually all calls already used the RNG class so essentially this change is only cosmetic, but by providing a function I think it is easier to avoid using multiple random generators.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1// $Id: SVindex.cc 1004 2008-01-23 18:24:53Z 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/matrix.h"
29#include "yat/utility/vector.h"
30
31#include <algorithm>
32#include <cassert>
33#include <cctype>
34#include <cmath>
35#include <limits>
36#include <utility>
37#include <vector>
38
39namespace theplu {
40namespace yat {
41namespace classifier { 
42
43  SVindex::SVindex(void)
44    : nof_sv_(0), vec_(std::vector<size_t>(0))
45  {
46  }
47
48  SVindex::SVindex(const size_t n)
49    : nof_sv_(0), vec_(std::vector<size_t>(n))
50  {
51    for (size_t i=0; i<vec_.size(); i++)
52      vec_[i]=i;
53  }
54
55  size_t SVindex::index_first(void) const
56  {
57    assert(index_first_<size());
58    return index_first_;
59  }
60
61  size_t SVindex::index_second(void) const
62  {
63    assert(index_second_<size());
64    return index_second_;
65  }
66
67  void SVindex::init(const utility::vector& alpha, const double tol)
68  {
69    nof_sv_=0;
70    size_t nof_nsv=0;
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
Note: See TracBrowser for help on using the repository browser.