File size: 5,367 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use super::CheckFixedLengthSequence;
use super::FunnelConfig;
use super::WildcardChecker;
use loda_rust_core::util::BigIntVec;

/// Funnel of bloomfilters for use during mining.
///
/// Originally LODA-RUST could only mine for sequences that had 40 or more terms.
/// The OEIS 'stripped' file contains around 360k sequences of average length 38 terms.
/// So only half of the OEIS sequences got added to the bloomfilter.
/// Later, I added support for shorter than 40 terms sequences, 
/// by inserting a `WILDCARD_MAGIC_VALUE` into the bloomfilter.
/// 
/// A potential candiate program must fulfill these 4 stages:
/// - 10 terms
/// - 20 terms
/// - 30 terms
/// - 40 terms
///
/// The sooner a potential candidate program can be rejected,
/// the sooner it's possible to mutate the genome and try again.
///
/// If there is a sequence that has only 33 terms in the OEIS 'stripped' file, 
/// then the first 33 terms are followed by 7 wildcard terms (`WILDCARD_MAGIC_VALUE`).
/// This sequence can now be inserted into the bloomfilter.
///
/// When wildcard-checking if a sequence is contained in the bloomfilter, it works like this:
/// When a candiate program passes 10terms, 20terms, 30terms bloomfilters, and gets rejected by the 40terms bloomfilter.
/// Then replace the last term with a wildcard symbol, and try again.
/// Then replace the second last term with a wildcard symbol, and try again.
/// Then replace the third last term with a wildcard symbol, and try again.
/// After around 10 attempts it can be determined if it's in the bloomfilter or not.
#[derive(Clone, Debug)]
pub struct Funnel {
    checker10: CheckFixedLengthSequence,
    checker20: CheckFixedLengthSequence,
    checker30: CheckFixedLengthSequence,
    checker40: CheckFixedLengthSequence,

    metric_number_of_candidates_with_10terms: u64,
    metric_number_of_candidates_with_20terms: u64,
    metric_number_of_candidates_with_30terms: u64,
    metric_number_of_candidates_with_40terms: u64,
}

impl Funnel {
    pub fn new(
        checker10: CheckFixedLengthSequence, 
        checker20: CheckFixedLengthSequence,
        checker30: CheckFixedLengthSequence,
        checker40: CheckFixedLengthSequence,
    ) -> Self {
        Self {
            checker10: checker10, 
            checker20: checker20,
            checker30: checker30,
            checker40: checker40,
            metric_number_of_candidates_with_10terms: 0,
            metric_number_of_candidates_with_20terms: 0,
            metric_number_of_candidates_with_30terms: 0,
            metric_number_of_candidates_with_40terms: 0,
        }
    }

    pub fn check10(&mut self, terms: &BigIntVec) -> bool {
        if !self.checker10.check(terms) {
            return false;
        }
        self.metric_number_of_candidates_with_10terms += 1;
        true
    }

    #[allow(dead_code)]
    pub fn check20(&mut self, terms: &BigIntVec) -> bool {
        if !self.checker20.check(terms) {
            return false;
        }
        self.metric_number_of_candidates_with_20terms += 1;
        true
    }

    #[allow(dead_code)]
    pub fn check30(&mut self, terms: &BigIntVec) -> bool {
        if !self.checker30.check(terms) {
            return false;
        }
        self.metric_number_of_candidates_with_30terms += 1;
        true
    }

    #[allow(dead_code)]
    pub fn check40(&mut self, terms: &BigIntVec) -> bool {
        if !self.checker40.check(terms) {
            return false;
        }
        self.metric_number_of_candidates_with_40terms += 1;
        true
    }

    pub fn check20_with_wildcards(&mut self, terms: &BigIntVec) -> Option<usize> {
        let result: Option<usize> = self.checker20.check_with_wildcards(terms, FunnelConfig::MINIMUM_NUMBER_OF_REQUIRED_TERMS);
        if result.is_some() {
            self.metric_number_of_candidates_with_20terms += 1;
        }
        result
    }

    pub fn check30_with_wildcards(&mut self, terms: &BigIntVec) -> Option<usize> {
        let result: Option<usize> = self.checker30.check_with_wildcards(terms, FunnelConfig::MINIMUM_NUMBER_OF_REQUIRED_TERMS);
        if result.is_some() {
            self.metric_number_of_candidates_with_30terms += 1;
        }
        result
    }

    pub fn mut_check40_with_wildcards(&mut self, terms: &mut BigIntVec) -> Option<usize> {
        let result: Option<usize> = self.checker40.mut_check_with_wildcards(terms, FunnelConfig::MINIMUM_NUMBER_OF_REQUIRED_TERMS);
        if result.is_some() {
            self.metric_number_of_candidates_with_40terms += 1;
        }
        result
    }

    pub fn metric_number_of_candidates_with_10terms(&self) -> u64 {
        self.metric_number_of_candidates_with_10terms
    }

    pub fn metric_number_of_candidates_with_20terms(&self) -> u64 {
        self.metric_number_of_candidates_with_20terms
    }

    pub fn metric_number_of_candidates_with_30terms(&self) -> u64 {
        self.metric_number_of_candidates_with_30terms
    }

    pub fn metric_number_of_candidates_with_40terms(&self) -> u64 {
        self.metric_number_of_candidates_with_40terms
    }

    pub fn reset_metrics(&mut self) {
        self.metric_number_of_candidates_with_10terms = 0;
        self.metric_number_of_candidates_with_20terms = 0;
        self.metric_number_of_candidates_with_30terms = 0;
        self.metric_number_of_candidates_with_40terms = 0;
    }
}