source: trunk/test/kernel_test.cc @ 1261

Last change on this file since 1261 was 1261, checked in by Peter, 13 years ago

changing some u_int to size_t

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1// $Id: kernel_test.cc 1261 2008-04-08 14:01:33Z 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 "Suite.h"
27
28#include "yat/utility/Matrix.h"
29#include "yat/classifier/KernelFunction.h"
30#include "yat/classifier/PolynomialKernelFunction.h"
31#include "yat/classifier/GaussianKernelFunction.h"
32#include "yat/classifier/KernelLookup.h"
33#include "yat/classifier/Kernel_MEV.h"
34#include "yat/classifier/Kernel_SEV.h"
35#include "yat/classifier/MatrixLookup.h"
36
37#include <cassert>
38#include <cmath>
39#include <cstddef>
40#include <cstdlib>
41#include <fstream>
42#include <iostream>
43#include <vector>
44
45using namespace theplu::yat;
46
47bool test_MEV(const classifier::MatrixLookup& data, 
48              const classifier::KernelFunction* kf, 
49              const utility::Matrix& control, const double error_bound,
50              test::Suite& suite);
51
52bool test_SEV(const classifier::MatrixLookup& data, 
53              const classifier::KernelFunction* kf, 
54              const utility::Matrix& control, const double error_bound,
55              test::Suite& suite);
56
57
58int main(int argc, char* argv[])
59
60{ 
61  test::Suite suite(argc, argv);
62  suite.err() << "testing kernels" << std::endl;
63
64  utility::Matrix data2_core(2,3);
65  data2_core(0,0)=0;
66  data2_core(1,0)=0;
67  data2_core(0,1)=0;
68  data2_core(1,1)=1;
69  data2_core(0,2)=1;
70  data2_core(1,2)=0;
71  classifier::MatrixLookup data2(data2_core);
72  classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction(); 
73  classifier::Kernel_SEV kernel2(data2,*kf2);
74  assert(kernel2.size()==3);
75  if(kernel2(0,0) || kernel2(0,1) || kernel2(0,2) || 
76     kernel2(1,0) || kernel2(1,1)!=1 || kernel2(1,2) || 
77     kernel2(2,0) || kernel2(2,1) || kernel2(2,2)!=1){
78    suite.add(false);
79    suite.err() << "Found:\n";
80    for (size_t i=0; i<3; i++){
81      for (size_t j=0; j<3; j++)
82        suite.err() << kernel2(i,j) << " ";
83      suite.err() << std::endl;
84    }
85    suite.err() << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl;
86  }
87  classifier::KernelLookup kv2(kernel2);
88  delete kf2;
89
90  // Peter, the hardcoded number below reflects precision in files
91  // containing kernel matrices calculated in matlab.
92  double error_bound = 1e-6; 
93  std::ifstream is(test::filename("data/nm_data_centralized.txt").c_str());
94  utility::Matrix data_core(is);
95  is.close();
96
97  classifier::MatrixLookup data(data_core);
98
99  is.open(test::filename("data/nm_kernel.txt").c_str());
100  utility::Matrix kernel_matlab(is);
101  is.close();
102  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 
103  suite.add(test_MEV(data,kf,kernel_matlab,error_bound, suite) &&
104            test_SEV(data,kf,kernel_matlab,error_bound, suite));
105  delete kf;
106 
107  is.open(test::filename("data/nm_kernel2.txt").c_str());
108  utility::Matrix kernel_matlab2(is);
109  is.close();
110  kf = new classifier::PolynomialKernelFunction(2); 
111  suite.add(test_MEV(data,kf,kernel_matlab2,error_bound, suite) &&
112            test_SEV(data,kf,kernel_matlab2,error_bound, suite));
113  delete kf;
114
115  // Checking that GaussianKernelFunction.
116  suite.err() << "Checking GaussianKernelFunction.\n";
117  is.open(test::filename("data/nm_kernel_gaussian.txt").c_str());
118  utility::Matrix kernel_gaussian(is);
119  is.close();
120  kf = new classifier::GaussianKernelFunction(100); 
121  suite.add(test_MEV(data,kf,kernel_gaussian,error_bound, suite));
122  suite.add(test_SEV(data,kf,kernel_gaussian,error_bound, suite));
123  delete kf;
124
125  data_core.resize(1,5);
126  for (size_t i=0; i<data_core.columns(); i++)
127    data_core(0,i)=i;
128  data = classifier::MatrixLookup(data_core);
129  classifier::PolynomialKernelFunction pkf;
130  classifier::Kernel_SEV kernel(data,pkf);
131 
132  return suite.return_value();
133}
134
135bool test_MEV(const classifier::MatrixLookup& data, 
136              const classifier::KernelFunction* kf, 
137              const utility::Matrix& control, const double error_bound,
138              test::Suite& suite)
139{
140  suite.err() << "testing Kernel_MEV" << std::endl;
141  classifier::Kernel_MEV kernel(data,*kf);
142  for(size_t i=0;i<control.rows();i++)
143    for(size_t j=0;j<control.columns();j++)
144      if (std::abs(kernel(i,j)-control(i,j))>error_bound){
145        suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): "
146                    << kernel(i,j)
147                    << "\n and control(" << i << "," << j << "): "
148                    << control(i,j) << "\n"
149                    << "difference: " << kernel(i,j)-control(i,j) << "\n"
150                    << "expected less than " << error_bound << "\n";
151        return false;
152      }
153  // checking view
154  std::vector<size_t> index_vec(3);
155  index_vec[0]=1;
156  index_vec[1]=2;
157  index_vec[2]=3;
158  utility::Index index(index_vec);
159  classifier::KernelLookup kv_raw(kernel);
160  classifier::KernelLookup kv(kv_raw,index,index);
161  if (kv.rows()!=index.size()){
162    suite.err() << "Error: KernelLookup(kernel, index)\n"
163                << "Size of KernelLookup is " << kv.rows() << "\n"
164                << "expected " << index.size() << std::endl;
165   
166    return false;
167  }
168  classifier::KernelLookup kv2(kernel);
169  if (kv2.rows()!=kernel.size()){
170    suite.err() << "Error: KernelLookup(kernel)\n" << std::endl
171           << "Size of KernelLookup is " << kv.rows() << std::endl
172           << "expected " << kernel.size() << std::endl;
173   
174    return false;
175  }
176  return true;
177}
178
179bool test_SEV(const classifier::MatrixLookup& data, 
180              const classifier::KernelFunction* kf, 
181              const utility::Matrix& control, const double error_bound,
182              test::Suite& suite)
183{
184  suite.err() << "testing Kernel_SEV" << std::endl;
185  classifier::Kernel_SEV kernel(data,*kf);
186  for(size_t i=0;i<control.rows();i++)
187    for(size_t j=0;j<control.columns();j++)
188      if (std::abs(kernel(i,j)-control(i,j))>error_bound){
189        suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): "
190                    << kernel(i,j)
191                    << "\n and control(" << i << "," << j << "): "
192                    << control(i,j) << "\n"
193                    << "difference: " << kernel(i,j)-control(i,j) << "\n"
194                    << "expected less than " << error_bound << "\n";
195        return false;
196      }
197
198  // checking view
199  std::vector<size_t> index_vec(3);
200  index_vec[0]=1;
201  index_vec[1]=2;
202  index_vec[2]=3;
203  utility::Index index(index_vec);
204  classifier::KernelLookup kv_raw(kernel);
205  classifier::KernelLookup kv(kv_raw,index, index);
206  if (kv.rows()!=index.size()){
207    suite.err() << "Error: KernelLookup(kernel, index)\n" << std::endl
208           << "Size of KernelLookup is " << kv.rows() << std::endl
209           << "expected " << index.size() << std::endl;
210   
211    return false;
212  }
213  classifier::KernelLookup kv2(kernel);
214  if (kv2.rows()!=kernel.size()){
215    suite.err() << "Error: KernelLookup(kernel)\n" << std::endl
216           << "Size of KernelLookup is " << kv.rows() << std::endl
217           << "expected " << kernel.size() << std::endl;
218   
219    return false;
220  }
221  return true;
222}
223
224
Note: See TracBrowser for help on using the repository browser.