www.gusucode.com > GAVPai_Book_MathworksCntrlFileEx_May2019 > GAVPai_Book_MathworksCntrlFileEx_May2019/Chapter2_ESHOF_BealeFnOptmzn.m

    % Vijayalakshmi Pai G A, Metaheuristics for Portfolio Optimization, An
% Introduction using MATLAB, ISTE-Wiley, 2017.

% A naive demonstration of Evolution Strategy with Hall of Fame (ESHOF) for Beale
% function Optimization 
% Chapter 2  Sec.2.6 A

% Program coding:  Dr G A Vijayalakshmi Pai
% Note: Coding style has been kept naive and direct to favor novices in
% MATLAB. Users familiar with MATLAB are encouraged to improvise the code
%------------------------------------------------------------------------ 



clear all

% Set Evolution Strategy and Problem parameters 
popln_size = 100;              % population size 
chromosm_length = 2;           % chromosome length  
total_generations = 500;       % total generations

% mu is one third of poplulation size
mu = round ((1/3)*popln_size);

% lambda is two thirds of population size
lambda = popln_size - mu;          

% crossover rate
crossover_rate = 0.61; 

% mutation rate
mutation_rate = 0.01;             

% bounds 
bounds = [-4.5, 4.5];

% initialize  Hall of Fame
HOF_fitness = 9999;
hof_indx = 0;
HOF_chromosome = zeros(1, chromosm_length);

% generate random initial population
fix(clock)
initial_popln = bounds(1) + (bounds(2)-bounds(1))*rand(popln_size, chromosm_length); % generate initial popln within range [-4.5, 4.5]
next_gen_pool = initial_popln;
gen_indx =1;
t=1;

% while loop for generations begins
while (gen_indx <= total_generations)         
                                              
        % set parent population and compute fitness of parent population
        parent_popln = next_gen_pool;
        parent_popln_fitness = comp_fitness_Beale(parent_popln);
        
        % determine the best fit parents (mu) to be passed on to the next
        % generation
        [parent_popln_fitness_sort, parent_indx]= sort(parent_popln_fitness);
        parent_indx = parent_indx(1:mu);
        next_gen_pool(1:mu,:) = parent_popln(parent_indx, :);
        
        % perform cross over and mutation on the parent population to obtain
        % offspring population
        offsprng_popln= arith_varpoint_crossover(parent_popln, crossover_rate);
        mutat_popln = realnumber_unifrm_mutation(offsprng_popln,  bounds,  mutation_rate);

        % compute fitness of offspring population
        mutat_popln_fitness = comp_fitness_Beale(mutat_popln);
        
        % determine best fit offspring (lambda) to be passed on to the next
        % generation
        [mutat_popln_fitness_sort, mutat_indx]= sort(mutat_popln_fitness);
        offspring_indx = mutat_indx(1:lambda);
        next_gen_pool(mu+1:popln_size,:) = mutat_popln(offspring_indx, :);
        
        % induct best of best  fit offspring chromosome into   Hall of Fame
        if (mutat_popln_fitness_sort(1) < HOF_fitness)   
            HOF_fitness = mutat_popln_fitness_sort(1)
            HOF_genhistory(t) = gen_indx;
            HOF_funcvalhistory(t) = HOF_fitness;
            t=t+1;
            hof_indx = mutat_indx(1);
            HOF_chromosome = mutat_popln(hof_indx,:)
        end
        
        gen_indx = gen_indx + 1;

        end            % while loop for generations ends
fix(clock)  

% output optimal function values and variables represented by the individual
% in the Hall of Fame 

optimal_function_value = HOF_fitness
x_optimal = HOF_chromosome  
disp('Successful execution!');