I'm trying to convert an individual (a gp program) into python code. However when I call evaluate() I'm getting an error I'm not sure how to fix. I'm solving symbolic regression problem and these are my primitives.
pset = gp.PrimitiveSet("MAIN", 1)
pset.addPrimitive(operator.add, 2)
pset.addPrimitive(operator.sub, 2)
pset.addPrimitive(operator.mul, 2)
pset.addPrimitive(safeDiv, 2)
pset.addEphemeralConstant(lambda: random.randint(-1,1))
pset.renameArguments(ARG0='x')The error I'm getting is:
NameError: name 'x' is not defined--
You received this message because you are subscribed to the Google Groups "deap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Hi George,
I am not on my computer right now, so I cannot validate it, but I just had an insight on your problem.
I looked again at the decorator code ( https://code.google.com/p/deap/source/browse/deap/gp.py#743 ) and I think you have unveil a potential bug ( or at least a documentation flaw) in Deap.
Thé decorator only modifies the returned individuals, and NOT the ones returned by reference. In your crossover function, you discard thé returned individuals (which would ne normally OK with standard, undecorated operators).
Coule you try to use the return value in your crossover and mutate functions, like :
child1, child2 = toolbox.mate(child1, child2)
I think it may solve your issue. If so, please tell us and we will think of a potential fix ( at least a doc update).
Have a good day, and sorry again for the typos, it is not easy to type this on a phone ;-)
Marc-André
Sadly it's holiday time and I don't have so much time so I can't do a lot of tests, I think I can find some time tomorrow and the day after it for little bit re factoring. I appreciate your time and thanks for the response again.
def configure(self):
""" Creates the toolbox and sets all the needed parameters"""
self.setPrimitiveFunctions()
self.setTerminals(self.terminals)
if not self.isMax : creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
else : creator.create("FitnessMax", base.Fitness, weights=(-1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin, pset=self.pset)
self.toolbox.register("expr", gp.genRamped, pset= self.pset, min_=self.depthInitialMin, max_=self.depthInitialMax)
self.toolbox.register("individual", tools.initIterate, creator.Individual, self.toolbox.expr)
self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual)
self.toolbox.register("select", tools.selTournament, tournsize=3)
self.toolbox.register("mate", gp.cxOnePoint)
self.toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
self.toolbox.register('mutate', gp.mutUniform, expr= self.toolbox.expr_mut)
self.toolbox.decorate('mutate',gp.staticDepthLimit(self.maxDepthLimit))
self.toolbox.decorate('mate',gp.staticDepthLimit(self.maxDepthLimit))
self.configureArguments()
self.outputIndividuals()Hi again,