SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 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>
22 #include <shogun/base/Version.h>
23 
27 namespace shogun
28 {
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  * Macros for registering parameters/model selection parameters
44  ******************************************************************************/
45 
46 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
47 #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
48 
49 #define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
50 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
51 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
52 
53 #define SG_ADD4(param, name, description, ms_available) {\
54  m_parameters->add(param, name, description);\
55  if (ms_available)\
56  m_model_selection_parameters->add(param, name, description);\
57 }
58 
59 #define SG_ADD5(param, name, description, ms_available, gradient_available) {\
60  m_parameters->add(param, name, description);\
61  if (ms_available)\
62  m_model_selection_parameters->add(param, name, description);\
63  if (gradient_available)\
64  m_gradient_parameters->add(param, name, description);\
65 }
66 
67 #define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__)
68 
69 /*******************************************************************************
70  * End of macros for registering parameters/model selection parameters
71  ******************************************************************************/
72 
77 };
78 
81 {
84 };
85 
98 class CSGObject : public SGRefObject
99 {
100 public:
102  CSGObject();
103 
105  CSGObject(const CSGObject& orig);
106 
108  virtual ~CSGObject();
109 
113  virtual CSGObject *shallow_copy() const;
114 
118  virtual CSGObject *deep_copy() const;
119 
125  virtual const char* get_name() const = 0;
126 
135  virtual bool is_generic(EPrimitiveType* generic) const;
136 
139  template<class T> void set_generic();
140 
145  void unset_generic();
146 
151  virtual void print_serializable(const char* prefix="");
152 
162  virtual bool save_serializable(CSerializableFile* file,
163  const char* prefix="", int32_t param_version=Version::get_version_parameter());
164 
176  virtual bool load_serializable(CSerializableFile* file,
177  const char* prefix="", int32_t param_version=Version::get_version_parameter());
178 
193  int32_t file_version, CSerializableFile* file,
194  const char* prefix="");
195 
208  DynArray<TParameter*>* load_all_file_parameters(int32_t file_version,
209  int32_t current_version,
210  CSerializableFile* file, const char* prefix="");
211 
226  void map_parameters(DynArray<TParameter*>* param_base,
227  int32_t& base_version,
228  DynArray<const SGParamInfo*>* target_param_infos);
229 
234  void set_global_io(SGIO* io);
235 
240  SGIO* get_global_io();
241 
247 
253 
259 
265 
269 
271  void print_modsel_params();
272 
279  char* get_modsel_param_descr(const char* param_name);
280 
287  index_t get_modsel_param_index(const char* param_name);
288 
296 
297 protected:
317  virtual TParameter* migrate(DynArray<TParameter*>* param_base,
318  const SGParamInfo* target);
319 
342  virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base,
343  const SGParamInfo* target, TParameter*& replacement,
344  TParameter*& to_migrate, char* old_name=NULL);
345 
354  virtual void load_serializable_pre() throw (ShogunException);
355 
364  virtual void load_serializable_post() throw (ShogunException);
365 
374  virtual void save_serializable_pre() throw (ShogunException);
375 
384  virtual void save_serializable_post() throw (ShogunException);
385 
386 public:
388  virtual void update_parameter_hash();
389 
393  virtual bool parameter_hash_changed();
394 
407  virtual bool equals(CSGObject* other, float64_t accuracy=0.0, bool tolerant=false);
408 
417  virtual CSGObject* clone();
418 
419 private:
420  void set_global_objects();
421  void unset_global_objects();
422  void init();
423 
429  bool is_param_new(const SGParamInfo param_info) const;
430 
439  bool save_parameter_version(CSerializableFile* file, const char* prefix="",
440  int32_t param_version=Version::get_version_parameter());
441 
445  int32_t load_parameter_version(CSerializableFile* file,
446  const char* prefix="");
447 
456  void get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
457  uint32_t& total_length);
458 
459 public:
462 
465 
468 
471 
474 
477 
480 
482  uint32_t m_hash;
483 
484 private:
485 
486  EPrimitiveType m_generic;
487  bool m_load_pre_called;
488  bool m_load_post_called;
489  bool m_save_pre_called;
490  bool m_save_post_called;
491 };
492 }
493 #endif // __SGOBJECT_H__
virtual const char * get_name() const =0
SGStringList< char > get_modelsel_names()
Definition: SGObject.cpp:1077
Parallel * get_global_parallel()
Definition: SGObject.cpp:224
virtual bool save_serializable(CSerializableFile *file, const char *prefix="", int32_t param_version=Version::get_version_parameter())
Definition: SGObject.cpp:261
virtual void update_parameter_hash()
Definition: SGObject.cpp:196
Class that holds informations about a certain parameter of an CSGObject. Contains name...
Definition: ParameterMap.h:32
Class SGRefObject is a reference count based memory management class.
Definition: SGRefObject.h:46
ParameterMap * m_parameter_map
Definition: SGObject.h:479
virtual TParameter * migrate(DynArray< TParameter * > *param_base, const SGParamInfo *target)
Definition: SGObject.cpp:893
int32_t index_t
Definition: common.h:62
virtual CSGObject * clone()
Definition: SGObject.cpp:1302
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual CSGObject * shallow_copy() const
Definition: SGObject.cpp:140
void unset_generic()
Definition: SGObject.cpp:250
Version * get_global_version()
Definition: SGObject.cpp:237
parameter struct
Definition: Parameter.h:32
virtual void save_serializable_pre()
Definition: SGObject.cpp:1009
virtual bool is_generic(EPrimitiveType *generic) const
Definition: SGObject.cpp:243
EModelSelectionAvailability
Definition: SGObject.h:74
Parameter * m_parameters
Definition: SGObject.h:470
static int32_t get_version_parameter()
Definition: Version.cpp:106
Parallel * parallel
Definition: SGObject.h:464
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:1101
DynArray< TParameter * > * load_file_parameters(const SGParamInfo *param_info, int32_t file_version, CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:489
Parameter class.
Definition: Parameter.h:225
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:98
virtual ~CSGObject()
Definition: SGObject.cpp:131
void build_gradient_parameter_dictionary(CMap< TParameter *, CSGObject * > *dict)
Definition: SGObject.cpp:1185
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:320
DynArray< TParameter * > * load_all_file_parameters(int32_t file_version, int32_t current_version, CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:648
Version * version
Definition: SGObject.h:467
EGradientAvailability
Definition: SGObject.h:80
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:36
virtual void save_serializable_post()
Definition: SGObject.cpp:1014
void print_modsel_params()
Definition: SGObject.cpp:1053
Class Version provides version information.
Definition: Version.h:28
Parameter * m_model_selection_parameters
Definition: SGObject.h:473
virtual bool equals(CSGObject *other, float64_t accuracy=0.0, bool tolerant=false)
Definition: SGObject.cpp:1206
virtual CSGObject * deep_copy() const
Definition: SGObject.cpp:146
void set_global_parallel(Parallel *parallel)
Definition: SGObject.cpp:189
virtual void load_serializable_pre()
Definition: SGObject.cpp:999
virtual void load_serializable_post()
Definition: SGObject.cpp:1004
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:833
SGIO * get_global_io()
Definition: SGObject.cpp:183
void map_parameters(DynArray< TParameter * > *param_base, int32_t &base_version, DynArray< const SGParamInfo * > *target_param_infos)
Definition: SGObject.cpp:686
index_t get_modsel_param_index(const char *param_name)
Definition: SGObject.cpp:1114
void set_global_io(SGIO *io)
Definition: SGObject.cpp:176
uint32_t m_hash
Definition: SGObject.h:482
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:244
Parameter * m_gradient_parameters
Definition: SGObject.h:476
virtual void print_serializable(const char *prefix="")
Definition: SGObject.cpp:255
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:209
void set_global_version(Version *version)
Definition: SGObject.cpp:230
template class SGStringList
Definition: SGObject.h:40

SHOGUN Machine Learning Toolbox - Documentation