MindyGramKernel.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) 2006 Konrad Rieck
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/config.h"
00012 
00013 #ifdef HAVE_MINDY
00014 
00015 #include <mindy.h>
00016 
00017 #ifndef _MINDYGRAMKERNEL_H___
00018 #define _MINDYGRAMKERNEL_H___
00019 
00020 #include "lib/common.h"
00021 #include "kernel/Kernel.h"
00022 
00023 /* Similarity coefficients */
00024 #define NO_SICO         -1
00025 
00026 /* Parameter specifications */
00027 typedef struct {
00028         char *name;                                 /* Name of parameter */
00029         int idx;                                        /* Index in param array (see sm.h) */
00030         double val;                                 /* Default value */
00031         char *descr;                                /* Description */
00032 } param_spec_t;
00033 
00034 class CMindyGramKernel: public CKernel
00035 {
00036     public:
00037             /* Constructors */
00038             CMindyGramKernel(INT ch, CHAR *measure, DREAL width);
00039             CMindyGramKernel(CFeatures *l, CFeatures *r, CHAR *measure, DREAL width);
00040             virtual ~CMindyGramKernel();
00041             
00042             /* Set options */
00043             void set_param(CHAR *param);
00044             /* Set MD5 cache size */
00045             void set_md5cache(INT c);
00046             /* Set normalization */
00047             void set_norm(ENormalizationType e);
00048 
00049             /* Init and cleanup functions */
00050             void parse_params(CHAR *);
00051             virtual bool init(CFeatures* l, CFeatures* r);
00052             virtual void cleanup();
00053             virtual void remove_lhs();
00054             virtual void remove_rhs();
00055 
00056             /* Identification functions */
00057             inline virtual EKernelType get_kernel_type() { return K_MINDYGRAM; }
00058             inline virtual EFeatureType get_feature_type() { return F_ULONG; }
00059             inline virtual EFeatureClass get_feature_class() { return C_MINDYGRAM; }
00060             inline virtual const CHAR* get_name() { return "MindyGram"; }
00061 
00062             /* Optimization functions */
00063             virtual bool init_optimization(INT count, INT *IDX, DREAL * weights);
00064             virtual bool delete_optimization();
00065             virtual DREAL compute_optimized(INT idx);
00066             virtual void add_to_normal(INT idx, DREAL weight);
00067             virtual void clear_normal();
00068 
00069             /* Load and ysave functions */
00070             bool load_init(FILE* src);
00071             bool save_init(FILE* dest);
00072 
00073             /* Normalize function for optimization */
00074             inline DREAL normalize_weight(DREAL v, INT i, ENormalizationType n) {
00075                     switch (n) {
00076                             case NO_NORMALIZATION:
00077                                     return v;
00078                             case SQRT_NORMALIZATION:
00079                                     return v / sqrt(sdiag_lhs[i]);
00080                             case FULL_NORMALIZATION:
00081                                     return v / sdiag_lhs[i];
00082                             default:
00083                                     ASSERT(0);
00084                     }
00085                     return -CMath::INFTY;
00086             }
00087 
00088 protected:
00089             /* Kernel function */
00090             DREAL compute(INT idx_a, INT idx_b);
00091 
00092 private:
00093             /* Arrays of kernel matrix diagonals */
00094             DREAL* sdiag_lhs;
00095             DREAL* sdiag_rhs;
00096     
00097             /* Name of similartiy measure */
00098             CHAR *measure;
00099             /* Initialization flag */
00100             bool initialized;
00101             /* Similarity coefficient or 0 */
00102             sico_t simcof;
00103             /* Normalization mode */
00104             ENormalizationType norm;
00105             /* Kernel function (=> Mindy similarity measure) */
00106             sm_t *kernel;
00107             /* Normal vector for optimization */
00108             gram_t *normal;
00109             /* MD5 cache size */
00110             size_t cache;
00111             /* Kernel width */
00112             DREAL width;
00113 
00114 };
00115 #endif /* _MINDYGRAMKERNEL_H__ */
00116 #endif /* HAVE_MINDY */

SHOGUN Machine Learning Toolbox - Documentation