public override void Mutate(int range, float probability)
    {
        if (probability > Random.Range(0.0f, 1.0f))
        {
            float decide = Random.Range(0.0f, 3.0f);
            if (this.Parent.GetType() != typeof(Exponent_Operator))//set to a new value operand
            {
                //Debug.Log("changing operand to new value");
                this.Value = Random.Range(-range, range);
            }

            /*else if (decide <= 2)//set to variable operand
             * {
             *  //Debug.Log("changing operand to variable");
             *  if (this.Parent != null)
             *  {
             *      if (this.Parent.Child1 == this)
             *      {
             *          this.Parent.Child1 = new Variable();
             *          this.Parent.Child1.Parent = this.Parent;
             *      }
             *      else if (this.Parent.Child2 == this)
             *      {
             *          this.Parent.Child2 = new Variable();
             *          this.Parent.Child2.Parent = this.Parent;
             *      }
             *      else
             *      {
             *          Debug.Assert(false);
             *      }
             *  }
             * } else if (decide <= 3)//set to random operator with basic value operand children
             * {
             *
             * // Debug.Log("changing operand to operator " + this.asString());
             *  Operator op = new Add_Operator();
             *  float decideOperator = Random.Range(0, 7);
             *  //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
             *  try
             *  {
             *      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;
             *      }
             *      else
             *      {
             *          Debug.Assert(false);
             *      }
             *  } catch (System.NullReferenceException e)
             *  {
             *      Debug.Log("error " + this.getHead().asString());
             *      throw new System.NullReferenceException();
             *  }
             *  //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);
        }
    }
Пример #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);
     }
 }
Пример #3
0
 public override void Mutate(int range, float probability)
 {
     Child1.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);
        }
    }