SHOGUN  4.1.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 
18 #include <shogun/lib/common.h>
19 #include <shogun/lib/DataType.h>
21 #include <shogun/base/Version.h>
22 
26 namespace shogun
27 {
28 class RefCount;
29 class SGIO;
30 class Parallel;
31 class Parameter;
32 class ParameterMap;
33 class SGParamInfo;
34 class CSerializableFile;
35 
36 template <class T, class K> class CMap;
37 
38 struct TParameter;
39 template <class T> class DynArray;
40 template <class T> class SGStringList;
41 
42 /*******************************************************************************
43  * define reference counter macros
44  ******************************************************************************/
45 
46 #ifdef USE_REFERENCE_COUNTING
47 #define SG_REF(x) { if (x) (x)->ref(); }
48 #define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } }
49 #define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } }
50 #else
51 #define SG_REF(x)
52 #define SG_UNREF(x)
53 #define SG_UNREF_NO_NULL(x)
54 #endif
55 
56 /*******************************************************************************
57  * Macros for registering parameters/model selection parameters
58  ******************************************************************************/
59 
60 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
61 #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
62 
63 #define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
64 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
65 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
66 
67 #define SG_ADD4(param, name, description, ms_available) {\
68  m_parameters->add(param, name, description);\
69  if (ms_available)\
70  m_model_selection_parameters->add(param, name, description);\
71 }
72 
73 #define SG_ADD5(param, name, description, ms_available, gradient_available) {\
74  m_parameters->add(param, name, description);\
75  if (ms_available)\
76  m_model_selection_parameters->add(param, name, description);\
77  if (gradient_available)\
78  m_gradient_parameters->add(param, name, description);\
79 }
80 
81 #define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__)
82 
83 /*******************************************************************************
84  * End of macros for registering parameters/model selection parameters
85  ******************************************************************************/
86 
91 };
92 
95 {
98 };
99 
113 {
114 public:
116  CSGObject();
117 
119  CSGObject(const CSGObject& orig);
120 
122  virtual ~CSGObject();
123 
124 #ifdef USE_REFERENCE_COUNTING
125 
129  int32_t ref();
130 
135  int32_t ref_count();
136 
142  int32_t unref();
143 #endif //USE_REFERENCE_COUNTING
144 
145 #ifdef TRACE_MEMORY_ALLOCS
146  static void list_memory_allocs();
147 #endif
148 
152  virtual CSGObject *shallow_copy() const;
153 
157  virtual CSGObject *deep_copy() const;
158 
164  virtual const char* get_name() const = 0;
165 
174  virtual bool is_generic(EPrimitiveType* generic) const;
175 
178  template<class T> void set_generic();
179 
184  void unset_generic();
185 
190  virtual void print_serializable(const char* prefix="");
191 
201  virtual bool save_serializable(CSerializableFile* file,
202  const char* prefix="", int32_t param_version=Version::get_version_parameter());
203 
215  virtual bool load_serializable(CSerializableFile* file,
216  const char* prefix="", int32_t param_version=Version::get_version_parameter());
217 
232  int32_t file_version, CSerializableFile* file,
233  const char* prefix="");
234 
247  DynArray<TParameter*>* load_all_file_parameters(int32_t file_version,
248  int32_t current_version,
249  CSerializableFile* file, const char* prefix="");
250 
265  void map_parameters(DynArray<TParameter*>* param_base,
266  int32_t& base_version,
267  DynArray<const SGParamInfo*>* target_param_infos);
268 
273  void set_global_io(SGIO* io);
274 
279  SGIO* get_global_io();
280 
286 
292 
298 
304 
308 
310  void print_modsel_params();
311 
318  char* get_modsel_param_descr(const char* param_name);
319 
326  index_t get_modsel_param_index(const char* param_name);
327 
335 
336 protected:
356  virtual TParameter* migrate(DynArray<TParameter*>* param_base,
357  const SGParamInfo* target);
358 
381  virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base,
382  const SGParamInfo* target, TParameter*& replacement,
383  TParameter*& to_migrate, char* old_name=NULL);
384 
392  virtual void load_serializable_pre() throw (ShogunException);
393 
401  virtual void load_serializable_post() throw (ShogunException);
402 
410  virtual void save_serializable_pre() throw (ShogunException);
411 
419  virtual void save_serializable_post() throw (ShogunException);
420 
421 public:
423  virtual void update_parameter_hash();
424 
428  virtual bool parameter_hash_changed();
429 
442  virtual bool equals(CSGObject* other, float64_t accuracy=0.0, bool tolerant=false);
443 
452  virtual CSGObject* clone();
453 
454 private:
455  void set_global_objects();
456  void unset_global_objects();
457  void init();
458 
464  bool is_param_new(const SGParamInfo param_info) const;
465 
474  bool save_parameter_version(CSerializableFile* file, const char* prefix="",
475  int32_t param_version=Version::get_version_parameter());
476 
480  int32_t load_parameter_version(CSerializableFile* file,
481  const char* prefix="");
482 
491  void get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
492  uint32_t& total_length);
493 
494 public:
497 
500 
503 
506 
509 
512 
515 
517  uint32_t m_hash;
518 
519 private:
520 
521  EPrimitiveType m_generic;
522  bool m_load_pre_called;
523  bool m_load_post_called;
524  bool m_save_pre_called;
525  bool m_save_post_called;
526 
527  RefCount* m_refcount;
528 };
529 }
530 #endif // __SGOBJECT_H__
virtual const char * get_name() const =0
SGStringList< char > get_modelsel_names()
Definition: SGObject.cpp:1135
template class SGStringList
Definition: SGObject.h:40
Parallel * get_global_parallel()
Definition: SGObject.cpp:278
virtual bool save_serializable(CSerializableFile *file, const char *prefix="", int32_t param_version=Version::get_version_parameter())
Definition: SGObject.cpp:315
virtual void update_parameter_hash()
Definition: SGObject.cpp:250
Class that holds informations about a certain parameter of an CSGObject. Contains name...
Definition: ParameterMap.h:32
ParameterMap * m_parameter_map
Definition: SGObject.h:514
virtual TParameter * migrate(DynArray< TParameter * > *param_base, const SGParamInfo *target)
Definition: SGObject.cpp:949
int32_t index_t
Definition: common.h:62
virtual CSGObject * clone()
Definition: SGObject.cpp:1360
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual CSGObject * shallow_copy() const
Definition: SGObject.cpp:194
void unset_generic()
Definition: SGObject.cpp:304
Version * get_global_version()
Definition: SGObject.cpp:291
parameter struct
Definition: Parameter.h:32
virtual void save_serializable_pre()
Definition: SGObject.cpp:1067
virtual bool is_generic(EPrimitiveType *generic) const
Definition: SGObject.cpp:297
EModelSelectionAvailability
Definition: SGObject.h:88
Parameter * m_parameters
Definition: SGObject.h:505
static int32_t get_version_parameter()
Definition: Version.cpp:106
Parallel * parallel
Definition: SGObject.h:499
Implements a map of ParameterMapElement instances Maps one key to a set of values.
Definition: ParameterMap.h:165
char * get_modsel_param_descr(const char *param_name)
Definition: SGObject.cpp:1159
DynArray< TParameter * > * load_file_parameters(const SGParamInfo *param_info, int32_t file_version, CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:545
Parameter class.
Definition: Parameter.h:225
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
virtual ~CSGObject()
Definition: SGObject.cpp:140
void build_gradient_parameter_dictionary(CMap< TParameter *, CSGObject * > *dict)
Definition: SGObject.cpp:1243
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
virtual bool load_serializable(CSerializableFile *file, const char *prefix="", int32_t param_version=Version::get_version_parameter())
Definition: SGObject.cpp:374
DynArray< TParameter * > * load_all_file_parameters(int32_t file_version, int32_t current_version, CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:704
Version * version
Definition: SGObject.h:502
EGradientAvailability
Definition: SGObject.h:94
virtual void save_serializable_post()
Definition: SGObject.cpp:1072
void print_modsel_params()
Definition: SGObject.cpp:1111
Class Version provides version information.
Definition: Version.h:28
Parameter * m_model_selection_parameters
Definition: SGObject.h:508
virtual bool equals(CSGObject *other, float64_t accuracy=0.0, bool tolerant=false)
Definition: SGObject.cpp:1264
virtual CSGObject * deep_copy() const
Definition: SGObject.cpp:200
void set_global_parallel(Parallel *parallel)
Definition: SGObject.cpp:243
virtual void load_serializable_pre()
Definition: SGObject.cpp:1057
virtual void load_serializable_post()
Definition: SGObject.cpp:1062
Class Parallel provides helper functions for multithreading.
Definition: Parallel.h:27
virtual void one_to_one_migration_prepare(DynArray< TParameter * > *param_base, const SGParamInfo *target, TParameter *&replacement, TParameter *&to_migrate, char *old_name=NULL)
Definition: SGObject.cpp:889
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SGIO * get_global_io()
Definition: SGObject.cpp:237
void map_parameters(DynArray< TParameter * > *param_base, int32_t &base_version, DynArray< const SGParamInfo * > *target_param_infos)
Definition: SGObject.cpp:742
index_t get_modsel_param_index(const char *param_name)
Definition: SGObject.cpp:1172
void set_global_io(SGIO *io)
Definition: SGObject.cpp:230
uint32_t m_hash
Definition: SGObject.h:517
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:243
Parameter * m_gradient_parameters
Definition: SGObject.h:511
virtual void print_serializable(const char *prefix="")
Definition: SGObject.cpp:309
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:263
void set_global_version(Version *version)
Definition: SGObject.cpp:284
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:36

SHOGUN Machine Learning Toolbox - Documentation