% ----------------------------------------------------------------- -------------------------------------------------- -----------%
% ----------------------------------------------------------------- -------------------------------------------------- -----------%
%%
% Seeking maximum% of the following function
% F (x) = 10 * sin (5x) 7 * COS (4X) x ∈ [0, 10]%
% The value of x is expressed as a two-value problem with a 10-bit form.
%%
% ----------------------------------------------------------------- -------------------------------------------------- -----------%
% ----------------------------------------------------------------- -------------------------------------------------- -----------%
% Programming
% -----------------------------------------------
% 2.1 Initialization (encoding)
The function of the% initpop.m function is to implement the initialization of the group, and POPSIZE indicates the size of the group, and chromlength indicates the length of the chromosome (length of the bin),
The% length size depends on the length of the variable binary encoding (10 in this example).
% Genetic Algorithm Procedure
% Name: initpop.m
%initialization
Function pop = initpop (Podsize, Chromlength)
POP = Round (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod (Pod,
% Roud is rounded to each unit of the matrix. The initial population produced.
% 2.2 Calculate Target Functions
% 2.2.1 Transforms the binary number into a decimal number (1)
% Genetic Algorithm Procedure
% Name: decodebinary.m
% The row of [2 ^ N 2 ^ (N-1) ... 1] is then summed, and the binary transformation into decimal
Function pop2 = decodeBinary (POP)
[PX, PY] = Size (POP);% seeking POP line and number of cases
For i = 1: py
POP1 (:, i) = 2. ^ (PY-1). * POP (:, i);
PY = PY-1;
end
POP2 = SUM (POP1, 2);% seeking the sum of POP1
% 2.2.2 Convert binary coding into a decimal number (2)% decodechrom.m function function to convert chromosome (or binary encoding) into decimal, parameter spoint means the starting position of the binary string to be decoded
% (For multiple variables, if there are two variables, 20 is indicated, each variable 10 is, then the first variable starts from 1, and the other variable starts from 11. This example is 1),
% Parameter 1engthen represents the length (this example is 10).
% Genetic Algorithm Procedure
% Name: decodechrom.m
% Convert binary coding into ten
Function pop2 = decodechrom (POP, Spoint, Length)
POP1 = POP (:, spoint: Spoint length-1);
POP2 = decodebinary (POP1);
% 2.2.3 Calculate Target Functions
The function of the% CalobjValue.m function is to implement the calculation of the target function, and its formula uses the example emulation of this article, and can be modified according to different optimization issues.
% Genetic Algorithm Procedure
% Name: CalobjValue.m
% Implementation of the calculation of the target function
Function [objvalue] = CalobjValue (POP)
Temp1 = decodechrom (POP, 1, 10);% transforms POP per line into a decimal number
X = Temp1 * 10/1023;% transforms the number in the binary domain into the variable domain
ObjValue = 10 * sin (5 * x) 7 * COS (4 * x);% compute the target function value
% 2.3 Calculate the adaptation value of the individual
% Genetic Algorithm Procedure
% Name: CalfitValue.m
% Calculate individual adaptation value
Function Fitvalue = CalfitValue (Objvalue)
Global cmin;
Cmin = 0;
[px, py] = size (objvalue);
FOR i = 1: PX
IF Objvalue (i) cmin> 0
Temp = Cmin Objvalue (i);
Else
TEMP = 0.0;
end
Fitvalue (i) = TEMP;
end
FITVALUE = Fitvalue ';
% 2.4 Select copy
% The choice or replication operation is to determine which individuals can enter the next generation. This method is used in the program. This method is easier to implement.
% Depending on Equation PI = Fi / σFi = Fi / Fsum, select the steps:
% 1) Calculate FSUM and PI by (1)
% 2) Generate {0,1} random number Rand (.), Seeking s = rand (.) * Fsum
% 3) Ask the minimum K in ≥ ≥ S, then the kth individual is selected.
% 4) Perform N times 2), 3) Operation, get n individual, becoming the t = t 1 generation group
% Genetic Algorithm Procedure
% Name: Selection.m
% Select copy
Function [newpop] = Selection (POP, FitValue)
Totalfit = SUM (FitValue);% seeking the sum of the corresponding value
FITVALUE = FitValue / Totalfit;% of the probability of selecting individuals
Fitvalue = cumsum (fitvalue);%, as FitValue = [1 2 3 4], cumsum (FitValue) = [1 3 6 10] [PX, PY] = Size (POP);
MS = sort (Rand (PX, 1));% from small to large
Fitin = 1;
NEWIN = 1;
While newin <= PX
IF (MS (NEWIN) NEWPOP (Newin, :) = POP (FITIN, :);
NEWIN = Newin 1;
Else
Fitin = FITIN 1;
end
end
% 2.5 cross
% Crossover, cross each individual in the group, intersects a certain probability PC, ie, two individuals from a certain location of their respective strings.
% (Usually randomized) begins to exchange, which is similar to gene splitting and recombination during biological evolution. For example, suppose 2 parents individual x1, x2 is:
% X1 = 0100110
% X2 = 1010001
% At the third bit of each individual,% is started, and after paying, it is obtained from 2 new children, Y1, Y2, respectively:
% Y1 = 0100001
% Y2 = 1010110
% This 2 child individual has some features of 2 parent individuals, respectively. Using the exchange, we may be able to combine individuals with higher suitability in the child.
% In fact, it is another characteristic of the genetic algorithm to distinguish between other traditional optimization methods.
% Genetic Algorithm Procedure
% Name: Crossover.m
%cross
Function [newPOP] = Crossover (POP, PC)
[PX, PY] = SIZE (POP);
NewPop = Ones (SIZE (POP));
For i = 1: 2: PX-1
IF (Rand Cpoint = Round (Rand * Py);
NewPOP (i,:) = [POP (i, 1: cpoint) POP (i 1, CPOINT 1: PY)];
NewPOP (i 1,:) = [POP (i 1, 1: cpoint) POP (i, cpoint 1: py)];
Else
NewPOP (i, :) = POP (i, :);
NewPOP (i 1, :) = POP (i 1, :);
end
end
% 2.6 variation
% Variation, mutation of genes is generally existently in the process of evolution of biology. Variation is that each of the individuals in the father is flipped in probability PM, that is, "1" becomes "0",
% Or by "0" to "1". The variation characteristics of the genetic algorithm can cause the solution to randomly search for the creating space, so that the global optimal solution can be obtained to some extent.
% Genetic Algorithm Procedure
% Name: Mutation.m
%Mutations
Function [newpop] = MUTATION (POP, PM)
[PX, PY] = SIZE (POP);
NewPop = Ones (SIZE (POP));
FOR i = 1: PX
IF (Rand Mpoint = Round (Rand * Py);
IF MPOINT <= 0
MPOINT = 1;
end
NewPOP (i, :) = POP (i, :);
IF ANY (NewPoP (i, mpoint) == 0
NewPOP (i, mpoint) = 1;
Else
NewPOP (i, mpoint) = 0;
end
Else
NewPOP (i, :) = POP (i, :); end
end
% 2.7 finds the maximum adaptation value in the group and its individual
% Genetic Algorithm Procedure
% Name: Best.m
% Ask the value of the maximum adaptation value in the group
Function [Bestindividual, Bestfit] = BEST (POP, Fitvalue)
[PX, PY] = SIZE (POP);
Bestindividual = POP (1, :);
BESTFIT = Fitvalue (1);
For i = 2: PX
IF Fitvalue (i)> bestfit
Bestindividual = POP (i, :);
BESTFIT = FitValue (i);
end
end
% 2.8 Main Program
% Genetic Algorithm Program
% Name: genmain05.m
Clear
CLF
POPSIZE = 20;% group size
ChromLength = 10;% string length (individual length)
PC = 0.6;% cross probability
PM = 0.001;% variation probability
POP = initpop (popsize, chromlength);% randomly generated initial group
FOR i = 1: 20% 20 is iterative
[ObjValue] = CalobjValue (POP);% calculation target function
FITVALUE = CalfitValue (ObjValue);% of the appropriateness of each individual in the group
[newPOP] = Selection (POP, FitValue);% copy
[NewPOP] = Crossover (POP, PC);% cross
[newPOP] = Mutation (POP, PC);% variation
[Bestindividual, Bestfit] = BEST (POP, FITVALUE);% of the individual finds the most adaptable value in the group and its adaptation value
Y (i) = max (bestfit);
n (i) = i;
POP5 = BESTINDIVIDUAL;
X (i) = decodechrom (pop5, 1, chromlength) * 10/1023;
POP = newpop;
end
FPLOT ('10 * sin (5 * x) 7 * COS (4 * x) ', [0 10])
Hold On
Plot (x, y, 'r *')
Hold Off