maltekrupa / go-flow-ea Goto Github PK
View Code? Open in Web Editor NEWImplementation of an evolutionary algorithm via Flow-Based-Programming
Implementation of an evolutionary algorithm via Flow-Based-Programming
We start with a very basic setup to optimize a vector of bits.
Bit vector like:
type population struct {
individuals []individual
maxIndividuals int
maxFitness int
avgFitness float64
}
type individual struct {
entities []int
amount int
fitness int
generation int
}
Random
func (p *population) ParentSelection() (individual, individual) {
// Returns two random individuals which will be the parents
length := len(p.individuals)
mother := rand.Intn(length)
father := rand.Intn(length)
for {
if mother == father {
mother = rand.Intn(length)
} else {
break
}
}
return p.TwoIndividual(mother, father)
}
1-point crossover
func (p *population) RandomOnePointCrossover(x, y individual) (individual, individual) {
randOnePoint := rand.Intn(len(x.entities))
if debug {
fmt.Println("RandomOnePointCrossover -", "Split at point:", randOnePoint)
fmt.Println("RandomOnePointCrossover -", "Mother:", x)
fmt.Println("RandomOnePointCrossover -", "Father:", y)
}
tempGene1 := make([]int, 0, len(x.entities))
tempGene2 := make([]int, 0, len(y.entities))
tempGene1 = append(tempGene1, x.entities[:randOnePoint]...)
tempGene2 = append(tempGene2, y.entities[:randOnePoint]...)
tempGene1 = append(tempGene1, y.entities[randOnePoint:]...)
tempGene2 = append(tempGene2, x.entities[randOnePoint:]...)
retGene1 := individual{tempGene1, len(tempGene1), fitness(tempGene1), 0}
retGene2 := individual{tempGene2, len(tempGene2), fitness(tempGene2), 0}
if debug {
fmt.Println("RandomOnePointCrossover -", "Child1:", retGene1)
fmt.Println("RandomOnePointCrossover -", "Child2:", retGene2)
}
return retGene1, retGene2
}
Greedy (Strongest set of entities wins)
func (p *population) KillWeak() {
// fmt.Println(p.individuals)
sort.Sort(byFitness(p.individuals))
p.individuals = append(p.individuals[len(p.individuals)-amountOfIndividuals:])
// fmt.Println(p.individuals)
p.RefreshAvgFitness()
}
Create some representation for the configuration and a boilerplate for go-flow. Since we'll need a form of input for these values, we start with a simple cli-app.
type Config struct {
entities int
individuals int
targetFitness int
seed string
}
This should be part of a component and saved at the end.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.