Skip to content

dothinking/basicGA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 %

About

Practise Genetic Algorithm in Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages