fuzzy_logic_rs/
rules.rs

1#[derive(Debug)]
2pub enum Kind {
3    OR,
4    AND,
5}
6
7#[derive(Debug)]
8pub struct Rule {
9    relations: Vec<i32>,
10    weight: f64,
11    method: Kind,
12}
13
14impl Rule {
15    pub fn new_or(relations: Vec<i32>, weight: f64) -> Self {
16        assert!(weight <= 1.0, "Weight must be less or equal to 1.0");
17        assert!(weight >= 0.0, "Weight must be less or equal to 1.0");
18        Self {
19            relations,
20            weight,
21            method: Kind::OR,
22        }
23    }
24
25    pub fn new_and(relations: Vec<i32>, weight: f64) -> Self {
26        assert!(weight <= 1.0, "Weight must be less or equal to 1.0");
27        assert!(weight >= 0.0, "Weight must be less or equal to 1.0");
28        Self {
29            relations,
30            weight,
31            method: Kind::AND,
32        }
33    }
34    pub fn get_rules(&self) -> &[i32] {
35        &self.relations[..]
36    }
37
38    pub fn get_kind(&self) -> &Kind {
39        &self.method
40    }
41
42    pub fn get_weight(&self) -> f64 {
43        self.weight
44    }
45
46    pub fn get_input_rules(&self, input_size: usize) -> &[i32] {
47        &self.relations[..input_size]
48    }
49
50    pub fn get_output_rules(&self, input_size: usize) -> &[i32] {
51        &self.relations[input_size..]
52    }
53}
54
55#[derive(Debug)]
56pub enum OutputRelation {
57    Constant,
58    Linear,
59    Custom,
60}
61
62#[derive(Debug)]
63pub struct TSKRule {
64    input_relations: Vec<i32>,
65    output_relations: Vec<OutputRelation>,
66    weight: f64,
67    method: Kind,
68}
69
70impl TSKRule {
71    pub fn new_or(
72        input_relations: Vec<i32>,
73        output_relations: Vec<OutputRelation>,
74        weight: f64,
75    ) -> Self {
76        assert!(weight <= 1.0, "Weight must be less or equal to 1.0");
77        assert!(weight >= 0.0, "Weight must be less or equal to 1.0");
78        Self {
79            input_relations,
80            output_relations,
81            weight,
82            method: Kind::OR,
83        }
84    }
85
86    pub fn new_and(
87        input_relations: Vec<i32>,
88        output_relations: Vec<OutputRelation>,
89        weight: f64,
90    ) -> Self {
91        assert!(weight <= 1.0, "Weight must be less or equal to 1.0");
92        assert!(weight >= 0.0, "Weight must be less or equal to 1.0");
93        Self {
94            input_relations,
95            output_relations,
96            weight,
97            method: Kind::AND,
98        }
99    }
100
101    pub fn get_kind(&self) -> &Kind {
102        &self.method
103    }
104
105    pub fn get_weight(&self) -> f64 {
106        self.weight
107    }
108
109    pub fn get_input_rules(&self) -> &[i32] {
110        &self.input_relations
111    }
112
113    pub fn get_output_rules(&self) -> &[OutputRelation] {
114        &self.output_relations
115    }
116}