SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SubsetStack.cpp
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) 2012 Heiko Strathmann
8  */
9 
11 #include <shogun/io/SGIO.h>
12 #include <shogun/base/Parameter.h>
13 
14 using namespace shogun;
15 
17 {
18  init();
19 }
20 
22 {
23  init();
24 
25  for (int32_t i=0; i < other.m_active_subsets_stack->get_num_elements(); ++i)
26  {
27  m_active_subset=(CSubset*)other.m_active_subsets_stack->get_element(i);
28  m_active_subsets_stack->append_element(m_active_subset);
29  }
30 }
31 
33 {
34  SG_UNREF(m_active_subsets_stack);
35  SG_UNREF(m_active_subset);
36 }
37 
39 {
40  /* delete all active subsets, backwards due to DynArray implementation */
41  for (index_t i=m_active_subsets_stack->get_num_elements()-1; i>=0; --i)
42  m_active_subsets_stack->delete_element(i);
43 
44  SG_UNREF(m_active_subset);
45 }
46 
47 void CSubsetStack::init()
48 {
49  SG_ADD((CSGObject**)&m_active_subset, "active_subset",
50  "Currently active subset", MS_NOT_AVAILABLE);
51  SG_ADD((CSGObject**)&m_active_subsets_stack, "active_subsets_stack",
52  "Stack of active subsets", MS_NOT_AVAILABLE);
53 
54  m_active_subset=NULL;
55  m_active_subsets_stack=new CDynamicObjectArray();
56  SG_REF(m_active_subsets_stack);
57 }
58 
60 {
61  /* if there are already subsets on stack, do some legality checks */
62  if (m_active_subsets_stack->get_num_elements())
63  {
64  /* check that subsets may only be smaller or equal than existing */
65  CSubset* latest=(CSubset*)m_active_subsets_stack->get_last_element();
66  if (subset.vlen>latest->m_subset_idx.vlen)
67  {
68  subset.display_vector("subset");
69  latest->m_subset_idx.display_vector("last on stack");
70  SG_ERROR("%s::add_subset(): Provided index vector is "
71  "larger than the subsets on the stubset stack!\n", get_name());
72  }
73 
74  /* check for range of indices */
75  index_t max_index=SGVector<index_t>::max(subset.vector, subset.vlen);
76  if (max_index>=latest->m_subset_idx.vlen)
77  {
78  subset.display_vector("subset");
79  latest->m_subset_idx.display_vector("last on stack");
80  SG_ERROR("%s::add_subset(): Provided index vector contains"
81  " indices larger than possible range!\n", get_name());
82  }
83 
84  /* clean up */
85  SG_UNREF(latest);
86  }
87 
88  /* active subset will be changed anyway, no setting to NULL */
89  SG_UNREF(m_active_subset);
90 
91  /* two cases: stack is empty/stack is not empty */
92  if (m_active_subsets_stack->get_num_elements())
93  {
94  /* if there are alreay subsets, we need to map given one through
95  * existing ones */
96 
97  /* get latest current subset */
98  CSubset* latest=(CSubset*)m_active_subsets_stack->get_last_element();
99 
100  /* create new index vector */
101  SGVector<index_t> new_active_subset=SGVector<index_t>(subset.vlen);
102 
103  /* using the latest current subset, transform all indices by the latest
104  * added subset (dynamic programming greets you) */
105  for (index_t i=0; i<subset.vlen; ++i)
106  {
107  new_active_subset.vector[i]=
108  latest->m_subset_idx.vector[subset.vector[i]];
109  }
110 
111  /* replace active subset */
112  m_active_subset=new CSubset(new_active_subset);
113  SG_REF(m_active_subset);
114  SG_UNREF(latest);
115  }
116  else
117  {
118  /* just use plain given subset since there is nothing to map */
119  m_active_subset=new CSubset(subset);
120  SG_REF(m_active_subset);
121  }
122 
123  /* add current active subset on stack of active subsets in any case */
124  m_active_subsets_stack->append_element(m_active_subset);
125 }
126 
128 {
129  index_t num_subsets=m_active_subsets_stack->get_num_elements();
130  if (num_subsets)
131  {
132  /* unref current subset */
133  SG_UNREF(m_active_subset);
134  m_active_subset=NULL;
135 
136  /* delete last element on stack */
137  if (num_subsets>=1)
138  {
139  index_t last_idx=m_active_subsets_stack->get_num_elements()-1;
140  m_active_subsets_stack->delete_element(last_idx);
141  }
142 
143  /* if there are subsets left on stack, set the next one as active */
144  if (num_subsets>1)
145  {
146  /* use new last element on stack as active subset */
147  index_t last_idx=m_active_subsets_stack->get_num_elements()-1;
148  m_active_subset=(CSubset*)
149  m_active_subsets_stack->get_element(last_idx);
150  }
151 
152  /* otherwise, active subset is just empty */
153  }
154  else
155  {
156  SG_DEBUG("%s::remove_subset() was called but there is no subset set."
157  "\n", get_name());
158  }
159 }

SHOGUN Machine Learning Toolbox - Documentation