internal void Mutate() { // pick any node but the root (since that would be a replacement of the entire tree, not a mutation) NodeBaseType <T, S> randomNode = null; do { randomNode = SelectRandomNode(); } while (randomNode == Root); // build a random subtree bool buildFullTrees = Randomizer.GetFloatFromZeroToOne() < 0.5F; int depth = Randomizer.IntBetween(TreeMinDepth, TreeMaxDepth); var newSubtree = AddRandomNode(null, depth, buildFullTrees); // replace the randomly selected node with the new random one var parent = (FunctionNode <T, S>)randomNode.Parent; newSubtree.Parent = parent; // find the link from the parent to the old child, and replace it for (int i = 0; i < parent.Children.Count; i++) { if (parent.Children[i] == randomNode) { parent.Children[i] = newSubtree; break; } } }
public void CreateRandom() { // ramped half-and-half approach: // do grow or full approach, evenly split // full: choose function nodes for all levels except the maximum, which is always terminal // grow: randomly choose function or terminal, except for max depth which is always terminal bool buildFullTrees = Randomizer.GetFloatFromZeroToOne() < 0.5; int depth = Randomizer.IntBetween(TreeMinDepth, TreeMaxDepth); Root = AddRandomNode(null, depth, buildFullTrees); }