jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
lof_storage.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_ANOMALY_LOF_STORAGE_HPP_
18 #define JUBATUS_CORE_ANOMALY_LOF_STORAGE_HPP_
19 
20 #include <iosfwd>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include <msgpack.hpp>
26 #include "jubatus/util/data/serialization.h"
27 #include "jubatus/util/data/unordered_map.h"
28 #include "jubatus/util/data/unordered_set.h"
29 #include "jubatus/util/lang/shared_ptr.h"
30 #include "jubatus/util/text/json.h"
31 
32 #include "../common/type.hpp"
33 #include "../common/unordered_map.hpp"
34 #include "../framework/mixable_helper.hpp"
35 
36 namespace jubatus {
37 namespace core {
38 namespace recommender {
39 class recommender_base;
40 } // namespace recommender
41 namespace anomaly {
42 
43 struct lof_entry {
44  float kdist;
45  float lrd;
46 
47  MSGPACK_DEFINE(kdist, lrd);
48 };
49 
50 typedef jubatus::util::data::unordered_map<std::string, lof_entry> lof_table_t;
51 
52 class lof_storage {
53  public:
54  static const uint32_t DEFAULT_NEIGHBOR_NUM;
55  static const uint32_t DEFAULT_REVERSE_NN_NUM;
56 
57  struct config {
58  config();
59 
62 
63  template<typename Ar>
64  void serialize(Ar& ar) {
65  ar
66  & JUBA_MEMBER(nearest_neighbor_num)
67  & JUBA_MEMBER(reverse_nearest_neighbor_num);
68  }
69  };
70 
71  lof_storage();
72  explicit lof_storage(
73  jubatus::util::lang::shared_ptr<core::recommender::recommender_base>
74  nn_engine);
75 
76  // config contains parameters for the underlying nearest neighbor search
77  explicit lof_storage(
78  const config& config,
79  jubatus::util::lang::shared_ptr<core::recommender::recommender_base>
80  nn_engine);
81 
82  virtual ~lof_storage();
83 
84  // For Analyze
85  // calculate lrd of query and lrd values of its neighbors
86  float collect_lrds(
87  const common::sfv_t& query,
88  jubatus::util::data::unordered_map<std::string, float>&
89  neighbor_lrd) const;
90  float collect_lrds(
91  const std::string& id,
92  jubatus::util::data::unordered_map<std::string, float>&
93  neighbor_lrd) const;
94 
95  // For Update
96  void remove_row(const std::string& row);
97  void clear();
98  void get_all_row_ids(std::vector<std::string>& ids) const;
99  void update_row(const std::string& row, const common::sfv_t& diff);
100 
101  void update_all(); // Update kdists and lrds
102 
103  std::string name() const;
104 
105  // getter & setter & update for kdist and lrd values
106  float get_kdist(const std::string& row) const;
107  float get_lrd(const std::string& row) const;
108 
109  bool has_row(const std::string& row) const;
110 
111  // just for test
112  void set_nn_engine(
113  jubatus::util::lang::shared_ptr<core::recommender::recommender_base>
114  nn_engine);
115 
116  void get_diff(lof_table_t& diff) const;
117  bool put_diff(const lof_table_t& mixed_diff);
118  void mix(const lof_table_t& lhs, lof_table_t& rhs) const;
119 
121  return storage::version();
122  }
123 
125  packer.pack(*this);
126  }
127  void unpack(msgpack::object o) {
128  o.convert(this);
129  }
130 
132 
133  private:
134  static void mark_removed(lof_entry& entry);
135  static bool is_removed(const lof_entry& entry);
136 
138  const std::vector<std::pair<std::string, float> >& neighbors,
139  jubatus::util::data::unordered_map<std::string, float>&
140  neighbor_lrd) const;
141 
142  void collect_neighbors(
143  const std::string& row,
144  jubatus::util::data::unordered_set<std::string>& nn) const;
145 
146  void update_entries(
147  const jubatus::util::data::unordered_set<std::string>& rows);
148  void update_kdist(const std::string& row);
149  void update_lrd(const std::string& row);
150 
152  const std::string& row,
153  const std::vector<std::pair<std::string, float> >& neighbors);
155  const std::string& row,
156  const std::vector<std::pair<std::string, float> >& neighbors);
157 
158  lof_table_t lof_table_; // table for storing k-dist and lrd values
159  lof_table_t lof_table_diff_;
160 
161  uint32_t neighbor_num_; // k of k-nn
162  uint32_t reverse_nn_num_; // ck of ck-nn as an approx. of k-reverse-nn
163 
164  jubatus::util::lang::shared_ptr<core::recommender::recommender_base>
166 };
167 
170 
171 } // namespace anomaly
172 } // namespace core
173 } // namespace jubatus
174 
175 #endif // JUBATUS_CORE_ANOMALY_LOF_STORAGE_HPP_
static bool is_removed(const lof_entry &entry)
float get_kdist(const std::string &row) const
MSGPACK_DEFINE(kdist, lrd)
void update_row(const std::string &row, const common::sfv_t &diff)
float lrd
Definition: lof_storage.hpp:45
void mix(const lof_table_t &lhs, lof_table_t &rhs) const
float get_lrd(const std::string &row) const
static const uint32_t DEFAULT_NEIGHBOR_NUM
Definition: lof_storage.hpp:54
MSGPACK_DEFINE(lof_table_, lof_table_diff_, neighbor_num_, reverse_nn_num_)
void update_kdist(const std::string &row)
static const uint32_t DEFAULT_REVERSE_NN_NUM
Definition: lof_storage.hpp:55
float kdist
Definition: lof_storage.hpp:44
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_
void update_lrd_with_neighbors(const std::string &row, const std::vector< std::pair< std::string, float > > &neighbors)
void get_diff(lof_table_t &diff) const
void update_entries(const jubatus::util::data::unordered_set< std::string > &rows)
bool put_diff(const lof_table_t &mixed_diff)
void pack(framework::packer &packer) const
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
void get_all_row_ids(std::vector< std::string > &ids) const
framework::linear_mixable_helper< lof_storage, lof_table_t > mixable_lof_storage
void unpack(msgpack::object o)
void set_nn_engine(jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine)
void collect_neighbors(const std::string &row, jubatus::util::data::unordered_set< std::string > &nn) const
static void mark_removed(lof_entry &entry)
float collect_lrds(const common::sfv_t &query, jubatus::util::data::unordered_map< std::string, float > &neighbor_lrd) const
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
storage::version get_version() const
void remove_row(const std::string &row)
Definition: lof_storage.hpp:43
bool has_row(const std::string &row) const
void update_kdist_with_neighbors(const std::string &row, const std::vector< std::pair< std::string, float > > &neighbors)
void update_lrd(const std::string &row)
jubatus::util::data::unordered_map< std::string, lof_entry > lof_table_t
Definition: lof_storage.hpp:50
float collect_lrds_from_neighbors(const std::vector< std::pair< std::string, float > > &neighbors, jubatus::util::data::unordered_map< std::string, float > &neighbor_lrd) const