Skip to content

Commit d8c81e8

Browse files
committed
Add FuzzyRule and custom operator
1 parent f7dcd8b commit d8c81e8

File tree

4 files changed

+53
-36
lines changed

4 files changed

+53
-36
lines changed

Sources/FuzzyLogic/BinaryFuzzyRelation+implication.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@ public extension BinaryFuzzyRelation {
88
method: ImplicationMethod = .mamdani
99
) -> Self where P.Universe == U, Q.Universe == V {
1010
.init {
11-
method.function(antecedent[$0.0], consequent[$0.1])
11+
(antecedent --> consequent)
12+
.apply($0.0, $0.1, method: method)
13+
}
14+
}
15+
16+
static func implication<P: FuzzySet, Q: FuzzySet>(
17+
rule: FuzzyRule<P, Q>,
18+
method: ImplicationMethod = .mamdani
19+
) -> Self where P.Universe == U, Q.Universe == V {
20+
.init {
21+
rule.apply($0.0, $0.1, method: method)
1222
}
1323
}
1424
}

Sources/FuzzyLogic/FuzzyRule.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import FuzzySets
2+
3+
public struct FuzzyRule<P: FuzzySet, Q: FuzzySet> {
4+
public let antecedent: P
5+
public let consequent: Q
6+
7+
public init(antecedent: P, consequent: Q) {
8+
self.antecedent = antecedent
9+
self.consequent = consequent
10+
}
11+
12+
public func apply(
13+
_ p: P.Universe,
14+
_ q: Q.Universe,
15+
method: ImplicationMethod = .mamdani
16+
) -> Grade {
17+
method.function(antecedent[p], consequent[q])
18+
}
19+
}
20+
21+
infix operator -->: TernaryPrecedence // lower than ==, &&, ||, etc
22+
public func --> <P: FuzzySet, Q: FuzzySet> (lhs: P, rhs: Q) -> FuzzyRule<P, Q> {
23+
.init(antecedent: lhs, consequent: rhs)
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import FuzzySets
2+
import FuzzyRelations
3+
4+
public extension FuzzySetComposition {
5+
static func generalizedModusPonens<P: FuzzySet, Q: FuzzySet>(
6+
premise: FuzzyRule<P, Q>,
7+
fact: IterableFuzzySet<U, Seq>,
8+
method: ImplicationMethod = .mamdani
9+
) -> Self where P.Universe == U, Q.Universe == V {
10+
.init(
11+
set: fact,
12+
relation: .implication(
13+
rule: premise,
14+
method: method
15+
)
16+
)
17+
}
18+
}

Sources/FuzzyLogic/GeneralizedModusPonens.swift

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)