public AptNode Mutate(Random r, bool video) { var nodeIndex = r.Next(0, Count()); var(nodeToMutate, _) = GetNthNode(nodeIndex); var leafChance = r.Next(0, Settings.MUTATE_LEAF_CHANCE); AptNode newNode; if (leafChance == 0) { newNode = GetRandomLeaf(r, video); } else { newNode = GetRandomNode(r); } newNode.parent = nodeToMutate.parent; 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]; newNode.children[i].parent = newNode; } } while (newNode.AddLeaf(AptNode.GetRandomLeaf(r, video))) { } if (newNode.parent != null) { for (int i = 0; i < newNode.parent.children.Length; i++) { if (newNode.parent.children[i] == nodeToMutate) { newNode.parent.children[i] = newNode; } } return(null); } else { return(newNode); } }
public Pic BreedWith(Pic partner, Random r, GameState state) { var result = Clone(state); var partnerClone = partner.Clone(state); if (result.type != partner.type && r.Next(0, Settings.CROSSOVER_ROOT_CHANCE) == 0) { //Not gradient -> gradient if (partner.type == PicType.GRADIENT) { result.pos = new float[partner.pos.Length]; Array.Copy(partner.pos, result.pos, partner.pos.Length); result.colors = new Color[partner.colors.Length]; Array.Copy(partner.colors, result.colors, partner.colors.Length); var newMachines = new StackMachine[1]; var newTrees = new AptNode[1]; var(tree, machine) = result.GetRandomTree(r); newTrees[0] = tree.Clone(); newMachines[0] = new StackMachine(newTrees[0]); result.Machines = newMachines; result.Trees = newTrees; } //Gradient -> not gradient else if (result.type == PicType.GRADIENT) { result.pos = null; result.colors = null; var newMachines = new StackMachine[3]; var newTrees = new AptNode[3]; var i = r.Next(0, newTrees.Length); newTrees[i] = result.Trees[0].Clone(); newMachines[i] = new StackMachine(newTrees[i]); for (i = 0; i < newTrees.Length; i++) { if (newTrees[i] == null) { newTrees[i] = AptNode.GetRandomLeaf(r, video); newMachines[i] = new StackMachine(newTrees[i]); } } result.Trees = newTrees; result.Machines = newMachines; } result.type = partner.type; return(result); } else { var(ft, fs) = result.GetRandomTree(r); var(st, ss) = partnerClone.GetRandomTree(r); var rootBred = ft.BreedWith(st, r, video); if (rootBred != null) { for (int i = 0; i < result.Trees.Length; i++) { if (result.Trees[i] == ft) { result.Trees[i] = rootBred; } } } return(result); } }