diff --git a/Project.toml b/Project.toml index 685e62b..d1664a1 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Fuzzy" uuid = "a9166f1b-85e5-4df0-9c26-e06b441f12e8" authors = ["Phelipe Wesley and contributors"] -version = "0.3.0" +version = "0.3.1" [compat] julia = "1.6" diff --git a/README.md b/README.md index 9bf2aba..1233d39 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/DotsMF.jl b/src/DotsMF.jl new file mode 100644 index 0000000..25a2632 --- /dev/null +++ b/src/DotsMF.jl @@ -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 diff --git a/src/Fuzzy.jl b/src/Fuzzy.jl index 1356f7d..fb47347 100644 --- a/src/Fuzzy.jl +++ b/src/Fuzzy.jl @@ -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 diff --git a/test/fis_mamdani.jl b/test/fis_mamdani.jl index ed8d72e..4026ba5 100644 --- a/test/fis_mamdani.jl +++ b/test/fis_mamdani.jl @@ -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") @@ -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 \ No newline at end of file diff --git a/test/triangular_mf.jl b/test/triangular_mf.jl index fce1cb8..6edca18 100644 --- a/test/triangular_mf.jl +++ b/test/triangular_mf.jl @@ -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. \ No newline at end of file +@assert Fuzzy.mean_at(mf, 1.0) == center +@assert Fuzzy.mean_at(mf, 0.0) == (l_vertex + r_vertex) / 2.0 \ No newline at end of file