a genetic algorithm to solve some special functions
DESCRIPTION
A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS. BY EMMANUEL SARKODIE ADABOR SUPERVISED BY MR. J. ACKORA-PRAH. OUTLINE. BACKGROUND STATEMENT OF PROBLEM OBJECTIVE METHODOLOGY APPLICATIONS TO SPECIAL FUNCTIONS CONCLUSION RECOMMENDATIONS. BACKGROUND. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/2.jpg)
OUTLINEOUTLINE
BACKGROUNDSTATEMENT OF PROBLEMOBJECTIVEMETHODOLOGYAPPLICATIONS TO SPECIAL FUNCTIONSCONCLUSIONRECOMMENDATIONS
2
![Page 3: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/24.jpg)
Convergence CriteriaConvergence Criteria
Maximum generationElapsed timeNo change in fitnessStall generationsStall time limit
24
![Page 25: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/29.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/30.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/31.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/33.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/36.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/37.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/38.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/39.jpg)
END OF PRESENTATIONEND OF PRESENTATION
THANK YOU
39
![Page 40: A GENETIC ALGORITHM TO SOLVE SOME SPECIAL FUNCTIONS](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/40.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/41.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/42.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/43.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/44.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/45.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/46.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/47.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/48.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/49.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/50.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/51.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/52.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/53.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/54.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/55.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/56.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/57.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/58.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/59.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/60.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/61.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/62.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/63.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/64.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/65.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/66.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/67.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813b99550346895da4cd02/html5/thumbnails/68.jpg)
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