温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Go语言中怎么实现一个遗传算法

发布时间:2021-07-06 15:58:45 来源:亿速云 阅读:128 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关Go语言中怎么实现一个遗传算法,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

Go语言坚决拥护组合(composition),同时也很反对继承的做法,在网络上引起了强烈的讨论,同时也让人们重新思考了语言该往哪个方向发展。所以,从这个角度来看,Go语言与其它语言的差别可能也没有那么大。

本文将重点介绍如何用Go语言实现遗传算法。如果你还没有参加过GoLang Tour,我还建议你快速看一下这门语言的介绍。

话不多说,让我们开始从代码说起吧!***个例子与我以前做过的很类似:找到一个二次的最小值。

type GeneticAlgorithmSettings struct {   PopulationSize int   MutationRate int   CrossoverRate int   NumGenerations int   KeepBestAcrossPopulation bool }  type GeneticAlgorithmRunner interface {   GenerateInitialPopulation(populationSize int) []interface{}   PerformCrossover(individual1, individual2 interface{}, mutationRate int) interface{}   PerformMutation(individual interface{}) interface{}   Sort([]interface{}) }

我立马定义了一组设置,以便在稍后启动的算法中用到。

第二部分的GeneticAlgorithmRunner这个看起来有点奇怪。GeneticAlgorithmRunner是一个接口,询问如何生成初始种群,执行corssovers和mutataions,并对答案进行排序,以便在Population中保持***的个体,这样下一代才会更加优秀。我认为这看起来很奇怪,因为“接口”通常用于面向对象的语言,通常会要求对象实现某些特性和方法。这里没有什么差别。这一小段代码实际上是在说,它正在请求一些东西来定义这些方法的细节。我是这样做的:

type QuadraticGA struct {}  func (l QuadraticGA) GenerateInitialPopulation(populationSize int) []interface{}{   initialPopulation := make([]interface{}, 0, populationSize)   for i:= 0; i < populationSize; i++ {     initialPopulation = append(initialPopulation, makeNewEntry())   }   return initialPopulation }  func (l QuadraticGA) PerformCrossover(result1, result2 interface{}, _ int) interface{}{   return (result1.(float64) + result2.(float64)) / 2 }  func (l QuadraticGA) PerformMutation(_ interface{}, _ int) interface{}{   return makeNewEntry() }  func (l QuadraticGA) Sort(population []interface{}){   sort.Slice(population, func(i, j int) bool {     return calculate(population[i].(float64)) > calculate(population[j].(float64))   }) }

更奇怪的是,我从来没有提到过这些方法的接口。请记住,因为没有对象,也没有继承。QuadraticGA结构体是一个空白对象,隐式地作为GeneticAlgorithmRunner。每个必需的方法都在括号中绑定到该结构体,就像Java中的“@  override”。现在,结构体和设置需要传递给运行该算法的模块。

settings := ga.GeneticAlgorithmSettings{    PopulationSize: 5,    MutationRate: 10,    CrossoverRate: 100,    NumGenerations: 20,    KeepBestAcrossPopulation: true, }  best, err := ga.Run(QuadraticGA{}, settings)  if err != nil {    println(err) }else{    fmt.Printf("Best: x: %f  y: %f\n", best, calculate(best.(float64))) }

很简单,对吧?“QuadraticGA  {}”只是简单地创建了该结构的一个新实例,其余的则由Run()方法完成。该方法返回搜索结果和发生的任何错误,因为Go不相信try /  catch&mdash;&mdash;另一场战争作者采取了严格的设计立场。

现在来计算每个项的性能,以求二次函数求出的二次函数来求出一个新的X值的方法:

func makeNewEntry() float64 {    return highRange * rand.Float64() }  func calculate(x float64) float64 {    return  math.Pow(x, 2) - 6*x + 2 // minimum should be at x=3 }

既然已经为二次实现创建了接口,那么GA本身需要完成:

func Run(geneticAlgoRunner GeneticAlgorithmRunner, settings GeneticAlgorithmSettings) (interface{}, error){     population := geneticAlgoRunner.GenerateInitialPopulation(settings.PopulationSize)     geneticAlgoRunner.Sort(population)     bestSoFar := population[len(population) - 1]     for i:= 0; i < settings.NumGenerations; i++ {        newPopulation := make([]interface{}, 0, settings.PopulationSize)        if settings.KeepBestAcrossPopulation {          newPopulation = append(newPopulation, bestSoFar)       }        // perform crossovers with random selection       probabilisticListOfPerformers := createStochasticProbableListOfIndividuals(population)        newPopIndex := 0       if settings.KeepBestAcrossPopulation{          newPopIndex = 1       }       for ; newPopIndex < settings.PopulationSize; newPopIndex++ {          indexSelection1 := rand.Int() % len(probabilisticListOfPerformers)          indexSelection2 := rand.Int() % len(probabilisticListOfPerformers)           // crossover          newIndividual := geneticAlgoRunner.PerformCrossover(             probabilisticListOfPerformers[indexSelection1],             probabilisticListOfPerformers[indexSelection2], settings.CrossoverRate)           // mutate          if rand.Intn(101) < settings.MutationRate {             newIndividual = geneticAlgoRunner.PerformMutation(newIndividual)          }           newPopulation = append(newPopulation, newIndividual)       }        population = newPopulation        // sort by performance       geneticAlgoRunner.Sort(population)        // keep the best so far       bestSoFar = population[len(population) - 1]     }     return bestSoFar, nil }  func createStochasticProbableListOfIndividuals(population []interface{}) []interface{} {     totalCount, populationLength:= 0, len(population)    for j:= 0; j < populationLength; j++ {       totalCount += j    }     probableIndividuals := make([]interface{}, 0, totalCount)    for index, individual := range population {       for i:= 0; i < index; i++{          probableIndividuals = append(probableIndividuals, individual)       }    }     return probableIndividuals }

很像以前,一个新的人口被创造出来,人口的成员将会世代交配,而他们的后代可能携带突变。一个人的表现越好,就越有可能交配。随着时间的推移,算法收敛到***的答案,或者至少是一个相当不错的答案。

那么当它运行时,它返回了什么呢?

Best: x: 3.072833 y: -6.994695

不坏!由于人口规模只有5、20代,而且输入的范围被限制在[0 100],这一搜索就钉在了顶点上。

现在,您可能想知道为什么我定义了所有的接口方法来返回“接口{}”。这就像Go和generics一样。没有对象,因此没有对象类型返回,但是没有描述的大小的数据仍然可以在堆栈上传递。这本质上也是这个返回类型的含义:它传递一些已知的和类似的类型的对象。有了这个“泛型”,我就可以将GA移动到它自己的包中,并将相同的代码移到多个不同类型的数据上。

我们有两个输入的3D二次方程,而不是一个二维二次方程的单个输入。接口方法只需要很小的改变:

type Quad3D struct {    x, y float64 } func makeNewQuadEntry(newX, newY float64) Quad3D {    return Quad3D{       x: newX,       y: newY,    } }  func calculate3D(entry Quad3D) float64 {    return math.Pow(entry.x, 2)- 6 * entry.x + math.Pow(entry.y, 2)- 6 * entry.y + 2 }  type Quadratic3dGA struct { }  func (l Quadratic3dGA) GenerateInitialPopulation(populationSize int)[]interface{}{     initialPopulation := make([]interface{}, 0, populationSize)    for i:= 0; i < populationSize; i++ { initialPopulation = append(initialPopulation, makeNewQuadEntry(makeNewEntry(), makeNewEntry())) } return initialPopulation } func (l Quadratic3dGA) PerformCrossover(result1, result2 interface{}, mutationRate int) interface{}{ r1Entry, r2Entry := result1.(Quad3D), result2.(Quad3D) return makeNewQuadEntry((r1Entry.x + r2Entry.x) / 2, (r1Entry.y + r2Entry.y) / 2,) } func (l Quadratic3dGA) PerformMutation(_ interface{}) interface{}{ return makeNewQuadEntry(makeNewEntry(), makeNewEntry()) } func (l Quadratic3dGA) Sort(population []interface{}){ sort.Slice(population, func(i, j int) bool { return calculate3D(population[i].(Quad3D)) > calculate3D(population[j].(Quad3D))    }) }  func quadratic3dMain(){    settings := ga.GeneticAlgorithmSettings{       PopulationSize: 25,       MutationRate: 10,       CrossoverRate: 100,       NumGenerations: 20,       KeepBestAcrossPopulation: true,    }     best, err := ga.Run(Quadratic3dGA{}, settings)    entry := best.(Quad3D)     if err != nil {       println(err)    }else{       fmt.Printf("Best: x: %f  y: %f  z: %f\n", entry.x, entry.y, calculate3D(entry))    } }

而不是到处都是float64s,任何地方都可以通过Quad3D的条目;每一个都有一个X和一个Y值。对于创建的每个条目,都使用contructor  makeNewQuadEntry创建。Run()方法中的代码都没有更改。

当它运行时,我们得到这个输出:

Best: x: 3.891671 y: 4.554884 z: -12.787259

很接近了!

哦,我忘了说走快了!在Java中执行此操作时,即使使用相同的设置,也会有明显的等待时间。在一个相对较小的范围内求解二次方程并不是很复杂,但它对一个人来说是值得注意的。

Go是本地编译的,比如C。当二进制执行时,它似乎马上就吐出一个答案。这里有一个简单的方法来度量每次运行的执行时间:

func main() {    beforeQuadTime := time.Now()    quadraticMain()    afterQuadTime := time.Since(beforeQuadTime)    fmt.Printf("%d\n", afterQuadTime)     before3dQuadTime := time.Now()    quadratic3dMain()    after3dQuatTime := time.Since(before3dQuadTime)    fmt.Printf("%d\n", after3dQuatTime) }

边注:我能说我很高兴我们是一个开发者社区,让他们从过去的错误中走出来,并把综合的时间模块和包构建成一种语言吗?Java 8  +拥有它们,Python拥有它们,并拥有它们。这使我开心。

现在的输出:

Best: x: 3.072833 y: -6.994695 136,876 Best: x: 3.891671 y: 4.554884 z: -12.787259 4,142,778

那“近乎瞬间”的感觉是我想要传达的,现在我们有了很难的数字。136,876看起来很大,但要在纳秒内报告时间。

重申一遍:纳秒。不是几毫秒,我们都习惯了在互联网时代或者其他像Python和Java这样的通用语言;纳秒。1/1,000,000毫秒。

这意味着我们在不到一毫秒的时间里找到了一个使用遗传算法来搜索答案的二次方程的答案。这句话,“该死的瞬间”似乎很合适,不是吗?这包括打印到终端。

那么,要计算更密集的东西呢?在我展示一种寻找好的梦幻足球lineups的方法之前,我在Fanduel上使用。这包括从电子表格中读取数据,制作和过滤lineups,并进行更复杂的交叉和突变。强制寻找***解决方案可能需要超过75,000年(至少使用我当时使用的Python)。

我不需要再检查所有的细节,你可以自己去看代码,但我会在这里显示输出:

Best: 121.409960:, $58100 QB: Aaron Rodgers - 23.777778 RB: Latavius Murray - 15.228571 RB: DeMarco Murray - 19.980000 WR: Kelvin Benjamin - 11.800000 WR: Stefon Diggs - 14.312500 WR: Alshon Jeffery - 9.888889 TE: Connor Hamlett - 8.200000 D: Philadelphia Eagles - 10.777778 K: Phil Dawson - 7.444444 16,010,182

上述就是小编为大家分享的Go语言中怎么实现一个遗传算法了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI