How can I train ANFIS using Particle Swarm Optimization algorithm ?
Show older comments
I have my ANFIS developed from a dataset, I want to optimize it using PSO algorithm, please I need someone that will help me through. Please I need guidance, codes or proposed tutorials that I will easily learn how to optimize the ANFIS with PSO.
2 Comments
Sam Chak
on 27 Sep 2023
Hi @Ahmad
If I remember correctly, the Adaptive Network component of ANFIS has the capability to minimize the prescribed error measure using the hybrid learning method: Gradient Descent + Least Square Estimation. What role does the PSO play when you mention 'optimizing the ANFIS with PSO'? Are you suggesting replacing the optimizing 'AN' part with 'PSO', effectively creating 'PSOFIS'?
Ahmad
on 27 Sep 2023
Accepted Answer
More Answers (2)
Hi @Ahmad
To achieve a lower prescribed error measure, you can manually increase the number of Membership Functions until the desired error measure is reached. Naturally, I believe that PSO (Particle Swarm Optimization) can be used to find the optimal value for this parameter. However, since this is a relatively simple problem, manual tuning or tuning through iteration can also produce the desired result quickly, as demonstrated in the following example.
x = (0:0.1:10)';
y = sin(2*x)./exp(x/5);
% settings for FIS 1
genOpt1 = genfisOptions('GridPartition');
genOpt1.NumMembershipFunctions = 5; % <-- initial number of MFs
genOpt1.InputMembershipFunctionType = 'gaussmf';
inFIS1 = genfis(x, y, genOpt1);
opt = anfisOptions('InitialFIS', inFIS1);
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;
% compute RMSE of FIS 1
outFIS1 = anfis([x y], opt);
yFIS1 = evalfis(outFIS1, x);
Error1 = rmse(yFIS1, y)
% settings for FIS 2
genOpt2 = genfisOptions('GridPartition');
genOpt2.NumMembershipFunctions = 25; % <-- number of MFs is increased 5x
genOpt2.InputMembershipFunctionType = 'gaussmf';
inFIS2 = genfis(x, y, genOpt2);
opt = anfisOptions('InitialFIS', inFIS2);
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;
% compute RMSE of FIS 2
outFIS2 = anfis([x y], opt);
yFIS2 = evalfis(outFIS2, x);
Error2 = rmse(yFIS2, y)
% plots
plot(x, abs(y - yFIS1), x, abs(y - yFIS2)), grid on
xlabel('x')
ylabel('Absolute Error')
legend('FIS 1', 'FIS 2')
7 Comments
Ahmad
on 9 Oct 2023
Sam Chak
on 9 Oct 2023
Hi @Ahmad
Thank you for reaching out and seeking help with your PSO code. I'm happy to help you right here on the forum whenever I feel free. Please feel free to describe the issue you're facing.
I also understand that you're doing research as a student, and I encourage you to share your problem openly on the forum. That way, not only can other experts help you, but others in the forum facing the same issue can also benefit from the discussion.
If you have any specific questions or issues, don't hesitate to ask.
Sam Chak
on 10 Oct 2023
@Ahmad, Your developer's PSO code is fairly sophisticated, especially when you embed 'anfis()' into it, instead of calling 'pso()' to optimize the FIS. Anyway, if you disable 'setMembershipFunctionsCustom()', the code works. Do you know what the function customize when you developed this code?
Ahmad
on 10 Oct 2023
Sam Chak
on 10 Oct 2023
I commented out setMembershipFunctionsCustom(), and it works on my data. Honestly, I don't know how your user-supplied PSO code works. The 'anfis()' function itself has a built-in hybrid optimization algorithm, 'Gradient Descent + Least Square Estimation'. If you want me to investigate the data training issue using anfis, please attach the data 'hearts.csv', provided that the patient data is anonymized.
Ahmad
on 11 Oct 2023
Hi @Ahmad
For the new data "heart_dataset.csv" with 13 inputs to 1 output, the Subtractive Clustering method will generate 209 rules because each fuzzy input has 209 membership functions. While the number of rules may seem daunting, if the Grid Partitioning method is used with the fewest number of MFs for each input, even more rules will be required—specifically, (2 MFs)^(13 Inputs) = 8192 rules. Nonetheless, with my proposed approach, the RMSE for predicting the 13-input Boolean function is ZERO (0). You can express the ANFIS-based Boolean function as follows:

where the firing strength of a rule k is given by
and the first-order conclusion is a linear function given by
with
to
are all constants.

% Observational Data
data = readtable('heart_dataset.csv');
X = table2array(data(:, 1:13)); % 13 Inputs of patient data
Y = table2array(data(:, 14)); % 1 Output (target)
data_Train = [X Y];
% Setting up the initial FIS using Subtractive Clustering method
genOpt = genfisOptions('SubtractiveClustering');
inFIS = genfis(X, Y, genOpt);
anOpt = anfisOptions('InitialFIS', inFIS, 'EpochNumber', 3);
% Training data with ANFIS
outFIS = anfis(data_Train, anOpt)
% Predict using trained FIS & display RMSE
Ypred1 = round(evalfis(outFIS, X));
rmse1 = sqrt(mean((Y - Ypred1).^2));
txt1 = sprintf('RMSE of the prediction is %.4f.', rmse1);
disp(txt1)
3 Comments
Ahmad
on 13 Oct 2023
Sam Chak
on 14 Oct 2023
Hi @Ahmad
I'm feeling a bit confused about the approach that has been taken. The code is roughly 80% identical to the solution, except for the fact that the data is divided in a biased manner into training and cross-validation sets. Additionally, the initial FIS is set up in an unguided manner using the Grid Partitioning method with the user-specified number of Membership Functions (MFs) as [4 3 2 2 4 3] for the six fuzzy inputs.
Since the problem of training the full dataset using ANFIS has been satisfactorily resolved, and you are continuing to pursue the training solution for partial data using PSO, I encourage you to create a New Question and specify the specific problem you are facing. If your PSO works flawlessly for other test problems like the Rosenbrock Function, it is likely not a coding issue but a mathematical formulation problem.
Please ensure that the title of your question is clear, for example: "How can I formulate the optimization problem to train a user-specified model using PSO?" The model can be ANFIS, Convolutional Neural Networks (CNN), or Support Vector Machines (SVM).
Things to consider:
It is still not clear which parameters in the internally-optimized ANFIS are intended to be optimized using PSO. There are many parameters to consider, such as the partitioning technique, number of MFs, input and output MF types, as well as other options specific to the partitioning techniques. Please identify these parameters.
If the mathematical modeling of the patients' heart conditions is unimportant to you, and you don't need to explain the nature or mechanism of the model set, then consider training the data using Neural Networks, where you can employ binary classifiers.
Please be reminded to share your user-developed PSO code for optimization purposes and the MATLAB-built-in ANFIS code for training purposes in the New Question.
Ahmad
on 14 Oct 2023
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!
