Skip to content

Commit 48820a3

Browse files
authored
add singleton mf and missing code generation function for semielliptic mf (#32)
1 parent f791d45 commit 48820a3

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed

docs/src/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
44

55
## Unreleased
66

7-
- ![](https://img.shields.io/badge/new%20feature-green.svg) added semi-elliptic membership functions
7+
- ![](https://img.shields.io/badge/new%20feature-green.svg) added semi-elliptic and singleton membership functions
88
- ![](https://img.shields.io/badge/new%20feature-green.svg) added `gensurf` to plot generating surface
99
- ![](https://img.shields.io/badge/bugfix-purple.svg) fix plotting of systems with several rules and membership functions.
1010

src/FuzzyLogic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ include("tojulia.jl")
1717
include("readwrite.jl")
1818

1919
export DifferenceSigmoidMF, LinearMF, GeneralizedBellMF, GaussianMF, ProductSigmoidMF,
20-
SigmoidMF, TrapezoidalMF, TriangularMF, SShapeMF, ZShapeMF, PiShapeMF,
20+
SigmoidMF, SingletonMF, TrapezoidalMF, TriangularMF, SShapeMF, ZShapeMF, PiShapeMF,
2121
SemiEllipticMF, PiecewiseLinearMF, WeightedMF, Type2MF, ..,
2222
ProdAnd, MinAnd, LukasiewiczAnd, DrasticAnd, NilpotentAnd, HamacherAnd, EinsteinAnd,
2323
ProbSumOr, MaxOr, BoundedSumOr, DrasticOr, NilpotentOr, EinsteinOr, HamacherOr,

src/membership_functions.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22
abstract type AbstractPredicate end
33
abstract type AbstractMembershipFunction <: AbstractPredicate end
44

5+
"""
6+
Singleton membership function. Equal to one at a single point and zero elsewhere.
7+
8+
### Fields
9+
10+
$(TYPEDFIELDS)
11+
12+
### Example
13+
14+
```julia
15+
mf = SingletonMF(4)
16+
```
17+
18+
"""
19+
struct SingletonMF{T <: Real} <: AbstractMembershipFunction
20+
"Point at which the membership function has value 1."
21+
c::T
22+
end
23+
(mf::SingletonMF)(x::Real) = mf.c == x ? one(x) : zero(x)
24+
525
"""
626
Generalized Bell membership function ``\\frac{1}{1+\\vert\\frac{x-c}{a}\\vert^{2b}}``.
727

src/plotting.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ using RecipesBase
66
x -> mf(x), low, high
77
end
88

9+
# special case: singleton mf
10+
@recipe function f(mf::SingletonMF, low::Real, high::Real)
11+
legend --> nothing
12+
line --> :stem
13+
marker --> :circle
14+
markersize --> 10
15+
xlims --> (low, high)
16+
[mf.c], [1]
17+
end
18+
919
@recipe function f(mf::Type2MF, low::Real, high::Real)
1020
legend --> nothing
1121
@series begin

src/tojulia.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ end
126126
# MEMBERSHIP FUNCTIONS CODE GENERATION #
127127
########################################
128128

129+
to_expr(mf::SingletonMF, x = :x) = :($x == $(mf.c) ? one($x) : zero($x))
130+
129131
to_expr(mf::GaussianMF, x = :x) = :(exp(-($x - $(mf.mu))^2 / $(2 * mf.sig^2)))
130132
function to_expr(mf::TriangularMF, x = :x)
131133
:(max(min(($x - $(mf.a)) / $(mf.b - mf.a), ($(mf.c) - $x) / $(mf.c - mf.b)), 0))
@@ -196,6 +198,14 @@ function to_expr(p::PiShapeMF, x = :x)
196198
end)
197199
end
198200

201+
function to_expr(se::SemiEllipticMF, x = :x)
202+
:(if $(se.cd - se.rd) <= $x <= $(se.cd + se.rd)
203+
sqrt(1 - ($(se.cd) - $x)^2 / $(se.rd)^2)
204+
else
205+
zero($x)
206+
end)
207+
end
208+
199209
function to_expr(plmf::PiecewiseLinearMF, x = :x)
200210
:(if $x <= $(plmf.points[1][1])
201211
$(float(plmf.points[1][2]))

test/test_membership_functions.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ end
6060
@test f(2) == eval(to_expr(f, 2)) == 1 / (1 + exp(4))
6161
end
6262

63+
@testset "Singleton MF" begin
64+
f = SingletonMF(4)
65+
@test f(4) === eval(to_expr(f, 4)) === 1
66+
@test f(4.0) === eval(to_expr(f, 4.0)) === 1.0
67+
@test iszero(f(3))
68+
@test iszero(eval(to_expr(f, 3)))
69+
@test iszero(f(5))
70+
end
71+
6372
@testset "Trapezoidal MF" begin
6473
f = TrapezoidalMF(1, 5, 7, 8)
6574
@test f(0) == eval(to_expr(f, 0)) == 0
@@ -119,11 +128,11 @@ end
119128

120129
@testset "Semi-Elliptic MF" begin
121130
mf = SemiEllipticMF(5.0, 4.0)
122-
@test mf(0) == 0
123-
@test mf(1) == 0
124-
@test mf(5) == 1
125-
@test mf(9) == 0
126-
@test mf(10) == 0
131+
@test mf(0) == eval(to_expr(mf, 0)) == 0
132+
@test mf(1) == eval(to_expr(mf, 1)) == 0
133+
@test mf(5) == eval(to_expr(mf, 5)) == 1
134+
@test mf(9) == eval(to_expr(mf, 9)) == 0
135+
@test mf(10) == eval(to_expr(mf, 10)) == 0
127136
end
128137

129138
@testset "Piecewise linear Membership function" begin

0 commit comments

Comments
 (0)