org.encog.ml.prg

## Class EncogProgram

- java.lang.Object
- - org.encog.ml.ea.genome.BasicGenome
  - - org.encog.ml.prg.EncogProgram

- All Implemented Interfaces:
    Serializable, Genome, MLError, MLInput, MLInputOutput, MLMethod, MLOutput, MLRegression

      public class EncogProgram
      extends BasicGenome
      implements MLRegression, MLError

  Holds an Encog Programming Language (EPL) program. A Encog program is internally represented as a tree structure. It can be rendered to a variety of forms, such as RPN, common infix expressions, or Latex. The Encog Workbench also allows display as a graphical tree. An Encog Program is both a genome and phenome. No decoding is necessary. Every Encog Program has a context. The context is the same for every Encog Program in a population. The context defines which opcodes should be used, as well as the defined variables. The actual values for the variables are not stored in the context. Rather they are stored in a variable holder. Each program usually has its own variable holder, though it is possible to share.
  See Also:
    Serialized Form

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `EncogProgram()` Construct the Encog program and create a default context and variable holder.
    `EncogProgram(EncogProgramContext theContext)` Construct the Encog program with the specified context, but create a new variable holder.
    `EncogProgram(EncogProgramContext theContext, EncogProgramVariables theVariables)` Construct an Encog program using the specified context and variable holder.
    `EncogProgram(String expression)` Construct an Encog program using the specified expression, but create an empty context and variable holder.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `double` | `calculateError(MLDataSet data)` Calculate the error of the ML method, given a dataset.
    `ProgramNode` | `compileEPL(String code)` Compile the specified EPL into an actual program node structure, for later execution.
    `ProgramNode` | `compileExpression(String expression)` Compile the specified expression.
    `MLData` | `compute(MLData input)` Compute the output from the input MLData.
    `void` | `copy(Genome source)` Copy from the specified genome into this one.
    `String` | `dumpAsCommonExpression()`
    `ExpressionValue` | `evaluate()` Execute the program and return the result.
    `ProgramNode` | `findNode(int index)` Find the specified node by index.
    `String` | `generateEPL()`
    `EncogProgramContext` | `getContext()`
    `Object` | `getExtraData(String name)` Get extra data that might be needed by user extended opcodes.
    `FunctionFactory` | `getFunctions()`
    `int` | `getInputCount()`
    `int` | `getOutputCount()`
    `ValueType` | `getReturnType()`
    `ProgramNode` | `getRootNode()`
    `EncogProgramVariables` | `getVariables()`
    `static boolean` | `parseBoolean(String str)` Parse the specified program, or expression, and return the result.
    `static ExpressionValue` | `parseExpression(String str)` Parse the specified program, or expression, and return the result.
    `static double` | `parseFloat(String str)` Parse the specified program, or expression, and return the result.
    `static String` | `parseString(String str)` Parse the specified program, or expression, and return the result.
    `void` | `replaceNode(ProgramNode replaceThisNode, ProgramNode replaceWith)` Replace the specified node with another.
    `int` | `selectRandomVariable(Random rnd, List<ValueType> desiredTypes)` Select a random variable from the defined variables.
    `void` | `setExtraData(String name, Object value)` Set extra data that might be needed by extensions.
    `void` | `setRootNode(ProgramNode theRootNode)` Set the root node for the program.
    `int` | `size()`
    `String` | `toString()`

    - ### Methods inherited from class org.encog.ml.ea.genome.BasicGenome

      `getAdjustedScore, getBirthGeneration, getPopulation, getScore, getSpecies, setAdjustedScore, setBirthGeneration, setPopulation, setScore, setSpecies`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### EncogProgram

          public EncogProgram()

      Construct the Encog program and create a default context and variable holder. Use all available opcodes.

    - #### EncogProgram

          public EncogProgram(EncogProgramContext theContext)

      Construct the Encog program with the specified context, but create a new variable holder.
      Parameters:
        `theContext` - The context.

    - #### EncogProgram

          public EncogProgram(EncogProgramContext theContext,
                      EncogProgramVariables theVariables)

      Construct an Encog program using the specified context and variable holder.
      Parameters:
        `theContext` - The context.
        `theVariables` - The variable holder.

    - #### EncogProgram

          public EncogProgram(String expression)

      Construct an Encog program using the specified expression, but create an empty context and variable holder.
      Parameters:
        `expression` - The expression.

  - ### Method Detail

    - #### parseBoolean

          public static boolean parseBoolean(String str)

      Parse the specified program, or expression, and return the result. No variables can be defined for this as a default context is used. The result is returned as a boolean.
      Parameters:
        `str` - The program expression.
      Returns:
        The value the expression was evaluated to.

    - #### parseExpression

          public static ExpressionValue parseExpression(String str)

      Parse the specified program, or expression, and return the result. No variables can be defined for this as a default context is used. The result is returned as a boolean.
      Parameters:
        `str` - The program expression.
      Returns:
        The value the expression was evaluated to.

    - #### parseFloat

          public static double parseFloat(String str)

      Parse the specified program, or expression, and return the result. No variables can be defined for this as a default context is used. The result is returned as a float.
      Parameters:
        `str` - The program expression value.
      Returns:
        The value the expression was evaluated to.

    - #### parseString

          public static String parseString(String str)

      Parse the specified program, or expression, and return the result. No variables can be defined for this as a default context is used. The result is returned as a string.
      Parameters:
        `str` - The program expression value.
      Returns:
        The value the expression was evaluated to.

    - #### calculateError

          public double calculateError(MLDataSet data)

      Calculate the error of the ML method, given a dataset.
      Specified by:
        `calculateError` in interface `MLError`
      Parameters:
        `data` - The dataset.
      Returns:
        The error.

    - #### compileEPL

          public ProgramNode compileEPL(String code)

      Compile the specified EPL into an actual program node structure, for later execution.
      Parameters:
        `code` - The code to compile.
      Returns:
        The root node.

    - #### compileExpression

          public ProgramNode compileExpression(String expression)

      Compile the specified expression.
      Parameters:
        `expression` - The expression.
      Returns:
        The program node that this was compiled into.

    - #### compute

          public MLData compute(MLData input)

      Compute the output from the input MLData. The individual values of the input will be mapped to the variables defined in the context. The order is the same between the input and the defined variables. The input will be mapped to the appropriate types. Enums will use their ordinal number. The result will be a single number MLData.
      Specified by:
        `compute` in interface `MLRegression`
      Parameters:
        `input` - The input to the program.
      Returns:
        A single numer MLData.

    - #### copy

          public void copy(Genome source)

      Copy from the specified genome into this one.
      Specified by:
        `copy` in interface `Genome`
      Parameters:
        `source` - The source genome.

    - #### dumpAsCommonExpression

          public String dumpAsCommonExpression()

      Returns:
        The string as a common "infix" expression.

    - #### evaluate

          public ExpressionValue evaluate()

      Execute the program and return the result.
      Returns:
        The result of running the program.

    - #### findNode

          public ProgramNode findNode(int index)

      Find the specified node by index. The tree is traversed to do this. This is typically used to select a random node.
      Parameters:
        `index` - The index being sought.
      Returns:
        The program node found.

    - #### generateEPL

          public String generateEPL()

      Returns:
        The string as an EPL expression. EPL is the format that EncogPrograms are usually persisted as.

    - #### getContext

          public EncogProgramContext getContext()

      Returns:
        The program context. The program context may be shared over multiple programs.

    - #### getFunctions

          public FunctionFactory getFunctions()

      Returns:
        The function factory from the context.

    - #### getInputCount

          public int getInputCount()

      Specified by:
        `getInputCount` in interface `MLInput`
      Returns:
        The input.

    - #### getOutputCount

          public int getOutputCount()

      Specified by:
        `getOutputCount` in interface `MLOutput`
      Returns:
        The output count.

    - #### getReturnType

          public ValueType getReturnType()

      Returns:
        The return type, from the context.

    - #### getRootNode

          public ProgramNode getRootNode()

      Returns:
        The root node of the program.

    - #### getVariables

          public EncogProgramVariables getVariables()

      Returns:
        The variable holder.

    - #### replaceNode

          public void replaceNode(ProgramNode replaceThisNode,
                         ProgramNode replaceWith)

      Replace the specified node with another.
      Parameters:
        `replaceThisNode` - The node to replace.
        `replaceWith` - The node that is replacing that node.

    - #### selectRandomVariable

          public int selectRandomVariable(Random rnd,
                                 List<ValueType> desiredTypes)

      Select a random variable from the defined variables.
      Parameters:
        `rnd` - A random number generator.
        `desiredTypes` - The desired types that the variable can be.
      Returns:
        The index of the defined variable, or -1 if unable to define.

    - #### setRootNode

          public void setRootNode(ProgramNode theRootNode)

      Set the root node for the program.
      Parameters:
        `theRootNode` - The new root node.

    - #### size

          public int size()

      Specified by:
        `size` in interface `Genome`
      Returns:
        Return the size of this genome. This size is a relative number that indicates the complexity of the genome.

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `BasicGenome`

    - #### getExtraData

          public Object getExtraData(String name)

      Get extra data that might be needed by user extended opcodes.
      Parameters:
        `name` - The name the data was stored under.
      Returns:
        The extra data.

    - #### setExtraData

          public void setExtraData(String name,
                          Object value)

      Set extra data that might be needed by extensions.
      Parameters:
        `name` - The name of the data stored.
        `value` - The data.

Copyright © 2014. All Rights Reserved.