source: trunk/test/kernel_lookup_test.cc @ 1134

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

using Index class instead of std::vector<size_t>

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1// $Id: kernel_lookup_test.cc 1134 2008-02-23 22:52:43Z 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/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,utility::Index(index_odd),
92                              utility::Index(index_even));
93  if (k2.rows()!=index_odd.size() || k2.columns()!=index_even.size()) {
94    ok =false;
95    *error << "ERROR:" << std::endl;
96    *error << "Dimensions do not agree." << std::endl;
97  }
98  for (size_t i=0; i<k2.rows(); i++) 
99    for (size_t j=0; j<k2.columns(); j++) 
100      if (k2(i,j)!=kernel(index_odd[i],index_even[j])) {
101        ok =false;
102        *error << "ERROR:\n"
103               << "KernelLookup::KernelLookup(const Kernel& data)"
104               << std::endl;
105        *error << "k(" << i << "," << j << ") is " << k2(i,j) 
106               << "expected " << kernel(index_odd[i],index_even[j]) << std::endl;
107      }
108  if (ok)
109    *error << "Ok." << std::endl;
110 
111  *error << "KernelLookup::KernelLookup(const KernelLookup&,\n"
112         << "                           const vector<size_t>&,\n"
113         << "                           const vector<size_t>&)...";
114  std::vector<size_t> one(1,1);
115  classifier::KernelLookup k3(k2,utility::Index(one),utility::Index(one));
116  if (k3.rows()!=one.size() || k3.columns()!=one.size()) {
117    ok =false;
118    *error <<   "ERROR:" << std::endl;
119    *error << "Dimensions do not agree." << std::endl;
120  }
121  else if (k3(0,0)!=k2(1,1)){
122    ok = false;
123    *error << "ERROR:\n k3(0,0) found to be " << k3(0,0) 
124           << " expected " << k2(1,1) << std::endl;
125  }
126  else
127    *error << "Ok." << std::endl;
128
129  *error << "KernelLookup::KernelLookup(const KernelLookup&)...";
130  classifier::KernelLookup k4(k2);
131  if (k4.rows()!=k2.rows() || k4.columns()!=k2.columns()) {
132    ok =false;
133    *error <<   "ERROR:" << std::endl;
134    *error << "Dimensions do not agree." << std::endl;
135    *error << "Dimension: rows " << k4.rows() << " columns " << k4.columns()
136           << "\nOriginal: rows " << k2.rows() << " columns " << k2.columns()
137           << std::endl;
138  }
139  else {
140    for (size_t i=0; i<k4.rows(); i++) 
141      for (size_t j=0; j<k4.columns(); j++) 
142        if (k4(i,j)!=k2(i,j)) {
143          ok =false;
144          *error << "ERROR:\n"
145                 << "KernelLookup::KernelLookup(const KernelLookup&)"
146                 << std::endl;
147          *error << "k(" << i << "," << j << ") is " << k4(i,j) 
148                 << "expected " << k2(i,j) << std::endl;
149        }
150  }
151 
152  KernelLookup k5(k1,utility::Index(index_even),utility::Index(index_even));
153  std::vector<size_t> index5;
154  index5.push_back(0);
155  index5.push_back(2);
156  const KernelLookup* k6 = k5.training_data(utility::Index(index5)); 
157  for (size_t s=0; s<k6->rows(); s++) 
158    for (size_t t=0; t<k6->rows(); t++) 
159      ok = ok && ((*k6)(s,t)==(*k6)(t,s));
160
161
162  if (ok)
163    *error << "Ok." << std::endl;
164
165  if (ok)
166    *error << "Test Ok." << std::endl;
167  if (error!=&std::cerr)
168    delete error;
169  return (ok ? 0 : -1);
170}
171
Note: See TracBrowser for help on using the repository browser.