SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SGObject.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) 2008-2010 Soeren Sonnenburg
8  * Written (W) 2011-2013 Heiko Strathmann
9  * Written (W) 2013-2014 Thoralf Klein
10  * Copyright (C) 2008-2010 Fraunhofer Institute FIRST and Max Planck Society
11  */
12 
13 #ifndef __SGOBJECT_H__
14 #define __SGOBJECT_H__
15 
16 #include <shogun/lib/config.h>
17 #include <shogun/lib/common.h>
18 #include <shogun/lib/DataType.h>
20 #include <shogun/base/Version.h>
21 #include <shogun/base/unique.h>
22 #include <shogun/io/SGIO.h>
23 #include <shogun/lib/tag.h>
24 #include <shogun/lib/any.h>
25 
29 namespace shogun
30 {
31 class RefCount;
32 class SGIO;
33 class Parallel;
34 class Parameter;
35 class ParameterMap;
36 class SGParamInfo;
37 class CSerializableFile;
38 
39 template <class T, class K> class CMap;
40 
41 struct TParameter;
42 template <class T> class DynArray;
43 template <class T> class SGStringList;
44 
45 /*******************************************************************************
46  * define reference counter macros
47  ******************************************************************************/
48 
49 #ifdef USE_REFERENCE_COUNTING
50 #define SG_REF(x) { if (x) (x)->ref(); }
51 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
52 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
53 #else
54 #define SG_REF(x)
55 #define SG_UNREF(x)
56 #define SG_UNREF_NO_NULL(x)
57 #endif
58 
59 /*******************************************************************************
60  * Macros for registering parameters/model selection parameters
61  ******************************************************************************/
62 
63 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
64 #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
65 
66 #define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
67 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
68 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
69 
70 #define SG_ADD4(param, name, description, ms_available) {\
71  m_parameters->add(param, name, description);\
72  if (ms_available)\
73  m_model_selection_parameters->add(param, name, description);\
74 }
75 
76 #define SG_ADD5(param, name, description, ms_available, gradient_available) {\
77  m_parameters->add(param, name, description);\
78  if (ms_available)\
79  m_model_selection_parameters->add(param, name, description);\
80  if (gradient_available)\
81  m_gradient_parameters->add(param, name, description);\
82 }
83 
84 #define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__)
85 
86 /*******************************************************************************
87  * End of macros for registering parameters/model selection parameters
88  ******************************************************************************/
89 
94 };
95 
98 {
101 };
102 
116 {
117 public:
119  CSGObject();
120 
122  CSGObject(const CSGObject& orig);
123 
125  virtual ~CSGObject();
126 
127 #ifdef USE_REFERENCE_COUNTING
128 
132  int32_t ref();
133 
138  int32_t ref_count();
139 
145  int32_t unref();
146 #endif //USE_REFERENCE_COUNTING
147 
148 #ifdef TRACE_MEMORY_ALLOCS
149  static void list_memory_allocs();
150 #endif
151 
155  virtual CSGObject *shallow_copy() const;
156 
160  virtual CSGObject *deep_copy() const;
161 
167  virtual const char* get_name() const = 0;
168 
177  virtual bool is_generic(EPrimitiveType* generic) const;
178 
181  template<class T> void set_generic();
182 
187  void unset_generic();
188 
193  virtual void print_serializable(const char* prefix="");
194 
202  virtual bool save_serializable(CSerializableFile* file,
203  const char* prefix="");
204 
214  virtual bool load_serializable(CSerializableFile* file,
215  const char* prefix="");
216 
221  void set_global_io(SGIO* io);
222 
227  SGIO* get_global_io();
228 
234 
240 
246 
252 
256 
258  void print_modsel_params();
259 
266  char* get_modsel_param_descr(const char* param_name);
267 
274  index_t get_modsel_param_index(const char* param_name);
275 
283 
289  bool has(const std::string& name) const
290  {
291  BaseTag tag(name);
292  return has_with_base_tag(tag);
293  }
294 
300  template <typename T>
301  bool has(const Tag<T>& tag) const
302  {
303  return has<T>(tag.name());
304  }
305 
311  template <typename T, typename U=void>
312  bool has(const std::string& name) const
313  {
314  BaseTag tag(name);
315  if(!has_with_base_tag(tag))
316  return false;
317  const Any value = get_with_base_tag(tag);
318  return value.same_type<T>();
319  }
320 
327  template <typename T>
328  void set(const Tag<T>& _tag, const T& value)
329  {
330  if(has_with_base_tag(_tag))
331  {
332  if(has<T>(_tag.name()))
333  set_with_base_tag(_tag, erase_type(value));
334  else
335  {
336  SG_ERROR("Type for parameter with name \"%s\" is not correct.\n",
337  _tag.name().c_str());
338  }
339  }
340  else
341  {
342  SG_ERROR("\"%s\" does not have a parameter with name \"%s\".\n",
343  get_name(), _tag.name().c_str());
344  }
345  }
346 
353  template <typename T, typename U=void>
354  void set(const std::string& name, const T& value)
355  {
356  Tag<T> tag(name);
357  set(tag, value);
358  }
359 
366  template <typename T>
367  T get(const Tag<T>& _tag) const
368  {
369  const Any value = get_with_base_tag(_tag);
370  try
371  {
372  return recall_type<T>(value);
373  }
374  catch(std::logic_error)
375  {
376  SG_ERROR("Type for parameter with name \"%s\" is not correct in \"%s\".\n",
377  _tag.name().c_str(), get_name());
378  }
379  }
380 
387  template <typename T, typename U=void>
388  T get(const std::string& name) const
389  {
390  Tag<T> tag(name);
391  return get(tag);
392  }
393 
394 protected:
402  virtual void load_serializable_pre() throw (ShogunException);
403 
411  virtual void load_serializable_post() throw (ShogunException);
412 
420  virtual void save_serializable_pre() throw (ShogunException);
421 
429  virtual void save_serializable_post() throw (ShogunException);
430 
438  template <typename T>
439  void register_param(Tag<T>& _tag, const T& value)
440  {
441  set_with_base_tag(_tag, erase_type(value));
442  }
443 
451  template <typename T>
452  void register_param(const std::string& name, const T& value)
453  {
454  BaseTag tag(name);
455  set_with_base_tag(tag, erase_type(value));
456  }
457 
458 public:
460  virtual void update_parameter_hash();
461 
465  virtual bool parameter_hash_changed();
466 
479  virtual bool equals(CSGObject* other, float64_t accuracy=0.0, bool tolerant=false);
480 
489  virtual CSGObject* clone();
490 
491 private:
492  void set_global_objects();
493  void unset_global_objects();
494  void init();
495 
503  bool has_with_base_tag(const BaseTag& _tag) const;
504 
511  void set_with_base_tag(const BaseTag& _tag, const Any& any);
512 
519  Any get_with_base_tag(const BaseTag& _tag) const;
520 
529  void get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
530  uint32_t& total_length);
531 
532  class Self;
533  Unique<Self> self;
534 
535 public:
538 
541 
544 
547 
550 
553 
555  uint32_t m_hash;
556 
557 private:
558 
559  EPrimitiveType m_generic;
560  bool m_load_pre_called;
561  bool m_load_post_called;
562  bool m_save_pre_called;
563  bool m_save_post_called;
564 
565  RefCount* m_refcount;
566 };
567 }
568 #endif // __SGOBJECT_H__
virtual const char * get_name() const =0
SGStringList< char > get_modelsel_names()
Definition: SGObject.cpp:531
template class SGStringList
Definition: SGObject.h:43
Parallel * get_global_parallel()
Definition: SGObject.cpp:310
virtual void update_parameter_hash()
Definition: SGObject.cpp:281
int32_t index_t
Definition: common.h:62
virtual CSGObject * clone()
Definition: SGObject.cpp:747
Base class for all tags. This class stores name and not the type information for a shogun object...
Definition: basetag.h:48
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual CSGObject * shallow_copy() const
Definition: SGObject.cpp:225
void register_param(const std::string &name, const T &value)
Definition: SGObject.h:452
void register_param(Tag< T > &_tag, const T &value)
Definition: SGObject.h:439
void unset_generic()
Definition: SGObject.cpp:336
Version * get_global_version()
Definition: SGObject.cpp:323
parameter struct
virtual void save_serializable_pre()
Definition: SGObject.cpp:464
#define SG_ERROR(...)
Definition: SGIO.h:129
virtual bool is_generic(EPrimitiveType *generic) const
Definition: SGObject.cpp:329
EModelSelectionAvailability
Definition: SGObject.h:91
Allows to store objects of arbitrary types by using a BaseAnyPolicy and provides a type agnostic API...
Definition: any.h:159
Parameter * m_parameters
Definition: SGObject.h:546
virtual bool load_serializable(CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:402
Parallel * parallel
Definition: SGObject.h:540
bool has(const std::string &name) const
Definition: SGObject.h:289
void set(const std::string &name, const T &value)
Definition: SGObject.h:354
std::string name() const
Definition: basetag.h:79
char * get_modsel_param_descr(const char *param_name)
Definition: SGObject.cpp:555
bool same_type() const
Definition: any.h:234
void set(const Tag< T > &_tag, const T &value)
Definition: SGObject.h:328
Parameter class.
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
virtual ~CSGObject()
Definition: SGObject.cpp:172
void build_gradient_parameter_dictionary(CMap< TParameter *, CSGObject * > *dict)
Definition: SGObject.cpp:630
Template Dynamic array class that creates an array that can be used like a list or an array...
Definition: DynArray.h:32
double float64_t
Definition: common.h:50
Version * version
Definition: SGObject.h:543
EGradientAvailability
Definition: SGObject.h:97
virtual void save_serializable_post()
Definition: SGObject.cpp:469
void print_modsel_params()
Definition: SGObject.cpp:507
Acts as an identifier for a shogun object. It contains type information and name of the object...
Definition: tag.h:47
Class Version provides version information.
Definition: Version.h:28
Parameter * m_model_selection_parameters
Definition: SGObject.h:549
virtual bool equals(CSGObject *other, float64_t accuracy=0.0, bool tolerant=false)
Definition: SGObject.cpp:651
virtual CSGObject * deep_copy() const
Definition: SGObject.cpp:231
void set_global_parallel(Parallel *parallel)
Definition: SGObject.cpp:274
virtual void load_serializable_pre()
Definition: SGObject.cpp:454
virtual void load_serializable_post()
Definition: SGObject.cpp:459
Class Parallel provides helper functions for multithreading.
Definition: Parallel.h:27
virtual bool save_serializable(CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:347
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
bool has(const Tag< T > &tag) const
Definition: SGObject.h:301
SGIO * get_global_io()
Definition: SGObject.cpp:268
index_t get_modsel_param_index(const char *param_name)
Definition: SGObject.cpp:568
void set_global_io(SGIO *io)
Definition: SGObject.cpp:261
uint32_t m_hash
Definition: SGObject.h:555
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:243
Parameter * m_gradient_parameters
Definition: SGObject.h:552
virtual void print_serializable(const char *prefix="")
Definition: SGObject.cpp:341
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:295
void set_global_version(Version *version)
Definition: SGObject.cpp:316
Any erase_type(const T &v)
Definition: any.h:296
bool has(const std::string &name) const
Definition: SGObject.h:312
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:39

SHOGUN Machine Learning Toolbox - Documentation