Tune FIS with Training Data
4 views (last 30 days)
Show older comments
In the example contained in the Fuzzy logic user guide documentation by mathworks, Tune Fuzzy Inference System at the Command Line, page 225, I understand the code used for tuning the FIS, but i dont know how they come up with tunedfismpgprediction.mat . below is the sample code:
[data,name] = loadGasData;
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)'];
fisin = mamfis;
for i = 1:6
fisin = addInput(fisin,dataRange(i,:),'Name',name(i),'NumMFs',2);
end
fisin = addOutput(fisin,dataRange(7,:),'Name',name(7),'NumMFs',64);
figure
plotfis(fisin)
options = tunefisOptions('Method','particleswarm',...
'OptimizationType','learning', ...
'NumMaxRules',64);
options.MethodOptions.MaxIterations = 20;
rng('default')
runtunefis = false;
%% This is the stage where am confused, I dont know how they get tunedfismpgprediction.mat
if runtunefis
fisout1 = tunefis(fisin,[],trnX,trnY,options); %#ok
else
tunedfis = load('tunedfismpgprediction.mat');
fisout1 = tunedfis.fisout1;
fprintf('Training RMSE = %.3f MPG\n',calculateRMSE(fisout1,trnX,trnY));
end
plotfis(fisout1)
Accepted Answer
Sam Chak
on 25 Nov 2023
Edited: Sam Chak
on 25 Nov 2023
Hi @Ahmad
Long story short, the MATLAB data file 'tunedfismpgprediction.mat' contains 2 pre-trained FIS files because this is a two-stage tuning process. In the MATLAB Fuzzy Logic Toolbox, developers have already tuned the FIS so that you can directly load them to see the results. You only need those FIS files when the 'runtunefis' parameter is set to 'false'. See my annotations below. Took me more than an hour to find facts and prepare the annotated code.
You can read the article in this link for more details:
%% 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 Mamdani FIS for tuning
fisin = mamfis;
for i = 1:6
fisin = addInput(fisin, dataRange(i,:), 'Name', name(i), 'NumMFs', 2);
end
fisin = addOutput(fisin, dataRange(7,:), 'Name', name(7), 'NumMFs', 64);
figure
plotfis(fisin)
%% Setting
runtunefis = true; % set true if you really want to learn tuning FIS from scratch
% runtunefis = false; % set false to load pre-trained results if you can't wait 5 min
%% 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', 64);
options.MethodOptions.MaxIterations = 20;
if runtunefis
rng('default')
fisout1 = tunefis(fisin, [], trnX, trnY, options); % carry out the tuning
else % if set false previously, it loads the already tuned FIS
tunedfis = load('tunedfismpgprediction.mat'); % there are 2 FIS files
fisout1 = tunedfis.fisout1; % the PSO-tuned FIS in Stage 1 is named 'fisout1.fis'
fprintf('Training RMSE = %.3f MPG\n',calculateRMSE(fisout1, trnX, trnY));
end
figure
plotfis(fisout1) % view the PSO-tuned FIS
[fisout1.Rules.Description]' % view all tuned 64 Rules, if you like
plotActualAndExpectedResultsWithRMSE(fisout1, vldX, vldY) % calculate the RMSE to check accuracy
%% Stage 2: Use the rule base from Stage 1 to tune the parameters of the input/output MFs and rules using the Pattern Search optimizer
[in, out, rule] = getTunableSettings(fisout1);
options.OptimizationType = 'tuning';
options.Method = 'patternsearch';
options.MethodOptions.MaxIterations = 60;
options.MethodOptions.UseCompletePoll = true;
if runtunefis % if previously set true, it will tune Stage 1 FIS using Pattern Search optimizer
rng('default')
fisout = tunefis(fisout1, [in; out; rule], trnX, trnY, options);
else % if previously set false, it will retrieve the already tuned Stage 2 FIS
fisout = tunedfis.fisout; % the Pattern Search-tuned FIS in Stage 2 is named 'fisout.fis'
fprintf('Training RMSE = %.3f MPG\n',calculateRMSE(fisout, trnX, trnY));
end
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
5 Comments
More Answers (2)
Sam Chak
on 6 Dec 2023
You have the option to select one of the five tuning algorithms as shown below:
- "ga" — genetic algorithm
- "particleswarm" — particle swarm
- "patternsearch" — pattern search
- "simulannealbnd" — simulated annealing algorithm
- "anfis" — adaptive neuro-fuzzy
Note that the first four tuning algorithms require the Global Optimization Toolbox, while the "anfis" method is a built-in algorithm in the Fuzzy Logic Toolbox.
In the following code, the "anfis" method is used to learn the rule base in Stage 1, and the result is employed in Stage 2 to tune the parameters of the fuzzy system using the "ga" method.
%% 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 Mamdani FIS for tuning
fisin = mamfis;
for i = 1:6
fisin = addInput(fisin, dataRange(i,:), 'Name', name(i), 'NumMFs', 2);
end
fisin = addOutput(fisin, dataRange(7,:), 'Name', name(7), 'NumMFs', 64);
figure
plotfis(fisin)
%% Stage 1: Learn only the rule base of the FIS using ANFIS
options = tunefisOptions('Method', 'anfis', 'OptimizationType', 'learning', 'NumMaxRules', 64);
options.MethodOptions.MaxIterations = 20;
rng('default')
fisout1 = tunefis(fisin, [], 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
[fisout1.Rules.Description]' % 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
4 Comments
Michael Bamidele
on 28 May 2024
%% 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;
%comment out these two lines for the codes to run well
Walter Roberson
on 24 Nov 2023
After
if runtunefis
fisout1 = tunefis(fisin,[],trnX,trnY,options); %#ok
and before the else there is an implied
save('tunedfismpgprediction.mat', 'fisout1');
0 Comments
See Also
Categories
Find more on Fuzzy Inference System Tuning in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!