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

SHOGUN Machine Learning Toolbox - Documentation