~ruther/ctu-fee-eoa

ref: b0acf3341ffe9fa3b55366eb6dff6a5e1e9a63b7 ctu-fee-eoa/codes/eoa_lib/src/bounded.rs -rw-r--r-- 2.8 KiB
b0acf334 — Rutherther feat(plotter): add plotting of standard deviation a month ago
                                                                                
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
use nalgebra::{allocator::Allocator, DefaultAllocator, Dim, OVector, U1};
use rand::{Rng, RngCore};

use crate::binary_string::BinaryString;

pub trait Bounded<D> {
    type Item;

    fn is_in_bounds(&self, obj: &Self::Item) -> bool;
    fn bound(&self, obj: Self::Item) -> Self::Item;
    fn next_random(&self, size: D, rand: &mut dyn RngCore) -> Self::Item;
}

pub struct BoundedBinaryString<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D>
{
    min: Option<BinaryString<D>>,
    max: Option<BinaryString<D>>,
}

impl<D> BoundedBinaryString<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D>
{
    pub fn unbounded() -> Self {
        Self {
            min: None,
            max: None,
        }
    }
}

impl<D> Bounded<D> for BoundedBinaryString<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D>
{
    type Item = BinaryString<D>;

    fn is_in_bounds(&self, obj: &Self::Item) -> bool {
        if let Some(min) = &self.min {
            if obj < min {
                return false;
            }
        }

        if let Some(max) = &self.max {
            if obj > max {
                return false;
            }
        }

        return true;
    }

    fn bound(&self, obj: Self::Item) -> Self::Item {
        if let Some(min) = &self.min {
            if &obj < min {
                return min.clone();
            }
        }

        if let Some(max) = &self.max {
            if &obj > max {
                return max.clone();
            }
        }

        obj
    }

    fn next_random(&self, size: D, rand: &mut dyn RngCore) -> Self::Item {
        // TODO... this is very suboptimal, favoring the max, min.
        self.bound(
            BinaryString::from_ovector(
                OVector::<i8, D>::from_fn_generic(size, U1, |_, _| rand.random_range(0..=1))
            ))
    }
}

pub struct BoundedOVector<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D>
{
    min_max: OVector<(f64, f64), D>
}

impl<D> BoundedOVector<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D>
{
    pub fn new(min: OVector<f64, D>, max: OVector<f64, D>) -> Self {
        Self {
            min_max: min.zip_map(&max, |min, max| (min, max))
        }
    }
}

impl<D> Bounded<D> for BoundedOVector<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D>
{
    type Item = OVector<f64, D>;

    fn is_in_bounds(&self, obj: &Self::Item) -> bool {
        obj.iter()
            .zip(self.min_max.iter())
            .all(|(&c, &(min, max))| c <= max && c >= min)
    }

    fn bound(&self, mut obj: Self::Item) -> Self::Item {
        obj
            .zip_apply(&self.min_max, |c, (min, max)| *c = c.clamp(min, max));
        obj
    }

    fn next_random(&self, _: D, rand: &mut dyn RngCore) -> Self::Item {
        self.min_max.map(|(min, max)| rand.random_range(min..=max))
    }
}