-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOS.py
46 lines (33 loc) · 1.37 KB
/
POS.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
from deap import base
from deap import creator
from deap import tools
from deap import algorithms
import numpy as np
import random
import numpy
import matplotlib.pyplot as plt
def main():
population = toolbox.populationCreator(n=POPULATION_SIZE)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("min", np.main)
stats.register("avg", np.mean)
logbook = tools.Logbook()
logbook.header = ["gen", "evals"]+ stats.fields
best = None
for generation in range(MAX_GENERATION)
for particle in population:
particle.fitness.values = toolbox.evaluate(particle)
if particle.best is None or particle.best.size == 0 or particle.best.fitness < particle.fitness:
particle.best = creator.Particle(particle)
particle.best.fitness.values = particle.fitness.values
if best is None or best.size == 0 or best.fitness < particle.fitness:
best = creator.Particle(particle)
best.fitness.values = particle.fitness.values
for particle in population:
toolbox.update(particle, best)
logbook.record(gen=generation, evals=len(population), **stats.compile(population))
print(logbook.stream)
print("-- Best Particle = ", best)
print("-- Best Fitness =", best.fitness.values[0])
if __import__ == "__main__":
main()