Exemplo n.º 1
0
 public void AddUniform(IUniform uniform)
 {
     if (!this.uniformList.TryGetValue(uniform.GetName(), out IUniform testUniform))
     {
         this.uniformList.Add(uniform.GetName(), uniform);
     }
 }
Exemplo n.º 2
0
        public void LinkShaderUniform(IUniform uniform)
        {
            var typedUniform = (GlobalUniform <T>)uniform;

            typedUniform.UpdateValue(this);
            LinkedUniforms.Add(typedUniform);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generates the next random value according to a Pareto
        /// distribution.
        /// </summary>
        /// <returns>
        /// The next random value.
        /// </returns>
        public override double NextDouble()
        {
            IUniform u = GetUniform();
            double   d = 1.0 - u.NextDouble();

            return(1.0 / Math.Pow(d, 1.0 / Shape));
        }
Exemplo n.º 4
0
 public void SetUniform(IUniform uniform)
 {
     if (this.uniformList.ContainsKey(uniform.GetName()))
     {
         this.uniformList[uniform.GetName()] = uniform;
     }
 }
Exemplo n.º 5
0
        public void MeanOfManyObservations()
        {
            IUniform  valGen          = UniformStreams.DefaultStreams.GetUniform();
            IUniform  timeGen         = UniformStreams.DefaultStreams.GetUniform();
            double    weightedSum     = 0.0;
            double    sumOfWeights    = 0.0;
            TimeValue lastObservation = new TimeValue(0L, valGen.NextDouble() * 100.0);

            TimeWeightedMean twm = new TimeWeightedMean(new Simulation());

            twm.Observe(lastObservation.Value, lastObservation.Time);

            for (int i = 0; i < 1000; i++)
            {
                long      newTime        = lastObservation.Time + (long)(timeGen.NextDouble() * 10.0);
                TimeValue newObservation = new TimeValue(newTime, valGen.NextDouble() * 100.0);
                double    weight         = newObservation.Time - lastObservation.Time;
                weightedSum  += weight * lastObservation.Value;
                sumOfWeights += weight;

                twm.Observe(newObservation.Value, newObservation.Time);
                lastObservation = newObservation;
            }

            double mean = weightedSum / sumOfWeights;

            Assert.AreEqual(mean, twm.Value);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Generates the next random value according to a logistic
        /// distribution.
        /// </summary>
        /// <returns>
        /// The next random value.
        /// </returns>
        public override double NextDouble()
        {
            IUniform u = GetUniform();
            double   d = u.NextDouble();

            return(Math.Log(d / (1.0 - d)) / Shape);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Generates the next random value according to a Beta
        /// distribution.
        /// </summary>
        /// <returns>
        /// The next random value.
        /// </returns>
        public override double NextDouble()
        {
            IUniform u     = GetUniform();
            double   gamma = Gamma.Generate(u, Scale, 1.0);

            return(gamma == 0.0 ? 0.0 :
                   gamma / (gamma + Gamma.Generate(u, Shape, 1.0)));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Returns the next <see cref="IUniform"/> random number generator.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Each time this method is called a different <see cref="IUniform"/>
        /// is returned until the last <see cref="IUniform"/> in the set is
        /// returned.  At that point, the cycle begins again with the first
        /// <see cref="IUniform"/> in the set.
        /// </para>
        /// <para>
        /// If <see cref="Length"/> is one (1), then this method will always
        /// return the same <see cref="IUniform"/>.
        /// </para>
        /// </remarks>
        /// <returns>
        /// The next <see cref="IUniform"/> random number generator.
        /// </returns>
        public IUniform GetUniform()
        {
            IUniform rng = _prngs[_index++];

            if (_index >= _prngs.Length)
            {
                _index = 0;
            }
            return(rng);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Generates the next random value according to a Weibull
        /// distribution.
        /// </summary>
        /// <returns>
        /// The next random value.
        /// </returns>
        public override double NextDouble()
        {
            IUniform u = GetUniform();
            double   d = 1.0 - u.NextDouble();

            for (; d <= 1e-10; d = 1.0 - u.NextDouble())
            {
                ;
            }
            return(Scale * Math.Pow(-Math.Log(d), 1.0 / Shape));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Generates the next random value according to an exponential
        /// distribution.
        /// </summary>
        /// <returns>
        /// The next random value.
        /// </returns>
        public override double NextDouble()
        {
            IUniform u = GetUniform();
            double   d = u.NextDouble();

            if (d <= 1e-10)
            {
                d = 1e-10;             // <-- SEMI!
            }
            return(-Math.Log(d) / Lambda);
        }
Exemplo n.º 11
0
 /// <summary>
 /// Create a new <see cref="NonUniform"/> that obtains its
 /// <see cref="IUniform"/> generator from the given
 /// <see cref="IUniformSource"/>.
 /// </summary>
 /// <remarks>
 /// If <paramref name="source"/> is <see langword="null"/>, this
 /// constructor behaves exactly like the no-arg constructor and
 /// obtains an <see cref="IUniform"/> generator from the set of
 /// default random numbers.
 /// </remarks>
 /// <param name="source">
 /// The <see cref="IUniformSource"/> from which to obtain the
 /// <see cref="IUniform"/> generator.
 /// </param>
 protected NonUniform(IUniformSource source)
 {
     if (source != null)
     {
         _rng = source.GetUniform();
     }
     else
     {
         _rng = UniformStreams.DefaultStreams.GetUniform();
     }
 }
Exemplo n.º 12
0
        internal void AssignUniforms(Shader shader, ShaderUniformAssignmentHandler assignmentHandler)
        {
            this.textures.Clear();
            this.shader = shader;

            foreach (ShaderUniform uniform in this.shader.Uniforms)
            {
                this.currentlyToBeAssignedUniform = uniform;
                assignmentHandler(this, uniform);
            }

            this.currentlyToBeAssignedUniform = null;
            this.shader = null;
        }
Exemplo n.º 13
0
        public void GetUniformAndDirect()
        {
            UniformStreams s = UniformStreams.DefaultStreams;
            IUniform       u1 = null, u2 = null;

            for (int i = 0; i < s.Length; i++)
            {
                u1 = s.GetUniform();
                Assert.AreEqual(s[i], u1);
                if (u2 != null)
                {
                    Assert.AreNotEqual(u1, u2);
                }
                u2 = u1;
            }

            Assert.AreEqual(s[0], s.GetUniform());
        }
Exemplo n.º 14
0
    private IEnumerator <Task> PreSchooler(Process process, object data)
    {
        int      id            = _idPool++;
        IUniform rnd           = UniformStreams.DefaultStreams[0];
        long     delayDuration = (long)(rnd.NextDouble() * 100.0);

        Console.WriteLine(id + ": Attempting to get a thwacker @ " + Now);
        yield return(_thwacker.Acquire(process));                        // (1)

        Console.WriteLine(id + ": Got the a thwacker @ " + Now);
        yield return(process.Delay(delayDuration));

        Console.WriteLine(id + ": Releasing the thwacker @ " + Now);
        yield return(_thwacker.Release(process));                        // (2)

        Console.WriteLine(id + ": PreSchooler process ends @ " + Now);
        yield break;
    }
Exemplo n.º 15
0
        /// <summary>
        /// Generates the next random value according to a triangular
        /// distribution.
        /// </summary>
        /// <returns>
        /// The next random value.
        /// </returns>
        public override double NextDouble()
        {
            IUniform u     = GetUniform();
            double   d     = u.NextDouble();
            double   w     = Width;
            double   split = (_mode - _min) / w;
            double   result;

            if (d <= split)
            {
                result = _min + Math.Sqrt(w * (_mode - _min) * d);
            }
            else
            {
                result = _max - Math.Sqrt(w * (_max - _mode) * (1.0 - d));
            }

            return(result);
        }
Exemplo n.º 16
0
        public void VarianceOfManyObservations()
        {
            IUniform  valGen          = UniformStreams.DefaultStreams.GetUniform();
            IUniform  timeGen         = UniformStreams.DefaultStreams.GetUniform();
            TimeValue lastObservation = new TimeValue(0L, valGen.NextDouble() * 100.0);

            double[] weights = new double[1000];
            double[] values  = new double[1000];

            TimeWeightedVariance twv = new TimeWeightedVariance(new Simulation());
            TimeWeightedMean     twm = new TimeWeightedMean(new Simulation());

            twm.Observe(lastObservation.Value, lastObservation.Time);
            twv.Observe(lastObservation.Value, lastObservation.Time);

            for (int i = 0; i < 1000; i++)
            {
                long      newTime        = lastObservation.Time + (long)(timeGen.NextDouble() * 10.0);
                TimeValue newObservation = new TimeValue(newTime, valGen.NextDouble() * 100.0);
                double    weight         = newObservation.Time - lastObservation.Time;

                twm.Observe(newObservation.Value, newObservation.Time);
                twv.Observe(newObservation.Value, newObservation.Time);

                weights[i] = weight;
                values[i]  = lastObservation.Value;

                lastObservation = newObservation;
            }

            double sumW     = 0.0;
            double sumWVals = 0.0;

            for (int i = 0; i < 1000; i++)
            {
                sumW     += weights[i];
                sumWVals += weights[i] * Math.Pow(values[i] - twm.Value, 2.0);
            }

            double variance = sumWVals / sumW;

            Assert.AreEqual(variance, twv.Value, 0.00001);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Static method used by both <see cref="Normal"/> and
        /// <see cref="Lognormal"/> to generate normally distributed pseudo
        /// random values.
        /// </summary>
        /// <param name="u">The uniform generator.</param>
        /// <param name="mu">The mean.</param>
        /// <param name="sigma">The standard deviation.</param>
        /// <param name="next">The cached next value.</param>
        /// <returns>
        /// A pseudo random value that is normally distributed.
        /// </returns>
        internal static double Generate(IUniform u, double mu, double sigma,
                                        ref double next)
        {
            double z = next;

            if (Double.IsNaN(z))
            {
                double x2pi = u.NextDouble() * Math.PI * 2.0;
                z = 1.0 - u.NextDouble();
                double g2rad = Math.Sqrt(-2.0 * Math.Log(z));
                z    = Math.Cos(x2pi) * g2rad;
                next = Math.Sin(x2pi) * g2rad;
            }
            else
            {
                next = Double.NaN;
            }

            return(mu + z * sigma);
        }
Exemplo n.º 18
0
 /// <summary>
 /// Sets an uniform persistently. It will be updated each time the visual is drawn.
 /// </summary>
 public void SetUniform(IUniform uniform)
 {
     uniforms.Remove(uniform);
     uniforms.Add(uniform);
 }
Exemplo n.º 19
0
 private RndGenerater(int seed, IUniform uniform)
 {
     this.Seed    = seed;
     this.Uniform = uniform;
 }
Exemplo n.º 20
0
        public static RndGenerater Build(int seed)
        {
            IUniform uniform = UniformMersenneTwister.Build(seed);

            return(new RndGenerater(seed, uniform));
        }
Exemplo n.º 21
0
        /// <summary>
        /// Static method used by both <see cref="Gamma"/> and
        /// <see cref="Beta"/> to generate gamma distributed pseudo
        /// random values.
        /// </summary>
        /// <param name="u">The uniform generator.</param>
        /// <param name="alpha">The scale parameter.</param>
        /// <param name="beta">The shape parameter.</param>
        /// <returns>
        /// A pseudo random value that is distributed according to a
        /// gamma distribution.
        /// </returns>
        internal static double Generate(IUniform u, double alpha, double beta)
        {
            double d = u.NextDouble();
            double result;

            if (alpha > 1.0)
            {
                double ainv = Math.Sqrt(2.0 * alpha - 1.0);
                double bbb  = alpha - Log4;
                double ccc  = alpha + ainv;

                for (; ; d = u.NextDouble())
                {
                    if (d > 1e-10 && d < 1.0)
                    {
                        double d2 = 1.0 - u.NextDouble();
                        double v  = Math.Log(d / (1.0 - d)) / ainv;
                        double x  = alpha * Math.Exp(v);
                        double z  = d * d * d2;
                        double r  = bbb + ccc * v - x;
                        if (r + GammaMagicConst - 4.5 * z >= 0.0 ||
                            r >= Math.Log(z))
                        {
                            result = x * beta;
                            break;
                        }
                    }
                }
            }
            else if (alpha == 1.0)
            {
                for (; d < 1e-10; d = u.NextDouble())
                {
                    ;
                }
                result = -Math.Log(d) * beta;
            }
            else
            {
                for (; ; d = u.NextDouble())
                {
                    double x;
                    double b = (Math.E + alpha) / Math.E;
                    double p = b * d;
                    if (p <= 1.0)
                    {
                        x = Math.Pow(p, 1.0 / alpha);
                    }
                    else
                    {
                        x = -Math.Log((b - p) / alpha);
                    }

                    double d2 = u.NextDouble();

                    if (!(p <= 1.0 && d2 > Math.Exp(-x) ||
                          p > 1.0 && d2 > Math.Pow(x, alpha - 1.0)))
                    {
                        result = x * beta;
                        break;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 22
0
 /// <summary>
 /// Create a new <see cref="NonUniform"/> that obtains its
 /// <see cref="IUniform"/> generator from the set of default random
 /// numbers.
 /// <seealso cref="UniformStreams.DefaultStreams"/>
 /// </summary>
 protected NonUniform()
 {
     _rng = UniformStreams.DefaultStreams.GetUniform();
 }
Exemplo n.º 23
0
        public void UnlinkShaderUniform(IUniform uniform)
        {
            var typedUniform = (GlobalUniform <T>)uniform;

            LinkedUniforms.Remove(typedUniform);
        }