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

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

Addresses #153. Introduced yat namespace. Removed alignment namespace. Clean up of code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: SVindex.cc 680 2006-10-11 17:49:03Z jari $
2
3/*
4  Copyright (C) The authors contributing to this file.
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  void SVindex::init(const utility::vector& alpha, const double tol)
56  {
57    nof_sv_=0;
58    size_t nof_nsv=0;
59    for (size_t i=0; i<alpha.size(); i++) 
60      if (alpha(i)<tol){
61        nof_nsv++;
62        vec_[vec_.size()-nof_nsv]=i;
63      }
64      else{
65        vec_[nof_sv_]=i;
66        nof_sv_++;
67      }
68    assert(nof_sv_+nof_nsv==vec_.size());
69
70  }
71
72  void SVindex::sv_first(void)
73  {
74    // if already sv, do nothing
75    if (index_first_<nof_sv())
76      return;
77
78    // swap elements
79    if(index_second_==nof_sv_){
80      index_second_=index_first_;
81    }
82    vec_[index_first_]=vec_[nof_sv_];
83    vec_[nof_sv_]=value_first_;
84    index_first_ = nof_sv_;
85
86    nof_sv_++;
87
88  }
89
90  void SVindex::sv_second(void)
91  {
92    // if already sv, do nothing
93    if (index_second_<nof_sv())
94      return;
95
96    // swap elements
97    if(index_first_==nof_sv_){
98      index_first_=index_second_;
99    }
100
101    vec_[index_second_]=vec_[nof_sv_];
102    vec_[nof_sv_]=value_second_;
103    index_second_=nof_sv_;
104
105    nof_sv_++;
106  }
107
108  void SVindex::nsv_first(void)
109  {
110    // if already nsv, do nothing
111    if ( !(index_first_<nof_sv()) )
112      return;
113   
114    if(index_second_==nof_sv_-1)
115      index_second_=index_first_;
116    vec_[index_first_]=vec_[nof_sv_-1];
117    vec_[nof_sv_-1]=value_first_;
118    index_first_=nof_sv_-1;
119   
120    nof_sv_--;
121  }
122
123  void SVindex::nsv_second(void)
124  {
125    // if already nsv, do nothing
126    if ( !(index_second_<nof_sv()) )
127      return;
128
129    if(index_first_==nof_sv_-1)
130      index_first_=index_second_;
131    vec_[index_second_]=vec_[nof_sv_-1];
132    vec_[nof_sv_-1]=value_second_;
133    index_second_ = nof_sv_-1;
134   
135    nof_sv_--;
136  }
137
138
139  void SVindex::shuffle(void)
140  {
141    random::DiscreteUniform a;
142    random_shuffle(vec_.begin()+nof_sv_, vec_.end(), a); 
143  }
144
145  void SVindex::update_first(const size_t i)
146  {
147    assert(i<size());
148    index_first_=i;
149    value_first_=vec_[i];
150  }
151
152  void SVindex::update_second(const size_t i)
153  {
154    assert(i<size());
155    index_second_=i;
156    value_second_=vec_[i];
157  }
158
159}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.