Genetic algorithm (genetic algorithm, GA) is a new global optimization algorithm developed in recent years, it borrows the viewpoint of biological genetics, through the mechanism of natural selection, heredity, mutation and so on, realizes each individual's adaptability enhancement. This reflects the nature of the "natural selection, survival of the fittest" evolutionary process.
Professor Holland the idea of GA algorithm for the first time in 1962, which attracted a large number of researchers, and quickly promoted to optimization, search, machine learning and so on, and laid a solid theoretical foundation.
When using genetic algorithm to solve the problem, the first thing to do is to encode the model structure and parameters of the problem, which is usually represented by string, and the problem is symbolized and discretized. There are also GA (genetic algorithm in continuous spaces, gacs) defined in continuous space, which are not discussed.
A serial operation of the genetic algorithm (seguential genetic algoritm, SGA) in accordance with the following process:
(1) To deal with the problem of coding;
(2) Random initialization Group X (0): = (x1, x2, ... xn);
(3) The fitness F (xi) is calculated for each individual in the current group X (t), and the fitness indicates the performance of the individual.
(4) Using selection operator to produce intermediate XR (t);
(5) Applying other operators to XR (t), producing a new generation group X (T+1), which aims to extend the coverage of the limited individual and embody the idea of global search;
(6) T:=t+1 if the termination condition is not met (3).
The most commonly used operators in GA are as follows:
(1) Selection operator (selection/reproduction): The selection operator selects an individual from a certain probability in a group, and the probability pi selected by Xi is proportional to the fitness value. The most common method of implementation is the roulette wheel (roulette wheel) model.
(2) crossover operator (Crossover): crossover operator crosses the genetic chain of the selected two individuals according to the probability PC, generates two new individuals, and the crossover position is random. Where the PC is a system parameter.
(3) Mutation operator (Mutation): Mutation operator changes the genetic chain of a new individual by probability pm, which is the inverse of the two-value gene chain (0,1 code).
The implementations of the various operators are varied, and many new operators are being continually proposed to improve some of GA's performance. System parameters (individual number n, gene chain length L, crossover probability pc, mutation probability PM, etc.) have great influence on the convergence speed and the result of the algorithm, and should choose different values depending on the specific problems.
The program design of GA should take the generality into account, and have strong ability to adapt to the new operator. The inheritance of classes in OOP provides us with this possibility. Defines two basic structures: genes (allele) and individuals (individual), with individual collections as data members of group class Tpopulation, and TSGA classes are derived from groups that define the basic operations of GA. For any instance of an application, you can derive from the Tsga class and define a new action.
The Tpopulation class contains two important processes:
Fillfitness: An evaluation function that decodes each individual (decode) and calculates its fitness value, which is implemented in the user class.
Statistic: The current group of statistics, such as the total fitness sumfitness, the average fitness average, the best individual fmax, the worst individual fmin.
The structure and class definition of the SGA are as follows (written in C + +):
typedef char allele; Gene type
typedef struct{
Allele *chrom;
float Fitness; Fitness of chromosome
}individual; Individual definition
Class tpopulation{//Group class definition
Public
int size; Size of Population:n
int lchrom; Length of Chromosome:l
float sumfitness, average;
Individual *fmin, *fmax;
Individual *pop;
tpopulation (int popsize, int strlength);
~tpopulation ();
Inline individual &individual (int i) {return pop[i];
void Fillfitness (); evaluation function
virtual void Statistics (); Statistical functions
};
Class Tsga:public tpopulation{//TSGA classes derive from group classes
Public
float Pcross; Probability of Crossover
float pmutation; Probability of Mutation
int Gen; Counter of Generation
TSGA (int size, int strlength, float pm=0.03, float pc=0.6):
Tpopulation (size, strlength)
{gen=0; pcross=pc; pmutation=pm;} ;
Virtual individual& Select ();
virtual void Crossover (individual &parent1, individual
&parent2,individual &child1, individual &child2);
Virtual allele Mutation (allele alleleval);
virtual void Generate (); Create a new generation
};
The user GA class is defined as follows:
class TSGAfit : public TSGA{
public:
TSGAfit(int size,float pm=0.0333,float pc=0.6)
:TSGA(size,24,pm,pc){};
void print();
};
Because GA is a probabilistic process, the case of each iteration is not the same; the system parameters are different and the iterations are different. In the experiment, the parameters are generally selected as follows: Individual number n=50-200, mutation probability pm=0.03, crossover probability pc=0.6. The mutation probability is too big, can cause the instability.