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

Last change on this file since 831 was 831, checked in by Peter, 15 years ago

Refs #185.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1// $Id: SVindex.cc 831 2007-03-27 13:22:09Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/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::DiscreteUniform a;
123    random_shuffle(vec_.begin()+nof_sv_, vec_.end(), a); 
124  }
125
126  size_t SVindex::size(void) const
127  {
128    return vec_.size();
129  }
130
131  void SVindex::sv_first(void)
132  {
133    // if already sv, do nothing
134    if (index_first_<nof_sv())
135      return;
136
137    // swap elements
138    if(index_second_==nof_sv_){
139      index_second_=index_first_;
140    }
141    vec_[index_first_]=vec_[nof_sv_];
142    vec_[nof_sv_]=value_first_;
143    index_first_ = nof_sv_;
144
145    nof_sv_++;
146
147  }
148
149  void SVindex::sv_second(void)
150  {
151    // if already sv, do nothing
152    if (index_second_<nof_sv())
153      return;
154
155    // swap elements
156    if(index_first_==nof_sv_){
157      index_first_=index_second_;
158    }
159
160    vec_[index_second_]=vec_[nof_sv_];
161    vec_[nof_sv_]=value_second_;
162    index_second_=nof_sv_;
163
164    nof_sv_++;
165  }
166
167  void SVindex::update_first(const size_t i)
168  {
169    assert(i<size());
170    index_first_=i;
171    value_first_=vec_[i];
172  }
173
174  void SVindex::update_second(const size_t i)
175  {
176    assert(i<size());
177    index_second_=i;
178    value_second_=vec_[i];
179  }
180
181  size_t SVindex::value_first(void) const
182  {
183    assert(value_first_<size());
184    return value_first_;
185  }
186
187  size_t SVindex::value_second(void) const
188  {
189    assert(value_second_<size());
190    return value_second_;
191  }
192
193  size_t SVindex::operator()(size_t i) const
194  {
195    assert(i<size());
196    assert(vec_[i]<size());
197    return vec_[i];
198  }
199
200}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.