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
use std::ops::AddAssign;
use num_traits::{Num, ToPrimitive};
use super::{Agent, Bandit, Counter, RecordCounter};
pub struct Game<'a, T: AddAssign + Num + ToPrimitive> {
agent: &'a mut dyn Agent<T>,
bandit: &'a dyn Bandit<T>,
wins: RecordCounter<u32>,
rewards: RecordCounter<T>,
}
impl<'a, T: AddAssign + Copy + Num + ToPrimitive> Game<'a, T> {
pub fn new(agent: &'a mut dyn Agent<T>, bandit: &'a dyn Bandit<T>) -> Game<'a, T> {
assert_eq!(agent.arms(), bandit.arms());
Game {
agent,
bandit,
wins: RecordCounter::new(),
rewards: RecordCounter::new(),
}
}
pub fn arms(&self) -> usize {
self.bandit.arms()
}
fn pull_arm(&mut self) {
let current_action = self.agent.action();
self.wins
.update((current_action == self.bandit.best_arm()) as u32);
let reward = self.bandit.reward(current_action);
self.rewards.update(reward);
self.agent.step(current_action, reward);
}
pub fn reset(&mut self, q_init: Vec<f64>) {
self.agent.reset(q_init);
self.rewards.reset();
self.wins.reset();
}
pub fn rewards(&self) -> &Vec<T> {
self.rewards.record()
}
pub fn run(&mut self, steps: u32) {
for _ in 1..=steps {
self.pull_arm()
}
}
pub fn wins(&self) -> &Vec<u32> {
self.wins.record()
}
}