Extract 2 more abstractions and fix discrete set def val

This commit is contained in:
Alexander Ignatov 2021-12-28 12:43:47 +02:00
parent 968df3b5de
commit 37faa1e3e2
12 changed files with 167 additions and 100 deletions

View File

@ -24,16 +24,20 @@ where A.Universe == U, B.Universe == V {
self.second = b
self.function = .init(membershipFunction)
}
public func grade(forElements elements: (U, V)) -> Grade {
function(elements)
}
extension BinaryCartesianProduct: FuzzySet {
public func grade(forElement element: (U, V)) -> Grade {
function(element)
}
public subscript(_ u: U, _ v: V) -> Grade {
grade(forElements: (u, v))
grade(forElement: (u, v))
}
public func asFuzzySet() -> AnyFuzzySet<(U, V)> {
}
extension BinaryCartesianProduct: AnyFuzzySetRepresentable {
public func eraseToAnyFuzzySet() -> AnyFuzzySet<(U, V)> {
.init(membershipFunction: function)
}
}

View File

@ -11,20 +11,20 @@ public class BinaryFuzzyRelation<U, V> {
public init(_ membershipFunction: @escaping MembershipFunction<(U, V)>.FunctionType) {
self.function = .init(membershipFunction)
}
public func grade(forElements elements: (U, V)) -> Grade {
}
extension BinaryFuzzyRelation: FuzzySet {
public func grade(forElement elements: (U, V)) -> Grade {
function(elements)
}
public subscript(_ u: U, _ v: V) -> Grade {
grade(forElements: (u, v))
grade(forElement: (u, v))
}
public subscript(_ x: (U, V)) -> Grade {
grade(forElements: x)
}
public func asFuzzySet() -> AnyFuzzySet<(U, V)> {
}
extension BinaryFuzzyRelation: AnyFuzzySetRepresentable {
public func eraseToAnyFuzzySet() -> AnyFuzzySet<(U, V)> {
.init(membershipFunction: function)
}
}

View File

@ -11,20 +11,20 @@ class HomogenousFuzzyRelation<U> {
public init(_ membershipFunction: @escaping MembershipFunction<[U]>.FunctionType) {
self.function = .init(membershipFunction)
}
public func grade(forElements elements: [U]) -> Grade {
function(elements)
}
extension HomogenousFuzzyRelation: FuzzySet {
public func grade(forElement element: [U]) -> Grade {
function(element)
}
public subscript(_ u: U...) -> Grade {
grade(forElements: u)
grade(forElement: u)
}
public subscript(_ u: [U]) -> Grade {
grade(forElements: u)
}
public func asFuzzySet() -> AnyFuzzySet<[U]> {
}
extension HomogenousFuzzyRelation: AnyFuzzySetRepresentable {
public func eraseToAnyFuzzySet() -> AnyFuzzySet<[U]> {
.init(membershipFunction: function)
}
}

View File

@ -11,20 +11,20 @@ public class TernaryFuzzyRelation<U, V, W> {
public init(_ membershipFunction: @escaping MembershipFunction<(U, V, W)>.FunctionType) {
self.function = .init(membershipFunction)
}
public func grade(forElements elements: (U, V, W)) -> Grade {
function(elements)
}
extension TernaryFuzzyRelation: FuzzySet {
public func grade(forElement element: (U, V, W)) -> Grade {
function(element)
}
public subscript(_ u: U, _ v: V, _ w: W) -> Grade {
grade(forElements: (u, v, w))
grade(forElement: (u, v, w))
}
public subscript(_ x: (U, V, W)) -> Grade {
grade(forElements: x)
}
public func asFuzzySet() -> AnyFuzzySet<(U, V, W)> {
}
extension TernaryFuzzyRelation: AnyFuzzySetRepresentable {
public func eraseToAnyFuzzySet() -> AnyFuzzySet<(U, V, W)> {
.init(membershipFunction: function)
}
}

View File

@ -7,20 +7,6 @@ public protocol FuzzySet {
func grade(forElement element: Universe) -> Grade
subscript(_ element: Universe) -> Grade { get }
func alphaCut(_ alpha: Grade) -> Self
func complement(method: ComplementFunction) -> Self
func intersection(_ other: Self, method: TNormFunction) -> Self
func union(_ other: Self, method: SNormFunction) -> Self
func difference(_ other: Self, method: DifferenceFunction) -> Self
func symmetricDifference(_ other: Self, method: SymmetricDifferenceFunction) -> Self
func power(_ n: Double) -> Self
}
public extension FuzzySet {

View File

@ -0,0 +1,16 @@
public protocol FuzzySetOperations: FuzzySet {
func alphaCut(_ alpha: Grade) -> Self
func complement(method: ComplementFunction) -> Self
func intersection(_ other: Self, method: TNormFunction) -> Self
func union(_ other: Self, method: SNormFunction) -> Self
func difference(_ other: Self, method: DifferenceFunction) -> Self
func symmetricDifference(_ other: Self, method: SymmetricDifferenceFunction) -> Self
func power(_ n: Double) -> Self
}

View File

@ -1,6 +1,6 @@
import RealModule
public struct AnyFuzzySet<Universe>: FuzzySet {
public struct AnyFuzzySet<Universe> {
internal let membershipFunction: MembershipFunction<Universe>
public init(membershipFunction: MembershipFunction<Universe>) {
@ -10,11 +10,19 @@ public struct AnyFuzzySet<Universe>: FuzzySet {
public init(membershipFunction: @escaping MembershipFunction<Universe>.FunctionType) {
self.membershipFunction = MembershipFunction(membershipFunction)
}
}
// MARK: - Fuzzy set
extension AnyFuzzySet: FuzzySet {
public func grade(forElement element: Universe) -> Grade {
membershipFunction(element)
}
}
// MARK: - Fuzzy set operations
extension AnyFuzzySet: FuzzySetOperations {
public func alphaCut(_ alpha: Grade) -> Self {
.init { max(membershipFunction($0), alpha) }
}
@ -56,16 +64,34 @@ public extension AnyFuzzySet {
}
}
// MARK: - From crisp set
public extension AnyFuzzySet where Universe: Hashable {
static func fromCrispSet(_ set: Set<Universe>) -> Self {
.init(membershipFunction: .fromCrispSet(set))
}
}
// MARK: - Type erasure
public extension DiscreteMutableFuzzySet {
func eraseToAnyFuzzySet() -> AnyFuzzySet<Universe> {
public protocol AnyFuzzySetRepresentable: FuzzySet {
func eraseToAnyFuzzySet() -> AnyFuzzySet<Universe>
}
extension AnyFuzzySet {
public init<FS: AnyFuzzySetRepresentable>(_ fuzzySet: FS) where FS.Universe == Universe {
self = fuzzySet.eraseToAnyFuzzySet()
}
}
extension DiscreteMutableFuzzySet: AnyFuzzySetRepresentable {
public func eraseToAnyFuzzySet() -> AnyFuzzySet<Universe> {
.init(membershipFunction: .fromDictionary(grades))
}
}
public extension IterableFuzzySet {
func eraseToAnyFuzzySet() -> AnyFuzzySet<Universe> {
extension IterableFuzzySet: AnyFuzzySetRepresentable {
public func eraseToAnyFuzzySet() -> AnyFuzzySet<Universe> {
.init(membershipFunction: function)
}
}

View File

@ -1,58 +1,64 @@
public struct DiscreteMutableFuzzySet<Universe: Hashable> {
public private(set) var grades: [Universe: Grade]
public private(set) var defaultGrade: Grade
public init(elementToGradeMap: [Universe: Grade] = [:]) {
public init(_ elementToGradeMap: [Universe: Grade] = [:], defaultGrade: Grade = 0) {
self.grades = elementToGradeMap
self.defaultGrade = defaultGrade
}
}
// MARK: - FuzzySet conformance
// MARK: - Fuzzy set
extension DiscreteMutableFuzzySet: FuzzySet {
/// - Complexity: O(1)
public func grade(forElement element: Universe) -> Grade {
grades[element] ?? 0
grades[element] ?? defaultGrade
}
}
// MARK: - Fuzzy set operations
extension DiscreteMutableFuzzySet: FuzzySetOperations {
public func alphaCut(_ alpha: Grade) -> Self {
var result = Self(elementToGradeMap: grades)
var result = Self(grades, defaultGrade: defaultGrade)
result.applyAlphaCut(alpha)
return result
}
public func complement(method: ComplementFunction = .standard) -> Self {
var result = Self(elementToGradeMap: grades)
var result = Self(grades, defaultGrade: defaultGrade)
result.formComplement(method: method)
return result
}
public func intersection(_ other: Self, method: TNormFunction = .minimum) -> Self {
var result = Self(elementToGradeMap: grades)
var result = Self(grades, defaultGrade: defaultGrade)
result.formIntersection(other, method: method)
return result
}
public func union(_ other: Self, method: SNormFunction) -> Self {
var result = Self(elementToGradeMap: grades)
var result = Self(grades, defaultGrade: defaultGrade)
result.formUnion(other, method: method)
return result
}
public func difference(_ other: Self, method: DifferenceFunction = .tNormAndComplement(.minimum, .standard)) -> Self {
var result = Self(elementToGradeMap: grades)
var result = Self(grades, defaultGrade: defaultGrade)
result.formDifference(other, method: method)
return result
}
public func symmetricDifference(_ other: Self, method: SymmetricDifferenceFunction = .absoluteValue) -> Self {
var result = Self(elementToGradeMap: grades)
var result = Self(grades, defaultGrade: defaultGrade)
result.formSymmetricDifference(other, method: method)
return result
}
public func power(_ n: Double) -> Self {
var result = Self(elementToGradeMap: grades)
var result = Self(grades, defaultGrade: defaultGrade)
result.applyPower(n)
return result
}
@ -63,6 +69,7 @@ extension DiscreteMutableFuzzySet: FuzzySet {
public extension DiscreteMutableFuzzySet {
/// - Complexity: O(1)
mutating func setGrade(_ grade: Grade, forElement element: Universe) {
guard grade != defaultGrade else { return }
grades[element] = grade
}
@ -84,19 +91,19 @@ public extension DiscreteMutableFuzzySet {
}
mutating func formIntersection(_ other: Self, method: TNormFunction = .minimum) {
applyFunction(method.function, whenMergingWith: other.grades)
applyFunction(method.function, whenMergingWith: other.grades, otherDefaultGrade: other.defaultGrade)
}
mutating func formUnion(_ other: Self, method: SNormFunction = .maximum) {
applyFunction(method.function, whenMergingWith: other.grades)
applyFunction(method.function, whenMergingWith: other.grades, otherDefaultGrade: other.defaultGrade)
}
mutating func formDifference(_ other: Self, method: DifferenceFunction = .tNormAndComplement(.minimum, .standard)) {
applyFunction(method.function, whenMergingWith: other.grades)
applyFunction(method.function, whenMergingWith: other.grades, otherDefaultGrade: other.defaultGrade)
}
mutating func formSymmetricDifference(_ other: Self, method: SymmetricDifferenceFunction = .absoluteValue) {
applyFunction(method.function, whenMergingWith: other.grades)
applyFunction(method.function, whenMergingWith: other.grades, otherDefaultGrade: other.defaultGrade)
}
mutating func applyPower(_ n: Double) {
@ -108,6 +115,8 @@ public extension DiscreteMutableFuzzySet {
public extension DiscreteMutableFuzzySet {
var support: Set<Universe> {
assert(defaultGrade == 0, "Cannot compute core of DiscreteMutableFuzzySet with defaultGrade > 0")
let elements = grades
.filter { $0.value > 0 }
.map { $0.key }
@ -115,6 +124,8 @@ public extension DiscreteMutableFuzzySet {
}
var core: Set<Universe> {
assert(defaultGrade != 1, "Cannot compute core of DiscreteMutableFuzzySet with defaultGrade==1")
let elements = grades
.filter { $0.value == 1 }
.map { $0.key }
@ -122,26 +133,25 @@ public extension DiscreteMutableFuzzySet {
}
var height: Grade {
grades.values.max() ?? 0
guard let maximum = grades.values.max() else {
return defaultGrade
}
return max(defaultGrade, maximum)
}
var isNormal: Bool {
grades.values.contains { $0 == 1 }
guard defaultGrade != 1 else { return true }
return grades.values.contains { $0 == 1 }
}
}
// MARK: - From crisp set
public extension DiscreteMutableFuzzySet {
static func fromCrispSet(_ set: Set<Universe>) -> Self {
let gradeTuples = set.map { ($0, 1.0 ) }
let gradeDictionary = Dictionary(uniqueKeysWithValues: gradeTuples)
return .init(elementToGradeMap: gradeDictionary)
}
}
public extension Set {
func fuzzified() -> DiscreteMutableFuzzySet<Set.Element> {
DiscreteMutableFuzzySet.fromCrispSet(self)
return .init(gradeDictionary)
}
}
@ -152,7 +162,7 @@ extension DiscreteMutableFuzzySet: CustomStringConvertible {
let guts = grades.map {
"\($0.value)/\($0.key)"
}.joined(separator: ", ")
return "{\(guts)}"
return "<FuzzySet: {\(guts)}, other values == \(defaultGrade)>"
}
}
@ -165,9 +175,23 @@ private extension DiscreteMutableFuzzySet {
}
let newMap = Dictionary(uniqueKeysWithValues: newGradeTuples)
grades = newMap
defaultGrade = function(defaultGrade)
sanitize()
}
mutating func applyFunction(_ function: (Grade, Grade) -> Grade, whenMergingWith anotherDictionary: [Universe: Grade]) {
mutating func applyFunction(
_ function: (Grade, Grade) -> Grade,
whenMergingWith anotherDictionary: [Universe: Grade],
otherDefaultGrade: Grade
) {
grades.merge(anotherDictionary, uniquingKeysWith: function)
defaultGrade = function(defaultGrade, otherDefaultGrade)
sanitize()
}
mutating func sanitize() {
grades = grades.filter {
$0.value != defaultGrade
}
}
}

View File

@ -26,12 +26,17 @@ public struct IterableFuzzySet<Universe, S: Sequence> where S.Element == Univers
}
}
// MARK: - FuzzySet conformance
// MARK: - Fuzzy set
extension IterableFuzzySet: FuzzySet {
public func grade(forElement element: Universe) -> Grade {
function(element)
}
}
// MARK: - Fuzzy set operations
extension IterableFuzzySet: FuzzySetOperations {
public func alphaCut(_ alpha: Grade) -> Self {
.init(sequence) {
@ -76,7 +81,7 @@ extension IterableFuzzySet: FuzzySet {
}
}
// MARK: - Sequence conformance
// MARK: - Sequence
extension IterableFuzzySet: Sequence {
public func makeIterator() -> Iterator {
@ -117,6 +122,14 @@ public extension IterableFuzzySet where Universe: Hashable {
}
}
// MARK: - From crisp set
public extension IterableFuzzySet where S == Set<Universe> {
static func fromCrispSet(_ set: S) -> Self {
.init(set, membershipFunction: .one)
}
}
// MARK: - Utility
extension IterableFuzzySet.Element: Equatable where Universe: Equatable {}

View File

@ -4,12 +4,12 @@ import FuzzySets
final class FuzzyRelationsTests: XCTestCase {
func test_cartesianProduct_discreteSets() {
let fs1 = DiscreteMutableFuzzySet(elementToGradeMap: [
let fs1 = DiscreteMutableFuzzySet([
"a1": 1,
"a2": 0.6,
"a3": 0.3,
])
let fs2 = DiscreteMutableFuzzySet(elementToGradeMap: [
let fs2 = DiscreteMutableFuzzySet([
"b1": 0.6,
"b2": 0.9,
"b3": 0.1,
@ -41,7 +41,7 @@ final class FuzzyRelationsTests: XCTestCase {
for (elements, grade) in zip(expectedTuples, expectedGrades) {
XCTAssertApproximatelyEqual(grade, sut[elements.0, elements.1])
XCTAssertApproximatelyEqual(grade, sut.grade(forElements: elements))
XCTAssertApproximatelyEqual(grade, sut.grade(forElement: elements))
}
}
@ -80,7 +80,7 @@ final class FuzzyRelationsTests: XCTestCase {
for (elements, grade) in zip(expectedTuples, expectedGrades) {
XCTAssertApproximatelyEqual(grade, sut[elements.0, elements.1])
XCTAssertApproximatelyEqual(grade, sut.grade(forElements: elements))
XCTAssertApproximatelyEqual(grade, sut.grade(forElement: elements))
}
}
}

View File

@ -12,7 +12,7 @@ final class AnyFuzzySetTests: XCTestCase {
"f": 0.0,
]
let set = DiscreteMutableFuzzySet(elementToGradeMap: expected)
let set = DiscreteMutableFuzzySet(expected)
let sut = set.eraseToAnyFuzzySet()

View File

@ -22,7 +22,7 @@ final class DiscreteMutableFuzzySetTests: XCTestCase {
"c": 1.0,
]
let sut = DiscreteMutableFuzzySet(elementToGradeMap: parameters)
let sut = DiscreteMutableFuzzySet(parameters)
let expected = [
"a": 0.69,
@ -40,9 +40,9 @@ final class DiscreteMutableFuzzySetTests: XCTestCase {
"a": 0.69,
"b": 1,
]
let sut = DiscreteMutableFuzzySet(elementToGradeMap: parameters)
let expected1 = "{0.69/a, 1.0/b}"
let expected2 = "{1.0/b, 0.69/a}"
let sut = DiscreteMutableFuzzySet(parameters)
let expected1 = "<FuzzySet: {0.69/a, 1.0/b}, other values == 0.0>"
let expected2 = "<FuzzySet: {1.0/b, 0.69/a}, other values == 0.0>"
let result = String(describing: sut)
@ -86,12 +86,10 @@ final class DiscreteMutableFuzzySetTests: XCTestCase {
"": 0.0,
]
let fs1 = cs.fuzzified()
let fs2 = DiscreteMutableFuzzySet.fromCrispSet(cs)
let sut = DiscreteMutableFuzzySet.fromCrispSet(cs)
for (element, grade) in expected {
assertExpectedGrade(element: element, expectedGrade: grade, sut: fs1)
assertExpectedGrade(element: element, expectedGrade: grade, sut: fs2)
assertExpectedGrade(element: element, expectedGrade: grade, sut: sut)
}
}
@ -114,8 +112,8 @@ final class DiscreteMutableFuzzySetTests: XCTestCase {
"f": 0.5,
]
let set = DiscreteMutableFuzzySet(elementToGradeMap: initial)
var sut2 = DiscreteMutableFuzzySet(elementToGradeMap: initial)
let set = DiscreteMutableFuzzySet(initial)
var sut2 = DiscreteMutableFuzzySet(initial)
let sut1 = set.alphaCut(alpha)
sut2.applyAlphaCut(alpha)
@ -143,7 +141,7 @@ final class DiscreteMutableFuzzySetTests: XCTestCase {
"e": 0.999,
"f": 1.0,
]
let set = DiscreteMutableFuzzySet(elementToGradeMap: initial)
let set = DiscreteMutableFuzzySet(initial)
let sut = set.complement()