SHOGUN  v3.0.0
 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 
48  std::string str_ordering;
49  switch(ordering)
50  {
51  case NATURAL:
52  str_ordering="NATURAL";
53  break;
54  case LARGEST_FIRST:
55  str_ordering="LARGEST_FIRST";
56  break;
57  case DYNAMIC_LARGEST_FIRST:
58  str_ordering="DYNAMIC_LARGEST_FIRST";
59  break;
60  case DISTANCE_TWO_LARGEST_FIRST:
61  str_ordering="DISTANCE_TWO_LARGEST_FIRST";
62  break;
63  case SMALLEST_LAST:
64  str_ordering="SMALLEST_LAST";
65  break;
66  case DISTANCE_TWO_SMALLEST_LAST:
67  str_ordering="DISTANCE_TWO_SMALLEST_LAST";
68  break;
69  case INCIDENCE_DEGREE:
70  str_ordering="INCIDENCE_DEGREE";
71  break;
72  case DISTANCE_TWO_INCIDENCE_DEGREE:
73  str_ordering="DISTANCE_TWO_INCIDENCE_DEGREE";
74  break;
75  case RANDOM:
76  str_ordering="RANDOM";
77  break;
78  }
79 
80  std::string str_coloring;
81  switch(coloring)
82  {
83  case DISTANCE_ONE:
84  str_coloring="DISTANCE_ONE";
85  break;
86  case ACYCLIC:
87  str_coloring="ACYCLIC";
88  break;
89  case ACYCLIC_FOR_INDIRECT_RECOVERY:
90  str_coloring="ACYCLIC_FOR_INDIRECT_RECOVERY";
91  break;
92  case STAR:
93  str_coloring="STAR";
94  break;
95  case RESTRICTED_STAR:
96  str_coloring="RESTRICTED_STAR";
97  break;
98  case DISTANCE_TWO:
99  str_coloring="DISTANCE_TWO";
100  break;
101  }
102 
103  m_ordering=SGString<char>(index_t(str_ordering.size()));
104  m_coloring=SGString<char>(index_t(str_coloring.size()));
105  memcpy(m_ordering.string, str_ordering.data(), str_ordering.size());
106  memcpy(m_coloring.string, str_coloring.data(), str_coloring.size());
107 
108  SG_REF(m_matrix_operator);
109 }
110 
111 void CProbingSampler::init()
112 {
113  m_matrix_operator=NULL;
114  m_power=1;
115 
116  SG_ADD(&m_coloring_vector, "coloring_vector", "the coloring vector generated"
117  " from coloring", MS_NOT_AVAILABLE);
118 
119  SG_ADD(&m_power, "matrix_power", "power of the sparse-matrix for coloring",
121 
122  SG_ADD(&m_ordering, "ordering_variant", "ordering variant for coloring",
124 
125  SG_ADD(&m_coloring, "coloring_variant", "coloring variant for coloring",
127 
128  SG_ADD((CSGObject**)&m_matrix_operator, "matrix_operator",
129  "the sparse-matrix linear opeator for coloring", MS_NOT_AVAILABLE);
130 }
131 
132 CProbingSampler::~CProbingSampler()
133 {
134  SG_UNREF(m_matrix_operator);
135 }
136 
137 SGVector<int32_t> CProbingSampler::get_coloring_vector() const
138 {
139  return m_coloring_vector;
140 }
141 
142 void CProbingSampler::precompute()
143 {
144  SG_DEBUG("Entering\n");
145  // do coloring things here and save the coloring vector
146  SparsityStructure* sp_str=m_matrix_operator->get_sparsity_structure(m_power);
147 
148  GraphColoringInterface* Color
149  =new GraphColoringInterface(SRC_MEM_ADOLC, sp_str->m_ptr, sp_str->m_num_rows);
150 
151  std::string ordering(m_ordering.string, m_ordering.slen);
152  std::string coloring(m_coloring.string, m_coloring.slen);
153  Color->Coloring(ordering, coloring);
154 
155  std::vector<int32_t> vi_VertexColors;
156  Color->GetVertexColors(vi_VertexColors);
157 
158  REQUIRE(vi_VertexColors.size()==static_cast<uint32_t>(m_dimension),
159  "dimension mismatch, %d vs %d!\n", vi_VertexColors.size(), m_dimension);
160 
161  m_coloring_vector=SGVector<int32_t>(vi_VertexColors.size());
162 
163  for (std::vector<int32_t>::iterator it=vi_VertexColors.begin();
164  it!=vi_VertexColors.end(); it++)
165  {
166  index_t i=static_cast<index_t>(std::distance(vi_VertexColors.begin(), it));
167  m_coloring_vector[i]=*it;
168  }
169 
170  Map<VectorXi> colors(m_coloring_vector.vector, m_coloring_vector.vlen);
171  m_num_samples=colors.maxCoeff()+1;
172  SG_DEBUG("Using %d samples (aka colours) for probing trace sampler\n",
173  m_num_samples);
174 
175  delete sp_str;
176  delete Color;
177 
178  SG_DEBUG("Leaving\n");
179 }
180 
181 SGVector<float64_t> CProbingSampler::sample(index_t idx) const
182 {
183  REQUIRE(idx<m_num_samples, "Given index (%d) must be smaller than "
184  "number of samples to draw (%d)\n", idx, m_num_samples);
185 
186  SGVector<float64_t> s(m_dimension);
187  s.set_const(0.0);
188 
189  for (index_t i=0; i<m_dimension; ++i)
190  {
191  if (m_coloring_vector[i]==idx)
192  {
194  s[i]=(x>0)-(x<0);
195  }
196  }
197 
198  return s;
199 }
200 
201 }
202 
203 #endif // HAVE_EIGEN3
204 #endif // HAVE_COLPACK

SHOGUN Machine Learning Toolbox - Documentation