Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary Referen GRAMMATICAL OPTIMIZATION THE SOURCE CODE Muhammad Adil Raja Roaming Researchers, Inc. cbna April 27, 2015
Jul 15, 2015
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GRAMMATICAL OPTIMIZATIONTHE SOURCE CODE
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 27, 2015
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
OUTLINE I
1 INTRODUCTION
2 GE’S MAIN
3 INITIALIZER
4 GENOTYPE
5 GRAMMAR
6 PRODUCTION
7 RULE
8 MAPPER
9 SYMBOL
10 TREE
11 SUMMARY
12 REFERENCES
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
INTRODUCTION I
Grammatical optimization (GO) is an adaptation ofgrammatical evolution (GE).
The algorithm is implemented in Java.
The algorithm is aimed at general optimization problems.
With slight modification it can also be used for other typesof problems such as classification.
The source code has many Java files.
In what follows you will find the source code of variouscomponents of Java Beagle.
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE’S MAIN I
/∗∗ GEsMain . java∗∗ Created on August 4 , 2007 , 5:31 PM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
package GrammaticOpt imizat ion ;
/∗∗∗∗ @author a d i l r a j a∗ /
import java . u t i l .∗ ;
public class GEsMain {
/∗∗ Creates a new ins tance of GEsMain ∗ /public GEsMain ( ) {}
/∗∗∗ @param args the command l i n e arguments
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE’S MAIN II
∗ /public s t a t i c void main ( S t r i n g [ ] args ) {
/ / TODO code a p p l i c a t i o n l o g i c hereRandom rand=new Random ( ) ;i n t [ ] newArray= new i n t [ 1 0 ] ;newArray [ 0 ] = 0 ;newArray [ 1 ] = 2 ;newArray [ 2 ] = 1 ;newArray [ 3 ] = 4 ;newArray [ 4 ] = 4 ;newArray [ 5 ] = 4 ;newArray [ 6 ] = 4 ;newArray [ 7 ] = 4 ;newArray [ 8 ] = 4 ;newArray [ 9 ] = 4 ;
A r rayL i s t < In teger > a r r L i s t =new A r r a y L i s t ( ) ;for ( i n t i =0; i <10; i ++){
a r r L i s t . add ( ( I n tege r ) newArray [ i ] ) ;}Genotype geno=new Genotype ( newArray , 10 , true , 1 0 ) ;
GEGrammar grammar=new GEGrammar( geno ) ;grammar . setMaxWraps ( 9 ) ;grammar . readBNFFile ( " / Users / a d i l r a j a / mywork / ad i l s−java / GrammaticOpt imizat ion / d i s t / grammar . bnf " ) ;
/ / t r y {/ / grammar . genotype2phenotype ( t r ue ) ;
/ / }
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE’S MAIN III
/ / catch ( java . lang . Except ion e ) {/ / System . out . p r i n t l n ( e+" In Main " ) ;
/ / }I t e r a t o r <Symbol> symbIt=grammar . getPhenotype ( ) . i t e r a t o r ( ) ;I t e r a t o r <Rule> r u l e I t =grammar . i t e r a t o r ( ) ;System . out . p r i n t l n ( " Here are the r e s u l t s : phenotype s izse= "+grammar . getPhenotype ( ) . s i ze ( ) ) ;while ( symbIt . hasNext ( ) ) {
System . out . p r i n t ( symbIt . next ( ) . getSymbol ( ) ) ;}i f ( grammar . phenotype . ge tVa l i d ( )== true )
System . out . p r i n t l n ( " \ nThe above phenotype i s v a l i d " ) ;else
System . out . p r i n t l n ( " \ nThe above phenotype i s not v a l i d " ) ;System . out . p r i n t l n ( " \ n \ nHere i s the grammar " ) ;while ( r u l e I t . hasNext ( ) ) {
Rule tmpRule= r u l e I t . next ( ) ;System . out . p r i n t ( " \ n "+tmpRule . l hs . get ( 0 ) . getSymbol ( )+ " : : = " ) ;I t e r a t o r <Product ion > p r o d I t =tmpRule . i t e r a t o r ( ) ;while ( p r o d I t . hasNext ( ) ) {
I t e r a t o r <Symbol> symbIt2= p r o d I t . next ( ) . i t e r a t o r ( ) ;while ( symbIt2 . hasNext ( ) ) {
System . out . p r i n t ( symbIt2 . next ( ) . getSymbol ( ) ) ;}
}}System . out . p r i n t l n ( "My name i s John Rambo "+grammar . s i ze ( ) ) ;System . out . p r i n t l n ( newArray . leng th ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE’S MAIN IV
S t r i n g s t r 1 =new S t r i n g ( " A d i l " ) ;S t r i n g s t r 2 =new S t r i n g ( " A d i l " ) ;i f ( s t r 1 . equals ( s t r 2 ) )
System . out . p r i n t l n ( " ad i lRa ja " ) ;Symbol symb=new Symbol ( ) ;System . out . p r i n t l n ( symb . getType ( ) . t o S t r i n g ( ) ) ;symb . setType ( SymbolType . NTSymbol ) ;System . out . p r i n t l n ( symb . getType ( ) . t o S t r i n g ( ) ) ;
r u l e I t =grammar . i t e r a t o r ( ) ;System . out . p r i n t l n ( " \ n \ nHere i s the grammar ’ s NT/ T i n f o " ) ;while ( r u l e I t . hasNext ( ) ) {
Rule tmpRule= r u l e I t . next ( ) ;System . out . p r i n t ( " \ n "+tmpRule . l hs . get ( 0 ) . getType ( ) . t o S t r i n g ( )+ " : : = " ) ;I t e r a t o r <Product ion > p r o d I t =tmpRule . i t e r a t o r ( ) ;while ( p r o d I t . hasNext ( ) ) {
I t e r a t o r <Symbol> symbIt2= p r o d I t . next ( ) . i t e r a t o r ( ) ;while ( symbIt2 . hasNext ( ) ) {
System . out . p r i n t ( symbIt2 . next ( ) . getType ( ) . t o S t r i n g ( )+ " " ) ;}
}}
}
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
INITIALIZER I
/∗∗ I n i t i a l i s e r . java∗∗ Created on Ju ly 2 , 2007 , 6:05 PM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
/∗∗∗∗ @author a d i l r a j a∗∗ /
package GrammaticOpt imizat ion ;
public inter face I n i t i a l i s e r {public i n t getPopSize ( ) ;public void setPopSize ( f i n a l i n t j ) ;public i n t get Index ( ) ;public void set Index ( f i n a l i n t i ) ;public boolean i n i t ( f i n a l i n t i ) ;
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GENOTYPE I
/∗∗ Genotype . java∗∗ Created on June 1 , 2007 , 11:35 PM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
/∗∗∗∗ @author a d i l r a j a∗ /
package GrammaticOpt imizat ion ;
import java . u t i l .∗ ;import java . u t i l . A r r a y L i s t ;
/∗ Defau l t Const ruc tor . Creates a genotype s t r u c t u r e o f leng th newLength , using the elements o f newArray , and sets∗ i t s v a l i d f i e l d to newValid and i t s f i t n e s s to newFitness . Non−s p e c i f i e d arguments are given d e f a u l t values∗ , as s p e c i f i e d i n the f u n c t i o n pro to type .∗∗ /
public class Genotype extends Ar rayL i s t < In teger >{ / / codonType
private Vector genome ;private boolean _l ibGE_Genotype_val id ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GENOTYPE II
private double _l ibGE_Genotype_f i tness ;private i n t _libGE_Genotype_maxCodonValue ;private i n t _l ibGE_Genotype_ef fect iveSize ;private i n t _libGE_Genotype_wraps ;
public Genotype ( ) {
}
/∗∗ Creates a new ins tance of Genotype ∗ /public Genotype ( f i n a l i n t [ ] newArray , f i n a l i n t newLength , f i n a l boolean newValid , f i n a l double newFitness ) {
_l ibGE_Genotype_val id=newValid ;_ l ibGE_Genotype_f i tness=newFitness ;th is . setMaxCodonValue (32767) ; / / INT_MAXfor ( i n t i i =0; i i <newLength ; i i ++) {
th is . add (new I n t ege r ( newArray [ i i ] ) ) ;}s e t E f f e c t i v e S i z e ( 0 ) ;setWraps ( 0 ) ;
}
/∗ Const ruc tor . Creates a genotype s t r u c t u r e w i th the contents o f newvector , and sets i t s v a l i d∗ f i e l d to newValid and i t s f i t n e s s to newFitness .∗Non−s p e c i f i e d arguments are given d e f a u l t values , as s p e c i f i e d i n he f u n c t i o n pro to type∗ /
public Genotype ( f i n a l Ar rayL i s t < In teger > newVector , f i n a l boolean newValid , f i n a l double newFitness ) {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GENOTYPE III
_l ibGE_Genotype_val id=newValid ;_ l ibGE_Genotype_f i tness=newFitness ;th is . addAl l ( newVector ) ;setMaxCodonValue (32767) ;s e t E f f e c t i v e S i z e ( 0 ) ;setWraps ( 0 ) ;
}
/∗ Const ruc tor . Creates an empty genotype s t ruc tu re , and sets maCodonValue to the value∗s p e c i f i e d as argument∗ /
public Genotype ( f i n a l i n t maxCodonValue ) {setMaxCodonValue ( maxCodonValue ) ;s e t E f f e c t i v e S i z e ( 0 ) ;setWraps ( 0 ) ;
}
/∗Copy cons t ruc to r∗ /
public Genotype ( f i n a l Genotype copy ) {se tVa l i d ( copy . ge tVa l i d ( ) ) ;se tF i tness ( copy . ge tF i tness ( ) ) ;setMaxCodonValue ( copy . getMaxCodonValue ( ) ) ;s e t E f f e c t i v e S i z e ( copy . g e t E f f e c t i v e S i z e ( ) ) ;setWraps ( copy . getWraps ( ) ) ;
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GENOTYPE IV
/∗Returns the cu r ren t v a l i d f i e l d∗ /
public f i n a l boolean ge tVa l i d ( ) {return _l ibGE_Genotype_val id ;
}
/∗Set a new value f o r the v a l i d f i e l d∗ /
public void se tVa l i d ( f i n a l boolean newValid ) {_l ibGE_Genotype_val id=newValid ;
}
/∗Returns the cu r ren t f i t n e s s score∗ /
public f i n a l double ge tF i tness ( ) {return _l ibGE_Genotype_f i tness ;
}
/∗Sets a new f i t n e s s score∗ /
public void se tF i tness ( f i n a l double newFitness ) {_l ibGE_Genotype_f i tness=newFitness ;
}
/∗Returns the maximum conta inab le value i n a codon∗ /
public f i n a l i n t getMaxCodonValue ( ) {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GENOTYPE V
return _libGE_Genotype_maxCodonValue ;}
/∗Sets the maximum conta inab le value i n a codon∗ /
public void setMaxCodonValue ( f i n a l i n t newMaxCodonValue ) {_libGE_Genotype_maxCodonValue=newMaxCodonValue ;
}
/∗Returns e f f e c t i v e leng th o f Genotype∗ /
public f i n a l i n t g e t E f f e c t i v e S i z e ( ) {return _l ibGE_Genotype_ef fect iveSize ;
}
/∗Sets e f f e c t i v e leng th o f genotype∗ /
public void s e t E f f e c t i v e S i z e ( f i n a l i n t newEf fec t iveSize ) {_ l ibGE_Genotype_ef fect iveSize=newEf fec t iveSize ;
}
/∗Returns number o f wrapping events∗ /
public f i n a l i n t getWraps ( ) {return _libGE_Genotype_wraps ;
}
/∗Sets the number o f wrapping events
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GENOTYPE VI
∗ /public void setWraps ( f i n a l i n t newWraps ) {
_libGE_Genotype_wraps=newWraps ;}
/∗Used to p r i n t the genome as a s t r i n g∗ /
public S t r i n g p r in tCon ten ts ( ) {/ / S t r i n g s=new S t r i n g ( ) ;return th is . toAr ray ( ) . t o S t r i n g ( ) ;
}}/ / complete
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GRAMMAR I
/∗∗ Grammar . java∗∗ Created on Ju ly 26 , 2007 , 6:09 PM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
package GrammaticOpt imizat ion ;
/∗∗∗∗ @author a d i l r a j a∗ /
import java . u t i l . I t e r a t o r ;
public abstract class Grammar extends Mapper {private boolean validGrammar ;private i n t star tSymbol ;
protected abstract boolean genotype2phenotype ( ) ;protected abstract boolean phenotype2genotype ( ) ;
/∗∗∗ Creates a new ins tance of Grammar∗ /
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GRAMMAR II
public Grammar ( ) {super ( ) ;setValidGrammar ( fa lse ) ;s tar tSymbol =0;
}
/∗∗∗Const ruc tor s e t t i n g the genotype s t r u c t u r e o f t h i s mapper to newGenotype .∗ /
public Grammar ( f i n a l Genotype newGenotype ) {super ( newGenotype ) ;setValidGrammar ( fa lse ) ;s tar tSymbol =0;
}
/∗∗∗Const ruc tor s e t t i n g the phenotype s t r u c t u r e o f t h i s mapper to newPhenotype .∗ /
public Grammar ( f i n a l Phenotype newPhenotype ) {super ( newPhenotype ) ;setValidGrammar ( fa lse ) ;s tar tSymbol =0;
}
/∗∗∗Copy Const ruc tor
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GRAMMAR III
∗ /public Grammar ( f i n a l Grammar copy ) {
super ( ) ;setValidGrammar ( copy . getValidGrammar ( ) ) ;s tar tSymbol=copy . s tar tSymbol ;
}
/∗∗∗ Return the v a l i d i t y o f the cu r ren t grammar .∗ /
public f i n a l boolean getValidGrammar ( ) {return validGrammar ;
}
/∗∗∗Set the v a l i d i t y o f the grammar .∗ /
protected void setValidGrammar ( f i n a l boolean newValidGrammar ) {validGrammar=newValidGrammar ;
}
/∗∗∗Return cu r ren t s t a r t symbol .∗ /
public f i n a l Symbol getStar tSymbol ( ) {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GRAMMAR IV
/ / r e t u r n f r o n t ( ) . l hs . f r o n t ( ) ;/ / r e t u r n (∗ t h i s ) [ s tar tSymbol ] . l hs . f r o n t ( ) ;Symbol symb=nul l ;i f ( star tSymbol < th is . s i ze ( ) ){symb= th is . get ( s tar tSymbol ) . l hs . get ( 0 ) ;
}return symb ;
}
/∗∗∗Change s t a r t symbol by index on Vector o f r u l es .∗ /
public boolean setStar tSymbol ( f i n a l i n t index ) {i f ( index < th is . s i ze ( ) ) { / / Check boundaries .
star tSymbol= index ;genotype2phenotype ( ) ; / / Update phenotype .return true ;
}return fa lse ;
}
/∗∗∗Change s t a r t symbol by symbol p o i n t e r .∗ /
public boolean setStar tSymbol ( f i n a l Symbol newStartSymbol ) {i n t i i =0;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GRAMMAR V
I t e r a t o r <Rule> i t = th is . i t e r a t o r ( ) ;while ( i t . hasNext ( ) ) {
/ / Work by ∗p o i n t e r ∗ .i f ( i t . next ( ) . l hs . get ( 0 ) . getSymbol ( ) . compareTo ( newStartSymbol . getSymbol ( ) ) = = 0 ) {
s tar tSymbol= i i ;genotype2phenotype ( ) ; / / Update phenotype .return true ;
}i i ++;
}return fa lse ;
}
/∗∗∗Change s t a r t symbol by s t r i n g .∗ /
public boolean setStar tSymbol ( f i n a l S t r i n g newStartSymbol ) {i n t i i =0;I t e r a t o r <Rule> i t = th is . i t e r a t o r ( ) ;while ( i t . hasNext ( ) ) {
/ / Work by ∗s t r i n g ∗ .S t r i n g cur rentStar tSymbol= i t . next ( ) . l hs . get ( 0 ) . getSymbol ( ) ;i f ( cur rentStar tSymbol . compareTo ( newStartSymbol )==0) {
s tar tSymbol= i i ;genotype2phenotype ( ) ; / / Update phenotype .return true ;
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GRAMMAR VI
i i ++;}return fa lse ;
}
/∗∗∗Return p o i n t e r to cu r ren t s t a r t r u l e .∗ /
public f i n a l Rule ge tS ta r tRu le ( ) {return th is . get ( 0 ) ;
}
}/ / complete
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR I
/∗∗ CFGrammar . java∗∗ Created on Ju ly 26 , 2007 , 12:21 PM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
package GrammaticOpt imizat ion ;
/∗∗∗∗ @author a d i l r a j a∗ /
import java . u t i l .∗ ;import java . u t i l . A r r a y L i s t ;import java . u t i l . I t e r a t o r ;import java . i o .∗ ;
public abstract class CFGrammar extends Grammar {protected Tree de r i va t i onT ree ;protected abstract boolean genotype2phenotype ( ) ;protected abstract boolean phenotype2genotype ( ) ;
/∗∗∗ Creates a new ins tance of CFGrammar
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR II
∗ /public CFGrammar ( ) {
super ( ) ;}/∗∗∗Const ruc tor s e t t i n g the genotype s t r u c t u r e o f t h i s mapper to newGenotype .∗ /
public CFGrammar( f i n a l Genotype newGenotype ) {super ( newGenotype ) ;
}
/∗∗∗ Const ruc tor s e t t i n g the phenotype s t r u c t u r e o f t h i s mapper to newPhenotype .∗ /
public CFGrammar( f i n a l Phenotype newPhenotype ) {super ( newPhenotype ) ;
}
/∗∗∗ Copy Const ruc tor .∗ /
public CFGrammar( f i n a l CFGrammar copy ) {super ( copy ) ;/ / Now must go through the copied grammar , and rep lace re ferences to/ / the copy ’ s lhs symbols w i th re ferences to the new lhs symbols .I t e r a t o r r u l e I t = th is . i t e r a t o r ( ) ; / / i t e r a t o r o f RuleI t e r a t o r <Rule> p r o d I t ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR III
I t e r a t o r <Product ion > symbIt ;Rule r u l e P t r =nul l ;i n t ru leC t =0 , prodCt =0 , symbCt=0;while ( r u l e I t . hasNext ( ) ) {
p r o d I t = r u l e I t ;while ( p r o d I t . hasNext ( ) ) {
symbIt= p r o d I t . next ( ) . i t e r a t o r ( ) ;while ( symbIt . hasNext ( ) ) {
Symbol symb=symbIt . next ( ) . get ( 0 ) ;i f ( symb . getType ( ) . t o S t r i n g ( ) . compareTo ( "NTSymbol " )==0) {
t ry {r u l e P t r = f indRu le (symb ) ;
}catch ( Except ion e ) {
/ / do noth ing}
i f ( r u l e P t r ==nul l ) {/∗ Undefined NTSymbol − create new symbol ∗ /
symb=new Symbol ( symb . getSymbol ( ) , nul l ) ;}else {
/∗ Poin t to symbol ’ s d e f i n i t i o n ∗ /symb= r u l e P t r . l hs . get ( r u l e P t r . l hs . s ize () −1);
}th is . get ( ru leC t ) . get ( prodCt ) . inser tSymbol ( symb , symbCt ) ;
}symbIt . next ( ) ;symbCt++;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR IV
}p r o d I t . next ( ) ;prodCt ++;
}r u l e I t . next ( ) ;r u l eC t ++;
}/ / I n v a l i d a t e phenotype − lower c lasses i n h ie ra rchy can always c a l l/ / genotype2Phenotype ( ) to regenerate i tphenotype . c l ea r ( ) ;phenotype . se tVa l i d ( fa lse ) ;/ / Don ’ t copy d e r i v a t i o n t ree : procedure i s too complex and expensive ./ / I f t r ee i s requested , i t w i l l be regenerated .
}
/∗∗∗ Opens the f i l e whose name i s passed as an argument , reads i t s contents onto∗a charac te r s t r i n g , and c a l l s readBNFString .∗ /
public boolean readBNFFile ( f i n a l S t r i n g f i lename ) {/∗FILE ∗pF i l e ;i f ( ! ( pF i l e =fopen ( f i lename , " r " ) ) ) {
ce r r << " Could not open grammar f i l e " << f i lename << " . \ nExecut ion aborted . \ n " ;e x i t ( 1 ) ;
}fseek ( pF i le , 0 ,SEEK_END) ;i n t s i ze = f t e l l ( pF i l e ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR V
char program [ s ize + 1 ] ;s t rncpy ( program , " " , s i ze ) ;char l i n e [ 1 0 2 4 ] ;rewind ( pF i l e ) ;wh i le ( f ge t s ( l i n e ,1024 , pF i l e ) )
s t r c a t ( program , l i n e ) ;f c l o s e ( pF i l e ) ;s t r c a t ( program , " \ n " ) ;r e t u r n readBNFString ( program ) ;∗ /S t r i n g program=new S t r i n g ( ) ;
/ / t r y/ / {
/ / Open the f i l e t h a t i s the f i r s t/ / command l i n e parameter
/ / F i le Inpu tSt ream fst ream = new Fi le InputSt ream ( " a d i l . bnf " ) ;/ / Convert our i npu t stream to a/ / DataInputStream
/ / BufferedReader i n = new BufferedReader (new InputStreamReader ( fs t ream ) ) ;/ / Continue to read l i n e s whi le/ / there are s t i l l some l e f t to read
/ / DataInput s = new DataInputStream (new Fi le InputSt ream ( " ~ / mywork /GE/ libGE −0.26/EXAMPLES/ I n t e r t w i n e d S p i r a l s / GE_MITGALIB " ) ) ;/ / S t r i n g t h i s L i n e ;
/ / wh i le ( ( t h i s L i n e =s . readLine ( ) ) != n u l l )/ / {
/ / program . concat ( t h i s L i n e ) ;/ / }
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR VI
/ / s . c lose ( ) ;/ / }
/ / catch ( Except ion e )/ / {
/ / System . out . p r i n t l n ( e+" F i l e i npu t e r r o r " ) ;/ / }/ / program . concat ( " \ n " ) ;
program="<expr > : : = ( < expr > <op> <expr > ) \ n | DIV( < expr > , <expr > ) \ n | ABS( < expr > ) \ n | GT( < expr > , <expr > ) \ n | <var >\n<op> : : = + | − | ∗ \n<var > : : = X | V | ( −1.0)\n " ;
return readBNFString ( program ) ;}
/∗∗∗ Reads i n the BNF grammar s p e c i f i e d by i t s argument t e x t . Returns t rue i f∗ l oad ing o f grammar was successfu l , f a l s e otherwise .∗ /
public boolean readBNFString ( S t r i n g stream ) { / / the stream was f i n a l/ / Delete the cu r ren t grammar , i f anyth is . c l ea r ( ) ;
Rule newRule=new Rule ( 0 ) ; / / Used to create new ru l es f o r grammarboolean i n se r tRu le = fa lse ; / / I f newRule i s to be inse r t ed onto grammarRule cur ren tRu le=nul l ; / / Used i n pass 2 to add product ions to cu r ren t r u l eProduct ion newProduction=new Product ion ( 1 ) ;newProduction . c l ea r ( ) ; / / Used to create new product ions f o r grammarSymbol newSymbol=new Symbol ( ) ; / / Used to create new symbols f o r grammarSymbol newTokenSeparator=new Symbol ( ) ; / / Used to create token separators f o r grammar
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR VII
i n t stream_size=stream . leng th ( ) ;i n t i i , j j ; / / Working v a r i a b l e schar currentChar ; / / Current char o f i npu tboolean sk ip= fa lse ; / / SKip an i t e r a t i o n on parser ( f o r escaped newl ines )boolean quoted= fa lse ; / / I f cu r ren t char i s quotedboolean non_terminal= fa lse ; / / I f cu r ren t t e x t i s a non−t e rm ina l symbolchar separated =0; / / I f there was a separa tor between prev ious token and cu r ren t oneAr rayL i s t <Symbol> nonTerminals=new A r r a y L i s t ( ) ;nonTerminals . c l ea r ( ) ; / / Contains po in te r s to a l l def ined non−t e rm ina l sI t e r a t o r <Symbol> nonTermina ls I t ;S t r i n g c u r r e n t B u f f e r =new S t r i n g ( ) ; / / Bu f fe r used to add new symbols to grammar/ / States o f parserf i n a l i n t START = 0;f i n a l i n t START_RULE = 1;f i n a l i n t LHS_READ = 2;f i n a l i n t PRODUCTION = 3;f i n a l i n t START_OF_LINE = 4;i n t s ta te =START; / / Current s t a t e o f parser
/ / # de f ine libGE_DEBUG_CFGRAMMAR_PARSERstream+=" \ n " ;for ( i n t pass =1; pass <=2;pass ++){
i i = j j =0;while ( i i <=stream_size ) {
i f ( i i <stream_size ) {currentChar=stream . charAt ( i i ) ;
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR VIII
else {/ / Simulate presence of endl a t end of grammarcurrentChar= ’ \ n ’ ;
}System . out . p r i n t l n ( " I am here "+ i i ) ;i f ( stream . charAt ( i i )== ’ \ \ ’ ) { / / Escape sequence
i i ++;i f ( i i >=stream_size ) {
/ / Escape sequence as l a s t char i s i n v a l i dERROR_IN_GRAMMAR( ) ;
}else i f ( ( non_terminal )&&( stream . charAt ( i i ) ! = ’ \ n ’ ) ) {
/ / Only escaped newl ine al lowed i n s i d e non−t e rm ina lSystem . out . p r i n t l n ( "ERROR: Escape sequence detected i n s i d e non−t e rm ina l symbol . \ n " ) ;ERROR_IN_GRAMMAR( ) ;
}i f ( stream . charAt ( i i )== ’ \ ’ ’ ) { / / S ing le quote
currentChar= ’ \ ’ ’ ;}else i f ( stream . charAt ( i i )== ’ \ " ’ ) { / / Double quote
currentChar= ’ \ " ’ ;}else i f ( stream . charAt ( i i )== ’ \ \ ’ ) { / / Backslash
currentChar= ’ \ \ ’ ;}else i f ( stream . charAt ( i i )== ’ 0 ’ ) { / / Nu l l charac te r
currentChar= ’ \0 ’ ;}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR IX
/ / e lse i f ( stream . charAt ( i i )== ’a ’ ) { / / Audib le b e l l/ / currentChar = ’ \ a ’ ;
/ / }else i f ( stream . charAt ( i i )== ’ b ’ ) { / / Backspace
currentChar= ’ \ b ’ ;}else i f ( stream . charAt ( i i )== ’ f ’ ) { / / Formfeed
currentChar= ’ \ f ’ ;}else i f ( stream . charAt ( i i )== ’ n ’ ) { / / Newline
currentChar= ’ \ n ’ ;}else i f ( stream . charAt ( i i )== ’ r ’ ) { / / Carr iage r e t u r n
currentChar= ’ \ r ’ ;}else i f ( stream . charAt ( i i )== ’ t ’ ) { / / Ho r i zon ta l tab
currentChar= ’ \ t ’ ;}
/ / e lse i f ( stream [ i i ]== ’ v ’ ) { / / V e r t i c a l tab/ / currentChar = ’ \ v ’ ;
/ / }else i f ( stream . charAt ( i i )== ’ \ n ’ ) { / / Escaped newl ine
/ / Ignore newl inesk ip= true ;
}else i f ( stream . charAt ( i i )== ’ \ r ’ ) { / / Escaped DOS r e t u r n
/ / Ignore newl inesk ip= true ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR X
i f ( stream . charAt (++ i i ) ! = ’ \ n ’ ) {System . out . p r i n t l n ( "ERROR: \ \ r charac te r not fo l l owed by \ \ n . " ) ;ERROR_IN_GRAMMAR( ) ;
}}else { / / Normal charac te r
currentChar=stream . charAt ( i i ) ;}i f ( ( ! sk ip )&&(pass > 1 ) ) {
i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {newSymbol . setType ( SymbolType . TSymbol ) ;
}i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;}else {
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;c u r r e n t B u f f e r +=currentChar ;
}}
}
else {switch ( s t a t e ) {case (START ) :
i f ( currentChar== ’ \ r ’ ) {break ; / / Ignore DOS newl ine f i r s t char
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XI
switch ( currentChar ) {case ’ ’ : / / Ignore whitespacescase ’ \ t ’ : / / Ignore tabscase ’ \ n ’ : / / Ignore newl ines
break ;case ’ < ’ : / / START OF RULE
newSymbol . setType ( SymbolType . NTSymbol ) ;i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;}else {
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;c u r r e n t B u f f e r +=currentChar ;
}s t a t e =START_RULE;break ;
defaul t : / / I l l i g a lSystem . out . p r i n t l n ( "ERROR: Character "+ currentChar + " caused an e r r o r . \ n " ) ;ERROR_IN_GRAMMAR( ) ;
}break ;case (START_RULE ) : / / Read the lhs Non−t e rm ina l symbol
i f ( currentChar== ’ \ r ’ ) {break ; / / Ignore DOS newl ine f i r s t char
}switch ( currentChar ) {case ’ \ n ’ : / / Newlines are i l l i g a l here
System . out . p r i n t l n ( "ERROR: Newline i n s i d e non−t e rm ina l . " ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XII
ERROR_IN_GRAMMAR( ) ;break ;
case ’ > ’ : / / Poss ib le end of non−t e rm ina l symboli f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;}else {
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;c u r r e n t B u f f e r +=currentChar ;
}newSymbol . setSymbol ( c u r r e n t B u f f e r ) ;
/ / newSymbol . setType ( SymbolType . NTSymbol ) ; / / t h i s was added a d i li f ( pass ==1){
/ / F i r s t pass/ / Check i f new symbol d e f i n i t i o n/ / i nse r tRu le = ! ( f i ndRu le ( newSymbol ) ) ;Rule tmpRulePtr=nul l ;t ry {
tmpRulePtr= f indRu le ( newSymbol ) ;}catch ( Except ion e ) {
/ / do noth ing}i f ( tmpRulePtr==nul l )
i nse r tRu le = true ;else
i n se r tRu le = fa lse ;i f ( i nse r tRu le ) {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XIII
/ / Create new r u l e f o r symbolnewRule . c l ea r ( ) ;t ry {
newRule . l hs . add (new Symbol ( newSymbol ) ) ;}catch ( java . lang . Except ion e ) {
System . out . p r i n t l n ( e + " newRule sucks " ) ;
}/ / Add to grammart ry {
nonTerminals . add ( newRule . l hs . get ( newRule . l hs . s i ze ( ) −1) ) ;}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
System . out . p r i n t l n ( e+ " nonTerminals suck " ) ;}
}else { / / E x i s t i n g symbol , do noth ing}
}else {
/ / Second pass/ / Po in t cur ren tRu le to p rev ious l y def ined r u l et ry {
i f ( ( cur ren tRu le= f indRu le ( newSymbol ) )== nul l ) {System . out . p r i n t l n ( "ERROR: Grammar changed between parser passes . " ) ;ERROR_IN_GRAMMAR( ) ;
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XIV
}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
System . out . p r i n t l n ( e+"ERROR: Grammar changed between parser passes . " ) ;ERROR_IN_GRAMMAR( ) ;
}catch ( java . lang . Except ion e ) {
System . out . p r i n t l n ( e+"ERROR: Grammar changed between parser passes . " ) ;}
/ / i f ( ( cur ren tRu le= f indRu le ( newSymbol ) )== n u l l ) {/ / System . out . p r i n t l n ( "ERROR: Grammar changed between parser passes . " ) ;
/ / ERROR_IN_GRAMMAR( ) ;/ / }
}/ / Reset the b u f f e rc u r r e n t B u f f e r = " " ;/ / l hs f o r t h i s r u l e has been reads ta te =LHS_READ;break ;
defaul t : / / Check f o r non−escaped spec ia l charac te rsi f ( ( ( currentChar== ’ " ’ ) | | ( currentChar== ’ | ’ ) | | ( currentChar== ’< ’ ) ) ) {
System . out . p r i n t l n ( "ERROR: I n v a l i d charac te r ’ " + currentChar + " ’ i n s i d e non−t e rm ina l symbol . " ) ;ERROR_IN_GRAMMAR( ) ;
}i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;}else {
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XV
c u r r e n t B u f f e r +=currentChar ;}
}break ;case (LHS_READ ) : / / Must read : : = token
i f ( currentChar== ’ \ r ’ ) {break ; / / Ignore DOS newl ine f i r s t char
}switch ( currentChar ) {
case ’ ’ : / / Ignore whitespacescase ’ \ t ’ : / / Ignore tabscase ’ \ n ’ : / / Ignore newl ines
break ;case ’ : ’ : / / Par t o f : : = token
i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;
}else {
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;c u r r e n t B u f f e r +=currentChar ;
}break ;
case ’ = ’ : / / Should be end of : : = tokent ry {i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;}else {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XVI
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;c u r r e n t B u f f e r +=currentChar ;
}/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;
}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
System . out . p r i n t l n ( e ) ;}i f ( c u r r e n t B u f f e r . equals ( " : : = " )== fa lse ) { / / Something other than : : = was read
System . out . p r i n t l n ( "ERROR: Token ’ " + c u r r e n t B u f f e r + " ’ caused an e r r o r " ) ;ERROR_IN_GRAMMAR( ) ;
}/ / Reset the b u f f e rc u r r e n t B u f f e r = " " ;/ / START OF PRODUCTIONnewProduction . c l ea r ( ) ;s t a t e =PRODUCTION;break ;
defaul t : / / I l l i g a lSystem . out . p r i n t l n ( "ERROR: Character ’ " + currentChar + " ’ caused an e r r o r . " ) ;ERROR_IN_GRAMMAR( ) ;
}break ;
case (PRODUCTION) : / / Read every th ing u n t i l | token or \ n , or EOLi f ( currentChar== ’ \ r ’ ) {
break ; / / Ignore DOS newl ine f i r s t char}i f ( pass ==1){
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XVII
i f ( currentChar== ’ \ n ’ )s t a t e =START_OF_LINE ;
}else switch ( currentChar ) {
case ’ | ’ : / / Poss ib le end of produc t ioni f ( quoted ) { / / Normal charac te r
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;}else {
c u r r e n t B u f f e r +=currentChar ;}break ;
}case ’ \ n ’ : / / End of produc t ion ( and poss ib l y r u l e )
/ / Reset separa tor markerseparated =0;i f ( ( c u r r e n t B u f f e r . leng th ( ) ! = 0 ) | | ( newProduction . s ize ( ) ! = 0 ) ) { / / There i s a symbol to add
System . out . p r i n t l n ( "A symbol ’ " + c u r r e n t B u f f e r + " ’ " + newSymbol . getType ( ) . t o S t r i n g ( ) + " \ t " ) ;i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
/ / No symbol e x i s t s ; c reate te rm ina l empty symbolnewSymbol . setType ( SymbolType . TSymbol ) ;
}i f ( non_terminal ) { / / Current non−t e rm ina l symbol i sn ’ t f i n i s h e d
System . out . p r i n t l n ( "ERROR: Symbol ’ " + c u r r e n t B u f f e r + " ’ caused an e r r o r . " ) ;ERROR_IN_GRAMMAR( ) ;
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XVIII
newSymbol . setSymbol ( c u r r e n t B u f f e r ) ;i f ( newSymbol . getType ( ) . t o S t r i n g ( ) . equals ( "NTSymbol " ) ) {
/ / Find r u l e t h a t de f ines t h i s symbolRule tempRule=nul l ;t ry {
tempRule= f indRu le ( newSymbol ) ;}catch ( Except ion e ) {
/ / do noth ing again}i f ( tempRule != nul l ) {
newProduction . add (new Symbol ( tempRule . l hs . get ( tempRule . l hs . s ize ( ) −1 ) ) ) ;}else {
/ / Undefined symbol , i n s e r t anywaynewProduction . add (new Symbol ( newSymbol ) ) ;
}}else { / / Add te rm ina l symbol
newProduction . add (new Symbol ( newSymbol ) ) ;}
/ / Reset the symbol/ / newSymbol . c l ea r ( ) ; / / t h i s i s not requ i red , the garbage c o l l e c t o r does t h i s
newSymbol . c l ea r ( ) ;}else { / / Empty token ; ignored
;}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XIX
/ / END OF PRODUCTION/ / Add produc t ion to cu r ren t r u l et ry {
cur ren tRu le . add (new Product ion ( newProduction ) ) ;}catch ( Except ion e ) {
System . out . p r i n t l n ( e+" But the cur ren tRu le i s n u l l . . . i t sucks too " ) ;}/ / Reset the b u f f e rc u r r e n t B u f f e r = " " ;i f ( currentChar== ’ \ n ’ )
s t a t e =START_OF_LINE ;else {
/ / START OF PRODUCTIONnewProduction . c l ea r ( ) ;
}break ;
case ’ < ’ : / / Poss ib le s t a r t o f non−t e rm ina l symbolcase ’ > ’ : / / Poss ib le end of non−t e rm ina l symbolcase ’ ’ : / / Poss ib le token separa torcase ’ \ t ’ : / / Poss ib le token separa tori f ( ( quoted ) / / Normal charac te r| | ( ( ( currentChar== ’ ’ ) | | ( currentChar== ’ \ t ’ ) )&&( non_terminal ) ) ) { / / Spaces i n s i d e non−t e rm ina l s are accepted
i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;
}else {
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XX
c u r r e n t B u f f e r +=currentChar ;}i f ( ! non_terminal ) newSymbol . setType ( SymbolType . TSymbol ) ;break ;
}i f ( currentChar== ’> ’ ) { / / This i s a lso the end of a non−t e rm ina l symbol
i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;
}else {
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;c u r r e n t B u f f e r +=currentChar ;
}non_terminal= fa lse ;
}i f ( c u r r e n t B u f f e r . leng th ( ) > 0 ) {
i f ( non_terminal ) { / / Current non−t e rm ina l symbol i sn ’ t f i n i s h e dSystem . out . p r i n t l n ( "ERROR: Symbol " + c u r r e n t B u f f e r + " ’ caused an e r r o r . " ) ;ERROR_IN_GRAMMAR( ) ;
}i f ( ( currentChar== ’ ’ ) | | ( currentChar== ’ \ t ’ ) ) { / / Token separa tor
separated =1;}newSymbol . setSymbol ( c u r r e n t B u f f e r ) ;i f ( newSymbol . getType ( ) . t o S t r i n g ( ) . equals ( "NTSymbol " ) ) {
/ / Find r u l e t h a t de f ines t h i s symbolRule tempRule=nul l ;t ry {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXI
tempRule= f indRu le ( newSymbol ) ;}catch ( java . lang . Except ion e ) {
System . out . p r i n t l n ( e+" F ind ing the r u l e sucks . . . " ) ;}i f ( tempRule != nul l ) {
newProduction . add (new Symbol ( tempRule . l hs . get ( tempRule . l hs . s ize ( ) −1 ) ) ) ;}else {
/ / Undefined symbol , i n s e r t anywaynewProduction . add (new Symbol ( newSymbol ) ) ;
}}else { / / Add te rm ina l symbol
newProduction . add (new Symbol ( newSymbol ) ) ;}/ / Reset the symbol
newSymbol . c l ea r ( ) ;}else { / / Empty b u f f e r
i f ( ( ( currentChar== ’ ’ ) | | ( currentChar== ’ \ t ’ ) ) / / Token separa tor&&(newProduction . s ize ( ) > 0 ) ) {
/ / Probably a token separa tor a f t e r a non−t e rm ina l symbolseparated =1;
}}/ / Reset the b u f f e rc u r r e n t B u f f e r = " " ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXII
i f ( currentChar== ’< ’ ) { / / This i s a lso the s t a r t o f a non−t e rm ina l symbol/ / Spec ia l case ; must create new Symbol herenewSymbol . c l ea r ( ) ;newSymbol . setType ( SymbolType . NTSymbol ) ;i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;}else {
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;c u r r e n t B u f f e r +=currentChar ;
}non_terminal= true ; / / Now reading a non−t e rm ina l symboli f ( separated ! = 0 ) { / / I n s e r t a token separa tor
separated =0;newTokenSeparator=new Symbol ( ) ;newTokenSeparator . setSymbol ( " " ) ;newTokenSeparator . setType ( SymbolType . TSymbol ) ;newProduction . add (new Symbol ( newTokenSeparator ) ) ;
}}break ;defaul t : / / Add charac te r to cu r ren t b u f f e ri f ( separated ! = 0 ) { / / I n s e r t a token separa tor
separated =0;newTokenSeparator=new Symbol ( ) ;newTokenSeparator . setSymbol ( " " ) ;newTokenSeparator . setType ( SymbolType . TSymbol ) ;newProduction . add (new Symbol ( newTokenSeparator ) ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXIII
}i f ( currentChar== ’ " ’ ) { / / S t a r t ( or end ) quoted sec t ion
quoted =! quoted ;newSymbol . setType ( SymbolType . TSymbol ) ;break ;
}i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
newSymbol . setType ( SymbolType . TSymbol ) ;}i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;}else {
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;c u r r e n t B u f f e r +=currentChar ;
}}break ;case (START_OF_LINE ) :i f ( currentChar== ’ \ r ’ ) {
break ; / / Ignore DOS newl ine f i r s t char}switch ( currentChar ) {case ’ ’ : / / Ignore whitespacescase ’ \ t ’ : / / Ignore tabscase ’ \ n ’ : / / Ignore newl ines
break ;case ’ | ’ : / / S t a r t o f new produc t ion
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXIV
s ta te =PRODUCTION;i f ( pass ==2){
/ / START OF PRODUCTIONnewProduction . c l ea r ( ) ;
}break ;
case ’ < ’ : / / S t a r t o f l hs non−t e rm ina l symbol
/ / END OF RULEi f ( pass ==1){
/ / Add cu r ren t r u l ei f ( i nse r tRu le ) {
t ry {th is . add (new Rule ( newRule ) ) ;
}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
System . out . p r i n t l n ( e ) ;}
}}/ / START OF RULEnewSymbol . setType ( SymbolType . NTSymbol ) ;i f ( c u r r e n t B u f f e r . leng th ( ) = = 0 ) {
c u r r e n t B u f f e r =Character . t o S t r i n g ( currentChar ) ;}else {
/ / c u r r e n t B u f f e r . concat ( Character . t o S t r i n g ( currentChar ) ) ;c u r r e n t B u f f e r +=currentChar ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXV
}s t a t e =START_RULE;break ;
defaul t : / / I l l i g a lSystem . out . p r i n t l n ( "ERROR: Detected l i n e s t a r t i n g w i th te rm ina l symbol . " ) ;ERROR_IN_GRAMMAR( ) ;
}break ;
defaul t : / / Imposs ib le e r ro r , q u i t the program now !System . out . p r i n t l n ( " I n t e r n a l e r r o r . \ nPlease r e p o r t e r r o r ’ Imposs ib le s ta te i n CFGrammar parser ’ to . . . " ) ;System . e x i t ( 0 ) ;
}}
sk ip= fa lse ;i i ++;
}/ / END OF PASSi f ( s t a t e !=START_OF_LINE ) { / / This must be the s ta te o f the parser
System . out . p r i n t l n ( " Parsing e r r o r reading grammar . " ) ;ERROR_IN_GRAMMAR( ) ;
}i f ( pass ==1){
/ / Add cu r ren t r u l ei f ( i nse r tRu le ) {
th is . add (new Rule ( newRule ) ) ;}
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXVI
}
updateRuleFie lds ( ) ;setValidGrammar ( true ) ;genotype2phenotype ( ) ;return true ;}
/∗∗∗ Reads i n the BNF grammar−pa r t s p e c i f i e d by i t s argument tex t , and adds i t to∗the cu r ren t grammar . Returns t rue i f load ing o f new grammar pa r t was∗successfu l , f a l s e otherwise .∗ /
public boolean addBNFString ( f i n a l S t r i n g stream ) {/ / FIXME Slow implementat ion .
/ / Copy cu r ren t grammarAr rayL i s t <Rule> grammarCopy= th is ;/ / Create appended grammar s t r i n gS t r i n g newGrammarStream=outputBNF ( ) ;S t r i n g newGrammar=new S t r i n g ( newGrammarStream ) ;newGrammar=stream ;/ / Read new grammart ry {
i f ( ! readBNFString (newGrammar ) ) {/ / Restore prev ious s ta te o f MapperreadBNFString ( newGrammarStream ) ;return fa lse ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXVII
}return true ;
}catch ( java . lang . Except ion e ) {
return fa lse ;}
}
/∗∗∗ P r e t t y p r i n t the cu r ren t BNF grammar .∗ /
public S t r i n g outputBNF ( ) {S t r i n g stream=nul l ;i f ( ! getValidGrammar ( ) ) {
return nul l ;}I t e r a t o r <Rule> r _ i t = th is . i t e r a t o r ( ) ;while ( r _ i t . hasNext ( ) ) {
Rule tmp ;tmp= r _ i t . next ( ) ;stream . concat ( tmp . lhs . get ( tmp . lhs . s i ze () −1). getSymbol ( ) + " : : = " ) ;/ / go through product ionsI t e r a t o r <Product ion > p _ i t =tmp . i t e r a t o r ( ) ;while ( p _ i t . hasNext ( ) ) {
/ / go through symbolsI t e r a t o r <Symbol> s _ i t = p _ i t . next ( ) . i t e r a t o r ( ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXVIII
while ( s _ i t . hasNext ( ) ) {stream . concat ( s _ i t . next ( ) . getSymbol ( ) ) ;
}stream . concat ( " | " ) ;
}stream=stream . t r i m ( ) ;stream . concat ( " \ n " ) ;/ / get next r u l e
}return stream ;
}
/∗∗∗ Returns the address o f the r u l e d e f i n i n g the argument non−t e rm ina l symbol ,∗ i f i t e x i s t s ; o therwise re tu rns NULL .∗ /
public Rule f indRu le ( f i n a l Symbol nontermina l ) throws Except ion {
I t e r a t o r <Rule> r u l e I t = th is . i t e r a t o r ( ) ;Rule tmp ;while ( r u l e I t . hasNext ( ) ) {
tmp= r u l e I t . next ( ) ;i f ( tmp . lhs . get ( tmp . lhs . s i ze () −1).makeComparison ( nontermina l ) )
return tmp ;}return nul l ;
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXIX
/∗∗∗Returns the ca l cu la ted recu rs i ve nature o f the Rule passed as argument ,∗and updates i t s minimum mapping depth ( minimumDepth )∗ /
private boolean i sRecurs ive ( A r rayL i s t <Symbol> v i s i t edRu les , Rule cur ren tRu le ) {I t e r a t o r <Product ion > p r o d I t =cur ren tRu le . i t e r a t o r ( ) ;I t e r a t o r <Symbol> symbIt ;I t e r a t o r <Symbol> v i s i t e d R u l e s I t = v i s i t e dRu l es . i t e r a t o r ( ) ;Rule r u l e P t r =nul l ;boolean r e s u l t ;
/ / DON ’T DO IT − otherwise minimumDepth i s not updated p rope r l y/ / I f t h i s r u l e has a l ready been labeled , e x i t/ / i f ( currentRule−>getRecurs ive ( ) ! = SI_Undef ) {/ / r e t u r n currentRule−>getRecurs ive ( ) ;/ / }
/ / Check i f t h i s i s a recu rs i ve c a l l to a p rev ious l y v i s i t e d r u l ewhile ( v i s i t e d R u l e s I t . hasNext ( ) ) {
i f ( v i s i t e d R u l e s I t . next ( ) . makeComparison ( cur ren tRu le . l hs . get ( cur ren tRu le . l hs . s ize ( ) −1) ) ) {cur ren tRu le . setRecurs ive ( true ) ;return true ;
}}
/ / Go through each produc t ion i n the r u l ewhile ( p r o d I t . hasNext ( ) ) {
Product ion tmp_pd= p r o d I t . next ( ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXX
tmp_pd . setMinimumDepth ( 0 ) ;symbIt=tmp_pd . i t e r a t o r ( ) ;/ / Go through each symbol i n the produc t ionwhile ( symbIt . hasNext ( ) ) {
Symbol tmp_symb=symbIt . next ( ) ;i f ( tmp_symb . getType ( ) . t o S t r i n g ( ) . equals ( "NTSymbol " ) ) { / / Symbol i s non−te rm ina l , poss ib le recu rs ion
t ry {r u l e P t r = f indRu le ( tmp_symb ) ; / / Find d e f i n i n g r u l e
}catch ( java . lang . Except ion e ) {
/ / do noth ing}i f ( r u l e P t r != nul l ) { / / Symbol i s def ined
/ / Recursive c a l lv i s i t ed Ru le s . add ( cur ren tRu le . l hs . get ( cur ren tRu le . l hs . s ize ( ) −1) ) ;r e s u l t = isRecurs ive ( v i s i t edRu les , r u l e P t r ) ;v i s i t ed Ru le s . remove ( v i s i t e dRu le s . s ize () −1);i f ( r e s u l t ) { / / Product ion i s recu rs i ve
/ / Mark produc t ion as recu rs i vetmp_pd . setRecurs ive ( true ) ;/ / Mark cu r ren t r u l e a lso as recu rs i vecur ren tRu le . setRecurs ive ( true ) ;
}i f ( tmp_pd . getMinimumDepth ( ) < ( r u l e P t r . getMinimumDepth ( ) + 1 ) ) {
tmp_pd . setMinimumDepth ( r u l e P t r . getMinimumDepth ( ) + 1 ) ;}
}else { / / Non−def ined non−t e rm ina l symbols are considered te rm ina l symbols
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXXI
i f ( tmp_pd . getMinimumDepth ( ) < 1 ) {tmp_pd . setMinimumDepth ( 1 ) ;
}}
}else { / / Terminal symbol
i f ( tmp_pd . getMinimumDepth ( ) < 1 ) {tmp_pd . setMinimumDepth ( 1 ) ;
}}
}/ / i f ( p rod I t−>getRecurs ive ( )== SI_Undef ) { / / Product ion i s not recu rs i ve/ / p rod I t−>setRecurs ive ( SI_NonRec ) ;/ / }i f ( cur ren tRu le . getMinimumDepth () > tmp_pd . getMinimumDepth ( ) ) { / / Update r u l e minimumDepth
cur ren tRu le . setMinimumDepth ( tmp_pd . getMinimumDepth ( ) ) ;}/ / p r o d I t ++;
}/ / i f ( currentRule−>getRecurs ive ( ) ! = SI_Rec ) { / / Rule i s not recurs ive , as i t con ta ins no recu rs i ve produc t ions/ / currentRule−>setRecurs ive ( SI_NonRec ) ;/ / r e t u r n SI_NonRec ;/ / }/ / r e t u r n SI_Rec ;return cur ren tRu le . getRecurs ive ( ) ;
}
/∗∗
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXXII
∗ Update recu rs i ve and minimumDepth f i e l d s f o r every Rule∗ /
/ / and Product ion i n grammar .private void updateRuleFie lds ( ) {
A r rayL i s t <Symbol> v i s i t ed Ru le s =new A r r a y L i s t ( ) ;I t e r a t o r <Rule> r u l e I t = th is . i t e r a t o r ( ) ;
c l ea rRu leF ie lds ( ) ;
/ / Go through each r u l e i n the grammarwhile ( r u l e I t . hasNext ( ) ) {
t ry {v i s i t ed Ru le s . c l ea r ( ) ;
}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
System . out . p r i n t l n ( e+" UpdateRuleFields sucks " ) ;}
Rule tmp_rule= r u l e I t . next ( ) ;tmp_rule . setRecurs ive ( isRecurs ive ( v i s i t edRu les , tmp_rule ) ) ;
}/ / Second pass , to c o r r e c t l y update a l l r ecu rs i ve ru l esr u l e I t = th is . i t e r a t o r ( ) ;while ( r u l e I t . hasNext ( ) ) {
t ry {v i s i t ed Ru les . c l ea r ( ) ;
}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXXIII
System . out . p r i n t l n ( e+" UpdateRuleFields sucks " ) ;}
Rule tmp_rule= r u l e I t . next ( ) ;tmp_rule . setRecurs ive ( isRecurs ive ( v i s i t edRu les , tmp_rule ) ) ;
}}
/∗∗∗ Update recu rs i ve and minimumDepth f i e l d s f o r every Rule∗ /
/ / and Product ion i n grammar .private void c lea rRu leF ie lds ( ) {
I t e r a t o r <Rule> r u l e I t = th is . i t e r a t o r ( ) ;/ / Reset minimum depths and recurs ion f i e l d swhile ( r u l e I t . hasNext ( ) ) {
Rule tmp_rule= r u l e I t . next ( ) ;tmp_rule . setMinimumDepth (32767 > >1);tmp_rule . setRecurs ive ( fa lse ) ;
}}
/ / ERROR FUNCTIONprivate boolean ERROR_IN_GRAMMAR( ) {
System . out . p r i n t l n ( "Grammar not loaded ; " ) ;t ry {
th is . c l ea r ( ) ;}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
CONTEXT-FREE GRAMMAR XXXIV
System . out . p r i n t l n ( e ) ;}
setValidGrammar ( fa lse ) ;t ry {
phenotype . c l ea r ( ) ;phenotype . se tVa l i d ( fa lse ) ;
}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
System . out . p r i n t l n ( e ) ;}
return fa lse ;
}}/ / F i r s t implementat ion i s complete , t h i s f i l e may pose a l o t o f bugs .
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR I
/∗∗ GEGrammar . java∗∗ Created on August 2 , 2007 , 12:22 AM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
package GrammaticOpt imizat ion ;
/∗∗∗∗ @author a d i l r a j a∗ /
import java . u t i l .∗ ;import java . u t i l . L i s t I t e r a t o r ;
public class GEGrammar extends CFGrammar {
private i n t maxWraps ;protected Ar rayL i s t <Product ion > produc t ions ;
/∗∗∗ Creates a new ins tance of GEGrammar∗ /
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR II
public GEGrammar ( ) {super ( ) ;setMaxWraps ( 0 ) ;
}
/∗∗∗Const ruc tor s e t t i n g the genotype s t r u c t u r e o f t h i s mapper to newGenotype .∗ /
public GEGrammar( f i n a l Genotype newGenotype ) {super ( newGenotype ) ;setMaxWraps ( 0 ) ;p roduct ions=new A r r a y L i s t ( ) ;
}
/∗∗∗Const ruc tor s e t t i n g the phenotype s t r u c t u r e o f t h i s mapper to newPhenotype .∗ /
public GEGrammar( f i n a l Phenotype newPhenotype ) {super ( newPhenotype ) ;setMaxWraps ( 0 ) ;
}/∗∗∗ Copy Const ruc tor .∗ /
public GEGrammar( f i n a l GEGrammar copy ) throws Except ion {super ( copy ) ;setMaxWraps ( copy . getMaxWraps ( ) ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR III
/ / Ca l l genotype2Phenotype ( ) to regenerate phenotype s t ruc tu re ,/ / and product ions and de r i va t i onT ree s t r u c t u r e s .genotype2phenotype ( true ) ;
}
/∗∗∗ Return number o f maximum al lowed wrapping events .∗ /
public f i n a l i n t getMaxWraps ( ) {return maxWraps ;
}
/∗∗∗ Set the new number o f maximum al lowed wrapping events .∗ /
public void setMaxWraps ( f i n a l i n t newMaxWraps ) {maxWraps=newMaxWraps ;
}
/∗∗∗ Bu i lds the the cu r ren t d e r i v a t i o n t ree , and re tu rns i t s address ;∗ i f d e r i v a t i o n t ree i s imposs ib le to bu i ld , r e tu rns NULL .∗ /
public f i n a l Tree getTree ( ) throws Except ion {i f ( ( ! getValidGrammar ( ) ) | | ( ! genotype . ge tVa l i d ( ) ) | | ( getGenotype ( ) . s i ze ( ) ! = 0 ) ) {
return nul l ;}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR IV
genotype2phenotype ( true ) ;return th is . de r i va t i onT ree ;
}
/∗∗∗ Returns a vec to r o f a l l p roduct ions used dur ing the mapping process .∗ /
public f i n a l Ar rayL i s t <Product ion > getProduct ions ( ) throws Except ion {i f ( ( ! getValidGrammar ( ) ) | | ( ! genotype . ge tVa l i d ( ) ) | | ( getGenotype ( ) . s i ze ( ) ! = 0 ) ) {
return nul l ;}genotype2phenotype ( true ) ;return produc t ions ;
}
/∗∗∗ S t r i c t implementat ion o f genotype2Phenotype i n t e r f a c e . Ca l l s l o c a l∗ genotype2phenotype ( bool ) method , w i th bool= f a l s e .∗ /
public boolean genotype2phenotype ( ) {
/ / t r y {return genotype2phenotype ( fa lse ) ;
/ / }/ / catch ( Except ion e ) {
/ / System . out . p r i n t l n ( e+" genotype2phenotype ( ) " ) ;/ / r e t u r n f a l s e ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR V
/ / }}
/∗∗∗ Updates the contents o f the phenotype s t ruc tu re , based on the cu r ren t∗ genotype and the cu r ren t grammar , and according to the standard GE∗ mapping process . Returns t rue upon a success fu l mapping , and f a l s e∗ otherwise , and also updates the v a l i d f i e l d o f the phenotype .∗ With argument set to t rue , a lso updates de r i va t i onT ree .∗ /
public boolean genotype2phenotype ( f i n a l boolean bu i l dDe r i va t i onT ree ) {th is . de r i va t i onT ree =new Tree ( ) ; / / I n i t i a l i z e the t reeboolean re tu rnVa lue= true ;i n t newEf fec t iveSize =0;/ / S t a r t by s e t t i n g e f f e c t i v e S i z e to 0genotype . s e t E f f e c t i v e S i z e ( newEf fec t iveSize ) ;
th is . phenotype=new Phenotype ( true , 1 ) ;i f ( ! phenotype . isEmpty ( ) )
phenotype . c l ea r ( ) ;
i f ( bu i l dDe r i va t i onT ree ) {th is . p roduc t ions . c l ea r ( ) ;
}/ / Quick sa fe t y checks/ / i f ( ( ! getValidGrammar ( ) ) | | ( ! genotype . ge tVa l i d ( ) ) | | ( ! getGenotype()−> s ize ( ) ) ) {i f ( ! getValidGrammar ( ) ) {
phenotype . c l ea r ( ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR VI
phenotype . se tVa l i d ( fa lse ) ;return fa lse ;
}
/ / Wraps counter and nontermina ls stacki n t wraps =0;Stack <Symbol> nontermina ls=new Stack ( ) ;
/ / I t e r a t o r sI t e r a t o r <Rule> r u l e I t ;L i s t I t e r a t o r <Product ion > p r o d I t ;I t e r a t o r < In teger > genoI t=genotype . i t e r a t o r ( ) ;I n tege r tmpIn t ; / / s h a l l con ta in the values i n Genotype
/ / S t a r t w i th the s t a r t symbolnontermina ls . push ( getStar tSymbol ( ) ) ;i f ( bu i l dDe r i va t i onT ree ) {
/ / Use s t a r t symbol as the de r i va t i onT ree nodet ry {
de r i va t i onT ree . setData ( getStar tSymbol ( ) ) ;}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
System . out . p r i n t l n ( e+" The c a l l : de r i va t i onT ree . setData ( getStar tSymbol ( ) ) ; i n GEGrammar sucks " ) ;}
}
boolean gotToUseWrap= true ;I n t ege r codonGenoIt=new I n t ege r ( genoI t . next ( ) . i n tVa lue ( ) ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR VII
/ / Get r i d o f a l l non−t e rm ina l symbolswhile ( ( ! nontermina ls . empty ( ) )&&( wraps<=getMaxWraps ( ) ) ) {
/ / Do a mapping stepswitch ( genotype2phenotypeStep ( nonterminals , codonGenoIt , bu i l dDe r i va t i onT ree ) ) {
case −1: re turnVa lue= fa lse ;break ;
case 0: ;break ;
case 1: codonGenoIt=new I n t ege r ( genoI t . next ( ) . i n tVa lue ( ) ) ;/ / genoI t ++;newEf fec t iveSize ++;i f ( gotToUseWrap ) {
wraps ++;gotToUseWrap= fa lse ;
}/ / Check i f wrap i s neededi f ( ! genoI t . hasNext ( ) ) {
/ / newEf fec t iveSize+=genotype . s ize ( ) ;genoI t=genotype . i t e r a t o r ( ) ;codonGenoIt=new I n t ege r ( genoI t . next ( ) . i n tVa lue ( ) ) ;gotToUseWrap= true ;
}break ;
defaul t : System . out . p r i n t l n ( " I n t e r n a l e r r o r i n genotype2Phenotype ( ) " ) ;System . out . p r i n t l n ( " Execut ion aborted . " ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR VIII
System . e x i t ( 0 ) ;}
}/ / newEf fec t iveSize +=( genoIt−genotype . begin ( ) ) ;/ / Was the mapping success fu l ?i f ( ( wraps>getMaxWraps ( ) ) | | ( ! nontermina ls . empty ( ) ) ) {
re tu rnVa lue= fa lse ;/ / Add remaining symbols i n nontermina ls queue to phenotypewhile ( ! nontermina ls . empty ( ) ) {
phenotype . add ( nontermina ls . pop ( ) ) ;}
}phenotype . se tVa l i d ( re turnVa lue ) ;genotype . s e t E f f e c t i v e S i z e ( newEf fec t iveSize ) ;genotype . setWraps ( wraps ) ;/ / Now b u i l d d e r i v a t i o n t ree , based on product ions vec to ri f ( bu i l dDe r i va t i onT ree ) {
de r i va t i onT ree . c l ea r ( ) ;de r i va t i onT ree . setData ( getStar tSymbol ( ) ) ;de r i va t i onT ree . se tCur ren tLeve l ( 1 ) ;de r i va t i onT ree . setDepth ( 1 ) ;p r o d I t =produc t ions . l i s t I t e r a t o r ( ) ; / / something wrong herebui ldDTree ( der i va t ionTree , p r o d I t ) ;
}return re tu rnVa lue ;
}
/∗∗
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR IX
∗ Updates the contents o f the genotype s t ruc tu re , based on the cu r ren t∗ phenotype and the cu r ren t grammar , and according to a mapping process∗ corresponding to the inverse o f the standard GE mapping process .∗ Returns t rue upon a success fu l inverse mapping , and f a l s e otherwise .∗ /
public boolean phenotype2genotype ( ) {return fa lse ;
/ / FIXME}
/∗∗∗ Performs one step of the mapping process , t h a t is , maps the next∗non−t e rm ina l symbol on the nontermina ls stack passed as argument , using the∗codon at the p o s i t i o n po in ted by genoI t .∗Returns number o f codons consumed , −1 i f not success fu l∗ /
public i n t genotype2phenotypeStep ( Stack <Symbol> nonterminals , I n tege r codonGenoIt , boolean bu i l dDe r i va t i onT ree ) {
L i s t I t e r a t o r <Product ion > p r o d I t ;i n t re tu rnVa lue=−1;
/ / Find the r u l e f o r the cu r ren t non−t e rm ina lRule r u l e P t r =nul l ;t ry {
r u l e P t r = f indRu le ( nontermina ls . peek ( ) ) ;}catch ( java . lang . Except ion e ) {
System . out . p r i n t l n ( e+" genotype2phenotypeStep ( ) s r u l e P t r sucks " ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR X
}/ / ce r r << " mapping " << ∗( nontermina ls . top ( ) ) << " w i th " << ∗genoI t << " \ n " ;
i f ( r u l e P t r ==nul l ) { / / Undefined symbol − could be an extens ion symbolt ry {
i f ( nontermina ls . peek ( ) . getSymbol ( ) . s t a r t s W i t h ( "<GECodonValue " )&&( codonGenoIt != nul l ) ) {/ / I n s e r t codon value/ / Ex t rac t range f o r value from non−t e rm ina l s p e c i f i c a t i o ni n t low =0 , high=−1, p o i n t e r = "<GECodonValue " . leng th ( ) ;/ / currentChar i s the f i r s t charac te r a f t e r "<GECodonValue "char currentChar=nontermina ls . peek ( ) . getSymbol ( ) . subs t r i ng ( po in te r , p o i n t e r +1 ) . charAt ( 0 ) ;
/ / Look f o r range d e f i n i t i o n swhile ( Character . t o S t r i n g ( currentChar ) . compareTo ( "> " ) ! = 0 ) {
i f ( Character . t o S t r i n g ( currentChar ) . compareTo ( "−" )==0) {/ / Low range s p e c i f i c a t i o ncurrentChar=nontermina ls . peek ( ) . getSymbol ( ) . subs t r i ng (++ po in te r , p o i n t e r +1 ) . charAt ( 0 ) ;while ( Character . i s D i g i t ( currentChar ) ) {
low =( low∗10)+( currentChar− ’ 0 ’ ) ;
currentChar=nontermina ls . peek ( ) . getSymbol ( ) . subs t r i ng (++ po in te r , p o i n t e r +1 ) . charAt ( 0 ) ;}
}else i f ( Character . t o S t r i n g ( currentChar ) . compareTo ( "+ " )==0) {
/ / High range s p e c i f i c a t i o ncurrentChar=nontermina ls . peek ( ) . t o S t r i n g ( ) . subs t r i ng (++ po in te r , p o i n t e r +1 ) . charAt ( 0 ) ;while ( Character . i s D i g i t ( currentChar ) ) {
i f ( h igh==−1){
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR XI
high =0;}high =( high∗10)+( currentChar− ’ 0 ’ ) ;currentChar=nontermina ls . peek ( ) . t o S t r i n g ( ) . subs t r i ng (++ po in te r , p o i n t e r +1 ) . charAt ( 0 ) ;
}}else { / / Ignore e r r o r s
currentChar=nontermina ls . peek ( ) . t o S t r i n g ( ) . subs t r i ng (++ po in te r , p o i n t e r +1 ) . charAt ( 0 ) ;}
}/ / High range was not spec i f i ed , so set i t to maximumi f ( h igh==−1){
high=genotype . getMaxCodonValue ( ) ;}/ / Remove non−t e rm ina lnontermina ls . pop ( ) ;/ / P r i n t value onto " codon "S t r i n g codon ;i f ( h igh==low ) {
/ / Catch d i v i s i o n by zerocodon= In tege r . t o S t r i n g ( low ) ;
}else {
codon= In tege r . t o S t r i n g ( codonGenoIt . i n tVa lue ()%( high−low +1)+ low ) ;}/ / I n s e r t symbol w i th value onto phenotypephenotype . add (new Symbol ( codon , nul l ) ) ;re tu rnVa lue =1;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR XII
}else {
/ / Unknown symbol or spec ia l symbol t h a t requ i res non−empty genotype/ / Inc lude symbol on phenotypephenotype . add ( nontermina ls . pop ( ) ) ;/ / Remove non−t e rm ina l/ / nontermina ls . pop ( ) ;/ / I n v a l i d a t e mappingre tu rnVa lue=−1;
}}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
System . out . p r i n t l n ( e ) ;}
}
/ / e lse i f ( r u l e P t r−>getMinimumDepth () >=INT_MAX> >1 ) { / / Stuck on recu rs i ve r u l e/ / Al low recu rs i ve ru les , but on ly i f they consume a codonelse i f ( ( r u l e P t r . getMinimumDepth () >=(32767 > >1)) / / Stuck on recu rs i ve r u l e
&&( r u l e P t r . s i ze ( ) < = 1 ) ) { / / No codon w i l l be consumed/ / Inc lude symbol on phenotypephenotype . add ( nontermina ls . pop ( ) ) ;/ / Remove non−t e rm ina l/ / nontermina ls . pop ( ) ;/ / I n v a l i d a t e mappingre tu rnVa lue=−1;
}else {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR XIII
/ / Remove non−t e rm ina lnontermina ls . pop ( ) ;/ / Choose produc t ioni f ( codonGenoIt==nul l&&r u l e P t r . s i ze ( ) > 1 ) {
/ / Empty genotype , but symbol requ i res choice/ / Inc lude symbol on phenotypephenotype . add ( r u l e P t r . l hs . get ( 0 ) ) ;/ / I n v a l i d a t e mappingre tu rnVa lue=−1;
}else {
i f ( codonGenoIt==nul l ) { / / Empty genotypep r o d I t = r u l e P t r . l i s t I t e r a t o r ( 0 ) ;
}else {
p r o d I t = r u l e P t r . l i s t I t e r a t o r ( 0 ) ;i n t tmp=0;tmp=codonGenoIt . i n tVa lue ()% r u l e P t r . s i ze ( ) ;for ( i n t i =0; i <tmp ; i ++)
p r o d I t . next ( ) ;/ / i f ( p r o d I t . hasPrevious ( ) )
/ / p r o d I t . prev ious ( ) ;}/ / Place produc t ion on product ions vec to rProduct ion tmpProd= p r o d I t . next ( ) ;i f ( bu i l dDe r i va t i onT ree ) {
produc t ions . add ( tmpProd ) ; / / tmpProd i s a lso used l a t t e r}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR XIV
/ / Put a l l t e rm ina l symbols a t s t a r t o f p roduc t ion onto phenotypei n t s _ s t a r t =0;i n t s_stop =0;t ry {
s_stop=tmpProd . s ize ( ) ; / / Ins tead of p r o d I t . s i ze ( ) ;}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
System . out . p r i n t l n ( e+" tmpProd i s n u l l i n GEGrammar" ) ;}
while ( ( s_s ta r t <s_stop)&&tmpProd . get ( s _ s t a r t ) . getType ( ) . t o S t r i n g ( ) . compareTo ( " TSymbol " )==0) {phenotype . add ( tmpProd . get ( s _ s t a r t + + ) ) ;/ / s _ s t a r t ++;
}/ / Push a l l remaining symbols from produc t ion onto nontermina ls queue , backwardsfor ( ; s_stop > s _ s t a r t ; s_stop−−){
nontermina ls . push ( tmpProd . get ( s_stop −1));}/ / 0 or 1 choice f o r cu r ren t ru le , didn ’ t consume genotype codoni f ( r u l e P t r . s i ze ( ) <=1) {
re turnVa lue =0;}else {
re tu rnVa lue =1;}
}}i f ( phenotype==nul l ) System . out . p r i n t l n ( " Phenotype i s messy " ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR XV
/ / F i n a l l y , pop a l l t e rm ina l symbols on top of s tack and i n s e r t onto phenotype/ / t r y {
while ( true ) {t ry {
i f ( ( nontermina ls . empty ( ) ) | | ( ! nontermina ls . peek ( ) . getType ( ) . t o S t r i n g ( ) . equalsIgnoreCase ( " TSymbol " ) ) ) {break ;
}phenotype . add ( nontermina ls . pop ( ) ) ;
}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {System . out . p r i n t l n ( e+" The wh i le loop at the end of genotype2phenotypeStep sucks " ) ;System . e x i t ( 0 ) ;
}catch ( java . u t i l . EmptyStackException e ) {
System . out . p r i n t l n ( e+" GEGrammar sucks . . . Stack i s empty " ) ;System . e x i t ( 0 ) ;
}}
/ / }
/ / catch ( java . lang . Nu l lPo in te rExcep t ion e ) {/ / System . out . p r i n t l n ( e+" The wh i le loop at the end of genotype2phenotypeStep sucks " ) ;
/ / }
return re tu rnVa lue ;}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR XVI
/∗∗∗ Bu i lds the d e r i v a t i o n t ree , based on the product ions vec to r .∗Arguments are cu r ren t t r ee node , and i t e r a t o r on produc t ions vec to r .∗ /
public void bui ldDTree ( Tree currentNode , L i s t I t e r a t o r <Product ion > p r o d I t ) {
/ / I f cu r ren t symbol i s not a non−te rm ina l , or i f a l l p roduct ions have been t rea tedi f ( currentNode . getData ( ) . getType ( ) . t o S t r i n g ( ) . compareTo ( "NTSymbol " ) ! = 0 | | ! p r o d I t . hasNext ( ) ) {
/ / Cor rec t product ions i t e r a t o r , as no produc t ion was read from i tp r o d I t . prev ious ( ) ;return ;
}/ / Create new t ree l e v e lI t e r a t o r <Symbol> symbIt= p r o d I t . next ( ) . i t e r a t o r ( ) ; ;while ( symbIt . hasNext ( ) ) {
currentNode . add (new Tree ( symbIt . next ( ) , currentNode . getCur rentLeve l ( )+1 , currentNode . getDepth ( ) + 1 ) ) ;/ / symbIt ++;}
/ / Expand each c h i l d nodeI t e r a t o r <Tree> t r e e I t =currentNode . i t e r a t o r ( ) ;while ( t r e e I t . hasNext ( ) ) {
t ry {
bui ldDTree ( t r e e I t . next ( ) , p r o d I t ) ;/ / p r o d I t . next ( ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
GE GRAMMAR XVII
}catch ( java . u t i l . NoSuchElementException e ) {
System . out . p r i n t l n ( e+" The l a s t l i n e s o f bu i ldTree suck " ) ;System . e x i t ( 0 ) ;
}
/ / t r e e I t ++;}
}
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
BACKUS NAUR FORM I
<expr > : : = mymul( < expr > , <expr >) | mysub( < expr > , <expr >) | myadd( < expr > , <expr >) | pd iv ( < expr > , <expr >) | i f_cond ( < expr > , <expr > , <expr > , <expr >)| ( < expr >)| s in ( < expr >) | cos ( < expr >) | tan ( < expr >) | exp( < expr >)| x | y | 1.000 | <const> | −<const>
<const> : : = 0. < d i g i t >< d i g i t >< d i g i t >< d i g i t > : : = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
PRODUCTION I
/∗∗ Product ion . java∗∗ Created on June 3 , 2007 , 12:24 AM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
/∗∗∗∗ @author a d i l r a j a∗ /
package GrammaticOpt imizat ion ;
import java . u t i l .∗ ;import java . u t i l . A r r a y L i s t ;import java . u t i l . I t e r a t o r ;
public class Product ion extends Ar rayL i s t <Symbol >{
private boolean r ecu rs i ve ;private i n t minimumDepth ;/∗∗
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
PRODUCTION II
∗Creates a new produc t ion w i th newLength elements∗ /
public Product ion ( f i n a l i n t newLength ) {super ( ) ;th is . ensureCapacity ( newLength ) ;setRecurs ive ( fa lse ) ;setMinimumDepth (32767 > >1);
}/∗∗∗Copy cons t ruc to r ; copy a l l symbols∗ /
public Product ion ( f i n a l Product ion copy ) {I t e r a t o r <Symbol> i t r = copy . i t e r a t o r ( ) ;while ( i t r . hasNext ( ) ) {
Symbol tmpsymb= i t r . next ( ) ;i f ( tmpsymb . getType ( ) . t o S t r i n g ( ) . compareTo ( " TSymbol " )==0) {
th is . add (new Symbol ( tmpsymb ) ) ; / / Terminal symbol , c reate new one}else
th is . add ( tmpsymb ) ;}setRecurs ive ( copy . recu rs i ve ) ;setMinimumDepth ( copy . minimumDepth ) ;
}/∗∗∗Delete a l l t e rm ina l symbols and a l l symbol re ferences∗ /
public void c learTermina ls ( ) {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
PRODUCTION III
I t e r a t o r <Symbol> i t r = th is . i t e r a t o r ( ) ;while ( i t r . hasNext ( ) ) {
Symbol tmpsymb= i t r . next ( ) ;i f ( tmpsymb . getType ( ) . t o S t r i n g ( ) . compareTo ( " TSymbol " )==0) {
th is . remove ( tmpsymb ) ;}
}}
/∗∗∗Return the recu rs i ve nature o f t h i s produc t ion∗ /
public f i n a l boolean getRecurs ive ( ) {return th is . r ecu rs i ve ;
}
/∗∗∗Update the recu rs i ve nature o f t h i s produc t ion∗ /
public void setRecurs ive ( f i n a l boolean newRecursive ) {th is . r ecu rs i ve =newRecursive ;
}/∗∗∗Return the minimum mapping depth o f t h i s produc t ion∗ /
public f i n a l i n t getMinimumDepth ( ) {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
PRODUCTION IV
return th is . minimumDepth ;}
/∗∗∗Update the minimum mapping depth o f t h i s produc t ion∗ /
public void setMinimumDepth ( f i n a l i n t newMinimumDepth ) {th is . minimumDepth=newMinimumDepth ;
}
/∗∗∗P r i n t the contents o f t h i s produc t ion∗ /
/∗∗∗This removes and adds a symbol a t a p a r t i c u l a r l o c a t i o n i n t h i s ob jec t . To be c a l l e d l a t t e r from CFGrammar c lass .∗ /
public void inser tSymbol ( f i n a l Symbol symb , i n t pos ) {th is . remove ( pos ) ;th is . add ( pos , symb ) ;
}
}/ / f i l e complete
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
RULE I
/∗∗ Rule . java∗∗ Created on June 21 , 2007 , 12:06 PM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
package GrammaticOpt imizat ion ;/∗∗∗∗ @author a d i l r a j a∗ /
import java . u t i l .∗ ;import java . u t i l . A r r a y L i s t ; / / The l e f t−hand s ide o f the r u l e
public class Rule extends Ar rayL i s t <Product ion >{private boolean r ecu rs i ve ; / / Recursive nature o f r u l eprivate i n t minimumDepth ; / / Minimum depth o f parse t ree f o r produc t ion to map to te rm ina l symbol ( s )public Ar rayL i s t <Symbol> lhs ;/∗∗∗Creates a new r u l e w i th newLength elements .∗ /
public Rule ( f i n a l i n t newLength ) {super ( newLength ) ;th is . ensureCapacity ( newLength ) ;setRecurs ive ( fa lse ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
RULE II
setMinimumDepth (32767 > >1);l hs =new A r r a y L i s t ( ) ;
}/∗∗∗Copy cons t ruc to r∗ /
public Rule ( f i n a l Rule copy ) {super ( ) ;l hs=new A r r a y L i s t ( ) ;I t e r a t o r <Symbol> l h s I t =copy . l hs . i t e r a t o r ( ) ;while ( l h s I t . hasNext ( ) ) {
Symbol tmpsymb= l h s I t . next ( ) ;l hs . add (new Symbol ( tmpsymb ) ) ;
}setRecurs ive ( copy . getRecurs ive ( ) ) ;setMinimumDepth ( copy . getMinimumDepth ( ) ) ;
}
/∗∗∗Delete a l l product ions , and a l l symbols s tored i n lhs .∗ /
public void c l ea r ( ) {t ry {l hs . c l ea r ( ) ; / / c l ea r the lhs vec to r
super . c l ea r ( ) ; / / Clear produc t ion vec to r}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
RULE III
catch ( java . lang . Except ion e ) {System . out . p r i n t ( e ) ;
}}
/∗∗∗Return the recu rs i ve nature o f t h i s r u l e .∗ /
public f i n a l boolean getRecurs ive ( ) {return r ecu rs i ve ;
}
/∗∗∗Update the recu rs i ve nature o f t h i s r u l e .∗ /
public void setRecurs ive ( f i n a l boolean newRecursive ) {recu rs i ve =newRecursive ;
}
/∗∗∗Return the minimum mapping depth o f t h i s r u l e .∗ /
public f i n a l i n t getMinimumDepth ( ) {return minimumDepth ;
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
RULE IV
/∗∗∗ Update the minimum mapping depth o f t h i s Rule .∗ /
public void setMinimumDepth ( f i n a l i n t newMinimumDepth ) {minimumDepth=newMinimumDepth ;
}
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
MAPPER I
/∗∗ Mapper . java∗∗ Created on June 1 , 2007 , 11:24 PM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
/∗∗∗∗ @author a d i l r a j a∗ /
package GrammaticOpt imizat ion ;
import java . u t i l .∗ ;import java . u t i l . A r r a y L i s t ;
public abstract class Mapper extends Ar rayL i s t <Rule> {
protected Genotype genotype ;protected Phenotype phenotype ;protected abstract boolean genotype2phenotype ( ) ;protected abstract boolean phenotype2genotype ( ) ;
public Mapper ( ) {super ( ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
MAPPER II
phenotype=new Phenotype ( ) ;genotype=new Genotype ( ) ;
}/∗∗∗Const ruc tor w i th genotype s t r u c t u r e . set to new newGenotype∗ /
public Mapper ( f i n a l Genotype newGenotype ) {super ( ) ;th is . genotype=newGenotype ;
}/∗∗∗Const ruc tor w i th phenotype s t r u c t o r . se t phenotype to newPhenotype∗ /
public Mapper ( f i n a l Phenotype newPhenotype ) {super ( ) ;th is . phenotype=new Phenotype ( newPhenotype ) ;
}/∗∗∗Copy Const ruc tor∗ /public Mapper ( f i n a l Mapper copy ) {
th is . genotype=new Genotype ( copy . genotype ) ;th is . phenotype=new Phenotype ( copy . phenotype ) ;
}/∗∗∗ r e tu rns the genotype s t r u c t u r e o f t h i s mapper∗ /
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
MAPPER III
public f i n a l Genotype getGenotype ( ) {return th is . genotype ;
}/∗∗∗Sets t h i s mapper ’ s genotype s t r u c t u r e to be a copy of the argument , newgenotype ,∗and c a l l s the genotype2phenotype p r i v a t e method .∗ /
public void setGenotype ( f i n a l Genotype newGenotype ) {th is . genotype=new Genotype ( newGenotype ) ;genotype2phenotype ( ) ;
}/∗∗∗ r e tu rns a p o i n t e r to the phenotype s t r u c t u r e o f t h i s mapper∗ /
public f i n a l Phenotype getPhenotype ( ) {return phenotype ;
}/∗∗∗Sets t h i s mapper ’ s phenotype s t r u c t u r e to be a copy of the argument phenotype∗s t r uc tu re , and c a l l s the phenotype2genotype p r i v a t e method∗ /
public void setPhenotype ( f i n a l Phenotype newPhenotype ) {phenotype=new Phenotype ( newPhenotype ) ;phenotype2genotype ( ) ;
}/∗∗∗Sets the maximum codon value o f the genotype s t r u c t u r e∗ /
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
MAPPER IV
public void setGenotypeMaxCodonValue ( f i n a l i n t newMaxCodonValue ) { / / CodonType= i n tth is . genotype . setMaxCodonValue ( newMaxCodonValue ) ;
}/ / something l e f t here
}/ / f i l e complete
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
SYMBOL I
/∗∗ Symbol . java∗∗ Created on June 2 , 2007 , 6:05 PM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
/∗∗∗∗ @author a d i l r a j a∗∗ /
package GrammaticOpt imizat ion ;
import java . u t i l .∗ ;import java . lang . S t r i n g ;
/ /
enum SymbolType {NTSymbol , TSymbol
} ;
public class Symbol {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
SYMBOL II
private SymbolType type ;private S t r i n g symbol ;/∗∗ Creates a new ins tance of Symbol ∗ //∗Creat a new symbol ob jec t as a copy of newArry and wi th type newType , i f s p e c i f i e d∗ /
public Symbol ( ) {setType ( SymbolType . TSymbol ) ;th is . symbol=new S t r i n g ( ) ;
}
public Symbol ( f i n a l S t r i n g newArray , f i n a l SymbolType newType ) {i f ( newArray != nul l )
th is . symbol=new S t r i n g ( newArray ) ;else
th is . symbol=new S t r i n g ( " " ) ;i f ( newType != nul l )
setType ( newType ) ;else
setType ( SymbolType . TSymbol ) ;}/∗∗∗ Copy Const ruc tor∗ /
public Symbol ( f i n a l Symbol copy ) {t ry {
th is . symbol=new S t r i n g ( copy . getSymbol ( ) ) ;}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
SYMBOL III
System . out . p r i n t l n ( e ) ;}setType ( copy . getType ( ) ) ;
}
/∗∗∗ Get the type of the symbol∗ /
public f i n a l SymbolType getType ( ) {return th is . type ;
}/∗Set the type of the symbol∗ /
public void setType ( f i n a l SymbolType newType ) {th is . type=newType ;
}/∗∗∗Returns the symbol∗ /
public f i n a l S t r i n g getSymbol ( ) {return th is . symbol ;
}/∗∗∗Sets the symbol∗ /
public void setSymbol ( f i n a l S t r i n g newSymbol ) {th is . symbol=newSymbol ;
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
SYMBOL IV
/∗∗∗∗ /
public boolean makeComparison ( f i n a l Symbol newSymbol ) {i f ( th is . symbol . compareTo ( newSymbol . symbol )==0 && th is . getType ( ) . t o S t r i n g ( ) . compareTo ( newSymbol . getType ( ) . t o S t r i n g ( ) ) = = 0 )
return true ;return fa lse ;
}public void c l ea r ( ) {
th is . symbol= " " ;}
}/ / under cons ide ra t i on
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
TREE I
/∗∗ Tree . java∗∗ Created on Ju ly 27 , 2007 , 3:53 PM∗∗ To change t h i s template , choose Tools | Template Manager∗ and open the template i n the e d i t o r .∗ /
package GrammaticOpt imizat ion ;
/∗∗∗∗ @author a d i l r a j a∗ /
import java . u t i l .∗ ;import java . u t i l . A r r a y L i s t ;
public class Tree extends Ar rayL i s t <Tree> {
private Symbol data ; / / Tree i s made of ob jec ts o f type symbol as opposed to a r b i t r a r y type .private i n t depth ;private i n t cu r ren tLeve l ;private boolean v a l i d ;/∗∗ Creates a new ins tance of Tree ∗ /public Tree ( ) {}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
TREE II
/∗∗∗ Const ruc tor ; i n i t i a l i s e s depth and cu r ren t l e v e l .∗ /
public Tree ( f i n a l i n t newDepth , f i n a l i n t newCurrentLevel ) {depth=newDepth ;cu r ren tLeve l=newCurrentLevel ;se tVa l i d ( fa lse ) ;
}
/∗∗∗Const ruc tor ; i n i t i a l i s e s data to match arguments .∗ /
public Tree ( f i n a l Symbol newData , f i n a l i n t newCurrentLevel , f i n a l i n t newDepth ) {data=newData ;depth=newDepth ;cu r ren tLeve l=newCurrentLevel ;se tVa l i d ( true ) ;
}
/∗∗∗Copy cons t ruc to r .∗ /
public Tree ( f i n a l Tree copy ) {super ( copy ) ;setData ( copy . getData ( ) ) ;setDepth ( copy . getDepth ( ) ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
TREE III
se tCur ren tLeve l ( copy . getCur rentLeve l ( ) ) ;se tVa l i d ( copy . ge tVa l i d ( ) ) ;
}
/∗∗∗ Return depth from t h i s node u n t i l the deepest l e a f .∗ /
/ / template <c lass T>public f i n a l i n t getDepth ( ) {
return depth ;}
/∗∗∗Set new depth o f t h i s node .∗ /
public void setDepth ( f i n a l i n t newDepth ) {depth=newDepth ;
}
/∗∗∗Return l e v e l o f t h i s node .∗ /
public f i n a l i n t getCur rentLeve l ( ) {return cu r ren tLeve l ;
}
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
TREE IV
/∗∗∗Set new cu r ren t l e v e l o f t h i s node .∗ /
public void se tCur ren tLeve l ( f i n a l i n t newCurrentLevel ) {
cu r ren tLeve l=newCurrentLevel ;}
/∗∗∗Return data s tored i n t h i s node .∗ /
public f i n a l Symbol getData ( ) {i f ( ge tVa l i d ( ) ) {
return data ;}else {
return nul l ;}
}/∗∗∗ Set data to s to re i n t h i s node .∗ /
public void setData ( f i n a l Symbol newData ) {data=new Symbol ( newData ) ;se tVa l i d ( true ) ;
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
TREE V
}
/∗∗∗Return v a l i d i t y o f t h i s node .∗ /
public f i n a l boolean ge tVa l i d ( ) {return v a l i d ;
}
/∗∗∗ Set v a l i d i t y o f t h i s node .∗ /
public void se tVa l i d ( f i n a l boolean newValid ) {v a l i d =newValid ;
}}/ / complete
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
SUMMARY
Java Beagle implements a simple genetic algorithm inJava.Implementation was focused at efficiency, ease of use andsimplicity.Much of it is based on Beagle Puppy.It is open source and can be found online.
Introduction GE’s Main Initializer Genotype Grammar Production Rule Mapper Symbol Tree Summary References
REFERENCES
GO can be found online.This presentation is developed with Beamer:
Darmstadt, crane.