Posted By

a2-hopewell on 03/21/14


Tagged


Versions (?)

Individual


 / Published in: Java
 

Individual class for Genetic Algorithm

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package simplega;
  6.  
  7. import java.util.Random;
  8. import classification.Rule;
  9.  
  10. /**
  11.  *
  12.  * @author Andee
  13.  */
  14. public class Gene {
  15.  
  16. public static int genelength = 192;
  17. public int[] gene;
  18. private int fitness = 0;
  19.  
  20. Rule rule = new Rule();
  21.  
  22. public Gene()
  23. {
  24. this.gene = new int[genelength];
  25. }
  26.  
  27. public void setGene(int index, int gene)
  28. {
  29. this.gene[index] = gene;
  30. }
  31.  
  32. public int getGene(int index)
  33. {
  34. return gene[index];
  35. }
  36.  
  37. public void generateGene()
  38. {
  39. int allele;
  40. Random rand = new Random();
  41.  
  42. for (int i = 0; i < gene.length; i++) {
  43. allele = rand.nextInt(2);
  44. gene[i] = allele;
  45. }
  46. }
  47.  
  48. public void setFitness(int fitness)
  49. {
  50. this.fitness = fitness;
  51. }
  52.  
  53. public int getFitness()
  54. {
  55. return fitness;
  56. }
  57.  
  58. public int evalFitness(Gene gene, Gene dataset)
  59. {
  60. /*for(int i = 0; i < genelength; i++)
  61.   {
  62.   fitness += this.getGene(i);
  63.   }*/
  64.  
  65. fitness = rule.evalFitness(gene, dataset);
  66.  
  67. this.setFitness(fitness);
  68.  
  69. return fitness;
  70. }
  71. }

Report this snippet  

You need to login to post a comment.