Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #478 from karlnapf/master
made DynamicObjectArray non-generic
  • Loading branch information
Soeren Sonnenburg committed Apr 22, 2012
2 parents d453d98 + 5027750 commit e4ab8a1
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 165 deletions.
Expand Up @@ -4,7 +4,7 @@
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Heiko Strathmann
* Written (W) 2011-2012 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

Expand Down Expand Up @@ -50,7 +50,7 @@ CModelSelectionParameters* create_param_tree()
return root;
}

void apply_parameter_tree(CDynamicObjectArray<CParameterCombination>* combinations)
void apply_parameter_tree(CDynamicObjectArray* combinations)
{
/* create some data */
float64_t* matrix=SG_MALLOC(float64_t, 6);
Expand Down Expand Up @@ -78,7 +78,8 @@ void apply_parameter_tree(CDynamicObjectArray<CParameterCombination>* combinatio
for (index_t i=0; i<combinations->get_num_elements(); ++i)
{
SG_SPRINT("applying:\n");
CParameterCombination* current_combination=combinations->get_element(i);
CParameterCombination* current_combination=(CParameterCombination*)
combinations->get_element(i);
current_combination->print_tree();
Parameter* current_parameters=svm->m_parameters;
current_combination->apply_to_modsel_parameter(current_parameters);
Expand Down Expand Up @@ -117,14 +118,15 @@ int main(int argc, char **argv)
SG_SPRINT("----------------------------------\n");

/* build combinations of parameter trees */
CDynamicObjectArray<CParameterCombination>* combinations=tree->get_combinations();
CDynamicObjectArray* combinations=tree->get_combinations();

apply_parameter_tree(combinations);

/* print and directly delete them all */
for (index_t i=0; i<combinations->get_num_elements(); ++i)
{
CParameterCombination* combination=combinations->get_element(i);
CParameterCombination* combination=(CParameterCombination*)
combinations->get_element(i);
SG_UNREF(combination);
}

Expand Down
Expand Up @@ -4,7 +4,7 @@
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Heiko Strathmann
* Written (W) 2011-2012 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

Expand Down Expand Up @@ -180,13 +180,14 @@ void test_get_combinations(CModelSelectionParameters* tree)
tree->print_tree();

/* build combinations of parameter trees */
CDynamicObjectArray<CParameterCombination>* combinations=tree->get_combinations();
CDynamicObjectArray* combinations=tree->get_combinations();

/* print and directly delete them all */
SG_SPRINT("----------------------------------\n");
for (index_t i=0; i<combinations->get_num_elements(); ++i)
{
CParameterCombination* combination=combinations->get_element(i);
CParameterCombination* combination=(CParameterCombination*)
combinations->get_element(i);
combination->print_tree();
SG_UNREF(combination);
}
Expand Down
Expand Up @@ -4,7 +4,7 @@
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Heiko Strathmann
* Written (W) 2011-2012 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

Expand Down Expand Up @@ -80,12 +80,11 @@ void test_leaf_sets_multiplication()
SGVector<float64_t> param_vector(6, true);
CMath::range_fill_vector(param_vector.vector, param_vector.vlen);

CDynamicObjectArray<CDynamicObjectArray<CParameterCombination> > sets;
CDynamicObjectArray sets;
CParameterCombination* new_root=new CParameterCombination();
SG_REF(new_root);

CDynamicObjectArray<CParameterCombination>* current=new CDynamicObjectArray<
CParameterCombination>();
CDynamicObjectArray* current=new CDynamicObjectArray();
sets.append_element(current);
Parameter* p=new Parameter();
p->add(&param_vector.vector[0], "0");
Expand All @@ -98,21 +97,22 @@ void test_leaf_sets_multiplication()
current->append_element(pc);

/* first case: one element */
CDynamicObjectArray<CParameterCombination>* result_simple=
CDynamicObjectArray* result_simple=
CParameterCombination::leaf_sets_multiplication(sets, new_root);

SG_SPRINT("one set\n");
for (index_t i=0; i<result_simple->get_num_elements(); ++i)
{
CParameterCombination* current=result_simple->get_element(i);
CParameterCombination* current=(CParameterCombination*)
result_simple->get_element(i);
current->print_tree();
SG_UNREF(current);
}
SG_UNREF(result_simple);

/* now more elements are created */

current=new CDynamicObjectArray<CParameterCombination>();
current=new CDynamicObjectArray();
sets.append_element(current);
p=new Parameter();
p->add(&param_vector.vector[2], "2");
Expand All @@ -124,7 +124,7 @@ void test_leaf_sets_multiplication()
pc=new CParameterCombination(p);
current->append_element(pc);

current=new CDynamicObjectArray<CParameterCombination>();
current=new CDynamicObjectArray();
sets.append_element(current);
p=new Parameter();
p->add(&param_vector.vector[4], "4");
Expand All @@ -137,13 +137,14 @@ void test_leaf_sets_multiplication()
current->append_element(pc);

/* second case: more element */
CDynamicObjectArray<CParameterCombination>* result_complex=
CDynamicObjectArray* result_complex=
CParameterCombination::leaf_sets_multiplication(sets, new_root);

SG_SPRINT("more sets\n");
for (index_t i=0; i<result_complex->get_num_elements(); ++i)
{
CParameterCombination* current=result_complex->get_element(i);
CParameterCombination* current=(CParameterCombination*)
result_complex->get_element(i);
current->print_tree();
SG_UNREF(current);
}
Expand Down
Expand Up @@ -4,7 +4,7 @@
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Heiko Strathmann
* Written (W) 2011-2012 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

Expand Down Expand Up @@ -114,13 +114,14 @@ int main(int argc, char **argv)
tree->print_tree();

/* build combinations of parameter trees */
CDynamicObjectArray<CParameterCombination>* combinations=tree->get_combinations();
CDynamicObjectArray* combinations=tree->get_combinations();

/* print and directly delete them all */
SG_SPRINT("----------------------------------\n");
for (index_t i=0; i<combinations->get_num_elements(); ++i)
{
CParameterCombination* combination=combinations->get_element(i);
CParameterCombination* combination=(CParameterCombination*)
combinations->get_element(i);
combination->print_tree();
SG_UNREF(combination);
}
Expand Down
Expand Up @@ -4,7 +4,7 @@
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Written (W) 2011 Heiko Strathmann
# Written (W) 2011-2012 Heiko Strathmann
# Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
#

Expand All @@ -13,7 +13,6 @@
def modelselection_parameter_tree_modular(dummy):
from shogun.ModelSelection import ParameterCombination
from shogun.ModelSelection import ModelSelectionParameters, R_EXP, R_LINEAR
from shogun.ModelSelection import DynamicParameterCombinationArray
from shogun.Kernel import PowerKernel
from shogun.Kernel import GaussianKernel
from shogun.Kernel import DistantSegmentsKernel
Expand Down
9 changes: 1 addition & 8 deletions src/interfaces/modular/ModelSelection.i
Expand Up @@ -4,7 +4,7 @@
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Heiko Strathmann
* Written (W) 2011-2012 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

Expand All @@ -26,12 +26,5 @@ SERIALIZABLE_DUMMY(shogun::CrossValidationResult);
%include <shogun/modelselection/ModelSelection.h>
%include <shogun/modelselection/GridSearchModelSelection.h>
%include <shogun/modelselection/ParameterCombination.h>

/* Templated Class DynamicObjectArray */
%include <shogun/lib/DynamicObjectArray.h>
namespace shogun
{
%template(DynamicParameterCombinationArray) CDynamicObjectArray<CParameterCombination>;
}

%include <shogun/modelselection/ModelSelectionParameters.h>
3 changes: 1 addition & 2 deletions src/shogun/base/DynArray.h
Expand Up @@ -18,7 +18,6 @@
namespace shogun
{
template <class T> class CDynamicArray;
template <class T> class CDynamicObjectArray;

/** @brief Template Dynamic array class that creates an array that can
* be used like a list or an array.
Expand All @@ -31,7 +30,7 @@ template <class T> class CDynamicObjectArray;
template <class T> class DynArray
{
template<class U> friend class CDynamicArray;
template<class U> friend class CDynamicObjectArray;
friend class CDynamicObjectArray;
friend class CCommUlongStringKernel;

public:
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/evaluation/CrossValidationSplitting.cpp
Expand Up @@ -47,7 +47,7 @@ void CCrossValidationSplitting::build_subsets()
for (index_t i=0; i<indices.vlen; ++i)
{
/* fill current subset */
CDynamicArray<index_t>* current=
CDynamicArray<index_t>* current=(CDynamicArray<index_t>*)
m_subset_indices->get_element(current_subset);

/* add element of current index */
Expand Down
12 changes: 6 additions & 6 deletions src/shogun/evaluation/SplittingStrategy.cpp
Expand Up @@ -4,7 +4,7 @@
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Heiko Strathmann
* Written (W) 2011-2012 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

Expand Down Expand Up @@ -42,7 +42,7 @@ void CSplittingStrategy::reset_subsets()
if (m_subset_indices)
SG_UNREF(m_subset_indices);

m_subset_indices=new CDynamicObjectArray<CDynamicArray<index_t> >();
m_subset_indices=new CDynamicObjectArray();
SG_REF(m_subset_indices);

/* construct all arrays */
Expand Down Expand Up @@ -83,8 +83,8 @@ SGVector<index_t> CSplittingStrategy::generate_subset_indices(index_t subset_idx
}

/* construct SGVector copy from index vector */
CDynamicArray<index_t>* to_copy=m_subset_indices->get_element_safe(
subset_idx);
CDynamicArray<index_t>* to_copy=(CDynamicArray<index_t>*)
m_subset_indices->get_element_safe(subset_idx);

index_t num_elements=to_copy->get_num_elements();
SGVector<index_t> result(num_elements, true);
Expand All @@ -106,8 +106,8 @@ SGVector<index_t> CSplittingStrategy::generate_subset_inverse(index_t subset_idx
get_name(), get_name());
}

CDynamicArray<index_t>* to_invert=m_subset_indices->get_element_safe(
subset_idx);
CDynamicArray<index_t>* to_invert=(CDynamicArray<index_t>*)
m_subset_indices->get_element_safe(subset_idx);

SGVector<index_t> result(
m_labels->get_num_labels()-to_invert->get_num_elements(), true);
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/evaluation/SplittingStrategy.h
Expand Up @@ -100,7 +100,7 @@ class CSplittingStrategy: public CSGObject
CLabels* m_labels;

/** subset indices */
CDynamicObjectArray<CDynamicArray<index_t> >* m_subset_indices;
CDynamicObjectArray* m_subset_indices;

/** additional variable to store number of index subsets */
index_t m_num_subsets;
Expand Down
17 changes: 10 additions & 7 deletions src/shogun/evaluation/StratifiedCrossValidationSplitting.cpp
Expand Up @@ -4,7 +4,7 @@
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Heiko Strathmann
* Written (W) 2011-2012 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

Expand Down Expand Up @@ -63,7 +63,7 @@ void CStratifiedCrossValidationSplitting::build_subsets()
SGVector<float64_t> unique_labels=m_labels->get_unique_labels();

/* for every label, build set for indices */
CDynamicObjectArray<CDynamicArray<index_t> > label_indices;
CDynamicObjectArray label_indices;
for (index_t i=0; i<unique_labels.vlen; ++i)
label_indices.append_element(new CDynamicArray<index_t> ());

Expand All @@ -75,7 +75,8 @@ void CStratifiedCrossValidationSplitting::build_subsets()
{
if (m_labels->get_label(j)==unique_labels.vector[i])
{
CDynamicArray<index_t>* current=label_indices.get_element(i);
CDynamicArray<index_t>* current=(CDynamicArray<index_t>*)
label_indices.get_element(i);
current->append_element(j);
SG_UNREF(current);
}
Expand All @@ -85,7 +86,8 @@ void CStratifiedCrossValidationSplitting::build_subsets()
/* shuffle created label sets */
for (index_t i=0; i<label_indices.get_num_elements(); ++i)
{
CDynamicArray<index_t>* current=label_indices.get_element(i);
CDynamicArray<index_t>* current=(CDynamicArray<index_t>*)
label_indices.get_element(i);
current->shuffle();
SG_UNREF(current);
}
Expand All @@ -95,12 +97,13 @@ void CStratifiedCrossValidationSplitting::build_subsets()
for (index_t i=0; i<unique_labels.vlen; ++i)
{
/* current index set for current label */
CDynamicArray<index_t>* current=label_indices.get_element(i);
CDynamicArray<index_t>* current=(CDynamicArray<index_t>*)
label_indices.get_element(i);

for (index_t j=0; j<current->get_num_elements(); ++j)
{
CDynamicArray<index_t>* next=m_subset_indices->get_element(
target_set++);
CDynamicArray<index_t>* next=(CDynamicArray<index_t>*)
m_subset_indices->get_element(target_set++);
next->append_element(current->get_element(j));
target_set%=m_subset_indices->get_num_elements();
SG_UNREF(next);
Expand Down

0 comments on commit e4ab8a1

Please sign in to comment.