-
-
Save soravux/8864096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# imports | |
import BBP | |
import numpy | |
import random | |
import time | |
import wx | |
from math import pi, sqrt, log10 | |
from deap import base | |
from deap import creator | |
from deap import tools | |
from deap import algorithms | |
from scoop import futures | |
# GA Configuration | |
creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) | |
creator.create("Individual", list, fitness=creator.FitnessMin) | |
toolbox = base.Toolbox() | |
# Attribute generator | |
FLT_M1_min, FLT_M1_max = 0.0, 1.0 | |
FLT_M2_min, FLT_M2_max = 0.0, 1.0 | |
FLT_M3_min, FLT_M3_max = 0.0, 1.0 | |
N_CYCLES = 1 | |
toolbox.register("attr_M1", random.uniform, FLT_M1_min, FLT_M1_max) | |
toolbox.register("attr_M2", random.uniform, FLT_M2_min, FLT_M2_max) | |
toolbox.register("attr_M3", random.uniform, FLT_M3_min, FLT_M3_max) | |
# Structure initializers | |
toolbox.register("individual", tools.initCycle, creator.Individual,(toolbox.attr_M1, | |
toolbox.attr_M2,toolbox.attr_M3), n = N_CYCLES) | |
toolbox.register("population", tools.initRepeat, list, toolbox.individual) | |
# Fitness function | |
def evaluate(individual, am, bm): | |
M1 = individual[0] | |
M2 = individual[1] | |
M3 = individual[2] | |
# BBP Function | |
a, b = BBP.bbp(M1, M2, M3) | |
error_a = abs(20*log10(a)-20*log10(am)) | |
error_b = abs(20*log10(b)-20*log10(bm)) | |
# Evaluation of the result | |
evaluation = sqrt(error_a) + sqrt(error_b) | |
return evaluation, | |
# Operator registering | |
toolbox.register("mate", tools.cxTwoPoints) | |
toolbox.register("mutate", tools.mutGaussian,mu=0.0, sigma=0.2, indpb=0.2) | |
toolbox.register("select", tools.selTournament, tournsize=4) | |
# Parallelization | |
toolbox.register("map", futures.map) | |
# vars | |
class file_vars: | |
def __init__(self): | |
self.loop = [] | |
self.a = [] | |
self.b = [] | |
def load_files(): | |
path = './file/file' | |
content = [line.rstrip('\n') for line in open(path)] | |
for line in content: | |
word = line.split() | |
if word[0]=='loop': file_vars.loop.append(float(word[2])) | |
if word[0]=='a': file_vars.a.append(float(word[2])) | |
if word[0]=='b': file_vars.b.append(float(word[2])) | |
def main(am, bm): | |
# Initialization the basic random number generator. | |
random.seed(64) | |
# Initials | |
CXPB, MUTPB, NGEN, MU = 0.5, 0.2, 1, 4 | |
# Generation of population | |
pop = toolbox.population(n = MU) | |
toolbox.register("evaluate", evaluate, am=am, bm=bm) | |
hof = tools.HallOfFame(1) | |
stats = tools.Statistics(lambda ind: ind.fitness.values) | |
stats.register("avg", numpy.mean) | |
stats.register("std", numpy.std) | |
stats.register("min", min) | |
stats.register("max", max) | |
algorithms.eaSimple(pop, toolbox, CXPB, MUTPB, NGEN, stats=stats, | |
halloffame=hof, verbose=True) | |
return pop, stats, hof | |
if __name__ == "__main__": | |
start_time = time.time() | |
ea_result = futures.map( | |
main, | |
file_vars.a | |
file_vars.b | |
) | |
for pop, stats, hof in ea_result: | |
print("Best individual is %s" % hof) | |
results.append(hof[0]) | |
print time.time() - start_time, "seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment