SlideShare a Scribd company logo
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
JAVA BEAGLE
THE 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 S
OUTLINE I
1 INTRODUCTION
2 PUPPY’S MAIN
3 PUPPY
4 NODE
5 EVALUATION
6 TREE
7 STATISTICS
8 STATISTICS IO
9 CONTEXT
10 PRIMITIVES
11 FUNCTIONS
12 SUMMARY
13 REFERENCES
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
INTRODUCTION I
Java Beagle is an implementation of a simple genetic
programming (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 types
of problems such as classification.
It is based on Beagle Puppy software for genetic
programming.
The source code has many Java files.
1 PuppyMain.java: The main entry point to the program that
runs the GP algorithmd.
2 Puppy.java: This class implements the algorithms that are
necessary to run the GP, such as crossover and mutation.
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
INTRODUCTION II
3 Node.java: The data-type Node specifies the
implementation of the GP genotype.
4 Evaluation.java: The routines for evaluation of a node in GP
population.
5 Tree.java: builds and Keeps the syntax tree of a GP
program.
In what follows you will find the source code of various
components of Java Beagle.
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
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 in 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 . Functions . ∗ ;
import java . u t i l . ∗ ;
import java . lang . ∗ ;
import java . io . ∗ ;
public class PuppysMain {
public static float [ ] [ ] X;
public static float [ ] Target ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN II
public static Context lContext ;
public static int cacheHits , noCacheHits ;
/∗∗ Creates a new instance of PuppysMain ∗/
public PuppysMain ( ) {
}
/∗∗
∗ @param args the command l i n e arguments
∗/
public static void main ( String [ ] args ) {
/ / TODO code a p p l i c a t i o n l o g i c here
new Context ( ) ;
Context . i n s e r t (new Plus ( " plus " , 2 ) ) ;
Context . i n s e r t (new Minus ( " minus " , 2 ) ) ;
lContext . i n s e r t (new MyDivide ( " mydivide " , 2 ) ) ;
lContext . i n s e r t (new AdilPower ( " adilpower " , 2 ) ) ;
Context . i n s e r t (new Times ( " times " , 2 ) ) ;
lContext . i n s e r t (new Sin ( " sin " , 1 ) ) ; / / sin
lContext . i n s e r t (new Cos( " cos " , 1 ) ) ; / / cos
lContext . i n s e r t (new AdilLog ( " a d i l l o g " , 1 ) ) ; / / ln
lContext . i n s e r t (new AdilLog10 ( " adillog10 " , 1 ) ) ; / / log−10
/ / lContext . 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 S
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 S
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 . mutationProba =0.1F ;
Context . inInitGrowProba =0.5F ;
Context . xoverNodeDistribProb =0.5F ;
Context . maxTreeDepth=17;
Context . initTreeDepthMax =6;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN V
Context . initTreeDepthMin =2;
Context . tournamentSize =7;
Context . trainDataFileX=new String ( " . / OnP563 / p563−input−params−cond−n−exp−wise . t x t " ) ;
/ / Context . trainDataFileX =" a d i l . t x t " ;
Context . trainDataFileY=new String ( " . / OnP563 / Subj−MOS−1328−cond−wise−sorted . t x t " ) ;
Context . testDataFileX=new String ( " . / OnP563 / combined−p563−features−Nortel−czech . t x t " ) ;
Context . testDataFileY=new String ( " . / OnP563 / combined−ACR−MOS−Nortel−czech . t x t " ) ;
Context . useTestData=true ;
Context . length_of_run =100;
int numRuns=2;
int 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 ( Integer . t oSt ri ng ( k +1)) , 0 ) ) ;
/ / }
ArrayList < S t a t i s t i c s > s t a t i s t i c s =new ArrayList ( ) ;
/∗
∗Lets do an example problem here
∗/
/ / Context . XData=new f l o a t [ 1 0 0 0 ] [ 2 ] ;
/ / Context . Target=new double [1000];
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 ] = dice . nextFloat ()∗10−5;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN VI
/ / Context . XData [ i ] [ 1 ] = dice . nextFloat ()∗10−5;
/ / Context . Target [ i ]=Math . pow( Context . XData [ i ] [ 0 ] , 4 ) + Context . XData [ i ] [ 0 ] ∗ Context . XD
/ / Context . Target [ i ] = ( double ) ( Context . XData [ i ] [ 0 ] ∗ ( Context . XData [ i ] [ 0 ] ∗ ( Context . XD
/ / }
Context . readDataPatterns (1328 , 44 , true ) ;
/ / System . e x i t ( 0 ) ;
i f ( Context . useTestData ) / / i f testdata i s used as well
Context . readDataPatterns (276 , 44 , false ) ;
int lPopSize =1000;
Puppy puppy=new Puppy ( ) ;
cacheHits =0;
for ( int i =0; i <numRuns ; i ++){ / / the run s t a r t s here
S t a t i s t i c s thisRunStats=new S t a t i s t i c s ( ) ;
thisRunStats . t r a i n F i t H i s t o r y =new double [ Context . length_of_run ] ;
i f ( Context . useTestData )
thisRunStats . t e s t F i t H i s t o r y =new double [ Context . length_of_run ] ;
ArrayList <Tree> lPopulation=new ArrayList ( Context . popSize ) ;
puppy . i n i t i a l i z e P o p u l a t i o n ( lPopulation ) ;
puppy . evaluatePopulation ( lPopulation ) ;
int j =0;
f i t n e s s =500;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN VII
Tree bestTree=null ;
while ( j <Context . length_of_run ) { / / run s t a r t s from here
System . out . p r i n t l n ( ) ;
bestTree=puppy . applyAllEvolutionarySteps ( lPopulation ) ;
double tmpFitness =200;
tmpFitness=lPopulation . get ( 0 ) . getTrainingFitness ( ) ;
double testFitness =Double .MAX_VALUE;
i f ( Context . useTestData ) {
testFitness =lPopulation . get ( 0 ) . evaluateTreeForTestData ( ) ;
}
thisRunStats . t r a i n F i t H i s t o r y [ j ]= tmpFitness ;
/ / thisRunStats . t e s t F i t H i s t o r y [ j ]= testFitness ;
System . out . p r i n t l n ( " CurrentBest "+tmpFitness+" TestFitness : "+ testFitness +" Generation
System . out . p r i n t l n ( " Xover Prob : "+Context . xoverProba+" Mutation Prob : "+Context . mutati
StringBuilder s t b l d r =new StringBuilder ( ) ;
lPopulation . get ( 0 ) . writeTree (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 generation ends here
bestTree . setSExpression ( ) ; / / set the s−expression now, s h a l l be used l a t t e r
thisRunStats . 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 ( thisRunStats ) ;
} / / a run ends here
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN VIII
try {
S t a t i s t i c s I O . writeStatsToDisk ( s t a t i s t i c s , " SimulationResults . expr " ) ;
}
catch ( java . io . IOException e ) {
System . out . p r i n t l n ( e ) ;
}
ArrayList < S t a t i s t i c s > stato=null ;
try {
stato =( ArrayList < S t a t i s t i c s >) S t a t i s t i c s I O . readStatsFromDisk ( " SimulationResults . ex
} catch ( java . io . IOException 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 ( int i =0; i <stato . size ( ) ; i ++){
System . out . p r i n t l n ( stato . get ( i ) . b e s t I n d i v i d u a l . getTrainingFitness ( ) ) ;
}
}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
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 in 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 . bcel . 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 int parentPopSize ;
private Random dice ;
private ArrayList <Tree> childPop ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY II
private ArrayList <Integer > lMateVector ;
public double [ ] [ ] trainingDataX ;
public double [ ] trainingDataY ;
private double bestFitness ;
private int dynamicLevel ;
/∗∗ Creates a new instance of Puppy ∗/
public Puppy ( ) {
dice=new Random ( ) ;
childPop=new ArrayList ( ) ;
lMateVector=new ArrayList ( ) ;
this . dynamicLevel =6; / / to be changed
this . bestFitness =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 half−and−h a l f a population of GP trees .
∗  param ioPopulation Population to i n i t i a l i z e .
∗  param inInitGrowProba 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 , in opposition to f u l l .
∗  param inMinDepth Minimum i n i t i a l i z a t i o n tree depth allowed .
∗  param inMaxDepth Maximum i n i t i a l i z a t i o n tree depth allowed .
∗  ingroup Puppy
∗/
public ArrayList <Tree> i n i t i a l i z e P o p u l a t i o n ( ArrayList <Tree> ioPopulation )
{
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY III
this . parentPopSize=Context . popSize ;
int inMinDepth=Context . initTreeDepthMin ;
int inMaxDepth=Context . initTreeDepthMax ;
float inInitGrowProba=Context . inInitGrowProba ;
assert ( inMinDepth <= inMaxDepth ) ;
/ / ArrayList <Tree> ioPopulation=new ArrayList ( ) ;
this . dice=new Random ( ) ;
for ( int i =0; i <Context . popSize ; ++ i ) {
ioPopulation . add ( i ,new Tree ( ) ) ;
ioPopulation . get ( i ) . clear ( ) ;
int l I n i t D e p t h = dice . nextInt ( inMaxDepth−inMinDepth )+ inMinDepth +1;
Tree tmpTr=new Tree ( ) ;
i f ( dice . nextFloat ( ) >= inInitGrowProba ) {
i n i t i a l i z e T r e e F u l l ( ioPopulation . get ( i ) , l I n i t D e p t h ) ;
/ / ioPopulation . add ( i , new Tree ( tmpTr ) ) ;
ioPopulation . get ( i ) . setValid ( false ) ;
ioPopulation . get ( i ) . setEvaluated ( false ) ;
/ / tmpTr . clear ( ) ;
}
else {
initializeTreeGrow ( ioPopulation . get ( i ) , inMinDepth , l I n i t D e p t h ) ;
/ / ioPopulation . add ( i , new Tree ( tmpTr ) ) ;
ioPopulation . get ( i ) . setValid ( false ) ;
ioPopulation . get ( i ) . setEvaluated ( false ) ;
/ / tmpTr . clear ( ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY IV
}
}
evaluatePopulation ( ioPopulation ) ;
return ioPopulation ;
}
/∗∗
∗ I n i t i a l i z e a GP tree with f u l l approach .
∗  param ioTree Tree to i n i t i a l i z e .
∗  param ioContext Evolutionary context .
∗  param inDepth Actual depth to go in i n i t i a l i z a t i o n .
∗  return Generated tree size .
∗  ingroup Puppy
∗
∗ I f the tree i s not empty , the i n i t i a l i z a t i o n append the generated sub−tree to the actual t
∗/
public int i n i t i a l i z e T r e e F u l l ( Tree ioTree , int inDepth )
{
i f ( inDepth >= 1){ / / assert was here
i f ( inDepth == 1) {
assert ( Context . mTerminalSet . size ( ) > 0 ) ;
P r i m i t i v e lTerminal = Context . mTerminalSet . get ( dice . nextInt ( Context . mTerminalSet . size ( ) ) ) ;
ioTree . add (new Node( lTerminal . giveReference ( dice ) , 1 ) ) ;
return 1;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY V
assert ( Context . mFunctionSet . size ( ) > 0 ) ;
P r i m i t i v e lFunction =Context . mFunctionSet . get ( dice . nextInt ( Context . mFunctionSet . size ( ) ) ) ;
int lNodeIndex = ioTree . size ( ) ;
ioTree . add (new Node( lFunction . giveReference ( dice ) , 0 ) ) ;
int lNbArgs = ioTree . get ( lNodeIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
int lTreeSize = 1;
/ / Tree tmpT ;
for ( int 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);
/ / iTree=tmpT ;
}
ioTree . get ( lNodeIndex ) . setSubTreeSize ( lTreeSize ) ;
return lTreeSize ;
}
else return 0;
}
/∗∗
∗ I n i t i a l i z e a GP tree with grow approach .
∗  param ioTree Tree to i n i t i a l i z e .
∗  param ioContext Evolutionary context .
∗  param inMinDepth Minimal depth to go in i n i t i a l i z a t i o n .
∗  param inMaxDepth Maximal depth to go in i n i t i a l i z a t i o n .
∗  return Generated tree size .
∗  ingroup Puppy
∗
∗ I f the tree i s not empty , the i n i t i a l i z a t i o n append the generated sub−tree to the actual t
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY VI
∗/
public int initializeTreeGrow ( Tree ioTree , int inMinDepth , int inMaxDepth )
{
i f ( inMinDepth >= 1 && inMinDepth <= inMaxDepth ) {
P r i m i t i v e l P r i m i t = null ;
i f ( inMinDepth > 1) {
assert ( Context . mFunctionSet . size ( ) > 0 ) ;
l P r i m i t = Context . mFunctionSet . get ( dice . nextInt ( Context . mFunctionSet . size ( ) ) ) ;
}
else i f ( inMaxDepth == 1) {
assert ( Context . mTerminalSet . size ( ) > 0 ) ;
l P r i m i t = Context . mTerminalSet . get ( dice . nextInt ( Context . mTerminalSet . size ( ) ) ) ;
}
else {
int lIndexSel = dice . nextInt ( Context . mFunctionSet . size ( ) + Context . mTerminalSet . size ( ) ) ;
i f ( lIndexSel >= Context . mFunctionSet . size ( ) ) {
l P r i m i t = Context . mTerminalSet . get ( lIndexSel − Context . mFunctionSet . size ( ) ) ;
}
else l P r i m i t = Context . mFunctionSet . get ( lIndexSel ) ;
}
/ / i n t lNodeIndex = tmpArr . size ( ) ;
int lNodeIndex = ioTree . size ( ) ;
ioTree . add (new Node( l P r i m i t . giveReference ( dice ) , 1 ) ) ;
int lTreeSize = 1;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY VII
int lMinDepth = ( inMinDepth > 1) ? ( inMinDepth−1) : 1;
int 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
try {
for ( int i =0; i <lNbArgs ; i ++) {
lTreeSize+= initializeTreeGrow ( ioTree , lMinDepth , inMaxDepth −1);
/ / ioTree=tmpT ;
}
}
catch ( java . lang . StackOverflowError e ) {
System . out . p r i n t l n ( e+" in echange subtrees : "+inMinDepth+" "+inMaxDepth ) ;
System . e x i t ( 0 ) ;
}
ioTree . get ( lNodeIndex ) . setSubTreeSize ( lTreeSize ) ;
return lTreeSize ;
}
else {
System . out . p r i n t l n ( "The problem i s here " ) ;
return 0;
}
}
/∗∗
∗ Apply tournament selection to a population of trees .
∗  param ioPopulation Population to apply selection on .
∗  param ioContext Evolutionary context .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY VIII
∗  param inNumberParticipants Number of p a r t i c i p a n t s to each tournament selection .
∗  ingroup Puppy
∗This function has to be modified . This only s u i t s the replacement c r i t e r i a but not an e l i t i s
∗The routine also implements LPP by Sean Luke
∗/
public ArrayList <Tree> applySelectionTournament ( ArrayList <Tree> ioPopulation )
{
i f ( ioPopulation . size ( ) == 0) return null ;
int inNumberParticipants=Context . tournamentSize ;
ArrayList <Tree> childPop=new ArrayList ( ioPopulation . size ( ) ) ;
int [ ] l I n d i c e s =new int [ ioPopulation . size ( ) ] ;
for ( int i =0; i <ioPopulation . size ( ) ; i ++)
l I n d i c e s [ i ]=0;
for ( int i =0; i <ioPopulation . size ( ) ; i ++) {
int lChoosenIndividual = dice . nextInt ( ioPopulation . size ( ) ) ;
for ( int j =1; j <inNumberParticipants ; ++ j ) {
int l T r i e d I n d i v i d u a l = dice . nextInt ( ioPopulation . size ( ) ) ; / / the f o l l o w i n g l i n e implements
i f ( ioPopulation . get ( l T r i e d I n d i v i d u a l ) . compareRank ( ioPopulation . get ( lChoosenIndividual ) )
lChoosenIndividual = l T r i e d I n d i v i d u a l ;
}
}
++ l I n d i c e s [ lChoosenIndividual ] ;
}
int lNextEmpty = 0;
int 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 S
PUPPY IX
while ( ( l N e x t F i l l e d < ioPopulation . size ( ) ) && ( 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 < ioPopulation . size ( ) ) {
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 ;
childPop . add ( ioPopulation . get ( l N e x t F i l l e d ) ) ; / / remove ( lNextEmpty ) ;
/ / ioPopulation . add ( ioPopulation . 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 < ioPopulation . size ( ) ) && ( 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 ( childPop . size () < ioPopulation . size ( ) )
childPop . add ( ioPopulation . get ( dice . nextInt ( ioPopulation . size ( ) ) ) ) ; / / f i l l the remaining
return childPop ;
}
/∗∗
∗ Apply tournament selection to a population of trees .
∗  param ioPopulation Population to apply selection on .
∗  param ioContext Evolutionary context .
∗  param inNumberParticipants Number of p a r t i c i p a n t s to each tournament selection .
∗  ingroup Puppy
∗This function has to be modified . This only s u i t s the replacement c r i t e r i a but not an e l i t i s
∗The routine also implements LPP by Sean Luke
∗/
public ArrayList <Tree> applySelectionTournament1 ( ArrayList <Tree> ioPopulation )
{
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY X
i f ( ioPopulation . size ( ) == 0) return null ;
ArrayList <Tree> childPop=new ArrayList ( ioPopulation . size ( ) ) ;
int inNumberParticipants=Context . tournamentSize ;
/ / choose the f i r s t candidate and store in the c h i l d population
while ( childPop . size () < ioPopulation . size ( ) ) {
int j j =0;
int lChosenCand1=0 , lChosenCand2=0;
int tmpCand1=ioPopulation . size ()+1 , tmpCand2=ioPopulation . size ( ) + 1 ;
while ( j j <inNumberParticipants ) {
lChosenCand1=dice . nextInt ( ioPopulation . size ( ) ) ;
i f ( tmpCand1<ioPopulation . size ( ) ) {
i f ( ioPopulation . get ( lChosenCand1 ) . compareRank ( ioPopulation . get ( tmpCand1 ) ) ) { / / | | ( ioP
tmpCand1=lChosenCand1 ; / / choose an i n d i v i d u a l ,
}
}
else
tmpCand1=lChosenCand1 ; / / choose t h i s
j j ++;
}
childPop . add (new Tree ( ioPopulation . get ( tmpCand1 ) ) ) ; / / Add t h i s to the c h i l d population
/ / choose the second candidate now and store in the c h i l d population
j j =0;
while ( j j <inNumberParticipants ) {
lChosenCand2=dice . nextInt ( ioPopulation . size ( ) ) ;
i f ( tmpCand2<=ioPopulation . size ( ) ) { / / implements Gustafson and LPP
i f ( ! ioPopulation . get ( tmpCand1 ) . compareTrees ( ioPopulation . get ( tmpCand2 ) )
&& ioPopulation . get ( lChosenCand2 ) . compareRank ( ioPopulation . get ( tmpCand2 ) )
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XI
| | ( ioPopulation . get ( lChosenCand2 ) . compareTrees ( ioPopulation . get ( tmpCand2 ) )
&& ioPopulation . get ( lChosenCand2 ) . size () < ioPopulation . get ( tmpCand2 ) . size ( ) ) ) {
tmpCand2=lChosenCand2 ; / / choose an i n d i v i d u a l ,
}
}
else
tmpCand2=lChosenCand2 ; / / choose t h i s
j j ++;
}
childPop . add (new Tree ( ioPopulation . get ( tmpCand2 ) ) ) ; / / Add t h i s to the c h i l d population
}
for ( int i =0; i <ioPopulation . size ( ) ; i ++){
childPop . get ( i ) . usedMutationStd=false ;
childPop . get ( i ) . usedMutationSwap=false ;
childPop . get ( i ) . usedXover=false ;
}
return childPop ;
}
/ / what i s t h i s function doing?
/∗∗
∗ Apply sub−tree crossover operation on a population of GP trees .
∗  param ioPopulation Population to apply crossover on .
∗  param ioContext Evolutionary context .
∗  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 modified by crossover .
∗  param inDistribProba P r o b a b i l i t y that a crossover exchange two sub−trees of non−terminal
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XII
∗  param inMaxTreeDepth Maximum tree depth allowed .
∗  ingroup Puppy
∗/
public void applyCrossover ( ArrayList <Tree> ioPopulation )
{
int inMaxTreeDepth=Context . maxTreeDepth ;
float inDistribProba=Context . inInitGrowProba ; / / not used here but s t i l l
float inMatingProba=Context . xoverProba ;
i f ( ( ioPopulation . size ( ) % 2) != 0) ioPopulation . remove ( lMateVector . size () −1);
/ / System . out . p r i n t l n ( "MATING VECTOR SIZE= "+ lMateVector . size ( ) ) ;
/ / f o r ( unsigned i n t j =0; j <lMateVector . size ( ) ; ++ j ) {
/ / std : : cout << j << " : " << ioPopulation [ lMateVector [ j ] ] << std : : endl ;
/ / }
/ / childPop . clear ( ) ; / / = new ArrayList ( ) ;
for ( int j =0; j <ioPopulation . size ( ) ; j +=2) {
try {
mateTrees ( ioPopulation . get ( j ) , ioPopulation . get ( j + 1 ) ) ;
}
catch ( java . lang . NullPointerException e ) {
System . out . p r i n t l n ( e+" in apply crossover . . . c a l l to mateTrees " ) ;
System . e x i t ( 0 ) ;
}
try {
/ / childPop . add ( trees . get ( 0 ) ) ; / / t h i s l i n e i s meant to implement Elitism , as
/ / childPop . add ( trees . get ( 1 ) ) ;
/ / ioPopulation . add (new Tree ( ioPopulation . get ( lMateVector . get ( mate ) . intValue
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XIII
catch ( java . lang . Exception e ) {
/ / System . out . p r i n t l n ( j +" "+mate+" "+"WAITING In applyCrossover "+e ) ; / / + " "+ i
/ / System . e x i t ( 0 ) ;
}
}
}
/∗∗
∗ Mate two GP trees f o r crossover .
∗  param ioTree1 F i r s t tree to mate .
∗  param ioTree2 Second tree to mate .
∗  param ioContext Evolutionary context .
∗  param inDistribProba 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 tree depth allowed .
∗  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 checks
assert ( ioTree1 . size ( ) > 0 ) ;
assert ( ioTree2 . size ( ) > 0 ) ;
float inDistribProba=Context . xoverNodeDistribProb ;
int inMaxTreeDepth=Context . maxTreeDepth ;
/ / ArrayList <Tree> trees= n u l l ;
/ / Crossover loop . Try the given number of attempts 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 S
PUPPY XIV
for ( int 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 chang
/ / node f o r another node of the same tree i f the types mismatch .
boolean lNode1IsTerminal = true ;
i f ( ioTree1 . size ( ) > 1){
i f ( dice . nextFloat ( ) >= inDistribProba ) lNode1IsTerminal =true ;
else lNode1IsTerminal = false ;
}
int lChoosenNode1=0;
try {
lChoosenNode1= dice . nextInt ( ioTree1 . size ( ) ) ; / / cant choose the root node
}
catch ( java . lang . IllegalArgumentException e ) {
System . out . p r i n t l n ( ioTree1 . size ()+ " mate tree "+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 . nextInt ( ioTree1 . size ( ) ) ;
}
i f ( lChoosenNode1==0) lChoosenNode1+=1;
boolean lNode2IsTerminal = true ;
i f ( ioTree2 . size ( ) > 1) lNode2IsTerminal = ( dice . nextFloat ( ) >= inDistribProba ) ;
int lChoosenNode2 = dice . nextInt ( ioTree2 . size ( ) ) ;
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 . nextInt ( ioTree2 . size ( ) ) ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XV
i f ( lChoosenNode2==0)lChoosenNode2+=1;
/ / lChoosenNode2=ioTree2 . size () −1;// t r i a l
/ / Set f i r s t stack to the node of the f i r s t tree .
/ / Check i f depth i s ok . Do a new crossover attempt i f not .
ArrayList <Integer > lStack1=ioTree1 . setStackToNode ( lChoosenNode1 ) ;
int lNewDepthTree1 =lStack1 . size ( ) + ioTree2 . getDepth ( lChoosenNode2 ) − 1;
i f ( lNewDepthTree1 > inMaxTreeDepth ) continue ;
/ / Set second stack to the node of the second tree .
/ / Check i f depth i s ok . Do a new crossover attempt i f not .
ArrayList <Integer > lStack2=ioTree2 . setStackToNode ( lChoosenNode2 ) ;
int lNewDepthTree2 =lStack2 . size ( ) + 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 . setValid ( false ) ;
/ / ioTree2 . setValid ( false ) ;
/ / trees . get ( 0 ) . setValid ( false ) ;
/ / trees . get ( 1 ) . setValid ( false ) ;
/ / }
/ / catch ( java . lang . Exception e ) {
/ / System . out . p r i n t l n ( e+"The return value i s n u l l in mate−trees : " ) ;
/ / System . e x i t ( 0 ) ;
/ / }
break ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XVI
}
/ / return trees ;
}
/∗∗
∗ Exchange two sub−trees .
∗  param ioTree1 Tree containing the f i r s t sub−tree to exchange .
∗  param inNode1 Index of root node to sub−tree to swap in f i r s t tree .
∗  param inStack1 Stack containing the parents to the f i r s t sub−tree root node .
∗  param ioTree2 Tree containing the second sub−tree to exchange .
∗  param inNode2 Index of root node to sub−tree to swap in second tree .
∗  param inStack2 Stack containing the parents to the second sub−tree root node .
∗  ingroup Puppy
∗/
public void exchangeSubTrees ( f i n a l Tree iTree1 , int inNode1 , f i n a l ArrayList <Integer > inStack1
{
/ / assert ( iTree1 != iTree2 ) ; / / I can mate mate between s i m i l a r trees as well , in the worst ca
assert ( inStack1 . size ( ) > 0 ) ;
assert ( inStack2 . size ( ) > 0 ) ;
Tree ioTree1=new Tree ( iTree1 ) ;
Tree ioTree2=new Tree ( iTree2 ) ;
int lSwapSize1 = ioTree1 . get ( inNode1 ) . getSubTreeSize ( ) ;
int lSwapSize2 = ioTree2 . get ( inNode2 ) . getSubTreeSize ( ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XVII
Tree ioSubTree1=new Tree ( ) ;
Tree ioSubTree2=new Tree ( ) ;
for ( int i =0; i <lSwapSize1 ; i ++){
try {
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 ( int i =0; i <lSwapSize2 ; i ++){
try {
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 . addAll ( inNode1 , ioSubTree2 ) ;
ioTree2 . addAll ( inNode2 , ioSubTree1 ) ;
int l D i f f S i z e = lSwapSize1 − lSwapSize2 ;
for ( int i =0; i <( inStack1 . size () −1); ++ i )
try {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XVIII
ioTree1 . get ( inStack1 . get ( i ) . intValue ( ) ) . setSubTreeSize ( ioTree1 . get ( inStack1 . get ( i ) . intValu
}
catch ( java . lang . IndexOutOfBoundsException e )
{
System . out . p r i n t l n ( " exchangeSubTrees4 "+e ) ;
System . e x i t ( 0 ) ;
}
for ( int j =0; j <( inStack2 . size () −1); ++ j )
try {
ioTree2 . get ( inStack2 . get ( j ) . intValue ( ) ) . setSubTreeSize ( ioTree2 . get ( inStack2 . get ( j ) . intValu
}
catch ( java . lang . IndexOutOfBoundsException e )
{
System . out . p r i n t l n ( " exchangeSubTrees5 "+e ) ;
System . e x i t ( 0 ) ;
}
iTree1 . clear ( ) ;
iTree1 . addAll ( ioTree1 ) ;
iTree2 . clear ( ) ;
iTree2 . addAll ( ioTree2 ) ;
iTree1 . setEvaluated ( false ) ;
iTree2 . setEvaluated ( false ) ;
iTree1 . setValid ( false ) ;
iTree2 . setValid ( false ) ;
iTree1 . usedXover=true ;
iTree2 . usedXover=true ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XIX
/∗∗
∗ Apply standard ( Koza ’ s ) mutation on a GP trees .
∗ This performs subtree mutation
∗ Input : − ioTree GP tree to mutate .
∗ ioContext Evolutionary context .
∗ inMaxRegenDepth Maximum tree regeneration depth allowed .
∗ inMaxDepth Maximum tree depth allowed .
∗ Output : −
∗ ioTree − Mutated GP Tree
∗  ingroup Puppy
∗/
public void mutateStandard ( Tree ioTree , int inMaxRegenDepth , int inMaxDepth )
{
/ / System . out . p r i n t l n ( " Following i s the tree before mutation :  n " ) ; / / + ioTree . writeTree ( 0 ) ) ;
/ / f o r ( i n t i =0; i <ioTree . size ( ) ; 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 . size () <=0)System . out . p r i n t l n ( "HUGE ERROR IN MUTATESTANDARD" ) ;
assert ( ioTree . size ( ) > 0 ) ;
int lMutIndex = dice . nextInt ( ioTree . size () −1);
i f ( lMutIndex ==0) lMutIndex +=1;
Tree lNewTree=new Tree ( ) ;
for ( int i =0; i <lMutIndex ; i ++){
lNewTree . add ( ioTree . get ( i ) ) ; / /NEW
}
ArrayList <Integer > lStack=ioTree . setStackToNode ( lMutIndex ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XX
lStack . remove ( lStack . size () −1);
int lTreeDepth = dice . nextInt ( inMaxRegenDepth )+1;
int lTreeDepth2 = inMaxDepth − lStack . size ( ) ;
i f ( lTreeDepth2 < lTreeDepth ) lTreeDepth = lTreeDepth2 ;
assert ( lTreeDepth > 0 ) ;
initializeTreeGrow ( lNewTree , 1 , lTreeDepth ) ;
for ( int i =( lMutIndex+ioTree . get ( lMutIndex ) . getSubTreeSize ( ) ) ; i <ioTree . size ( ) ; i ++){
try {
lNewTree . add ( ioTree . get ( i ) ) ; / /NEW
}
catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( e+" in mutateStandard "+" "+lMutIndex+" "+ioTree . get ( lMutIndex ) . getS
System . e x i t ( 0 ) ;
}
}
int l D i f f S i z e =0;
try {
l D i f f S i z e =ioTree . get ( lMutIndex ) . getSubTreeSize ( ) − lNewTree . get ( lMutIndex ) . getSubTreeS
}
catch ( java . lang . Exception e ) {
System . out . p r i n t l n ( e+" mutIndex : "+lMutIndex ) ;
/ / System . out . p r i n t l n ( ioTree . writeTree ( 0 ) ) ;
for ( int i i =0; i i <ioTree . size ( ) ; i i ++)
System . out . p r i n t ( ioTree . get ( i i ) . getSubTreeSize ()+ " " ) ;
System . e x i t ( 0 ) ;
}
for ( int i =0; i <lStack . size ( ) ; ++ i )
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXI
lNewTree . get ( lStack . get ( i ) ) . setSubTreeSize ( lNewTree . get ( lStack . get ( i ) ) . getSubTreeSize()−
/ / ioTree= n u l l ;
/ / ioTree=lNewTree ;
lNewTree . setValid ( false ) ;
lNewTree . setEvaluated ( false ) ;
ioTree . setValid ( false ) ;
ioTree . setEvaluated ( false ) ;
ioTree . clear ( ) ;
for ( int i =0; i <lNewTree . size ( ) ; i ++)
ioTree . add (new Node( lNewTree . get ( i ) ) ) ;
ioTree . usedMutationStd=true ;
/ / System . out . p r i n t l n ( " Following i s the tree a f t e r mutation :  n " ) ; / / + ioTree . writeTree ( 0 ) ) ;
/ / f o r ( i n t i =0; i <ioTree . size ( ) ; 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 : "+ lMutIndex ) ;
/ / return lNewTree ;
}
/∗ !
∗  b r i e f Apply swap point mutation to a population of GP trees .
∗  param ioPopulation Population to mutate .
∗  param ioContext Evolutionary context .
∗  param inMutationProba Mutation p r o b a b i l i t y .
∗  param inDistribProba P r o b a b i l i t y to mutate a function node , in opposition to a terminal .
∗  ingroup Puppy
∗/
public void applyMutationSwap ( ArrayList <Tree> ioPopulation )
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXII
{
float inMutationProba=Context . swapMutationProba ;
for ( int i =0; i <ioPopulation . size ( ) ; ++ i ) {
i f ( dice . nextFloat ( ) < inMutationProba ) {
mutateSwap ( ioPopulation . get ( i ) ) ;
}
}
}
/∗ !
∗  b r i e f Swap mutate a GP tree .
∗  param ioTree GP tree to mutate .
∗  param ioContext Evolutionary context .
∗  param inDistribProba P r o b a b i l i t y to mutate a function node , in opposition to a terminal .
∗  ingroup Puppy
∗/
public void mutateSwap ( Tree ioTree )
{
float inDistribProba=Context . xoverNodeDistribProb ;
assert ( ioTree . size ( ) > 0 ) ;
int lMutIndex = dice . nextInt ( ioTree . size ( ) ) ;
i f ( ioTree . size ( ) > 1) {
boolean lType = ( dice . nextFloat ( ) < inDistribProba ) ;
while ( ( ioTree . get ( lMutIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) != 0) != lType ) {
lMutIndex = dice . nextInt ( ioTree . size ( ) ) ;
}
}
int lNbArgs = ioTree . get ( lMutIndex ) . 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 S
PUPPY XXIII
i f ( lNbArgs == 0) {
assert ( Context . mTerminalSet . size ( ) > 0 ) ;
P r i m i t i v e lTerminal =
Context . mTerminalSet . get ( dice . nextInt ( Context . mTerminalSet . size ( ) ) ) ;
ioTree . get ( lMutIndex ) . s e t P r i m i t i v e ( lTerminal . clone ( ) . giveReference ( dice ) ) ;
}
else {
ArrayList <Integer > lKArgsFunction=new ArrayList ( ) ;
for ( int i =0; i <Context . mFunctionSet . size ( ) ; ++ i ) {
i f ( Context . mFunctionSet . get ( i ) . g e t A r i t y ( ) == lNbArgs ) {
lKArgsFunction . add (new Integer ( i ) ) ;
}
}
assert ( lKArgsFunction . size ( ) > 0 ) ;
P r i m i t i v e lFunction =Context . mFunctionSet . get ( lKArgsFunction . get ( dice . nextInt ( lKArgsFuncti
ioTree . get ( lMutIndex ) . s e t P r i m i t i v e ( lFunction . clone ( ) ) ;
}
ioTree . usedMutationSwap=true ;
}
/∗∗
∗ Apply standard ( Koza ’ s ) mutation to a population of GP trees .
∗  param ioPopulation Population to mutate .
∗  param ioContext Evolutionary context .
∗  param inMutationProba Mutation p r o b a b i l i t y .
∗  param inMaxRegenDepth Maximum tree regeneration depth allowed .
∗  param inMaxDepth Maximum tree depth allowed .
∗  ingroup Puppy
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXIV
∗/
public void applyMutationStandard ( ArrayList <Tree> ioPopulation )
{
float inMutationProba=Context . mutationProba ;
int inMaxRegenDepth=Context . initTreeDepthMax ;
int inMaxDepth=Context . maxTreeDepth ;
/ / System . out . p r i n t l n ( " Lets see how Mutation works  n"+ ioPopulation . get ( t h i s . parentPopSize ) .
for ( int i =this . parentPopSize ; i <ioPopulation . size ( ) ; i ++) {
i f ( dice . nextFloat () <= inMutationProba ) {
mutateStandard ( ioPopulation . get ( i ) , inMaxRegenDepth , inMaxDepth ) ;
}
}
/ / System . out . p r i n t l n ( " Mutation worked l i k e t h i s  n"+ ioPopulation . get ( t h i s . parentPopSize ) . w
/ / return ioPopulation ;
}
/∗∗
∗adapt the p r o b a b i l i t i e s of genetic operators in a dynamic fashion
∗/
public void adaptOperatorProbabilities ( List <Tree> lPopulation ) {
I t e r a t o r <Tree> popItr =lPopulation . i t e r a t o r ( ) ;
int numUsedXover=1 , numUsedMutStd=1 , numUsedMutSwap=1;
int numImpXover=0 , numImpMutStd=0 , numImpMutSwap=0;
while ( popItr . hasNext ( ) ) {
Tree tmpTree= popItr . next ( ) ;
i f ( tmpTree . usedXover ) {
numUsedXover++;
i f ( tmpTree . getTrainingFitness () <= Context . previousBest )
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXV
numImpXover++;
}
i f ( tmpTree . usedMutationStd ) {
numUsedMutStd++;
i f ( tmpTree . getTrainingFitness () <= Context . previousBest )
numImpMutStd++;
}
i f ( tmpTree . usedMutationSwap ) {
numUsedMutSwap++;
i f ( tmpTree . getTrainingFitness () <= Context . previousBest )
numImpMutSwap++;
}
}
Context . xoverProba =( float ) ( numImpXover / numUsedXover ) ;
Context . mutationProba =( float ) ( numImpMutStd / numUsedMutStd ) ;
Context . swapMutationProba =( float ) ( numImpMutSwap / numUsedMutSwap ) ;
i f ( Context . xoverProba <0.5F) Context . xoverProba =0.7F ;
i f ( Context . mutationProba <0.01F) Context . mutationProba =0.15F ;
i f ( Context . swapMutationProba <0.01F) Context . swapMutationProba =0.15F ;
}
/∗∗
∗Perform a l l Evolutionary steps in one go
∗1) ApplySelectionTournament
∗2) ApplyCrossover
∗3) ApplyMutationStandard
∗4) Evaluate f i t n e s s of the population
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXVI
∗5) Apply e l i t i s t s u r v i v a l
∗/
public Tree applyAllEvolutionarySteps ( ArrayList <Tree> lPopulation ) {
ArrayList <Tree> childPop=applySelectionTournament1 ( lPopulation ) ;
applyCrossover ( childPop ) ;
applyMutationStandard ( childPop ) ;
applyMutationSwap ( childPop ) ;
/ / Set the S−Expressions now here as t h i s w i l l save some computation
lPopulation . addAll ( childPop ) ;
evaluatePopulation ( lPopulation ) ;
adaptOperatorProbabilities ( lPopulation . subList ( Context . popSize , lPopulation . size () −1));
this . applySurvival1 ( lPopulation ) ;
for ( int i =0; i <10; i ++)
lPopulation . get ( i ) . tuneTreesCoeffs ( ) ;
Collections . sort ( lPopulation , new FitnessComparator ( ) ) ; / / sort again i f GA i s used
return lPopulation . get ( 0 ) ; / / return the best tree as i t remains the f i r s t element a f t e r so
}
/∗∗
∗Evaluate a given population
∗/
public void evaluatePopulation ( ArrayList <Tree> lPopulation ) {
I t e r a t o r <Tree> i t =lPopulation . i t e r a t o r ( ) ;
double bestFit =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 S
PUPPY XXVII
double f i t n e s s =tmpTree . evaluateTree ( ) ;
/ / i t . next ( ) . tuneTreesCoeffs ( ) ;
i f ( fitness <Context . previousBest ) Context . previousBest= 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 applySurvival1 ( ArrayList <Tree> ioPopulation ) {
Collections . sort ( ioPopulation . subList (0 , ioPopulation . size ()/2 −1) , new FitnessComparator ( ) )
Collections . sort ( ioPopulation . subList ( ioPopulation . size ( ) / 2 , ioPopulation . size () −1) , new F
/ / Collections . sort ( ioPopulation , new DepthComparator ( ) ) / / Sort wrt depth as well now −− I g
int dynDepth=ioPopulation . get ( ioPopulation . size ( ) / 2 ) . getDepth ( 0 ) ; / / of the best c h i l d
/ / i f ( dynDepth> t h i s . dynamicLevel )
i f ( dynDepth>=6 && ioPopulation . get ( ioPopulation . size ( ) / 2 ) . getTrainingFitness () < ioPopulatio
this . dynamicLevel=dynDepth ;
int children_ ;
for ( int i =ioPopulation . size ( ) / 2 ; i <ioPopulation . size ( ) ; i ++){ / / remove from among childPop
Tree tmpTree=ioPopulation . get ( i ) ;
i f ( tmpTree . getDepth (0) > this . dynamicLevel ) { / / dynamic tree depth
ioPopulation . remove ( i ) ;
i −−;
}
}
while ( ioPopulation . subList ( this . parentPopSize , ioPopulation . size ( ) ) . size () > this . parentPopS
ioPopulation . remove ( ioPopulation . size () −1); / / remove the extra children here
int i =this . parentPopSize −1;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXVIII
while ( ioPopulation . size () > this . parentPopSize )
ioPopulation . remove ( i −−); / / remove the less f i t parents now
Collections . sort ( ioPopulation , new FitnessComparator ( ) ) ;
Context . previousBest=ioPopulation . get ( 0 ) . getTrainingFitness ( ) ;
System . out . p r i n t l n ( this . dynamicLevel ) ;
}
}
/∗∗
∗Fitness Comparator
∗/
class FitnessComparator implements java . u t i l . Comparator<Tree >{
public int compare ( Tree o1 , Tree o2 ) {
double f i t 1 =o1 . getTrainingFitness ( ) ;
double f i t 2 =o2 . getTrainingFitness ( ) ;
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 S
PUPPY XXIX
∗Size ( Tree Depth ) Comparator
∗/
class DepthComparator implements java . u t i l . Comparator<Tree >{
public int compare ( Tree o1 , Tree o2 ) {
int depth1=o1 . getDepth ( 0 ) ;
int 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 S
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 in 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 . io . ∗ ;
/∗∗
∗
∗ @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 mPrimitive ; / / ! < Smart pointer to the associated p r i m i t i v e .
private int mSubTreeSize ; / / ! < Sub−tree size , including actual node .
/∗∗ Creates a new instance of Node ∗/
public Node( P r i m i t i v e p r i m i t i v e , int size ) {
this . mPrimitive= p r i m i t i v e . clone ( ) ;
this . mSubTreeSize=size ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
NODE II
/∗∗
∗Copy Constructor
∗/
public Node(Node copy ) {
/ / t h i s . mPrimitive=new P r i m i t i v e ( copy . mPrimitive ) ;
this . mPrimitive=copy . mPrimitive . clone ( ) ;
this . 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 . mPrimitive=new P r i m i t i v e ( prim ) ;
this . mPrimitive=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 this . mPrimitive ;
}
/∗∗
∗void subtree size
∗/
public void setSubTreeSize ( f i n a l int treeSize ) {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
NODE III
this . mSubTreeSize=treeSize ;
}
/∗∗
∗Return the subtree size
∗/
public f i n a l int getSubTreeSize ( ) {
return this . mSubTreeSize ;
}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
EVALUATION I
/∗
∗ Evaluation . java
∗
∗ Created on September 6 , 2007, 9:18 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗Keeps the Evaluation of a subtee l y i n g in a cache
∗/
package GeneticProgramming ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
public class Evaluation {
public double [ ] evaluation ;
public boolean cachedFlag ;
/∗∗
∗The default constructor f o r Terminal nodes
∗/
public Evaluation ( ) {
this . cachedFlag=true ; / / Vals f o r Ts always get true
this . evaluation=new double [ 1 ] ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
EVALUATION II
}
/∗∗
∗The contructor f o r NT subtrees
∗/
public Evaluation ( double [ ] eval , boolean cFlag ) {
this . evaluation=new double [ eval . length ] ;
for ( int i =0; i <eval . length ; i ++){
this . evaluation [ i ]= eval [ i ] ;
this . cachedFlag=cFlag ; / / Defines Whether an entry 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 S
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 in 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 . io . ∗ ;
public class Tree extends ArrayList <Node> implements S e r i a l i z a b l e {
private double fitness , testFitness ;
private boolean v a l i d ;
private String s_expression ;
private boolean evaluated ; / / t e l l s i f the f i t n e s s of t h i s tree has been evaluated
private double slope ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
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 contains the r e s u l t of the i n d i v i d u a l over whole
public static double [ ] forConstants ; / / These two variables s h a l l be used by the p r i m i t i v e s
public boolean usedXover , usedMutationStd , usedMutationSwap ;
/∗∗ Creates a new instance of Tree ∗/
public Tree ( ) {
this . f i t n e s s =Double .MAX_VALUE; / / assign a f i t n e s s value
this . v a l i d =false ;
this . evaluated=false ;
}
/∗∗
∗ Construct a new tree , with given f i t n e s s and v a l i d i t y f l a g .
∗/
public Tree ( double sFitness , boolean i V a l i d ) {
this . f i t n e s s =sFitness ;
this . v a l i d = i V a l i d ;
this . s_expression=new String ( ) ;
this . evaluated=false ; / / that i t has not been evaluated by default
this . slope =1;
this . i n t e r c e p t =0;
}
/∗∗
∗ copy Constructor
∗/
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE III
public Tree ( f i n a l Tree copy ) {
this . f i t n e s s =copy . f i t n e s s ;
this . testFitness =copy . testFitness ;
this . v a l i d =copy . v a l i d ;
/ / t h i s . s_expression=copy . writeTree ( 0 ) ;
this . evaluated=copy . evaluated ;
for ( int i =0; i <copy . size ( ) ; i ++)
this . add (new Node( copy . get ( i ) ) ) ;
this . slope=copy . slope ;
this . 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 tree depth at given index .
∗  param inIndex Index of sub−tree root to get the depth from .
∗  return Sub−tree depth .
∗/
public f i n a l int getDepth ( int inIndex )
{
i f ( inIndex < this . size ( ) ) { / / I had assert here
int lNbArgs = this . get ( inIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
int lDepth = 1;
int j = inIndex + 1;
for ( int i =0; i <lNbArgs ; ++ i ) {
int lChildDepth = getDepth ( j ) + 1;
i f ( lChildDepth > lDepth ) lDepth = lChildDepth ;
j += this . get ( j ) . getSubTreeSize ( ) ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE IV
return lDepth ;
}
else {
return 0;
}
}
/∗∗
∗ Compare e qu al it y of two trees .
∗ Two trees 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 bucketting reasons
∗ return : True i s trees are equals , false i f not .
∗/
public f i n a l boolean compareTrees ( f i n a l Tree aTree )
{
i f ( this==aTree ) return true ;
return ( this . v a l i d && aTree . v a l i d && ( java . lang . Math . abs ( this . f i t n e s s − aTree . f i t n e s s ) <0.01
}
/∗∗
∗ Compare ranking of two trees .
∗  return True i s actual tree i s less than a tree given as argument , false i f not .
∗/
public f i n a l boolean compareRank ( Tree aTree )
{
return ( this . v a l i d && aTree . v a l i d && ( this . 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 S
TREE V
/∗
∗ I n t e r p r e t the GP tree .
∗  param outResult Datum containing the r e s u l t of the i n t e r p r e t a t i o n .
∗  param ioContext Evolutionary context .
∗
void Puppy : : Tree : : i n t e r p r e t ( void∗ outResult , Puppy : : Context& ioContext )
{
assert ( size ( ) > 0 ) ;
ioContext . mTree = t h i s ;
ioContext . mCallStack . push_back ( 0 ) ;
f r o n t ( ) . mPrimitive−>execute ( outResult , ioContext ) ;
ioContext . mCallStack . pop_back ( ) ;
} ∗/
/∗∗
∗ Set c a l l stack to include the c o r r e c t l y r e f e r to a given node .
∗  param inIndex Node index to which c a l l stack must be set .
∗  param outCallStack Result of c a l l stack s e t t i n g .
∗/
public f i n a l java . u t i l . ArrayList <Integer > setStackToNode ( int inIndex )
{
assert ( inIndex < size ( ) ) ;
ArrayList <Integer > callStack=new ArrayList ( ) ;
int i = 0;
callStack . add (new Integer ( i ) ) ;
while ( i < inIndex ) {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE VI
int lNbArgs=this . get ( i ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
int lChildIndex = i + 1;
for ( int j =0; j <lNbArgs ; ++ j ) {
i f ( ( lChildIndex+this . get ( lChildIndex ) . getSubTreeSize ( ) ) > inIndex ) break ;
lChildIndex += this . get ( lChildIndex ) . getSubTreeSize ( ) ;
}
assert ( lChildIndex < this . size ( ) ) ;
i = lChildIndex ;
callStack . add (new Integer ( i ) ) ;
}
assert ( i == inIndex ) ;
return callStack ;
}
/∗∗
∗Returns the slope parameter found by l i n e a r scaling
∗/
public f i n a l double getSlope ( ) {
return this . slope ;
}
/∗∗
∗Returns the i n t e r c e p t parameter found by l i n e a r scaling
∗/
public f i n a l double getIntercept ( ) {
return this . i n t e r c e p t ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE VII
/∗∗
∗  b r i e f Write GP tree at given index as a s−expression i n t o a Java String object .
∗  param ioOS C++ output stream to write tree i n t o .
∗  param inIndex Actual node index in tree .
∗/
public void writeTree ( int inIndex , StringBuilder expression )
{
i f ( inIndex >= this . size ( ) ) return ;
/ / expression . append ( " " ) ;
int lNbArgs = this . get ( inIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
expression . append ( this . get ( inIndex ) . g e t P r i m i t i v e ( ) . getName ( ) ) ;
i f ( lNbArgs > 0) expression . append ( " ( " ) ;
int j = inIndex + 1;
for ( int i =0; i <lNbArgs ; ++ i ) {
writeTree ( j , expression ) ;
i f ( i <lNbArgs−1)
expression . append ( " , " ) ;
try {
j = j +this . get ( j ) . getSubTreeSize ( ) ;
}
catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( e+" In write tree " ) ;
/ / System . e x i t ( 0 ) ;
}
}
i f ( lNbArgs > 0) expression . append ( " ) " ) ;
/ / return expression . t r t r i m ( ) ; / / removes the " " added in the beginning .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE VIII
}
/∗∗
∗  b r i e f Write GP tree at given index as a s−expression i n t o a Java String object .
∗  param ioOS C++ output stream to write tree i n t o .
∗  param inIndex Actual node index in tree .
∗/
public void writeTree2 ( int inIndex , StringBuilder expression )
{
i f ( inIndex >= this . size ( ) ) return ;
/ / expression . append ( " " ) ;
int lNbArgs = this . get ( inIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
expression . append ( this . get ( inIndex ) . g e t P r i m i t i v e ( ) . getName ( ) ) ;
/ / i f ( lNbArgs > 0) expression . append ( " ( " ) ;
int j = inIndex + 1;
for ( int i =0; i <lNbArgs ; ++ i ) {
writeTree ( j , expression ) ;
/ / i f ( i <lNbArgs−1)
/ / expression . append ( " , " ) ;
try {
j = j +this . get ( j ) . getSubTreeSize ( ) ;
}
catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( e+" In write tree " ) ;
/ / System . e x i t ( 0 ) ;
}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE IX
/ / i f ( lNbArgs > 0) expression . append ( " ) " ) ;
/ / return expression . t r t r i m ( ) ; / / removes the " " added in the beginning .
}
/∗∗
∗sets the s−expression
∗/
public void setSExpression ( ) {
StringBuilder expr=new StringBuilder ( this . size ( ) ) ;
this . writeTree (0 , expr ) ;
this . s_expression=new String ( expr . t oS tr in g ( ) ) ;
}
/∗∗
∗Gets the s−epression
∗/
public f i n a l String getSExpression ( ) {
return this . s_expression ;
}
/∗∗
∗Set the v a l i d f l a g to something
∗/
public void setValid ( f i n a l boolean inValid ) {
this . v a l i d = inValid ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE X
/∗∗
∗Get the v a l i d i t y status of t h i s tree
∗/
public f i n a l boolean getValid ( ) {
return this . 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 ) {
this . s e t P r i m i t i v e ( prim ) ;
}
/∗∗
∗Set the evaluated f l a g of t h i s tree
∗/
public void setEvaluated ( f i n a l boolean eval ) {
this . evaluated=eval ;
}
/∗∗
∗Get the evaluation f l a g of t h i s tree
∗/
public f i n a l boolean getEvaluated ( ) {
return this . evaluated ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XI
/∗∗
∗Set the f i t n e s s of t h i s Tree
∗/
public void setTrainingFitness ( f i n a l double f i t ) {
this . 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 of t h i s Tree
∗/
public f i n a l double getTrainingFitness ( ) {
return this . f i t n e s s ;
}
/∗∗
∗Return the f i t n e s s of Testing data
∗/
public f i n a l double getTestingFitness ( ) {
return this . testFitness ;
}
/ / public void run ( ) {
/ / t h i s . evaluateTree ( ) ;
/ / }
/∗∗
∗ Evaluate f i t n e s s of a Tree
∗
∗ inX Independant sample values f o r evaluation .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XII
∗ inF Dependant sample values f o r evaluation .
∗  return Number of f i t n e s s evaluated .
∗  ingroup SymbReg
∗/
public double evaluateTree ( )
{
assert ( Context . XData . length == Context . Target . length ) ;
i f ( this . evaluated )
return this . f i t n e s s ;
double [ ] target =Context . Target ;
double [ ] evolved=this . interpretTree ( Context . XData ) ;
i f ( evolved . length != Context . Target . length ) {
double tmpEvolved=evolved [ 0 ] ; / / i f everything that i s returned i s a constant
evolved=new double [ Context . Target . length ] ;
for ( int z=0;z<Context . Target . length ; z++)
evolved [ z ]= tmpEvolved ;
}
double [ ] slope_n_intercept=new double [ 2 ] ;
linearScaling ( slope_n_intercept , evolved , Context . Target ) ;
/ / t h i s . t r a i n i n g R e s u l t =new double [ evolved . length ] ;
double tmpFitness =0;
double res ;
for ( int i =0; i <evolved . length ; i ++){
res=Context . Target [ i ]−( slope_n_intercept [0]∗ evolved [ i ]+ slope_n_intercept [ 1 ] ) ;
tmpFitness+=res∗res ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XIII
}
tmpFitness=tmpFitness / evolved . length ;
/ / i f ( tmpFitness < t h i s . f i t n e s s ) {
this . f i t n e s s =tmpFitness ;
this . slope=slope_n_intercept [ 0 ] ;
this . i n t e r c e p t =slope_n_intercept [ 1 ] ;
/ / }
i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) )
this . 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 ) ;
this . v a l i d = true ;
this . evaluated=true ;
return this . f i t n e s s ;
}
/∗∗
∗ Evaluate f i t n e s s of a Tree
∗
∗ inX Independant sample values f o r evaluation .
∗ inF Dependant sample values f o r evaluation .
∗  return Number of f i t n e s s evaluated .
∗  ingroup SymbReg
∗/
public double evaluateTreeForTestData ( )
{
assert ( Context . TestXData . length == Context . TestTarget . length ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XIV
double [ ] target =Context . TestTarget ;
double [ ] evolved=this . interpretTree ( Context . TestXData ) ;
i f ( evolved . length != Context . TestTarget . length ) {
double tmpEvolved=evolved [ 0 ] ; / / i f everything that i s returned i s a constant
evolved=new double [ Context . TestTarget . length ] ;
for ( int z=0;z<Context . TestTarget . length ; 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 . length ] ;
double tmpFitness =0;
double res ;
for ( int i =0; i <evolved . length ; i ++){
res=Context . TestTarget [ i ]−( this . slope∗evolved [ i ]+ this . i n t e r c e p t ) ;
tmpFitness+=res∗res ;
}
tmpFitness=tmpFitness / evolved . length ;
/ / i f ( tmpFitness < t h i s . f i t n e s s ) {
this . testFitness =tmpFitness ;
i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) )
this . testFitness =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 ) ;
this . v a l i d = true ;
this . evaluated=true ;
return this . testFitness ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XV
/∗∗
∗LinearScaling , by Maarten Keijzer , i s implemented here
∗/
public void linearScaling ( double [ ] slope_n_intercept , double [ ] evolved , double [ ] target ) {
assert ( evolved . length== target . length ) ;
double meanTarget=0 , meanEvolved=0;
for ( int i =0; i <evolved . length ; i ++){
meanEvolved+=evolved [ i ] ;
meanTarget+= target [ i ] ;
}
meanEvolved /= evolved . length ;
meanTarget /= target . length ;
double numerator =0 , denominator =0;
for ( int i =0; i <evolved . length ; i ++){
numerator +=( target [ i ]−meanTarget )∗( evolved [ i ]−meanEvolved ) ;
denominator+=java . lang . Math . pow( evolved [ i ]−meanEvolved , 2 ) ;
}
/ / numerator=numerator / target . length ;
/ / denominator /= evolved . length ;
double slope1 =1;
i f ( denominator !=0) slope1=numerator / denominator ;
else i f ( meanEvolved==0) slope1 =1;
else slope1=meanTarget / meanEvolved ;
slope_n_intercept [0]= slope1 ;
slope_n_intercept [1]= meanTarget−slope∗meanEvolved ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XVI
}
/∗∗
∗ I n t e r p r e t the GP tree .
∗ outResult Datum containing the r e s u l t of the i n t e r p r e t a t i o n .
∗ XData to be evaluated
∗/
public double [ ] interpretTree ( float [ ] [ ] xdata )
{
assert ( this . size ( ) > 0 ) ;
ArrayList <Integer > callStack=new ArrayList ( ) ;
callStack . add (new Integer ( 0 ) ) ;
double [ ] outResult=this . get ( 0 ) . g e t P r i m i t i v e ( ) . execute ( callStack , this , xdata ) ;
callStack . remove ( callStack . size () −1);
return outResult ;
}
/∗∗
∗ tune the terminals ( constants ) of t h i s tree
∗
∗ inX Independant sample values f o r evaluation .
∗ inF Dependant sample values f o r evaluation .
∗  return Number of f i t n e s s evaluated .
∗  ingroup SymbReg
∗/
public double tuneTreesCoeffs ( )
{
assert ( Context . XData . length == Context . Target . length ) ;
/ / i f ( t h i s . evaluated ) return 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 S
TREE XVII
I t e r a t o r <Node> nodItr =this . i t e r a t o r ( ) ;
int i i =0;
while ( nodItr . hasNext ( ) ) {
Node tmpNode= nodItr . 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 ( ) . startsWith ( "X" ) )
i i ++; / / f i n d the number of constants
}
i f ( i i <=1)
return this . 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
int [ ] constIndex=new int [ i i ] ; / / keeps the indices of constants in t h i s tree
double [ ] constants=new double [ i i ] ; / / t h i s array would keep the constants f o r the interim ( a t
int j j =0;
for ( i i =0; i i <this . size ( ) ; i i ++){
i f ( this . get ( i i ) . g e t P r i m i t i v e ( ) . g e t A r i t y ()==0
&& ! this . get ( i i ) . g e t P r i m i t i v e ( ) . getName ( ) . startsWith ( "X" ) ) {
constIndex [ j j ]= i i ; / / t h i s loop copies the indices to the array
constants [ j j ]= Double . valueOf ( this . 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 here
Nursery nurse=new Nursery ( ) ;
ArrayList <Genotype> pop=nurse . i n i t i a l i z e P o p u l a t i o n (100 , constIndex . length , −6, +6); / / should
for ( int i =0; i <50; i ++){ / / numGens i s chosen to be 150
ArrayList <Genotype> childPop=nurse . applySelectionTournament ( pop , 4 ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XVIII
nurse . applySinglePointCrossover ( childPop , constIndex . length , 0 . 8 ) ;
nurse . applyMutationUniform ( childPop , constIndex . length , 0 . 2 ) ;
pop . addAll ( childPop ) ; / / append the two pops here
for ( int j =0; j <pop . size ( ) ; j ++){
i f ( pop . get ( j ) . getEvaluatedFlag ()== false ) { / / i f i t has not been evaluated
double [ ] tmpGenes=pop . get ( j ) . getGenotype ( ) ;
for ( int k=0;k<constIndex . length ; k++)
this . get ( constIndex [ k ] ) . g e t P r i m i t i v e ( ) . setName ( Double . to St rin g ( tmpGenes [ k
double [ ] evolved=this . interpretTree ( Context . XData ) ;
i f ( evolved . length != Context . Target . length ) {
double tmpEvolved=evolved [ 0 ] ; / / i f everything that i s returned i s a constan
evolved=new double [ Context . Target . length ] ;
for ( int z=0;z<Context . Target . length ; z++)
evolved [ z ]= tmpEvolved ;
}
double [ ] slope_n_intercept=new double [ 2 ] ;
linearScaling ( slope_n_intercept , evolved , Context . Target ) ;
/ / t h i s . t r a i n i n g R e s u l t =new double [ evolved . length ] ;
double tmpFitness =0;
double res ;
for ( int i j =0; i j <Context . Target . length ; i j ++){
res=Context . Target [ i j ]−( slope_n_intercept [0]∗ evolved [ i j ]+ slope_n_intercep
tmpFitness+=res∗res ;
}
tmpFitness=tmpFitness / evolved . length ;
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 S
TREE XIX
i f ( tmpFitness <this . f i t n e s s ) {
this . f i t n e s s =tmpFitness ;
this . slope=slope_n_intercept [ 0 ] ;
this . i n t e r c e p t =slope_n_intercept [ 1 ] ;
for ( int k=0;k<tmpGenes . length ; k++)
constants [ k ]=tmpGenes [ k ] ;
}
pop . get ( j ) . setFitness ( tmpFitness ) ;
i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) )
{
this . f i t n e s s =Double .MAX_VALUE; / / assign a very large f i t n e s s
pop . get ( j ) . setFitness ( Double .MAX_VALUE) ;
}
pop . get ( j ) . setEvaluated ( true ) ;
} / / i f getEvaluated
}
nurse . applySurvival ( pop ) ;
}
for ( int i =0; i <constants . length ; i ++)
this . get ( constIndex [ i ] ) . g e t P r i m i t i v e ( ) . setName ( Double . to St rin g ( constants [ i ] ) ) ; / / set th
/ / System . out . p r i n t l n ( " Current best "+ t h i s . f i t n e s s ) ;
this . v a l i d = true ;
this . evaluated=true ;
return this . f i t n e s s ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XX
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
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 in the e d i t o r .
∗/
package GeneticProgramming ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import java . io . ∗ ;
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 instance of S t a t i s t i c s ∗/
public S t a t i s t i c s ( ) {
}
/∗∗
∗copy constructor
∗/
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 . length ] ;
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 . length ] ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
STATISTICS II
for ( int i =0; i <copy . t r a i n F i t H i s t o r y . length ; 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 static double [ ] t r a i n F i t H i s t o r y ;
public static double [ ] t e s t F i t H i s t o r y ;
public static 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 S
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 in the e d i t o r .
∗/
package GeneticProgramming ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import java . io . ∗ ;
import java . u t i l . ∗ ;
public class S t a t i s t i c s I O {
/∗∗ Creates a new instance 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 ( ) {
}
/∗∗
∗writeStatsToDisk
∗/
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
STATISTICS IO II
public static void writeStatsToDisk ( ArrayList < S t a t i s t i c s > simStats , String filename ) throw
ObjectOutputStream out = null ;
try {
out = new ObjectOutputStream (new FileOutputStream ( filename ) ) ;
out . writeObject ( ( Object ) simStats ) ;
out . flush ( ) ;
} f i n a l l y {
i f ( out != null ) {
try {
out . close ( ) ;
} catch ( IOException exception ) { }
}
}
}
public static ArrayList < S t a t i s t i c s > readStatsFromDisk ( String filename ) throws IOException
ObjectInputStream in = null ;
try {
in=new ObjectInputStream (new FileInputStream ( filename ) ) ;
return ( ArrayList < S t a t i s t i c s >) in . readObject ( ) ;
} f i n a l l y {
i f ( in != null ) {
try {
in . close ( ) ;
} catch ( IOException e ) { }
}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
STATISTICS IO III
}
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
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 in 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 . io . ∗ ;
public class Context {
/∗∗ Creates a new instance of Context ∗/
public Context ( ) {
this . mFunctionSet=new ArrayList ( ) ;
this . mTerminalSet=new ArrayList ( ) ;
this . mPrimitiveMap=new TreeMap ( ) ;
this .mCache=new MyLinkedHashMap(500 , .75F , true ) ;
this . previousBest=Double .MAX_VALUE;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT II
}
/∗∗
∗  b r i e f Add a new p r i m i t i v e in the sets of 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 static 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 )
{
assert ( ! mPrimitiveMap . 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 static void i n i t D a t a ( float [ ] [ ] X, float [ ] Y) {
assert (X. length==Y. length ) ;
XData=new float [X. length ] [ X [ 0 ] . length ] ;
Target=new double [X. length ] ;
for ( int i =0; i <X. length ; i ++){
for ( int j =0; j <X [ 0 ] . length ; 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 S
CONTEXT III
}
/∗∗
∗This i s used to i n i t i a l i z e the Test data
∗/
public static void initTestData ( float [ ] [ ] X, double [ ] Y) {
assert (X. length==Y. length ) ;
TestXData=new float [X. length ] [ X [ 0 ] . length ] ;
TestTarget=new double [X. length ] ;
for ( int i =0; i <X. length ; i ++){
for ( int j =0; j <X [ 0 ] . length ; j ++)
TestXData [ i ] [ j ]=X[ i ] [ j ] ;
TestTarget [ i ]=Y[ i ] ;
}
}
/∗∗
∗This function i s used to read in data patterns . To be used by the java program
∗params :
∗filename−the f i l e containing IO patterns
∗sizeX− number of patterns
∗sizeY− number of variables ( f o r input data )
∗trainORtest− i f true , then t r a i n data , else t e s t data
∗/
public static void readDataPatterns ( int sizeX , int sizeY , boolean trainORtest ) {
StreamTokenizer tok=null ;
String filenameX , filenameY ;
i f ( trainORtest ) {
filenameX=trainDataFileX ;
filenameY=trainDataFileY ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT IV
}
else {
filenameX=testDataFileX ;
filenameY=testDataFileY ;
}
try {
tok = new StreamTokenizer (new BufferedReader (new FileReader ( filenameX ) ) ) ;
tok . ordinaryChar ( ’  n ’ ) ;
/ / tok . ordinaryChar ( )
}
catch ( java . io . FileNotFoundException e ) {
System . out . p r i n t l n ( e+" in Context . java " ) ;
}
int i =0;
int j =0;
float [ ] [ ] curDat=new float [ sizeX ] [ sizeY ] ;
String s t r ;
try {
j =0;
while ( tok . nextToken ( ) ! = StreamTokenizer .TT_EOF) {
i f ( j >=44){ j =0; i ++;}
switch ( tok . ttype ) {
case StreamTokenizer .TT_EOL:
i ++;
j =0;
break ;
case ’  r ’ :
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT V
i ++;
j =0;
break ;
case ’n ’ :
i ++;
j =0;
break ;
case StreamTokenizer .TT_NUMBER:
curDat [ i ] [ j ] = ( float ) tok . nval ;
/ / System . out . p r i n t l n ( tok . nval ) ;
/ / j = j +1;
/ / i f ( j ==3){
/ / j =0;
/ / i ++;
/ / }
j ++;
break ;
default :
/ / i f ( tok . ttype != StreamTokenizer .TT_NUMBER && ( tok . sval . compareTo ( "  t " ) ! = 0
/ / j =0;
/ / i ++;
/ / }
break ;
}
}
}
catch ( java . io . EOFException e ) {
/ / do nothing
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT VI
}
catch ( java . io . IOException e ) {
/ / do nothing
}
catch ( java . lang . ArrayIndexOutOfBoundsException e ) {
/ / do nothing agin : (
System . out . p r i n t l n ( e+" Here l i e s the bug "+trainORtest+ 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 ( ) ;
}
try {
tok = new StreamTokenizer (new BufferedReader (new FileReader ( filenameY ) ) ) ;
tok . ordinaryChar ( ’  n ’ ) ;
/ / tok . ordinaryChar ( )
}
catch ( java . io . FileNotFoundException e ) {
System . out . p r i n t l n ( e+" in Context . java " ) ;
}
i =0;
double [ ] DatY=new double [ sizeX ] ;
try {
j =0;
while ( tok . nextToken ( ) ! = StreamTokenizer .TT_EOF) {
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT VII
switch ( tok . ttype ) {
case StreamTokenizer .TT_EOL:
/ / i ++;
break ;
/ / case ’  n ’ :
/ / i ++;
/ / j =0;
/ / break ;
case StreamTokenizer .TT_NUMBER:
DatY [ i ]= tok . nval ;
i ++;
/ / System . out . p r i n t l n ( tok . nval ) ;
break ;
default :
/ / j ++;
break ;
}
}
}
catch ( java . io . EOFException e ) {
/ / do nothing
}
catch ( java . io . IOException e ) {
/ / do nothing
}
catch ( java . lang . ArrayIndexOutOfBoundsException e ) {
/ / do nothing 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 S
CONTEXT VIII
System . e x i t ( 0 ) ;
}
i f ( trainORtest ) {
XData=curDat ;
Target=DatY ;
}
else {
TestXData=curDat ;
TestTarget=DatY ;
}
/ / f o r ( i n t j j =0; j j <XData . length ; j j ++){
/ / XData [ j j ][1]∗=100;
/ / }
}
public static ArrayList <Primitive > mFunctionSet ; / / ! < Set of functions usable to
public static ArrayList <Primitive > mTerminalSet ; / / ! < Set of terminals usable to
public static TreeMap<String , Primitive > mPrimitiveMap ; / / ! < Name−p r i m i t i v e map.
public static LinkedHashMap<String , Evaluation > mCache;
public static float mutationProba , swapMutationProba , xoverProba , inInitGrowProba , xoverNodeDi
public static int dynamicTreeDepth , popSize , initTreeDepthMin , initTreeDepthMax , tournamentSiz
public static float [ ] [ ] XData ;
public static double [ ] Target ;
public static float [ ] [ ] TestXData ;
public static double [ ] TestTarget ;
public static String trainDataFileX ;
public static String trainDataFileY ;
public static String testDataFileX ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT IX
public static String testDataFileY ;
public static boolean useTestData ;
public static int length_of_run ;
public static double previousBest ; / / f i t n e s s of the best i n d i v i d u a l of the previous generation
}
class MyLinkedHashMap extends LinkedHashMap {
/ / This method i s called j u s t a f t e r a new entry has been added
public MyLinkedHashMap ( int val1 , float val2 , boolean val3 ) {
super ( val1 , val2 , val3 ) ;
}
public boolean removeEldestEntry (Map. Entry eldest ) {
return size ( ) > 500;
}
/∗ public boolean containsKey ( String key ) {
I t e r a t o r <String > s t r = t h i s . keySet ( ) . i t e r a t o r ( ) ;
while ( s t r . hasNext ( ) ) {
i f ( s t r . next ( ) . compareTo ( key )==0){
return true ;
}
}
return false ;
}
public Evaluation get ( String key ) {
Evaluation 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 . entrySet ( ) . i t e r a t o r ( ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT X
while ( 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 oS tri ng ( ) . compareTo ( key )==0){
return ( Evaluation ) temp . getValue ( ) ;
}
}
return n u l l ;
} ∗/
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES I
/∗
∗ PrimitiveHandle . java
∗
∗ Created on August 10 , 2007, 9:02 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in 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 . bcel . 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 . io . ∗ ;
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 String name;
protected int a r i t y ;
protected int mRefCounter ;
protected Random dice ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES II
/∗∗ Creates a new instance of PrimitiveHandle ∗/
public P r i m i t i v e ( ) {
}
/∗∗
∗  param inNumberArguments Number of arguments 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 .
∗/
public P r i m i t i v e ( String inName , int a r i t y )
{
this .name=new String ( inName ) ;
this . a r i t y = a r i t y ;
mRefCounter=0;
}
/∗∗
∗  b r i e f Copy−construct 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 )
{
this .name=new String ( copy .name ) ;
this . a r i t y =copy . a r i t y ;
mRefCounter=0;
}
/∗∗
∗Assign the input argument ( P r i m i t i v e object ) to
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES III
∗t h i s object
∗/
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 ( this == p r i m i t i v e ) return this ;
name = new String ( p r i m i t i v e .name ) ;
this . a r i t y = p r i m i t i v e . a r i t y ;
return this ;
}
/∗∗
∗  b r i e f Give a reference on the actual p r i m i t i v e .
∗  param object of Random
∗  return P r i m i t i v e handle to t h i s pointer .
∗/
public abstract P r i m i t i v e giveReference (Random dice ) ; / / { return n u l l ; }
/∗ {
i f ( t h i s . getName ( ) . equalsIgnoreCase ( " Ephemeral " ) ) { / / should return the Ephemeral random numbe
return new P r i m i t i v e ( java . lang . Double . t oS tr in g ( dice . nextDouble ()∗12 −6) ,0);
}
return 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 S
PRIMITIVES IV
public void setName ( String inName )
{
this .name =new String ( inName ) ;
}
/∗∗
∗return the name of t h i s p r i m i t i v e
∗/
public f i n a l String getName ( ) {
return this .name;
}
/∗∗
∗  b r i e f Set the number of arguments of the p r i m i t i v e .
∗  param inNumberArguments Number of arguments of the p r i m i t i v e .
∗/
public void s e t A r i t y ( int i n A r i t y )
{
this . a r i t y = i n A r i t y ;
}
/∗ !
∗  b r i e f Set the value of the p r i m i t i v e ( do nothing f o r basic p r i m i t i v e ) .
∗  param inValue New value to use .
∗/
/ / void Puppy : : P r i m i t i v e : : setValue ( const void∗ inValue )
/ / { }
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES V
/∗∗
∗Returns the a r i t y of t h i s p r i m i t i v e
∗/
public f i n a l int g e t A r i t y ( ) {
return this . a r i t y ;
}
/∗∗
∗  b r i e f Get the value of the nth argument .
∗  param inN Index of the argument to get .
∗  param outResult Value of the nth argument .
∗  param ioContext Evolutionary context .
∗ XData
∗ patternNumber
∗/
public double [ ] getArgument ( int inN , ArrayList <Integer > callStack , Tree tree ,
f i n a l float [ ] [ ] XData )
{
assert ( inN >= 0 ) ;
assert ( inN < this . g e t A r i t y ( ) ) ;
/ /
/ /
int lIndex = callStack . get ( callStack . size () −1). intValue ( ) + 1;
for ( int i =0; i <inN ; ++ i ) lIndex += tree . get ( lIndex ) . getSubTreeSize ( ) ;
callStack . add (new Integer ( lIndex ) ) ;
double [ ] outResult=tree . get ( lIndex ) . g e t P r i m i t i v e ( ) . execute ( callStack , tree , XData ) ;
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES VI
callStack . remove ( callStack . size () −1);
return outResult ;
}
public abstract P r i m i t i v e clone ( ) ;
/∗∗
∗Function i s used to execute t h i s p r i m i t i v e
∗outResult
∗callStack
∗tree to which t h i s p r i m i t i v e belongs
∗XData
∗patternNumber , that comes in from evaluateTree in Tree
∗/
/ / public abstract double [ ] execute ( ArrayList <Integer > callStack , Tree tree , f l o a t [ ] [ ] XData ) ;
public abstract double [ ] execute ( ArrayList <Integer > callStack , Tree tree , float [ ] [ ] XData ) ; / /
/ / c a l l the cache here?
/∗ StringBuilder key=new StringBuilder ( tree . size ()− callStack . get ( callStack . size () −1). intVal
tree . writeTree2 ( callStack . get ( callStack . size () −1). intValue ( ) , key ) ;
Evaluation evaluation=PuppysMain . lContext .mCache. get ( key . to St rin g ( ) ) ;
i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . only non−terminals are evaluated here
i f ( evaluation != n u l l ) {
/ / double [ ] evaled=lContext .mCache. get ( key . t oSt ri ng ( ) ) . evaluation ;
/ / f o r ( i n t i =0; i <evaled . length ; i ++)
/ / outResult [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lContext .mCache. get ( key . t oS tr in g ( ) ) . cachedFlag=true ; / / the entry i s now
PuppysMain . cacheHits+=tree . get ( callStack . size () −1). getSubTreeSize ( ) ; ;
return evaluation . evaluation ;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES VII
}
∗/
/ / double lArg2 ;
/∗ double [ ] outResult= n u l l ;
switch ( t h i s . a r i t y ) {
case 2 : { / / f o r a l l the two a r i t y functions
double [ ] lArg2= n u l l ;
outResult=getArgument (0 , callStack , tree , XData ) ;
lArg2=getArgument (1 , callStack , tree , XData ) ;
i f ( t h i s . getName ( ) . compareTo ( " plus ")==0){
i f ( outResult . length==lArg2 . length )
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] += lArg2 [ i ] ;
else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
f o r ( i n t i =lArg2 . length;−−i >=0;)
lArg2 [ i ]+= outResult [ 0 ] ;
outResult=lArg2 ; / / point outResult to t h i s array now
}
else i f ( lArg2 . length ==1){
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ]+= lArg2 [ 0 ] ;
}
}
else i f ( t h i s . getName ( ) . compareTo ( " minus ")==0){
i f ( outResult . length==lArg2 . length )
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] −= lArg2 [ i ] ;
else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES VIII
f o r ( i n t i =lArg2 . length;−−i >=0;)
lArg2 [ i ]−=outResult [ 0 ] ;
outResult=lArg2 ; / / point outResult to t h i s array now
}
else i f ( lArg2 . length ==1){
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ]−=lArg2 [ 0 ] ;
}
}
else i f ( t h i s . getName ( ) . compareTo ( " times ")==0){
i f ( outResult . length==lArg2 . length )
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] ∗= lArg2 [ i ] ;
else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
f o r ( i n t i =lArg2 . length;−−i >=0;)
lArg2 [ i ]∗= outResult [ 0 ] ;
outResult=lArg2 ; / / point outResult to t h i s array now
}
else i f ( lArg2 . length ==1){
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ]∗= lArg2 [ 0 ] ;
}
}
else i f ( t h i s . getName ( ) . compareTo ( " adilpower ")==0){
i f ( outResult . length==lArg2 . length )
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] =Math . pow( outResult [ i ] , lArg2 [ i ] ) ;
else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES IX
f o r ( i n t i =lArg2 . length;−−i >=0;)
lArg2 [ i ]=Math . pow( lArg2 [ i ] , outResult [ 0 ] ) ;
outResult=lArg2 ; / / point outResult to t h i s array now
}
else i f ( lArg2 . length ==1){
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ]=Math . pow( outResult [ i ] , lArg2 [ 0 ] ) ;
}
}
else { / / divide
i f ( outResult . length==lArg2 . length ) {
f o r ( i n t i =outResult . length;−−i >=0;){
t r y {
outResult [ i ] /= lArg2 [ i ] ;
}
catch ( Exception e ) {
outResult [ i ]= Double .NaN;
}
}
}
else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
f o r ( i n t i =lArg2 . length;−−i >=0;){
t r y {
lArg2 [ i ]+= outResult [ 0 ] ;
}
catch ( Exception e ) {
outResult [ i ]= Double .NaN;
}
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES X
}
outResult=lArg2 ; / / point outResult to t h i s array now
}
else i f ( lArg2 . length ==1){
f o r ( i n t i =outResult . length;−−i >=0;){
t r y {
outResult [ i ]+= lArg2 [ 0 ] ;
}
catch ( Exception e ) {
outResult [ i ]= Double .NaN;
}
}
}
}
break ;
}
case 1 : {
outResult=getArgument (0 , callStack , tree , XData ) ;
i f ( t h i s .name. compareTo ( " sin " ) = = 0 ) / / sin
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] = ( Math . sin ( outResult [ i ] ) ) ;
else i f ( t h i s .name. compareTo ( " cos " ) = = 0 ) / / cos
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] = ( Math . cos ( outResult [ i ] ) ) ;
else i f ( t h i s .name. compareTo ( " a d i l l o g ")==0)
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] = ( Math . log ( outResult [ i ] ) ) ;
else i f ( t h i s .name. compareTo ( " adillog10 ")==0)
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES XI
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] = ( Math . log10 ( outResult [ i ] ) ) ;
else i f ( t h i s .name. compareTo ( " a d i l s q r t ")==0){
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ]=Math . sqrt ( outResult [ i ] ) ;
}
break ;
}
default : { / / A terminal has been reached
i f ( ! t h i s .name. startsWith ( "X " ) ) {
/ / double tmpVal=Double . valueOf ( t h i s .name ) . doubleValue ( ) ;
/ / f o r ( i n t i =0; i <outResult . length ; i ++)
/ / outResult [ i ]= tmpVal ;
outResult=new double [ 1 ] ;
outResult [0]= Double . valueOf ( t h i s .name ) . doubleValue ( ) ;
}
else i f ( t h i s .name. substring ( 0 , 1 ) . compareTo ( "X")==0){
/ / The f o l l o w i n g l i n e would automatically assign the p e r t i n e n t entry in the pattern
i n t tmp=Integer . valueOf ( t h i s .name. substring ( 1 ) ) . intValue () −1;
outResult=new double [ XData . length ] ;
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] = ( double ) XData [ i ] [ tmp ] ; / / Return the whole array of the Input data ,
}
break ;
}
}
/∗ PuppysMain . noCacheHits ++;
i f ( evaluation== n u l l ) { / / i . e . i f the entry i s not present
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES XII
i f ( t h i s . g e t A r i t y ()==0){
lContext .mCache. put ( key . to St ri ng ( ) , new Evaluation ( ) ) ; / / c a l l the default construc
/ / but the actual evaluation i s done here . Aimed at saving extra array−copying
}
else { / / the tree has c h i l d ( ren ) .
i n t lIndex = callStack . get ( callStack . size () −1). intValue ( ) + 1 ; / / index of the next
boolean hasCached=true ; / / that the subtree i s cahched
f o r ( i n t i =0; i < t h i s . a r i t y ; i ++){
StringBuilder subTree=new StringBuilder ( tree . size ()− lIndex ) ;
tree . writeTree2 ( lIndex , subTree ) ;
String s t r =new String ( subTree . t oS tr in g ( ) ) ;
Evaluation evaluation2=lContext .mCache. get ( s t r ) ;
t r y {
i f ( evaluation2 != n u l l )
i f ( evaluation2 . cachedFlag== false )
hasCached= false ;
/ / System . out . p r i n t l n ( " Alarming " ) ;
}
catch ( Exception e ) {
hasCached= false ;
System . out . p r i n t l n ( "Do I reach here? "+e ) ;
/ / System . e x i t ( 0 ) ;
}
lIndex+=tree . get ( lIndex ) . getSubTreeSize ( ) ;
}
i f ( hasCached ) { / / That i s hasCached survived through a l l the subChildren
lContext .mCache. put (new String ( key . to St ri ng ( ) ) , new Evaluation ( outResult , fals
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES XIII
}
}
} ∗/
/ / return outResult ;
/ / }
}
/∗
∗ 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 in 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 S
PRIMITIVES XIV
public class Ephemeral extends P r i m i t i v e {
/∗∗ Creates a new instance of Ephemeral ∗/
public Ephemeral ( ) {
}
/∗∗
∗  param inNumberArguments Number of arguments 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 .
∗/
public Ephemeral ( String inName , int a r i t y ) {
super ( inName , a r i t y ) ;
}
/∗∗
∗  b r i e f Copy−construct 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 String ( 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 reference on the actual p r i m i t i v e .
Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES XV
∗  param object of Random
∗  return P r i m i t i v e handle to t h i s pointer .
∗/
public P r i m i t i v e giveReference (Random dice )
{
this .name=java . lang . Double . toS tr in g ( dice . nextDouble ()∗12 −6);
return this ;
}
/∗∗
∗Function i s used to execute t h i s p r i m i t i v e
∗outResult
∗callStack
∗tree to which t h i s p r i m i t i v e belongs
∗XData
∗patternNumber , that comes in from evaluateTree in Tree
∗/
public double [ ] execute ( ArrayList <Integer > callStack , Tree tree , float [ ] [ ] XData ) {
/ / c a l l the cache here?
/∗ StringBuilder key=new StringBuilder ( tree . size ()− callStack . get ( callStack . size () −1). intVal
tree . writeTree2 ( callStack . get ( callStack . size () −1). intValue ( ) , key ) ;
Evaluation evaluation=PuppysMain . lContext .mCache. get ( key . to St rin g ( ) ) ;
i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . only non−terminals are evaluated here
i f ( evaluation != n u l l ) {
/ / double [ ] evaled=lContext .mCache. get ( key . t oSt ri ng ( ) ) . evaluation ;
/ / f o r ( i n t i =0; i <evaled . length ; i ++)
/ / outResult [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lContext .mCache. get ( key . t oS tr in g ( ) ) . cachedFlag=true ; / / the entry i s now
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle
Java Beagle

More Related Content

What's hot

JPoint 2016 - Валеев Тагир - Странности Stream API
JPoint 2016 - Валеев Тагир - Странности Stream APIJPoint 2016 - Валеев Тагир - Странности Stream API
JPoint 2016 - Валеев Тагир - Странности Stream API
tvaleev
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
archana singh
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)riue
 
Java 8 Puzzlers [as presented at OSCON 2016]
Java 8 Puzzlers [as presented at  OSCON 2016]Java 8 Puzzlers [as presented at  OSCON 2016]
Java 8 Puzzlers [as presented at OSCON 2016]
Baruch Sadogursky
 
Python profiling
Python profilingPython profiling
Python profiling
dreampuf
 
Understanding Lemon Generated Parser
Understanding Lemon Generated ParserUnderstanding Lemon Generated Parser
Understanding Lemon Generated Parser
vivekanandan r
 
Metaprogramming in julia
Metaprogramming in juliaMetaprogramming in julia
Metaprogramming in julia
岳華 杜
 
20170415 當julia遇上資料科學
20170415 當julia遇上資料科學20170415 當julia遇上資料科學
20170415 當julia遇上資料科學
岳華 杜
 
Programming with ZooKeeper - A basic tutorial
Programming with ZooKeeper - A basic tutorialProgramming with ZooKeeper - A basic tutorial
Programming with ZooKeeper - A basic tutorial
Jeff Smith
 
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 AutumnGoptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Masashi Shibata
 
Data structures lab
Data structures labData structures lab
Data structures lab
Ragu Ram
 
Evolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and Effective
Naresha K
 
Introduction to julia
Introduction to juliaIntroduction to julia
Introduction to julia
岳華 杜
 
The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184
Mahmoud Samir Fayed
 
The Language for future-julia
The Language for future-juliaThe Language for future-julia
The Language for future-julia
岳華 杜
 
From NumPy to PyTorch
From NumPy to PyTorchFrom NumPy to PyTorch
From NumPy to PyTorch
Mike Ruberry
 
Clips basics how to make expert system in clips | facts adding | rules makin...
Clips basics  how to make expert system in clips | facts adding | rules makin...Clips basics  how to make expert system in clips | facts adding | rules makin...
Clips basics how to make expert system in clips | facts adding | rules makin...
NaumanMalik30
 
merged_document_3
merged_document_3merged_document_3
merged_document_3tori hoff
 
Python opcodes
Python opcodesPython opcodes
Python opcodes
alexgolec
 

What's hot (19)

JPoint 2016 - Валеев Тагир - Странности Stream API
JPoint 2016 - Валеев Тагир - Странности Stream APIJPoint 2016 - Валеев Тагир - Странности Stream API
JPoint 2016 - Валеев Тагир - Странности Stream API
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 
Java 8 Puzzlers [as presented at OSCON 2016]
Java 8 Puzzlers [as presented at  OSCON 2016]Java 8 Puzzlers [as presented at  OSCON 2016]
Java 8 Puzzlers [as presented at OSCON 2016]
 
Python profiling
Python profilingPython profiling
Python profiling
 
Understanding Lemon Generated Parser
Understanding Lemon Generated ParserUnderstanding Lemon Generated Parser
Understanding Lemon Generated Parser
 
Metaprogramming in julia
Metaprogramming in juliaMetaprogramming in julia
Metaprogramming in julia
 
20170415 當julia遇上資料科學
20170415 當julia遇上資料科學20170415 當julia遇上資料科學
20170415 當julia遇上資料科學
 
Programming with ZooKeeper - A basic tutorial
Programming with ZooKeeper - A basic tutorialProgramming with ZooKeeper - A basic tutorial
Programming with ZooKeeper - A basic tutorial
 
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 AutumnGoptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
 
Data structures lab
Data structures labData structures lab
Data structures lab
 
Evolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and Effective
 
Introduction to julia
Introduction to juliaIntroduction to julia
Introduction to julia
 
The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184
 
The Language for future-julia
The Language for future-juliaThe Language for future-julia
The Language for future-julia
 
From NumPy to PyTorch
From NumPy to PyTorchFrom NumPy to PyTorch
From NumPy to PyTorch
 
Clips basics how to make expert system in clips | facts adding | rules makin...
Clips basics  how to make expert system in clips | facts adding | rules makin...Clips basics  how to make expert system in clips | facts adding | rules makin...
Clips basics how to make expert system in clips | facts adding | rules makin...
 
merged_document_3
merged_document_3merged_document_3
merged_document_3
 
Python opcodes
Python opcodesPython opcodes
Python opcodes
 

Viewers also liked

Formatting Tips
Formatting TipsFormatting Tips
Formatting Tips
adil raja
 
Subclasses and Subtypes
Subclasses and SubtypesSubclasses and Subtypes
Subclasses and Subtypes
adil raja
 
Inheritance and Substitution
Inheritance and SubstitutionInheritance and Substitution
Inheritance and Substitution
adil raja
 
An Evolutionary Approach to Speech Quality Estimation
An Evolutionary Approach to Speech Quality EstimationAn Evolutionary Approach to Speech Quality Estimation
An Evolutionary Approach to Speech Quality Estimation
adil raja
 
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
adil raja
 
Generics
GenericsGenerics
Generics
adil raja
 
Object Interconnections
Object InterconnectionsObject Interconnections
Object Interconnections
adil raja
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
adil raja
 
A Course Outline About Computer Networks
A Course Outline About Computer NetworksA Course Outline About Computer Networks
A Course Outline About Computer Networks
adil raja
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Sockets
adil raja
 
ULMAN-GUI
ULMAN-GUIULMAN-GUI
ULMAN-GUI
adil raja
 
Messages, Instances and Initialization
Messages, Instances and InitializationMessages, Instances and Initialization
Messages, Instances and Initialization
adil raja
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistan
adil raja
 
Software Frameworks
Software FrameworksSoftware Frameworks
Software Frameworks
adil raja
 
The Product
The ProductThe Product
The Product
adil raja
 
Implementation
ImplementationImplementation
Implementation
adil raja
 
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
adil raja
 
IXP SDK
IXP SDKIXP SDK
IXP SDK
adil raja
 
Research Methods: Objectives and Contents
Research Methods: Objectives and ContentsResearch Methods: Objectives and Contents
Research Methods: Objectives and Contents
adil raja
 
Object-Oriented Design
Object-Oriented DesignObject-Oriented Design
Object-Oriented Design
adil raja
 

Viewers also liked (20)

Formatting Tips
Formatting TipsFormatting Tips
Formatting Tips
 
Subclasses and Subtypes
Subclasses and SubtypesSubclasses and Subtypes
Subclasses and Subtypes
 
Inheritance and Substitution
Inheritance and SubstitutionInheritance and Substitution
Inheritance and Substitution
 
An Evolutionary Approach to Speech Quality Estimation
An Evolutionary Approach to Speech Quality EstimationAn Evolutionary Approach to Speech Quality Estimation
An Evolutionary Approach to Speech Quality Estimation
 
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
 
Generics
GenericsGenerics
Generics
 
Object Interconnections
Object InterconnectionsObject Interconnections
Object Interconnections
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
A Course Outline About Computer Networks
A Course Outline About Computer NetworksA Course Outline About Computer Networks
A Course Outline About Computer Networks
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Sockets
 
ULMAN-GUI
ULMAN-GUIULMAN-GUI
ULMAN-GUI
 
Messages, Instances and Initialization
Messages, Instances and InitializationMessages, Instances and Initialization
Messages, Instances and Initialization
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistan
 
Software Frameworks
Software FrameworksSoftware Frameworks
Software Frameworks
 
The Product
The ProductThe Product
The Product
 
Implementation
ImplementationImplementation
Implementation
 
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
A Methodology for Deriving VoIP Equipment Impairment Factors for a Mixed NB/W...
 
IXP SDK
IXP SDKIXP SDK
IXP SDK
 
Research Methods: Objectives and Contents
Research Methods: Objectives and ContentsResearch Methods: Objectives and Contents
Research Methods: Objectives and Contents
 
Object-Oriented Design
Object-Oriented DesignObject-Oriented Design
Object-Oriented Design
 

Similar to Java Beagle

An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
Abner Chih Yi Huang
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
BTI360
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
fantasiatheoutofthef
 
Evolutionary Nursery
Evolutionary NurseryEvolutionary Nursery
Evolutionary Nursery
adil raja
 
Empathic Programming - How to write comprehensible code
Empathic Programming - How to write comprehensible codeEmpathic Programming - How to write comprehensible code
Empathic Programming - How to write comprehensible code
Mario Gleichmann
 
Tips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET ApplicationTips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET Application
Joni
 
Javascript: The Important Bits
Javascript: The Important BitsJavascript: The Important Bits
Javascript: The Important Bits
Zumba Fitness - Technology Team
 
Java 7 JUG Summer Camp
Java 7 JUG Summer CampJava 7 JUG Summer Camp
Java 7 JUG Summer Camp
julien.ponge
 
Fee managment system
Fee managment systemFee managment system
Fee managment systemfairy9912
 
Java 7 at SoftShake 2011
Java 7 at SoftShake 2011Java 7 at SoftShake 2011
Java 7 at SoftShake 2011julien.ponge
 
Alastair Butler - 2015 - Round trips with meaning stopovers
Alastair Butler - 2015 - Round trips with meaning stopoversAlastair Butler - 2015 - Round trips with meaning stopovers
Alastair Butler - 2015 - Round trips with meaning stopovers
Association for Computational Linguistics
 
Beauty and the beast - Haskell on JVM
Beauty and the beast  - Haskell on JVMBeauty and the beast  - Haskell on JVM
Beauty and the beast - Haskell on JVM
Jarek Ratajski
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
charanjit1717
 
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coinsoft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
soft-shake.ch
 

Similar to Java Beagle (20)

An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
 
Linq introduction
Linq introductionLinq introduction
Linq introduction
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
Oop 1
Oop 1Oop 1
Oop 1
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
Evolutionary Nursery
Evolutionary NurseryEvolutionary Nursery
Evolutionary Nursery
 
Empathic Programming - How to write comprehensible code
Empathic Programming - How to write comprehensible codeEmpathic Programming - How to write comprehensible code
Empathic Programming - How to write comprehensible code
 
WOTC_Import
WOTC_ImportWOTC_Import
WOTC_Import
 
Inheritance
InheritanceInheritance
Inheritance
 
Tips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET ApplicationTips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET Application
 
Javascript: The Important Bits
Javascript: The Important BitsJavascript: The Important Bits
Javascript: The Important Bits
 
Java 7 JUG Summer Camp
Java 7 JUG Summer CampJava 7 JUG Summer Camp
Java 7 JUG Summer Camp
 
JAVA SE 7
JAVA SE 7JAVA SE 7
JAVA SE 7
 
Fee managment system
Fee managment systemFee managment system
Fee managment system
 
Java 7 at SoftShake 2011
Java 7 at SoftShake 2011Java 7 at SoftShake 2011
Java 7 at SoftShake 2011
 
Alastair Butler - 2015 - Round trips with meaning stopovers
Alastair Butler - 2015 - Round trips with meaning stopoversAlastair Butler - 2015 - Round trips with meaning stopovers
Alastair Butler - 2015 - Round trips with meaning stopovers
 
Beauty and the beast - Haskell on JVM
Beauty and the beast  - Haskell on JVMBeauty and the beast  - Haskell on JVM
Beauty and the beast - Haskell on JVM
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
 
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coinsoft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
 
Pdr ppt
Pdr pptPdr ppt
Pdr ppt
 

More from adil raja

ANNs.pdf
ANNs.pdfANNs.pdf
ANNs.pdf
adil raja
 
A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specification
adil raja
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
adil raja
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystified
adil raja
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)
adil raja
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Research
adil raja
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocol
adil raja
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Execution
adil raja
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
adil raja
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
adil raja
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
adil raja
 
VoIP
VoIPVoIP
VoIP
adil raja
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specifications
adil raja
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
adil raja
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
adil raja
 
Modeling the Effect of packet Loss on Speech Quality: GP Based Symbolic Regre...
Modeling the Effect of packet Loss on Speech Quality: GP Based Symbolic Regre...Modeling the Effect of packet Loss on Speech Quality: GP Based Symbolic Regre...
Modeling the Effect of packet Loss on Speech Quality: GP Based Symbolic Regre...
adil raja
 
Modelling the Effect of Packet Loss on Speech Quality
Modelling the Effect of Packet Loss on Speech QualityModelling the Effect of Packet Loss on Speech Quality
Modelling the Effect of Packet Loss on Speech Quality
adil raja
 
A Random Presentation
A Random PresentationA Random Presentation
A Random Presentation
adil raja
 
Packet Processing Application
Packet Processing ApplicationPacket Processing Application
Packet Processing Application
adil raja
 

More from adil raja (20)

ANNs.pdf
ANNs.pdfANNs.pdf
ANNs.pdf
 
A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specification
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystified
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Research
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocol
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Execution
 
Thesis
ThesisThesis
Thesis
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
 
VoIP
VoIPVoIP
VoIP
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specifications
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 
Modeling the Effect of packet Loss on Speech Quality: GP Based Symbolic Regre...
Modeling the Effect of packet Loss on Speech Quality: GP Based Symbolic Regre...Modeling the Effect of packet Loss on Speech Quality: GP Based Symbolic Regre...
Modeling the Effect of packet Loss on Speech Quality: GP Based Symbolic Regre...
 
Modelling the Effect of Packet Loss on Speech Quality
Modelling the Effect of Packet Loss on Speech QualityModelling the Effect of Packet Loss on Speech Quality
Modelling the Effect of Packet Loss on Speech Quality
 
A Random Presentation
A Random PresentationA Random Presentation
A Random Presentation
 
Packet Processing Application
Packet Processing ApplicationPacket Processing Application
Packet Processing Application
 

Recently uploaded

Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 

Recently uploaded (20)

Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 

Java Beagle

  • 1. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S JAVA BEAGLE THE SOURCE CODE Muhammad Adil Raja Roaming Researchers, Inc. cbna April 27, 2015
  • 2. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S OUTLINE I 1 INTRODUCTION 2 PUPPY’S MAIN 3 PUPPY 4 NODE 5 EVALUATION 6 TREE 7 STATISTICS 8 STATISTICS IO 9 CONTEXT 10 PRIMITIVES 11 FUNCTIONS 12 SUMMARY 13 REFERENCES
  • 3. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S INTRODUCTION I Java Beagle is an implementation of a simple genetic programming (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 types of problems such as classification. It is based on Beagle Puppy software for genetic programming. The source code has many Java files. 1 PuppyMain.java: The main entry point to the program that runs the GP algorithmd. 2 Puppy.java: This class implements the algorithms that are necessary to run the GP, such as crossover and mutation.
  • 4. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S INTRODUCTION II 3 Node.java: The data-type Node specifies the implementation of the GP genotype. 4 Evaluation.java: The routines for evaluation of a node in GP population. 5 Tree.java: builds and Keeps the syntax tree of a GP program. In what follows you will find the source code of various components of Java Beagle.
  • 5. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S 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 in 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 . Functions . ∗ ; import java . u t i l . ∗ ; import java . lang . ∗ ; import java . io . ∗ ; public class PuppysMain { public static float [ ] [ ] X; public static float [ ] Target ;
  • 6. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY’S MAIN II public static Context lContext ; public static int cacheHits , noCacheHits ; /∗∗ Creates a new instance of PuppysMain ∗/ public PuppysMain ( ) { } /∗∗ ∗ @param args the command l i n e arguments ∗/ public static void main ( String [ ] args ) { / / TODO code a p p l i c a t i o n l o g i c here new Context ( ) ; Context . i n s e r t (new Plus ( " plus " , 2 ) ) ; Context . i n s e r t (new Minus ( " minus " , 2 ) ) ; lContext . i n s e r t (new MyDivide ( " mydivide " , 2 ) ) ; lContext . i n s e r t (new AdilPower ( " adilpower " , 2 ) ) ; Context . i n s e r t (new Times ( " times " , 2 ) ) ; lContext . i n s e r t (new Sin ( " sin " , 1 ) ) ; / / sin lContext . i n s e r t (new Cos( " cos " , 1 ) ) ; / / cos lContext . i n s e r t (new AdilLog ( " a d i l l o g " , 1 ) ) ; / / ln lContext . i n s e r t (new AdilLog10 ( " adillog10 " , 1 ) ) ; / / log−10 / / lContext . 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 ) ) ;
  • 7. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S 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 ) ) ;
  • 8. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S 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 . mutationProba =0.1F ; Context . inInitGrowProba =0.5F ; Context . xoverNodeDistribProb =0.5F ; Context . maxTreeDepth=17; Context . initTreeDepthMax =6;
  • 9. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY’S MAIN V Context . initTreeDepthMin =2; Context . tournamentSize =7; Context . trainDataFileX=new String ( " . / OnP563 / p563−input−params−cond−n−exp−wise . t x t " ) ; / / Context . trainDataFileX =" a d i l . t x t " ; Context . trainDataFileY=new String ( " . / OnP563 / Subj−MOS−1328−cond−wise−sorted . t x t " ) ; Context . testDataFileX=new String ( " . / OnP563 / combined−p563−features−Nortel−czech . t x t " ) ; Context . testDataFileY=new String ( " . / OnP563 / combined−ACR−MOS−Nortel−czech . t x t " ) ; Context . useTestData=true ; Context . length_of_run =100; int numRuns=2; int 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 ( Integer . t oSt ri ng ( k +1)) , 0 ) ) ; / / } ArrayList < S t a t i s t i c s > s t a t i s t i c s =new ArrayList ( ) ; /∗ ∗Lets do an example problem here ∗/ / / Context . XData=new f l o a t [ 1 0 0 0 ] [ 2 ] ; / / Context . Target=new double [1000]; 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 ] = dice . nextFloat ()∗10−5;
  • 10. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY’S MAIN VI / / Context . XData [ i ] [ 1 ] = dice . nextFloat ()∗10−5; / / Context . Target [ i ]=Math . pow( Context . XData [ i ] [ 0 ] , 4 ) + Context . XData [ i ] [ 0 ] ∗ Context . XD / / Context . Target [ i ] = ( double ) ( Context . XData [ i ] [ 0 ] ∗ ( Context . XData [ i ] [ 0 ] ∗ ( Context . XD / / } Context . readDataPatterns (1328 , 44 , true ) ; / / System . e x i t ( 0 ) ; i f ( Context . useTestData ) / / i f testdata i s used as well Context . readDataPatterns (276 , 44 , false ) ; int lPopSize =1000; Puppy puppy=new Puppy ( ) ; cacheHits =0; for ( int i =0; i <numRuns ; i ++){ / / the run s t a r t s here S t a t i s t i c s thisRunStats=new S t a t i s t i c s ( ) ; thisRunStats . t r a i n F i t H i s t o r y =new double [ Context . length_of_run ] ; i f ( Context . useTestData ) thisRunStats . t e s t F i t H i s t o r y =new double [ Context . length_of_run ] ; ArrayList <Tree> lPopulation=new ArrayList ( Context . popSize ) ; puppy . i n i t i a l i z e P o p u l a t i o n ( lPopulation ) ; puppy . evaluatePopulation ( lPopulation ) ; int j =0; f i t n e s s =500;
  • 11. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY’S MAIN VII Tree bestTree=null ; while ( j <Context . length_of_run ) { / / run s t a r t s from here System . out . p r i n t l n ( ) ; bestTree=puppy . applyAllEvolutionarySteps ( lPopulation ) ; double tmpFitness =200; tmpFitness=lPopulation . get ( 0 ) . getTrainingFitness ( ) ; double testFitness =Double .MAX_VALUE; i f ( Context . useTestData ) { testFitness =lPopulation . get ( 0 ) . evaluateTreeForTestData ( ) ; } thisRunStats . t r a i n F i t H i s t o r y [ j ]= tmpFitness ; / / thisRunStats . t e s t F i t H i s t o r y [ j ]= testFitness ; System . out . p r i n t l n ( " CurrentBest "+tmpFitness+" TestFitness : "+ testFitness +" Generation System . out . p r i n t l n ( " Xover Prob : "+Context . xoverProba+" Mutation Prob : "+Context . mutati StringBuilder s t b l d r =new StringBuilder ( ) ; lPopulation . get ( 0 ) . writeTree (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 generation ends here bestTree . setSExpression ( ) ; / / set the s−expression now, s h a l l be used l a t t e r thisRunStats . 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 ( thisRunStats ) ; } / / a run ends here
  • 12. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY’S MAIN VIII try { S t a t i s t i c s I O . writeStatsToDisk ( s t a t i s t i c s , " SimulationResults . expr " ) ; } catch ( java . io . IOException e ) { System . out . p r i n t l n ( e ) ; } ArrayList < S t a t i s t i c s > stato=null ; try { stato =( ArrayList < S t a t i s t i c s >) S t a t i s t i c s I O . readStatsFromDisk ( " SimulationResults . ex } catch ( java . io . IOException 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 ( int i =0; i <stato . size ( ) ; i ++){ System . out . p r i n t l n ( stato . get ( i ) . b e s t I n d i v i d u a l . getTrainingFitness ( ) ) ; } } }
  • 13. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S 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 in 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 . bcel . 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 int parentPopSize ; private Random dice ; private ArrayList <Tree> childPop ;
  • 14. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY II private ArrayList <Integer > lMateVector ; public double [ ] [ ] trainingDataX ; public double [ ] trainingDataY ; private double bestFitness ; private int dynamicLevel ; /∗∗ Creates a new instance of Puppy ∗/ public Puppy ( ) { dice=new Random ( ) ; childPop=new ArrayList ( ) ; lMateVector=new ArrayList ( ) ; this . dynamicLevel =6; / / to be changed this . bestFitness =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 half−and−h a l f a population of GP trees . ∗ param ioPopulation Population to i n i t i a l i z e . ∗ param inInitGrowProba 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 , in opposition to f u l l . ∗ param inMinDepth Minimum i n i t i a l i z a t i o n tree depth allowed . ∗ param inMaxDepth Maximum i n i t i a l i z a t i o n tree depth allowed . ∗ ingroup Puppy ∗/ public ArrayList <Tree> i n i t i a l i z e P o p u l a t i o n ( ArrayList <Tree> ioPopulation ) {
  • 15. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY III this . parentPopSize=Context . popSize ; int inMinDepth=Context . initTreeDepthMin ; int inMaxDepth=Context . initTreeDepthMax ; float inInitGrowProba=Context . inInitGrowProba ; assert ( inMinDepth <= inMaxDepth ) ; / / ArrayList <Tree> ioPopulation=new ArrayList ( ) ; this . dice=new Random ( ) ; for ( int i =0; i <Context . popSize ; ++ i ) { ioPopulation . add ( i ,new Tree ( ) ) ; ioPopulation . get ( i ) . clear ( ) ; int l I n i t D e p t h = dice . nextInt ( inMaxDepth−inMinDepth )+ inMinDepth +1; Tree tmpTr=new Tree ( ) ; i f ( dice . nextFloat ( ) >= inInitGrowProba ) { i n i t i a l i z e T r e e F u l l ( ioPopulation . get ( i ) , l I n i t D e p t h ) ; / / ioPopulation . add ( i , new Tree ( tmpTr ) ) ; ioPopulation . get ( i ) . setValid ( false ) ; ioPopulation . get ( i ) . setEvaluated ( false ) ; / / tmpTr . clear ( ) ; } else { initializeTreeGrow ( ioPopulation . get ( i ) , inMinDepth , l I n i t D e p t h ) ; / / ioPopulation . add ( i , new Tree ( tmpTr ) ) ; ioPopulation . get ( i ) . setValid ( false ) ; ioPopulation . get ( i ) . setEvaluated ( false ) ; / / tmpTr . clear ( ) ;
  • 16. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY IV } } evaluatePopulation ( ioPopulation ) ; return ioPopulation ; } /∗∗ ∗ I n i t i a l i z e a GP tree with f u l l approach . ∗ param ioTree Tree to i n i t i a l i z e . ∗ param ioContext Evolutionary context . ∗ param inDepth Actual depth to go in i n i t i a l i z a t i o n . ∗ return Generated tree size . ∗ ingroup Puppy ∗ ∗ I f the tree i s not empty , the i n i t i a l i z a t i o n append the generated sub−tree to the actual t ∗/ public int i n i t i a l i z e T r e e F u l l ( Tree ioTree , int inDepth ) { i f ( inDepth >= 1){ / / assert was here i f ( inDepth == 1) { assert ( Context . mTerminalSet . size ( ) > 0 ) ; P r i m i t i v e lTerminal = Context . mTerminalSet . get ( dice . nextInt ( Context . mTerminalSet . size ( ) ) ) ; ioTree . add (new Node( lTerminal . giveReference ( dice ) , 1 ) ) ; return 1; }
  • 17. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY V assert ( Context . mFunctionSet . size ( ) > 0 ) ; P r i m i t i v e lFunction =Context . mFunctionSet . get ( dice . nextInt ( Context . mFunctionSet . size ( ) ) ) ; int lNodeIndex = ioTree . size ( ) ; ioTree . add (new Node( lFunction . giveReference ( dice ) , 0 ) ) ; int lNbArgs = ioTree . get ( lNodeIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ; int lTreeSize = 1; / / Tree tmpT ; for ( int 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); / / iTree=tmpT ; } ioTree . get ( lNodeIndex ) . setSubTreeSize ( lTreeSize ) ; return lTreeSize ; } else return 0; } /∗∗ ∗ I n i t i a l i z e a GP tree with grow approach . ∗ param ioTree Tree to i n i t i a l i z e . ∗ param ioContext Evolutionary context . ∗ param inMinDepth Minimal depth to go in i n i t i a l i z a t i o n . ∗ param inMaxDepth Maximal depth to go in i n i t i a l i z a t i o n . ∗ return Generated tree size . ∗ ingroup Puppy ∗ ∗ I f the tree i s not empty , the i n i t i a l i z a t i o n append the generated sub−tree to the actual t
  • 18. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY VI ∗/ public int initializeTreeGrow ( Tree ioTree , int inMinDepth , int inMaxDepth ) { i f ( inMinDepth >= 1 && inMinDepth <= inMaxDepth ) { P r i m i t i v e l P r i m i t = null ; i f ( inMinDepth > 1) { assert ( Context . mFunctionSet . size ( ) > 0 ) ; l P r i m i t = Context . mFunctionSet . get ( dice . nextInt ( Context . mFunctionSet . size ( ) ) ) ; } else i f ( inMaxDepth == 1) { assert ( Context . mTerminalSet . size ( ) > 0 ) ; l P r i m i t = Context . mTerminalSet . get ( dice . nextInt ( Context . mTerminalSet . size ( ) ) ) ; } else { int lIndexSel = dice . nextInt ( Context . mFunctionSet . size ( ) + Context . mTerminalSet . size ( ) ) ; i f ( lIndexSel >= Context . mFunctionSet . size ( ) ) { l P r i m i t = Context . mTerminalSet . get ( lIndexSel − Context . mFunctionSet . size ( ) ) ; } else l P r i m i t = Context . mFunctionSet . get ( lIndexSel ) ; } / / i n t lNodeIndex = tmpArr . size ( ) ; int lNodeIndex = ioTree . size ( ) ; ioTree . add (new Node( l P r i m i t . giveReference ( dice ) , 1 ) ) ; int lTreeSize = 1;
  • 19. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY VII int lMinDepth = ( inMinDepth > 1) ? ( inMinDepth−1) : 1; int 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 try { for ( int i =0; i <lNbArgs ; i ++) { lTreeSize+= initializeTreeGrow ( ioTree , lMinDepth , inMaxDepth −1); / / ioTree=tmpT ; } } catch ( java . lang . StackOverflowError e ) { System . out . p r i n t l n ( e+" in echange subtrees : "+inMinDepth+" "+inMaxDepth ) ; System . e x i t ( 0 ) ; } ioTree . get ( lNodeIndex ) . setSubTreeSize ( lTreeSize ) ; return lTreeSize ; } else { System . out . p r i n t l n ( "The problem i s here " ) ; return 0; } } /∗∗ ∗ Apply tournament selection to a population of trees . ∗ param ioPopulation Population to apply selection on . ∗ param ioContext Evolutionary context .
  • 20. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY VIII ∗ param inNumberParticipants Number of p a r t i c i p a n t s to each tournament selection . ∗ ingroup Puppy ∗This function has to be modified . This only s u i t s the replacement c r i t e r i a but not an e l i t i s ∗The routine also implements LPP by Sean Luke ∗/ public ArrayList <Tree> applySelectionTournament ( ArrayList <Tree> ioPopulation ) { i f ( ioPopulation . size ( ) == 0) return null ; int inNumberParticipants=Context . tournamentSize ; ArrayList <Tree> childPop=new ArrayList ( ioPopulation . size ( ) ) ; int [ ] l I n d i c e s =new int [ ioPopulation . size ( ) ] ; for ( int i =0; i <ioPopulation . size ( ) ; i ++) l I n d i c e s [ i ]=0; for ( int i =0; i <ioPopulation . size ( ) ; i ++) { int lChoosenIndividual = dice . nextInt ( ioPopulation . size ( ) ) ; for ( int j =1; j <inNumberParticipants ; ++ j ) { int l T r i e d I n d i v i d u a l = dice . nextInt ( ioPopulation . size ( ) ) ; / / the f o l l o w i n g l i n e implements i f ( ioPopulation . get ( l T r i e d I n d i v i d u a l ) . compareRank ( ioPopulation . get ( lChoosenIndividual ) ) lChoosenIndividual = l T r i e d I n d i v i d u a l ; } } ++ l I n d i c e s [ lChoosenIndividual ] ; } int lNextEmpty = 0; int l N e x t F i l l e d = 0;
  • 21. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY IX while ( ( l N e x t F i l l e d < ioPopulation . size ( ) ) && ( 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 < ioPopulation . size ( ) ) { 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 ; childPop . add ( ioPopulation . get ( l N e x t F i l l e d ) ) ; / / remove ( lNextEmpty ) ; / / ioPopulation . add ( ioPopulation . 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 < ioPopulation . size ( ) ) && ( 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 ( childPop . size () < ioPopulation . size ( ) ) childPop . add ( ioPopulation . get ( dice . nextInt ( ioPopulation . size ( ) ) ) ) ; / / f i l l the remaining return childPop ; } /∗∗ ∗ Apply tournament selection to a population of trees . ∗ param ioPopulation Population to apply selection on . ∗ param ioContext Evolutionary context . ∗ param inNumberParticipants Number of p a r t i c i p a n t s to each tournament selection . ∗ ingroup Puppy ∗This function has to be modified . This only s u i t s the replacement c r i t e r i a but not an e l i t i s ∗The routine also implements LPP by Sean Luke ∗/ public ArrayList <Tree> applySelectionTournament1 ( ArrayList <Tree> ioPopulation ) {
  • 22. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY X i f ( ioPopulation . size ( ) == 0) return null ; ArrayList <Tree> childPop=new ArrayList ( ioPopulation . size ( ) ) ; int inNumberParticipants=Context . tournamentSize ; / / choose the f i r s t candidate and store in the c h i l d population while ( childPop . size () < ioPopulation . size ( ) ) { int j j =0; int lChosenCand1=0 , lChosenCand2=0; int tmpCand1=ioPopulation . size ()+1 , tmpCand2=ioPopulation . size ( ) + 1 ; while ( j j <inNumberParticipants ) { lChosenCand1=dice . nextInt ( ioPopulation . size ( ) ) ; i f ( tmpCand1<ioPopulation . size ( ) ) { i f ( ioPopulation . get ( lChosenCand1 ) . compareRank ( ioPopulation . get ( tmpCand1 ) ) ) { / / | | ( ioP tmpCand1=lChosenCand1 ; / / choose an i n d i v i d u a l , } } else tmpCand1=lChosenCand1 ; / / choose t h i s j j ++; } childPop . add (new Tree ( ioPopulation . get ( tmpCand1 ) ) ) ; / / Add t h i s to the c h i l d population / / choose the second candidate now and store in the c h i l d population j j =0; while ( j j <inNumberParticipants ) { lChosenCand2=dice . nextInt ( ioPopulation . size ( ) ) ; i f ( tmpCand2<=ioPopulation . size ( ) ) { / / implements Gustafson and LPP i f ( ! ioPopulation . get ( tmpCand1 ) . compareTrees ( ioPopulation . get ( tmpCand2 ) ) && ioPopulation . get ( lChosenCand2 ) . compareRank ( ioPopulation . get ( tmpCand2 ) )
  • 23. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XI | | ( ioPopulation . get ( lChosenCand2 ) . compareTrees ( ioPopulation . get ( tmpCand2 ) ) && ioPopulation . get ( lChosenCand2 ) . size () < ioPopulation . get ( tmpCand2 ) . size ( ) ) ) { tmpCand2=lChosenCand2 ; / / choose an i n d i v i d u a l , } } else tmpCand2=lChosenCand2 ; / / choose t h i s j j ++; } childPop . add (new Tree ( ioPopulation . get ( tmpCand2 ) ) ) ; / / Add t h i s to the c h i l d population } for ( int i =0; i <ioPopulation . size ( ) ; i ++){ childPop . get ( i ) . usedMutationStd=false ; childPop . get ( i ) . usedMutationSwap=false ; childPop . get ( i ) . usedXover=false ; } return childPop ; } / / what i s t h i s function doing? /∗∗ ∗ Apply sub−tree crossover operation on a population of GP trees . ∗ param ioPopulation Population to apply crossover on . ∗ param ioContext Evolutionary context . ∗ 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 modified by crossover . ∗ param inDistribProba P r o b a b i l i t y that a crossover exchange two sub−trees of non−terminal
  • 24. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XII ∗ param inMaxTreeDepth Maximum tree depth allowed . ∗ ingroup Puppy ∗/ public void applyCrossover ( ArrayList <Tree> ioPopulation ) { int inMaxTreeDepth=Context . maxTreeDepth ; float inDistribProba=Context . inInitGrowProba ; / / not used here but s t i l l float inMatingProba=Context . xoverProba ; i f ( ( ioPopulation . size ( ) % 2) != 0) ioPopulation . remove ( lMateVector . size () −1); / / System . out . p r i n t l n ( "MATING VECTOR SIZE= "+ lMateVector . size ( ) ) ; / / f o r ( unsigned i n t j =0; j <lMateVector . size ( ) ; ++ j ) { / / std : : cout << j << " : " << ioPopulation [ lMateVector [ j ] ] << std : : endl ; / / } / / childPop . clear ( ) ; / / = new ArrayList ( ) ; for ( int j =0; j <ioPopulation . size ( ) ; j +=2) { try { mateTrees ( ioPopulation . get ( j ) , ioPopulation . get ( j + 1 ) ) ; } catch ( java . lang . NullPointerException e ) { System . out . p r i n t l n ( e+" in apply crossover . . . c a l l to mateTrees " ) ; System . e x i t ( 0 ) ; } try { / / childPop . add ( trees . get ( 0 ) ) ; / / t h i s l i n e i s meant to implement Elitism , as / / childPop . add ( trees . get ( 1 ) ) ; / / ioPopulation . add (new Tree ( ioPopulation . get ( lMateVector . get ( mate ) . intValue }
  • 25. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XIII catch ( java . lang . Exception e ) { / / System . out . p r i n t l n ( j +" "+mate+" "+"WAITING In applyCrossover "+e ) ; / / + " "+ i / / System . e x i t ( 0 ) ; } } } /∗∗ ∗ Mate two GP trees f o r crossover . ∗ param ioTree1 F i r s t tree to mate . ∗ param ioTree2 Second tree to mate . ∗ param ioContext Evolutionary context . ∗ param inDistribProba 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 tree depth allowed . ∗ 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 checks assert ( ioTree1 . size ( ) > 0 ) ; assert ( ioTree2 . size ( ) > 0 ) ; float inDistribProba=Context . xoverNodeDistribProb ; int inMaxTreeDepth=Context . maxTreeDepth ; / / ArrayList <Tree> trees= n u l l ; / / Crossover loop . Try the given number of attempts to mate two i n d i v i d u a l s .
  • 26. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XIV for ( int 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 chang / / node f o r another node of the same tree i f the types mismatch . boolean lNode1IsTerminal = true ; i f ( ioTree1 . size ( ) > 1){ i f ( dice . nextFloat ( ) >= inDistribProba ) lNode1IsTerminal =true ; else lNode1IsTerminal = false ; } int lChoosenNode1=0; try { lChoosenNode1= dice . nextInt ( ioTree1 . size ( ) ) ; / / cant choose the root node } catch ( java . lang . IllegalArgumentException e ) { System . out . p r i n t l n ( ioTree1 . size ()+ " mate tree "+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 . nextInt ( ioTree1 . size ( ) ) ; } i f ( lChoosenNode1==0) lChoosenNode1+=1; boolean lNode2IsTerminal = true ; i f ( ioTree2 . size ( ) > 1) lNode2IsTerminal = ( dice . nextFloat ( ) >= inDistribProba ) ; int lChoosenNode2 = dice . nextInt ( ioTree2 . size ( ) ) ; 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 . nextInt ( ioTree2 . size ( ) ) ; }
  • 27. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XV i f ( lChoosenNode2==0)lChoosenNode2+=1; / / lChoosenNode2=ioTree2 . size () −1;// t r i a l / / Set f i r s t stack to the node of the f i r s t tree . / / Check i f depth i s ok . Do a new crossover attempt i f not . ArrayList <Integer > lStack1=ioTree1 . setStackToNode ( lChoosenNode1 ) ; int lNewDepthTree1 =lStack1 . size ( ) + ioTree2 . getDepth ( lChoosenNode2 ) − 1; i f ( lNewDepthTree1 > inMaxTreeDepth ) continue ; / / Set second stack to the node of the second tree . / / Check i f depth i s ok . Do a new crossover attempt i f not . ArrayList <Integer > lStack2=ioTree2 . setStackToNode ( lChoosenNode2 ) ; int lNewDepthTree2 =lStack2 . size ( ) + 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 . setValid ( false ) ; / / ioTree2 . setValid ( false ) ; / / trees . get ( 0 ) . setValid ( false ) ; / / trees . get ( 1 ) . setValid ( false ) ; / / } / / catch ( java . lang . Exception e ) { / / System . out . p r i n t l n ( e+"The return value i s n u l l in mate−trees : " ) ; / / System . e x i t ( 0 ) ; / / } break ;
  • 28. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XVI } / / return trees ; } /∗∗ ∗ Exchange two sub−trees . ∗ param ioTree1 Tree containing the f i r s t sub−tree to exchange . ∗ param inNode1 Index of root node to sub−tree to swap in f i r s t tree . ∗ param inStack1 Stack containing the parents to the f i r s t sub−tree root node . ∗ param ioTree2 Tree containing the second sub−tree to exchange . ∗ param inNode2 Index of root node to sub−tree to swap in second tree . ∗ param inStack2 Stack containing the parents to the second sub−tree root node . ∗ ingroup Puppy ∗/ public void exchangeSubTrees ( f i n a l Tree iTree1 , int inNode1 , f i n a l ArrayList <Integer > inStack1 { / / assert ( iTree1 != iTree2 ) ; / / I can mate mate between s i m i l a r trees as well , in the worst ca assert ( inStack1 . size ( ) > 0 ) ; assert ( inStack2 . size ( ) > 0 ) ; Tree ioTree1=new Tree ( iTree1 ) ; Tree ioTree2=new Tree ( iTree2 ) ; int lSwapSize1 = ioTree1 . get ( inNode1 ) . getSubTreeSize ( ) ; int lSwapSize2 = ioTree2 . get ( inNode2 ) . getSubTreeSize ( ) ;
  • 29. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XVII Tree ioSubTree1=new Tree ( ) ; Tree ioSubTree2=new Tree ( ) ; for ( int i =0; i <lSwapSize1 ; i ++){ try { 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 ( int i =0; i <lSwapSize2 ; i ++){ try { 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 . addAll ( inNode1 , ioSubTree2 ) ; ioTree2 . addAll ( inNode2 , ioSubTree1 ) ; int l D i f f S i z e = lSwapSize1 − lSwapSize2 ; for ( int i =0; i <( inStack1 . size () −1); ++ i ) try {
  • 30. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XVIII ioTree1 . get ( inStack1 . get ( i ) . intValue ( ) ) . setSubTreeSize ( ioTree1 . get ( inStack1 . get ( i ) . intValu } catch ( java . lang . IndexOutOfBoundsException e ) { System . out . p r i n t l n ( " exchangeSubTrees4 "+e ) ; System . e x i t ( 0 ) ; } for ( int j =0; j <( inStack2 . size () −1); ++ j ) try { ioTree2 . get ( inStack2 . get ( j ) . intValue ( ) ) . setSubTreeSize ( ioTree2 . get ( inStack2 . get ( j ) . intValu } catch ( java . lang . IndexOutOfBoundsException e ) { System . out . p r i n t l n ( " exchangeSubTrees5 "+e ) ; System . e x i t ( 0 ) ; } iTree1 . clear ( ) ; iTree1 . addAll ( ioTree1 ) ; iTree2 . clear ( ) ; iTree2 . addAll ( ioTree2 ) ; iTree1 . setEvaluated ( false ) ; iTree2 . setEvaluated ( false ) ; iTree1 . setValid ( false ) ; iTree2 . setValid ( false ) ; iTree1 . usedXover=true ; iTree2 . usedXover=true ; }
  • 31. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XIX /∗∗ ∗ Apply standard ( Koza ’ s ) mutation on a GP trees . ∗ This performs subtree mutation ∗ Input : − ioTree GP tree to mutate . ∗ ioContext Evolutionary context . ∗ inMaxRegenDepth Maximum tree regeneration depth allowed . ∗ inMaxDepth Maximum tree depth allowed . ∗ Output : − ∗ ioTree − Mutated GP Tree ∗ ingroup Puppy ∗/ public void mutateStandard ( Tree ioTree , int inMaxRegenDepth , int inMaxDepth ) { / / System . out . p r i n t l n ( " Following i s the tree before mutation : n " ) ; / / + ioTree . writeTree ( 0 ) ) ; / / f o r ( i n t i =0; i <ioTree . size ( ) ; 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 . size () <=0)System . out . p r i n t l n ( "HUGE ERROR IN MUTATESTANDARD" ) ; assert ( ioTree . size ( ) > 0 ) ; int lMutIndex = dice . nextInt ( ioTree . size () −1); i f ( lMutIndex ==0) lMutIndex +=1; Tree lNewTree=new Tree ( ) ; for ( int i =0; i <lMutIndex ; i ++){ lNewTree . add ( ioTree . get ( i ) ) ; / /NEW } ArrayList <Integer > lStack=ioTree . setStackToNode ( lMutIndex ) ;
  • 32. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XX lStack . remove ( lStack . size () −1); int lTreeDepth = dice . nextInt ( inMaxRegenDepth )+1; int lTreeDepth2 = inMaxDepth − lStack . size ( ) ; i f ( lTreeDepth2 < lTreeDepth ) lTreeDepth = lTreeDepth2 ; assert ( lTreeDepth > 0 ) ; initializeTreeGrow ( lNewTree , 1 , lTreeDepth ) ; for ( int i =( lMutIndex+ioTree . get ( lMutIndex ) . getSubTreeSize ( ) ) ; i <ioTree . size ( ) ; i ++){ try { lNewTree . add ( ioTree . get ( i ) ) ; / /NEW } catch ( java . lang . IndexOutOfBoundsException e ) { System . out . p r i n t l n ( e+" in mutateStandard "+" "+lMutIndex+" "+ioTree . get ( lMutIndex ) . getS System . e x i t ( 0 ) ; } } int l D i f f S i z e =0; try { l D i f f S i z e =ioTree . get ( lMutIndex ) . getSubTreeSize ( ) − lNewTree . get ( lMutIndex ) . getSubTreeS } catch ( java . lang . Exception e ) { System . out . p r i n t l n ( e+" mutIndex : "+lMutIndex ) ; / / System . out . p r i n t l n ( ioTree . writeTree ( 0 ) ) ; for ( int i i =0; i i <ioTree . size ( ) ; i i ++) System . out . p r i n t ( ioTree . get ( i i ) . getSubTreeSize ()+ " " ) ; System . e x i t ( 0 ) ; } for ( int i =0; i <lStack . size ( ) ; ++ i )
  • 33. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XXI lNewTree . get ( lStack . get ( i ) ) . setSubTreeSize ( lNewTree . get ( lStack . get ( i ) ) . getSubTreeSize()− / / ioTree= n u l l ; / / ioTree=lNewTree ; lNewTree . setValid ( false ) ; lNewTree . setEvaluated ( false ) ; ioTree . setValid ( false ) ; ioTree . setEvaluated ( false ) ; ioTree . clear ( ) ; for ( int i =0; i <lNewTree . size ( ) ; i ++) ioTree . add (new Node( lNewTree . get ( i ) ) ) ; ioTree . usedMutationStd=true ; / / System . out . p r i n t l n ( " Following i s the tree a f t e r mutation : n " ) ; / / + ioTree . writeTree ( 0 ) ) ; / / f o r ( i n t i =0; i <ioTree . size ( ) ; 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 : "+ lMutIndex ) ; / / return lNewTree ; } /∗ ! ∗ b r i e f Apply swap point mutation to a population of GP trees . ∗ param ioPopulation Population to mutate . ∗ param ioContext Evolutionary context . ∗ param inMutationProba Mutation p r o b a b i l i t y . ∗ param inDistribProba P r o b a b i l i t y to mutate a function node , in opposition to a terminal . ∗ ingroup Puppy ∗/ public void applyMutationSwap ( ArrayList <Tree> ioPopulation )
  • 34. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XXII { float inMutationProba=Context . swapMutationProba ; for ( int i =0; i <ioPopulation . size ( ) ; ++ i ) { i f ( dice . nextFloat ( ) < inMutationProba ) { mutateSwap ( ioPopulation . get ( i ) ) ; } } } /∗ ! ∗ b r i e f Swap mutate a GP tree . ∗ param ioTree GP tree to mutate . ∗ param ioContext Evolutionary context . ∗ param inDistribProba P r o b a b i l i t y to mutate a function node , in opposition to a terminal . ∗ ingroup Puppy ∗/ public void mutateSwap ( Tree ioTree ) { float inDistribProba=Context . xoverNodeDistribProb ; assert ( ioTree . size ( ) > 0 ) ; int lMutIndex = dice . nextInt ( ioTree . size ( ) ) ; i f ( ioTree . size ( ) > 1) { boolean lType = ( dice . nextFloat ( ) < inDistribProba ) ; while ( ( ioTree . get ( lMutIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) != 0) != lType ) { lMutIndex = dice . nextInt ( ioTree . size ( ) ) ; } } int lNbArgs = ioTree . get ( lMutIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
  • 35. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XXIII i f ( lNbArgs == 0) { assert ( Context . mTerminalSet . size ( ) > 0 ) ; P r i m i t i v e lTerminal = Context . mTerminalSet . get ( dice . nextInt ( Context . mTerminalSet . size ( ) ) ) ; ioTree . get ( lMutIndex ) . s e t P r i m i t i v e ( lTerminal . clone ( ) . giveReference ( dice ) ) ; } else { ArrayList <Integer > lKArgsFunction=new ArrayList ( ) ; for ( int i =0; i <Context . mFunctionSet . size ( ) ; ++ i ) { i f ( Context . mFunctionSet . get ( i ) . g e t A r i t y ( ) == lNbArgs ) { lKArgsFunction . add (new Integer ( i ) ) ; } } assert ( lKArgsFunction . size ( ) > 0 ) ; P r i m i t i v e lFunction =Context . mFunctionSet . get ( lKArgsFunction . get ( dice . nextInt ( lKArgsFuncti ioTree . get ( lMutIndex ) . s e t P r i m i t i v e ( lFunction . clone ( ) ) ; } ioTree . usedMutationSwap=true ; } /∗∗ ∗ Apply standard ( Koza ’ s ) mutation to a population of GP trees . ∗ param ioPopulation Population to mutate . ∗ param ioContext Evolutionary context . ∗ param inMutationProba Mutation p r o b a b i l i t y . ∗ param inMaxRegenDepth Maximum tree regeneration depth allowed . ∗ param inMaxDepth Maximum tree depth allowed . ∗ ingroup Puppy
  • 36. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XXIV ∗/ public void applyMutationStandard ( ArrayList <Tree> ioPopulation ) { float inMutationProba=Context . mutationProba ; int inMaxRegenDepth=Context . initTreeDepthMax ; int inMaxDepth=Context . maxTreeDepth ; / / System . out . p r i n t l n ( " Lets see how Mutation works n"+ ioPopulation . get ( t h i s . parentPopSize ) . for ( int i =this . parentPopSize ; i <ioPopulation . size ( ) ; i ++) { i f ( dice . nextFloat () <= inMutationProba ) { mutateStandard ( ioPopulation . get ( i ) , inMaxRegenDepth , inMaxDepth ) ; } } / / System . out . p r i n t l n ( " Mutation worked l i k e t h i s n"+ ioPopulation . get ( t h i s . parentPopSize ) . w / / return ioPopulation ; } /∗∗ ∗adapt the p r o b a b i l i t i e s of genetic operators in a dynamic fashion ∗/ public void adaptOperatorProbabilities ( List <Tree> lPopulation ) { I t e r a t o r <Tree> popItr =lPopulation . i t e r a t o r ( ) ; int numUsedXover=1 , numUsedMutStd=1 , numUsedMutSwap=1; int numImpXover=0 , numImpMutStd=0 , numImpMutSwap=0; while ( popItr . hasNext ( ) ) { Tree tmpTree= popItr . next ( ) ; i f ( tmpTree . usedXover ) { numUsedXover++; i f ( tmpTree . getTrainingFitness () <= Context . previousBest )
  • 37. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XXV numImpXover++; } i f ( tmpTree . usedMutationStd ) { numUsedMutStd++; i f ( tmpTree . getTrainingFitness () <= Context . previousBest ) numImpMutStd++; } i f ( tmpTree . usedMutationSwap ) { numUsedMutSwap++; i f ( tmpTree . getTrainingFitness () <= Context . previousBest ) numImpMutSwap++; } } Context . xoverProba =( float ) ( numImpXover / numUsedXover ) ; Context . mutationProba =( float ) ( numImpMutStd / numUsedMutStd ) ; Context . swapMutationProba =( float ) ( numImpMutSwap / numUsedMutSwap ) ; i f ( Context . xoverProba <0.5F) Context . xoverProba =0.7F ; i f ( Context . mutationProba <0.01F) Context . mutationProba =0.15F ; i f ( Context . swapMutationProba <0.01F) Context . swapMutationProba =0.15F ; } /∗∗ ∗Perform a l l Evolutionary steps in one go ∗1) ApplySelectionTournament ∗2) ApplyCrossover ∗3) ApplyMutationStandard ∗4) Evaluate f i t n e s s of the population
  • 38. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XXVI ∗5) Apply e l i t i s t s u r v i v a l ∗/ public Tree applyAllEvolutionarySteps ( ArrayList <Tree> lPopulation ) { ArrayList <Tree> childPop=applySelectionTournament1 ( lPopulation ) ; applyCrossover ( childPop ) ; applyMutationStandard ( childPop ) ; applyMutationSwap ( childPop ) ; / / Set the S−Expressions now here as t h i s w i l l save some computation lPopulation . addAll ( childPop ) ; evaluatePopulation ( lPopulation ) ; adaptOperatorProbabilities ( lPopulation . subList ( Context . popSize , lPopulation . size () −1)); this . applySurvival1 ( lPopulation ) ; for ( int i =0; i <10; i ++) lPopulation . get ( i ) . tuneTreesCoeffs ( ) ; Collections . sort ( lPopulation , new FitnessComparator ( ) ) ; / / sort again i f GA i s used return lPopulation . get ( 0 ) ; / / return the best tree as i t remains the f i r s t element a f t e r so } /∗∗ ∗Evaluate a given population ∗/ public void evaluatePopulation ( ArrayList <Tree> lPopulation ) { I t e r a t o r <Tree> i t =lPopulation . i t e r a t o r ( ) ; double bestFit =Double .MAX_VALUE; while ( i t . hasNext ( ) ) { Tree tmpTree= i t . next ( ) ; / / new Thread ( tmpTree ) . s t a r t ( ) ;
  • 39. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XXVII double f i t n e s s =tmpTree . evaluateTree ( ) ; / / i t . next ( ) . tuneTreesCoeffs ( ) ; i f ( fitness <Context . previousBest ) Context . previousBest= 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 applySurvival1 ( ArrayList <Tree> ioPopulation ) { Collections . sort ( ioPopulation . subList (0 , ioPopulation . size ()/2 −1) , new FitnessComparator ( ) ) Collections . sort ( ioPopulation . subList ( ioPopulation . size ( ) / 2 , ioPopulation . size () −1) , new F / / Collections . sort ( ioPopulation , new DepthComparator ( ) ) / / Sort wrt depth as well now −− I g int dynDepth=ioPopulation . get ( ioPopulation . size ( ) / 2 ) . getDepth ( 0 ) ; / / of the best c h i l d / / i f ( dynDepth> t h i s . dynamicLevel ) i f ( dynDepth>=6 && ioPopulation . get ( ioPopulation . size ( ) / 2 ) . getTrainingFitness () < ioPopulatio this . dynamicLevel=dynDepth ; int children_ ; for ( int i =ioPopulation . size ( ) / 2 ; i <ioPopulation . size ( ) ; i ++){ / / remove from among childPop Tree tmpTree=ioPopulation . get ( i ) ; i f ( tmpTree . getDepth (0) > this . dynamicLevel ) { / / dynamic tree depth ioPopulation . remove ( i ) ; i −−; } } while ( ioPopulation . subList ( this . parentPopSize , ioPopulation . size ( ) ) . size () > this . parentPopS ioPopulation . remove ( ioPopulation . size () −1); / / remove the extra children here int i =this . parentPopSize −1;
  • 40. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XXVIII while ( ioPopulation . size () > this . parentPopSize ) ioPopulation . remove ( i −−); / / remove the less f i t parents now Collections . sort ( ioPopulation , new FitnessComparator ( ) ) ; Context . previousBest=ioPopulation . get ( 0 ) . getTrainingFitness ( ) ; System . out . p r i n t l n ( this . dynamicLevel ) ; } } /∗∗ ∗Fitness Comparator ∗/ class FitnessComparator implements java . u t i l . Comparator<Tree >{ public int compare ( Tree o1 , Tree o2 ) { double f i t 1 =o1 . getTrainingFitness ( ) ; double f i t 2 =o2 . getTrainingFitness ( ) ; 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; } } /∗∗
  • 41. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PUPPY XXIX ∗Size ( Tree Depth ) Comparator ∗/ class DepthComparator implements java . u t i l . Comparator<Tree >{ public int compare ( Tree o1 , Tree o2 ) { int depth1=o1 . getDepth ( 0 ) ; int depth2=o2 . getDepth ( 0 ) ; i f ( depth1<depth2 ) return −1; i f ( depth1==depth2 ) return 0; return 1; } }
  • 42. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S 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 in 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 . io . ∗ ; /∗∗ ∗ ∗ @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 mPrimitive ; / / ! < Smart pointer to the associated p r i m i t i v e . private int mSubTreeSize ; / / ! < Sub−tree size , including actual node . /∗∗ Creates a new instance of Node ∗/ public Node( P r i m i t i v e p r i m i t i v e , int size ) { this . mPrimitive= p r i m i t i v e . clone ( ) ; this . mSubTreeSize=size ; }
  • 43. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S NODE II /∗∗ ∗Copy Constructor ∗/ public Node(Node copy ) { / / t h i s . mPrimitive=new P r i m i t i v e ( copy . mPrimitive ) ; this . mPrimitive=copy . mPrimitive . clone ( ) ; this . 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 . mPrimitive=new P r i m i t i v e ( prim ) ; this . mPrimitive=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 this . mPrimitive ; } /∗∗ ∗void subtree size ∗/ public void setSubTreeSize ( f i n a l int treeSize ) {
  • 44. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S NODE III this . mSubTreeSize=treeSize ; } /∗∗ ∗Return the subtree size ∗/ public f i n a l int getSubTreeSize ( ) { return this . mSubTreeSize ; } }
  • 45. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S EVALUATION I /∗ ∗ Evaluation . java ∗ ∗ Created on September 6 , 2007, 9:18 PM ∗ ∗ To change t h i s template , choose Tools | Template Manager ∗ and open the template in the e d i t o r . ∗Keeps the Evaluation of a subtee l y i n g in a cache ∗/ package GeneticProgramming ; /∗∗ ∗ ∗ @author a d i l r a j a ∗/ public class Evaluation { public double [ ] evaluation ; public boolean cachedFlag ; /∗∗ ∗The default constructor f o r Terminal nodes ∗/ public Evaluation ( ) { this . cachedFlag=true ; / / Vals f o r Ts always get true this . evaluation=new double [ 1 ] ;
  • 46. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S EVALUATION II } /∗∗ ∗The contructor f o r NT subtrees ∗/ public Evaluation ( double [ ] eval , boolean cFlag ) { this . evaluation=new double [ eval . length ] ; for ( int i =0; i <eval . length ; i ++){ this . evaluation [ i ]= eval [ i ] ; this . cachedFlag=cFlag ; / / Defines Whether an entry i s e f f e c t i v e l y cached } } }
  • 47. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S 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 in 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 . io . ∗ ; public class Tree extends ArrayList <Node> implements S e r i a l i z a b l e { private double fitness , testFitness ; private boolean v a l i d ; private String s_expression ; private boolean evaluated ; / / t e l l s i f the f i t n e s s of t h i s tree has been evaluated private double slope ;
  • 48. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S 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 contains the r e s u l t of the i n d i v i d u a l over whole public static double [ ] forConstants ; / / These two variables s h a l l be used by the p r i m i t i v e s public boolean usedXover , usedMutationStd , usedMutationSwap ; /∗∗ Creates a new instance of Tree ∗/ public Tree ( ) { this . f i t n e s s =Double .MAX_VALUE; / / assign a f i t n e s s value this . v a l i d =false ; this . evaluated=false ; } /∗∗ ∗ Construct a new tree , with given f i t n e s s and v a l i d i t y f l a g . ∗/ public Tree ( double sFitness , boolean i V a l i d ) { this . f i t n e s s =sFitness ; this . v a l i d = i V a l i d ; this . s_expression=new String ( ) ; this . evaluated=false ; / / that i t has not been evaluated by default this . slope =1; this . i n t e r c e p t =0; } /∗∗ ∗ copy Constructor ∗/
  • 49. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE III public Tree ( f i n a l Tree copy ) { this . f i t n e s s =copy . f i t n e s s ; this . testFitness =copy . testFitness ; this . v a l i d =copy . v a l i d ; / / t h i s . s_expression=copy . writeTree ( 0 ) ; this . evaluated=copy . evaluated ; for ( int i =0; i <copy . size ( ) ; i ++) this . add (new Node( copy . get ( i ) ) ) ; this . slope=copy . slope ; this . 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 tree depth at given index . ∗ param inIndex Index of sub−tree root to get the depth from . ∗ return Sub−tree depth . ∗/ public f i n a l int getDepth ( int inIndex ) { i f ( inIndex < this . size ( ) ) { / / I had assert here int lNbArgs = this . get ( inIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ; int lDepth = 1; int j = inIndex + 1; for ( int i =0; i <lNbArgs ; ++ i ) { int lChildDepth = getDepth ( j ) + 1; i f ( lChildDepth > lDepth ) lDepth = lChildDepth ; j += this . get ( j ) . getSubTreeSize ( ) ; }
  • 50. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE IV return lDepth ; } else { return 0; } } /∗∗ ∗ Compare e qu al it y of two trees . ∗ Two trees 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 bucketting reasons ∗ return : True i s trees are equals , false i f not . ∗/ public f i n a l boolean compareTrees ( f i n a l Tree aTree ) { i f ( this==aTree ) return true ; return ( this . v a l i d && aTree . v a l i d && ( java . lang . Math . abs ( this . f i t n e s s − aTree . f i t n e s s ) <0.01 } /∗∗ ∗ Compare ranking of two trees . ∗ return True i s actual tree i s less than a tree given as argument , false i f not . ∗/ public f i n a l boolean compareRank ( Tree aTree ) { return ( this . v a l i d && aTree . v a l i d && ( this . f i t n e s s < aTree . f i t n e s s ) ) ; }
  • 51. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE V /∗ ∗ I n t e r p r e t the GP tree . ∗ param outResult Datum containing the r e s u l t of the i n t e r p r e t a t i o n . ∗ param ioContext Evolutionary context . ∗ void Puppy : : Tree : : i n t e r p r e t ( void∗ outResult , Puppy : : Context& ioContext ) { assert ( size ( ) > 0 ) ; ioContext . mTree = t h i s ; ioContext . mCallStack . push_back ( 0 ) ; f r o n t ( ) . mPrimitive−>execute ( outResult , ioContext ) ; ioContext . mCallStack . pop_back ( ) ; } ∗/ /∗∗ ∗ Set c a l l stack to include the c o r r e c t l y r e f e r to a given node . ∗ param inIndex Node index to which c a l l stack must be set . ∗ param outCallStack Result of c a l l stack s e t t i n g . ∗/ public f i n a l java . u t i l . ArrayList <Integer > setStackToNode ( int inIndex ) { assert ( inIndex < size ( ) ) ; ArrayList <Integer > callStack=new ArrayList ( ) ; int i = 0; callStack . add (new Integer ( i ) ) ; while ( i < inIndex ) {
  • 52. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE VI int lNbArgs=this . get ( i ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ; int lChildIndex = i + 1; for ( int j =0; j <lNbArgs ; ++ j ) { i f ( ( lChildIndex+this . get ( lChildIndex ) . getSubTreeSize ( ) ) > inIndex ) break ; lChildIndex += this . get ( lChildIndex ) . getSubTreeSize ( ) ; } assert ( lChildIndex < this . size ( ) ) ; i = lChildIndex ; callStack . add (new Integer ( i ) ) ; } assert ( i == inIndex ) ; return callStack ; } /∗∗ ∗Returns the slope parameter found by l i n e a r scaling ∗/ public f i n a l double getSlope ( ) { return this . slope ; } /∗∗ ∗Returns the i n t e r c e p t parameter found by l i n e a r scaling ∗/ public f i n a l double getIntercept ( ) { return this . i n t e r c e p t ; }
  • 53. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE VII /∗∗ ∗ b r i e f Write GP tree at given index as a s−expression i n t o a Java String object . ∗ param ioOS C++ output stream to write tree i n t o . ∗ param inIndex Actual node index in tree . ∗/ public void writeTree ( int inIndex , StringBuilder expression ) { i f ( inIndex >= this . size ( ) ) return ; / / expression . append ( " " ) ; int lNbArgs = this . get ( inIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ; expression . append ( this . get ( inIndex ) . g e t P r i m i t i v e ( ) . getName ( ) ) ; i f ( lNbArgs > 0) expression . append ( " ( " ) ; int j = inIndex + 1; for ( int i =0; i <lNbArgs ; ++ i ) { writeTree ( j , expression ) ; i f ( i <lNbArgs−1) expression . append ( " , " ) ; try { j = j +this . get ( j ) . getSubTreeSize ( ) ; } catch ( java . lang . IndexOutOfBoundsException e ) { System . out . p r i n t l n ( e+" In write tree " ) ; / / System . e x i t ( 0 ) ; } } i f ( lNbArgs > 0) expression . append ( " ) " ) ; / / return expression . t r t r i m ( ) ; / / removes the " " added in the beginning .
  • 54. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE VIII } /∗∗ ∗ b r i e f Write GP tree at given index as a s−expression i n t o a Java String object . ∗ param ioOS C++ output stream to write tree i n t o . ∗ param inIndex Actual node index in tree . ∗/ public void writeTree2 ( int inIndex , StringBuilder expression ) { i f ( inIndex >= this . size ( ) ) return ; / / expression . append ( " " ) ; int lNbArgs = this . get ( inIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ; expression . append ( this . get ( inIndex ) . g e t P r i m i t i v e ( ) . getName ( ) ) ; / / i f ( lNbArgs > 0) expression . append ( " ( " ) ; int j = inIndex + 1; for ( int i =0; i <lNbArgs ; ++ i ) { writeTree ( j , expression ) ; / / i f ( i <lNbArgs−1) / / expression . append ( " , " ) ; try { j = j +this . get ( j ) . getSubTreeSize ( ) ; } catch ( java . lang . IndexOutOfBoundsException e ) { System . out . p r i n t l n ( e+" In write tree " ) ; / / System . e x i t ( 0 ) ; } }
  • 55. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE IX / / i f ( lNbArgs > 0) expression . append ( " ) " ) ; / / return expression . t r t r i m ( ) ; / / removes the " " added in the beginning . } /∗∗ ∗sets the s−expression ∗/ public void setSExpression ( ) { StringBuilder expr=new StringBuilder ( this . size ( ) ) ; this . writeTree (0 , expr ) ; this . s_expression=new String ( expr . t oS tr in g ( ) ) ; } /∗∗ ∗Gets the s−epression ∗/ public f i n a l String getSExpression ( ) { return this . s_expression ; } /∗∗ ∗Set the v a l i d f l a g to something ∗/ public void setValid ( f i n a l boolean inValid ) { this . v a l i d = inValid ; }
  • 56. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE X /∗∗ ∗Get the v a l i d i t y status of t h i s tree ∗/ public f i n a l boolean getValid ( ) { return this . 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 ) { this . s e t P r i m i t i v e ( prim ) ; } /∗∗ ∗Set the evaluated f l a g of t h i s tree ∗/ public void setEvaluated ( f i n a l boolean eval ) { this . evaluated=eval ; } /∗∗ ∗Get the evaluation f l a g of t h i s tree ∗/ public f i n a l boolean getEvaluated ( ) { return this . evaluated ; }
  • 57. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE XI /∗∗ ∗Set the f i t n e s s of t h i s Tree ∗/ public void setTrainingFitness ( f i n a l double f i t ) { this . 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 of t h i s Tree ∗/ public f i n a l double getTrainingFitness ( ) { return this . f i t n e s s ; } /∗∗ ∗Return the f i t n e s s of Testing data ∗/ public f i n a l double getTestingFitness ( ) { return this . testFitness ; } / / public void run ( ) { / / t h i s . evaluateTree ( ) ; / / } /∗∗ ∗ Evaluate f i t n e s s of a Tree ∗ ∗ inX Independant sample values f o r evaluation .
  • 58. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE XII ∗ inF Dependant sample values f o r evaluation . ∗ return Number of f i t n e s s evaluated . ∗ ingroup SymbReg ∗/ public double evaluateTree ( ) { assert ( Context . XData . length == Context . Target . length ) ; i f ( this . evaluated ) return this . f i t n e s s ; double [ ] target =Context . Target ; double [ ] evolved=this . interpretTree ( Context . XData ) ; i f ( evolved . length != Context . Target . length ) { double tmpEvolved=evolved [ 0 ] ; / / i f everything that i s returned i s a constant evolved=new double [ Context . Target . length ] ; for ( int z=0;z<Context . Target . length ; z++) evolved [ z ]= tmpEvolved ; } double [ ] slope_n_intercept=new double [ 2 ] ; linearScaling ( slope_n_intercept , evolved , Context . Target ) ; / / t h i s . t r a i n i n g R e s u l t =new double [ evolved . length ] ; double tmpFitness =0; double res ; for ( int i =0; i <evolved . length ; i ++){ res=Context . Target [ i ]−( slope_n_intercept [0]∗ evolved [ i ]+ slope_n_intercept [ 1 ] ) ; tmpFitness+=res∗res ;
  • 59. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE XIII } tmpFitness=tmpFitness / evolved . length ; / / i f ( tmpFitness < t h i s . f i t n e s s ) { this . f i t n e s s =tmpFitness ; this . slope=slope_n_intercept [ 0 ] ; this . i n t e r c e p t =slope_n_intercept [ 1 ] ; / / } i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) ) this . 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 ) ; this . v a l i d = true ; this . evaluated=true ; return this . f i t n e s s ; } /∗∗ ∗ Evaluate f i t n e s s of a Tree ∗ ∗ inX Independant sample values f o r evaluation . ∗ inF Dependant sample values f o r evaluation . ∗ return Number of f i t n e s s evaluated . ∗ ingroup SymbReg ∗/ public double evaluateTreeForTestData ( ) { assert ( Context . TestXData . length == Context . TestTarget . length ) ;
  • 60. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE XIV double [ ] target =Context . TestTarget ; double [ ] evolved=this . interpretTree ( Context . TestXData ) ; i f ( evolved . length != Context . TestTarget . length ) { double tmpEvolved=evolved [ 0 ] ; / / i f everything that i s returned i s a constant evolved=new double [ Context . TestTarget . length ] ; for ( int z=0;z<Context . TestTarget . length ; 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 . length ] ; double tmpFitness =0; double res ; for ( int i =0; i <evolved . length ; i ++){ res=Context . TestTarget [ i ]−( this . slope∗evolved [ i ]+ this . i n t e r c e p t ) ; tmpFitness+=res∗res ; } tmpFitness=tmpFitness / evolved . length ; / / i f ( tmpFitness < t h i s . f i t n e s s ) { this . testFitness =tmpFitness ; i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) ) this . testFitness =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 ) ; this . v a l i d = true ; this . evaluated=true ; return this . testFitness ; }
  • 61. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE XV /∗∗ ∗LinearScaling , by Maarten Keijzer , i s implemented here ∗/ public void linearScaling ( double [ ] slope_n_intercept , double [ ] evolved , double [ ] target ) { assert ( evolved . length== target . length ) ; double meanTarget=0 , meanEvolved=0; for ( int i =0; i <evolved . length ; i ++){ meanEvolved+=evolved [ i ] ; meanTarget+= target [ i ] ; } meanEvolved /= evolved . length ; meanTarget /= target . length ; double numerator =0 , denominator =0; for ( int i =0; i <evolved . length ; i ++){ numerator +=( target [ i ]−meanTarget )∗( evolved [ i ]−meanEvolved ) ; denominator+=java . lang . Math . pow( evolved [ i ]−meanEvolved , 2 ) ; } / / numerator=numerator / target . length ; / / denominator /= evolved . length ; double slope1 =1; i f ( denominator !=0) slope1=numerator / denominator ; else i f ( meanEvolved==0) slope1 =1; else slope1=meanTarget / meanEvolved ; slope_n_intercept [0]= slope1 ; slope_n_intercept [1]= meanTarget−slope∗meanEvolved ;
  • 62. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE XVI } /∗∗ ∗ I n t e r p r e t the GP tree . ∗ outResult Datum containing the r e s u l t of the i n t e r p r e t a t i o n . ∗ XData to be evaluated ∗/ public double [ ] interpretTree ( float [ ] [ ] xdata ) { assert ( this . size ( ) > 0 ) ; ArrayList <Integer > callStack=new ArrayList ( ) ; callStack . add (new Integer ( 0 ) ) ; double [ ] outResult=this . get ( 0 ) . g e t P r i m i t i v e ( ) . execute ( callStack , this , xdata ) ; callStack . remove ( callStack . size () −1); return outResult ; } /∗∗ ∗ tune the terminals ( constants ) of t h i s tree ∗ ∗ inX Independant sample values f o r evaluation . ∗ inF Dependant sample values f o r evaluation . ∗ return Number of f i t n e s s evaluated . ∗ ingroup SymbReg ∗/ public double tuneTreesCoeffs ( ) { assert ( Context . XData . length == Context . Target . length ) ; / / i f ( t h i s . evaluated ) return t h i s . f i t n e s s ;
  • 63. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE XVII I t e r a t o r <Node> nodItr =this . i t e r a t o r ( ) ; int i i =0; while ( nodItr . hasNext ( ) ) { Node tmpNode= nodItr . 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 ( ) . startsWith ( "X" ) ) i i ++; / / f i n d the number of constants } i f ( i i <=1) return this . 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 int [ ] constIndex=new int [ i i ] ; / / keeps the indices of constants in t h i s tree double [ ] constants=new double [ i i ] ; / / t h i s array would keep the constants f o r the interim ( a t int j j =0; for ( i i =0; i i <this . size ( ) ; i i ++){ i f ( this . get ( i i ) . g e t P r i m i t i v e ( ) . g e t A r i t y ()==0 && ! this . get ( i i ) . g e t P r i m i t i v e ( ) . getName ( ) . startsWith ( "X" ) ) { constIndex [ j j ]= i i ; / / t h i s loop copies the indices to the array constants [ j j ]= Double . valueOf ( this . 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 here Nursery nurse=new Nursery ( ) ; ArrayList <Genotype> pop=nurse . i n i t i a l i z e P o p u l a t i o n (100 , constIndex . length , −6, +6); / / should for ( int i =0; i <50; i ++){ / / numGens i s chosen to be 150 ArrayList <Genotype> childPop=nurse . applySelectionTournament ( pop , 4 ) ;
  • 64. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE XVIII nurse . applySinglePointCrossover ( childPop , constIndex . length , 0 . 8 ) ; nurse . applyMutationUniform ( childPop , constIndex . length , 0 . 2 ) ; pop . addAll ( childPop ) ; / / append the two pops here for ( int j =0; j <pop . size ( ) ; j ++){ i f ( pop . get ( j ) . getEvaluatedFlag ()== false ) { / / i f i t has not been evaluated double [ ] tmpGenes=pop . get ( j ) . getGenotype ( ) ; for ( int k=0;k<constIndex . length ; k++) this . get ( constIndex [ k ] ) . g e t P r i m i t i v e ( ) . setName ( Double . to St rin g ( tmpGenes [ k double [ ] evolved=this . interpretTree ( Context . XData ) ; i f ( evolved . length != Context . Target . length ) { double tmpEvolved=evolved [ 0 ] ; / / i f everything that i s returned i s a constan evolved=new double [ Context . Target . length ] ; for ( int z=0;z<Context . Target . length ; z++) evolved [ z ]= tmpEvolved ; } double [ ] slope_n_intercept=new double [ 2 ] ; linearScaling ( slope_n_intercept , evolved , Context . Target ) ; / / t h i s . t r a i n i n g R e s u l t =new double [ evolved . length ] ; double tmpFitness =0; double res ; for ( int i j =0; i j <Context . Target . length ; i j ++){ res=Context . Target [ i j ]−( slope_n_intercept [0]∗ evolved [ i j ]+ slope_n_intercep tmpFitness+=res∗res ; } tmpFitness=tmpFitness / evolved . length ; i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) ) tmpFitness=Double .MAX_VALUE;
  • 65. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE XIX i f ( tmpFitness <this . f i t n e s s ) { this . f i t n e s s =tmpFitness ; this . slope=slope_n_intercept [ 0 ] ; this . i n t e r c e p t =slope_n_intercept [ 1 ] ; for ( int k=0;k<tmpGenes . length ; k++) constants [ k ]=tmpGenes [ k ] ; } pop . get ( j ) . setFitness ( tmpFitness ) ; i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) ) { this . f i t n e s s =Double .MAX_VALUE; / / assign a very large f i t n e s s pop . get ( j ) . setFitness ( Double .MAX_VALUE) ; } pop . get ( j ) . setEvaluated ( true ) ; } / / i f getEvaluated } nurse . applySurvival ( pop ) ; } for ( int i =0; i <constants . length ; i ++) this . get ( constIndex [ i ] ) . g e t P r i m i t i v e ( ) . setName ( Double . to St rin g ( constants [ i ] ) ) ; / / set th / / System . out . p r i n t l n ( " Current best "+ t h i s . f i t n e s s ) ; this . v a l i d = true ; this . evaluated=true ; return this . f i t n e s s ; }
  • 66. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S TREE XX }
  • 67. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S 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 in the e d i t o r . ∗/ package GeneticProgramming ; /∗∗ ∗ ∗ @author a d i l r a j a ∗/ import java . io . ∗ ; 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 instance of S t a t i s t i c s ∗/ public S t a t i s t i c s ( ) { } /∗∗ ∗copy constructor ∗/ 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 . length ] ; 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 . length ] ;
  • 68. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S STATISTICS II for ( int i =0; i <copy . t r a i n F i t H i s t o r y . length ; 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 static double [ ] t r a i n F i t H i s t o r y ; public static double [ ] t e s t F i t H i s t o r y ; public static Tree b e s t I n d i v i d u a l ; }
  • 69. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S 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 in the e d i t o r . ∗/ package GeneticProgramming ; /∗∗ ∗ ∗ @author a d i l r a j a ∗/ import java . io . ∗ ; import java . u t i l . ∗ ; public class S t a t i s t i c s I O { /∗∗ Creates a new instance 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 ( ) { } /∗∗ ∗writeStatsToDisk ∗/
  • 70. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S STATISTICS IO II public static void writeStatsToDisk ( ArrayList < S t a t i s t i c s > simStats , String filename ) throw ObjectOutputStream out = null ; try { out = new ObjectOutputStream (new FileOutputStream ( filename ) ) ; out . writeObject ( ( Object ) simStats ) ; out . flush ( ) ; } f i n a l l y { i f ( out != null ) { try { out . close ( ) ; } catch ( IOException exception ) { } } } } public static ArrayList < S t a t i s t i c s > readStatsFromDisk ( String filename ) throws IOException ObjectInputStream in = null ; try { in=new ObjectInputStream (new FileInputStream ( filename ) ) ; return ( ArrayList < S t a t i s t i c s >) in . readObject ( ) ; } f i n a l l y { i f ( in != null ) { try { in . close ( ) ; } catch ( IOException e ) { } } }
  • 71. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S STATISTICS IO III } }
  • 72. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S 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 in 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 . io . ∗ ; public class Context { /∗∗ Creates a new instance of Context ∗/ public Context ( ) { this . mFunctionSet=new ArrayList ( ) ; this . mTerminalSet=new ArrayList ( ) ; this . mPrimitiveMap=new TreeMap ( ) ; this .mCache=new MyLinkedHashMap(500 , .75F , true ) ; this . previousBest=Double .MAX_VALUE;
  • 73. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S CONTEXT II } /∗∗ ∗ b r i e f Add a new p r i m i t i v e in the sets of 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 static 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 ) { assert ( ! mPrimitiveMap . 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 static void i n i t D a t a ( float [ ] [ ] X, float [ ] Y) { assert (X. length==Y. length ) ; XData=new float [X. length ] [ X [ 0 ] . length ] ; Target=new double [X. length ] ; for ( int i =0; i <X. length ; i ++){ for ( int j =0; j <X [ 0 ] . length ; j ++) XData [ i ] [ j ]=X[ i ] [ j ] ; Target [ i ]=Y[ i ] ; }
  • 74. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S CONTEXT III } /∗∗ ∗This i s used to i n i t i a l i z e the Test data ∗/ public static void initTestData ( float [ ] [ ] X, double [ ] Y) { assert (X. length==Y. length ) ; TestXData=new float [X. length ] [ X [ 0 ] . length ] ; TestTarget=new double [X. length ] ; for ( int i =0; i <X. length ; i ++){ for ( int j =0; j <X [ 0 ] . length ; j ++) TestXData [ i ] [ j ]=X[ i ] [ j ] ; TestTarget [ i ]=Y[ i ] ; } } /∗∗ ∗This function i s used to read in data patterns . To be used by the java program ∗params : ∗filename−the f i l e containing IO patterns ∗sizeX− number of patterns ∗sizeY− number of variables ( f o r input data ) ∗trainORtest− i f true , then t r a i n data , else t e s t data ∗/ public static void readDataPatterns ( int sizeX , int sizeY , boolean trainORtest ) { StreamTokenizer tok=null ; String filenameX , filenameY ; i f ( trainORtest ) { filenameX=trainDataFileX ; filenameY=trainDataFileY ;
  • 75. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S CONTEXT IV } else { filenameX=testDataFileX ; filenameY=testDataFileY ; } try { tok = new StreamTokenizer (new BufferedReader (new FileReader ( filenameX ) ) ) ; tok . ordinaryChar ( ’ n ’ ) ; / / tok . ordinaryChar ( ) } catch ( java . io . FileNotFoundException e ) { System . out . p r i n t l n ( e+" in Context . java " ) ; } int i =0; int j =0; float [ ] [ ] curDat=new float [ sizeX ] [ sizeY ] ; String s t r ; try { j =0; while ( tok . nextToken ( ) ! = StreamTokenizer .TT_EOF) { i f ( j >=44){ j =0; i ++;} switch ( tok . ttype ) { case StreamTokenizer .TT_EOL: i ++; j =0; break ; case ’ r ’ :
  • 76. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S CONTEXT V i ++; j =0; break ; case ’n ’ : i ++; j =0; break ; case StreamTokenizer .TT_NUMBER: curDat [ i ] [ j ] = ( float ) tok . nval ; / / System . out . p r i n t l n ( tok . nval ) ; / / j = j +1; / / i f ( j ==3){ / / j =0; / / i ++; / / } j ++; break ; default : / / i f ( tok . ttype != StreamTokenizer .TT_NUMBER && ( tok . sval . compareTo ( " t " ) ! = 0 / / j =0; / / i ++; / / } break ; } } } catch ( java . io . EOFException e ) { / / do nothing
  • 77. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S CONTEXT VI } catch ( java . io . IOException e ) { / / do nothing } catch ( java . lang . ArrayIndexOutOfBoundsException e ) { / / do nothing agin : ( System . out . p r i n t l n ( e+" Here l i e s the bug "+trainORtest+ 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 ( ) ; } try { tok = new StreamTokenizer (new BufferedReader (new FileReader ( filenameY ) ) ) ; tok . ordinaryChar ( ’ n ’ ) ; / / tok . ordinaryChar ( ) } catch ( java . io . FileNotFoundException e ) { System . out . p r i n t l n ( e+" in Context . java " ) ; } i =0; double [ ] DatY=new double [ sizeX ] ; try { j =0; while ( tok . nextToken ( ) ! = StreamTokenizer .TT_EOF) {
  • 78. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S CONTEXT VII switch ( tok . ttype ) { case StreamTokenizer .TT_EOL: / / i ++; break ; / / case ’ n ’ : / / i ++; / / j =0; / / break ; case StreamTokenizer .TT_NUMBER: DatY [ i ]= tok . nval ; i ++; / / System . out . p r i n t l n ( tok . nval ) ; break ; default : / / j ++; break ; } } } catch ( java . io . EOFException e ) { / / do nothing } catch ( java . io . IOException e ) { / / do nothing } catch ( java . lang . ArrayIndexOutOfBoundsException e ) { / / do nothing agin : ( System . out . p r i n t l n ( e+" Here l i e s the bug " ) ;
  • 79. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S CONTEXT VIII System . e x i t ( 0 ) ; } i f ( trainORtest ) { XData=curDat ; Target=DatY ; } else { TestXData=curDat ; TestTarget=DatY ; } / / f o r ( i n t j j =0; j j <XData . length ; j j ++){ / / XData [ j j ][1]∗=100; / / } } public static ArrayList <Primitive > mFunctionSet ; / / ! < Set of functions usable to public static ArrayList <Primitive > mTerminalSet ; / / ! < Set of terminals usable to public static TreeMap<String , Primitive > mPrimitiveMap ; / / ! < Name−p r i m i t i v e map. public static LinkedHashMap<String , Evaluation > mCache; public static float mutationProba , swapMutationProba , xoverProba , inInitGrowProba , xoverNodeDi public static int dynamicTreeDepth , popSize , initTreeDepthMin , initTreeDepthMax , tournamentSiz public static float [ ] [ ] XData ; public static double [ ] Target ; public static float [ ] [ ] TestXData ; public static double [ ] TestTarget ; public static String trainDataFileX ; public static String trainDataFileY ; public static String testDataFileX ;
  • 80. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S CONTEXT IX public static String testDataFileY ; public static boolean useTestData ; public static int length_of_run ; public static double previousBest ; / / f i t n e s s of the best i n d i v i d u a l of the previous generation } class MyLinkedHashMap extends LinkedHashMap { / / This method i s called j u s t a f t e r a new entry has been added public MyLinkedHashMap ( int val1 , float val2 , boolean val3 ) { super ( val1 , val2 , val3 ) ; } public boolean removeEldestEntry (Map. Entry eldest ) { return size ( ) > 500; } /∗ public boolean containsKey ( String key ) { I t e r a t o r <String > s t r = t h i s . keySet ( ) . i t e r a t o r ( ) ; while ( s t r . hasNext ( ) ) { i f ( s t r . next ( ) . compareTo ( key )==0){ return true ; } } return false ; } public Evaluation get ( String key ) { Evaluation 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 . entrySet ( ) . i t e r a t o r ( ) ;
  • 81. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S CONTEXT X while ( 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 oS tri ng ( ) . compareTo ( key )==0){ return ( Evaluation ) temp . getValue ( ) ; } } return n u l l ; } ∗/ }
  • 82. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES I /∗ ∗ PrimitiveHandle . java ∗ ∗ Created on August 10 , 2007, 9:02 PM ∗ ∗ To change t h i s template , choose Tools | Template Manager ∗ and open the template in 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 . bcel . 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 . io . ∗ ; 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 String name; protected int a r i t y ; protected int mRefCounter ; protected Random dice ;
  • 83. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES II /∗∗ Creates a new instance of PrimitiveHandle ∗/ public P r i m i t i v e ( ) { } /∗∗ ∗ param inNumberArguments Number of arguments 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 . ∗/ public P r i m i t i v e ( String inName , int a r i t y ) { this .name=new String ( inName ) ; this . a r i t y = a r i t y ; mRefCounter=0; } /∗∗ ∗ b r i e f Copy−construct 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 ) { this .name=new String ( copy .name ) ; this . a r i t y =copy . a r i t y ; mRefCounter=0; } /∗∗ ∗Assign the input argument ( P r i m i t i v e object ) to
  • 84. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES III ∗t h i s object ∗/ 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 ( this == p r i m i t i v e ) return this ; name = new String ( p r i m i t i v e .name ) ; this . a r i t y = p r i m i t i v e . a r i t y ; return this ; } /∗∗ ∗ b r i e f Give a reference on the actual p r i m i t i v e . ∗ param object of Random ∗ return P r i m i t i v e handle to t h i s pointer . ∗/ public abstract P r i m i t i v e giveReference (Random dice ) ; / / { return n u l l ; } /∗ { i f ( t h i s . getName ( ) . equalsIgnoreCase ( " Ephemeral " ) ) { / / should return the Ephemeral random numbe return new P r i m i t i v e ( java . lang . Double . t oS tr in g ( dice . nextDouble ()∗12 −6) ,0); } return 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 . ∗/
  • 85. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES IV public void setName ( String inName ) { this .name =new String ( inName ) ; } /∗∗ ∗return the name of t h i s p r i m i t i v e ∗/ public f i n a l String getName ( ) { return this .name; } /∗∗ ∗ b r i e f Set the number of arguments of the p r i m i t i v e . ∗ param inNumberArguments Number of arguments of the p r i m i t i v e . ∗/ public void s e t A r i t y ( int i n A r i t y ) { this . a r i t y = i n A r i t y ; } /∗ ! ∗ b r i e f Set the value of the p r i m i t i v e ( do nothing f o r basic p r i m i t i v e ) . ∗ param inValue New value to use . ∗/ / / void Puppy : : P r i m i t i v e : : setValue ( const void∗ inValue ) / / { }
  • 86. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES V /∗∗ ∗Returns the a r i t y of t h i s p r i m i t i v e ∗/ public f i n a l int g e t A r i t y ( ) { return this . a r i t y ; } /∗∗ ∗ b r i e f Get the value of the nth argument . ∗ param inN Index of the argument to get . ∗ param outResult Value of the nth argument . ∗ param ioContext Evolutionary context . ∗ XData ∗ patternNumber ∗/ public double [ ] getArgument ( int inN , ArrayList <Integer > callStack , Tree tree , f i n a l float [ ] [ ] XData ) { assert ( inN >= 0 ) ; assert ( inN < this . g e t A r i t y ( ) ) ; / / / / int lIndex = callStack . get ( callStack . size () −1). intValue ( ) + 1; for ( int i =0; i <inN ; ++ i ) lIndex += tree . get ( lIndex ) . getSubTreeSize ( ) ; callStack . add (new Integer ( lIndex ) ) ; double [ ] outResult=tree . get ( lIndex ) . g e t P r i m i t i v e ( ) . execute ( callStack , tree , XData ) ;
  • 87. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES VI callStack . remove ( callStack . size () −1); return outResult ; } public abstract P r i m i t i v e clone ( ) ; /∗∗ ∗Function i s used to execute t h i s p r i m i t i v e ∗outResult ∗callStack ∗tree to which t h i s p r i m i t i v e belongs ∗XData ∗patternNumber , that comes in from evaluateTree in Tree ∗/ / / public abstract double [ ] execute ( ArrayList <Integer > callStack , Tree tree , f l o a t [ ] [ ] XData ) ; public abstract double [ ] execute ( ArrayList <Integer > callStack , Tree tree , float [ ] [ ] XData ) ; / / / / c a l l the cache here? /∗ StringBuilder key=new StringBuilder ( tree . size ()− callStack . get ( callStack . size () −1). intVal tree . writeTree2 ( callStack . get ( callStack . size () −1). intValue ( ) , key ) ; Evaluation evaluation=PuppysMain . lContext .mCache. get ( key . to St rin g ( ) ) ; i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . only non−terminals are evaluated here i f ( evaluation != n u l l ) { / / double [ ] evaled=lContext .mCache. get ( key . t oSt ri ng ( ) ) . evaluation ; / / f o r ( i n t i =0; i <evaled . length ; i ++) / / outResult [ i ] = ( f l o a t ) evaled [ i ] ; PuppysMain . lContext .mCache. get ( key . t oS tr in g ( ) ) . cachedFlag=true ; / / the entry i s now PuppysMain . cacheHits+=tree . get ( callStack . size () −1). getSubTreeSize ( ) ; ; return evaluation . evaluation ; }
  • 88. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES VII } ∗/ / / double lArg2 ; /∗ double [ ] outResult= n u l l ; switch ( t h i s . a r i t y ) { case 2 : { / / f o r a l l the two a r i t y functions double [ ] lArg2= n u l l ; outResult=getArgument (0 , callStack , tree , XData ) ; lArg2=getArgument (1 , callStack , tree , XData ) ; i f ( t h i s . getName ( ) . compareTo ( " plus ")==0){ i f ( outResult . length==lArg2 . length ) f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ] += lArg2 [ i ] ; else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned f o r ( i n t i =lArg2 . length;−−i >=0;) lArg2 [ i ]+= outResult [ 0 ] ; outResult=lArg2 ; / / point outResult to t h i s array now } else i f ( lArg2 . length ==1){ f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ]+= lArg2 [ 0 ] ; } } else i f ( t h i s . getName ( ) . compareTo ( " minus ")==0){ i f ( outResult . length==lArg2 . length ) f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ] −= lArg2 [ i ] ; else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
  • 89. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES VIII f o r ( i n t i =lArg2 . length;−−i >=0;) lArg2 [ i ]−=outResult [ 0 ] ; outResult=lArg2 ; / / point outResult to t h i s array now } else i f ( lArg2 . length ==1){ f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ]−=lArg2 [ 0 ] ; } } else i f ( t h i s . getName ( ) . compareTo ( " times ")==0){ i f ( outResult . length==lArg2 . length ) f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ] ∗= lArg2 [ i ] ; else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned f o r ( i n t i =lArg2 . length;−−i >=0;) lArg2 [ i ]∗= outResult [ 0 ] ; outResult=lArg2 ; / / point outResult to t h i s array now } else i f ( lArg2 . length ==1){ f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ]∗= lArg2 [ 0 ] ; } } else i f ( t h i s . getName ( ) . compareTo ( " adilpower ")==0){ i f ( outResult . length==lArg2 . length ) f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ] =Math . pow( outResult [ i ] , lArg2 [ i ] ) ; else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
  • 90. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES IX f o r ( i n t i =lArg2 . length;−−i >=0;) lArg2 [ i ]=Math . pow( lArg2 [ i ] , outResult [ 0 ] ) ; outResult=lArg2 ; / / point outResult to t h i s array now } else i f ( lArg2 . length ==1){ f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ]=Math . pow( outResult [ i ] , lArg2 [ 0 ] ) ; } } else { / / divide i f ( outResult . length==lArg2 . length ) { f o r ( i n t i =outResult . length;−−i >=0;){ t r y { outResult [ i ] /= lArg2 [ i ] ; } catch ( Exception e ) { outResult [ i ]= Double .NaN; } } } else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned f o r ( i n t i =lArg2 . length;−−i >=0;){ t r y { lArg2 [ i ]+= outResult [ 0 ] ; } catch ( Exception e ) { outResult [ i ]= Double .NaN; }
  • 91. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES X } outResult=lArg2 ; / / point outResult to t h i s array now } else i f ( lArg2 . length ==1){ f o r ( i n t i =outResult . length;−−i >=0;){ t r y { outResult [ i ]+= lArg2 [ 0 ] ; } catch ( Exception e ) { outResult [ i ]= Double .NaN; } } } } break ; } case 1 : { outResult=getArgument (0 , callStack , tree , XData ) ; i f ( t h i s .name. compareTo ( " sin " ) = = 0 ) / / sin f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ] = ( Math . sin ( outResult [ i ] ) ) ; else i f ( t h i s .name. compareTo ( " cos " ) = = 0 ) / / cos f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ] = ( Math . cos ( outResult [ i ] ) ) ; else i f ( t h i s .name. compareTo ( " a d i l l o g ")==0) f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ] = ( Math . log ( outResult [ i ] ) ) ; else i f ( t h i s .name. compareTo ( " adillog10 ")==0)
  • 92. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES XI f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ] = ( Math . log10 ( outResult [ i ] ) ) ; else i f ( t h i s .name. compareTo ( " a d i l s q r t ")==0){ f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ]=Math . sqrt ( outResult [ i ] ) ; } break ; } default : { / / A terminal has been reached i f ( ! t h i s .name. startsWith ( "X " ) ) { / / double tmpVal=Double . valueOf ( t h i s .name ) . doubleValue ( ) ; / / f o r ( i n t i =0; i <outResult . length ; i ++) / / outResult [ i ]= tmpVal ; outResult=new double [ 1 ] ; outResult [0]= Double . valueOf ( t h i s .name ) . doubleValue ( ) ; } else i f ( t h i s .name. substring ( 0 , 1 ) . compareTo ( "X")==0){ / / The f o l l o w i n g l i n e would automatically assign the p e r t i n e n t entry in the pattern i n t tmp=Integer . valueOf ( t h i s .name. substring ( 1 ) ) . intValue () −1; outResult=new double [ XData . length ] ; f o r ( i n t i =outResult . length;−−i >=0;) outResult [ i ] = ( double ) XData [ i ] [ tmp ] ; / / Return the whole array of the Input data , } break ; } } /∗ PuppysMain . noCacheHits ++; i f ( evaluation== n u l l ) { / / i . e . i f the entry i s not present
  • 93. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES XII i f ( t h i s . g e t A r i t y ()==0){ lContext .mCache. put ( key . to St ri ng ( ) , new Evaluation ( ) ) ; / / c a l l the default construc / / but the actual evaluation i s done here . Aimed at saving extra array−copying } else { / / the tree has c h i l d ( ren ) . i n t lIndex = callStack . get ( callStack . size () −1). intValue ( ) + 1 ; / / index of the next boolean hasCached=true ; / / that the subtree i s cahched f o r ( i n t i =0; i < t h i s . a r i t y ; i ++){ StringBuilder subTree=new StringBuilder ( tree . size ()− lIndex ) ; tree . writeTree2 ( lIndex , subTree ) ; String s t r =new String ( subTree . t oS tr in g ( ) ) ; Evaluation evaluation2=lContext .mCache. get ( s t r ) ; t r y { i f ( evaluation2 != n u l l ) i f ( evaluation2 . cachedFlag== false ) hasCached= false ; / / System . out . p r i n t l n ( " Alarming " ) ; } catch ( Exception e ) { hasCached= false ; System . out . p r i n t l n ( "Do I reach here? "+e ) ; / / System . e x i t ( 0 ) ; } lIndex+=tree . get ( lIndex ) . getSubTreeSize ( ) ; } i f ( hasCached ) { / / That i s hasCached survived through a l l the subChildren lContext .mCache. put (new String ( key . to St ri ng ( ) ) , new Evaluation ( outResult , fals
  • 94. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES XIII } } } ∗/ / / return outResult ; / / } } /∗ ∗ 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 in 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 . ∗ ;
  • 95. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES XIV public class Ephemeral extends P r i m i t i v e { /∗∗ Creates a new instance of Ephemeral ∗/ public Ephemeral ( ) { } /∗∗ ∗ param inNumberArguments Number of arguments 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 . ∗/ public Ephemeral ( String inName , int a r i t y ) { super ( inName , a r i t y ) ; } /∗∗ ∗ b r i e f Copy−construct 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 String ( 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 reference on the actual p r i m i t i v e .
  • 96. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S PRIMITIVES XV ∗ param object of Random ∗ return P r i m i t i v e handle to t h i s pointer . ∗/ public P r i m i t i v e giveReference (Random dice ) { this .name=java . lang . Double . toS tr in g ( dice . nextDouble ()∗12 −6); return this ; } /∗∗ ∗Function i s used to execute t h i s p r i m i t i v e ∗outResult ∗callStack ∗tree to which t h i s p r i m i t i v e belongs ∗XData ∗patternNumber , that comes in from evaluateTree in Tree ∗/ public double [ ] execute ( ArrayList <Integer > callStack , Tree tree , float [ ] [ ] XData ) { / / c a l l the cache here? /∗ StringBuilder key=new StringBuilder ( tree . size ()− callStack . get ( callStack . size () −1). intVal tree . writeTree2 ( callStack . get ( callStack . size () −1). intValue ( ) , key ) ; Evaluation evaluation=PuppysMain . lContext .mCache. get ( key . to St rin g ( ) ) ; i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . only non−terminals are evaluated here i f ( evaluation != n u l l ) { / / double [ ] evaled=lContext .mCache. get ( key . t oSt ri ng ( ) ) . evaluation ; / / f o r ( i n t i =0; i <evaled . length ; i ++) / / outResult [ i ] = ( f l o a t ) evaled [ i ] ; PuppysMain . lContext .mCache. get ( key . t oS tr in g ( ) ) . cachedFlag=true ; / / the entry i s now