Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
bandit.h
Go to the documentation of this file.
1/* Brush
2copyright 2024 William La Cava
3license: GNU/GPL v3
4*/
5
6#ifndef BANDIT_H
7#define BANDIT_H
8
9#include "../init.h"
10#include "../types.h"
11#include "../program/program.h"
13#include "../util/utils.h"
14#include "bandit_operator.h"
15#include "dummy.h"
16#include "thompson.h"
17#include "linear_thompson.h"
18
19namespace Brush {
20namespace MAB {
21
22using namespace Brush;
23
32template <typename T>
33struct Bandit
34{
35 using Iter = tree<Node>::pre_order_iterator;
36
37public:
41 std::shared_ptr<BanditOperator<T>> pbandit;
42 // TODO: This should be a shared pointer to allow multiple instances of Bandit to share the same operator.
43
44 std::string type;
45 vector<T> arms;
46
47 std::map<T, float> probabilities;
48
49 Bandit();
51
57 Bandit(string type, vector<T> arms);
58
64 Bandit(string type, map<T, float> arms_probs);
65
69 void set_bandit();
70
74 void set_arms(vector<T> arms);
75
79 vector<T> get_arms();
80
84 string get_type();
85
89 void set_type(string type);
90
94 map<T, float> get_probs();
95
100 void set_probs(map<T, float> arms_probs);
101
107 map<T, float> sample_probs(bool update=false);
108
117 T choose(const VectorXf& context);
118
124 void update(T arm, float reward, VectorXf& context);
125
126 template <ProgramType PT>
127 VectorXf get_context(const Program<PT>& program, Iter spot,
128 const SearchSpace &ss, const Dataset &d);
129};
130
131//TODO: serialization should save the type of bandit and its parameters
132
133} // MAB
134} // Brush
135#endif
holds variable type data.
Definition data.h:51
< nsga2 selection operator for getting the front
Definition bandit.cpp:4
tree< Node >::pre_order_iterator Iter
Definition program.h:37
tree< Node >::pre_order_iterator Iter
Definition bandit.h:35
VectorXf get_context(const Program< PT > &program, Iter spot, const SearchSpace &ss, const Dataset &d)
Definition bandit.cpp:103
T choose(const VectorXf &context)
Selects an arm using the tree and fitness as context.
Definition bandit.cpp:93
std::shared_ptr< BanditOperator< T > > pbandit
A shared pointer to the bandit operator (policy).
Definition bandit.h:41
void set_arms(vector< T > arms)
Sets the arms of the bandit.
Definition bandit.cpp:73
string get_type()
Gets the type of the bandit.
Definition bandit.cpp:58
void set_type(string type)
Sets the type of the bandit.
Definition bandit.cpp:63
vector< T > get_arms()
Gets the arms of the bandit.
Definition bandit.cpp:68
map< T, float > get_probs()
Gets the probabilities associated with each arm.
Definition bandit.cpp:78
std::string type
Definition bandit.h:44
vector< T > arms
Definition bandit.h:45
void update(T arm, float reward, VectorXf &context)
Updates the bandit's state based on the chosen arm and the received reward.
Definition bandit.cpp:98
std::map< T, float > probabilities
Definition bandit.h:47
void set_probs(map< T, float > arms_probs)
Sets the probabilities associated with each arm.
Definition bandit.cpp:83
void set_bandit()
Sets the bandit operator (policy).
Definition bandit.cpp:41
map< T, float > sample_probs(bool update=false)
Samples the probabilities associated with each arm using the policy.
Definition bandit.cpp:88
An individual program, a.k.a. model.
Definition program.h:50
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...