import java.util.collections; import java.util.linkedlist; import java.util.random; public class GeneticAlgorithms static long BEGIN; static final boolean _DEBUG = true; LinkedList<Candidate> population = new LinkedList<Candidate>(); final Random rand = new Random(); final int populationsize = 10; final int parentusepercent = 10; public GeneticAlgorithms() for (int i = 0; i < populationsize; i++) Candidate c = new Candidate(); c.random(); population.add(c); Collections.sort(population); // Método de ordenamiento System.out.println("Inicializa población ordenada");
print(); void print() System.out.println("-- print"); for (Candidate c : population) System.out.println(c); /** * Estrategia de seleccion: Tournament method * Estrategia de reemplazo: elitism 10% * and steady state find 4 random in population not same let 2 fight, and 2 * fight the winners makes 2 children */ void producenextgen() LinkedList<Candidate> newpopulation = new LinkedList<Candidate>(); while (newpopulation.size() < populationsize * (1.0 - (parentusepercent / 100.0))) int size = population.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); Candidate c1 = population.get(i); Candidate c2 = population.get(j); Candidate c3 = population.get(k); Candidate c4 = population.get(l); int f1 = c1.fitness(); int f2 = c2.fitness(); int f3 = c3.fitness(); int f4 = c4.fitness(); Candidate w1, w2; if (f1 > f2) w1 = c1; else w1 = c2;
if (f3 > f4) w2 = c3; else w2 = c4; Candidate child1, child2; // Method one-point crossover random pivot // int pivot = rand.nextint(candidate.size-2) + 1; // cut interval // is 1.. size-1 // child1 = newchild(w1,w2,pivot); // child2 = newchild(w2,w1,pivot); // Method uniform crossover Candidate[] 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) mutate(child1); if (m2) mutate(child2);
boolean ischild1good = child1.fitness() >= w1.fitness(); boolean ischild2good = child2.fitness() >= w2.fitness(); newpopulation.add(ischild1good? child1 : w1); newpopulation.add(ischild2good? child2 : w2); // add top percent parent int j = (int) (populationsize * parentusepercent / 100.0); for (int i = 0; i < j; i++) newpopulation.add(population.get(i)); population = newpopulation; Collections.sort(population); // Cruza a partir de un pivote Candidate newchild(candidate c1, Candidate c2, int pivot) Candidate child = new Candidate(); for (int i = 0; i < pivot; i++) child.genotype[i] = c1.genotype[i];
for (int j = pivot; j < Candidate.SIZE; j++) child.genotype[j] = c2.genotype[j]; return child; // Cruza uniforme Candidate[] newchilds(candidate c1, Candidate c2) Candidate child1 = new Candidate(); Candidate child2 = new Candidate(); for (int i = 0; i < Candidate.SIZE; i++) boolean b = rand.nextfloat() >= 0.5; if (b) child1.genotype[i] = c1.genotype[i]; child2.genotype[i] = c2.genotype[i]; else child1.genotype[i] = c2.genotype[i]; child2.genotype[i] = c1.genotype[i];
return new Candidate[] child1, child2 ; void mutate(candidate c) int i = rand.nextint(candidate.size); c.genotype[i] =!c.genotype[i]; // flip public static void main(string[] args) BEGIN = System.currentTimeMillis(); GeneticAlgorithms ga = new GeneticAlgorithms(); ga.run(); long END = System.currentTimeMillis(); System.out.println("Tiempo: " + (END - BEGIN) / 1000.0 + " sec."); void run() final int maxsteps = 50000; int count = 0;
while (count < maxsteps) count++; producenextgen(); System.out.println("\nResultado"); print(); import java.util.random; public class Candidate implements Comparable<Candidate> public static final int SIZE = 10; public boolean[] genotype; final Random rand = new Random(); public Candidate() genotype = new boolean[size]; void random()
for (int i = 0; i < genotype.length; i++) genotype[i] = 0.5 > rand.nextfloat(); private String gene() StringBuilder sb = new StringBuilder(); for (int i = 0; i < genotype.length; i++) sb.append(genotype[i] == true? 1 : 0); return sb.tostring(); int fitness() int sum = 0; for (int i = 0; i < genotype.length; i++) if (genotype[i]) sum++; return sum;
public int compareto(candidate o) int f1 = this.fitness(); int f2 = o.fitness(); if (f1 < f2) return 1; else if (f1 > f2) return -1; else return 0; @Override public String tostring() return "gene=" + gene() + " fit=" + fitness();