Giter VIP home page Giter VIP logo

basicga's Introduction

Practise Genetic Algorithm in Python Build Status

  • single-objective minimization
  • built-in Individual and Operators for mathematical functions, travelling salesman problem
  • user-defined Individuals and Operators
  • based on Numpy

An example for solving the minimum solution of common test function schaffer N4 is shown below.

Import modules

from GA.GAPopulation.DecimalIndividual import DecimalFloatIndividual
from GA.GAPopulation.Population import Population
from GA.GAOperators.Selection import RouletteWheelSelection
from GA.GAOperators.Crossover import DecimalCrossover
from GA.GAOperators.Mutation import DecimalMutation
from GA.GAProcess import GA
import numpy as np

Initialize GA

# individual
dimension = 2
ranges = [(-10, 10)] * dimension
I = DecimalFloatIndividual(ranges)

# population
P = Population(I, 50)

# operators
S = RouletteWheelSelection()
C = DecimalCrossover([0.5, 0.9], 0.5) # adaptive crossover rate
# C = DecimalCrossover(0.9, 0.5) # constant crossover rate
M = DecimalMutation(0.12)

# GA
g = GA(P, S, C, M)

Solve

# schaffer-N4
# sol: x=[0,1.25313], min=0.292579
schaffer_n4 = lambda x: 0.5 + (np.cos(np.sin(abs(x[0]**2-x[1]**2)))**2-0.5) / (1.0+0.001*(x[0]**2+x[1]**2))**2

res = g.run(schaffer_n4, gen=800)   

x = [0,1.25313]
print('{0} : {1}'.format(res.evaluation, res.solution)) # 0.29257882535592317 : [1.25339239e+00 6.28576519e-05]
print('error: {:<3f} %'.format((res.evaluation/schaffer_n4(x)-1.0)*100)) # error: 0.000066 %

basicga's People

Contributors

dothinking avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.