public override Node differentiate() { Node p = new Cosine_Operator(); Node c1 = Child1; Multiply_Operator m = new Multiply_Operator(); Node c2 = c1.differentiate(); m.Child1 = c2; p.Child1 = c1; m.Child2 = p; p.Parent = m; c2.Parent = m; return(m); }
public override void Mutate(int range, float probability) { if (false) { float decide = Random.Range(0.0f, 3.0f); if (decide <= 1)//set to a new value operand { //Debug.Log("changing variable to value operand"); if (this.Parent.Child1 == this) { Operand o = new Operand(Random.Range(-range, range)); o.Parent = this.Parent; this.Parent.Child1 = o; } else if (this.Parent.Child2 == this) { Operand o = new Operand(Random.Range(-range, range)); o.Parent = this.Parent; this.Parent.Child2 = o; } else { Debug.Assert(false); } } else if (decide <= 2)//set to variable { //Debug.Log("variable is staying as variable"); } else if (decide <= 3)//set to random operator with basic value operand children { // Debug.Log("changing variable to operator"); Operator op = new Add_Operator(); float decideOperator = Random.Range(0.0f, 7.0f); //generator randome operator if (decideOperator <= 1) { op = new Add_Operator(); } else if (decideOperator <= 2) { op = new Minus_Operator(); } else if (decideOperator <= 3) { op = new Multiply_Operator(); } else if (decideOperator <= 4) { op = new Divide_Operator(); } else if (decideOperator <= 5) { op = new Exponent_Operator(); } else if (decideOperator <= 6) { op = new Sin_Operator(); } else if (decideOperator <= 7) { op = new Cosine_Operator(); } //find which of the parents children is this object if (this.Parent.Child1 == this) { this.Parent.Child1 = op; op.Parent = this.Parent; } else if (this.Parent.Child2 == this) { this.Parent.Child2 = op; op.Parent = this.Parent; } //want to only add one child if its a sin or cosine operator if (op is Cosine_Operator || op is Sin_Operator) { op.Child1 = new Operand(1); op.Child1.Parent = op; } else { op.Child1 = new Operand(1); op.Child2 = new Operand(1); op.Child1.Parent = op; op.Child2.Parent = op; } } } if (Child1 != null) { Child1.Mutate(range, probability); } if (Child2 != null) { Child2.Mutate(range, probability); } }
//returns a node object corrosponding to the character public Node parseNodeType(string[] text) { try { for (int i = 0; i < text.Length; i++) { // print("" + (text[i])); } if (text[GlobalCount] == "*") { GlobalCount++; return(new Multiply_Operator()); } else if (text[GlobalCount] == "~") //use '~' instead of - to make dealing with double negatives easier { GlobalCount++; return(new Minus_Operator()); } else if (text[GlobalCount] == "+") { GlobalCount++; return(new Add_Operator()); } else if (text[GlobalCount] == "/") { GlobalCount++; return(new Divide_Operator()); } else if (text[GlobalCount] == "x") { GlobalCount++; return(new Variable()); } else if (text[GlobalCount] == "(") { GlobalCount++; Parser newParse = new Parser(); string[] sss = getBracketText(text); Node tree = newParse.ParseNodeTree(sss); return(tree.getHead()); } else if (text[GlobalCount] == "s") { GlobalCount += 4; Parser newParse = new Parser(); string[] sss = getBracketText(text); Node tree = newParse.ParseNodeTree(sss); Sin_Operator sinOp = new Sin_Operator(); sinOp.Child1 = tree; return(sinOp); } else if (text[GlobalCount] == "c") { GlobalCount += 4; Parser newParse = new Parser(); string[] sss = getBracketText(text); Node tree = newParse.ParseNodeTree(sss); Cosine_Operator sinOp = new Cosine_Operator(); sinOp.Child1 = tree; return(sinOp); } else if (text[GlobalCount] == "^") { GlobalCount++; return(new Exponent_Operator()); } else { if (text[GlobalCount] == "-") { GlobalCount++; float oper = getOperand(text); return(new Operand(oper * -1));//return negative number } else { float oper = getOperand(text); return(new Operand(oper)); } //if the text is not a number, then it will throw a conversion error } }catch (System.IndexOutOfRangeException e) { print(e); string inputString = ""; for (int i = 0; i < text.Length; i++) { inputString = inputString + text[i]; } print(inputString + " " + text.Length); return(null); } }
public override void Mutate(int range, float probability) { Debug.Assert(probability >= 0 && probability <= 1); if (false)//change operator to another operator or an operand { //Debug.Log("from this view: " + this.Parent.GetType() + " " + this.GetType() + " " + this.Child1.GetType() + " " + this.Child2.GetType()); // Debug.Log("from parent: " + this.Parent.Child1.GetType()); //Debug.Log("from child view: " + this.Child2.Parent.GetType() + " " + this.Child2.Parent.GetType()); float decide = Random.Range(0.0f, 8.0f); if (decide < 0) { //Debug.Log("changing operator to add"); Node newNode = new Add_Operator(); this.replaceNode(newNode); } else if (decide < 1) { //Debug.Log("changing operator to minus"); Node newNode = new Minus_Operator(); this.replaceNode(newNode); } else if (decide < 2) { //Debug.Log("changing operator to mult"); Node newNode = new Multiply_Operator(); this.replaceNode(newNode); } else if (decide < 3) { //Debug.Log("changing operator to divide"); Node newNode = new Divide_Operator(); this.replaceNode(newNode); } else if (decide < 4) { //Debug.Log("changing operator to cos"); Node newNode = new Cosine_Operator(); this.replaceNode(newNode); } else if (decide < 5) { //Debug.Log("changing operator to sin"); Node newNode = new Sin_Operator(); this.replaceNode(newNode); } else if (decide < 6) { //Debug.Log("changing operator to exponent"); Node newNode = new Exponent_Operator(); this.replaceNode(newNode); } else if (decide < 7) { //Debug.Log("changing operator to operand"); Node newNode = new Operand(Random.Range(-range, range)); this.replaceNode(newNode); } else if (decide < 8) { //Debug.Log("changing operator to variable"); Node newNode = new Variable(); this.replaceNode(newNode); } } if (Child1 != null) { Child1.Mutate(range, probability); } if (Child2 != null) { Child2.Mutate(range, probability); } }