I use deep to do the genetic programming. My individual is expression tree. I add some customized operator with arity 1 and 2.
pset = gp.PrimitiveSet("MAIN", 7)
pset.addPrimitive(operator.add, 2)
pset.addPrimitive(operator.sub, 2)
pset.addPrimitive(operator.mul, 2)
pset.addPrimitive(cop.delay10, 1)
pset.addPrimitive(cop.arg_max, 1)
pset.addPrimitive(cop.arg_min, 1)
pset.addPrimitive(cop.rank, 1)
pset.addPrimitive(cop.ma_n, 1)
pset.addPrimitive(cop.std_n, 1)
pset.addPrimitive(cop.max_diff, 1)
pset.addPrimitive(cop.min_diff, 1)
pset.addPrimitive(cop.factor_cov, 2)
pset.addPrimitive(cop.factor_corr, 2)
pset.renameArguments(ARG0="open")
pset.renameArguments(ARG1="high")
pset.renameArguments(ARG2="low")
pset.renameArguments(ARG3="close")
pset.renameArguments(ARG4="volume")
pset.renameArguments(ARG5="turn")
pset.renameArguments(ARG6="re_turn")
# Creators
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)
# Create Individuals
toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=2, max_=5)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
# Create initial population
N_POP = 4
toolbox.register('population', tools.initRepeat, list, toolbox.individual)
toolbox.register("compile", gp.compile, pset=pset)
def evaluate_fit():
... # the part I customize my fitness score calculation function
toolbox.register("evaluate", evaluate_fit, stock_map=stock_dict, mkt=mkt_value, daily_return=wap_return)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("mate", tools.cxOnePoint)
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
pop = toolbox.population(n=N_POP)
hof = tools.HallOfFame(2)
pop = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 2,
halloffame=hof, verbose=False)
Most of my parameters' setting is very similar to the offical example :https://github.com/DEAP/deap/blob/454b4f65a9c944ea2c90b38a75d384cddf524220/examples/gp/symbreg.py I use same crossover method:csOnePoint, all the other setting of my program is same as this example. I think the only different between my program and this example is the customized operator and evaluation method. But I don't know why it always have error when I do crossover step, I have this error:
ValueError: Invalid slice assignation : insertion of an incomplete subtree is not allowed in PrimitiveTree. A tree is defined as incomplete when some nodes cannot be mapped to any position in the tree, considering the primitives' arity. For instance, the tree [sub, 4, 5, 6] is incomplete if the arity of sub is 2, because it would produce an orphan node (the 6).
I understand this may means the tree after crossover is not fulfill the arity requirement, but I don't know why I have this problem. When I try the symberg.py example, I don't encounter this problem.