Ejemplo n.º 1
0
        public void fillDoubleMapp(ref int[] chromosone, int[,] mappingArray, Candidate parent)
        {
            int genToMap;
            int tempGen;

            bool mappingDirection = false; //false X True Y

            for (int i = 0; i < chromosone.Length; i++)
            {
                if (chromosone[i] == 0)
                {
                    genToMap = parent.chromoson[i];
                    tempGen  = genToMap;
                    while (chromosone.Contains(tempGen))
                    {
                        int mapped = directedMap(mappingArray, tempGen, mappingDirection);
                        IntegrityHelper.checkGens(parent);
                        if (tempGen == mapped)
                        {
                            mappingDirection = !mappingDirection;
                        }
                        tempGen = mapped;
                    }
                    if (!chromosone.Contains(tempGen))
                    {
                        chromosone[i] = tempGen;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void IntegrityHelperTestFalse()
        {
            Candidate        cand1 = new Candidate();
            Candidate        cand2 = cand1;
            List <Candidate> list  = new List <Candidate>();

            list.Add(cand1);
            list.Add(cand2);
            Assert.IsFalse(IntegrityHelper.checkCandidateDuplicates(list));
        }
Ejemplo n.º 3
0
        public override List <Candidate> Crossover(Candidate parentX, Candidate parentY)
        {
            List <Candidate> childrenList = new List <Candidate>();
            float            chance       = (float)RandomCrossover.NextDouble();

            if (chance < CrossoverChance)
            {
                IntegrityHelper.checkGens(parentX);
                IntegrityHelper.checkGens(parentY);
                int startIndex = RandomCrossover.Next(0, parentX.chromoson.Count() - 1);
                int endIndex   = RandomCrossover.Next(0, parentX.chromoson.Count() - 1); //random indexes

                if (startIndex > endIndex)
                {
                    int temp = startIndex;
                    startIndex = endIndex;
                    endIndex   = temp;
                }
                int[,] mappingArray = createMappingArray(parentX, parentY, startIndex, endIndex);
                int[] childXChromosome = new int[parentX.chromoson.Count()];
                int[] childYChromosome = new int[parentX.chromoson.Count()];

                FillChromosone(ref childXChromosome, startIndex, mappingArray, 1);
                FillChromosone(ref childYChromosome, startIndex, mappingArray, 0);

                fillMissingGens(ref childXChromosome, parentX, startIndex, endIndex);
                fillMissingGens(ref childYChromosome, parentY, startIndex, endIndex);

                fillMappedGens(ref childXChromosome, mappingArray, parentX);
                fillDoubleMapp(ref childXChromosome, mappingArray, parentX);

                fillMappedGens(ref childYChromosome, mappingArray, parentY);
                fillDoubleMapp(ref childYChromosome, mappingArray, parentY);

                Candidate childX = new Candidate(parentX.generation, childXChromosome.ToList(), parentX.solver, parentX.solver.time.ElapsedMilliseconds.ToString());
                Candidate childY = new Candidate(parentY.generation, childYChromosome.ToList(), parentY.solver, parentY.solver.time.ElapsedMilliseconds.ToString());

                childX.generation = (parentX.generation + 1);
                childY.generation = (parentY.generation + 1);
                childrenList.Add(childX);
                childrenList.Add(childY);
                IntegrityHelper.checkGens(childrenList);
            }
            else
            {
                Candidate childX = new Candidate(parentX.generation, parentX.chromoson, parentX.solver, parentX.solver.time.ElapsedMilliseconds.ToString());
                Candidate childY = new Candidate(parentY.generation, parentY.chromoson, parentY.solver, parentY.solver.time.ElapsedMilliseconds.ToString());
                childX.generation = (parentX.generation + 1);
                childY.generation = (parentY.generation + 1);
                childrenList.Add(childX);
                childrenList.Add(childY);
                IntegrityHelper.checkGens(childrenList);
            }
            return(childrenList);
        }
        public override List <Candidate> Crossover(Candidate parentX, Candidate parentY)
        {
            List <Candidate> childrenList = new List <Candidate>();
            float            chance       = (float)RandomCrossover.NextDouble();

            if (chance < CrossoverChance)
            {
                int startIndex = RandomCrossover.Next(0, parentX.chromoson.Count() - 1);
                int endIndex   = RandomCrossover.Next(0, parentX.chromoson.Count() - 1);
                if (startIndex > endIndex)
                {
                    int temp = startIndex;
                    startIndex = endIndex;
                    endIndex   = temp;
                }
                int[] childXChromosome = new int[parentX.chromoson.Count()];
                int[] childYChromosome = new int[parentX.chromoson.Count()];
                int[] parentYSection   = new int[endIndex - startIndex];
                int[] parentXSection   = new int[endIndex - startIndex];

                fillParentSection(ref parentXSection, parentX.chromoson, startIndex, endIndex);
                fillParentSection(ref parentYSection, parentY.chromoson, startIndex, endIndex);
                FillChromosoneWithParenScetion(ref childXChromosome, parentYSection, startIndex);
                FillChromosoneWithParenScetion(ref childYChromosome, parentXSection, startIndex);

                FillChromosoneWithMissingGens(ref childXChromosome, parentX.chromoson, endIndex);
                FillChromosoneWithMissingGens(ref childYChromosome, parentY.chromoson, endIndex);

                Candidate childX = new Candidate(parentX.generation + 1, childXChromosome.ToList(), parentX.solver, parentX.solver.time.ElapsedMilliseconds.ToString());
                Candidate childY = new Candidate(parentY.generation + 1, childYChromosome.ToList(), parentY.solver, parentY.solver.time.ElapsedMilliseconds.ToString());


                childrenList.Add(childX);
                childrenList.Add(childY);
                IntegrityHelper.checkGens(childrenList);
            }
            else
            {
                Candidate childX = new Candidate(parentX.generation, parentX.chromoson, parentX.solver, parentX.solver.time.ElapsedMilliseconds.ToString());
                Candidate childY = new Candidate(parentY.generation, parentY.chromoson, parentY.solver, parentY.solver.time.ElapsedMilliseconds.ToString());
                childX.generation = (parentX.generation + 1);
                childY.generation = (parentY.generation + 1);
                childrenList.Add(childX);
                childrenList.Add(childY);
                IntegrityHelper.checkGens(childrenList);
            }
            return(childrenList);
        }