source: trunk/test/kernel_lookup_test.cc @ 865

Last change on this file since 865 was 865, checked in by Peter, 16 years ago

changing URL to http://trac.thep.lu.se/trac/yat

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1// $Id: kernel_lookup_test.cc 865 2007-09-10 19:41:04Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of the yat library, http://trac.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/DataLookup1D.h"
26#include "yat/classifier/KernelLookup.h"
27#include "yat/classifier/Kernel_SEV.h"
28#include "yat/classifier/MatrixLookup.h"
29#include "yat/classifier/PolynomialKernelFunction.h"
30
31#include <fstream>
32#include <iostream>
33#include <vector>
34
35using namespace theplu::yat;
36
37int main(const int argc,const char* argv[])
38{
39  using namespace theplu::yat::classifier;
40
41  std::ostream* error;
42  if (argc>1 && argv[1]==std::string("-v"))
43    error = &std::cerr;
44  else {
45    error = new std::ofstream("/dev/null");
46    if (argc>1)
47      std::cout << "lookup_test -v : for printing extra information\n";
48  }
49
50  bool ok =true;
51  *error << "\nTesting KernelLookup" << std::endl;
52  utility::matrix data_core(1,5);
53  for (size_t i=0; i<data_core.columns(); i++)
54    data_core(0,i)=i;
55  classifier::MatrixLookup data(data_core);
56  classifier::PolynomialKernelFunction kf;
57  classifier::Kernel_SEV kernel(data,kf);
58
59  *error << "KernelLookup::KernelLookup(const Kernel&)...";
60  classifier::KernelLookup k1(kernel);
61  if (k1.rows()!=kernel.size() || k1.columns()!=kernel.size()) {
62    ok =false;
63    *error <<   "ERROR:" << std::endl;
64    *error << "Dimensions do not agree." << std::endl;
65  }
66  else {
67    for (size_t i=0; i<k1.rows(); i++) 
68      for (size_t j=0; j<k1.columns(); j++) 
69        if (k1(i,j)!=kernel(i,j)) {
70          ok =false;
71          *error << "ERROR:\n"
72                 << "KernelLookup::KernelLookup(const Kernel& data)"
73                 << std::endl;
74          *error << "k(" << i << "," << j << ") is " << k1(i,j) 
75                 << "expected " << kernel(i,j) << std::endl;
76        }
77    if (ok)
78      *error << "Ok." << std::endl;
79  }
80
81  std::vector<size_t> index_odd;
82  index_odd.push_back(1);
83  index_odd.push_back(3);
84  std::vector<size_t> index_even;
85  index_even.push_back(2);
86  index_even.push_back(0);
87  index_even.push_back(4);
88  *error << "KernelLookup::KernelLookup(const Kernel&,\n"
89         << "                           const vector<size_t>&,\n"
90         << "                           const vector<size_t>&)...";
91  classifier::KernelLookup k2(kernel,index_odd,index_even);
92  if (k2.rows()!=index_odd.size() || k2.columns()!=index_even.size()) {
93    ok =false;
94    *error << "ERROR:" << std::endl;
95    *error << "Dimensions do not agree." << std::endl;
96  }
97  for (size_t i=0; i<k2.rows(); i++) 
98    for (size_t j=0; j<k2.columns(); j++) 
99      if (k2(i,j)!=kernel(index_odd[i],index_even[j])) {
100        ok =false;
101        *error << "ERROR:\n"
102               << "KernelLookup::KernelLookup(const Kernel& data)"
103               << std::endl;
104        *error << "k(" << i << "," << j << ") is " << k2(i,j) 
105               << "expected " << kernel(index_odd[i],index_even[j]) << std::endl;
106      }
107  if (ok)
108    *error << "Ok." << std::endl;
109 
110  *error << "KernelLookup::KernelLookup(const KernelLookup&,\n"
111         << "                           const vector<size_t>&,\n"
112         << "                           const vector<size_t>&)...";
113  std::vector<size_t> one(1,1);
114  classifier::KernelLookup k3(k2,one,one);
115  if (k3.rows()!=one.size() || k3.columns()!=one.size()) {
116    ok =false;
117    *error <<   "ERROR:" << std::endl;
118    *error << "Dimensions do not agree." << std::endl;
119  }
120  else if (k3(0,0)!=k2(1,1)){
121    ok = false;
122    *error << "ERROR:\n k3(0,0) found to be " << k3(0,0) 
123           << " expected " << k2(1,1) << std::endl;
124  }
125  else
126    *error << "Ok." << std::endl;
127
128  *error << "KernelLookup::KernelLookup(const KernelLookup&)...";
129  classifier::KernelLookup k4(k2);
130  if (k4.rows()!=k2.rows() || k4.columns()!=k2.columns()) {
131    ok =false;
132    *error <<   "ERROR:" << std::endl;
133    *error << "Dimensions do not agree." << std::endl;
134    *error << "Dimension: rows " << k4.rows() << " columns " << k4.columns()
135           << "\nOriginal: rows " << k2.rows() << " columns " << k2.columns()
136           << std::endl;
137  }
138  else {
139    for (size_t i=0; i<k4.rows(); i++) 
140      for (size_t j=0; j<k4.columns(); j++) 
141        if (k4(i,j)!=k2(i,j)) {
142          ok =false;
143          *error << "ERROR:\n"
144                 << "KernelLookup::KernelLookup(const KernelLookup&)"
145                 << std::endl;
146          *error << "k(" << i << "," << j << ") is " << k4(i,j) 
147                 << "expected " << k2(i,j) << std::endl;
148        }
149  }
150 
151  KernelLookup k5(k1,index_even,index_even);
152  std::vector<size_t> index5;
153  index5.push_back(0);
154  index5.push_back(2);
155  const KernelLookup* k6 = k5.training_data(index5); 
156  for (size_t s=0; s<k6->rows(); s++) 
157    for (size_t t=0; t<k6->rows(); t++) 
158      ok = ok && ((*k6)(s,t)==(*k6)(t,s));
159
160
161  if (ok)
162    *error << "Ok." << std::endl;
163
164  if (ok)
165    *error << "Test Ok." << std::endl;
166  if (error!=&std::cerr)
167    delete error;
168  return (ok ? 0 : -1);
169}
170
Note: See TracBrowser for help on using the repository browser.