I'm currently using DEAP for the genetic algorithm in Python. I want to create an initial population of individuals that have length no_sensors
. My problem though is that due to the random.choice(nodes)
function, some nodes end up being the same and the initial length ends up being less than no_sensors
. I was wondering if there's a way to fix this:
creator.create("FitnessMax", base.Fitness, weights=(2.0, -1.0))
creator.create("Individual", set, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("attr_item", random.choice, nodes)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_item, n=no_sensors)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
Basically, I need a fixed length of unique items from the list nodes
. I was thinking of using random.sample(nodes, no_sensors)
but I can't seem to incorporate that into the code without incurring errors
You can check out other example here.