Package org.encog.ml.ea.species

Examples of org.encog.ml.ea.species.Species


    final EncogReadHelper in = new EncogReadHelper(is);
    EncogFileSection section;

    int count = 0;
    Species lastSpecies = null;
    while ((section = in.readNextSection()) != null) {
      if (section.getSectionName().equals("BASIC")
          && section.getSubSectionName().equals("PARAMS")) {
        final Map<String, String> params = section.parseParams();
        result.getProperties().putAll(params);
      } else if (section.getSectionName().equals("BASIC")
          && section.getSubSectionName().equals("EPL-POPULATION")) {
        for (final String line : section.getLines()) {
          final List<String> cols = EncogFileSection
              .splitColumns(line);

          if (cols.get(0).equalsIgnoreCase("s")) {
            lastSpecies = new BasicSpecies();
            lastSpecies.setAge(Integer.parseInt(cols.get(1)));
            lastSpecies.setBestScore(CSVFormat.EG_FORMAT.parse(cols
                .get(2)));
            lastSpecies.setPopulation(result);
            lastSpecies.setGensNoImprovement(Integer.parseInt(cols
                .get(3)));
            result.getSpecies().add(lastSpecies);
          } else if (cols.get(0).equalsIgnoreCase("p")) {
            double score = 0;
            double adjustedScore = 0;

            if (cols.get(1).equalsIgnoreCase("nan")
                || cols.get(2).equalsIgnoreCase("nan")) {
              score = Double.NaN;
              adjustedScore = Double.NaN;
            } else {
              score = CSVFormat.EG_FORMAT.parse(cols.get(1));
              adjustedScore = CSVFormat.EG_FORMAT.parse(cols
                  .get(2));
            }

            final String code = cols.get(3);
            final EncogProgram prg = new EncogProgram(context);
            prg.compileEPL(code);
            prg.setScore(score);
            prg.setSpecies(lastSpecies);
            prg.setAdjustedScore(adjustedScore);
            if (lastSpecies == null) {
              throw new EncogError(
                  "Have not defined a species yet");
            } else {
              lastSpecies.add(prg);
            }
            count++;
          }
        }
      } else if (section.getSectionName().equals("BASIC")
          && section.getSubSectionName().equals("EPL-OPCODES")) {
        for (final String line : section.getLines()) {
          final List<String> cols = EncogFileSection
              .splitColumns(line);
          final String name = cols.get(0);
          final int args = Integer.parseInt(cols.get(1));
          result.getContext().getFunctions().addExtension(name, args);
        }
      } else if (section.getSectionName().equals("BASIC")
          && section.getSubSectionName().equals("EPL-SYMBOLIC")) {
        boolean first = true;
        for (final String line : section.getLines()) {
          if (!first) {
            final List<String> cols = EncogFileSection
                .splitColumns(line);
            final String name = cols.get(0);
            final String t = cols.get(1);
            ValueType vt = null;

            if (t.equalsIgnoreCase("f")) {
              vt = ValueType.floatingType;
            } else if (t.equalsIgnoreCase("b")) {
              vt = ValueType.booleanType;
            } else if (t.equalsIgnoreCase("i")) {
              vt = ValueType.intType;
            } else if (t.equalsIgnoreCase("s")) {
              vt = ValueType.stringType;
            } else if (t.equalsIgnoreCase("e")) {
              vt = ValueType.enumType;
            }

            final int enumType = Integer.parseInt(cols.get(2));
            final int enumCount = Integer.parseInt(cols.get(3));
            final VariableMapping mapping = new VariableMapping(
                name, vt, enumType, enumCount);
            if (mapping.getName().length() > 0) {
              result.getContext().defineVariable(mapping);
            } else {
              result.getContext().setResult(mapping);
            }
          } else {
            first = false;
          }
        }
      }
    }
    result.setPopulationSize(count);

    // set the best genome, should be the first genome in the first species
    if (result.getSpecies().size() > 0) {
      final Species species = result.getSpecies().get(0);
      if (species.getMembers().size() > 0) {
        result.setBestGenome(species.getMembers().get(0));
      }

      // set the leaders
      for (final Species sp : result.getSpecies()) {
        if (sp.getMembers().size() > 0) {
View Full Code Here


   *            The program to add.
   */
  public void addPopulationMember(final PrgPopulation population,
      final EncogProgram prg) {
    synchronized (this) {
      final Species defaultSpecies = population.getSpecies().get(0);
      prg.setSpecies(defaultSpecies);
      defaultSpecies.add(prg);
      this.contents.add(prg.dumpAsCommonExpression());
    }
  }
View Full Code Here

  @Override
  public void generate(final Random rnd, final Population pop) {
    // prepare population
    this.contents.clear();
    pop.getSpecies().clear();
    final Species defaultSpecies = pop.createSpecies();

    // determine thread usage
    if (this.score.requireSingleThreaded()) {
      this.actualThreads = 1;
    } else if (this.threads == 0) {
      this.actualThreads = Runtime.getRuntime().availableProcessors();
    } else {
      this.actualThreads = this.threads;
    }

    // start up
    ExecutorService taskExecutor = null;

    if (this.threads == 1) {
      taskExecutor = Executors.newSingleThreadScheduledExecutor();
    } else {
      taskExecutor = Executors.newFixedThreadPool(this.actualThreads);
    }

    for (int i = 0; i < pop.getPopulationSize(); i++) {
      taskExecutor.execute(new GenerateWorker(this, (PrgPopulation) pop));
    }

    taskExecutor.shutdown();
    try {
      taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
    } catch (final InterruptedException e) {
      throw new GeneticError(e);
    }

    // just pick a leader, for the default species.
    defaultSpecies.setLeader(defaultSpecies.getMembers().get(0));
  }
View Full Code Here

    result.getInnovationIDGenerate().setCurrentID(nextInnovationID);
    result.getGeneIDGenerate().setCurrentID(nextGeneID);

    // find first genome, which should be the best genome
    if (result.getSpecies().size() > 0) {
      final Species species = result.getSpecies().get(0);
      if (species.getMembers().size() > 0) {
        result.setBestGenome(species.getMembers().get(0));
      }
    }

    return result;
  }
View Full Code Here

    }

    out.addSubSection("SPECIES");

    // make sure the best species goes first
    final Species bestSpecies = pop.determineBestSpecies();
    if (bestSpecies != null) {
      saveSpecies(out, bestSpecies);
    }

    // now write the other species, other than the best one
View Full Code Here

    EncogProgram prg1 = new EncogProgram(context);
    EncogProgram prg2 = new EncogProgram(context);
    prg1.compileExpression("x+1");
    prg2.compileExpression("(x+5)/2");
   
    Species defaultSpecies = pop.createSpecies();
    defaultSpecies.add(prg1);
    defaultSpecies.add(prg2);
    return pop;
  }
View Full Code Here

  /**
   * {@inheritDoc}
   */
  @Override
  public Species createSpecies() {
    final Species species = new BasicSpecies();
    species.setPopulation(this);
    getSpecies().add(species);
    return species;
  }
View Full Code Here

   */
  public void purgeInvalidGenomes() {
    // remove any invalid genomes
    int speciesNum = 0;
    while (speciesNum < getSpecies().size()) {
      Species species = getSpecies().get(speciesNum);

      int genomeNum = 0;
      while (genomeNum < species.getMembers().size()) {
        Genome genome = species.getMembers().get(genomeNum);
        if (Double.isInfinite(genome.getScore())
            || Double.isInfinite(genome.getAdjustedScore())
            || Double.isNaN(genome.getScore())
            || Double.isNaN(genome.getAdjustedScore())) {
          species.getMembers().remove(genome);
        } else {
          genomeNum++;
        }
      }
     
      // is the species now empty?
      if (species.getMembers().size() == 0) {
        getSpecies().remove(species);
      } else {
        // new leader needed?
        if( !species.getMembers().contains(species.getLeader()) ) {
          species.setLeader(species.getMembers().get(0));
          species.setBestScore(species.getLeader().getScore());
        }
       
        // onto the next one!
        speciesNum++;
      }
View Full Code Here

      final CalculateScore calculateScore, final int populationSize) {
    super(TrainingImplementationType.Iterative);

    // Create the population
    final Population population = new BasicPopulation(populationSize, null);
    final Species defaultSpecies = population.createSpecies();

    for (int i = 0; i < population.getPopulationSize(); i++) {
      final MLEncodable chromosomeNetwork = (MLEncodable) phenotypeFactory
          .factor();
      final MLMethodGenome genome = new MLMethodGenome(chromosomeNetwork);
      defaultSpecies.add(genome);
    }
    defaultSpecies.setLeader(defaultSpecies.getMembers().get(0));
   
    population.setGenomeFactory(new MLMethodGenomeFactory(phenotypeFactory,
        population));
   
    // create the trainer
    this.genetic = new MLMethodGeneticAlgorithmHelper(population,
        calculateScore);
    this.genetic.setCODEC(new MLEncodableCODEC());

    GenomeComparator comp = null;
    if (calculateScore.shouldMinimize()) {
      comp = new MinimizeScoreComp();
    } else {
      comp = new MaximizeScoreComp();
    }
    this.genetic.setBestComparator(comp);
    this.genetic.setSelectionComparator(comp);

   
    // create the operators
    final int s = Math
        .max(defaultSpecies.getMembers().get(0).size() / 5, 1);
    getGenetic().setPopulation(population);

    this.genetic.addOperation(0.9, new Splice(s));
    this.genetic.addOperation(0.1, new MutatePerturb(1.0));
  }
View Full Code Here

TOP

Related Classes of org.encog.ml.ea.species.Species

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.