jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
bandit_factory.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2015 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 "bandit_factory.hpp"
18 
19 #include <string>
20 #include "../common/jsonconfig.hpp"
21 #include "epsilon_greedy.hpp"
22 #include "ucb1.hpp"
23 #include "softmax.hpp"
24 #include "exp3.hpp"
25 
26 using jubatus::util::lang::shared_ptr;
29 
30 namespace jubatus {
31 namespace core {
32 namespace bandit {
33 
36  double epsilon;
37 
38  template<class Ar>
39  void serialize(Ar& ar) {
40  ar & JUBA_MEMBER(assume_unrewarded) & JUBA_MEMBER(epsilon);
41  }
42 };
43 
44 struct ucb1_config {
46 
47  template<class Ar>
48  void serialize(Ar& ar) {
49  ar & JUBA_MEMBER(assume_unrewarded);
50  }
51 };
52 
55  double tau;
56 
57  template<class Ar>
58  void serialize(Ar& ar) {
59  ar & JUBA_MEMBER(assume_unrewarded) & JUBA_MEMBER(tau);
60  }
61 };
62 
63 struct exp3_config {
65  double gamma;
66 
67  template<class Ar>
68  void serialize(Ar& ar) {
69  ar & JUBA_MEMBER(assume_unrewarded) & JUBA_MEMBER(gamma);
70  }
71 };
72 
73 shared_ptr<bandit_base> bandit_factory::create(
74  const std::string& name,
75  const common::jsonconfig::config& param) {
76  if (name == "epsilon_greedy") {
77  if (param.type() == json::json::Null) {
78  throw JUBATUS_EXCEPTION(
80  "parameter block is not specified in config"));
81  }
83  config_cast_check<epsilon_greedy_config>(param);
84  return shared_ptr<bandit_base>(
85  new epsilon_greedy(conf.assume_unrewarded, conf.epsilon));
86  } else if (name == "ucb1") {
87  if (param.type() == json::json::Null) {
88  throw JUBATUS_EXCEPTION(
90  "parameter block is not specified in config"));
91  }
92  ucb1_config conf =
93  config_cast_check<ucb1_config>(param);
94  return shared_ptr<bandit_base>(new ucb1(conf.assume_unrewarded));
95  } else if (name == "softmax") {
96  if (param.type() == json::json::Null) {
97  throw JUBATUS_EXCEPTION(
99  "parameter block is not specified in config"));
100  }
101  softmax_config conf = config_cast_check<softmax_config>(param);
102  return shared_ptr<bandit_base>(
103  new softmax(conf.assume_unrewarded, conf.tau));
104  } else if (name == "exp3") {
105  if (param.type() == json::json::Null) {
106  throw JUBATUS_EXCEPTION(
108  "parameter block is not specified in config"));
109  }
110  exp3_config conf = config_cast_check<exp3_config>(param);
111  return shared_ptr<bandit_base>(
112  new exp3(conf.assume_unrewarded, conf.gamma));
113  } else {
114  throw JUBATUS_EXCEPTION(
115  common::unsupported_method("bandit(" + name + ")"));
116  }
117 }
118 
119 } // namespace bandit
120 } // namespace core
121 } // namespace jubatus
T config_cast_check(const config &c)
Definition: cast.hpp:311
jubatus::util::text::json::json::json_type_t type() const
Definition: config.hpp:84
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
static jubatus::util::lang::shared_ptr< bandit_base > create(const std::string &name, const common::jsonconfig::config &param)