Plif.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 Gunnar Raetsch
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #ifndef __PLIF_H__
00012 #define __PLIF_H__
00013 
00014 #include "lib/common.h"
00015 #include "lib/Mathematics.h"
00016 #include "structure/PlifBase.h"
00017 
00018 enum ETransformType
00019 {
00020     T_LINEAR,
00021     T_LOG,
00022     T_LOG_PLUS1,
00023     T_LOG_PLUS3,
00024     T_LINEAR_PLUS3
00025 };
00026 
00028 class CPlif: public CPlifBase
00029 {
00030     public:
00035         CPlif(int32_t len=0);
00036         virtual ~CPlif();
00037 
00039         void init_penalty_struct_cache();
00040 
00047         float64_t lookup_penalty_svm(
00048             float64_t p_value, float64_t *d_values) const;
00049 
00056         float64_t lookup_penalty(
00057             float64_t p_value, float64_t* svm_values) const;
00058 
00065         float64_t lookup_penalty(int32_t p_value, float64_t* svm_values) const;
00066 
00072         inline float64_t lookup(float64_t p_value)
00073         {
00074             ASSERT(use_svm == 0);
00075             return lookup_penalty(p_value, NULL);
00076         }
00077 
00079         void penalty_clear_derivative();
00080 
00086         void penalty_add_derivative_svm(
00087             float64_t p_value, float64_t* svm_values) ;
00088 
00094         void penalty_add_derivative(float64_t p_value, float64_t* svm_values);
00095 
00101         const float64_t * get_cum_derivative(int32_t & p_len) const
00102         {
00103             p_len = len;
00104             return cum_derivatives;
00105         }
00106 
00112         bool set_transform_type(const char *type_str);
00113 
00118         const char* get_transform_type()
00119         {
00120             if (transform== T_LINEAR)
00121                 return "linear";
00122             else if (transform== T_LOG)
00123                 return "log";
00124             else if (transform== T_LOG_PLUS1)
00125                 return "log(+1)";
00126             else if (transform== T_LOG_PLUS3)
00127                 return "log(+3)";
00128             else if (transform== T_LINEAR_PLUS3)
00129                 return "(+3)";
00130             else 
00131                 SG_ERROR("wrong type");
00132             return "";
00133         }
00134 
00135 
00140         void set_id(int32_t p_id)
00141         {
00142             id=p_id;
00143         }
00144 
00149         int32_t get_id() const
00150         {
00151             return id;
00152         }
00153 
00158         int32_t get_max_id() const
00159         {
00160             return get_id();
00161         }
00162 
00167         void set_use_svm(int32_t p_use_svm)
00168         {
00169             invalidate_cache();
00170             use_svm=p_use_svm;
00171         }
00172 
00177         int32_t get_use_svm() const
00178         {
00179             return use_svm;
00180         }
00181 
00186         virtual bool uses_svm_values() const
00187         {
00188             return (get_use_svm()!=0);
00189         }
00190 
00195         void set_use_cache(int32_t p_use_cache)
00196         {
00197             invalidate_cache();
00198             use_cache=p_use_cache;
00199         }
00200 
00203         void invalidate_cache()
00204         {
00205             delete[] cache;
00206             cache=NULL;
00207         }
00208         
00213         int32_t get_use_cache()
00214         {
00215             return use_cache;
00216         }
00217 
00224         void set_plif(
00225             int32_t p_len, float64_t *p_limits, float64_t* p_penalties)
00226         {
00227             ASSERT(len==p_len);
00228 
00229             for (int32_t i=0; i<len; i++)
00230             {
00231                 limits[i]=p_limits[i];
00232                 penalties[i]=p_penalties[i];
00233             }
00234 
00235             invalidate_cache();
00236             penalty_clear_derivative();
00237         }
00238 
00244         void set_plif_limits(float64_t *p_limits, int32_t p_len)
00245         {
00246             ASSERT(len==p_len);
00247 
00248             for (int32_t i=0; i<len; i++)
00249                 limits[i]=p_limits[i];
00250 
00251             invalidate_cache();
00252             penalty_clear_derivative();
00253         }
00254 
00255 
00261         void set_plif_penalty(float64_t* p_penalties, int32_t p_len)
00262         {
00263             ASSERT(len==p_len);
00264 
00265             for (int32_t i=0; i<len; i++)
00266                 penalties[i]=p_penalties[i];
00267 
00268             invalidate_cache();
00269             penalty_clear_derivative();
00270         }
00271 
00276         void set_plif_length(int32_t p_len)
00277         {
00278             if (len!=p_len)
00279             {
00280                 len=p_len;
00281                 delete[] limits;
00282                 delete[] penalties;
00283                 delete[] cum_derivatives;
00284 
00285                 SG_DEBUG( "set_plif len=%i\n", p_len);
00286                 limits=new float64_t[len];
00287                 penalties=new float64_t[len];
00288                 cum_derivatives=new float64_t[len];
00289             }
00290 
00291             for (int32_t i=0; i<len; i++)
00292             {
00293                 limits[i]=0.0;
00294                 penalties[i]=0.0;
00295             }
00296 
00297             invalidate_cache();
00298             penalty_clear_derivative();
00299         }
00300 
00305         float64_t* get_plif_limits()
00306         {
00307             return limits;
00308         }
00309 
00314         float64_t* get_plif_penalties()
00315         {
00316             return penalties;
00317         }
00322         inline void set_max_value(float64_t p_max_value)
00323         {
00324             max_value=p_max_value;
00325             invalidate_cache();
00326         }
00327 
00332         virtual float64_t get_max_value() const
00333         {
00334             return max_value;
00335         }
00336 
00341         inline void set_min_value(float64_t p_min_value)
00342         {
00343             min_value=p_min_value;
00344             invalidate_cache();
00345         }
00346 
00351         virtual float64_t get_min_value() const
00352         {
00353             return min_value;
00354         }
00355 
00360         void set_plif_name(char *p_name);
00361 
00366         inline char* get_plif_name() const
00367         {
00368             if (name)
00369                 return name;
00370             else
00371             {
00372                 char buf[20];
00373                 sprintf(buf, "plif%i", id);
00374                 //name = strdup(buf);
00375                 return strdup(buf);
00376             }
00377         }
00378 
00383         bool get_do_calc();
00384 
00389         void set_do_calc(bool b);
00390         
00394         void get_used_svms(int32_t* num_svms, int32_t* svm_ids);
00395         
00400         inline int32_t get_plif_len()
00401         {
00402             return len;
00403         }
00404 
00409         virtual void list_plif() const 
00410         {
00411             SG_PRINT("CPlif(min_value=%1.2f, max_value=%1.2f, use_svm=%i)\n", min_value, max_value, use_svm) ;
00412         }
00413 
00415         inline virtual const char* get_name() const { return "Plif"; }
00416 
00417     protected:
00419         int32_t len;
00421         float64_t *limits;
00423         float64_t *penalties;
00425         float64_t *cum_derivatives;
00427         float64_t max_value;
00429         float64_t min_value;
00431         float64_t *cache;
00433         enum ETransformType transform;
00435         int32_t id;
00437         char * name;
00439         int32_t use_svm;
00441         bool use_cache;
00445         bool do_calc;
00446 };
00447 
00448 void delete_penalty_struct(CPlif** PEN, int32_t P);
00449 
00450 #endif

SHOGUN Machine Learning Toolbox - Documentation