I have faced with a problem in my code:
pset = gp.PrimitiveSetTyped("main", [], str)
pset.addPrimitive(addRight,[str,str],str)
pset.addPrimitive(addLeft,[str,str],str)
pset.addPrimitive(branch,[str],str)
pset.addPrimitive(chain,[int],str)
pset.addPrimitive(makeBond,[str,str,int],str)
pset.addEphemeralConstant("atom",lambda : random.choice(atom_set),str)
pset.addEphemeralConstant("nums",lambda : random.randint(0,4),int)
expr = gp.genFull(pset, min_=1, max_=3)
tree = gp.PrimitiveTree(expr)
print tree
>>branch(branch(addLeft('As', 'C')))
The code above works correctly but only in rare starts. Usually, I have an exception:
Traceback (most recent call last):
File "C:/Experiments/SMARTS/gp.py", line 81, in <module>
expr = gp.genFull(pset, min_=1, max_=3)
File "C:\Anaconda\lib\site-packages\deap\gp.py", line 516, in genFull
return generate(pset, min_, max_, condition, type_)
File "C:\Anaconda\lib\site-packages\deap\gp.py", line 600, in generate
prim = random.choice(pset.primitives[type_])
File "C:\Anaconda\lib\random.py", line 273, in choice
return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
IndexError: The gp.generate function tried to add a primitive of type '<type 'int'>', but there is none available.
If I modify code without 'int' types, all works fine:
pset = gp.PrimitiveSetTyped("main", [], str)
pset.addPrimitive(addRight,[str,str],str)
pset.addPrimitive(addLeft,[str,str],str)
pset.addPrimitive(branch,[str],str)
pset.addEphemeralConstant("atom",lambda : random.choice(atom_set),str)
expr = gp.genFull(pset, min_=1, max_=3)
tree = gp.PrimitiveTree(expr)
print tree
This code has never thrown an exception