{"id":13413346,"url":"https://github.com/khezen/evoli","last_synced_at":"2025-10-25T21:03:02.818Z","repository":{"id":33654627,"uuid":"37307096","full_name":"khezen/evoli","owner":"khezen","description":"Genetic Algorithm and Particle Swarm Optimization","archived":false,"fork":false,"pushed_at":"2021-10-27T10:26:23.000Z","size":826,"stargazers_count":31,"open_issues_count":21,"forks_count":10,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-07-31T20:52:15.727Z","etag":null,"topics":["evolutionary-algorithms","genetic-algorithm","go","golang","particle-swarm-optimization"],"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/khezen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-12T06:58:30.000Z","updated_at":"2024-06-05T02:21:11.000Z","dependencies_parsed_at":"2022-09-10T12:42:27.352Z","dependency_job_id":null,"html_url":"https://github.com/khezen/evoli","commit_stats":null,"previous_names":["khezen/darwin"],"tags_count":11,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khezen%2Fevoli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khezen%2Fevoli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khezen%2Fevoli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khezen%2Fevoli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khezen","download_url":"https://codeload.github.com/khezen/evoli/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247779796,"owners_count":20994569,"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":["evolutionary-algorithms","genetic-algorithm","go","golang","particle-swarm-optimization"],"created_at":"2024-07-30T20:01:38.294Z","updated_at":"2025-10-25T21:03:02.732Z","avatar_url":"https://github.com/khezen.png","language":"Go","funding_links":[],"categories":["机器学习","Machine Learning","Relational Databases"],"sub_categories":["检索及分析资料库","Search and Analytic Databases","Advanced Console UIs","SQL 查询语句构建库"],"readme":"# *evoli*\n\n[![GoDoc](https://img.shields.io/badge/go-documentation-blue.svg)](https://godoc.org/github.com/khezen/evoli)\n[![Build Status](https://github.com/khezen/evoli/workflows/build/badge.svg?branch=master)](https://github.com/khezen/evoli/actions?query=workflow%3Abuild) [![codecov](https://img.shields.io/codecov/c/github/khezen/evoli/master.svg)](https://codecov.io/gh/khezen/evoli)\n[![Go Report Card](https://goreportcard.com/badge/github.com/khezen/evoli)](https://goreportcard.com/report/github.com/khezen/evoli)\n\nGenetic Algorithm and Particle Swarm Optimization written in Go\n\n## Example\n\n### Problem\n\nGiven `f(x,y) = cos(x^2 * y^2) * 1/(x^2 * y^2 + 1)`\n\nFind `(x,y)` such as `f(x,y)` reaches its maximum \n\nAnswer `f(0,0) = 1`\n\n### Particle Swarm Optimization\n\n```golang\npackage main\n\nimport (\n\t\"fmt\"\n\t\"math\"\n\t\"math/rand\"\n\n\t\"github.com/khezen/evoli\"\n)\n\n// 3d cosine that gets smaller as you move away from 0,0\nfunc f(x, y float64) float64 {\n\td := x*x + y*y\n\treturn math.Cos(d) * (1 / (d/10 + 1))\n}\n\ntype FIndividual struct {\n\tv       []float64\n\tx       []float64\n\tfitness float64\n}\n\nfunc (i *FIndividual) Equal(other evoli.Individual) bool {\n\treturn i == other\n}\n\nfunc (i *FIndividual) Fitness() float64 {\n\treturn i.fitness\n}\n\nfunc (i *FIndividual) SetFitness(newFitness float64) {\n\ti.fitness = newFitness\n}\n\ntype FPositioner struct {\n}\n\nfunc (p *FPositioner) Position(indiv, pBest, gBest evoli.Individual, c1, c2 float64) (evoli.Individual, error) {\n\tfIndiv, ok1 := indiv.(*FIndividual)\n\tfPBest, ok2 := pBest.(*FIndividual)\n\tfGBest, ok3 := gBest.(*FIndividual)\n\tif !ok1 || !ok2 || !ok3 {\n\t\treturn nil, fmt.Errorf(\"invalid individual type\")\n\t}\n\tnewIndiv := FIndividual{\n\t\tv: make([]float64, len(fIndiv.v)),\n\t\tx: make([]float64, len(fIndiv.v)),\n\t}\n\tw := 0.9\n\tfor d := range fIndiv.v {\n\t\trp := rand.Float64()\n\t\trg := rand.Float64()\n\t\tnewIndiv.v[d] = w*fIndiv.v[d] +\n\t\t\tc1*rp*(fPBest.x[d]-fIndiv.x[d]) +\n\t\t\tc2*rg*(fGBest.x[d]-fIndiv.x[d])\n\n\t\tnewIndiv.x[d] = fIndiv.x[d] + newIndiv.v[d]\n\t}\n\treturn \u0026newIndiv, nil\n}\n\ntype FEvaluater struct {\n}\n\nfunc (e *FEvaluater) Evaluate(indiv evoli.Individual) (Fitness float64, err error) {\n\tfIndiv, ok := indiv.(*FIndividual)\n\tif !ok {\n\t\treturn 0, fmt.Errorf(\"invalid individual type\")\n\t}\n\treturn f(fIndiv.x[0], fIndiv.x[1]), nil\n}\n\nfunc main() {\n\tpop := evoli.NewPopulation(50)\n\tfor i := 0; i \u003c pop.Cap(); i++ {\n\t\tx := rand.Float64()*20 - 10\n\t\ty := rand.Float64()*20 - 10\n\t\tvx := rand.Float64()*20 - 10\n\t\tvy := rand.Float64()*20 - 10\n\t\tpop.Add(\u0026FIndividual{\n\t\t\tx: []float64{x, y},\n\t\t\tv: []float64{vx, vy},\n\t\t})\n\t}\n\tpositioner := \u0026FPositioner{}\n\tevaluator := \u0026FEvaluater{}\n\n\tsw := evoli.NewSwarm(pop, positioner, .2, .2, evaluator)\n\n\tfor i := 0; i \u003c 100; i++ {\n\t\terr := sw.Next()\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n\n\t// evaluate the latest population\n\tfor _, v := range sw.Population().Slice() {\n\t\tf, err := evaluator.Evaluate(v)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tv.SetFitness(f)\n\t}\n\n\tfmt.Printf(\"Max Value: %.2f\\n\", sw.Alpha().Fitness())\n}\n```\n\n```bash\nMax Value: 1.00\n```\n\n### Gentic Algorithm\n\n```golang\npackage main\n\nimport (\n\t\"fmt\"\n\t\"math\"\n\t\"math/rand\"\n\n\t\"github.com/khezen/evoli\"\n)\n\n// 3d cosine that gets smaller as you move away from 0,0\nfunc h(x, y float64) float64 {\n\td := x*x + y*y\n\treturn math.Cos(d) * (1 / (d/10 + 1))\n}\n\ntype HIndividual struct {\n\tv       []float64\n\tx       []float64\n\tfitness float64\n}\n\nfunc (i *HIndividual) Equal(other evoli.Individual) bool {\n\treturn i == other\n}\n\nfunc (i *HIndividual) Fitness() float64 {\n\treturn i.fitness\n}\n\nfunc (i *HIndividual) SetFitness(newFitness float64) {\n\ti.fitness = newFitness\n}\n\ntype HMutater struct {\n}\n\nfunc (m *HMutater) Mutate(indiv evoli.Individual, mutationProbability float64) (evoli.Individual, error) {\n\thIndiv1, ok := indiv.(*HIndividual)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"invalid individual type\")\n\t}\n\tx := hIndiv1.x[0]\n\ty := hIndiv1.x[1]\n\tvx := hIndiv1.v[0]\n\tvy := hIndiv1.v[1]\n\tif mutationProbability \u003e rand.Float64() {\n\t\tx = rand.Float64()*20 - 10\n\t}\n\tif mutationProbability \u003e rand.Float64() {\n\t\ty = rand.Float64()*20 - 10\n\t}\n\tif mutationProbability \u003e rand.Float64() {\n\t\tvx = rand.Float64()*20 - 10\n\t}\n\tif mutationProbability \u003e rand.Float64() {\n\t\tvy = rand.Float64()*20 - 10\n\t}\n\treturn \u0026HIndividual{\n\t\tx: []float64{x, y},\n\t\tv: []float64{vx, vy},\n\t}, nil\n}\n\ntype HCrosser struct {\n}\n\nfunc (h *HCrosser) Cross(indiv1, indiv2 evoli.Individual) (evoli.Individual, evoli.Individual, error) {\n\thIndiv1, ok1 := indiv1.(*HIndividual)\n\thIndiv2, ok2 := indiv2.(*HIndividual)\n\tif !ok1 || !ok2 {\n\t\treturn nil, nil, fmt.Errorf(\"invalid individual type\")\n\t}\n\treturn \u0026HIndividual{\n\t\t\tx: []float64{hIndiv1.x[0], hIndiv2.x[1]},\n\t\t\tv: []float64{hIndiv1.v[0], hIndiv2.v[1]},\n\t\t}, \u0026HIndividual{\n\t\t\tx: []float64{hIndiv2.x[0], hIndiv1.x[1]},\n\t\t\tv: []float64{hIndiv2.v[0], hIndiv1.v[1]},\n\t\t}, nil\n}\n\ntype HEvaluater struct {\n}\n\nfunc (e *HEvaluater) Evaluate(indiv evoli.Individual) (Fitness float64, err error) {\n\tfIndiv, ok := indiv.(*HIndividual)\n\tif !ok {\n\t\treturn 0, fmt.Errorf(\"invalid individual type\")\n\t}\n\treturn h(fIndiv.x[0], fIndiv.x[1]), nil\n}\n\nfunc main() {\n\tpop := evoli.NewPopulation(50)\n\tfor i := 0; i \u003c pop.Cap(); i++ {\n\t\tx := rand.Float64()*20 - 10\n\t\ty := rand.Float64()*20 - 10\n\t\tvx := rand.Float64()*20 - 10\n\t\tvy := rand.Float64()*20 - 10\n\t\tpop.Add(\u0026HIndividual{\n\t\t\tx: []float64{x, y},\n\t\t\tv: []float64{vx, vy},\n\t\t})\n\t}\n\tcrosser := \u0026HCrosser{}\n\tmutater := \u0026HMutater{}\n\tevaluator := \u0026HEvaluater{}\n\tmutationProbability := .20\n\tselecter := evoli.NewTruncationSelecter()\n\tsurvivorSize := 30\n\n\tga := evoli.NewGenetic(pop, selecter, survivorSize, crosser, mutater, mutationProbability, evaluator)\n\n\tfor i := 0; i \u003c 100; i++ {\n\t\terr := ga.Next()\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n\n\t// evaluate the latest population\n\tfor _, v := range ga.Population().Slice() {\n\t\tf, err := evaluator.Evaluate(v)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tv.SetFitness(f)\n\t}\n\n\tfmt.Printf(\"Max Value: %.2f\\n\", ga.Alpha().Fitness())\n}\n```\n\n```bash\nMax Value: 1.00\n```\n\n\n## Issues\n\nIf you have any problems or questions, please ask for help through a [GitHub issue](https://github.com/khezen/evoli/issues).\n\n## Contributions\n\nHelp is always welcome! For example, documentation (like the text you are reading now) can always use improvement. There's always code that can be improved. If you ever see something you think should be fixed, you should own it. If you have no idea what to start on, you can browse the issues labeled with [help wanted](https://github.com/khezen/evoli/labels/help%20wanted).\n\nAs a potential contributor, your changes and ideas are welcome at any hour of the day or night, weekdays, weekends, and holidays. Please do not ever hesitate to ask a question or send a pull request.\n\n[Code of conduct](https://github.com/khezen/evoli/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhezen%2Fevoli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhezen%2Fevoli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhezen%2Fevoli/lists"}