function [BestSolution BestCost] = TrainUsingICA_Fcn(data,k,d,n)
%% Imperialist Competitive Algorithm (CCA);
% A Socio Politically Inspired Optimization Strategy.
% 2008
% To use this code, you should only prepare your cost function and apply
% CCA to it. Please read the guide available in my home page.
%% Problem Statement
ProblemParams.CostFuncName = 'ICA_CostFunction_Fcn'; % You should state the name of your cost function here.
ProblemParams.NPar = d*k; % Number of optimization variables of your objective function. "NPar" is the dimention of the optimization problem.
ProblemParams.VarMin = ones(1,d*k) ; % Lower limit of the optimization parameters. You can state the limit in two ways. 1) 2),
ProblemParams.VarMax = ones(1,d*k); % Lower limit of the optimization parameters. You can state the limit in two ways. 1) 2)
CostFuncExtraParams.k = k;
CostFuncExtraParams.d = d;
CostFuncExtraParams.n = n;
CostFuncExtraParams.data = data;
ProblemParams.CostFuncExtraParams = CostFuncExtraParams;
% Modifying the size of VarMin and VarMax to have a general form
for i=1:d
t=data(:,i);
for j=0:k-1
ProblemParams.VarMin(1,i+(j*d)) =min(t,[],1) ;
ProblemParams.VarMax(1,i+(j*d)) =max(t,[],1) ;
end
end
ProblemParams.SearchSpaceSize = ProblemParams.VarMax - ProblemParams.VarMin;
%% Algorithmic Parameter Setting
AlgorithmParams.NumOfCountries = 100; % Number of initial countries.
AlgorithmParams.NumOfInitialImperialists = 8; % Number of Initial Imperialists.
AlgorithmParams.NumOfAllColonies = AlgorithmParams.NumOfCountries - AlgorithmParams.NumOfInitialImperialists;
AlgorithmParams.NumOfDecades = 500;
AlgorithmParams.RevolutionRate = 0.3; % Revolution is the process in which the socio-political characteristics of a country change suddenly.
AlgorithmParams.AssimilationCoefficient = 5; % In the original paper assimilation coefficient is shown by "beta".
AlgorithmParams.AssimilationAngleCoefficient = .5; % In the original paper assimilation angle coefficient is shown by "gama".
AlgorithmParams.Zeta = 0.05; % Total Cost of Empire = Cost of Imperialist + Zeta * mean(Cost of All Colonies);
AlgorithmParams.DampRatio = 0.99;
AlgorithmParams.StopIfJustOneEmpire = false; % Use "true" to stop the algorithm when just one empire is remaining. Use "false" to continue the algorithm.
AlgorithmParams.UnitingThreshold = 0.02; % The percent of Search Space Size, which enables the uniting process of two Empires.
%% Display Setting
DisplayParams.PlotEmpires = false; % "true" to plot. "false" to cancel ploting.
if DisplayParams.PlotEmpires
DisplayParams.EmpiresFigureHandle = figure('Name','Plot of Empires','NumberTitle','off');
DisplayParams.EmpiresAxisHandle = axes;
end
DisplayParams.PlotCost = false; % "true" to plot. "false"
if DisplayParams.PlotCost
DisplayParams.CostFigureHandle = figure('Name','Plot of Minimum and Mean Costs','NumberTitle','off');
DisplayParams.CostAxisHandle = axes;
end
ColorMatrix = [1 0 0 ; 0 1 0 ; 0 0 1 ; 1 1 0 ; 1 0 1 ; 0 1 1 ; 1 1 1 ;
0.5 0.5 0.5; 0 0.5 0.5 ; 0.5 0 0.5 ; 0.5 0.5 0 ; 0.5 0 0 ; 0 0.5 0 ; 0 0 0.5 ;
1 0.5 1 ; 0.1*[1 1 1]; 0.2*[1 1 1]; 0.3*[1 1 1]; 0.4*[1 1 1]; 0.5*[1 1 1]; 0.6*[1 1 1]];
DisplayParams.ColorMatrix = [ColorMatrix ; sqrt(ColorMatrix)];
DisplayParams.AxisMargin.Min = ProblemParams.VarMin;
DisplayParams.AxisMargin.Max = ProblemParams.VarMax;
%% Creation of Initial Empires
InitialCountries = GenerateNewCountry(AlgorithmParams.NumOfCountries , ProblemParams);
% Calculates the cost of each country. The less the cost is, the more is the power.
%if isempty(ProblemParams.CostFuncExtraParams)
% InitialCost = feval(ProblemParams.CostFuncName,InitialCountries);
%else
InitialCost = feval(ProblemParams.CostFuncName,InitialCountries,ProblemParams.CostFuncExtraParams);
%end
[InitialCost,SortInd] = sort(InitialCost); % Sort the cost in assending order. The best countries will be in higher places
InitialCountries = InitialCountries(SortInd,:); % Sort the population with respect to their cost.
Empires = CreateInitialEmpires(InitialCountries,InitialCost,AlgorithmParams, ProblemParams);
%% Main Loop
MinimumCost = repmat(nan,AlgorithmParams.NumOfDecades,1);
MeanCost = repmat(nan,AlgorithmParams.NumOfDecades,1);
if DisplayParams.PlotCost
axes(DisplayParams.CostAxisHandle);
if any(findall(0)==DisplayParams.CostFigureHandle)
h_MinCostPlot=plot(MinimumCost,'r','LineWidth',1.5,'YDataSource','MinimumCost');
hold on;
h_MeanCostPlot=plot(MeanCost,'k:','LineWidth',1.5,'YDataSource','MeanCost');
hold off;
pause(0.05);
end
end
for Decade = 1:AlgorithmParams.NumOfDecades
AlgorithmParams.RevolutionRate = AlgorithmParams.DampRatio * AlgorithmParams.RevolutionRate;
% Remained = AlgorithmParams.NumOfDecades - Decade
for ii = 1:numel(Empires)
%% Assimilation; Movement of Colonies Toward Imperialists (Assimilation Policy)
Empires(ii) = AssimilateColonies(Empires(ii),AlgorithmParams,ProblemParams);
%% Revolution; A Sudden Change in the Socio-Political Characteristics
Empires(ii) = RevolveColonies(Empires(ii),AlgorithmParams,ProblemParams);
%% New Cost Evaluation
if isempty(ProblemParams.CostFuncExtraParams)
Empires(ii).ColoniesCost = feval(ProblemParams.CostFuncName,Empires(ii).ColoniesPosition);
else
Empires(ii).ColoniesCost = feval(ProblemParams.CostFuncName,Empires(ii).ColoniesPosition,ProblemParams.CostFuncExtraParams);
end
%% Empire Possession (****** Power Possession, Empire Possession)
Empires(ii) = PossesEmpire(Empires(ii));
%% Computation of Total Cost for Empires
Empires(ii).TotalCost = Empires(ii).ImperialistCost + AlgorithmParams.Zeta * mean(Empires(ii).ColoniesCost);
end
%% Uniting Similiar Empires
Empires = UniteSimilarEmpires(Empires,AlgorithmParams,ProblemParams);
%% Imperialistic Competition
Empires = ImperialisticCompetition(Empires);
if numel(Empires) == 1 && AlgorithmParams.StopIfJustOneEmpire
break
end
%% Displaying the Results
DisplayEmpires(Empires,AlgorithmParams,ProblemParams,DisplayParams);
ImerialistCosts = [Empires.ImperialistCost];
MinimumCost(Decade) = min(ImerialistCosts);
MeanCost(Decade) = mean(ImerialistCosts);
if DisplayParams.PlotCost
refreshdata(h_MinCostPlot);
refreshdata(h_MeanCostPlot);
drawnow;
pause(0.01);
end
end
% End of Algorithm
BestCost = MinimumCost(end);
BestIndex = find(ImerialistCosts == min(ImerialistCosts)); BestIndex = BestIndex(1);
BestSolution = Empires(BestIndex).ImperialistPosition;