DistanceKernel.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2007 Christian Gehl
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/config.h"
00012 
00013 #include "lib/common.h"
00014 #include "lib/io.h"
00015 #include "kernel/DistanceKernel.h"
00016 #include "features/RealFeatures.h"
00017 
00018 CDistanceKernel::CDistanceKernel(int32_t size, float64_t w, CDistance* d)
00019 : CKernel(size), distance(d), width(w)
00020 {
00021     ASSERT(distance);
00022     SG_REF(distance);
00023 }
00024 
00025 CDistanceKernel::CDistanceKernel(
00026     CFeatures *l, CFeatures *r, float64_t w , CDistance* d)
00027 : CKernel(10), distance(d), width(w)
00028 {
00029     ASSERT(distance);
00030     SG_REF(distance);
00031     init(l, r);
00032 }
00033 
00034 CDistanceKernel::~CDistanceKernel()
00035 {
00036     // important to have the cleanup of CKernel first, it calls get_name which
00037     // uses the distance
00038     cleanup();
00039     SG_UNREF(distance);
00040 }
00041 
00042 bool CDistanceKernel::init(CFeatures* l, CFeatures* r)
00043 {
00044     ASSERT(distance);
00045     CKernel::init(l,r);
00046     distance->init(l,r);
00047     return init_normalizer();
00048 }
00049 
00050 float64_t CDistanceKernel::compute(int32_t idx_a, int32_t idx_b)
00051 {
00052     float64_t result=distance->distance(idx_a, idx_b);
00053     return exp(-result/width);
00054 }
00055 
00056 bool CDistanceKernel::load_init(FILE* src)
00057 {
00058     return false;
00059 }
00060 
00061 bool CDistanceKernel::save_init(FILE* dest)
00062 {
00063     return false;
00064 }

SHOGUN Machine Learning Toolbox - Documentation