예제 #1
0
    public override Node differentiate()
    {
        Add_Operator      a1 = new Add_Operator();
        Multiply_Operator m1 = new Multiply_Operator();
        Multiply_Operator m2 = new Multiply_Operator();
        Node c1 = Child1;
        Node c2 = Child2.differentiate();
        Node c3 = Child1.differentiate();
        Node c4 = Child2;

        a1.Child1 = m1;
        m1.Parent = a1;
        a1.Child2 = m2;
        m2.Parent = a1;

        m1.Child1 = c1;
        c1.Parent = m1;
        m1.Child2 = c2;
        c2.Parent = m1;

        m2.Child1 = c3;
        c3.Parent = m2;
        m2.Child2 = c4;
        c4.Parent = m2;

        return(a1);
    }
예제 #2
0
 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);
     }
 }
    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);
        }
    }