コード例 #1
0
ファイル: Order1Crossover.cs プロジェクト: peadotnet/PEA.NET
        public int[] CrossoverGenes(int[] parent1Genes, int[] parent2Genes, GeneRange swapRange)
        {
            var length     = parent1Genes.Length;
            var childGenes = new int[length];

            HashSet <int> swapRangeGenes = new HashSet <int>();

            for (int i = swapRange.Position; i <= swapRange.End; i++)
            {
                swapRangeGenes.Add(parent1Genes[i]);
            }

            int pos = 0;

            for (int p = 0; p < length; p++)
            {
                if (p >= swapRange.Position && p <= swapRange.End)
                {
                    childGenes[p] = parent1Genes[p];
                }
                else
                {
                    while (swapRangeGenes.Contains(parent2Genes[pos]))
                    {
                        pos++;
                    }

                    childGenes[p] = parent2Genes[pos];
                    pos++;
                }
            }
            return(childGenes);
        }
コード例 #2
0
        public override PermutationChromosome Mutate(PermutationChromosome chromosome)
        {
            if (chromosome == null)
            {
                return(null);
            }
            if (chromosome.Genes.Length < 2)
            {
                return(chromosome);
            }

            //TODO: ConflictCheck, retry

            //int retryCount = ParameterSet.GetInt(ParameterNames.FailedMutationRetryCount);
            //while (true)
            //{
            var sourceRange    = GetSourceRange(chromosome);
            int targetPosition = GetRandomPositionWithTabuRange(chromosome, sourceRange);

            if (targetPosition < sourceRange.Position)
            {
                var swapRange = new GeneRange(targetPosition, sourceRange.Position - targetPosition);
                chromosome.Genes = SwapTwoRange(chromosome.Genes, swapRange, sourceRange);
            }
            else
            {
                int sourceEndPosition = sourceRange.Position + sourceRange.Length;
                var swapRange         = new GeneRange(sourceEndPosition, targetPosition - sourceEndPosition);
                chromosome.Genes = SwapTwoRange(chromosome.Genes, sourceRange, swapRange);
            }
            //    if (success || retryCount-- < 0) break;
            //}
            return(chromosome);
        }
コード例 #3
0
        public bool IsDisjointWith(GeneRange other)
        {
            var thisEnd  = this.Position + this.Length;
            var otherEnd = other.Position + other.Length;

            return(thisEnd < other.Position || otherEnd < this.Position);
        }
コード例 #4
0
        public int GetRandomPositionWithTabuRange(PermutationChromosome chromosome, GeneRange tabuRange)
        {
            int  position   = 0;
            bool getFromEnd = false;

            if (tabuRange.Position == 0)
            {
                getFromEnd = true;
            }
            else if (tabuRange.Position + tabuRange.Length < chromosome.Genes.Length)
            {
                var decision = Random.GetDouble(0, 1);
                if (decision > 0.5)
                {
                    getFromEnd = true;
                }
            }

            if (getFromEnd)
            {
                position = Random.GetInt(tabuRange.Position + tabuRange.Length, chromosome.Genes.Length);
            }
            else
            {
                position = Random.GetInt(0, tabuRange.Position);
            }
            return(position);
        }
コード例 #5
0
        public Dictionary <int, int> GenerateGeneMap(int[] parent2Genes, GeneRange swapRange)
        {
            var swapRegionGeneMap = new Dictionary <int, int>();

            for (int pos = swapRange.Position; pos < swapRange.Position + swapRange.Length; pos++)
            {
                swapRegionGeneMap.Add(parent2Genes[pos], pos);
            }

            return(swapRegionGeneMap);
        }
コード例 #6
0
        public int[] SwapTwoRange(int[] genes, GeneRange range1, GeneRange range2)
        {
            if (range2.Position < range1.Position)
            {
                var r = range2;
                range2 = range1;
                range1 = r;
            }

            if (range2.Position < range1.Position + range1.Length)
            {
                if (range1.IsDisjointWith(range2))
                {
                    var error = true;
                }

                throw new ArgumentException("Gene ranges are overlapped!");
            }
            var temp = new int[genes.Length];
            var range1EndPosition = range1.Position + range1.Length;
            var range2EndPosition = range2.Position + range2.Length;
            var middleLength      = range2.Position - range1EndPosition;

            if (range1.Position > 0)
            {
                Array.Copy(genes, 0, temp, 0, range1.Position);
            }
            Array.Copy(genes, range2.Position, temp, range1.Position, range2.Length);
            if (middleLength > 0)
            {
                Array.Copy(genes, range1EndPosition, temp, range1.Position + range2.Length, middleLength);
            }
            Array.Copy(genes, range1.Position, temp, range2EndPosition - range1.Length, range1.Length);
            if (range2EndPosition < genes.Length)
            {
                Array.Copy(genes, range2EndPosition, temp, range2EndPosition, genes.Length - range2EndPosition);
            }

            return(temp);
        }
コード例 #7
0
        public int[] CrossoverGenes(int[] parent1Genes, int[] parent2Genes, GeneRange swapRange)
        {
            var length     = parent1Genes.Length;
            var childGenes = new int[length];

            var swapRegionGeneMap = GenerateGeneMap(parent1Genes, swapRange);

            if (swapRange.Position > 0)
            {
                CopyWithDuplicationElimination(parent2Genes, childGenes, swapRegionGeneMap, 0, swapRange.Position);
            }

            Array.Copy(parent1Genes, swapRange.Position, childGenes, swapRange.Position, swapRange.Length);

            int swapEnd = swapRange.Position + swapRange.Length;

            if (swapEnd < parent2Genes.Length)
            {
                CopyWithDuplicationElimination(parent2Genes, childGenes, swapRegionGeneMap, swapEnd, length);
            }

            return(childGenes);
        }
コード例 #8
0
        public override PermutationChromosome Mutate(PermutationChromosome chromosome)
        {
            if (chromosome == null)
            {
                return(null);
            }
            if (chromosome.Genes.Length < 2)
            {
                return(chromosome);
            }

            //TODO: ConflictCheck, retry

            //int retryCount = ParameterSet.GetInt(ParameterNames.FailedMutationRetryCount);
            //while (true)
            //{
            var sourcePosition = Random.GetInt(0, chromosome.Genes.Length);

            int targetPosition = Random.GetIntWithTabu(0, chromosome.Genes.Length, sourcePosition);

            if (sourcePosition > targetPosition)
            {
                var tempPos = sourcePosition;
                sourcePosition = targetPosition;
                targetPosition = tempPos;
            }

            var sourceRange = new GeneRange(sourcePosition, 1);
            var swapRange   = new GeneRange(sourcePosition + 1, targetPosition - sourcePosition - 1);

            chromosome.Genes = SwapTwoRange(chromosome.Genes, sourceRange, swapRange);

            //    if (success || retryCount-- < 0) break;
            //}
            return(chromosome);
        }