python使用遗传算法_用python编程遗传算法

本文介绍了如何使用Python编程实现遗传算法,详细解析了遗传算法的原理及其在Python中的应用。
摘要由CSDN通过智能技术生成

python使用遗传算法

遗传算法概述: (Outline of a Genetic Algorithm:)

A genetic algorithm is:

遗传算法是:

“A technique in artificial intelligence that uses the ideas of genetic mutation, recombination, and survival of the fittest. A large population of potential solutions is maintained from which pairs are cross-matched to produce new members. The best examples in the population are used to breed the next generation.”

“人工智能技术运用了基因突变,重组和适者生存的思想。 维护了大量潜在的解决方案,从这些解决方案中交叉配对以产生新成员。 人口中最好的例子被用来繁殖下一代。”

A Dictionary of Computer Science (Oxford Quick Reference) (p. 235). OUP Oxford. Kindle Edition.

《计算机科学词典》(牛津快速参考)(第235页)。 OUP牛津大学。 Kindle版。

At the core, you have a population, and within the population are individuals, in basic terms sets of, two individuals will breed until changes occur within the population over time. To code this algorithm, I started with an outline as to what a Genetic Algorithm is, and separated them into the following functions:

从根本上讲,您有一个种群,而在种群中,按照基本术语来说,个体是两个个体,它们将繁殖直到种群内的时间随时间发生变化。 为了对该算法进行编码,我首先概述了遗传算法是什么,并将其分为以下功能:

  • Main

    主要
  • PrintArray

    打印阵列
  • Substring

    子串
  • Crossover

    交叉
  • Fitness

    适合度
  • Mutate

    变异

我如何用Python编写简单的遗传算法 (How I programmed a simple Genetic Algorithm in Python)

I then began to write the code, taking into account the above interpretation of what a genetic algorithm is, in computing and by relating it to the real world for better interpretation and understanding therein.

然后我开始在编写代码时,考虑到以上对遗传算法的解释,在计算中并将其与现实世界联系起来,以便在其中进行更好的解释和理解。

Main Function:

主功能:

Here I set up all the underlying logic and data to utilise per generation, each run of the program.

在这里,我设置了程序的每一代,每次运行都要利用的所有基础逻辑和数据。

# Main
population = [""] * (4)population[0] = "00000000"
population[1] = "00000010"
population[2] = "00001000"
population[3] = "00100001"
generation = 0
maximumFitnessReached = False
while not maximumFitnessReached:
print(str(generation) + " " + printArray(population))
bestFitness = 0
bestIndex = 0
secondBestIndex = 0
for i in range(0, len(population) - 1 + 1, 1):
currentFitness = fitness(population[i])
if currentFitness == 1.0:
maximumFitnessReached = True
else:
if currentFitness >= bestFitness:
bestFitness = currentFitness
secondBestIndex = bestIndex
bestIndex = i
for i in range(0, len(population) - 1 + 1, 1):
population[i] = mutate(crossover(population[bestIndex], population[secondBestIndex]))
generation = generation + 1

Cross Over Function:

交叉功能:

The cross over function takes Parent 1 (A) and 2 (B) breeding them together to create a new formation, a creation so to speak. It is the outcome of two individuals or parents breeding to create a new child, a crossover of features to create it.

交叉功能将父级1(A)和父级2(B)育种在一起以创建新的编队,可以这么说。 这是两个人或父母育成一个新孩子的结果,一个新功能的交叉。

def crossover(parentA, parentB):
splitLength = int(random.random() * int(len(parentA) - 1))
child = substring(parentA, 0, splitLength) + substring(parentB, splitLength, len(parentB))
return child

Fitness Function:

健身功能:

This function determines the fitness, the value of 1’s to each individual, ranking them to create the ideal final generation, the feedback.

此功能确定适合度,每个人的1的值,对他们进行排名以创建理想的最终代(反馈)。

def fitness(individual):
countOfOnes = 0
for i in range(0, len(individual) - 1 + 1, 1):
if individual[i] == "1":
countOfOnes = countOfOnes + 1
fitness = float(countOfOnes) / len(individual)
return fitness

Mutate Function:

变异功能:

Takes aspects of the parents and applies randomness to form a child that is relevant to the number ‘1’.

从父母的角度出发,运用随机性形成与数字“ 1”相关的孩子。

def mutate(individual):
if int(random.random() * 2) == 0:
pass
else:
changeIndex = int(random.random() * len(individual))
if individual[changeIndex] == "0":
changedCharacter = "1"
else:
changedCharacter = "0"
individual = substring(individual, 0, changeIndex) + changedCharacter + substring(individual, changeIndex + 1, len(individual))
return individual

Other functions:

其他功能:

Other functions were ‘printArray’ and ‘substring’ function as can be seen in the full code on my GitHub, linked below. These functions necessitated to the aspects of printing relevant information to the screen respective of the Genetic Algorithms outputs.

其他功能是'printArray'和'substring'函数,可以在我的GitHub的完整代码中看到,链接如下。 这些功能对于将相关信息打印到屏幕上的遗传算法输出的各个方面都是必需的。

结论: (In Conclusion:)

This is a simple example of a genetic algorithm to show how one works. Its main aim is to get to a full set of ‘1’s’ rather than ‘0’s’ after breeding so many generations.

这是一个遗传算法的简单示例,以展示其工作原理。 它的主要目的是在繁殖了许多世代之后获得完整的“ 1”而不是“ 0”。

I don’t usually code in Python, but it is an excellent language if you often want to explain and produce something in detail quickly.

我通常不使用Python编写代码,但是如果您经常想快速解释并详细制作某些东西,则它是一种出色的语言。

There are different ways you can code a Genetic Algorithm, as well as mixing it with other programming concepts and data manipulation etc. however I felt that scope of things was diving too deep for the necessities of this example.

可以使用多种方法来编码遗传算法,以及将其与其他编程概念和数据处理等混合使用。但是,我觉得事情的范围对于此示例的必要性而言过于深入。

The full code of this can be viewed and downloaded at

完整的代码可以在以下位置查看和下载

https://github.com/Some-T/Genetic-Algorithm-Python

https://github.com/Some-T/Genetic-Algorithm-Python

翻译自: https://medium.com/@JamieCropley/programming-a-genetic-algorithm-in-python-ff22ca3407dc

python使用遗传算法

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值