def mutInsert(individual, pset):
 index = random.randrange(len(individual))
 node = individual[index]
 slice_ = individual.searchSubtree(index)
 choice = random.choice
 primitives = [p for p in pset.primitives[node.ret] if (node.ret in p.args)]
 if (len(primitives) == 0):
  return (individual,)
 new_node = choice(primitives)
 new_subtree = ([None] * len(new_node.args))
 position = choice([i for (i, a) in enumerate(new_node.args) if (a == node.ret)])
 for (i, arg_type) in enumerate(new_node.args):
  if (i != position):
   term = choice(pset.terminals[arg_type])
   if isclass(term):
    term = term()
   new_subtree[i] = term
 new_subtree[position:(position + 1)] = individual[slice_]
 new_subtree.insert(0, new_node)
 individual[slice_] = new_subtree
 return (individual,)

def mutInsert ( individual , pset ) : index = random . randrange ( len ( individual ) ) node = individual [ index ] slice_ = individual . searchSubtree ( index ) choice = random . choice primitives = [ p for p in pset . primitives [ node . ret ] if ( node . ret in p . args ) ] if ( len ( primitives ) == 0 ) : return ( individual , ) new_node = choice ( primitives ) new_subtree = ( [ None ] * len ( new_node . args ) ) position = choice ( [ i for ( i , a ) in enumerate ( new_node . args ) if ( a == node . ret ) ] ) for ( i , arg_type ) in enumerate ( new_node . args ) : if ( i != position ) : term = choice ( pset . terminals [ arg_type ] ) if isclass ( term ) : term = term ( ) new_subtree [ i ] = term new_subtree [ position : ( position + 1 ) ] = individual [ slice_ ] new_subtree . insert ( 0 , new_node ) individual [ slice_ ] = new_subtree return ( individual , )
inserts a new branch at a random position in *individual* .