Esempio n. 1
0
        /// <summary>
        /// Starts, and handles, the evolution.
        /// </summary>
        /// <returns>The best individual at the end of the evolution.</returns>
        public Evolvable Evolve()
        {
            for (var i = 0; i < this.NumberOfGenerations; i++)
            {
                var candidates = this.SelectParents();
                this.SpawnChildren(candidates);
                this.EvaluatePopulation();
                this.SelectPopulationForNextGeneration();
            }

            Evolvable best = null;

            foreach (var individual in this.Population)
            {
                if (best == null)
                {
                    best = individual;
                }
                if (individual.Fitness > best.Fitness)
                {
                    best = individual;
                }
            }

            return(best);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a recombination between this object and other.
        /// </summary>
        /// <param name="other">The object to recombine with this object. Must be an EvolvableDoubleArray.</param>
        /// <returns>A recombination between the two objects.</returns>
        public override Evolvable SpawnRecombination(Evolvable other)
        {
            if (other.GetType() != this.GetType())
            {
                return(this);
            }
            var tempOther = (EvolvableDoubleArray)other;

            var tempDArray = new EvolvableDoubleArray(this.MutationChance, this.Random);
            var tempArray  = new double[this.Numbers.Length];

            for (int i = 0; i < this.Numbers.Length; i++)
            {
                tempArray[i] = ((this.Numbers[i] + tempOther.Numbers[i]) / 2) + (this.Random.NextDouble() - 0.5);
            }

            tempDArray.SetNumbers(tempArray);

            return(tempDArray);
        }
Esempio n. 3
0
        /// <summary>
        /// Starts, and handles, the evolution.
        /// </summary>
        /// <param name="sb">The string builder to write time to.</param>
        /// <returns>The best individual at the end of the evolution.</returns>
        public Evolvable Evolve(StringBuilder sb)
        {
            var sw = new Stopwatch();

            sw.Start();

            for (var i = 0; i < this.NumberOfGenerations; i++)
            {
                var candidates = this.SelectParents();
                this.SpawnChildren(candidates);
                this.EvaluatePopulation();
                this.SelectPopulationForNextGeneration();

                sb.AppendLine(string.Format("\tIt took {0} ms to advance the evolution to generation {1}.", sw.ElapsedMilliseconds, i + 1));

                var pop     = this.Population.OrderByDescending(s => s.Fitness).ToList();
                var average = this.Population.Sum(s => s.Fitness) / this.Population.Count;
                sb.AppendLine(string.Format("\t\t The highest fitness in the population is {0}", pop[0].Fitness));
                sb.AppendLine(string.Format("\t\t The average fitness in the population is {0}", average));
                sb.AppendLine(string.Format("\t\t The lowest fitness in the population is {0}", pop[this.Population.Count - 1].Fitness));

                sw.Restart();
            }

            Evolvable best = null;

            foreach (var individual in this.Population)
            {
                if (best == null)
                {
                    best = individual;
                }
                if (individual.Fitness > best.Fitness)
                {
                    best = individual;
                }
            }

            return(best);
        }
Esempio n. 4
0
 /// <summary>
 /// Creates a recombination between this evolvable and other.
 /// </summary>
 /// <param name="other">The other evolvable to create a recombination with.</param>
 /// <returns>A recombination between this evolvable and other.</returns>
 public abstract Evolvable SpawnRecombination(Evolvable other);
Esempio n. 5
0
 /// <summary>
 /// Combines this individual with another to form a new individual.
 /// </summary>
 /// <param name="other">The individual to combine with.</param>
 /// <returns>The newly formed individual.</returns>
 public override Evolvable SpawnRecombination(Evolvable other)
 {
     throw new NotImplementedException();
 }