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

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

References #83. Changing project name to yat. Compilation will fail in this revision.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: SVindex.cc 675 2006-10-10 12:08:45Z 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 "yat/classifier/SVindex.h"
25
26#include "yat/classifier/DataLookup2D.h"
27#include "yat/random/random.h"
28#include "yat/statistics/Averager.h"
29#include "yat/utility/matrix.h"
30#include "yat/utility/vector.h"
31
32#include <algorithm>
33#include <cassert>
34#include <cctype>
35#include <cmath>
36#include <limits>
37#include <utility>
38#include <vector>
39
40
41namespace theplu {
42namespace classifier { 
43
44  SVindex::SVindex(void)
45    : nof_sv_(0), vec_(std::vector<size_t>(0))
46  {
47  }
48
49  SVindex::SVindex(const size_t n)
50    : nof_sv_(0), vec_(std::vector<size_t>(n))
51  {
52    for (size_t i=0; i<vec_.size(); i++)
53      vec_[i]=i;
54  }
55
56  void SVindex::init(const utility::vector& alpha, const double tol)
57  {
58    nof_sv_=0;
59    size_t nof_nsv=0;
60    for (size_t i=0; i<alpha.size(); i++) 
61      if (alpha(i)<tol){
62        nof_nsv++;
63        vec_[vec_.size()-nof_nsv]=i;
64      }
65      else{
66        vec_[nof_sv_]=i;
67        nof_sv_++;
68      }
69    assert(nof_sv_+nof_nsv==vec_.size());
70
71  }
72
73  void SVindex::sv_first(void)
74  {
75    // if already sv, do nothing
76    if (index_first_<nof_sv())
77      return;
78
79    // swap elements
80    if(index_second_==nof_sv_){
81      index_second_=index_first_;
82    }
83    vec_[index_first_]=vec_[nof_sv_];
84    vec_[nof_sv_]=value_first_;
85    index_first_ = nof_sv_;
86
87    nof_sv_++;
88
89  }
90
91  void SVindex::sv_second(void)
92  {
93    // if already sv, do nothing
94    if (index_second_<nof_sv())
95      return;
96
97    // swap elements
98    if(index_first_==nof_sv_){
99      index_first_=index_second_;
100    }
101
102    vec_[index_second_]=vec_[nof_sv_];
103    vec_[nof_sv_]=value_second_;
104    index_second_=nof_sv_;
105
106    nof_sv_++;
107  }
108
109  void SVindex::nsv_first(void)
110  {
111    // if already nsv, do nothing
112    if ( !(index_first_<nof_sv()) )
113      return;
114   
115    if(index_second_==nof_sv_-1)
116      index_second_=index_first_;
117    vec_[index_first_]=vec_[nof_sv_-1];
118    vec_[nof_sv_-1]=value_first_;
119    index_first_=nof_sv_-1;
120   
121    nof_sv_--;
122  }
123
124  void SVindex::nsv_second(void)
125  {
126    // if already nsv, do nothing
127    if ( !(index_second_<nof_sv()) )
128      return;
129
130    if(index_first_==nof_sv_-1)
131      index_first_=index_second_;
132    vec_[index_second_]=vec_[nof_sv_-1];
133    vec_[nof_sv_-1]=value_second_;
134    index_second_ = nof_sv_-1;
135   
136    nof_sv_--;
137  }
138
139
140  void SVindex::shuffle(void)
141  {
142    random::DiscreteUniform a;
143    random_shuffle(vec_.begin()+nof_sv_, vec_.end(), a); 
144  }
145
146  void SVindex::update_first(const size_t i)
147  {
148    assert(i<size());
149    index_first_=i;
150    value_first_=vec_[i];
151  }
152
153  void SVindex::update_second(const size_t i)
154  {
155    assert(i<size());
156    index_second_=i;
157    value_second_=vec_[i];
158  }
159
160}} // of namespace classifier and namespace theplu
Note: See TracBrowser for help on using the repository browser.