SHOGUN
v3.2.0
|
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-2009 Christian Gehl 00008 * Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/lib/config.h> 00012 00013 #include <shogun/lib/common.h> 00014 #include <shogun/io/SGIO.h> 00015 #include <shogun/kernel/DistanceKernel.h> 00016 #include <shogun/features/DenseFeatures.h> 00017 00018 using namespace shogun; 00019 00020 CDistanceKernel::CDistanceKernel() 00021 : CKernel(0), distance(NULL), width(0.0) 00022 { 00023 register_params(); 00024 } 00025 00026 CDistanceKernel::CDistanceKernel(int32_t size, float64_t w, CDistance* d) 00027 : CKernel(size), distance(d) 00028 { 00029 ASSERT(distance) 00030 set_width(w); 00031 SG_REF(distance); 00032 register_params(); 00033 } 00034 00035 CDistanceKernel::CDistanceKernel( 00036 CFeatures *l, CFeatures *r, float64_t w , CDistance* d) 00037 : CKernel(10), distance(d) 00038 { 00039 set_width(w); 00040 ASSERT(distance) 00041 SG_REF(distance); 00042 init(l, r); 00043 register_params(); 00044 } 00045 00046 CDistanceKernel::~CDistanceKernel() 00047 { 00048 // important to have the cleanup of CKernel first, it calls get_name which 00049 // uses the distance 00050 cleanup(); 00051 SG_UNREF(distance); 00052 } 00053 00054 bool CDistanceKernel::init(CFeatures* l, CFeatures* r) 00055 { 00056 ASSERT(distance) 00057 CKernel::init(l,r); 00058 distance->init(l,r); 00059 return init_normalizer(); 00060 } 00061 00062 float64_t CDistanceKernel::compute(int32_t idx_a, int32_t idx_b) 00063 { 00064 float64_t result=distance->distance(idx_a, idx_b); 00065 return exp(-result/width); 00066 } 00067 00068 void CDistanceKernel::register_params() 00069 { 00070 SG_ADD(&width, "width", "Kernel width.", MS_AVAILABLE); 00071 SG_ADD((CSGObject**) &distance, "distance", "Distance to be used.", 00072 MS_AVAILABLE); 00073 }