This repository was archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy.rs
More file actions
75 lines (63 loc) · 2.38 KB
/
Copy pathpolicy.rs
File metadata and controls
75 lines (63 loc) · 2.38 KB
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
use std::cmp::Ordering;
use std::marker::PhantomData;
use rand::{Rng, thread_rng};
use amfiteatr_core::agent::{InformationSet, Policy};
use crate::domain::{ClassicAction, ClassicGameDomain, ClassicGameError, UsizeAgentId};
use crate::domain::ClassicAction::{Down, Up};
/// Classic pure strategy - allways one specified action from [`ClassicAction`].
pub struct ClassicPureStrategy<ID: UsizeAgentId, IS: InformationSet<ClassicGameDomain<ID>>>{
pub action: ClassicAction,
_is: PhantomData<IS>,
_id: PhantomData<ID>,
}
impl<ID: UsizeAgentId, IS: InformationSet<ClassicGameDomain<ID>>> ClassicPureStrategy<ID, IS>{
pub fn new(action: ClassicAction) -> Self{
Self{
action,
_is: Default::default(),
_id: Default::default()
}
}
}
impl<ID: UsizeAgentId, IS: InformationSet<ClassicGameDomain<ID>>> Policy<ClassicGameDomain<ID>> for ClassicPureStrategy<ID, IS>{
type InfoSetType = IS ;
fn select_action(&self, _state: &Self::InfoSetType) -> Option<ClassicAction> {
Some(self.action)
}
}
/// Selects action [`Up`] with given probability, otherwise [`Down`].
pub struct ClassicMixedStrategy<ID: UsizeAgentId, IS: InformationSet<ClassicGameDomain<ID>>>{
probability_up: f64,
_is: PhantomData<IS>,
_id: PhantomData<ID>,
}
impl<ID: UsizeAgentId, IS: InformationSet<ClassicGameDomain<ID>>> ClassicMixedStrategy<ID, IS>{
pub fn new(probability_up: f64) -> Self{
Self{
probability_up,
_is: Default::default(),
_id: Default::default(),
}
}
pub fn new_checked(probability: f64) -> Result<Self, ClassicGameError<ID>>{
if probability < 0.0 || probability > 1.0{
Err(ClassicGameError::NotAProbability(probability))
} else{
Ok(Self::new(probability))
}
}
}
impl<ID: UsizeAgentId, IS: InformationSet<ClassicGameDomain<ID>>> Policy<ClassicGameDomain<ID>> for ClassicMixedStrategy<ID, IS>{
type InfoSetType = IS ;
fn select_action(&self, _state: &Self::InfoSetType) -> Option<ClassicAction> {
let mut rng = thread_rng();
let sample = rng.gen_range(0.0..1.0);
sample.partial_cmp(&self.probability_up).and_then(|o|{
match o{
Ordering::Less => Some(Up),
Ordering::Equal => Some(Down),
Ordering::Greater => Some(Down),
}
})
}
}