I cannot get inference rules right #35
-
Hi! 👋Thank you for such a remarkable library! I'm new to Fuzzy Logic and Julia, and I'm trying to use it for my classes because I was asked to do it in theory and I wanted some practice with a proper use case library. The problem with inference rulesI embed my documented notebook with a link to everything documented here. The Notebook |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @Psyhackological , Very cool you found the package and wanted to use it in your class. Hopefully it was useful. The problem are the membership functions senior = TrapezoidalMF(50, 60, 80, 80) In this case you have the same value for the right shoulder and right foot. This leads to 0/0 division which leads to the Meanwhile, you can define it as senior = LinearMF(50, 60) This leads to the same membership function in this case (because # Define the fuzzy inference system using the mamfis function
fis = @mamfis function rest_time(age, hours_worked)::rest
# Define the domains and membership functions for age
age := begin
domain = 15:80
junior = TrapezoidalMF(15, 15, 30, 35)
mid = TrapezoidalMF(30, 35, 50, 60)
senior = LinearMF(50, 60)
end
# Define the domains and membership functions for hours_worked
hours_worked := begin
domain = 1:12
little = TrapezoidalMF(1, 1, 2, 4)
average = TrapezoidalMF(2, 4, 6, 8)
much = LinearMF(6, 8)
end
# Define the domains and membership functions for rest
rest := begin
domain = 10:120
light = LinearMF(25, 15)
sufficient = TrapezoidalMF(15, 25, 60, 90)
proper = LinearMF(60, 90)
end
# age: Junior rules
(age == junior && hours_worked == little) --> rest == light
(age == junior && hours_worked == average) --> rest == light
(age == junior && hours_worked == much) --> rest == sufficient
# age: Mid rules
(age == mid && hours_worked == little) --> rest == light
(age == mid && hours_worked == average) --> rest == sufficient
(age == mid && hours_worked == much) --> rest == proper
# age: Senior rules
(age == senior && hours_worked == little) --> rest == sufficient
(age == senior && hours_worked == average) --> rest == proper
(age == senior && hours_worked == much) --> rest == proper
# I was told to use
and = MinAnd # "to activate the rules we use the MIN operator"
implication = MinImplication # in example implication output "MIN(0,65;0)=0"
aggregator = MaxAggregator # "In the case of aggregation, we use the MAX operator"
defuzzifier = CentroidDefuzzifier # "We calculate the result using the centre of gravity method"
or = MaxOr # I don't use it but it's a default
# These are the defaults ones but I want to be explicit
end This works res = fis(age=80, hours_worked=2)
res[:rest] produces Looking at the other questions in your notebook
I don't understand what you mean. Your FIS has 9 rules, what are the extra rules?
As you probably noticed, your plot is a bit messy. The version on main fixed this and the next release will have logic to automatically resize the plot so that it looks good. Meanwhile, you can do plot(fis; size=(1500,1800)) to get a bigger plot that is readable. THanks for your quesiton, hopefully this helps. Let me know if you have further problems |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
For now, I've figured out: age := begin
domain = 15:80
junior = PiecewiseLinearMF([(15, 1), (30, 1), (35, 0)]) # 15..=30
mid = TrapezoidalMF(30, 35, 50, 60) # 35..=50
senior = PiecewiseLinearMF([(50, 0), (60, 1), (80, 1)]) # 60..=80
end But maybe I'm wrong with my logic... I did it for the rest but does not seem to change anything. Also: fis_ex = compilefis(fis)
eval(fis_ex)
function test_fis(ages, hours_worked)
for age in ages
for hours in hours_worked
rest_minutes = rest_time(age, hours) # Directly store the returned Float64 value
println("Age: $age, Hours Worked: $hours, Rest: $rest_minutes minutes")
end
end
end
# Test cases with specified age groups and hours worked
test_ages = [15, 20, 45, 70, 80] # Represent junior, mid, senior
test_hours_worked = [1, 2, 5, 10, 12] # Represent low, average, high work hours
test_fis(test_ages, test_hours_worked) Outputs NaN: Age: 15, Hours Worked: 1, Rest: NaN minutes
Age: 15, Hours Worked: 2, Rest: NaN minutes
Age: 15, Hours Worked: 5, Rest: NaN minutes
Age: 15, Hours Worked: 10, Rest: NaN minutes
Age: 15, Hours Worked: 12, Rest: NaN minutes
Age: 20, Hours Worked: 1, Rest: NaN minutes
Age: 20, Hours Worked: 2, Rest: 15.410451045104509 minutes
Age: 20, Hours Worked: 5, Rest: 15.410451045104509 minutes
Age: 20, Hours Worked: 10, Rest: 48.10487999999999 minutes
Age: 20, Hours Worked: 12, Rest: 48.10487999999999 minutes
Age: 45, Hours Worked: 1, Rest: NaN minutes
Age: 45, Hours Worked: 2, Rest: 15.410451045104509 minutes
Age: 45, Hours Worked: 5, Rest: 48.10487999999999 minutes
Age: 45, Hours Worked: 10, Rest: 96.66768516255193 minutes
Age: 45, Hours Worked: 12, Rest: 96.66768516255193 minutes
Age: 70, Hours Worked: 1, Rest: NaN minutes
Age: 70, Hours Worked: 2, Rest: 48.10487999999999 minutes
Age: 70, Hours Worked: 5, Rest: 96.66768516255193 minutes
Age: 70, Hours Worked: 10, Rest: 96.66768516255193 minutes
Age: 70, Hours Worked: 12, Rest: 96.66768516255193 minutes
Age: 80, Hours Worked: 1, Rest: NaN minutes
Age: 80, Hours Worked: 2, Rest: 48.10487999999999 minutes
Age: 80, Hours Worked: 5, Rest: 96.66768516255193 minutes
Age: 80, Hours Worked: 10, Rest: 96.66768516255193 minutes
Age: 80, Hours Worked: 12, Rest: 96.66768516255193 minutes Still probably related to
|
Beta Was this translation helpful? Give feedback.
Hi @Psyhackological ,
Very cool you found the package and wanted to use it in your class. Hopefully it was useful.
The problem are the membership functions
In this case you have the same value for the right shoulder and right foot. This leads to 0/0 division which leads to the
NaN
, which then poisons all the other computations. The problem with this is that if you see the membership function as a mathematical function, now it is not continuous at80
. What would you expect the value at it to be?0
or1
?Meanwhile, you can define it as
This leads to the same membership function in this case (because
80
is the boundary of the d…