Skip to content

Commit 3132e5a

Browse files
authored
update membership functiosn (#4)
1 parent 9ef26f7 commit 3132e5a

File tree

8 files changed

+243
-17
lines changed

8 files changed

+243
-17
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
*.jl.mem
44
Manifest.toml
55

6+
# automatically generated files during docs building
67
/docs/build/
78
indigo.css
89
/docs/src/tutorials/
910
/docs/src/applications/
10-
/docs/src/notebooks/
11+
/docs/src/notebooks/
12+
/docs/src/api/memberships.md

docs/make.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Pkg.add(Pkg.PackageSpec(name = "Documenter", rev = "30baed5")) # TODO remove onc
1313

1414
using Documenter
1515
using DocThemeIndigo
16+
using InteractiveUtils
1617
using FuzzyLogic
1718
using Literate
1819

@@ -53,6 +54,46 @@ for (root, _, files) in walkdir(jldir), file in files
5354
Literate.notebook(ipath, nbdir; execute = IS_CI, credit = false)
5455
end
5556

57+
###################
58+
# CREATE API DOCS #
59+
###################
60+
61+
function generate_memberships()
62+
mfs = subtypes(FuzzyLogic.AbstractMembershipFunction)
63+
open(joinpath(@__DIR__, "src", "api", "memberships.md"), "w") do f
64+
write(f, """```@setup memberships
65+
using FuzzyLogic
66+
using Plots
67+
```
68+
69+
# Membership functions
70+
71+
""")
72+
for mf in mfs
73+
sec = string(mf)[1:(end - 2)] * " membership function"
74+
write(f, """## $sec
75+
76+
```@docs
77+
$mf
78+
```
79+
80+
""")
81+
docstring = match(r"```julia\nmf = (\w+\(.+\))\n```", string(Docs.doc(mf)))
82+
if !isnothing(docstring)
83+
mfex = only(docstring.captures)
84+
write(f, """
85+
```@example memberships
86+
plot($mfex, 0, 10) # hide
87+
```
88+
89+
""")
90+
end
91+
end
92+
end
93+
end
94+
95+
generate_memberships()
96+
5697
###############
5798
# CREATE HTML #
5899
###############

docs/src/api/memberships.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

docs/src/changelog.md

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

1111
- initial domain specific language design and parser
12-
- initial membership functions: triangular, trapezoidal, gaussian, bell, linear, sigmoid, sum of sigmoids, product of sigmoids.
12+
- initial membership functions: triangular, trapezoidal, gaussian, bell, linear, sigmoid, sum of sigmoids, product of sigmoids, s-shaped, z-shaped, pi-shaped.
1313
- initial implementation of Mamdani and Sugeno inference systems (type 1)
1414
- min, prod and Łukasiewicz t-norms with corresponding conorms
1515
- min and prod implication

src/FuzzyLogic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ include("evaluation.jl")
1313
include("plotting.jl")
1414

1515
export DifferenceSigmoidMF, LinearMF, GeneralizedBellMF, GaussianMF, ProductSigmoidMF,
16-
SigmoidMF, TrapezoidalMF, TriangularMF,
16+
SigmoidMF, TrapezoidalMF, TriangularMF, SShapeMF, ZShapeMF, PiShapeMF,
1717
ProdAnd, MinAnd, ProbSumOr, MaxOr, MinImplication, ProdImplication,
1818
MaxAggregator, ProbSumAggregator, CentroidDefuzzifier, BisectorDefuzzifier,
1919
@fis, FuzzyInferenceSystem, Domain, Variable

src/docstrings.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,4 @@ using DocStringExtensions
1212
$(TYPEDEF)
1313
1414
$(DOCSTRING)
15-
16-
## Fields
17-
$(TYPEDFIELDS)
1815
"""

src/membership_functions.jl

Lines changed: 168 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22

33
abstract type AbstractMembershipFunction end
44

5-
@doc raw"""
6-
Generalized Bell membership function ``\frac{1}{1+\vert\frac{x-c}{a}\vert^{2b}}``.
5+
"""
6+
Generalized Bell membership function ``\\frac{1}{1+\\vert\\frac{x-c}{a}\\vert^{2b}}``.
7+
8+
### Fields
9+
10+
$(TYPEDFIELDS)
11+
12+
### Example
13+
14+
```julia
15+
mf = GeneralizedBellMF(2, 4, 5)
16+
```
717
"""
818
struct GeneralizedBellMF{T <: Real, S <: Real} <: AbstractMembershipFunction
919
"Width of the curve, the bigger the wider."
@@ -17,6 +27,16 @@ end
1727

1828
"""
1929
Gaussian membership function ``e^{-\\frac{(x-μ)²}{2σ²}}``.
30+
31+
### Fields
32+
33+
$(TYPEDFIELDS)
34+
35+
### Example
36+
37+
```julia
38+
mf = GaussianMF(5.0, 1.5)
39+
```
2040
"""
2141
struct GaussianMF{T <: Real} <: AbstractMembershipFunction
2242
"mean ``μ``."
@@ -28,6 +48,16 @@ end
2848

2949
"""
3050
Triangular membership function.
51+
52+
### Fields
53+
54+
$(TYPEDFIELDS)
55+
56+
### Example
57+
58+
```julia
59+
mf = TriangularMF(3, 5, 7)
60+
```
3161
"""
3262
struct TriangularMF{T <: Real} <: AbstractMembershipFunction
3363
"left foot."
@@ -41,6 +71,16 @@ end
4171

4272
"""
4373
Trapezoidal membership function.
74+
75+
### Fields
76+
77+
$(TYPEDFIELDS)
78+
79+
### Example
80+
81+
```julia
82+
mf = TrapezoidalMF(1, 3, 7, 9)
83+
```
4484
"""
4585
struct TrapezoidalMF{T <: Real} <: AbstractMembershipFunction
4686
"left foot."
@@ -59,6 +99,16 @@ end
5999
"""
60100
Linear membership function.
61101
If ``a < b``, it is increasing (S-shaped), otherwise it is decreasing (Z-shaped).
102+
103+
### Fields
104+
105+
$(TYPEDFIELDS)
106+
107+
### Example
108+
109+
```julia
110+
mf = LinearMF(2, 8)
111+
```
62112
"""
63113
struct LinearMF{T <: Real} <: AbstractMembershipFunction
64114
"foot."
@@ -68,8 +118,18 @@ struct LinearMF{T <: Real} <: AbstractMembershipFunction
68118
end
69119
(mf::LinearMF)(x) = max(min((x - mf.a) / (mf.b - mf.a), 1), 0)
70120

71-
@doc raw"""
72-
Sigmoid membership function ``\frac{1}{1+e^{-a(x-c)}}``.
121+
"""
122+
Sigmoid membership function ``\\frac{1}{1+e^{-a(x-c)}}``.
123+
124+
### Fields
125+
126+
$(TYPEDFIELDS)
127+
128+
### Example
129+
130+
```julia
131+
mf = SigmoidMF(2, 5)
132+
```
73133
"""
74134
struct SigmoidMF{T <: Real} <: AbstractMembershipFunction
75135
"parameter controlling the slope of the curve."
@@ -81,6 +141,16 @@ end
81141

82142
"""
83143
Difference of two sigmoids. See also [`SigmoidMF`](@ref).
144+
145+
### Fields
146+
147+
$(TYPEDFIELDS)
148+
149+
### Example
150+
151+
```julia
152+
mf = DifferenceSigmoidMF(5, 2, 5, 7)
153+
```
84154
"""
85155
struct DifferenceSigmoidMF{T <: Real} <: AbstractMembershipFunction
86156
"slope of the first sigmoid."
@@ -99,6 +169,16 @@ end
99169

100170
"""
101171
Product of two sigmoids. See also [`SigmoidMF`](@ref).
172+
173+
### Fields
174+
175+
$(TYPEDFIELDS)
176+
177+
### Example
178+
179+
```julia
180+
mf = ProductSigmoidMF(2, 3, -5, 8)
181+
```
102182
"""
103183
struct ProductSigmoidMF{T <: Real} <: AbstractMembershipFunction
104184
"slope of the first sigmoid."
@@ -113,3 +193,87 @@ end
113193
function (mf::ProductSigmoidMF)(x)
114194
return 1 / ((1 + exp(-mf.a1 * (x - mf.c1))) * (1 + exp(-mf.a2 * (x - mf.c2))))
115195
end
196+
197+
"""
198+
S-shaped membership function.
199+
200+
### Fields
201+
202+
$(TYPEDFIELDS)
203+
204+
### Example
205+
206+
```julia
207+
mf = SShapeMF(1, 8)
208+
```
209+
"""
210+
struct SShapeMF{T <: Real} <: AbstractMembershipFunction
211+
"foot."
212+
a::T
213+
"shoulder."
214+
b::T
215+
end
216+
function (s::SShapeMF)(x::T) where {T <: Real}
217+
x <= s.a && return zero(float(T))
218+
x >= s.b && return one(float(T))
219+
x >= (s.a + s.b) / 2 && return 1 - 2 * ((x - s.b) / (s.b - s.a))^2
220+
return 2 * ((x - s.a) / (s.b - s.a))^2
221+
end
222+
223+
"""
224+
Z-shaped membership function.
225+
226+
### Fields
227+
228+
$(TYPEDFIELDS)
229+
230+
### Example
231+
232+
```julia
233+
mf = ZShapeMF(3, 7)
234+
```
235+
"""
236+
struct ZShapeMF{T <: Real} <: AbstractMembershipFunction
237+
"shoulder."
238+
a::T
239+
"foot."
240+
b::T
241+
end
242+
function (z::ZShapeMF)(x::T) where {T <: Real}
243+
x <= z.a && return one(float(T))
244+
x >= z.b && return zero(float(T))
245+
x >= (z.a + z.b) / 2 && return 2 * ((x - z.b) / (z.b - z.a))^2
246+
return 1 - 2 * ((x - z.a) / (z.b - z.a))^2
247+
end
248+
249+
"""
250+
Π-shaped membership function.
251+
252+
### Fields
253+
254+
$(TYPEDFIELDS)
255+
256+
### Example
257+
258+
```julia
259+
mf = PiShapeMF(1, 4, 5, 10)
260+
```
261+
"""
262+
struct PiShapeMF{T <: Real} <: AbstractMembershipFunction
263+
"left foot."
264+
a::T
265+
"left shoulder."
266+
b::T
267+
"right shoulder."
268+
c::T
269+
"right foot."
270+
d::T
271+
end
272+
function (p::PiShapeMF)(x::T) where {T <: Real}
273+
(x <= p.a || x >= p.d) && return zero(float(T))
274+
p.b <= x <= p.c && return one(float(T))
275+
x <= (p.a + p.b) / 2 && return 2 * ((x - p.a) / (p.b - p.a))^2
276+
x <= p.b && return 1 - 2 * ((x - p.b) / (p.b - p.a))^2
277+
x <= (p.c + p.d) / 2 && return 1 - 2 * ((x - p.c) / (p.d - p.c))^2
278+
return 2 * ((x - p.d) / (p.d - p.c))^2
279+
end

test/test_membership_functions.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,32 @@ end
7878
@test f(8) == 0
7979
@test f(9) == 0
8080
end
81+
82+
@testset "S-shaped MF" begin
83+
mf = SShapeMF(1, 8)
84+
@test mf(0) == 0
85+
@test mf(1) == 0
86+
@test mf(4.5) == 0.5
87+
@test mf(8) == 1
88+
@test mf(9) == 1
89+
end
90+
91+
@testset "Z-shaped MF" begin
92+
mf = ZShapeMF(3, 7)
93+
@test mf(2) == 1
94+
@test mf(3) == 1
95+
@test mf(5) == 0.5
96+
@test mf(7) == 0
97+
@test mf(9) == 0
98+
end
99+
100+
@testset "Pi-shaped MF" begin
101+
mf = PiShapeMF(1, 4, 5, 10)
102+
@test mf(1) == 0
103+
@test mf(2.5) == 0.5
104+
@test mf(4) == 1
105+
@test mf(4.5) == 1
106+
@test mf(5) == 1
107+
@test mf(7.5) == 0.5
108+
@test mf(10) == 0
109+
end

0 commit comments

Comments
 (0)