I've been working on generating roads through maximizing specific properties of the road. I'm using the DEAP Framework for the evolution part. My roads are represented as dicts. I've encountered a problem, while I'm mating two roads. The deap-tutorial states, that the parameter are changed inside the the toolbox operation: 'The general rule for crossover operators is that they only mate individuals, this means that an independent copies must be made prior to mating the individuals if the original individuals have to be kept or is are references to other individuals.' So as I tried to copy and paste the solution in the tutorial combined with my crossover operation I end up either with a list element, which doesn't have a fitness.value, or the toolbox operation doesn't change the road at all. Is there anything wrong with the way I'm registering the individuals?
creator.create("FitnessMax", base.Fitness, weights=(1.0, 1.0))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("getInd", getRandomTrack)
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.getInd, n=1)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evalTest)
toolbox.register("mutate", mutateTestCase)
toolbox.register("mate", tools.cross)
toolbox.register("select", tools.selNSGA2)
The crossover operation just loads a saved dict for now, because the problem is about not being seen as a individual by the toolbox:
def cross(ind1, ind2):
with open('c:/Users/****/.asfaultenv/output/final
/test_0004.json') as f
ind2 = json.load(f)
with open('c:/Users/****/.asfaultenv/output/final
/test_0004.json') as f
ind1 = json.load(f)
return (ind1), (ind2)
And here is the loop, which should load the individuals and mate them:
# Clone the selected individuals
offspring = [toolbox.clone(ind) for ind in pop]
# I've also used the approach beneath
# offspring = list(map(toolbox.clone, pop))
for child1, child2 in zip(offspring[::2], offspring[1::2]):
toolbox.mate(child1, child2)
if child1 is not None:
del child1.fitness.values
if child2 is not None:
del child2.fitness.values
As I mentioned above, this way, the children are not being manipulated in any way and when I try something like this:
child1, child2 = toolbox.mate(child1, child2)
Then I'll get the error: 'list' object has no attribute 'fitness'
Thank you for taking the time.