Created
May 19, 2019 22:19
-
-
Save shipof123/3bf2c6f7ee633eb8ee0501de66fc40f7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/python | |
# Connell Paxton | |
from __future__ import print_function | |
from random import SystemRandom | |
rand = SystemRandom() | |
# utility func for data collection & error handling | |
import sys | |
def eprint(*args, **kwargs): | |
print(*args, file=sys.stderr, end='') | |
import numpy | |
from deap import algorithms | |
from deap import base | |
from deap import creator | |
from deap import tools | |
IND_INIT_SIZE = 5 | |
MAX_ITEM = 50 | |
MAX_WEIGHT = 50 | |
NBR_ITEMS = 20 | |
items = {} | |
for i in range(NBR_ITEMS): | |
items[i] = (rand.randint(1, 10), rand.uniform(0, 100)) | |
creator.create("Fitness", base.Fitness, weights=(-1.0, 1.0)) | |
creator.create("Individual", set, fitness=creator.Fitness) | |
toolbox = base.Toolbox() | |
toolbox.register("attr_item", rand.randrange, NBR_ITEMS) | |
toolbox.register("individual", tools.initRepeat, creator.Individual, | |
toolbox.attr_item, IND_INIT_SIZE) | |
toolbox.register("population", tools.initRepeat, list, toolbox.individual) | |
def evalKnapsack(individual): | |
weight = 0.0 | |
value = 0.0 | |
for item in individual: | |
weight += items[item][0] | |
value += items[item][1] | |
if len(individual) > MAX_ITEM or weight > MAX_WEIGHT: | |
return 10000, 0 | |
return weight, value | |
def cxSet(ind1, ind2): | |
"""Apply a crossover operation on input sets. The first child is the | |
intersection of the two sets, the second child is the difference of the | |
two sets. | |
""" | |
temp = set(ind1) | |
ind1 &= ind2 | |
ind2 ^= temp | |
return ind1, ind2 | |
def mutSet(individual): | |
"""Mutation that pops or add an element.""" | |
if rand.random() < 0.5: | |
if len(individual) > 0: | |
individual.remove(rand.choice(sorted(tuple(individual)))) | |
else: | |
individual.add(rand.randrange(NBR_ITEMS)) | |
return individual, | |
toolbox.register("evaluate", evalKnapsack) | |
toolbox.register("mate", cxSet) | |
toolbox.register("mutate", mutSet) | |
toolbox.register("select", tools.selNSGA2) | |
def main(): | |
from sys import argv | |
NGEN = 25 | |
MU = 50 | |
LAMBDA = 100 | |
CXPB = float(argv[1]) | |
MUTPB = float(argv[2]) | |
pop = toolbox.population(n=MU) | |
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) | |
algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, | |
halloffame=hof) | |
record = stats.compile(pop) | |
eprint(argv[1]+'\t'+argv[2]) | |
for k in ['avg', 'std', 'min', 'max']: | |
eprint('\t'+str(record[k][0])+'\t'+str(record[k][1])) | |
eprint('\n') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment