public class GESpecies extends IntegerVectorSpecies
GESpecies generates GPIndividuals from GEIndividuals through the application of a grammar parse graph computed by the GrammarParser.
GESpecies uses a GrammarParser to do its dirty work. This parser's job is to take a grammar (in the form of a BufferedReader) and convert it to a tree of GrammarNodes which define the parse graph of the grammar. The GESpecies then interprets his parse graph according to the values in the GEIndividual to produce the equivalent GPIndividual, which is then evaluated.
To do this, GESpecies relies on a subsidiary GPSpecies which defines the GPIndividual and various GPFunctionSets from which to build the parser. This is a grand hack -- the GPSpecies does not know it's being used this way, and so we must provide various dummy parameters to keep the GPSpecies happy even though they'll never be used.
If you are daring, you can replace the GrammarParser with one of your own to customize the parse structure and grammar.
ECJ's Default GE Grammar GE traditionally can use any grammar, and builds parse graphs from that. For simplicity, and in order to remain as compatable as possible with ECJ's existing GP facilities (and GP tradition), ECJ only uses a single Lisp-like grammar which generates standard ECJ trees. This doesn't lose much in generality as the grammar is quite genral.
The grammar assumes that expansion points are enclosed in <> and functions are enclosed in (). For example:
# This is a comment
<prog> ::= <op>
<op> ::= (if-food-ahead <op> <op>)
<op> ::= (progn2 <op> <op>)
<op> ::= (progn3 <op> <op> <op>)
<op> ::= (left) | (right) | (move)
alternatively the grammar could also be writen in the following format:
<prog> ::= <op>
<op> ::= (if-food-ahead <op> <op>) | (progn2 <op> <op>) | (progn3 <op> <op> <op>) | (left) | (right) | (move)
Note that you can use several lines to define the same grammar rule: for example, <op> was defined by several lines when it could have consisted of several elements separated by vertical pipes ( | ). Either way is fine, or a combination of both.
GPNodes are included in the grammar by using their name. This includes ERCs, ADFs, ADMs, and ADFArguments, which should all work just fine. For example, since most ERC GPNodes are simply named "ERC", if you have only one ERC GPNode in your function set, you can just use (ERC) in your grammar.
Once the gammar file has been created and setup has been run trees can the be created using the genome (chromosome) of a GEIndividual. A genome of an individual is an array of random integers each of which are one int long. These numbers are used when a decision point (a rule having more that one choice) is reached within the grammar. Once a particular gene (index) in the genome has been used it will not be used again (this may change) when creating the tree.
For example:
number of chromosomes used = 0
genome = {23, 654, 86}
the current rule we are considering is <op>.
%lt;op> can map into one of the following: (if-food-ahead <op> <op>) | (progn2 <op> <op>) | (progn3 <op> <op> <op>)
| (left) | (right) | (move)
Since the rule <op> has more than one choice that it can map to, we must consult the genome to decide which choice to take. In this case
the number of chromosomes used is 0 so genome[0] is used and number of chromosomes used is incremented. Since values in the genome can
be negitive values they are offset by 128 (max negitive of a int) giving us a value from 0-255. A modulus is performed on this resulting
number by the number of choices present for the given rule. In the above example since we are using genome[0] the resulting operation would
look like: 23+128=151, number of choices for <op> = 6, 151%6=1 so we use choices[1] which is: (progn2 <op> <op>). If all the genes
in a genome are used and the tree is still incompete an invalid tree error is returned.
Each node in the tree is a GPNode and trees are constructed depth first.
Parameters
base.file String |
(the file is where the rules of the gammar are stored) |
base.gp-species classname, inherits and != ec.gp.GPSpecies |
(the GPSpecies subservient to the GESpecies) |
base.parser classname, inherits and != ge.GrammarParser |
(the GrammarParser used by the GESpecies) |
Default Base
ge.GESpecies
Modifier and Type | Field and Description |
---|---|
static int |
BIG_TREE_ERROR |
java.util.HashMap |
ERCBank
All the ERCs created so far, the ERCs are mapped as,
"key --> list of ERC nodes", where the key = (genome[i] - minGene[i]);
The ERCBank is "static", beacause we need one identical copy
for all the individuals; Moreover, this copy may be sent to
other sub-populations as well.
|
GPSpecies |
gpspecies
The GPSpecies subsidiary to GESpecies.
|
GrammarRuleNode[] |
grammar
The parsed grammars.
|
GrammarParser[] |
grammarParser
Parser for each grammar -- khaled
|
java.lang.String |
initScheme |
static java.lang.String |
P_FILE |
static java.lang.String |
P_GESPECIES |
static java.lang.String |
P_GPSPECIES |
static java.lang.String |
P_INITSCHEME |
static java.lang.String |
P_PARSER |
static java.lang.String |
P_PASSES |
GrammarParser |
parser_prototype
The prototypical parser used to parse the grammars.
|
int |
passes
The number of passes permitted through the genome if we're wrapping.
|
C_RANDOM_WALK_MUTATION, C_RESET_MUTATION, maxGene, minGene, mutationIsBounded, mutationType, P_MAXGENE, P_MINGENE, P_MUTATION_BOUNDED, P_MUTATIONTYPE, P_NUM_SEGMENTS, P_RANDOM_WALK_PROBABILITY, P_SEGMENT, P_SEGMENT_END, P_SEGMENT_START, P_SEGMENT_TYPE, randomWalkProbability, V_RANDOM_WALK_MUTATION, V_RESET_MUTATION
C_ANY_POINT, C_GEOMETRIC, C_INTERMED_RECOMB, C_LINE_RECOMB, C_NONE, C_ONE_POINT, C_ONE_POINT_NO_NOP, C_SIMULATED_BINARY, C_TWO_POINT, C_TWO_POINT_NO_NOP, C_UNIFORM, chunksize, crossoverDistributionIndex, crossoverProbability, crossoverType, duplicateRetries, dynamicInitialSize, genomeIncreaseProbability, genomeResizeAlgorithm, genomeSize, lineDistance, maxInitialSize, minInitialSize, mutationProbability, P_CHUNKSIZE, P_CROSSOVER_DISTRIBUTION_INDEX, P_CROSSOVERPROB, P_CROSSOVERTYPE, P_DUPLICATE_RETRIES, P_GENOMESIZE, P_GEOMETRIC_PROBABILITY, P_LINEDISTANCE, P_MUTATIONPROB, P_UNIFORM_MAX, P_UNIFORM_MIN, P_VECTORSPECIES, V_ANY_POINT, V_GEOMETRIC, V_INTERMED_RECOMB, V_LINE_RECOMB, V_ONE_POINT, V_ONE_POINT_NO_NOP, V_SIMULATED_BINARY, V_TWO_POINT, V_TWO_POINT_NO_NOP, V_UNIFORM
f_prototype, i_prototype, P_FITNESS, P_INDIVIDUAL, P_PIPE, pipe_prototype
Constructor and Description |
---|
GESpecies() |
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
clone()
Creates a new individual cloned from a prototype,
and suitable to begin use in its own evolutionary
context.
|
int |
consumed(EvolutionState state,
GEIndividual ind,
int threadnum)
Returns the number of elements consumed from the GEIndividual array to produce
the tree, else returns -1 if an error occurs, specifically if all elements were
consumed and the tree had still not been completed.
|
Parameter |
defaultBase()
Returns the default base for this prototype.
|
java.util.List |
flattenSexp(EvolutionState state,
int threadnum,
GPTree tree)
Flattens an S-expression
|
java.util.List |
gatherNodeString(EvolutionState state,
int threadnum,
GPNode node,
int index)
Used by the above function
|
java.lang.String |
getKeyFromNode(EvolutionState state,
int threadnum,
GPNode node,
int index) |
int |
makeTree(EvolutionState state,
int[] genome,
GPTree tree,
int position,
int treeNum,
int threadnum,
java.util.HashMap ercMapsForFancyPrint)
makeTree, edits the tree that its given by adding a root (and all subtrees attached)
|
int |
makeTrees(EvolutionState state,
GEIndividual ind,
GPTree[] trees,
int threadnum,
java.util.HashMap ercMapsForFancyPrint)
creates all of an individual's trees
|
int |
makeTrees(EvolutionState state,
int[] genome,
GPTree[] trees,
int threadnum,
java.util.HashMap ercMapsForFancyPrint) |
GPIndividual |
map(EvolutionState state,
GEIndividual ind,
int threadnum,
java.util.HashMap ercMapsForFancyPrint)
Returns a dummy GPIndividual with a single tree which was built by mapping
over the elements of the given GEIndividual.
|
Individual |
newIndividual(EvolutionState state,
int thread)
This is an ugly hack to simulate the "Sensible Initialization",
First we create a GPIndividual, then reverse-map it to GEIndividuals,
We do not need to call IntegerVectorSpecies.newIndividual() since it is overriden
by the GPSpecies.newIndividual();
Moreover, as in the case for non-identical representations (i,e, GP-GE island
models etc,), the grammar rules, tree constraints, ERC's etc, are supposed to be
identical across all islands, so we are using the same "gpspecies" inside this class.
|
GPNode |
obtainERC(EvolutionState state,
int genomeVal,
int threadnum,
GPNode node,
java.util.HashMap ercMapsForFancyPrint)
Loads an ERC from the ERCBank given the value in the genome.
|
int[] |
parseSexp(java.util.ArrayList flatSexp,
GrammarParser gp)
The LL(1) parsing algorithm to parse the lisp tree, the lisp tree is actually
fed as a flattened list, the parsing code uses the "exact" (and as-is) procedure
described in the dragon book.
|
GEIndividual |
reverseMap(EvolutionState state,
GPIndividual ind,
int threadnum)
Reverse of the original map() function, takes a GPIndividual and returns
a corresponding GEIndividual; The GPIndividual may contain more than one trees,
and such cases are handled accordingly, see the 3rd bullet below --
NOTE:
This reverse mapping is only valid for S-expression trees ;
This procedure supports ERC for the current population (not for population
/subpopulation from other islands); However, that could be done by merging
all ERCBanks from all the sub-populations but that is not done yet ;
Support for the ADF's are done as follows -- suppose in one GPIndividual,
there are N trees -- T1, T2, ,,, Tn and each of them follows n different
grammars G1, G2, ,,, Gn respectively; now if they are reverse-mapped to
int arrays, there will be n int arrays A1[], A2[], ,,, An[]; and suppose
the i-th tree Ti is reverse mapped to int array Ai[] and morevoer Ai[] is
the longest among all the arrays (Bj[]s); so Bi[] is sufficient to build
all ADF trees Tjs.
|
void |
setup(EvolutionState state,
Parameter base)
The default version of setup(...) loads requested pipelines and calls setup(...) on them and normalizes their probabilities.
|
inNumericalTypeRange, inNumericalTypeRange, loadParametersForGene, maxGene, minGene, mutationIsBounded, mutationType, randomWalkProbability
contains, contains, contains, contains, duplicateRetries, fill, fill, fill, fill, initializeGenomeSegmentsByEndIndices, initializeGenomeSegmentsByStartIndices, mutationProbability, setupGenome
buildMisc, newIndividual, newIndividual
public static final java.lang.String P_GESPECIES
public static final java.lang.String P_FILE
public static final java.lang.String P_GPSPECIES
public static final java.lang.String P_PARSER
public static final java.lang.String P_PASSES
public static final java.lang.String P_INITSCHEME
public static final int BIG_TREE_ERROR
public GPSpecies gpspecies
public java.util.HashMap ERCBank
public GrammarRuleNode[] grammar
public int passes
public java.lang.String initScheme
public GrammarParser parser_prototype
public GrammarParser[] grammarParser
public void setup(EvolutionState state, Parameter base)
Species
setup
in interface Prototype
setup
in interface Setup
setup
in class IntegerVectorSpecies
Prototype.setup(EvolutionState,Parameter)
public Individual newIndividual(EvolutionState state, int thread)
newIndividual
in class VectorSpecies
public int makeTrees(EvolutionState state, GEIndividual ind, GPTree[] trees, int threadnum, java.util.HashMap ercMapsForFancyPrint)
state
- Evolution statetrees
- array of trees for the individualind
- the GEIndividualthreadnum
- tread numberpublic int makeTrees(EvolutionState state, int[] genome, GPTree[] trees, int threadnum, java.util.HashMap ercMapsForFancyPrint)
public int makeTree(EvolutionState state, int[] genome, GPTree tree, int position, int treeNum, int threadnum, java.util.HashMap ercMapsForFancyPrint)
state
- ind
- tree
- position
- treeNum
- threadnum
- public GPNode obtainERC(EvolutionState state, int genomeVal, int threadnum, GPNode node, java.util.HashMap ercMapsForFancyPrint)
public java.lang.Object clone()
Prototype
Typically this should be a full "deep" clone. However, you may share certain elements with other objects rather than clone hem, depending on the situation:
Implementations.
public Object clone()
{
try
{
return super.clone();
}
catch ((CloneNotSupportedException e)
{ throw new InternalError(); } // never happens
}
public Object clone()
{
try
{
MyObject myobj = (MyObject) (super.clone());
// put your deep-cloning code here...
}
catch ((CloneNotSupportedException e)
{ throw new InternalError(); } // never happens
return myobj;
}
public Object clone()
{
MyObject myobj = (MyObject) (super.clone());
// put your deep-cloning code here...
return myobj;
}
public Parameter defaultBase()
Prototype
defaultBase
in interface Prototype
defaultBase
in class VectorSpecies
public int consumed(EvolutionState state, GEIndividual ind, int threadnum)
public GPIndividual map(EvolutionState state, GEIndividual ind, int threadnum, java.util.HashMap ercMapsForFancyPrint)
public java.util.List flattenSexp(EvolutionState state, int threadnum, GPTree tree)
public java.util.List gatherNodeString(EvolutionState state, int threadnum, GPNode node, int index)
public java.lang.String getKeyFromNode(EvolutionState state, int threadnum, GPNode node, int index)
public int[] parseSexp(java.util.ArrayList flatSexp, GrammarParser gp)
public GEIndividual reverseMap(EvolutionState state, GPIndividual ind, int threadnum)