-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathpyevolve_ex4_sigmatrunc.py
More file actions
49 lines (37 loc) · 1.21 KB
/
pyevolve_ex4_sigmatrunc.py
File metadata and controls
49 lines (37 loc) · 1.21 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
from pyevolve import G1DList
from pyevolve import GSimpleGA
from pyevolve import Selectors
from pyevolve import Initializators, Mutators
from pyevolve import Scaling
from pyevolve import Consts
import math
def eval_func(ind):
score = 0.0
var_x = ind[0]
var_z = var_x**2+2*var_x+1*math.cos(var_x)
return var_z
def run_main():
# Genome instance
genome = G1DList.G1DList(1)
genome.setParams(rangemin=-60.0, rangemax=60.0)
# Change the initializator to Real values
genome.initializator.set(Initializators.G1DListInitializatorReal)
# Change the mutator to Gaussian Mutator
genome.mutator.set(Mutators.G1DListMutatorRealGaussian)
# Removes the default crossover
genome.crossover.clear()
# The evaluator function (objective function)
genome.evaluator.set(eval_func)
# Genetic Algorithm Instance
ga = GSimpleGA.GSimpleGA(genome)
ga.setMinimax(Consts.minimaxType["minimize"])
pop = ga.getPopulation()
pop.scaleMethod.set(Scaling.SigmaTruncScaling)
ga.selector.set(Selectors.GRouletteWheel)
ga.setGenerations(100)
# Do the evolution
ga.evolve(10)
# Best individual
print ga.bestIndividual()
if __name__ == "__main__":
run_main()