SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TreeMachine.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2012 Chiyuan Zhang
4  * Written (w) 2014 Parijat Mazumdar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation are those
28  * of the authors and should not be interpreted as representing official policies,
29  * either expressed or implied, of the Shogun Development Team.
30  */
31 
32 #ifndef TREEMACHINE_H__
33 #define TREEMACHINE_H__
34 
35 #include <shogun/lib/config.h>
36 
40 
41 namespace shogun
42 {
43 
48 template <class T> class CTreeMachine : public CBaseMulticlassMachine
49 {
50 public:
53 
56 
59  {
60  m_root=NULL;
61  SG_ADD((CSGObject**)&m_root,"m_root", "tree structure", MS_NOT_AVAILABLE);
62  }
63 
65  virtual ~CTreeMachine()
66  {
68  }
69 
73  virtual const char* get_name() const { return "TreeMachine"; }
74 
79  {
81  SG_REF(root);
82  m_root=root;
83  }
84 
89  {
90  SG_REF(m_root);
91  return m_root;
92  }
93 
98  {
99  CTreeMachine<T>* cloned_tree=new CTreeMachine<T>();
100  node_t* clone_root;
101  if (!strcmp(m_root->get_name(),"TreeMachineNode"))
102  {
103  clone_root=new node_t();
104  SG_REF(clone_root);
105  }
106  else
107  {
108  clone_root=new bnode_t();
109  SG_REF(clone_root);
110  }
111 
112  cloned_tree->set_root(clone_root);
113  clone_root->data=m_root->data;
114  clone_root->machine(m_root->machine());
115  CDynamicObjectArray* children=m_root->get_children();
116  for (int32_t i=0;i<children->get_num_elements();i++)
117  {
118  CSGObject* el=children->get_element(i);
119  node_t* child=NULL;
120  if (el!=NULL)
121  {
122  if (!strcmp(m_root->get_name(),"TreeMachineNode"))
123  child=dynamic_cast<node_t*>(el);
124  else
125  child=dynamic_cast<bnode_t*>(el);
126  }
127 
128  CTreeMachine* child_tree=new CTreeMachine();
129  child_tree->set_root(child);
130  CTreeMachine* clone_child_tree=child_tree->clone_tree();
131 
132  node_t* child_node_copy=clone_child_tree->get_root();
133  if ((!strcmp(m_root->get_name(),"BinaryTreeMachineNode")) && (child_node_copy!=NULL))
134  child_node_copy=dynamic_cast<bnode_t*>(child_node_copy);
135 
136  clone_root->add_child(child_node_copy);
137 
138  SG_UNREF(child_node_copy);
139  SG_UNREF(clone_child_tree);
140  SG_UNREF(child_tree);
141  SG_UNREF(child);
142  }
143 
144  SG_UNREF(children);
145  SG_UNREF(clone_root);
146  return cloned_tree;
147  }
148 
149 protected:
150 
152  virtual void store_model_features() { }
153 
154 protected:
157 };
158 
159 } /* namespace shogun */
160 
161 #endif /* end of include guard: TREEMACHINE_H__ */
162 
CTreeMachineNode< T > node_t
Definition: TreeMachine.h:52
The node of the tree structure forming a TreeMachine The node contains pointer to its parent and poin...
CTreeMachineNode< T > * get_root()
Definition: TreeMachine.h:88
virtual void store_model_features()
Definition: TreeMachine.h:152
CTreeMachineNode< T > * m_root
Definition: TreeMachine.h:156
#define SG_REF(x)
Definition: SGObject.h:54
void set_root(CTreeMachineNode< T > *root)
Definition: TreeMachine.h:78
virtual ~CTreeMachine()
Definition: TreeMachine.h:65
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
CTreeMachine * clone_tree()
Definition: TreeMachine.h:97
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
The node of the tree structure forming a TreeMachine The node contains a pointer to its parent and a ...
virtual const char * get_name() const
Definition: TreeMachine.h:73
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CBinaryTreeMachineNode< T > bnode_t
Definition: TreeMachine.h:55
CSGObject * get_element(int32_t index) const
class TreeMachine, a base class for tree based multiclass classifiers. This class is derived from CBa...
Definition: TreeMachine.h:48
#define SG_ADD(...)
Definition: SGObject.h:84

SHOGUN Machine Learning Toolbox - Documentation