public void NextDouble()
        {
            // no params
            int count = 30, minVal = 0, maxVal = 1;
            var nums = new List <double>(ArrayBuilder.repeat(() => GlobalRandom.NextDouble(), count));

            foreach (var num in nums)
            {
                Assert.IsTrue(num >= minVal && num < maxVal);
            }

            // count
            nums = new List <double>(GlobalRandom.NextDoubleArr(count));
            Assert.IsTrue(nums.Count == count);
            foreach (var num in nums)
            {
                Assert.IsTrue(num >= minVal && num < maxVal);
            }

            // count, min, max
            minVal = -2; maxVal = 2;
            nums   = new List <double>(GlobalRandom.NextDoubleArr(count, minVal, maxVal));
            Assert.IsTrue(nums.Count == count);
            foreach (var num in nums)
            {
                Assert.IsTrue(num >= minVal && num < maxVal);
            }
        }
        public override void Update(UpdateEventArgs e)
        {
            this.asteroidChance = Math.Min(asteroidChanceMax, this.asteroidChance + (float)e.ElapsedTimeInS * asteroidChanceIncrease);

            if (this.environment.Offset > firePlanetChanceThreshold)
            {
                this.fireplanetChance = GameMath.Clamp(firePlanetChanceInitial, firePlanetChanceMax, this.fireplanetChance + (float)e.ElapsedTimeInS * firePlanetChanceIncrease);
            }
            if (this.environment.Offset > repellingPlanetChanceThreshold)
            {
                this.repellingPlanetChance = GameMath.Clamp(repellingPlanetChanceInitial, repellingPlanetChanceMax, this.repellingPlanetChance + (float)e.ElapsedTimeInS * repellingPlanetChanceIncrease);
            }

            // Check for planets out of the screen
            while (!this.planets.First.Value.IsOnScreen())
            {
                this.environment.RemoveWorldObject(this.planets.First.Value.ID);

                if (this.planets.First.Value is FirePlanet)
                {
                    this.fireplanetCount--;
                }
                if (this.planets.First.Value is RepellingPlanet)
                {
                    this.repellingPlanetCount--;
                }

                this.environment.Planets.RemoveFirst();
            }

            // Check if spacecore is still visible
            if (this.spacecore != null && this.spacecore.Position.X + 645 + LevelGenerator.SideBuffer < this.environment.Offset)
            {
                this.environment.RemoveWorldObject("spacecore");
                this.spacecore = null;
            }

            // Always have at least a certain amount of planets on the playfield
            if (this.planets.Count < minPlanets)
            {
                this.generatePlanet();
            }

            if (GlobalRandom.NextDouble() < this.asteroidChance)
            {
                this.generateAsteroid();
            }

            if (GlobalRandom.NextDouble() < spacecoreChance && this.spacecore == null)
            {
                this.generateSpacecore();
            }
        }
Exemple #3
0
 public static void OpenRandomWalls(this IEnumerable <GeneratingTile> tiles, Func <GeneratingTile, float> probability, float minOpen, float maxOpen)
 {
     foreach (var tile in tiles)
     {
         // ReSharper disable once AccessToForEachVariableInClosure
         foreach (var direction in Helpers.activeDirections
                  .Where(d => GlobalRandom.NextDouble() < probability(tile)))
         {
             tile.OpenTileWall(direction, minOpen, maxOpen);
         }
     }
 }
Exemple #4
0
        public static MatrixD Random(this MatrixBuilder <double> mb, int rows, int cols, double minVal, double maxVal)
        {
            var matrix = new double[rows, cols];

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    matrix[i, j] = GlobalRandom.NextDouble(minVal, maxVal);
                }
            }
            return(MatrixD.Build.DenseOfArray(matrix));
        }
        private void generateSpacecore()
        {
            // y-coordinate
            float y = 0;

            do
            {
                y = (float)GlobalRandom.NextDouble(-345, 345);
            } while (!this.checkPosition(y, 15));

            this.spacecore = new Spacecore(this.environment, new Vector2(655 + SideBuffer + this.environment.Offset, y));
            this.environment.AddWorldObject("spacecore", this.spacecore);
        }
        private void generateAsteroid()
        {
            // Random radius
            float r = GameMath.Clamp(10, 18, (float)GlobalRandom.NormalDouble(14, 4));

            // y-coordinate
            float y = 0;

            do
            {
                y = (float)GlobalRandom.NextDouble(-360 + r, 360 - r);
            } while (!this.checkPosition(y, r));

            this.environment.AddAsteroid(new Vector2(640 + SideBuffer + r + this.environment.Offset, y), r);
        }
        private void generatePlanet()
        {
            // Random radius
            float r = GameMath.Clamp(10, 80, (float)GlobalRandom.NormalDouble(45, 10));

            // y-coordinate
            float y = 0;

            do
            {
                y = (float)GlobalRandom.NextDouble(-360 + r, 360 - r);
            } while (!this.checkPosition(y, r));

            this.environment.AddPlanet(this.selectFactory(), new Vector2(640 + SideBuffer + r + this.environment.Offset, y), r);
        }
        private IPlanetFactory selectFactory()
        {
            double a = GlobalRandom.NextDouble();

            if (a < this.fireplanetChance && this.fireplanetCount < 2 * minPlanets * this.fireplanetChance)
            {
                return(new FirePlanetFactory());
            }
            else if (a < this.repellingPlanetChance + this.fireplanetChance && this.repellingPlanetCount < 2 * minPlanets * this.repellingPlanetChance)
            {
                return(new RepellingPlanetFactory());
            }
            else
            {
                return(new OrdinaryPlanetFactory());
            }
        }
Exemple #9
0
        public void start()
        {
            var inputs  = new double[countIns][];
            var outputs = new double[countIns][];

            for (var i = 0; i < countIns; i++)
            {
                inputs[i]  = new double[] { GlobalRandom.NextDouble(-100, 100) };
                outputs[i] = new double[] { func(inputs[i][0]) };
            }

            var ins  = MatrixD.Build.DenseOfRowArrays(inputs);
            var outs = MatrixD.Build.DenseOfRowArrays(outputs);

            var nn = new net11Builder().build();

            nn.train(ins, outs, iterations, batchSize);
        }
Exemple #10
0
        public void backwardLearn()
        {
            var learnRate = GlobalRandom.NextDouble();
            int inSize, outSize;
            var countExamples = 3;

            inSize = outSize = 5;

            var input         = MatrixD.Build.Random(countExamples, inSize);
            var layer         = new BiasLayer(inSize);
            var nextGradients = MatrixD.Build.Random(countExamples, outSize);

            var expectedBiases = layer.Biases.ShallowCopy();
            var gradientSums   = nextGradients.ColumnSums().AsArray();

            for (int i = 0; i < expectedBiases.Length; i++)
            {
                expectedBiases[i] -= learnRate * gradientSums[i];
            }

            layer.backwardLearn(input, nextGradients, learnRate);
            Assert.IsTrue(expectedBiases.EEquals(layer.Biases));
        }