org.encog.ml.prg
Class EncogProgram
- java.lang.Object
-
- org.encog.ml.ea.genome.BasicGenome
-
- org.encog.ml.prg.EncogProgram
-
- All Implemented Interfaces:
- java.io.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(EncogProgramContext theContext, java.lang.String theExpression)Construct an Encog program using the specified expression, but create an empty context and variable holder.EncogProgram(java.lang.String expression)Construct an Encog program using the specified expression, but create an empty context and variable holder.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description doublecalculateError(MLDataSet data)Calculate the error of the ML method, given a dataset.ProgramNodecompileEPL(java.lang.String code)Compile the specified EPL into an actual program node structure, for later execution.ProgramNodecompileExpression(java.lang.String expression)Compile the specified expression.doublecompute(double... x)MLDatacompute(MLData input)Compute the output from the input MLData.voidcopy(Genome source)Copy from the specified genome into this one.java.lang.StringdumpAsCommonExpression()ExpressionValueevaluate()Execute the program and return the result.ProgramNodefindNode(int index)Find the specified node by index.java.lang.StringgenerateEPL()EncogProgramContextgetContext()java.lang.ObjectgetExtraData(java.lang.String name)Get extra data that might be needed by user extended opcodes.FunctionFactorygetFunctions()intgetInputCount()intgetOutputCount()ValueTypegetReturnType()ProgramNodegetRootNode()EncogProgramVariablesgetVariables()static booleanparseBoolean(java.lang.String str)Parse the specified program, or expression, and return the result.static ExpressionValueparseExpression(java.lang.String str)Parse the specified program, or expression, and return the result.static doubleparseFloat(java.lang.String str)Parse the specified program, or expression, and return the result.static java.lang.StringparseString(java.lang.String str)Parse the specified program, or expression, and return the result.voidreplaceNode(ProgramNode replaceThisNode, ProgramNode replaceWith)Replace the specified node with another.intselectRandomVariable(java.util.Random rnd, java.util.List<ValueType> desiredTypes)Select a random variable from the defined variables.voidsetExtraData(java.lang.String name, java.lang.Object value)Set extra data that might be needed by extensions.voidsetRootNode(ProgramNode theRootNode)Set the root node for the program.intsize()java.lang.StringtoString()-
Methods inherited from class org.encog.ml.ea.genome.BasicGenome
getAdjustedScore, getBirthGeneration, getPopulation, getScore, getSource, getSpecies, setAdjustedScore, setBirthGeneration, setPopulation, setScore, setSource, setSpecies
-
-
-
-
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(java.lang.String expression)
Construct an Encog program using the specified expression, but create an empty context and variable holder.- Parameters:
expression- The expression.
-
EncogProgram
public EncogProgram(EncogProgramContext theContext, java.lang.String theExpression)
Construct an Encog program using the specified expression, but create an empty context and variable holder.- Parameters:
theContext- The context for this program.theExpression- The expression.
-
-
Method Detail
-
parseBoolean
public static boolean parseBoolean(java.lang.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(java.lang.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(java.lang.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 java.lang.String parseString(java.lang.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:
calculateErrorin interfaceMLError- Parameters:
data- The dataset.- Returns:
- The error.
-
compileEPL
public ProgramNode compileEPL(java.lang.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(java.lang.String expression)
Compile the specified expression.- Parameters:
expression- The expression.- Returns:
- The program node that this was compiled into.
-
compute
public double compute(double... x)
-
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:
computein interfaceMLRegression- 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.
-
dumpAsCommonExpression
public java.lang.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 java.lang.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:
getInputCountin interfaceMLInput- Returns:
- The input.
-
getOutputCount
public int getOutputCount()
- Specified by:
getOutputCountin interfaceMLOutput- 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(java.util.Random rnd, java.util.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()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classBasicGenome
-
getExtraData
public java.lang.Object getExtraData(java.lang.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(java.lang.String name, java.lang.Object value)Set extra data that might be needed by extensions.- Parameters:
name- The name of the data stored.value- The data.
-
-
DMelt 3.0 © DataMelt by jWork.ORG