Esempio n. 1
0
        /// <summary>
        /// Generate a random number in the specified range.
        /// </summary>
        /// <param name="rnd">Random number generator.</param>
        /// <param name="min">The minimum value.</param>
        /// <param name="max">The maximum value.</param>
        /// <returns>A random number.</returns>
        public static double Randomize(EncogRandom rnd, double min, double max)
        {
            double range = max - min;

            return((range * rnd.NextDouble()) + min);
        }
Esempio n. 2
0
 /// <summary>
 /// Generate a random number in the specified range.
 /// </summary>
 /// <param name="rnd">Random number generator.</param>
 /// <param name="min">The minimum value.</param>
 /// <param name="max">The maximum value.</param>
 /// <returns>A random number.</returns>
 public static double Randomize(EncogRandom rnd, double min, double max)
 {
     double range = max - min;
     return (range * rnd.NextDouble()) + min;
 }
Esempio n. 3
0
        /// <inheritdoc/>
        public void PerformOperation(EncogRandom rnd, IGenome[] parents,
                                     int parentIndex, IGenome[] offspring,
                                     int offspringIndex)
        {
            var mom = (NEATGenome)parents[parentIndex + 0];
            var dad = (NEATGenome)parents[parentIndex + 1];

            var best    = FavorParent(rnd, mom, dad);
            var notBest = (best == mom) ? mom : dad;

            var selectedLinks   = new List <NEATLinkGene>();
            var selectedNeurons = new List <NEATNeuronGene>();

            int          curMom       = 0; // current gene index from mom
            int          curDad       = 0; // current gene index from dad
            NEATLinkGene selectedGene = null;

            // add in the input and bias, they should always be here
            int alwaysCount = ((NEATGenome)parents[0]).InputCount
                              + ((NEATGenome)parents[0]).OutputCount + 1;

            for (int i = 0; i < alwaysCount; i++)
            {
                AddNeuronId(i, selectedNeurons, best, notBest);
            }

            while ((curMom < mom.NumGenes) || (curDad < dad.NumGenes))
            {
                NEATLinkGene momGene       = null; // the mom gene object
                NEATLinkGene dadGene       = null; // the dad gene object
                long         momInnovation = -1;
                long         dadInnovation = -1;

                // grab the actual objects from mom and dad for the specified
                // indexes
                // if there are none, then null
                if (curMom < mom.NumGenes)
                {
                    momGene       = mom.LinksChromosome[curMom];
                    momInnovation = momGene.InnovationId;
                }

                if (curDad < dad.NumGenes)
                {
                    dadGene       = dad.LinksChromosome[curDad];
                    dadInnovation = dadGene.InnovationId;
                }

                // now select a gene for mom or dad. This gene is for the baby
                if ((momGene == null) && (dadGene != null))
                {
                    if (best == dad)
                    {
                        selectedGene = dadGene;
                    }
                    curDad++;
                }
                else if ((dadGene == null) && (momGene != null))
                {
                    if (best == mom)
                    {
                        selectedGene = momGene;
                    }
                    curMom++;
                }
                else if (momInnovation < dadInnovation)
                {
                    if (best == mom)
                    {
                        selectedGene = momGene;
                    }
                    curMom++;
                }
                else if (dadInnovation < momInnovation)
                {
                    if (best == dad)
                    {
                        selectedGene = dadGene;
                    }
                    curDad++;
                }
                else if (dadInnovation == momInnovation)
                {
                    selectedGene = rnd.NextDouble() < 0.5f ? momGene : dadGene;
                    curMom++;
                    curDad++;
                }

                if (selectedGene != null)
                {
                    if (selectedLinks.Count == 0)
                    {
                        selectedLinks.Add(selectedGene);
                    }
                    else
                    {
                        if (selectedLinks[selectedLinks.Count - 1]
                            .InnovationId != selectedGene
                            .InnovationId)
                        {
                            selectedLinks.Add(selectedGene);
                        }
                    }

                    // Check if we already have the nodes referred to in
                    // SelectedGene.
                    // If not, they need to be added.
                    AddNeuronId(selectedGene.FromNeuronId, selectedNeurons,
                                best, notBest);
                    AddNeuronId(selectedGene.ToNeuronId, selectedNeurons,
                                best, notBest);
                }
            }

            // now create the required nodes. First sort them into order
            selectedNeurons.Sort();

            // finally, create the genome
            var factory = (INEATGenomeFactory)_owner
                          .Population.GenomeFactory;
            var babyGenome = factory.Factor(selectedNeurons,
                                            selectedLinks, mom.InputCount, mom.OutputCount);

            babyGenome.BirthGeneration = _owner.IterationNumber;
            babyGenome.Population      = _owner.Population;
            babyGenome.SortGenes();

            offspring[offspringIndex] = babyGenome;
        }