def main(): random.seed(64) hosts = htoolbox.population(n=300) parasites = ptoolbox.population(n=300) hof = tools.HallOfFame(1) hstats = tools.Statistics(lambda ind: ind.fitness.values) hstats.register("avg", numpy.mean) hstats.register("std", numpy.std) hstats.register("min", numpy.min) hstats.register("max", numpy.max) logbook = tools.Logbook() logbook.header = "gen", "evals", "std", "min", "avg", "max" MAXGEN = 50 H_CXPB, H_MUTPB = 0.5, 0.3 P_CXPB, P_MUTPB = 0.5, 0.3 fits = htoolbox.map(htoolbox.evaluate, hosts, parasites) for host, parasite, fit in zip(hosts, parasites, fits): host.fitness.values = parasite.fitness.values = fit hof.update(hosts) record = hstats.compile(hosts) logbook.record(gen=0, evals=len(hosts), **record) print(logbook.stream) for g in range(1, MAXGEN): hosts = htoolbox.select(hosts, len(hosts)) parasites = ptoolbox.select(parasites, len(parasites)) hosts = algorithms.varAnd(hosts, htoolbox, H_CXPB, H_MUTPB) parasites = algorithms.varAnd(parasites, ptoolbox, P_CXPB, P_MUTPB) fits = htoolbox.map(htoolbox.evaluate, hosts, parasites) for host, parasite, fit in zip(hosts, parasites, fits): host.fitness.values = parasite.fitness.values = fit hof.update(hosts) record = hstats.compile(hosts) logbook.record(gen=g, evals=len(hosts), **record) print(logbook.stream) best_network = sn.SortingNetwork(INPUTS, hof[0]) print(best_network) print(best_network.draw()) print("%i errors" % best_network.assess()) return hosts, logbook, hof
def main(): random.seed(64) population = toolbox.population(n=300) hof = tools.ParetoFront() stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean, axis=0) stats.register("std", numpy.std, axis=0) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0) logbook = tools.Logbook() logbook.header = "gen", "evals", "std", "min", "avg", "max" CXPB, MUTPB, ADDPB, DELPB, NGEN = 0.5, 0.2, 0.01, 0.01, 40 # Evaluate every individuals fitnesses = toolbox.map(toolbox.evaluate, population) for ind, fit in zip(population, fitnesses): ind.fitness.values = fit hof.update(population) record = stats.compile(population) logbook.record(gen=0, evals=len(population), **record) print(logbook.stream) # Begin the evolution for g in range(1, NGEN): offspring = [toolbox.clone(ind) for ind in population] # Apply crossover and mutation for ind1, ind2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(ind1, ind2) del ind1.fitness.values del ind2.fitness.values # Note here that we have a different scheme of mutation than in the # original algorithm, we use 3 different mutations subsequently. for ind in offspring: if random.random() < MUTPB: toolbox.mutate(ind) del ind.fitness.values if random.random() < ADDPB: toolbox.addwire(ind) del ind.fitness.values if random.random() < DELPB: toolbox.delwire(ind) del ind.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit population = toolbox.select(population + offspring, len(offspring)) hof.update(population) record = stats.compile(population) logbook.record(gen=g, evals=len(invalid_ind), **record) print(logbook.stream) best_network = sn.SortingNetwork(INPUTS, hof[0]) print(stats) print(best_network) print(best_network.draw()) print("%i errors, length %i, depth %i" % hof[0].fitness.values) return population, logbook, hof
def evalEvoSN(individual, dimension): network = sn.SortingNetwork(dimension, individual) return network.assess(), network.length, network.depth
def main(): random.seed(64) population = toolbox.population(n=300) hof = tools.ParetoFront() stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("Avg", tools.mean) stats.register("Std", tools.std) stats.register("Min", min) stats.register("Max", max) CXPB, MUTPB, ADDPB, DELPB, NGEN = 0.5, 0.2, 0.01, 0.01, 40 # Evaluate every individuals fitnesses = toolbox.map(toolbox.evaluate, population) for ind, fit in zip(population, fitnesses): ind.fitness.values = fit hof.update(population) stats.update(population) # Begin the evolution for g in xrange(NGEN): print "-- Generation %i --" % g offspring = [toolbox.clone(ind) for ind in population] # Apply crossover and mutation for ind1, ind2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(ind1, ind2) del ind1.fitness.values del ind2.fitness.values # Note here that we have a different sheme of mutation than in the # original algorithm, we use 3 different mutations subsequently. for ind in offspring: if random.random() < MUTPB: toolbox.mutate(ind) del ind.fitness.values if random.random() < ADDPB: toolbox.addwire(ind) del ind.fitness.values if random.random() < DELPB: toolbox.delwire(ind) del ind.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit print " Evaluated %i individuals" % len(invalid_ind) population = toolbox.select(population + offspring, len(offspring)) hof.update(population) stats.update(population) print " Min %s" % stats.Min[0][-1][0] print " Max %s" % stats.Max[0][-1][0] print " Avg %s" % stats.Avg[0][-1][0] print " Std %s" % stats.Std[0][-1][0] best_network = sn.SortingNetwork(INPUTS, hof[0]) print best_network print best_network.draw() print "%i errors, length %i, depth %i" % hof[0].fitness.values return population, stats, hof
def evalNetwork(host, parasite, dimension): network = sn.SortingNetwork(dimension, host) return network.assess(parasite),
def main(): random.seed(64) population = toolbox.population(n=500) hof = tools.ParetoFront() stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("Avg", tools.mean) stats.register("Std", tools.std) stats.register("Min", min) stats.register("Max", max) CXPB, MUTPB, ADDPB, DELPB, NGEN = 0.5, 0.2, 0.01, 0.01, 10 # Evaluate every individuals fitnesses = toolbox.map(toolbox.evaluate, population) for ind, fit in zip(population, fitnesses): ind.fitness.values = fit hof.update(population) stats.update(population) # Begin the evolution for g in xrange(NGEN): t1 = time.time() print "-- Generation %i --" % g offspring = [toolbox.clone(ind) for ind in population] t2 = time.time() # Apply crossover and mutation for ind1, ind2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(ind1, ind2) del ind1.fitness.values del ind2.fitness.values t3 = time.time() # Note here that we have a different scheme of mutation than in the # original algorithm, we use 3 different mutations subsequently. for ind in offspring: if random.random() < MUTPB: toolbox.mutate(ind) del ind.fitness.values if random.random() < ADDPB: toolbox.addwire(ind) del ind.fitness.values if random.random() < DELPB: toolbox.delwire(ind) del ind.fitness.values t4 = time.time() # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit print " Evaluated %i individuals" % len(invalid_ind) t5 = time.time() population = toolbox.select(population + offspring, len(offspring)) t6 = time.time() #hof.update(population) stats.update(population) t7 = time.time() print stats print("Times :") print("Clone : " + str(t2 - t1) + " (" + str( (t2 - t1) / (t7 - t1)) + "%)") print("Cx : " + str(t3 - t2) + " (" + str( (t3 - t2) / (t7 - t1)) + "%)") print("Mut : " + str(t4 - t3) + " (" + str( (t4 - t3) / (t7 - t1)) + "%)") print("Eval : " + str(t5 - t4) + " (" + str( (t5 - t4) / (t7 - t1)) + "%)") print("Select : " + str(t6 - t5) + " (" + str( (t6 - t5) / (t7 - t1)) + "%)") print("HOF + stats : " + str(t7 - t6) + " (" + str( (t7 - t6) / (t7 - t1)) + "%)") print("TOTAL : " + str(t7 - t1)) best_network = sn.SortingNetwork(INPUTS, hof[0]) print best_network print best_network.draw() print "%i errors, length %i, depth %i" % hof[0].fitness.values return population, stats, hof
def main(): #random.seed(64) population = tools.population() hof = halloffame.ParetoFront() CXPB, MUTPB, ADDPB, DELPB, NGEN = 0.5, 0.2, 0.01, 0.01, 40 # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in population if not ind.fitness.valid] fitnesses = tools.map(tools.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit hof.update(population) # Begin the evolution for g in xrange(NGEN): print "-- Generation %i --" % g offsprings = [tools.clone(ind) for ind in population] # Apply crossover and mutation for ind1, ind2 in zip(offsprings[::2], offsprings[1::2]): if random.random() < CXPB: tools.mate(ind1, ind2) del ind1.fitness.values del ind2.fitness.values # Note here that we have a different sheme of mutation than in the # original algorithm, we use 3 different mutations subsequently. for ind in offsprings: if random.random() < MUTPB: tools.mutate(ind) del ind.fitness.values if random.random() < ADDPB: tools.addwire(ind) del ind.fitness.values if random.random() < DELPB: tools.delwire(ind) del ind.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offsprings if not ind.fitness.valid] fitnesses = tools.map(tools.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit print " Evaluated %i individuals" % len(invalid_ind) population = tools.select(population + offsprings, n=len(offsprings)) hof.update(population) fits = [ind.fitness.values for ind in population] # Gather all the fitnesses in one list and print the stats fits_t = zip(*fits) # Transpose fitnesses for analysis minimums = map(min, fits_t) maximums = map(max, fits_t) lenght = len(population) sums = map(sum, fits_t) sums2 = [sum(x * x for x in fit) for fit in fits_t] means = [sum_ / lenght for sum_ in sums] std_devs = [ abs(sum2 / lenght - mean**2)**0.5 for sum2, mean in zip(sums2, means) ] print " Min %s" % ", ".join(map(str, minimums)) print " Max %s" % ", ".join(map(str, maximums)) print " Avg %s" % ", ".join(map(str, means)) print " Std %s" % ", ".join(map(str, std_devs)) best_network = sn.SortingNetwork(INPUTS, hof[0]) print best_network print best_network.draw() print "%i errors, length %i, depth %i" % hof[0].fitness.values