예제 #1
0
        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);
            }
        }
예제 #2
0
        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();
        }
예제 #3
0
        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)));
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }