All Projects → khezen → evoli

khezen / evoli

Licence: MIT License
Genetic Algorithm and Particle Swarm Optimization

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to evoli

zoofs
zoofs is a python library for performing feature selection using a variety of nature-inspired wrapper algorithms. The algorithms range from swarm-intelligence to physics-based to Evolutionary. It's easy to use , flexible and powerful tool to reduce your feature size.
Stars: ✭ 142 (+545.45%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms, particle-swarm-optimization
triangula
Generate high-quality triangulated and polygonal art from images.
Stars: ✭ 3,775 (+17059.09%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
EvOLuTIoN
A simple simulation in Unity, which uses genetic algorithm to optimize forces applied to cubes
Stars: ✭ 44 (+100%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Smart-Algorithm
智能算法-遗传算法、蚁群算法、粒子群算法实现。实现版本Java,Python,MatLab多版本实现
Stars: ✭ 277 (+1159.09%)
Mutual labels:  genetic-algorithm, particle-swarm-optimization
Circle Evolution
Evolutionary Art Using Circles in Python
Stars: ✭ 237 (+977.27%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
datafsm
Machine Learning Finite State Machine Models from Data with Genetic Algorithms
Stars: ✭ 14 (-36.36%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
sopt
sopt:A simple python optimization library
Stars: ✭ 42 (+90.91%)
Mutual labels:  genetic-algorithm, particle-swarm-optimization
Bluepyopt
Blue Brain Python Optimisation Library
Stars: ✭ 143 (+550%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
geneticalgorithm2
Supported highly optimized and flexible genetic algorithm package for python
Stars: ✭ 36 (+63.64%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
GARI
GARI (Genetic Algorithm for Reproducing Images) reproduces a single image using Genetic Algorithm (GA) by evolving pixel values.
Stars: ✭ 41 (+86.36%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
TorchGA
Train PyTorch Models using the Genetic Algorithm with PyGAD
Stars: ✭ 47 (+113.64%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Watchmaker
The Watchmaker Framework for Evolutionary Computation
Stars: ✭ 189 (+759.09%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Scikit Opt
Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Optimization Algorithm,Immune Algorithm, Artificial Fish Swarm Algorithm, Differential Evolution and TSP(Traveling salesman)
Stars: ✭ 2,791 (+12586.36%)
Mutual labels:  genetic-algorithm, particle-swarm-optimization
machine-learning-blackjack-solution
Finding an optimal Blackjack strategy using AI
Stars: ✭ 40 (+81.82%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Evolutionary Computation Course
Jupyter/IPython notebooks about evolutionary computation.
Stars: ✭ 173 (+686.36%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
opt4j
Modular Java framework for meta-heuristic optimization
Stars: ✭ 25 (+13.64%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Evolutionary.jl
Evolutionary & genetic algorithms for Julia
Stars: ✭ 142 (+545.45%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Evolutionsimulator
Evolution Simulator with Box2D
Stars: ✭ 143 (+550%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
biteopt
Derivative-Free Optimization Method for Global Optimization (C++)
Stars: ✭ 91 (+313.64%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
neuroevolution-robots
Neuroevolution demo through TensorFlow.js, Neataptic, and Box2D
Stars: ✭ 31 (+40.91%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms

evoli

GoDoc Build Status codecov Go Report Card

Genetic Algorithm and Particle Swarm Optimization written in Go

Example

Problem

Given f(x,y) = cos(x^2 * y^2) * 1/(x^2 * y^2 + 1)

Find (x,y) such as f(x,y) reaches its maximum

Answer f(0,0) = 1

Particle Swarm Optimization

package main

import (
	"fmt"
	"math"
	"math/rand"

	"github.com/khezen/evoli"
)

// 3d cosine that gets smaller as you move away from 0,0
func f(x, y float64) float64 {
	d := x*x + y*y
	return math.Cos(d) * (1 / (d/10 + 1))
}

type FIndividual struct {
	v       []float64
	x       []float64
	fitness float64
}

func (i *FIndividual) Equal(other evoli.Individual) bool {
	return i == other
}

func (i *FIndividual) Fitness() float64 {
	return i.fitness
}

func (i *FIndividual) SetFitness(newFitness float64) {
	i.fitness = newFitness
}

type FPositioner struct {
}

func (p *FPositioner) Position(indiv, pBest, gBest evoli.Individual, c1, c2 float64) (evoli.Individual, error) {
	fIndiv, ok1 := indiv.(*FIndividual)
	fPBest, ok2 := pBest.(*FIndividual)
	fGBest, ok3 := gBest.(*FIndividual)
	if !ok1 || !ok2 || !ok3 {
		return nil, fmt.Errorf("invalid individual type")
	}
	newIndiv := FIndividual{
		v: make([]float64, len(fIndiv.v)),
		x: make([]float64, len(fIndiv.v)),
	}
	w := 0.9
	for d := range fIndiv.v {
		rp := rand.Float64()
		rg := rand.Float64()
		newIndiv.v[d] = w*fIndiv.v[d] +
			c1*rp*(fPBest.x[d]-fIndiv.x[d]) +
			c2*rg*(fGBest.x[d]-fIndiv.x[d])

		newIndiv.x[d] = fIndiv.x[d] + newIndiv.v[d]
	}
	return &newIndiv, nil
}

type FEvaluater struct {
}

func (e *FEvaluater) Evaluate(indiv evoli.Individual) (Fitness float64, err error) {
	fIndiv, ok := indiv.(*FIndividual)
	if !ok {
		return 0, fmt.Errorf("invalid individual type")
	}
	return f(fIndiv.x[0], fIndiv.x[1]), nil
}

func main() {
	pop := evoli.NewPopulation(50)
	for i := 0; i < pop.Cap(); i++ {
		x := rand.Float64()*20 - 10
		y := rand.Float64()*20 - 10
		vx := rand.Float64()*20 - 10
		vy := rand.Float64()*20 - 10
		pop.Add(&FIndividual{
			x: []float64{x, y},
			v: []float64{vx, vy},
		})
	}
	positioner := &FPositioner{}
	evaluator := &FEvaluater{}

	sw := evoli.NewSwarm(pop, positioner, .2, .2, evaluator)

	for i := 0; i < 100; i++ {
		err := sw.Next()
		if err != nil {
			panic(err)
		}
	}

	// evaluate the latest population
	for _, v := range sw.Population().Slice() {
		f, err := evaluator.Evaluate(v)
		if err != nil {
			panic(err)
		}
		v.SetFitness(f)
	}

	fmt.Printf("Max Value: %.2f\n", sw.Alpha().Fitness())
}
Max Value: 1.00

Gentic Algorithm

package main

import (
	"fmt"
	"math"
	"math/rand"

	"github.com/khezen/evoli"
)

// 3d cosine that gets smaller as you move away from 0,0
func h(x, y float64) float64 {
	d := x*x + y*y
	return math.Cos(d) * (1 / (d/10 + 1))
}

type HIndividual struct {
	v       []float64
	x       []float64
	fitness float64
}

func (i *HIndividual) Equal(other evoli.Individual) bool {
	return i == other
}

func (i *HIndividual) Fitness() float64 {
	return i.fitness
}

func (i *HIndividual) SetFitness(newFitness float64) {
	i.fitness = newFitness
}

type HMutater struct {
}

func (m *HMutater) Mutate(indiv evoli.Individual, mutationProbability float64) (evoli.Individual, error) {
	hIndiv1, ok := indiv.(*HIndividual)
	if !ok {
		return nil, fmt.Errorf("invalid individual type")
	}
	x := hIndiv1.x[0]
	y := hIndiv1.x[1]
	vx := hIndiv1.v[0]
	vy := hIndiv1.v[1]
	if mutationProbability > rand.Float64() {
		x = rand.Float64()*20 - 10
	}
	if mutationProbability > rand.Float64() {
		y = rand.Float64()*20 - 10
	}
	if mutationProbability > rand.Float64() {
		vx = rand.Float64()*20 - 10
	}
	if mutationProbability > rand.Float64() {
		vy = rand.Float64()*20 - 10
	}
	return &HIndividual{
		x: []float64{x, y},
		v: []float64{vx, vy},
	}, nil
}

type HCrosser struct {
}

func (h *HCrosser) Cross(indiv1, indiv2 evoli.Individual) (evoli.Individual, evoli.Individual, error) {
	hIndiv1, ok1 := indiv1.(*HIndividual)
	hIndiv2, ok2 := indiv2.(*HIndividual)
	if !ok1 || !ok2 {
		return nil, nil, fmt.Errorf("invalid individual type")
	}
	return &HIndividual{
			x: []float64{hIndiv1.x[0], hIndiv2.x[1]},
			v: []float64{hIndiv1.v[0], hIndiv2.v[1]},
		}, &HIndividual{
			x: []float64{hIndiv2.x[0], hIndiv1.x[1]},
			v: []float64{hIndiv2.v[0], hIndiv1.v[1]},
		}, nil
}

type HEvaluater struct {
}

func (e *HEvaluater) Evaluate(indiv evoli.Individual) (Fitness float64, err error) {
	fIndiv, ok := indiv.(*HIndividual)
	if !ok {
		return 0, fmt.Errorf("invalid individual type")
	}
	return h(fIndiv.x[0], fIndiv.x[1]), nil
}

func main() {
	pop := evoli.NewPopulation(50)
	for i := 0; i < pop.Cap(); i++ {
		x := rand.Float64()*20 - 10
		y := rand.Float64()*20 - 10
		vx := rand.Float64()*20 - 10
		vy := rand.Float64()*20 - 10
		pop.Add(&HIndividual{
			x: []float64{x, y},
			v: []float64{vx, vy},
		})
	}
	crosser := &HCrosser{}
	mutater := &HMutater{}
	evaluator := &HEvaluater{}
	mutationProbability := .20
	selecter := evoli.NewTruncationSelecter()
	survivorSize := 30

	ga := evoli.NewGenetic(pop, selecter, survivorSize, crosser, mutater, mutationProbability, evaluator)

	for i := 0; i < 100; i++ {
		err := ga.Next()
		if err != nil {
			panic(err)
		}
	}

	// evaluate the latest population
	for _, v := range ga.Population().Slice() {
		f, err := evaluator.Evaluate(v)
		if err != nil {
			panic(err)
		}
		v.SetFitness(f)
	}

	fmt.Printf("Max Value: %.2f\n", ga.Alpha().Fitness())
}
Max Value: 1.00

Issues

If you have any problems or questions, please ask for help through a GitHub issue.

Contributions

Help 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.

As 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.

Code of conduct.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].