示例#1
0
  public void enableDisableMutate(Genome genome, boolean enable) {
    List<Gene> candidates = new ArrayList<Gene>();

    // find the genes that are not this enablestate
    for (Gene gene : genome.genes) {
      if (gene.isEnabled() != enable) {
        candidates.add(gene);
      }
    }

    if (candidates.isEmpty()) {
      return;
    }

    // flip the enablestate of a random candidate
    int randomIndex = rand.nextInt(candidates.size());
    Gene gene = candidates.get(randomIndex);
    gene.setEnabled(!gene.isEnabled());
  }
示例#2
0
  private Genome crossover(Genome g1, Genome g2) // splice two genomes together
      {
    // -- Make sure g1 is the higher fitness genome
    if (g2.fitness > g1.fitness) {
      Genome tempg = g1;
      g1 = g2;
      g2 = tempg;
    }

    Genome child = new Genome();

    HashMap<Integer, Gene> innovations2 = new HashMap<Integer, Gene>();

    for (int i = 0; i < g2.getGenes().size(); i++) {
      Gene gene = g2.getGenes().get(i);

      innovations2.put(gene.getInnovation(), gene);
    }

    for (int i = 0; i < g1.getGenes().size(); i++) {
      Gene gene1 = g1.getGenes().get(i);
      Gene gene2 = innovations2.get(gene1.getInnovation());

      if (gene2 != null && rand.nextBoolean() && gene2.isEnabled()) {
        child.getGenes().add(gene2.copy());
      } else {
        child.getGenes().add(gene1.copy());
      }
    }

    // set to the max
    if (g1.maxneuron > g2.maxneuron) {
      child.maxneuron = g1.maxneuron;
    } else {
      child.maxneuron = g2.maxneuron;
    }

    for (String key : g1.mutationRates.keySet()) // give the child the mutations of g1
    {
      child.mutationRates.put(key, g1.mutationRates.get(key));
    }

    return child;
  }
示例#3
0
  private void nodeMutate(Genome genome) {
    if (genome.genes.size() == 0) return;

    genome.maxneuron = genome.maxneuron + 1;

    int randomIndex = rand.nextInt(genome.genes.size());
    Gene gene = genome.genes.get(randomIndex);
    if (!gene.isEnabled()) return;

    gene.setEnabled(false);

    Gene gene1 = gene.copy();
    gene1.setOut(genome.maxneuron);
    gene1.setWeight(1.0f);
    gene1.innovation = newInnovation();
    gene1.setEnabled(true);
    genome.genes.add(gene1);

    Gene gene2 = gene.copy();
    gene2.setInto(genome.maxneuron);
    gene2.innovation = newInnovation();
    gene2.setEnabled(true);
    genome.genes.add(gene2);
  }