/// <summary> /// Factory to reproduce generation /// </summary> /// <param name="parameters">Generation parameters</param> /// <param name="evaluation">Generation evaluation. Create via <see cref="Evaluate"/></param> /// <param name="generationNumber"></param> /// <returns></returns> public static Generation Reproduce(GenerationParameters parameters, GenerationEvaluation evaluation, int generationNumber) { var gen = new Generation(generationNumber, parameters.instancesCount); for (var index = 0; index < gen.Instances.Length; index++) { var instance = gen.Instances[index]; if (index < parameters.preservedCount) { instance.NN.genes = evaluation.pool.Best().Genes; instance.NN.UpdateFromGenes(); } else if (index < parameters.preservedCount + parameters.newRandomCount) { instance.NN.SetRandomValues(-1, 1); } else { parameters.crossoverFunction.Crossover(instance.NN, evaluation, parameters.selectionFunction, parameters.fitnessPower); parameters.mutationFunction.Mutate(instance.NN, parameters.mutationProbability, parameters.mutationFactor); instance.NN.UpdateFromGenes(); } instance.dieOnCollision = parameters.dieOneCollision; instance.fitnessFunction = parameters.fitnessFunction; } return(gen); }
private void LogData(GenerationEvaluation evaluation, int generationNumber) { var max = evaluation.FitnessMaximum; var min = evaluation.FitnessMinimum; var avg = evaluation.FitnessAverage; var med = evaluation.FitnessMedian; using (var sw = File.AppendText($"{Application.dataPath}/{dataFilePath}")) sw.WriteLine($"{generationNumber}: {max}, {min}, {avg}, {med}"); using (var sw = File.AppendText($"{Application.dataPath}/{fitnessesFilePath}")) sw.WriteLine($"{generationNumber}: {string.Join(", ", evaluation.Fitnesses)}"); }