Exemplo n.º 1
0
  private void assignNeuronDepth(Gene[] nodeGenes, int depth, Chromosome mutated) {
    int i;
    NEATNodeGene node;
    // ArrayList nodeGenes = this.findOutputNodes(this.candidateNodes(genes));

    for (i = 0; i < nodeGenes.length; i++) {
      node = (NEATNodeGene) nodeGenes[i];
      if (node.getType() == NEATNodeGene.OUTPUT) {
        if (depth == 1) {
          node.setDepth(depth);
          this.assignNeuronDepth(
              this.findSourceNodes(node.id(), mutated.genes()), depth + 1, mutated);
        }
      } else if (node.getType() == NEATNodeGene.HIDDEN) {
        if (node.getDepth() == 0) {
          // we have an unassigned depth
          node.setDepth(depth);
          this.assignNeuronDepth(
              this.findSourceNodes(node.id(), mutated.genes()), depth + 1, mutated);
        }
      } else if (node.getType() == NEATNodeGene.INPUT) {
        node.setDepth(Integer.MAX_VALUE);
      }
    }
  }
Exemplo n.º 2
0
  private Gene[] ensureLegalLinks(Gene[] genes) {
    ArrayList links;
    NEATLinkGene link;
    NEATNodeGene from;
    NEATNodeGene to;
    Gene[] newGenes = null;
    ArrayList tmpGenes = new ArrayList();
    int i;

    // only need to prune if recurrency not allowed
    if (!this.recurrencyAllowed) {
      // only return enabled links
      links = this.candidateLinks(genes, false);
      for (i = 0; i < genes.length; i++) {
        if (genes[i] instanceof NEATLinkGene) {
          link = (NEATLinkGene) genes[i];
          from = this.findNode(link.getFromId(), genes);
          to = this.findNode(link.getToId(), genes);
          if (from.getDepth() > to.getDepth()) {
            // not recurrent - so keep
            tmpGenes.add(genes[i]);
          }
        } else {
          tmpGenes.add(genes[i]);
        }
      }
      newGenes = new Gene[tmpGenes.size()];
      for (i = 0; i < newGenes.length; i++) {
        newGenes[i] = (Gene) tmpGenes.get(i);
      }
    } else {
      newGenes = genes;
    }

    return (newGenes);
  }