void GeneticAlgorithm::mutate(Genotype& genotype) {

    // Generate a dummy genotype with which to test mutations with.
    Genotype dummyGenotype = genotype;
    do { // Whilst the mutated genome are acceptable to the user...

        // Generate a dummy genotype with which to test mutations with.
        dummyGenotype = genotype;

        // Apply mutations to each gene in the dummy genome.
        for (int gene = 0; gene < dummyGenotype.myGenes.size(); gene++) {

            // Generate prototype mutations until one fits into [0, 1].
            double newValue = 0;
            do {
                newValue = dummyGenotype.getGene(gene) + myRand.randNorm(0.0,
                           myMutationVariance);
            } while ((newValue < 0) || (newValue > 1));

            // Apply the mutation.
            dummyGenotype.setGene(gene, newValue);
        }
    } while(myGenomeChecker.checkGenomeIsValid(dummyGenotype) == false);
    // The genome is valid, so overwrite the original genotype.
    genotype = dummyGenotype;
}
void GeneticAlgorithm::mutate ( )
{
	int transfer;
	int first;
	double x;
	vector<int>::iterator geneIterator;


	for (int i = 0; i < population.size(); i++ )
	{
		transfer = 0;
		Genotype g;
		g.setGene(*population[i].getGene());
		for (geneIterator = g.getGene()->begin(); geneIterator != g.getGene()->end(); ++geneIterator)
		{

			x = rand ( ) % 1000 / 1000.0;
			if ( x < PMUTATION )
			{
				++transfer;

				if ( transfer % 2 == 1 )
					first = distance(g.getGene()->begin(),geneIterator);

				else
				{
					if(g.getGene()->at(first) < *geneIterator)
					{
						++g.getGene()->at(first);
						--*geneIterator;
					}
					else if(g.getGene()->at(first) > *geneIterator)
					{
						--g.getGene()->at(first);
						++*geneIterator;
					}
					else
					{
						++g.getGene()->at(first);
						++*geneIterator;
					}
				}
			}
		}

		verifyAndPush(g);

	}

	return;
}
void TanCTRNNetwork::reconstructNetwork(Genotype& genotype) {

	//  Initialise the parameters according to the genotype.
	int gene = 0;

	/* Reconstruct the weights between the neurons.
	 * Note these GENE NUMBERS are expected to be at the begining of the genome by 
	 * writeWeightsToGenome.
	 */
	for (int dest = 0; dest < myNoNeurons; dest++) {
		for (int source = 0; source < myNoNeurons; source++) {
			// Note inputs now have incoming weights. Is this good?
			myWeights[dest * myNoNeurons + source] = genotype.myGenes.at(gene) * myMaxWeight * 2 - myMaxWeight;
			gene++;
		}
	}

	// If requested, on first construction remap the inital weights to an alternative
	// bounds.
	if((genotype.myHasEverBeenConstructed == false) &&
	   (abs(myInitialWeightModifier - myMaxWeight) > 0.000001)) {
		
		for (int dest = 0; dest < myNoNeurons; dest++) {
			for (int source = 0; source < myNoNeurons; source++) {
				myWeights[dest * myNoNeurons + source] = myWeights[dest * myNoNeurons + source]
				                                         * (myInitialWeightModifier / myMaxWeight);
			}
		}
		// And write these weights back into the genenome.
		writeWeightsToGenotype(genotype);
	}
	
	// If requested, impose a feedforward structure.
	if(myNetworkIsFeedforward) {
		for (int dest = 0; dest < myNoNeurons; dest++) {
			for (int source = 0; source < myNoNeurons; source++) {
				int destLayer, sourceLayer;
				if(dest < myNoInputs) { destLayer = 0; }
				else if(dest < myNoInputs + myFFLayer1Size) { destLayer = 1; }
				else if(dest < myNoInputs + myFFLayer1Size + myFFLayer2Size) { destLayer = 2; }
				else { destLayer = 3; }
				if(source < myNoInputs) { sourceLayer = 0; }
				else if(source < myNoInputs + myFFLayer1Size) { sourceLayer = 1; }
				else if(source < myNoInputs + myFFLayer1Size + myFFLayer2Size) { sourceLayer = 2; }
				else { sourceLayer = 3; }
				
				if(sourceLayer != (destLayer - 1)) {
					myWeights[dest * myNoNeurons + source] = 0.0;
				}
			}
		}
	}
	
	// Sensor weights.
	mySensorWeight = genotype.myGenes.at(gene) * myMaxWeight * 2 - myMaxWeight;
	gene++;

	// Output weights and biases (if required).
	if(myUseOutputWeightsAndBiases) {
		for(int o = 0; o < myNoOutputs; o++) {
			myOutputWeights[o] = genotype.myGenes.at(gene) * myMaxWeight * 2 - myMaxWeight;
			gene++;
			myOutputBiases[o] = genotype.myGenes.at(gene) * myMaxWeight * 2 - myMaxWeight;
			gene++;
		}
	}

	/* If the population should be seeded with centre-crossing networks, and the network has never been constructed
	 * before, ALTER THE GENES so the bias genes are centre crossing.
	 */
	for (int i = 0; i < myNoNeurons; i++) {
		if (mySeedWithCentreCrossing && genotype.myHasEverBeenConstructed == false) {
			genotype.setGene(gene, 0.5); // XXX IS THIS RIGHT FOR CENTRE CROSSING IN TANH???
		}
		
		myBiases[i] = genotype.myGenes.at(gene) * myMaxWeight * 2 - myMaxWeight;
		gene++;

		// Set time constants, scaled exponentially to [exp(0) exp(5)]
		myTimeConstants[i] = exp(genotype.myGenes.at(gene) * 5);
		gene++;
		
		// If requested, actually set the time constants to 1.
		if(mySetTausToOne) { myTimeConstants[i] = 1.0; }
		
		// If requested, actually set the biases to 0.
		if(mySetBiasesToZero) { myBiases[i] = 0.0; }
	}
	
	// Record in the genotype that it has been reconstructed.
	genotype.myHasEverBeenConstructed = true;
}