예제 #1
0
파일: Splice.cs 프로젝트: shturm/encog-prim
        /// <inheritdoc/>
        public void PerformOperation(EncogRandom rnd, IGenome[] parents, int parentIndex,
                                     IGenome[] offspring, int offspringIndex)
        {
            IArrayGenome mother     = (IArrayGenome)parents[parentIndex];
            IArrayGenome father     = (IArrayGenome)parents[parentIndex + 1];
            IArrayGenome offspring1 = (IArrayGenome)this.owner.Population.GenomeFactory.Factor();
            IArrayGenome offspring2 = (IArrayGenome)this.owner.Population.GenomeFactory.Factor();

            offspring[offspringIndex]     = offspring1;
            offspring[offspringIndex + 1] = offspring2;

            int geneLength = mother.Size;

            // the chromosome must be cut at two positions, determine them
            int cutpoint1 = (int)(rnd.Next(geneLength - this.cutLength));
            int cutpoint2 = cutpoint1 + this.cutLength;

            // handle cut section
            for (int i = 0; i < geneLength; i++)
            {
                if (!((i < cutpoint1) || (i > cutpoint2)))
                {
                    offspring1.Copy(father, i, i);
                    offspring2.Copy(mother, i, i);
                }
            }

            // handle outer sections
            for (int i = 0; i < geneLength; i++)
            {
                if ((i < cutpoint1) || (i > cutpoint2))
                {
                    offspring1.Copy(mother, i, i);
                    offspring2.Copy(father, i, i);
                }
            }
        }
예제 #2
0
        /// <inheritdoc/>
        public void PerformOperation(EncogRandom rnd, IGenome[] parents, int parentIndex,
                                     IGenome[] offspring, int offspringIndex)
        {
            IArrayGenome parent = (IArrayGenome)parents[parentIndex];

            offspring[offspringIndex] = this.owner.Population.GenomeFactory.Factor();
            IArrayGenome child = (IArrayGenome)offspring[offspringIndex];

            child.Copy(parent);

            int length = parent.Size;
            int iswap1 = (int)(rnd.NextDouble() * length);
            int iswap2 = (int)(rnd.NextDouble() * length);

            // can't be equal
            if (iswap1 == iswap2)
            {
                // move to the next, but
                // don't go out of bounds
                if (iswap1 > 0)
                {
                    iswap1--;
                }
                else
                {
                    iswap1++;
                }
            }

            // make sure they are in the right order
            if (iswap1 > iswap2)
            {
                int temp = iswap1;
                iswap1 = iswap2;
                iswap2 = temp;
            }

            child.Swap(iswap1, iswap2);
        }