0% found this document useful (0 votes)
7 views10 pages

Understanding Genetic Algorithms

Genetic algorithms are computational models inspired by evolution, using chromosome-like structures to find solutions to various problems. The algorithm begins with a random population, evaluates fitness, and selects higher-performing chromosomes for reproduction, with parameters such as population size, crossover, and mutation rates affecting performance. The process continues until certain stopping criteria are met, such as reaching a maximum number of generations or finding an acceptable solution.

Uploaded by

truongthieuvy463
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Understanding Genetic Algorithms

Genetic algorithms are computational models inspired by evolution, using chromosome-like structures to find solutions to various problems. The algorithm begins with a random population, evaluates fitness, and selects higher-performing chromosomes for reproduction, with parameters such as population size, crossover, and mutation rates affecting performance. The process continues until certain stopping criteria are met, such as reaching a maximum number of generations or finding an acceptable solution.

Uploaded by

truongthieuvy463
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Genetic Algorithm

1. Concepts
Genetic algorithms belong to a class of computational models that take inspiration from
the process of evolution. They represent possible solutions to specific problems using simple,
chromosome-like data structures and use recombination operations to maintain essential
information. While commonly employed as function optimizers, genetic algorithms have been
successfully applied to a wide variety of problems.
The process of executing a genetic algorithm typically starts with an initial population of
chromosomes, often generated randomly. Each chromosome is evaluated based on its
performance, and reproductive opportunities are distributed in a way that favors chromosomes
offering better solutions to the problem. These higher-performing chromosomes are more likely
to be selected for reproduction than those representing weaker solutions. The quality of a
solution is generally assessed relative to the performance of the current population.
2. Parameters in genetic algorithms
Size of population: number of individuals used for crossbreeding in the population
 If the population size is small, fewer offspring are produced in each generation,
and individuals with poor characteristics can directly impact the population. This
means that although the solution to the given problem might not be optimal, the
speed at which a solution is found will be faster.
 If the population size is too large, the search space for individuals with good traits
will be broader. However, these high-quality individuals will take longer to
interact through crossover to influence the population. This means the time
required to find the optimal solution for the given problem will be longer.
Crossover: If the crossover rate is high, the offspring tend to resemble their parent
generation.
Mutation: If the mutation rate is high, the offspring are more likely to differ significantly
from their parents, introducing greater diversity into the population. (Note: Typically, crossover
rate + mutation rate = 1, and the mutation rate is less than or equal to the crossover rate).
 Advantage: If the mutation rate is high, there is a greater chance of discovering
beneficial mutations. If good mutations are found, the algorithm's performance
will closely approach the optimal solution.
 Disadvantage: Not all mutations are beneficial. While beneficial mutations may
occur, harmful mutations may also arise and affect the population negatively. In
most cases, mutations tend to be unhelpful.
3. Genetic algorithm
Begin

Randomly initialize the


initial population

Evaluate fitness
function (J)

Update Jmin with


J < Jmin
current best J

Check generation
count

Return the best found


solution

End

Fig. Flowchart
Initialization parameters for genetic algorithm:
 Maximum number of generations (max_generation): After crossover and
evaluation, when the generation index “n” exceeds the maximum number of
generations, the algorithm stops and returns the result.
 Maximum number of identical individuals (max_stall_generation): When the
number of repeated (similar) individuals reaches this threshold, the algorithm
stops and returns the result.
 Threshold (epsilon): When an individual has a cost value J less than the defined
threshold, it is considered an acceptable solution, and the algorithm stops and
outputs the result.
 Population size (pop_size): Each individual contains 6 parameters in total,
represented as K1 through K6.
 Parameter ranges (range): Defines the minimum and maximum bounds for each
of the six parameters.
 Decimal point position (dec): Indicates the position of the decimal point for each
gene.
 Number of significant digits (sig): The number of meaningful digits for the
chromosome values.
 Crossover probability (cross_prob): The probability that crossover occurs during
reproduction.
 Mutation probability (mutate_prob): The probability that mutation occurs in the
offspring.
Code:
clc;clear all;
rand('state',sum(100*clock));
max_generation=50;
max_stall_generation=20;
epsilon=0.0001;
pop_size=20;
npar = 6;
range=[ 0 0 0 0 0 0 ;...
1 1 100 1 1 1000 ];
dec=[0 0 2 0 0 3 ];
sig=[3 3 5 3 3 6 ];
cross_prob = 0.9;
mutate_prob = 0.1;
elitism = 1;
rho=0.02;
par=Init(pop_size,npar,range);
Terminal=0;
generation = 0;
stall_generation=0;
Jmin=10^100;
for pop_index=1:pop_size,
K1=par(pop_index,1);
K2=par(pop_index,2);
K3=par(pop_index,3);
K4=par(pop_index,4);
K5=par(pop_index,5);
K6=par(pop_index,6);
sim('GCS_GA.slx');
if length(e1)>3900
J=e1'*e1+e2'*e2;
if(J<Jmin)
K1
K2
K3
K4
K5
K6
Jmin=J
%J=e4'*e4
fitness(pop_index)=1/(J+eps);
else
fitness(pop_index)=1/(J+eps);
end
end
end;
[bestfit,bestchrom]=max(fitness);
K10=par(bestchrom,1);
K20=par(bestchrom,2);
K30=par(bestchrom,3);
K40=par(bestchrom,4);
K50=par(bestchrom,5);
K60=par(bestchrom,6);
J0=1/bestfit+0.001;
while ~Terminal,
generation = generation+1;
disp(['generation #' num2str(generation) ' of maximum ' num2str(max_generation)]);
pop=Encode_Decimal_Unsigned(par,sig,dec);
parent=Select_Linear_Ranking(pop,fitness,0.2,elitism,bestchrom);
child=Cross_Twopoint(parent,cross_prob,elitism,bestchrom);
pop=Mutate_Uniform(child,mutate_prob,elitism,bestchrom);
par=Decode_Decimal_Unsigned(pop,sig,dec);
for pop_index=1:pop_size,
K1=par(pop_index,1);
K2=par(pop_index,2);
K3=par(pop_index,3);
K4=par(pop_index,4);
K5=par(pop_index,5);
K6=par(pop_index,6);
sim('GCS_GA.slx');
if length(e1)>3900
J=e1'*e1+e2'*e2;
if(J<Jmin)
K1
K2
K3
K4
K5
K6
Jmin=J
fitness(pop_index)=1/(J+eps);
else
fitness(pop_index)=1/(J+eps);
end
end
end;
[bestfit(generation),bestchrom]=max(fitness);
if generation == max_generation
Terminal = 1;
elseif generation>1,
if abs(bestfit(generation)-bestfit(generation-1))<epsilon,
stall_generation=stall_generation+1;
if stall_generation == max_stall_generation, Terminal = 1;end
else
stall_generation=0;
end;
end;
end; %While
plot(1./bestfit)
K1=par(bestchrom,1)
K2=par(bestchrom,2)
K3=par(bestchrom,3)
K4=par(bestchrom,4)
K5=par(bestchrom,5)
K6=par(bestchrom,6)
J=1/bestfit(end)
sim('GCS_GA.slx');

Fig. Connection diagram on matlab simulink


4. Simulation results and evaluation of objective function J
0.25 Vị trí cẩu

0.2

0.15

0.1

0.05
x (m)

-0.05

-0.1

-0.15

-0.2

-0.25

0 5 10 15 20 25 30 35 40
Time (seconds)

Vận tốc cẩu


0.2

0.15

0.1

0.05

0
xd (m/s)

-0.05

-0.1

-0.15

-0.2

-0.25

-0.3

0 5 10 15 20 25 30 35 40
Time (seconds)
Góc con lắc
0.2

0.15

0.1

0.05
theta (rad)

-0.05

-0.1

-0.15

-0.2

-0.25
0 5 10 15 20 25 30 35
Time (seconds)

1.5
Vận tốc góc con lắc

0.5
thetad (rad/s)

-0.5

-1

-1.5

0 5 10 15 20 25 30 35 40
Time (seconds)
Đồ thị hàm chỉ tiêu chất lượng J
18.6
J

18.4

18.2

18
J

17.8

17.6

17.4
0 5 10 15 20 25 30 35 40 45
Số thế hệ

You might also like