Ejemplo 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);
            }
        }
Ejemplo n.º 2
0
        public void PrintConfidenceIntervals(string fileName)
        {
            ConfidenceIntervalStats.Compute( );
            ConfidenceIntervalStats.PrintToCsv("ConfidenceIntervals.csv");

            var properties = new[]
            {
                "Quality",
                "Variance Quality",
                "Covariance Quality",
                "Reproduction",
                "Variance Reproduction",
                "Covariance Reproduction"
            };

            var ci = "\n\n\nHeritability Confidence Intervals:\n\n";

            for (var i = 0; i < properties.Length; i++)
            {
                var property = properties[i];
                var interval = Utility.CalculateConfidenceInterval(
                    HeritabilitySummaries.Select(x => x.Values[i]).ToList( ),
                    V.Z);
                ci += $"{property,-33}: {interval}\n";
            }

            var lines = ci.Split('\n').Select(x => x.TrimEnd( ));

            File.WriteAllLines(fileName, lines);
        }
Ejemplo n.º 3
0
        public virtual void Run( )
        {
            var options = ProgressBarOptions.Default;

            options.EnableTaskBarProgress = true;

            using (var pbar = new ProgressBar(V.Runs, "Simulating", options))
            {
                ParallelEnumerable
                .Range(0, V.Runs)
                .ForAll(i =>
                {
                    var iteration = new TIteration( );
                    iteration.Init(V, StrategyCollection);
                    iteration.Run( );

                    lock ( SyncLock )
                    {
                        ++Wins[iteration.Winner];
                        ++TimeStepsCount[iteration.TimeStepsPassed];
                        ConfidenceIntervalStats?.AddRun(iteration.GenerationHistory);
                    }

                    if (iteration.TimeStepsPassed > 2)
                    {
                        HeritabilitySummaries.Add(iteration.Heritability);
                    }

                    // ReSharper disable once AccessToDisposedClosure
                    pbar.Tick( );
                }
                        );
            }

            var clean = HeritabilitySummaries
                        .Where(x => !x.Values.Any(double.IsNaN))
                        .ToList( );

            for (var i = 0; i < HeritabilityMean.ValueCount; i++)
            {
                var index = i;
                (HeritabilityMean.Values[index], HeritabilitySd.Values[index]) =
                    clean.Select(x => x.Values[index]).MeanStandardDeviation( );
            }

            PrintHeritabilitySummaries("Heritability.csv");
            if (V.IncludeConfidenceIntervals)
            {
                PrintConfidenceIntervals("ConfidenceIntervals.txt");
            }
        }