https://github.com/adrian-lin-1-0-0/go-genetic-algorithme
Genetic Algorithm implementation in Go
https://github.com/adrian-lin-1-0-0/go-genetic-algorithme
genetic-algorithm go golang
Last synced: 3 months ago
JSON representation
Genetic Algorithm implementation in Go
- Host: GitHub
- URL: https://github.com/adrian-lin-1-0-0/go-genetic-algorithme
- Owner: adrian-lin-1-0-0
- Created: 2023-07-11T10:48:27.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-07-21T13:50:05.000Z (almost 2 years ago)
- Last Synced: 2025-01-25T18:43:58.976Z (5 months ago)
- Topics: genetic-algorithm, go, golang
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Genetic Algorithm
## Install
```bash
go get github.com/adrian-lin-1-0-0/go-genetic-algorithme
```## Class Diagram
Factory Method
```mermaid
---
title: Genetic Algorithm
---
classDiagram
class Organism{
<>
+GetChromosome() Chromosome
+Fitness() float64
}class Chromosome{
}
class Gene{
}class OrganismFactory{
<>
+Create() Organism
+CreateWithChromosome(Chromosome) Organism
+CreateGene() Gene
}
Organism o-- Chromosome
Chromosome o-- Gene
OrganismFactory ..> Gene
OrganismFactory ..> Chromosome
OrganismFactory ..> Organismclass Population{
}
class GeneticAlgorithm{
+Run() Organism
-selection(Population)Population
-crossover(Population)Population
-mutation(Population)Population
-terminationCondition()bool
}GeneticAlgorithm o-- OrganismFactory
GeneticAlgorithm ..> Population
Population o-- Organism```
## Example
```go
package mainimport (
"fmt"
"math/rand"ga "github.com/adrian-lin-1-0-0/go-genetic-algorithme"
)func main() {
gaOpts := (&ga.GeneticAlgorithmOptions{}).
SetMaxGeneration(100).
SetMutationRate(0.01).
SetPopulationSize(2000).
SetOrganismFactory(&OrganismFactory{}).
SetFitnessThreshold(19)geneticAlgorithm := ga.NewGeneticAlgorithm(gaOpts)
res := geneticAlgorithm.Run()
fmt.Println(string(res.GetChromosome()))
//to be or not to be
}type Organism struct {
chromosome ga.Chromosome
fitness float64
}func (o Organism) GetChromosome() ga.Chromosome {
return o.chromosome
}func (o Organism) Fitness() float64 {
if o.fitness != 0 {
return o.fitness
}
target := []rune("to be or not to be")
chromosome := o.chromosome
fitness := 0.0
for i, gene := range chromosome {
if gene == ga.Gene(target[i]) {
fitness++
}
}
fitness++
o.fitness = fitness
return o.fitness
}type OrganismFactory struct {
}func (o *OrganismFactory) Create() ga.Organism {
charRange := []rune("abcdefghijklmnopqrstuvwxyz ")chromosome := make(ga.Chromosome, 18)
for i := 0; i < 18; i++ {
chromosome[i] = ga.Gene(charRange[rand.Intn(len(charRange))])
}
return Organism{chromosome, 0}
}func (o *OrganismFactory) CreateWithChromosome(chromosome ga.Chromosome) ga.Organism {
return Organism{chromosome, 0}
}func (o *OrganismFactory) CreateGene() ga.Gene {
charRange := []rune("abcdefghijklmnopqrstuvwxyz ")return ga.Gene(charRange[rand.Intn(len(charRange))])
}```