by changing input of fuzzy system the output does not change

4 views (last 30 days)
Hello every one
I wrote a code in matlab to simulate a fuzzy design but when i use evalfis to see outputs by changing the inputs of fuzzy system outout does not change what should i do
here is my code :
clc ; clear all ;close all ;warning off
% Create a new fuzzy inference system
fis = newfis('TrafficLightController');
% Add inputs
fis = addvar(fis, 'input', 'ZaMir', [0 4]);
fis = addvar(fis, 'input', 'ZaMir_V2x', [0 4]);
fis = addvar(fis, 'input', 'MirGh', [2 6]);
fis = addvar(fis, 'input', 'MirGh_V2x', [0 5]);
fis = addvar(fis, 'input', 'GhMir', [2 6]);
fis = addvar(fis, 'input', 'GhMir_V2x', [0 6]);
% Add membership functions for inputs
for i = 1:6
fis = addmf(fis, 'input', i, 'Low', 'gaussmf', [0.5 1]);
fis = addmf(fis, 'input', i, 'Medium', 'gaussmf', [0.5 3]);
fis = addmf(fis, 'input', i, 'High', 'gaussmf', [0.5 5]);
end
% Add outputs
fis = addvar(fis, 'output', 'ZAMIR_light', [0 40]);
fis = addvar(fis, 'output', 'MirGh_light', [0 50]);
fis = addvar(fis, 'output', 'GhMir_light', [0 60]);
% Add membership functions for outputs
for i = 1:3
fis = addmf(fis, 'output', i, 'BigLow', 'gaussmf', [5 0]);
fis = addmf(fis, 'output', i, 'Low', 'gaussmf', [5 10]);
fis = addmf(fis, 'output', i, 'Medium', 'gaussmf', [5 20]);
fis = addmf(fis, 'output', i, 'High', 'gaussmf', [5 30]);
fis = addmf(fis, 'output', i, 'BigHigh', 'gaussmf', [5 40]);
end
rules_ZAMIR = [
1 1 0 0 0 0 1 0 0 1 1;
1 2 0 0 0 0 2 0 0 1 1;
1 3 0 0 0 0 3 0 0 1 1;
2 1 0 0 0 0 4 0 0 1 1;
2 2 0 0 0 0 4 0 0 1 1;
2 3 0 0 0 0 2 0 0 1 1;
3 1 0 0 0 0 5 0 0 1 1;
3 2 0 0 0 0 4 0 0 1 1;
3 3 0 0 0 0 4 0 0 1 1;
];
% Add rules to the system
fis = addrule(fis, rules_ZAMIR);
% Define rules for MirGh_light
rules_MirGh = [
0 0 1 1 0 0 0 1 0 1 1;
0 0 1 2 0 0 0 2 0 1 1;
0 0 1 3 0 0 0 3 0 1 1;
0 0 2 1 0 0 0 4 0 1 1;
0 0 2 2 0 0 0 4 0 1 1;
0 0 2 3 0 0 0 2 0 1 1;
0 0 3 1 0 0 0 5 0 1 1;
0 0 3 2 0 0 0 4 0 1 1;
0 0 3 3 0 0 0 4 0 1 1;
];
% Add rules to the system
fis = addrule(fis, rules_MirGh);
rules_GhMir = [
0 0 0 0 1 1 0 0 1 1 1;
0 0 0 0 1 1 0 0 2 1 1;
0 0 0 0 1 3 0 0 3 1 1;
0 0 0 0 2 1 0 0 4 1 1;
0 0 0 0 2 2 0 0 4 1 1;
0 0 0 0 2 3 0 0 2 1 1;
0 0 0 0 3 1 0 0 5 1 1;
0 0 0 0 3 2 0 0 4 1 1;
0 0 0 0 3 3 0 0 4 1 1;
];
fis = addrule(fis, rules_GhMir);
% Evaluate the fuzzy inference system
input_values = [2 1 4 1 4 3]; % Example input values
output_values = evalfis(input_values, fis);
fprintf('Green light time for Street A: %f seconds\n', output_values(1));
fprintf('Green light time for Street B: %f seconds\n', output_values(2));
fprintf('Green light time for Street C: %f seconds\n', output_values(3));

Answers (1)

Sam Chak
Sam Chak on 25 Jun 2023
The outputs change in the two tests below. More importantly, please check if the decision surfaces are expected as designed. In not, you need to reshape the fuzzy sets and retabulate the rules.
% Create a new fuzzy inference system
fis = newfis('TrafficLightController');
% Add inputs
fis = addvar(fis, 'input', 'ZaMir', [0 6]);
fis = addvar(fis, 'input', 'ZaMir_V2x', [0 6]);
fis = addvar(fis, 'input', 'MirGh', [0 6]);
fis = addvar(fis, 'input', 'MirGh_V2x', [0 6]);
fis = addvar(fis, 'input', 'GhMir', [0 6]);
fis = addvar(fis, 'input', 'GhMir_V2x', [0 6]);
% Add membership functions for inputs
for i = 1:6
fis = addmf(fis, 'input', i, 'L', 'gaussmf', [0.5 1]);
fis = addmf(fis, 'input', i, 'M', 'gaussmf', [0.5 3]);
fis = addmf(fis, 'input', i, 'H', 'gaussmf', [0.5 5]);
end
% Add outputs
fis = addvar(fis, 'output', 'ZAMIR_light', [0 40]);
fis = addvar(fis, 'output', 'MirGh_light', [0 50]);
fis = addvar(fis, 'output', 'GhMir_light', [0 60]);
% Add membership functions for outputs
for i = 1:3
fis = addmf(fis, 'output', i, 'BigLow', 'gaussmf', [5 0]);
fis = addmf(fis, 'output', i, 'Low', 'gaussmf', [5 10]);
fis = addmf(fis, 'output', i, 'Medium', 'gaussmf', [5 20]);
fis = addmf(fis, 'output', i, 'High', 'gaussmf', [5 30]);
fis = addmf(fis, 'output', i, 'BigHigh', 'gaussmf', [5 40]);
end
figure(1)
tiledlayout(2, 3);
nexttile
% subplot(2, 3, 1)
plotmf(fis, 'input', 1)
nexttile
plotmf(fis, 'input', 3)
nexttile
plotmf(fis, 'input', 5)
nexttile
plotmf(fis, 'input', 2)
nexttile
plotmf(fis, 'input', 4)
nexttile
plotmf(fis, 'input', 6)
rules_ZAMIR = [
1 1 0 0 0 0 1 0 0 1 1;
1 2 0 0 0 0 2 0 0 1 1;
1 3 0 0 0 0 3 0 0 1 1;
2 1 0 0 0 0 4 0 0 1 1;
2 2 0 0 0 0 4 0 0 1 1;
2 3 0 0 0 0 2 0 0 1 1;
3 1 0 0 0 0 5 0 0 1 1;
3 2 0 0 0 0 4 0 0 1 1;
3 3 0 0 0 0 4 0 0 1 1;
];
% Add rules to the system
fis = addrule(fis, rules_ZAMIR);
% Define rules for MirGh_light
rules_MirGh = [
0 0 1 1 0 0 0 1 0 1 1;
0 0 1 2 0 0 0 2 0 1 1;
0 0 1 3 0 0 0 3 0 1 1;
0 0 2 1 0 0 0 4 0 1 1;
0 0 2 2 0 0 0 4 0 1 1;
0 0 2 3 0 0 0 2 0 1 1;
0 0 3 1 0 0 0 5 0 1 1;
0 0 3 2 0 0 0 4 0 1 1;
0 0 3 3 0 0 0 4 0 1 1;
];
% Add rules to the system
fis = addrule(fis, rules_MirGh);
rules_GhMir = [
0 0 0 0 1 1 0 0 1 1 1;
0 0 0 0 1 1 0 0 2 1 1;
0 0 0 0 1 3 0 0 3 1 1;
0 0 0 0 2 1 0 0 4 1 1;
0 0 0 0 2 2 0 0 4 1 1;
0 0 0 0 2 3 0 0 2 1 1;
0 0 0 0 3 1 0 0 5 1 1;
0 0 0 0 3 2 0 0 4 1 1;
0 0 0 0 3 3 0 0 4 1 1;
];
fis = addrule(fis, rules_GhMir);
figure(2)
opt1 = gensurfOptions('OutputIndex', 1);
opt1.InputIndex = [1 2];
opt1.NumGridPoints = 41;
gensurf(fis, opt1)
figure(3)
opt2 = gensurfOptions('OutputIndex', 2);
opt2.InputIndex = [3 4];
opt2.NumGridPoints = 41;
gensurf(fis, opt2)
figure(4)
opt3 = gensurfOptions('OutputIndex', 3);
opt3.InputIndex = [5 6];
opt3.NumGridPoints = 41;
gensurf(fis, opt3)
% Test 1
input_values = [2 1 4 1 4 3]; % Example input values
output_values = evalfis(input_values, fis);
fprintf('Green light time for Street A: %f seconds\n', output_values(1));
Green light time for Street A: 20.876365 seconds
fprintf('Green light time for Street B: %f seconds\n', output_values(2));
Green light time for Street B: 34.014731 seconds
fprintf('Green light time for Street C: %f seconds\n', output_values(3));
Green light time for Street C: 30.000000 seconds
% Test 2
input_values = [2 4 3 3 4 2]; % Example input values
output_values = evalfis(input_values, fis);
fprintf('Green light time for Street A: %f seconds\n', output_values(1));
Green light time for Street A: 20.000000 seconds
fprintf('Green light time for Street B: %f seconds\n', output_values(2));
Green light time for Street B: 29.993243 seconds
fprintf('Green light time for Street C: %f seconds\n', output_values(3));
Green light time for Street C: 34.998340 seconds

Categories

Find more on Fuzzy Inference System Modeling 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!