Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package circuit;
- import java.util.Random;
- /**
- * Classe que "implementa" o algoritmo genético
- */
- public class GeneticAlgorithm {
- Population pop;
- float pcrossover;
- float pmutate;
- /**
- * Construtor
- * @param pop uma população
- */
- GeneticAlgorithm(Population pop) {
- this.pop=pop;
- pcrossover=0;
- pmutate=0;
- }
- /**
- * Construtor
- * @param pop uma população
- * @param pcrossover a probabilidade de crossover
- * @param pmutate a probabilidade de mutação
- */
- GeneticAlgorithm(Population pop, float pcrossover, float pmutate) {
- this.pop=pop;
- this.pcrossover=pcrossover;
- this.pmutate=pmutate;
- }
- /**
- * Método que pesquisa e devolve o melhor indivíduo encontrado
- * @return pop.getBestIndividual(), o melhor indivíduo
- */
- public Individual search() {
- int i=0;
- Random r=new Random();
- Population best=pop.getElite(10);
- do{
- for(int j=0;j<best.size;j++){
- Individual x,y;
- Individual child[]= new Individual[2];
- x=best.selectIndividual();
- y=best.selectIndividual();
- i++;
- if(r.nextFloat()<=pcrossover){
- child=x.crossover(y);
- }else{
- child[0]=x;child[1]=y;
- }
- if(r.nextFloat()<=pmutate) child[0].mutate();
- if(r.nextFloat()<=pmutate) child[1].mutate();
- best.addIndividual(child[0]);
- best.addIndividual(child[1]);
- pop=best;
- }
- }while(i<1000);
- return pop.bestInd;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement