Skip to content

Commit 26307b3

Browse files
authored
added more norms and plots to documentation (#10)
* added more norms and plots to documentation * add tests
1 parent 436edcb commit 26307b3

File tree

8 files changed

+181
-25
lines changed

8 files changed

+181
-25
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ indigo.css
99
/docs/src/tutorials/
1010
/docs/src/applications/
1111
/docs/src/notebooks/
12-
/docs/src/api/memberships.md
12+
/docs/src/api/memberships.md
13+
/docs/src/api/logical.md

docs/make.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,51 @@ function generate_memberships()
9292
end
9393
end
9494

95+
function generate_norms()
96+
connectives = [
97+
subtypes(FuzzyLogic.AbstractAnd),
98+
subtypes(FuzzyLogic.AbstractOr),
99+
subtypes(FuzzyLogic.AbstractImplication),
100+
]
101+
titles = ["Conjuction", "Disjunction", "Implication"]
102+
open(joinpath(@__DIR__, "src", "api", "logical.md"), "w") do f
103+
write(f, """```@setup logicals
104+
using FuzzyLogic
105+
using Plots
106+
```
107+
108+
# Logical connectives
109+
""")
110+
for (t, c) in zip(titles, connectives)
111+
write(f, """
112+
## $t methods
113+
114+
""")
115+
116+
for ci in c
117+
write(f, """
118+
### $(nameof(ci))
119+
120+
```@docs
121+
$ci
122+
```
123+
124+
```@example logicals
125+
x = y = 0:0.01:1 # hide
126+
contourf(x, y, (x, y) -> $ci()(x, y)) # hide
127+
```
128+
""")
129+
end
130+
end
131+
end
132+
end
133+
134+
# Logical connectives
135+
95136
generate_memberships()
96137

138+
generate_norms()
139+
97140
###############
98141
# CREATE HTML #
99142
###############

docs/src/api/logical.md

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

docs/src/changelog.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

5+
## unreleased
6+
7+
- added support Lukasiewicz, drastic, nilpotent and Hamacher T-norms and corresponding S-norms.
8+
59
## v0.1.0 -- 2023-01-10
610

711
[view release on GitHub](https://github.com/lucaferranti/FuzzyLogic.jl/releases/tag/v0.1.0)
@@ -11,7 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1115
- initial domain specific language design and parser
1216
- initial membership functions: triangular, trapezoidal, gaussian, bell, linear, sigmoid, sum of sigmoids, product of sigmoids, s-shaped, z-shaped, pi-shaped.
1317
- initial implementation of Mamdani and Sugeno inference systems (type 1)
14-
- min, prod and Łukasiewicz t-norms with corresponding conorms
18+
- min and prod t-norms with corresponding conorms
1519
- min and prod implication
1620
- max and probabilistic sum aggregation method
1721
- centroid and bisector defuzzifier

src/FuzzyLogic.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ include("plotting.jl")
1414

1515
export DifferenceSigmoidMF, LinearMF, GeneralizedBellMF, GaussianMF, ProductSigmoidMF,
1616
SigmoidMF, TrapezoidalMF, TriangularMF, SShapeMF, ZShapeMF, PiShapeMF,
17-
ProdAnd, MinAnd, ProbSumOr, MaxOr, MinImplication, ProdImplication,
17+
ProdAnd, MinAnd, LukasiewiczAnd, DrasticAnd, NilpotentAnd, HamacherAnd,
18+
ProbSumOr, MaxOr, BoundedSumOr, DrasticOr, NilpotentOr, EinsteinOr,
19+
MinImplication, ProdImplication,
1820
MaxAggregator, ProbSumAggregator, CentroidDefuzzifier, BisectorDefuzzifier,
1921
@mamfis, MamdaniFuzzySystem, @sugfis, SugenoFuzzySystem,
2022
LinearSugenoOutput, ConstantSugenoOutput

src/options.jl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,45 @@ Product T-norm defining conjuction as ``A ∧ B = AB``.
1717
struct ProdAnd <: AbstractAnd end
1818
(pa::ProdAnd)(x, y) = x * y
1919

20+
"""
21+
Lukasiewicz T-norm defining conjuction as ``A ∧ B = \\max(0, A + B - 1)``.
22+
"""
23+
struct LukasiewiczAnd <: AbstractAnd end
24+
(la::LukasiewiczAnd)(x, y) = max(0, x + y - 1)
25+
26+
"""
27+
Drastic T-norm defining conjuction as ``A ∧ B = \\min(A, B)`` is ``A = 1`` or ``B = 1`` and
28+
``A ∧ B = 0`` otherwise.
29+
"""
30+
struct DrasticAnd <: AbstractAnd end
31+
function (da::DrasticAnd)(x::T, y::S) where {T <: Real, S <: Real}
32+
TS = promote_type(T, S)
33+
isone(x) && return TS(y)
34+
isone(y) && return TS(x)
35+
zero(TS)
36+
end
37+
38+
"""
39+
Nilpotent T-norm defining conjuction as ``A ∧ B = \\min(A, B)`` when ``A + B > 1`` and
40+
``A ∧ B = 0`` otherwise.
41+
"""
42+
struct NilpotentAnd <: AbstractAnd end
43+
function (na::NilpotentAnd)(x::T, y::S) where {T <: Real, S <: Real}
44+
m = min(x, y)
45+
x + y > 1 && return m
46+
return zero(m)
47+
end
48+
49+
"""
50+
Hamacher T-norm defining conjuction as ``A ∧ B = \\frac{AB}{A + B - AB}`` if ``A ≂̸ 0 ≂̸ B``
51+
and ``A ∧ B = 0`` otherwise.
52+
"""
53+
struct HamacherAnd <: AbstractAnd end
54+
function (ha::HamacherAnd)(x::T, y::S) where {T <: Real, S <: Real}
55+
iszero(x) && iszero(y) && return zero(float(promote_type(T, S)))
56+
(x * y) / (x + y - x * y)
57+
end
58+
2059
## S-Norms
2160

2261
abstract type AbstractOr <: AbstractFISSetting end
@@ -33,6 +72,40 @@ Probabilistic sum S-norm defining disjunction as ``A ∨ B = A + B - AB``.
3372
struct ProbSumOr <: AbstractOr end
3473
(pso::ProbSumOr)(x, y) = x + y - x * y
3574

75+
"""
76+
Bounded sum S-norm defining disjunction as ``A ∨ B = \\min(1, A + B)``.
77+
"""
78+
struct BoundedSumOr <: AbstractOr end
79+
(la::BoundedSumOr)(x, y) = min(1, x + y)
80+
81+
"""
82+
Drastic S-norm defining disjunction as ``A ∨ B = \\min(1, A + B)``.
83+
"""
84+
struct DrasticOr <: AbstractOr end
85+
function (da::DrasticOr)(x::T, y::S) where {T <: Real, S <: Real}
86+
TS = promote_type(T, S)
87+
iszero(x) && return TS(y)
88+
iszero(y) && return TS(x)
89+
one(TS)
90+
end
91+
92+
"""
93+
Nilpotent S-norm defining disjunction as ``A ∨ B = \\max(A, B)`` when ``A + B < 1`` and
94+
``A ∧ B = 1`` otherwise.
95+
"""
96+
struct NilpotentOr <: AbstractOr end
97+
function (na::NilpotentOr)(x::T, y::S) where {T <: Real, S <: Real}
98+
m = max(x, y)
99+
x + y < 1 && return m
100+
return one(m)
101+
end
102+
103+
"""
104+
Einstein S-norm defining disjunction as ``A ∨ B = \\frac{A + B}{1 + AB}``.
105+
"""
106+
struct EinsteinOr <: AbstractOr end
107+
(ha::EinsteinOr)(x, y) = (x + y) / (1 + x * y)
108+
36109
## Implication
37110

38111
abstract type AbstractImplication <: AbstractFISSetting end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using SafeTestsets, Test
22

33
testfiles = [
44
"test_membership_functions.jl",
5+
"test_settings.jl",
56
"test_parser.jl",
67
"test_evaluation.jl",
78
"test_plotting.jl",

test/test_settings.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using FuzzyLogic
2+
using Test
3+
4+
@testset "test T--norms" begin
5+
@test MinAnd()(0.4, 0.2) == 0.2
6+
@test MinAnd()(1.0, 1.0) == 1.0
7+
@test MinAnd()(0.0, 0.0) == 0.0
8+
9+
@test ProdAnd()(0.4, 0.2) 0.08
10+
@test ProdAnd()(1.0, 1.0) == 1.0
11+
@test ProdAnd()(0.0, 0.0) == 0.0
12+
13+
@test DrasticAnd()(1.0, 0.2) == 0.2
14+
@test DrasticAnd()(0.3, 1.0) == 0.3
15+
@test DrasticAnd()(0.9, 0.9) == 0.0
16+
17+
@test LukasiewiczAnd()(0.4, 0.2) == 0
18+
@test LukasiewiczAnd()(0.7, 0.5) 0.2
19+
@test LukasiewiczAnd()(0.5, 0.5) 0
20+
21+
@test NilpotentAnd()(0.4, 0.2) == 0.0
22+
@test NilpotentAnd()(0.5, 0.7) == 0.5
23+
@test NilpotentAnd()(0.5, 0.5) == 0.0
24+
25+
@test HamacherAnd()(0.0, 0.0) == 0.0
26+
@test HamacherAnd()(0.4, 0.2) 0.15384615384615388
27+
@test HamacherAnd()(1.0, 1.0) == 1.0
28+
end
29+
30+
@testset "test S-norms" begin
31+
@test MaxOr()(0.4, 0.2) == 0.4
32+
@test MaxOr()(1.0, 1.0) == 1.0
33+
@test MaxOr()(0.0, 0.0) == 0.0
34+
35+
@test ProbSumOr()(0.5, 0.5) == 0.75
36+
@test ProbSumOr()(1.0, 0.2) == 1.0
37+
@test ProbSumOr()(1.0, 0.0) == 1.0
38+
39+
@test BoundedSumOr()(0.2, 0.3) == 0.5
40+
@test BoundedSumOr()(0.6, 0.6) == 1.0
41+
@test BoundedSumOr()(0.0, 0.0) == 0.0
42+
43+
@test DrasticOr()(0.2, 0.0) == 0.2
44+
@test DrasticOr()(0.0, 0.2) == 0.2
45+
@test DrasticOr()(0.01, 0.01) == 1.0
46+
47+
@test NilpotentOr()(0.2, 0.3) == 0.3
48+
@test NilpotentOr()(0.5, 0.6) == 1.0
49+
@test NilpotentOr()(0.7, 0.1) == 0.7
50+
51+
@test EinsteinOr()(0.0, 0.0) == 0.0
52+
@test EinsteinOr()(0.5, 0.5) == 0.8
53+
@test EinsteinOr()(1.0, 1.0) == 1.0
54+
end

0 commit comments

Comments
 (0)