Example #1
0
void Animal::evolve(NeuralNetwork *bestNN)
// Modify the neuronal network of the animal closer to the best one
{
    NeuralNetwork* NN = evolveNN();
    std::vector<NeuronLayer> layers = NN->getL();
    std::vector<NeuronLayer> bestLayers = bestNN->getL();
    const unsigned int layersNum=layers.size();
    //for each layer
    for (unsigned int layer=0; layer<layersNum; layer++)
    {
        std::vector<Neuron> neurons = layers.at(layer).getN();
        std::vector<Neuron> bestNeurons = bestLayers.at(layer).getN();
        const unsigned int neuronsNum = neurons.size();

        //for each neuron
        for (unsigned int neuron=0; neuron<neuronsNum; neuron++)
        {
            const std::vector<double> weights = neurons.at(neuron).getWeights();
            const std::vector<double> bestWeights = bestNeurons.at(neuron).getWeights();
            std::vector<double> newWeights;
            //for each weight
            for(unsigned int w=0; w<weights.size(); w++)
            {   newWeights.push_back(weights.at(neuron)+(bestWeights.at(neuron)-weights.at(neuron))*0.1);
            }
            neurons.at(neuron).setWeights(newWeights);
        }
    }
}