source: trunk/test/kernel_test.cc @ 307

Last change on this file since 307 was 307, checked in by Peter, 18 years ago

changed names of Kernels and made them work

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.9 KB
Line 
1// $Id: kernel_test.cc 307 2005-05-03 13:28:29Z peter $
2
3
4// C++ tools include
5////////////////////
6#include <c++_tools/gslapi/matrix.h>
7#include <c++_tools/gslapi/vector.h>
8#include <c++_tools/svm/PolynomialKernelFunction.h>
9#include <c++_tools/svm/Kernel_MEV.h>
10#include <c++_tools/svm/Kernel_SEV.h>
11
12#include <cmath>
13#include <fstream>
14#include <iostream>
15#include <cstdlib>
16
17
18using namespace theplu;
19
20bool test_MEV(const gslapi::matrix& data, const svm::KernelFunction* kf, 
21              const gslapi::matrix& control, const double error_bound)
22{
23  svm::Kernel_MEV kernel(data,*kf);
24  for(u_int i=0;i<control.rows();i++)
25    for(u_int j=0;j<control.columns();j++)
26      if (fabs(kernel(i,j)-control(i,j))>error_bound)
27        return false;
28
29  return true;
30}
31
32bool test_SEV(const gslapi::matrix& data, const svm::KernelFunction* kf, 
33              const gslapi::matrix& control, const double error_bound)
34{
35  svm::Kernel_SEV kernel(data,*kf);
36  for(u_int i=0;i<control.rows();i++)
37    for(u_int j=0;j<control.columns();j++)
38      if (fabs(kernel(i,j)-control(i,j))>error_bound)
39        return false;
40
41  return true;
42}
43
44
45int main()
46
47{ 
48  bool ok = true;
49  // Peter, the hardcoded number below should be changed.
50  double error_bound = 1e-8; 
51  std::ifstream is("data/nm_data_centralized.txt");
52  gslapi::matrix transposed_data(is);
53  is.close();
54  // Because how the kernel is treated is changed, data must be transposed.
55  gslapi::matrix data=transposed_data;
56
57  is.open("data/nm_kernel.txt");
58  gslapi::matrix kernel_matlab(is);
59  is.close();
60  svm::KernelFunction* kf = new svm::PolynomialKernelFunction(); 
61  ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound)
62        & test_SEV(data,kf,kernel_matlab,error_bound));
63  delete kf;
64 
65  is.open("data/nm_kernel2.txt");
66  gslapi::matrix kernel_matlab2(is);
67  is.close();
68  kf = new svm::PolynomialKernelFunction(2); 
69  ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound)
70        & test_SEV(data,kf,kernel_matlab2,error_bound));
71 
72  if (ok=true) 
73    return 0;
74  return -1;
75}
Note: See TracBrowser for help on using the repository browser.