Package org.encog.neural.neat.training

Examples of org.encog.neural.neat.training.NEATGenome


   */
  @Override
  public void performOperation(final Random rnd, final Genome[] parents,
      final int parentIndex, final Genome[] offspring,
      final int offspringIndex) {
    final NEATGenome target = obtainGenome(parents, parentIndex, offspring,
        offspringIndex);
    int countTrysToFindOldLink = getOwner().getMaxTries();

    final NEATPopulation pop = ((NEATPopulation) target.getPopulation());

    // the link to split
    NEATLinkGene splitLink = null;

    final int sizeBias = ((NEATGenome)parents[0]).getInputCount()
        + ((NEATGenome)parents[0]).getOutputCount() + 10;

    // if there are not at least
    int upperLimit;
    if (target.getLinksChromosome().size() < sizeBias) {
      upperLimit = target.getNumGenes() - 1
          - (int) Math.sqrt(target.getNumGenes());
    } else {
      upperLimit = target.getNumGenes() - 1;
    }

    while ((countTrysToFindOldLink--) > 0) {
      // choose a link, use the square root to prefer the older links
      final int i = RangeRandomizer.randomInt(0, upperLimit);
      final NEATLinkGene link = target.getLinksChromosome().get(i);

      // get the from neuron
      final long fromNeuron = link.getFromNeuronID();

      if ((link.isEnabled())
          && (target.getNeuronsChromosome()
              .get(getElementPos(target, fromNeuron))
              .getNeuronType() != NEATNeuronType.Bias)) {
        splitLink = link;
        break;
      }
    }

    if (splitLink == null) {
      return;
    }

    splitLink.setEnabled(false);

    final long from = splitLink.getFromNeuronID();
    final long to = splitLink.getToNeuronID();

    final NEATInnovation innovation = ((NEATPopulation)getOwner().getPopulation()).getInnovations()
        .findInnovationSplit(from, to);

    // add the splitting neuron
    final ActivationFunction af = ((NEATPopulation)getOwner().getPopulation())
        .getActivationFunctions().pick(new Random());

    target.getNeuronsChromosome().add(
        new NEATNeuronGene(NEATNeuronType.Hidden, af, innovation
            .getNeuronID(), innovation.getInnovationID()));

    // add the other two sides of the link
    createLink(target, from, innovation.getNeuronID(),
        splitLink.getWeight());
    createLink(target, innovation.getNeuronID(), to, pop.getWeightRange());
   
    target.sortGenes();
  }
View Full Code Here


   */
  @Override
  public void performOperation(final Random rnd, final Genome[] parents,
      final int parentIndex, final Genome[] offspring,
      final int offspringIndex) {
    final NEATGenome target = obtainGenome(parents, parentIndex, offspring,
        offspringIndex);
    final double weightRange = ((NEATPopulation)getOwner().getPopulation()).getWeightRange();
    final List<NEATLinkGene> list = this.linkSelection.selectLinks(rnd,
        target);
    for (final NEATLinkGene gene : list) {
View Full Code Here

  @Override
  public void performOperation(Random rnd, Genome[] parents, int parentIndex,
      Genome[] offspring, int offspringIndex) {
    int countTrysToAddLink = this.getOwner().getMaxTries();

    NEATGenome target = this.obtainGenome(parents, parentIndex, offspring,
        offspringIndex);

    // the link will be between these two neurons
    long neuron1ID = -1;
    long neuron2ID = -1;

    // try to add a link
    while ((countTrysToAddLink--) > 0) {
      final NEATNeuronGene neuron1 = chooseRandomNeuron(target, true);
      final NEATNeuronGene neuron2 = chooseRandomNeuron(target, false);

      if (neuron1 == null || neuron2 == null) {
        return;
      }

      // do not duplicate
      // do not go to a bias neuron
      // do not go from an output neuron
      // do not go to an input neuron
      if (!isDuplicateLink(target, neuron1.getId(), neuron2.getId())
          && (neuron2.getNeuronType() != NEATNeuronType.Bias)
          && (neuron2.getNeuronType() != NEATNeuronType.Input)) {

        if ( ((NEATPopulation)getOwner().getPopulation()).getActivationCycles() != 1
            || neuron1.getNeuronType() != NEATNeuronType.Output) {
          neuron1ID = neuron1.getId();
          neuron2ID = neuron2.getId();
          break;
        }
      }
    }

    // did we fail to find a link
    if ((neuron1ID < 0) || (neuron2ID < 0)) {
      return;
    }

    double r = ((NEATPopulation) target.getPopulation()).getWeightRange();
    createLink(target, neuron1ID, neuron2ID,
        RangeRandomizer.randomize(rnd, -r, r));
    target.sortGenes();
  }
View Full Code Here

  @Override
  public void performOperation(final Random rnd, final Genome[] parents,
      final int parentIndex, final Genome[] offspring,
      final int offspringIndex) {

    final NEATGenome mom = (NEATGenome) parents[parentIndex + 0];
    final NEATGenome dad = (NEATGenome) parents[parentIndex + 1];

    final NEATGenome best = favorParent(rnd, mom, dad);
    final NEATGenome notBest = (best != mom) ? mom : dad;

    final List<NEATLinkGene> selectedLinks = new ArrayList<NEATLinkGene>();
    final List<NEATNeuronGene> selectedNeurons = new ArrayList<NEATNeuronGene>();

    int curMom = 0; // current gene index from mom
    int curDad = 0; // current gene index from dad
    NEATLinkGene selectedGene = null;

    // add in the input and bias, they should always be here
    final int alwaysCount = ((NEATGenome)parents[0]).getInputCount()
        + ((NEATGenome)parents[0]).getOutputCount() + 1;
    for (int i = 0; i < alwaysCount; i++) {
      addNeuronID(i, selectedNeurons, best, notBest);
    }

    while ((curMom < mom.getNumGenes()) || (curDad < dad.getNumGenes())) {
      NEATLinkGene momGene = null; // the mom gene object
      NEATLinkGene dadGene = null; // the dad gene object
      long momInnovation = -1;
      long dadInnovation = -1;

      // grab the actual objects from mom and dad for the specified
      // indexes
      // if there are none, then null
      if (curMom < mom.getNumGenes()) {
        momGene = mom.getLinksChromosome().get(curMom);
        momInnovation = momGene.getInnovationId();
      }

      if (curDad < dad.getNumGenes()) {
        dadGene = dad.getLinksChromosome().get(curDad);
        dadInnovation = dadGene.getInnovationId();
      }

      // now select a gene for mom or dad. This gene is for the baby
      if ((momGene == null) && (dadGene != null)) {
        if (best == dad) {
          selectedGene = dadGene;
        }
        curDad++;
      } else if ((dadGene == null) && (momGene != null)) {
        if (best == mom) {
          selectedGene = momGene;
        }
        curMom++;
      } else if (momInnovation < dadInnovation) {
        if (best == mom) {
          selectedGene = momGene;
        }
        curMom++;
      } else if (dadInnovation < momInnovation) {
        if (best == dad) {
          selectedGene = dadGene;
        }
        curDad++;
      } else if (dadInnovation == momInnovation) {
        if (Math.random() < 0.5f) {
          selectedGene = momGene;
        }

        else {
          selectedGene = dadGene;
        }
        curMom++;
        curDad++;
      }

      if (selectedGene != null) {
        if (selectedLinks.size() == 0) {
          selectedLinks.add(selectedGene);
        } else {
          if (selectedLinks.get(selectedLinks.size() - 1)
              .getInnovationId() != selectedGene
              .getInnovationId()) {
            selectedLinks.add(selectedGene);
          }
        }

        // Check if we already have the nodes referred to in
        // SelectedGene.
        // If not, they need to be added.
        addNeuronID(selectedGene.getFromNeuronID(), selectedNeurons,
            best, notBest);
        addNeuronID(selectedGene.getToNeuronID(), selectedNeurons,
            best, notBest);
      }

    }

    // now create the required nodes. First sort them into order
    Collections.sort(selectedNeurons);

    // finally, create the genome
    final NEATGenomeFactory factory = (NEATGenomeFactory) this.owner
        .getPopulation().getGenomeFactory();
    final NEATGenome babyGenome = factory.factor(selectedNeurons,
        selectedLinks, mom.getInputCount(), mom.getOutputCount());
    babyGenome.setBirthGeneration(this.owner.getIteration());
    babyGenome.setPopulation(this.owner.getPopulation());
    babyGenome.sortGenes();

    offspring[offspringIndex] = babyGenome;
  }
View Full Code Here

          nextInnovationID = Math.max(nextInnovationID,
              innovationID + 1);
        }
      } else if (section.getSectionName().equals("NEAT-POPULATION")
          && section.getSubSectionName().equals("SPECIES")) {
        NEATGenome lastGenome = null;
        BasicSpecies lastSpecies = null;

        for (final String line : section.getLines()) {
          final List<String> cols = EncogFileSection
              .splitColumns(line);

          if (cols.get(0).equalsIgnoreCase("s")) {
            lastSpecies = new BasicSpecies();
            lastSpecies.setPopulation(result);
            lastSpecies.setAge(Integer.parseInt(cols.get(1)));
            lastSpecies.setBestScore(CSVFormat.EG_FORMAT.parse(cols
                .get(2)));
            lastSpecies.setGensNoImprovement(Integer.parseInt(cols
                .get(3)));
            result.getSpecies().add(lastSpecies);
          } else if (cols.get(0).equalsIgnoreCase("g")) {
            final boolean isLeader = lastGenome == null;
            lastGenome = new NEATGenome();
            lastGenome.setInputCount(result.getInputCount());
            lastGenome.setOutputCount(result.getOutputCount());
            lastGenome.setSpecies(lastSpecies);
            lastGenome.setAdjustedScore(CSVFormat.EG_FORMAT
                .parse(cols.get(1)));
            lastGenome.setScore(CSVFormat.EG_FORMAT.parse(cols
                .get(2)));
            lastGenome.setBirthGeneration(Integer.parseInt(cols
                .get(3)));
            lastSpecies.add(lastGenome);
            if (isLeader) {
              lastSpecies.setLeader(lastGenome);
            }
          } else if (cols.get(0).equalsIgnoreCase("n")) {
            final NEATNeuronGene neuronGene = new NEATNeuronGene();
            final int geneID = Integer.parseInt(cols.get(1));
            neuronGene.setId(geneID);

            final ActivationFunction af = EncogFileSection
                .parseActivationFunction(cols.get(2));
            neuronGene.setActivationFunction(af);

            neuronGene.setNeuronType(PersistNEATPopulation
                .stringToNeuronType(cols.get(3)));
            neuronGene
                .setInnovationId(Integer.parseInt(cols.get(4)));
            lastGenome.getNeuronsChromosome().add(neuronGene);
            nextGeneID = Math.max(geneID + 1, nextGeneID);
          } else if (cols.get(0).equalsIgnoreCase("l")) {
            final NEATLinkGene linkGene = new NEATLinkGene();
            linkGene.setId(Integer.parseInt(cols.get(1)));
            linkGene.setEnabled(Integer.parseInt(cols.get(2)) > 0);
            linkGene.setFromNeuronID(Integer.parseInt(cols.get(3)));
            linkGene.setToNeuronID(Integer.parseInt(cols.get(4)));
            linkGene.setWeight(CSVFormat.EG_FORMAT.parse(cols
                .get(5)));
            linkGene.setInnovationId(Integer.parseInt(cols.get(6)));
            lastGenome.getLinksChromosome().add(linkGene);
          }
        }

      } else if (section.getSectionName().equals("NEAT-POPULATION")
          && section.getSubSectionName().equals("CONFIG")) {
View Full Code Here

  @Override
  public void performOperation(final Random rnd, final Genome[] parents,
      final int parentIndex, final Genome[] offspring,
      final int offspringIndex) {

    final NEATGenome target = obtainGenome(parents, parentIndex, offspring,
        offspringIndex);

    if (target.getLinksChromosome().size() < NEATMutateRemoveLink.MIN_LINK) {
      // don't remove from small genomes
      return;
    }

    // determine the target and remove
    final int index = RangeRandomizer.randomInt(0, target
        .getLinksChromosome().size() - 1);
    final NEATLinkGene targetGene = target.getLinksChromosome().get(index);
    target.getLinksChromosome().remove(index);

    // if this orphaned any nodes, then kill them too!
    if (!isNeuronNeeded(target, targetGene.getFromNeuronID())) {
      removeNeuron(target, targetGene.getFromNeuronID());
    }
View Full Code Here

    double numDisjoint = 0;
    double numExcess = 0;
    double numMatched = 0;
    double weightDifference = 0;

    final NEATGenome genome1 = (NEATGenome)gen1;
    final NEATGenome genome2 = (NEATGenome)gen2;
   
    final int genome1Size = genome1.getLinksChromosome().size();
    final int genome2Size = genome2.getLinksChromosome().size();
    final int n = 1;// Math.max(genome1Size, genome2Size);

    int g1 = 0;
    int g2 = 0;

    while ((g1 < genome1Size) || (g2 < genome2Size)) {

      if (g1 == genome1Size) {
        g2++;
        numExcess++;
        continue;
      }

      if (g2 == genome2Size) {
        g1++;
        numExcess++;
        continue;
      }

      // get innovation numbers for each gene at this point
      final long id1 = genome1.getLinksChromosome().get(g1)
          .getInnovationId();
      final long id2 = genome2.getLinksChromosome().get(g2)
          .getInnovationId();

      // innovation numbers are identical so increase the matched score
      if (id1 == id2) {

        // get the weight difference between these two genes
        weightDifference += Math.abs(genome1.getLinksChromosome()
            .get(g1).getWeight()
            - genome2.getLinksChromosome().get(g2).getWeight());
        g1++;
        g2++;
        numMatched++;
      }
View Full Code Here

    out.addColumn(species.getBestScore());
    out.addColumn(species.getGensNoImprovement());
    out.writeLine();

    for (final Genome genome : species.getMembers()) {
      final NEATGenome neatGenome = (NEATGenome) genome;
      out.addColumn("g");
      out.addColumn(neatGenome.getAdjustedScore());
      out.addColumn(neatGenome.getScore());
      out.addColumn(neatGenome.getBirthGeneration());
      out.writeLine();

      for (final NEATNeuronGene neatNeuronGene : neatGenome
          .getNeuronsChromosome()) {
        out.addColumn("n");
        out.addColumn(neatNeuronGene.getId());
        out.addColumn(neatNeuronGene.getActivationFunction());
        out.addColumn(PersistNEATPopulation
            .neuronTypeToString(neatNeuronGene.getNeuronType()));
        out.addColumn(neatNeuronGene.getInnovationId());
        out.writeLine();
      }
      for (final NEATLinkGene neatLinkGene : neatGenome
          .getLinksChromosome()) {
        out.addColumn("l");
        out.addColumn(neatLinkGene.getId());
        out.addColumn(neatLinkGene.isEnabled());
        out.addColumn(neatLinkGene.getFromNeuronID());
View Full Code Here

    NEATPopulation pop = new NEATPopulation(2,1,100);
    pop.reset();
    CalculateScore score = new TrainingSetScore(trainingSet);
    final EvolutionaryAlgorithm train = NEATUtil.constructNEATTrainer(pop,score);
       
    NEATGenome genome1 = new NEATGenome();
    genome1.setAdjustedScore(3.0);
    NEATGenome genome2 = new NEATGenome();
    genome2.setAdjustedScore(2.0);
    NEATGenome genome3 = new NEATGenome();
    genome3.setAdjustedScore(1.0);
   
    List<NEATGenome> list = new ArrayList<NEATGenome>();
    list.add(genome1);
    list.add(genome2);
    list.add(genome3);
View Full Code Here

    NEATPopulation pop = new NEATPopulation(2,1,100);
    pop.reset();
    CalculateScore score = new TrainingSetScore(trainingSet);
    final EvolutionaryAlgorithm train = NEATUtil.constructNEATTrainer(pop,score);
       
    NEATGenome genome1 = new NEATGenome();
    genome1.setAdjustedScore(3.0);
    NEATGenome genome2 = new NEATGenome();
    genome2.setAdjustedScore(2.0);
    genome2.setBirthGeneration(200);
    NEATGenome genome3 = new NEATGenome();
    genome3.setAdjustedScore(2.0);
    genome3.setBirthGeneration(100);
   
    List<NEATGenome> list = new ArrayList<NEATGenome>();
    list.add(genome1);
    list.add(genome2);
    list.add(genome3);
View Full Code Here

TOP

Related Classes of org.encog.neural.neat.training.NEATGenome

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.