mardi 25 octobre 2016

Java - Genetic Algorithm

I´m sorry if this is not the kind of request that it is allowed here, but I've been trying to develop a genetic algorithm and it is due next Tuesday, I've been hitting my head against a wall trying to figure it out, so far I have this:

public class Cromosoma {

private String representacionBinaria;

public Cromosoma() {
    this.setRepresentacionBinaria(this.generarAleatoriamenteCromosoma());
}

private String generarAleatoriamenteCromosoma() {
    String cromosoma = "";
    for (int i = 1; i <= 40; i++) {
        cromosoma += ((int) (Math.random() * 10)) % 2;
    }
    return cromosoma;
}

public String getRepresentacionBinaria() {
    return this.representacionBinaria;
}

private void setRepresentacionBinaria(String representacionBinaria) {
    this.representacionBinaria = representacionBinaria;
}

public String getRepresentacionHexadecimal() {

    return this.gettoHexString(this.getRepresentacionBinaria());
}

public String gettoHexString(String ss) {
    String toBuild = "";
    String toRet = "";
    int endi = ss.length();
    int begi = endi;
    for (int ii = 0; ii < ss.length() - 4; ii += 4) {
        endi = ss.length() - ii;
        begi = endi - 4;
        String test = ss.substring(begi, endi);
        toBuild += Long.toHexString(Long.parseLong(test, 2));
    }
    endi = begi;
    begi = 0;
    toBuild += Long.toHexString(Long.parseLong(ss.substring(begi, endi), 2));

    for (int ii = toBuild.length(); ii >= 1; ii--) {
        toRet += toBuild.substring(ii - 1, ii);
    }
    return toRet;
}

public int getFitness(ClaveGenetica claveGenetica) {
    int b = 0;
    int h = 0;

    for (int i = 0; i < claveGenetica.getCromosoma().getRepresentacionBinaria().length(); i++) {
        if (representacionBinaria.charAt(i) == claveGenetica.getCromosoma().getRepresentacionBinaria().charAt(i)) {
            b++;
        }
    }

    for (int i = 0; i < claveGenetica.getCromosoma().getRepresentacionHexadecimal().length(); i++) {
        if (getRepresentacionHexadecimal().charAt(i) == claveGenetica.getCromosoma().getRepresentacionHexadecimal().charAt(i)) {
            h++;
        }

    }

    return (h + 1) * (b * b);
}

_

public class ClaveGenetica {

private Cromosoma cromosoma;

public ClaveGenetica() {
    this.setCromosoma(new Cromosoma());
}

public Cromosoma getCromosoma() {
    return cromosoma;
}

public void setCromosoma(Cromosoma cromosoma) {
    this.cromosoma = cromosoma;
}

public Integer getFitness() {
    return this.getCromosoma().getFitness(this);
}
}

_

public class Poblacion {

private ArrayList<Cromosoma> cromosomas;

public Poblacion(Integer cantidadIndividuos) {
    this.setCromosomas(new ArrayList<>(cantidadIndividuos));
    this.generarPoblacion(cantidadIndividuos);
}

private void generarPoblacion(int cantidadIndividuos) {
    for (Integer i = 1; i <= cantidadIndividuos; i++)
        this.agregarCromosoma();
}

public void agregarCromosoma() {
    this.getCromosomas().add(new Cromosoma());
}

public void hacerTorneo() {

}

public void seleccionarParaReproducir() {

}

public void seleccionarParaMutar() {

}

public Boolean isClaveGeneticaEnLaPoblacion(ClaveGenetica claveGenetica) {
    for (Cromosoma cromosoma : this.getCromosomas())
        if (cromosoma.getFitness(claveGenetica) == claveGenetica.getFitness())
            return Boolean.TRUE;
    return Boolean.FALSE;
}

public ArrayList<Cromosoma> getCromosomas() {
    return this.cromosomas;
}

private void setCromosomas(ArrayList<Cromosoma> cromosomas) {
    this.cromosomas = cromosomas;
}

}

But it is incomplete, I need the following things:

1) Each Cromosoma needs to have it's own fitness as a variable, so when I do for example new Poblacion(), they have it (fitness) defined.

2) Check if any of the cromosomas have fitness = 17600, if it is, then inform number of generations created. Like: "Cromosoma 4 has 17600 fitness. Number of generations: 3. Algorithm finished."

3) If the solution is not found in the first generation, make tournaments between random cromosomas in the poblacion in twos for the best fitness. Example: Cromosoma 2(600 fitness) vs Cromosoma 3(400 fitness). Winner: Cromosoma 2. Cromosoma 3 gets discarded.

4) Apply genetic operator "crossing point". Form 10 pairs randomly among cromosomas in the new population. Given two chromosomes pair, a crossing point is established, a random number between 0 and 39 (binary). Exchanging bits of parents from crossing point, generating two children. Parents are discarded.

5) Mutate population. Mutation consists in altering some of the bits, changing 4 random bits, from 0 to 1, or 1 to 0 of a random chromosome.

6) Repeat 2) but with the new population after 3), 4) and 5) and inform number of generations created.

Please I'm desperate, I don't know what else to do or who to ask.

Thanks!




Aucun commentaire:

Enregistrer un commentaire