├── .gitattributes ├── .gitignore ├── README.md ├── example ├── X.txt ├── Y.txt ├── dataforZSCORE.mat ├── ffoa.m ├── ffoaRegression.m ├── fruitFlySwarmPopulationSize.m ├── iffoa.m ├── performance2.m ├── test1.m ├── test2.m ├── testNegtive.m ├── testZscore.m └── test_FFOA.m ├── ffoa.m └── iffoa.m /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | 49 | # scratch file 50 | *.asv -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Fruit Fly Optimization Algorithm 2 | The Fruit Fly Optimization Algotithm (FFOA) was first proposed by Wentsao Pan in this 3 | [paper](http://www.sciencedirect.com/science/article/pii/S0950705111001365). 4 | Refer to the paper for more details. 5 | -------------------------------------------------------------------------------- /example/X.txt: -------------------------------------------------------------------------------- 1 | No X1 X2 X3 X4 X5 Y 2 | 1 0.016 0.177 0.4208 1.038 0.022 0 3 | 2 1.0957 0.19 0.2224 0.816 0.933 1 4 | 3 0.2543 0.206 0.5264 0.4 0.015 1 5 | 4 1.2257 0.224 0.3272 1.05 1.049 1 6 | 5 0.3872 0.228 0.2256 0.98 0.998 1 7 | 6 1.6066 0.231 0.2832 1.054 1.009 1 8 | 7 1.1594 0.252 0.3344 0.946 0.987 0 9 | 8 0.3424 0.26 0.3408 0.196 0.126 1 10 | 9 0.8604 0.261 0.2616 0.994 0.996 0 11 | 10 0.5107 0.264 0.2352 0.888 0.004 1 12 | 11 0.8981 0.271 0.1536 0.688 0.857 1 13 | 12 0.0878 0.275 0.2072 0.732 0.908 1 14 | 13 0.2384 0.297 0.2336 0.036 0.099 0 15 | 14 0.8591 0.308 0.4208 0.132 1.031 1 16 | 15 0.5861 0.309 0.1656 0.836 0.896 1 17 | 16 0.6329 0.311 -0.24 -0.134 0.611 0 18 | 17 0.6173 0.311 0.1256 0.714 0.848 0 19 | 18 0.7811 0.326 0.2896 0.834 0.909 1 20 | 19 0.3937 0.329 0.2552 0.024 0.493 0 21 | 20 0.1211 0.055 0.3744 1.142 1.077 1 22 | -------------------------------------------------------------------------------- /example/Y.txt: -------------------------------------------------------------------------------- 1 | 2.3004 3.7374 2.5857 4.5432 3.1143 2 | 4.8273 4.4022 2.1431 3.8536 2.2954 3 | 3.2338 2.5213 1.5934 3.9610 3.0800 4 | 0.9335 2.8670 3.7588 2.2826 3.2200 5 | -------------------------------------------------------------------------------- /example/dataforZSCORE.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xziya/fruit-fly-optimization-algorithm/983af065d7f6a621f57f3c5dc0cb653e6daa31f1/example/dataforZSCORE.mat -------------------------------------------------------------------------------- /example/ffoa.m: -------------------------------------------------------------------------------- 1 | %FFOA fruit fly optimization algorithm. 2 | function [peak, route, yy] =ffoa(FUN, varargin) 3 | 4 | if ~mod(nargin, 2) 5 | error('MATLAB:narginchk:notEnoughInputs', ... 6 | 'I have no idea about this, you can guess it'); 7 | end 8 | 9 | % default parameters settings 10 | maxIterations = 100; 11 | popSize = 20; 12 | plotFlag = 0; 13 | 14 | for ind = 1:2:nargin-1 15 | switch lower(varargin{ind}) 16 | case 'maxiterations' 17 | maxIterations = varargin{ind + 1}; 18 | case 'popsize' 19 | popSize = varargin{ind + 1}; 20 | case 'minmax' 21 | if strcmp(varargin{ind + 1}, 'min') 22 | flagMinMax = 0; 23 | else 24 | flagMinMax = 1; 25 | end 26 | case 'plotflag' 27 | plotFlag = varargin{ind + 1}; 28 | otherwise 29 | error('The function don''t support this parameter'); 30 | end 31 | end 32 | if ~exist('flagMinMax', 'var') 33 | error('You must specify the parameter ''MinMax''') 34 | end 35 | 36 | % random initial fruit fly swarm location 37 | XAxis = 10*rand(); 38 | YAxis = 10*rand(); 39 | 40 | % set parameters and variables 41 | if flagMinMax 42 | smellBest = -inf; 43 | else 44 | smellBest = inf; 45 | end 46 | X = zeros(popSize, 1); 47 | Y = zeros(popSize, 1); 48 | D = zeros(popSize, 1); 49 | S = zeros(popSize, 1); 50 | smell = zeros(popSize, 1); 51 | yy = zeros(maxIterations, 1); 52 | XBest = yy; 53 | YBest = XBest; 54 | 55 | % interative optimization start 56 | for g = 1:maxIterations 57 | % give the random direction and distance for the search of food using 58 | % osphresis by an individual fruit fly. 59 | for i = 1:popSize 60 | X(i) = XAxis + 2*rand() - 1; 61 | Y(i) = YAxis + 2*rand() - 1; 62 | % since the food location cannot be known, the distance to the 63 | % origin is thus estimated first(Dist), then the smell 64 | % concentration judgment value(S) is calculated, and this value is 65 | % the reciprocal of distance. 66 | D(i) = (X(i)^2 + Y(i)^2)^0.5; 67 | S(i) = 1/D(i); 68 | % substitude smell concentration judgment value(S) into smell 69 | % concentration judgment function(or called Fitness function) so as 70 | % to find the smell concentration(smelli) of the individual 71 | % location of the fruit fly. 72 | smell(i) = FUN(S(i)); 73 | end 74 | % find out the fruit fly with maximal smell concentration (finding the 75 | % maximal value)among the fruit fly swarm. 76 | if flagMinMax 77 | [bestSmell, bestIndex] = max(smell); 78 | else 79 | [bestSmell, bestIndex] = min(smell); 80 | end 81 | % determine whether the smell concentration better than the previous 82 | % iteration of concertration, if yes then keep the best smell 83 | % concentration value and x,y coordinate, and at this moment, the fruit 84 | % fly swarm will use vision to fly towards that location. 85 | if flagMinMax 86 | if bestSmell > smellBest 87 | XAxis = X(bestIndex); 88 | YAxis = Y(bestIndex); 89 | smellBest = bestSmell; 90 | end 91 | else 92 | if bestSmell < smellBest 93 | XAxis = X(bestIndex); 94 | YAxis = Y(bestIndex); 95 | smellBest = bestSmell; 96 | end 97 | end 98 | yy(g) = smellBest; 99 | XBest(g) = XAxis; 100 | YBest(g) = YAxis; 101 | end 102 | peak = yy(end); 103 | route = [XBest, YBest]; 104 | 105 | if plotFlag 106 | % draw smell concentration of each iteration 107 | figure(1) 108 | plot(yy, 'LineWidth', 1.5); 109 | title('Optimization process', 'FontSize', 12); 110 | xlabel('Iteration Number', 'FontSize', 12), ylabel('Smell', 'FontSize', 12) 111 | figure(2), hold on 112 | axis([min(XBest)-5 max(XBest)+5 min(YBest)-5 max(YBest)+5]); 113 | h = animatedline; 114 | h.LineStyle = 'none'; h.Marker = '.'; h.Color = 'r'; 115 | for id = 1:maxIterations 116 | addpoints(h, XBest(id), YBest(id)); 117 | drawnow 118 | end 119 | plot(XBest, YBest, 'b-') 120 | text(XBest(end), YBest(end), ['(', num2str(XBest(end)), ', ',... 121 | num2str(YBest(end)), ')'], 'Interpreter', 'latex'); 122 | title('Fruit fly flying route', 'FontSize', 14) 123 | xlabel('XAxis'), ylabel('YAxis') 124 | end -------------------------------------------------------------------------------- /example/ffoaRegression.m: -------------------------------------------------------------------------------- 1 | % empty memory 2 | clc, clear 3 | 4 | w = [3; 1]; 5 | x = [ones(10,1), (1:10)']; 6 | y = x*w + 0.1*rand(10, 1); 7 | 8 | % random initial fruit fly swarm location 9 | XAxis = 10*rand(1,2) - 5; 10 | YAxis = 10*rand(1,2) - 5; 11 | maxIterations = 100; 12 | sizePop = 20; 13 | 14 | smellBest = inf; 15 | for it = 1:maxIterations 16 | % optimization started, use the sense of smell to find food 17 | for p = 1:sizePop 18 | % give the random direction and distance for the search of food 19 | % using osphresis by an individual fruit fly. 20 | X(p, :) = XAxis + 2*rand(1,2) - 1; 21 | Y(p, :) = YAxis + 2*rand(1,2) - 1; 22 | 23 | % calculating the distance from the origin. 24 | D(p, :) = (X(p, :).^2 + Y(p, :).^2).^0.5; 25 | S(p, :) = 1./D(p, :); 26 | a = S(p, :); 27 | yc = x*a'; 28 | yDelta = yc - y; 29 | smell(p) = (sum(yDelta.^2)/10)^0.5; 30 | end 31 | [bestSmell, bestIndex] = min(smell); 32 | 33 | % keep the best smell concentration value and x,y coordinate, and at 34 | % this moment, the fruit fly swarm will use vision to fly towards that 35 | % location. 36 | if bestSmell < smellBest 37 | XAxis = X(bestIndex, :); 38 | YAxis = Y(bestIndex, :); 39 | bestS = S(bestIndex, :); 40 | smellBest = bestSmell; 41 | end 42 | yy(it) = smellBest; 43 | XBest(it, :) = XAxis; 44 | YBest(it, :) = YAxis; 45 | end 46 | 47 | wEstimate = 1./sqrt((XBest(end, :).^2 + YBest(end, :).^2)) 48 | figure(1) 49 | plot(yy) 50 | title('Optimization process') 51 | xlabel('Iteration Number'), ylabel('RMSE') 52 | figure(2), hold on 53 | axis([min(XBest(:))-5 max(XBest(:))+5 ... 54 | min(YBest(:))-5 max(YBest(:))+5]); 55 | for id = 1:maxIterations 56 | plot(XBest(id, :), YBest(id, :), 'o'); 57 | drawnow; 58 | pause(0.05); 59 | end 60 | plot(XBest, YBest) 61 | text(XBest(end), YBest(end), {['(', num2str(XBest(end, 1)), ', ',... 62 | num2str(YBest(end, 1)), ')'], ['(', num2str(XBest(end, 2)), ', ',... 63 | num2str(YBest(end, 2)), ')']}, 'Interpreter', 'latex'); 64 | title('Fruit fly flying route', 'FontSize', 14) 65 | xlabel('XAxis'), ylabel('YAxis') -------------------------------------------------------------------------------- /example/fruitFlySwarmPopulationSize.m: -------------------------------------------------------------------------------- 1 | % test with different population of fruit fly swarm 2 | FUN = @(x)(-5 + x^2); 3 | figure(1), hold on, grid on 4 | i = 0; 5 | for popSize = [3 6 9] 6 | [peak, route] = ffoa(FUN, 'maxIterations', 20, 'popSize', popSize,... 7 | 'minmax', 'min'); 8 | i = i + 1; 9 | subplot(3, 1, i) 10 | plot(route(:, 1), route(:, 2), 'LineWidth', 1, 'LineStyle', '-', ... 11 | 'Marker', 'o', 'MarkerSize', 5, 'MarkerFaceColor', 'r'); 12 | xlabel('XAxis'), ylabel('YAxis') 13 | title(['Fruit fly flying route with population size = ', ... 14 | num2str(popSize)]); 15 | end 16 | hold off -------------------------------------------------------------------------------- /example/iffoa.m: -------------------------------------------------------------------------------- 1 | %FFOA fruit fly optimization algorithm. 2 | function [peak, route, yy] = iffoa(FUN, varargin) 3 | 4 | if ~mod(nargin, 2) 5 | error('MATLAB:narginchk:notEnoughInputs', ... 6 | 'I have no idea about this, you can guess it'); 7 | end 8 | 9 | maxIterations = 100; 10 | popSize = 20; 11 | plotFlag = 0; 12 | 13 | for ind = 1:2:nargin-1 14 | switch lower(varargin{ind}) 15 | case 'maxiterations' 16 | maxIterations = varargin{ind + 1}; 17 | case 'popsize' 18 | popSize = varargin{ind + 1}; 19 | case 'minmax' 20 | if strcmp(varargin{ind + 1}, 'min') 21 | flagMinMax = 0; 22 | else 23 | flagMinMax = 1; 24 | end 25 | case 'plotflag' 26 | plotFlag = varargin{ind + 1}; 27 | otherwise 28 | error('The function don''t support this parameter'); 29 | end 30 | end 31 | if ~exist('flagMinMax', 'var') 32 | error('You must specify the parameter ''MinMax''') 33 | end 34 | 35 | % random initial fruit fly swarm location 36 | XAxis = 10*rand() - 5; 37 | YAxis = 10*rand() - 5; 38 | 39 | % set parameters and variables 40 | if flagMinMax 41 | smellBest = -inf; 42 | else 43 | smellBest = inf; 44 | end 45 | X = zeros(popSize, 1); 46 | Y = zeros(popSize, 1); 47 | D = zeros(popSize, 1); 48 | S = zeros(popSize, 1); 49 | smell = zeros(popSize, 1); 50 | yy = zeros(maxIterations, 1); 51 | XBest = yy; 52 | YBest = XBest; 53 | 54 | % interative optimization start 55 | for g = 1:maxIterations 56 | % give the random direction and distance for the search of food using 57 | % osphresis by an individual fruit fly. 58 | for i = 1:popSize 59 | X(i) = XAxis + 2*rand() - 1; 60 | Y(i) = YAxis + 2*rand() - 1; 61 | % since the food location cannot be known, the distance to the 62 | % origin is thus estimated first(Dist), then the smell 63 | % concentration judgment value(S) is calculated, and this value is 64 | % the reciprocal of distance. 65 | D(i) = (X(i)^2 + Y(i)^2)^0.5; 66 | S(i) = 1/D(i); 67 | % substitude smell concentration judgment value(S) into smell 68 | % concentration judgment function(or called Fitness function) so as 69 | % to find the smell concentration(smelli) of the individual 70 | % location of the fruit fly. 71 | if (X(i) < 0 && Y(i) > 0) || (X(i) > 0 && Y(i) < 0) 72 | S(i) = -S(i); 73 | end 74 | smell(i) = FUN(S(i)); 75 | end 76 | % find out the fruit fly with maximal smell concentration (finding the 77 | % maximal value)among the fruit fly swarm. 78 | if flagMinMax 79 | [bestSmell, bestIndex] = max(smell); 80 | else 81 | [bestSmell, bestIndex] = min(smell); 82 | end 83 | % determine whether the smell concentration better than the previous 84 | % iteration of concertration, if yes then keep the best smell 85 | % concentration value and x,y coordinate, and at this moment, the fruit 86 | % fly swarm will use vision to fly towards that location. 87 | if flagMinMax 88 | if bestSmell > smellBest 89 | XAxis = X(bestIndex); 90 | YAxis = Y(bestIndex); 91 | smellBest = bestSmell; 92 | end 93 | else 94 | if bestSmell < smellBest 95 | XAxis = X(bestIndex); 96 | YAxis = Y(bestIndex); 97 | smellBest = bestSmell; 98 | end 99 | end 100 | yy(g) = smellBest; 101 | XBest(g) = XAxis; 102 | YBest(g) = YAxis; 103 | end 104 | peak = yy(end); 105 | route = [XBest, YBest]; 106 | 107 | if plotFlag 108 | % draw smell concentration of each iteration 109 | figure(1) 110 | plot(yy, 'LineWidth', 1.5); 111 | title('Optimization process', 'FontSize', 12); 112 | xlabel('Iteration Number', 'FontSize', 12), ylabel('Smell', 'FontSize', 12) 113 | figure(2), hold on 114 | axis([min(XBest)-5 max(XBest)+5 min(YBest)-5 max(YBest)+5]); 115 | h = animatedline; 116 | h.LineStyle = 'none'; h.Marker = '.'; h.Color = 'r'; 117 | for id = 1:maxIterations 118 | addpoints(h, XBest(id), YBest(id)); 119 | drawnow 120 | end 121 | plot(XBest, YBest, 'b-') 122 | text(XBest(end), YBest(end), ['(', num2str(XBest(end)), ', ',... 123 | num2str(YBest(end)), ')'], 'Interpreter', 'latex'); 124 | title('Fruit fly flying route', 'FontSize', 14) 125 | xlabel('XAxis'), ylabel('YAxis') 126 | end -------------------------------------------------------------------------------- /example/performance2.m: -------------------------------------------------------------------------------- 1 | FUN11 = @(x)(-5 + (x+3)^2); 2 | [peak1, route1, yy1] = ffoa(FUN11, 'maxIterations', 100, 'popSize', 20,... 3 | 'minmax', 'min', 'plotFlag', 0); 4 | [peak2, route2, yy2] = iffoa(FUN11, 'maxIterations', 100, 'popSize', 20,... 5 | 'minmax', 'min', 'plotFlag', 0); 6 | h = plot([yy1, yy2], 'LineWidth', 2); 7 | set(h(1), 'LineStyle', '--'); 8 | legend({'ffoa', 'iffoa'}, 'FontSize', 12) 9 | xlabel('Iteration Number'), ylabel('Smell') 10 | title('Optimization process') 11 | grid on -------------------------------------------------------------------------------- /example/test1.m: -------------------------------------------------------------------------------- 1 | % empty memory 2 | clc 3 | clear 4 | 5 | % random initial fruit fly swarm location 6 | XAxis = 10*rand(); 7 | YAxis = 10*rand(); 8 | 9 | % set parameters 10 | maxIterations = 100; 11 | sizePop = 5; 12 | smellBest = inf; 13 | 14 | % % optimization started, use the sense of smell to find food 15 | % for i = 1:sizePop 16 | % % give the random direction and distance for the search of food using 17 | % % osphresis by an individual fruit fly. 18 | % X(i) = XAxis + 2*rand() - 1; 19 | % Y(i) = YAxis + 2*rand() - 1; 20 | % % since the food location cannot be known, the distance to the origin 21 | % % is thus estimated first(Dist), then the smell concentration judgment 22 | % % value(S) is calculated, and this value is the reciprocal of distance. 23 | % D(i) = (X(i)^2 + Y(i)^2)^0.5; 24 | % S(i) = 1/D(i); 25 | % % substitude smell concentration judgment value(S) into smell 26 | % % concentration judgment function(or called Fitness function)so as to 27 | % % find the smell concentration(Smelli)of the individual location of the 28 | % % fruit fly. 29 | % smell(i) = -5 + S(i)^2; 30 | % end 31 | % % find out the fruit fly with maximal smell concentration(finding the 32 | % % maximal value)among the fruit fly swarm. 33 | % [bestSmell, bestIndex] = min(smell); 34 | % 35 | % % keep the best smell concentration value and x,y coordinate, and at this 36 | % % moment, the fruit fly swarm will use vision to fly towards that location. 37 | % XAxis = X(bestIndex); 38 | % YAxis = Y(bestIndex); 39 | % smellBest = bestSmell; 40 | 41 | % interative optimization start 42 | for g = 1:maxIterations 43 | % give the random direction and distance for the search of food using 44 | % osphresis by an individual fruit fly. 45 | for i = 1:sizePop 46 | X(i) = XAxis + 2*rand() - 1; 47 | Y(i) = YAxis + 2*rand() - 1; 48 | % since the food location cannot be known, the distance to the 49 | % origin is thus estimated first(Dist), then the smell 50 | % concentration judgment value(S) is calculated, and this value is 51 | % the reciprocal of distance. 52 | D(i) = (X(i)^2 + Y(i)^2)^0.5; 53 | S(i) = 1/D(i); 54 | % substitude smell concentration judgment value(S) into smell 55 | % concentration judgment function(or called Fitness function) so as 56 | % to find the smell concentration(smelli) of the individual 57 | % location of the fruit fly. 58 | smell(i) = -5 + S(i)^2; 59 | end 60 | % find out the fruit fly with maximal smell concentration (finding the 61 | % maximal value)among the fruit fly swarm. 62 | [bestSmell, bestIndex] = min(smell); 63 | % determine whether the smell concentration better than the previous 64 | % iteration of concertration, if yes then keep the best smell 65 | % concentration value and x,y coordinate, and at this moment, the fruit 66 | % fly swarm will use vision to fly towards that location. 67 | if bestSmell < smellBest 68 | XAxis = X(bestIndex); 69 | YAxis = Y(bestIndex); 70 | smellBest = bestSmell; 71 | end 72 | yy(g) = smellBest; 73 | XBest(g) = XAxis; 74 | YBest(g) = YAxis; 75 | figure(2), hold on 76 | axis([0 100 0 100]) 77 | plot(XBest(g), YBest(g), 'r.'); 78 | drawnow; 79 | pause(0.1); 80 | end 81 | 82 | % draw smell concentration of each iteration 83 | figure(1) 84 | plot(yy, 'LineWidth', 1.5); 85 | title('Optimization process', 'FontSize', 12); 86 | xlabel('Iteration Number', 'FontSize', 12), ylabel('Smell', 'FontSize', 12) 87 | figure(2) 88 | plot(XBest, YBest, 'b-') 89 | text(XBest(end), YBest(end), ['(', num2str(XBest(end)), ', ',... 90 | num2str(YBest(end)), ')'], 'Interpreter', 'latex'); 91 | title('Fruit fly flying route', 'FontSize', 14) 92 | xlabel('XAxis'), ylabel('YAxis') -------------------------------------------------------------------------------- /example/test2.m: -------------------------------------------------------------------------------- 1 | % test function 2 | clear, clc 3 | FUN = @(x)(sin(x - 2) + 0.01*x.^2); 4 | [peak, route] = ffoa(FUN, 'maxIterations', 100, 'popSize', 20,... 5 | 'minmax', 'min', 'plotFlag', 1); 6 | peak -------------------------------------------------------------------------------- /example/testNegtive.m: -------------------------------------------------------------------------------- 1 | FUN = @(x)(3 - (x+3)^2); 2 | [peak, route] = ffoa(FUN, 'maxIterations', 100, 'popSize', 20,... 3 | 'minmax', 'max', 'plotFlag', 1); -------------------------------------------------------------------------------- /example/testZscore.m: -------------------------------------------------------------------------------- 1 | % empty memory 2 | clc, clear 3 | load dataforZSCORE.mat 4 | [row, col] = size(TXY); 5 | set = row/5; 6 | row1 = row - set; 7 | tr = TXY(1:row1, 1:col - 1); 8 | t1 = TXY(1:row1, col); 9 | te = TXY(row1+1:end, 1:col - 1); 10 | t2 = TXY(row1+1:end, 1:col); 11 | 12 | % random initial fruit fly swarm location 13 | XAxis = 10*rand(1,5) - 5; 14 | YAxis = 10*rand(1,5) - 5; 15 | maxIterations = 100; 16 | sizePop = 20; 17 | 18 | smellBest = inf; 19 | for it = 1:maxIterations 20 | % optimization started, use the sense of smell to find food 21 | for p = 1:sizePop 22 | % give the random direction and distance for the search of food 23 | % using osphresis by an individual fruit fly. 24 | X(p, :) = XAxis + 2*rand(1,5) - 1; 25 | Y(p, :) = YAxis + 2*rand(1,5) - 1; 26 | 27 | % calculating the distance from the origin. 28 | D(p, :) = (X(p, :).^2 + Y(p, :).^2).^0.5; 29 | S(p, :) = 1./D(p, :); 30 | a = S(p, :); 31 | yc = tr*a'; 32 | y = yc - t1; 33 | smell(p) = (sum(y.^2)/row1)^0.5; 34 | end 35 | [bestSmell, bestIndex] = min(smell); 36 | 37 | % keep the best smell concentration value and x,y coordinate, and at 38 | % this moment, the fruit fly swarm will use vision to fly towards that 39 | % location. 40 | if bestSmell < smellBest 41 | XAxis = X(bestIndex, :); 42 | YAxis = Y(bestIndex, :); 43 | bestS = S(bestIndex, :); 44 | smellBest = bestSmell; 45 | end 46 | yy(it) = smellBest; 47 | XBest(it, :) = XAxis; 48 | YBest(it, :) = YAxis; 49 | end 50 | 51 | figure(1) 52 | plot(yy) 53 | title('Optimization process') 54 | xlabel('Iteration Number'), ylabel('RMSE') 55 | figure(2) 56 | plot(XBest, YBest, 'b.'); 57 | title('Fruit fly flying route') 58 | xlabel('XAxis'), ylabel('YAxis') -------------------------------------------------------------------------------- /example/test_FFOA.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear 3 | X_axis=10*rand(); 4 | Y_axis=10*rand(); 5 | % X_axis=0; 6 | % Y_axis=10; 7 | generations=10; 8 | population=20; 9 | for i=1:population 10 | X(i)=X_axis+2*rand()-1; 11 | Y(i)=Y_axis+2*rand()-1; 12 | D(i)=(X(i)^2+Y(i)^2)^0.5; 13 | S(i)=1/D(i); 14 | Smell(i)=-5+S(i)^2; 15 | end 16 | [bestSmell bestIndex]=min(Smell); 17 | X_axis=X(bestIndex); 18 | Y_axis=Y(bestIndex); 19 | Smellbest=bestSmell 20 | for g=1:generations 21 | for i=1:population 22 | X(i)=X_axis+2*rand()-1; 23 | Y(i)=Y_axis+2*rand()-1; 24 | D(i)=(X(i)^2+Y(i)^2)^0.5; 25 | S(i)=1/D(i); 26 | Smell(i)=-5+S(i)^2; 27 | end 28 | [bestSmell bestIndex]=min(Smell); 29 | if bestSmell>Smellbest 30 | X_axis=X(bestIndex); 31 | Y_axis=Y(bestIndex); 32 | Smellbest=bestSmell; 33 | end 34 | yy(g)=Smellbest 35 | Xbest(g)=X_axis; 36 | Ybest(g)=Y_axis; 37 | end 38 | %Smellbest 39 | % Xbest 40 | % Ybest 41 | % yy 42 | figure(1) 43 | plot(yy) 44 | title('Optimization process','fontsize',12) 45 | xlabel('Iteration Number','fontsize',12); 46 | ylabel('Smell','fontsize',12); 47 | figure(2) 48 | plot(Xbest,Ybest,'*') 49 | title('Fruit fly flying route','fontsize',14) 50 | xlabel('X-axis','fontsize',12); 51 | ylabel('Y-axis','fontsize',12); -------------------------------------------------------------------------------- /ffoa.m: -------------------------------------------------------------------------------- 1 | % FFOA fruit fly optimization algorithm. 2 | function [peak, route, yy] =ffoa(FUN, varargin) 3 | 4 | if ~mod(nargin, 2) 5 | error('MATLAB:narginchk:notEnoughInputs', ... 6 | 'I have no idea about this, you can guess it'); 7 | end 8 | 9 | % default parameters settings 10 | maxIterations = 100; 11 | popSize = 20; 12 | plotFlag = 0; 13 | 14 | for ind = 1:2:nargin-1 15 | switch lower(varargin{ind}) 16 | case 'maxiterations' 17 | maxIterations = varargin{ind + 1}; 18 | case 'popsize' 19 | popSize = varargin{ind + 1}; 20 | case 'minmax' 21 | if strcmp(varargin{ind + 1}, 'min') 22 | flagMinMax = 0; 23 | else 24 | flagMinMax = 1; 25 | end 26 | case 'plotflag' 27 | plotFlag = varargin{ind + 1}; 28 | otherwise 29 | error('The function don''t support this parameter'); 30 | end 31 | end 32 | if ~exist('flagMinMax', 'var') 33 | error('You must specify the parameter ''MinMax''') 34 | end 35 | 36 | % random initial fruit fly swarm location 37 | XAxis = 10*rand(); 38 | YAxis = 10*rand(); 39 | 40 | % set parameters and variables 41 | if flagMinMax 42 | smellBest = -inf; 43 | else 44 | smellBest = inf; 45 | end 46 | X = zeros(popSize, 1); 47 | Y = zeros(popSize, 1); 48 | D = zeros(popSize, 1); 49 | S = zeros(popSize, 1); 50 | smell = zeros(popSize, 1); 51 | yy = zeros(maxIterations, 1); 52 | XBest = yy; 53 | YBest = XBest; 54 | 55 | % interative optimization start 56 | for g = 1:maxIterations 57 | % give the random direction and distance for the search of food using 58 | % osphresis by an individual fruit fly. 59 | for i = 1:popSize 60 | X(i) = XAxis + 2*rand() - 1; 61 | Y(i) = YAxis + 2*rand() - 1; 62 | % since the food location cannot be known, the distance to the 63 | % origin is thus estimated first(Dist), then the smell 64 | % concentration judgment value(S) is calculated, and this value is 65 | % the reciprocal of distance. 66 | D(i) = (X(i)^2 + Y(i)^2)^0.5; 67 | S(i) = 1/D(i); 68 | % substitude smell concentration judgment value(S) into smell 69 | % concentration judgment function(or called Fitness function) so as 70 | % to find the smell concentration(smelli) of the individual 71 | % location of the fruit fly. 72 | smell(i) = FUN(S(i)); 73 | end 74 | % find out the fruit fly with maximal smell concentration (finding the 75 | % maximal value)among the fruit fly swarm. 76 | if flagMinMax 77 | [bestSmell, bestIndex] = max(smell); 78 | else 79 | [bestSmell, bestIndex] = min(smell); 80 | end 81 | % determine whether the smell concentration better than the previous 82 | % iteration of concertration, if yes then keep the best smell 83 | % concentration value and x,y coordinate, and at this moment, the fruit 84 | % fly swarm will use vision to fly towards that location. 85 | if flagMinMax 86 | if bestSmell > smellBest 87 | XAxis = X(bestIndex); 88 | YAxis = Y(bestIndex); 89 | smellBest = bestSmell; 90 | end 91 | else 92 | if bestSmell < smellBest 93 | XAxis = X(bestIndex); 94 | YAxis = Y(bestIndex); 95 | smellBest = bestSmell; 96 | end 97 | end 98 | yy(g) = smellBest; 99 | XBest(g) = XAxis; 100 | YBest(g) = YAxis; 101 | end 102 | peak = yy(end); 103 | route = [XBest, YBest]; 104 | 105 | if plotFlag 106 | % draw smell concentration of each iteration 107 | figure(1) 108 | plot(yy, 'LineWidth', 1.5); 109 | title('Optimization process', 'FontSize', 12); 110 | xlabel('Iteration Number', 'FontSize', 12), ylabel('Smell', 'FontSize', 12) 111 | figure(2), hold on 112 | axis([min(XBest)-5 max(XBest)+5 min(YBest)-5 max(YBest)+5]); 113 | h = animatedline; 114 | h.LineStyle = 'none'; h.Marker = '.'; h.Color = 'r'; 115 | for id = 1:maxIterations 116 | addpoints(h, XBest(id), YBest(id)); 117 | drawnow 118 | end 119 | plot(XBest, YBest, 'b-') 120 | text(XBest(end), YBest(end), ['(', num2str(XBest(end)), ', ',... 121 | num2str(YBest(end)), ')'], 'Interpreter', 'latex'); 122 | title('Fruit fly flying route', 'FontSize', 14) 123 | xlabel('XAxis'), ylabel('YAxis') 124 | end -------------------------------------------------------------------------------- /iffoa.m: -------------------------------------------------------------------------------- 1 | % FFOA fruit fly optimization algorithm. 2 | function [peak, route, yy] = iffoa(FUN, varargin) 3 | 4 | if ~mod(nargin, 2) 5 | error('MATLAB:narginchk:notEnoughInputs', ... 6 | 'I have no idea about this, you can guess it'); 7 | end 8 | 9 | maxIterations = 100; 10 | popSize = 20; 11 | plotFlag = 0; 12 | 13 | for ind = 1:2:nargin-1 14 | switch lower(varargin{ind}) 15 | case 'maxiterations' 16 | maxIterations = varargin{ind + 1}; 17 | case 'popsize' 18 | popSize = varargin{ind + 1}; 19 | case 'minmax' 20 | if strcmp(varargin{ind + 1}, 'min') 21 | flagMinMax = 0; 22 | else 23 | flagMinMax = 1; 24 | end 25 | case 'plotflag' 26 | plotFlag = varargin{ind + 1}; 27 | otherwise 28 | error('The function don''t support this parameter'); 29 | end 30 | end 31 | if ~exist('flagMinMax', 'var') 32 | error('You must specify the parameter ''MinMax''') 33 | end 34 | 35 | % random initial fruit fly swarm location 36 | XAxis = 10*rand() - 5; 37 | YAxis = 10*rand() - 5; 38 | 39 | % set parameters and variables 40 | if flagMinMax 41 | smellBest = -inf; 42 | else 43 | smellBest = inf; 44 | end 45 | X = zeros(popSize, 1); 46 | Y = zeros(popSize, 1); 47 | D = zeros(popSize, 1); 48 | S = zeros(popSize, 1); 49 | smell = zeros(popSize, 1); 50 | yy = zeros(maxIterations, 1); 51 | XBest = yy; 52 | YBest = XBest; 53 | 54 | % interative optimization start 55 | for g = 1:maxIterations 56 | % give the random direction and distance for the search of food using 57 | % osphresis by an individual fruit fly. 58 | for i = 1:popSize 59 | X(i) = XAxis + 2*rand() - 1; 60 | Y(i) = YAxis + 2*rand() - 1; 61 | % since the food location cannot be known, the distance to the 62 | % origin is thus estimated first(Dist), then the smell 63 | % concentration judgment value(S) is calculated, and this value is 64 | % the reciprocal of distance. 65 | D(i) = (X(i)^2 + Y(i)^2)^0.5; 66 | S(i) = 1/D(i); 67 | % substitude smell concentration judgment value(S) into smell 68 | % concentration judgment function(or called Fitness function) so as 69 | % to find the smell concentration(smelli) of the individual 70 | % location of the fruit fly. 71 | if (X(i) < 0 && Y(i) > 0) || (X(i) > 0 && Y(i) < 0) 72 | S(i) = -S(i); 73 | end 74 | smell(i) = FUN(S(i)); 75 | end 76 | % find out the fruit fly with maximal smell concentration (finding the 77 | % maximal value)among the fruit fly swarm. 78 | if flagMinMax 79 | [bestSmell, bestIndex] = max(smell); 80 | else 81 | [bestSmell, bestIndex] = min(smell); 82 | end 83 | % determine whether the smell concentration better than the previous 84 | % iteration of concertration, if yes then keep the best smell 85 | % concentration value and x,y coordinate, and at this moment, the fruit 86 | % fly swarm will use vision to fly towards that location. 87 | if flagMinMax 88 | if bestSmell > smellBest 89 | XAxis = X(bestIndex); 90 | YAxis = Y(bestIndex); 91 | smellBest = bestSmell; 92 | end 93 | else 94 | if bestSmell < smellBest 95 | XAxis = X(bestIndex); 96 | YAxis = Y(bestIndex); 97 | smellBest = bestSmell; 98 | end 99 | end 100 | yy(g) = smellBest; 101 | XBest(g) = XAxis; 102 | YBest(g) = YAxis; 103 | end 104 | peak = yy(end); 105 | route = [XBest, YBest]; 106 | 107 | if plotFlag 108 | % draw smell concentration of each iteration 109 | figure(1) 110 | plot(yy, 'LineWidth', 1.5); 111 | title('Optimization process', 'FontSize', 12); 112 | xlabel('Iteration Number', 'FontSize', 12), ylabel('Smell', 'FontSize', 12) 113 | figure(2), hold on 114 | axis([min(XBest)-5 max(XBest)+5 min(YBest)-5 max(YBest)+5]); 115 | h = animatedline; 116 | h.LineStyle = 'none'; h.Marker = '.'; h.Color = 'r'; 117 | for id = 1:maxIterations 118 | addpoints(h, XBest(id), YBest(id)); 119 | drawnow 120 | end 121 | plot(XBest, YBest, 'b-') 122 | text(XBest(end), YBest(end), ['(', num2str(XBest(end)), ', ',... 123 | num2str(YBest(end)), ')'], 'Interpreter', 'latex'); 124 | title('Fruit fly flying route', 'FontSize', 14) 125 | xlabel('XAxis'), ylabel('YAxis') 126 | end --------------------------------------------------------------------------------