public TGPTerminal FindRandomTerminal(TGPTerminal current_terminal = null)
        {
            for (int attempts = 0; attempts < 10; attempts++)
            {
                double r = mWeightSum * TreeGP.Distribution.DistributionModel.GetUniform();

                double current_sum = 0;
                foreach (KeyValuePair <TGPTerminal, double> point in mTerminals)
                {
                    current_sum += point.Value;
                    if (current_sum >= r)
                    {
                        if (point.Key != current_terminal)
                        {
                            return(point.Key);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            return(current_terminal);
        }
 public void AddTerminal(TGPTerminal terminal, double weight = 1)
 {
     mTerminals.Add(new KeyValuePair <TGPTerminal, double>(terminal, weight));
     terminal.mTerminalIndex = mTerminals.Count - 1;
     terminal.mIsConstant    = true;
     mWeightSum += weight;
 }
Exemple #3
0
 public void AddTerminal(TGPTerminal terminal, double weight = 1)
 {
     mTerminals[terminal.Symbol] = new KeyValuePair <TGPTerminal, double>(terminal, weight);
     terminal.mTerminalIndex     = mTerminals.Count - 1;
     terminal.mIsConstant        = false;
     mWeightSum += weight;
 }
        /// <summary>
        /// Method that implements the "Point Mutation" described in Section 2.4 of "A Field Guide to Genetic Programming"
        /// In Section 5.2.2 of "A Field Guide to Genetic Programming", this is also described as node replacement mutation
        /// </summary>
        public virtual void MicroMutate()
        {
            TGPNode node = FindRandomNode();

            if (node.IsTerminal)
            {
                TGPTerminal terminal   = FindRandomTerminal();
                int         trials     = 0;
                int         max_trials = 50;
                while (node.Primitive == terminal)
                {
                    terminal = FindRandomTerminal();
                    trials++;
                    if (trials > max_trials)
                    {
                        break;
                    }
                }
                if (terminal != null)
                {
                    node.Primitive = terminal;
                }
            }
            else
            {
                int         parameter_count = node.Arity;
                TGPOperator op = mOperatorSet.FindRandomOperator(parameter_count, (TGPOperator)node.Primitive);
                if (op != null)
                {
                    node.Primitive = op;
                }
            }
        }
        public void AddConstant(string symbol, object value, double weight = 1)
        {
            TGPTerminal terminal = new TGPTerminal("");

            terminal.Value = value;
            AddTerminal(terminal);
            terminal.mSymbol = symbol;
        }
        /// <summary>
        /// Method that adds a function with no arguments to the constant set
        /// </summary>
        /// <param name="symbol">The symbol for the function</param>
        /// <param name="function">The actual mechanism of the function which returns a value</param>
        /// <param name="weight">weight for the function for selection</param>
        public void AddFunction(string symbol, TGPTerminal.FunctorHandle function, double weight = 1)
        {
            TGPTerminal terminal = new TGPTerminal("");

            terminal.FunctionHandle = function;
            terminal.Value          = null;
            AddTerminal(terminal);
            terminal.mSymbol = symbol;
        }
Exemple #7
0
        public TGPTerminal Clone()
        {
            TGPTerminal clone = new TGPTerminal(mSymbol);

            clone.mTerminalIndex = mTerminalIndex;
            clone.mIsConstant    = mIsConstant;
            clone.mValue         = mValue;
            clone.FunctionHandle = FunctionHandle;
            return(clone);
        }
 internal void Update(params object[] tags)
 {
     foreach (KeyValuePair <TGPTerminal, double> pair in mTerminals)
     {
         TGPTerminal terminal = pair.Key;
         if (terminal.FunctionHandle != null)
         {
             terminal.Value = terminal.FunctionHandle(tags);
         }
     }
 }
Exemple #9
0
 public bool IsVariable(TGPPrimitive primitive)
 {
     if (primitive.IsTerminal)
     {
         TGPTerminal terminal = primitive as TGPTerminal;
         if (!terminal.IsConstant)
         {
             return(true);
         }
     }
     return(false);
 }