Skip to content

Commit

Permalink
Merge pull request #20 from phelipe/feature/plot-points
Browse files Browse the repository at this point in the history
Feature - plot points
  • Loading branch information
phelipe authored Nov 27, 2021
2 parents e006ebf + 5509e95 commit 00b403c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Fuzzy"
uuid = "a9166f1b-85e5-4df0-9c26-e06b441f12e8"
authors = ["Phelipe Wesley <[email protected]> and contributors"]
version = "0.3.0"
version = "0.3.1"

[compat]
julia = "1.6"
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Fuzzy

=============

Mamdani and Sugeno type Fuzzy Inference System in julia. This code is based in [Lepisma](https://github.com/lepisma/Fuzzy.jl).


[![CI](https://github.com/phelipe/Fuzzy.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/phelipe/Fuzzy.jl/actions/workflows/ci.yml)


## Install

```julia
Expand Down Expand Up @@ -114,6 +113,18 @@ julia> eval_fis(fis, in_vals)
- Einstein sum (E-SUM)
- Hamacher sum (H-SUM)

## Prepare to plot your Fuzzy sets

- Create points to use in plot packages using the chart_prepare

```julia
julia> input_a = Dict("small" => TriangularMF(1, 2, 3), "large" => TriangularMF(5, 6, 7));
julia> x = range(0, 8, length = 100);
julia> data = chart_prepare(ipa, x)
julia> using Plots
julia> plot(x, data["values"], label = data["names"])
```

## License

MIT
31 changes: 31 additions & 0 deletions src/DotsMF.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
get_dots(membership_function::MF, input_point_vector::Union{StepRange,StepRangeLen}) =
get_dots(membership_function, collect(input_point_vector))

function get_dots(membership_function::MF, input_point_vector::Vector{T}) where {T<:Number}
return Real.(map(x -> Fuzzy.eval(membership_function, x), input_point_vector))
end

chart_prepare(sets_dict::Dict{String,N}, input_point_vector::Union{StepRange,StepRangeLen}) where {N<:MF} =
chart_prepare(sets_dict, collect(input_point_vector))


"""
Create points
eval_fis(sets_dict, input_point_vector)
Parameters
----------
`sets_dict` dictionary of membership functions
`input_point_vector` array of points
"""
function chart_prepare(sets_dict::Dict{String,N}, input_point_vector::Vector{T}) where {T<:Number,N<:MF}
names = String[]
outputs = Array{T,1}[]
for (name, membership_function) in sets_dict
push!(names, name)
push!(outputs, get_dots(membership_function, input_point_vector))
end
return Dict("names" => reshape(names, 1, :), "values" => outputs)
end
43 changes: 26 additions & 17 deletions src/Fuzzy.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
module Fuzzy

export TriangularMF, GaussianMF, BellMF, TrapezoidalMF, SigmoidMF
export TriangularMF, GaussianMF, BellMF, TrapezoidalMF, SigmoidMF

export Rule, FISMamdani, FISSugeno
export Rule, FISMamdani, FISSugeno

export mean_at, eval_fis
export mean_at, eval_fis

export minimum_value, algebraic_product, bounded_difference, drastic_product, einstein_product, hamacher_product
export chart_prepare

export maximum_value, algebraic_sum, bounded_sum, drastic_sum, einstein_sum, hamacher_sum
export minimum_value, algebraic_product, bounded_difference, drastic_product, einstein_product, hamacher_product

# T-Norm
include("T-norm.jl")
export maximum_value, algebraic_sum, bounded_sum, drastic_sum, einstein_sum, hamacher_sum

# S-Norm
include("S-norm.jl")
export MFDict

# Membership functions
include("MF.jl")
# T-Norm
include("T-norm.jl")

# Membership functions evaluations
include("EvalMF.jl")
# S-Norm
include("S-norm.jl")

# FIS
include("FIS.jl")
# Membership functions
include("MF.jl")

# Evaluation functions
include("Eval.jl")
# Membership functions evaluations
include("EvalMF.jl")

# FIS
include("FIS.jl")

# Evaluation functions
include("Eval.jl")

#Dots function
include("DotsMF.jl")

MFDict = Dict{String,MF}

end
18 changes: 9 additions & 9 deletions test/fis_mamdani.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ ival2 = [5.7, 2.0]
@assert eval_fis(fis, ival2) == 6.0


service = Dict( "poor" => TrapezoidalMF(0.0, 0.0, 2.0, 4.0),
"good" => TriangularMF(3., 5., 7.),
"excellent" => TrapezoidalMF(6., 8., 10., 10.))
service = Dict("poor" => TrapezoidalMF(0.0, 0.0, 2.0, 4.0),
"good" => TriangularMF(3.0, 5.0, 7.0),
"excellent" => TrapezoidalMF(6.0, 8.0, 10.0, 10.0))

food = Dict("rancid" => TrapezoidalMF(0., 0., 3., 6.),
"delicious" => TrapezoidalMF(4., 7., 10., 10.))
food = Dict("rancid" => TrapezoidalMF(0.0, 0.0, 3.0, 6.0),
"delicious" => TrapezoidalMF(4.0, 7.0, 10.0, 10.0))

inputs = [service, food]

tip = Dict("cheap" => TrapezoidalMF(10., 10., 20., 30.),
"average" => TriangularMF(20., 30., 40.),
"generous" => TrapezoidalMF(30., 40, 50., 50.))
tip = Dict("cheap" => TrapezoidalMF(10.0, 10.0, 20.0, 30.0),
"average" => TriangularMF(20.0, 30.0, 40.0),
"generous" => TrapezoidalMF(30.0, 40, 50.0, 50.0))

rule1 = Rule(["poor", "rancid"], "cheap", "MAX")

Expand All @@ -50,6 +50,6 @@ rules = [rule1, rule2, rule3]

fis_tips = FISMamdani(inputs, tip, rules)

in_vals = [7., 8,]
in_vals = [7.0, 8,]

@assert eval_fis(fis_tips, in_vals) == 45.0
14 changes: 7 additions & 7 deletions test/triangular_mf.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
l_vertex = 2.
center = 5.
r_vertex = 7.
l_vertex = 2.0
center = 5.0
r_vertex = 7.0

mf = TriangularMF(l_vertex, center, r_vertex)

# Evaluation tests
@assert Fuzzy.eval(mf, center) == 1
@assert Fuzzy.eval(mf, l_vertex) == Fuzzy.eval(mf, r_vertex) == 0.
@assert Fuzzy.eval(mf, (l_vertex + center) / 2.) == Fuzzy.eval(mf, (r_vertex + center) / 2.) == 0.5
@assert Fuzzy.eval(mf, l_vertex) == Fuzzy.eval(mf, r_vertex) == 0.0
@assert Fuzzy.eval(mf, (l_vertex + center) / 2.0) == Fuzzy.eval(mf, (r_vertex + center) / 2.0) == 0.5

# Mean finding tests
@assert Fuzzy.mean_at(mf, 1.) == center
@assert Fuzzy.mean_at(mf, 0.) == (l_vertex + r_vertex) / 2.
@assert Fuzzy.mean_at(mf, 1.0) == center
@assert Fuzzy.mean_at(mf, 0.0) == (l_vertex + r_vertex) / 2.0

0 comments on commit 00b403c

Please sign in to comment.