jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
clustering.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2013 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #include "clustering.hpp"
18 
19 #include <algorithm>
20 #include <utility>
21 #include <vector>
22 #include "jubatus/util/lang/bind.h"
23 #include "../clustering/clustering.hpp"
24 #include "../fv_converter/datum_to_fv_converter.hpp"
25 #include "../common/vector_util.hpp"
26 #include "../fv_converter/revert.hpp"
27 #include "../fv_converter/weight_manager.hpp"
28 #include "../fv_converter/mixable_weight_manager.hpp"
29 
30 namespace jubatus {
31 namespace core {
32 namespace driver {
33 
34 using std::vector;
35 using std::pair;
36 using jubatus::util::lang::shared_ptr;
37 using fv_converter::datum;
39 using fv_converter::weight_manager;
40 
42  shared_ptr<core::clustering::clustering> clustering_method,
43  shared_ptr<fv_converter::datum_to_fv_converter> converter)
44  : converter_(converter),
45  clustering_(clustering_method),
46  wm_(mixable_weight_manager::model_ptr(new weight_manager)) {
47  register_mixable(clustering_->get_mixable());
48  register_mixable(&wm_);
49 
50  converter_->set_weight_manager(wm_.get_model());
51 }
52 
53 clustering::~clustering() {
54 }
55 
56 void clustering::push(const vector<datum>& points) {
57  clustering_->push(to_weighted_point_vector(points));
58 }
59 
60 datum clustering::get_nearest_center(
61  const datum& point) const {
62  return to_datum(
63  clustering_->get_nearest_center(to_sfv_const(point)));
64 }
65 
67  clustering::get_nearest_members(const datum& point) const {
68  return to_weighted_datum_vector(
69  clustering_->get_nearest_members(to_sfv_const(point)));
70 }
71 
72 vector<datum> clustering::get_k_center() const {
73  return to_datum_vector(clustering_->get_k_center());
74 }
75 
77 clustering::get_core_members() const {
78  vector<vector<core::clustering::weighted_point> > src =
79  clustering_->get_core_members();
80 
82  ret.reserve(src.size());
83  std::transform(
84  src.begin(),
85  src.end(),
86  std::back_inserter(ret),
87  jubatus::util::lang::bind(
88  &clustering::to_weighted_datum_vector,
89  this, jubatus::util::lang::_1));
90 
91  return ret;
92 }
93 
94 size_t clustering::get_revision() const {
95  return clustering_->get_revision();
96 }
97 
98 void clustering::do_clustering() {
99  clustering_->do_clustering();
100 }
101 
102 // private
103 
104 common::sfv_t clustering::to_sfv(const datum& dat) {
105  common::sfv_t ret;
106  converter_->convert_and_update_weight(dat, ret);
108  return ret;
109 }
110 
111 common::sfv_t clustering::to_sfv_const(const datum& dat) const {
112  common::sfv_t ret;
113  converter_->convert(dat, ret);
115  return ret;
116 }
117 
118 datum clustering::to_datum(const common::sfv_t& src) const {
119  datum ret;
121  return ret;
122 }
123 
124 core::clustering::weighted_point clustering::to_weighted_point(
125  const datum& src) {
127  ret.data = to_sfv(src);
128  ret.weight = 1;
129  ret.original = src;
130  return ret;
131 }
132 
133 pair<double, datum> clustering::to_weighted_datum(
134  const core::clustering::weighted_point& src) const {
135  return std::make_pair(src.weight, src.original);
136 }
137 
138 vector<datum> clustering::to_datum_vector(
139  const vector<common::sfv_t>& src) const {
140  vector<datum> ret;
141  ret.reserve(src.size());
142  std::transform(
143  src.begin(),
144  src.end(),
145  std::back_inserter(ret),
146  jubatus::util::lang::bind(
147  &clustering::to_datum,
148  this, jubatus::util::lang::_1));
149  return ret;
150 }
151 
152 vector<core::clustering::weighted_point>
153  clustering::to_weighted_point_vector(
154  const vector<datum>& src) {
155  vector<core::clustering::weighted_point> ret;
156  ret.reserve(src.size());
157  std::transform(
158  src.begin(),
159  src.end(),
160  std::back_inserter(ret),
161  jubatus::util::lang::bind(
162  &clustering::to_weighted_point,
163  this, jubatus::util::lang::_1));
164  return ret;
165 }
166 
168  clustering::to_weighted_datum_vector(
169  const vector<core::clustering::weighted_point>& src) const {
171  ret.reserve(src.size());
172  std::transform(
173  src.begin(),
174  src.end(),
175  std::back_inserter(ret),
176  jubatus::util::lang::bind(
177  &clustering::to_weighted_datum,
178  this, jubatus::util::lang::_1));
179  return ret;
180 }
181 
182 void clustering::pack(framework::packer& pk) const {
183  pk.pack_array(2);
184  clustering_->pack(pk);
185  wm_.get_model()->pack(pk);
186 }
187 
188 void clustering::unpack(msgpack::object o) {
189  if (o.type != msgpack::type::ARRAY || o.via.array.size != 2) {
190  throw msgpack::type_error();
191  }
192 
193  // clear
194  clustering_->clear();
195  converter_->clear_weights();
196 
197  // load
198  clustering_->unpack(o.via.array.ptr[0]);
199  wm_.get_model()->unpack(o.via.array.ptr[1]);
200 }
201 
202 void clustering::clear() {
203  clustering_->clear();
204  converter_->clear_weights();
205 }
206 
207 } // namespace driver
208 } // namespace core
209 } // namespace jubatus
void revert_feature(const common::sfv_t &fv, fv_converter::datum &data)
Definition: revert.cpp:31
std::vector< cluster_unit > cluster_set
Definition: types.hpp:37
std::vector< std::pair< cluster_weight, jubatus::core::fv_converter::datum > > cluster_unit
Definition: types.hpp:36
framework::linear_mixable_helper< weight_manager, versioned_weight_diff > mixable_weight_manager
clustering(jubatus::util::lang::shared_ptr< core::clustering::clustering > clustering_method, jubatus::util::lang::shared_ptr< fv_converter::datum_to_fv_converter > converter)
void sort_and_merge(sfv_t &sfv)
Definition: vector_util.cpp:28
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
storage::mixable_lsh_index_storage::model_ptr model_ptr
Definition: euclid_lsh.cpp:90