Beispiel #1
0
        public PoolServer(string poolName, PoolType type, int speed, int delaySec, double rejectPercent, GetTargetShareHandler handler)
        {
            _rnd = new MersenneTwister((uint)DateTime.Now.Ticks);
            Round = 1;
            PoolName = poolName;
            Type = type;
            GHashSpeed = speed;
            DelaySec = delaySec;
            GetNextShare = handler;
            RejectPercentile = rejectPercent;
            CurrentRealShare = 0;
            MyValidShare = 0;
            MyLostShare = 0;
            TotalScore = 0;
            MyScore = 0;

            MyTotalProfit = 0;
            MyTotalValidShare = 0;
            MyTotalShare = 0;

            BaseProgress = 0;
            PenaltyFactor = 1.0f;

            //            Initialize(initialProgress);
        }
        protected override void GenerateMesh()
        {
            foreach(Transform child in transform)
                DestroyImmediate(child.gameObject);

            var rand = new MersenneTwister(randomSeed);

            var totalHeight = 0.0f;
            var scaleDown = 1.0f;
            var combineMeshInstances = new List<CombineInstance>();
            for(int i = 0; i < layerCount; ++i)
            {
                var baseRadius = Mathf.Lerp(baseRadiusMin, baseRadiusMax, rand.NextSinglePositive()) * scaleDown;
                var baseBezier = GenerateBaseBezier(baseRadius * scaleDown, rand);

                var previousTotalHeight = totalHeight;
                totalHeight += Mathf.Lerp(layerHeightMin, layerHeightMax, rand.NextSinglePositive()) * scaleDown;
                var heightBezier = Bezier.ConstructSmoothSpline(
                    new Vector3[]{
                        Vector3.up * previousTotalHeight,
                        Vector3.up * totalHeight
                    }
                );

                var heightSegs = (uint)heightSegments;
                var pathSegs = (uint)(radiusSegments * scaleDown);

                if( heightSegs > 0 && pathSegs > 2 )
                {
                    var combineMeshInstance = new CombineInstance();
                    combineMeshInstance.mesh = Loft.GenerateMesh(
                        heightBezier,
                        baseBezier,
                        heightSegs,
                        pathSegs
                    );
                    combineMeshInstances.Add(combineMeshInstance);

                    var topCap = new CombineInstance();
                    topCap.mesh = baseBezier.Triangulate(pathSegs, Vector3.up * totalHeight);
                    combineMeshInstances.Add(topCap);
                }

                scaleDown *= Mathf.Lerp(scaleDownMin, scaleDownMax, rand.NextSinglePositive());
            }

            var meshFilter = GetComponent<MeshFilter>();
            if( meshFilter.sharedMesh )
                DestroyImmediate(meshFilter.sharedMesh);

            meshFilter.sharedMesh = new Mesh();
            meshFilter.sharedMesh.CombineMeshes(combineMeshInstances.ToArray(), true, false);

            foreach(var combineInstance in combineMeshInstances)
                DestroyImmediate(combineInstance.mesh);
        }
        public void SampleTest()
        {
            var normal = new Normal(0.0, 1.0);
            var rnd = new MersenneTwister();

            var ms = new MetropolisSampler<double>(0.2, normal.Density, x => Normal.Sample(rnd, x, 0.1), 10);
            ms.RandomSource = rnd;

            double sample = ms.Sample();
        }
        public void MetropolisConstructor()
        {
            var normal = new Normal(0.0, 1.0);
            var rnd = new MersenneTwister();

            var ms = new MetropolisSampler<double>(0.2, normal.Density, x => Normal.Sample(rnd, x, 0.1), 10);
            Assert.IsNotNull(ms.RandomSource);

            ms.RandomSource = rnd;
            Assert.IsNotNull(ms.RandomSource);
        }
        public void SampleArrayTest()
        {
            var normal = new Normal(0.0, 1.0);
            var rnd = new MersenneTwister();

            var ms = new MetropolisHastingsSampler<double>(0.2, normal.Density, (x, y) => (new Normal(x, 0.1)).Density(y), x => Normal.Sample(rnd, x, 0.1), 10)
                     {
                         RandomSource = rnd
                     };

            ms.Sample(5);
        }
        public void MetropolisHastingsConstructor()
        {
            var normal = new Normal(0.0, 1.0);
            var rnd = new MersenneTwister();

            var ms = new MetropolisHastingsSampler<double>(0.2, normal.Density, (x, y) => (new Normal(x, 0.1)).Density(y), x => Normal.Sample(rnd, x, 0.1), 10)
                     {
                         RandomSource = rnd
                     };
            Assert.IsNotNull(ms.RandomSource);

            ms.RandomSource = new Random();
            Assert.IsNotNull(ms.RandomSource);
        }
Beispiel #7
0
        public Flower(int difficulty)
        {
            // Default values
            SliceSize = 1200;
            EarliHopFactor = 4.0f;
            PPLNSFactor = 4.0f;
            ScoreFactor = 4.0f;
            Threshold = 0.43f;
            PowerValue = 2.6f;
            SelectType = 0;

            _difficulty = difficulty;
            _rnd = new MersenneTwister((uint)DateTime.Now.Ticks);
            _slicedShare = new Dictionary<string, double>();
            _slice = new Dictionary<string, int>();
        }
Beispiel #8
0
        public void InitializeSimutlator()
        {
            var seed = (uint)DateTime.Now.Ticks;
            _rnd = new MersenneTwister(seed);
            _currentSimGeneration = 0;

            //
            //
            // Simulation Configuration Here //
            //
            //
            _simConfig = new BasicSimConfig(GetNextTarget);
            //_simConfig = new BclcTest(GetNextTarget);
            //_simConfig = new MinecoTest(GetNextTarget);

            labelAdvPerTick.Text = _simConfig.InitialSimulationSpeedUp.ToString();

            SetupGeneration();
        }
 public RouletteRoundShare(int difficulty)
 {
     _difficulty = difficulty;
     _rnd = new MersenneTwister((uint)DateTime.Now.Ticks);
     Threshold = 0.43f;
 }
    public void SampleProportionalWithoutRepetitionTest() {
      {
        // select 1 of 100 uniformly (weights = 0)
        var items = Enumerable.Range(0, 100);
        var random = new MersenneTwister(31415);
        var weights = Enumerable.Repeat(0.0, 100);
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
          Assert.AreEqual(sample.Count(), 1);
        }
      }
      {
        // select 1 of 1 uniformly (weights = 0)
        var items = Enumerable.Range(0, 1);
        var random = new MersenneTwister(31415);
        var weights = Enumerable.Repeat(0.0, 1);
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
          Assert.AreEqual(sample.Count(), 1);
        }
      }
      {
        // select 1 of 2 non-uniformly (weights = 1, 2)
        var items = Enumerable.Range(0, 2);
        var random = new MersenneTwister(31415);
        var weights = new double[] { 1.0, 2.0 };
        var zeroSelected = 0;
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 1, weights, false, false).ToArray();
          Assert.AreEqual(sample.Count(), 1);
          if (sample[0] == 0) zeroSelected++;
        }
        Assert.IsTrue(zeroSelected > 0 && zeroSelected < 1000);
      }
      {
        // select 2 of 2 non-uniformly (weights = 1, 1000)
        var items = Enumerable.Range(0, 2);
        var random = new MersenneTwister(31415);
        var weights = new double[] { 1.0, 1000.0 };
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
          Assert.AreEqual(sample.Count(), 2);
          Assert.AreEqual(sample.Distinct().Count(), 2);
        }
      }
      {
        // select 2 from 1 uniformly (weights = 0), this does not throw an exception but instead returns a sample with 1 element!
        var items = Enumerable.Range(0, 1);
        var random = new MersenneTwister(31415);
        var weights = Enumerable.Repeat(0.0, 1);
        var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 2, weights, false, false).ToArray();
        Assert.AreEqual(sample.Count(), 1);
      }

      {
        // select 10 of 100 uniformly (weights = 0)
        var items = Enumerable.Range(0, 100);
        var random = new MersenneTwister(31415);
        var weights = Enumerable.Repeat(0.0, 100);
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
          Assert.AreEqual(sample.Count(), 10);
          Assert.AreEqual(sample.Distinct().Count(), 10);
        }
      }

      {
        // select 100 of 100 uniformly (weights = 0)
        var items = Enumerable.Range(0, 100);
        var random = new MersenneTwister(31415);
        var weights = Enumerable.Repeat(0.0, 100);
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
          Assert.AreEqual(sample.Count(), 100);
          Assert.AreEqual(sample.Distinct().Count(), 100);
        }
      }

      {
        // select 10 of 10 uniformly (weights = 1)
        var items = Enumerable.Range(0, 10);
        var random = new MersenneTwister(31415);
        var weights = Enumerable.Repeat(1.0, 10);
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, false, false).ToArray();
          Assert.AreEqual(sample.Count(), 10);
          Assert.AreEqual(sample.Distinct().Count(), 10);
        }
      }

      {
        // select 10 of 10 uniformly (weights = 1)
        var items = Enumerable.Range(0, 10);
        var random = new MersenneTwister(31415);
        var weights = Enumerable.Repeat(1.0, 10);
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, true, false).ToArray();
          Assert.AreEqual(sample.Count(), 10);
          Assert.AreEqual(sample.Distinct().Count(), 10);
        }
      }

      {
        // select 10 of 10 uniformly (weights = 1)
        var items = Enumerable.Range(0, 10);
        var random = new MersenneTwister(31415);
        var weights = Enumerable.Repeat(1.0, 10);
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 10, weights, true, true).ToArray();
          Assert.AreEqual(sample.Count(), 10);
          Assert.AreEqual(sample.Distinct().Count(), 10);
        }
      }

      {
        // select 5 of 10 uniformly (weights = 0..n)
        var items = Enumerable.Range(0, 10);
        var random = new MersenneTwister(31415);
        var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, false, false).ToArray();
          Assert.AreEqual(sample.Count(), 5);
          Assert.AreEqual(sample.Distinct().Count(), 5);
        }
      }

      {
        // select 5 of 10 uniformly (weights = 0..n)
        var items = Enumerable.Range(0, 10);
        var random = new MersenneTwister(31415);
        var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, true, false).ToArray();
          Assert.AreEqual(sample.Count(), 5);
          Assert.AreEqual(sample.Distinct().Count(), 5);
        }
      }

      {
        // select 5 of 10 uniformly (weights = 0..n)
        var items = Enumerable.Range(0, 10);
        var random = new MersenneTwister(31415);
        var weights = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        for (int i = 0; i < 1000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 5, weights, true, true).ToArray();
          Assert.AreEqual(sample.Count(), 5);
          Assert.AreEqual(sample.Distinct().Count(), 5);
        }
      }

      {
        // select 10 of 100 uniformly (weights = 1)
        // repeat 1000000 times and calculate statistics
        var items = Enumerable.Range(0, 100);
        var random = new MersenneTwister(31415);
        var weights = Enumerable.Repeat(1.0, 100);
        var selectionCount = new int[100, 100]; // frequency of selecting item at pos
        for (int i = 0; i < 1000000; i++) {
          var sample = RandomEnumerable.SampleProportionalWithoutRepetition(items, random, 100, weights, false, false).ToArray();
          Assert.AreEqual(sample.Count(), 100);
          Assert.AreEqual(sample.Distinct().Count(), 100);

          int pos = 0;
          foreach (var item in sample) {
            selectionCount[item, pos]++;
            pos++;
          }
        }
        var sb = new StringBuilder();
        for (int item = 0; item < 100; item++) {
          for (int pos = 0; pos < 100; pos++) {
            sb.AppendFormat("{0} ", selectionCount[item, pos]);
          }
          sb.AppendLine();
        }
        Console.WriteLine(sb.ToString());
      }
    }
        public void SamplesFollowsCorrectDistribution()
        {
            Random rnd = new MersenneTwister(1);

            foreach (var dd in _discreteDistributions)
            {
                dd.RandomSource = rnd;
                VapnikChervonenkisTest(Error, ErrorProbability, dd.Samples().Select(x => (double)x).Take(NumberOfTestSamples), dd);
            }

            foreach (var cd in _continuousDistributions)
            {
                cd.RandomSource = rnd;
                VapnikChervonenkisTest(Error, ErrorProbability, cd.Samples().Take(NumberOfTestSamples), cd);
            }
        }
        public void SampleFollowsCorrectDistribution()
        {
            Random rnd = new MersenneTwister(1);

            foreach (var dd in _discreteDistributions)
            {
                dd.RandomSource = rnd;
                var samples = new double[NumberOfTestSamples];
                for (var i = 0; i < NumberOfTestSamples; i++)
                {
                    samples[i] = dd.Sample();
                }

                VapnikChervonenkisTest(Error, ErrorProbability, samples, dd);
            }

            foreach (var cd in _continuousDistributions)
            {
                cd.RandomSource = rnd;
                var samples = new double[NumberOfTestSamples];
                for (var i = 0; i < NumberOfTestSamples; i++)
                {
                    samples[i] = cd.Sample();
                }

                VapnikChervonenkisTest(Error, ErrorProbability, samples, cd);
            }
        }
        public void SamplesFollowsCorrectDistribution()
        {
            Random rnd = new MersenneTwister();
            var cd = new NormalGamma(1.0, 4.0, 3.0, 3.5);

            // Sample from the distribution.
            var samples = cd.Samples().Take(CommonDistributionTests.NumberOfTestSamples).ToArray();

            // Extract the mean and precisions.
            var means = samples.Select(mp => mp.Mean);
            var precs = samples.Select(mp => mp.Precision);
            var meanMarginal = cd.MeanMarginal();
            var precMarginal = cd.PrecisionMarginal();

            // Check the precision distribution.
            CommonDistributionTests.VapnikChervonenkisTest(
                CommonDistributionTests.Error,
                CommonDistributionTests.ErrorProbability,
                precs,
                precMarginal);

            // Check the mean distribution.
            CommonDistributionTests.VapnikChervonenkisTest(
                CommonDistributionTests.Error,
                CommonDistributionTests.ErrorProbability,
                means,
                meanMarginal);
        }
 // todo: remove this
 private static void SlotAdvances(BWRng pidrng, MersenneTwister ivrng)
 {
     pidrng.GetNext64BitNumber();
     pidrng.GetNext64BitNumber();
     pidrng.GetNext64BitNumber();
     pidrng.GetNext64BitNumber();
     pidrng.GetNext64BitNumber();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
     ivrng.Next();
 }
        private Bezier GenerateBaseBezier(float size, MersenneTwister rand)
        {
            var centerJitterX = Mathf.Lerp(-1.0f, 1.0f, size * centerJitterAmount * rand.NextSinglePositive());
            var centerJitterZ = Mathf.Lerp(-1.0f, 1.0f, size * centerJitterAmount * rand.NextSinglePositive());
            var centerJitter = new Vector3(centerJitterX, 0.0f, centerJitterZ);
            var cpCount = 32;
            var step = Mathf.PI * -2.0f / (float)cpCount;
            var cps = new Vector3[cpCount];

            for(int i = 0; i < cpCount; ++i)
            {
                var t = (float)i * step;
                var x = Mathf.Sin(t) * size;
                var z = Mathf.Cos(t) * size;
                cps[i].x = Mathf.Lerp(0.0f, x, 1.0f - (waviness * rand.NextSinglePositive()));
                cps[i].z = Mathf.Lerp(0.0f, z, 1.0f - (waviness * rand.NextSinglePositive()));
                cps[i] += centerJitter;
            }

            return Bezier.ConstructSmoothSpline(cps, true);
        }
 public MersennePlusN(Int32 seed)
 {
     mt = new MersenneTwister(seed);
 }
 public MersennePlusN()
 {
     this.seed = new Random().Next();
     mt = new MersenneTwister(seed);
 }
 private void Advance(BWRng pidrng, MersenneTwister ivrng, List<DreamRadarFrame.Spin> spins)
 {
     // first PIDRNG advance = spin
     spins.Add((DreamRadarFrame.Spin) pidrng.GetNext32BitNumber(8));
     pidrng.GetNext64BitNumber();
     ivrng.Next();
     ivrng.Next();
 }
        private DreamRadarFrame GeneratePokemon(BWRng pidrng2, MersenneTwister ivrng2)
        {
            var pidrng = new BWRng(pidrng2.Seed);
            var ivrng = new MersenneTwister(ivrng2);
            var frame = new DreamRadarFrame();

            frame.Pid = GeneratePID(pidrng);
            // two unknown advances
            pidrng.GetNext64BitNumber();
            pidrng.GetNext64BitNumber();
            frame.Nature = pidrng.GetNext32BitNumber(25);
            // IVs
            frame.Hp = ivrng.Next() >> 27;
            frame.Atk = ivrng.Next() >> 27;
            frame.Def = ivrng.Next() >> 27;
            frame.Spa = ivrng.Next() >> 27;
            frame.Spd = ivrng.Next() >> 27;
            frame.Spe = ivrng.Next() >> 27;

            return frame;
        }
        public void SampleFollowsCorrectDistribution()
        {
            Random rnd = new MersenneTwister();
            //var cd = new NormalGamma(1.0, 4.0, 3.0, 3.5);
            var cd = new NormalGamma(1.0, 4.0, 7.0, 3.5);

            // Sample from the distribution.
            MeanPrecisionPair[] samples = new MeanPrecisionPair[CommonDistributionTests.NumberOfTestSamples];
            for (int i = 0; i < CommonDistributionTests.NumberOfTestSamples; i++)
            {
                samples[i] = cd.Sample();
            }

            // Extract the mean and precisions.
            var means = samples.Select(mp => mp.Mean);
            var precs = samples.Select(mp => mp.Precision);
            var meanMarginal = cd.MeanMarginal();
            var precMarginal = cd.PrecisionMarginal();

            // Check the precision distribution.
            CommonDistributionTests.VapnikChervonenkisTest(
                CommonDistributionTests.Error,
                CommonDistributionTests.ErrorProbability,
                precs,
                precMarginal);

            // Check the mean distribution.
            CommonDistributionTests.VapnikChervonenkisTest(
                CommonDistributionTests.Error,
                CommonDistributionTests.ErrorProbability,
                means,
                meanMarginal);
        }
        public List<DreamRadarFrame> Generate(ulong seed, Profile profile)
        {
            var frames = new List<DreamRadarFrame>();
            // Build the PIDRNG
            uint initialFrame = Functions.initialPIDRNG(seed, profile);
            var pidrng = new BWRng(seed);
            pidrng.Advance(initialFrame);

            // Build the MTRNG
            // todo: use fast MTRNG when available
            var ivrng = new MersenneTwister((uint) (seed >> 32));
            // advance 8 frames for BW2
            for (uint i = 0; i < 10; ++i) ivrng.Next();

            // one single advancement for entering the menu
            pidrng.GetNext64BitNumber();

            var spins = new List<DreamRadarFrame.Spin>();
            // initial advances
            for (uint i = 0; i < initialFrame; ++i) Advance(pidrng, ivrng, spins);

            // slot advances
            // we're always doing the slot 1 advance here
            pidrng.GetNext64BitNumber();

            for (uint i = 1; i < TargetSlot; ++i) SlotAdvances(pidrng, ivrng);

            for (uint i = initialFrame; i <= MaxFrame; ++i)
            {
                DreamRadarFrame frame = GeneratePokemon(pidrng, ivrng);

                var arrSpins = new DreamRadarFrame.Spin[spins.Count];
                spins.CopyTo(arrSpins);
                frame.Spins = arrSpins;

                // add checks/comparisons on the frame here
                // nature/IVs
                frames.Add(frame);
                Advance(pidrng, ivrng, spins);
            }

            return frames;
        }