1

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.

jingting
  • 11
  • 1

2 Answers2

0

Having the same problem and sadly getting more confused as dig into it.

When I replicate the code from here that raises the error:

for node in val[1:]:
    total += node.arity - 1
if total != 0:
    raise 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).")

And just run on the tress for crossover individually, both trees have arity scores of 0.

Chris
  • 15,819
  • 3
  • 24
  • 37
Philip
  • 157
  • 1
  • 10
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). You can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/low-quality-posts/25099519) – Izaak van Dongen Jan 15 '20 at 13:56
  • Ahh apologies wasn't 100% sure on process and just wanted to save anyone looking into it some time. Can delete if this is not the place – Philip Jan 16 '20 at 13:57
0

You are using tools.cxOnePoint, you should be using gp.cxOnePoint

Krux99
  • 1
  • @TheMaker If you look at the mate function in the question, he is using the cxOnePoint function from tools. This doesn't take into account genetic programming, and so it fails. To fix it, all you have to do is replace `toolbox.register("mate", tools.cxOnePoint)` in the above code with `toolbox.register("mate", gp.cxOnePoint)` – Krux99 May 24 '20 at 04:33
  • Ok, you could include this explanation in the answer, couldn't you? – 10 Rep May 24 '20 at 17:33