Beispiel #1
0
 @Override
 public Genotype clone() {
   Genotype retValue = new Genotype(token.clone());
   retValue.left = left == null ? null : left.clone();
   retValue.right = right == null ? null : right.clone();
   return retValue;
 }
Beispiel #2
0
 /**
  * This function fixes any branches ending with a condition by adding the missing cases to this
  * node.
  *
  * @param g The root node.
  */
 private void fixupTree(Genotype g) {
   if (g.token.getToken() == TOKENS.H_LT_N || g.token.getToken() == TOKENS.H_GT_N) {
     if (g.left == null) g.left = new Genotype(new Token(false));
     if (g.right == null) g.right = new Genotype(new Token(false));
   }
   if (g.left != null) fixupTree(g.left);
   if (g.right != null) fixupTree(g.right);
 }
Beispiel #3
0
 /** This function constructs the tree for debug purposes in a text view. */
 public StringBuilder toString(StringBuilder prefix, boolean isTail, StringBuilder sb) {
   if (right != null) {
     right.toString(
         new StringBuilder().append(prefix).append(isTail ? "│   " : "    "), false, sb);
   }
   sb.append(prefix).append(isTail ? "└── " : "┌── ").append(token.toString()).append("\n");
   if (left != null) {
     left.toString(new StringBuilder().append(prefix).append(isTail ? "    " : "│   "), true, sb);
   }
   return sb;
 }
Beispiel #4
0
 /**
  * A recursive function that converts a string into a genotype.
  *
  * @param g The current node.
  * @param s The string (array to pass by reference).
  * @return The filled node.
  */
 private Genotype decodeString(Genotype g, String[] s) {
   char in = s[0].charAt(0);
   if (in == ';') {
     s[0] = s[0].substring(1);
     return null;
   }
   int token = Integer.parseInt(s[0].substring(0, 2));
   int amount = Integer.parseInt(s[0].substring(2, 4));
   if (g != null) g.token = new Token(TokenMap.get(token), amount);
   else g = new Genotype(new Token(TokenMap.get(token), amount));
   s[0] = s[0].substring(4);
   g.left = decodeString(g.left, s);
   g.right = decodeString(g.right, s);
   return g;
 }
Beispiel #5
0
 /**
  * This function tries to place a new node in a tree at the correct location.
  *
  * @param g The node to place.
  * @param previous The previous node.
  */
 private void place(Genotype g, Genotype previous) {
   if (g == null) {
     g = new Genotype(new Token(true));
     if (previous.left == null) previous.left = g;
     else previous.right = g;
     return;
   }
   if (g.left == null) place(g.left, g);
   else if (g.right == null) place(g.right, g);
   else {
     int cleft = count(g.left);
     int cright = count(g.right);
     if (cleft <= cright) place(g.left, g);
     else place(g.right, g);
   }
 }
Beispiel #6
0
 private void tryMutate(Genotype g) {
   if (Random.getRandom(1, 100) <= TBedford_Robot.mutationChance) g.token = new Token(true);
   if (g.left != null) tryMutate(g.left);
   if (g.right != null) tryMutate(g.right);
 }