Results 1 to 6 of 6
  1. #1
    fabricio is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Doubt about backpropagation with genetic algorithm in Java

    Well, I have developed a code with those two artificial inteligence techinics. During my job, I realized that backpropagation was working very good, but just alone, and when I tried to conect with GA, it was not working, because the weight was rising.

    So, I'm sending the code, and I would like to get any help to solve this problem. Indeed if someone tell me what is going on, I would be thankfull.

    Well, I attached my code, and I'm waiting for any tip!
    Attached Files Attached Files

  2. #2
    fabricio is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Main
    Java Code:
    public class Main {
        public static void main(String args[]){
        	
        	
            CGeneticNet nnuga = new CGeneticNet();
            simplenet sn = nnuga.Run();
            
                    
            if(sn != null){
                System.out.println("Solution found!!");
                System.out.println("0 xor 0 = " + sn.rodar(0,0,0));
                System.out.println("0 xor 1 = " + sn.rodar(0,1,1));
                System.out.println("1 xor 0 = " + sn.rodar(1,0,1));
                System.out.println("1 xor 1 = " + sn.rodar(1,1,0));
    	} else {
                System.out.println("Solution not found!!");
    	}
            
            
        }
    }
    Backpropagation
    Java Code:
    public class simplenet {
    	
    	public double m_fPesos[][] = new double[3][3];
    	
    	public static void main(String args[]){
    		simplenet sn = new simplenet();
    		
    		sn.iniciaMatriz();
    		
    		sn.SaidaMatrizPesos();
    	}
    	
    		
    		public void SaidaMatrizPesos(){
    			for (int i=0;i<3;i++) {
    				for (int j=0;j<3;j++) {
    					System.out.print(m_fPesos[i][j] + "  ");
    				}
    				System.out.println();
    			}
    		}
    		
    		
    		public void iniciaMatriz(){
    			for (int i=0;i<3;i++) {
    				for (int j=0;j<3;j++) {
    					// Número aleatório entre -3 and 3.
    					m_fPesos[i][j] = ((Math.random())/(32767/6) - 3);
    				}
    				
    			}	
    		}
    
    				
    		//Método executar/rodar
    		public double rodar(double i1,double i2,double d){
    			// Essas são as principais variáveis usadas na  
    			// rotina. 
    			double net1, net2, i3, i4;
    			
    			// Calcular o calor dos net para as camadas escondidas dos neurônios
    			net1 = 1 * m_fPesos[0][0] + i1 * m_fPesos[1][0] +
    				  i2 * m_fPesos[2][0];
    			net2 = 1 * m_fPesos[0][1] + i1 * m_fPesos[1][1] +
    				  i2 * m_fPesos[2][1];
    
    			// Use the hardlimiter function - the Sigmoid.
    			i3 = sigmoid(net1);
    			i4 = sigmoid(net2);
    
    			// Now, calculate the net for the final output layer.
    			net1 = 1 * m_fPesos[0][2] + i3 * m_fPesos[1][2] +
    			   	  i4 * m_fPesos[2][2];
    			
    			//System.out.println("Net > "+net1);
    			
    			return sigmoid(net1);
    		}
    	
    
    		//Treinamento
    		public double treinamento(double i1, double i2, double d){
    			// Função de treinamento retorna o erro
    			double var = (double) (Math.abs(rodar(i1,i2,d) - d));
    			//System.out.println("Erro BackPropagation > " + var);
    			return var;
    		}
    
    		public double sigmoid(double num) {
    			return (double)(1/(1+Math.exp(-num)));
    		}
    
    		public void alterarPesos(double montante){
    			SaidaMatrizPesos();
    			//System.out.println("Saída após modificação dos Pesos");
    			//System.out.println("Montante > "+ montante);
    			for (int i=0;i<3;i++) {
    				for (int j=0;j<3;j++) {
    					m_fPesos[i][j] += (double)(Math.random())/(32767/(montante * 2)) - montante;
    					
    				}
    				System.out.println("");
    			}
    			System.out.println("");
    		}
    		
    		//Pega Pesos
    		public void pegaPesos(double vetorPesos[]){
    			int z = 0;
    			for (int i=0;i<3;i++) {
    				for (int j=0;j<3;j++) {
    					vetorPesos[z] = m_fPesos[i][j];
    					z++;
    				}
    			}			
    		}
    		
    		public void configPesos(double vetorPesos[]) {
    			int z = 0;
    			for (int i=0;i<3;i++) {
    				for (int j=0;j<3;j++) {
    					m_fPesos[i][j] = vetorPesos[z];
    					z++;
    				}
    			}
    	}
    	
    	}
    GA
    Java Code:
    
    public class CGeneticNet{
        private int CGN_POPULATION = 150;
        private double CGN_THRESHOLD = 0.001;
        private int CGN_MAXITER = 1000;
        private simplenet[] m_pcPopulation;
        private double m_fErrors[];
        
        public CGeneticNet(){
            m_pcPopulation = new simplenet[CGN_POPULATION];
            m_fErrors = new double[CGN_POPULATION];
    
            for(int i=0;i < CGN_POPULATION; i++){
                // Weights automatically initialized 
                // in neural network constructor.
                m_pcPopulation[i] = new simplenet();
            }
        }
        
        public simplenet Run(){
            double error = 0;
            
            for(int iter=0;iter < CGN_MAXITER; iter++){
                for (int i=0;i<CGN_POPULATION;i++) {
                    error = ((m_pcPopulation[i].treinamento(0,0,0) + m_pcPopulation[i].treinamento(0,1,1) + m_pcPopulation[i].treinamento(1,0,1) + m_pcPopulation[i].treinamento(1,1,0)) / 4);
                    
                    System.out.println("Erro AG = " + error);
                    if(error < CGN_THRESHOLD)
                    {
                        System.out.println("Venci");
                        return m_pcPopulation[i];
                    }
                    m_fErrors[i] = error;
                }
                
                SortFitnesses();
                NewPopulation();
    	}
    
    	return null;
        }
    
        void SortFitnesses(){
            int n = CGN_POPULATION;
    	int disorder = n;
    
    	while(disorder != 0){
    		
    			disorder = 0;
              
    			for(int i = 1; i < n; i++){
                
    				if(m_fErrors[i] < m_fErrors[i-1]){
                        double m_fTemp = m_fErrors[i-1];
    
                        m_fErrors[i-1] = m_fErrors[i];
                        m_fErrors[i] = m_fTemp;
    
                        simplenet tempnn = m_pcPopulation[i-1];
    
                        m_pcPopulation[i-1] = m_pcPopulation[i];
                        m_pcPopulation[i] = tempnn;
    
                        disorder++;
    
                    }
                }
                
                n--;
                
                
    
    	}
        }
    
        void NewPopulation(){
    	int id1, id2;
    
    	double wp1[] = new double[9];
    	double wp2[] = new double[9];
    
    	for (int i = 0; i < CGN_POPULATION/2; i++){
                id1 = (int) (Math.random() % CGN_POPULATION / 2);
                id2 = (int) (Math.random() % CGN_POPULATION / 2 + CGN_POPULATION / 2);
    
                m_pcPopulation[id1].pegaPesos(wp1);
                m_pcPopulation[id2].pegaPesos(wp2);
    
                for (int j = 0; j < 2; j++){
                    double temp;
    
                    temp = wp1[j+6];
                    wp1[j+6] = wp2[j+6];
                    wp2[j+6] = temp;
                }
    
                if(Math.random()%10 < 2){
                    for (int k = 0; k < 3; k++){
                        for (int j=0;j<3;j++) {
                                m_pcPopulation[id1].alterarPesos((double)(Math.random())/(32767/2) - 1);
                                m_pcPopulation[id2].alterarPesos((double)(Math.random())/(32767/2) - 1);
                                
                        }
                    }
                }
    
                m_pcPopulation[id2].configPesos(wp1);
            }
        }
    }

  3. #3
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I was a bit curious so I tried to run and understand your code, but you didn't make it easy:
    First a compliment: you did send a SSCCE.
    But I also want to grumble a bit: if you explain more you make it far more easy for potential helpers:
    - It is not easy to understand the Portuguese. I would at least provide a list of translations.
    - It is confusing that there is a file "Retro" in the zip that isn't in the listing: you made me compare all the files to see where the differences are.
    - Some remarks about the coding:
    ClassNames should start with a capital( simplenet ).
    Methodnames should start with a lower-case letter( SaidaMatrizPesos() ).
    - It is not wrong but very confusing and unnecessary i.m.h.o. to let a method work by changing the argument (pegaPesos).

    Maybe you can help your helpers a bit by explaining the algorithms, and where they differ from your expectations. I start to see it a little, but it requires a lot of reverse-engineering: it would be polite to make understanding easier.
    I must go to sleep now, maybe until later.
    Last edited by Jodokus; 05-14-2011 at 01:38 AM. Reason: Confusing Portuguese for Spanish

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    To start: it would be nice if you gave some reaction, also if you expect something different. For me at least you don't have to provide the translations any more, I deciphered them, but if you expect help from others then Portuguese members and me it would still be a good idea.

    Now the Code, my opinion for what it is worth:
    I have been looking at it again. I understand better now what is going on in your code, and it is funny in a way. I think you cheated a bit in a smart way (I'm not picking on you anymore, I'm talking code now): Backpropagation is a technique to correct weights in a neural network (NN). You don't do that but you just try to select the best performing NN with a Genetic algorithm (GA).

    To start: I don't trust the lines like this (there are more in your code):

    // Número aleatório entre -3 and 3.
    m_fPesos[i][j] = ((Math.random())/(32767/6) - 3);
    m_fPesos[i][j] += (double)(Math.random())/(32767/(montante * 2)) - montante;

    At first: what is the 32767. I suspect 2^16 divided by two, but you are working with doubles... And the outcome of the first line is clustering very tight around -3 (not random between -3 and 3), the second around -montante. I don't think that's right.

    id1 = (int) (Math.random() % CGN_POPULATION / 2);
    id2 = (int) (Math.random() % CGN_POPULATION / 2 + CGN_POPULATION / 2);
    These lines are probably wrong: the outcomes are resp. 0 and half the populationsize


    Waiting for a reaction now.
    Last edited by Jodokus; 05-15-2011 at 12:54 AM. Reason: spelling

  5. #5
    fabricio is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Thank you

    Hey, sorry because I got late to answer!

    Well, I solved my problem, and now I have a program working, with you like I can send you by email.

    Best regards!

    Quote Originally Posted by Jodokus View Post
    To start: it would be nice if you gave some reaction, also if you expect something different. For me at least you don't have to provide the translations any more, I deciphered them, but if you expect help from others then Portuguese members and me it would still be a good idea.

    Now the Code, my opinion for what it is worth:
    I have been looking at it again. I understand better now what is going on in your code, and it is funny in a way. I think you cheated a bit in a smart way (I'm not picking on you anymore, I'm talking code now): Backpropagation is a technique to correct weights in a neural network (NN). You don't do that but you just try to select the best performing NN with a Genetic algorithm (GA).

    To start: I don't trust the lines like this (there are more in your code):






    At first: what is the 32767. I suspect 2^16 divided by two, but you are working with doubles... And the outcome of the first line is clustering very tight around -3 (not random between -3 and 3), the second around -montante. I don't think that's right.


    These lines are probably wrong: the outcomes are resp. 0 and half the populationsize


    Waiting for a reaction now.

  6. #6
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I tried to reply yesterday, but it vanished into thin air when I tried to post it. I then tried to say that I'm happy that you're still alive, and that I don't really need the code, but thanks.
    When there is something interesting in it, just give a short description of what it does, how, and how it performs or meets your expectations, just to meet our curiousity.
    I liked the idea of a compact combination of a small Neural Network and selection.
    No bug ever had to calculate its fitnessfunction.

Similar Threads

  1. Genetic Algorithm Data Structure Dillema
    By otacon in forum Advanced Java
    Replies: 5
    Last Post: 05-07-2011, 11:50 PM
  2. JGAP genetic algorithms for Java
    By Iskatel in forum Advanced Java
    Replies: 10
    Last Post: 11-04-2010, 04:26 PM
  3. Q about Genetic programming
    By m00nchile in forum New To Java
    Replies: 8
    Last Post: 05-12-2010, 11:23 AM
  4. genetic algorithm
    By rpsaranya in forum New To Java
    Replies: 1
    Last Post: 03-05-2010, 07:30 AM
  5. genetic algorithms
    By nurfadhillah in forum Advanced Java
    Replies: 1
    Last Post: 10-19-2008, 04:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •