-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rs
341 lines (321 loc) · 8.38 KB
/
app.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use actix::prelude::*;
use rand::{rngs::ThreadRng, Rng};
use rand_distr::{Bernoulli, Beta, Distribution};
use serde::Serialize;
use std::collections::HashMap;
const EXPLORATION_TRIALS: usize = 30;
/// Actor for managing state
pub struct AppState {
/// a map to the history of flips for each participant
past: HashMap<String, Vec<(usize, bool)>>,
/// algorithm states
algorithm_state: AlgoState,
/// whether or not to print out log messages
verbose: bool,
}
impl AppState {
pub fn new(rng: ThreadRng, prob_heads: Vec<f64>, verbose: bool) -> AppState {
AppState {
past: HashMap::new(),
algorithm_state: AlgoState::new(
rng,
prob_heads
.iter()
.map(|p| Bernoulli::new(*p).unwrap())
.collect(),
),
verbose,
}
}
fn to_dump(&self) -> Dump {
Dump {
algorithms: self.algorithm_state.to_dump(),
players: self
.past
.iter()
.map(|e| (e.0.clone(), e.1.clone()))
.collect::<Vec<(String, Vec<(usize, bool)>)>>(),
}
}
}
/// the current state of the algorithms
struct AlgoState {
/// The state of naive
naive: NaiveAlgoState,
/// The past of Upper Condfidence Bound
ucb: UcbAlgoState,
/// The past of Thompson Sampling
thompson: ThompsonAlgoState,
/// The rng for the algorithms
rng: ThreadRng,
/// The arms / coins
arms: Vec<Bernoulli>,
}
impl AlgoState {
/// Create a new AlgoState
fn new(rng: ThreadRng, arms: Vec<Bernoulli>) -> AlgoState {
AlgoState {
naive: NaiveAlgoState::new(arms.len()),
ucb: UcbAlgoState::new(arms.len()),
thompson: ThompsonAlgoState::new(arms.len()),
rng,
arms,
}
}
/// Run every algorithm once
fn update(&mut self) {
self.naive.choose_flip(&mut self.rng, &self.arms);
self.ucb.choose_flip(&mut self.rng, &self.arms);
self.thompson.choose_flip(&mut self.rng, &self.arms);
}
/// Dump the state of the algorithms
fn to_dump(&self) -> Vec<(String, Vec<(usize, bool)>)> {
vec![
self.naive.to_dump(),
self.ucb.to_dump(),
self.thompson.to_dump(),
]
}
}
trait Algorithm {
fn new(num_arms: usize) -> Self;
fn choose_flip(&mut self, rng: &mut ThreadRng, arms: &[Bernoulli]);
fn flip(&mut self, rng: &mut ThreadRng, probs: &[Bernoulli], arm: usize);
fn to_dump(&self) -> (String, Vec<(usize, bool)>);
}
struct NaiveAlgoState {
/// The heads and tails seen during exploration period for each arm
stats: Vec<(u32, u32)>,
/// The past of naive: which coin it flipped and the result
past_flips: Vec<(usize, bool)>,
/// Best coin.
/// The best coin seen so far, evaluated once after the exploration phase has ended
best_coin: Option<usize>,
}
impl Algorithm for NaiveAlgoState {
fn new(num_arms: usize) -> NaiveAlgoState {
NaiveAlgoState {
stats: vec![(0, 0); num_arms],
past_flips: Vec::new(),
best_coin: None,
}
}
fn choose_flip(&mut self, rng: &mut ThreadRng, probs: &[Bernoulli]) {
if self.past_flips.len() < EXPLORATION_TRIALS {
// continue exploration phase
let arm = rng.gen_range(0..probs.len());
self.flip(rng, probs, arm);
} else if let Some(index) = self.best_coin {
// flip the best coin seen in exploration phase
self.flip(rng, probs, index);
} else {
// find the best coin seen in exploration phase and set its index
let index = self
.stats
.iter()
.enumerate()
.fold((0, 0.0), |a, index_heads_tails| {
let proportion = index_heads_tails.1 .0 as f64
/ (index_heads_tails.1 .0 as f64 + index_heads_tails.1 .1 as f64);
if proportion > a.1 {
(index_heads_tails.0, proportion)
} else {
a
}
})
.0;
self.best_coin = Some(index);
self.flip(rng, probs, index);
}
}
fn flip(&mut self, rng: &mut ThreadRng, probs: &[Bernoulli], arm: usize) {
let result = probs[arm].sample(rng);
if result {
self.stats[arm].0 += 1;
} else {
self.stats[arm].1 += 1;
}
self.past_flips.push((arm, result));
}
fn to_dump(&self) -> (String, Vec<(usize, bool)>) {
(
"Naive Strategy".to_string(),
self.past_flips
.iter()
.copied()
.collect::<Vec<(usize, bool)>>(),
)
}
}
struct UcbAlgoState {
/// The past of UCB
past_flips: Vec<(usize, bool)>,
/// Heads, tails seen for each arm (ucb without an exploration period)
arm_results: Vec<(u32, u32)>,
/// Total flips so far
total_flips: u32,
}
impl Algorithm for UcbAlgoState {
fn new(num_arms: usize) -> UcbAlgoState {
UcbAlgoState {
past_flips: Vec::new(),
arm_results: vec![(0, 0); num_arms],
total_flips: 0,
}
}
fn choose_flip(&mut self, rng: &mut ThreadRng, arms: &[Bernoulli]) {
let arm = self
.arm_results
.iter()
.enumerate()
.fold((0, 0.0), |a, index_heads_tails| {
let proportion = index_heads_tails.1 .0 as f64
/ (index_heads_tails.1 .0 as f64 + index_heads_tails.1 .1 as f64);
let confidence = proportion
+ f64::sqrt(
2.0 * f64::log(self.total_flips as f64, 10.0)
/ (index_heads_tails.1 .0 + index_heads_tails.1 .1) as f64,
);
if confidence > a.1 || confidence.is_nan() {
(index_heads_tails.0, confidence)
} else {
a
}
});
self.flip(rng, arms, arm.0);
}
fn flip(&mut self, rng: &mut ThreadRng, arms: &[Bernoulli], arm: usize) {
let result = arms[arm].sample(rng);
if result {
self.arm_results[arm].0 += 1;
} else {
self.arm_results[arm].1 += 1;
}
self.past_flips.push((arm, result));
self.total_flips += 1;
}
fn to_dump(&self) -> (String, Vec<(usize, bool)>) {
(
"UCB Strategy".to_string(),
self.past_flips
.iter()
.copied()
.collect::<Vec<(usize, bool)>>(),
)
}
}
struct ThompsonAlgoState {
/// The past of Thompson
past_flips: Vec<(usize, bool)>,
/// Heads, tails, and beta distribution (storing = less update) seen for each arm
arm_results: Vec<(u32, u32, Beta<f64>)>,
}
impl Algorithm for ThompsonAlgoState {
fn new(num_arms: usize) -> ThompsonAlgoState {
ThompsonAlgoState {
past_flips: Vec::new(),
arm_results: vec![(1, 1, Beta::new(1.0, 1.0).unwrap()); num_arms],
}
}
fn choose_flip(&mut self, rng: &mut ThreadRng, arms: &[Bernoulli]) {
// choose the arm with the highest sample from its beta distribution
let arm = self
.arm_results
.iter()
.enumerate()
.fold((0, 0.0), |a, (index, (_, _, beta))| {
let sample = beta.sample(rng);
if sample > a.1 {
(index, sample)
} else {
a
}
})
.0;
self.flip(rng, arms, arm);
}
fn flip(&mut self, rng: &mut ThreadRng, arms: &[Bernoulli], index: usize) {
let mut arm = &mut self.arm_results[index];
let result = arms[index].sample(rng);
if result {
arm.0 += 1;
} else {
arm.1 += 1;
}
arm.2 = Beta::new(arm.0 as f64, arm.1 as f64).unwrap();
self.past_flips.push((index, result));
}
fn to_dump(&self) -> (String, Vec<(usize, bool)>) {
(
"Thompson Strategy".to_string(),
self.past_flips
.iter()
.copied()
.collect::<Vec<(usize, bool)>>(),
)
}
}
impl Actor for AppState {
type Context = actix::Context<Self>;
}
#[derive(Serialize)]
pub struct Dump {
algorithms: Vec<(String, Vec<(usize, bool)>)>,
players: Vec<(String, Vec<(usize, bool)>)>,
}
#[derive(Message, Debug)]
#[rtype(result = "()")]
pub struct CoinFlipped {
pub user_id: String,
pub arm: usize,
pub result: bool,
}
#[derive(Message, Debug)]
#[rtype(result = "()")]
pub struct Flush {}
/// Register a new player \
/// Forwarded from App
#[derive(Message, Debug)]
#[rtype(i32)]
pub struct GetCount {
pub id: String,
}
/// Handler for CoinFlipped message.
impl Handler<CoinFlipped> for AppState {
type Result = ();
fn handle(&mut self, msg: CoinFlipped, _: &mut Context<Self>) -> Self::Result {
if self.verbose {
println!("{:?}", msg);
}
self.past
.entry(msg.user_id)
.or_insert_with(Vec::new)
.push((msg.arm, msg.result));
self.algorithm_state.update();
}
}
/// Handler for CoinFlipped message.
impl Handler<Flush> for AppState {
type Result = ();
fn handle(&mut self, msg: Flush, _: &mut Context<Self>) -> Self::Result {
if self.verbose {
println!("{:?}", msg);
}
let file = std::fs::File::create("dump.cbor").expect("Could not open output file");
serde_cbor::to_writer(file, &self.to_dump()).expect("Could not write to output file");
println!("Dumped to dump.cbor");
}
}
/// Handler for CoinFlipped message.
impl Handler<GetCount> for AppState {
type Result = i32;
fn handle(&mut self, msg: GetCount, _: &mut Context<Self>) -> Self::Result {
if self.verbose {
println!("{:?}", msg);
}
self.past
.get(&msg.id)
.map(|past| past.iter().fold(0, |a, e| if e.1 { a + 1 } else { a - 1 }))
.unwrap_or(0)
}
}