{"id":13413345,"url":"https://github.com/MaxHalford/eaopt","last_synced_at":"2025-03-14T19:32:02.770Z","repository":{"id":51679214,"uuid":"50751224","full_name":"MaxHalford/eaopt","owner":"MaxHalford","description":":four_leaf_clover: Evolutionary optimization library for Go (genetic algorithm, partical swarm optimization, differential evolution)","archived":false,"fork":false,"pushed_at":"2024-03-07T11:26:39.000Z","size":584,"stargazers_count":886,"open_issues_count":8,"forks_count":96,"subscribers_count":31,"default_branch":"master","last_synced_at":"2024-10-22T10:33:59.897Z","etag":null,"topics":["differential-evolution","evolutionary-algorithms","evolutionary-computation","genetic-algorithm","go","machine-learning","metaheuristics","optimization","parallel","particle-swarm-optimization","speciation"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/MaxHalford/eaopt","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MaxHalford.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-01-31T00:04:52.000Z","updated_at":"2024-10-22T00:36:58.000Z","dependencies_parsed_at":"2022-08-03T08:30:53.243Z","dependency_job_id":"27dde51f-87a9-4802-a775-61b5e6ed16ff","html_url":"https://github.com/MaxHalford/eaopt","commit_stats":{"total_commits":293,"total_committers":20,"mean_commits":14.65,"dds":"0.14675767918088733","last_synced_commit":"acd0f5778f8e7eca8524d53cb9b236cecb67e7ba"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxHalford%2Feaopt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxHalford%2Feaopt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxHalford%2Feaopt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxHalford%2Feaopt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MaxHalford","download_url":"https://codeload.github.com/MaxHalford/eaopt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221498739,"owners_count":16833055,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["differential-evolution","evolutionary-algorithms","evolutionary-computation","genetic-algorithm","go","machine-learning","metaheuristics","optimization","parallel","particle-swarm-optimization","speciation"],"created_at":"2024-07-30T20:01:38.269Z","updated_at":"2025-03-14T19:32:02.737Z","avatar_url":"https://github.com/MaxHalford.png","language":"Go","readme":"\u003cdiv align=\"center\"\u003e\n  \u003c!-- Logo --\u003e\n  \u003cimg src=\"https://docs.google.com/drawings/d/e/2PACX-1vRzNhZcghWl-xRaynEAQn4moZeyh6pmD8helS099eJ7V_TRGM9dE6e0T5IF9bOG7S4K62CKZtSUpans/pub?w=477\u0026h=307\" alt=\"logo\"/\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003c!-- Awesome Go --\u003e\n  \u003ca href=\"https://github.com/avelino/awesome-go\"\u003e\n    \u003cimg src=\"https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg\" alt=\"awesome_go\" /\u003e\n  \u003c/a\u003e\n  \u003c!-- Awesome Machine Learning --\u003e\n  \u003ca href=\"https://github.com/josephmisiti/awesome-machine-learning\"\u003e\n    \u003cimg src=\"https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg\" alt=\"awesome_ml\" /\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n\u003cbr/\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003c!-- godoc --\u003e\n  \u003ca href=\"https://godoc.org/github.com/MaxHalford/eaopt\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square\" alt=\"godoc\" /\u003e\n  \u003c/a\u003e\n  \u003c!-- Build status --\u003e\n  \u003ca href=\"https://travis-ci.org/MaxHalford/eaopt\"\u003e\n    \u003cimg src=\"https://img.shields.io/travis/MaxHalford/eaopt/master.svg?style=flat-square\" alt=\"build_status\" /\u003e\n  \u003c/a\u003e\n  \u003c!-- Test coverage --\u003e\n  \u003ca href=\"https://coveralls.io/github/MaxHalford/eaopt?branch=master\"\u003e\n    \u003cimg src=\"https://coveralls.io/repos/github/MaxHalford/eaopt/badge.svg?branch=master\u0026style=flat-square\" alt=\"test_coverage\" /\u003e\n  \u003c/a\u003e\n  \u003c!-- Go report card --\u003e\n  \u003ca href=\"https://goreportcard.com/report/github.com/MaxHalford/eaopt\"\u003e\n    \u003cimg src=\"https://goreportcard.com/badge/github.com/MaxHalford/eaopt?style=flat-square\" alt=\"go_report_card\" /\u003e\n  \u003c/a\u003e\n  \u003c!-- Code Climate --\u003e\n  \u003ca href=\"https://codeclimate.com/github/MaxHalford/eaopt\"\u003e\n    \u003cimg src=\"https://codeclimate.com/github/MaxHalford/eaopt/badges/gpa.svg\" alt=\"Code Climate\" /\u003e\n  \u003c/a\u003e\n  \u003c!-- Hound --\u003e\n  \u003ca href=\"https://houndci.com\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg\" alt=\"Hound\" /\u003e\n  \u003c/a\u003e\n  \u003c!-- License --\u003e\n  \u003ca href=\"https://opensource.org/licenses/MIT\"\u003e\n    \u003cimg src=\"http://img.shields.io/:license-mit-ff69b4.svg?style=flat-square\" alt=\"license\"/\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n\u003cbr/\u003e\n\n\u003cdiv align=\"center\"\u003eeaopt is an evolutionary optimization library\u003c/div\u003e\n\n\u003cbr/\u003e\n\n**Table of Contents**\n  - [Changelog](#changelog)\n  - [Example](#example)\n  - [Background](#background)\n  - [Features](#features)\n  - [Usage](#usage)\n    - [General advice](#general-advice)\n    - [Genetic algorithms](#genetic-algorithms)\n      - [Overview](#overview)\n      - [Implementing the Genome interface](#implementing-the-genome-interface)\n      - [Instantiating the GA struct](#instantiating-the-ga-struct)\n      - [Calling the Minimize method](#calling-the-minimize-method)\n      - [Using the Slice interface](#using-the-slice-interface)\n      - [Models](#models)\n      - [Multiple populations and migration](#multiple-populations-and-migration)\n      - [Speciation](#speciation)\n      - [Logging population statistics](#logging-population-statistics)\n    - [Particle swarm optimization](#particle-swarm-optimization)\n    - [Differential evolution](#differential-evolution)\n    - [OpenAI evolution strategy](#openai-evolution-strategy)\n    - [Hill climbing](#hill-climbing)\n    - [Simulated annealing](#simulated-annealing)\n  - [A note on parallelism](#a-note-on-parallelism)\n  - [FAQ](#faq)\n  - [Dependencies](#dependencies)\n  - [License](#license)\n\n## Changelog\n\n- **11/11/18**: a simple version of [OpenAI's evolution strategy](https://blog.openai.com/evolution-strategies/) has been implemented, it's called `OES`.\n- **02/08/18**: gago has now become eaopt. You can still everything you could do before but the scope is now larger than genetic algorithms. The goal is to implement many more evolutionary optimization algorithms on top of the existing codebase.\n\n## Example\n\nThe following example attempts to minimize the [Drop-Wave function](https://www.sfu.ca/~ssurjano/drop.html) using a genetic algorithm. The Drop-Wave function is known to have a minimum value of -1 when each of it's arguments is equal to 0.\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/MaxHalford/eaopt-examples/blob/master/drop_wave/chart.png\" alt=\"drop_wave_chart\" /\u003e\n  \u003cimg src=\"https://github.com/MaxHalford/eaopt-examples/blob/master/drop_wave/function.png\" alt=\"drop_wave_function\" /\u003e\n\u003c/div\u003e\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    m \"math\"\n    \"math/rand\"\n\n    \"github.com/MaxHalford/eaopt\"\n)\n\n// A Vector contains float64s.\ntype Vector []float64\n\n// Evaluate a Vector with the Drop-Wave function which takes two variables as\n// input and reaches a minimum of -1 in (0, 0). The function is simple so there\n// isn't any error handling to do.\nfunc (X Vector) Evaluate() (float64, error) {\n    var (\n        numerator   = 1 + m.Cos(12*m.Sqrt(m.Pow(X[0], 2)+m.Pow(X[1], 2)))\n        denominator = 0.5*(m.Pow(X[0], 2)+m.Pow(X[1], 2)) + 2\n    )\n    return -numerator / denominator, nil\n}\n\n// Mutate a Vector by resampling each element from a normal distribution with\n// probability 0.8.\nfunc (X Vector) Mutate(rng *rand.Rand) {\n    eaopt.MutNormalFloat64(X, 0.8, rng)\n}\n\n// Crossover a Vector with another Vector by applying uniform crossover.\nfunc (X Vector) Crossover(Y eaopt.Genome, rng *rand.Rand) {\n    eaopt.CrossUniformFloat64(X, Y.(Vector), rng)\n}\n\n// Clone a Vector to produce a new one that points to a different slice.\nfunc (X Vector) Clone() eaopt.Genome {\n    var Y = make(Vector, len(X))\n    copy(Y, X)\n    return Y\n}\n\n// VectorFactory returns a random vector by generating 2 values uniformally\n// distributed between -10 and 10.\nfunc VectorFactory(rng *rand.Rand) eaopt.Genome {\n    return Vector(eaopt.InitUnifFloat64(2, -10, 10, rng))\n}\n\nfunc main() {\n    // Instantiate a GA with a GAConfig\n    var ga, err = eaopt.NewDefaultGAConfig().NewGA()\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n\n    // Set the number of generations to run for\n    ga.NGenerations = 10\n\n    // Add a custom print function to track progress\n    ga.Callback = func(ga *eaopt.GA) {\n        fmt.Printf(\"Best fitness at generation %d: %f\\n\", ga.Generations, ga.HallOfFame[0].Fitness)\n    }\n\n    // Find the minimum\n    err = ga.Minimize(VectorFactory)\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n}\n\n```\n\n```sh\n\u003e\u003e\u003e Best fitness at generation 0: -0.550982\n\u003e\u003e\u003e Best fitness at generation 1: -0.924220\n\u003e\u003e\u003e Best fitness at generation 2: -0.987282\n\u003e\u003e\u003e Best fitness at generation 3: -0.987282\n\u003e\u003e\u003e Best fitness at generation 4: -0.987282\n\u003e\u003e\u003e Best fitness at generation 5: -0.987282\n\u003e\u003e\u003e Best fitness at generation 6: -0.987282\n\u003e\u003e\u003e Best fitness at generation 7: -0.997961\n\u003e\u003e\u003e Best fitness at generation 8: -0.999954\n\u003e\u003e\u003e Best fitness at generation 9: -0.999995\n\u003e\u003e\u003e Best fitness at generation 10: -0.999999\n```\n\nAll the examples can be found [in this repository](https://github.com/MaxHalford/eaopt-examples).\n\n## Background\n\nEvolutionary optimization algorithms are a subdomain of evolutionary computation. Their goal is to minimize/maximize a function without using any gradient information (usually because there isn't any gradient available). They share the common property of exploring the search space by breeding, mutating, evaluating, and sorting so-called *individuals*. Most evolutionary algorithms are designed to handle real valued functions, however in practice they are commonly used for handling more exotic problems. For example genetic algorithms can be used to find the optimal structure of a neural network.\n\neaopt provides implementations for various evolutionary optimization algorithms. Implementation-wise, the idea is that most (if not all) of said algorithms can be written as special cases of a genetic algorithm. Indeed this is made possible by using a generic definition of a genetic algorithm by allowing the mutation, crossover, selection, and replacement procedures to be modified at will. The `GA` struct is thus the most flexible struct of eaopt, the other algorithms are written on top of it. If you don't find any algorithm that suits your need then you can easily write your own operators (as is done in most of the [examples](https://github.com/MaxHalford/eaopt-examples)).\n\n## Features\n\n- Different evolutionary algorithms are available with a consistent API\n- You can practically do anything by using the `GA` struct\n- Speciation and migration procedures are available\n- Common genetic operators (mutation, crossover, selection, migration, speciation) are already implemented\n- Function evaluation can be done in parallel if your function is costly\n\n## Usage\n\n### General advice\n\n- Evolutionary algorithms are usually designed for solving specific kinds of problems. Take a look at the `Minimize` function of each method to get an idea of what type of function it can optimize.\n- Use the associated constructor function of each method to initialize it. For example use the `NewPSO` function instead of instantiating the `PSO` struct yourself. Along with making your life easier, these functions provide the added benefit of checking for parameter input errors.\n- If you're going to use the `GA` struct then be aware that some evolutionary operators are already implemented in eaopt (you don't necessarily have to reinvent the wheel).\n- Don't feel overwhelmed by the fact that algorithms are implemented as special cases of genetic algorithms. It doesn't matter if you just want to get things done, it just makes things easier under the hood.\n\n### Genetic algorithms\n\n#### Overview\n\nGenetic algorithms are the backbone of eaopt. Most of the other algorithms available in eaopt are implemented as special cases of GAs. A GA isn't an algorithm per say, but rather a blueprint which can be used to optimize any kind of problem.\n\nIn a nutshell, a GA solves an optimization problem by doing the following:\n\n1. Generate random solutions to a problem.\n2. Assign a fitness to each solutions.\n3. Check if a new best solution has been found.\n4. Apply genetic operators following a given evolutionary model.\n5. Repeat from step 2 until the stopping criterion is satisfied.\n\nThis description is voluntarily vague. It is up to the user to define the problem and the genetic operators to use. Different categories of genetic operators exist:\n\n- Mutation operators modify an existing solution.\n- Crossover operators generate a new solution by combining two or more existing ones.\n- Selection operators selects individuals that are to be evolved.\n- Migration swaps individuals between populations.\n- Speciation clusters individuals into subpopulations.\n\nPopular stopping criteria include\n\n- a fixed number of generations,\n- a fixed duration,\n- an indicator that the population is stagnating.\n\nGenetic algorithms can be used via the `GA` struct. The necessary steps for using the GA struct are\n\n1. Implement the `Genome` interface to model your problem\n2. Instantiate a `GA` struct (preferably via the `GAConfig` struct)\n3. Call the GA's `Minimize` function and check the `HallOfFame` field\n\n\n#### Implementing the Genome interface\n\nTo use the `GA` struct you first have to implement the `Genome` interface, which is used to define the logic that is specific to your problem (logic that eaopt doesn't know about). For example this is where you will define an `Evaluate()` method for evaluating a particular problem. The `GA` struct contains context-agnostic information. For example this is where you can choose the number of individuals in a population (which is a separate concern from your particular problem). Apart from a good design pattern, decoupling the problem definition from the optimization through the `Genome` interface means that eaopt can be used to optimize *any* kind of problem.\n\nLet's have a look at the `Genome` interface.\n\n```go\ntype Genome interface {\n    Evaluate() (float64, error)\n    Mutate(rng *rand.Rand)\n    Crossover(genome Genome, rng *rand.Rand)\n    Clone() Genome\n}\n```\n\nThe `Evaluate()` method returns the fitness of a genome. The sweet thing is that you can do whatever you want in this method. Your struct that implements the interface doesn't necessarily have to be a slice. The `Evaluate()` method is *your* problem to deal with. eaopt only needs it's output to be able to function. You can also return an `error` which eaopt will catch and return when calling `ga.Initialize()` and `ga.Evolve()`.\n\nThe `Mutate(rng *rand.Rand)` method is where you can modify an existing solution by tinkering with it's variables. The way in which you should mutate a solution essentially boils down to your particular problem. eaopt provides some common mutation methods that you can use instead of reinventing the wheel -- this is what is being done in most of the [examples](https://github.com/MaxHalford/eaopt-examples).\n\nThe `Crossover(genome Genome, rng *rand.Rand)` method combines two individuals. The important thing to notice is that the type of first argument differs from the struct calling the method. Indeed the first argument is a `Genome` that has to be casted into your struct before being able to apply a crossover operator. This is due to the fact that Go doesn't provide generics out of the box; it's easier to convince yourself by checking out the examples.\n\nThe `Clone()` method is there to produce independent copies of the struct you want to evolve. This is necessary for internal reasons and ensures that pointer fields are not pointing to identical memory addresses. Usually this is not too difficult implement; you just have to make sure that the clones you produce are not shallow copies of the genome that is being cloned. This is also fairly easy to unit test.\n\nOnce you have implemented the `Genome` interface you have provided eaopt with all the information it couldn't guess for you.\n\n#### Instantiate the GA struct\n\nYou can now instantiate a `GA` and use it to find an optimal solution to your problem. The `GA` struct has a lot of fields, hence the recommended way is to use the `GAConfig` struct and call it's `NewGA` method.\n\nLet's have a look at the `GAConfig` struct.\n\n```go\ntype GAConfig struct {\n    // Required fields\n    NPops        uint\n    PopSize      uint\n    NGenerations uint\n    HofSize      uint\n    Model        Model\n\n    // Optional fields\n\t  ParallelInit bool // Whether to initialize Individuals in parallel or not\n    ParallelEval bool // Whether to evaluate Individuals in parallel or not\n    Migrator     Migrator\n    MigFrequency uint // Frequency at which migrations occur\n    Speciator    Speciator\n    Logger       *log.Logger\n    Callback     func(ga *GA)\n    EarlyStop    func(ga *GA) bool\n    RNG          *rand.Rand\n}\n```\n\n- Required fields\n  - `NPops` determines the number of populations that will be used.\n  - `PopSize` determines the number of individuals inside each population.\n  - `NGenerations` determines for many generations the populations will be evolved.\n  - `HofSize` determines how many of the best individuals should be recorded.\n  - `Model` is a struct that determines how to evolve each population of individuals.\n- Optional fields\n  - `ParallelInit` determines if a population is initialized in parallel. The rule of thumb is to set this to `true` if your genome initialization method is expensive, if not it won't be worth the overhead. Refer to the [section on parallelism](#a-note-on-parallelism) for a more comprehensive explanation.\n  - `ParallelEval` determines if a population is evaluated in parallel. The rule of thumb is to set this to `true` if your `Evaluate` method is expensive, if not it won't be worth the overhead. Refer to the [section on parallelism](#a-note-on-parallelism) for a more comprehensive explanation.\n  - `Migrator` and `MigFrequency` should be provided if you want to exchange individuals between populations in case of a multi-population GA. If not the populations will be run independently. Again this is an advanced concept in the genetic algorithms field that you shouldn't deal with at first.\n  - `Speciator` will split each population in distinct species at each generation. Each specie will be evolved separately from the others, after all the species has been evolved they are regrouped.\n  - `Logger` can be used to record basic population statistics, you can read more about it in the [logging section](#logging-population-statistics).\n  - `Callback` will execute any piece of code you wish every time `ga.Evolve()` is called. `Callback` will also be called when `ga.Initialize()` is. Using a callback can be useful for many things:\n    - Calculating specific population statistics that are not provided by the logger\n    - Changing parameters of the GA after a certain number of generations\n    - Monitoring convergence\n  - `EarlyStop` will be called before each generation to check if the evolution should be stopped early.\n  - `RNG` can be set to make results reproducible. If it is not provided then a default `rand.New(rand.NewSource(time.Now().UnixNano()))` will be used. If you want to make your results reproducible use a constant source, e.g. `rand.New(rand.NewSource(42))`.\n\nOnce you have instantiated a `GAConfig` you can call it's `NewGA` method to obtain a `GA`. The `GA` struct has the following definition:\n\n```go\ntype GA struct {\n    GAConfig\n\n    Populations Populations\n    HallOfFame  Individuals\n    Age         time.Duration\n    Generations uint\n}\n```\n\nNaturally a `GA` stores a copy of the `GAConfig` that was used to instantiate it. Apart from this the following fields are available:\n\n- `Populations` is where all the current populations and individuals are kept.\n- `HallOfFame` contains the `HofSize` best individuals ever encountered. This slice is always sorted, meaning that the first element of the slice will be the best individual ever encountered.\n- `Age` indicates the duration the GA has spent evolving.\n- `Generations` indicates how many how many generations have gone by.\n\nYou could bypass the `NewGA` method instantiate a `GA` with a `GAConfig` but this would leave the `GAConfig`'s fields unchecked for input errors.\n\n\n#### Calling the Minimize method\n\nYou are now all set to find an optimal solution to your problem. To do so you have to call the GA's `Minimize` function which has the following signature:\n\n```go\nfunc (ga *GA) Minimize(newGenome func(rng *rand.Rand) Genome) error\n```\n\nYou have to provide the `Minimize` a function which returns a `Genome`. It is recommended that the `Genome` thus produced contains random values. This is where the connection between the `Genome` interface and the `GA` struct is made.\n\nThe `Minimize` function will return an error (`nil` if everything went okay) once it is done. You can done access the first entry in the `HallOfFame` field to retrieve the best encountered solution.\n\n\n#### Using the Slice interface\n\nClassically GAs are used to optimize problems where the genome has a slice representation - eg. a vector or a sequence of DNA code. Almost all the mutation and crossover algorithms available in eaopt are based on the `Slice` interface which has the following definition.\n\n```go\ntype Slice interface {\n    At(i int) interface{}\n    Set(i int, v interface{})\n    Len() int\n    Swap(i, j int)\n    Slice(a, b int) Slice\n    Split(k int) (Slice, Slice)\n    Append(Slice) Slice\n    Replace(Slice)\n    Copy() Slice\n}\n```\n\nInternally `IntSlice`, `Float64Slice` and `StringSlice` implement this interface so that you can use the available operators for most use cases. If however you wish to use the operators with slices of a different type you will have to implement the `Slice` interface. Although there are many methods to implement, they are all trivial (have a look at [`slice.go`](slice.go) and the [TSP example](https://github.com/MaxHalford/eaopt-examples/tree/master/tsp_grid).\n\n\n#### Models\n\neaopt makes it easy to use different so called *models*. Simply put, a models defines how a GA evolves a population of individuals through a sequence of genetic operators. It does so without considering whatsoever the intrinsics of the underlying operators. In a nutshell, an evolution model attempts to mimic evolution in the real world. **It's extremely important to choose a good model because it is usually the highest influence on the performance of a GA**.\n\n##### Generational model\n\nThe generational model is one the, if not the most, popular models. Simply put it generates *n* offsprings from a population of size *n* and replaces the population with the offsprings. The offsprings are generated by selecting 2 individuals from the population and applying a crossover method to the selected individuals until the *n* offsprings have been generated. The newly generated offsprings are then optionally mutated before replacing the original population. Crossover generates two new individuals, thus if the population size isn't an even number then the second individual from the last crossover (individual *n+1*) won't be included in the new population.\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://docs.google.com/drawings/d/e/2PACX-1vQrkFXTHkak2GiRpDarsEIDHnsFWqXd9A98Cq2UUIR1keyMSU8NUE8af7_87KiQnmCKKBEb0IiQVsZM/pub?w=960\u0026h=720\" alt=\"generational\" width=\"70%\" /\u003e\n\u003c/div\u003e\n\n##### Steady state model\n\nThe steady state model differs from the generational model in that the entire population isn't replaced between each generations. Instead of adding the children of the selected parents into the next generation, the 2 best individuals out of the two parents and two children are added back into the population so that the population size remains constant. However, one may also replace the parents with the children regardless of their fitness. This method has the advantage of not having to evaluate the newly generated offsprings. Whats more, crossover often generates individuals who are sub-par but who have a lot of potential; giving individuals generated from crossover a chance can be beneficial on the long run.\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://docs.google.com/drawings/d/e/2PACX-1vTTk7b1QS67CZTr7-ksBMlk_cIDhm2YMZjemmrhXbLei5_VgvXCsINCLu8uia3ea6Ouj9I3V5HcZUwS/pub?w=962\u0026h=499\" alt=\"steady-state\" width=\"70%\" /\u003e\n\u003c/div\u003e\n\n##### Select down to size model\n\nThe select down to size model uses two selection rounds. The first one is similar to the one used in the generational model. Parents are selected to generate new individuals through crossover. However, the offsprings are then merged with the original population and a second selection round occurs to determine which individuals will survive to the next generation. Formally *m* offsprings are generated from a population of *n*, the *n+m* individuals are then \"selected down to size\" so that there only remains *n* individuals.\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://docs.google.com/drawings/d/e/2PACX-1vSyXQLPkWOOffKfnTRcdwrKvHTN9rWvdqGVT1fC6vcXGJAQPzxQVmauYLhSd2Xh74vQMhBEnhrSt1od/pub?w=969\u0026h=946\" alt=\"select-down-to-size\" width=\"70%\" /\u003e\n\u003c/div\u003e\n\n##### Ring model\n\nIn the ring model, crossovers are applied to neighbours in a one-directional ring topology. Two by the two neighbours generate 2 offsprings. The best out of the 4 individuals (2 parents + 2 offsprings) replaces the first neighbour.\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://docs.google.com/drawings/d/e/2PACX-1vTCsgqnEXj4KCn_C7IxHZXSw9XMP3RK_YeW5AoVKUSRHzq6CIFlp7fbBA-DK9mtFV330kROwrEsP6tj/pub?w=960\u0026h=625\" alt=\"ring\" width=\"70%\" /\u003e\n\u003c/div\u003e\n\n##### Mutation only\n\nIt's possible to run a GA without crossover simply by mutating individuals. This can be done with the `ModMutationOnly` struct. At each generation each individual is mutated. `ModMutationOnly` has a `strict` field to determine if the mutant should replace the initial individual only if it's fitness is lower.\n\n#### Speciation\n\nClusters, also called species in the literature, are a partitioning of individuals into smaller groups of similar individuals. Programmatically a cluster is a list of lists that each contain individuals. Individuals inside each species are supposed to be similar. The similarity depends on a metric, for example it could be based on the fitness of the individuals. In the literature, speciation is also called *speciation*.\n\nThe purpose of a partitioning individuals is to apply genetic operators to similar individuals. In biological terms this encourages \"incest\" and maintains isolated species. For example in nature animals usually breed with local mates and don't breed with different animal species.\n\nUsing speciation/speciation with genetic algorithms became \"popular\" when they were first applied to the [optimization of neural network topologies](https://www.wikiwand.com/en/Neuroevolution_of_augmenting_topologies). By mixing two neural networks during crossover, the resulting neural networks were often useless because the inherited weights were not optimized for the new topology. This meant that newly generated neural networks were not performing well and would likely disappear during the selection phase. Thus speciation was introduced so that neural networks evolved in similar groups in order for new neural networks wouldn't disappear immediately. Instead the similar neural networks would evolve between each other until they were good enough to mixed with the other neural networks.\n\nWith eaopt it's possible to use speciation on top of all the rest. To do so the `Speciator` field of the `GA` struct has to specified.\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://docs.google.com/drawings/d/e/2PACX-1vRLr7j4ML-ZeXFfvjko9aepRAkCgBlpg4dhuWhB-vXCQ17gJFmDQHrcUbcPFwlqzvaPAXwDxx5ld1kf/pub?w=686\u0026h=645\" alt=\"speciation\" width=\"70%\" /\u003e\n\u003c/div\u003e\n\n#### Multiple populations and migration\n\nMulti-populations GAs run independent populations in parallel. They are not frequently used, however they are very easy to understand and to implement. In eaopt a `GA` struct contains a `Populations` field which stores each population in a slice. The number of populations is specified in the `GAConfig`'s `NPops` field.\n\nIf `Migrator` and `MigFrequency` are not provided the populations will be run independently in parallel. However, if they are provided then at each generation number that is divisible by `MigFrequency` (for example 5 divides generation number 25) individuals will be exchanged between the populations following the `Migrator`.\n\nUsing multi-populations can be an easy way to gain in diversity. Moreover, not using multi-populations on a multi-core architecture is a waste of resources.\n\nWith eaopt you can use multi-populations and speciation at the same time. The following flowchart shows what that would look like.\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://docs.google.com/drawings/d/14VVpTkWquhrcG_oQ61hvZgjKlYWZs_UZRVnL22HFYKM/pub?w=1052\u0026h=607\" alt=\"multi-population_and_speciation\" width=\"70%\" /\u003e\n\u003c/div\u003e\n\n\n#### Logging population statistics\n\nIt's possible to log statistics for each population at every generation. To do so you simply have to provide the `GA` struct a `Logger` from the Go standard library. This is quite convenient because it allows you to decide where to write the log output, whether it be in a file or directly in the standard output.\n\n```go\nga.Logger = log.New(os.Stdout, \"\", log.Ldate|log.Ltime)\n```\n\nIf a logger is provided, each row in the log output will include\n\n- the population ID,\n- the population minimum fitness,\n- the population maximum fitness,\n- the population average fitness,\n- the population's fitness standard deviation.\n\n### Particle swarm optimization\n\n#### Description\n\n[Particle swarm optimization (PSO)](https://www.wikiwand.com/en/Particle_swarm_optimization) can be used to optimize real valued functions. It maintains a population of candidate solutions called particles. The particles move around the search-space according to a mathematical formula that takes as input the particle's position and it's velocity. Each particle's movement is influenced by its's local best encountered position, as well as the best overall position in the search-space (these values are updated after each generation). This is expected to move the swarm toward the best solutions.\n\nAs can be expected there are many variants of PSO. The `SPSO` struct implements the [SPSO-2011 standard](http://clerc.maurice.free.fr/pso/SPSO_descriptions.pdf).\n\n#### Example\n\nIn this example we're going to minimize th [Styblinski-Tang function](https://www.sfu.ca/~ssurjano/stybtang.html) with two dimensions. The global minimum is about -39.16599 times the number of dimensions.\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    m \"math\"\n    \"math/rand\"\n\n    \"github.com/MaxHalford/eaopt\"\n)\n\nfunc StyblinskiTang(X []float64) (y float64) {\n    for _, x := range X {\n        y += m.Pow(x, 4) - 16*m.Pow(x, 2) + 5*x\n    }\n    return 0.5 * y\n}\n\nfunc main() {\n    // Instantiate SPSO\n    var spso, err = eaopt.NewDefaultSPSO()\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n\n    // Fix random number generation\n    spso.GA.RNG = rand.New(rand.NewSource(42))\n\n    // Run minimization\n    _, y, err := spso.Minimize(StyblinskiTang, 2)\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n\n    // Output best encountered solution\n    fmt.Printf(\"Found minimum of %.5f, the global minimum is %.5f\\n\", y, -39.16599*2)\n}\n```\n\nThis should produce the following output.\n\n```sh\n\u003e\u003e\u003e Found minimum of -78.23783, the global minimum is -78.33198\n```\n\n#### Parameters\n\nYou can (and should) instantiate an `SPSO` with the `NewSPSO` method. You can also use the `NewDefaultSPSO` method as is done in the previous example.\n\n```go\nfunc NewSPSO(nParticles, nSteps uint, min, max, w float64, parallel bool, rng *rand.Rand) (*SPSO, error)\n```\n\n- `nParticles` is the number of particles to use\n- `nSteps` is the number of steps during which evolution occurs\n- `min` and `max` are the boundaries from which the initial values are sampled from\n- `w` is the velocity amplifier\n- `parallel` determines if the particles are evaluated in parallel or not\n- `rng` is a random number generator, you can set it to `nil` if you want it to be random\n\n### Differential evolution\n\n#### Description\n\n[Differential evolution (DE)](https://www.wikiwand.com/en/Differential_evolution) somewhat resembles PSO and is also used for optimizing real-valued functions. At each generation, each so-called agent is moved according to the position of 3 randomly sampled agents. If the new position is not better than the current one then it is discarded.\n\n#### Example\n\nIn this example we're going to minimize the [Ackley function](https://www.sfu.ca/~ssurjano/ackley.html) with two dimensions. The global minimum is 0.\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    m \"math\"\n    \"math/rand\"\n\n    \"github.com/MaxHalford/eaopt\"\n)\n\nfunc Ackley(x []float64) float64 {\n    var (\n        a, b, c = 20.0, 0.2, 2 * m.Pi\n        s1, s2  float64\n        d       = float64(len(x))\n    )\n    for _, xi := range x {\n        s1 += xi * xi\n        s2 += m.Cos(c * xi)\n    }\n    return -a*m.Exp(-b*m.Sqrt(s1/d)) - m.Exp(s2/d) + a + m.Exp(1)\n}\n\nfunc main() {\n    // Instantiate DiffEvo\n    var de, err = eaopt.NewDefaultDiffEvo()\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n\n    // Fix random number generation\n    de.GA.RNG = rand.New(rand.NewSource(42))\n\n    // Run minimization\n    _, y, err := de.Minimize(Ackley, 2)\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n\n    // Output best encountered solution\n    fmt.Printf(\"Found minimum of %.5f, the global minimum is 0\\n\", y)\n}\n```\n\nThis should produce the following output.\n\n```sh\n\u003e\u003e\u003e Found minimum of 0.00001, the global minimum is 0\n```\n\n#### Parameters\n\nYou can (and should) instantiate an `DiffEvo` with the `NewDiffEvo` method. You can also use the `NewDefaultDiffEvo` method as is done in the previous example.\n\n```go\nfunc NewDiffEvo(nAgents, nSteps uint, min, max, cRate, dWeight float64, parallel bool, rng *rand.Rand) (*DiffEvo, error)\n```\n\n- `nAgents` is the number of agents to use (it has to be at least 4)\n- `nSteps` is the number of steps during which evolution occurs\n- `min` and `max` are the boundaries from which the initial values are sampled from\n- `cRate` is the crossover rate\n- `dWeight` is the differential weight\n- `parallel` determines if the agents are evaluated in parallel or not\n- `rng` is a random number generator, you can set it to `nil` if you want it to be random\n\n\n### OpenAI evolution strategy\n\n#### Description\n\n[OpenAI](https://openai.com/) proposed [a simple evolution strategy](https://blog.openai.com/evolution-strategies/) based on the use of natural gradients. The algorithm is dead simple:\n\n1. Choose a center `mu` at random\n2. Sample points around `mu` using a normal distribution\n3. Evaluate each point and obtain the natural gradient `g`\n4. Move `mu` along the natural gradient `g` using a learning rate\n5. Repeat from step 2 until satisfied\n\n#### Example\n\nIn this example we're going to minimize the [Rastrigin function](https://www.sfu.ca/~ssurjano/rastr.html) with three dimensions. The global minimum is 0.\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    m \"math\"\n    \"math/rand\"\n\n    \"github.com/MaxHalford/eaopt\"\n)\n\nfunc Rastrigin(x []float64) (y float64) {\n    y = 10 * float64(len(x))\n    for _, xi := range x {\n        y += m.Pow(xi, 2) - 10*m.Cos(2*m.Pi*xi)\n    }\n    return y\n}\n\nfunc main() {\n    // Instantiate DiffEvo\n    var oes, err = eaopt.NewDefaultOES()\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n\n    // Fix random number generation\n    oes.GA.RNG = rand.New(rand.NewSource(42))\n\n    // Run minimization\n    _, y, err := oes.Minimize(Rastrigin, 2)\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n\n    // Output best encountered solution\n    fmt.Printf(\"Found minimum of %.5f, the global minimum is 0\\n\", y)\n}\n```\n\nThis should produce the following output.\n\n```sh\n\u003e\u003e\u003e Found minimum of 0.02270, the global minimum is 0\n```\n\n#### Parameters\n\nYou can (and should) instantiate an `OES` with the `NewOES` method. You can also use the `NewDefaultOES` method as is done in the previous example.\n\n```go\nfunc NewOES(nPoints, nSteps uint, sigma, lr float64, parallel bool, rng *rand.Rand) (*OES, error)\n```\n\n- `nPoints` is the number of points to use (it has to be at least 3)\n- `nSteps` is the number of steps during which evolution occurs\n- `sigma` determines the shape of the normal distribution used to sample new points\n- `lr` is the learning rate\n- `parallel` determines if the agents are evaluated in parallel or not\n- `rng` is a random number generator, you can set it to `nil` if you want it to be random\n\n### Hill climbing\n\n#### Description\n\n[Hill climbing](https://en.wikipedia.org/wiki/Hill_climbing) is a very simple optimization strategy.  It works as follows:\n\n1. Randomly select a point to be the \"current\" point.  Evaluate it.\n2. Randomly alter one of the coordinates, and evaluate this new point.\n3. If the new point is better than the current point, make the new point the current point.  Otherwise, do nothing.\n4. Repeat from step 2 until satisfied.\n\nOne implements hill climbing in eaopt using a genetic algorithm with the `ModMutationOnly` model and `Strict` set to `true`.\n\n#### Example\n\nIn this example we minimize a [Bohachevsky function](https://www.sfu.ca/~ssurjano/boha.html).  The global minimum is 0.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"math\"\n\t\"math/rand\"\n\n\t\"github.com/MaxHalford/eaopt\"\n)\n\n// A Coord2D is a coordinate in two dimensions.\ntype Coord2D struct {\n\tX float64\n\tY float64\n}\n\n// Evaluate evalutes a Bohachevsky function at the current coordinates.\nfunc (c *Coord2D) Evaluate() (float64, error) {\n\tz := c.X*c.X + 2*c.Y*c.Y - 0.3*math.Cos(3*math.Pi*c.X) - 0.4*math.Cos(4*math.Pi*c.Y) + 0.7\n\treturn z, nil\n}\n\n// Mutate replaces one of the current coordinates with a random value in [-100, -100].\nfunc (c *Coord2D) Mutate(rng *rand.Rand) {\n\tif rng.Intn(2) == 0 {\n\t\tc.X = rng.Float64()*200.0 - 100.0\n\t} else {\n\t\tc.Y = rng.Float64()*200.0 - 100.0\n\t}\n}\n\n// Crossover does nothing.  It is defined only so *Coord2D implements the eaopt.Genome interface.\nfunc (c *Coord2D) Crossover(other eaopt.Genome, rng *rand.Rand) {}\n\n// Clone returns a copy of a *Coord2D.\nfunc (c *Coord2D) Clone() eaopt.Genome {\n\treturn \u0026Coord2D{X: c.X, Y: c.Y}\n}\n\nfunc main() {\n\t// Hill climbing is implemented as a GA using the ModMutationOnly model\n\t// with the Strict option.\n\tcfg := eaopt.NewDefaultGAConfig()\n\tcfg.Model = eaopt.ModMutationOnly{Strict: true}\n\tcfg.NGenerations = 9999\n\n\t// Add a custom callback function to track progress.\n\tminFit := math.MaxFloat64\n\tcfg.Callback = func(ga *eaopt.GA) {\n\t\thof := ga.HallOfFame[0]\n\t\tfit := hof.Fitness\n\t\tif fit == minFit {\n\t\t\t// Output only when we make an improvement.\n\t\t\treturn\n\t\t}\n\t\tbest := hof.Genome.(*Coord2D)\n\t\tfmt.Printf(\"Best fitness at generation %4d: %10.5f at (%9.5f, %9.5f)\\n\",\n\t\t\tga.Generations, fit, best.X, best.Y)\n\t\tminFit = fit\n\t}\n\n\t// Run the hill-climbing algorithm.\n\tga, err := cfg.NewGA()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = ga.Minimize(func(rng *rand.Rand) eaopt.Genome {\n\t\treturn \u0026Coord2D{\n\t\t\tX: rng.Float64()*200.0 - 100.0,\n\t\t\tY: rng.Float64()*200.0 - 100.0,\n\t\t}\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Output the best encountered solution.\n\tbest := ga.HallOfFame[0].Genome.(*Coord2D)\n\tfmt.Printf(\"Found a minimum at (%.5f, %.5f).\\n\", best.X, best.Y)\n\tfmt.Println(\"The global minimum is known to lie at (0, 0).\")\n}\n```\n\nThis should produce output along the following lines:\n\n```sh\n\u003e\u003e\u003e Best fitness at generation    0:  347.29009 at (-17.77572,   3.91475)\n\u003e\u003e\u003e Best fitness at generation    1:   91.64634 at ( -7.79357,  -3.88125)\n\u003e\u003e\u003e Best fitness at generation    7:   84.31780 at (  8.42342,  -2.53296)\n\u003e\u003e\u003e Best fitness at generation    8:   22.81205 at (  3.64011,  -2.09039)\n\u003e\u003e\u003e Best fitness at generation   13:    3.17994 at ( -1.37117,   0.63824)\n\u003e\u003e\u003e Best fitness at generation   25:    2.58142 at ( -1.05701,  -0.29836)\n\u003e\u003e\u003e Best fitness at generation   26:    2.34391 at (  0.96249,  -0.57655)\n\u003e\u003e\u003e Best fitness at generation   70:    1.21768 at (  0.23307,   0.17318)\n\u003e\u003e\u003e Best fitness at generation  117:    0.78975 at (  0.68306,  -0.10444)\n\u003e\u003e\u003e Best fitness at generation  119:    0.71870 at ( -0.10934,  -0.14860)\n\u003e\u003e\u003e Best fitness at generation  130:    0.47829 at (  0.02383,   0.46641)\n\u003e\u003e\u003e Best fitness at generation  277:    0.11108 at (  0.00798,  -0.05851)\n\u003e\u003e\u003e Best fitness at generation  492:    0.07006 at ( -0.07098,   0.00333)\n\u003e\u003e\u003e Best fitness at generation  860:    0.03000 at (  0.01445,  -0.02850)\n\u003e\u003e\u003e Best fitness at generation 1149:    0.02192 at ( -0.03899,   0.00333)\n\u003e\u003e\u003e Best fitness at generation 1270:    0.02055 at ( -0.03333,   0.01191)\n\u003e\u003e\u003e Best fitness at generation 1596:    0.00650 at ( -0.02072,   0.00333)\n\u003e\u003e\u003e Best fitness at generation 2945:    0.00249 at ( -0.00509,   0.00794)\n\u003e\u003e\u003e Best fitness at generation 5551:    0.00134 at (  0.00482,  -0.00548)\n\u003e\u003e\u003e Best fitness at generation 6551:    0.00063 at ( -0.00390,  -0.00350)\n\u003e\u003e\u003e Best fitness at generation 6780:    0.00056 at (  0.00364,   0.00333)\n\u003e\u003e\u003e Found a minimum at (0.00364, 0.00333).\n\u003e\u003e\u003e The global minimum is known to lie at (0, 0).\n```\n\n### Simulated Annealing\n\n#### Description\n\n[Simulated annealing](https://en.wikipedia.org/wiki/Simulated_annealing) is an optimization strategy inspired by metallurgical annealing.  It works as follows:\n\n1. Randomly select a point to be the current point. Evaluate it.\n2. Select a nearby point (i.e., via a small mutation), and evaluate it.\n3. If the new point is better than the current point, make the new point the current point.\n4. If the new point is worse than the current point, decide randomly—but with decreasing likelihood—whether make the new point the current point.\n5. Repeat from step 2 for a given number of generations.\n\nOne implements simulated annealing in eaopt using a genetic algorithm with the `ModSimulatedAnnealing` model.  `ModSimulatedAnnealing` takes a user-defined function `Accept` that returns a probability of moving to a worse point as a function of the current generation number, the total number of generations, the fitness of the current point and the fitness of the mutated point.  It is assumed that `Accept` is non-increasing with the generation number.\n\nAn `Accept` function that returns a constant `0` (never accepting a worse move) is equivalent to the `ModMutationOnly` model with `Strict: true`:\n```Go\nfunc(g, ng uint, e0, e1 float64) float64 { return 0.0 }\n```\nLikewise, an `Accept` function that returns a constant `1` (always accepting a worse move) is equivalent to the `ModMutationOnly` model with `Strict: false`:\n```Go\nfunc(g, ng uint, e0, e1 float64) float64 { return 1.0 }\n```\nMore commonly, `Accept` will return a function of \"temperature\", which drops from 1 to 0 as `g` increases from 0 to `ng`.  Returning the temperature itself is perfectly legitimate:\n```Go\nfunc(g, ng uint, e0, e1 float64) float64 {\n\tt := 1.0 - float64(g)/float64(ng)\n\treturn t\n}\n```\nAn exponential causes the acceptance probability to drop quickly at first and slowly later, ending a bit above zero:\n```Go\nfunc(g, ng uint, e0, e1 float64) float64 {\n\tt := 1.0 - float64(g)/float64(ng)\n\treturn math.Exp(-3.0 * t)\n}\n```\nA cosine causes the acceptance probability to drop slowly at the beginning and ending of the evolution and quickly in between:\n```Go\nfunc(g, ng uint, e0, e1 float64) float64 {\n\tt := 1.0 - float64(g)/float64(ng)\n\treturn (math.Cos(t*math.Pi) + 1.0) / 2.0\n}\n```\n\nThe following figure graphs the preceding three `Accept` functions:\n![probability](https://github.com/MaxHalford/eaopt/assets/650041/5a7a0c0f-5c64-40bf-95ca-1db04e58d577)\n\u003c!--\nGnuplot command for the above:\n\nplot [0:1] 1-x title 'p(t) = 1-t', exp(-3*x) title 'p(t) = exp(-3*t)', (cos(x*pi)+1)/2 title 'p(t) = (cos(t*π)+1)/2'\n--\u003e\n\nThe `e0` and `e1` parameters can be used to make the acceptance probability also a function of how much worse the mutation is than its parent.  However, doing so requires *a priori* knowledge of the range of values `e0` and `e1` can take, which is not available in many cases.\n\n#### Example\n\nThe following is a complete program that uses simulated annealing to find a minimum of the [Holder table function](https://www.sfu.ca/~ssurjano/holder.html):\n\n```Go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"math\"\n\t\"math/rand\"\n\n\t\"github.com/MaxHalford/eaopt\"\n)\n\n// A Coord2D is a coordinate in two dimensions.\ntype Coord2D struct {\n\tX float64\n\tY float64\n}\n\n// Evaluate evaluates the Holder-table function at the current coordinates.\nfunc (c *Coord2D) Evaluate() (float64, error) {\n\tz := -math.Abs(math.Sin(c.X) * math.Cos(c.Y) *\n\t\tmath.Exp(math.Abs(1.0-math.Sqrt(c.X*c.X+c.Y*c.Y)/math.Pi)))\n\treturn z, nil\n}\n\n// Mutate replaces one of the current coordinates with a random value in [-10, 10).\nfunc (c *Coord2D) Mutate(rng *rand.Rand) {\n\tv := rng.Float64()*20.0 - 10.0\n\tif rng.Intn(2) == 0 {\n\t\tc.X = v\n\t} else {\n\t\tc.Y = v\n\t}\n}\n\n// Crossover does nothing.  It is defined only so *Coord2D implements the eaopt.Genome interface.\nfunc (c *Coord2D) Crossover(other eaopt.Genome, rng *rand.Rand) {}\n\n// Clone returns a copy of a *Coord2D.\nfunc (c *Coord2D) Clone() eaopt.Genome {\n\tcc := *c\n\treturn \u0026cc\n}\n\nfunc main() {\n\t// Simulated annealing is implemented as a GA using the ModSimulatedAnnealing model.\n\tcfg := eaopt.NewDefaultGAConfig()\n\tcfg.Model = eaopt.ModSimulatedAnnealing{\n\t\tAccept: func(g, ng uint, e0, e1 float64) float64 {\n\t\t\t// Accept readily in early generations but reluctantly in later generations.\n\t\t\tt := 1.0 - float64(g)/float64(ng)\n\t\t\treturn (math.Cos(t*math.Pi) + 1.0) / 2.0\n\t\t},\n\t}\n\tcfg.NGenerations = 999\n\n\t// Add a custom callback function to track progress.\n\tminFit := math.MaxFloat64\n\tcfg.Callback = func(ga *eaopt.GA) {\n\t\thof0 := ga.HallOfFame[0]\n\t\tfit := hof0.Fitness\n\t\tif fit == minFit {\n\t\t\t// Output only when we make an improvement.\n\t\t\treturn\n\t\t}\n\t\tbest := hof0.Genome.(*Coord2D)\n\t\tfmt.Printf(\"Best fitness at generation %3d: %10.5f at (%9.5f, %9.5f)\\n\",\n\t\t\tga.Generations, fit,\n\t\t\tbest.X, best.Y)\n\t\tminFit = fit\n\t}\n\n\t// Run the simulated-annealing algorithm.\n\tga, err := cfg.NewGA()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\terr = ga.Minimize(func(rng *rand.Rand) eaopt.Genome {\n\t\tvar c Coord2D\n\t\tc.X = rng.Float64()\n\t\tc.Y = rng.Float64()\n\t\treturn \u0026c\n\t})\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Output the best encountered solution.\n\thof0 := ga.HallOfFame[0]\n\tbest := hof0.Genome.(*Coord2D)\n\tfmt.Printf(\"Found a minimum at (%.5f, %.5f) --\u003e %.5f.\\n\",\n\t\tbest.X, best.Y, hof0.Fitness)\n\tfmt.Println(\"The global minimum is known to lie at (±8.05502, ±9.66459) and have value -19.20850.\")\n}\n```\nThis should produce output like the following:\n```\nBest fitness at generation   0:   -1.62856 at (  0.97142,   0.13185)\nBest fitness at generation   1:   -5.70555 at (  0.81465,  -9.68769)\nBest fitness at generation   2:   -8.22368 at ( -8.35803,   6.91242)\nBest fitness at generation   3:  -13.40055 at (  7.36818,  -9.18324)\nBest fitness at generation   6:  -16.45462 at ( -7.49961,  -9.68769)\nBest fitness at generation   7:  -16.53317 at (  8.50224,  -9.39214)\nBest fitness at generation   8:  -19.06737 at ( -7.93491,   9.65411)\nBest fitness at generation  12:  -19.18453 at (  8.02260,  -9.70168)\nBest fitness at generation  44:  -19.18972 at ( -8.04929,   9.70731)\nBest fitness at generation  80:  -19.19616 at ( -8.04283,   9.63179)\nBest fitness at generation  82:  -19.20817 at (  8.05422,   9.65894)\nFound a minimum at (8.05422, 9.65894) --\u003e -19.20817.\nThe global minimum is known to lie at (±8.05502, ±9.66459) and have value -19.20850.\n```\n\n## A note on parallelism\n\nEvolutionary algorithms are famous for being [embarrassingly parallel](https://www.wikiwand.com/en/Embarrassingly_parallel). Most of the operations can be run independently each one from another. For example individuals can be mutated in parallel because mutation doesn't have any side effects.\n\nThe Go language provides nice mechanisms to run stuff in parallel, provided you have more than one core available. However, parallelism is only worth it when the functions you want to run in parallel are heavy. If the functions are cheap then the overhead of spawning routines will be too high and not worth it. It's simply not worth using a routine for each individual because operations at an individual level are often not time consuming enough.\n\nBy default eaopt will evolve populations in parallel. This is because evolving one population implies a lot of operations and parallelism is worth it. If your `Evaluate` method is heavy then it might be worth evaluating individuals in parallel, which can done by setting the `GA`'s `ParallelEval` field to `true`. Evaluating individuals in parallel can be done regardless of the fact that you are using more than one population. If your genome initialization method is heavy then it might be worth initializing individuals in parallel, which can done by setting the `GA`'s `ParallelInit` field to `true`. Initializing individuals in parallel can be done regardless of the fact that you are using more than one population.\n\n\n## FAQ\n\n**What if I don't want to use crossover?**\n\nAlas you still have to implement the `Genome` interface. You can however provide a blank `Crossover` method just to satisfy the interface.\n\n```go\ntype Vector []float64\n\nfunc (X Vector) Crossover(Y eaopt.Genome, rng *rand.Rand) {}\n```\n\n\n**Why aren't my `Mutate` and `Crossover` methods modifying my `Genome`s?**\n\nThe `Mutate` and `Crossover` methods have to modify the values of the `Genome` in-place. The following code will work because the `Vector` is a slice; slices in Go are references to underlying data, hence modifying a slice modifies them in-place.\n\n```go\ntype Vector []float64\n\nfunc (X Vector) Mutate(rng *rand.Rand) {\n    eaopt.MutNormal(X, rng, 0.5)\n}\n```\n\nOn the contrary, mutating other kind of structs will require the `*` symbol to access the struct's pointer. Notice the `*Name` in the following example.\n\n```go\ntype Name string\n\nfunc (n *Name) Mutate(rng *rand.Rand) {\n    n = randomName()\n}\n```\n\n\n**Are evolutionary optimization algorithms any good?**\n\nFor real-valued, differentiable functions, evolutionary optimization algorithms will probably not fair well against methods  based on gradient descent. Intuitively this is because evolutionary optimization algorithms ignore the shape and slope of the function. However gradient descent algorithms usually get stuck in local optimas, whereas evolutionary optimization algorithms don't.\n\nAs mentioned earlier, some problems can simply not be written down as [closed-form expressions](https://www.wikiwand.com/en/Closed-form_expression). In this case methods based on gradient information can't be used whilst evolutionary optimization algorithms can still be used. For example tuning the number of layers and of neurons per layer in a neural network is an open problem that doesn't yet have a reliable solution. Neural networks architectures used in production are usually designed by human experts. The field of [neuroevolution](https://www.wikiwand.com/en/Neuroevolution) aims to train neural networks with evolutionary algorithms.\n\n**How can I contribute?**\n\nFeel free to implement your own operators or to make suggestions! Check out the [CONTRIBUTING file](CONTRIBUTING.md) for some guidelines. [This repository](https://github.com/fcampelo/EC-Bestiary) has a long list of existing evolutionary algorithms.\n\n\n## Dependencies\n\nYou can see the list of dependencies [here](https://godoc.org/github.com/MaxHalford/eaopt?imports) and the graph view [here](https://godoc.org/github.com/MaxHalford/eaopt?import-graph). Here is the list of external dependencies:\n\n- [golang.org/x/sync/errgroup](https://godoc.org/golang.org/x/sync/errgroup)\n\n\n## License\n\nThe MIT License (MIT). Please see the [LICENSE file](LICENSE) for more information.\n","funding_links":[],"categories":["Machine Learning","Evolutionary Algorithms","机器学习","Go","Repositories","Relational Databases"],"sub_categories":["Advanced Console UIs","Vector Database","Search and Analytic Databases","检索及分析资料库","SQL 查询语句构建库","Tools","[Tools](#tools-1)","Speech Recognition","交流"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMaxHalford%2Feaopt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMaxHalford%2Feaopt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMaxHalford%2Feaopt/lists"}