Exemplo n.º 1
0
        //In this method we assume a standard mutation rate of 1.0;
        public void Mutate(VRandom rng, double rate)
        {
            //makes shure the rate is positive
            rate = Math.Abs(rate);

            double selector = rng.NextDouble();


            if (selector < P_EXPAND)
            {
                //expands the network
                Expand(rng);
            }
            else if (selector < P_TOGGEL)
            {
                Axon ax = GetRandomAxon(rng);

                if (ax.Enabled)
                {
                    //outright disables the nuron
                    ax.Enabled = false;
                }
                else
                {
                    //resets the neuron to a large weight
                    ax.Weight  = rng.RandGauss() * SD_NEW;
                    ax.Enabled = true;
                }
            }
            else
            {
                Axon ax = GetRandomAxon(rng);

                if (ax.Enabled)
                {
                    //permutes the weight by a small amount
                    double delta = rng.RandGauss() * SD_SHIFT;
                    ax.Weight = ax.Weight + delta;
                }
                else
                {
                    //resets the nuron to a small weight
                    double delta = rng.RandGauss() * SD_SHIFT;
                    ax.Weight  = delta;
                    ax.Enabled = true;
                }
            }
        }
Exemplo n.º 2
0
        public void Mutate(VRandom rng, double rate)
        {
            if (rng.RandBool(P_Insert * rate))
            {
                //selects a random character in the ASCII range
                int c = rng.RandInt(32, 127);
                mystr = mystr + (char)c;
            }
            else
            {
                //converts the string to a char array
                char[] temp = mystr.ToCharArray();

                //increments or decrements a random character
                int index = rng.RandInt(0, temp.Length);
                int shift = (int)(rng.RandGauss() * 5.0);
                int c     = (int)temp[index] + shift;

                //makes shure the resulting character is valid
                if (c < 32)
                {
                    c = 32;
                }
                if (c > 126)
                {
                    c = 126;
                }
                temp[index] = (char)c;

                //sets the new mutated string
                mystr = new String(temp);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Preterbs the current neural net by some random amount without
        /// creating a clone. The rate of mutaiton determins how many of the
        /// network connections are preturbed. For exampe, a mutation rate
        /// of 0.5 indicates that half the weights will be perturbed.
        /// </summary>
        /// <param name="rate">Rate of mutation</param>
        public void MutateSelf(double rate)
        {
            //clamps the rate to be between zero and one
            rate = VMath.Clamp(rate);

            if (rng.RandBool(P_Node))
            {
                //changes the activation funciton of a single node
                NuronOld node = RandNuron();
                node.Func = RandFunc();
                return;
            }

            //lists all the axons in the network
            var axons = ListAxons();

            foreach (Axon ax in axons)
            {
                //mutates weights based on the rate of mutation
                if (rng.RandBool(1.0 - rate))
                {
                    continue;
                }

                if (ax.Enabled)
                {
                    //permutes the weight by a small amount
                    double delta = rng.RandGauss() * SDS;
                    ax.Weight = ax.Weight + delta;
                }
                else
                {
                    //resets the neuron to a small weight
                    ax.Weight  = rng.RandGauss() * SDS;
                    ax.Enabled = true;
                }
            }
        }
Exemplo n.º 4
0
        public void Randomize(VRandom rng)
        {
            //used to list all the axons
            var ittr = axons.ListItems();

            //sets the weight of each axon to a random value
            foreach (Axon ax in ittr)
            {
                if (!ax.Enabled)
                {
                    continue;
                }
                ax.Weight = rng.RandGauss() * SD_NEW;
            }
        }
Exemplo n.º 5
0
        //This is the (Old) One. It assumes a rate between 0.0 and 1.0
        public void MutateAlt(VRandom rng, double rate)
        {
            //clamps the rate to be between zero and one
            rate = VMath.Clamp(rate);

            //expands the size of the network at random
            //if (rng.RandBool(P_EXPAND)) Expand(rng);
            if (rng.RandBool(rate))
            {
                Expand(rng);
            }

            //used in itterating the structure
            var ittr1 = nurons.ListItems();
            var ittr2 = axons.ListItems();

            //foreach (Nuron n in ittr1)
            //{
            //    //skips over input nurons
            //    if (n.IsInput) continue;

            //    //mutates nodes based on the augmented mutation rate
            //    if (!rng.RandBool(P_NODE * rate)) continue;

            //    //updates the activation funciton
            //    n.Func = GetRandomActivation(rng);
            //}

            foreach (Axon ax in ittr2)
            {
                if (rng.RandBool(P_TOGGEL * rate))
                {
                    //toggeles the enabled state
                    ax.Enabled = !ax.Enabled;
                    if (ax.Enabled)
                    {
                        ax.Weight = 0.0;
                    }
                }

                if (ax.Enabled)
                {
                    //permutes the weight by a small amount
                    double delta = rng.RandGauss() * SD_NEW;
                    ax.Weight = ax.Weight + (delta * rate);
                }
            }
        }
Exemplo n.º 6
0
        private void Expand(VRandom rng)
        {
            Nuron n1, n2;

            //generates a pair of random nurons on diffrent levels
            bool pass = GetRandomPair(rng, out n1, out n2);

            if (!pass)
            {
                return;
            }

            //determins if this should be a recurent conneciton
            bool testrec = recurent;

            testrec = testrec && !n1.IsInput;
            testrec = testrec && rng.RandBool(P_Recur);

            if (testrec)
            {
                Nuron nx = n1;
                n1 = n2;
                n2 = nx;
            }

            //creates a temporary axon between the two nurons
            double w    = rng.RandGauss() * SD_NEW;
            Axon   temp = new Axon(n1.Index, n2.Index, w);

            //atempts to gain the actual axon if it exists
            Axon actual = axons.GetValue(temp.Index);

            if (actual == null)
            {
                //adds the new axon into the network
                axons.Add(temp.Index, temp);
            }
            else if (actual.Enabled == false)
            {
                //reneables the axon and updates it weight
                actual.Enabled = true;
                actual.Weight  = temp.Weight;
            }
            else
            {
                //determins if we are able to insert a new node
                int nid1 = GetRandomIndex(rng);
                if (nid1 < 0)
                {
                    return;
                }

                //sets the level of the new node in the middle
                if (Math.Abs(n1.Level - n2.Level) < 3)
                {
                    return;
                }
                int level = (n1.Level + n2.Level) / 2;

                //adds a new node to the network
                ActFunc act = GetRandomActivation(rng);
                Nuron   n3  = new Nuron(this, act, nid1, level);
                nurons.Add(n3.Index, n3);

                //inserts two new axons where the old one used to be
                int nid0 = actual.Source;
                int nid2 = actual.Target;

                //Axon ax1 = new Axon(nid0, nid1, actual.Weight);
                //Axon ax2 = new Axon(nid1, nid2, temp.Weight);

                Axon ax1 = new Axon(nid0, nid1, actual.Weight);
                Axon ax2 = new Axon(nid1, nid2, 1.0);

                //we use the overwitre comand because there may be hash collisions
                axons.Overwrite(ax1.Index, ax1);
                axons.Overwrite(ax2.Index, ax2);

                //deactivates the old axon
                actual.Enabled = false;
            }
        }