Exemple #1
0
 /** Initializes map of genotypes to probabilities */
 private void initGeneMap() {
   int size = genotype.getGeneList().size();
   for (int i = 0; i < size; i++) {
     geneProbMap.put(i, 1);
   }
   geneProbSum = size;
 }
Exemple #2
0
  /**
   * Clones the genotype passed in and mutates it in one of three ways. The step size as well as the
   * probabilities increase.
   *
   * @return Returns mutated genotype.
   */
  public Genotype mutate() {
    int newVal;
    Genotype clone = genotype.cloneForMutation();

    Mutation cloneMut = new Mutation(clone);

    // Initializes trees
    cloneMut.initWith(this);

    Type mutationType = randType(); // chooses random mutation type
    // int mutationGeneIndex = randGeneIndex(); //chooses random index
    int mutationGeneIndex = rand.nextInt(clone.getGeneList().size());
    float stepSize = stepSize();
    Vector3f mutationVector = randVector().normalize().mult(stepSize);

    switch (mutationType) {
      case REMOVE_BLOCK:
        // Filter illegal removals
        //  if only 2 or less genes left
        //  if trying to remove the root gene
        if (clone.getNumberOfGenes() > 2 && mutationGeneIndex != 0) {
          System.out.println("REMOVE_BLOCK");
          System.out.println("mutationGeneIndex: " + mutationGeneIndex);
          clone.removeGeneByIndex(mutationGeneIndex);

          // Creates probability that this is chosen again
          newVal = cloneMut.typeProbMap.get(Type.REMOVE_BLOCK) + 1;
          cloneMut.typeProbMap.put(Type.REMOVE_BLOCK, newVal);
        }
        break;
      case ADD_BLOCK:
        System.out.println("ADD_BLOCK");
        clone.addChildBlock(mutationGeneIndex);

        // updates probability that this is chosen again
        newVal = cloneMut.typeProbMap.get(Type.ADD_BLOCK) + 1;
        cloneMut.typeProbMap.put(Type.ADD_BLOCK, newVal);
        break;
      case MODIFY_BLOCK:
        System.out.println("MODIFY_BLOCK");
        clone.mutateGeneByIndex(mutationGeneIndex, mutationVector);

        // updates probability that this is chosen again
        newVal = cloneMut.typeProbMap.get(Type.MODIFY_BLOCK) + 1;
        cloneMut.typeProbMap.put(Type.MODIFY_BLOCK, newVal);

        // Uses definition of step size in spec
        if (stepSize < maxStepSize / 2) {
          cloneMut.maxStepSize *= STEP_ADJUST_RATE; // arbitrary value
        } else {
          cloneMut.maxStepSize /= STEP_ADJUST_RATE;
        }

        // Decides whether modifying the block will make it bigger or smaller
        cloneMut.xMean += (mutationVector.x - xMean) / 2;
        cloneMut.yMean += (mutationVector.y - yMean) / 2;
        cloneMut.zMean += (mutationVector.z - zMean) / 2;

        break;
    }

    clone.setMutation(cloneMut);
    return clone;
  }