예제 #1
0
        public void CreateStartPopulation()
        {
            // clear existing population
            Genomes.Clear();

            //Parallel.For(0, _settings.PopulationSize, (i) =>
            for (int i = 0; i < _settings.PopulationSize; i++)
            {
                var genome = new Genome <TMainModel, TCombinationModel>(_settings);

                // Make sure all genomes are initially different
                int initPasses = 0;
                do
                {
                    genome.Initialize();
                    initPasses++;
                    lock (Genomes)
                    {
                        if (!Genomes.AsParallel().Any(g => g.GetUniqueGenesString().Equals(genome.GetUniqueGenesString())))
                        {
                            break; // we are all good
                        }
                        if (initPasses > 10)
                        {
                            throw new InvalidOperationException("No more combinations possible, reduce population size!");
                        }
                    }
                } while (true);


                lock (Genomes)
                {
                    Genomes.Add(genome);
                }
                //Debug.WriteLineIf(initPasses>1, $"# Re-initialized because of duplicate genome ({initPasses})");
            }
            ;

            //reset all variables
            Generation        = 0;
            TotalFitnessScore = 0;
        }