Plif.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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 #include "lib/matlab.h"
00019
00020 enum ETransformType
00021 {
00022 T_LINEAR,
00023 T_LOG,
00024 T_LOG_PLUS1,
00025 T_LOG_PLUS3,
00026 T_LINEAR_PLUS3
00027 };
00028
00030 class CPlif: public CPlifBase
00031 {
00032 public:
00037 CPlif(INT len=0);
00038 ~CPlif();
00039
00041 void init_penalty_struct_cache();
00042
00049 DREAL lookup_penalty_svm(DREAL p_value, DREAL *d_values) const;
00050
00057 DREAL lookup_penalty(DREAL p_value, DREAL* svm_values) const;
00058
00065 DREAL lookup_penalty(INT p_value, DREAL* svm_values) const;
00066
00072 inline DREAL lookup(DREAL 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(DREAL p_value, DREAL* svm_values) ;
00087
00093 void penalty_add_derivative(DREAL p_value, DREAL* svm_values) ;
00094
00100 const DREAL * get_cum_derivative(INT & p_len) const
00101 {
00102 p_len = len;
00103 return cum_derivatives;
00104 }
00105
00111 bool set_transform_type(const char *type_str);
00112
00117 const char* get_transform_type()
00118 {
00119 if (transform== T_LINEAR)
00120 return "linear";
00121 else if (transform== T_LOG)
00122 return "log";
00123 else if (transform== T_LOG_PLUS1)
00124 return "log(+1)";
00125 else if (transform== T_LOG_PLUS3)
00126 return "log(+3)";
00127 else if (transform== T_LINEAR_PLUS3)
00128 return "(+3)";
00129 else
00130 SG_ERROR("wrong type");
00131 return "";
00132 }
00133
00134
00139 void set_id(INT p_id)
00140 {
00141 id=p_id;
00142 }
00143
00148 INT get_id() const
00149 {
00150 return id;
00151 }
00152
00157 INT get_max_id() const
00158 {
00159 return get_id();
00160 }
00161
00166 void set_use_svm(INT p_use_svm)
00167 {
00168 delete[] cache;
00169 cache=NULL;
00170 use_svm=p_use_svm;
00171 }
00172
00177 INT 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(INT p_use_cache)
00196 {
00197 delete[] cache;
00198 cache=NULL;
00199 use_cache=p_use_cache;
00200 }
00201
00206 INT get_use_cache()
00207 {
00208 return use_cache;
00209 }
00210
00219 void set_plif(INT p_len, DREAL *p_limits, DREAL* p_penalties)
00220 {
00221 len=p_len;
00222 delete[] limits;
00223 delete[] penalties;
00224 delete[] cum_derivatives;
00225 delete[] cache;
00226 cache=NULL;
00227
00228 limits=new DREAL[len];
00229 penalties=new DREAL[len];
00230 cum_derivatives=new DREAL[len];
00231
00232 for (INT i=0; i<len; i++)
00233 {
00234 limits[i]=p_limits[i];
00235 penalties[i]=p_penalties[i];
00236 }
00237
00238 penalty_clear_derivative();
00239 }
00240
00245 void set_plif_length(INT p_len)
00246 {
00247 if (len!=p_len)
00248 {
00249 len=p_len;
00250 delete[] limits;
00251 delete[] penalties;
00252 delete[] cum_derivatives;
00253 SG_DEBUG( "set_plif len=%i\n", p_len);
00254 limits=new DREAL[len];
00255 penalties=new DREAL[len];
00256 cum_derivatives=new DREAL[len];
00257 }
00258 delete[] cache;
00259 cache=NULL;
00260 for (INT i=0; i<len; i++)
00261 {
00262 limits[i]=0.0;
00263 penalties[i]=0.0;
00264 }
00265 penalty_clear_derivative();
00266 }
00267
00273 void set_plif_limits(DREAL* p_limits, INT p_len)
00274 {
00275 delete[] cache;
00276 cache=NULL;
00277 ASSERT(len==p_len);
00278
00279 for (INT i=0; i<len; i++)
00280 limits[i]=p_limits[i];
00281
00282 penalty_clear_derivative();
00283 }
00284
00289 DREAL* get_plif_limits()
00290 {
00291 return limits;
00292 }
00293
00299 void set_plif_penalty(DREAL* p_penalties, INT p_len)
00300 {
00301 delete[] cache;
00302 cache=NULL;
00303 ASSERT(len==p_len);
00304
00305 for (INT i=0; i<len; i++)
00306 penalties[i]=p_penalties[i];
00307
00308 penalty_clear_derivative();
00309 }
00314 DREAL* get_plif_penalties()
00315 {
00316 return penalties;
00317 }
00322 inline void set_max_value(DREAL p_max_value)
00323 {
00324 delete[] cache;
00325 cache=NULL;
00326 max_value=p_max_value;
00327 }
00328
00333 virtual DREAL get_max_value() const
00334 {
00335 return max_value;
00336 }
00337
00342 inline void set_min_value(DREAL p_min_value)
00343 {
00344 delete[] cache;
00345 cache=NULL;
00346 min_value=p_min_value;
00347 }
00348
00353 virtual DREAL get_min_value() const
00354 {
00355 return min_value;
00356 }
00357
00362 void set_name(char *p_name);
00363
00368 inline char * get_name() const
00369 {
00370 if (name)
00371 return name;
00372 else
00373 {
00374 char buf[20];
00375 sprintf(buf, "plif%i", id);
00376
00377 return strdup(buf);
00378 }
00379 }
00380
00385 bool get_do_calc();
00386
00391 void set_do_calc(bool b);
00392
00396 void get_used_svms(INT* num_svms, INT* svm_ids);
00397
00402 inline INT get_plif_len()
00403 {
00404 return len;
00405 }
00406
00407 protected:
00409 INT len;
00411 DREAL *limits;
00413 DREAL *penalties;
00415 DREAL *cum_derivatives;
00417 DREAL max_value;
00419 DREAL min_value;
00421 DREAL *cache;
00423 enum ETransformType transform;
00425 INT id;
00427 char * name;
00429 INT use_svm;
00431 bool use_cache;
00435 bool do_calc;
00436 };
00437
00438 #ifdef HAVE_MATLAB
00439 CPlif** read_penalty_struct_from_cell(const mxArray * mx_penalty_info, INT P) ;
00440 #endif
00441
00442 void delete_penalty_struct(CPlif** PEN, INT P) ;
00443
00444 #endif