Пример #1
0
        /// <summary>
        /// breads the two NetChromo objects to create a new child NetChromo that inherits traits from both parent NetChromos
        /// </summary>
        /// <param name="mother">One of the parent NetChromo s</param>
        /// <param name="father">The other of the parent NetChromo s</param>
        /// <param name="mutationChance">The chance of a mutation happening. [0..1] should be a small value</param>
        /// <param name="mutationRate">How large of a chance will happen to a trait if it mutates [0..1] should be a small value</param>
        /// <returns>A new NetChromo based of the two parent NetChromos</returns>
        public override Chromo breed(Chromo father, double mutationChance, double mutationRate)
        {
            NetChromo _father = (NetChromo)father;
            NetChromo _mother = this;

            NetChromo child = new NetChromo(_mother);


            for (int a = 0; a < child.chromoData.Length; a++)
            {
                for (int b = 0; b < child.chromoData[a].Length; b++)
                {
                    for (int c = 0; c < child.chromoData[a][b].Length; c++)
                    {
                        // half the time the child should have the farthers gene not the mothers
                        if (ChromoRand.NextDouble() < 0.5)
                        {
                            child.chromoData[a][b][c] = _father.chromoData[a][b][c];
                        }

                        // some of the time we will mutate a gene
                        if (ChromoRand.NextDouble() < mutationChance)
                        {
                            child.chromoData[a][b][c] += mutationRate - (2.0 * mutationRate * ChromoRand.NextDouble());
                        }
                    }
                }
            }

            return(child);
        }
Пример #2
0
        /// <summary>
        /// Converts a <see cref="Chromo"/> into a <see cref="Net"/> via the <see cref="IChromosonable"/> interface.
        /// </summary>
        /// <param name="chromo"> A <see cref="Chromo"/> representing this Net.</param>
        /// <returns> A <see cref="Net"/> that the input represented</returns>
        public object FromChromo(Chromo chromo)
        {
            NetChromo netChromo = (NetChromo)chromo;

            return(new Net(netChromo.getChromoData()));
        }
Пример #3
0
 /// <summary>
 /// Breeds two <see cref="Chromo"/>s to create a new <see cref="Chromo"/> that shares both of there genes with some mutations.
 /// </summary>
 /// <param name="other">The other parent <see cref="Chromo"/> that the child will shared its genes with.</param>
 /// <param name="mutationChance">The chance of a mutation occurring.</param>
 /// <param name="mutationRate">How much a gene will change if it mutates.</param>
 /// <returns>A child <see cref="Chromo"/> that shares this' and other's genes.</returns>
 public abstract Chromo Breed(Chromo other, double mutationChance, double mutationRate);