Download - Java Beagle
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
JAVA BEAGLETHE SOURCE CODE
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 27, 2015
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
OUTLINE I1 INTRODUCTION
2 PUPPY’S MAIN
3 PUPPY
4 NODE
5 EVALUATION
6 TREE
7 STATISTICS
8 STATISTICS IO9 CONTEXT
10 PRIMITIVES
11 FUNCTIONS
12 SUMMARY
13 REFERENCES
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
INTRODUCTION I
Java Beagle is an implementation of a simple geneticprogramming (GP) framework.The algorithm is implemented in Java.The algorithm is aimed at symbolic regression problems.With slight modification it can also be used for other typesof problems such as classification.It is based on Beagle Puppy software for geneticprogramming.The source code has many Java files.
1 PuppyMain.java: The main entry point to the program thatruns the GP algorithmd.
2 Puppy.java: This class implements the algorithms that arenecessary to run the GP, such as crossover and mutation.
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
INTRODUCTION II
3 Node.java: The data-type Node specifies theimplementation of the GP genotype.
4 Evaluation.java: The routines for evaluation of a node in GPpopulation.
5 Tree.java: builds and Keeps the syntax tree of a GPprogram.
In what follows you will find the source code of variouscomponents of Java Beagle.
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY’S MAIN I
/∗∗ PuppysMain . java∗∗ Created on August 14 , 2007 , 8:15 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 GeneticProgramming ;
/∗∗∗∗ @author a d i l r a j a∗ /
import GeneticProgramming . P r i m i t i v e s .∗ ;import GeneticProgramming . P r i m i t i v e s . Terminals .∗ ;import GeneticProgramming . P r i m i t i v e s . Funct ions .∗ ;import java . u t i l .∗ ;import java . lang .∗ ;import java . i o .∗ ;
public class PuppysMain {
public s t a t i c f l o a t [ ] [ ] X ;public s t a t i c f l o a t [ ] Target ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY’S MAIN II
public s t a t i c Context lCon tex t ;public s t a t i c i n t cacheHits , noCacheHits ;
/∗∗ Creates a new ins tance of PuppysMain ∗ /public PuppysMain ( ) {}
/∗∗∗ @param args the command l i n e arguments∗ /
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 herenew Context ( ) ;Context . i n s e r t (new Plus ( " p lus " , 2 ) ) ;Context . i n s e r t (new Minus ( " minus " , 2 ) ) ;lCon tex t . i n s e r t (new MyDivide ( " mydivide " , 2 ) ) ;lCon tex t . i n s e r t (new AdilPower ( " ad i lpower " , 2 ) ) ;Context . i n s e r t (new Times ( " t imes " , 2 ) ) ;lCon tex t . i n s e r t (new Sin ( " s in " , 1 ) ) ; / / s i nlCon tex t . i n s e r t (new Cos ( " cos " , 1 ) ) ; / / coslCon tex t . i n s e r t (new Adi lLog ( " a d i l l o g " , 1 ) ) ; / / l nlCon tex t . i n s e r t (new AdilLog10 ( " a d i l l og 1 0 " , 1 ) ) ; / / log−10
/ / lCon tex t . i n s e r t (new P r i m i t i v e ( " i f " , 3 ) ) ;Context . i n s e r t (new X1( "X1" , 0 ) ) ;Context . i n s e r t (new X2( "X2" , 0 ) ) ;Context . i n s e r t (new X3( "X3" , 0 ) ) ;Context . i n s e r t (new X4( "X4" , 0 ) ) ;Context . i n s e r t (new X5( "X5" , 0 ) ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY’S MAIN III
Context . i n s e r t (new X6( "X6" , 0 ) ) ;Context . i n s e r t (new X7( "X7" , 0 ) ) ;Context . i n s e r t (new X8( "X8" , 0 ) ) ;Context . i n s e r t (new X9( "X9" , 0 ) ) ;Context . i n s e r t (new X10 ( "X10" , 0 ) ) ;Context . i n s e r t (new X11 ( "X11" , 0 ) ) ;Context . i n s e r t (new X12 ( "X12" , 0 ) ) ;Context . i n s e r t (new X13 ( "X13" , 0 ) ) ;Context . i n s e r t (new X14 ( "X14" , 0 ) ) ;Context . i n s e r t (new X15 ( "X15" , 0 ) ) ;Context . i n s e r t (new X16 ( "X16" , 0 ) ) ;Context . i n s e r t (new X17 ( "X17" , 0 ) ) ;Context . i n s e r t (new X18 ( "X18" , 0 ) ) ;Context . i n s e r t (new X19 ( "X19" , 0 ) ) ;Context . i n s e r t (new X20 ( "X20" , 0 ) ) ;Context . i n s e r t (new X21 ( "X21" , 0 ) ) ;Context . i n s e r t (new X22 ( "X22" , 0 ) ) ;Context . i n s e r t (new X23 ( "X23" , 0 ) ) ;Context . i n s e r t (new X24 ( "X24" , 0 ) ) ;Context . i n s e r t (new X25 ( "X25" , 0 ) ) ;Context . i n s e r t (new X26 ( "X26" , 0 ) ) ;Context . i n s e r t (new X27 ( "X27" , 0 ) ) ;Context . i n s e r t (new X28 ( "X28" , 0 ) ) ;Context . i n s e r t (new X29 ( "X29" , 0 ) ) ;Context . i n s e r t (new X30 ( "X30" , 0 ) ) ;Context . i n s e r t (new X31 ( "X31" , 0 ) ) ;Context . i n s e r t (new X32 ( "X32" , 0 ) ) ;Context . i n s e r t (new X33 ( "X33" , 0 ) ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY’S MAIN IV
Context . i n s e r t (new X34 ( "X34" , 0 ) ) ;Context . i n s e r t (new X35 ( "X35" , 0 ) ) ;Context . i n s e r t (new X36 ( "X36" , 0 ) ) ;Context . i n s e r t (new X37 ( "X37" , 0 ) ) ;Context . i n s e r t (new X38 ( "X38" , 0 ) ) ;Context . i n s e r t (new X39 ( "X39" , 0 ) ) ;Context . i n s e r t (new X40 ( "X40" , 0 ) ) ;Context . i n s e r t (new X41 ( "X41" , 0 ) ) ;Context . i n s e r t (new X42 ( "X42" , 0 ) ) ;Context . i n s e r t (new X43 ( "X43" , 0 ) ) ;Context . i n s e r t (new X44 ( "X44" , 0 ) ) ;Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;Context . popSize =2000;Context . xoverProba =0.95F ;Context . mutat ionProba =0.1F ;Context . in In i tGrowProba =0.5F ;Context . xoverNodeDist r ibProb =0.5F ;Context . maxTreeDepth=17;Context . in i tTreeDepthMax =6;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY’S MAIN V
Context . in i tTreeDepthMin =2;Context . tournamentSize =7;Context . t r a i nDa taF i l eX =new S t r i n g ( " . / OnP563 / p563−i npu t−params−cond−n−exp−wise . t x t " ) ;
/ / Context . t r a i nDa taF i l eX =" a d i l . t x t " ;Context . t r a i nDa taF i l eY =new S t r i n g ( " . / OnP563 / Subj−MOS−1328−cond−wise−sor ted . t x t " ) ;Context . tes tDa taF i l eX=new S t r i n g ( " . / OnP563 / combined−p563−fea tures−Nor te l−czech . t x t " ) ;Context . tes tDa taF i l eY=new S t r i n g ( " . / OnP563 / combined−ACR−MOS−Nor te l−czech . t x t " ) ;Context . useTestData= true ;Context . leng th_of_run =100;i n t numRuns=2;i n t numVARs=44;
/ / f o r ( i n t k =0;k <5; k ++){/ / Context . i n s e r t (new P r i m i t i v e ( "X " . concat ( I n tege r . t o S t r i n g ( k +1 ) ) , 0 ) ) ;
/ / }Ar rayL i s t < S t a t i s t i c s > s t a t i s t i c s =new A r r a y L i s t ( ) ;
/∗∗Lets do an example problem here∗ /
/ / Context . XData=new f l o a t [ 1 0 0 0 ] [ 2 ] ;/ / Context . Target=new double [ 1 0 0 0 ] ;
double f i t n e s s ;/ / Random dice=new Random ( ) ;/ / f o r ( i n t i =0; i <1000; i ++){/ / Context . XData [ i ] [ 0 ] = d ice . nex tF loa t ()∗10−5;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY’S MAIN VI
/ / Context . XData [ i ] [ 1 ] = d ice . nex tF loa t ()∗10−5;/ / Context . Target [ i ]= Math . pow( Context . XData [ i ] [ 0 ] , 4 ) + Context . XData [ i ] [ 0 ]∗ Context . XData [ i ] [ 1 ] + Math . pow( Context . XData [ i ] [ 1 ] , 4 ) ;
/ / Context . Target [ i ] = ( double ) ( Context . XData [ i ] [ 0 ]∗ ( Context . XData [ i ] [ 0 ]∗ ( Context . XData [ i ] [ 0 ]∗ ( Context . XData [ i ] [ 0 ] + 1 ) + 1 ) + 1 ) ) ;/ / }
Context . readDataPatterns (1328 , 44 , true ) ;/ / System . e x i t ( 0 ) ;
i f ( Context . useTestData ) / / i f t e s tda ta i s used as we l lContext . readDataPatterns (276 , 44 , fa lse ) ;
i n t lPopSize =1000;Puppy puppy=new Puppy ( ) ;
cacheHits =0;
for ( i n t i =0; i <numRuns ; i ++){ / / the run s t a r t s hereS t a t i s t i c s th isRunSta ts=new S t a t i s t i c s ( ) ;th isRunSta ts . t r a i n F i t H i s t o r y =new double [ Context . leng th_of_run ] ;i f ( Context . useTestData )
th isRunSta ts . t e s t F i t H i s t o r y =new double [ Context . leng th_of_run ] ;
A r rayL i s t <Tree> lPopu la t i on =new A r r a y L i s t ( Context . popSize ) ;
puppy . i n i t i a l i z e P o p u l a t i o n ( lPopu la t i on ) ;puppy . eva lua tePopu la t ion ( lPopu la t i on ) ;
i n t j =0;f i t n e s s =500;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY’S MAIN VII
Tree bestTree=nul l ;while ( j <Context . leng th_of_run ) { / / run s t a r t s from here
System . out . p r i n t l n ( ) ;bestTree=puppy . app l yA l lEvo lu t i ona ryS teps ( lPopu la t i on ) ;double tmpFitness =200;
tmpFitness= lPopu la t i on . get ( 0 ) . ge tT ra in i ngF i t ness ( ) ;double t e s t F i t n e s s =Double .MAX_VALUE;i f ( Context . useTestData ) {
t e s t F i t n e s s = lPopu la t i on . get ( 0 ) . evaluateTreeForTestData ( ) ;}th isRunSta ts . t r a i n F i t H i s t o r y [ j ]= tmpFitness ;
/ / th isRunSta ts . t e s t F i t H i s t o r y [ j ]= t e s t F i t n e s s ;System . out . p r i n t l n ( " CurrentBest "+tmpFitness+" TestF i tness : "+ t e s t F i t n e s s +" Generat ion "+ j ) ;System . out . p r i n t l n ( " Xover Prob : "+Context . xoverProba+" Mutat ion Prob : "+Context . mutat ionProba ) ;S t r i n g B u i l d e r s t b l d r =new S t r i n g B u i l d e r ( ) ;lPopu la t i on . get ( 0 ) . wr i teTree (0 , s t b l d r ) ;System . out . p r i n t l n ( s t b l d r ) ;
System . gc ( ) ;j ++;} / / a generat ion ends herebestTree . setSExpression ( ) ; / / se t the s−expression now , s h a l l be used l a t t e rth isRunSta ts . b e s t I n d i v i d u a l =new Tree ( bestTree ) ;s t a t i s t i c s . add ( th isRunSta ts ) ;
} / / a run ends here
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY’S MAIN VIII
t ry {S t a t i s t i c s I O . wr i teSta tsToDisk ( s t a t i s t i c s , " S imu la t ionResu l ts . expr " ) ;
}catch ( java . i o . IOExcept ion e ) {
System . out . p r i n t l n ( e ) ;}
A r rayL i s t < S t a t i s t i c s > s ta to =nul l ;t ry {
s t a t o =( Ar rayL i s t < S t a t i s t i c s >) S t a t i s t i c s I O . readStatsFromDisk ( " S imu la t ionResu l ts . expr " ) ;} catch ( java . i o . IOExcept ion e ) { }catch ( java . lang . ClassNotFoundException e ) { }System . out . p r i n t l n ( " Here are the s t a t i s t i c s " ) ;for ( i n t i =0; i < s ta to . s ize ( ) ; i ++){
System . out . p r i n t l n ( s t a to . get ( i ) . b e s t I n d i v i d u a l . ge tT ra in i ngF i t ness ( ) ) ;}
}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY I
/∗∗ Puppy . java∗∗ Created on August 11 , 2007 , 7: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 GeneticProgramming ;
/∗∗∗∗ @author a d i l r a j a∗ /
import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;import com. sun . org . apache . bce l . i n t e r n a l . c l a s s f i l e . JavaClass ;import java . u t i l .∗ ;import java . u t i l .Random;import java . lang .∗ ;
public class Puppy {
private i n t parentPopSize ;private Random dice ;private Ar rayL i s t <Tree> chi ldPop ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY II
private Ar rayL i s t < In teger > lMateVector ;public double [ ] [ ] t ra in ingDataX ;public double [ ] t ra in ingDataY ;private double bes tF i tness ;private i n t dynamicLevel ;
/∗∗ Creates a new ins tance of Puppy ∗ /public Puppy ( ) {
d ice=new Random ( ) ;ch i ldPop=new A r r a y L i s t ( ) ;lMateVector=new A r r a y L i s t ( ) ;th is . dynamicLevel =6; / / to be changedth is . bes tF i tness =1000;/ / I n i t i a l i z e the IO data here
}
/∗∗∗ I n i t i a l i z e ramped ha l f−and−h a l f a popu la t ion o f GP t rees .∗ \ param ioPopu la t i on Popula t ion to i n i t i a l i z e .∗ \ param inIn i tGrowProba P r o b a b i l i t y to use grow i n i t i a l i z a t i o n , i n oppos i t i on to f u l l .∗ \ param inMinDepth Minimum i n i t i a l i z a t i o n t ree depth al lowed .∗ \ param inMaxDepth Maximum i n i t i a l i z a t i o n t ree depth al lowed .∗ \ ingroup Puppy∗ /
public Ar rayL i s t <Tree> i n i t i a l i z e P o p u l a t i o n ( A r rayL i s t <Tree> ioPopu la t i on ){
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY III
th is . parentPopSize=Context . popSize ;i n t inMinDepth=Context . in i tTreeDepthMin ;i n t inMaxDepth=Context . in i tTreeDepthMax ;f l o a t in In i tGrowProba=Context . in In i tGrowProba ;
asser t ( inMinDepth <= inMaxDepth ) ;/ / A r rayL i s t <Tree> ioPopu la t i on=new A r r a y L i s t ( ) ;th is . d ice=new Random ( ) ;for ( i n t i =0; i <Context . popSize ; ++ i ) {
i oPopu la t i on . add ( i ,new Tree ( ) ) ;i oPopu la t i on . get ( i ) . c l ea r ( ) ;
i n t l I n i t D e p t h = dice . n e x t I n t ( inMaxDepth−inMinDepth )+ inMinDepth +1;Tree tmpTr=new Tree ( ) ;
i f ( d ice . nex tF loa t ( ) >= in In i tGrowProba ) {i n i t i a l i z e T r e e F u l l ( i oPopu la t i on . get ( i ) , l I n i t D e p t h ) ;
/ / i oPopu la t i on . add ( i , new Tree ( tmpTr ) ) ;i oPopu la t i on . get ( i ) . se tVa l i d ( fa lse ) ;i oPopu la t i on . get ( i ) . setEvaluated ( fa lse ) ;
/ / tmpTr . c l ea r ( ) ;}
else {i n i t i a l i z e T r e e G r o w ( ioPopu la t i on . get ( i ) , inMinDepth , l I n i t D e p t h ) ;
/ / i oPopu la t i on . add ( i , new Tree ( tmpTr ) ) ;i oPopu la t i on . get ( i ) . se tVa l i d ( fa lse ) ;i oPopu la t i on . get ( i ) . setEvaluated ( fa lse ) ;
/ / tmpTr . c l ea r ( ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY IV
}
}eva lua tePopu la t ion ( ioPopu la t i on ) ;return i oPopu la t i on ;
}
/∗∗∗ I n i t i a l i z e a GP t ree wi th f u l l approach .∗ \ param ioTree Tree to i n i t i a l i z e .∗ \ param ioContex t Evo lu t i ona ry contex t .∗ \ param inDepth Actua l depth to go i n i n i t i a l i z a t i o n .∗ \ r e t u r n Generated t ree s ize .∗ \ ingroup Puppy∗∗ I f the t ree i s not empty , the i n i t i a l i z a t i o n append the generated sub−t r ee to the ac tua l t r ee .∗ /
public i n t i n i t i a l i z e T r e e F u l l ( Tree ioTree , i n t inDepth ){
i f ( inDepth >= 1 ) { / / asser t was here
i f ( inDepth == 1) {asser t ( Context . mTerminalSet . s i ze ( ) > 0 ) ;P r i m i t i v e lTe rm ina l = Context . mTerminalSet . get ( d ice . n e x t I n t ( Context . mTerminalSet . s i ze ( ) ) ) ;ioTree . add (new Node( lTe rm ina l . giveReference ( d ice ) , 1 ) ) ;return 1;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY V
asser t ( Context . mFunctionSet . s i ze ( ) > 0 ) ;P r i m i t i v e lFunc t i on =Context . mFunctionSet . get ( d ice . n e x t I n t ( Context . mFunctionSet . s i ze ( ) ) ) ;i n t lNodeIndex = ioTree . s ize ( ) ;ioTree . add (new Node( lFunc t i on . giveReference ( d ice ) , 0 ) ) ;i n t lNbArgs = ioTree . get ( lNodeIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;i n t lT reeSize = 1;
/ / Tree tmpT ;for ( i n t i =0; i <lNbArgs ; ++ i ) {
lTreeSize+= i n i t i a l i z e T r e e F u l l ( ioTree , inDepth −1);/ / iT ree=tmpT ;
}ioTree . get ( lNodeIndex ) . setSubTreeSize ( lTreeSize ) ;return lT reeSize ;}else return 0;
}
/∗∗∗ I n i t i a l i z e a GP t ree wi th grow approach .∗ \ param ioTree Tree to i n i t i a l i z e .∗ \ param ioContex t Evo lu t i ona ry contex t .∗ \ param inMinDepth Minimal depth to go i n i n i t i a l i z a t i o n .∗ \ param inMaxDepth Maximal depth to go i n i n i t i a l i z a t i o n .∗ \ r e t u r n Generated t ree s ize .∗ \ ingroup Puppy∗∗ I f the t ree i s not empty , the i n i t i a l i z a t i o n append the generated sub−t r ee to the ac tua l t r ee .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY VI
∗ /public i n t i n i t i a l i z e T r e e G r o w ( Tree ioTree , i n t inMinDepth , i n t inMaxDepth ){
i f ( inMinDepth >= 1 && inMinDepth <= inMaxDepth ) {
P r i m i t i v e l P r i m i t = nul l ;i f ( inMinDepth > 1) {
asser t ( Context . mFunctionSet . s i ze ( ) > 0 ) ;l P r i m i t = Context . mFunctionSet . get ( d ice . n e x t I n t ( Context . mFunctionSet . s i ze ( ) ) ) ;
}else i f ( inMaxDepth == 1) {
asser t ( Context . mTerminalSet . s i ze ( ) > 0 ) ;l P r i m i t = Context . mTerminalSet . get ( d ice . n e x t I n t ( Context . mTerminalSet . s i ze ( ) ) ) ;
}else {
i n t l I ndexSe l = d ice . n e x t I n t ( Context . mFunctionSet . s i ze ( ) + Context . mTerminalSet . s i ze ( ) ) ;i f ( l I ndexSe l >= Context . mFunctionSet . s i ze ( ) ) {
l P r i m i t = Context . mTerminalSet . get ( l I ndexSe l − Context . mFunctionSet . s i ze ( ) ) ;}else l P r i m i t = Context . mFunctionSet . get ( l I ndexSe l ) ;
}
/ / i n t lNodeIndex = tmpArr . s i ze ( ) ;i n t lNodeIndex = ioTree . s ize ( ) ;ioTree . add (new Node( l P r i m i t . giveReference ( d ice ) , 1 ) ) ;
i n t lT reeSize = 1;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY VII
i n t lMinDepth = ( inMinDepth > 1) ? ( inMinDepth−1) : 1 ;i n t lNbArgs = ioTree . get ( lNodeIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ; / / index was lNodeIndex
t ry {for ( i n t i =0; i <lNbArgs ; i ++) {
lTreeSize+= i n i t i a l i z e T r e e G r o w ( ioTree , lMinDepth , inMaxDepth−1);
/ / ioTree=tmpT ;}
}catch ( java . lang . StackOver f lowError e ) {
System . out . p r i n t l n ( e+" i n echange subtrees : "+inMinDepth+" "+inMaxDepth ) ;System . e x i t ( 0 ) ;
}ioTree . get ( lNodeIndex ) . setSubTreeSize ( lTreeSize ) ;return lT reeSize ;
}else {
System . out . p r i n t l n ( " The problem i s here " ) ;return 0;
}}
/∗∗∗ Apply tournament s e l e c t i o n to a popu la t ion o f t rees .∗ \ param ioPopu la t i on Popula t ion to apply s e l e c t i o n on .∗ \ param ioContex t Evo lu t i ona ry contex t .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY VIII
∗ \ param inNumberPar t ic ipants Number o f p a r t i c i p a n t s to each tournament s e l e c t i o n .∗ \ ingroup Puppy∗This f u n c t i o n has to be modi f ied . This on ly s u i t s the replacement c r i t e r i a but not an e l i t i s t c r i t e r i a .∗The r o u t i n e a lso implements LPP by Sean Luke∗ /
public Ar rayL i s t <Tree> applySelect ionTournament ( A r rayL i s t <Tree> ioPopu la t i on ){
i f ( i oPopu la t i on . s i ze ( ) == 0) return nul l ;
i n t inNumberPar t ic ipants=Context . tournamentSize ;A r rayL i s t <Tree> chi ldPop=new A r r a y L i s t ( i oPopu la t i on . s ize ( ) ) ;
i n t [ ] l I n d i c e s =new i n t [ i oPopu la t i on . s ize ( ) ] ;for ( i n t i =0; i < ioPopu la t i on . s ize ( ) ; i ++)
l I n d i c e s [ i ] = 0 ;for ( i n t i =0; i < ioPopu la t i on . s ize ( ) ; i ++) {
i n t lChoosenInd iv idua l = d ice . n e x t I n t ( i oPopu la t i on . s i ze ( ) ) ;for ( i n t j =1; j < inNumberPar t ic ipants ; ++ j ) {
i n t l T r i e d I n d i v i d u a l = d ice . n e x t I n t ( i oPopu la t i on . s ize ( ) ) ; / / the f o l l o w i n g l i n e implements LPPi f ( i oPopu la t i on . get ( l T r i e d I n d i v i d u a l ) . compareRank ( ioPopu la t i on . get ( lChoosen Ind iv idua l ) ) | | ( i oPopu la t i on . get ( lChoosenInd iv idua l ) . compareTrees ( ioPopu la t i on . get ( l T r i e d I n d i v i d u a l ) ) && ioPopu la t i on . get ( l T r i e d I n d i v i d u a l ) . s i ze () < ioPopu la t i on . get ( lChoosen Ind iv idua l ) . s i ze ( ) ) ) {
lChoosenInd iv idua l = l T r i e d I n d i v i d u a l ;}
}++ l I n d i c e s [ lChoosenInd iv idua l ] ;
}
i n t lNextEmpty = 0;i n t l N e x t F i l l e d = 0;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY IX
while ( ( l N e x t F i l l e d < ioPopu la t i on . s ize ( ) ) && ( l I n d i c e s [ l N e x t F i l l e d ] <= 1 ) ) l N e x t F i l l e d ++;while ( l N e x t F i l l e d < ioPopu la t i on . s i ze ( ) ) {
while ( l I n d i c e s [ l N e x t F i l l e d ] > 1) {while ( l I n d i c e s [ lNextEmpty ] ! = 0) ++lNextEmpty ;
chi ldPop . add ( ioPopu la t i on . get ( l N e x t F i l l e d ) ) ; / / remove ( lNextEmpty ) ;/ / i oPopu la t i on . add ( ioPopu la t i on . get ( l N e x t F i l l e d ) ) ;−−l I n d i c e s [ l N e x t F i l l e d ] ;++ l I n d i c e s [ lNextEmpty ] ;
}while ( ( l N e x t F i l l e d < ioPopu la t i on . s ize ( ) ) && ( l I n d i c e s [ l N e x t F i l l e d ] <= 1 ) ) ++ l N e x t F i l l e d ;
}while ( ch i ldPop . s ize () < ioPopu la t i on . s i ze ( ) )
ch i ldPop . add ( ioPopu la t i on . get ( d ice . n e x t I n t ( i oPopu la t i on . s ize ( ) ) ) ) ; / / f i l l the remaining places randomlyreturn chi ldPop ;
}
/∗∗∗ Apply tournament s e l e c t i o n to a popu la t ion o f t rees .∗ \ param ioPopu la t i on Popula t ion to apply s e l e c t i o n on .∗ \ param ioContex t Evo lu t i ona ry contex t .∗ \ param inNumberPar t ic ipants Number o f p a r t i c i p a n t s to each tournament s e l e c t i o n .∗ \ ingroup Puppy∗This f u n c t i o n has to be modi f ied . This on ly s u i t s the replacement c r i t e r i a but not an e l i t i s t c r i t e r i a .∗The r o u t i n e a lso implements LPP by Sean Luke∗ /
public Ar rayL i s t <Tree> applySelect ionTournament1 ( A r rayL i s t <Tree> ioPopu la t i on ){
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY X
i f ( i oPopu la t i on . s i ze ( ) == 0) return nul l ;
A r rayL i s t <Tree> chi ldPop=new A r r a y L i s t ( i oPopu la t i on . s ize ( ) ) ;i n t inNumberPar t ic ipants=Context . tournamentSize ;/ / choose the f i r s t candidate and s to re i n the c h i l d popu la t ionwhile ( ch i ldPop . s ize () < ioPopu la t i on . s i ze ( ) ) {i n t j j =0;i n t lChosenCand1=0 , lChosenCand2 =0;
i n t tmpCand1= ioPopu la t i on . s i ze ( )+1 , tmpCand2= ioPopu la t i on . s i ze ( ) + 1 ;while ( j j < inNumberPar t ic ipants ) {
lChosenCand1=dice . n e x t I n t ( i oPopu la t i on . s ize ( ) ) ;i f ( tmpCand1< ioPopu la t i on . s i ze ( ) ) {
i f ( i oPopu la t i on . get ( lChosenCand1 ) . compareRank ( ioPopu la t i on . get ( tmpCand1 ) ) ) { / / | | ( i oPopu la t i on . get ( lChosenCand ) . compareTrees ( tmpCand ) && ioPopu la t i on . get ( lChoosenCand ) . s ize () < ioPopu la t i on . get ( l T r i e d I n d i v i d u a l ) . s i ze ( ) ) ) {tmpCand1=lChosenCand1 ; / / choose an i n d i v i d u a l ,
}}else
tmpCand1=lChosenCand1 ; / / choose t h i sj j ++;
}chi ldPop . add (new Tree ( ioPopu la t i on . get ( tmpCand1 ) ) ) ; / / Add t h i s to the c h i l d popu la t ion
/ / choose the second candidate now and s to re i n the c h i l d popu la t ionj j =0;while ( j j < inNumberPar t ic ipants ) {
lChosenCand2=dice . n e x t I n t ( i oPopu la t i on . s ize ( ) ) ;i f ( tmpCand2<= ioPopu la t i on . s i ze ( ) ) { / / implements Gustafson and LPP
i f ( ! i oPopu la t i on . get ( tmpCand1 ) . compareTrees ( ioPopu la t i on . get ( tmpCand2 ) )&& ioPopu la t i on . get ( lChosenCand2 ) . compareRank ( ioPopu la t i on . get ( tmpCand2 ) )
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XI
| | ( i oPopu la t i on . get ( lChosenCand2 ) . compareTrees ( ioPopu la t i on . get ( tmpCand2 ) )&& ioPopu la t i on . get ( lChosenCand2 ) . s i ze () < ioPopu la t i on . get ( tmpCand2 ) . s ize ( ) ) ) {
tmpCand2=lChosenCand2 ; / / choose an i n d i v i d u a l ,
}}else
tmpCand2=lChosenCand2 ; / / choose t h i sj j ++;
}chi ldPop . add (new Tree ( ioPopu la t i on . get ( tmpCand2 ) ) ) ; / / Add t h i s to the c h i l d popu la t ion}
for ( i n t i =0; i < ioPopu la t i on . s ize ( ) ; i ++){chi ldPop . get ( i ) . usedMutat ionStd= fa lse ;ch i ldPop . get ( i ) . usedMutationSwap= fa lse ;ch i ldPop . get ( i ) . usedXover= fa lse ;
}return chi ldPop ;
}
/ / what i s t h i s f u n c t i o n doing?
/∗∗∗ Apply sub−t r ee crossover opera t ion on a popu la t ion o f GP t rees .∗ \ param ioPopu la t i on Popula t ion to apply crossover on .∗ \ param ioContex t Evo lu t i ona ry contex t .∗ \ param inMatingProba P r o b a b i l i t y f o r each i n d i v i d u a l to be modi f ied by crossover .∗ \ param i n D i s t r i b P r o b a P r o b a b i l i t y t h a t a crossover exchange two sub−t r ees o f non−t e rm ina l roo ts .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XII
∗ \ param inMaxTreeDepth Maximum t ree depth al lowed .∗ \ ingroup Puppy∗ /
public void applyCrossover ( A r rayL i s t <Tree> ioPopu la t i on ){
i n t inMaxTreeDepth=Context . maxTreeDepth ;f l o a t i n D i s t r i b P r o b a =Context . in In i tGrowProba ; / / not used here but s t i l lf l o a t inMatingProba=Context . xoverProba ;i f ( ( i oPopu la t i on . s ize ( ) % 2) != 0) ioPopu la t i on . remove ( lMateVector . s i ze () −1);
/ / System . out . p r i n t l n ( "MATING VECTOR SIZE= "+ lMateVector . s i ze ( ) ) ;/ / f o r ( unsigned i n t j =0; j < lMateVector . s i ze ( ) ; ++ j ) {/ / s td : : cout << j << " : " << ioPopu la t i on [ lMateVector [ j ] ] << std : : endl ;/ / }
/ / ch i ldPop . c l ea r ( ) ; / / = new A r r a y L i s t ( ) ;for ( i n t j =0; j < ioPopu la t i on . s ize ( ) ; j +=2) {
t ry {mateTrees ( ioPopu la t i on . get ( j ) , i oPopu la t i on . get ( j + 1 ) ) ;
}catch ( java . lang . Nu l lPo in te rExcep t ion e ) {
System . out . p r i n t l n ( e+" i n apply crossover . . . c a l l to mateTrees " ) ;System . e x i t ( 0 ) ;
}t ry {
/ / ch i ldPop . add ( t rees . get ( 0 ) ) ; / / t h i s l i n e i s meant to implement E l i t i s m , as i t r e t a i n s the parent and c h i l d pops/ / ch i ldPop . add ( t rees . get ( 1 ) ) ;
/ / i oPopu la t i on . add (new Tree ( ioPopu la t i on . get ( lMateVector . get ( mate ) . i n tVa lue ( ) ) ) ) ;}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XIII
catch ( java . lang . Except ion e ) {
/ / System . out . p r i n t l n ( j +" "+mate+" "+"WAITING In applyCrossover "+e ) ; / / + " "+ ioPopu la t i on . s ize ( ) ) ;/ / System . e x i t ( 0 ) ;
}}
}
/∗∗∗ Mate two GP t rees f o r crossover .∗ \ param ioTree1 F i r s t t r ee to mate .∗ \ param ioTree2 Second t ree to mate .∗ \ param ioContex t Evo lu t i ona ry contex t .∗ \ param i n D i s t r i b P r o b a D i s t r i b u t i o n p r o b a b i l i t y .∗ \ param inMaxTreeDepth Maximum t ree depth al lowed .∗ \ ingroup Puppy∗ /
public void mateTrees ( f i n a l Tree ioTree1 , f i n a l Tree ioTree2 ){
/ / I n i t i a l parameters checksasser t ( ioTree1 . s ize ( ) > 0 ) ;asser t ( ioTree2 . s ize ( ) > 0 ) ;
f l o a t i n D i s t r i b P r o b a =Context . xoverNodeDist r ibProb ;i n t inMaxTreeDepth=Context . maxTreeDepth ;
/ / A r rayL i s t <Tree> t rees= n u l l ;/ / Crossover loop . Try the given number o f at tempts to mate two i n d i v i d u a l s .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XIV
for ( i n t i =0; i <7; ++ i ) {
/ / Choose a type of node ( branch or l e a f ) f o l l o w i n g the d i s t r i b u t i o n p r o b a b i l i t y and change the/ / node f o r another node of the same t ree i f the types mismatch .boolean lNode1IsTerminal = true ;i f ( ioTree1 . s ize ( ) > 1 ) {
i f ( d ice . nex tF loa t ( ) >= i n D i s t r i b P r o b a ) lNode1IsTerminal = true ;else lNode1IsTerminal = fa lse ;
}i n t lChoosenNode1 =0;t ry {
lChoosenNode1= dice . n e x t I n t ( ioTree1 . s ize ( ) ) ; / / cant choose the roo t node}catch ( java . lang . I l l ega lArgumentExcept ion e ) {
System . out . p r i n t l n ( ioTree1 . s ize ( )+ " mate t ree "+e ) ;System . e x i t ( 0 ) ;
}while ( ( ioTree1 . get ( lChoosenNode1 ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) == 0) != lNode1IsTerminal ) {
lChoosenNode1 = dice . n e x t I n t ( ioTree1 . s ize ( ) ) ;}i f ( lChoosenNode1==0) lChoosenNode1+=1;
boolean lNode2IsTerminal = true ;i f ( ioTree2 . s ize ( ) > 1) lNode2IsTerminal = ( d ice . nex tF loa t ( ) >= i n D i s t r i b P r o b a ) ;i n t lChoosenNode2 = dice . n e x t I n t ( ioTree2 . s ize ( ) ) ;while ( ( ioTree2 . get ( lChoosenNode2 ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) == 0) != lNode2IsTerminal ) {
lChoosenNode2 = dice . n e x t I n t ( ioTree2 . s ize ( ) ) ;}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XV
i f ( lChoosenNode2==0) lChoosenNode2+=1;/ / lChoosenNode2=ioTree2 . s ize ( ) −1 ; / / t r i a l
/ / Set f i r s t s tack to the node of the f i r s t t r ee ./ / Check i f depth i s ok . Do a new crossover at tempt i f not .Ar rayL i s t < In teger > lStack1=ioTree1 . setStackToNode ( lChoosenNode1 ) ;i n t lNewDepthTree1 = lStack1 . s ize ( ) + ioTree2 . getDepth ( lChoosenNode2 ) − 1;i f ( lNewDepthTree1 > inMaxTreeDepth ) continue ;
/ / Set second stack to the node of the second t ree ./ / Check i f depth i s ok . Do a new crossover at tempt i f not .Ar rayL i s t < In teger > lStack2=ioTree2 . setStackToNode ( lChoosenNode2 ) ;i n t lNewDepthTree2 = lStack2 . s ize ( ) + ioTree1 . getDepth ( lChoosenNode1 ) − 1;i f ( lNewDepthTree2 > inMaxTreeDepth ) continue ;
/ / The crossover i s v a l i d ./ / t r y {
exchangeSubTrees ( ioTree1 , lChoosenNode1 , lStack1 , ioTree2 , lChoosenNode2 , lStack2 ) ;/ / ioTree1 . se tVa l i d ( f a l s e ) ;/ / ioTree2 . se tVa l i d ( f a l s e ) ;/ / t r ees . get ( 0 ) . se tVa l i d ( f a l s e ) ;/ / t r ees . get ( 1 ) . se tVa l i d ( f a l s e ) ;
/ / }/ / catch ( java . lang . Except ion e ) {
/ / System . out . p r i n t l n ( e+"The r e t u r n value i s n u l l i n mate−t r ees : " ) ;/ / System . e x i t ( 0 ) ;
/ / }break ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XVI
}
/ / r e t u r n t rees ;}
/∗∗∗ Exchange two sub−t r ees .∗ \ param ioTree1 Tree con ta in ing the f i r s t sub−t r ee to exchange .∗ \ param inNode1 Index of roo t node to sub−t r ee to swap i n f i r s t t r ee .∗ \ param inStack1 Stack con ta in ing the parents to the f i r s t sub−t r ee roo t node .∗ \ param ioTree2 Tree con ta in ing the second sub−t r ee to exchange .∗ \ param inNode2 Index of roo t node to sub−t r ee to swap i n second t ree .∗ \ param inStack2 Stack con ta in ing the parents to the second sub−t r ee roo t node .∗ \ ingroup Puppy∗ /
public void exchangeSubTrees ( f i n a l Tree iTree1 , i n t inNode1 , f i n a l Ar rayL i s t < In teger > inStack1 , f i n a l Tree iTree2 , i n t inNode2 , f i n a l Ar rayL i s t < In teger > inStack2 ){/ / asser t ( iTree1 != iTree2 ) ; / / I can mate mate between s i m i l a r t rees as wel l , i n the worst case . See the c a l l s to t h i s func
asser t ( inStack1 . s ize ( ) > 0 ) ;asser t ( inStack2 . s ize ( ) > 0 ) ;
Tree ioTree1=new Tree ( iTree1 ) ;Tree ioTree2=new Tree ( iTree2 ) ;
i n t lSwapSize1 = ioTree1 . get ( inNode1 ) . getSubTreeSize ( ) ;i n t lSwapSize2 = ioTree2 . get ( inNode2 ) . getSubTreeSize ( ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XVII
Tree ioSubTree1=new Tree ( ) ;Tree ioSubTree2=new Tree ( ) ;
for ( i n t i =0; i <lSwapSize1 ; i ++){t ry {
ioSubTree1 . add ( ioTree1 . remove ( inNode1 ) ) ; / / I had i here NEW}catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( " exchangeSubTrees1 "+e ) ;System . e x i t ( 0 ) ;
}}for ( i n t i =0; i <lSwapSize2 ; i ++){
t ry {ioSubTree2 . add ( ioTree2 . remove ( inNode2 ) ) ; / / I had i here NEW
}catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( " exchangeSubTrees2 "+e ) ;System . e x i t ( 0 ) ;
}}
ioTree1 . addAl l ( inNode1 , ioSubTree2 ) ;ioTree2 . addAl l ( inNode2 , ioSubTree1 ) ;
i n t l D i f f S i z e = lSwapSize1 − lSwapSize2 ;
for ( i n t i =0; i <( inStack1 . s ize () −1); ++ i )t ry {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XVIII
ioTree1 . get ( inStack1 . get ( i ) . i n tVa lue ( ) ) . setSubTreeSize ( ioTree1 . get ( inStack1 . get ( i ) . i n tVa lue ( ) ) . getSubTreeSize()− l D i f f S i z e ) ;}catch ( java . lang . IndexOutOfBoundsException e )
{System . out . p r i n t l n ( " exchangeSubTrees4 "+e ) ;System . e x i t ( 0 ) ;
}for ( i n t j =0; j <( inStack2 . s ize () −1); ++ j )
t ry {ioTree2 . get ( inStack2 . get ( j ) . i n tVa lue ( ) ) . setSubTreeSize ( ioTree2 . get ( inStack2 . get ( j ) . i n tVa lue ( ) ) . getSubTreeSize ( )+ l D i f f S i z e ) ;}catch ( java . lang . IndexOutOfBoundsException e )
{System . out . p r i n t l n ( " exchangeSubTrees5 "+e ) ;System . e x i t ( 0 ) ;
}iTree1 . c l ea r ( ) ;iTree1 . addAl l ( ioTree1 ) ;iTree2 . c l ea r ( ) ;iTree2 . addAl l ( ioTree2 ) ;iTree1 . setEvaluated ( fa lse ) ;iTree2 . setEvaluated ( fa lse ) ;iTree1 . se tVa l i d ( fa lse ) ;iTree2 . se tVa l i d ( fa lse ) ;iTree1 . usedXover= true ;iTree2 . usedXover= true ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XIX
/∗∗∗ Apply standard ( Koza ’ s ) mutat ion on a GP t rees .∗ This performs subtree mutat ion∗ I npu t : − ioTree GP t ree to mutate .∗ i oContex t Evo lu t i ona ry contex t .∗ inMaxRegenDepth Maximum t ree regenera t ion depth al lowed .∗ inMaxDepth Maximum t ree depth al lowed .∗ Output : −∗ ioTree − Mutated GP Tree∗ \ ingroup Puppy∗ /
public void mutateStandard ( Tree ioTree , i n t inMaxRegenDepth , i n t inMaxDepth ){
/ / System . out . p r i n t l n ( " Fo l lowing i s the t ree before mutat ion : \ n " ) ; / / + ioTree . wr i teTree ( 0 ) ) ;/ / f o r ( i n t i =0; i < ioTree . s ize ( ) ; i ++){/ / System . out . p r i n t ( ioTree . get ( i ) . g e t P r i m i t i v e ( ) . getName ( )+ ioTree . get ( i ) . getSubTreeSize ( ) + " " ) ;/ / }
i f ( ioTree . s ize () <=0) System . out . p r i n t l n ( "HUGE ERROR IN MUTATESTANDARD" ) ;asser t ( ioTree . s ize ( ) > 0 ) ;i n t lMut Index = dice . n e x t I n t ( ioTree . s ize () −1);i f ( lMut Index ==0) lMut Index +=1;Tree lNewTree=new Tree ( ) ;for ( i n t i =0; i < lMut Index ; i ++){
lNewTree . add ( ioTree . get ( i ) ) ; / /NEW}
A r rayL i s t < In teger > lS tack= ioTree . setStackToNode ( lMut Index ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XX
lS tack . remove ( lS tack . s ize () −1);i n t lTreeDepth = dice . n e x t I n t ( inMaxRegenDepth )+1 ;i n t lTreeDepth2 = inMaxDepth − lS tack . s ize ( ) ;i f ( lTreeDepth2 < lTreeDepth ) lTreeDepth = lTreeDepth2 ;asser t ( lTreeDepth > 0 ) ;
i n i t i a l i z e T r e e G r o w ( lNewTree , 1 , lTreeDepth ) ;for ( i n t i =( lMut Index+ ioTree . get ( lMut Index ) . getSubTreeSize ( ) ) ; i < ioTree . s ize ( ) ; i ++){
t ry {lNewTree . add ( ioTree . get ( i ) ) ; / /NEW
}catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( e+" i n mutateStandard "+ " "+ lMut Index+" "+ ioTree . get ( lMut Index ) . getSubTreeSize ( )+ " "+ ioTree . s ize ( ) ) ;System . e x i t ( 0 ) ;
}}i n t l D i f f S i z e =0;t ry {
l D i f f S i z e = ioTree . get ( lMut Index ) . getSubTreeSize ( ) − lNewTree . get ( lMut Index ) . getSubTreeSize ( ) ;}catch ( java . lang . Except ion e ) {
System . out . p r i n t l n ( e+" mutIndex : "+ lMut Index ) ;/ / System . out . p r i n t l n ( ioTree . wr i teTree ( 0 ) ) ;
for ( i n t i i =0; i i < ioTree . s ize ( ) ; i i ++)System . out . p r i n t ( ioTree . get ( i i ) . getSubTreeSize ( )+ " " ) ;
System . e x i t ( 0 ) ;}for ( i n t i =0; i < lS tack . s ize ( ) ; ++ i )
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XXI
lNewTree . get ( lS tack . get ( i ) ) . setSubTreeSize ( lNewTree . get ( lS tack . get ( i ) ) . getSubTreeSize()− l D i f f S i z e ) ;/ / ioTree= n u l l ;
/ / ioTree=lNewTree ;lNewTree . se tVa l i d ( fa lse ) ;lNewTree . setEvaluated ( fa lse ) ;ioTree . se tVa l i d ( fa lse ) ;ioTree . setEvaluated ( fa lse ) ;ioTree . c l ea r ( ) ;for ( i n t i =0; i <lNewTree . s ize ( ) ; i ++)
ioTree . add (new Node( lNewTree . get ( i ) ) ) ;ioTree . usedMutat ionStd= true ;/ / System . out . p r i n t l n ( " Fo l lowing i s the t ree a f t e r mutat ion : \ n " ) ; / / + ioTree . wr i teTree ( 0 ) ) ;/ / f o r ( i n t i =0; i < ioTree . s ize ( ) ; i ++){/ / System . out . p r i n t ( ioTree . get ( i ) . g e t P r i m i t i v e ( ) . getName ( )+ ioTree . get ( i ) . getSubTreeSize ( ) + " " ) ;/ / }/ / System . out . p r i n t l n ( " \ nMutIndex : "+ lMut Index ) ;/ / r e t u r n lNewTree ;}
/∗ !∗ \ b r i e f Apply swap po in t mutat ion to a popu la t ion o f GP t rees .∗ \ param ioPopu la t i on Popula t ion to mutate .∗ \ param ioContex t Evo lu t i ona ry contex t .∗ \ param inMutat ionProba Mutat ion p r o b a b i l i t y .∗ \ param i n D i s t r i b P r o b a P r o b a b i l i t y to mutate a f u n c t i o n node , i n oppos i t i on to a te rm ina l .∗ \ ingroup Puppy∗ /
public void applyMutationSwap ( Ar rayL i s t <Tree> ioPopu la t i on )
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XXII
{f l o a t inMutat ionProba=Context . swapMutationProba ;for ( i n t i =0; i < ioPopu la t i on . s ize ( ) ; ++ i ) {
i f ( d ice . nex tF loa t ( ) < inMutat ionProba ) {mutateSwap ( ioPopu la t i on . get ( i ) ) ;
}}
}
/∗ !∗ \ b r i e f Swap mutate a GP t ree .∗ \ param ioTree GP t ree to mutate .∗ \ param ioContex t Evo lu t i ona ry contex t .∗ \ param i n D i s t r i b P r o b a P r o b a b i l i t y to mutate a f u n c t i o n node , i n oppos i t i on to a te rm ina l .∗ \ ingroup Puppy∗ /
public void mutateSwap ( Tree ioTree ){
f l o a t i n D i s t r i b P r o b a =Context . xoverNodeDist r ibProb ;asser t ( ioTree . s ize ( ) > 0 ) ;i n t lMut Index = dice . n e x t I n t ( ioTree . s ize ( ) ) ;i f ( ioTree . s ize ( ) > 1) {
boolean lType = ( d ice . nex tF loa t ( ) < i n D i s t r i b P r o b a ) ;while ( ( ioTree . get ( lMut Index ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) != 0) != lType ) {
lMut Index = dice . n e x t I n t ( ioTree . s ize ( ) ) ;}
}i n t lNbArgs = ioTree . get ( lMut Index ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XXIII
i f ( lNbArgs == 0) {asser t ( Context . mTerminalSet . s i ze ( ) > 0 ) ;P r i m i t i v e lTe rm ina l =
Context . mTerminalSet . get ( d ice . n e x t I n t ( Context . mTerminalSet . s i ze ( ) ) ) ;ioTree . get ( lMut Index ) . s e t P r i m i t i v e ( lTe rm ina l . c lone ( ) . giveReference ( d ice ) ) ;
}else {
A r rayL i s t < In teger > lKArgsFunct ion=new A r r a y L i s t ( ) ;for ( i n t i =0; i <Context . mFunctionSet . s i ze ( ) ; ++ i ) {
i f ( Context . mFunctionSet . get ( i ) . g e t A r i t y ( ) == lNbArgs ) {lKArgsFunct ion . add (new I n t ege r ( i ) ) ;
}}asser t ( lKArgsFunct ion . s ize ( ) > 0 ) ;P r i m i t i v e lFunc t i on =Context . mFunctionSet . get ( lKArgsFunct ion . get ( d ice . n e x t I n t ( lKArgsFunct ion . s ize ( ) ) ) ) ;ioTree . get ( lMut Index ) . s e t P r i m i t i v e ( lFunc t i on . c lone ( ) ) ;
}ioTree . usedMutationSwap= true ;
}
/∗∗∗ Apply standard ( Koza ’ s ) mutat ion to a popu la t ion o f GP t rees .∗ \ param ioPopu la t i on Popula t ion to mutate .∗ \ param ioContex t Evo lu t i ona ry contex t .∗ \ param inMutat ionProba Mutat ion p r o b a b i l i t y .∗ \ param inMaxRegenDepth Maximum t ree regenera t ion depth al lowed .∗ \ param inMaxDepth Maximum t ree depth al lowed .∗ \ ingroup Puppy
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XXIV
∗ /public void applyMutat ionStandard ( A r rayL i s t <Tree> ioPopu la t i on ){
f l o a t inMutat ionProba=Context . mutat ionProba ;i n t inMaxRegenDepth=Context . in i tTreeDepthMax ;i n t inMaxDepth=Context . maxTreeDepth ;
/ / System . out . p r i n t l n ( " Lets see how Mutat ion works \ n"+ ioPopu la t i on . get ( t h i s . parentPopSize ) . wr i teTree ( 0 ) ) ;for ( i n t i = th is . parentPopSize ; i < ioPopu la t i on . s ize ( ) ; i ++) {
i f ( d ice . nex tF loa t () <= inMutat ionProba ) {mutateStandard ( ioPopu la t i on . get ( i ) , inMaxRegenDepth , inMaxDepth ) ;
}}
/ / System . out . p r i n t l n ( " Mutat ion worked l i k e t h i s \ n"+ ioPopu la t i on . get ( t h i s . parentPopSize ) . wr i teTree ( 0 ) ) ;/ / r e t u r n ioPopu la t i on ;
}
/∗∗∗adapt the p r o b a b i l i t i e s o f genet ic opera tors i n a dynamic fash ion∗ /
public void adap tOpe ra to rP robab i l i t i e s ( L i s t <Tree> lPopu la t i on ) {I t e r a t o r <Tree> p o p I t r = lPopu la t i on . i t e r a t o r ( ) ;i n t numUsedXover=1 , numUsedMutStd=1 , numUsedMutSwap=1;i n t numImpXover=0 , numImpMutStd=0 , numImpMutSwap=0;while ( p o p I t r . hasNext ( ) ) {
Tree tmpTree= p o p I t r . next ( ) ;i f ( tmpTree . usedXover ) {
numUsedXover++;i f ( tmpTree . ge tT ra in i ngF i t ness () <= Context . prev iousBest )
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XXV
numImpXover++;}i f ( tmpTree . usedMutat ionStd ) {
numUsedMutStd++;i f ( tmpTree . ge tT ra in i ngF i t ness () <= Context . prev iousBest )
numImpMutStd++;}i f ( tmpTree . usedMutationSwap ) {
numUsedMutSwap++;i f ( tmpTree . ge tT ra in i ngF i t ness () <= Context . prev iousBest )
numImpMutSwap++;}
}Context . xoverProba =( f l o a t ) ( numImpXover / numUsedXover ) ;Context . mutat ionProba =( f l o a t ) ( numImpMutStd / numUsedMutStd ) ;Context . swapMutationProba =( f l o a t ) ( numImpMutSwap / numUsedMutSwap ) ;
i f ( Context . xoverProba <0.5F) Context . xoverProba =0.7F ;i f ( Context . mutationProba <0.01F) Context . mutat ionProba =0.15F ;i f ( Context . swapMutationProba <0.01F) Context . swapMutationProba =0.15F ;
}
/∗∗∗Perform a l l Evo lu t i ona ry steps i n one go∗1) ApplySelect ionTournament∗2) ApplyCrossover∗3) ApplyMutat ionStandard∗4) Evaluate f i t n e s s o f the popu la t ion
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XXVI
∗5) Apply e l i t i s t s u r v i v a l∗ /
public Tree app lyA l lEvo lu t i ona ryS teps ( A r rayL i s t <Tree> lPopu la t i on ) {A r rayL i s t <Tree> chi ldPop=applySelect ionTournament1 ( lPopu la t i on ) ;applyCrossover ( chi ldPop ) ;applyMutat ionStandard ( chi ldPop ) ;applyMutationSwap ( chi ldPop ) ;/ / Set the S−Expressions now here as t h i s w i l l save some computat ionl Popu la t i on . addAl l ( ch i ldPop ) ;eva lua tePopu la t ion ( lPopu la t i on ) ;adap tOpe ra to rP robab i l i t i e s ( lPopu la t i on . subL is t ( Context . popSize , lPopu la t i on . s ize ( ) −1) ) ;th is . app lySurv iva l1 ( lPopu la t i on ) ;for ( i n t i =0; i <10; i ++)
lPopu la t i on . get ( i ) . tuneTreesCoeffs ( ) ;C o l l e c t i o n s . s o r t ( lPopu la t ion , new FitnessComparator ( ) ) ; / / s o r t again i f GA i s usedreturn l Popu la t i on . get ( 0 ) ; / / r e t u r n the best t r ee as i t remains the f i r s t element a f t e r s o r t i n g
}
/∗∗∗Evaluate a given popu la t ion∗ /
public void eva lua tePopu la t ion ( A r rayL i s t <Tree> lPopu la t i on ) {I t e r a t o r <Tree> i t = lPopu la t i on . i t e r a t o r ( ) ;double b e s t F i t =Double .MAX_VALUE;while ( i t . hasNext ( ) ) {
Tree tmpTree= i t . next ( ) ;/ / new Thread ( tmpTree ) . s t a r t ( ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XXVII
double f i t n e s s =tmpTree . evaluateTree ( ) ;/ / i t . next ( ) . tuneTreesCoeffs ( ) ;
i f ( f i t n e s s <Context . prev iousBest ) Context . prev iousBest= f i t n e s s ;}
}
/∗∗∗an e l i t i s t s u r v i v a l c r i t e r i o n∗ /
public void app lySurv iva l1 ( A r rayL i s t <Tree> ioPopu la t i on ) {C o l l e c t i o n s . s o r t ( i oPopu la t i on . subL is t (0 , i oPopu la t i on . s ize () /2 −1) , new FitnessComparator ( ) ) ; / / Sor t the pop wr t f i t n e s s f i r s t i n descending orderC o l l e c t i o n s . s o r t ( i oPopu la t i on . subL is t ( i oPopu la t i on . s ize ( ) / 2 , i oPopu la t i on . s ize ()−1) , new FitnessComparator ( ) ) ; / / Sor t the pop wr t f i t n e s s f i r s t i n descending order
/ / C o l l e c t i o n s . s o r t ( ioPopu la t ion , new DepthComparator ( ) ) / / Sor t wr t depth as we l l now −− I guess no need f o r t h i s s o r ti n t dynDepth= ioPopu la t i on . get ( i oPopu la t i on . s i ze ( ) / 2 ) . getDepth ( 0 ) ; / / o f the best c h i l d
/ / i f ( dynDepth> t h i s . dynamicLevel )i f ( dynDepth>=6 && ioPopu la t i on . get ( i oPopu la t i on . s ize ( ) / 2 ) . ge tT ra in i ngF i t ness () < ioPopu la t i on . get ( 0 ) . ge tT ra in i ngF i t ness ( ) )
th is . dynamicLevel=dynDepth ;i n t ch i l d ren_ ;for ( i n t i = ioPopu la t i on . s ize ( ) / 2 ; i < ioPopu la t i on . s ize ( ) ; i ++){ / / remove from among chi ldPop
Tree tmpTree= ioPopu la t i on . get ( i ) ;i f ( tmpTree . getDepth (0) > th is . dynamicLevel ) { / / dynamic t ree depth
i oPopu la t i on . remove ( i ) ;i−−;
}}while ( i oPopu la t i on . subL is t ( th is . parentPopSize , i oPopu la t i on . s ize ( ) ) . s i ze () > th is . parentPopSize / 2 )
ioPopu la t i on . remove ( ioPopu la t i on . s i ze () −1); / / remove the ex t ra c h i l d r e n herei n t i = th is . parentPopSize−1;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XXVIII
while ( i oPopu la t i on . s i ze () > th is . parentPopSize )ioPopu la t i on . remove ( i −−); / / remove the less f i t parents now
C o l l e c t i o n s . s o r t ( ioPopu la t ion , new FitnessComparator ( ) ) ;Context . prev iousBest= ioPopu la t i on . get ( 0 ) . ge tT ra in i ngF i t ness ( ) ;System . out . p r i n t l n ( th is . dynamicLevel ) ;
}
}
/∗∗∗Fi tness Comparator∗ /
class FitnessComparator implements java . u t i l . Comparator<Tree >{public i n t compare ( Tree o1 , Tree o2 ) {
double f i t 1 =o1 . ge tT ra in i ngF i t ness ( ) ;double f i t 2 =o2 . ge tT ra in i ngF i t ness ( ) ;i f ( f i t 1 < f i t 2 ) return −1;i f ( f i t 1 == f i t 2 ) return 0;return 1;
}}
/∗∗
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PUPPY XXIX
∗Size ( Tree Depth ) Comparator
∗ /
class DepthComparator implements java . u t i l . Comparator<Tree >{public i n t compare ( Tree o1 , Tree o2 ) {
i n t depth1=o1 . getDepth ( 0 ) ;i n t depth2=o2 . getDepth ( 0 ) ;i f ( depth1 <depth2 ) return −1;i f ( depth1==depth2 ) return 0;return 1;
}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
NODE I
/∗∗ Node . java∗∗ Created on August 11 , 2007 , 11:44 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 GeneticProgramming ;
import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;import java . i o .∗ ;
/∗∗∗∗ @author a d i l r a j a∗ /
public class Node implements S e r i a l i z a b l e {private P r i m i t i v e mPr imi t i ve ; / / ! < Smart p o i n t e r to the assoc iated p r i m i t i v e .
private i n t mSubTreeSize ; / / ! < Sub−t r ee size , i n c l u d i n g ac tua l node .
/∗∗ Creates a new ins tance of Node ∗ /public Node( P r i m i t i v e p r i m i t i v e , i n t s ize ) {
th is . mPr im i t i ve= p r i m i t i v e . c lone ( ) ;th is . mSubTreeSize=s ize ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
NODE II
/∗∗∗Copy Const ruc tor∗ /
public Node(Node copy ) {/ / t h i s . mPr imi t i ve=new P r i m i t i v e ( copy . mPr imi t i ve ) ;th is . mPr im i t i ve=copy . mPr imi t i ve . c lone ( ) ;th is . mSubTreeSize=copy . mSubTreeSize ;
}
/∗∗∗Set the p r i m i t i v e∗ /
public void s e t P r i m i t i v e ( f i n a l P r i m i t i v e prim ) {/ / t h i s . mPr imi t i ve=new P r i m i t i v e ( prim ) ;
th is . mPr im i t i ve=prim . clone ( ) ;}
/∗∗∗Return the p r i m i t i v e∗ /
public f i n a l P r i m i t i v e g e t P r i m i t i v e ( ) {return th is . mPr im i t i ve ;
}/∗∗∗vo id subtree s ize∗ /
public void setSubTreeSize ( f i n a l i n t t reeS ize ) {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
NODE III
th is . mSubTreeSize= t reeS ize ;}
/∗∗∗Return the subtree s ize∗ /
public f i n a l i n t getSubTreeSize ( ) {return th is . mSubTreeSize ;
}}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
EVALUATION I
/∗∗ Eva lua t ion . java∗∗ Created on September 6 , 2007 , 9:18 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 .∗Keeps the Eva lua t ion o f a subtee l y i n g i n a cache∗ /
package GeneticProgramming ;
/∗∗∗∗ @author a d i l r a j a∗ /
public class Eva lua t ion {public double [ ] eva lua t i on ;public boolean cachedFlag ;/∗∗∗The d e f a u l t cons t r uc to r f o r Terminal nodes∗ /
public Eva lua t ion ( ) {th is . cachedFlag= true ; / / Vals f o r Ts always get t r ueth is . eva lua t i on=new double [ 1 ] ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
EVALUATION II
}
/∗∗∗The c o n t r u c t o r f o r NT subtrees∗ /
public Eva lua t ion ( double [ ] eval , boolean cFlag ) {th is . eva lua t i on=new double [ eva l . l eng th ] ;for ( i n t i =0; i <eva l . l eng th ; i ++){
th is . eva lua t i on [ i ]= eva l [ i ] ;th is . cachedFlag=cFlag ; / / Def ines Whether an en t ry i s e f f e c t i v e l y cached}
}}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE I
/∗∗ Tree . java∗∗ Created on August 10 , 2007 , 10:36 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 GeneticProgramming ;
/∗∗∗∗ @author a d i l r a j a∗ /
import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;import java . u t i l .∗ ;import java . lang .∗ ;import EvoNursery .∗ ;import java . i o .∗ ;
public class Tree extends Ar rayL i s t <Node> implements S e r i a l i z a b l e {private double f i t n e s s , t e s t F i t n e s s ;private boolean v a l i d ;private S t r i n g s_expression ;private boolean evaluated ; / / t e l l s i f the f i t n e s s o f t h i s t r ee has been evaluatedprivate double slope ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE II
private double i n t e r c e p t ;private double [ ] t r a i n i n g R e s u l t ; / / This conta ins the r e s u l t o f the i n d i v i d u a l over whole datapublic s t a t i c double [ ] fo rConstants ; / / These two v a r i a b l e s s h a l l be used by the p r i m i t i v e s executepublic boolean usedXover , usedMutationStd , usedMutationSwap ;
/∗∗ Creates a new ins tance of Tree ∗ /public Tree ( ) {
th is . f i t n e s s =Double .MAX_VALUE; / / assign a f i t n e s s valueth is . v a l i d = fa lse ;th is . evaluated= fa lse ;
}
/∗∗∗ Const ruct a new tree , w i th given f i t n e s s and v a l i d i t y f l a g .∗ /
public Tree ( double sFi tness , boolean i V a l i d ) {th is . f i t n e s s =sF i tness ;th is . v a l i d = i V a l i d ;th is . s_expression=new S t r i n g ( ) ;th is . evaluated= fa lse ; / / t h a t i t has not been evaluated by d e f a u l tth is . s lope =1;th is . i n t e r c e p t =0;
}
/∗∗∗ copy Const ruc tor∗ /
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE III
public Tree ( f i n a l Tree copy ) {th is . f i t n e s s =copy . f i t n e s s ;th is . t e s t F i t n e s s =copy . t e s t F i t n e s s ;th is . v a l i d =copy . v a l i d ;
/ / t h i s . s_expression=copy . wr i teTree ( 0 ) ;th is . evaluated=copy . evaluated ;for ( i n t i =0; i <copy . s ize ( ) ; i ++)
th is . add (new Node( copy . get ( i ) ) ) ;th is . s lope=copy . s lope ;th is . i n t e r c e p t =copy . i n t e r c e p t ;
}
/∗∗∗ \ b r i e f Return t ree depth a t given index .∗ \ param in Index Index of sub−t r ee roo t to get the depth from .∗ \ r e t u r n Sub−t r ee depth .∗ /
public f i n a l i n t getDepth ( i n t i n Index ){
i f ( in Index < th is . s i ze ( ) ) { / / I had asser t herei n t lNbArgs = th is . get ( in Index ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;i n t lDepth = 1;i n t j = in Index + 1;for ( i n t i =0; i <lNbArgs ; ++ i ) {
i n t lCh i ldDepth = getDepth ( j ) + 1 ;i f ( lCh i ldDepth > lDepth ) lDepth = lCh i ldDepth ;j += th is . get ( j ) . getSubTreeSize ( ) ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE IV
return lDepth ;}else {
return 0;}
}
/∗∗∗ Compare e q u a l i t y o f two t rees .∗ Two t rees are condired equal i f t h e i r f i t n e s s d i f f e r s by less than 0 .01 .∗ For bucke t t i ng reasons∗ r e t u r n : True i s t rees are equals , f a l s e i f not .∗ /
public f i n a l boolean compareTrees ( f i n a l Tree aTree ){
i f ( th is==aTree ) return true ;return ( th is . v a l i d && aTree . v a l i d && ( java . lang . Math . abs ( th is . f i t n e s s − aTree . f i t n e s s ) < 0 . 0 1 ) ) ;
}
/∗∗∗ Compare rank ing o f two t rees .∗ \ r e t u r n True i s ac tua l t r ee i s less than a t ree given as argument , f a l s e i f not .∗ /
public f i n a l boolean compareRank ( Tree aTree ){
return ( th is . v a l i d && aTree . v a l i d && ( th is . f i t n e s s < aTree . f i t n e s s ) ) ;}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE V
/∗∗ I n t e r p r e t the GP t ree .∗ \ param outResu l t Datum con ta in ing the r e s u l t o f the i n t e r p r e t a t i o n .∗ \ param ioContex t Evo lu t i ona ry contex t .∗
vo id Puppy : : Tree : : i n t e r p r e t ( vo id∗ outResul t , Puppy : : Context& ioContex t ){
asser t ( s i ze ( ) > 0 ) ;ioContex t . mTree = t h i s ;ioContex t . mCallStack . push_back ( 0 ) ;f r o n t ( ) . mPr imi t ive−>execute ( outResul t , ioContex t ) ;ioContex t . mCallStack . pop_back ( ) ;
} ∗ /
/∗∗∗ Set c a l l s tack to inc lude the c o r r e c t l y r e f e r to a given node .∗ \ param in Index Node index to which c a l l s tack must be set .∗ \ param outCa l lS tack Resul t o f c a l l s tack s e t t i n g .∗ /
public f i n a l java . u t i l . A r rayL i s t < In teger > setStackToNode ( i n t i n Index ){
asser t ( in Index < s ize ( ) ) ;
A r rayL i s t < In teger > c a l l S t a c k =new A r r a y L i s t ( ) ;i n t i = 0 ;c a l l S ta c k . add (new I n t ege r ( i ) ) ;while ( i < in Index ) {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE VI
i n t lNbArgs= th is . get ( i ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;i n t lCh i l d I ndex = i + 1 ;for ( i n t j =0; j <lNbArgs ; ++ j ) {
i f ( ( lCh i l d I ndex + th is . get ( lCh i l d I ndex ) . getSubTreeSize ( ) ) > in Index ) break ;lCh i l d I ndex += th is . get ( lCh i l d I ndex ) . getSubTreeSize ( ) ;
}asser t ( lCh i l d I ndex < th is . s i ze ( ) ) ;i = lCh i l d I ndex ;c a l l S ta c k . add (new I n t ege r ( i ) ) ;
}asser t ( i == in Index ) ;return c a l l S ta c k ;
}
/∗∗∗Returns the slope parameter found by l i n e a r sca l i ng∗ /
public f i n a l double getSlope ( ) {return th is . s lope ;
}
/∗∗∗Returns the i n t e r c e p t parameter found by l i n e a r sca l i ng∗ /
public f i n a l double g e t I n t e r c e p t ( ) {return th is . i n t e r c e p t ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE VII
/∗∗∗ \ b r i e f Wr i te GP t ree at given index as a s−expression i n t o a Java S t r i n g ob jec t .∗ \ param ioOS C++ output stream to w r i t e t ree i n t o .∗ \ param in Index Actua l node index i n t ree .∗ /
public void wr i teTree ( i n t in Index , S t r i n g B u i l d e r expression ){
i f ( in Index >= th is . s i ze ( ) ) return ;/ / expression . append ( " " ) ;
i n t lNbArgs = th is . get ( in Index ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
expression . append ( th is . get ( in Index ) . g e t P r i m i t i v e ( ) . getName ( ) ) ;i f ( lNbArgs > 0) expression . append ( " ( " ) ;i n t j = in Index + 1;for ( i n t i =0; i <lNbArgs ; ++ i ) {
wr i teTree ( j , expression ) ;i f ( i <lNbArgs−1)
expression . append ( " , " ) ;t ry {
j = j + th is . get ( j ) . getSubTreeSize ( ) ;}catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( e+" In w r i t e t r ee " ) ;/ / System . e x i t ( 0 ) ;
}}i f ( lNbArgs > 0) expression . append ( " ) " ) ;/ / r e t u r n expression . t r t r i m ( ) ; / / removes the " " added i n the beginning .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE VIII
}
/∗∗∗ \ b r i e f Wr i te GP t ree at given index as a s−expression i n t o a Java S t r i n g ob jec t .∗ \ param ioOS C++ output stream to w r i t e t ree i n t o .∗ \ param in Index Actua l node index i n t ree .∗ /
public void wr i teTree2 ( i n t in Index , S t r i n g B u i l d e r expression ){
i f ( in Index >= th is . s i ze ( ) ) return ;/ / expression . append ( " " ) ;
i n t lNbArgs = th is . get ( in Index ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
expression . append ( th is . get ( in Index ) . g e t P r i m i t i v e ( ) . getName ( ) ) ;/ / i f ( lNbArgs > 0) expression . append ( " ( " ) ;i n t j = in Index + 1;for ( i n t i =0; i <lNbArgs ; ++ i ) {
wr i teTree ( j , expression ) ;/ / i f ( i <lNbArgs−1)
/ / expression . append ( " , " ) ;t ry {
j = j + th is . get ( j ) . getSubTreeSize ( ) ;}catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( e+" In w r i t e t r ee " ) ;/ / System . e x i t ( 0 ) ;
}}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE IX
/ / i f ( lNbArgs > 0) expression . append ( " ) " ) ;/ / r e t u r n expression . t r t r i m ( ) ; / / removes the " " added i n the beginning .
}
/∗∗∗sets the s−expression∗ /
public void setSExpression ( ) {S t r i n g B u i l d e r expr=new S t r i n g B u i l d e r ( th is . s i ze ( ) ) ;th is . wr i teTree (0 , expr ) ;th is . s_expression=new S t r i n g ( expr . t o S t r i n g ( ) ) ;
}/∗∗∗Gets the s−epression∗ /
public f i n a l S t r i n g getSExpression ( ) {return th is . s_expression ;
}
/∗∗∗Set the v a l i d f l a g to something∗ /
public void se tVa l i d ( f i n a l boolean i n V a l i d ) {th is . v a l i d = i n V a l i d ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE X
/∗∗∗Get the v a l i d i t y s ta tus o f t h i s t r ee∗ /
public f i n a l boolean ge tVa l i d ( ) {return th is . v a l i d ;
}/∗∗Sets the p r i m i t i v e to new value∗ /
public void s e t P r i m i t i v e ( f i n a l P r i m i t i v e prim ) {th is . s e t P r i m i t i v e ( prim ) ;
}
/∗∗∗Set the evaluated f l a g o f t h i s t r ee∗ /
public void setEvaluated ( f i n a l boolean eval ) {th is . evaluated=eval ;
}
/∗∗∗Get the eva lua t i on f l a g o f t h i s t r ee∗ /
public f i n a l boolean getEvaluated ( ) {return th is . evaluated ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE XI
/∗∗∗Set the f i t n e s s o f t h i s Tree∗ /
public void se tT ra i n i ngF i t ness ( f i n a l double f i t ) {th is . f i t n e s s = f i t ;
}
/∗∗∗Return the t r a i n i n g f i t n e s s o f t h i s Tree∗ /
public f i n a l double ge tT ra in i ngF i t ness ( ) {return th is . f i t n e s s ;
}/∗∗∗Return the f i t n e s s o f Test ing data∗ /
public f i n a l double ge tTes t ingF i tness ( ) {return th is . t e s t F i t n e s s ;
}
/ / p u b l i c vo id run ( ) {/ / t h i s . evaluateTree ( ) ;
/ / }
/∗∗∗ Evaluate f i t n e s s o f a Tree∗∗ inX Independant sample values f o r eva lua t i on .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE XII
∗ inF Dependant sample values f o r eva lua t i on .∗ \ r e t u r n Number o f f i t n e s s evaluated .∗ \ ingroup SymbReg∗ /
public double evaluateTree ( ){
asser t ( Context . XData . leng th == Context . Target . leng th ) ;i f ( th is . evaluated )
return th is . f i t n e s s ;
double [ ] t a r g e t =Context . Target ;double [ ] evolved= th is . i n t e r p r e t T r e e ( Context . XData ) ;
i f ( evolved . leng th != Context . Target . leng th ) {double tmpEvolved=evolved [ 0 ] ; / / i f eve ry th ing t h a t i s re turned i s a constantevolved=new double [ Context . Target . leng th ] ;for ( i n t z =0;z<Context . Target . leng th ; z++)
evolved [ z ]= tmpEvolved ;}double [ ] s lope_n_ in te rcep t=new double [ 2 ] ;
l i n e a r S c a l i n g ( s lope_n_ in te rcept , evolved , Context . Target ) ;/ / t h i s . t r a i n i n g R e s u l t =new double [ evolved . leng th ] ;
double tmpFitness =0;double res ;for ( i n t i =0; i <evolved . leng th ; i ++){
res=Context . Target [ i ]−( s lope_n_ in te rcep t [0 ]∗ evolved [ i ]+ s lope_n_ in te rcep t [ 1 ] ) ;tmpFitness+=res∗res ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE XIII
}
tmpFitness=tmpFitness / evolved . leng th ;/ / i f ( tmpFitness < t h i s . f i t n e s s ) {
th is . f i t n e s s =tmpFitness ;th is . s lope=s lope_n_ in te rcep t [ 0 ] ;th is . i n t e r c e p t =s lope_n_ in te rcep t [ 1 ] ;
/ / }
i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) )th is . f i t n e s s =Double .MAX_VALUE;
/ / System . out . p r i n t l n ( " Current best "+ t h i s . f i t n e s s ) ;th is . v a l i d = true ;th is . evaluated= true ;
return th is . f i t n e s s ;}
/∗∗∗ Evaluate f i t n e s s o f a Tree∗∗ inX Independant sample values f o r eva lua t i on .∗ inF Dependant sample values f o r eva lua t i on .∗ \ r e t u r n Number o f f i t n e s s evaluated .∗ \ ingroup SymbReg∗ /
public double evaluateTreeForTestData ( ){
asser t ( Context . TestXData . leng th == Context . TestTarget . leng th ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE XIV
double [ ] t a r g e t =Context . TestTarget ;double [ ] evolved= th is . i n t e r p r e t T r e e ( Context . TestXData ) ;
i f ( evolved . leng th != Context . TestTarget . leng th ) {double tmpEvolved=evolved [ 0 ] ; / / i f eve ry th ing t h a t i s re turned i s a constantevolved=new double [ Context . TestTarget . leng th ] ;for ( i n t z =0;z<Context . TestTarget . leng th ; z++)
evolved [ z ]= tmpEvolved ;}
/ / t h i s . t r a i n i n g R e s u l t =new double [ evolved . leng th ] ;double tmpFitness =0;double res ;for ( i n t i =0; i <evolved . leng th ; i ++){
res=Context . TestTarget [ i ]−( th is . s lope∗evolved [ i ]+ th is . i n t e r c e p t ) ;tmpFitness+=res∗res ;
}tmpFitness=tmpFitness / evolved . leng th ;
/ / i f ( tmpFitness < t h i s . f i t n e s s ) {th is . t e s t F i t n e s s =tmpFitness ;
i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) )th is . t e s t F i t n e s s =Double .MAX_VALUE;
/ / System . out . p r i n t l n ( " Current best "+ t h i s . f i t n e s s ) ;th is . v a l i d = true ;th is . evaluated= true ;
return th is . t e s t F i t n e s s ;}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE XV
/∗∗∗LinearSca l ing , by Maarten Ke i j ze r , i s implemented here∗ /
public void l i n e a r S c a l i n g ( double [ ] s lope_n_ in te rcep t , double [ ] evolved , double [ ] t a r g e t ) {asser t ( evolved . leng th== t a r g e t . leng th ) ;double meanTarget=0 , meanEvolved =0;
for ( i n t i =0; i <evolved . leng th ; i ++){meanEvolved+=evolved [ i ] ;meanTarget+= t a r g e t [ i ] ;
}meanEvolved /= evolved . leng th ;meanTarget /= t a r g e t . leng th ;
double numerator =0 , denominator =0;for ( i n t i =0; i <evolved . leng th ; i ++){
numerator +=( t a r g e t [ i ]−meanTarget )∗ ( evolved [ i ]−meanEvolved ) ;denominator+= java . lang . Math . pow( evolved [ i ]−meanEvolved , 2 ) ;
}/ / numerator=numerator / t a r g e t . leng th ;/ / denominator /= evolved . leng th ;
double slope1 =1;i f ( denominator !=0 ) slope1=numerator / denominator ;else i f ( meanEvolved==0) slope1 =1;else slope1=meanTarget / meanEvolved ;s lope_n_ in te rcep t [ 0 ] = slope1 ;s lope_n_ in te rcep t [ 1 ] = meanTarget−slope∗meanEvolved ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE XVI
}
/∗∗∗ I n t e r p r e t the GP t ree .∗ outResu l t Datum con ta in ing the r e s u l t o f the i n t e r p r e t a t i o n .∗ XData to be evaluated∗ /
public double [ ] i n t e r p r e t T r e e ( f l o a t [ ] [ ] xdata ){
asser t ( th is . s i ze ( ) > 0 ) ;A r rayL i s t < In teger > c a l lS t a c k =new A r r a y L i s t ( ) ;c a l l S ta c k . add (new I n t ege r ( 0 ) ) ;double [ ] ou tResu l t= th is . get ( 0 ) . g e t P r i m i t i v e ( ) . execute ( ca l lS tack , this , xdata ) ;c a l l S ta c k . remove ( c a l l S t ac k . s ize () −1);return outResu l t ;
}/∗∗∗ tune the te rm ina l s ( constants ) o f t h i s t r ee∗∗ inX Independant sample values f o r eva lua t i on .∗ inF Dependant sample values f o r eva lua t i on .∗ \ r e t u r n Number o f f i t n e s s evaluated .∗ \ ingroup SymbReg∗ /
public double tuneTreesCoeffs ( ){
asser t ( Context . XData . leng th == Context . Target . leng th ) ;/ / i f ( t h i s . evaluated ) r e t u r n t h i s . f i t n e s s ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE XVII
I t e r a t o r <Node> n o d I t r = th is . i t e r a t o r ( ) ;i n t i i =0;while ( n o d I t r . hasNext ( ) ) {
Node tmpNode= n o d I t r . next ( ) ;i f ( tmpNode . g e t P r i m i t i v e ( ) . g e t A r i t y ()==0
&& ! tmpNode . g e t P r i m i t i v e ( ) . getName ( ) . s t a r t s W i t h ( "X" ) )i i ++; / / f i n d the number o f constants
}i f ( i i <=1)
return th is . evaluateTree ( ) ; / / i . e . no c o e f f i c i e n t s to be tuned by GA so evaluate the ind onlyi n t [ ] const Index=new i n t [ i i ] ; / / keeps the ind i ces o f constants i n t h i s t r eedouble [ ] constants=new double [ i i ] ; / / t h i s ar ray would keep the constants f o r the i n t e r i m ( a temp ar ray f o r best combo)i n t j j =0;
for ( i i =0; i i < th is . s i ze ( ) ; i i ++) {i f ( th is . get ( i i ) . g e t P r i m i t i v e ( ) . g e t A r i t y ()==0
&& ! th is . get ( i i ) . g e t P r i m i t i v e ( ) . getName ( ) . s t a r t s W i t h ( "X" ) ) {const Index [ j j ]= i i ; / / t h i s loop copies the ind i ces to the ar rayconstants [ j j ]= Double . valueOf ( th is . get ( i i ) . g e t P r i m i t i v e ( ) . getName ( ) ) ;j j ++;
}}
/ / I n i t i a l i z e the GA hereNursery nurse=new Nursery ( ) ;A r rayL i s t <Genotype> pop=nurse . i n i t i a l i z e P o p u l a t i o n (100 , const Index . length , −6, +6 ) ; / / should i n i t i a l i z e a popu la t ion
for ( i n t i =0; i <50; i ++){ / / numGens i s chosen to be 150Ar rayL i s t <Genotype> chi ldPop=nurse . applySelect ionTournament ( pop , 4 ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE XVIII
nurse . app lySing lePoin tCrossover ( chi ldPop , const Index . length , 0 . 8 ) ;nurse . applyMutat ionUni form ( chi ldPop , const Index . length , 0 . 2 ) ;pop . addAl l ( ch i ldPop ) ; / / append the two pops herefor ( i n t j =0; j <pop . s ize ( ) ; j ++){
i f ( pop . get ( j ) . getEvaluatedFlag ()== fa lse ) { / / i f i t has not been evaluateddouble [ ] tmpGenes=pop . get ( j ) . getGenotype ( ) ;for ( i n t k =0;k<const Index . leng th ; k++)
th is . get ( const Index [ k ] ) . g e t P r i m i t i v e ( ) . setName ( Double . t o S t r i n g ( tmpGenes [ k ] ) ) ; / / i n s e r t the genes i n t o t reedouble [ ] evolved= th is . i n t e r p r e t T r e e ( Context . XData ) ;i f ( evolved . leng th != Context . Target . leng th ) {
double tmpEvolved=evolved [ 0 ] ; / / i f eve ry th ing t h a t i s re turned i s a constantevolved=new double [ Context . Target . leng th ] ;for ( i n t z =0;z<Context . Target . leng th ; z++)
evolved [ z ]= tmpEvolved ;}double [ ] s lope_n_ in te rcep t=new double [ 2 ] ;l i n e a r S c a l i n g ( s lope_n_ in te rcept , evolved , Context . Target ) ;
/ / t h i s . t r a i n i n g R e s u l t =new double [ evolved . leng th ] ;double tmpFitness =0;double res ;for ( i n t i j =0; i j <Context . Target . leng th ; i j ++) {
res=Context . Target [ i j ]−( s lope_n_ in te rcep t [0 ]∗ evolved [ i j ]+ s lope_n_ in te rcep t [ 1 ] ) ;tmpFitness+=res∗res ;
}tmpFitness=tmpFitness / evolved . leng th ;i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) )
tmpFitness=Double .MAX_VALUE;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE XIX
i f ( tmpFitness < th is . f i t n e s s ) {th is . f i t n e s s =tmpFitness ;
th is . s lope=s lope_n_ in te rcep t [ 0 ] ;th is . i n t e r c e p t =s lope_n_ in te rcep t [ 1 ] ;for ( i n t k =0;k<tmpGenes . leng th ; k++)
constants [ k ]= tmpGenes [ k ] ;}pop . get ( j ) . se tF i tness ( tmpFitness ) ;i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) ){
th is . f i t n e s s =Double .MAX_VALUE; / / assign a very la rge f i t n e s spop . get ( j ) . se tF i tness ( Double .MAX_VALUE) ;
}pop . get ( j ) . setEvaluated ( true ) ;
} / / i f getEvaluated}nurse . app l ySurv i va l ( pop ) ;
}for ( i n t i =0; i <constants . leng th ; i ++)
th is . get ( const Index [ i ] ) . g e t P r i m i t i v e ( ) . setName ( Double . t o S t r i n g ( constants [ i ] ) ) ; / / se t the coe f f s i n the t ree
/ / System . out . p r i n t l n ( " Current best "+ t h i s . f i t n e s s ) ;th is . v a l i d = true ;th is . evaluated= true ;
return th is . f i t n e s s ;}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
TREE XX
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
STATISTICS I
/∗∗ S t a t i s t i c s . java∗∗ Created on September 14 , 2007 , 3: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 GeneticProgramming ;
/∗∗∗∗ @author a d i l r a j a∗ /
import java . i o .∗ ;public class S t a t i s t i c s implements S e r i a l i z a b l e {
/∗∗ Creates a new ins tance of S t a t i s t i c s ∗ /public S t a t i s t i c s ( ) {}/∗∗∗copy cons t ruc to r∗ /
public S t a t i s t i c s ( S t a t i s t i c s copy ) {t r a i n F i t H i s t o r y =new double [ copy . t r a i n F i t H i s t o r y . leng th ] ;t e s t F i t H i s t o r y =new double [ copy . t e s t F i t H i s t o r y . leng th ] ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
STATISTICS II
for ( i n t i =0; i <copy . t r a i n F i t H i s t o r y . leng th ; i ++){t r a i n F i t H i s t o r y [ i ]= copy . t r a i n F i t H i s t o r y [ i ] ;t e s t F i t H i s t o r y [ i ]= copy . t e s t F i t H i s t o r y [ i ] ;
}b e s t I n d i v i d u a l =new Tree ( copy . b e s t I n d i v i d u a l ) ;
}
public s t a t i c double [ ] t r a i n F i t H i s t o r y ;public s t a t i c double [ ] t e s t F i t H i s t o r y ;public s t a t i c Tree b e s t I n d i v i d u a l ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
STATISTICS IO I
/∗∗ S t a t i s t i c s I O . java∗∗ Created on September 15 , 2007 , 6:30 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 GeneticProgramming ;
/∗∗∗∗ @author a d i l r a j a∗ /
import java . i o .∗ ;import java . u t i l .∗ ;
public class S t a t i s t i c s I O {
/∗∗ Creates a new ins tance of S t a t i s t i c s I O ∗ /public S t a t i s t i c s I O ( ) {}
/∗∗∗wr i teSta tsToDisk∗ /
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
STATISTICS IO II
public s t a t i c void wr i teSta tsToDisk ( A r rayL i s t < S t a t i s t i c s > simStats , S t r i n g f i lename ) throws IOExcept ion {ObjectOutputStream out = nul l ;
t ry {out = new ObjectOutputStream (new Fi leOutputStream ( f i lename ) ) ;out . w r i t eOb jec t ( ( Object ) s imStats ) ;out . f l u s h ( ) ;
} f i n a l l y {i f ( out != nul l ) {
t ry {out . c lose ( ) ;
} catch ( IOExcept ion except ion ) { }}
}}public s t a t i c Ar rayL i s t < S t a t i s t i c s > readStatsFromDisk ( S t r i n g f i lename ) throws IOException , ClassNotFoundException {
Object InputStream i n = nul l ;t ry {
i n =new Object InputStream (new Fi le InputSt ream ( f i lename ) ) ;return ( A r rayL i s t < S t a t i s t i c s >) i n . readObject ( ) ;
} f i n a l l y {i f ( i n != nul l ) {
t ry {i n . c lose ( ) ;
} catch ( IOExcept ion e ) { }}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
STATISTICS IO III
}}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
CONTEXT I
/∗∗ Context . java∗∗ Created on August 11 , 2007 , 8:34 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 GeneticProgramming ;
/∗∗∗∗ @author a d i l r a j a∗ /
import GeneticProgramming . P r i m i t i v e s .∗ ;import java . u t i l .∗ ;import java . i o .∗ ;public class Context {
/∗∗ Creates a new ins tance of Context ∗ /public Context ( ) {
th is . mFunctionSet=new A r r a y L i s t ( ) ;th is . mTerminalSet=new A r r a y L i s t ( ) ;th is . mPrimitiveMap=new TreeMap ( ) ;th is . mCache=new MyLinkedHashMap(500 , .75F , true ) ;th is . prev iousBest=Double .MAX_VALUE;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
CONTEXT II
}
/∗∗∗ \ b r i e f Add a new p r i m i t i v e i n the sets o f p r i m i t i v e .∗ \ param i n P r i m i t i v e P r i m i t i v e added .∗ /
public s t a t i c void i n s e r t ( P r i m i t i v e i n P r i m i t i v e ){
asser t ( ! mPrimit iveMap . containsKey ( i n P r i m i t i v e . getName ( ) ) ) ;mPrimitiveMap . put ( i n P r i m i t i v e . getName ( ) , i n P r i m i t i v e ) ;i f ( i n P r i m i t i v e . g e t A r i t y ( ) == 0) mTerminalSet . add ( i n P r i m i t i v e ) ;else mFunctionSet . add ( i n P r i m i t i v e ) ;
}
/∗∗∗This i s used to i n i t i a l i z e the data∗ /
public s t a t i c void i n i t D a t a ( f l o a t [ ] [ ] X , f l o a t [ ] Y ) {asser t (X . leng th==Y. leng th ) ;
XData=new f l o a t [X . leng th ] [ X [ 0 ] . l eng th ] ;Target=new double [X . leng th ] ;for ( i n t i =0; i <X . leng th ; i ++){
for ( i n t j =0; j <X [ 0 ] . l eng th ; j ++)XData [ i ] [ j ]=X [ i ] [ j ] ;
Target [ i ]=Y [ i ] ;}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
CONTEXT III
}/∗∗∗This i s used to i n i t i a l i z e the Test data∗ /
public s t a t i c void i n i t T e s t D a t a ( f l o a t [ ] [ ] X , double [ ] Y ) {asser t (X . leng th==Y. leng th ) ;TestXData=new f l o a t [X . leng th ] [ X [ 0 ] . l eng th ] ;TestTarget=new double [X . leng th ] ;for ( i n t i =0; i <X . leng th ; i ++){
for ( i n t j =0; j <X [ 0 ] . l eng th ; j ++)TestXData [ i ] [ j ]=X [ i ] [ j ] ;
TestTarget [ i ]=Y [ i ] ;}
}/∗∗∗This f u n c t i o n i s used to read i n data pa t te rns . To be used by the java program∗params :∗f i lename−the f i l e con ta in ing IO pa t te rns∗sizeX− number o f pa t te rns∗sizeY− number o f v a r i a b l e s ( f o r i npu t data )∗ t ra inORtes t− i f t rue , then t r a i n data , e lse t e s t data∗ /
public s t a t i c void readDataPatterns ( i n t sizeX , i n t sizeY , boolean t ra inORtes t ) {StreamTokenizer tok=nul l ;S t r i n g fi lenameX , f i lenameY ;i f ( t ra inORtes t ) {
f i lenameX= t ra i nDa taF i l eX ;f i lenameY= t ra i nDa taF i l eY ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
CONTEXT IV
}else {
f i lenameX= tes tDa taF i l eX ;f i lenameY= tes tDa taF i l eY ;
}t ry {
tok = new StreamTokenizer (new BufferedReader (new Fi leReader ( f i lenameX ) ) ) ;tok . ord inaryChar ( ’ \ n ’ ) ;/ / tok . ord inaryChar ( )
}catch ( java . i o . Fi leNotFoundExcept ion e ) {
System . out . p r i n t l n ( e+" i n Context . java " ) ;}
i n t i =0;i n t j =0;f l o a t [ ] [ ] curDat=new f l o a t [ s izeX ] [ sizeY ] ;
S t r i n g s t r ;t ry {
j =0;while ( tok . nextToken ( ) ! = StreamTokenizer .TT_EOF ) {
i f ( j >=44){ j =0; i ++ ; }switch ( tok . t t ype ) {case StreamTokenizer .TT_EOL:
i ++;j =0;break ;
case ’ \ r ’ :
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
CONTEXT V
i ++;j =0;break ;
case ’ n ’ :i ++;j =0;break ;
case StreamTokenizer .TT_NUMBER:curDat [ i ] [ j ] = ( f l o a t ) tok . nva l ;
/ / System . out . p r i n t l n ( tok . nva l ) ;/ / j = j +1;
/ / i f ( j ==3){/ / j =0;
/ / i ++;/ / }
j ++;break ;
defaul t :/ / i f ( tok . t t ype != StreamTokenizer .TT_NUMBER && ( tok . sva l . compareTo ( " \ t " ) ! = 0 | | tok . sva l . compareTo ( " " ) ! = 0 ) ) { / / means a newl ine
/ / j =0;/ / i ++;
/ / }break ;
}}
}catch ( java . i o . EOFException e ) {
/ / do noth ing
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
CONTEXT VI
}catch ( java . i o . IOExcept ion e ) {
/ / do noth ing}catch ( java . lang . ArrayIndexOutOfBoundsException e ) {
/ / do noth ing agin : (System . out . p r i n t l n ( e+" Here l i e s the bug "+ t ra inORtes t+ j + i ) ;
/ / System . e x i t ( 0 ) ;}System . out . p r i n t l n ( " Here i s what I read \ n "+ i + j ) ;
for ( i =0; i <2; i ++){for ( j =0; j <3; j ++)
System . out . p r i n t ( curDat [ i ] [ j ]+ " \ t " ) ;System . out . p r i n t l n ( ) ;
}t ry {
tok = new StreamTokenizer (new BufferedReader (new Fi leReader ( f i lenameY ) ) ) ;tok . ord inaryChar ( ’ \ n ’ ) ;/ / tok . ord inaryChar ( )
}catch ( java . i o . Fi leNotFoundExcept ion e ) {
System . out . p r i n t l n ( e+" i n Context . java " ) ;}
i =0;double [ ] DatY=new double [ s izeX ] ;
t ry {j =0;while ( tok . nextToken ( ) ! = StreamTokenizer .TT_EOF ) {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
CONTEXT VII
switch ( tok . t t ype ) {case StreamTokenizer .TT_EOL:
/ / i ++;break ;
/ / case ’ \ n ’ :/ / i ++;
/ / j =0;/ / break ;
case StreamTokenizer .TT_NUMBER:DatY [ i ]= tok . nva l ;i ++;
/ / System . out . p r i n t l n ( tok . nva l ) ;break ;
defaul t :/ / j ++;
break ;}
}}catch ( java . i o . EOFException e ) {
/ / do noth ing}
catch ( java . i o . IOExcept ion e ) {/ / do noth ing
}catch ( java . lang . ArrayIndexOutOfBoundsException e ) {
/ / do noth ing agin : (System . out . p r i n t l n ( e+" Here l i e s the bug " ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
CONTEXT VIII
System . e x i t ( 0 ) ;}
i f ( t ra inORtes t ) {XData=curDat ;Target=DatY ;
}else {
TestXData=curDat ;TestTarget=DatY ;
}/ / f o r ( i n t j j =0; j j <XData . leng th ; j j ++) {
/ / XData [ j j ] [ 1 ]∗=100 ;/ / }
}
public s t a t i c Ar rayL i s t < P r i m i t i v e > mFunctionSet ; / / ! < Set o f f u n c t i o n s usable to b u i l d t rees .public s t a t i c Ar rayL i s t < P r i m i t i v e > mTerminalSet ; / / ! < Set o f t e rm ina l s usable to b u i l d t rees .public s t a t i c TreeMap<St r ing , P r i m i t i v e > mPrimit iveMap ; / / ! < Name−p r i m i t i v e map.public s t a t i c LinkedHashMap<St r ing , Evaluat ion > mCache ;public s t a t i c f l o a t mutationProba , swapMutationProba , xoverProba , in In i tGrowProba , xoverNodeDist r ibProb ;public s t a t i c i n t dynamicTreeDepth , popSize , in i tTreeDepthMin , ini tTreeDepthMax , tournamentSize , maxTreeDepth ;public s t a t i c f l o a t [ ] [ ] XData ;public s t a t i c double [ ] Target ;public s t a t i c f l o a t [ ] [ ] TestXData ;public s t a t i c double [ ] TestTarget ;public s t a t i c S t r i n g t r a i nDa taF i l eX ;public s t a t i c S t r i n g t r a i nDa taF i l eY ;public s t a t i c S t r i n g tes tDa taF i leX ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
CONTEXT IX
public s t a t i c S t r i n g tes tDa taF i leY ;public s t a t i c boolean useTestData ;public s t a t i c i n t l eng th_of_run ;public s t a t i c double prev iousBest ; / / f i t n e s s o f the best i n d i v i d u a l o f the prev ious generat ion
}
class MyLinkedHashMap extends LinkedHashMap {/ / This method i s c a l l e d j u s t a f t e r a new en t ry has been addedpublic MyLinkedHashMap ( i n t val1 , f l o a t val2 , boolean va l3 ) {
super ( val1 , val2 , va l3 ) ;}public boolean removeEldestEntry (Map. Entry e ldes t ) {
return s ize ( ) > 500;}
/∗ p u b l i c boolean containsKey ( S t r i n g key ) {I t e r a t o r <St r ing > s t r = t h i s . keySet ( ) . i t e r a t o r ( ) ;wh i le ( s t r . hasNext ( ) ) {
i f ( s t r . next ( ) . compareTo ( key )==0) {r e t u r n t rue ;
}}r e t u r n f a l s e ;
}p u b l i c Eva lua t ion get ( S t r i n g key ) {
Eva lua t ion eval= n u l l ;I t e r a t o r <Map. Entry > e n t r y I t r = t h i s . en t rySet ( ) . i t e r a t o r ( ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
CONTEXT X
whi le ( e n t r y I t r . hasNext ( ) ) {Map. Entry temp= e n t r y I t r . next ( ) ;i f ( temp . getKey ( ) . t o S t r i n g ( ) . compareTo ( key )==0) {
r e t u r n ( Eva lua t ion ) temp . getValue ( ) ;
}}r e t u r n n u l l ;
} ∗ /}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES I
/∗∗ Pr im i t i veHand le . java∗∗ Created on August 10 , 2007 , 9:02 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 GeneticProgramming . P r i m i t i v e s ;
/∗∗∗∗ @author a d i l r a j a∗ /
import com. sun . org . apache . bce l . i n t e r n a l . c l a s s f i l e . JavaClass ;import java . u t i l .∗ ;import GeneticProgramming .∗ ;import java . i o .∗ ;
public abstract class P r i m i t i v e implements S e r i a l i z a b l e {protected S t r i n g name ;protected i n t a r i t y ;protected i n t mRefCounter ;protected Random dice ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES II
/∗∗ Creates a new ins tance of Pr im i t i veHand le ∗ /public P r i m i t i v e ( ) {}
/∗∗∗ \ param inNumberArguments Number o f arguments o f the p r i m i t i v e .∗ \ param inName Name of the p r i m i t i v e .∗ /
public P r i m i t i v e ( S t r i n g inName , i n t a r i t y ){
th is . name=new S t r i n g ( inName ) ;th is . a r i t y = a r i t y ;mRefCounter =0;
}
/∗∗∗ \ b r i e f Copy−cons t ruc t a p r i m i t i v e .∗ \ param i n R i g h t P r i m i t P r i m i t i v e to copy .∗ /
public P r i m i t i v e ( f i n a l P r i m i t i v e copy ){
th is . name=new S t r i n g ( copy . name ) ;th is . a r i t y =copy . a r i t y ;mRefCounter =0;
}
/∗∗∗Assign the i npu t argument ( P r i m i t i v e ob jec t ) to
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES III
∗ t h i s ob jec t∗ /
public P r i m i t i v e assign ( f i n a l P r i m i t i v e p r i m i t i v e ){
i f ( th is == p r i m i t i v e ) return th is ;name = new S t r i n g ( p r i m i t i v e . name ) ;th is . a r i t y = p r i m i t i v e . a r i t y ;return th is ;
}
/∗∗∗ \ b r i e f Give a re ference on the ac tua l p r i m i t i v e .∗ \ param ob jec t o f Random∗ \ r e t u r n P r i m i t i v e handle to t h i s p o i n t e r .∗ /
public abstract P r i m i t i v e giveReference (Random dice ) ; / / { r e t u r n n u l l ; }/∗ {
i f ( t h i s . getName ( ) . equalsIgnoreCase ( " Ephemeral " ) ) { / / should r e t u r n the Ephemeral random number ins teadr e t u r n new P r i m i t i v e ( java . lang . Double . t o S t r i n g ( d ice . nextDouble ()∗12 −6) ,0);
}r e t u r n t h i s ;
} ∗ /
/∗∗∗ \ b r i e f Set the name of the p r i m i t i v e .∗ \ param inName Name of the p r i m i t i v e .∗ /
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES IV
public void setName ( S t r i n g inName ){
th is . name =new S t r i n g ( inName ) ;}
/∗∗∗ r e t u r n the name of t h i s p r i m i t i v e∗ /
public f i n a l S t r i n g getName ( ) {return th is . name ;
}
/∗∗∗ \ b r i e f Set the number o f arguments o f the p r i m i t i v e .∗ \ param inNumberArguments Number o f arguments o f the p r i m i t i v e .∗ /
public void s e t A r i t y ( i n t i n A r i t y ){
th is . a r i t y = i n A r i t y ;}
/∗ !∗ \ b r i e f Set the value o f the p r i m i t i v e ( do noth ing f o r bas ic p r i m i t i v e ) .∗ \ param inValue New value to use .∗ /
/ / vo id Puppy : : P r i m i t i v e : : setValue ( const vo id∗ inValue )/ / { }
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES V
/∗∗∗Returns the a r i t y o f t h i s p r i m i t i v e∗ /
public f i n a l i n t g e t A r i t y ( ) {return th is . a r i t y ;
}
/∗∗∗ \ b r i e f Get the value o f the nth argument .∗ \ param inN Index of the argument to get .∗ \ param outResu l t Value o f the nth argument .∗ \ param ioContex t Evo lu t i ona ry contex t .∗ XData∗ patternNumber∗ /
public double [ ] getArgument ( i n t inN , A r rayL i s t < In teger > ca l lS tack , Tree t ree ,f i n a l f l o a t [ ] [ ] XData )
{asser t ( inN >= 0 ) ;asser t ( inN < th is . g e t A r i t y ( ) ) ;/ /
/ /i n t l I ndex = c a l lS t a c k . get ( c a l l S t a c k . s ize () −1). i n tVa lue ( ) + 1 ;for ( i n t i =0; i <inN ; ++ i ) l I ndex += t ree . get ( l I ndex ) . getSubTreeSize ( ) ;c a l l S ta c k . add (new I n t ege r ( l I ndex ) ) ;double [ ] ou tResu l t= t r ee . get ( l I ndex ) . g e t P r i m i t i v e ( ) . execute ( ca l lS tack , t ree , XData ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES VI
c a l l S ta c k . remove ( c a l l S t ac k . s ize () −1);return outResu l t ;
}public abstract P r i m i t i v e clone ( ) ;/∗∗∗Funct ion i s used to execute t h i s p r i m i t i v e∗outResu l t∗c a l l S ta c k∗ t r ee to which t h i s p r i m i t i v e belongs∗XData∗patternNumber , t h a t comes i n from evaluateTree i n Tree∗ /
/ / p u b l i c abs t r ac t double [ ] execute ( A r rayL i s t < In teger > ca l lS tack , Tree t ree , f l o a t [ ] [ ] XData ) ;public abstract double [ ] execute ( A r rayL i s t < In teger > ca l lS tack , Tree t ree , f l o a t [ ] [ ] XData ) ; / / { r e t u r n n u l l ; }
/ / c a l l the cache here?/∗ S t r i n g B u i l d e r key=new S t r i n g B u i l d e r ( t r ee . s ize ()− c a l l S t ac k . get ( c a l l S t ac k . s i ze () −1). i n tVa lue ( ) ) ;
t r ee . wr i teTree2 ( c a l l S t a c k . get ( c a l l S t a c k . s ize () −1). i n tVa lue ( ) , key ) ;Eva lua t ion eva lua t i on=PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) ;i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . on ly non−t e rm ina l s are evaluated here
i f ( eva lua t i on != n u l l ) {/ / double [ ] evaled= lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . eva lua t i on ;
/ / f o r ( i n t i =0; i <evaled . leng th ; i ++)/ / ou tResu l t [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . cachedFlag= t rue ; / / the en t ry i s now cached , even i f i t was not p rev ious l yPuppysMain . cacheHits+= t ree . get ( c a l lS t a c k . s ize () −1). getSubTreeSize ( ) ; ;
r e t u r n eva lua t i on . eva lua t i on ;}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES VII
}∗ /
/ / double lArg2 ;/∗ double [ ] ou tResu l t= n u l l ;
sw i tch ( t h i s . a r i t y ) {case 2 : { / / f o r a l l the two a r i t y f u n c t i o n sdouble [ ] lArg2= n u l l ;ou tResu l t=getArgument (0 , ca l lS tack , t ree , XData ) ;lArg2=getArgument (1 , ca l lS tack , t ree , XData ) ;i f ( t h i s . getName ( ) . compareTo ( " p lus " ) = = 0 ) {
i f ( ou tResu l t . l eng th== lArg2 . leng th )f o r ( i n t i =outResu l t . l eng th;−−i >=0; )
outResu l t [ i ] += lArg2 [ i ] ;e lse i f ( ou tResu l t . l eng th = = 1 ) { / / i . e . a te rm ina l re tu rned
f o r ( i n t i = lArg2 . leng th;−−i >=0; )lArg2 [ i ]+= outResu l t [ 0 ] ;
ou tResu l t= lArg2 ; / / po i n t outResu l t to t h i s ar ray now}e lse i f ( lArg2 . leng th ==1){
f o r ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ]+= lArg2 [ 0 ] ;
}}e lse i f ( t h i s . getName ( ) . compareTo ( " minus " ) = = 0 ) {
i f ( ou tResu l t . l eng th== lArg2 . leng th )f o r ( i n t i =outResu l t . l eng th;−−i >=0; )
outResu l t [ i ] −= lArg2 [ i ] ;e lse i f ( ou tResu l t . l eng th = = 1 ) { / / i . e . a te rm ina l re tu rned
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES VIII
f o r ( i n t i = lArg2 . leng th;−−i >=0; )lArg2 [ i ]−=outResu l t [ 0 ] ;
ou tResu l t= lArg2 ; / / po i n t outResu l t to t h i s ar ray now}e lse i f ( lArg2 . leng th ==1){
f o r ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ]−=lArg2 [ 0 ] ;
}}
e lse i f ( t h i s . getName ( ) . compareTo ( " t imes " ) = = 0 ) {i f ( ou tResu l t . l eng th== lArg2 . leng th )
f o r ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ] ∗= lArg2 [ i ] ;
e lse i f ( ou tResu l t . l eng th = = 1 ) { / / i . e . a te rm ina l re tu rnedf o r ( i n t i = lArg2 . leng th;−−i >=0; )
lArg2 [ i ]∗= outResu l t [ 0 ] ;ou tResu l t= lArg2 ; / / po i n t outResu l t to t h i s ar ray now
}e lse i f ( lArg2 . leng th ==1){
f o r ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ]∗= lArg2 [ 0 ] ;
}}
e lse i f ( t h i s . getName ( ) . compareTo ( " ad i lpower " ) = = 0 ) {i f ( ou tResu l t . l eng th== lArg2 . leng th )
f o r ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ] =Math . pow( outResu l t [ i ] , lArg2 [ i ] ) ;
e lse i f ( ou tResu l t . l eng th = = 1 ) { / / i . e . a te rm ina l re tu rned
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES IX
f o r ( i n t i = lArg2 . leng th;−−i >=0; )lArg2 [ i ]= Math . pow( lArg2 [ i ] , ou tResu l t [ 0 ] ) ;
ou tResu l t= lArg2 ; / / po i n t outResu l t to t h i s ar ray now}e lse i f ( lArg2 . leng th ==1){
f o r ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ]= Math . pow( outResu l t [ i ] , lArg2 [ 0 ] ) ;
}}e lse { / / d i v i d e
i f ( ou tResu l t . l eng th== lArg2 . leng th ) {f o r ( i n t i =outResu l t . l eng th;−−i >=0 ; ) {
t r y {outResu l t [ i ] /= lArg2 [ i ] ;
}catch ( Except ion e ) {
outResu l t [ i ]= Double .NaN;}
}}e lse i f ( ou tResu l t . l eng th = = 1 ) { / / i . e . a te rm ina l re tu rned
f o r ( i n t i = lArg2 . leng th;−−i >=0 ; ) {t r y {
lArg2 [ i ]+= outResu l t [ 0 ] ;}catch ( Except ion e ) {
outResu l t [ i ]= Double .NaN;}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES X
}ou tResu l t= lArg2 ; / / po i n t outResu l t to t h i s ar ray now
}e lse i f ( lArg2 . leng th ==1){
f o r ( i n t i =outResu l t . l eng th;−−i >=0 ; ) {t r y {
outResu l t [ i ]+= lArg2 [ 0 ] ;}catch ( Except ion e ) {
outResu l t [ i ]= Double .NaN;}
}}
}break ;
}case 1 : {
outResu l t=getArgument (0 , ca l lS tack , t ree , XData ) ;i f ( t h i s . name . compareTo ( " s in " ) = = 0 ) / / s in
f o r ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ] = ( Math . s in ( outResu l t [ i ] ) ) ;
e lse i f ( t h i s . name . compareTo ( " cos " ) = = 0 ) / / cosf o r ( i n t i =outResu l t . l eng th;−−i >=0; )
outResu l t [ i ] = ( Math . cos ( outResu l t [ i ] ) ) ;e lse i f ( t h i s . name . compareTo ( " a d i l l o g " )==0)
f o r ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ] = ( Math . log ( outResu l t [ i ] ) ) ;
e lse i f ( t h i s . name . compareTo ( " a d i l l o g1 0 " )==0)
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XI
f o r ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ] = ( Math . log10 ( outResu l t [ i ] ) ) ;
e lse i f ( t h i s . name . compareTo ( " a d i l s q r t " ) = = 0 ) {f o r ( i n t i =outResu l t . l eng th;−−i >=0; )
outResu l t [ i ]= Math . s q r t ( outResu l t [ i ] ) ;}break ;
}d e f a u l t : { / / A te rm ina l has been reachedi f ( ! t h i s . name . s t a r t s W i t h ( "X " ) ) {/ / double tmpVal=Double . valueOf ( t h i s . name ) . doubleValue ( ) ;
/ / f o r ( i n t i =0; i <outResu l t . l eng th ; i ++)/ / ou tResu l t [ i ]= tmpVal ;outResu l t=new double [ 1 ] ;ou tResu l t [ 0 ] = Double . valueOf ( t h i s . name ) . doubleValue ( ) ;
}e lse i f ( t h i s . name . subs t r i ng ( 0 , 1 ) . compareTo ( "X" ) = = 0 ) {
/ / The f o l l o w i n g l i n e would a u t o m a t i c a l l y assign the p e r t i n e n t en t ry i n the pa t t e rn to the r e s u l t to be sent out .i n t tmp= In tege r . valueOf ( t h i s . name . subs t r i ng ( 1 ) ) . i n tVa lue ()−1;outResu l t=new double [ XData . leng th ] ;f o r ( i n t i =outResu l t . l eng th;−−i >=0; )
outResu l t [ i ] = ( double ) XData [ i ] [ tmp ] ; / / Return the whole ar ray o f the Inpu t data , t h i s should save some data .}break ;
}}/∗ PuppysMain . noCacheHits ++;
i f ( eva lua t i on == n u l l ) { / / i . e . i f the en t ry i s not present
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XII
i f ( t h i s . g e t A r i t y ( ) = = 0 ) {lCon tex t . mCache . put ( key . t o S t r i n g ( ) , new Eva lua t ion ( ) ) ; / / c a l l the d e f a u l t cons t r uc to r o f Eva lua t ion/ / but the ac tua l eva lua t i on i s done here . Aimed at saving ex t ra array−copying
}e lse { / / the t ree has c h i l d ( ren ) .
i n t l I ndex = c a l l S ta c k . get ( c a l l S t ac k . s ize () −1). i n tVa lue ( ) + 1 ; / / index o f the next subtreeboolean hasCached= t rue ; / / t h a t the subtree i s cahchedf o r ( i n t i =0; i < t h i s . a r i t y ; i ++){
S t r i n g B u i l d e r subTree=new S t r i n g B u i l d e r ( t r ee . s ize ()− l I ndex ) ;t r ee . wr i teTree2 ( l Index , subTree ) ;S t r i n g s t r =new S t r i n g ( subTree . t o S t r i n g ( ) ) ;Eva lua t ion eva lua t ion2= lCon tex t . mCache . get ( s t r ) ;t r y {
i f ( eva lua t ion2 != n u l l )i f ( eva lua t ion2 . cachedFlag== f a l s e )
hasCached= f a l s e ;/ / System . out . p r i n t l n ( " Alarming " ) ;}catch ( Except ion e ) {
hasCached= f a l s e ;System . out . p r i n t l n ( " Do I reach here? "+e ) ;
/ / System . e x i t ( 0 ) ;}l I ndex += t ree . get ( l I ndex ) . getSubTreeSize ( ) ;
}i f ( hasCached ) { / / That i s hasCached surv ived through a l l the subChi ldren
lCon tex t . mCache . put (new S t r i n g ( key . t o S t r i n g ( ) ) , new Eva lua t ion ( outResul t , f a l s e ) ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XIII
}}
} ∗ // / r e t u r n outResu l t ;
/ / }
}
/∗∗ Ephemeral . java∗∗ Created on September 27 , 2007 , 11:49 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 GeneticProgramming . P r i m i t i v e s . Terminals ;
/∗∗∗∗ @author a d i l r a j a∗ /
import GeneticProgramming .∗ ;import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;import java . u t i l .∗ ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XIV
public class Ephemeral extends P r i m i t i v e {
/∗∗ Creates a new ins tance of Ephemeral ∗ /public Ephemeral ( ) {}/∗∗
∗ \ param inNumberArguments Number o f arguments o f the p r i m i t i v e .∗ \ param inName Name of the p r i m i t i v e .∗ /
public Ephemeral ( S t r i n g inName , i n t a r i t y ) {super ( inName , a r i t y ) ;
}/∗∗
∗ \ b r i e f Copy−cons t ruc t a p r i m i t i v e .∗ \ param i n R i g h t P r i m i t P r i m i t i v e to copy .∗ /
public Ephemeral ( f i n a l Ephemeral copy ){
super ( copy ) ;/ / t h i s . name=new S t r i n g ( copy . name ) ;/ / t h i s . a r i t y =copy . a r i t y ;/ / mRefCounter =0;}
/∗∗∗ \ b r i e f Give a re ference on the ac tua l p r i m i t i v e .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XV
∗ \ param ob jec t o f Random∗ \ r e t u r n P r i m i t i v e handle to t h i s p o i n t e r .∗ /
public P r i m i t i v e giveReference (Random dice ){
th is . name=java . lang . Double . t o S t r i n g ( d ice . nextDouble ()∗12−6);return th is ;
}
/∗∗∗Funct ion i s used to execute t h i s p r i m i t i v e∗outResu l t∗c a l l S ta c k∗ t r ee to which t h i s p r i m i t i v e belongs∗XData∗patternNumber , t h a t comes i n from evaluateTree i n Tree∗ /
public double [ ] execute ( A r rayL i s t < In teger > ca l lS tack , Tree t ree , f l o a t [ ] [ ] XData ) {/ / c a l l the cache here?
/∗ S t r i n g B u i l d e r key=new S t r i n g B u i l d e r ( t r ee . s ize ()− c a l l S t ac k . get ( c a l l S t ac k . s i ze () −1). i n tVa lue ( ) ) ;t r ee . wr i teTree2 ( c a l l S t a c k . get ( c a l l S t a c k . s ize () −1). i n tVa lue ( ) , key ) ;Eva lua t ion eva lua t i on=PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) ;i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . on ly non−t e rm ina l s are evaluated here
i f ( eva lua t i on != n u l l ) {/ / double [ ] evaled= lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . eva lua t i on ;
/ / f o r ( i n t i =0; i <evaled . leng th ; i ++)/ / ou tResu l t [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . cachedFlag= t rue ; / / the en t ry i s now cached , even i f i t was not p rev ious l y
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XVI
PuppysMain . cacheHits+= t ree . get ( c a l lS t a c k . s ize () −1). getSubTreeSize ( ) ; ;
r e t u r n eva lua t i on . eva lua t i on ;}
}∗ /
/ / double lArg2 ;/ / double tmpVal=Double . valueOf ( t h i s . name ) . doubleValue ( ) ;
/ / f o r ( i n t i =0; i <outResu l t . l eng th ; i ++)/ / ou tResu l t [ i ]= tmpVal ;double [ ] ou tResu l t=new double [ 1 ] ;ou tResu l t [ 0 ] = Double . valueOf ( th is . name ) . doubleValue ( ) ;
/∗ PuppysMain . noCacheHits ++;i f ( eva lua t i on == n u l l ) { / / i . e . i f the en t ry i s not present
i f ( t h i s . g e t A r i t y ( ) = = 0 ) {lCon tex t . mCache . put ( key . t o S t r i n g ( ) , new Eva lua t ion ( ) ) ; / / c a l l the d e f a u l t cons t r uc to r o f Eva lua t ion/ / but the ac tua l eva lua t i on i s done here . Aimed at saving ex t ra array−copying
}e lse { / / the t ree has c h i l d ( ren ) .
i n t l I ndex = c a l l S ta c k . get ( c a l l S t ac k . s ize () −1). i n tVa lue ( ) + 1 ; / / index o f the next subtreeboolean hasCached= t rue ; / / t h a t the subtree i s cahchedf o r ( i n t i =0; i < t h i s . a r i t y ; i ++){
S t r i n g B u i l d e r subTree=new S t r i n g B u i l d e r ( t r ee . s ize ()− l I ndex ) ;t r ee . wr i teTree2 ( l Index , subTree ) ;S t r i n g s t r =new S t r i n g ( subTree . t o S t r i n g ( ) ) ;Eva lua t ion eva lua t ion2= lCon tex t . mCache . get ( s t r ) ;t r y {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XVII
i f ( eva lua t ion2 != n u l l )i f ( eva lua t ion2 . cachedFlag== f a l s e )
hasCached= f a l s e ;/ / System . out . p r i n t l n ( " Alarming " ) ;}catch ( Except ion e ) {
hasCached= f a l s e ;System . out . p r i n t l n ( " Do I reach here? "+e ) ;
/ / System . e x i t ( 0 ) ;}l I ndex += t ree . get ( l I ndex ) . getSubTreeSize ( ) ;
}i f ( hasCached ) { / / That i s hasCached surv ived through a l l the subChi ldren
lCon tex t . mCache . put (new S t r i n g ( key . t o S t r i n g ( ) ) , new Eva lua t ion ( outResul t , f a l s e ) ) ;}
}
} ∗ /return outResu l t ;
}public Ephemeral c lone ( ) {
return (new Ephemeral ( th is ) ) ;}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XVIII
/∗∗ X1 . java∗∗ Created on September 27 , 2007 , 11:49 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 GeneticProgramming . P r i m i t i v e s . Terminals ;
/∗∗∗∗ @author a d i l r a j a∗ /
import GeneticProgramming .∗ ;import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;import java . u t i l .∗ ;
public class X1 extends P r i m i t i v e {
/∗∗ Creates a new ins tance of X1 ∗ /public X1 ( ) {}/∗∗
∗ \ param inNumberArguments Number o f arguments o f the p r i m i t i v e .∗ \ param inName Name of the p r i m i t i v e .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XIX
∗ /public X1( S t r i n g inName , i n t a r i t y ) {
super ( inName , a r i t y ) ;}/∗∗
∗ \ b r i e f Copy−cons t ruc t a p r i m i t i v e .∗ \ param i n R i g h t P r i m i t P r i m i t i v e to copy .∗ /
public X1( f i n a l X1 copy ){
super ( copy ) ;/ / t h i s . name=new S t r i n g ( copy . name ) ;/ / t h i s . a r i t y =copy . a r i t y ;/ / mRefCounter =0;}
/∗∗∗ \ b r i e f Give a re ference on the ac tua l p r i m i t i v e .∗ \ param ob jec t o f Random∗ \ r e t u r n P r i m i t i v e handle to t h i s p o i n t e r .∗ /
public X1 giveReference (Random dice ){/ / i f ( t h i s . getName ( ) . equalsIgnoreCase ( " Ephemeral " ) ) { / / should r e t u r n the Ephemeral random number ins tead
/ / r e t u r n new P r i m i t i v e ( java . lang . Double . t o S t r i n g ( d ice . nextDouble ()∗12 −6) ,0);/ / }return th is ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XX
}
public double [ ] execute ( A r rayL i s t < In teger > ca l lS tack , Tree t ree , f l o a t [ ] [ ] XData ) {/ / c a l l the cache here?
/∗ S t r i n g B u i l d e r key=new S t r i n g B u i l d e r ( t r ee . s ize ()− c a l l S t ac k . get ( c a l l S t ac k . s i ze () −1). i n tVa lue ( ) ) ;t r ee . wr i teTree2 ( c a l l S t a c k . get ( c a l l S t a c k . s ize () −1). i n tVa lue ( ) , key ) ;Eva lua t ion eva lua t i on=PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) ;i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . on ly non−t e rm ina l s are evaluated here
i f ( eva lua t i on != n u l l ) {/ / double [ ] evaled= lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . eva lua t i on ;
/ / f o r ( i n t i =0; i <evaled . leng th ; i ++)/ / ou tResu l t [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . cachedFlag= t rue ; / / the en t ry i s now cached , even i f i t was not p rev ious l yPuppysMain . cacheHits+= t ree . get ( c a l lS t a c k . s ize () −1). getSubTreeSize ( ) ; ;
r e t u r n eva lua t i on . eva lua t i on ;}
}∗ /
/ / double lArg2 ;
/ / The f o l l o w i n g l i n e would a u t o m a t i c a l l y assign the p e r t i n e n t en t ry i n the pa t t e rn to the r e s u l t to be sent out .double [ ] ou tResu l t=new double [ XData . leng th ] ;for ( i n t i =outResu l t . l eng th;−−i >=0; )
outResu l t [ i ] = ( double ) XData [ i ] [ 0 ] ; / / Return the whole ar ray o f the Inpu t data , t h i s should save some data ./∗ PuppysMain . noCacheHits ++;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XXI
i f ( eva lua t i on == n u l l ) { / / i . e . i f the en t ry i s not presenti f ( t h i s . g e t A r i t y ( ) = = 0 ) {
lCon tex t . mCache . put ( key . t o S t r i n g ( ) , new Eva lua t ion ( ) ) ; / / c a l l the d e f a u l t cons t r uc to r o f Eva lua t ion/ / but the ac tua l eva lua t i on i s done here . Aimed at saving ex t ra array−copying
}e lse { / / the t ree has c h i l d ( ren ) .
i n t l I ndex = c a l l S ta c k . get ( c a l l S t ac k . s ize () −1). i n tVa lue ( ) + 1 ; / / index o f the next subtreeboolean hasCached= t rue ; / / t h a t the subtree i s cahchedf o r ( i n t i =0; i < t h i s . a r i t y ; i ++){
S t r i n g B u i l d e r subTree=new S t r i n g B u i l d e r ( t r ee . s ize ()− l I ndex ) ;t r ee . wr i teTree2 ( l Index , subTree ) ;S t r i n g s t r =new S t r i n g ( subTree . t o S t r i n g ( ) ) ;Eva lua t ion eva lua t ion2= lCon tex t . mCache . get ( s t r ) ;t r y {
i f ( eva lua t ion2 != n u l l )i f ( eva lua t ion2 . cachedFlag== f a l s e )
hasCached= f a l s e ;/ / System . out . p r i n t l n ( " Alarming " ) ;}catch ( Except ion e ) {
hasCached= f a l s e ;System . out . p r i n t l n ( " Do I reach here? "+e ) ;
/ / System . e x i t ( 0 ) ;}l I ndex += t ree . get ( l I ndex ) . getSubTreeSize ( ) ;
}i f ( hasCached ) { / / That i s hasCached surv ived through a l l the subChi ldren
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XXII
lCon tex t . mCache . put (new S t r i n g ( key . t o S t r i n g ( ) ) , new Eva lua t ion ( outResul t , f a l s e ) ) ;}
}
} ∗ /return outResu l t ;
}public X1 clone ( ) {
return (new X1( th is ) ) ;}
}
/∗∗ X1 . java∗∗ Created on September 27 , 2007 , 11:49 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 GeneticProgramming . P r i m i t i v e s . Terminals ;
/∗∗∗∗ @author a d i l r a j a
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XXIII
∗ /import GeneticProgramming .∗ ;import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;import java . u t i l .∗ ;
public class X2 extends P r i m i t i v e {
/∗∗ Creates a new ins tance of X1 ∗ /public X2 ( ) {}/∗∗
∗ \ param inNumberArguments Number o f arguments o f the p r i m i t i v e .∗ \ param inName Name of the p r i m i t i v e .∗ /
public X2( S t r i n g inName , i n t a r i t y ) {super ( inName , a r i t y ) ;
}/∗∗
∗ \ b r i e f Copy−cons t ruc t a p r i m i t i v e .∗ \ param i n R i g h t P r i m i t P r i m i t i v e to copy .∗ /
public X2( f i n a l X2 copy ){
super ( copy ) ;/ / t h i s . name=new S t r i n g ( copy . name ) ;/ / t h i s . a r i t y =copy . a r i t y ;/ / mRefCounter =0;}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XXIV
/∗∗∗ \ b r i e f Give a re ference on the ac tua l p r i m i t i v e .∗ \ param ob jec t o f Random∗ \ r e t u r n P r i m i t i v e handle to t h i s p o i n t e r .∗ /
public X2 giveReference (Random dice ){/ / i f ( t h i s . getName ( ) . equalsIgnoreCase ( " Ephemeral " ) ) { / / should r e t u r n the Ephemeral random number ins tead
/ / r e t u r n new P r i m i t i v e ( java . lang . Double . t o S t r i n g ( d ice . nextDouble ()∗12 −6) ,0);/ / }return th is ;
}
public double [ ] execute ( A r rayL i s t < In teger > ca l lS tack , Tree t ree , f l o a t [ ] [ ] XData ) {/ / c a l l the cache here?
/∗ S t r i n g B u i l d e r key=new S t r i n g B u i l d e r ( t r ee . s ize ()− c a l l S t ac k . get ( c a l l S t ac k . s i ze () −1). i n tVa lue ( ) ) ;t r ee . wr i teTree2 ( c a l l S t a c k . get ( c a l l S t a c k . s ize () −1). i n tVa lue ( ) , key ) ;Eva lua t ion eva lua t i on=PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) ;i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . on ly non−t e rm ina l s are evaluated here
i f ( eva lua t i on != n u l l ) {/ / double [ ] evaled= lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . eva lua t i on ;
/ / f o r ( i n t i =0; i <evaled . leng th ; i ++)/ / ou tResu l t [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . cachedFlag= t rue ; / / the en t ry i s now cached , even i f i t was not p rev ious l yPuppysMain . cacheHits+= t ree . get ( c a l lS t a c k . s ize () −1). getSubTreeSize ( ) ; ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XXV
r e t u r n eva lua t i on . eva lua t i on ;}
}∗ /
/ / double lArg2 ;
/ / The f o l l o w i n g l i n e would a u t o m a t i c a l l y assign the p e r t i n e n t en t ry i n the pa t t e rn to the r e s u l t to be sent out .double [ ] ou tResu l t=new double [ XData . leng th ] ;for ( i n t i =outResu l t . l eng th;−−i >=0; )
outResu l t [ i ] = ( double ) XData [ i ] [ 1 ] ; / / Return the whole ar ray o f the Inpu t data , t h i s should save some data ./∗ PuppysMain . noCacheHits ++;
i f ( eva lua t i on == n u l l ) { / / i . e . i f the en t ry i s not presenti f ( t h i s . g e t A r i t y ( ) = = 0 ) {
lCon tex t . mCache . put ( key . t o S t r i n g ( ) , new Eva lua t ion ( ) ) ; / / c a l l the d e f a u l t cons t r uc to r o f Eva lua t ion/ / but the ac tua l eva lua t i on i s done here . Aimed at saving ex t ra array−copying
}e lse { / / the t ree has c h i l d ( ren ) .
i n t l I ndex = c a l l S ta c k . get ( c a l l S t ac k . s ize () −1). i n tVa lue ( ) + 1 ; / / index o f the next subtreeboolean hasCached= t rue ; / / t h a t the subtree i s cahchedf o r ( i n t i =0; i < t h i s . a r i t y ; i ++){
S t r i n g B u i l d e r subTree=new S t r i n g B u i l d e r ( t r ee . s ize ()− l I ndex ) ;t r ee . wr i teTree2 ( l Index , subTree ) ;S t r i n g s t r =new S t r i n g ( subTree . t o S t r i n g ( ) ) ;Eva lua t ion eva lua t ion2= lCon tex t . mCache . get ( s t r ) ;t r y {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
PRIMITIVES XXVI
i f ( eva lua t ion2 != n u l l )i f ( eva lua t ion2 . cachedFlag== f a l s e )
hasCached= f a l s e ;/ / System . out . p r i n t l n ( " Alarming " ) ;}catch ( Except ion e ) {
hasCached= f a l s e ;System . out . p r i n t l n ( " Do I reach here? "+e ) ;
/ / System . e x i t ( 0 ) ;}l I ndex += t ree . get ( l I ndex ) . getSubTreeSize ( ) ;
}i f ( hasCached ) { / / That i s hasCached surv ived through a l l the subChi ldren
lCon tex t . mCache . put (new S t r i n g ( key . t o S t r i n g ( ) ) , new Eva lua t ion ( outResul t , f a l s e ) ) ;}
}
} ∗ /return outResu l t ;
}
public X2 clone ( ) {return (new X2( th is ) ) ;
}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS I
/∗∗ Adi lLog . java∗∗ Created on September 27 , 2007 , 11:49 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 GeneticProgramming . P r i m i t i v e s . Funct ions ;
/∗∗∗∗ @author a d i l r a j a∗ /
import GeneticProgramming .∗ ;import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;import java . u t i l .∗ ;
public class Adi lLog extends P r i m i t i v e {
/∗∗ Creates a new ins tance of X1 ∗ /public Adi lLog ( ) {}/∗∗
∗ \ param inNumberArguments Number o f arguments o f the p r i m i t i v e .∗ \ param inName Name of the p r i m i t i v e .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS II
∗ /public Adi lLog ( S t r i n g inName , i n t a r i t y ) {
super ( inName , a r i t y ) ;}/∗∗
∗ \ b r i e f Copy−cons t ruc t a p r i m i t i v e .∗ \ param i n R i g h t P r i m i t P r i m i t i v e to copy .∗ /
public Adi lLog ( f i n a l Adi lLog copy ){
super ( copy ) ;/ / t h i s . name=new S t r i n g ( copy . name ) ;/ / t h i s . a r i t y =copy . a r i t y ;/ / mRefCounter =0;}
/∗∗∗ \ b r i e f Give a re ference on the ac tua l p r i m i t i v e .∗ \ param ob jec t o f Random∗ \ r e t u r n P r i m i t i v e handle to t h i s p o i n t e r .∗ /
public Adi lLog giveReference (Random dice ){/ / i f ( t h i s . getName ( ) . equalsIgnoreCase ( " Ephemeral " ) ) { / / should r e t u r n the Ephemeral random number ins tead
/ / r e t u r n new P r i m i t i v e ( java . lang . Double . t o S t r i n g ( d ice . nextDouble ()∗12 −6) ,0);/ / }return th is ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS III
}
/∗∗∗Funct ion i s used to execute t h i s p r i m i t i v e∗outResu l t∗c a l l S ta c k∗ t r ee to which t h i s p r i m i t i v e belongs∗XData∗patternNumber , t h a t comes i n from evaluateTree i n Tree∗ /
public double [ ] execute ( A r rayL i s t < In teger > ca l lS tack , Tree t ree , f l o a t [ ] [ ] XData ) {/ / c a l l the cache here?
/∗ S t r i n g B u i l d e r key=new S t r i n g B u i l d e r ( t r ee . s ize ()− c a l l S t ac k . get ( c a l l S t ac k . s i ze () −1). i n tVa lue ( ) ) ;t r ee . wr i teTree2 ( c a l l S t a c k . get ( c a l l S t a c k . s ize () −1). i n tVa lue ( ) , key ) ;Eva lua t ion eva lua t i on=PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) ;i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . on ly non−t e rm ina l s are evaluated here
i f ( eva lua t i on != n u l l ) {/ / double [ ] evaled= lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . eva lua t i on ;
/ / f o r ( i n t i =0; i <evaled . leng th ; i ++)/ / ou tResu l t [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . cachedFlag= t rue ; / / the en t ry i s now cached , even i f i t was not p rev ious l yPuppysMain . cacheHits+= t ree . get ( c a l lS t a c k . s ize () −1). getSubTreeSize ( ) ; ;
r e t u r n eva lua t i on . eva lua t i on ;}
}∗ /
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS IV
/ / double lArg2 ;double [ ] ou tResu l t=nul l ;ou tResu l t=getArgument (0 , ca l lS tack , t ree , XData ) ;
for ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ] = ( Math . log ( outResu l t [ i ] ) ) ;
/∗ PuppysMain . noCacheHits ++;i f ( eva lua t i on == n u l l ) { / / i . e . i f the en t ry i s not present
i f ( t h i s . g e t A r i t y ( ) = = 0 ) {lCon tex t . mCache . put ( key . t o S t r i n g ( ) , new Eva lua t ion ( ) ) ; / / c a l l the d e f a u l t cons t r uc to r o f Eva lua t ion/ / but the ac tua l eva lua t i on i s done here . Aimed at saving ex t ra array−copying
}e lse { / / the t ree has c h i l d ( ren ) .
i n t l I ndex = c a l l S ta c k . get ( c a l l S t ac k . s ize () −1). i n tVa lue ( ) + 1 ; / / index o f the next subtreeboolean hasCached= t rue ; / / t h a t the subtree i s cahchedf o r ( i n t i =0; i < t h i s . a r i t y ; i ++){
S t r i n g B u i l d e r subTree=new S t r i n g B u i l d e r ( t r ee . s ize ()− l I ndex ) ;t r ee . wr i teTree2 ( l Index , subTree ) ;S t r i n g s t r =new S t r i n g ( subTree . t o S t r i n g ( ) ) ;Eva lua t ion eva lua t ion2= lCon tex t . mCache . get ( s t r ) ;t r y {
i f ( eva lua t ion2 != n u l l )i f ( eva lua t ion2 . cachedFlag== f a l s e )
hasCached= f a l s e ;/ / System . out . p r i n t l n ( " Alarming " ) ;}catch ( Except ion e ) {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS V
hasCached= f a l s e ;System . out . p r i n t l n ( " Do I reach here? "+e ) ;
/ / System . e x i t ( 0 ) ;}l I ndex += t ree . get ( l I ndex ) . getSubTreeSize ( ) ;
}i f ( hasCached ) { / / That i s hasCached surv ived through a l l the subChi ldren
lCon tex t . mCache . put (new S t r i n g ( key . t o S t r i n g ( ) ) , new Eva lua t ion ( outResul t , f a l s e ) ) ;}
}
} ∗ /return outResu l t ;
}public Adi lLog clone ( ) {
return (new Adi lLog ( th is ) ) ;}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS VI
/∗∗ AdilPower . java∗∗ Created on September 27 , 2007 , 11:49 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 GeneticProgramming . P r i m i t i v e s . Funct ions ;
/∗∗∗∗ @author a d i l r a j a∗ /
import GeneticProgramming .∗ ;import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;import java . u t i l .∗ ;
public class AdilPower extends P r i m i t i v e {
/∗∗ Creates a new ins tance of X1 ∗ /public AdilPower ( ) {}/∗∗
∗ \ param inNumberArguments Number o f arguments o f the p r i m i t i v e .∗ \ param inName Name of the p r i m i t i v e .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS VII
∗ /public AdilPower ( S t r i n g inName , i n t a r i t y ) {
super ( inName , a r i t y ) ;}/∗∗
∗ \ b r i e f Copy−cons t ruc t a p r i m i t i v e .∗ \ param i n R i g h t P r i m i t P r i m i t i v e to copy .∗ /
public AdilPower ( f i n a l AdilPower copy ){
super ( copy ) ;/ / t h i s . name=new S t r i n g ( copy . name ) ;/ / t h i s . a r i t y =copy . a r i t y ;/ / mRefCounter =0;}
/∗∗∗ \ b r i e f Give a re ference on the ac tua l p r i m i t i v e .∗ \ param ob jec t o f Random∗ \ r e t u r n P r i m i t i v e handle to t h i s p o i n t e r .∗ /
public AdilPower giveReference (Random dice ){/ / i f ( t h i s . getName ( ) . equalsIgnoreCase ( " Ephemeral " ) ) { / / should r e t u r n the Ephemeral random number ins tead
/ / r e t u r n new P r i m i t i v e ( java . lang . Double . t o S t r i n g ( d ice . nextDouble ()∗12 −6) ,0);/ / }return th is ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS VIII
}
/∗∗∗Funct ion i s used to execute t h i s p r i m i t i v e∗outResu l t∗c a l l S ta c k∗ t r ee to which t h i s p r i m i t i v e belongs∗XData∗patternNumber , t h a t comes i n from evaluateTree i n Tree∗ /
public double [ ] execute ( A r rayL i s t < In teger > ca l lS tack , Tree t ree , f l o a t [ ] [ ] XData ) {/ / c a l l the cache here?
/∗ S t r i n g B u i l d e r key=new S t r i n g B u i l d e r ( t r ee . s ize ()− c a l l S t ac k . get ( c a l l S t ac k . s i ze () −1). i n tVa lue ( ) ) ;t r ee . wr i teTree2 ( c a l l S t a c k . get ( c a l l S t a c k . s ize () −1). i n tVa lue ( ) , key ) ;Eva lua t ion eva lua t i on=PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) ;i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . on ly non−t e rm ina l s are evaluated here
i f ( eva lua t i on != n u l l ) {/ / double [ ] evaled= lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . eva lua t i on ;
/ / f o r ( i n t i =0; i <evaled . leng th ; i ++)/ / ou tResu l t [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . cachedFlag= t rue ; / / the en t ry i s now cached , even i f i t was not p rev ious l yPuppysMain . cacheHits+= t ree . get ( c a l lS t a c k . s ize () −1). getSubTreeSize ( ) ; ;
r e t u r n eva lua t i on . eva lua t i on ;}
}∗ /
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS IX
/ / double lArg2 ;double [ ] ou tResu l t=nul l ;
double [ ] lArg2=nul l ;ou tResu l t=getArgument (0 , ca l lS tack , t ree , XData ) ;lArg2=getArgument (1 , ca l lS tack , t ree , XData ) ;
i f ( ou tResu l t . l eng th== lArg2 . leng th )for ( i n t i =outResu l t . l eng th;−−i >=0; )
outResu l t [ i ] =Math . pow( outResu l t [ i ] , lArg2 [ i ] ) ;else i f ( ou tResu l t . l eng th ==1){ / / i . e . a te rm ina l re turned
for ( i n t i = lArg2 . leng th;−−i >=0; )lArg2 [ i ]= Math . pow( lArg2 [ i ] , ou tResu l t [ 0 ] ) ;
ou tResu l t= lArg2 ; / / po i n t outResu l t to t h i s ar ray now}else i f ( lArg2 . leng th ==1){
for ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ]= Math . pow( outResu l t [ i ] , lArg2 [ 0 ] ) ;
}
/∗ PuppysMain . noCacheHits ++;i f ( eva lua t i on == n u l l ) { / / i . e . i f the en t ry i s not present
i f ( t h i s . g e t A r i t y ( ) = = 0 ) {lCon tex t . mCache . put ( key . t o S t r i n g ( ) , new Eva lua t ion ( ) ) ; / / c a l l the d e f a u l t cons t r uc to r o f Eva lua t ion/ / but the ac tua l eva lua t i on i s done here . Aimed at saving ex t ra array−copying
}e lse { / / the t ree has c h i l d ( ren ) .
i n t l I ndex = c a l l S ta c k . get ( c a l l S t ac k . s ize () −1). i n tVa lue ( ) + 1 ; / / index o f the next subtreeboolean hasCached= t rue ; / / t h a t the subtree i s cahchedf o r ( i n t i =0; i < t h i s . a r i t y ; i ++){
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS X
S t r i n g B u i l d e r subTree=new S t r i n g B u i l d e r ( t r ee . s ize ()− l I ndex ) ;t r ee . wr i teTree2 ( l Index , subTree ) ;S t r i n g s t r =new S t r i n g ( subTree . t o S t r i n g ( ) ) ;Eva lua t ion eva lua t ion2= lCon tex t . mCache . get ( s t r ) ;t r y {
i f ( eva lua t ion2 != n u l l )i f ( eva lua t ion2 . cachedFlag== f a l s e )
hasCached= f a l s e ;/ / System . out . p r i n t l n ( " Alarming " ) ;}catch ( Except ion e ) {
hasCached= f a l s e ;System . out . p r i n t l n ( " Do I reach here? "+e ) ;
/ / System . e x i t ( 0 ) ;}l I ndex += t ree . get ( l I ndex ) . getSubTreeSize ( ) ;
}i f ( hasCached ) { / / That i s hasCached surv ived through a l l the subChi ldren
lCon tex t . mCache . put (new S t r i n g ( key . t o S t r i n g ( ) ) , new Eva lua t ion ( outResul t , f a l s e ) ) ;}
}
} ∗ /return outResu l t ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS XI
public AdilPower clone ( ) {return (new AdilPower ( th is ) ) ;
}
}
/∗∗ Cos . java∗∗ Created on September 27 , 2007 , 11:49 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 GeneticProgramming . P r i m i t i v e s . Funct ions ;
/∗∗∗∗ @author a d i l r a j a∗ /
import GeneticProgramming .∗ ;import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;import java . u t i l .∗ ;
public class Cos extends P r i m i t i v e {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS XII
/∗∗ Creates a new ins tance of X1 ∗ /public Cos ( ) {}/∗∗
∗ \ param inNumberArguments Number o f arguments o f the p r i m i t i v e .∗ \ param inName Name of the p r i m i t i v e .∗ /
public Cos ( S t r i n g inName , i n t a r i t y ) {super ( inName , a r i t y ) ;
}/∗∗
∗ \ b r i e f Copy−cons t ruc t a p r i m i t i v e .∗ \ param i n R i g h t P r i m i t P r i m i t i v e to copy .∗ /
public Cos ( f i n a l Cos copy ){
super ( copy ) ;/ / t h i s . name=new S t r i n g ( copy . name ) ;/ / t h i s . a r i t y =copy . a r i t y ;/ / mRefCounter =0;}
/∗∗∗ \ b r i e f Give a re ference on the ac tua l p r i m i t i v e .∗ \ param ob jec t o f Random∗ \ r e t u r n P r i m i t i v e handle to t h i s p o i n t e r .∗ /
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS XIII
public Cos giveReference (Random dice ){/ / i f ( t h i s . getName ( ) . equalsIgnoreCase ( " Ephemeral " ) ) { / / should r e t u r n the Ephemeral random number ins tead
/ / r e t u r n new P r i m i t i v e ( java . lang . Double . t o S t r i n g ( d ice . nextDouble ()∗12 −6) ,0);/ / }return th is ;
}
/∗∗∗Funct ion i s used to execute t h i s p r i m i t i v e∗outResu l t∗c a l l S ta c k∗ t r ee to which t h i s p r i m i t i v e belongs∗XData∗patternNumber , t h a t comes i n from evaluateTree i n Tree∗ /
public double [ ] execute ( A r rayL i s t < In teger > ca l lS tack , Tree t ree , f l o a t [ ] [ ] XData ) {/ / c a l l the cache here?
/∗ S t r i n g B u i l d e r key=new S t r i n g B u i l d e r ( t r ee . s ize ()− c a l l S t ac k . get ( c a l l S t ac k . s i ze () −1). i n tVa lue ( ) ) ;t r ee . wr i teTree2 ( c a l l S t a c k . get ( c a l l S t a c k . s ize () −1). i n tVa lue ( ) , key ) ;Eva lua t ion eva lua t i on=PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) ;i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . on ly non−t e rm ina l s are evaluated here
i f ( eva lua t i on != n u l l ) {/ / double [ ] evaled= lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . eva lua t i on ;
/ / f o r ( i n t i =0; i <evaled . leng th ; i ++)/ / ou tResu l t [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lCon tex t . mCache . get ( key . t o S t r i n g ( ) ) . cachedFlag= t rue ; / / the en t ry i s now cached , even i f i t was not p rev ious l y
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS XIV
PuppysMain . cacheHits+= t ree . get ( c a l lS t a c k . s ize () −1). getSubTreeSize ( ) ; ;
r e t u r n eva lua t i on . eva lua t i on ;}
}∗ /
/ / double lArg2 ;double [ ] ou tResu l t=nul l ;ou tResu l t=getArgument (0 , ca l lS tack , t ree , XData ) ;
for ( i n t i =outResu l t . l eng th;−−i >=0; )outResu l t [ i ] = ( Math . cos ( outResu l t [ i ] ) ) ;
/∗ PuppysMain . noCacheHits ++;i f ( eva lua t i on == n u l l ) { / / i . e . i f the en t ry i s not present
i f ( t h i s . g e t A r i t y ( ) = = 0 ) {lCon tex t . mCache . put ( key . t o S t r i n g ( ) , new Eva lua t ion ( ) ) ; / / c a l l the d e f a u l t cons t r uc to r o f Eva lua t ion/ / but the ac tua l eva lua t i on i s done here . Aimed at saving ex t ra array−copying
}e lse { / / the t ree has c h i l d ( ren ) .
i n t l I ndex = c a l l S ta c k . get ( c a l l S t ac k . s ize () −1). i n tVa lue ( ) + 1 ; / / index o f the next subtreeboolean hasCached= t rue ; / / t h a t the subtree i s cahchedf o r ( i n t i =0; i < t h i s . a r i t y ; i ++){
S t r i n g B u i l d e r subTree=new S t r i n g B u i l d e r ( t r ee . s ize ()− l I ndex ) ;t r ee . wr i teTree2 ( l Index , subTree ) ;S t r i n g s t r =new S t r i n g ( subTree . t o S t r i n g ( ) ) ;Eva lua t ion eva lua t ion2= lCon tex t . mCache . get ( s t r ) ;t r y {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
FUNCTIONS XV
i f ( eva lua t ion2 != n u l l )i f ( eva lua t ion2 . cachedFlag== f a l s e )
hasCached= f a l s e ;/ / System . out . p r i n t l n ( " Alarming " ) ;}catch ( Except ion e ) {
hasCached= f a l s e ;System . out . p r i n t l n ( " Do I reach here? "+e ) ;
/ / System . e x i t ( 0 ) ;}l I ndex += t ree . get ( l I ndex ) . getSubTreeSize ( ) ;
}i f ( hasCached ) { / / That i s hasCached surv ived through a l l the subChi ldren
lCon tex t . mCache . put (new S t r i n g ( key . t o S t r i n g ( ) ) , new Eva lua t ion ( outResul t , f a l s e ) ) ;}
}
} ∗ /return outResu l t ;
}public Cos clone ( ) {
return (new Cos ( th is ) ) ;}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions 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 Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions Summary References
REFERENCES
Java beagle can be found online.This presentation is developed with Beamer:
Darmstadt, spruce.