a genetic algorithm to solve some special functions by emmanuel sarkodie adabor supervised by mr. j....

68
A GENETIC ALGORITHM TO A GENETIC ALGORITHM TO SOLVE SOME SPECIAL SOLVE SOME SPECIAL FUNCTIONS FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Upload: jocelyn-heath

Post on 03-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

A GENETIC ALGORITHM TO A GENETIC ALGORITHM TO SOLVE SOME SPECIAL SOLVE SOME SPECIAL

FUNCTIONSFUNCTIONS

BY

EMMANUEL SARKODIE ADABOR

SUPERVISED BY

MR. J. ACKORA-PRAH

1

Page 2: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

OUTLINEOUTLINE

BACKGROUNDSTATEMENT OF PROBLEMOBJECTIVEMETHODOLOGYAPPLICATIONS TO SPECIAL FUNCTIONSCONCLUSIONRECOMMENDATIONS

2

Page 3: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

BACKGROUNDBACKGROUND

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

Page 4: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

BACKROUND (CONT.)BACKROUND (CONT.)

4. Evolutionary programming (Forgel et al.1966) The following principles in Darwin’s principle

of the survival of the fittest inspired the genetic algorithm:

1. Species live in a competitive world.

2. Survival depends on the fitness competition and offspring who are stronger than or equally as strong as their parents.

4

Page 5: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

BACKROUND (CONT.)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

Page 6: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

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

PROBLEM STATEMENTPROBLEM STATEMENT

6

Page 7: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

OBJECTIVEOBJECTIVE

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

Page 8: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

METHODOLOGYMETHODOLOGY

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

Page 9: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

DEFINITION (CONT’D)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

Page 10: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

DEFINITION (CONT’D)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

Page 11: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

OVERVIEW OF GAOVERVIEW OF GA

11

Create initial population

Evaluate the fitness of each individual

Select parent based on fitness

Create new population

Evaluation

Selection

Recombination

Page 12: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

EXAMPLE TO ILLUSTRATE EXAMPLE TO ILLUSTRATE OPERATORS AND OPERATIONS IN GAOPERATORS 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 encodingsBinary encoding is used to solve the

example

12

Page 13: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

SolutionSolution

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 Elitist selections

13

Page 14: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

ROULETTE WHEELROULETTE 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

14

1

( ) ,ii n

kk

fP x w

f

Page 15: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

ROULETTE WHEEL ROULETTE WHEEL (CONT’D)(CONT’D)

The expected count is calculated by

Where n is population size and i is the i-th chromosome

15

1

i

n

ii

f xExpected count

average f x

f xwhere average f x

n

Page 16: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Roulette Wheel is formed asRoulette Wheel is formed as

16

12.47%

54.11%

13.26%

2.16%s1

s2

s3

s4

Page 17: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Table of SelectionTable of Selection

String No. Initial

population

x value Function

value (x2)

P(x) Percentage

probability

Expected

count

Actual

count

1 0 1 1 0 0 12 144 0.1247 12.47% 0.4987 1

2 1 1 0 0 1 25 625 0.5411 54.11% 2.1645 2

3 0 0 1 0 1 5 25 0.0216 2.61% 0.0866 0

4 1 0 0 1 1 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

Page 18: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

CrossoverCrossover

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

Page 19: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Crossover cont’dCrossover cont’d

19

Parent 1 0 1 1 0 0Parent 2 1 1 0 0 1

Offspring 1 0 1 1 0 1Offspring 2 1 1 0 0 0

Similar action is performed for the next strings resulting

Page 20: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Table of CrossoverTable of Crossover

String No. Mating pool

Crossover point

Offspring after

crossover

x value Function value (x2)

1 0 1 1 0 0 4 0 1 1 0 1 13 169

2 1 1 0 0 1 4 1 1 0 0 0 24 576

3 1 1 0 0 1 3 1 1 0 1 1 27 729

4 1 0 0 1 1 3 1 0 0 0 1 17 289

Sum 1763

Average 440.75

Maximum 72920

Page 21: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

MutationMutation

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

Page 22: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Mutation cont’dMutation cont’d

String No. Offspring after

crossover

Mutation Chromosomes

for flipping

Offspring after mutation

x value Function value (x2)

1 0 1 1 0 1 1 0 0 0 0 1 1 1 0 1 29 841

2 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 24 576

3 1 1 0 1 1 0 0 0 0 0 1 1 0 1 1 27 729

4 1 0 0 0 1 0 0 1 0 0 1 0 1 0 0 20 400

Sum 2564

Average 636.5

Maximum 841

22

Page 23: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Solution cont’dSolution 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

Page 24: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Convergence CriteriaConvergence Criteria

Maximum generationElapsed timeNo change in fitnessStall generationsStall time limit

24

Page 25: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

APPLICATION OF GA TO APPLICATION OF GA TO SPECIAL FUNCTIONSSPECIAL 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

Page 26: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

SolutionSolution

The following parameters were used in the simulation of all three functions:

Probability of crossover = 0.8Probability of mutation = 0.2Initial population = 50Maximum generations = 100Stall generations = 50

26

Page 27: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

ROSENBROCK’S FUNCTION ROSENBROCK’S FUNCTION OR VALLEYOR VALLEY

The function has the following definition

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

22 2, 1 100f x y x y x

Page 28: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Overview of Rosenbrock’s Overview of Rosenbrock’s function function

28

Minimum point

The global optimum lies inside a long, narrow, parabolic shaped flat valley

Page 29: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Solution cont’dSolution cont’d Global minimum is 0.0000496 (approximately zero (0)) It occurred at (1.0070, 1.0140) of the 51st generation

29

Page 30: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

RASTRIGIN’S FUNCTIONRASTRIGIN’S FUNCTION

Function has the following definition

where xi lies in [-5.12, 5.12]

30

2

110 10cos 2

n

i ii

f x n x x

Page 31: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Overview of Rastrigin’s functionOverview of Rastrigin’s function

31

The function is highly multimodal. However, the location of the minima are regularly distributed.

Page 32: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Solution cont’dSolution cont’d Global minimum is 0.00000000239 (approximately zero ) It occurred at the point 0.00000347 of the 51st generation

32

Page 33: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Solution Cont’dSolution 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

Page 34: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

SCHWEFEL’S FUNCTIONSCHWEFEL’S FUNCTION

Function has the following definition

where xi lies in [-500, 500]

34

1

sinn

i ii

f x x x

Page 35: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Overview of Schwefel’s functionOverview of Schwefel’s function

35

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

Page 36: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Solution Solution Global minimum is -418.9829 It occurred at 420.9618 at the 51st generation

36The global minimum for an n = 10 is -4.7620 ×10114

Page 37: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

CONCLUSIONSCONCLUSIONS

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

Page 38: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

RECOMMENDATIONSRECOMMENDATIONS

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

Page 39: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

END OF PRESENTATIONEND OF PRESENTATION

THANK YOU

39

Page 40: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CODEGA CODE

function [x,fval,exitFlag,output,population,scores] = … ga(FUN,GenomeLength,Aineq,Bineq,Aeq,Beq,LB,UB,nonlcon,options)

defaultopt = struct('PopulationType', 'doubleVector', ... 'PopInitRange', [0;1], ... 'PopulationSize', 20, ... 'EliteCount', 2, ... 'CrossoverFraction', 0.8, ... 'MigrationDirection','forward', ... 'MigrationInterval',20, ...

40

Page 41: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA CONT’D

'InitialPopulation',[], ... 'InitialScores', [], ... 'InitialPenalty', 10, ... 'PenaltyFactor', 100, ... 'PlotInterval',1, ... 'CreationFcn',@gacreationuniform, ... 'FitnessScalingFcn', @fitscalingrank, ... 'SelectionFcn', @selectionroulette, ... 'CrossoverFcn',@crossovertwopoint, ... 'MutationFcn',@mutationgaussian, ...

41

Page 42: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA CONT’D

'MigrationFraction',0.2, ... 'Generations', 100, ... 'TimeLimit', inf, ... 'FitnessLimit', -inf, ... 'StallGenLimit', 50, ... 'StallTimeLimit', 20, ... 'TolFun', 1e-6, ... 'TolCon', 1e-6, ...

42

Page 43: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 44: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 45: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 46: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 47: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 48: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA CONT’D

nonlcon = FUN.nonlcon; else Aineq = []; Bineq = []; Aeq = []; Beq = []; LB = []; UB = []; nonlcon = []; end % optional fields if isfield(FUN, 'randstate') && isfield(FUN, 'randnstate')

&& ...

48

Page 49: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 50: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 51: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 52: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 53: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 54: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 55: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 56: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 57: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 58: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 59: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 60: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 61: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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

Page 62: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA CONT’D

for i = 1:pop feasible(i) =

isTrialFeasible(options.InitialPopulation(i,:)',Aineq,Bineq,Aeq,Beq,LB,UB,options.TolCon);

end options.InitialPopulation(~feasible,:) = []; try % InitialScores may not be present options.InitialScores(~feasible) = []; catch end end

62

Page 63: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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.PopInitRange);

63

Page 64: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA CONT’D

% Print some diagnostic information if asked for if options.Verbosity > 2 gadiagnose(FitnessFcn,NonconFcn,GenomeLength,nineqc

str,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

Page 65: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

GA CONT’DGA 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,output,Iterate,s

ubtype); end

65

Page 66: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Function codesFunction codesRosenbrock’s Functionfunction f=rosenbrock(x)sumc=0;for i=1:length(x)-1sumc = sumc+100*((x(i+1)-x(i)^2)^2) + (1-

x(i))^2;endf = sumc

66

Page 67: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Codes cont’dCodes cont’d

Rastrigin’s Functionfunction scores = rastriginsfcn(x) scores = 10.0 * size(x,2) + sum(x .^2 - 10.0

* cos(2 * pi .*x),2);

67

Page 68: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH 1

Codes cont’dCodes cont’d

Schwefel’s functionfunction y=schwefel(x)sz=size(x);if sz(1)==1 x=x'; endy=-sum(x.*sin(sqrt(abs(x))));

68