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__

SHOGUN Machine Learning Toolbox - Documentation