Exemplo n.º 1
0
        public Simulation(Variables v,
                          IStrategyCollection strategyCollection)
        {
            V = v;
            StrategyCollection = strategyCollection;

            Wins = new Dictionary <Winner, int>
            {
                [Winner.Cooperator] = 0,
                [Winner.Defector]   = 0,
                [Winner.Cooperator | Winner.Fix] = 0,
                [Winner.Defector | Winner.Fix]   = 0,
                [Winner.Tie] = 0
            };
            TimeStepsCount = Enumerable
                             .Range(0, V.MaxTimeSteps + 1)
                             .ToDictionary(x => x, x => 0);

            HeritabilitySummaries = new List <HeritabilitySummary> (V.Runs);
            HeritabilityMean      = new HeritabilitySummary( );
            HeritabilitySd        = new HeritabilitySummary( );

            if (V.IncludeConfidenceIntervals)
            {
                ConfidenceIntervalStats = new ConfidenceIntervalStats(V.MaxTimeSteps, V.Runs, V.Z);
            }
        }
Exemplo n.º 2
0
        public override void CalculateHeritability( )
        {
            if (GenerationsPassed <= 2)
            {
                return;
            }

            var groups = History
                         .GroupBy(x => x.parent)
                         .Select(x => (parent: x.Key, offsprings: x.Select(y => y.offspring).ToList( )))
                         .Where(x => x.offsprings.Any( ))
                         .ToList( );

            var covPhenoQuality = groups.Select(x => x.parent.PhenotypicQuality)
                                  .PopulationCovariance(groups.Select(x => x.offsprings.Average(y => y.PhenotypicQuality)));
            var covGeneticQuality = groups.Select(x => x.parent.GeneticQuality)
                                    .PopulationCovariance(groups.Select(x => x.offsprings.Average(y => y.GeneticQuality)));

            var varPhenoQuality = groups
                                  .Select(x => x.parent.PhenotypicQuality)
                                  .PopulationVariance( );
            var varGeneticQuality = groups
                                    .Select(x => x.parent.GeneticQuality)
                                    .PopulationVariance( );

            var pairs = History.Take(History.Count - V.PopulationSize);

            groups = pairs
                     .GroupBy(x => x.parent)
                     .Select(x => (parent: x.Key, offsprings: x.Select(y => y.offspring).ToList( )))
                     .Where(x => x.offsprings.Any( ))
                     .ToList( );
            var covReproduction = groups.Select(x => (double)x.parent.OffspringCount)
                                  .PopulationCovariance(groups.Select(x => x.offsprings.Average(y => y.OffspringCount)));
            var varReproduction = groups
                                  .Select(x => x.parent.PhenotypicQuality)
                                  .PopulationVariance( );

            Heritability = new HeritabilitySummary
            {
                PhenotypicQuality           = covPhenoQuality / varPhenoQuality,
                VariancePhenotypicQuality   = varPhenoQuality,
                CovariancePhenotypicQuality = covPhenoQuality,
                GeneticQuality           = covGeneticQuality / varGeneticQuality,
                VarianceGeneticQuality   = varGeneticQuality,
                CovarianceGeneticQuality = covGeneticQuality,
                Reproduction             = covReproduction / varReproduction,
                VarianceReproduction     = varReproduction,
                CovarianceReproduction   = covReproduction
            };

            if (IsLoggingEnabled)
            {
                Logger.Debug("\n\nHeritability Calculations: \n");
                var generations = History
                                  .GroupBy(x => x.parent)
                                  .Select(x => (parent: x.Key,
                                                offsprings: x.Select(y => y.offspring)
                                                .ToList( )))
                                  .Batch(V.PopulationSize)
                                  .Select(x => x.ToList( ))
                                  .ToList( );
                for (var i = 0; i < generations.Count; i++)
                {
                    var generation = generations[i];
                    Logger.Debug($"\n\nGen#{i + 1}\n");
                    foreach (var(parent, offsprings) in generation)
                    {
                        Logger.Debug($"{parent} -> {offsprings.Count} Offsprings:");
                        foreach (var offspring in offsprings)
                        {
                            Logger.Debug($"\t{offspring} OffspringCount: {offspring.OffspringCount}");
                        }
                    }
                }

                Logger.Debug($"\n{Heritability}");
            }
        }