At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search
Home » Articles » Neural Networks » Applications/Code

XORGA

This is a simple example program that shows how to use Genetic Algorithms to optimize a neural network - in this case, the weights are evolved to solve the XOR problem. This program accompanies the Using Genetic Algorithms with Neural Networks essay.

Project Breakdown

The project is broken down into two main classes, the genetic algorithm class and the neural network class. The neural network class is basically a simplified (and slightly customized) version of the CBPNet class. The genetic algorithm class evolves the networks, and controls the problem at hand (as in the XOR problem is hard-coded in the GA - although it is easy to modify/recompile for other problems).

The classes are as follows:

class CSimpleNet {
	public:
		CSimpleNet();
		~CSimpleNet();

		void	AlterWeights(float);
		float	Train(float, float, float);
		float	Run(float, float, float);

		// Set/retrieve the weights
		void	SetWeights(float *);
		void	GetWeights(float *);

	protected:
		float m_fWeights[3][3];

		inline float Sigmoid(float);
};
The major differences between CBPNet and CSimpleNet are the Train function - in CSimpleNet it simply returns the difference between the output of the network and the desired output, in CBPNet, back-propagation is performed on the network, and the output is returned.

The genetic algorithm class is similarly simple:

class CGeneticNet {
	public:
		CGeneticNet();
		~CGeneticNet();
	
		CSimpleNet *Run();

	protected:
		float		m_fErrors[CGN_POPULATION];

		CSimpleNet	*m_pcPopulation[CGN_POPULATION];	

		void	NewPopulation();
		void	SetWeights(int);
		void	SortFitnesses();
};
The important functions you should look at are Run() and NewPopulation(). The XOR problem is coded in the Run() function in the following line:
error = ((m_pcPopulation[i]->Train(0,0,0) +
          m_pcPopulation[i]->Train(0,1,1) +
          m_pcPopulation[i]->Train(1,0,1) +
          m_pcPopulation[i]->Train(1,1,0)) / 4.0f);
Anyway, you can download the code from here. Have fun.

Submitted: 31/05/2000

Article content copyright © James Matthews, 2000.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)
- NeuroEvolving Robotic Operatives (NERO) (25/06/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -