コード例 #1
0
        public TransportSSA(ModelInfo modelInfo, float duration, int repeats, int samples)
            : base(modelInfo, duration, repeats, samples)
        {
            Configuration config = Configuration.CurrentConfiguration;

            // Transport events
            diffusionReactions = model.Reactions.Where(r => !r.IsLocal).ToList();

            // Reaction events
            reactionReactions = model.Reactions.Where(r => r.IsLocal).ToList();

            currentReactionRates = new float[reactionReactions.Count];
            kernels        = new float[model.Species.Count][];                                // jagged array (i.e. array of arrays)
            speciesIndices = new int[model.Species.Count][];                                  // nearest neighbors of the current species
            lambda         = new int[model.Species.Count];                                    // X(t+tau) = X(t) + lambda, diffusion changes
            diffusionRates = new float[diffusionReactions.Count];
            _partiallyExecutedReactionsEvents = new List <int>();

            // Jagged array initialization
            for (int iSpecies = 0; iSpecies < model.Species.Count; ++iSpecies)
            {
                int numberOfNieghbors = NumberOfNeighbors(iSpecies) + 1;       // +1 for the current node + number of neighbors
                kernels[iSpecies]        = new float[numberOfNieghbors];
                speciesIndices[iSpecies] = new int[numberOfNieghbors];
            }

            epsilon = config.GetParameterWithDefault("tssa.epsilon", 0.01f);
            greensFunctionIterations = config.GetParameterWithDefault("tssa.greensFunctionIterations", 100);
            verbose = config.GetParameterWithDefault("tssa.verbose", false);

            _distributionSampler = RandLibSampler.CreateRandLibSampler(rng);

            // Get the transport rates
            FindDiffusionRates(ref diffusionRates);
        }
コード例 #2
0
ファイル: TauLeaping.cs プロジェクト: JSchripsema-IDM/IDM-CMS
        // ReSharper restore InconsistentNaming

        // The algorithm presented here (specifically in regards to the critical
        // versus noncritical reactions to avoid negative species) can be found in
        // 'Avoiding Negative Populations in Explicit Poisson Tau-Leaping'
        // Cao, Gillespie, Petzold.  Journal of Chemical Physics.

        public TauLeaping(ModelInfo modelInfo, float duration, int repeats, int samples, ModelBuilder modelBuilder = null)
            : base(modelInfo, duration, repeats, samples, modelBuilder)
        {
            Configuration config = Configuration.CurrentConfiguration;

            _currentRates   = new float[model.Reactions.Count];
            _jacobianMatrix = new float[model.Reactions.Count, model.Species.Count];

            _nonCriticalReactions = new List <Reaction>();
            _criticalReactions    = new List <Reaction>();

            // We'll need at most reactions.Count values in one or the other array
            _criticalRates    = new float[model.Reactions.Count];
            _noncriticalRates = new float[model.Reactions.Count];

            epsilon  = config.GetParameterWithDefault("tau-leaping.epsilon", 0.001f);
            nc       = config.GetParameterWithDefault("tau-leaping.Nc", 2);
            multiple = config.GetParameterWithDefault("tau-leaping.Multiple", 10);
            SSAruns  = config.GetParameterWithDefault("tau-leaping.SSARuns", 100);

            CheckParameters();

            _distributionSampler = RandLibSampler.CreateRandLibSampler(rng);

            Console.WriteLine("epsilon =  {0}", epsilon);
            Console.WriteLine("nc =       {0}", nc);
            Console.WriteLine("multiple = {0}", multiple);
            Console.WriteLine("SSA runs = {0}", SSAruns);
        }
コード例 #3
0
ファイル: ExitTimes.cs プロジェクト: clorton/IDM-CMS
        public ExitTimes(ModelInfo modelInfo, double duration, int repeats, int samples)
            : base(modelInfo, duration, repeats, samples, new ModelBuilder())
        {
            currentRates     = new double[model.Reactions.Count];
            lambda           = new List <double>();
            lambdaLists      = new List <List <double> >();
            exitTimes        = new List <double>();
            numberOfGammaRNs = 0.0;

            _distributionSampler = RandLibSampler.CreateRandLibSampler(rng);

            Configuration config = Configuration.CurrentConfiguration;

            _epsilon          = Math.Min(config.GetParameterWithDefault("et.epsilon", 0.5), 1.0);
            _eventName        = config.GetParameterWithDefault("et.eventName", "exitTimeEvent");
            _verbose          = config.GetParameterWithDefault("et.verbose", false);
            _testing          = config.GetParameterWithDefault("et.testing", false);
            _convergenceTest  = config.GetParameterWithDefault("et.convergence", false);
            _efficiencyCutoff = config.GetParameterWithDefault("et.efficiencyCutoff", 0.2);

            targetCondition = model.Predicates.First(s => s.Name == _eventName);

            sampleNumber  = 1;
            eventAchieved = false;

            if (_verbose)
            {
                Console.WriteLine("\n predicate: {0} with initial evaluation: {1} \n", targetCondition.Name, targetCondition.Value);
                Console.WriteLine("\n epsilon = {0} \n", _epsilon);
            }
        }
コード例 #4
0
 public BLeaping(ModelInfo modelInfo, double duration, int repeats, int samples)
     : base(modelInfo, duration, repeats, samples)
 {
     _currentRates        = new double[model.Reactions.Count];
     _tau                 = Configuration.CurrentConfiguration.GetParameterWithDefault("b-leaping.Tau", 0.1);
     _step                = 1;
     _deltaTau            = 0.0;
     _distributionSampler = RandLibSampler.CreateRandLibSampler(rng);
 }
コード例 #5
0
        public void TestGenerateExponential()
        {
            float[]             distribution = LoadDistributionProfile <float>("distlib\\distributions\\GeneratedExponential.txt");
            DistributionSampler sampler      = GetAesCounterSampler();

            int[] bins   = SampleContinuous(() => sampler.GenerateExponential(0.5f), distribution.Length);
            bool  passed = ValidateDistribution(distribution, bins, 0.025f);

            Assert.IsTrue(passed);
        }
コード例 #6
0
        public void TestBinomial80Pt5()
        {
            float[]             distribution = LoadDistributionProfile <float>("distlib\\distributions\\Binomial80Pt5.txt");
            DistributionSampler sampler      = GetAesCounterSampler();

            int[] bins   = SampleDiscrete(() => sampler.GenerateBinomial(80, 0.5f), distribution.Length);
            bool  passed = ValidateDistribution(distribution, bins, 0.01f, 24);

            Assert.IsTrue(passed);
        }
コード例 #7
0
        public void TestStandardExponential()
        {
            float[]             distribution = LoadDistributionProfile <float>("distlib\\distributions\\StandardExponential.txt");
            DistributionSampler sampler      = GetAesCounterSampler();

            int[] bins   = SampleContinuous(sampler.StandardExponential, distribution.Length);
            bool  passed = ValidateDistribution(distribution, bins, 0.01f);

            Assert.IsTrue(passed);
        }
コード例 #8
0
        public void TestGeneratedGamma3Pt5()
        {
            float[]             distribution = LoadDistributionProfile <float>("distlib\\distributions\\GeneratedGamma3Pt5.txt");
            int                 binCount     = distribution.Length;
            DistributionSampler sampler      = GetAesCounterSampler();

            int[] bins   = SampleContinuous(() => sampler.GenerateGamma(3.0f, 0.5f), binCount);
            bool  passed = ValidateDistribution(distribution, bins, 0.01f, 1);

            Assert.IsTrue(passed);
        }
コード例 #9
0
        public void TestStandardGamma14()
        {
            float[]             distribution = LoadDistributionProfile <float>("distlib\\distributions\\StandardGammaShape14.txt");
            int                 binCount     = distribution.Length;
            DistributionSampler sampler      = GetAesCounterSampler();

            int[] bins   = SampleContinuous(() => sampler.StandardGamma(14.0f), binCount);
            bool  passed = ValidateDistribution(distribution, bins, 0.015f, 39);

            Assert.IsTrue(passed);
        }
コード例 #10
0
ファイル: Normal.cs プロジェクト: JSchripsema-IDM/IDM-CMS
        public Normal(float mean, float variance)
        {
            if (variance <= 0.0f)
            {
                throw new ArgumentException("Variance must be greater than 0.");
            }

            _mean     = mean;
            _variance = variance;

            _distributionSampler = RandLibSampler.CreateRandLibSampler(RNGFactory.GetRNG());
        }
コード例 #11
0
        public void TestUniformClosedOpen()
        {
            DistributionSampler sampler = GetRandLibSampler();
            bool  zero;
            bool  one;
            float sample;
            bool  inBounds = TestUniformSampler(sampler.GenerateUniformCO, 0.0f, 1.0f, out zero, out one, out sample);

            Assert.IsTrue(inBounds, string.Format("RandLibSampler.UniformCO() failed with value {0}.", sample));
            Assert.IsTrue(zero, "RandLibSampler.UniformCO() failed to generate 0.0");
            Assert.IsFalse(one, "RandLibSampler.UniformCO() generated 1.0");
        }
コード例 #12
0
        public void UniformClosedClosedMinMax()
        {
            DistributionSampler sampler = GetRandLibSampler();
            bool  min;
            bool  max;
            float sample;
            bool  inBounds = TestUniformSampler(() => sampler.GenerateUniformCC(UniformMin, UniformMax), UniformMin, UniformMax, out min, out max, out sample);

            Assert.IsTrue(inBounds, string.Format("RandLibSampler.UniformCC(min,max) failed with value {0}.", sample));
            Assert.IsTrue(min, string.Format("RandLibSampler.UniformCC(min,max) failed to generate {0}", min));
            Assert.IsTrue(max, string.Format("RandLibSampler.UniformCC(min,max) failed to generate {0}", max));
        }
コード例 #13
0
        public void TestMultinomial4From4()
        {
            float[]             distribution = LoadDistributionProfile <float>("distlib\\distributions\\Multinomial4From4.txt");
            DistributionSampler sampler      = GetAesCounterSampler();

            const int nEvents     = 4;
            const int nCategories = 4;
            var       bins        = SampleMultinomial(nCategories, nEvents, distribution.Length, sampler);

            bool passed = ValidateDistribution(distribution, bins, 0.01f);

            Assert.IsTrue(passed);
        }
コード例 #14
0
ファイル: RLeaping.cs プロジェクト: JSchripsema-IDM/IDM-CMS
        public RLeaping(ModelInfo modelInfo, float duration, int repeats, int samples)
            : base(modelInfo, duration, repeats, samples)
        {
            Configuration config = Configuration.CurrentConfiguration;

            reactions              = new List <Reaction>(model.Reactions);
            currentRates           = new float[reactions.Count];
            jacobianMatrix         = new float[reactions.Count, model.Species.Count];
            _probabilityVector     = new float[reactions.Count];
            _executionsPerReaction = new int[reactions.Count];

            _epsilon         = config.GetParameterWithDefault("r-leaping.epsilon", 0.1f);
            _theta           = config.GetParameterWithDefault("r-leaping.theta", 0.0f);
            _sortingInterval = config.GetParameterWithDefault("r-leaping.sorting interval", 365.0f);
            _verbose         = config.GetParameterWithDefault("r-leaping.verbose", false);

            _distributionSampler = RandLibSampler.CreateRandLibSampler(rng);
        }
コード例 #15
0
ファイル: MidPoint.cs プロジェクト: JSchripsema-IDM/IDM-CMS
        public MidPoint(ModelInfo modelInfo, float duration, int repeats, int samples)
            : base(modelInfo, duration, repeats, samples, new MpModelBuilder())
        {
            Configuration config = Configuration.CurrentConfiguration;

            _distributionSampler = RandLibSampler.CreateRandLibSampler(rng);

            epsilon  = config.GetParameterWithDefault("midpoint.epsilon", 0.01f);
            nc       = config.GetParameterWithDefault("midpoint.Nc", 2);
            multiple = config.GetParameterWithDefault("midpoint.Multiple", 10);
            SSAruns  = config.GetParameterWithDefault("midpoint.SSARuns", 100);

            CheckParameters();

            Console.WriteLine("epsilon =  {0}", epsilon);
            Console.WriteLine("nc =       {0}", nc);
            Console.WriteLine("multiple = {0}", multiple);
            Console.WriteLine("SSA runs = {0}", SSAruns);
        }
コード例 #16
0
        public void TestGenerateNormal()
        {
            float[]             distribution = LoadDistributionProfile <float>("distlib\\distributions\\GeneratedNormal.txt");
            int                 binCount     = distribution.Length;
            DistributionSampler sampler      = GetAesCounterSampler();
            var                 bins         = new int[binCount];

            for (int i = 0; i < SampleCount; i++)
            {
                float sample = sampler.GenerateNormal(-2.0f, 0.5f);
                int   index  = (int)(Math.Floor(sample * 10.0f)) + 40;
                if ((index >= 0) && (index < binCount))
                {
                    bins[index]++;
                }
            }

            bool passed = ValidateDistribution(distribution, bins, 0.025f);

            Assert.IsTrue(passed);
        }
コード例 #17
0
ファイル: RLeapingFast.cs プロジェクト: clorton/IDM-CMS
        public RLeapingFast(ModelInfo modelInfo, double duration, int repeats, int samples)
            : base(modelInfo, duration, repeats, samples)
        {
            Configuration config = Configuration.CurrentConfiguration;

            reactions    = new List <Reaction>(model.Reactions);
            currentRates = new double[reactions.Count];

            _highestOrderReaction = new int[model.Species.Count];
            _muHat     = new double[model.Species.Count];
            _sigmaHat2 = new double[model.Species.Count];
            _varHat    = new double[model.Species.Count];

            _probabilityVector     = new double[reactions.Count];
            _executionsPerReaction = new int[reactions.Count];

            _epsilon         = config.GetParameterWithDefault("r-leaping.epsilon", 0.1);
            _sortingInterval = config.GetParameterWithDefault("r-leaping.sorting interval", 365.0);
            _verbose         = config.GetParameterWithDefault("r-leaping.verbose", false);

            _distributionSampler = RandLibSampler.CreateRandLibSampler(rng);
        }
コード例 #18
0
        public void TestGeneratePoisson10()
        {
            int[] distribution          = LoadDistributionProfile <int>("distlib\\distributions\\PoissonLambda10.txt");
            int   binCount              = distribution.Length;
            DistributionSampler sampler = GetAesCounterSampler();

            int[] bins = SampleDiscrete(() => sampler.GeneratePoisson(10.0f), binCount);

            bool passed = true;

            for (int i = 0; i < binCount; i++)
            {
                float error = Math.Abs(distribution[i] - bins[i]) / (float)SampleCount;
                if (error > 0.002502352f)
                {
                    passed = false;
                }
                Console.WriteLine("{0},{1}", distribution[i], bins[i]);
            }

            Assert.IsTrue(passed);
        }
コード例 #19
0
        public FractionalDiffusion(ModelInfo modelInfo, float duration, int repeats, int samples)
            : base(modelInfo, duration, repeats, samples, new ModelBuilder())
        {
            Configuration config = Configuration.CurrentConfiguration;

            #region initializeFields
            _reactions                     = new List <Reaction>(model.Reactions);
            _alpha                         = config.GetParameterWithDefault("fd.alpha", 0.5f);
            _verbose                       = config.GetParameterWithDefault("fd.verbose", false);
            _Dalpha                        = config.GetParameterWithDefault("fd.Dalpha", 1.0f);
            _constant                      = config.GetParameterWithDefault("fd.constant", 0.25f);
            _h                             = config.GetParameterWithDefault("fd.h", 1.0f);
            _truncationDistance            = config.GetParameterWithDefault("fd.truncation", (int)Math.Round(((double)(modelInfo.Locales.Count() - 1)) / 4.0));
            _fourierNumber                 = 0.5;
            _n                             = modelInfo.Locales.Count() - 1;
            _currentRates                  = new float[_reactions.Count];
            _field                         = new int[_n];
            _numberOfNonDiscretizedSpecies = modelInfo.Species.Count() / _n;
            _tau                           = (float)TimeStep(_fourierNumber, _constant, _h, _alpha, _Dalpha);
            _dispersalChanges              = new int[model.Species.Count];
            _executionsPerReaction         = new int[model.Reactions.Count];
            _distributionSampler           = RandLibSampler.CreateRandLibSampler(rng);
            #endregion

            if (_verbose == true)
            {
                Console.WriteLine("number of locales = {0}", modelInfo.Locales.Count() - 1);
                Console.WriteLine("number of species = {0}", _numberOfNonDiscretizedSpecies);
                Console.WriteLine("truncation = {0}", _truncationDistance);
                Console.WriteLine("alpha = {0}", _alpha);
                Console.WriteLine("D_alpha = {0}", _Dalpha);
                Console.WriteLine("Fouier number (1d) = {0}", _fourierNumber);
                Console.WriteLine("h = {0}", _h);
                Console.WriteLine("C = {0}", _constant);
                Console.WriteLine("fractional diffusion tau = {0}", _tau);
            }
        }
コード例 #20
0
ファイル: SampleSpace4.cs プロジェクト: PotaninMarat/droid
 public SampleSpace4(string unused = null)
 {
     this._space = Space4.ZeroOne;
     this._distribution_sampler = new DistributionSampler();
 }
コード例 #21
0
        public void RandLibToStringTest()
        {
            DistributionSampler sampler = GetRandLibSampler();

            Assert.AreEqual("RandLib Distribution Sampler (RandLib Random Variate Generator (RandLib PRNG))", sampler.ToString());
        }
コード例 #22
0
        public void AesCounterToStringTest()
        {
            DistributionSampler sampler = GetAesCounterSampler();

            Assert.AreEqual("RandLib Distribution Sampler (AesCounter Random Variate Generator (AesCounter PRNG))", sampler.ToString());
        }
コード例 #23
0
        private static int[] SampleMultinomial(int nCategories, int nEvents, int binCount, DistributionSampler sampler)
        {
            int nTotal            = nCategories * (nCategories + 1) / 2;
            var probabilityVector = new float[nCategories];

            for (int i = 0; i < nCategories; i++)
            {
                probabilityVector[i] = (float)(i + 1) / nTotal;
            }

            var bins   = new int[binCount];
            var events = new int[nCategories];

            for (int i = 0; i < SampleCount; i++)
            {
                sampler.GenerateMultinomial(nEvents, probabilityVector, events);
                int index = 0;
                for (int j = nCategories - 1; j >= 0; j--)
                {
                    index *= (nEvents + 1);
                    index += events[j];
                }
                bins[index]++;
            }

            return(bins);
        }
コード例 #24
0
        private DistributionSampler GetAesCounterSampler()
        {
            DistributionSampler sampler = RandLibSampler.CreateRandLibSampler(AesCounterVariateGenerator.CreateAesCounterVariateGenerator(new uint[] { 20130110, 19680201 }));

            return(sampler);
        }