File: Plif.h

package info (click to toggle)
shogun 0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 8,688 kB
  • ctags: 6,563
  • sloc: cpp: 61,677; python: 5,233; sh: 2,767; makefile: 555; objc: 37
file content (444 lines) | stat: -rw-r--r-- 7,880 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 1999-2008 Gunnar Raetsch
 * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
 */

#ifndef __PLIF_H__
#define __PLIF_H__

#include "lib/common.h"
#include "lib/Mathematics.h"
#include "structure/PlifBase.h"

#include "lib/matlab.h"

enum ETransformType
{
	T_LINEAR,
	T_LOG,
	T_LOG_PLUS1,
	T_LOG_PLUS3,
	T_LINEAR_PLUS3
};

/** class Plif */
class CPlif: public CPlifBase
{
	public:
		/** constructor
		 *
		 * @param len len
		 */
		CPlif(INT len=0);
		~CPlif();

		/** init penalty struct cache */
		void init_penalty_struct_cache();

		/** lookup penalty SVM
		 *
		 * @param p_value value
		 * @param d_values d values
		 * @return the penalty
		 */
		DREAL lookup_penalty_svm(DREAL p_value, DREAL *d_values) const;

		/** lookup penalty DREAL
		 *
		 * @param p_value value
		 * @param svm_values SVM values
		 * @return the penalty
		 */
		DREAL lookup_penalty(DREAL p_value, DREAL* svm_values) const;

		/** lookup penalty INT
		 *
		 * @param p_value value
		 * @param svm_values SVM values
		 * @return the penalty
		 */
		DREAL lookup_penalty(INT p_value, DREAL* svm_values) const;

		/** lookup
		 *
		 * @param p_value value
		 * @return a penalty
		 */
		inline DREAL lookup(DREAL p_value)
		{
			ASSERT(use_svm == 0);
			return lookup_penalty(p_value, NULL);
		}

		/** penalty clear derivative */
		void penalty_clear_derivative();

		/** penalty add derivative SVM
		 *
		 * @param p_value value
		 * @param svm_values SVM values
		 */
		void penalty_add_derivative_svm(DREAL p_value, DREAL* svm_values) ;

		/** penalty add derivative
		 *
		 * @param p_value value
		 * @param svm_values SVM values
		 */
		void penalty_add_derivative(DREAL p_value, DREAL* svm_values) ;

		/** get cum derivative
		 *
		 * @param p_len len
		 * @return cum derivative
		 */
		const DREAL * get_cum_derivative(INT & p_len) const
		{
			p_len = len;
			return cum_derivatives;
		}

		/** set transform type
		 *
		 * @param type_str type (string)
		 * @return if setting was succesful
		 */
		bool set_transform_type(const char *type_str);

		/** get transform type
		 *
		 * @return type_str type (string)
		 */
		const char* get_transform_type()
		{
			if (transform== T_LINEAR)
				return "linear";
			else if (transform== T_LOG)
				return "log";
			else if (transform== T_LOG_PLUS1)
				return "log(+1)";
			else if (transform== T_LOG_PLUS3)
				return "log(+3)";
			else if (transform== T_LINEAR_PLUS3)
				return "(+3)";
			else 
				SG_ERROR("wrong type");
			return "";
		}


		/** set ID
		 *
		 * @param p_id the id to set
		 */
		void set_id(INT p_id)
		{
			id=p_id;
		}

		/** get ID
		 *
		 * @return the ID
		 */
		INT get_id() const
		{
			return id;
		}

		/** get maximum ID
		 *
		 * @return maximum ID
		 */
		INT get_max_id() const
		{
			return get_id();
		}

		/** set use SVM
		 *
		 * @param p_use_svm if SVM shall be used
		 */
		void set_use_svm(INT p_use_svm)
		{
			delete[] cache;
			cache=NULL;
			use_svm=p_use_svm;
		}

		/** get use SVM
		 *
		 * @return if SVM is used
		 */
		INT get_use_svm() const
		{
			return use_svm;
		}

		/** check if plif uses SVM values
		 *
		 * @return if plif uses SVM values
		 */
		virtual bool uses_svm_values() const
		{
			return (get_use_svm()!=0);
		}

		/** set use cache
		 *
		 * @param p_use_cache if cache shall be used
		 */
		void set_use_cache(INT p_use_cache)
		{
			delete[] cache;
			cache=NULL;
			use_cache=p_use_cache;
		}
		
		/** get use cache
		 *
		 * @return if cache is used
		 */
		INT get_use_cache()
		{
			return use_cache;
		}

		/** set plif
		 *
		 * for swig use set_plif_len, set_plif_limits, set_plif_penalty
		 *
		 * @param p_len len
		 * @param p_limits limit
		 * @param p_penalties penalties
		 */
		void set_plif(INT p_len, DREAL *p_limits, DREAL* p_penalties)
		{
			len=p_len;
			delete[] limits;
			delete[] penalties;
			delete[] cum_derivatives;
			delete[] cache;
			cache=NULL;

			limits=new DREAL[len];
			penalties=new DREAL[len];
			cum_derivatives=new DREAL[len];

			for (INT i=0; i<len; i++)
			{
				limits[i]=p_limits[i];
				penalties[i]=p_penalties[i];
			}

			penalty_clear_derivative();
		}

		/** set plif length
		 *
		 * @param p_len len
		 */
		void set_plif_length(INT p_len)
		{
			if (len!=p_len)
			{
				len=p_len;
				delete[] limits;
				delete[] penalties;
				delete[] cum_derivatives;
				SG_DEBUG( "set_plif len=%i\n", p_len);
				limits=new DREAL[len];
				penalties=new DREAL[len];
				cum_derivatives=new DREAL[len];
			}
			delete[] cache;
			cache=NULL;
			for (INT i=0; i<len; i++)
			{
				limits[i]=0.0;
				penalties[i]=0.0;
			}
			penalty_clear_derivative();
		}

		/** set plif limits
		 *
		 * @param p_limits limits
		 * @param p_len len
		 */
		void set_plif_limits(DREAL* p_limits, INT p_len)
		{
			delete[] cache;
			cache=NULL;
			ASSERT(len==p_len);

			for (INT i=0; i<len; i++)
				limits[i]=p_limits[i];

			penalty_clear_derivative();
		}

		/** get Plif limits
		 *
		 * @return limits
		 */
		DREAL* get_plif_limits()
		{
			return limits;
		}

		/** set plif penalty
		 *
		 * @param p_penalties penalties
		 * @param p_len len
		 */
		void set_plif_penalty(DREAL* p_penalties, INT p_len)
		{
			delete[] cache;
			cache=NULL;
			ASSERT(len==p_len);

			for (INT i=0; i<len; i++)
				penalties[i]=p_penalties[i];

			penalty_clear_derivative();
		}
		/** get plif penalty
 		 *	
 		 * @return plif penalty
 		 */ 
		DREAL* get_plif_penalties()
		{
			return penalties;
		}
		/** set maximum value
		 *
		 * @param p_max_value maximum value
		 */
		inline void set_max_value(DREAL p_max_value)
		{
			delete[] cache;
			cache=NULL;
			max_value=p_max_value;
		}

		/** get maximum value
		 *
		 * @return maximum value
		 */
		virtual DREAL get_max_value() const
		{
			return max_value;
		}

		/** set minimum value
		 *
		 * @param p_min_value minimum value
		 */
		inline void set_min_value(DREAL p_min_value)
		{
			delete[] cache;
			cache=NULL;
			min_value=p_min_value;
		}

		/** get minimum value
		 *
		 * @return minimum value
		 */
		virtual DREAL get_min_value() const
		{
			return min_value;
		}

		/** set name
		 *
		 * @param p_name name
		 */
		void set_name(char *p_name);

		/** get name
		 *
		 * @return name
		 */
		inline char * get_name() const
		{
			if (name)
				return name;
			else
			{
				char buf[20];
				sprintf(buf, "plif%i", id);
				//name = strdup(buf);
				return strdup(buf);
			}
		}

		/** get do calc
		 *
		 * @return if calc shall be done
		 */
		bool get_do_calc();

		/** set do calc
		 *
		 * @param b if calc shall be done
		 */
		void set_do_calc(bool b);
		
		/** get SVM_ids and number of SVMs used
		 *
		 */
		void get_used_svms(INT* num_svms, INT* svm_ids);
		
		/** get plif len
		 *
		 * @return plif len
		 */
		inline INT get_plif_len()
		{
			return len;
		}

	protected:
		/** len */
		INT len;
		/** limits */
		DREAL *limits;
		/** penalties */
		DREAL *penalties;
		/** cum derivatives */
		DREAL *cum_derivatives;
		/** maximum value */
		DREAL max_value;
		/** minimum value */
		DREAL min_value;
		/** cache */
		DREAL *cache;
		/** transform type */
		enum ETransformType transform;
		/** id */
		INT id;
		/** name */
		char * name;
		/** if SVM shall be used */
		INT use_svm;
		/** if cache shall be used */
		bool use_cache;
		/** do calc
		 *  if this is true: lookup_penalty behaves normal
		 *  else: lookup_penalty returns the p_value untransformed*/
		bool do_calc;
};

#ifdef HAVE_MATLAB
CPlif** read_penalty_struct_from_cell(const mxArray * mx_penalty_info, INT P) ;
#endif

void delete_penalty_struct(CPlif** PEN, INT P) ;

#endif