public void WriteToStream_Should_Write_All_Population_DNA_To_Given_Stream() { int individuals = 5; int genesEach = 20; MockRandomEnvironment.Setup(m => m.GetNextDouble(0, It.IsAny <double>())).Returns(0.1f); PopulationManager populationManagerTested = GetPopulationManager(); populationManagerTested.GeneratePopulation(individuals, genesEach); double fitness = 0; populationManagerTested.Population.ForEach(dna => dna.Fitness = fitness++); MemoryStream stream = new MemoryStream(); populationManagerTested.WriteToStream(stream); stream.Position = 0; using (StreamReader sr = new StreamReader(stream)) { int currentIndividual = 0; do { string line = sr.ReadLine(); string expectedLine = $"{populationManagerTested.Population[currentIndividual].Fitness}:{populationManagerTested.Population[currentIndividual].ToString()}"; line.Should().Be(expectedLine); currentIndividual++; }while (currentIndividual < individuals); } }
private App(MySqlConnection dbConn, Dictionary <string, string> cities) { //1. init the populationManager with connection to the database populationManager = new PopulationManager(cities); //2. Generate the first population population = populationManager.GeneratePopulation(); }
public void GeneratePopulation_Should_Create_Given_Number_Of_Individuals_With_Given_Number_Of_Genes_Each_Between_One_And_Minus_One() { int individuals = 10; int genesEach = 200; PopulationManager populationManagerTested = GetPopulationManager(); populationManagerTested.GeneratePopulation(individuals, genesEach); populationManagerTested.Population.Should().HaveCount(individuals); populationManagerTested.Population.ForEach(p => p.Genes.Should().HaveCount(genesEach)); populationManagerTested.Population.ForEach(p => p.Genes.ForEach(g => g.Should().BeInRange(-1, 1))); }
public void NextGeneration_Should_Set_Fitness_Of_Population_To_Zero() { int individuals = 5; int genesEach = 20; MockRandomEnvironment.Setup(m => m.GetNextDouble(0, It.IsAny <double>())).Returns(0.1f); PopulationManager populationManagerTested = GetPopulationManager(); populationManagerTested.GeneratePopulation(individuals, genesEach); double fitness = 0; populationManagerTested.Population.ForEach(dna => dna.Fitness = fitness++); populationManagerTested.NextGeneration(); populationManagerTested.Population.Select(i => i.Fitness).Should().OnlyContain(i => i == 0.0f); }
public void NextGeneration_Should_Complete_Population_To_Reach_Same_Size_By_Combining_Best() { int individuals = 5; int genesEach = 20; MockRandomEnvironment.Setup(m => m.GetNextDouble(0, It.IsAny <double>())).Returns(0.1f); PopulationManager populationManagerTested = GetPopulationManager(); populationManagerTested.GeneratePopulation(individuals, genesEach); double fitness = 0; populationManagerTested.Population.ForEach(dna => dna.Fitness = fitness++); populationManagerTested.NextGeneration(); populationManagerTested.Population.Should().HaveCount(individuals); }
public void NextGeneration_Should_Copy_At_Least_The_Individual_With_Best_Fitness() { int individuals = 5; int genesEach = 20; PopulationManager populationManagerTested = GetPopulationManager(); populationManagerTested.GeneratePopulation(individuals, genesEach); double fitness = 0; populationManagerTested.Population.ForEach(dna => dna.Fitness = fitness++); var oldPopulation = populationManagerTested.Population.ToList(); var best = oldPopulation.OrderByDescending(dna => dna.Fitness).First(); populationManagerTested.NextGeneration(); populationManagerTested.Population.Should().Contain(best); }
private static PopulationManager GeneratePopulation(int inputCount, int outputCount, int depth, int hiddenNeuronsPerLayer) { PopulationManager pop = new PopulationManager(new RandomEnvironment()); if (File.Exists(PopulationFileToLoad)) { FileStream stream = new FileStream(PopulationFileToLoad, FileMode.Open); pop.LoadFromStream(stream); Console.WriteLine($"Loaded from file {PopulationFileToLoad}"); } else { Console.WriteLine($"File {PopulationFileToLoad} not found, generating population at random"); pop.GeneratePopulation(30, hiddenNeuronsPerLayer * (inputCount + depth * hiddenNeuronsPerLayer + outputCount)); } return(pop); }