Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. public class Chromosome {
  2.  
  3. private String binaryRepresentation;
  4. int fitness = 0;
  5.  
  6. public Chromosome() {
  7. this.setBinaryRepresentation(this.generateRandomChromosome());
  8. }
  9.  
  10. private String generateRandomChromosome() {
  11. String chromosome = "";
  12. for (int i = 1; i <= 40; i++) {
  13. chromosome += ((int) (Math.random() * 10)) % 2;
  14. }
  15. return chromosome;
  16. }
  17.  
  18. public String getBinaryRepresentation() {
  19. return this.binaryRepresentation;
  20. }
  21.  
  22. private void setBinaryRepresentation(String binaryRepresentation) {
  23. this.binaryRepresentation = binaryRepresentation;
  24. }
  25.  
  26. public String getHexadecimalRepresentation() {
  27.  
  28. return this.gettoHexString(this.getBinaryRepresentation());
  29. }
  30.  
  31. public String gettoHexString(String ss) {
  32. String toBuild = "";
  33. String toRet = "";
  34. int endi = ss.length();
  35. int begi = endi;
  36. for (int ii = 0; ii < ss.length() - 4; ii += 4) {
  37. endi = ss.length() - ii;
  38. begi = endi - 4;
  39. String test = ss.substring(begi, endi);
  40. toBuild += Long.toHexString(Long.parseLong(test, 2));
  41. }
  42. endi = begi;
  43. begi = 0;
  44. toBuild += Long.toHexString(Long.parseLong(ss.substring(begi, endi), 2));
  45.  
  46. for (int ii = toBuild.length(); ii >= 1; ii--) {
  47. toRet += toBuild.substring(ii - 1, ii);
  48. }
  49. return toRet;
  50. }
  51.  
  52. public int getFitness(GeneticKey geneticKey) {
  53. int b = 0;
  54. int h = 0;
  55.  
  56. for (int i = 0; i < geneticKey.getChromosome().getBinaryRepresentation().length(); i++) {
  57. if (binaryRepresentation.charAt(i) == geneticKey.getChromosome().getBinaryRepresentation().charAt(i)) {
  58. b++;
  59. }
  60. }
  61.  
  62. for (int i = 0; i < geneticKey.getChromosome().getHexadecimalRepresentation().length(); i++) {
  63. if (getHexadecimalRepresentation().charAt(i) == geneticKey.getChromosome().getHexadecimalRepresentation().charAt(i)) {
  64. h++;
  65. }
  66.  
  67. }
  68.  
  69. return fitness = (h + 1) * (b * b);
  70. }
  71.  
  72.  
  73. // public void verificarFitness(Chromosome cromosoma) {
  74. // if (cromosoma.getFitness() == GeneticKey.getFitness()) {
  75. // System.out.println("Hola");
  76. // } else {
  77. // System.out.println("Chau");
  78. // }
  79. // }
  80.  
  81.  
  82. public boolean mutate(String binaryRepresentation) {
  83. ArrayList<String> uniques = new ArrayList<String>();
  84. String indices = "";
  85. for (int i = 1; i <= 4; i++) {
  86. indices += ((int) (Math.random() * 10)) % 2;
  87. }
  88. return uniques.add(indices);
  89. }
  90.  
  91. public String crossOver() {
  92. String A = "HelloWorld";
  93. String B = "zzzzzXxxxx";
  94. String C = A.substring(0, 4) + B.substring(5, 9);
  95. String D = B.substring(0, 4) + A.substring(5, 9);
  96. String E = A.substring(0, (int) (Math.random())) + B.substring((int) (Math.random()), 9);
  97.  
  98. return C + D + E;
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement