jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
nearest_neighbor_base.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012 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 #ifndef JUBATUS_CORE_NEAREST_NEIGHBOR_NEAREST_NEIGHBOR_BASE_HPP_
18 #define JUBATUS_CORE_NEAREST_NEIGHBOR_NEAREST_NEIGHBOR_BASE_HPP_
19 
20 #include <stdint.h>
21 #include <iosfwd>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 #include "jubatus/util/lang/shared_ptr.h"
26 #include "../common/type.hpp"
27 #include "../framework/mixable_versioned_table.hpp"
28 #include "../framework/mixable.hpp"
29 
30 namespace jubatus {
31 namespace core {
32 namespace storage {
33 class column_table;
34 } // namespace storage
35 namespace nearest_neighbor {
36 
38  public:
39  explicit nearest_neighbor_base(
40  jubatus::util::lang::shared_ptr<storage::column_table> table,
41  const std::string& id);
43 
44  void get_all_row_ids(std::vector<std::string>& ids) const;
45 
46  jubatus::util::lang::shared_ptr<const storage::column_table>
47  get_const_table() const {
48  return mixable_table_->get_model();
49  }
50  jubatus::util::lang::shared_ptr<storage::column_table> get_table() {
51  return mixable_table_->get_model();
52  }
53 
54  virtual std::string type() const = 0;
55 
56  virtual void clear();
57 
58  virtual void set_row(const std::string& id, const common::sfv_t& sfv) = 0;
59  virtual void neighbor_row(
60  const common::sfv_t& query,
61  std::vector<std::pair<std::string, float> >& ids,
62  uint64_t ret_num) const = 0;
63  virtual void neighbor_row(
64  const std::string& query_id,
65  std::vector<std::pair<std::string, float> >& ids,
66  uint64_t ret_num) const = 0;
67  virtual float calc_similarity(float distance) const {
68  return 1 - distance;
69  }
70 
71  virtual void similar_row(
72  const common::sfv_t& query,
73  std::vector<std::pair<std::string, float> >& ids,
74  uint64_t ret_num) const;
75  virtual void similar_row(
76  const std::string& query_id,
77  std::vector<std::pair<std::string, float> >& ids,
78  uint64_t ret_num) const;
79 
80  void pack(framework::packer& packer) const;
81  void unpack(msgpack::object o);
82 
84 
85  protected:
86  std::string my_id_;
87 
88  private:
89  jubatus::util::lang::shared_ptr<framework::mixable_versioned_table>
91 };
92 
93 } // namespace nearest_neighbor
94 } // namespace core
95 } // namespace jubatus
96 
97 #endif // JUBATUS_CORE_NEAREST_NEIGHBOR_NEAREST_NEIGHBOR_BASE_HPP_
virtual void set_row(const std::string &id, const common::sfv_t &sfv)=0
jubatus::util::lang::shared_ptr< storage::column_table > get_table()
jubatus::util::lang::shared_ptr< framework::mixable_versioned_table > mixable_table_
virtual void neighbor_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, uint64_t ret_num) const =0
void get_all_row_ids(std::vector< std::string > &ids) const
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
jubatus::util::lang::shared_ptr< const storage::column_table > get_const_table() const
nearest_neighbor_base(jubatus::util::lang::shared_ptr< storage::column_table > table, const std::string &id)
virtual void similar_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, uint64_t ret_num) const