from ClonalG Immune Algorithm for Optimiation with Constraints by Djaghloul Mehdi
This algorithm deal with multimodal optimization problems under constraints.

Sol=jack_immune_projection(w0,pop_size,best_pop_size,clone_size_factor,KImmune_fin,Max_error,low_limit,High_limit,eva_plot)
function Sol=jack_immune_projection(w0,pop_size,best_pop_size,clone_size_factor,KImmune_fin,Max_error,low_limit,High_limit,eva_plot)
%--------------------------------------------------------------------------
% Djaghloul Mehdi ClonalG Immune Algorithm for Optimiation with Constraints
%--------------------------------------------------------------------------
%% sol=jack_immune_projection(w0,pop_size,best_pop_size,clone_size_factor,KImmune_fin,Max_error,low_limit,High_limit)
% w0 : Initial candidate solution,
% pop_size : Population  size,
% best_pop_size : Size ot the best selected antbodies,
% clone_size_factor : Colnale factor
% KImmune_fin : Maximum iterations number ,
% Max_error : Error tolerance for the solution,
% low_limit : Low limit
% High_limit : High limit
% eva_plot :  Best_Fitness plot
% You muste create a fitness function named 'fit_fun' 
%--------------------------------------------------------------------------


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%==========================================================================
%--------------------------------------------------------------------------
% I N I T I A L I S A T I O N 
%--------------------------------------------------------------------------

Nbre_var=length(w0);
Ini_Ab=[w0 (rand(Nbre_var,pop_size-1)-rand(Nbre_var,pop_size-1))];
Sol_tr = w0;
minF_evolution = [];
%==========================================================================

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%================================ ClonalG =================================
%-------------------------------------------------------------------------- 
% ClonalG 
%--------------------------------------------------------------------------
for kImmune=1:KImmune_fin
%--------------------------------------------------------------------------

    minFw0 = fit_fun(w0);
     if  minFw0 <= Max_error
         Sol=w0
         minFw0   
         break
     end

%-------------------------------- Selection -------------------------------         
%-----------%
% Selection % %First selection of efficient Ab by order
%-----------%
for ksel=1:pop_size
    Sel_Ab_cri(ksel)=fit_fun(Ini_Ab(:,ksel)); % fitness estimation for all the selected Ab
end
% Order
Min_fit_sel=[];
Min_fit=[];
for ksel=1:pop_size

    [sel index]=min(Sel_Ab_cri);
    
    Min_fit=[Min_fit ,sel];
    Min_fit_sel=[ Min_fit_sel ,Ini_Ab(:,index) ]; % croissent order
    
    if index <= (length(Sel_Ab_cri))
        Sel_Ab_cri(index)=Sel_Ab_cri(end);
    end
    
    Sel_Ab_cri=Sel_Ab_cri(1:end-1);
    
end

Sel_best_pop=Min_fit_sel(:,1:best_pop_size); % Re-selection based on best selected population factor 
Fit_best_pop=Min_fit(:,1:best_pop_size);

%---------------------------- *END* Selection -----------------------------

%--------------------------------------------------------------------------
%-----------------------%
% Fitness Normalisation % 
%-----------------------% 

Max_Fit=max(Fit_best_pop);
Min_Fit=min(Fit_best_pop);
Fit_best_pop=Fit_best_pop/Max_Fit;

%------------------------- *END* Fitness Normalisation --------------------

%--------------------------------------------------------------------------
%---------%
% Clones % Best Ab clone 
%---------%     
Nc=[];
for kclone=1:best_pop_size
    
%-------------------------------- Maturate --------------------------------
%----------%
% Maturate % 
%----------%  

%    maturat_rate=0.05*exp(-0.01*Fit_best_pop(kclone));% PID mliha (0.001,-0.01)
     maturat_rate=(Fit_best_pop(kclone)/50)*exp(-0.01*Fit_best_pop(kclone));% PID mliha (0.001,-0.01)
    
    %clone_best_pop= Sel_best_pop(:,kclone)+ maturat_rate * (rand(Nbre_var,1)-rand(Nbre_var,1));
    %clone_best_pop= Sel_best_pop(:,kclone)+ maturat_rate * (rand(Nbre_var,1)-rand(Nbre_var,1))*Min_Fit;%);%
     clone_best_pop= Sel_best_pop(:,kclone)+ maturat_rate * 1 *(rand(Nbre_var,1)-rand(Nbre_var,1))*(Min_Fit/Max_error);%);% Mliha PID
    
%----------------------------- *END* Maturate ----------------------------- 

%------------------------------- Projection -------------------------------
%-------------%
% Projection  % %projection of mutated Ab in limited interval for every element of the antibody 
%-------------%

    for j=1:Nbre_var
        
        if clone_best_pop(j)> High_limit(j)
            
            Rest = mod((abs(clone_best_pop(j)-High_limit(j))),(High_limit(j)-low_limit(j)));
            
            if mod(floor((abs(clone_best_pop(j)-High_limit(j)))/(High_limit(j)-low_limit(j))),2) == 0
            
                clone_best_pop(j)=low_limit(j) + Rest;
                
            else
                clone_best_pop(j)=High_limit(j) - Rest;
            end
        elseif clone_best_pop(j)< low_limit(j)     
            
            Rest = mod((abs(clone_best_pop(j)-low_limit(j))),(High_limit(j)-low_limit(j)));
            
            if mod(floor((abs(clone_best_pop(j)-low_limit(j)))/(High_limit(j)-low_limit(j))),2) == 0
            
                clone_best_pop(j)=low_limit(j) + Rest;
                
            else
                clone_best_pop(j)=High_limit(j) - Rest;
            end
                        
        end 
    
    end 
%------------------------------ *END* Projection --------------------------
  
   Nci_size=round((clone_size_factor*pop_size)/kclone); % Clone population size estimation 
   
   Nci=[];
   
   for kNci=1:Nci_size       
       Nci=[Nci clone_best_pop];
   end
   
   Nc=[Nc Nci];   % Total clone  
      
end 

%------------------------------- *END* Clones -----------------------------

%------------------------------- Re-Selection -----------------------------
%--------------%
% Re-Selection % %Re-selection of efficient Ab by re-order
%--------------%
[Nclin Nccol]=size(Nc);
for kNcsel=1:Nccol
    Sel_Nc_cri(kNcsel)=fit_fun(Nc(:,kNcsel));
end
% Order
Min_fit_Ncsel=[];

for kNcsel=1:length(Nc)

    [Ncsel index]=min(Sel_Nc_cri);
    
    %Min_fit=[Min_fit ,sel];
    Min_fit_Ncsel=[ Min_fit_Ncsel ,Nc(:,index) ];
    
    if index <= (length(Sel_Ab_cri))
        Sel_Nc_cri(index)=Sel_Nc_cri(end);
    end
    
    Sel_Nc_cri=Sel_Nc_cri(1:end-1);
    
end

Re_Sel_best_pop=[Min_fit_Ncsel(:,1:best_pop_size-1) Sol_tr];
%Re_Sel_best_pop=Min_fit_Ncsel(:,1:best_pop_size-1);

%--------------------------- *END* Re-Selection ---------------------------

%-------------------------------- Out Test --------------------------------
%----------%
% Out Test % 
%----------%

minFtr = fit_fun(Min_fit_Ncsel(:,1));
minF = fit_fun(Sol_tr);
    
    if minFtr <= minF
        
        Sol_tr = Min_fit_Ncsel(:,1);
        minF = minFtr;
     
    end
    
    minF;
    Sol_tr;
    
    minF_evolution = [minF_evolution minF];
%------------------------------%
% Best_Fitnes Convergence Plot %
%------------------------------%

    if (nargin > 8)&(eva_plot == 'on') 
        kImmune_evolution = 0:1:(kImmune-1);
        figure(1)
        semilogy(kImmune_evolution, minF_evolution)
        xlabel('Iteration Nbr')
        ylabel( 'Best Fitness')
        hold on   
    end
 pause (0.2)
    if  minF <= Max_error
        Sol=Sol_tr;
        break
    else

    Ini_Ab(:,1:best_pop_size)=Re_Sel_best_pop;
    %Ini_Ab(:,1:best_pop_size)=[w0 Re_Sel_best_pop];
    end
%------------------------------ *END* Out Test ----------------------------


%--------------------------------------------------------------------------
%--------------------------------------------------------------------------

kImmune;
%=============================== *END* clonalG ============================
% 
%--------------------------------------------------------------------------
end
close all
if (nargin > 8)&(eva_plot == 'on') 
    kImmune_evolution = 0:1:(kImmune-1);
    figure(1)
    semilogy(kImmune_evolution ,  minF_evolution)
    xlabel('Iteration Nbr')
    ylabel( 'Best Fitness')
end
 



Contact us at files@mathworks.com