CombinedKernel.h

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) 1999-2008 Soeren Sonnenburg
00008  * Written (W) 1999-2008 Gunnar Raetsch
00009  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #ifndef _COMBINEDKERNEL_H___
00013 #define _COMBINEDKERNEL_H___
00014 
00015 #include "lib/List.h"
00016 #include "lib/io.h"
00017 #include "kernel/Kernel.h"
00018 
00019 #include "features/Features.h"
00020 #include "features/CombinedFeatures.h"
00021 
00037 class CCombinedKernel : public CKernel
00038 {
00039     public:
00046         CCombinedKernel(INT size=10, bool append_subkernel_weights=false);
00047 
00055         CCombinedKernel(CCombinedFeatures* l, CCombinedFeatures* r,
00056                 bool append_subkernel_weights=false);
00057 
00058         virtual ~CCombinedKernel();
00059 
00066         virtual bool init(CFeatures* lhs, CFeatures* rhs);
00067 
00069         virtual void cleanup();
00070 
00078         virtual bool load_init(FILE* src) { return false; }
00079 
00087         virtual bool save_init(FILE* dest) { return false; }
00088 
00093         virtual EKernelType get_kernel_type()
00094         {
00095             return K_COMBINED;
00096         }
00097 
00102         virtual EFeatureType get_feature_type()
00103         {
00104             return F_UNKNOWN;
00105         }
00106 
00111         virtual EFeatureClass get_feature_class()
00112         {
00113             return C_COMBINED;
00114         }
00115 
00120         virtual const CHAR* get_name() { return "Combined"; }
00121 
00123         void list_kernels();
00124 
00129         inline CKernel* get_first_kernel()
00130         {
00131             return kernel_list->get_first_element();
00132         }
00133 
00139         inline CKernel* get_first_kernel(CListElement<CKernel*>*&current)
00140         {
00141             return kernel_list->get_first_element(current);
00142         }
00143 
00149         inline CKernel* get_kernel(INT idx)
00150         {
00151             CKernel * k = get_first_kernel();
00152             for (INT i=1; i<idx; i++)
00153                 k = get_next_kernel(k);
00154             return k;
00155         }
00156 
00161         inline CKernel* get_last_kernel()
00162         {
00163             return kernel_list->get_last_element();
00164         }
00165 
00171         inline CKernel* get_next_kernel(const CKernel* current)
00172         {
00173             ASSERT(kernel_list->get_current_element()==current);
00174             return kernel_list->get_next_element();
00175         }
00176 
00182         inline CKernel* get_next_kernel(CListElement<CKernel*> *&current)
00183         {
00184             return kernel_list->get_next_element(current);
00185         }
00186 
00192         inline bool insert_kernel(CKernel* k)
00193         {
00194             ASSERT(k);
00195             SG_REF(k);
00196 
00197             if (!(k->has_property(KP_LINADD)))
00198                 unset_property(KP_LINADD);
00199 
00200             return kernel_list->insert_element(k);
00201         }
00202 
00208         inline bool append_kernel(CKernel* k)
00209         {
00210             ASSERT(k);
00211             SG_REF(k);
00212 
00213             if (!(k->has_property(KP_LINADD)))
00214                 unset_property(KP_LINADD);
00215 
00216             return kernel_list->append_element(k);
00217         }
00218 
00223         inline bool delete_kernel()
00224         {
00225             CKernel* k=kernel_list->delete_element();
00226             SG_UNREF(k);
00227 
00228             return (k!=NULL);
00229         }
00230 
00235         inline bool get_append_subkernel_weights()
00236         {
00237             return append_subkernel_weights;
00238         }
00239 
00244         inline int get_num_subkernels()
00245         {
00246             if (append_subkernel_weights)
00247             {
00248                 INT num_subkernels = 0 ;
00249                 CListElement<CKernel*> *current = NULL ;
00250                 CKernel * kn = get_first_kernel(current) ;
00251                 while(kn)
00252                 {
00253                     num_subkernels += kn->get_num_subkernels() ;
00254                     kn = get_next_kernel(current) ;
00255                 }
00256                 return num_subkernels ;
00257             }
00258             else
00259                 return kernel_list->get_num_elements();
00260         }
00261 
00263         virtual void remove_lhs();
00264 
00266         virtual void remove_rhs();
00267 
00275         virtual bool init_optimization(INT count, INT *IDX, DREAL * weights);
00276 
00281         virtual bool delete_optimization();
00282 
00288         virtual DREAL compute_optimized(INT idx);
00289 
00296         virtual void compute_batch(INT num_vec, INT* vec_idx, DREAL* target, INT num_suppvec, INT* IDX, DREAL* alphas, DREAL factor=1.0);
00297 
00302         static void* compute_optimized_kernel_helper(void* p);
00303 
00308         static void* compute_kernel_helper(void* p);
00309 
00320         void emulate_compute_batch(CKernel* k, INT num_vec, INT* vec_idx, DREAL* target, INT num_suppvec, INT* IDX, DREAL* weights);
00321 
00327         virtual void add_to_normal(INT idx, DREAL weight);
00328 
00330         virtual void clear_normal();
00331 
00337         virtual void compute_by_subkernel(INT idx, DREAL * subkernel_contrib);
00338 
00344         virtual const DREAL* get_subkernel_weights(INT& num_weights);
00345 
00351         virtual void set_subkernel_weights(DREAL* weights, INT num_weights);
00352 
00357         virtual void set_optimization_type(EOptimizationType t);
00358 
00364         virtual void set_precompute_matrix(bool flag, bool subkernel_flag)
00365         {
00366             precompute_matrix = flag; 
00367             precompute_subkernel_matrix = subkernel_flag; 
00368 
00369             if (!precompute_matrix)
00370             {
00371                 delete[] precomputed_matrix;
00372                 precomputed_matrix = NULL;
00373             }
00374             CListElement<CKernel*> *current = NULL ;
00375             CKernel *kn = get_first_kernel(current);
00376             while (kn)
00377             {
00378                 kn->set_precompute_matrix(subkernel_flag,false);
00379                 kn = get_next_kernel(current);
00380             }
00381         }
00382 
00383     protected:
00390         virtual DREAL compute(INT x, INT y);
00391 
00392     protected:
00394         CList<CKernel*>* kernel_list;
00396         INT   sv_count;
00398         INT*  sv_idx;
00400         DREAL* sv_weight;
00402         DREAL* subkernel_weights_buffer;
00404         bool append_subkernel_weights;
00405 };
00406 
00407 #endif /* _COMBINEDKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation