SHOGUN  v3.2.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 
SGObject.h
浏览该文件的文档.
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 #include <shogun/lib/common.h>
18 #include <shogun/lib/DataType.h>
21 
22 #include <shogun/base/Parallel.h>
23 #include <shogun/base/Version.h>
24 #include <shogun/io/SGIO.h>
25 
29 namespace shogun
30 {
31 class IO;
32 class Parallel;
33 class Version;
34 class Parameter;
35 class ParameterMap;
36 class SGParamInfo;
37 class SGRefObject;
38 class CSerializableFile;
39 
40 template <class T, class K> class CMap;
41 
42 struct TParameter;
43 template <class T> class DynArray;
44 template <class T> class SGStringList;
45 
46 /*******************************************************************************
47  * Macros for registering parameters/model selection parameters
48  ******************************************************************************/
49 
50 #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
51 #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)
52 
53 #define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
54 #define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
55 #define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
56 
57 #define SG_ADD4(param, name, description, ms_available) {\
58  m_parameters->add(param, name, description);\
59  if (ms_available)\
60  m_model_selection_parameters->add(param, name, description);\
61 }
62 
63 #define SG_ADD5(param, name, description, ms_available, gradient_available) {\
64  m_parameters->add(param, name, description);\
65  if (ms_available)\
66  m_model_selection_parameters->add(param, name, description);\
67  if (gradient_available)\
68  m_gradient_parameters->add(param, name, description);\
69 }
70 
71 #define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__)
72 
73 /*******************************************************************************
74  * End of macros for registering parameters/model selection parameters
75  ******************************************************************************/
76 
81 };
82 
85 {
88 };
89 
102 class CSGObject : public SGRefObject
103 {
104 public:
106  CSGObject();
107 
109  CSGObject(const CSGObject& orig);
110 
112  virtual ~CSGObject();
113 
117  virtual CSGObject *shallow_copy() const
118  {
120  return NULL;
121  }
122 
126  virtual CSGObject *deep_copy() const
127  {
129  return NULL;
130  }
131 
137  virtual const char* get_name() const = 0;
138 
147  virtual bool is_generic(EPrimitiveType* generic) const;
148 
151  template<class T> void set_generic();
152 
157  void unset_generic();
158 
163  virtual void print_serializable(const char* prefix="");
164 
174  virtual bool save_serializable(CSerializableFile* file,
175  const char* prefix="", int32_t param_version=Version::get_version_parameter());
176 
188  virtual bool load_serializable(CSerializableFile* file,
189  const char* prefix="", int32_t param_version=Version::get_version_parameter());
190 
205  int32_t file_version, CSerializableFile* file,
206  const char* prefix="");
207 
220  DynArray<TParameter*>* load_all_file_parameters(int32_t file_version,
221  int32_t current_version,
222  CSerializableFile* file, const char* prefix="");
223 
238  void map_parameters(DynArray<TParameter*>* param_base,
239  int32_t& base_version,
240  DynArray<const SGParamInfo*>* target_param_infos);
241 
246  void set_global_io(SGIO* io);
247 
252  SGIO* get_global_io();
253 
259 
265 
271 
277 
281 
283  void print_modsel_params();
284 
291  char* get_modsel_param_descr(const char* param_name);
292 
299  index_t get_modsel_param_index(const char* param_name);
300 
308 
309 protected:
329  virtual TParameter* migrate(DynArray<TParameter*>* param_base,
330  const SGParamInfo* target);
331 
354  virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base,
355  const SGParamInfo* target, TParameter*& replacement,
356  TParameter*& to_migrate, char* old_name=NULL);
357 
366  virtual void load_serializable_pre() throw (ShogunException);
367 
376  virtual void load_serializable_post() throw (ShogunException);
377 
386  virtual void save_serializable_pre() throw (ShogunException);
387 
396  virtual void save_serializable_post() throw (ShogunException);
397 
398 public:
404  virtual bool update_parameter_hash();
405 
417  virtual bool equals(CSGObject* other, float64_t accuracy=0.0);
418 
427  virtual CSGObject* clone();
428 
429 private:
430  void set_global_objects();
431  void unset_global_objects();
432  void init();
433 
439  bool is_param_new(const SGParamInfo param_info) const;
440 
449  bool save_parameter_version(CSerializableFile* file, const char* prefix="",
450  int32_t param_version=Version::get_version_parameter());
451 
455  int32_t load_parameter_version(CSerializableFile* file,
456  const char* prefix="");
457 
458  /*Gets an incremental hash of all parameters as well as the parameters
459  * of CSGObject children of the current object's parameters.
460  *
461  * @param param Parameter to hash
462  * @param current hash
463  * @param carry value for Murmur3 incremental hash
464  * @param total_length total byte length of all hashed
465  * parameters so far. Byte length of parameters will be added
466  * to the total length
467  */
468  void get_parameter_incremental_hash(Parameter* param,
469  uint32_t& hash, uint32_t& carry, uint32_t& total_length);
470 
471 public:
474 
477 
480 
483 
486 
489 
492 
494  uint32_t m_hash;
495 
496 private:
497 
498  EPrimitiveType m_generic;
499  bool m_load_pre_called;
500  bool m_load_post_called;
501  bool m_save_pre_called;
502  bool m_save_post_called;
503 };
504 }
505 #endif // __SGOBJECT_H__
virtual const char * get_name() const =0
SGStringList< char > get_modelsel_names()
Definition: SGObject.cpp:1060
template class SGStringList
Definition: SGObject.h:44
Parallel * get_global_parallel()
Definition: SGObject.cpp:209
virtual bool save_serializable(CSerializableFile *file, const char *prefix="", int32_t param_version=Version::get_version_parameter())
Definition: SGObject.cpp:246
Class that holds informations about a certain parameter of an CSGObject. Contains name...
Definition: ParameterMap.h:28
Class SGRefObject is a reference count based memory management class.
Definition: SGRefObject.h:46
ParameterMap * m_parameter_map
Definition: SGObject.h:491
virtual TParameter * migrate(DynArray< TParameter * > *param_base, const SGParamInfo *target)
Definition: SGObject.cpp:878
int32_t index_t
Definition: common.h:60
virtual CSGObject * clone()
Definition: SGObject.cpp:1273
virtual CSGObject * deep_copy() const
Definition: SGObject.h:126
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
void unset_generic()
Definition: SGObject.cpp:235
Version * get_global_version()
Definition: SGObject.cpp:222
parameter struct
Definition: Parameter.h:26
virtual void save_serializable_pre()
Definition: SGObject.cpp:994
virtual bool is_generic(EPrimitiveType *generic) const
Definition: SGObject.cpp:228
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:141
EModelSelectionAvailability
Definition: SGObject.h:78
Parameter * m_parameters
Definition: SGObject.h:482
virtual CSGObject * shallow_copy() const
Definition: SGObject.h:117
static int32_t get_version_parameter()
Definition: Version.cpp:104
Parallel * parallel
Definition: SGObject.h:476
Implements a map of ParameterMapElement instances Maps one key to a set of values.
Definition: ParameterMap.h:161
virtual bool update_parameter_hash()
Definition: SGObject.cpp:187
char * get_modsel_param_descr(const char *param_name)
Definition: SGObject.cpp:1084
DynArray< TParameter * > * load_file_parameters(const SGParamInfo *param_info, int32_t file_version, CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:474
Parameter class.
Definition: Parameter.h:216
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:102
virtual ~CSGObject()
Definition: SGObject.cpp:134
void build_gradient_parameter_dictionary(CMap< TParameter *, CSGObject * > *dict)
Definition: SGObject.cpp:1156
virtual bool equals(CSGObject *other, float64_t accuracy=0.0)
Definition: SGObject.cpp:1177
Template Dynamic array class that creates an array that can be used like a list or an array...
Definition: DynArray.h:30
double float64_t
Definition: common.h:48
virtual bool load_serializable(CSerializableFile *file, const char *prefix="", int32_t param_version=Version::get_version_parameter())
Definition: SGObject.cpp:305
DynArray< TParameter * > * load_all_file_parameters(int32_t file_version, int32_t current_version, CSerializableFile *file, const char *prefix="")
Definition: SGObject.cpp:633
Version * version
Definition: SGObject.h:479
EGradientAvailability
Definition: SGObject.h:84
virtual void save_serializable_post()
Definition: SGObject.cpp:999
void print_modsel_params()
Definition: SGObject.cpp:1036
Class Version provides version information.
Definition: Version.h:32
Parameter * m_model_selection_parameters
Definition: SGObject.h:485
void set_global_parallel(Parallel *parallel)
Definition: SGObject.cpp:180
virtual void load_serializable_pre()
Definition: SGObject.cpp:984
virtual void load_serializable_post()
Definition: SGObject.cpp:989
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:818
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:16
SGIO * get_global_io()
Definition: SGObject.cpp:174
void map_parameters(DynArray< TParameter * > *param_base, int32_t &base_version, DynArray< const SGParamInfo * > *target_param_infos)
Definition: SGObject.cpp:671
index_t get_modsel_param_index(const char *param_name)
Definition: SGObject.cpp:1097
void set_global_io(SGIO *io)
Definition: SGObject.cpp:167
uint32_t m_hash
Definition: SGObject.h:494
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:245
Parameter * m_gradient_parameters
Definition: SGObject.h:488
virtual void print_serializable(const char *prefix="")
Definition: SGObject.cpp:240
void set_global_version(Version *version)
Definition: SGObject.cpp:215
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:40

SHOGUN Machine Learning Toolbox - Documentation