Beispiel #1
0
 /// <summary>
 /// For activation functions that accept auxiliary arguments; generates random initial values for aux arguments for newly
 /// added nodes (from an 'add neuron' mutation).
 /// </summary>
 public double[] GetRandomAuxArgs(XorShiftRandom rng, double connectionWeightRange)
 {
     double[] auxArgs = new double[2];
     auxArgs[0] = (rng.NextDouble() - 0.5) * 2.0;
     auxArgs[1] = rng.NextDouble();
     return(auxArgs);
 }
Beispiel #2
0
        /// <summary>
        /// Initialise agent and prey positions. The prey is positioned randomly with at least 4 empty squares between it and a wall (in all directions).
        /// The agent is positioned randomly but such that the prey is within sensor range (distance 2 or less).
        /// </summary>
        public void InitPositions()
        {
            // Random pos at least 4 units away from any wall.
            _preyPos._x = 4 + _rng.Next(_gridSize - 8);
            _preyPos._y = 4 + _rng.Next(_gridSize - 8);

            // Agent position. Within range of the prey.
            double t = 2.0 * Math.PI * _rng.NextDouble();   // Random angle.
            double r = 2.0 + _rng.NextDouble() * 2.0;       // Distance between 2 and 4.

            _agentPos._x = _preyPos._x + (int)Math.Floor(Math.Cos(t) * r);
            _agentPos._y = _preyPos._y + (int)Math.Floor(Math.Sin(t) * r);
        }
Beispiel #3
0
 public void NextDouble10M()
 {
     for (int i = 0; i < __loops; i++)
     {
         _rng.NextDouble();
     }
 }
Beispiel #4
0
        public void TestProbabilisticRound()
        {
            var rng = new XorShiftRandom(0);

            for (int i = 0; i < 1000000; i++)
            {
                double valReal  = 100 * rng.NextDouble();
                double valRound = NumericsUtils.ProbabilisticRound(valReal, rng);
                Assert.IsTrue(valRound == Math.Floor(valReal) || valRound == Math.Ceiling(valReal));
            }
        }
Beispiel #5
0
        public void NextDouble()
        {
            int            sampleCount = 10000000;
            XorShiftRandom rng         = new XorShiftRandom();

            double[] sampleArr = new double[sampleCount];

            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = rng.NextDouble();
            }

            UniformDistributionTest(sampleArr, 0.0, 1.0);
        }
Beispiel #6
0
        private void PerformMutationOp_Position()
        {
            int oldPos = (int)_strmA.Position;
            int newPos = (int)(_rng.NextDouble() * 1.02 * _strmA.Position);

            _strmA.Position = newPos;
            _strmB.Position = newPos;

            Debug.WriteLine(string.Format("Position = {0} (was {1})", newPos, oldPos));
        }
Beispiel #7
0
        /// <summary>
        /// Take a sample from the standard gaussian distribution, i.e. with mean of 0 and standard deviation of 1.
        /// </summary>
        public double SampleStandard()
        {
            for (; ;)
            {
                // Select box at random.
                byte   u    = _rng.NextByte();
                int    i    = (int)(u & 0x7F);
                double sign = ((u & 0x80) == 0) ? -1.0 : 1.0;

                // Generate uniform random value with range [0,0xffffffff].
                uint u2 = _rng.NextUInt();

                // Special case for the base segment.
                if (0 == i)
                {
                    if (u2 < _xComp[0])
                    {                       // Generated x is within R0.
                        return(u2 * __UIntToU * _A_Div_Y0 * sign);
                    }
                    // Generated x is in the tail of the distribution.
                    return(SampleTail() * sign);
                }

                // All other segments.
                if (u2 < _xComp[i])
                {                   // Generated x is within the rectangle.
                    return(u2 * __UIntToU * _x[i] * sign);
                }

                // Generated x is outside of the rectangle.
                // Generate a random y coordinate and test if our (x,y) is within the distribution curve.
                // This execution path is relatively slow/expensive (makes a call to Math.Exp()) but relatively rarely executed,
                // although more often than the 'tail' path (above).
                double x = u2 * __UIntToU * _x[i];
                if (_y[i - 1] + ((_y[i] - _y[i - 1]) * _rng.NextDouble()) < GaussianPdfDenorm(x))
                {
                    return(x * sign);
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// Add objects to the Box2d world.
        /// </summary>
        protected override void PopulateWorld()
        {
            // ==== Define the ground body ====
            BodyDef groundBodyDef = new BodyDef();

            groundBodyDef.Position.Set(_trackLengthHalf, -1f);

            // Call the body factory which creates the ground box shape.
            // The body is also added to the world.
            Body groundBody = _world.CreateBody(groundBodyDef);

            // Define the ground box shape.
            PolygonDef groundShapeDef = new PolygonDef();

            // The extents are the half-widths of the box.
            groundShapeDef.SetAsBox(_trackLengthHalf + 1f, 1f);
            groundShapeDef.Friction            = _simParams._defaultFriction;
            groundShapeDef.Restitution         = _simParams._defaultRestitution;
            groundShapeDef.Filter.CategoryBits = 0x3;

            // Add the ground shape to the ground body.
            groundBody.CreateShape(groundShapeDef);

            // Add some small mounds/bumps to the ground.
            for (float x = -1f; x < 40f; x += 0.3f + ((float)_rng.NextDouble() * 0.2f))
            {
                WalkerWorldUtils.CreateMound(_world, x, 0f, _simParams._defaultFriction, _simParams._defaultRestitution);
            }

            // ==== Define walker torso.
            float walkerX = 0f;
            float walkerY = 1.55f;// + ((float)_rng.NextDouble() * 0.1f);

            BodyDef torsoBodyDef = new BodyDef();

            torsoBodyDef.Position.Set(walkerX, walkerY);
            torsoBodyDef.IsBullet = true;

            // Create walker torso.
            _torsoBody = _world.CreateBody(torsoBodyDef);
            PolygonDef torsoShapeDef = new PolygonDef();

            torsoShapeDef.SetAsBox(0.10f, 0.45f);
            torsoShapeDef.Friction            = _simParams._defaultFriction;
            torsoShapeDef.Restitution         = 0f;
            torsoShapeDef.Density             = 2f;
            torsoShapeDef.Filter.CategoryBits = 0x2;
            _torsoBody.CreateShape(torsoShapeDef);
            _torsoBody.SetMassFromShapes();

            // ===== Create legs.
            // Leg joint definition.
            RevoluteJointDef jointDef = new RevoluteJointDef();

            jointDef.CollideConnected = false;
            jointDef.EnableMotor      = true;
            jointDef.MaxMotorTorque   = 0f;

            // Other re-usable stuff .
            const float legRadius       = 0.05f; // Half the thickness of the leg
            Vec2        upperLegPosBase = new Vec2(walkerX, walkerY - 0.4f);
            Vec2        lowerLegPosBase = new Vec2(walkerX, walkerY - 0.9f);

            // ===== Create left leg.
            // Upper leg.
            Body upperLeftLegBody = CreatePole(upperLegPosBase, 0.5f, (float)SysMath.PI, legRadius, 2f, 0x1);

            // Join to torso (hip joint)
            jointDef.Initialize(_torsoBody, upperLeftLegBody, upperLegPosBase);
            _leftHipJoint = (RevoluteJoint)_world.CreateJoint(jointDef);

            // Lower leg.
            _leftLowerLegBody = CreatePole(lowerLegPosBase, __lowerLegLength, (float)SysMath.PI, legRadius, 2f, 0x1);
            // Join to upper leg (knee joint)
            jointDef.Initialize(upperLeftLegBody, _leftLowerLegBody, lowerLegPosBase);
            _leftKneeJoint = (RevoluteJoint)_world.CreateJoint(jointDef);

            // ===== Create right leg.
            // Upper leg.
            Body upperRightLegBody = CreatePole(upperLegPosBase, 0.5f, (float)SysMath.PI, legRadius, 2f, 0x1);

            // Join to torso (hip joint)
            jointDef.Initialize(_torsoBody, upperRightLegBody, upperLegPosBase);
            _rightHipJoint = (RevoluteJoint)_world.CreateJoint(jointDef);

            // Lower leg.
            _rightLowerLegBody = CreatePole(lowerLegPosBase, __lowerLegLength, (float)SysMath.PI, legRadius, 2f, 0x1);
            // Join to upper leg (knee joint)
            jointDef.Initialize(upperRightLegBody, _rightLowerLegBody, lowerLegPosBase);
            _rightKneeJoint = (RevoluteJoint)_world.CreateJoint(jointDef);
        }