source: trunk/test/kernel_test.cc @ 1210

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

refs #223 change fabs to std::abs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1// $Id: kernel_test.cc 1210 2008-03-06 16:00:44Z peter $
2
3/*
4  Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
6  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
7
8  This file is part of the yat library, http://trac.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "yat/utility/Matrix.h"
27#include "yat/classifier/KernelFunction.h"
28#include "yat/classifier/PolynomialKernelFunction.h"
29#include "yat/classifier/GaussianKernelFunction.h"
30#include "yat/classifier/KernelLookup.h"
31#include "yat/classifier/Kernel_MEV.h"
32#include "yat/classifier/Kernel_SEV.h"
33#include "yat/classifier/MatrixLookup.h"
34
35#include <cassert>
36#include <cmath>
37#include <cstdlib>
38#include <fstream>
39#include <iostream>
40#include <vector>
41
42using namespace theplu::yat;
43
44bool test_MEV(const classifier::MatrixLookup& data, 
45              const classifier::KernelFunction* kf, 
46              const utility::Matrix& control, const double error_bound,
47              std::ostream* error);
48
49bool test_SEV(const classifier::MatrixLookup& data, 
50              const classifier::KernelFunction* kf, 
51              const utility::Matrix& control, const double error_bound,
52              std::ostream* error);
53
54
55int main(const int argc,const char* argv[])
56
57{ 
58  std::ostream* error;
59  if (argc>1 && argv[1]==std::string("-v"))
60    error = &std::cerr;
61  else {
62    error = new std::ofstream("/dev/null");
63    if (argc>1)
64      std::cout << "kernel_test -v : for printing extra information\n";
65  }
66  *error << "testing kernels" << std::endl;
67  bool ok = true;
68
69  utility::Matrix data2_core(2,3);
70  data2_core(0,0)=0;
71  data2_core(1,0)=0;
72  data2_core(0,1)=0;
73  data2_core(1,1)=1;
74  data2_core(0,2)=1;
75  data2_core(1,2)=0;
76  classifier::MatrixLookup data2(data2_core);
77  classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction(); 
78  classifier::Kernel_SEV kernel2(data2,*kf2);
79  assert(kernel2.size()==3);
80  if(kernel2(0,0) || kernel2(0,1) || kernel2(0,2) || 
81     kernel2(1,0) || kernel2(1,1)!=1 || kernel2(1,2) || 
82     kernel2(2,0) || kernel2(2,1) || kernel2(2,2)!=1){
83    ok = false;
84    *error << "Found:\n";
85    for (size_t i=0; i<3; i++){
86      for (size_t j=0; j<3; j++)
87        *error << kernel2(i,j) << " ";
88      *error << std::endl;
89    }
90    *error << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl;
91  }
92  classifier::KernelLookup kv2(kernel2);
93  delete kf2;
94
95  // Peter, the hardcoded number below should be changed.
96  double error_bound = 1e-8; 
97  std::ifstream is("data/nm_data_centralized.txt");
98  utility::Matrix data_core(is);
99  is.close();
100
101  classifier::MatrixLookup data(data_core);
102
103  is.open("data/nm_kernel.txt");
104  utility::Matrix kernel_matlab(is);
105  is.close();
106  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 
107  ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound, error)
108        & test_SEV(data,kf,kernel_matlab,error_bound, error));
109  delete kf;
110 
111  is.open("data/nm_kernel2.txt");
112  utility::Matrix kernel_matlab2(is);
113  is.close();
114  kf = new classifier::PolynomialKernelFunction(2); 
115  ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound, error)
116        && test_SEV(data,kf,kernel_matlab2,error_bound, error));
117  delete kf;
118
119  // Checking that GaussianKernelFunction.
120  *error << "Checking GaussianKernelFunction.\n";
121  is.open("data/nm_kernel_gaussian.txt");
122  utility::Matrix kernel_gaussian(is);
123  is.close();
124  kf = new classifier::GaussianKernelFunction(100); 
125  ok = ok && test_MEV(data,kf,kernel_gaussian,error_bound, error);
126  ok = ok && test_SEV(data,kf,kernel_gaussian,error_bound, error);
127  delete kf;
128
129  data_core.resize(1,5);
130  for (size_t i=0; i<data_core.columns(); i++)
131    data_core(0,i)=i;
132  data = classifier::MatrixLookup(data_core);
133  classifier::PolynomialKernelFunction pkf;
134  classifier::Kernel_SEV kernel(data,pkf);
135 
136
137  if (error!=&std::cerr)
138    delete error;
139
140  if (ok=true) 
141    return 0;
142  return -1;
143}
144
145bool test_MEV(const classifier::MatrixLookup& data, 
146              const classifier::KernelFunction* kf, 
147              const utility::Matrix& control, const double error_bound,
148              std::ostream* error)
149{
150  *error << "testing Kernel_MEV" << std::endl;
151  classifier::Kernel_MEV kernel(data,*kf);
152  for(u_int i=0;i<control.rows();i++)
153    for(u_int j=0;j<control.columns();j++)
154      if (std::abs(kernel(i,j)-control(i,j))>error_bound)
155        return false;
156
157  // checking view
158  std::vector<size_t> index_vec(3);
159  index_vec[0]=1;
160  index_vec[1]=2;
161  index_vec[2]=3;
162  utility::Index index(index_vec);
163  classifier::KernelLookup kv_raw(kernel);
164  classifier::KernelLookup kv(kv_raw,index,index);
165  if (kv.rows()!=index.size()){
166    *error << "Error: KernelLookup(kernel, index)\n" << std::endl
167           << "Size of KernelLookup is " << kv.rows() << std::endl
168           << "expected " << index.size() << std::endl;
169   
170    return false;
171  }
172  classifier::KernelLookup kv2(kernel);
173  if (kv2.rows()!=kernel.size()){
174    *error << "Error: KernelLookup(kernel)\n" << std::endl
175           << "Size of KernelLookup is " << kv.rows() << std::endl
176           << "expected " << kernel.size() << std::endl;
177   
178    return false;
179  }
180
181  return true;
182}
183
184bool test_SEV(const classifier::MatrixLookup& data, 
185              const classifier::KernelFunction* kf, 
186              const utility::Matrix& control, const double error_bound,
187              std::ostream* error)
188{
189  *error << "testing Kernel_SEV" << std::endl;
190  classifier::Kernel_SEV kernel(data,*kf);
191  for(u_int i=0;i<control.rows();i++)
192    for(u_int j=0;j<control.columns();j++)
193      if (std::abs(kernel(i,j)-control(i,j))>error_bound)
194        return false;
195
196  // checking view
197  std::vector<size_t> index_vec(3);
198  index_vec[0]=1;
199  index_vec[1]=2;
200  index_vec[2]=3;
201  utility::Index index(index_vec);
202  classifier::KernelLookup kv_raw(kernel);
203  classifier::KernelLookup kv(kv_raw,index, index);
204  if (kv.rows()!=index.size()){
205    *error << "Error: KernelLookup(kernel, index)\n" << std::endl
206           << "Size of KernelLookup is " << kv.rows() << std::endl
207           << "expected " << index.size() << std::endl;
208   
209    return false;
210  }
211  classifier::KernelLookup kv2(kernel);
212  if (kv2.rows()!=kernel.size()){
213    *error << "Error: KernelLookup(kernel)\n" << std::endl
214           << "Size of KernelLookup is " << kv.rows() << std::endl
215           << "expected " << kernel.size() << std::endl;
216   
217    return false;
218  }
219  return true;
220}
221
222
Note: See TracBrowser for help on using the repository browser.