From 186d94ff1744db28f1ccf96a820e88a875ac22df Mon Sep 17 00:00:00 2001 From: Alexander Ignatov Date: Thu, 16 Dec 2021 12:26:50 +0200 Subject: [PATCH] Add membership function and continuous set basic defs --- Sources/FuzzyKit/ContinuousFuzzySet.swift | 27 +++++++++++++++++ Sources/FuzzyKit/MembershipFunction.swift | 29 +++++++++++++++++++ .../ContinuousFuzzySetTests.swift | 6 ++++ .../MembershipFunctionTests.swift | 13 +++++++++ 4 files changed, 75 insertions(+) create mode 100644 Sources/FuzzyKit/ContinuousFuzzySet.swift create mode 100644 Sources/FuzzyKit/MembershipFunction.swift create mode 100644 Tests/FuzzyKitTests/ContinuousFuzzySetTests.swift create mode 100644 Tests/FuzzyKitTests/MembershipFunctionTests.swift diff --git a/Sources/FuzzyKit/ContinuousFuzzySet.swift b/Sources/FuzzyKit/ContinuousFuzzySet.swift new file mode 100644 index 0000000..e310bf6 --- /dev/null +++ b/Sources/FuzzyKit/ContinuousFuzzySet.swift @@ -0,0 +1,27 @@ +public struct ContinuousFuzzySet: FuzzySet { + public typealias Element = Double + + private let membershipFunction: MembershipFunction + + public init(membershipFunction: MembershipFunction) { + self.membershipFunction = membershipFunction + } + + public init(membershipFunction: @escaping (Element) -> Grade) { + self.membershipFunction = MembershipFunction(membershipFunction) + } + + public func grade(forElement element: Element) -> Grade { + membershipFunction(element) + } +} + +public extension ContinuousFuzzySet { + static var empty: Self { + .init(membershipFunction: .zero) + } + + static var universe: Self { + .init(membershipFunction: .one) + } +} diff --git a/Sources/FuzzyKit/MembershipFunction.swift b/Sources/FuzzyKit/MembershipFunction.swift new file mode 100644 index 0000000..3dcc8a5 --- /dev/null +++ b/Sources/FuzzyKit/MembershipFunction.swift @@ -0,0 +1,29 @@ +public struct MembershipFunction { + + private let function: (Double) -> Double + + public init(_ function: @escaping (Double) -> Grade) { + self.function = function + } + + public func callAsFunction(_ x: Double) -> Double { + function(x) + } +} + +/// Common membership functions +public extension MembershipFunction { + static var zero: Self { + .init { _ in 0.0 } + } + + static var one: Self { + .init { _ in 1.0 } + } + + static func fuzzySingleton(_ a: Double) -> Self { + .init { $0 == a ? 1.0 : 0.0 } + } + + // TODO: Add more +} diff --git a/Tests/FuzzyKitTests/ContinuousFuzzySetTests.swift b/Tests/FuzzyKitTests/ContinuousFuzzySetTests.swift new file mode 100644 index 0000000..bf375e0 --- /dev/null +++ b/Tests/FuzzyKitTests/ContinuousFuzzySetTests.swift @@ -0,0 +1,6 @@ +import XCTest +import FuzzyKit + +final class ContinuousFuzzySetTests: XCTestCase { + // TODO +} diff --git a/Tests/FuzzyKitTests/MembershipFunctionTests.swift b/Tests/FuzzyKitTests/MembershipFunctionTests.swift new file mode 100644 index 0000000..9dda954 --- /dev/null +++ b/Tests/FuzzyKitTests/MembershipFunctionTests.swift @@ -0,0 +1,13 @@ +import XCTest +import FuzzyKit + +final class MembershipFunctionTests: XCTestCase { + func test_isCallable() throws { + let sut = MembershipFunction.one + let x = 1.0 + + let result = sut(x) + + XCTAssertEqual(result, x) + } +}