Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. package circuit;
  2.  
  3. import java.util.Random;
  4. /**
  5. * Classe que "implementa" o algoritmo genético
  6. */
  7. public class GeneticAlgorithm {
  8.  
  9. Population pop;
  10. float pcrossover;
  11. float pmutate;
  12. /**
  13. * Construtor
  14. * @param pop uma população
  15. */
  16. GeneticAlgorithm(Population pop) {
  17. this.pop=pop;
  18. pcrossover=0;
  19. pmutate=0;
  20. }
  21. /**
  22. * Construtor
  23. * @param pop uma população
  24. * @param pcrossover a probabilidade de crossover
  25. * @param pmutate a probabilidade de mutação
  26. */
  27. GeneticAlgorithm(Population pop, float pcrossover, float pmutate) {
  28. this.pop=pop;
  29. this.pcrossover=pcrossover;
  30. this.pmutate=pmutate;
  31. }
  32.  
  33. /**
  34. * Método que pesquisa e devolve o melhor indivíduo encontrado
  35. * @return pop.getBestIndividual(), o melhor indivíduo
  36. */
  37. public Individual search() {
  38. int i=0;
  39. Random r=new Random();
  40. Population best=pop.getElite(10);
  41. do{
  42. for(int j=0;j<best.size;j++){
  43. Individual x,y;
  44. Individual child[]= new Individual[2];
  45. x=best.selectIndividual();
  46. y=best.selectIndividual();
  47. i++;
  48. if(r.nextFloat()<=pcrossover){
  49. child=x.crossover(y);
  50. }else{
  51. child[0]=x;child[1]=y;
  52. }
  53. if(r.nextFloat()<=pmutate) child[0].mutate();
  54. if(r.nextFloat()<=pmutate) child[1].mutate();
  55. best.addIndividual(child[0]);
  56. best.addIndividual(child[1]);
  57. pop=best;
  58. }
  59. }while(i<1000);
  60. return pop.bestInd;
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement