Add FuzzyRule and custom operator
This commit is contained in:
parent
f7dcd8bcdb
commit
d8c81e8625
|
@ -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<P: FuzzySet, Q: FuzzySet>(
|
||||
rule: FuzzyRule<P, Q>,
|
||||
method: ImplicationMethod = .mamdani
|
||||
) -> Self where P.Universe == U, Q.Universe == V {
|
||||
.init {
|
||||
rule.apply($0.0, $0.1, method: method)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
import FuzzySets
|
||||
|
||||
public struct FuzzyRule<P: FuzzySet, Q: FuzzySet> {
|
||||
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 --> <P: FuzzySet, Q: FuzzySet> (lhs: P, rhs: Q) -> FuzzyRule<P, Q> {
|
||||
.init(antecedent: lhs, consequent: rhs)
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
import FuzzySets
|
||||
import FuzzyRelations
|
||||
|
||||
public extension FuzzySetComposition {
|
||||
static func generalizedModusPonens<P: FuzzySet, Q: FuzzySet>(
|
||||
premise: FuzzyRule<P, Q>,
|
||||
fact: IterableFuzzySet<U, Seq>,
|
||||
method: ImplicationMethod = .mamdani
|
||||
) -> Self where P.Universe == U, Q.Universe == V {
|
||||
.init(
|
||||
set: fact,
|
||||
relation: .implication(
|
||||
rule: premise,
|
||||
method: method
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
import FuzzySets
|
||||
import FuzzyRelations
|
||||
|
||||
public struct GeneralizedModusPonens<U, V, Seq: Sequence> where Seq.Element == U {
|
||||
|
||||
let composition: FuzzySetComposition<U, V, Seq>
|
||||
|
||||
public init<P: FuzzySet, Q: FuzzySet>(
|
||||
ruleAntecedent: P,
|
||||
ruleConsequent: Q,
|
||||
fact: IterableFuzzySet<U, Seq>,
|
||||
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<V> {
|
||||
.init { composition[$0] }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue