SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2011-2012 Heiko Strathmann 00008 * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/evaluation/SplittingStrategy.h> 00012 #include <shogun/labels/Labels.h> 00013 00014 using namespace shogun; 00015 00016 CSplittingStrategy::CSplittingStrategy() 00017 { 00018 init(); 00019 } 00020 00021 CSplittingStrategy::CSplittingStrategy(CLabels* labels, int32_t num_subsets) 00022 { 00023 init(); 00024 00025 m_num_subsets=num_subsets; 00026 00027 /* "assert" that num_subsets is smaller than num labels */ 00028 if (labels->get_num_labels()<num_subsets) 00029 { 00030 SG_ERROR("Only %d labels for %d subsets!\n", labels->get_num_labels(), 00031 num_subsets); 00032 } 00033 00034 m_labels=labels; 00035 SG_REF(m_labels); 00036 00037 reset_subsets(); 00038 } 00039 00040 void CSplittingStrategy::reset_subsets() 00041 { 00042 if (m_subset_indices) 00043 SG_UNREF(m_subset_indices); 00044 00045 m_subset_indices=new CDynamicObjectArray(); 00046 SG_REF(m_subset_indices); 00047 00048 /* construct all arrays */ 00049 for (index_t i=0; i<m_num_subsets; ++i) 00050 m_subset_indices->append_element(new CDynamicArray<index_t> ()); 00051 00052 m_is_filled=false; 00053 } 00054 00055 void CSplittingStrategy::init() 00056 { 00057 m_labels=NULL; 00058 m_subset_indices=NULL; 00059 SG_REF(m_subset_indices); 00060 m_is_filled=false; 00061 m_num_subsets=0; 00062 00063 m_parameters->add((CSGObject**)&m_labels, "labels", "Labels for subsets"); 00064 m_parameters->add((CSGObject**)&m_subset_indices, "subset_indices", 00065 "Set of sets of subset indices"); 00066 m_parameters->add(&m_is_filled, "is_filled", "Whether ther are index sets"); 00067 m_parameters->add(&m_num_subsets, "num_subsets", "Number of index sets"); 00068 } 00069 00070 CSplittingStrategy::~CSplittingStrategy() 00071 { 00072 SG_UNREF(m_labels); 00073 SG_UNREF(m_subset_indices); 00074 } 00075 00076 SGVector<index_t> CSplittingStrategy::generate_subset_indices(index_t subset_idx) 00077 { 00078 if (!m_is_filled) 00079 { 00080 SG_ERROR("Call %s::build_subsets() before accessing them! If this error" 00081 " stays, its an implementation error of %s::build_subsets()\n", 00082 get_name(), get_name()); 00083 } 00084 00085 /* construct SGVector copy from index vector */ 00086 CDynamicArray<index_t>* to_copy=(CDynamicArray<index_t>*) 00087 m_subset_indices->get_element_safe(subset_idx); 00088 00089 index_t num_elements=to_copy->get_num_elements(); 00090 SGVector<index_t> result(num_elements, true); 00091 00092 /* copy data */ 00093 memcpy(result.vector, to_copy->get_array(), sizeof(index_t)*num_elements); 00094 00095 SG_UNREF(to_copy); 00096 00097 return result; 00098 } 00099 00100 SGVector<index_t> CSplittingStrategy::generate_subset_inverse(index_t subset_idx) 00101 { 00102 if (!m_is_filled) 00103 { 00104 SG_ERROR("Call %s::build_subsets() before accessing them! If this error" 00105 " stays, its an implementation error of %s::build_subsets()\n", 00106 get_name(), get_name()); 00107 } 00108 00109 CDynamicArray<index_t>* to_invert=(CDynamicArray<index_t>*) 00110 m_subset_indices->get_element_safe(subset_idx); 00111 00112 SGVector<index_t> result( 00113 m_labels->get_num_labels()-to_invert->get_num_elements(), true); 00114 00115 index_t index=0; 00116 for (index_t i=0; i<m_labels->get_num_labels(); ++i) 00117 { 00118 /* add i to inverse indices if it is not in the to be inverted set */ 00119 if (to_invert->find_element(i)==-1) 00120 result.vector[index++]=i; 00121 } 00122 00123 SG_UNREF(to_invert); 00124 00125 return result; 00126 } 00127 00128 index_t CSplittingStrategy::get_num_subsets() const 00129 { 00130 return m_subset_indices->get_num_elements(); 00131 }