| ||||||||||||||
| ||||||||||||||
|
||||||||||||||
|
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 BreakdownThe 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.
|
|
|||||||||||||
All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -