Skip to content

Instantly share code, notes, and snippets.

Created February 7, 2014 11:08
Show Gist options
  • Save anonymous/8860828 to your computer and use it in GitHub Desktop.
Save anonymous/8860828 to your computer and use it in GitHub Desktop.
DEAP with SCOOP
# 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):
M1 = individual.__getitem__(0)
M2 = individual.__getitem__(1)
M3 = individual.__getitem__(2)
am = shared.getConst('am')
bm = shared.getConst('bm')
# 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("evaluate", evaluate)
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)
# Multiprocessing
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():
# 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)
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()
for k in file_vars.loop:
# Shared variables
shared.setConst(am=file_vars.a[file_vars.loop.index(k)])
shared.setConst(bm=file_vars.b[file_vars.loop.index(k)])
pop, stats, hof = main()
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