Posted By

a2-hopewell on 03/21/14


Tagged


Versions (?)

Rule


 / Published in: Java
 

Rule class for Genetic Algorithm

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package classification;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10.  
  11. import simplega.Gene;
  12.  
  13. /**
  14.  *
  15.  * @author Andee
  16.  */
  17. public class Rule {
  18.  
  19. final static int ruleLength = 5;
  20.  
  21. public Gene dataset;
  22.  
  23. public Rule()
  24. {
  25.  
  26. }
  27.  
  28. public Gene readFile(String filename) throws IOException
  29. {
  30. dataset = new Gene();
  31. int i, n = 0;
  32. BufferedReader br = new BufferedReader(new FileReader(filename));
  33.  
  34. while(br.readLine() != null)
  35. {
  36. for(i = 0; i < ruleLength; i++)
  37. {
  38. dataset.setGene(n, (br.read() - 48));
  39. n++;
  40. }
  41. br.read();
  42. dataset.setGene(n, (br.read() - 48));
  43. n++;
  44. }
  45.  
  46. return dataset;
  47. }
  48.  
  49. public int evalFitness(Gene gene, Gene dataset)
  50. {
  51. int fitness = 0;
  52. int a, b;
  53.  
  54. for(a = 0; a < Gene.genelength; a+=6)
  55. {
  56. for(b = 0; b < Gene.genelength; b+=6)
  57. {
  58. if((dataset.getGene(a) == gene.getGene(b)) &&
  59. (dataset.getGene(a+1) == gene.getGene(b+1)) &&
  60. (dataset.getGene(a+2) == gene.getGene(b+2)) &&
  61. (dataset.getGene(a+3) == gene.getGene(b+3)) &&
  62. (dataset.getGene(a+4) == gene.getGene(b+4)))
  63. {
  64. if(dataset.getGene(a+5) == gene.getGene(b+5))
  65. {
  66. fitness += 1;
  67. }
  68. break;
  69. }
  70. }
  71. }
  72.  
  73. return fitness;
  74. }
  75.  
  76. }

Report this snippet  

You need to login to post a comment.