Question

I ai mis en place un croisement d'un point comme suit:

public void onePointCrossover(Individual indi) {
    if (SGA.rand.nextDouble() < pc) {

        int xoverpoint = SGA.rand.nextInt(length);


        int tmp;
        for (int i=xoverpoint; i<length; i++){
            tmp = chromosome[i];
            chromosome[i] = indi.chromosome[i];
            indi.chromosome[i] = tmp;
        }   
    }   
}

Un point croisé. - point de croisement est sélectionné, une chaîne binaire du début du chromosome vers le point de croisement est copié à partir de l'un des parents, le reste est copié à partir du second parent

Parent 1 = chromosome et Parent 2 = indi.

Je tourne les parents à inplace enfants.

Je maintenant besoin de faire aussi un croisement de deux points, mais ayant quelques problèmes, c'est ce que j'ai jusqu'à présent, mais je crois que la moitié inférieure du code fait la même chose que un point de croisement plutôt que la permutation des sections intermédiaires .

       public void twoPointCrossover(Individual indi) {
        if (SGA.rand.nextDouble() < pc) {

            int xoverpoint = SGA.rand.nextInt(length);
            int xoverpoint2 = SGA.rand.nextInt(length);



            int tmp;

            if (xoverpoint > xoverpoint2){
                tmp = xoverpoint;
                xoverpoint = xoverpoint2;
                xoverpoint2 = tmp;
            }

            for (int i=xoverpoint; i<xoverpoint2; i++){
                tmp = chromosome[i];
                chromosome[i] = indi.chromosome[i];
                indi.chromosome[i] = tmp;
            }   
        }   
    }
}

Cela ne semble pas juste et toute aide sera appréciée tant! Merci!

Pas de solution correcte

Autres conseils

Vous devriez vérifier i < (or <=) xoverpoint2 plutôt que i<length dans la boucle.

Je travaille sur le même problème maintenant. Voici ma solution:

// Two-Point Crossover function
public Genome twoPtCrossover(Genome partner) {
    Genome child = new Genome(partner.genome.length);

    int crosspoint1 = xd.nextInt(genome.length);
    int crosspoint2 = xd.nextInt(genome.length);

    // Ensure crosspoints are different...
    if (crosspoint1 == crosspoint2){
        if(crosspoint1 == 0){
            crosspoint2++;
        } else {
            crosspoint1--;
        }
    }
    // .. and crosspoint1 is lower than crosspoint2
    if (crosspoint2 < crosspoint1) {
        int temp = crosspoint1;
        crosspoint1 = crosspoint2;
        crosspoint2 = temp;
    }

    for (int i = 0; i < genome.length; i++) {
        if (i < crosspoint1 || i > crosspoint2)
            child.genome[i] = genome[i];
        else
            child.genome[i] = partner.genome[i];
    }
    return child;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top