A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1 OUTLINE BACKGROUND STATEMENT OF PROBLEM OBJECTIVE METHODOLOGY APPLICATIONS TO SPECIAL FUNCTIONS CONCLUSION RECOMMENDATIONS 2 BACKGROUND Darwin’s principle of survival of the fittest was used as a starting point in introducing evolutionary computation (EC). EC has four stages of development namely; 1. Genetic algorithms (Holland, 1975) 2. Genetic programming (Koza, 1992, 1994) 3. Evolutionary strategies (Rocheuberg,1973) 3 BACKROUND (CONT.) 4. 1. 2. Evolutionary programming (Forgel et al.1966) The following principles in Darwin’s principle of the survival of the fittest inspired the genetic algorithm: Species live in a competitive world. Survival depends on the fitness competition and offspring who are stronger than or 4 equally as strong as their parents. BACKROUND (CONT.) 3. The offspring genetically take the characteristics of their parents 4. The offspring are however unique and there is probability of slight variations in some of their genes. 5. In the competitive environment less fit individuals die off and may not become parents for breeding. 5 PROBLEM STATEMENT Evolutionary concepts are of recent interest since some methods may not lead to the global optimum. There is therefore the need to find an algorithm that exhausts the entire search space for the global optima of problems 6 OBJECTIVE A GA is applied to solve Rosenbrock’s function, Rastrigin’s function and the Schwefel’s function to test its ability to search for global optima of complex, multivariable and multimodal problems. 7 METHODOLOGY A GA is a search technique used in computing to find approximate solutions to optimization and search problems through application of the principles of evolutionary biology. Terms associated with GA’s are defined as follows: An individual is a solution of optimization problem 8 DEFINITION (CONT’D) A population is set of solutions that form the domain search space. A generation is a set of solutions taken from the population and generated at an instant of time or iteration. Selection is the operation of selecting parents from the generation to produce offspring. Crossover is the process of taking two parents and producing from them a child. 9 DEFINITION (CONT’D) Mutation is random operation whereby the allele of the gene in a chromosome of the offspring is changed by a probability. Recombination is the operation whereby elements of the offspring form an intermediate generation and less fit chromosomes are taken from the generation Chromosomes represent the data structure of solutions. 10 OVERVIEW OF GA Create initial population Evaluate the fitness of each individual Evaluation Select parent based on fitness Create new population Selection Recombination 11 EXAMPLE TO ILLUSTRATE OPERATORS AND OPERATIONS IN GA Max f (x) = x² for x = 0,1, … 31; Encoding: representing individual genes. Encoding may be Binary, Octal, Hexadecimal or Permutation encodings Binary encoding is used to solve the example 12 Solution Using a five bit (binary integer) unsigned integer, numbers between 0(00000) and 31(11111) can be obtained. Population of size 4 is randomly initialized. Selection: choosing two parents from the population for crossing. Selection Methods include Roulette Wheel, Random, Rank, Tournament and 13 Elitist selections ROULETTE WHEEL Roulette wheel selection is used. A roulette wheel is constructed with the cumulative and relative fitness ( f ) of chromosomes. Let i denote ith chromosome. The relative fitness of each chromosome is P ( x ) wi fi , n fk k 1 14 ROULETTE WHEEL (CONT’D) The expected count is calculated by E xpected count w here f xi average f x average f x n f xi i 1 n Where n is population size and i is the i-th chromosome 15 Roulette Wheel is formed as s4 13.26% s1 12.47% 54.11% s3 2.16% s2 16 Table of Selection String No. Initial x value Function P(x) value (x2) population Percentage Expected Actual probability count count 1 01100 12 144 0.1247 12.47% 0.4987 1 2 11001 25 625 0.5411 54.11% 2.1645 2 3 00101 5 25 0.0216 2.61% 0.0866 0 4 10011 19 361 0.3126 31.26% 1.2502 1 Sum 1155 1.0000 100% 4.0000 4 Average 288.75 0.2500 25% 1.0000 1 Maximum 625 0.5411 54.11% 2.1645 2 17 Crossover The various crossover techniques include single point, two point, multipoint and uniform crossovers. In a single point crossover, the parents are cut at corresponding points and the sections after the cut are exchanged. 18 Crossover cont’d Parent 1 Parent 2 0 1 1 0 0 1 1 0 0 1 Offspring 1 0 1 1 0 1 Offspring 2 1 1 0 0 0 Similar action is performed for the next strings resulting 19 Table of Crossover String No. Mating pool Crossover point Offspring after crossover x value Function value (x2) 1 01100 4 01101 13 169 2 11001 4 11000 24 576 3 11001 3 11011 27 729 4 10011 3 10001 17 289 Sum 1763 Average 440.75 Maximum 729 20 Mutation Mutation prevents the algorithm to be trapped in a local minimum by maintaining diversity in population Different forms of Mutation include flipping, interchanging and reversing. Flipping is used. 21 Mutation cont’d String No. Offspring after crossover Mutation Chromosomes for flipping Offspring after mutation x value Function value (x2) 1 01101 10000 11101 29 841 2 11000 00000 11000 24 576 3 11011 00000 11011 27 729 4 10001 00100 10100 20 400 Sum 2564 Average 636.5 Maximum 841 22 Solution cont’d Replacements are made by comparing fitness values. The example was solved with crossover and mutation probabilities 1.0 and 0.001. The procedure showed an improvement on maximum fitness from 625 to 841 in just one generation. 23 Convergence Criteria Maximum generation Elapsed time No change in fitness Stall generations Stall time limit 24 APPLICATION OF GA TO SPECIAL FUNCTIONS The GA is used to solve Rosenbrock’s function, Rastrigin’s function and Schwefel’s function in order establish how good the algorithm is. A MATLAB code is implemented to minimize the functions. 25 Solution The following parameters were used in the simulation of all three functions: Probability of crossover = 0.8 Probability of mutation = 0.2 Initial population = 50 Maximum generations = 100 Stall generations = 50 26 ROSENBROCK’S FUNCTION OR VALLEY The function has the following definition f x , y 1 x 100 y x 2 2 2 where x and y lies in [-2.048, 2.048] It is also called banana function because its distinct shape in a contour plot 27 Overview of Rosenbrock’s function Minimum point The global optimum lies inside a long, narrow, parabolic shaped flat valley 28 Solution cont’d Global minimum is 0.0000496 (approximately zero (0)) It occurred at (1.0070, 1.0140) of the 51st generation 29 RASTRIGIN’S FUNCTION Function has the following definition n f x 10 n x i2 10 cos 2 x i i 1 where xi lies in [-5.12, 5.12] 30 Overview of Rastrigin’s function The function is highly multimodal. However, the location of the minima are regularly distributed. 31 Solution cont’d Global minimum is 0.00000000239 (approximately zero ) It occurred at the point 0.00000347 of the 51st generation 32 Solution Cont’d The global minimum for an n = 5 is 0.0309 It occurred at 0.0125, 0.0000, -0.0000, 0.0001, -0.0000. (which are all approximately zero). 33 SCHWEFEL’S FUNCTION Function has the following definition f x n i 1 x i sin xi where xi lies in [-500, 500] 34 Overview of Schwefel’s function The global minimum is geometrically distant, over the parameter space from the next best local minima. Therefore, algorithms are potentially prone to convergence in the wrong direction 35 Solution Global minimum is -418.9829 It occurred at 420.9618 at the 51st generation The global minimum for an n = 10 is -4.7620 ×10114 36 CONCLUSIONS The GA has been able to produce the global optima of complex multivariable and multimodal functions. These were all obtained under 1minute. The GA is therefore efficient, robust and reliable. 37 RECOMMENDATIONS It is recommended for problems with the properties of the three functions. It is recommended that further research is conducted to establish the variants of GA and their suitability to specific problems 38 END OF PRESENTATION THANK YOU 39 GA CODE function [x,fval,exitFlag,output,population,scores] = … ga(FUN,GenomeLength,Aineq,Bineq,Aeq,Beq,LB,UB,n onlcon,options) defaultopt = struct('PopulationType', 'doubleVector', ... 'PopInitRange', [0;1], ... 'PopulationSize', 20, ... 'EliteCount', 2, ... 'CrossoverFraction', 0.8, ... 'MigrationDirection','forward', ... 'MigrationInterval',20, ... 40 GA CONT’D 'InitialPopulation',[], ... 'InitialScores', [], ... 'InitialPenalty', 10, ... 'PenaltyFactor', 100, ... 'PlotInterval',1, ... 'CreationFcn',@gacreationuniform, ... 'FitnessScalingFcn', @fitscalingrank, ... 'SelectionFcn', @selectionroulette, ... 'CrossoverFcn',@crossovertwopoint, ... 'MutationFcn',@mutationgaussian, ... 41 GA CONT’D 'MigrationFraction',0.2, ... 'Generations', 100, ... 'TimeLimit', inf, ... 'FitnessLimit', -inf, ... 'StallGenLimit', 50, ... 'StallTimeLimit', 20, ... 'TolFun', 1e-6, ... 'TolCon', 1e-6, ... 42 GA CONT’D 'HybridFcn',[], ... 'Display', 'final', ... 'PlotFcns', [], ... 'OutputFcns', [], ... 'Vectorized','off'); % Check number of input arguments errmsg = nargchk(1,10,nargin); if ~isempty(errmsg) error('gads:ga:numberOfInputs',[errmsg,' GA requires at least 1 input argument.']); 43 GA CONT’D end % If just 'defaults' passed in, return the default options in X if nargin == 1 && nargout <= 1 && isequal(FUN,'defaults') x = defaultopt; return end 44 GA CONT’D if nargin < 10, options = []; if nargin < 9, nonlcon = []; if nargin < 8, UB = []; if nargin < 7, LB = []; if nargin <6, Beq = []; if nargin <5, Aeq = []; if nargin < 4, Bineq = []; if nargin < 3, Aineq = []; end; end; end; end 45 GA CONT’D end; end; end; end % Is third argument a structure if nargin == 3 && isstruct(Aineq) % Old syntax options = Aineq; Aineq = []; end % Input can be a problem structure if nargin == 1 try options = FUN.options; GenomeLength = FUN.nvars; 46 GA CONT’D % If using new syntax then must have all the fields; check one % field if isfield(FUN,'Aineq') Aineq = FUN.Aineq; Bineq = FUN.Bineq; Aeq = FUN.Aeq; Beq = FUN.Beq; LB = FUN.LB; UB = FUN.UB; 47 GA CONT’D nonlcon = FUN.nonlcon; else Aineq = []; Bineq = []; Aeq = []; Beq = []; LB = []; UB = []; nonlcon = []; end % optional fields if isfield(FUN, 'randstate') && isfield(FUN, 'randnstate') && ... 48 GA CONT’D isa(FUN.randstate, 'double') && isequal(size(FUN.randstate),[625, 1]) && ... isa(FUN.randnstate, 'double') && isequal(size(FUN.randnstate),[2, 1]) rand('twister',FUN.randstate); randn('state',FUN.randnstate); end FUN = FUN.fitnessfcn; catch error('gads:ga:invalidStructInput','The input should 49 GA CONT’D be a structure with valid fields or provide at least two arguments to GA.' ); end end % We need to check the GenomeLength here before we call any solver valid = isnumeric(GenomeLength) && isscalar(GenomeLength)&& (GenomeLength > 0) ... && (GenomeLength == floor(GenomeLength)); if(~valid) 50 GA CONT’D error('gads:ga:validNumberofVariables:notValidNvars',' Number of variables (NVARS) must be a positive integer.'); end % Use default options if empty if ~isempty(options) && ~isa(options,'struct') error('gads:ga:optionsNotAStruct','Tenth input argument must be a valid structure created with GAOPTIMSET.'); elseif isempty(options) 51 GA CONT’D options = defaultopt; end user_options = options; % All inputs should be double try dataType = superiorfloat(GenomeLength,Aineq,Bineq,Aeq,Beq,LB, UB); if ~isequal('double', dataType) error('gads:ga:dataType', … 52 GA CONT’D output.message = ''; % Determine the 'type' of the problem if ~isempty(nonlcon) type = 'nonlinearconstr'; % Determine the sub-problem type for the constrained problem (used in ALPS) if ~isempty(Aeq) || ~isempty(Beq) || ~isempty(Aineq) || ~isempty(Bineq) subtype = 'linearconstraints'; elseif ~isempty(LB) || ~isempty(UB) 53 GA CONT’D subtype = 'boundconstraints'; else subtype = 'unconstrained'; end % If Aeq or Aineq is not empty, then problem has linear constraints. elseif ~isempty(Aeq) || ~isempty(Beq) || ~isempty(Aineq) || ~isempty(Bineq) type = 'linearconstraints'; % This condition satisfies bound constraints 54 GA CONT’D elseif ~isempty(LB) || ~isempty(UB) type = 'boundconstraints'; % If all constraints fields are empty then it is unconstrained else type = 'unconstrained'; end % Initialize output structure output.problemtype = type; % If nonlinear constraints, then subtype is needed to process linear 55 GA CONT’D 'GA only accepts inputs of data type double.') end catch error('gads:ga:dataType', ... 'GA only accepts inputs of data type double.') end; % Remember the random number states used output.randstate = rand('twister'); output.randnstate = randn('state'); output.generations = 0; output.funccount = 0; 56 GA CONT’D % constraints (see function preProcessLinearConstr) if strcmp(type,'nonlinearconstr') type = subtype; end % Validate options and fitness function [options,GenomeLength,FitnessFcn,NonconFcn] = validate(GenomeLength,FUN,nonlcon,options,type); if ~strcmp(output.problemtype,'unconstrained') % Determine a start point if ~isempty(options.InitialPopulation) 57 GA CONT’D population = []; scores = []; % Bound correction [LB,UB,msg,exitFlag] = checkbound(LB,UB,GenomeLength); if exitFlag < 0 output.message = msg; if options.Verbosity > 0 fprintf('%s\n',msg) end; return; 58 GA CONT’D end % Linear constraints correction [Iterate.x,Aineq,Bineq,Aeq,Beq,LB,UB,msg,exitFlag] = ... preProcessLinearConstr(Iterate.x,Aineq,Bineq,Aeq,Beq, LB,UB,GenomeLength,type,options.Verbosity); if exitFlag < 0 output.message = msg; if options.Verbosity > 0 59 GA CONT’D fprintf('%s\n',msg) end return; end % If initial population was not empty then we replace the first individual % by the feasible point just found if ~isempty(options.InitialPopulation) && ~isempty(Iterate.x) options.InitialPopulation(1,:) = Iterate.x'; feasible = true(pop,1); 60 GA CONT’D try % InitialScores may not be present options.InitialScores(1) = []; catch end end % Verify that individuals in InitialPopulation are feasible if ~isempty(options.InitialPopulation) && ~strcmp(type,'unconstrained') pop = size(options.InitialPopulation,1); 61 GA CONT’D for i = 1:pop feasible(i) = isTrialFeasible(options.InitialPopulation(i,:)',Aineq,Bine q,Aeq,Beq,LB,UB,options.TolCon); end options.InitialPopulation(~feasible,:) = []; try % InitialScores may not be present options.InitialScores(~feasible) = []; catch end 62 end GA CONT’D % Validate nonlinear constraints [LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, ... Iterate,Aineq,Bineq,Aeq,Beq,LB,UB,type,options); options.LinearConstr = LinearConstr; % Make sure that bounds and PopInitRange are consistent options.PopInitRange = checkPopulationInitRange(LB,UB,options.PopInitRang 63 e); GA CONT’D % Print some diagnostic information if asked for if options.Verbosity > 2 gadiagnose(FitnessFcn,NonconFcn,GenomeLength,nine qcstr,neqcstr,ncstr,user_options); end % Call appropriate single objective optimization solver switch(output.problemtype) case 'unconstrained' [x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,GenomeLength, ... 64 GA CONT’D options,output,Iterate); case {'boundconstraints', 'linearconstraints'} [x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,GenomeLength, ... Aineq,Bineq,Aeq,Beq,LB,UB,options,output,Iterate); case 'nonlinearconstr' [x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,GenomeLength, ... Aineq,Bineq,Aeq,Beq,LB,UB,NonconFcn,options,outpu t,Iterate,subtype); 65 end Function codes Rosenbrock’s Function function f=rosenbrock(x) sumc=0; for i=1:length(x)-1 sumc = sumc+100*((x(i+1)-x(i)^2)^2) + (1x(i))^2; end f = sumc 66 Codes cont’d Rastrigin’s Function function scores = rastriginsfcn(x) scores = 10.0 * size(x,2) + sum(x .^2 - 10.0 * cos(2 * pi .*x),2); 67 Codes cont’d Schwefel’s function function y=schwefel(x) sz=size(x); if sz(1)==1 x=x'; end y=-sum(x.*sin(sqrt(abs(x)))); 68