Package org.encog.ml.prg.train

Examples of org.encog.ml.prg.train.PrgPopulation


   */
  @Override
  public Object read(final InputStream is) {
    final EncogProgramContext context = new EncogProgramContext();

    final PrgPopulation result = new PrgPopulation(context, 0);

    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) {
          sp.setLeader(sp.getMembers().get(0));
        }
      }
    }
View Full Code Here


   * {@inheritDoc}
   */
  @Override
  public void save(final OutputStream os, final Object obj) {
    final EncogWriteHelper out = new EncogWriteHelper(os);
    final PrgPopulation pop = (PrgPopulation) obj;

    out.addSection("BASIC");
    out.addSubSection("PARAMS");
    out.addProperties(pop.getProperties());
    out.addSubSection("EPL-OPCODES");
    for (final ProgramExtensionTemplate temp : pop.getContext()
        .getFunctions().getOpCodes()) {
      out.addColumn(temp.getName());
      out.addColumn(temp.getChildNodeCount());
      out.writeLine();
    }
    out.addSubSection("EPL-SYMBOLIC");
    out.addColumn("name");
    out.addColumn("type");
    out.addColumn("enum");
    out.addColumn("enum_type");
    out.addColumn("enum_count");
    out.writeLine();

    // write the first line, the result
    out.addColumn("");
    out.addColumn(getType(pop.getContext().getResult()));
    out.addColumn(pop.getContext().getResult().getEnumType());
    out.addColumn(pop.getContext().getResult().getEnumValueCount());
    out.writeLine();

    // write the next lines, the variables
    for (final VariableMapping mapping : pop.getContext()
        .getDefinedVariables()) {
      out.addColumn(mapping.getName());
      out.addColumn(getType(mapping));
      out.addColumn(mapping.getEnumType());
      out.addColumn(mapping.getEnumValueCount());
      out.writeLine();
    }
    out.addSubSection("EPL-POPULATION");
    for (final Species species : pop.getSpecies()) {
      if (species.getMembers().size() > 0) {
        out.addColumn("s");
        out.addColumn(species.getAge());
        out.addColumn(species.getBestScore());
        out.addColumn(species.getGensNoImprovement());
View Full Code Here

    if( "numeric".equalsIgnoreCase(funct) ) {
      StandardExtensions.createNumericOperators(context);
    }
   
    PrgPopulation pop = new PrgPopulation(context,populationSize);
   
    if( context.getFunctions().size()>0 ) {
      (new RampedHalfAndHalf(context,2,6)).generate(new Random(), pop);
    }
    return pop;
View Full Code Here

   * @return The newly created trainer.
   */
  public MLTrain create(final MLMethod method,
      final MLDataSet training, final String argsStr) {
   
    PrgPopulation pop = (PrgPopulation)method;
   
    final CalculateScore score = new TrainingSetScore(training);   
    TrainEA train = new TrainEA(pop, score);
    train.getRules().addRewriteRule(new RewriteConstants());
    train.getRules().addRewriteRule(new RewriteAlgebraic());
    train.setCODEC(new PrgCODEC());
    train.addOperation(0.8, new SubtreeCrossover());
    train.addOperation(0.1, new SubtreeMutation(pop.getContext(),4));
    train.addOperation(0.1, new ConstMutation(pop.getContext(),0.5,1.0));
    train.addScoreAdjuster(new ComplexityAdjustedScore());
    train.setSpeciation(new PrgSpeciation());
    return train;
  }
View Full Code Here

public class TestRewriteAlgebraic extends TestCase {
 
  public void eval(String start, String expect) {
    EncogProgramContext context = new EncogProgramContext();
    StandardExtensions.createNumericOperators(context);
    PrgPopulation pop = new PrgPopulation(context,1);
    CalculateScore score = new ZeroEvalScoreFunction();

    TrainEA genetic = new TrainEA(pop, score);
    genetic.setValidationMode(true);
    genetic.setCODEC(new PrgCODEC());
View Full Code Here

  private PrgPopulation create()
  {
    EncogProgramContext context = new EncogProgramContext();
    context.defineVariable("x");
    StandardExtensions.createAll(context);
    PrgPopulation pop = new PrgPopulation(context,10);
    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

  }
 
  @Test
  public void testPersistEG()
  {
    PrgPopulation pop = create();
    EncogDirectoryPersistence.saveObject(EG_FILENAME, pop);
    PrgPopulation pop2 = (PrgPopulation)EncogDirectoryPersistence.loadObject(EG_FILENAME);
    validate(pop2);
  }
View Full Code Here

  }
 
  @Test
  public void testPersistSerial() throws IOException, ClassNotFoundException
  {
    PrgPopulation pop = create();
    validate(pop);
    SerializeObject.save(SERIAL_FILENAME, pop);
    PrgPopulation pop2 = (PrgPopulation)SerializeObject.load(SERIAL_FILENAME);
    validate(pop2);   
  }
View Full Code Here

TOP

Related Classes of org.encog.ml.prg.train.PrgPopulation

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.