Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize genetic algorithm in R language

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to achieve genetic algorithm in R language", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "how to achieve genetic algorithm in R language" article.

What is genetic algorithm?

Genetic algorithm is a kind of randomized search method based on the evolution law of biology (survival of the fittest and genetic mechanism of survival of the fittest). It is a computational model that simulates Darwin's theory of evolution and Mendelian genetic mechanism. The main feature is that the structural object is operated directly, there is no limitation of derivation and function continuity, it has better global optimization ability, and the probabilistic optimization method can be used to obtain and guide the optimized search space automatically. adaptively adjust the search direction without the need for definite rules. Genetic algorithm consists of coding, fitness evaluation and genetic operation, in which genetic operation includes chromosome replication, mutation, crossover and so on.

Implementation of genetic algorithm

1. Coding

There are two kinds of genetic algorithm coding: floating-point coding and binary coding. We introduce the binary coding rules (because binary coding is convenient for chromosomes to carry out genetic, mutation and mutation operations). If the value range of a parameter is (LMagneU) and the parameter is represented by a binary code of length k, the corresponding relationship is as follows:

2. Decoding

The purpose of decoding is to restore unintuitive binary data to decimal. The decoding formula corresponding to the individual binary coding is as follows

The coding and decoding process of genetic algorithms can correspond to the genotypes and phenotypes of organisms macroscopically, and to the transcription and translation of genes microscopically.

3. Mating

Mating operations are operators that use single or multiple points for crossover. First, the positions of one or more mating points are randomly generated, and then the two individuals exchange part of the gene code at the mating point to form a sub-individual. For example, after the exchange of three genes on chromosomes S1T0100101 and S2B1010010, two progenies, S3T0100010 and S4T01010101, are formed.

4. Mutation

"mutation operation" is the use of basic bit operations for gene mutation. In order to avoid the premature convergence of the population in the iterative process, the small probability reversal of the genetic code is carried out for the individual population composed of binary gene coding. For example, chromosome 1101101 can be regarded as the daughter chromosome produced by mutation of the original chromosome by changing the 1 on the fourth position into 0 to get the chromosome 1100101.

5. Individual fitness evaluation.

In the theory of evolution, fitness indicates an individual's ability to adapt to the environment, as well as the individual's ability to reproduce. The fitness function of genetic algorithm is also called evaluation function, which is used to judge the quality of individuals in the population. It is evaluated according to the objective function of the problem.

Genetic algorithms generally do not need other external information in the process of search and evolution, and only use evaluation functions to evaluate the advantages and disadvantages of individuals or solutions, and serve as the basis for genetic operations in the future. Because in the genetic algorithm, the fitness function needs to compare and sort and calculate the interest rate on this basis, so the value of the fitness function should be positive.

6. Copy

The replication operation determines the possibility of inheritance in the next generation according to the fitness of the individual. If the fitness of individual I is fi, then the probability of individual I being selected is

If the individual fitness is high, the probability of being selected is high, and the probability of its gene diffusion in the population will be greater. Individuals with a relatively low probability of replication will be gradually eliminated in the process of inheritance.

Pseudocode implementation:

# class Chrom: chrom = [] fitness = 0 def showChrom (self): print (self.chrom) def showFitness (self): print (self.fitness)

# basic parameter N = 200 # number of individuals in the population mut = 0.2 # mutation probability acr = 0.2 # crossover probability

Pop = {} # Dictionary for i in range (N): pop ['chrom'+str (I)] = Chrom () chromNodes = 2 # number of chromosome nodes (number of variables) iterNum = 10000 # iterations chromRange = [[0,10], [0,10]] # chromosome range aveFitnessList = [] # average fitness bestFitnessList = [] # optimal fitness

# initial chromosome pop = Genetic.initialize (pop, chromNodes, chromRange) pop = Fitness.calFitness (pop) # calculate fitness bestChrom = Genetic.findBest (pop) # find the optimal chromosome bestFitnessList.append (bestChrom [1]) # press the current optimal fitness into the list aveFitnessList.append (Genetic.calAveFitness (pop, N)) # calculate and store the average fitness

# start iterative for t in range (iterNum): # chromosome mutation pop = Genetic.mutChrom (pop, mut, chromNodes, bestChrom, chromRange) # chromosome Exchange pop = Genetic.acrChrom (pop, acr, chromNodes) # find the optimal nowBestChrom = Genetic.findBest (pop) # compare the previous optimal time with the current optimal bestChrom = Genetic.compareChrom (nowBestChrom) BestChrom) # find and replace the worst worseChrom = Genetic.findWorse (pop) pop [worseChrom [0]]. Chrom = pop [bestChrom [0]] .chrom.copy () pop [worseChrom [0]]. Fitness = pop [bestChrom [0]]. Fitness # Storage optimal and average bestFitnessList.append (bestChrom [1]) aveFitnessList.append (Genetic.calAveFitness (pop, N)) is the content of this article on "how to implement genetic algorithms in R language" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 229

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report