-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetics.py
More file actions
69 lines (60 loc) · 2.71 KB
/
genetics.py
File metadata and controls
69 lines (60 loc) · 2.71 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
import random as rd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
# implements a differential evolution GA
class diffEvolver:
def __init__(self, ps, F, cp, nfe, rng = 123):
self.ps = ps # population size
self.F = F # scaling factor
self.cp = cp # crossover probability
self.nfe = nfe # number of function evaluations
self.rng = rng # random seed for reproducibility
self.pop = [] # keeps the population after evolution
self.evals = [] # keeps the evaluations of the population after evolution
# sets the function to initialize individuals
def setPopulator(self, populator):
self.populator = populator
# sets the DE reproduction method
def setReproduction(self, reproduction):
self.reproduction = reproduction
# sets the survival method after reproduction and before next iteration of evolution
def setSurvival(self, survival):
self.survival = survival
# sets the distance fucntion for survival calculation
def setDistance(self, distance):
self.distance = distance
# allows for changing the rng seed
def setSeed(self, rng):
self.rng = rng
# prints progress of evolution process
def verbosity(self, ngen, nevals, population, evaluations):
meanFit = sum(evaluations) / len(evaluations)
print('GENERATION: ', str(ngen), ' / Fitness evaluations: ', nevals, '. Mean fitness is ', '{:.2f}'.format(meanFit), ", Showing top 10 individuals...", sep = '')
top = (-np.array(evaluations)).argsort()
for i in range(10):
ind = ['{:.2f}'.format(d) for d in population[top[i]]]
print("\t", "Fitness: ", '{:.2f}'.format(evaluations[top[i]]), " / Genotype: ", str(ind), sep = '')
# GA
def evolve(self, surf):
rd.seed(self.rng)
pop = [self.populator(surf.dlims, surf.ulims) for i in range(self.ps)]
nevals = 0
gen = 1
while nevals < self.nfe:
evals = [surf.f(i) for i in pop]
if nevals % (np.floor( (10 * self.nfe) / 100 )) == 0:
self.verbosity(gen, nevals, pop, evals)
new_pop = pop.copy()
for i in range(self.ps):
offspring = self.reproduction(i, pop, self.F, self.cp, surf.dlims, surf.ulims)
replace = self.survival(offspring, surf.f(offspring), pop, evals, self.distance)
if replace > -1:
new_pop[replace] = offspring
pop = new_pop
nevals += self.ps * 2
gen += 1
print("DONE, FINAL POPULATION:")
self.verbosity(gen, nevals, pop, evals)
self.pop = pop
self.evals = evals