How can I fix the error message : Each rule must have a consequent pointing to a different output membership function.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
am tunning a fuzzy inference system using PSO, ANFIS and GA, I used PSO to learn the rules of the FIS labelled fisin, while the other two algorithms are used to tuned the parameters of the FIS. The rules are learned successfully using PSO. The issue is in the ANFIS, it keep showing an error message:
"Each rule must have a consequent pointing to a different output membership function."
The code is given below for references:
%% Load automobile fuel consumption data (https://archive.ics.uci.edu/dataset/9/auto+mpg)
[data,name] = loadGasData; % previous MATLAB versions used loadgas
X = data(:,1:6);
Y = data(:,7);
trnX = X(1:2:end,:); % Training input data set
trnY = Y(1:2:end,:); % Training output data set
vldX = X(2:2:end,:); % Validation input data set
vldY = Y(2:2:end,:); % Validation output data set
dataRange = [min(data)' max(data)'];
%% Create a Sugeno FIS for tuning
fisin = sugfis;
for i = 1:6
fisin = addInput(fisin, dataRange(i,:), 'Name', name(i), 'NumMFs', 2);
end
fisin = addOutput(fisin, dataRange(7,:), 'Name', name(7), 'NumMFs', 56);
figure
plotfis(fisin)
%% Stage 1: Learn the rule base using Particle Swarm optimizer, while keeping the input and output MF parameters constant
options = tunefisOptions('Method', 'particleswarm', 'OptimizationType', 'learning', 'NumMaxRules', 59);
options.MethodOptions.MaxIterations = 10;
rng('default')
fisout0 = tunefis(fisin, [], trnX, trnY, options); % carry out the tuning
fprintf('Training RMSE = %.3f MPG\n', calculateRMSE(fisout0, trnX, trnY));
[fisout0.Rules.Description]'
%% Stage 1: Tune the FIS parameters using Anfis
[in, out,rules] = getTunableSettings(fisout0);
options = tunefisOptions('Method', 'anfis');
% options.MethodOptions.MaxIterations = 20; % comment out this line
rng('default')
fisout1 = tunefis(fisout0, [in; out;rules], trnX, trnY, options); % carry out the tuning
fprintf('Training RMSE = %.3f MPG\n', calculateRMSE(fisout1, trnX, trnY));
figure
plotfis(fisout1) % view the PSO-tuned FIS
% view all tuned 64 Rules, if you like
plotActualAndExpectedResultsWithRMSE(fisout1, vldX, vldY) % calculate the RMSE to check accuracy
%% Stage 2: Use rule base from Stage 1 to tune FIS parameters using Genetic Algorithm
[in, out, rule] = getTunableSettings(fisout1);
options.OptimizationType = 'tuning';
options.Method = 'ga'; % Genetic Algorithm
options.MethodOptions.MaxIterations = 60;
options.MethodOptions.UseCompletePoll = true;
rng('default')
fisout = tunefis(fisout1, [in; out; rule], trnX, trnY, options);
fprintf('Training RMSE = %.3f MPG\n', calculateRMSE(fisout, trnX, trnY));
figure
plotfis(fisout) % view the Pattern Search-tuned FIS
plotActualAndExpectedResultsWithRMSE(fisout, vldX, vldY); % calculate the RMSE to check accuracy
% There are 2 Local functions that you need to create
%% Local function #1
function plotActualAndExpectedResultsWithRMSE(fis, x, y)
% Calculate RMSE bewteen actual and expected results
[rmse, actY] = calculateRMSE(fis, x, y);
% Plot results
figure
subplot(2,1,1)
hold on
bar(actY)
bar(y)
bar(min(actY, y),'FaceColor', [0.5 0.5 0.5])
hold off
axis([0 200 0 60])
xlabel("Validation input dataset index"),
ylabel("MPG")
legend(["Actual MPG" "Expected MPG" "Minimum of actual and expected values"], 'Location', 'NorthWest')
title("RMSE = " + num2str(rmse) + " MPG")
subplot(2,1,2)
bar(actY-y)
xlabel("Validation input dataset index"),ylabel("Error (MPG)")
title("Difference Between Actual and Expected Values")
end
%% Local function #2 (this one can be embedded in the local function #1)
function [rmse, actY] = calculateRMSE(fis, x, y)
% Specify options for FIS evaluation
persistent evalOptions
if isempty(evalOptions)
evalOptions = evalfisOptions("EmptyOutputFuzzySetMessage", "none", "NoRuleFiredMessage", "none", "OutOfRangeInputValueMessage", "none");
end
% Evaluate FIS
actY = evalfis(fis, x, evalOptions);
% Calculate RMSE
del = actY - y;
rmse = sqrt(mean(del.^2)); % the rmse() function was introduced in R2022b
% See https://www.mathworks.com/help/matlab/ref/rmse.html
end
Risposte (2)
Sam Chak
il 11 Dic 2023
Hi @Ahmad
The tunefis() command enables the tuning of fuzzy systems by specifying one of the five optimization methods and their options using the tunefisOptions() command. What adds complexity is that the 'MethodOptions' property differs for each algorithm. One needs to be very familiar with all five types of optimization methods and their respective options.
As seen previously, the 'anfis' method in tunefisOptions() threw some error messages during the tuning of the 2nd stage due to its lack of support for certain tuning options, rendering it less flexible in this approach. However, the anfis() direct tuning method itself proves to be quite powerful when the training options are appropriately specified.
In this brief example with 2 inputs and 1 output, Stage 1 is tuned using PSO, and Stage 2 is tuned using GA.
%% Sam's Data for 2 inputs and 1 output
x = linspace(-4, 4, 21)';
y = x;
z = tanh(x) + tanh(y);
data = [x, y, z];
name = ["in1" "in2" "out"];
%% Data allocation for training and validation
X = data(:,1:2);
Y = data(:,end);
trnX = X(1:2:end,:); % Training input data set
trnY = Y(1:2:end,:); % Training output data set
vldX = X(2:2:end,:); % Validation input data set
vldY = Y(2:2:end,:); % Validation output data set
dataRange = [min(data)' max(data)'];
%% Create a Sugeno FIS for tuning
fisin = sugfis;
for i = 1:2
fisin = addInput(fisin, dataRange(i,:), 'Name', name(i), 'NumMFs', 3);
end
fisin = addOutput(fisin, dataRange(3,:), 'Name', name(3), 'NumMFs', 3^2);
figure
plotfis(fisin)
%% Stage 1: Learn the rule base using Particle Swarm optimizer, while keeping the input and output MF parameters constant
options = tunefisOptions('Method', 'particleswarm', 'OptimizationType', 'learning', 'NumMaxRules', 3^2);
options.MethodOptions.MaxIterations = 10;
rng('default')
fisout1 = tunefis(fisin, [], trnX, trnY, options); % carry out the tuning
fprintf('Training RMSE = %.3f \n', calculateRMSE(fisout1, trnX, trnY));
figure
plotfis(fisout1) % view the PSO-tuned FIS
[fisout1.Rules.Description]' % view all tuned 9 Rules, if you like
plotActualAndExpectedResultsWithRMSE(fisout1, vldX, vldY) % calculate the RMSE to check accuracy
%% Stage 2: Use rule base from Stage 1 to tune parameters of input/output MFs & rules using GA optimizer
[in, out, rule] = getTunableSettings(fisout1);
% options.OptimizationType = 'tuning';
% options.Method = 'patternsearch';
% options.MethodOptions.MaxIterations = 20;
% options.MethodOptions.UseCompletePoll = true;
tuningOpt = tunefisOptions; % 'ga' optimizer is selected by default
tuningOpt.MethodOptions.MaxGenerations = 10; % max iterations for 'ga'
rng('default')
% fisout = tunefis(fisout1, [in; out; rule], trnX, trnY, options); % if use 'patternsearch';
fisout = tunefis(fisout1, [in; out; rule], trnX, trnY, tuningOpt); % if use 'ga';
fprintf('Training RMSE = %.3f \n', calculateRMSE(fisout, trnX, trnY));
figure
plotfis(fisout) % view the Pattern Search-tuned FIS
[fisout.Rules.Description]' % view all tuned 9 Rules, if you like
plotActualAndExpectedResultsWithRMSE(fisout, vldX, vldY); % calculate the RMSE to check accuracy
%% Local function #1
function plotActualAndExpectedResultsWithRMSE(fis, x, y)
% Calculate RMSE bewteen actual and expected results
[rmse, actY] = calculateRMSE(fis, x, y);
% Plot results
figure
subplot(2,1,1)
hold on
bar(actY)
bar(y)
bar(min(actY, y),'FaceColor', [0.5 0.5 0.5])
hold off
% axis([0 200 0 60])
xlabel("Validation input dataset index"),
ylabel("Amplitude")
legend(["Actual output" "Expected output" "Minimum of actual and expected values"], 'Location', 'NorthWest')
title("RMSE = " + num2str(rmse) + " amplitude")
subplot(2,1,2)
bar(actY-y)
xlabel("Validation input dataset index"), ylabel("Error")
title("Difference Between Actual and Expected Values")
end
%% Local function #2 (this one can be embedded in the local function #1)
function [rmse, actY] = calculateRMSE(fis, x, y)
% Specify options for FIS evaluation
persistent evalOptions
if isempty(evalOptions)
evalOptions = evalfisOptions("EmptyOutputFuzzySetMessage", "none", "NoRuleFiredMessage", "none", "OutOfRangeInputValueMessage", "none");
end
% Evaluate FIS
actY = evalfis(fis, x, evalOptions);
% Calculate RMSE
del = actY - y;
rmse = sqrt(mean(del.^2)); % the rmse() function was introduced in R2022b
% See https://www.mathworks.com/help/matlab/ref/rmse.html
end
Sam Chak
il 11 Dic 2023
Hi @Ahmad
As requested, Stage 1 is tuned using PSO, and Stage 2 is tuned using Simulated Annealing.
%% Sam's Data for 2 inputs and 1 output
x = linspace(-4, 4, 61)';
y = x;
z = tanh(x) + tanh(y);
data = [x, y, z];
name = ["in1" "in2" "out"];
%% Data allocation for training and validation
X = data(:,1:2);
Y = data(:,end);
trnX = X(1:2:end,:); % Training input data set
trnY = Y(1:2:end,:); % Training output data set
vldX = X(2:2:end,:); % Validation input data set
vldY = Y(2:2:end,:); % Validation output data set
dataRange = [min(data)' max(data)'];
%% Create a Sugeno FIS for tuning
fisin = sugfis;
for i = 1:2
fisin = addInput(fisin, dataRange(i,:), 'Name', name(i), 'NumMFs', 3);
end
fisin = addOutput(fisin, dataRange(3,:), 'Name', name(3), 'NumMFs', 3^2);
figure
plotfis(fisin)
%% Stage 1: Learn the rule base using Particle Swarm optimizer, while keeping the input and output MF parameters constant
options = tunefisOptions('Method', 'particleswarm', 'OptimizationType', 'learning', 'NumMaxRules', 3^2);
options.MethodOptions.MaxIterations = 10;
rng('default')
fisout1 = tunefis(fisin, [], trnX, trnY, options); % carry out the tuning
fprintf('Training RMSE = %.3f \n', calculateRMSE(fisout1, trnX, trnY));
figure
plotfis(fisout1) % view the PSO-tuned FIS
[fisout1.Rules.Description]' % view all tuned Rules
plotActualAndExpectedResultsWithRMSE(fisout1, vldX, vldY) % calculate the RMSE to check accuracy
%% Stage 2: Use rule base from Stage 1 to tune parameters of input/output MFs & rules using GA optimizer
[in, out, rule] = getTunableSettings(fisout1);
options.OptimizationType = 'tuning';
options.Method = 'simulannealbnd';
options.MethodOptions.MaxIterations = 180;
rng('default')
fisout = tunefis(fisout1, [in; out; rule], trnX, trnY, options);
fprintf('Training RMSE = %.3f \n', calculateRMSE(fisout, trnX, trnY));
figure
plotfis(fisout) % view the Simulated Annealing-tuned FIS
[fisout.Rules.Description]' % view all tuned Rules
plotActualAndExpectedResultsWithRMSE(fisout, vldX, vldY); % calculate the RMSE to check accuracy
%% Local function #1
function plotActualAndExpectedResultsWithRMSE(fis, x, y)
% Calculate RMSE bewteen actual and expected results
[rmse, actY] = calculateRMSE(fis, x, y);
% Plot results
figure
subplot(2,1,1)
hold on
bar(actY)
bar(y)
bar(min(actY, y),'FaceColor', [0.5 0.5 0.5])
hold off
% axis([0 200 0 60])
xlabel("Validation input dataset index"),
ylabel("Amplitude")
legend(["Actual output" "Expected output" "Minimum of actual and expected values"], 'Location', 'NorthWest')
title("RMSE = " + num2str(rmse) + " amplitude")
subplot(2,1,2)
bar(actY-y)
xlabel("Validation input dataset index"), ylabel("Error")
title("Difference Between Actual and Expected Values")
end
%% Local function #2 (this one can be embedded in the local function #1)
function [rmse, actY] = calculateRMSE(fis, x, y)
% Specify options for FIS evaluation
persistent evalOptions
if isempty(evalOptions)
evalOptions = evalfisOptions("EmptyOutputFuzzySetMessage", "none", "NoRuleFiredMessage", "none", "OutOfRangeInputValueMessage", "none");
end
% Evaluate FIS
actY = evalfis(fis, x, evalOptions);
% Calculate RMSE
del = actY - y;
rmse = sqrt(mean(del.^2)); % the rmse() function was introduced in R2022b
% See https://www.mathworks.com/help/matlab/ref/rmse.html
end
0 Commenti
Vedere anche
Categorie
Scopri di più su Fuzzy Inference System Tuning in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!