Exemplo n.º 1
0
        private void Crossover(Individuum mother, Individuum father)
        {
            Random       rnd        = new Random();
            Individuum   child1     = new Individuum();
            Individuum   child2     = new Individuum();
            List <Allel> genNewPart = new List <Allel>();
            List <KeyValuePair <Allel, int> > newPositions = new List <KeyValuePair <Allel, int> >();
            int crossoverPoint = rnd.Next(0, parents.population[0].gen.Count);


            for (int i = crossoverPoint; i < mother.gen.Count; i++)
            {
                Allel all      = mother.gen[i];
                int   position = father.gen.FindIndex(ax => ax.id == all.id);
                newPositions.Add(new KeyValuePair <Allel, int>(all, position));
            }
            newPositions.Sort((ind1, ind2) => ind1.Value.CompareTo(ind2.Value));

            foreach (KeyValuePair <Allel, int> kvp in newPositions)
            {
                genNewPart.Add(kvp.Key);
            }
            child1.gen.AddRange(mother.gen.GetRange(0, crossoverPoint));
            child1.gen.AddRange(genNewPart);


            newPositions = new List <KeyValuePair <Allel, int> >();
            genNewPart   = new List <Allel>();
            for (int i = crossoverPoint; i < father.gen.Count; i++)
            {
                Allel all      = father.gen[i];
                int   position = mother.gen.FindIndex(ax => ax.id == all.id);
                newPositions.Add(new KeyValuePair <Allel, int>(all, position));
            }
            newPositions.Sort((ind1, ind2) => ind1.Value.CompareTo(ind2.Value));

            foreach (KeyValuePair <Allel, int> kvp in newPositions)
            {
                genNewPart.Add(kvp.Key);
            }
            child2.gen.AddRange(father.gen.GetRange(0, crossoverPoint));
            child2.gen.AddRange(genNewPart);



            children.addIndividuum(child1);
            children.addIndividuum(child2);
        }
Exemplo n.º 2
0
 public void addIndividuum(Individuum ind)
 {
     population.Add(ind);
 }