A list,

Genetic Algorithm (GA) is a part of evolutionary computing. It is a computational model that simulates Darwin’s Genetic selection and natural selection of biological evolution process. It is a method to search for optimal solution by simulating natural evolution process. The algorithm is simple, universal, robust and suitable for parallel processing.

2 Characteristics and Application of Genetic Algorithm Genetic algorithm is a kind of robust search algorithm which can be used for complex system optimization. Compared with traditional optimization algorithm, it has the following characteristics: (1) The encoding of decision variables is taken as the operation object. Traditional optimization algorithms often directly use the actual value of the decision variable itself to optimize the calculation, but genetic algorithm uses a certain form of the decision variable encoding as the operation object. This method of encoding decision variables makes it possible for us to use the concepts of chromosomes and genes in biology for reference in optimization calculation, to imitate the genetic and evolutionary incentives of organisms in nature, and to easily apply genetic operators. (2) Directly use fitness as search information. The traditional optimization algorithm not only needs to use the value of the objective function, but also needs to satisfy the requirement of “the derivative of the objective function must exist” in order to determine the search direction. Genetic algorithm can only use the fitness function value transformed from the objective function value to determine the further search range, without the derivative value of the objective function and other auxiliary information. The objective function value or individual fitness value can be directly used to concentrate the search scope into the search space with higher fitness, so as to improve the search efficiency. (3) Search information of multiple points is used with implicit parallelism. Traditional optimization algorithms usually start from an initial point in the solution space to search the optimal solution iteratively. The search information provided by a single point is not much, so the search efficiency is not high, and there may be local optimal solution and stagnation. Genetic algorithms start the search process for optimal solutions from an initial population consisting of many individuals rather than from a single individual. The operation of selection, crossover and mutation on the initial population produces a new generation of population, which contains a lot of population information. This information can avoid searching some unnecessary points, so as to avoid falling into the local optimal solution and gradually approach the global optimal solution. (4) Use probabilistic search rather than deterministic rules. Traditional optimization algorithms often use deterministic search methods, the transfer of a search point to another search point has a certain direction and transfer relationship, such deterministic search may not reach the optimal shop, limit the scope of application of the algorithm. Genetic algorithm (GA) is a kind of adaptive search technology. Its selection, crossover, mutation and other operations are carried out in a probabilistic way, which increases the flexibility of the search process, and can converge to the optimal solution with a large probability, and has a good global optimization capability. However, crossover probability, mutation probability and other parameters can also affect the search results and search efficiency of the algorithm, so how to choose the parameters of genetic algorithm is a relatively important problem in its application. To sum up, genetic algorithm provides a general framework for solving complex system problems because the overall search strategy and optimal search mode of genetic algorithm do not depend on gradient information or other auxiliary knowledge in calculation, but only require the solution of objective function that affects the search direction and the corresponding fitness function. It is not dependent on the specific domain of the problem, and has strong robustness to the type of the problem, so it is widely used in a variety of fields, including: function optimization, combinational optimization production scheduling problems, automatic control, robotics, image processing (image recovery, image edge feature extraction…) Artificial life, genetic programming, machine learning.

Simple Genetic Algorithms (SGA) only uses selection operator, crossover operator and mutation operator. It is the basis of other Genetic Algorithms because of its Simple evolution process.

3.1 Basic flow of genetic algorithm

A number of initial populations encoded by a certain length (length is related to the accuracy of the problem to be solved) are generated in a random way.

Each individual was evaluated by fitness function, and the individuals with high fitness were selected to participate in genetic operation, while the individuals with low fitness were eliminated.

The collection of genetically manipulated individuals (replication, crossover, mutation) forms a new generation of population until the stop criterion (evolutionary algebra GEN>=?) is satisfied. ;

The best cashed individual in the offspring is taken as the execution result of the genetic algorithm.



Where GEN is the current algebra; M is population size and I is population size.

3.2 Implementation technology of genetic algorithm

Basic genetic algorithm (SGA) consists of coding, fitness function, genetic operator (selection, crossover, variation) and operation parameters.

3.2.1 coding

(1) Binary coding

The length of the binary-encoded string depends on the precision of the problem being solved. It is necessary to ensure that every individual in the solved space can be coded.

Advantages: coding, decoding simple operation, inheritance, cross easy to achieve

Disadvantages: Large length

(2) Other coding methods

Gray code, floating point coding, symbol coding, multi – parameter coding and so on

3.2.2 Fitness function

The fitness function should effectively reflect the gap between each chromosome and the optimal solution chromosome of the problem.

3.2.3 Selecting the operator



3.2.4 Crossover operator

Crossover operation refers to two mutually paired chromosomes in a way to exchange part of their genes, so as to form two new individuals; Crossover operation is an important feature of genetic algorithm which is different from other evolutionary algorithms and is the main method to generate new individuals. Before crossover, individuals in the group need to be matched, generally adopting the principle of random pairing.

Common crossover methods:

A single point of intersection

Double point crossing (multi-point crossing, the more points of crossing, the more likely the individual’s structure will be damaged, generally, the multi-point crossing is not adopted)

Uniform cross

Arithmetic crossover

3.2.5 Mutation operator

Mutation operation in genetic algorithm refers to the replacement of gene values at some loci in the coding string of individual chromosome with other alleles of that loci, thus forming a new individual.

In terms of the ability to generate new individuals in the process of genetic algorithm, crossover operation is the main method to generate new individuals, which determines the global search ability of genetic algorithm. Mutation operation is only an auxiliary method to generate new individuals, but it is also an essential operation step, which determines the local search ability of genetic algorithm. The combination of crossover operator and mutation operator completes the global search and local search of the search space, so that the genetic algorithm can complete the optimization process with good search performance.

3.2.6 Operating Parameters



4 Basic principles of genetic algorithm

4.1 Pattern Theorem



4.2 Building block hypothesis

Patterns with low order, short definition length, and fitness values higher than the population average are called gene blocks or building blocks.

Building block hypothesis: individual gene blocks can be spliced together to form individual coding strings with higher fitness through selection, crossover, mutation and other genetic operators.

The building block hypothesis illustrates the basic idea of solving all kinds of problems with genetic algorithms, that is, better solutions can be produced by directly joining the building blocks together.

Ii. Source code



clc;
clear;
close all;

%% Problem Definition

model=CreateModel();                        % Create Model

CostFunction=@(sol1) MyCost(sol1,model);	% Cost Function

Vars.xhat.Min=0;
Vars.xhat.Max=1;
Vars.xhat.Size=[1 model.n];
Vars.xhat.Count=prod(Vars.xhat.Size);
Vars.xhat.VelMax=0.1*(Vars.xhat.Max-Vars.xhat.Min);
Vars.xhat.VelMin=-Vars.xhat.VelMax;

Vars.yhat.Min=0;
Vars.yhat.Max=1;
Vars.yhat.Size=[1 model.n];
Vars.yhat.Count=prod(Vars.yhat.Size);
Vars.yhat.VelMax=0.1*(Vars.yhat.Max-Vars.yhat.Min);
Vars.yhat.VelMin=-Vars.yhat.VelMax;

Vars.rhat.Min=0;
Vars.rhat.Max=1;
Vars.rhat.Size=[1 model.n];
Vars.rhat.Count=prod(Vars.rhat.Size);
Vars.rhat.VelMax=0.1*(Vars.rhat.Max-Vars.rhat.Min);
Vars.rhat.VelMin=-Vars.rhat.VelMax;

%% PSO Parameters

MaxIt=500;      % Maximum Number of Iterations

nPop=50;        % Population Size (Swarm Size)

w=1.0;          % Inertia Weight
wdamp=0.99;     % Inertia Weight Damping Ratio
c1=0.7;         % Personal Learning Coefficient
c2=1.5;         % Global Learning Coefficient

% % Constriction Coefficients
% phi1=2.05;
% phi2=2.05;
% phi=phi1+phi2;
% chi=2/(phi2 -+sqrt(phi^24 -*phi));
% w=chi;          % Inertia Weight
% wdamp=1;        % Inertia Weight Damping Ratio
% c1=chi*phi1;    % Personal Learning Coefficient
% c2=chi*phi2;    % Global Learning Coefficient


%% Initialization

empty_particle.Position=[];
empty_particle.Cost=[];
empty_particle.Sol=[];
empty_particle.Velocity=[];
empty_particle.Best.Position=[];
empty_particle.Best.Cost=[];
empty_particle.Best.Sol=[];

particle=repmat(empty_particle,nPop,1);

GlobalBest.Cost=inf;

for i=1:nPop
    
    % Initialize Position
    particle(i).Position=CreateRandomSolution(model);
    
    % Initialize Velocity
    particle(i).Velocity.xhat=zeros(Vars.xhat.Size);
    particle(i).Velocity.yhat=zeros(Vars.yhat.Size);
    particle(i).Velocity.rhat=zeros(Vars.rhat.Size);
    
    % Evaluation
    [particle(i).Cost, particle(i).Sol]=CostFunction(particle(i).Position);
    
    % Update Personal Best
    particle(i).Best.Position=particle(i).Position;
    particle(i).Best.Cost=particle(i).Cost;
    particle(i).Best.Sol=particle(i).Sol;
    
    % Update Global Best
    if particle(i).Best.Cost<GlobalBest.Cost
        GlobalBest=particle(i).Best;
    end
end

BestCost=zeros(MaxIt,1);


%% PSO Main Loop

for it=1:MaxIt
    
    for i=1:nPop
        
        % ---- Motion on xhat
        
        % Update Velocity
        particle(i).Velocity.xhat = w*particle(i).Velocity.xhat ...
            +c1*rand(Vars.xhat.Size).*(particle(i).Best.Position.xhat-particle(i).Position.xhat) ...
            +c2*rand(Vars.xhat.Size).*(GlobalBest.Position.xhat-particle(i).Position.xhat);
        
        % Apply Velocity Limits
        particle(i).Velocity.xhat = max(particle(i).Velocity.xhat,Vars.xhat.VelMin);
        particle(i).Velocity.xhat = min(particle(i).Velocity.xhat,Vars.xhat.VelMax);
        
        % Update Position
        particle(i).Position.xhat = particle(i).Position.xhat + particle(i).Velocity.xhat;
        
        % Velocity Mirror Effect
        IsOutside=(particle(i).Position.xhat<Vars.xhat.Min | particle(i).Position.xhat>Vars.xhat.Max);
        particle(i).Velocity.xhat(IsOutside)=-particle(i).Velocity.xhat(IsOutside);
        
        % Apply Position Limits
        particle(i).Position.xhat = max(particle(i).Position.xhat,Vars.xhat.Min);
        particle(i).Position.xhat = min(particle(i).Position.xhat,Vars.xhat.Max);
        
        % ---- Motion on yhat
        
        % Update Velocity
        particle(i).Velocity.yhat = w*particle(i).Velocity.yhat ...
            +c1*rand(Vars.yhat.Size). *(particle(i).Best.Position.yhat-particle(i).Position.yhat). +c2*rand(Vars.yhat.Size). *(GlobalBest.Position.yhat-particle(i).Position.yhat);
        

function sol2=ImproveSolution(sol1,model,Vars)

    n=model.n;

    A=randperm(n);
    
    for i=A
        sol1=MoveMachine(i,sol1,model,Vars);
    end

    sol2=sol1;
    
end


function [sol2, z2]=MoveMachine(i,sol1,model,Vars)

    dmax=0.5;
    
    % Zero
    [newsol(1), z(1)]=RotateMachine(i,sol1,model);
    
    % Move Up
    newsol(2)=sol1;
    dy=unifrnd(0,dmax);
    newsol(2).yhat(i)=sol1.yhat(i)+dy;
    newsol(2).yhat(i)=max(newsol(2).yhat(i),Vars.yhat.Min);
    newsol(2).yhat(i)=min(newsol(2).yhat(i),Vars.yhat.Max);
    [newsol(2), z(2)]=RotateMachine(i,newsol(2),model);
    
    % Move Down
    newsol(3)=sol1;
    dy=unifrnd(0,dmax);
    newsol(3).yhat(i)=sol1.yhat(i)-dy;
    newsol(3).yhat(i)=max(newsol(3).yhat(i),Vars.yhat.Min);
    newsol(3).yhat(i)=min(newsol(3).yhat(i),Vars.yhat.Max);
    [newsol(3), z(3)]=RotateMachine(i,newsol(3),model);
    
    % Move Right
    newsol(4)=sol1;
    dx=unifrnd(0,dmax);
    newsol(4).xhat(i)=sol1.xhat(i)+dx;
    newsol(4).xhat(i)=max(newsol(4).xhat(i),Vars.xhat.Min);
    newsol(4).xhat(i)=min(newsol(4).xhat(i),Vars.xhat.Max);
    [newsol(4), z(4)]=RotateMachine(i,newsol(4),model);
    
    % Move Left
    newsol(5)=sol1;
    dx=unifrnd(0,dmax);
    newsol(5).xhat(i)=sol1.xhat(i)-dx;
    newsol(5).xhat(i)=max(newsol(5).xhat(i),Vars.xhat.Min);
    newsol(5).xhat(i)=min(newsol(5).xhat(i),Vars.xhat.Max);
    [newsol(5), z(5)]=RotateMachine(i,newsol(5),model);

    % Move Up-Right
    newsol(6)=sol1;
    dx=unifrnd(0,dmax);
    newsol(6).xhat(i)=sol1.xhat(i)+dx;
    newsol(6).xhat(i)=max(newsol(6).xhat(i),Vars.xhat.Min);
    newsol(6).xhat(i)=min(newsol(6).xhat(i),Vars.xhat.Max);
    dy=unifrnd(0,dmax);
    newsol(6).yhat(i)=sol1.yhat(i)+dy;
    newsol(6).yhat(i)=max(newsol(6).yhat(i),Vars.yhat.Min);
    newsol(6).yhat(i)=min(newsol(6).yhat(i),Vars.yhat.Max);
    [newsol(6), z(6)]=RotateMachine(i,newsol(6),model);

    % Move Up-Left
    newsol(7)=sol1;
    dx=unifrnd(0,dmax);
    newsol(7).xhat(i)=sol1.xhat(i)-dx;
    newsol(7).xhat(i)=max(newsol(7).xhat(i),Vars.xhat.Min);
    newsol(7).xhat(i)=min(newsol(7).xhat(i),Vars.xhat.Max);
    dy=unifrnd(0,dmax);
    newsol(7).yhat(i)=sol1.yhat(i)+dy;
    newsol(7).yhat(i)=max(newsol(7).yhat(i),Vars.yhat.Min);
    newsol(7).yhat(i)=min(newsol(7).yhat(i),Vars.yhat.Max);
    [newsol(7), z(7)]=RotateMachine(i,newsol(7),model);

    % Move Down-Right
    newsol(8)=sol1;
    dx=unifrnd(0,dmax);
    newsol(8).xhat(i)=sol1.xhat(i)+dx;
    newsol(8).xhat(i)=max(newsol(8).xhat(i),Vars.xhat.Min);
    newsol(8).xhat(i)=min(newsol(8).xhat(i),Vars.xhat.Max);
    dy=unifrnd(0,dmax);
    newsol(8).yhat(i)=sol1.yhat(i)-dy;
    newsol(8).yhat(i)=max(newsol(8).yhat(i),Vars.yhat.Min);
    newsol(8).yhat(i)=min(newsol(8).yhat(i),Vars.yhat.Max);
    [newsol(8), z(8)]=RotateMachine(i,newsol(8),model);

    % Move Down-Left
    newsol(9)=sol1;
    dx=unifrnd(0,dmax);
    newsol(9).xhat(i)=sol1.xhat(i)-dx;
    newsol(9).xhat(i)=max(newsol(9).xhat(i),Vars.xhat.Min);
    newsol(9).xhat(i)=min(newsol(9).xhat(i),Vars.xhat.Max);
    dy=unifrnd(0,dmax);
    newsol(9).yhat(i)=sol1.yhat(i)-dy;
    newsol(9).yhat(i)=max(newsol(9).yhat(i),Vars.yhat.Min);
    newsol(9).yhat(i)=min(newsol(9).yhat(i),Vars.yhat.Max);
    [newsol(9), z(9)]=RotateMachine(i,newsol(9),model);

    [z2, ind]=min(z);
    
    sol2=newsol(ind);
Copy the code

3. Operation results



Fourth, note

Version: 2014 a