SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SubsetStack.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2012-2014 Heiko Strathmann
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
32 #include <shogun/io/SGIO.h>
33 #include <shogun/base/Parameter.h>
34 
35 using namespace shogun;
36 
38 {
39  init();
40 }
41 
43 {
44  init();
45 
46  for (int32_t i=0; i < other.m_active_subsets_stack->get_num_elements(); ++i)
47  {
48  m_active_subset=(CSubset*)other.m_active_subsets_stack->get_element(i);
49  m_active_subsets_stack->append_element(m_active_subset);
50  }
51 }
52 
54 {
55  SG_UNREF(m_active_subsets_stack);
56  SG_UNREF(m_active_subset);
57 }
58 
60 {
61  /* delete all active subsets, backwards due to DynArray implementation */
62  for (index_t i=m_active_subsets_stack->get_num_elements()-1; i>=0; --i)
63  m_active_subsets_stack->delete_element(i);
64 
65  SG_UNREF(m_active_subset);
66 }
67 
68 void CSubsetStack::init()
69 {
70  SG_ADD((CSGObject**)&m_active_subset, "active_subset",
71  "Currently active subset", MS_NOT_AVAILABLE);
72  SG_ADD((CSGObject**)&m_active_subsets_stack, "active_subsets_stack",
73  "Stack of active subsets", MS_NOT_AVAILABLE);
74 
75  m_active_subset=NULL;
76  m_active_subsets_stack=new CDynamicObjectArray();
77  SG_REF(m_active_subsets_stack);
78 }
79 
81 {
82  /* if there are already subsets on stack, do some legality checks */
83  if (m_active_subsets_stack->get_num_elements())
84  {
85  /* check that subsets may only be smaller or equal than existing */
86  CSubset* latest=(CSubset*)m_active_subsets_stack->get_last_element();
87  if (subset.vlen>latest->m_subset_idx.vlen)
88  {
89  subset.display_vector("subset");
90  latest->m_subset_idx.display_vector("last on stack");
91  SG_ERROR("%s::add_subset(): Provided index vector is "
92  "larger than the subsets on the stubset stack!\n", get_name());
93  }
94 
95  /* check for range of indices */
96  index_t max_index=CMath::max(subset.vector, subset.vlen);
97  if (max_index>=latest->m_subset_idx.vlen)
98  {
99  subset.display_vector("subset");
100  latest->m_subset_idx.display_vector("last on stack");
101  SG_ERROR("%s::add_subset(): Provided index vector contains"
102  " indices larger than possible range!\n", get_name());
103  }
104 
105  /* clean up */
106  SG_UNREF(latest);
107  }
108 
109  /* active subset will be changed anyway, no setting to NULL */
110  SG_UNREF(m_active_subset);
111 
112  /* two cases: stack is empty/stack is not empty */
113  if (m_active_subsets_stack->get_num_elements())
114  {
115  /* if there are already subsets, we need to map given one through
116  * existing ones */
117 
118  /* get latest current subset */
119  CSubset* latest=(CSubset*)m_active_subsets_stack->get_last_element();
120 
121  /* create new index vector */
122  SGVector<index_t> new_active_subset=SGVector<index_t>(subset.vlen);
123 
124  /* using the latest current subset, transform all indices by the latest
125  * added subset (dynamic programming greets you) */
126  for (index_t i=0; i<subset.vlen; ++i)
127  {
128  new_active_subset.vector[i]=
129  latest->m_subset_idx.vector[subset.vector[i]];
130  }
131 
132  /* replace active subset */
133  m_active_subset=new CSubset(new_active_subset);
134  SG_REF(m_active_subset);
135  SG_UNREF(latest);
136  }
137  else
138  {
139  /* just use plain given subset since there is nothing to map */
140  m_active_subset=new CSubset(subset);
141  SG_REF(m_active_subset);
142  }
143 
144  /* add current active subset on stack of active subsets in any case */
145  m_active_subsets_stack->append_element(m_active_subset);
146 }
147 
149 {
151 }
152 
154 {
155  index_t num_subsets=m_active_subsets_stack->get_num_elements();
156  if (num_subsets)
157  {
158  /* unref current subset */
159  SG_UNREF(m_active_subset);
160  m_active_subset=NULL;
161 
162  /* delete last element on stack */
163  if (num_subsets>=1)
164  {
165  index_t last_idx=m_active_subsets_stack->get_num_elements()-1;
166  m_active_subsets_stack->delete_element(last_idx);
167  }
168 
169  /* if there are subsets left on stack, set the next one as active */
170  if (num_subsets>1)
171  {
172  /* use new last element on stack as active subset */
173  index_t last_idx=m_active_subsets_stack->get_num_elements()-1;
174  m_active_subset=(CSubset*)
175  m_active_subsets_stack->get_element(last_idx);
176  }
177 
178  /* otherwise, active subset is just empty */
179  }
180 
181  /* do nothing if nothing on stack */
182 }

SHOGUN Machine Learning Toolbox - Documentation