SHOGUN  6.1.3
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/base/Version.h>
17 #include <shogun/base/unique.h>
18 #include <shogun/io/SGIO.h>
19 #include <shogun/lib/DataType.h>
20 #include <shogun/lib/RxCppHeader.h>
22 #include <shogun/lib/any.h>
23 #include <shogun/lib/common.h>
24 #include <shogun/lib/config.h>
26 #include <shogun/lib/tag.h>
27 
28 #include <utility>
29 
33 namespace shogun
34 {
35 class RefCount;
36 class SGIO;
37 class Parallel;
38 class Parameter;
39 class CSerializableFile;
40 class ParameterObserverInterface;
41 
42 template <class T, class K> class CMap;
43 
44 struct TParameter;
45 template <class T> class DynArray;
46 template <class T> class SGStringList;
47 
48 /*******************************************************************************
49  * define reference counter macros
50  ******************************************************************************/
51 
52 #define SG_REF(x) { if (x) (x)->ref(); }
53 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
54 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
55 
56 /*******************************************************************************
57  * Macros for registering parameters/model selection parameters
58  ******************************************************************************/
59 
60 #ifdef _MSC_VER
61 
62 #define VA_NARGS(...) INTERNAL_EXPAND_ARGS_PRIVATE(INTERNAL_ARGS_AUGMENTER(__VA_ARGS__))
63 #define INTERNAL_ARGS_AUGMENTER(...) unused, __VA_ARGS__
64 #define INTERNAL_EXPAND(x) x
65 #define INTERNAL_EXPAND_ARGS_PRIVATE(...) INTERNAL_EXPAND(INTERNAL_GET_ARG_COUNT_PRIVATE(__VA_ARGS__, 5, 4, 3, 2, 1, 0))
66 #define INTERNAL_GET_ARG_COUNT_PRIVATE(_0_, _1_, _2_, _3_, _4_, _5_, count, ...) count
67 
68 #else
69 
70 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
71 #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
72 
73 #endif
74 
75 #define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
76 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
77 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
78 
79 #define SG_ADD4(param, name, description, ms_available) {\
80  m_parameters->add(param, name, description);\
81  if (ms_available)\
82  m_model_selection_parameters->add(param, name, description);\
83 }
84 
85 #define SG_ADD5(param, name, description, ms_available, gradient_available) {\
86  m_parameters->add(param, name, description);\
87  if (ms_available)\
88  m_model_selection_parameters->add(param, name, description);\
89  if (gradient_available)\
90  m_gradient_parameters->add(param, name, description);\
91 }
92 
93 #define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__)
94 
95 /*******************************************************************************
96  * End of macros for registering parameters/model selection parameters
97  ******************************************************************************/
98 
103 };
104 
107 {
110 };
111 
125 {
126 public:
131  typedef rxcpp::subscriber<
134 
136  CSGObject();
137 
139  CSGObject(const CSGObject& orig);
140 
142  virtual ~CSGObject();
143 
148  int32_t ref();
149 
154  int32_t ref_count();
155 
161  int32_t unref();
162 
163 #ifdef TRACE_MEMORY_ALLOCS
164  static void list_memory_allocs();
165 #endif
166 
170  virtual CSGObject *shallow_copy() const;
171 
175  virtual CSGObject *deep_copy() const;
176 
182  virtual const char* get_name() const = 0;
183 
192  virtual bool is_generic(EPrimitiveType* generic) const;
193 
196  template<class T> void set_generic();
197 
202  void unset_generic();
203 
208  virtual void print_serializable(const char* prefix="");
209 
217  virtual bool save_serializable(CSerializableFile* file,
218  const char* prefix="");
219 
229  virtual bool load_serializable(CSerializableFile* file,
230  const char* prefix="");
231 
236  void set_global_io(SGIO* io);
237 
242  SGIO* get_global_io();
243 
249 
255 
261 
267 
271 
273  void print_modsel_params();
274 
281  char* get_modsel_param_descr(const char* param_name);
282 
289  index_t get_modsel_param_index(const char* param_name);
290 
298 
304  bool has(const std::string& name) const
305  {
306  return type_erased_has(BaseTag(name));
307  }
308 
314  template <typename T>
315  bool has(const Tag<T>& tag) const
316  {
317  return has<T>(tag.name());
318  }
319 
325  template <typename T, typename U=void>
326  bool has(const std::string& name) const
327  {
328  BaseTag tag(name);
329  if(!type_erased_has(tag))
330  return false;
331  const Any value = type_erased_get(tag);
332  return value.same_type<T>();
333  }
334 
341  template <typename T>
342  void set(const Tag<T>& _tag, const T& value)
343  {
344  if(type_erased_has(_tag))
345  {
346  if(has<T>(_tag.name()))
347  type_erased_set(_tag, erase_type(value));
348  else
349  {
350  SG_ERROR("Type for parameter with name \"%s\" is not correct.\n",
351  _tag.name().c_str());
352  }
353  }
354  else
355  {
356  SG_ERROR("\"%s\" does not have a parameter with name \"%s\".\n",
357  get_name(), _tag.name().c_str());
358  }
359  }
360 
367  template <typename T, typename U=void>
368  void set(const std::string& name, const T& value)
369  {
370  Tag<T> tag(name);
371  set(tag, value);
372  }
373 
380  template <typename T>
381  T get(const Tag<T>& _tag) const
382  {
383  const Any value = type_erased_get(_tag);
384  try
385  {
386  return recall_type<T>(value);
387  }
388  catch (const std::logic_error&)
389  {
390  SG_ERROR("Type for parameter with name \"%s\" is not correct in \"%s\".\n",
391  _tag.name().c_str(), get_name());
392  }
393  // we won't be there
394  return recall_type<T>(value);
395  }
396 
403  template <typename T, typename U=void>
404  T get(const std::string& name) const
405  {
406  Tag<T> tag(name);
407  return get(tag);
408  }
409 
410 #ifndef SWIG
411 
416  {
417  return m_observable_params;
418  };
419 #endif
420 
423 
426 
427 protected:
435  virtual void load_serializable_pre() throw (ShogunException);
436 
444  virtual void load_serializable_post() throw (ShogunException);
445 
453  virtual void save_serializable_pre() throw (ShogunException);
454 
462  virtual void save_serializable_post() throw (ShogunException);
463 
471  template <typename T>
472  void register_param(Tag<T>& _tag, const T& value)
473  {
474  type_erased_set(_tag, erase_type(value));
475  }
476 
484  template <typename T>
485  void register_param(const std::string& name, const T& value)
486  {
487  BaseTag tag(name);
488  type_erased_set(tag, erase_type(value));
489  }
490 
491 public:
493  virtual void update_parameter_hash();
494 
498  virtual bool parameter_hash_changed();
499 
512  virtual bool equals(CSGObject* other, float64_t accuracy=0.0, bool tolerant=false);
513 
522  virtual CSGObject* clone();
523 
524 protected:
525  /* Iteratively clones all parameters of the provided instance into this instance.
526  * This will fail if the objects have different sets of registered parameters,
527  * or if they have a different type as defined by get_name().
528  *
529  * @param other object whose parameters are to be cloned into this instance
530  * @return true if cloning was successful
531  */
532  bool clone_parameters(CSGObject* other);
533 
534 private:
535  void set_global_objects();
536  void unset_global_objects();
537  void init();
538 
546  bool type_erased_has(const BaseTag& _tag) const;
547 
554  void type_erased_set(const BaseTag& _tag, const Any& any);
555 
562  Any type_erased_get(const BaseTag& _tag) const;
563 
572  void get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
573  uint32_t& total_length);
574 
575  class Self;
576  Unique<Self> self;
577 
578  class ParameterObserverList;
579  Unique<ParameterObserverList> param_obs_list;
580 
581 protected:
586  void observe(const ObservedValue value);
587 
595  const std::string& name, const SG_OBS_VALUE_TYPE type,
596  const std::string& description);
597 
598 public:
601 
604 
607 
610 
613 
616 
618  uint32_t m_hash;
619 
620 private:
621 
622  EPrimitiveType m_generic;
623  bool m_load_pre_called;
624  bool m_load_post_called;
625  bool m_save_pre_called;
626  bool m_save_post_called;
627 
628  RefCount* m_refcount;
629 
631  SGSubject* m_subject_params;
632 
634  SGObservable* m_observable_params;
635 
637  SGSubscriber* m_subscriber_params;
638 };
639 }
640 #endif // __SGOBJECT_H__
void list_observable_parameters()
Definition: SGObject.cpp:878
virtual const char * get_name() const =0
SGStringList< char > get_modelsel_names()
Definition: SGObject.cpp:536
Parallel * get_global_parallel()
Definition: SGObject.cpp:311
virtual void update_parameter_hash()
Definition: SGObject.cpp:282
SG_OBS_VALUE_TYPE
Definition: ObservedValue.h:54
int32_t index_t
Definition: common.h:72
virtual CSGObject * clone()
Definition: SGObject.cpp:734
Base class for all tags. This class stores name and not the type information for a shogun object...
Definition: basetag.h:48
bool clone_parameters(CSGObject *other)
Definition: SGObject.cpp:759
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual CSGObject * shallow_copy() const
Definition: SGObject.cpp:226
void register_param(const std::string &name, const T &value)
Definition: SGObject.h:485
void register_param(Tag< T > &_tag, const T &value)
Definition: SGObject.h:472
void unset_generic()
Definition: SGObject.cpp:337
rxcpp::subjects::subject< ObservedValue > SGSubject
Definition: SGObject.h:127
Version * get_global_version()
Definition: SGObject.cpp:324
parameter struct
virtual void save_serializable_pre()
Definition: SGObject.cpp:465
#define SG_ERROR(...)
Definition: SGIO.h:128
virtual bool is_generic(EPrimitiveType *generic) const
Definition: SGObject.cpp:330
EModelSelectionAvailability
Definition: SGObject.h:100
Allows to store objects of arbitrary types by using a BaseAnyPolicy and provides a type agnostic API...
Definition: any.h:290
Parameter * m_parameters
Definition: SGObject.h:609
virtual bool load_serializable(CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:403
void observe(const ObservedValue value)
Definition: SGObject.cpp:828
Parallel * parallel
Definition: SGObject.h:603
bool has(const std::string &name) const
Definition: SGObject.h:304
std::string name() const
Definition: basetag.h:79
char * get_modsel_param_descr(const char *param_name)
Definition: SGObject.cpp:560
bool same_type() const
Definition: any.h:374
Parameter class.
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:124
virtual ~CSGObject()
Definition: SGObject.cpp:172
void build_gradient_parameter_dictionary(CMap< TParameter *, CSGObject * > *dict)
Definition: SGObject.cpp:635
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:60
void register_observable_param(const std::string &name, const SG_OBS_VALUE_TYPE type, const std::string &description)
Definition: SGObject.cpp:871
Version * version
Definition: SGObject.h:606
EGradientAvailability
Definition: SGObject.h:106
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:42
virtual void save_serializable_post()
Definition: SGObject.cpp:470
void print_modsel_params()
Definition: SGObject.cpp:512
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:612
virtual bool equals(CSGObject *other, float64_t accuracy=0.0, bool tolerant=false)
Definition: SGObject.cpp:656
virtual CSGObject * deep_copy() const
Definition: SGObject.cpp:232
void set_global_parallel(Parallel *parallel)
Definition: SGObject.cpp:275
virtual void load_serializable_pre()
Definition: SGObject.cpp:455
virtual void load_serializable_post()
Definition: SGObject.cpp:460
Class Parallel provides helper functions for multithreading.
Definition: Parallel.h:27
virtual bool save_serializable(CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:348
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:315
rxcpp::subscriber< ObservedValue, rxcpp::observer< ObservedValue, void, void, void, void > > SGSubscriber
Definition: SGObject.h:133
SGIO * get_global_io()
Definition: SGObject.cpp:269
index_t get_modsel_param_index(const char *param_name)
Definition: SGObject.cpp:573
void set_global_io(SGIO *io)
Definition: SGObject.cpp:262
uint32_t m_hash
Definition: SGObject.h:618
int32_t ref_count()
Definition: SGObject.cpp:193
int32_t unref()
Definition: SGObject.cpp:200
void subscribe_to_parameters(ParameterObserverInterface *obs)
Definition: SGObject.cpp:811
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:218
SGObservable * get_parameters_observable()
Definition: SGObject.h:415
Parameter * m_gradient_parameters
Definition: SGObject.h:615
virtual void print_serializable(const char *prefix="")
Definition: SGObject.cpp:342
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:296
void set_global_version(Version *version)
Definition: SGObject.cpp:317
Any erase_type(const T &v)
Definition: any.h:447
bool has(const std::string &name) const
Definition: SGObject.h:326
rxcpp::observable< ObservedValue, rxcpp::dynamic_observable< ObservedValue > > SGObservable
Definition: SGObject.h:130
template class SGStringList
Definition: SGObject.h:46

SHOGUN Machine Learning Toolbox - Documentation