|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--gap.GPProgram
The GPProgram class contains a program tree and methods to execute and manipulate it.
Field Summary | |
protected gap.GPProgramArray |
adf
"Array" containing the automatically defined functions. |
static int |
ADF_EXCL
Used in methods with a "mask" argument. |
static int |
ADF_ONLY
Used in methods with a "mask" argument. |
static java.lang.String |
ADF_PFX
This token marks the beginning of an ADF program tree in a string representation. |
static java.lang.String |
ADFLINK_PFX
This token represent an ADF in a string representation. |
static java.lang.String |
ARG_PFX
This token represent a program argument in a string representation. |
static int |
EMPTY_MASK
Used in methods with a "mask" argument. |
protected static int |
ERROR
Error value. |
static int |
NO_TYPE
In methods with a "type" argument, use this to indicate that you don't care about the type. |
protected GPParams |
params
The parameters. |
protected GPFunction |
root
Root of the program tree. |
static int |
ROOT_EXCL
Used in methods with a "mask" argument. |
protected static Ref |
ROOT_REF
Constant reference which represents any reference to the root. |
static java.lang.String |
ROOT_TOKEN
This token marks the beginning of the main program tree in a string representation. |
static int |
TERMINALS_EXCL
Used in methods with a "mask" argument. |
Constructor Summary | |
|
GPProgram(GPParams params)
Create a random GPProgram. |
|
GPProgram(GPParams params,
java.io.File progrFile)
Create a GPProgram by parsing a LISP-style string. |
protected |
GPProgram(GPParams params,
gap.GPProgramArray linkedADFs)
Constructor for ADFs with linked ADFs. |
protected |
GPProgram(GPParams params,
int internal)
Construct a void program (without root and ADFs). |
|
GPProgram(GPParams params,
java.lang.String progr)
Create a GPProgram by parsing a LISP-style string. |
Method Summary | |
protected javax.swing.tree.MutableTreeNode |
asJTree(GPFunction tree)
Represent a subtree as a part of a JTree. |
javax.swing.JTree |
asJTree(java.lang.String name)
Return a graphical representation of the program tree. |
java.lang.Object |
clone()
Clone the program. |
void |
compile(java.lang.String name,
java.io.File f)
Compile the GPProgram to an independent Java class. |
void |
compile(java.lang.String name,
java.lang.String filename)
Compile the GPProgram to an independent Java class. |
protected GPFunction |
constructTree(int[] n,
java.lang.String[] tokens,
Ref father,
GPParams params,
java.util.HashMap fnames,
java.util.HashMap tnames)
Recursively construct a program subtree by parsing a list of tokens. |
protected int |
countPoints(GPFunction tree,
int type,
int mask)
Count a subset of the nodes in a subtree. |
int |
countProgramPoints(int type,
int mask)
Count a subset of the nodes. |
float |
exec(java.lang.Object o,
float[] args)
Execute this program. |
protected int |
extractSuffixNr(java.lang.String str,
java.lang.String pfx)
extractSuffixNr( "pfx5" ) == 5 |
boolean |
find(java.lang.Class[] fcl,
java.util.ArrayList refs)
Get all the references to functions of specified classes in the main program. |
boolean |
find(java.lang.Class[] fcl,
java.util.ArrayList refs,
java.util.ArrayList adfRefList,
boolean upmostOnly)
Get all the references to functions of specified classes in the main program and in the ADFs. |
protected boolean |
find(GPFunction tree,
java.lang.Class[] fcl,
java.util.ArrayList refs,
boolean upmostOnly)
Get all the references to functions of specified classes in a subtree. |
protected boolean |
find(GPFunction tree,
int[] fidx,
java.util.ArrayList refs,
boolean upmostOnly)
Get all the references to special functions of specified index in a (sub)tree. |
boolean |
find(int[] fidx,
java.util.ArrayList refs,
java.util.ArrayList adfRefList,
boolean upmostOnly)
Get all the references to special functions of specified index in the main program and in the ADFs. |
protected int |
getMaxDepth(GPFunction tree)
Compute the maximal depth of a tree. |
GPParams |
getParams()
Return the parameters. |
protected int |
getRefTo(GPFunction tree,
int n,
int type,
int mask,
Ref r)
Get a reference to a function by the number in a subtree. |
protected Ref |
getRefTo(GPProgram progr,
int n,
int type,
int mask)
Get a reference to a function by its number in the program tree. |
static GPProgram |
load(java.io.File f)
Load a program. |
static GPProgram |
load(java.lang.String filename)
Load a program. |
GPProgram |
mutate()
Mutate. |
protected GPProgram |
mutate(int mask)
Mutate. |
protected GPFunction |
newTree(int depth,
Ref father)
Create a new program tree. |
protected void |
parse(java.lang.String progr)
Parse a program string. |
protected boolean |
sameType(int type,
GPFunction f)
Compare a type with the type of a GPFunction. |
void |
save(java.io.File f)
Save the program. |
void |
save(java.lang.String filename)
Save the program. |
int |
size()
Compute the size of the program tree. |
java.lang.String |
toString()
Pretty print the program tree and the ADFs. |
GPProgram |
xover(GPProgram partner)
Crossover. |
protected GPProgram |
xover(GPProgram partner,
int mask)
Crossover. |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected GPFunction root
protected gap.GPProgramArray adf
protected GPParams params
protected static final Ref ROOT_REF
public static final java.lang.String ROOT_TOKEN
GPProgram(GPParams, String)
,
toString()
public static final java.lang.String ADF_PFX
GPProgram(GPParams, String)
,
toString()
public static final java.lang.String ADFLINK_PFX
GPProgram(GPParams, String)
,
toString()
public static final java.lang.String ARG_PFX
GPProgram(GPParams, String)
,
toString()
protected static final int ERROR
public static final int NO_TYPE
public static final int EMPTY_MASK
public static final int ROOT_EXCL
public static final int TERMINALS_EXCL
public static final int ADF_EXCL
public static final int ADF_ONLY
Constructor Detail |
public GPProgram(GPParams params) throws ParametersNotProperlySetException
params
- The parameters from which to create the program.ParametersNotProperlySetException
- If the parameters definition is inconsistent.protected GPProgram(GPParams params, gap.GPProgramArray linkedADFs) throws java.lang.InstantiationException
public GPProgram(GPParams params, java.io.File progrFile) throws InvalidProgramStringException, ParametersNotProperlySetException
The program string is read from the specified file and parsed as in
GPProgram(GPParams, String)
params
- The parameters of the program.progrFile
- An ASCII file from which to read the program string.InvalidProgramStringException
- If a syntax error is found or if the program string is not compatible
with the parameters.ParametersNotProperlySetException
- If the parameters definition is inconsistent.GPProgram(GPParams, String)
public GPProgram(GPParams params, java.lang.String progr) throws InvalidProgramStringException, ParametersNotProperlySetException
The string format is closed with respect to the toString() output: the result of the toString() method can be used as input for this constructor. Schema of a program string :
root: RootFunction( Func1(Term1, Term2), ADF0(Term3, Arg0) ) adf_root0: ADF0RootFunction( ... ) adf_root1: ADF1RootFunction( ... )
The string can also set random constants in terminals, if the name of
the corresponding field in the terminal class is randInt
or
randFloat
(as in the automatically generated classes).
The value has to be specified after the terminal name, in the toString() format:
Sum( IntegerNumber<5>, FloatNumber<4.32> )
Parenthesis and commas are ignored, so that the followting expressions are equivalent:
(Sum(Arg0 Arg1)) Sum( Arg0, Arg1 ) Sum Arg0 Arg1There are no ambiguities, as the number of argument of each function are fixed and the arguments are assigned in a depth-first way.
params
- The parameters of the program.progr
- The program string.InvalidProgramStringException
- If a syntax error is found or if the program string is not compatible
with the parameters.ParametersNotProperlySetException
- If the parameters definition is inconsistent.protected GPProgram(GPParams params, int internal)
internal
- Ignored.Method Detail |
public GPParams getParams()
protected GPFunction newTree(int depth, Ref father) throws java.lang.InstantiationException
The "grow" method is used.
depth
- Maximal depth.father
- The father of the subtree to be created. ROOT_REF
if root.java.lang.InstantiationException
- If it coldn't create an instance of a GPFunction or GPTerminal of the function set.ROOT_REF
protected int extractSuffixNr(java.lang.String str, java.lang.String pfx)
pfx
- String prefix.ERROR
if error.protected void parse(java.lang.String progr) throws InvalidProgramStringException, java.lang.InstantiationException
InvalidProgramStringException
- If a syntax error is found or if the program string is not compatible
with the parameters.GPProgram(GPParams, String)
protected GPFunction constructTree(int[] n, java.lang.String[] tokens, Ref father, GPParams params, java.util.HashMap fnames, java.util.HashMap tnames) throws InvalidProgramStringException, java.lang.InstantiationException
n
- Position in the tokens array.tokens
- Array of tokens extracted from the program string.father
- Reference to the father of the subtree. ROOT_REF
if root.fnames
- Names of the functions in the function set.tnames
- Names of the terminals in the terminal set.parse(String)
public float exec(java.lang.Object o, float[] args)
The object passed can be used for example to implement side effects of the functions or to store internal variables.
exec
in interface Executable
o
- An object that your GPFunctions may want to use.args
- The arguments of the main function.public java.lang.Object clone()
Make a deep clone of the program tree and of the ADFs. The parameters of the program are not cloned.
clone
in class java.lang.Object
public java.lang.String toString()
The returned string could be quite large.
toString
in class java.lang.Object
public GPProgram mutate() throws ParametersNotProperlySetException
If the created program is too large, ths function returns a clone of this program.
protected GPProgram mutate(int mask) throws ParametersNotProperlySetException
mask
- EMPTY_MASK, ROOT_EXCL and ADF_EXCL allowed.public GPProgram xover(GPProgram partner)
Assumed: this program and his partner share the same parameters.
First the function decides if the crossover point is going to be choosed in
an ADF (if defined) according to the geneticOpInADFFreq
variable
in the parameters. Then a random xover point (1) is chosen from this program
tree. A random compatible point (2) in the partner is then chosen and
inserted at point (1).
Neither this program nor the partner are modified. A clone is returned if the result is too large or il no compatible point is found.
protected GPProgram xover(GPProgram partner, int mask)
mask
- EMPTY_MASK and ADF_EXCL allowed.xover(GPProgram)
public int size()
public int countProgramPoints(int type, int mask)
type
- Count the nodes of this type.mask
- EMPTY_MASK, ROOT_EXCL, TERMINALS_EXCL, ADF_EXCL and ADF_ONLY (or a combination)
allowed.type
and with the mask
.NO_TYPE
protected int countPoints(GPFunction tree, int type, int mask)
tree
- The subtree.type
- Count the nodes of this type.mask
- EMPTY_MASK and TERMINALS_EXCL allowed.NO_TYPE
,
countProgramPoints(int, int)
protected int getMaxDepth(GPFunction tree)
tree
- The root of a program tree.protected Ref getRefTo(GPProgram progr, int n, int type, int mask)
Used to choose a random function in the program tree.
mask
- EMPTY_MASK, ROOT_EXCL and TERMINALS_EXCL allowed.protected int getRefTo(GPFunction tree, int n, int type, int mask, Ref r)
mask
- EMPTY_MASK, ROOT_EXCL and TERMINALS_EXCL allowed.protected boolean sameType(int type, GPFunction f)
The GPFunction could be an ADF or an Argument...
public boolean find(java.lang.Class[] fcl, java.util.ArrayList refs)
fcl
- Array of subclasses of GPFunction, to find in the program.refs
- The references are stored here. Set to null if you don't care about them.Ref
public boolean find(java.lang.Class[] fcl, java.util.ArrayList refs, java.util.ArrayList adfRefList, boolean upmostOnly)
Note: a changement in the function- OR terminal- set in an ADF (with respect to the corresponding sets in the main program tree) will stop this method from searching into that ADF.
fcl
- Array of subclasses of GPFunction, to find in the program.refs
- List of Refs in the main program.
Set to null if you don't care about them.adfRefList
- List (a list for each ADF) of lists of Refs in the ADFs.
Set to null if you don't care about them.upmostOnly
- If true
return references only to the upmost
functions.Ref
protected boolean find(GPFunction tree, java.lang.Class[] fcl, java.util.ArrayList refs, boolean upmostOnly)
public boolean find(int[] fidx, java.util.ArrayList refs, java.util.ArrayList adfRefList, boolean upmostOnly)
The indices of ADFunctions must be specified by their (positive) value, while those of arguments must be specified by the negative-1 of their value. E.g.: "find ADF #2 and argument #0": int[] fidx = {2,-1};
protected boolean find(GPFunction tree, int[] fidx, java.util.ArrayList refs, boolean upmostOnly)
public void compile(java.lang.String name, java.lang.String filename) throws CompileFailureException, java.io.IOException
name
- The name of the new class, package name included (e.g. 'ant.BestProgram').filename
- The name of the file in which the compiled class is written.public void compile(java.lang.String name, java.io.File f) throws CompileFailureException, java.io.IOException
name
- The name of the new class, package name included (e.g. 'ant.BestProgram').f
- The file in which the new class is written.public javax.swing.JTree asJTree(java.lang.String name)
name
- The name of the root node of the JTree.protected javax.swing.tree.MutableTreeNode asJTree(GPFunction tree)
public void save(java.lang.String filename) throws java.io.IOException
public void save(java.io.File f) throws java.io.IOException
public static GPProgram load(java.lang.String filename) throws java.io.IOException, java.lang.ClassNotFoundException
public static GPProgram load(java.io.File f) throws java.io.IOException, java.lang.ClassNotFoundException
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |