EVALFIS : FIS must have rules.

2 views (last 30 days)
Ceferino
Ceferino on 13 Oct 2023
Answered: Sam Chak on 17 Oct 2023
Here is my code. the bold code is giving an error: FIS must have rules but i have set them
% Create fuzzy input variables with triangular and trapezoidal membership functions
fis = mamfis;
% Add input variables
fis = addInput(fis, [0 100], "Name", "input_temperature");
fis = addInput(fis, [0 100], "Name", "input_humidity");
% Add output variable
fis = addOutput(fis, [0 100], "Name", "heater_strength");
% Add membership functions for input variables
fis = addMF(fis, "input_temperature", 'trimf', [0 0 50], "Name", 'cold');
fis = addMF(fis, "input_temperature", 'trimf', [0 50 100], "Name", 'medium');
fis = addMF(fis, "input_temperature", 'trimf', [50 100 100], "Name", 'hot');
fis = addMF(fis, "input_humidity", 'trimf', [0 0 50], "Name", 'low');
fis = addMF(fis, "input_humidity", 'trimf', [0 50 100], "Name", 'medium');
fis = addMF(fis, "input_humidity", 'trimf', [50 100 100], "Name", 'high');
% Add membership functions for the output variable
fis = addMF(fis, "heater_strength", 'trimf', [0 0 25], "Name", "low");
fis = addMF(fis, "heater_strength", 'trimf', [25 50 75], "Name", "medium");
fis = addMF(fis, "heater_strength", 'trimf', [50 75 100], "Name", "high");
% Define the fuzzy rules programmatically
rule1 = "IF input_temperature IS cold AND input_humidity IS high THEN heater_strength IS high";
rule2 = "IF input_temperature IS medium AND input_humidity IS medium THEN heater_strength IS medium";
rule3 = "IF input_temperature IS hot AND input_humidity IS low THEN heater_strength IS low";
% Create a cell array of rules
myRules = [rule1, rule2, rule3];
% Convert the rule strings to rule objects
ruleObjects = addrule(fis, myRules);
% Set input values
input_temperature = 35;
input_humidity = 70;
% Evaluate the fuzzy inference system
output = evalfis([input_temperature, input_humidity], fis);
% Print the input values and result
fprintf('Input Temperature: %f\n', input_temperature);
fprintf('Input Humidity: %f\n', input_humidity);
fprintf('Heater Strength: %f\n', output);
% Visualize the result
figure;
plotmf(fis, 'heater_strength', 1);
hold on;
plot([output output], [0 1], 'r', 'LineWidth', 2);
title('Heater Strength');
xlabel('Heater Strength');
ylabel('Membership Degree');
legend('Fuzzy Output', 'Heater Strength');

Answers (1)

Sam Chak
Sam Chak on 17 Oct 2023
Please note that the input argument order for evalfis() has changed since the R2018b release. Additionally, there was a minor issue with the "ruleObjects," which I have fixed. I spent a few hours beautifying the final part of the plot in Figure 4 because I believe you intended to visualize both the Aggregated Output Membership Function and the Defuzzified crisp value.
I hope you like it!
PS. You will also notice that there is a slight discrepancy between the results returned by the evalfis() and the defuzz() commands.
% Create fuzzy input variables with triangular and trapezoidal membership functions
fis = mamfis;
% Add input variables
fis = addInput(fis, [0 100], "Name", "inputTemperature");
fis = addInput(fis, [0 100], "Name", "inputHumidity" );
% Add membership functions for input variables
fis = addMF(fis, "inputTemperature", 'trimf', [ 0 0 50], "Name", 'cold' );
fis = addMF(fis, "inputTemperature", 'trimf', [ 0 50 100], "Name", 'medium');
fis = addMF(fis, "inputTemperature", 'trimf', [50 100 100], "Name", 'hot' );
fis = addMF(fis, "inputHumidity", 'trimf', [ 0 0 50], "Name", 'low' );
fis = addMF(fis, "inputHumidity", 'trimf', [ 0 50 100], "Name", 'medium');
fis = addMF(fis, "inputHumidity", 'trimf', [50 100 100], "Name", 'high' );
figure(1)
subplot(211), plotmf(fis, 'input', 1), grid on
subplot(212), plotmf(fis, 'input', 2), grid on
% Add output variable
fis = addOutput(fis, [0 100], "Name", "heaterStrength");
% Add membership functions for the output variable
fis = addMF(fis, "heaterStrength", 'trimf', [ 0 0 25], "Name", "low" );
fis = addMF(fis, "heaterStrength", 'trimf', [25 50 75], "Name", "medium");
fis = addMF(fis, "heaterStrength", 'trimf', [50 75 100], "Name", "high" );
figure(2)
plotmf(fis, 'output', 1), grid on
% Define the fuzzy rules programmatically
rule1 = "IF inputTemperature IS cold AND inputHumidity IS high THEN heaterStrength IS high";
rule2 = "IF inputTemperature IS medium AND inputHumidity IS medium THEN heaterStrength IS medium";
rule3 = "IF inputTemperature IS hot AND inputHumidity IS low THEN heaterStrength IS low";
% Create a cell array of rules
Rules = [rule1, rule2, rule3];
% Convert the rule strings to rule objects
fis = addRule(fis, Rules);
showrule(fis)
ans = 3×100 char array
'1. If (inputTemperature is cold) and (inputHumidity is high) then (heaterStrength is high) (1) ' '2. If (inputTemperature is medium) and (inputHumidity is medium) then (heaterStrength is medium) (1)' '3. If (inputTemperature is hot) and (inputHumidity is low) then (heaterStrength is low) (1) '
% Display a high-level diagram of the Fuzzy Inference System (FIS)
figure(3)
% ruleview(fis)
plotfis(fis)
% Set input values
input_temperature = 35;
input_humidity = 70;
input_values = [input_temperature, input_humidity];
% Evaluate the fuzzy inference system
output = evalfis(fis, input_values);
% Print the input values and result
fprintf('Input Temperature: %f\n', input_temperature);
Input Temperature: 35.000000
fprintf('Input Humidity: %f\n', input_humidity);
Input Humidity: 70.000000
fprintf('Heater Strength: %f\n', output);
Heater Strength: 58.877193
% Visualize the Defuzzification Result
xin1 = input_values(1);
xin2 = input_values(2);
in1mf1 = trimf(xin1, [ 0 0 50]); % temp cold
in1mf2 = trimf(xin1, [ 0 50 100]); % temp med
in1mf3 = trimf(xin1, [50 100 100]); % temp hot
in2mf1 = trimf(xin2, [ 0 0 50]); % humid low
in2mf2 = trimf(xin2, [ 0 50 100]); % humid med
in2mf3 = trimf(xin2, [50 100 100]); % humid high
fs1 = min(in1mf1, in2mf3) % Rule 1 firing strength
fs1 = 0.3000
fs2 = min(in1mf2, in2mf2) % Rule 2 firing strength
fs2 = 0.6000
fs3 = min(in1mf3, in2mf1) % Rule 3 firing strength
fs3 = 0
figure(4)
xout = 0:0.0001:100;
plotmf(fis, 'output', 1), ylim([-0.2 1.2]), hold on
pA1 = polyfit([25 50], [0 1], 1); % pA1(1)*x + pA1(2) = 0.6
pA2 = polyfit([50 75], [1 0], 1); % pA2(1)*x + pA2(2) = 0.6
AA = [pA1(1) 0; 0 pA2(1)];
Ab = [fs2-pA1(2); fs2-pA2(2)];
xA = linsolve(AA, Ab); % AA*xA = Ab
mfA = trapmf(xout, [25 xA(1) xA(2) 75]);
pB1 = polyfit([50 75], [0 1], 1); % pB1(1)*x + pB1(2) = 0.3
pB2 = polyfit([75 100], [1 0], 1); % pB2(1)*x + pB2(2) = 0.3
BA = [pB1(1) 0; 0 pB2(1)];
Bb = [fs1-pB1(2); fs1-pB2(2)];
xB = linsolve(BA, Bb); % BA*xB = Bb
mfB = trapmf(xout, [50 xB(1) xB(2) 100]);
aggomf = max(fs2*mfA, fs1*mfB);
plot(xout, aggomf, 'LineWidth', 3), grid on
xCentroid = defuzz(xout, aggomf, 'centroid')
xCentroid = 58.8816
hCentroid = line([xCentroid xCentroid], [-0.1 1.1], 'LineWidth', 2);
tCentroid = text(xCentroid, -0.1, ' centroid', 'FontWeight', 'bold');
yline(fs1, '--', 'fs1');
yline(fs2, '--', 'fs2');
h_gca = gca;
h_gca.YTick = [0.0 0.2 0.4 0.6 0.8 1.0];
% Create a text to point (xStart, yStart) and ending at (xFinal, yFinal)
x = [0.4 0.58]; % x-axis normalized figure coordinate [xStart, xFinal]
y = [0.7 0.50]; % y-axis normalized figure coordinate [yStart, yFinal]
txt = sprintf('heat str: %.3f ', xCentroid);
annotation('textarrow',x, y, 'String', txt)

Categories

Find more on Data Clustering in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!