コード例 #1
0
        public void Mutate1(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);
                bool add   = rng.RandBool();
                int  c     = (int)temp[index] + (add ? 1 : -1);

                //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);
            }
        }
コード例 #2
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);
                }
            }
        }
コード例 #3
0
        public void Mutate(VRandom rng, double rate)
        {
            if (rng.RandBool(P_Insert * rate))
            {
                int index = rng.RandInt(1, mystr.Length);
                int len   = mystr.Length;

                //splits the string at the instertion point
                string s1 = mystr.Substring(0, index);
                string s2 = mystr.Substring(index, len - index);

                //selects a random character in the ASCII range
                int c = rng.RandInt(32, 127);
                mystr = s1 + (char)c + s2;
            }

            //converts the string to a char array
            char[] temp = mystr.ToCharArray();

            if (rng.RandBool(P_Swap * rate))
            {
                //select two random indicies
                int i = rng.RandInt(0, temp.Length);
                int j = rng.RandInt(0, temp.Length);

                //swaps the characters at the two incicies
                char c = temp[i];
                temp[i] = temp[j];
                temp[j] = c;
            }

            for (int i = 0; i < temp.Length; i++)
            {
                //skips over characters with the inverse rate
                if (!rng.RandBool(rate))
                {
                    continue;
                }

                //selects a random character in the ASCII range
                int c = rng.RandInt(32, 127);
                temp[i] = (char)c;
            }

            //sets the new mutated string
            mystr = new String(temp);
        }
コード例 #4
0
        /// <summary>
        /// Combines the genes of the curent nural net with the genes of
        /// another network to create a brand new offspring. The idea is
        /// that the child network will possess trates from both its
        /// parents, similar to sexual reproduction.
        /// </summary>
        /// <param name="rng">Random number generator</param>
        /// <param name="mate">Mate of the curent network</param>
        /// <returns>The child of both networks</returns>
        public NetworkCPP Combine(VRandom rng, NetworkCPP mate)
        {
            //makes a clone of the dominate parent
            NetworkCPP child = new NetworkCPP(rng, this);

            //determins weather or not to do liniar crossover
            bool   liniar = rng.RandBool(P_Linear);
            double a      = rng.NextDouble();

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

            foreach (Axon ax in axons)
            {
                //obtains the matching child axon
                Axon axc = child.FindMatch(ax);
                if (axc == null)
                {
                    continue;
                }

                if (liniar)
                {
                    //chooses a value between the two weights
                    double weight = axc.Weight * (1.0 - a);
                    axc.Weight = weight + (ax.Weight * a);
                }
                else
                {
                    //determins the new weight based on crossover
                    bool cross = rng.RandBool();
                    if (cross)
                    {
                        axc.Weight = ax.Weight;
                    }
                }

                //has a chance of enabling if either are disabled
                bool en = ax.Enabled && axc.Enabled;
                axc.Enabled = en || rng.RandBool(0.25);
            }

            return(child);
        }
コード例 #5
0
        public void Mutate2(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();

                for (int i = 0; i < temp.Length; i++)
                {
                    //alters each character with a chance of 'rate'
                    if (!rng.RandBool(rate))
                    {
                        continue;
                    }

                    //increments or decrements the character
                    bool add = rng.RandBool();
                    int  c   = (int)temp[i] + (add ? 1 : -1);

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

                //sets the new mutated string
                mystr = new String(temp);
            }
        }
コード例 #6
0
        private void Procreate(Species <T> spec, Organism <T> rep, T scratch)
        {
            Organism <T> mom = spec.GetRandMember(rng);
            Organism <T> dad = null;

            if (crossover && rng.RandBool(CrossRate))
            {
                if (rng.RandBool(TransSpecies))
                {
                    //obtains the best dad from a random species
                    int sindex = rng.RandInt(species.Count);
                    dad = species[sindex].Prototype;

                    Console.Write("T");
                }
                else
                {
                    //obtains a random dad from the curent species
                    dad = spec.GetRandMember(rng);
                }

                if (mom.TrueFit > dad.TrueFit)
                {
                    //swaps the parents if mom is better than dad
                    Organism <T> temp = dad;
                    dad = mom;
                    mom = dad;
                }

                //creates a new offspring from both parents
                scratch.Overwrite(dad.Genome);
                scratch.Crossover(rng, mom.Genome);
                scratch.Mutate(rng, rate);
            }
            else
            {
                //creates a mutated offspring from the single parent
                scratch.Overwrite(mom.Genome);
                scratch.Mutate(rng, rate);
            }
        }
コード例 #7
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;
                }
            }
        }
コード例 #8
0
        public void Mutate4(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 c     = rng.RandInt(32, 127);
                temp[index] = (char)c;

                //sets the new mutated string
                mystr = new String(temp);
            }
        }
コード例 #9
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;
            }
        }
コード例 #10
0
        public void Crossover(VRandom rng, NetworkAuto genome)
        {
            //throw new NotImplementedException();

            //determins weather or not to do liniar crossover
            bool   liniar = rng.RandBool(P_Linear);
            double a      = rng.NextDouble();

            //lists all the axons and nurons in the mate
            var ittr1 = genome.nurons.ListItems();
            var ittr2 = genome.axons.ListItems();

            foreach (Nuron n in ittr1)
            {
                //obtains the matching child nuron
                Nuron nc = nurons.GetValue(n.Index);

                if (nc == null)
                {
                    //adds the missing nuron
                    nc = new Nuron(this, n);
                    nurons.Add(nc.Index, nc);
                }
                //else
                //{
                //    //determins the activation based on crossover
                //    bool cross = rng.RandBool();
                //    if (cross) nc.Func = n.Func;
                //}
            }

            foreach (Axon ax in ittr2)
            {
                //obtains the matching child axon
                Axon axc = axons.GetValue(ax.Index);

                if (axc == null)
                {
                    //adds the missing axon, but disabled
                    axc         = new Axon(ax);
                    axc.Enabled = false;
                    axons.Add(axc.Index, axc);
                }
                else
                {
                    if (liniar)
                    {
                        //chooses a value between the two weights
                        double weight = axc.Weight * (1.0 - a);
                        axc.Weight = weight + (ax.Weight * a);
                    }
                    else
                    {
                        //determins the new weight based on crossover
                        bool cross = rng.RandBool();
                        if (cross)
                        {
                            axc.Weight = ax.Weight;
                        }
                    }

                    //if the axon is present in both networks, it has a
                    //strong chance of becoming enabled
                    bool en = ax.Enabled && axc.Enabled;
                    axc.Enabled = en || rng.RandBool(0.25);
                }
            }
        }