Exemplo n.º 1
0
        //public static Individual recombine(Individual parent1, Individual parent2)
        //{
        //    //vermeiden, dass niedrigstes oder höchstes Gen gewählt wird, weil sonst Klon eines Elternteils entsteht
        //    int selectedGenPosition = GlobalSettings.random.Next(GlobalSettings.NumberOfGenes-1) + 1;
        //    Individual child = new Individual();
        //    if (selectedGenPosition > 0)
        //    {
        //        child.gens.RemoveRange(0, selectedGenPosition);
        //        child.gens.InsertRange(0, parent1.gens.GetRange(0, selectedGenPosition));
        //    }
        //    child.gens[selectedGenPosition].recombine(parent1.gens[selectedGenPosition], parent2.gens[selectedGenPosition]);
        //    if (selectedGenPosition < parent2.gens.Count - 1 )
        //    {
        //        child.gens.RemoveRange(selectedGenPosition + 1, parent2.gens.Count - 1 - selectedGenPosition);
        //        child.gens.InsertRange(selectedGenPosition + 1, parent2.gens.GetRange(selectedGenPosition + 1, parent2.gens.Count - 1 - selectedGenPosition));
        //    }
        //    return child;
        //}
        public static Individual recombine(Individual parent1, Individual parent2)
        {
            //vermeiden, dass niedrigstes oder höchstes Gen gewählt wird, weil sonst Klon eines Elternteils entsteht
            int selectedGenPosition = GlobalSettings.random.Next(GlobalSettings.NumberOfGenes * BinaryGene.Size - 2) + 1;
            int sliceGeneNumber = selectedGenPosition / BinaryGene.Size;
            Individual child = new Individual();
            child.gens.Clear();
            if (sliceGeneNumber > 0)
            {
                for (int i = 0; i < sliceGeneNumber; i++)
                {
                    child.gens.Add((parent1.gens[i] as BinaryGene).Clone() as BinaryGene);
                }
            }

            child.gens.Add(new BinaryGene(parent1.gens[sliceGeneNumber], parent2.gens[sliceGeneNumber], selectedGenPosition % BinaryGene.Size));

            if (sliceGeneNumber < parent2.gens.Count - 1)
            {
                for (int i = child.gens.Count; i < GlobalSettings.NumberOfGenes; i++)
                {
                    child.gens.Add((parent2.gens[i] as BinaryGene).Clone() as BinaryGene);
                }
            }

            return child;
        }
 public abstract double calculateFitness(Individual ind);