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 namespace shogun
00019 {
00020 
00021 enum ETransformType
00022 {
00023     T_LINEAR,
00024     T_LOG,
00025     T_LOG_PLUS1,
00026     T_LOG_PLUS3,
00027     T_LINEAR_PLUS3
00028 };
00029 
00031 class CPlif: public CPlifBase
00032 {
00033     public:
00038         CPlif(int32_t len=0);
00039         virtual ~CPlif();
00040 
00042         void init_penalty_struct_cache();
00043 
00050         float64_t lookup_penalty_svm(
00051             float64_t p_value, float64_t *d_values) const;
00052 
00059         float64_t lookup_penalty(
00060             float64_t p_value, float64_t* svm_values) const;
00061 
00068         float64_t lookup_penalty(int32_t p_value, float64_t* svm_values) const;
00069 
00075         inline float64_t lookup(float64_t p_value)
00076         {
00077             ASSERT(use_svm == 0);
00078             return lookup_penalty(p_value, NULL);
00079         }
00080 
00082         void penalty_clear_derivative();
00083 
00090         void penalty_add_derivative_svm(
00091             float64_t p_value, float64_t* svm_values, float64_t factor) ;
00092 
00099         void penalty_add_derivative(float64_t p_value, float64_t* svm_values, float64_t factor);
00100 
00106         const float64_t * get_cum_derivative(int32_t & p_len) const
00107         {
00108             p_len = len;
00109             return cum_derivatives;
00110         }
00111 
00117         bool set_transform_type(const char *type_str);
00118 
00123         const char* get_transform_type()
00124         {
00125             if (transform== T_LINEAR)
00126                 return "linear";
00127             else if (transform== T_LOG)
00128                 return "log";
00129             else if (transform== T_LOG_PLUS1)
00130                 return "log(+1)";
00131             else if (transform== T_LOG_PLUS3)
00132                 return "log(+3)";
00133             else if (transform== T_LINEAR_PLUS3)
00134                 return "(+3)";
00135             else 
00136                 SG_ERROR("wrong type");
00137             return "";
00138         }
00139 
00140 
00145         void set_id(int32_t p_id)
00146         {
00147             id=p_id;
00148         }
00149 
00154         int32_t get_id() const
00155         {
00156             return id;
00157         }
00158 
00163         int32_t get_max_id() const
00164         {
00165             return get_id();
00166         }
00167 
00172         void set_use_svm(int32_t p_use_svm)
00173         {
00174             invalidate_cache();
00175             use_svm=p_use_svm;
00176         }
00177 
00182         int32_t get_use_svm() const
00183         {
00184             return use_svm;
00185         }
00186 
00191         virtual bool uses_svm_values() const
00192         {
00193             return (get_use_svm()!=0);
00194         }
00195 
00200         void set_use_cache(int32_t p_use_cache)
00201         {
00202             invalidate_cache();
00203             use_cache=p_use_cache;
00204         }
00205 
00208         void invalidate_cache()
00209         {
00210             delete[] cache;
00211             cache=NULL;
00212         }
00213         
00218         int32_t get_use_cache()
00219         {
00220             return use_cache;
00221         }
00222 
00229         void set_plif(
00230             int32_t p_len, float64_t *p_limits, float64_t* p_penalties)
00231         {
00232             ASSERT(len==p_len);
00233 
00234             for (int32_t i=0; i<len; i++)
00235             {
00236                 limits[i]=p_limits[i];
00237                 penalties[i]=p_penalties[i];
00238             }
00239 
00240             invalidate_cache();
00241             penalty_clear_derivative();
00242         }
00243 
00249         void set_plif_limits(float64_t *p_limits, int32_t p_len)
00250         {
00251             if (len!=p_len)
00252                 SG_PRINT("len=%i p_len=%i\n", len, p_len) ;
00253             ASSERT(len==p_len);
00254 
00255             for (int32_t i=0; i<len; i++)
00256                 limits[i]=p_limits[i];
00257 
00258             invalidate_cache();
00259             penalty_clear_derivative();
00260         }
00261 
00262 
00268         void set_plif_penalty(float64_t* p_penalties, int32_t p_len)
00269         {
00270             ASSERT(len==p_len);
00271 
00272             for (int32_t i=0; i<len; i++)
00273                 penalties[i]=p_penalties[i];
00274 
00275             invalidate_cache();
00276             penalty_clear_derivative();
00277         }
00278 
00283         void set_plif_length(int32_t p_len)
00284         {
00285             if (len!=p_len)
00286             {
00287                 len=p_len;
00288                 delete[] limits;
00289                 delete[] penalties;
00290                 delete[] cum_derivatives;
00291 
00292                 SG_DEBUG( "set_plif len=%i\n", p_len);
00293                 limits=new float64_t[len];
00294                 penalties=new float64_t[len];
00295                 cum_derivatives=new float64_t[len];
00296             }
00297 
00298             for (int32_t i=0; i<len; i++)
00299             {
00300                 limits[i]=0.0;
00301                 penalties[i]=0.0;
00302             }
00303 
00304             invalidate_cache();
00305             penalty_clear_derivative();
00306         }
00307 
00312         float64_t* get_plif_limits()
00313         {
00314             return limits;
00315         }
00316 
00321         float64_t* get_plif_penalties()
00322         {
00323             return penalties;
00324         }
00329         inline void set_max_value(float64_t p_max_value)
00330         {
00331             max_value=p_max_value;
00332             invalidate_cache();
00333         }
00334 
00339         virtual float64_t get_max_value() const
00340         {
00341             return max_value;
00342         }
00343 
00348         inline void set_min_value(float64_t p_min_value)
00349         {
00350             min_value=p_min_value;
00351             invalidate_cache();
00352         }
00353 
00358         virtual float64_t get_min_value() const
00359         {
00360             return min_value;
00361         }
00362 
00367         void set_plif_name(char *p_name);
00368 
00373         inline char* get_plif_name() const
00374         {
00375             if (name)
00376                 return name;
00377             else
00378             {
00379                 char buf[20];
00380                 sprintf(buf, "plif%i", id);
00381                 //name = strdup(buf);
00382                 return strdup(buf);
00383             }
00384         }
00385 
00390         bool get_do_calc();
00391 
00396         void set_do_calc(bool b);
00397         
00401         void get_used_svms(int32_t* num_svms, int32_t* svm_ids);
00402         
00407         inline int32_t get_plif_len()
00408         {
00409             return len;
00410         }
00411 
00416         virtual void list_plif() const 
00417         {
00418             SG_PRINT("CPlif(min_value=%1.2f, max_value=%1.2f, use_svm=%i)\n", min_value, max_value, use_svm) ;
00419         }
00420 
00426         static void delete_penalty_struct(CPlif** PEN, int32_t P);
00427 
00429         inline virtual const char* get_name() const { return "Plif"; }
00430 
00431     protected:
00433         int32_t len;
00435         float64_t *limits;
00437         float64_t *penalties;
00439         float64_t *cum_derivatives;
00441         float64_t max_value;
00443         float64_t min_value;
00445         float64_t *cache;
00447         enum ETransformType transform;
00449         int32_t id;
00451         char * name;
00453         int32_t use_svm;
00455         bool use_cache;
00459         bool do_calc;
00460 };
00461 }
00462 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation