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 #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:
400  virtual void update_parameter_hash();
401 
405  virtual bool parameter_hash_changed();
406 
419  virtual bool equals(CSGObject* other, float64_t accuracy=0.0, bool tolerant=false);
420 
429  virtual CSGObject* clone();
430 
431 private:
432  void set_global_objects();
433  void unset_global_objects();
434  void init();
435 
441  bool is_param_new(const SGParamInfo param_info) const;
442 
451  bool save_parameter_version(CSerializableFile* file, const char* prefix="",
452  int32_t param_version=Version::get_version_parameter());
453 
457  int32_t load_parameter_version(CSerializableFile* file,
458  const char* prefix="");
459 
468  void get_parameter_incremental_hash(uint32_t& hash, uint32_t& carry,
469  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__

SHOGUN Machine Learning Toolbox - Documentation