aresio / simpful

A friendly python library for fuzzy logic reasoning
Academic Free License v3.0
130 stars 33 forks source link

Result plausibility #24

Open cupressus opened 1 year ago

cupressus commented 1 year ago

I am looking for guidance to apply simpful as intended. I prepared a minimalistic experiment (screenshot) and respective code (below). I used python 3.10.12 with simpful 2.11.0

defuzzifier_example

I tried various things to tweak my example for the desired outcome, but so far failed. I assume it is working as expected, but I don't find the result intuitive or explainable. Any discussion/clarification is greatly appreciated.

Be Good

from simpful import * # version 2.11.0

FS = FuzzySystem(show_banner=False)

# Create First Input "Rating"
S_1 = TriangleFuzzySet(a=0, b=0, c=5, term="laggard")
S_2 = TriangleFuzzySet(a=0, b=5, c=10, term="average")
S_3 = TriangleFuzzySet(a=5, b=10, c=10, term="leader")

FS.add_linguistic_variable(
    "Rating",
    LinguisticVariable(
        [
            S_1,
            S_2,
            S_3,
        ],
        concept="Rating",
        universe_of_discourse=[0, 10],
    ),
)

# Create Second Input "Revenues"
R_1 = TrapezoidFuzzySet(a=0, b=0, c=1, d=5, term="miss")
R_2 = TrapezoidFuzzySet(a=1, b=5, c=15, d=30, term="minor")
R_3 = TrapezoidFuzzySet(a=15, b=30, c=50, d=80, term="major")
R_4 = TrapezoidFuzzySet(a=50, b=80, c=100, d=100, term="pure")

FS.add_linguistic_variable(
    "Revenue",
    LinguisticVariable(
        [
            R_1,
            R_2,
            R_3,
            R_4,
        ],
        concept="Revenue",
        universe_of_discourse=[0, 100],
    ),
)

# Create Output
T_1 = TrapezoidFuzzySet(a=0, b=0, c=0, d=2.5, term="veryslim")
T_2 = TrapezoidFuzzySet(a=0, b=2.5, c=2.5, d=5, term="slim")
T_3 = TrapezoidFuzzySet(a=2.5, b=5, c=5, d=7.5, term="moderate")
T_4 = TrapezoidFuzzySet(a=5, b=7.5, c=7.5, d=10, term="strong")
T_5 = TrapezoidFuzzySet(a=7.5, b=10, c=10, d=10, term="verystrong")

FS.add_linguistic_variable(
    "Result",
    LinguisticVariable([T_1, T_2, T_3, T_4, T_5], universe_of_discourse=[0, 10]),
)

# Create Rules
FS.add_rules(
    [
        "IF (Rating IS leader) THEN (Result IS verystrong)",
        #
        "IF (Rating IS average) AND (Revenue IS pure) THEN (Result IS strong)",
        "IF (Rating IS average) AND (Revenue IS major) THEN (Result IS moderate)",
        "IF (Rating IS average) AND (Revenue IS minor) THEN (Result IS slim)",
        "IF (Rating IS average) AND (Revenue IS miss) THEN (Result IS veryslim)",
        #
        "IF (Rating IS laggard) THEN (Result IS veryslim)",
    ]
)

for i in range(0,40,1):
    FS.set_variable("Rating", 8)
    FS.set_variable("Revenue", i)

    print(f"For Rating frozen at 8. Revenue {i}: Result is: {FS.Mamdani_inference(['Result']).get('Result')}")