Tune FIS with Training Data
古いコメントを表示
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)
採用された回答
その他の回答 (2 件)
Sam Chak
2023 年 12 月 6 日
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 件のコメント
Ahmad
2023 年 12 月 6 日
Sam Chak
2023 年 12 月 6 日

Make the following changes, and it should work.
%% Stage 1: Learn only the rule base of the FIS using ANFIS
options = tunefisOptions('Method', 'anfis');
% options.MethodOptions.MaxIterations = 20; % comment out this line
Ahmad
2023 年 12 月 9 日
Michael Bamidele
2024 年 5 月 28 日
%% 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
2023 年 11 月 24 日
After
if runtunefis
fisout1 = tunefis(fisin,[],trnX,trnY,options); %#ok
and before the else there is an implied
save('tunedfismpgprediction.mat', 'fisout1');
カテゴリ
ヘルプ センター および File Exchange で Fuzzy Logic Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!