SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProbingSampler.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) 2013 Soumyajit De
8  */
9 
10 #include <shogun/lib/common.h>
11 
12 #ifdef HAVE_COLPACK
13 #ifdef HAVE_EIGEN3
14 
15 #include <vector>
16 #include <string>
17 #include <cstring>
18 #include <shogun/lib/SGVector.h>
19 #include <shogun/lib/SGString.h>
20 #include <shogun/base/Parameter.h>
25 #include <ColPack/ColPackHeaders.h>
26 
27 using namespace Eigen;
28 using namespace ColPack;
29 
30 namespace shogun
31 {
32 
33 CProbingSampler::CProbingSampler() : CTraceSampler()
34 {
35  init();
36 }
37 
38 CProbingSampler::CProbingSampler(
39  CSparseMatrixOperator<float64_t>* matrix_operator, int64_t power,
40  EOrderingVariant ordering, EColoringVariant coloring)
41  : CTraceSampler(matrix_operator->get_dimension())
42 {
43  init();
44 
45  m_power=power;
46  m_matrix_operator=matrix_operator;
47  m_ordering=ordering;
48  m_coloring=coloring;
49 
50  SG_REF(m_matrix_operator);
51 }
52 
53 void CProbingSampler::init()
54 {
55  m_matrix_operator=NULL;
56  m_power=1;
57  m_ordering=NATURAL;
58  m_coloring=DISTANCE_TWO;
59  m_is_precomputed=false;
60 
61  SG_ADD(&m_coloring_vector, "coloring_vector", "the coloring vector generated"
62  " from coloring", MS_NOT_AVAILABLE);
63 
64  SG_ADD(&m_power, "matrix_power", "power of the sparse-matrix for coloring",
66 
67  SG_ADD(&m_is_precomputed, "is_precomputed",
68  "flag that is true if already precomputed", MS_NOT_AVAILABLE);
69 
70  SG_ADD((CSGObject**)&m_matrix_operator, "matrix_operator",
71  "the sparse-matrix linear opeator for coloring", MS_NOT_AVAILABLE);
72 }
73 
74 CProbingSampler::~CProbingSampler()
75 {
76  SG_UNREF(m_matrix_operator);
77 }
78 
79 void CProbingSampler::set_coloring_vector(SGVector<int32_t> coloring_vector)
80 {
81  m_coloring_vector=coloring_vector;
82  m_is_precomputed=true;
83 }
84 
85 SGVector<int32_t> CProbingSampler::get_coloring_vector() const
86 {
87  return m_coloring_vector;
88 }
89 
90 void CProbingSampler::precompute()
91 {
92  SG_DEBUG("Entering\n");
93 
94  // if already precomputed, nothing to do
95  if (m_is_precomputed)
96  {
97  SG_DEBUG("Coloring vector already computed! Exiting!\n");
98  return;
99  }
100 
101  // do coloring things here and save the coloring vector
102  SparsityStructure* sp_str=m_matrix_operator->get_sparsity_structure(m_power);
103 
104  GraphColoringInterface* Color
105  =new GraphColoringInterface(SRC_MEM_ADOLC, sp_str->m_ptr, sp_str->m_num_rows);
106 
107  std::string str_ordering;
108  switch(m_ordering)
109  {
110  case NATURAL:
111  str_ordering="NATURAL";
112  break;
113  case LARGEST_FIRST:
114  str_ordering="LARGEST_FIRST";
115  break;
116  case DYNAMIC_LARGEST_FIRST:
117  str_ordering="DYNAMIC_LARGEST_FIRST";
118  break;
119  case DISTANCE_TWO_LARGEST_FIRST:
120  str_ordering="DISTANCE_TWO_LARGEST_FIRST";
121  break;
122  case SMALLEST_LAST:
123  str_ordering="SMALLEST_LAST";
124  break;
125  case DISTANCE_TWO_SMALLEST_LAST:
126  str_ordering="DISTANCE_TWO_SMALLEST_LAST";
127  break;
128  case INCIDENCE_DEGREE:
129  str_ordering="INCIDENCE_DEGREE";
130  break;
131  case DISTANCE_TWO_INCIDENCE_DEGREE:
132  str_ordering="DISTANCE_TWO_INCIDENCE_DEGREE";
133  break;
134  case RANDOM:
135  str_ordering="RANDOM";
136  break;
137  }
138 
139  std::string str_coloring;
140  switch(m_coloring)
141  {
142  case DISTANCE_ONE:
143  str_coloring="DISTANCE_ONE";
144  break;
145  case ACYCLIC:
146  str_coloring="ACYCLIC";
147  break;
148  case ACYCLIC_FOR_INDIRECT_RECOVERY:
149  str_coloring="ACYCLIC_FOR_INDIRECT_RECOVERY";
150  break;
151  case STAR:
152  str_coloring="STAR";
153  break;
154  case RESTRICTED_STAR:
155  str_coloring="RESTRICTED_STAR";
156  break;
157  case DISTANCE_TWO:
158  str_coloring="DISTANCE_TWO";
159  break;
160  }
161 
162  Color->Coloring(str_ordering, str_coloring);
163 
164  std::vector<int32_t> vi_VertexColors;
165  Color->GetVertexColors(vi_VertexColors);
166 
167  REQUIRE(vi_VertexColors.size()==static_cast<uint32_t>(m_dimension),
168  "dimension mismatch, %d vs %d!\n", vi_VertexColors.size(), m_dimension);
169 
170  m_coloring_vector=SGVector<int32_t>(vi_VertexColors.size());
171 
172  for (std::vector<int32_t>::iterator it=vi_VertexColors.begin();
173  it!=vi_VertexColors.end(); it++)
174  {
175  index_t i=static_cast<index_t>(std::distance(vi_VertexColors.begin(), it));
176  m_coloring_vector[i]=*it;
177  }
178 
179  Map<VectorXi> colors(m_coloring_vector.vector, m_coloring_vector.vlen);
180  m_num_samples=colors.maxCoeff()+1;
181  SG_DEBUG("Using %d samples (aka colours) for probing trace sampler\n",
182  m_num_samples);
183 
184  delete sp_str;
185  delete Color;
186 
187  // set the precomputed flag true
188  m_is_precomputed=true;
189 
190  SG_DEBUG("Leaving\n");
191 }
192 
193 SGVector<float64_t> CProbingSampler::sample(index_t idx) const
194 {
195  REQUIRE(idx<m_num_samples, "Given index (%d) must be smaller than "
196  "number of samples to draw (%d)\n", idx, m_num_samples);
197 
198  SGVector<float64_t> s(m_dimension);
199  s.set_const(0.0);
200 
201  for (index_t i=0; i<m_dimension; ++i)
202  {
203  if (m_coloring_vector[i]==idx)
204  {
206  s[i]=(x>0)-(x<0);
207  }
208  }
209 
210  return s;
211 }
212 
213 }
214 
215 #endif // HAVE_EIGEN3
216 #endif // HAVE_COLPACK

SHOGUN Machine Learning Toolbox - Documentation