PolyMatchStringKernel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "lib/common.h"
00012 #include "lib/io.h"
00013 #include "kernel/PolyMatchStringKernel.h"
00014 #include "features/Features.h"
00015 #include "features/StringFeatures.h"
00016
00017 CPolyMatchStringKernel::CPolyMatchStringKernel(
00018 INT size, INT d, bool i, bool un)
00019 : CStringKernel<CHAR>(size), degree(d), inhomogene(i),
00020 use_normalization(un), sqrtdiag_lhs(NULL), sqrtdiag_rhs(NULL),
00021 initialized(false)
00022 {
00023 }
00024
00025 CPolyMatchStringKernel::CPolyMatchStringKernel(
00026 CStringFeatures<CHAR>* l, CStringFeatures<CHAR>* r, INT d, bool i, bool un)
00027 : CStringKernel<CHAR>(10), degree(d), inhomogene(i), use_normalization(un),
00028 sqrtdiag_lhs(NULL), sqrtdiag_rhs(NULL), initialized(false)
00029 {
00030 init(l, r);
00031 }
00032
00033 CPolyMatchStringKernel::~CPolyMatchStringKernel()
00034 {
00035 cleanup();
00036 }
00037
00038 bool CPolyMatchStringKernel::init(CFeatures* l, CFeatures* r)
00039 {
00040 bool result=CStringKernel<CHAR>::init(l, r);
00041
00042 initialized=false;
00043
00044 if (sqrtdiag_lhs!=sqrtdiag_rhs)
00045 delete[] sqrtdiag_rhs;
00046 sqrtdiag_rhs=NULL;
00047 delete[] sqrtdiag_lhs;
00048 sqrtdiag_lhs=NULL;
00049
00050 if (use_normalization)
00051 {
00052 sqrtdiag_lhs=new DREAL[lhs->get_num_vectors()];
00053
00054 if (l==r)
00055 sqrtdiag_rhs=sqrtdiag_lhs;
00056 else
00057 sqrtdiag_rhs=new DREAL[rhs->get_num_vectors()];
00058
00059 this->lhs=(CStringFeatures<CHAR>*) l;
00060 this->rhs=(CStringFeatures<CHAR>*) l;
00061
00062 CKernel::init_sqrt_diag(sqrtdiag_lhs, lhs->get_num_vectors());
00063
00064
00065
00066 if (sqrtdiag_lhs!=sqrtdiag_rhs)
00067 {
00068 this->lhs=(CStringFeatures<CHAR>*) r;
00069 this->rhs=(CStringFeatures<CHAR>*) r;
00070
00071 CKernel::init_sqrt_diag(sqrtdiag_rhs, rhs->get_num_vectors());
00072 }
00073 }
00074
00075 this->lhs=(CStringFeatures<CHAR>*) l;
00076 this->rhs=(CStringFeatures<CHAR>*) r;
00077
00078 initialized=true;
00079 return result;
00080 }
00081
00082 void CPolyMatchStringKernel::cleanup()
00083 {
00084 if (sqrtdiag_lhs != sqrtdiag_rhs)
00085 delete[] sqrtdiag_rhs;
00086 sqrtdiag_rhs = NULL;
00087
00088 delete[] sqrtdiag_lhs;
00089 sqrtdiag_lhs = NULL;
00090
00091 initialized = false;
00092
00093 CKernel::cleanup();
00094 }
00095
00096 bool CPolyMatchStringKernel::load_init(FILE *src)
00097 {
00098 return false;
00099 }
00100
00101 bool CPolyMatchStringKernel::save_init(FILE *dest)
00102 {
00103 return false;
00104 }
00105
00106 DREAL CPolyMatchStringKernel::compute(INT idx_a, INT idx_b)
00107 {
00108 INT i, alen, blen, sum;
00109
00110
00111 CHAR* avec = ((CStringFeatures<CHAR>*) lhs)->get_feature_vector(idx_a, alen);
00112 CHAR* bvec = ((CStringFeatures<CHAR>*) rhs)->get_feature_vector(idx_b, blen);
00113
00114 ASSERT(alen==blen);
00115 DREAL sqrt = (initialized && use_normalization)?
00116 sqrtdiag_lhs[idx_a]*sqrtdiag_rhs[idx_b] : 1;
00117 for (i = 0, sum = inhomogene; i<alen; i++)
00118 if (avec[i]==bvec[i])
00119 sum++;
00120 return pow(sum, degree) / sqrt;
00121 }