package uk.ac.leeds.ccg.projects.MedAction.NeuralNetwork; public class GeneticLearning implements java.io.Serializable{ //private NeuroNetwork[] bestTwoNN=new NeuroNetwork[2]; private NeuralNetwork tempNN = null; int mutationRate = 0; double[][] childrenWeight = null; double[][] parentWeight = null; private int childNumber = 0; private int singleParentNumber = 0; //training data x and y double[][] x = null; double[][] y = null; double[][] randomPopulation = null; //Vector moreWeight=new Vector(); /** Creates a new instance of GeneticLearning */ public GeneticLearning(int singleParentNumber, int childNumber, int mutationRate,int firstTimeRandom, NeuralNetwork temp,double[][] x,double[][] y) throws Exception{ this.mutationRate = mutationRate; setChildNumber(childNumber); setParentNumber(singleParentNumber); this.x = x; this.y = y; //do a big random in this first time //randomPopulation = randomPopulation( firstTimeRandom, temp.getWeightsForGeneticLearning().length ); randomPopulation = getRandomPopulation( firstTimeRandom, temp.getWeights() ); tempNN = temp; System.out.println( firstTimeRandom + " random population done" ); //selectionTwo(); //bestNN=selection(singleParentNumber,this.totalW); parentWeight = selection( singleParentNumber, randomPopulation ); childrenWeight = new double[singleParentNumber*childNumber][]; } /** * After construct the Learning Object, this method will run the algorithm once and * return the best Neural Network in this round. */ public NeuralNetwork runOnce() throws Exception{ crossOver(); mutationWithCrossOveredChildren(); //mutationWithParentPopulation(); selectAndReplace(); double[][] theBest = selection( 1, parentWeight ); this.tempNN.setWeightByGeneticLearning( theBest[ 0 ] ); double[][] fire = tempNN.runNN( x ); System.out.println( NNUtility.getRMS( fire, y ) ); return this.tempNN; } public void crossOver() { double[][] parent = getParentWeight(); int parentNum = parent.length; int childNum = getChildNumber(); for ( int i = 0; i < parentNum; i ++ ) { for ( int j = 0; j < childNum; j ++ ) { setChildrenWeight( crossOverOnce( parent[ i ], parent[ randomMother( parentNum, i ) ] ), i * childNum + j ); } } System.out.println( " Cross Over Done " ); } private int randomMother( int total, int fatherIndex ) { int motherIndex = 0; do { motherIndex = new Double( Math.random() * total ).intValue(); } while ( motherIndex == fatherIndex ); return motherIndex; } private double[] crossOverOnce( double[] parent1, double[] parent2) { int crossOverPosition = new Double( Math.random() * parent1.length ).intValue(); double[] offSpring = new double[ parent1.length ]; for ( int i = 0; i < crossOverPosition; i ++ ) { offSpring[ i ] = parent1[ i ]; } for ( int i = crossOverPosition; i < parent2.length; i ++ ) { offSpring[ i ] = parent2[ i ]; } return offSpring; } private void mutationWithCrossOveredChildren() throws Exception{ double[][] children = this.getChildrenWeight(); for ( int i = 0; i < children.length; i ++ ) { // get a copy of a child array, in order to prevent confusing array reference double[] temp = new double[ children[ 0 ].length ]; for ( int j = 0; j < temp.length; j ++ ) { temp[ j ] = children[ i ][ j ]; } this.setChildrenWeight( mutation( temp, this.mutationRate ), i ); } System.out.println("Genetic Learning Mutation Done"); } public void setParentNumber(int parentNum){ this.singleParentNumber=parentNum; } private double[] getRandomWeightArray(int length){ double[] temp = new double[length]; for(int i=0;i tempRMS) this.setParentWeight(temp[0],i); } System.out.println("best child selection from chirdren population done"); } /* private NeuroNetwork[] selection(int num,double[][] inTotalWeight){ double temp=tempNN.RMS; double lastBest=0.0; NeuroNetwork[] bestNN=new NeuroNetwork[num]; int bestPosition=0; double[][] totalW=inTotalWeight; double[] rms=new double[totalW.length]; // get all the fire using all weights array for(int i=0;irms[j])&&(rms[j]>lastBest)) {temp=rms[j];bestPosition=j;} } bestNN[i]=new NeuroNetwork(layerNumber,neuroNumber,rate,alfa,x,y); bestNN[i].setWeightByGeneticLearning(totalW[bestPosition]); bestNN[i].getFire(); lastBest=bestNN[i].RMS; System.out.println("best selection from population " + i +" done"); System.out.println(System.currentTimeMillis()); } return bestNN; } */ private double[][] selection( int num, double[][][] inTotalWeight ) throws Exception{ double[][] fire = tempNN.runNN( x ); double temp=NNUtility.getRMS(fire,y); double[][] bestW=new double[num][]; double[][] totalW=inTotalWeight; double[] rms=new double[totalW.length]; int lastBestPosition=0; int thisBestPosition=0; // get all the fire using all weights array for(int i=0;irms[lastBestPosition])) {temp = rms[j];thisBestPosition=j;} } bestW[i]=this.getNewArray(totalW[thisBestPosition]); lastBestPosition=thisBestPosition; System.out.println("best selection from population " + i +" done"); } return bestW; } private double[] mutation(double[] in,int mutationRate){ double[] temp = new double[in.length];// for return for(int i=0;i