public void Operator_ConstructorInputsMultiply_CreatesOperatorTreeBasedUponInputs() { var left = new Operand(OperandEnum.x); var right = new Operand(OperandEnum.three); var op = new Operator(OperatorEnum.multiply, left, right); Assert.AreEqual(left.Type, op.Left.Type); Assert.AreEqual(right.Type, op.Right.Type); Assert.AreEqual("(x*3)", op.ToString()); Assert.AreEqual(15, op.Evalate(5)); Assert.AreEqual(false, op.DivideByZeroFlag); Assert.AreEqual(2, op.Size()); Assert.AreEqual(true, op.GetRandomNode().Type == op.Type || op.GetRandomNode().Type == left.Type || op.GetRandomNode().Type == right.Type); }
public void Operator_ConstructorInputsMinus_CreatesOperatorTreeBasedUponInputs() { var left = new Operand(OperandEnum.x); var Right = new Operand(OperandEnum.three); var op = new Operator(OperatorEnum.subtract, left, Right); Assert.AreEqual(left.Type, op.Left.Type, "left type"); Assert.AreEqual(Right.Type, op.Right.Type, "rigth type"); Assert.AreEqual("(x-3)", op.ToString(), "to string"); Assert.AreEqual(2, op.Evalate(5), "evaluate"); Assert.AreEqual(false, op.DivideByZeroFlag, "divide by zero flage"); Assert.AreEqual(2, op.Size(), "height"); Assert.AreEqual(true, op.GetRandomNode().Type == op.Type || op.GetRandomNode().Type == left.Type || op.GetRandomNode().Type == Right.Type, "random node"); }
public void Operator_ConstructorInputsDivideWithZero_CreatesOperatorTreeBasedUponInputs() { var left = new Operand(OperandEnum.x); var right = new Operand(OperandEnum.zero); var op = new Operator(OperatorEnum.divide, left, right); Assert.AreEqual(left.Type, op.Left.Type, "type of left"); Assert.AreEqual(right.Type, op.Right.Type, "type of right"); Assert.AreEqual("(x/0)", op.ToString(), "string"); Assert.AreEqual(double.MaxValue, op.Evalate(6), "evaluate"); Assert.AreEqual(true, op.DivideByZeroFlag, "divide by zero flage"); Assert.AreEqual(2, op.Size(), "height"); //Assert.AreEqual(4, op.GetRandomNode().Type); Assert.AreEqual(true, (op.GetRandomNode().Type == op.Type || op.GetRandomNode().Type == left.Type || op.GetRandomNode().Type == right.Type), "random node"); }
/// <summary> /// Gets a copy of the current node /// </summary> /// <returns></returns> public override Node GetCopy() { var left = _left.GetCopy(); left.IsLeftChild = true; var right = _right.GetCopy(); right.IsLeftChild = false; var op = new Operator(this.OperatorType, left, right); left.Parent = op; right.Parent = op; return op; }
/// <summary> /// Creates the right child based upon the given inputs /// </summary> /// <param name="currHeight"></param> /// <param name="minReachedLeft"></param> private void CreateRight(int currHeight, bool minReachedLeft = true) { if (currHeight + 1 < Settings.MaxTreeHeight) { if (currHeight < Settings.MinTreeHeight && !minReachedLeft) { _right = new Operator(currHeight + 1, minReachedLeft); } else { if (_rGen.NextDouble() < Settings.OperandProb) { _right = new Operand(); } else { _right = new Operator(currHeight + 1, minReachedLeft); } } } else { _right = new Operand(); } _right.Parent = this; _right.IsLeftChild = false; }
/// <summary> /// Increases it to min height /// </summary> /// <param name="currHeight"></param> /// <returns></returns> public override bool Grow(int currHeight) { if (Left.Size() == Right.Size()) { if (_rGen.NextDouble() < 0.5) { if (!Left.Grow(currHeight+1)) _left = new Operator(currHeight + 1, false); } else { if (!Right.Grow(currHeight + 1)) _right = new Operator(currHeight + 1, false); } } else if (Left.Size() > Right.Size()) { if (!Left.Grow(currHeight + 1)) _left = new Operator(currHeight + 1, false); } else { if (!Right.Grow(currHeight + 1)) _right = new Operator(currHeight + 1, false); } return true; }
public void Operator_Constructor_CreatesRandomTree() { var op = new Operator(); Assert.AreEqual(true, op.HasChildren(), "children"); Assert.AreEqual(false, op.HasParent(), "parent"); Assert.AreEqual(true, op.Type <= 3, "type <"); Assert.AreEqual(true, op.Type >= 0, "type >"); Assert.AreEqual(true, op.Size() >= Settings.MinTreeHeight, "min height"); Assert.AreEqual(true, op.Size() <= Settings.MaxTreeHeight, "max height"); }