source: trunk/test/kernel_test.cc @ 680

Last change on this file since 680 was 680, checked in by Jari Häkkinen, 16 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 Author Date Id Revision
File size: 6.1 KB
Line 
1// $Id: kernel_test.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 "yat/utility/matrix.h"
25#include "yat/classifier/KernelFunction.h"
26#include "yat/classifier/PolynomialKernelFunction.h"
27#include "yat/classifier/GaussianKernelFunction.h"
28#include "yat/classifier/KernelLookup.h"
29#include "yat/classifier/Kernel_MEV.h"
30#include "yat/classifier/Kernel_SEV.h"
31
32#include <cmath>
33#include <cstdlib>
34#include <fstream>
35#include <iostream>
36#include <vector>
37
38using namespace theplu::yat;
39
40bool test_MEV(const classifier::MatrixLookup& data, 
41              const classifier::KernelFunction* kf, 
42              const utility::matrix& control, const double error_bound,
43              std::ostream* error);
44
45bool test_SEV(const classifier::MatrixLookup& data, 
46              const classifier::KernelFunction* kf, 
47              const utility::matrix& control, const double error_bound,
48              std::ostream* error);
49
50
51int main(const int argc,const char* argv[])
52
53{ 
54  std::ostream* error;
55  if (argc>1 && argv[1]==std::string("-v"))
56    error = &std::cerr;
57  else {
58    error = new std::ofstream("/dev/null");
59    if (argc>1)
60      std::cout << "kernel_test -v : for printing extra information\n";
61  }
62  *error << "testing kernels" << std::endl;
63  bool ok = true;
64
65  utility::matrix data2_core(2,3);
66  data2_core(0,0)=0;
67  data2_core(1,0)=0;
68  data2_core(0,1)=0;
69  data2_core(1,1)=1;
70  data2_core(0,2)=1;
71  data2_core(1,2)=0;
72  classifier::MatrixLookup data2(data2_core);
73  classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction(); 
74  classifier::Kernel_SEV kernel2(data2,*kf2);
75  assert(kernel2.size()==3);
76  if(kernel2(0,0) || kernel2(0,1) || kernel2(0,2) || 
77     kernel2(1,0) || kernel2(1,1)!=1 || kernel2(1,2) || 
78     kernel2(2,0) || kernel2(2,1) || kernel2(2,2)!=1){
79    ok = false;
80    *error << "Found:\n";
81    for (size_t i=0; i<3; i++){
82      for (size_t j=0; j<3; j++)
83        *error << kernel2(i,j) << " ";
84      *error << std::endl;
85    }
86    *error << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl;
87  }
88  classifier::KernelLookup kv2(kernel2);
89  delete kf2;
90
91  // Peter, the hardcoded number below should be changed.
92  double error_bound = 1e-8; 
93  std::ifstream is("data/nm_data_centralized.txt");
94  utility::matrix data_core(is);
95  is.close();
96
97  classifier::MatrixLookup data(data_core);
98
99  is.open("data/nm_kernel.txt");
100  utility::matrix kernel_matlab(is);
101  is.close();
102  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 
103  ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound, error)
104        & test_SEV(data,kf,kernel_matlab,error_bound, error));
105  delete kf;
106 
107  is.open("data/nm_kernel2.txt");
108  utility::matrix kernel_matlab2(is);
109  is.close();
110  kf = new classifier::PolynomialKernelFunction(2); 
111  ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound, error)
112        & test_SEV(data,kf,kernel_matlab2,error_bound, error));
113  delete kf;
114
115  // Checking that a GaussianKernelFunction object can be built.
116  kf = new classifier::GaussianKernelFunction(); 
117  delete kf;
118
119  data_core = utility::matrix(1,5);
120  for (size_t i=0; i<data_core.columns(); i++)
121    data_core(0,i)=i;
122  data = classifier::MatrixLookup(data_core);
123  classifier::PolynomialKernelFunction pkf;
124  classifier::Kernel_SEV kernel(data,pkf);
125 
126
127  if (error!=&std::cerr)
128    delete error;
129
130  if (ok=true) 
131    return 0;
132  return -1;
133}
134
135bool test_MEV(const classifier::MatrixLookup& data, 
136              const classifier::KernelFunction* kf, 
137              const utility::matrix& control, const double error_bound,
138              std::ostream* error)
139{
140  *error << "testing Kernel_MEV" << std::endl;
141  classifier::Kernel_MEV kernel(data,*kf);
142  for(u_int i=0;i<control.rows();i++)
143    for(u_int j=0;j<control.columns();j++)
144      if (fabs(kernel(i,j)-control(i,j))>error_bound)
145        return false;
146
147  // checking view
148  std::vector<size_t> index(3);
149  index[0]=1;
150  index[1]=2;
151  index[2]=3;
152  classifier::KernelLookup kv_raw(kernel);
153  classifier::KernelLookup kv(kv_raw,index,index);
154  if (kv.rows()!=index.size()){
155    *error << "Error: KernelLookup(kernel, index)\n" << std::endl
156           << "Size of KernelLookup is " << kv.rows() << std::endl
157           << "expected " << index.size() << std::endl;
158   
159    return false;
160  }
161  classifier::KernelLookup kv2(kernel);
162  if (kv2.rows()!=kernel.size()){
163    *error << "Error: KernelLookup(kernel)\n" << std::endl
164           << "Size of KernelLookup is " << kv.rows() << std::endl
165           << "expected " << kernel.size() << std::endl;
166   
167    return false;
168  }
169
170  return true;
171}
172
173bool test_SEV(const classifier::MatrixLookup& data, 
174              const classifier::KernelFunction* kf, 
175              const utility::matrix& control, const double error_bound,
176              std::ostream* error)
177{
178  *error << "testing Kernel_SEV" << std::endl;
179  classifier::Kernel_SEV kernel(data,*kf);
180  for(u_int i=0;i<control.rows();i++)
181    for(u_int j=0;j<control.columns();j++)
182      if (fabs(kernel(i,j)-control(i,j))>error_bound)
183        return false;
184
185  // checking view
186  std::vector<size_t> index(3);
187  index[0]=1;
188  index[1]=2;
189  index[2]=3;
190  classifier::KernelLookup kv_raw(kernel);
191  classifier::KernelLookup kv(kv_raw,index, index);
192  if (kv.rows()!=index.size()){
193    *error << "Error: KernelLookup(kernel, index)\n" << std::endl
194           << "Size of KernelLookup is " << kv.rows() << std::endl
195           << "expected " << index.size() << std::endl;
196   
197    return false;
198  }
199  classifier::KernelLookup kv2(kernel);
200  if (kv2.rows()!=kernel.size()){
201    *error << "Error: KernelLookup(kernel)\n" << std::endl
202           << "Size of KernelLookup is " << kv.rows() << std::endl
203           << "expected " << kernel.size() << std::endl;
204   
205    return false;
206  }
207  return true;
208}
209
210
Note: See TracBrowser for help on using the repository browser.