Brush C++ API
A flexible interpretable machine learning framework
Toggle main menu visibility
Loading...
Searching...
No Matches
bandit.cpp
Go to the documentation of this file.
1
#include "
bandit.h
"
2
3
namespace
Brush
{
4
namespace
MAB
{
5
6
Bandit::Bandit
() {
7
set_type
(
"dynamic_thompson"
);
8
set_arms
({});
9
set_probs
({});
10
set_bandit
();
11
}
12
13
Bandit::Bandit
(
string
type
, vector<string>
arms
) :
type
(
type
) {
14
this->
set_arms
(arms);
15
16
map<string, float> arms_probs;
17
float
prob = 1.0 /
arms
.size();
18
for
(
const
auto
& arm :
arms
) {
19
arms_probs[arm] = prob;
20
}
21
this->
set_probs
(arms_probs);
22
this->
set_bandit
();
23
}
24
25
Bandit::Bandit
(
string
type
, map<string, float> arms_probs) :
type
(
type
) {
26
this->
set_probs
(arms_probs);
27
28
vector<string> arms_names;
29
for
(
const
auto
& pair : arms_probs) {
30
arms_names.push_back(pair.first);
31
}
32
this->
set_arms
(arms_names);
33
this->
set_bandit
();
34
}
35
36
void
Bandit::set_bandit
() {
37
if
(
type
==
"thompson"
) {
38
pbandit
= make_unique<ThompsonSamplingBandit>(
probabilities
);
39
}
else
if
(
type
==
"dynamic_thompson"
) {
40
pbandit
= make_unique<ThompsonSamplingBandit>(
probabilities
,
true
);
41
}
else
if
(
type
==
"dummy"
) {
42
pbandit
= make_unique<DummyBandit>(
probabilities
);
43
}
else
{
44
HANDLE_ERROR_THROW
(
"Undefined Selection Operator "
+ this->
type
+
"\n"
);
45
}
46
47
bandit_set
=
true
;
48
}
49
50
void
Bandit::ensure_bandit_set
()
const
{
51
if
(!
bandit_set
|| !
pbandit
) {
52
HANDLE_ERROR_THROW
(
"Bandit operator is not set. Call set_bandit() before use.\n"
);
53
}
54
}
55
56
string
Bandit::get_type
() {
57
return
type
;
58
}
59
60
void
Bandit::set_type
(
string
type
) {
61
this->type =
type
;
62
}
63
64
vector<string>
Bandit::get_arms
() {
65
return
arms
;
66
}
67
68
void
Bandit::set_arms
(vector<string>
arms
) {
69
this->arms =
arms
;
70
}
71
72
map<string, float>
Bandit::get_probs
() {
73
return
probabilities
;
74
}
75
76
void
Bandit::set_probs
(map<string, float> arms_probs) {
77
probabilities
= arms_probs;
78
}
79
80
map<string, float>
Bandit::sample_probs
(
bool
update
) {
81
ensure_bandit_set
();
82
map<string, float> new_probs = this->
pbandit
->sample_probs(
update
);
83
84
// making all probabilities strictly positive
85
float
eps = 1e-6;
86
87
for
(
auto
& pair : new_probs) {
88
if
(pair.second <= 0.0f) {
89
pair.second = eps;
90
}
91
}
92
93
return
new_probs;
94
}
95
96
string
Bandit::choose
() {
97
ensure_bandit_set
();
98
return
this->
pbandit
->choose();
99
}
100
101
void
Bandit::update
(
string
arm,
float
reward) {
102
ensure_bandit_set
();
103
this->
pbandit
->update(arm, reward);
104
}
105
106
}
// MAB
107
}
// Brush
bandit.h
HANDLE_ERROR_THROW
#define HANDLE_ERROR_THROW(err)
Definition
error.h:27
Brush::MAB
Definition
bandit.cpp:4
Brush
< nsga2 selection operator for getting the front
Definition
bandit.cpp:3
Brush::MAB::Bandit::Bandit
Bandit()
Definition
bandit.cpp:6
Brush::MAB::Bandit::ensure_bandit_set
void ensure_bandit_set() const
Definition
bandit.cpp:50
Brush::MAB::Bandit::update
void update(string arm, float reward)
Updates the bandit's state based on the chosen arm and the received reward.
Definition
bandit.cpp:101
Brush::MAB::Bandit::get_type
string get_type()
Gets the type of the bandit.
Definition
bandit.cpp:56
Brush::MAB::Bandit::bandit_set
bool bandit_set
Definition
bandit.h:122
Brush::MAB::Bandit::arms
vector< string > arms
Definition
bandit.h:43
Brush::MAB::Bandit::get_arms
vector< string > get_arms()
Gets the arms of the bandit.
Definition
bandit.cpp:64
Brush::MAB::Bandit::set_type
void set_type(string type)
Sets the type of the bandit.
Definition
bandit.cpp:60
Brush::MAB::Bandit::set_arms
void set_arms(vector< string > arms)
Sets the arms of the bandit.
Definition
bandit.cpp:68
Brush::MAB::Bandit::probabilities
std::map< string, float > probabilities
Definition
bandit.h:45
Brush::MAB::Bandit::type
std::string type
Definition
bandit.h:42
Brush::MAB::Bandit::set_probs
void set_probs(map< string, float > arms_probs)
Sets the probabilities associated with each arm.
Definition
bandit.cpp:76
Brush::MAB::Bandit::choose
string choose()
Selects an arm.
Definition
bandit.cpp:96
Brush::MAB::Bandit::sample_probs
map< string, float > sample_probs(bool update=false)
Samples the probabilities associated with each arm using the policy.
Definition
bandit.cpp:80
Brush::MAB::Bandit::set_bandit
void set_bandit()
Sets the bandit operator (policy).
Definition
bandit.cpp:36
Brush::MAB::Bandit::pbandit
std::shared_ptr< BanditOperator > pbandit
A shared pointer to the bandit operator (policy).
Definition
bandit.h:39
Brush::MAB::Bandit::get_probs
map< string, float > get_probs()
Gets the probabilities associated with each arm.
Definition
bandit.cpp:72
src
bandit
bandit.cpp
Generated by
1.17.0