public double Evaluate( IChromosome chromosome )
{
DoubleArrayChromosome daChromosome = (DoubleArrayChromosome) chromosome;
double[] chromosomeGenes = daChromosome.Value;
// total number of weight in neural network
int totalNumberOfWeights = 0;
// asign new weights and thresholds to network from the given chromosome
for ( int i = 0, layersCount = network.Layers.Length; i < layersCount; i++ )
{
Layer layer = network.Layers[i];
for ( int j = 0; j < layer.Neurons.Length; j++ )
{
ActivationNeuron neuron = layer.Neurons[j] as ActivationNeuron;
for ( int k = 0; k < neuron.Weights.Length; k++ )
{
neuron.Weights[k] = chromosomeGenes[totalNumberOfWeights++];
}
neuron.Threshold = chromosomeGenes[totalNumberOfWeights++];
}
}
// post check if all values are processed and lenght of chromosome
// is equal to network size
Debug.Assert( totalNumberOfWeights == daChromosome.Length );
double totalError = 0;
for ( int i = 0, inputVectorsAmount = input.Length; i < inputVectorsAmount; i++ )
{
double[] computedOutput = network.Compute( input[i] );
for ( int j = 0, outputLength = output[0].Length; j < outputLength; j++ )
{
double error = output[i][j] - computedOutput[j];
totalError += error * error;
}
}
if ( totalError > 0 )
return 1.0 / totalError;
// zero error means the best fitness
return double.MaxValue;
}
}