private GPCustomTreeNode RandomSwap(GPCustomTreeNode source)
{
GPCustomTreeNode retNode = null;
// swap root node ?
if ((root.Children == null) || (rand.Next(maxLevel) == 0))
{
// replace current root and return it
retNode = root;
root = source;
}
else
{
GPCustomTreeNode node = root;
for (; ; )
{
// choose random child
int r = rand.Next(node.Gene.ArgumentsCount);
GPCustomTreeNode child = (GPCustomTreeNode)node.Children[r];
// swap the random node, if it is an end node or
// random generator "selected" this node
if ((child.Children == null) || (rand.Next(maxLevel) == 0))
{
// swap the node with pair's one
retNode = child;
node.Children[r] = source;
break;
}
// go further by tree
node = child;
}
}
return retNode;
}