dimon2242

Individ

Jun 29th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.UUID;
  3.  
  4. /**
  5.  * Created by Dmitry Teplyakov on 28.06.17.
  6.  */
  7. public class Individ {
  8.     private UUID id;
  9.     private double result;
  10.     private byte chromosome;
  11.     private boolean isMutated;
  12.  
  13.  
  14.     Individ(int decChromosome) {
  15.         id = UUID.randomUUID();
  16.         chromosome = (byte) (decChromosome & ~192);
  17.         isMutated = false;
  18.         result = 0;
  19.     }
  20.  
  21.     public boolean isMutated() {
  22.         return isMutated;
  23.     }
  24.  
  25.     Individ(Individ firstParent, Individ secondParent) {
  26.         id = UUID.randomUUID();
  27.         chromosome = (byte) ((pairChromosome(firstParent.getChromosome(), secondParent.getChromosome())) & ~192);
  28.         isMutated = false;
  29.         result = 0;
  30.     }
  31.     byte getChromosome() {
  32.         return chromosome;
  33.     }
  34.     UUID getId() {
  35.         return id;
  36.     }
  37.     void setResult(double result) {
  38.         this.result = result;
  39.     }
  40.     double getResult() {
  41.         return result;
  42.     }
  43.     byte pairChromosome(byte chfParent, byte chsParent) {
  44.         byte chromosome = chfParent;
  45.         chromosome &= 240;
  46.         chromosome |= ((chsParent & 15));
  47.         System.out.println(String.format("%6s", Integer.toBinaryString(chromosome)).replace(' ', '0'));
  48.         return (byte) (chromosome & ~192);
  49.     }
  50.     int getFenotype() {
  51.         return chromosome - 10;
  52.     }
  53.     void mutation() {
  54.         Random random = new Random();
  55.         int offset = random.nextInt(6);
  56.         int mask = (1 << offset);
  57.         chromosome ^= mask; // Invert random bit
  58.         //System.out.println(offset); // debug ;)
  59.         isMutated = true;
  60.     }
  61.  
  62. }
Add Comment
Please, Sign In to add comment