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

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

% A naive demonstration of Differential Evolution (rand/1/bin) Strategy  for Beale
% function Optimization 
% Chapter 2  Sec.2.6 B

% 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 Differential Evolution and problem parameters
popln_size = 20;                    % set population size 
individual_length = 2;              % set individual length  
beta = 0.5;

% higher probabilty of recombination assures greater diversity
pr_recombi = 0.87;     
total_generations = 100;           %total generations
bounds = [[-4.5 -4.5];[4.5 4.5]];  %bounds for Beale function


% generate random initial population satisfying bounds of Beales function
fix(clock)
initial_popln = bounds (1,1) + (bounds (2,2) - bounds(1,1)) .*rand(popln_size, individual_length);
current_popln = initial_popln;

% begin generation cycles
gen_indx = 1;  
        
        % while loop for generation cycles begins
        while (gen_indx < total_generations)         
                                              
        gen_indx 
        
        % set current population as parent population 
        parent_popln = current_popln; 
        
        % compute parent population fitness
        parent_popln_fitness = comp_fitness_Beale(parent_popln);
        
        % obtain trial vector population through mutation
        trial_vector_popln = DE_mutation(parent_popln, beta, popln_size);
        
        % generate offspring population through crossover 
        offsprng_popln= DE_bin_Crossover(parent_popln, trial_vector_popln, pr_recombi,  individual_length);      
        
        % compute offspring population fitess
        offsprng_popln_fitness = comp_fitness_Beale(offsprng_popln);
        
        % select next generation using deterministic selection
        next_gen_pool = DE_selection(parent_popln, parent_popln_fitness, offsprng_popln,  offsprng_popln_fitness, popln_size);                
         
        % increment generation count
        gen_indx = gen_indx + 1;
        
        % set the current population
        current_popln = next_gen_pool;     
        end                                  % while loop for generations ends

% compute fitness of the current population as soon as the termination
% criterion is met with
popln_fitness = comp_fitness_Beale(current_popln);

% sort fitness values to obtain the individual with the maximum fitness
[popln_fitness_sort, final_indx]= sort(popln_fitness);

% obtain maximal objective function value and the corresponding optimal
% values of the variables
j=final_indx(1);
x_optimal = current_popln(j, :);
y_optimal = beale(x_optimal);
   
% display optimization results       
disp('Optimization results:')
x_optimal
y_optimal

fix(clock)
disp('Successful execution!');