I'm trying to solve XOR problem using neural network. For training I'm using genetic algorithm.
But after a certain number of generations (200), the error stuck in 1. And the outputs are correct except for 1 xor 0 where the output is 0 instead of 1 I don't understand why this is happening.
population size : 100
crossover rate : 70
mutation rate : 5
elite number : 2
activation function : sigmoid
selection method : Tournament Selection with 7 participant
Mutation algorithm =
for (int i=0; i< individual.getNbrOfWeights(); i++)
if (random(0,100) < mutationRate)
{
genome[i] = genome[i] + random(-0.1,0.1);
}
Fitness calculation =
double error = 0;
error = error + feedForward({0, 1}, 1);
error = error + feedForward({1, 0}, 1);
error = error + feedForward({1, 1}, 0);
error = error + feedForward({0, 0}, 0);
fitness = error;
where the error is the target-output
I tried in mutation to set weights in range of [-2 2], but it get even worst (the error stuck in 1.6 ). So I don't now if it is obligated to set weight in a certain range...
I really need your help, thanks in advance.
EDIT
In fact the problem was in weight initialization and mutation method.
When I set weights between [-1 1], the algorithm does not converge. But the more I enlarge the range, the more it gives better results, say between [-4 4].
for the mutation, I tried two methods (mutate one gene chosen randomly):
--> Add a random perturbation between [-0.1 0.1], with 5% mutation rate. This way, I got the best results (network outputs like desired ones) at generation 1800
--> Change the value of the gene with a new one. The new value should belong to the range. In this case, I had to put the mutation rate to 50% for that the algorithm could converge. And weights had to be between at least -7 and 7, otherwise it won't converge.