Esempio n. 1
0
        internal override void GenerateCurve()
        {
            //create the senseCurve, the start of the curve and start populating it with random values
            List <SensitivityPoint> sensCurve  = new List <SensitivityPoint>();
            SensitivityPoint        firstPoint = new SensitivityPoint(0, sensMean);

            sensCurve.Add(firstPoint);
            var seededRandom = new SystemRandomSource(42);//creates a random source with the seed "42"

            for (double timecode = curveTimestep; timecode < this.lenght; timecode += curveTimestep)
            {
                double sensDirection = seededRandom.NextDouble();                                              //create a random double to determine if sense is going to be faster or slower
                if (sensDirection >= 0.5)                                                                      //sens will be faster
                {
                    double           randomSens = sensMean + seededRandom.NextDouble() * (sensMax - sensMean); //generates a random value in the range of (basesens:maxsens)
                    SensitivityPoint sensPoint  = new SensitivityPoint(timecode, randomSens);
                    sensCurve.Add(sensPoint);
                }
                else // sensDirection <0.5 -> sens will be slower
                {
                    double           randomSens = sensMin + seededRandom.NextDouble() * (sensMean - sensMin);
                    SensitivityPoint sensPoint  = new SensitivityPoint(timecode, randomSens);
                    sensCurve.Add(sensPoint);
                }
            }
            SensitivityPoint finalSensPoint = new SensitivityPoint(this.lenght, 1);//Make sure the curve ends at base sens

            sensCurve.Add(finalSensPoint);
            base.sensCurve = sensCurve;
        }
Esempio n. 2
0
        private int CalculateProcessDuration(int to)
        {
            SystemRandomSource randomNumberGenerator = SystemRandomSource.Default;

            //double rand = randomNumberGenerator.NextDouble();
            //if ((rand = Math.Round(rand, 5)) == 1) rand = .99999;
            //double randomNumber = rand;
            //double x = -Math.Log(1.0 - randomNumber) * S[to];
            //int ret = (int)Math.Ceiling(x);
            //return ret;
            return((int)Math.Ceiling(-Math.Log(1.0 - randomNumberGenerator.NextDouble()) * S[to]));
        }
Esempio n. 3
0
        public int CalculateNextServer(int from)
        {
            int to = 0;
            SystemRandomSource randomNumberGenerator = SystemRandomSource.Default;
            double             randomNumber          = Math.Round(randomNumberGenerator.NextDouble(), 5);

            for (; to < DeviceNumber && randomNumber > 0; to++)
            {
                randomNumber -= P[from, to];
            }
            if (to != 0)
            {
                to--;
            }
            return(to);
        }
Esempio n. 4
0
        private int CalculateProcessDuration(int to)
        {
            // da li treba da se mnozi sa 1000? Sada je range: 0.00005..230
            SystemRandomSource randomNumberGenerator = SystemRandomSource.Default;
            double             rand = randomNumberGenerator.NextDouble();

            if ((rand = Math.Round(rand, 5)) == 1)
            {
                rand = .99999;
            }
            double randomNumber = rand;
            double x            = -Math.Log(1.0 - randomNumber) * S[to];

            //Console.Write("{0}\t", (int)x);
            int ret = (x < 1.0) ? 1 : (int)x;

            return(ret);
        }
 public double NextDouble()
 {
     return(random.NextDouble());
 }
Esempio n. 6
0
        private static GradientDescentResult GradientDescent(Matrix <double> anchorsIn, Vector <double> rangesIn,
                                                             Matrix <double> boundsIn = null, int nTrial = 100, double alpha = 0.001, double timeThreshold = 0)
        {
            System.Random random = new SystemRandomSource();

            int n   = anchorsIn.RowCount;
            int dim = anchorsIn.ColumnCount;
            var gradientDescentResult = new GradientDescentResult(nTrial, dim);

            if (boundsIn == null)
            {
                boundsIn = DenseMatrix.Build.Dense(1, dim);
            }
            Matrix <double> boundsTemp = anchorsIn.Stack(boundsIn);
            Matrix <double> bounds     = DenseMatrix.Build.Dense(2, dim);

            for (int i = 0; i < dim; i++)
            {
                bounds[0, i] = boundsTemp.Column(i).Min();
                bounds[1, i] = boundsTemp.Column(i).Max();
            }

            if (timeThreshold == 0)
            {
                timeThreshold = 1.0 / nTrial;
            }

            Vector <double> ranges = DenseVector.Build.Dense(n);

            for (int i = 0; i < nTrial; i++)
            {
                Vector <double> estimator0 = DenseVector.Build.Dense(dim);
                for (int j = 0; j < dim; j++)
                {
                    estimator0[j] = random.NextDouble() * (bounds[1, j] - bounds[0, j]) + bounds[0, j];
                }
                Vector <double> estimator = DenseVector.OfVector(estimator0);

                Stopwatch stopwatch = new Stopwatch();
                while (true)
                {
                    for (int j = 0; j < n; j++)
                    {
                        ranges[j] = GetEuclidean(anchorsIn.Row(j), estimator);
                    }
                    double error = GetEuclidean(rangesIn, ranges);

                    Vector <double> delta = DenseVector.Build.Dense(dim);
                    for (int j = 0; j < n; j++)
                    {
                        delta += (rangesIn[j] - ranges[j]) / ranges[j] * (estimator - anchorsIn.Row(j));
                    }
                    delta *= 2 * alpha;

                    Vector <double> estimatorNext = estimator - delta;
                    for (int j = 0; j < n; j++)
                    {
                        ranges[j] = MLAT.GetEuclidean(anchorsIn.Row(j), estimatorNext);
                    }
                    double errorNext = MLAT.GetEuclidean(rangesIn, ranges);
                    if (errorNext < error)
                    {
                        estimator = estimatorNext;
                    }
                    else
                    {
                        gradientDescentResult.EstimatorCandidate.SetRow(i, estimator);
                        gradientDescentResult.Error[i] = error;
                        break;
                    }
                    if (stopwatch.ElapsedMilliseconds > timeThreshold)
                    {
                        gradientDescentResult.Error[i] = double.MaxValue;
                        break;
                    }
                }
            }

            return(gradientDescentResult);
        }
Esempio n. 7
0
 public static double GenerateWeight() =>
 RandomSource.NextDouble();
Esempio n. 8
0
    static void Main(string[] args)
    {
        System.Random random = new SystemRandomSource();

        double          W = 9, L = 9, H = 3;
        Matrix <double> anchors = DenseMatrix.OfArray(new double[, ] {
            { 0, 0, H },
            { W, 0, H },
            { W, L, H },
            { 0, L, H }
        });
        Vector <double> node = DenseVector.OfArray(new double[] {
            W *random.NextDouble(),
            L *random.NextDouble(),
            H *random.NextDouble()
        });
        Vector <double> ranges = Vector <double> .Build.Dense(anchors.RowCount);

        double          error             = 0.5;
        Vector <double> ranges_with_error = DenseVector.OfVector(ranges);

        for (int i = 0; i < anchors.RowCount; i++)
        {
            ranges[i]            = Distance.Euclidean(anchors.Row(i), node);
            ranges_with_error[i] = ranges[i] + 2 * error * (random.NextDouble() - 0.5);
        }
        // TODO: You need to define search space boundary to prevent UNEXPECTED RESULT
        // If not, search space boundary is defined as a cube constrained to
        // minimum and maximum coordinates of x, y, z of anchors
        // If anchors are in the same plane, i.e., all anchors have the same (similar)
        // coordinate of at least one axes, you MUST define search space boundary
        // So, defining search space boundary is all up to you
        Matrix <double> bounds = new DenseMatrix(2, anchors.ColumnCount);

        for (int i = 0; i < anchors.ColumnCount; i++)
        {
            bounds[0, i] = anchors.Column(i).Minimum();
            bounds[1, i] = anchors.Column(i).Maximum();
        }
        // hard coded minimum height (0 m) of search boundary
        bounds[0, anchors.ColumnCount - 1] = 0;

        MLAT.GdescentResult gdescent_result = MLAT.mlat(anchors, ranges_with_error, bounds);

        Console.WriteLine("Anchors");
        Console.WriteLine(anchors);
        Console.WriteLine("Node");
        Console.WriteLine(node);
        Console.WriteLine("Ranges");
        Console.WriteLine(ranges);
        Console.WriteLine("Ranges with error");
        Console.WriteLine(ranges_with_error);
        Console.WriteLine("Estimator");
        Console.WriteLine(gdescent_result.estimator);
        Console.WriteLine("Full result");
        Console.WriteLine(gdescent_result.estimator_candidate);
        Console.WriteLine(gdescent_result.error);

        // prevent closing
        Console.ReadLine();
    }
Esempio n. 9
0
        public void RenderParallel()
        {
            Scene   scene   = Scene;
            Camera  camera  = Camera;
            Sampler sampler = Sampler;
            Buffer  buf     = PBuffer;

            (int w, int h) = (buf.W, buf.H);
            int spp     = SamplesPerPixel;
            int sppRoot = (int)(Math.Sqrt(SamplesPerPixel));

            scene.Compile();
            scene.rays = 0;

            // Stop watch timer
            Stopwatch sw = new Stopwatch();

            sw.Start();

            // Random Number Generator from on Math.Numerics
            System.Random rand = new SystemRandomSource(sw.Elapsed.Milliseconds, true);

            // Frame resolution
            int totalPixels = h * w;

            // Create a cancellation token for Parallel.For loop control
            CancellationTokenSource cts = new CancellationTokenSource();

            // ParallelOptions for Parallel.For
            ParallelOptions po = new ParallelOptions();

            po.CancellationToken = cts.Token;

            // Set number of cores/threads
            po.MaxDegreeOfParallelism = Environment.ProcessorCount;

            Console.WriteLine("{0} x {1} pixels, {2} spp, {3} core(s)", w, h, spp, po.MaxDegreeOfParallelism);

            if (StratifiedSampling)
            {
                _ = Parallel.For(0, w * h, po, (i, loopState) =>
                {
                    int y = i / w, x = i % w;
                    for (int u = 0; u < sppRoot; u++)
                    {
                        for (int v = 0; v < sppRoot; v++)
                        {
                            var fu        = (u + 0.5) / sppRoot;
                            var fv        = (v + 0.5) / sppRoot;
                            Ray ray       = camera.CastRay(x, y, w, h, fu, fv, rand);
                            Colour sample = sampler.Sample(scene, ray, rand);
                            buf.AddSample(x, y, sample);
                        }
                    }
                });
                Console.WriteLine("time elapsed:" + sw.Elapsed);
            }
            else
            {
                //Random subsampling
                _ = Parallel.ForEach(Partitioner.Create(0, totalPixels), po, (range) =>
                {
                    for (int i = range.Item1; i < range.Item2; i++)
                    {
                        for (int s = 0; s < spp; s++)
                        {
                            int y      = i / w, x = i % w;
                            var fu     = ThreadSafeRandom.NextDouble(rand);
                            var fv     = ThreadSafeRandom.NextDouble(rand);
                            var ray    = camera.CastRay(x, y, w, h, fu, fv, rand);
                            var sample = sampler.Sample(scene, ray, rand);
                            buf.AddSample(x, y, sample);
                        }
                    }
                });
                Console.WriteLine("time elapsed:" + sw.Elapsed);
            }

            if (AdaptiveSamples > 0)
            {
                _ = Parallel.For(0, w * h, po, (i, loopState) =>
                {
                    int y       = i / w, x = i % w;
                    double v    = buf.StandardDeviation(x, y).MaxComponent();
                    v           = Util.Clamp(v / AdaptiveThreshold, 0, 1);
                    v           = Math.Pow(v, AdaptiveExponent);
                    int samples = (int)(v * AdaptiveSamples);
                    for (int s = 0; s < samples; s++)
                    {
                        var fu        = rand.NextDouble();
                        var fv        = rand.NextDouble();
                        Ray ray       = camera.CastRay(x, y, w, h, fu, fv, rand);
                        Colour sample = sampler.Sample(scene, ray, rand);
                        buf.AddSample(x, y, sample);
                    }
                });
            }

            if (FireflySamples > 0)
            {
                _ = Parallel.For(0, w * h, po, (i, loopState) =>
                {
                    int y = i / w, x = i % w;
                    if (PBuffer.StandardDeviation(x, y).MaxComponent() > FireflyThreshold)
                    {
                        Parallel.For(0, FireflySamples, po, (e, loop) =>
                        {
                            var fu        = rand.NextDouble();
                            var fv        = rand.NextDouble();
                            Ray ray       = camera.CastRay(x, y, w, h, fu, fv, rand);
                            Colour sample = sampler.Sample(scene, ray, rand);
                            buf.AddSample(x, y, sample);
                        });
                    }
                });
            }
            sw.Stop();
        }