├── .gitattributes ├── IrisTesting.dat ├── IrisTraining.dat └── PSO_Train_ANFIS.m /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /IrisTesting.dat: -------------------------------------------------------------------------------- 1 | 4.60 3.10 1.50 0.20 1 2 | 5.00 3.60 1.40 0.20 1 3 | 5.40 3.90 1.70 0.40 1 4 | 4.60 3.40 1.40 0.30 1 5 | 5.40 3.90 1.30 0.40 1 6 | 5.10 3.50 1.40 0.30 1 7 | 5.00 3.20 1.20 0.20 1 8 | 5.50 3.50 1.30 0.20 1 9 | 4.90 3.10 1.50 0.10 1 10 | 4.40 3.00 1.30 0.20 1 11 | 5.10 3.40 1.50 0.20 1 12 | 5.00 3.50 1.30 0.30 1 13 | 4.50 2.30 1.30 0.30 1 14 | 5.10 3.80 1.90 0.40 1 15 | 4.80 3.00 1.40 0.30 1 16 | 5.10 3.80 1.60 0.20 1 17 | 4.60 3.20 1.40 0.20 1 18 | 5.30 3.70 1.50 0.20 1 19 | 5.00 3.30 1.40 0.20 1 20 | 7.00 3.20 4.70 1.40 2 21 | 6.40 3.20 4.50 1.50 2 22 | 6.90 3.10 4.90 1.50 2 23 | 5.50 2.30 4.00 1.30 2 24 | 5.70 2.80 4.50 1.30 2 25 | 6.30 3.30 4.70 1.60 2 26 | 5.60 2.70 4.20 1.30 2 27 | 5.70 3.00 4.20 1.20 2 28 | 5.70 2.90 4.20 1.30 2 29 | 6.20 2.90 4.30 1.30 2 30 | 5.10 2.50 3.00 1.10 2 31 | 5.70 2.80 4.10 1.30 2 32 | 6.30 3.30 6.00 2.50 3 33 | 5.80 2.70 5.10 1.90 3 34 | 7.10 3.00 5.90 2.10 3 35 | 6.30 2.90 5.60 1.80 3 36 | 6.50 3.00 5.80 2.20 3 37 | 6.50 3.00 5.20 2.00 3 38 | 6.20 3.40 5.40 2.30 3 39 | 5.90 3.00 5.10 1.80 3 40 | 4.70 3.20 1.30 0.20 1 41 | 4.90 3.10 1.50 0.10 1 42 | 5.80 4.00 1.20 0.20 1 43 | 4.40 3.20 1.30 0.20 1 44 | 5.00 3.50 1.60 0.60 1 45 | 6.50 2.80 4.60 1.50 2 46 | 6.40 2.80 5.60 2.20 3 47 | 6.30 2.80 5.10 1.50 3 48 | 6.10 2.60 5.60 1.40 3 49 | 7.70 3.00 6.10 2.30 3 50 | 6.30 3.40 5.60 2.40 3 -------------------------------------------------------------------------------- /IrisTraining.dat: -------------------------------------------------------------------------------- 1 | 5.10 3.50 1.40 0.20 1 2 | 4.90 3.00 1.40 0.20 1 3 | 5.70 3.80 1.70 0.30 1 4 | 5.10 3.80 1.50 0.30 1 5 | 5.40 3.40 1.70 0.20 1 6 | 5.10 3.70 1.50 0.40 1 7 | 4.60 3.60 1.00 0.20 1 8 | 5.10 3.30 1.70 0.50 1 9 | 4.80 3.40 1.90 0.20 1 10 | 5.00 3.00 1.60 0.20 1 11 | 5.00 3.40 1.60 0.40 1 12 | 5.20 3.50 1.50 0.20 1 13 | 5.20 3.40 1.40 0.20 1 14 | 4.70 3.20 1.60 0.20 1 15 | 4.80 3.10 1.60 0.20 1 16 | 5.40 3.40 1.50 0.40 1 17 | 5.20 4.10 1.50 0.10 1 18 | 5.50 4.20 1.40 0.20 1 19 | 4.90 3.10 1.50 0.10 1 20 | 4.90 2.40 3.30 1.00 2 21 | 5.20 2.70 3.90 1.40 2 22 | 5.00 2.00 3.50 1.00 2 23 | 5.90 3.00 4.20 1.50 2 24 | 6.00 2.20 4.00 1.00 2 25 | 6.10 2.90 4.70 1.40 2 26 | 5.60 2.90 3.60 1.30 2 27 | 6.70 3.10 4.40 1.40 2 28 | 5.60 3.00 4.50 1.50 2 29 | 5.80 2.70 4.10 1.00 2 30 | 6.20 2.20 4.50 1.50 2 31 | 5.60 2.50 3.90 1.10 2 32 | 5.90 3.20 4.80 1.80 2 33 | 6.10 2.80 4.00 1.30 2 34 | 6.30 2.50 4.90 1.50 2 35 | 6.10 2.80 4.70 1.20 2 36 | 6.40 2.90 4.30 1.30 2 37 | 6.60 3.00 4.40 1.40 2 38 | 6.70 3.00 5.00 1.70 2 39 | 5.70 2.60 3.50 1.00 2 40 | 5.50 2.40 3.80 1.10 2 41 | 5.50 2.40 3.70 1.00 2 42 | 5.80 2.70 3.90 1.20 2 43 | 6.00 2.70 5.10 1.60 2 44 | 5.40 3.00 4.50 1.50 2 45 | 6.70 3.10 4.70 1.50 2 46 | 6.30 2.30 4.40 1.30 2 47 | 5.60 3.00 4.10 1.30 2 48 | 5.50 2.50 4.00 1.30 2 49 | 5.50 2.60 4.40 1.20 2 50 | 6.10 3.00 4.60 1.40 2 51 | 5.80 2.60 4.00 1.20 2 52 | 5.00 2.30 3.30 1.00 2 53 | 7.60 3.00 6.60 2.10 3 54 | 4.90 2.50 4.50 1.70 3 55 | 7.30 2.90 6.30 1.80 3 56 | 6.70 2.50 5.80 1.80 3 57 | 7.20 3.60 6.10 2.50 3 58 | 6.50 3.20 5.10 2.00 3 59 | 6.40 2.70 5.30 1.90 3 60 | 6.80 3.00 5.50 2.10 3 61 | 5.70 2.50 5.00 2.00 3 62 | 5.80 2.80 5.10 2.40 3 63 | 6.40 3.20 5.30 2.30 3 64 | 7.70 3.80 6.70 2.20 3 65 | 5.60 2.80 4.90 2.00 3 66 | 7.70 2.80 6.70 2.00 3 67 | 6.30 2.70 4.90 1.80 3 68 | 6.70 3.30 5.70 2.10 3 69 | 7.20 3.20 6.00 1.80 3 70 | 6.10 3.00 4.90 1.80 3 71 | 6.40 2.80 5.60 2.10 3 72 | 7.20 3.00 5.80 1.60 3 73 | 7.40 2.80 6.10 1.90 3 74 | 7.90 3.80 6.40 2.00 3 75 | 6.00 3.00 4.80 1.80 3 76 | 6.90 3.10 5.40 2.10 3 77 | 6.70 3.10 5.60 2.40 3 78 | 6.90 3.10 5.10 2.30 3 79 | 5.80 2.70 5.10 1.90 3 80 | 6.80 3.20 5.90 2.30 3 81 | 6.60 2.90 4.60 1.30 2 82 | 6.80 2.80 4.80 1.40 2 83 | 6.00 2.90 4.50 1.50 2 84 | 6.00 3.40 4.50 1.60 2 85 | 6.50 3.00 5.50 1.80 3 86 | 7.70 2.60 6.90 2.30 3 87 | 6.00 2.20 5.00 1.50 3 88 | 6.90 3.20 5.70 2.30 3 89 | 6.20 2.80 4.80 1.80 3 90 | 6.40 3.10 5.50 1.80 3 91 | 6.70 3.30 5.70 2.50 3 92 | 6.70 3.00 5.20 2.30 3 93 | 6.30 2.50 5.00 1.90 3 94 | 4.40 2.90 1.40 0.20 1 95 | 5.40 3.70 1.50 0.20 1 96 | 4.80 3.40 1.60 0.20 1 97 | 4.80 3.00 1.40 0.10 1 98 | 5.00 3.40 1.50 0.20 1 99 | 4.30 3.00 1.10 0.10 1 100 | 5.70 4.40 1.50 0.40 1 -------------------------------------------------------------------------------- /PSO_Train_ANFIS.m: -------------------------------------------------------------------------------- 1 | % Author: KASHIF HUSSAIN, PhD. in Machine Learning and Metaheuristics, 2 | % Universiti Tun Hussein Onn Malaysia, Johor, Malaysia. 3 | % Email: usitsoft@hotmail.com 4 | % ANFIS trained by PSO for Iris Classification Problem 5 | % No. of inputs = 4, No. of output = 1, No. of MFs in each input = 2 6 | % Total Rules = 2^4=16 (Grid Partitioning) 7 | % Total PSO particles = 15 8 | % No. of parameters in each particle = 96 9 | % Maximum Iterations = 20 10 | % Error Tolerance = 0.05 11 | % Training Set = 100 12 | % Testing Set = 50 13 | 14 | function Main() 15 | close all 16 | clear all 17 | clc 18 | 19 | fid = fopen('IrisTraining.dat','r'); 20 | Training_data = textscan(fid, '%f%f%f%f%f'); 21 | SepalLength = Training_data{1}; % SepalLength 22 | SepalWidth = Training_data{2}; % SepalWidth 23 | PetalLength = Training_data{3}; % PetalLength 24 | PetalWidth = Training_data{4}; % PetalWidth 25 | Target = Training_data{5}/3; % Target = Class of Iris 26 | 27 | % Training ANFIS 28 | [TrainingMSE, bestParams, itr] = PSO_Train(SepalLength, SepalWidth, PetalLength, PetalWidth, Target); 29 | TrainingAcc = ((1 - TrainingMSE / 2) * 100); 30 | 31 | % Test ANFIS 32 | fid = fopen('IrisTesting.dat','r'); 33 | Training_data = textscan(fid, '%f%f%f%f%f'); 34 | SepalLength = Training_data{1}; % SepalLength 35 | SepalWidth = Training_data{2}; % SepalWidth 36 | PetalLength = Training_data{3}; % PetalLength 37 | PetalWidth = Training_data{4}; % PetalWidth 38 | Target = Training_data{5}/3; % Target = Class of Iris 39 | sse = 0; 40 | nans = 0; 41 | for t = 1: size(Target, 1) 42 | outputANFIS = ANFIS_GetOutput(bestParams, SepalLength(t), SepalWidth(t), PetalLength(t), PetalWidth(t)); 43 | se = (Target(t) - outputANFIS)^2; 44 | if isnan(se), 45 | nans = nans + 1; 46 | continue 47 | end 48 | sse = sse + se; % sum of squared error 49 | end 50 | TestingMSE = sse/(size(Target, 1)-nans); 51 | TestingAcc = ((1 - TestingMSE / 2) * 100); 52 | msgbox({'Iris', ... 53 | ['Training MSE: ',num2str(TrainingMSE),' Training Accuracy: ',num2str(TrainingAcc),'%'], ... 54 | ['Testing MSE: ',num2str(TestingMSE),' Testing Accuracy: ',num2str(TestingAcc),'%'], ... 55 | ['Iterations to converge: ',num2str(itr)] ... 56 | }); 57 | end 58 | 59 | function [TrainingMSE, bestParams, Iterations] = PSO_Train(SepalLength, SepalWidth, PetalLength, PetalWidth, Target) 60 | totalParam = 96; % 16*5 + 8*2 = 96 61 | maxIterations = 20; 62 | itr = 1; 63 | errTolerance = 0.05; 64 | 65 | % PSO initialization 66 | %-------------------------------------------------------- 67 | fbest = 1.0e+100; % Global best 68 | totalParticles = 15; % No. of particles 69 | Ub = 1 * ones(totalParticles,totalParam); 70 | Lb = -1 * ones(totalParticles,totalParam); 71 | 72 | minInertiaWeight = 0.4; 73 | maxInertiaWeight = 0.9; 74 | socialConst = 2; 75 | cognitiveConst = 2; 76 | velClampingFactor = 2; 77 | pbestval = 1.0e+100*ones(1,totalParticles); % Personal best values 78 | particles = init_Swarm(totalParticles, Lb(1,:), Ub(1,:), totalParam); 79 | velocity = init_Velocity(totalParticles, totalParam, velClampingFactor, Ub(1,:)); 80 | %-------------------------------------------------------- 81 | 82 | % Execute PSO 83 | %-------------------------------------------------------- 84 | while ((fbest > errTolerance) && (itr <= maxIterations)) 85 | for i=1:totalParticles 86 | sse = 0; 87 | nans = 0; 88 | for t = 1: size(Target, 1) 89 | outputANFIS = ANFIS_GetOutput(particles(i,:), SepalLength(t), SepalWidth(t), PetalLength(t), PetalWidth(t)); 90 | se = (Target(t) - outputANFIS)^2; % SE = Squared Error 91 | if isnan(se), 92 | nans = nans + 1; 93 | continue 94 | end 95 | sse = sse + se; % Sum of Squared Errors 96 | end 97 | fval = sse/(size(Target, 1)-nans); % MSE = Mean Squared Error 98 | 99 | % Personal Best 100 | if isnan(fval) || fval<=pbestval(i), 101 | pbest(i,:) = particles(i,:); 102 | pbestval(i) = fval; 103 | end 104 | 105 | % Global best 106 | if fval<=fbest, 107 | gbest = particles(i,:); 108 | fbest=fval; 109 | end 110 | end 111 | 112 | % update velocity 113 | w=((maxIterations - itr)*(maxInertiaWeight - minInertiaWeight))/(maxIterations-1) + minInertiaWeight; 114 | velocity = pso_velocity(totalParticles, totalParam, velocity, gbest, pbest, particles, w, socialConst, cognitiveConst, velClampingFactor, Ub); 115 | % update position 116 | particles = pso_move(particles,velocity,Lb,Ub); 117 | itr = itr + 1; 118 | end 119 | %-------------------------------------------------------- 120 | 121 | TrainingMSE = fbest; 122 | bestParams = gbest; 123 | Iterations = itr-1; 124 | end 125 | 126 | function outputANFIS = ANFIS_GetOutput(params, SepalLength, SepalWidth, PetalLength, PetalWidth) 127 | numOfInputs = 4; 128 | numOfMFTerms = 2; 129 | numOfRules = numOfMFTerms^numOfInputs; 130 | 131 | %===================================================== 132 | % Layer 0: Input Layer, Input variable names 133 | %===================================================== 134 | % 1- SepalLength 135 | fis.input(1).name = ['input' 'SepalLength']; 136 | fis.input(1).range=[4.30 7.90]; 137 | fis.input(1).value= SepalLength; 138 | % 2- SepalWidth 139 | fis.input(2).name = ['input' 'SepalWidth']; 140 | fis.input(2).range=[2.00 4.40]; 141 | fis.input(2).value= SepalWidth; 142 | % 3- PetalLength 143 | fis.input(3).name = ['input' 'PetalLength']; 144 | fis.input(3).range=[1.00 6.90]; 145 | fis.input(3).value= PetalLength; 146 | % 4- PetalWidth 147 | fis.input(4).name = ['input' 'PetalWidth']; 148 | fis.input(4).range=[0.10 2.50]; 149 | fis.input(4).value= PetalWidth; 150 | 151 | %===================================================== 152 | % Layer 1: Membership functions layer 153 | %===================================================== 154 | weightIndex = 1; 155 | for i=1:numOfInputs 156 | for j=1:numOfMFTerms 157 | fis.input(i).mf(j).params = [params(weightIndex) params(weightIndex+1)]; 158 | fis.input(i).mf(j).MD = gmf(double(fis.input(i).value), double(fis.input(i).mf(j).params(2)), double(fis.input(i).mf(j).params(1))); 159 | weightIndex = weightIndex + 2; 160 | end 161 | end 162 | 163 | %===================================================== 164 | % Layer 2a: Product Layer, Initialize rules list. Prod(membership degrees of all inputs) 165 | %===================================================== 166 | fis.rule=[]; 167 | for i=1:numOfRules 168 | fis.rule(i).antecedent=zeros(1:2); % membership degrees of SepalLength, SepalWidth, PetalLength,...,xn 169 | fis.rule(i).prod=1; % product of all antecedents of each rule 170 | fis.rule(i).norm=0; % w' = weight of this rule 171 | fis.rule(i).consequent=0; % w'.f 172 | end 173 | 174 | % ========================================================== 175 | % Layer 2b: Grid Partitioning: Create all possible combinations of inputs and input terms 176 | % ========================================================== 177 | for i=1:numOfInputs 178 | if i