public void InsertWarp(Random r) { var node = this; if (node.children == null) { return; } //1 in 3 chance of applying warp to a node that has an X and a Y as children if (node.children.Length >= 2 && r.Next(0, 3) == 0 && ((node.children[0].type == NodeType.X && node.children[1].type == NodeType.Y) || (node.children[1].type == NodeType.X && node.children[0].type == NodeType.Y))) { Console.WriteLine("WARP"); var newChildren = new AptNode[node.children.Length - 1]; var warp = new AptNode { type = NodeType.WARP1, children = new AptNode[5] }; warp.children[0] = new AptNode { type = NodeType.X }; warp.children[0].parent = warp; warp.children[1] = new AptNode { type = NodeType.Y }; warp.children[1].parent = warp; warp.children[4] = new AptNode { type = NodeType.CONSTANT, value = (float)r.NextDouble() * 2.0f - 1.0f }; warp.children[4].parent = warp; //fill in the stuff the warp node needs while (warp.AddLeaf(GetRandomLeaf(r))) { } newChildren[0] = warp; warp.parent = node; for (int i = 1; i < newChildren.Length; i++) { newChildren[i] = node.children[i + 1]; } node.children = newChildren; } else { foreach (var child in node.children) { child.InsertWarp(r); } } }
public static AptNode GenerateTree(int nodeCount, Random r) { AptNode first = GetRandomNode(r); for (int i = 1; i < nodeCount; i++) { first.AddRandom(GetRandomNode(r), r); } while (first.AddLeaf(GetRandomLeaf(r))) { //just keep adding leaves until we can't } ; first.InsertWarp(r); return(first); }
public static void ReplaceNode(AptNode nodeToMutate, AptNode newNode, Random r) { nodeToMutate.type = newNode.type; nodeToMutate.value = newNode.value; if (nodeToMutate.children != null && newNode.children != null) { for (int i = 0; i < nodeToMutate.children.Length; i++) { if (i == newNode.children.Length) { break; } newNode.children[i] = nodeToMutate.children[i]; } } while (newNode.AddLeaf(GetRandomLeaf(r))) { } nodeToMutate.children = newNode.children; }