{"id":17219504,"url":"https://github.com/kelindar/evolve","last_synced_at":"2025-09-10T00:41:09.398Z","repository":{"id":57545632,"uuid":"294952539","full_name":"kelindar/evolve","owner":"kelindar","description":"Golang implementation of a binary genetic algorithm with random binary crossover \u0026 mutation","archived":false,"fork":false,"pushed_at":"2025-06-17T22:03:32.000Z","size":4568,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-17T23:19:19.459Z","etag":null,"topics":["genetic-algorithm","genetic-programming"],"latest_commit_sha":null,"homepage":"","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/kelindar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["kelindar"]}},"created_at":"2020-09-12T13:47:20.000Z","updated_at":"2025-02-05T07:29:02.000Z","dependencies_parsed_at":"2023-02-09T20:30:52.680Z","dependency_job_id":null,"html_url":"https://github.com/kelindar/evolve","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/kelindar/evolve","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelindar%2Fevolve","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelindar%2Fevolve/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelindar%2Fevolve/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelindar%2Fevolve/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kelindar","download_url":"https://codeload.github.com/kelindar/evolve/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelindar%2Fevolve/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261618125,"owners_count":23185094,"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":["genetic-algorithm","genetic-programming"],"created_at":"2024-10-15T03:50:01.210Z","updated_at":"2025-06-24T06:07:52.228Z","avatar_url":"https://github.com/kelindar.png","language":"Go","funding_links":["https://github.com/sponsors/kelindar"],"categories":[],"sub_categories":[],"readme":"# Genetic Algorithm\r\n\r\n![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/kelindar/evolve)\r\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/kelindar/evolve)](https://pkg.go.dev/github.com/kelindar/evolve)\r\n[![Go Report Card](https://goreportcard.com/badge/github.com/kelindar/evolve)](https://goreportcard.com/report/github.com/kelindar/evolve)\r\n[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\r\n[![Coverage Status](https://coveralls.io/repos/github/kelindar/evolve/badge.svg)](https://coveralls.io/github/kelindar/evolve)\r\n\r\nThis repository contains a simple implementation of a genetic algorithm for evolving arbitrary types.  There's a double-buffering in place to prevent unnecessary allocations and a relatively simple API around it.\r\n\r\nIt also provides a `binary` package for evolving `[]byte` genomes. Under the hood, it uses a simple random binary crossover and mutation to do the trick.\r\n\r\n\r\n## Usage\r\n\r\nIn order to use this, we first need to create a \"phenotype\" representation which contains the dna. In this example we're using the `binary` package in order to evolve a string. It should implement the `Evolver` interface which contains `Genome()` and `Evolve()` methods, in the example here we are creating a simple text which contains the binary representation of the text itself.\r\n\r\n```go\r\n// Text represents a text with a dna (text itself in this case)\r\ntype text struct {\r\n\tdna evolve.Genome\r\n}\r\n\r\n// Genome returns the genome\r\nfunc (t *text) Genome() []byte {\r\n\treturn t.dna\r\n}\r\n\r\n// Evolve updates the genome\r\nfunc (t *text) Evolve(v []byte) {\r\n\tt.dna = v\r\n}\r\n```\r\nNext, we'll need a fitness function to evaluate how good a genome is. In this example we're creating a fitness function for an abritrary string which simply returns a `func(Evolver) float32`\r\n```go\r\n// fitnessFor returns a fitness function for a string\r\nfunc fitnessFor(text string) evolve.Fitness {\r\n\ttarget := []byte(text)\r\n\treturn func(v evolve.Evolver) float32 {\r\n\t\tvar score float32\r\n\t\tgenome := v.Genome().(*binary.Genome)\r\n\t\tfor i, v := range *genome {\r\n\t\t\tif v == target[i] {\r\n\t\t\t\tscore++\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn score / float32(len(target))\r\n\t}\r\n}\r\n```\r\n\r\nFinally, we can wire everything together by using `New()` function to create a population, and evolve it by repeatedly calling `Evolve()` method as shown below.\r\n\r\n```go\r\nfunc main() {\r\n\tconst target = \"Hello World\"\r\n\tconst n = 200\r\n\r\n\t// Create a fitness function\r\n\tfit := fitnessFor(target)\r\n\r\n\t// Create a population\r\n\tpopulation := make([]evolve.Evolver, 0, n)\r\n\tfor i := 0; i \u003c n; i++ {\r\n\t\tpopulation = append(population, new(text))\r\n\t}\r\n    \r\n\t// Create a population\r\n\tpop := evolve.New(population, fit, len(target))\r\n\r\n\t// Evolve over many generations\r\n\tfor i := 0 ; i \u003c 100000; i++ {\r\n\t\tpop.Evolve()\r\n\t}\r\n\r\n\t// Get the fittest member of the population\r\n\tfittest := pop.Fittest()\r\n}\r\n```\r\n\r\n## License\r\n\r\nTile is licensed under the [MIT License](LICENSE.md).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkelindar%2Fevolve","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkelindar%2Fevolve","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkelindar%2Fevolve/lists"}