SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CombinedKernel.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #ifndef _COMBINEDKERNEL_H___
13 #define _COMBINEDKERNEL_H___
14 
15 #include <shogun/lib/List.h>
16 #include <shogun/io/SGIO.h>
17 #include <shogun/kernel/Kernel.h>
18 
21 
22 namespace shogun
23 {
24 class CFeatures;
25 class CCombinedFeatures;
26 class CList;
27 class CListElement;
46 class CCombinedKernel : public CKernel
47 {
48  public:
55  CCombinedKernel(int32_t size=10, bool append_subkernel_weights=false);
56 
57  virtual ~CCombinedKernel();
58 
65  virtual bool init(CFeatures* lhs, CFeatures* rhs);
66 
68  virtual void cleanup();
69 
75  {
76  return K_COMBINED;
77  }
78 
84  {
85  return F_UNKNOWN;
86  }
87 
93  {
94  return C_COMBINED;
95  }
96 
101  virtual const char* get_name() const { return "CombinedKernel"; }
102 
104  void list_kernels();
105 
111  {
113  }
114 
121  {
122  return (CKernel*) kernel_list->get_first_element(current);
123  }
124 
130  inline CKernel* get_kernel(int32_t idx)
131  {
132  CKernel * k = get_first_kernel();
133  for (int32_t i=0; i<idx; i++)
134  {
135  SG_UNREF(k);
136  k = get_next_kernel();
137  }
138  return k;
139  }
140 
146  {
147  return (CKernel*) kernel_list->get_last_element();
148  }
149 
155  {
156  return (CKernel*) kernel_list->get_next_element();
157  }
158 
165  {
166  return (CKernel*) kernel_list->get_next_element(current);
167  }
168 
174  inline bool insert_kernel(CKernel* k)
175  {
176  ASSERT(k);
178 
179  if (!(k->has_property(KP_LINADD)))
181 
182  return kernel_list->insert_element(k);
183  }
184 
190  inline bool append_kernel(CKernel* k)
191  {
192  ASSERT(k);
194 
195  if (!(k->has_property(KP_LINADD)))
197 
198  return kernel_list->append_element(k);
199  }
200 
201 
206  inline bool delete_kernel()
207  {
209  SG_UNREF(k);
210 
211  if (!k)
212  {
213  num_lhs=0;
214  num_rhs=0;
215  }
216 
217  return (k!=NULL);
218  }
219 
225  {
227  }
228 
233  inline int32_t get_num_subkernels()
234  {
236  {
237  int32_t num_subkernels = 0 ;
238  CListElement* current = NULL ;
239  CKernel * k = get_first_kernel(current) ;
240 
241  while(k)
242  {
243  num_subkernels += k->get_num_subkernels() ;
244  SG_UNREF(k);
245  k = get_next_kernel(current) ;
246  }
247  return num_subkernels ;
248  }
249  else
250  return kernel_list->get_num_elements();
251  }
252 
257  virtual inline bool has_features()
258  {
259  return initialized;
260  }
261 
263  virtual void remove_lhs();
264 
266  virtual void remove_rhs();
267 
269  virtual void remove_lhs_and_rhs();
270 
278  virtual bool init_optimization(
279  int32_t count, int32_t *IDX, float64_t * weights);
280 
285  virtual bool delete_optimization();
286 
292  virtual float64_t compute_optimized(int32_t idx);
293 
300  virtual void compute_batch(
301  int32_t num_vec, int32_t* vec_idx, float64_t* target,
302  int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
303  float64_t factor=1.0);
304 
309  static void* compute_optimized_kernel_helper(void* p);
310 
315  static void* compute_kernel_helper(void* p);
316 
328  CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* target,
329  int32_t num_suppvec, int32_t* IDX, float64_t* weights);
330 
336  virtual void add_to_normal(int32_t idx, float64_t weight);
337 
339  virtual void clear_normal();
340 
346  virtual void compute_by_subkernel(
347  int32_t idx, float64_t * subkernel_contrib);
348 
354  virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
355 
361 
366  virtual void set_subkernel_weights(SGVector<float64_t> weights);
367 
373 
375  bool precompute_subkernels();
376 
381  {
382  return dynamic_cast<CCombinedKernel*>(n);
383  }
384 
394  CSGObject* obj, index_t index);
395 
401 
402  protected:
409  virtual float64_t compute(int32_t x, int32_t y);
410 
417  {
418  ASSERT(k);
419 
420  if (k->get_num_vec_lhs())
421  {
422  if (num_lhs)
425 
426  if (!get_num_subkernels())
427  {
428  initialized=true;
429 #ifdef USE_SVMLIGHT
430  cache_reset();
431 #endif //USE_SVMLIGHT
432  }
433  }
434  else
435  initialized=false;
436 
437  if (k->get_num_vec_rhs())
438  {
439  if (num_rhs)
442 
443  if (!get_num_subkernels())
444  {
445  initialized=true;
446 #ifdef USE_SVMLIGHT
447  cache_reset();
448 #endif //USE_SVMLIGHT
449  }
450  }
451  else
452  initialized=false;
453  }
454 
455  private:
456  void init();
457 
458  protected:
462  int32_t sv_count;
464  int32_t* sv_idx;
473 };
474 }
475 #endif /* _COMBINEDKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation