public override void Iteration()
{
iteration++;
IList<NEATGenome> newPop = new List<NEATGenome>();
int numSpawnedSoFar = 0;
foreach (ISpecies s in Population.Species)
{
if (numSpawnedSoFar < Population.Size())
{
var numToSpawn = (int) Math.Round(s.NumToSpawn);
bool bChosenBestYet = false;
while ((numToSpawn--) > 0)
{
NEATGenome baby = null;
if (!bChosenBestYet)
{
baby = (NEATGenome) s.Leader;
bChosenBestYet = true;
}
else
{
// if the number of individuals in this species is only
// one
// then we can only perform mutation
if (s.Members.Count == 1)
{
// spawn a child
baby = new NEATGenome((NEATGenome) s.ChooseParent());
}
else
{
var g1 = (NEATGenome) s.ChooseParent();
if (ThreadSafeRandom.NextDouble() < paramCrossoverRate)
{
var g2 = (NEATGenome) s.ChooseParent();
int numAttempts = 5;
while ((g1.GenomeID == g2.GenomeID)
&& ((numAttempts--) > 0))
{
g2 = (NEATGenome) s.ChooseParent();
}
if (g1.GenomeID != g2.GenomeID)
{
baby = Crossover(g1, g2);
}
}
else
{
baby = new NEATGenome(g1);
}
}
if (baby != null)
{
baby.GenomeID = Population.AssignGenomeID();
if (baby.Neurons.Size() < paramMaxPermittedNeurons)
{
baby.AddNeuron(paramChanceAddNode,
paramNumTrysToFindOldLink);
}
// now there's the chance a link may be added
baby.AddLink(paramChanceAddLink,
paramChanceAddRecurrentLink,
paramNumTrysToFindLoopedLink,
paramNumAddLinkAttempts);
// mutate the weights
baby.MutateWeights(paramMutationRate,
paramProbabilityWeightReplaced,
paramMaxWeightPerturbation);
baby.MutateActivationResponse(
paramActivationMutationRate,
paramMaxActivationPerturbation);
}
}
if (baby != null)
{
// sort the baby's genes by their innovation numbers
baby.SortGenes();
// add to new pop
// if (newPop.contains(baby)) {
// throw new EncogError("readd");
// }
newPop.Add(baby);
++numSpawnedSoFar;
if (numSpawnedSoFar == Population.Size())
{
numToSpawn = 0;
}
}
}
}
}
while (newPop.Count < Population.Size())
{
newPop.Add(TournamentSelection(Population.Size()/5));
}
Population.Clear();
foreach (NEATGenome genome in newPop)
{
Population.Add(genome);
}
ResetAndKill();
SortAndRecord();
SpeciateAndCalculateSpawnLevels();
}
NEATTraining::Iteration ( int count ) : void |
private void trainNetworkBackprop() { // IMLTrain train = new Backpropagation(this.network, this.input,this.ideal, 0.000001, 0.1); IMLDataSet aset = new BasicMLDataSet(input, ideal); int epoch = 1; // train the neural network ICalculateScore score = new TrainingSetScore(aset); IMLTrain trainAlt = new NeuralSimulatedAnnealing(network, score, 10, 2, 100); IMLTrain trainMain = new Backpropagation(network, aset, 0.001, 0.0); StopTrainingStrategy stop = new StopTrainingStrategy(); var pop = new NEATPopulation(INPUT_SIZE, OUTPUT_SIZE, 1000); // train the neural network var step = new ActivationStep(); step.Center = 0.5; pop.OutputActivationFunction = step; var train = new NEATTraining(score, pop); trainMain.AddStrategy(new Greedy()); trainMain.AddStrategy(new HybridStrategy(trainAlt)); trainMain.AddStrategy(stop); trainMain.AddStrategy(new HybridStrategy(train)); network.ClearContext(); while (!stop.ShouldStop()) { trainMain.Iteration(); train.Iteration(); Console.WriteLine(@"Training " + @"Epoch #" + epoch + @" Error:" + trainMain.Error+ @" Genetic iteration:"+trainAlt.IterationNumber+ @"neat iteration:"+train.IterationNumber ); epoch++; } }