Skip to content

Instantly share code, notes, and snippets.

@fmder
Created March 24, 2012 16:59
Show Gist options
  • Save fmder/2185104 to your computer and use it in GitHub Desktop.
Save fmder/2185104 to your computer and use it in GitHub Desktop.
import random
from deap import algorithms, base, creator, tools
import sortingnetwork as sn
INIT_SIZE, INPUTS, MAXGEN = 40, 12, 250
H_CXPB, H_MUTPB = 0.5, 0.3
P_CXPB, PMUT_PB = 0.5, 0.3
def evalNetwork(host, parasite, dimension):
network = sn.SortingNetwork(dimension, host)
cases = [[int(i) for i in ("{0:0%db}" % INPUTS).format(t)] for case in parasite]
return (network.assess(cases), len(host))
def mutComparator(individual, comparator, indpb):
for indx in xrange(len(individual)):
if random.random() < indpb:
individual[indx] = comparator()
return (individual,)
creator.create("FitnessHost", base.Fitness, weights=(-1.0, -1.0))
creator.create("FitnessParasite", base.Fitness, weights=(1.0, 0.0))
creator.create("Host", list, fitness=creator.FitnessHost)
creator.create("Parasite", list, fitness=creator.FitnessParasite)
htbx = base.Toolbox()
htbx.register("wire", random.randint, 0, INPUTS - 1)
htbx.register("comparator", tools.initRepeat, tuple, htbx.wire, n=2)
htbx.register("individual", tools.initRepeat, creator.Host, htbx.comparator, n=INIT_SIZE)
htbx.register("population", tools.initRepeat, list, htbx.individual)
ptbx = base.Toolbox()
ptbx.register("integer", random.randint, 0, 2**INPUTS - 1)
ptbx.register("individual", tools.initRepeat, creator.Parasite, ptbx.integer, n=200)
ptbx.register("population", tools.initRepeat, list, ptbx.individual)
htbx.register("evaluate", evalNetwork, dimension=INPUTS)
htbx.register("mate", tools.cxMessyOnePoint)
htbx.register("mutate", mutComparator, comparator=htbx.comparator, indpb=0.05)
htbx.register("select", tools.selNSGA2)
ptbx.register("mate", tools.cxOnePoint)
ptbx.register("mutate", tools.mutUniformInt, low=0, up=2**INPUTS - 1, indpb=0.05)
ptbx.register("select", tools.selTournament, tournsize=3)
def main():
random.seed(64)
hosts = htbx.population(n=300)
parasites = ptbx.population(n=300)
pareto = tools.ParetoFront()
hstats = tools.Statistics(lambda ind: ind.fitness.values)
hstats.register("avg", tools.mean)
hstats.register("std", tools.std)
hstats.register("min", min)
hstats.register("max", max)
logger = tools.EvolutionLogger(["gen", "evals"] + hstats.functions.keys())
logger.logHeader()
fits = htbx.map(htbx.evaluate, hosts, parasites)
for host, parasite, fit in zip(hosts, parasites, fits):
host.fitness.values = parasite.fitness.values = fit
hof.update(hosts)
hstats.update(hosts)
logger.logGeneration(gen=0, evals=len(hosts), stats=hstats)
for g in range(1, MAXGEN):
hoff = algorithms.varOr(htbx, hosts, len(hosts), H_CXPB, H_MUTPB)
parasites = algorithms.varAnd(ptbx, parasites, P_CXPB, P_MUTPB)
fits = htbx.map(htbx.evaluate, hoff, parasites)
for host, parasite, fit in zip(hoff, parasites, fits):
host.fitness.values = parasite.fitness.values = fit
hosts = htbx.select(hoff, len(hosts))
parasites = ptbx.select(parasites, len(parasites))
hof.update(hosts)
hstats.update(hosts)
logger.logGeneration(gen=g, evals=len(hosts), stats=hstats)
for ind in hof:
network = sn.SortingNetwork(INPUTS, ind)
print "%i errors, %i comparators" % (network.assess(), network.length)
return hosts, hstats, hof
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment