public void ExponentialDistribution(double lambda)
        {
            var n = new ExponentialDistribution(lambda);
            var t = 0.0;

            foreach (var x in Enumerable.Range(0, RepetitionCount).Select(i => n.NextDouble()))
            {
                t += x;
                BoundRecorder.Observe(x);
                UnboundRecorder.Observe(x);
            }
            ApproxEquals(RepetitionCount, BoundRecorder.Count);
            ApproxEquals(RepetitionCount, UnboundRecorder.Count);
            ApproxEquals(t, BoundRecorder.Total());
            ApproxEquals(t, UnboundRecorder.Total());
            ApproxEquals(n.Mean, BoundRecorder.Mean());
            ApproxEquals(n.Mean, UnboundRecorder.Mean());
            ApproxEquals(n.Variance, BoundRecorder.Variance());
            ApproxEquals(n.Variance, UnboundRecorder.Variance());
            ApproxEquals(Math.Sqrt(n.Variance), BoundRecorder.StdDev());
            ApproxEquals(Math.Sqrt(n.Variance), UnboundRecorder.StdDev());
        }
        public void ExponentialDistribution_WithUniformTimes(double lambda)
        {
            var n = new ExponentialDistribution(lambda);
            var t = 0.0;

            foreach (var i in Enumerable.Range(1, RepetitionCount))
            {
                var x = n.NextDouble();
                t += x;
                BoundRecorder.Observe(x, i);
                UnboundRecorder.Observe(x, i);
            }
            ApproxEquals(RepetitionCount, BoundRecorder.Count);
            ApproxEquals(RepetitionCount, UnboundRecorder.Count);
            ApproxEquals(t, BoundRecorder.Total());
            ApproxEquals(t, UnboundRecorder.Total());
            ApproxEquals(n.Mean, BoundRecorder.TimeMean(RepetitionCount + 1));
            ApproxEquals(n.Mean, UnboundRecorder.TimeMean(RepetitionCount + 1));
            ApproxEquals(n.Variance, BoundRecorder.TimeVariance(RepetitionCount + 1));
            ApproxEquals(n.Variance, UnboundRecorder.TimeVariance(RepetitionCount + 1));
            ApproxEquals(Math.Sqrt(n.Variance), BoundRecorder.TimeStdDev(RepetitionCount + 1));
            ApproxEquals(Math.Sqrt(n.Variance), UnboundRecorder.TimeStdDev(RepetitionCount + 1));
        }
        //---------------------------------------------------------------------
        ///<summary>
        /// Run the Biomass Insects extension at a particular timestep.
        ///</summary>
        public override void Run()
        {

            running = true;
            UI.WriteLine("   Processing landscape for Biomass Insect events ...");

            foreach(IInsect insect in manyInsect)
            {

                if(insect.MortalityYear == Model.Core.CurrentTime)
                    Outbreak.Mortality(insect);

                // Copy the data from current to last
                foreach (ActiveSite site in Model.Core.Landscape)
                    insect.LastYearDefoliation[site] = insect.ThisYearDefoliation[site];

                insect.ThisYearDefoliation.ActiveSiteValues = 0.0;
                //SiteVars.BiomassRemoved.ActiveSiteValues = 0;

                insect.ActiveOutbreak = false;
                insect.SingleOutbreakYear = false;

                NormalDistribution randVar = new NormalDistribution(RandomNumberGenerator.Singleton);
                randVar.Mu = 0;      // mean
                randVar.Sigma = 1;   // std dev
                double randomNum = randVar.NextDouble();

                ExponentialDistribution randVarE = new ExponentialDistribution(RandomNumberGenerator.Singleton);
                randVarE.Lambda = insect.MeanDuration;      // rate
                double randomNumE = randVarE.NextDouble();

                // First, has enough time passed since the last outbreak?
                double timeBetweenOutbreaks = insect.MeanTimeBetweenOutbreaks + (insect.StdDevTimeBetweenOutbreaks * randomNum);
                //double duration = insect.MeanDuration + (insect.StdDevDuration * randomNum);
                double duration = Math.Round(randomNumE + 1);
                
                if (duration > 5) // Limit maximum outbreak duration to 5 years for now.
                    duration = duration - 3;

                double timeAfterDuration = timeBetweenOutbreaks - duration;

                //UI.WriteLine("Calculated time between = {0}.  inputMeanTime={1}, inputStdTime={2}.", timeBetweenOutbreaks, insect.MeanTimeBetweenOutbreaks, insect.StdDevTimeBetweenOutbreaks);
                //UI.WriteLine("Calculated duration     = {0}.  inputMeanDura={1}, inputStdDura={2}.", duration, insect.MeanDuration, insect.StdDevDuration);
                //UI.WriteLine("Insect Start Time = {0}, Stop Time = {1}.", insect.OutbreakStartYear, insect.OutbreakStopYear);


                if(Model.Core.CurrentTime == 1)
                {
                    //UI.WriteLine("   Year 1:  Setting initial start and stop times.");
                    insect.OutbreakStartYear = (int) (timeBetweenOutbreaks / 2.0) + 1;
                    insect.OutbreakStopYear  = insect.OutbreakStartYear + (int) duration - 1;
                    UI.WriteLine("   {0} is not active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, Model.Core.CurrentTime);
                }
                else if(insect.OutbreakStartYear <= Model.Core.CurrentTime
                    && insect.OutbreakStopYear > Model.Core.CurrentTime)
                {
                    //UI.WriteLine("   An outbreak starts or continues.  Start and stop time do not change.");
                    insect.ActiveOutbreak = true;
                    UI.WriteLine("   {0} is active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, Model.Core.CurrentTime);

                    insect.MortalityYear = Model.Core.CurrentTime + 1;

                }
                //Special case for single year outbreak.
                else if(insect.OutbreakStartYear <= Model.Core.CurrentTime
                    && insect.OutbreakStopYear <= Model.Core.CurrentTime)
                {
                    insect.ActiveOutbreak = true;
                    UI.WriteLine("   {0} is active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, Model.Core.CurrentTime);

                    if (insect.OutbreakStartYear == insect.OutbreakStopYear)
                        insect.SingleOutbreakYear = true;
                    insect.MortalityYear = Model.Core.CurrentTime + 1;
                    insect.OutbreakStartYear = Model.Core.CurrentTime + (int)timeBetweenOutbreaks;
                    insect.OutbreakStopYear = insect.OutbreakStartYear + (int)duration - 1;
                }

                else if(insect.OutbreakStopYear <= Model.Core.CurrentTime
                    && timeAfterDuration > Model.Core.CurrentTime - insect.OutbreakStopYear)
                {
                    //UI.WriteLine("   In between outbreaks, reset start and stop times.");
                    insect.ActiveOutbreak = true;
                    UI.WriteLine("   {0} is active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, Model.Core.CurrentTime);

                    insect.MortalityYear = Model.Core.CurrentTime + 1;
                    insect.OutbreakStartYear = Model.Core.CurrentTime + (int) timeBetweenOutbreaks;
                    insect.OutbreakStopYear = insect.OutbreakStartYear + (int) duration - 1;
                }
                //UI.WriteLine("  Insect Start Time = {0}, Stop Time = {1}.", insect.OutbreakStartYear, insect.OutbreakStopYear);

                if(insect.ActiveOutbreak)
                {
                    //UI.WriteLine("   OutbreakStartYear={0}.", insect.OutbreakStartYear);

                    if(insect.OutbreakStartYear == Model.Core.CurrentTime || insect.SingleOutbreakYear)
                        // Initialize neighborhoodGrowthReduction with patches
                        Outbreak.InitializeDefoliationPatches(insect);
                    else
                        insect.NeighborhoodDefoliation.ActiveSiteValues = 0;

                    double sumDefoliation = 0.0;
                    int numSites = 0;
                    //foreach(ActiveSite site in Model.Core.Landscape)
                    //{
                    //    sumDefoliation += insect.ThisYearDefoliation[site];
                    //    if(insect.ThisYearDefoliation[site] > 0.0)
                    //        numSites++;

                    //}
                    double meanDefoliation = sumDefoliation / (double) numSites;


                    log.Write("{0},{1},{2},{3},{4},{5}",
                        Model.Core.CurrentTime,
                        insect.Name,
                        insect.OutbreakStartYear,
                        insect.OutbreakStopYear,
                        sumDefoliation,
                        numSites
                        );

                    //foreach (IEcoregion ecoregion in Ecoregions.Dataset)
                    //    log.Write(",{0}", 1);

                    log.WriteLine("");

                }


                //Only write maps if an outbreak is active.
                //if (!insect.ActiveOutbreak)
                //if (insect.OutbreakStartYear <= Model.Core.CurrentTime
                //    && insect.OutbreakStopYear + 1 >= Model.Core.CurrentTime)
                //if (insect.OutbreakStartYear <= Model.Core.CurrentTime)
                //    | insect.MortalityYear = Model.Core.CurrentTime)
                //    continue;

                //----- Write Insect GrowthReduction maps --------
                IOutputRaster<UShortPixel> map = CreateMap((Model.Core.CurrentTime - 1), insect.Name);

                using (map) {
                    UShortPixel pixel = new UShortPixel();
                    foreach (Site site in Model.Core.Landscape.AllSites) {
                        if (site.IsActive)
                                pixel.Band0 = (ushort) (insect.LastYearDefoliation[site] * 100.0);
                        else
                            //  Inactive site
                            pixel.Band0 = 0;

                        map.WritePixel(pixel);
                    }
                }

                //----- Write Initial Patch maps --------
                //IOutputRaster<UShortPixel> map2 = CreateMap(Model.Core.CurrentTime, ("InitialPatchMap" + insect.Name));
                //using (map2) {
                //    UShortPixel pixel = new UShortPixel();
                //    foreach (Site site in Model.Core.Landscape.AllSites) {
                //        if (site.IsActive)
                //        {
                //            if (insect.Disturbed[site])
                //                pixel.Band0 = (ushort) (SiteVars.InitialOutbreakProb[site] * 100);
                //            else
                //                pixel.Band0 = 0;
                //        }
                //        else
                //        {
                //            //  Inactive site
               //             pixel.Band0 = 0;
                //        }
                //        map2.WritePixel(pixel);
                //    }
                //}

                //----- Write Biomass Reduction maps --------
                IOutputRaster<UShortPixel> map3 = CreateMap(Model.Core.CurrentTime, ("BiomassRemoved" + insect.Name));
                using (map3) {
                    UShortPixel pixel = new UShortPixel();
                    foreach (Site site in Model.Core.Landscape.AllSites) {
                        if (site.IsActive)
                        {
                            // TESTING added by RMS
                            if(SiteVars.BiomassRemoved[site] > 0)
                               // UI.WriteLine("  Biomass revoved at {0}/{1}: {2}.", site.Location.Row, site.Location.Column, SiteVars.BiomassRemoved[site]);
                               pixel.Band0 = (ushort) (SiteVars.BiomassRemoved[site] / 100);
                            else
                               pixel.Band0 = 0;

                        }
                        else
                        {
                            //  Inactive site
                            pixel.Band0 = 0;
                        }
                        map3.WritePixel(pixel);
                        //Zero out the BiomassRemoved after the last insect mortality event in a given year.
                        //if (SiteVars.BiomassRemoved[site] > 0 && SiteVars.TimeOfLastEvent[site] < Model.Core.CurrentTime)
                            SiteVars.BiomassRemoved[site] = 0;
                    }
                }
            }

        }
Exemple #4
0
 Double Exponential(Double lambda)
 {
     Distribution.Lambda = lambda;
     return(Distribution.NextDouble());
 }
Exemple #5
0
        double Exponential(double lambda)
        {
            ran.Lambda = lambda;

            return(ran.NextDouble());
        }