Sie sind auf Seite 1von 6

Rosalba Pacheco Snchez

//Clase Candidato
import java.util.Random;
public class Candidato implements Comparable<Candidato> {
public static final int SIZE = 10;
public boolean[] genotipo;
final Random rand = new Random();

public Candidato() {
genotipo = new boolean[SIZE];
}

void random() {
for (int i = 0; i < genotipo.length; i++) {
genotipo[i] = 0.5 > rand.nextFloat();
}
}
private String gene() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < genotipo.length; i++) {
sb.append(genotipo[i] == true ? 1 : 0);
}
return sb.toString();
}
int fitness() {
int sum = 0;
for (int i = 0; i < genotipo.length; i++) {
if (genotipo[i])
sum++;
}
return sum;
}
public int compareTo(Candidato o) {
int f1 = this.fitness();
int f2 = o.fitness();

if (f1 < f2)
return 1;
else if (f1 > f2)
Rosalba Pacheco Snchez
return -1;
else
return 0;
}
@Override
public String toString() {
return "generacion=" + gene() + " fitness=" + fitness();
}
}

//CLASE AG
import java.util.Collections;
import java.util.LinkedList;
import java.util.Random;
public class AG {

static long BEGIN;
static final boolean _DEBUG = true;
LinkedList<Candidato> poblacion = new LinkedList<Candidato>();
final Random rand = new Random();

final int populacionSize = 50;
final int parentUsePercent = 10;
//Comenzar con una poblacin inicial, la cual puede ser generada de manera aleatoria.
public AG() {
for (int i = 0; i < populacionSize; i++) {
Candidato c = new Candidato();
c.random();
poblacion.add(c);
}

Collections.sort(poblacion); // sort method
System.out.println("Inicializar Poblacin");
print();
}
void print() {
System.out.println("-- print");
for (Candidato c : poblacion) {
Rosalba Pacheco Snchez
System.out.println(c);
}
}
/*
* Aplicar el operador de seleccin con base en el fitness de la poblacin
*/
void produceNextGen() {
LinkedList<Candidato> newpopulation = new LinkedList<Candidato>();

while (newpopulation.size() < populacionSize
* (1.0 - (parentUsePercent / 100.0))) {

int size = poblacion.size();
int i = rand.nextInt(size);
int j, k, l;
j = k = l = i;
while (j == i)
j = rand.nextInt(size);
while (k == i || k == j)
k = rand.nextInt(size);
while (l == i || l == j || k == l)
l = rand.nextInt(size);

Candidato c1 = poblacion.get(i);
Candidato c2 = poblacion.get(j);
Candidato c3 = poblacion.get(k);
Candidato c4 = poblacion.get(l);

int f1 = c1.fitness();
int f2 = c2.fitness();
int f3 = c3.fitness();
int f4 = c4.fitness();

Candidato w1, w2;

if (f1 > f2)
w1 = c1;
else
Rosalba Pacheco Snchez
w1 = c2;

if (f3 > f4)
w2 = c3;
else
w2 = c4;

Candidato child1, child2;

Candidato[] childs = newChilds(w1, w2);
child1 = childs[0];
child2 = childs[1];

double mutatePercent = 0.01;
boolean m1 = rand.nextFloat() <= mutatePercent;
boolean m2 = rand.nextFloat() <= mutatePercent;

if (m1)
mutacion(child1);
if (m2)
mutacion(child2);

boolean isChild1Good = child1.fitness() >= w1.fitness();
boolean isChild2Good = child2.fitness() >= w2.fitness();

newpopulation.add(isChild1Good ? child1 : w1);
newpopulation.add(isChild2Good ? child2 : w2);
}

int j = (int) (populacionSize * parentUsePercent / 100.0);
for (int i = 0; i < j; i++) {
newpopulation.add(poblacion.get(i));
}

poblacion = newpopulation;
Collections.sort(poblacion);
}

Rosalba Pacheco Snchez
Candidato newChild(Candidato c1, Candidato c2, int pivot) {
Candidato child = new Candidato();

for (int i = 0; i < pivot; i++) {
child.genotipo[i] = c1.genotipo[i];
}
for (int j = pivot; j < Candidato.SIZE; j++) {
child.genotipo[j] = c2.genotipo[j];
}

return child;
}

Candidato[] newChilds(Candidato c1, Candidato c2) {
Candidato child1 = new Candidato();
Candidato child2 = new Candidato();
for (int i = 0; i < Candidato.SIZE; i++) {
boolean b = rand.nextFloat() >= 0.5;
if (b) {
child1.genotipo[i] = c1.genotipo[i];
child2.genotipo[i] = c2.genotipo[i];
} else {
child1.genotipo[i] = c2.genotipo[i];
child2.genotipo[i] = c1.genotipo[i];
}
}

return new Candidato[] { child1, child2 };
}
void mutacion(Candidato c) {
int i = rand.nextInt(Candidato.SIZE);
c.genotipo[i] = !c.genotipo[i]; // flip
}
public static void main(String[] args) {
BEGIN = System.currentTimeMillis();

AG ga = new AG();
ga.run();
Rosalba Pacheco Snchez

long END = System.currentTimeMillis();
System.out.println("Time: " + (END - BEGIN) / 1000.0 + " sec.");
}
void run() {
final int maxSteps = 50000;
int count = 0;

while (count < maxSteps) {
count++;
produceNextGen();
}

System.out.println("\nResultado");
print();
}
}

Das könnte Ihnen auch gefallen