-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneticAlgorithm.py
58 lines (48 loc) · 2 KB
/
GeneticAlgorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# This is just a simple sample of how and what Genetic Algorithm can do.
import random
from math import floor
from cmath import sin
from random import uniform
from typing import Tuple
def fooFunc(input: Tuple[float, float, float]) -> float:
return 2 * input[0] + input[1] ** 3 + sin(input[2]) - 2.3673
def populationFunc(size: int) -> Tuple[float, float, float]:
population = []
for i in range(0, size):
x = random.uniform(-100, 100)
y = random.uniform(-100, 100)
z = random.uniform(-100, 100)
population.append( (x, y, z) )
return population
def fitnessFunc(population: Tuple[float, float, float], precision: float, evaluationFunc: callable) -> tuple( (Tuple[float, float, float], float) ):
fitness = []
for thing in population:
calc = evaluationFunc(thing)
calcAbs = abs(calc)
fitness.append( (thing, calcAbs) )
if calcAbs <= precision:
fitness.sort(key=lambda x: x[1])
fitness.append( (thing, calc) )
return fitness
fitness.sort(key=lambda x: x[1])
return fitness
def selectionMutationFunc(orderedPopulation: Tuple[float, float, float], selectionLen: int, populationLen: int) -> Tuple[float, float, float]:
newGen = []
for i in range(0, floor(populationLen / selectionLen)):
for person in orderedPopulation:
x = person[0][0] + random.uniform(-0.01, 0.01)
y = person[0][1] + random.uniform(-0.01, 0.01)
z = person[0][2] + random.uniform(-0.01, 0.01)
newGen.append( (x, y, z) )
return newGen
def printFitnessFunc(fitness: tuple( (Tuple[float, float, float], float) )):
for item in fitness:
print(item)
PRECISION = 0.0000001
population = populationFunc(1000)
fitness = fitnessFunc(population, PRECISION, fooFunc)
printFitnessFunc(fitness)
while fitness[0][1] > PRECISION:
population = selectionMutationFunc(fitness[ : 100], 100, 1000)
fitness = fitnessFunc(population, PRECISION, fooFunc)
printFitnessFunc(fitness)