From c6f841ec4b44b2481b28f248957bf3a0503f14f5 Mon Sep 17 00:00:00 2001 From: Alexander Ignatov Date: Thu, 30 Dec 2021 23:05:03 +0200 Subject: [PATCH] Add LinguisticVariable and new method --- Sources/FuzzyLogic/LinguisticVariable.swift | 39 +++++++++++++++++++ Sources/FuzzySets/FuzzySetOperations.swift | 2 + .../Implementations/AnyFuzzySet.swift | 6 ++- .../DiscreteMutableFuzzySet.swift | 6 +++ .../Implementations/IterableFuzzySet.swift | 6 +++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 Sources/FuzzyLogic/LinguisticVariable.swift diff --git a/Sources/FuzzyLogic/LinguisticVariable.swift b/Sources/FuzzyLogic/LinguisticVariable.swift new file mode 100644 index 0000000..3dd4a19 --- /dev/null +++ b/Sources/FuzzyLogic/LinguisticVariable.swift @@ -0,0 +1,39 @@ +import FuzzySets + +public struct LinguisticVariable { + + public typealias TermModifier = (Grade) -> Grade + + let baseTerms: [TermName: Term] + let termModifiers: [TermModifierName: TermModifier] + + public init( + baseTerms: [TermName: Term], + termModifiers: [TermModifierName: TermModifier] = [:] + ) { + self.baseTerms = baseTerms + self.termModifiers = termModifiers + } + + public func term(_ name: TermName) -> Term? { + baseTerms[name] + } + + public subscript(_ termName: TermName) -> Term? { + term(termName) + } + + public subscript(_ termName: TermName, applyModifier modifierName: TermModifierName) -> Term? { + applyModifier(modifierName, to: termName) + } + + public func applyModifier(_ modifierName: TermModifierName, to termName: TermName) -> Term? { + guard let term = term(termName) else { return nil } + return applyModifier(modifierName, to: term) + } + + public func applyModifier(_ modifierName: TermModifierName, to term: Term) -> Term? { + guard let modifier = termModifiers[modifierName] else { return nil } + return term.appliedCustomFunction(modifier) + } +} diff --git a/Sources/FuzzySets/FuzzySetOperations.swift b/Sources/FuzzySets/FuzzySetOperations.swift index e0c38ba..2658599 100644 --- a/Sources/FuzzySets/FuzzySetOperations.swift +++ b/Sources/FuzzySets/FuzzySetOperations.swift @@ -13,4 +13,6 @@ public protocol FuzzySetOperations: FuzzySet { func symmetricDifference(_ other: Self, method: SymmetricDifferenceFunction) -> Self func power(_ n: Double) -> Self + + func appliedCustomFunction(_ function: @escaping (Grade) -> Grade) -> Self } diff --git a/Sources/FuzzySets/Implementations/AnyFuzzySet.swift b/Sources/FuzzySets/Implementations/AnyFuzzySet.swift index 05d20cf..1c8be41 100644 --- a/Sources/FuzzySets/Implementations/AnyFuzzySet.swift +++ b/Sources/FuzzySets/Implementations/AnyFuzzySet.swift @@ -48,7 +48,11 @@ extension AnyFuzzySet: FuzzySetOperations { } public func power(_ n: Double) -> Self { - return .init { Double.pow(membershipFunction($0), n) } + .init { Double.pow(membershipFunction($0), n) } + } + + public func appliedCustomFunction(_ function: @escaping (Grade) -> Grade) -> Self { + .init { function(membershipFunction($0)) } } } diff --git a/Sources/FuzzySets/Implementations/DiscreteMutableFuzzySet.swift b/Sources/FuzzySets/Implementations/DiscreteMutableFuzzySet.swift index 334ecce..c72755e 100644 --- a/Sources/FuzzySets/Implementations/DiscreteMutableFuzzySet.swift +++ b/Sources/FuzzySets/Implementations/DiscreteMutableFuzzySet.swift @@ -62,6 +62,12 @@ extension DiscreteMutableFuzzySet: FuzzySetOperations { result.applyPower(n) return result } + + public func appliedCustomFunction(_ function: @escaping (Grade) -> Grade) -> Self { + var result = self + result.applyFunction(function) + return result + } } // MARK: - Mutability diff --git a/Sources/FuzzySets/Implementations/IterableFuzzySet.swift b/Sources/FuzzySets/Implementations/IterableFuzzySet.swift index 46aeebb..ebc84ac 100644 --- a/Sources/FuzzySets/Implementations/IterableFuzzySet.swift +++ b/Sources/FuzzySets/Implementations/IterableFuzzySet.swift @@ -91,6 +91,12 @@ extension IterableFuzzySet: FuzzySetOperations { Double.pow(function($0), n) } } + + public func appliedCustomFunction(_ function: @escaping (Grade) -> Grade) -> IterableFuzzySet { + .init(sequence) { + function(self.function($0)) + } + } } // MARK: - Sequence