diff --git a/Sources/FuzzyLogic/BinaryFuzzyRelation+implication.swift b/Sources/FuzzyLogic/BinaryFuzzyRelation+implication.swift index 9cf5254..67aef78 100644 --- a/Sources/FuzzyLogic/BinaryFuzzyRelation+implication.swift +++ b/Sources/FuzzyLogic/BinaryFuzzyRelation+implication.swift @@ -8,7 +8,17 @@ public extension BinaryFuzzyRelation { method: ImplicationMethod = .mamdani ) -> Self where P.Universe == U, Q.Universe == V { .init { - method.function(antecedent[$0.0], consequent[$0.1]) + (antecedent --> consequent) + .apply($0.0, $0.1, method: method) + } + } + + static func implication( + rule: FuzzyRule, + method: ImplicationMethod = .mamdani + ) -> Self where P.Universe == U, Q.Universe == V { + .init { + rule.apply($0.0, $0.1, method: method) } } } diff --git a/Sources/FuzzyLogic/FuzzyRule.swift b/Sources/FuzzyLogic/FuzzyRule.swift new file mode 100644 index 0000000..1c38c8a --- /dev/null +++ b/Sources/FuzzyLogic/FuzzyRule.swift @@ -0,0 +1,24 @@ +import FuzzySets + +public struct FuzzyRule { + public let antecedent: P + public let consequent: Q + + public init(antecedent: P, consequent: Q) { + self.antecedent = antecedent + self.consequent = consequent + } + + public func apply( + _ p: P.Universe, + _ q: Q.Universe, + method: ImplicationMethod = .mamdani + ) -> Grade { + method.function(antecedent[p], consequent[q]) + } +} + +infix operator -->: TernaryPrecedence // lower than ==, &&, ||, etc +public func --> (lhs: P, rhs: Q) -> FuzzyRule { + .init(antecedent: lhs, consequent: rhs) +} diff --git a/Sources/FuzzyLogic/FuzzySetComposition+generalizedModusPonens.swift b/Sources/FuzzyLogic/FuzzySetComposition+generalizedModusPonens.swift new file mode 100644 index 0000000..63ca9bc --- /dev/null +++ b/Sources/FuzzyLogic/FuzzySetComposition+generalizedModusPonens.swift @@ -0,0 +1,18 @@ +import FuzzySets +import FuzzyRelations + +public extension FuzzySetComposition { + static func generalizedModusPonens( + premise: FuzzyRule, + fact: IterableFuzzySet, + method: ImplicationMethod = .mamdani + ) -> Self where P.Universe == U, Q.Universe == V { + .init( + set: fact, + relation: .implication( + rule: premise, + method: method + ) + ) + } +} diff --git a/Sources/FuzzyLogic/GeneralizedModusPonens.swift b/Sources/FuzzyLogic/GeneralizedModusPonens.swift deleted file mode 100644 index 973eff6..0000000 --- a/Sources/FuzzyLogic/GeneralizedModusPonens.swift +++ /dev/null @@ -1,35 +0,0 @@ -import FuzzySets -import FuzzyRelations - -public struct GeneralizedModusPonens where Seq.Element == U { - - let composition: FuzzySetComposition - - public init( - ruleAntecedent: P, - ruleConsequent: Q, - fact: IterableFuzzySet, - method: ImplicationMethod = .mamdani - ) where P.Universe == U, Q.Universe == V { - self.composition = .init( - set: fact, - relation: .implication( - antecedent: ruleAntecedent, - consequent: ruleConsequent, - method: method - ) - ) - } -} - -extension GeneralizedModusPonens: FuzzySet { - public func grade(forElement element: V) -> Grade { - composition[element] - } -} - -extension GeneralizedModusPonens: AnyFuzzySetRepresentable { - public func eraseToAnyFuzzySet() -> AnyFuzzySet { - .init { composition[$0] } - } -}