LCBO - A Life Choice Based Optimizer
The following working MATLAB code can be used to execute/test the benchmark functions optimization and for future application developments. Each of the below mentioned function need to be in a separate file having the name as described here. The main file called Main_Runner is required to be executed and this will draw services from other functions as well as long as all these are in the same folder with the given name.
%% File Main_Runner
% % Program to optimize 23 test functions%/
clear;close all;clc;
population = 30;
numberOfChances = 500; % Iteration of lifeChoices
numberOfRuns = 1; % number of independent trials
bestCost = zeros(23,numberOfRuns);
for functionNum = 1:23
[xMin,xMax,dimension,objF] = GetFunctionDetails(functionNum);
for currentRun = 1:numberOfRuns
[bestCost(functionNum,currentRun), bestChoice, convergenceCurve]...
= LCO (population, numberOfChances, dimension, xMin, xMax,...
objF);
end
mean(bestCost(functionNum,:))
end
%% File LCO - The main optimizer
function [bestCost, bestChoice, convergenceCurve] = LCO (population,...
numberOfChances, dimension, xMin, xMax, objF)
x = initialise(xMin, xMax, population, dimension);
cost = zeros(1,population);
for i = 1:population
cost(i) = objF(x(i,:));
end
[x, cost] = getSortedPopulationAndCost(x, cost);
bestCost = cost(1);
bestChoice = x(1,:);
convergenceCurve = zeros(1,numberOfChances);
convergenceCurve(1) = bestCost;
r1 = 2.35;
r2 = 0.875;
r3 = 0.70;
n = ceil(sqrt(population));
for currentChance = 2:numberOfChances
f1 = (currentChance-1)/(numberOfChances-1);
f2 = 1-f1;
xn = zeros(1,dimension);
for current = 2:population
z=rand();
if (z > r2)
for currentDim = 1:dimension
random = rand(n,1);
xn(1,currentDim) = sum(random.*x(1:n,currentDim))/n;
end
elseif (z > r3)
if(length(xMax)>1)
for k = 1:dimension
xn(1,k) = xMax(k)-(x(current,k)-xMin(k))*rand();
end
else
xn(1,:) = xMax-(x(current,:)-xMin)*rand();
end
else
betterDiff = f2*r1*(x(current-1,:) - x(current,:));
bestDiff = f1*r1*(x(1,:) - x(current,:));
for currentDim = 1:dimension
xn(currentDim) = x(current,currentDim)...
+ rand()*betterDiff(1,currentDim)...
+ rand()*bestDiff(1,currentDim);
end
end
if(prod(xn >= xMin) && prod(xn <= xMax))
currCost = objF(xn);
if (currCost < cost(current))
x(current,:) = xn;
cost(current) = currCost;
end
end
end
[x, cost] = getSortedPopulationAndCost(x, cost);
bestCost = cost(1);
bestChoice = x(1,:);
convergenceCurve(currentChance) = bestCost;
end
end
function x = initialise(xMin, xMax, population, dimension)
if (size(xMin,2) == 1)
xMin = repmat(xMin, population, dimension);
else
xMin = repmat(xMin, population, 1);
end
if (size(xMax,2) == 1)
xMax = repmat(xMax, population, dimension);
else
xMax = repmat(xMax, population, 1);
end
x = xMin + (xMax - xMin).*rand(population, dimension);
end
function [x, cost] = getSortedPopulationAndCost(x, cost)
[cost, index] = sort(cost, 'ascend');
x = x(index,:);
end
%% Function GetFunctionDetails
function [xMin,xMax,dimension,objF] = GetFunctionDetails(functionNum)
switch functionNum
case 1
objF = @F1;
xMin = -100;
xMax = 100;
dimension = 30;
case 2
objF = @F2;
xMin = -10;
xMax = 10;
dimension = 30;
case 3
objF = @F3;
xMin = -100;
xMax = 100;
dimension = 30;
case 4
objF = @F4;
xMin = -100;
xMax = 100;
dimension = 30;
case 5
objF = @F5;
xMin = -30;
xMax = 30;
dimension = 30;
case 6
objF = @F6;
xMin = -100;
xMax = 100;
dimension = 30;
case 7
objF = @F7;
xMin = -1.28;
xMax = 1.28;
dimension = 30;
case 8
objF = @F8;
xMin = -500;
xMax = 500;
dimension = 30;
case 9
objF = @F9;
xMin = -5.12;
xMax = 5.12;
dimension = 30;
case 10
objF = @F10;
xMin = -32;
xMax = 32;
dimension = 30;
case 11
objF = @F11;
xMin = -600;
xMax = 600;
dimension = 30;
case 12
objF = @F12;
xMin = -50;
xMax = 50;
dimension = 30;
case 13
objF = @F13;
xMin = -50;
xMax = 50;
dimension = 30;
case 14
objF = @F14;
xMin = -65.536;
xMax = 65.536;
dimension = 2;
case 15
objF = @F15;
xMin = -5;
xMax = 5;
dimension = 4;
case 16
objF = @F16;
xMin = -5;
xMax = 5;
dimension = 2;
case 17
objF = @F17;
xMin = [-5,0];
xMax = [10,15];
dimension = 2;
case 18
objF = @F18;
xMin = -2;
xMax = 2;
dimension = 2;
case 19
objF = @F19;
xMin = 0;
xMax = 1;
dimension = 3;
case 20
objF = @F20;
xMin = 0;
xMax = 1;
dimension = 6;
case 21
objF = @F21;
xMin = 0;
xMax = 10;
dimension = 4;
case 22
objF = @F22;
xMin = 0;
xMax = 10;
dimension = 4;
case 23
objF = @F23;
xMin = 0;
xMax = 10;
dimension = 4;
end
end
% F1 begins here
function cost = F1(x)
cost = sum(x.^2);
end
% F2 begins here
function cost = F2(x)
cost = sum(abs(x))+prod(abs(x));
end
% F3 begins here
function cost = F3(x)
dimension = size(x,2);
cost = 0;
for i = 1:dimension
cost = cost + sum(x(1:i))^2;
end
end
% F4 begins here
function cost = F4(x)
cost = max(abs(x));
end
% F5 begins here
function cost = F5(x)
dimension = size(x,2);
cost = sum(100*(x(2:dimension)-(x(1:dimension-1).^2))...
.^2+(x(1:dimension-1)-1).^2);
end
% F6 begins here
function cost = F6(x)
cost = sum(abs((x+.5)).^2);
end
% F7 begins here
function cost = F7(x)
dimension = size(x,2);
cost = sum([1:dimension].*(x.^4))+rand;
end
% F8 begins here
function cost = F8(x)
cost = sum(-x.*sin(sqrt(abs(x))));
end
% F9 begins here
function cost = F9(x)
dimension = size(x,2);
cost = sum(x.^2-10*cos(2*pi.*x))+10*dimension;
end
% F10 begins here
function cost = F10(x)
dimesnion = size(x,2);
cost = -20*exp(-.2*sqrt(sum(x.^2)/dimesnion))...
-exp(sum(cos(2*pi.*x))/dimesnion)+20+exp(1);
end
% F11 begins here
function cost = F11(x)
dimension = size(x,2);
cost = sum(x.^2)/4000-prod(cos(x./sqrt([1:dimension])))+1;
end
% F12 begins here
function cost = F12(x)
dimension = size(x,2);
cost = (pi/dimension)*(10*((sin(pi*(1+(x(1)+1)/4)))^2)...
+sum((((x(1:dimension-1)+1)./4).^2).*(1+10.*...
((sin(pi.*(1+(x(2:dimension)+1)./4)))).^2))+...
((x(dimension)+1)/4)^2)+sum(Ufun(x,10,100,4));
end
% F13 begins here
function cost = F13(x)
dimension = size(x,2);
cost = .1*((sin(3*pi*x(1)))^2+sum((x(1:dimension-1)-1).^2.*...
(1+(sin(3.*pi.*x(2:dimension))).^2))+((x(dimension)-1)^2)*...
(1+(sin(2*pi*x(dimension)))^2))+sum(Ufun(x,5,100,4));
end
% F14 begins here
function cost = F14(x)
aS = [-32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 ...
-32 -16 0 16 32; -32 -32 -32 -32 -32 -16 -16 -16 -16 -16 0 0 0 0 ...
0 16 16 16 16 16 32 32 32 32 32];
for j=1:25
bS(j) = sum((x'-aS(:,j)).^6);
end
cost = (1/500+sum(1./([1:25]+bS))).^(-1);
end
% F15 begins here
function cost = F15(x)
aK = [.1957 .1947 .1735 .16 .0844 .0627 .0456 .0342 .0323 .0235 .0246];
bK = [.25 .5 1 2 4 6 8 10 12 14 16];bK=1./bK;
cost = sum((aK-((x(1).*(bK.^2+x(2).*bK))./(bK.^2+x(3).*bK+x(4)))).^2);
end
% F16 begins here
function cost = F16(x)
cost = 4*(x(1)^2)-2.1*(x(1)^4)+(x(1)^6)/3+x(1)*x(2)-4*(x(2)^2)+...
4*(x(2)^4);
end
% F17 begins here
function cost = F17(x)
cost = (x(2)-(x(1)^2)*5.1/(4*(pi^2))+5/pi*x(1)-6)^2+10*(1-1/(8*pi))*...
cos(x(1))+10;
end
% F18 begins here
function cost = F18(x)
cost =(1+(x(1)+x(2)+1)^2*(19-14*x(1)+3*(x(1)^2)-14*x(2)+6*x(1)*x(2)+...
3*x(2)^2))*(30+(2*x(1)-3*x(2))^2*(18-32*x(1)+12*(x(1)^2)+48*...
x(2)-36*x(1)*x(2)+27*(x(2)^2)));
end
% F19 begins here
function cost = F19(x)
aH = [3 10 30;.1 10 35;3 10 30;.1 10 35];cH=[1 1.2 3 3.2];
pH = [.3689 .117 .2673;.4699 .4387 .747;.1091 .8732 .5547;.03815 ...
.5743 .8828];
cost = 0;
for i = 1:4
cost = cost-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));
end
end
% F20 begins here
function cost = F20(x)
aH = [10 3 17 3.5 1.7 8;.05 10 17 .1 8 14;3 3.5 1.7 10 17 8;17 8 ...
.05 10 .1 14];
cH = [1 1.2 3 3.2];
pH = [.1312 .1696 .5569 .0124 .8283 .5886;.2329 .4135 .8307 .3736 ...
.1004 .9991;.2348 .1415 .3522 .2883 .3047 .6650;.4047 .8828 ...
.8732 .5743 .1091 .0381];
cost = 0;
for i = 1:4
cost = cost-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));
end
end
% F21 begins here
function cost = F21(x)
aSH = [4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;8 ...
1 8 1;6 2 6 2;7 3.6 7 3.6];
cSH = [.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
cost = 0;
for i = 1:5
cost = cost-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end
% F22 begins here
function cost = F22(x)
aSH = [4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;...
8 1 8 1;6 2 6 2;7 3.6 7 3.6];
cSH = [.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
cost = 0;
for i = 1:7
cost = cost-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end
% F23 begins here
function cost = F23(x)
aSH = [4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;...
8 1 8 1;6 2 6 2;7 3.6 7 3.6];
cSH = [.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
cost = 0;
for i = 1:10
cost = cost-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end
function cost = Ufun(x,a,k,m)
cost = k.*((x-a).^m).*(x>a)+k.*((-x-a).^m).*(x<(-a));
end
%% Function Analysis
%% This may not be required is analysis is undesired
format long;
for functionNum = 1:23
fprintf('Analysis for function %d \n', functionNum);
bestBestCost(functionNum) = min(bestCost(functionNum,:));
worstBestCost(functionNum) = max(bestCost(functionNum,:));
meanBestCost(functionNum) = mean(bestCost(functionNum,:));
stdBestCost(functionNum) = std(bestCost(functionNum,:));
fprintf('Best = %f, Worst = %f, Mean = %f, Std = %f \n\n',...
bestBestCost(functionNum), worstBestCost(functionNum),...
meanBestCost(functionNum), stdBestCost(functionNum));
end