1. Introduction Nursery Main Nursery Genotype Summary References
EVOLUTIONARY NURSEY
THE SOURCE CODE
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 27, 2015
2. Introduction Nursery Main Nursery Genotype Summary References
OUTLINE I
1 INTRODUCTION
2 NURSERY MAIN
3 NURSERY
4 GENOTYPE
5 SUMMARY
6 REFERENCES
3. Introduction Nursery Main Nursery Genotype Summary References
INTRODUCTION
Evolutionary nursery is an implementation of a simple
genetic algorithm (GA).
The algorithm is implemented in Java.
The algorithm is aimed at numerical optimization problems.
The source code has three main Java files.
1 NurseryMain.java: The main entry point to the program that
runs the genetic algorithm.
2 Nursery.java: This class implements the algorithms that are
necessary to run the GA, such as crossover and mutation.
3 Genotype.java: The data-type Genotype specifies the
implementation of the GA genotype.
In what follows you will find the source code of various
components of evolutionary nursery.
4. Introduction Nursery Main Nursery Genotype Summary References
NURSERY MAIN I
/∗
∗ NurseryMain . java
∗
∗ Created on September 3 , 2007, 8:52 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 EvoNursery ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import java . u t i l . ∗ ;
public class NurseryMain {
/∗∗ Creates a new instance of NurseryMain ∗/
public NurseryMain ( ) {
}
/∗∗
∗ @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
double Fitness =90;
double [ ] bestGene=new double [ 2 ] ;
5. Introduction Nursery Main Nursery Genotype Summary References
NURSERY MAIN II
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 (300 , 2 , −6, +6); / / should i n i t i a l i z e
int numGens=15;
while ( true ) {
for ( int i =0; i <numGens; i ++){
ArrayList <Genotype> childPop=nurse . applySelectionTournament ( pop , 4 ) ;
nurse . applySinglePointCrossover ( childPop , 2 , 0 . 8 ) ;
nurse . applyMutationUniform ( childPop , 2 , 0 . 2 ) ;
pop . addAll ( childPop ) ; / / append the two pops here
for ( int j =0; j <pop . size ( ) ; j ++){
double [ ] tmpGenes=pop . get ( j ) . getGenotype ( ) ;
double f i t n e s s =10∗2+tmpGenes [0]∗ tmpGenes[0]−10∗ java . lang . Math . cos(2∗3.141∗tmpG
+tmpGenes [1]∗ tmpGenes[1]−10∗ java . lang . Math . cos(2∗3.141∗tmpGenes [ 1 ] ) ;
pop . get ( j ) . setFitness ( f i t n e s s ) ;
i f ( fitness <Fitness ) {
Fitness= f i t n e s s ;
bestGene [0]= tmpGenes [ 0 ] ;
bestGene [1]= tmpGenes [ 1 ] ;
}
nurse . applySurvival ( pop ) ;
}
}
System . out . p r i n t l n ( Fitness+" "+bestGene [0]+ " "+bestGene [ 1 ] ) ;
}
}
}
6. Introduction Nursery Main Nursery Genotype Summary References
NURSERY I
/∗
∗ Nursery . java
∗
∗ Created on September 3 , 2007, 5:35 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗t h i s i s the f i l e where evolution would occur
∗/
package EvoNursery ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import java . u t i l . ∗ ;
public class Nursery {
/∗∗
∗
∗ Creates a new instance of Nursery
∗ I n i t i a l i z e s a population
∗ popSize−population size
∗ numCoeffs−− to be tuned
∗ upperBound −− The upper bound of a number
∗ lower Bound −− The lower bound of a coeff
∗/
7. Introduction Nursery Main Nursery Genotype Summary References
NURSERY II
private Random dice ;
private ArrayList <Integer > lMateVector ;
private double span ;
private double lowerBound ;
private int parentPopSize ;
public Nursery ( ) {
dice=new Random ( ) ; / /We need a Random number generator
this . lMateVector=new ArrayList ( ) ;
}
/∗∗
∗ I n i t i a l i z e a population
∗/
public ArrayList <Genotype> i n i t i a l i z e P o p u l a t i o n ( int popSize , int numCoeffs , double upperBo
ArrayList <Genotype> population=new ArrayList ( popSize ) ; / / creates an empty l i s t with t h i
/ / The elements would be added manually
this . span=upperBound−lowerBound ;
this . lowerBound=lowerBound ;
this . parentPopSize=popSize ;
double [ ] tmpGenotype=new double [ numCoeffs ] ;
for ( int j =0; j <popSize ; j ++){
for ( int i =0; i <numCoeffs ; i ++){
tmpGenotype [ i ]= dice . nextDouble ()∗ span+lowerBound ;
}
population . add (new Genotype ( tmpGenotype ) ) ;
8. Introduction Nursery Main Nursery Genotype Summary References
NURSERY III
}
return population ;
}
/∗∗
∗Puppy Style tournamentSelection
∗/
public ArrayList <Genotype> applySelectionTournament ( ArrayList <Genotype> ioPopulation , int inN
{
i f ( ioPopulation . size ( ) == 0) return null ;
ArrayList <Genotype> childPop=new ArrayList ( ioPopulation . size ( ) ) ;
/ / 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 ) . getFitness () < ioPopulation . get ( tmpCand1 ) . getFitness
tmpCand1=lChosenCand1 ; / / choose an i n d i v i d u a l ,
}
}
else
tmpCand1=lChosenCand1 ; / / choose t h i s
j j ++;
}
9. Introduction Nursery Main Nursery Genotype Summary References
NURSERY IV
childPop . add (new Genotype ( 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 ( lChosenCand2 ) . getFitness () < ioPopulation . get ( tmpCand2 ) . getFitness
{
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 Genotype ( ioPopulation . get ( tmpCand2 ) ) ) ; / / Add t h i s to the c h i l d population
}
return childPop ;
}
/∗∗
∗Apply single−point crossover
∗/
public void applySinglePointCrossover ( ArrayList <Genotype> ioPopulation , int genomeLength , dou
i f ( ( ioPopulation . size ( ) % 2) != 0) ioPopulation . remove ( lMateVector . size () −1);
10. Introduction Nursery Main Nursery Genotype Summary References
NURSERY V
for ( int j =0; j <ioPopulation . size ( ) ; j +=2) {
double [ ] tmpParent1=ioPopulation . get ( j ) . getGenotype ( ) ;
double [ ] tmpParent2=ioPopulation . get ( j +1). getGenotype ( ) ;
double swapGene ;
int XoverPoint=dice . nextInt ( genomeLength ) ;
i f ( genomeLength==2 && XoverPoint ==0) XoverPoint =1; / / The special case when genome length i s
for ( int i =0; i <XoverPoint ; i ++){
swapGene=tmpParent1 [ i ] ;
tmpParent1 [ i ]= tmpParent2 [ i ] ;
tmpParent2 [ i ]=swapGene ; / / xover i s no big deal
}
ioPopulation . get ( j ) . setEvaluated ( false ) ;
ioPopulation . get ( j +1). setEvaluated ( false ) ;
}
}
/∗∗
∗Applies standard mutation to the population . The numbers are drawn from a uniform d i s t r i b u t a
∗/
public void applyMutationUniform ( ArrayList <Genotype> ioPopulation , int genomeLength , double mu
I t e r a t o r <Genotype> genoIt=ioPopulation . i t e r a t o r ( ) ;
while ( genoIt . hasNext ( ) ) {
boolean f l a g =false ;
Genotype genome=genoIt . next ( ) ;
double [ ] tmpParent=genome . getGenotype ( ) ;
11. Introduction Nursery Main Nursery Genotype Summary References
NURSERY VI
for ( int i =0; i <genomeLength ; i ++){
i f ( dice . nextDouble () <= mutationProb ) {
tmpParent [ i ]= dice . nextDouble ()∗ this . span+this . lowerBound ;
f l a g =true ;
}
}
i f ( f l a g )
genome . setEvaluated ( false ) ;
}
}
/∗∗
∗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 applySurvival ( ArrayList <Genotype> ioPopulation ) {
Collections . sort ( ioPopulation , new FitnessComparator ( ) ) ; / / Sort the pop wrt f i t n e s s f i r s t
/ / Collections . sort ( ioPopulation , new DepthComparator ( ) ) / / Sort wrt f i t n e s s −− I guess no ne
while ( ioPopulation . size ( ) ! = this . parentPopSize )
ioPopulation . remove ( this . parentPopSize ) ;
}
}
12. Introduction Nursery Main Nursery Genotype Summary References
NURSERY VII
/∗∗
∗Fitness Comparator
∗/
class FitnessComparator implements java . u t i l . Comparator<Genotype >{
public int compare ( Genotype o1 , Genotype o2 ) {
double f i t 1 =o1 . getFitness ( ) ;
double f i t 2 =o2 . getFitness ( ) ;
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;
}
}
13. Introduction Nursery Main Nursery Genotype Summary References
GENOTYPE I
/∗
∗ Genotype . java
∗
∗ Created on September 3 , 2007, 5:19 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 EvoNursery ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
public class Genotype {
private double [ ] genes ;
private double f i t n e s s ;
private boolean evaluated ;
/∗∗
∗ Creates a new instance of Genotype
∗Sets the genotype to input genotypes
∗/
public Genotype ( double [ ] inGenes ) {
genes=new double [ inGenes . length ] ;
for ( int i =0; i <inGenes . length ; i ++){
this . genes [ i ]= inGenes [ i ] ;
}
14. Introduction Nursery Main Nursery Genotype Summary References
GENOTYPE II
this . f i t n e s s =Double .MAX_VALUE; / / set i t to a high value
this . evaluated=false ;
}
/∗∗
∗Copy Constructor
∗/
public Genotype ( Genotype copy ) {
double [ ] tmpGenes=copy . getGenotype ( ) ;
this . genes=new double [ tmpGenes . length ] ;
for ( int i =0; i <tmpGenes . length ; i ++){
this . genes [ i ]=tmpGenes [ i ] ;
}
this . f i t n e s s =copy . getFitness ( ) ; / / set i t to a high value
this . evaluated=copy . evaluated ;
}
/∗∗
∗setGenotype
∗/
public void setGenotype ( f i n a l double [ ] inGenes ) {
for ( int i =0; i <inGenes . length ; i ++){
this . genes [ i ]= inGenes [ i ] ;
}
}
/∗∗
∗Get the genotype array
∗/
15. Introduction Nursery Main Nursery Genotype Summary References
GENOTYPE III
public f i n a l double [ ] getGenotype ( ) {
return this . genes ;
}
/∗∗
∗set the f i t n e s s of the genotype
∗/
public void setFitness ( f i n a l double inFitness ) {
this . f i t n e s s =inFitness ;
}
/∗∗
∗get f i t n e s s of t h i s Genotype
∗/
public f i n a l double getFitness ( ) {
return this . f i t n e s s ;
}
/∗∗
∗Set the evaualted f l a g
∗/
public void setEvaluated ( f i n a l boolean eval ) {
this . evaluated=eval ;
}
/∗∗
∗Get the evaluation f l a g ’ s value
∗/
public f i n a l boolean getEvaluatedFlag ( ) {
return this . evaluated ;
}
17. Introduction Nursery Main Nursery Genotype Summary References
SUMMARY
Evolutionary implements a simple genetic algorithm in
Java.
Implementation was focused at efficiency, ease of use and
simplicity.
It is open source and can be found online.
18. Introduction Nursery Main Nursery Genotype Summary References
REFERENCES
Evolutionary nursery can be found online.
This presentation is developed with Beamer:
Darmstadt, monarca.