Interface onto a walker.
Beispiel #1
0
        private double EvaluateInner(IBlackBox box)
        {
            const double angleLimit = System.Math.PI / 3.0;

            // Init Box2D world.
            WalkerWorld world = new WalkerWorld(_rng);

            world.InitSimulationWorld();

            // Create an interface onto the walker.
            WalkerInterface walkerIface = world.CreateWalkerInterface();

            // Create a neural net controller for the walker.
            NeuralNetController walkerController = new NeuralNetController(walkerIface, box, world.SimulationParameters._frameRate);

            Vec2   hipPos    = walkerIface.LeftLegIFace.HipJointPosition;
            double torsoYMin = hipPos.Y;
            double torsoYMax = hipPos.Y;

            // Run the simulation.
            LegInterface leftLeg  = walkerIface.LeftLegIFace;
            LegInterface rightLeg = walkerIface.RightLegIFace;
            //double totalAppliedTorque = 0.0;
            int timestep = 0;

            for (; timestep < _maxTimesteps; timestep++)
            {
                // Simulate one timestep.
                world.Step();
                walkerController.Step();
                //totalAppliedTorque += walkerIface.TotalAppliedTorque;

                // Track hip joint height and min/max extents.
                hipPos = walkerIface.LeftLegIFace.HipJointPosition;
                if (hipPos.Y < torsoYMin)
                {
                    torsoYMin = hipPos.Y;
                }
                else if (hipPos.Y > torsoYMax)
                {
                    torsoYMax = hipPos.Y;
                }

                double heightRange = torsoYMax - torsoYMin;

                // Test for stopping conditions.
                if (hipPos.X < -0.7 || hipPos.X > 150f || heightRange > 0.20 ||
                    System.Math.Abs(leftLeg.HipJointAngle) > angleLimit || System.Math.Abs(leftLeg.KneeJointAngle) > angleLimit ||
                    System.Math.Abs(rightLeg.HipJointAngle) > angleLimit || System.Math.Abs(rightLeg.KneeJointAngle) > angleLimit)
                {   // Stop simulation.
                    break;
                }
            }

            // Final fitness calcs / adjustments.
            return(System.Math.Max(hipPos.X, 0.0));
        }
        /// <summary>
        /// Create a Box2D simulation world.
        /// </summary>
        protected override SimulationWorld CreateSimulationWorld()
        {
            // Init Box2D world.
            WalkerWorld world = new WalkerWorld(new XorShiftRandom());

            world.InitSimulationWorld();

            // Create an interface onto the walker.
            WalkerInterface walkerIface = world.CreateWalkerInterface();

            // Create a neural net controller for the walker.
            _walkerController = new NeuralNetController(walkerIface, _box, world.SimulationParameters._frameRate);
            return(world);
        }
Beispiel #3
0
        /// <summary>
        /// Create a Box2D simulation world.
        /// </summary>
        protected override SimulationWorld CreateSimulationWorld()
        {
            // Init Box2D world.
            WalkerWorld world = new WalkerWorld();

            world.InitSimulationWorld();

            // Create an interface onto the walker.
            WalkerInterface walkerIface = world.CreateWalkerInterface();

            // Create a neural net controller for the walker.
            _walkerController = new NeuralNetController(walkerIface, _box);
            return(world);
        }
Beispiel #4
0
        /// <summary>
        /// Evaluate the provided IBlackBox.
        /// </summary>
        public FitnessInfo Evaluate(IBlackBox box)
        {
            // Init Box2D world.
            WalkerWorld world = new WalkerWorld(_rng);

            world.InitSimulationWorld();

            // Create an interface onto the walker.
            WalkerInterface walkerIface = world.CreateWalkerInterface();

            // Create a neural net controller for the walker.
            NeuralNetController walkerController = new NeuralNetController(walkerIface, box);

            // Run the simulation.
            LegInterface leftLeg            = walkerIface.LeftLegIFace;
            LegInterface rightLeg           = walkerIface.RightLegIFace;
            double       angleLimit         = Math.PI * 0.8;
            double       totalAppliedTorque = 0.0;
            int          timestep           = 0;

            for (; timestep < _maxTimesteps; timestep++)
            {
                // Simulate one timestep.
                world.Step();
                walkerController.Step();
                totalAppliedTorque += walkerIface.TotalAppliedTorque;

                // Test for stopping conditions scoring zero.
                if (leftLeg.FootHeight > 0.3f && rightLeg.FootHeight > 0.3f ||
                    Math.Abs(leftLeg.HipJointAngle) > angleLimit || Math.Abs(leftLeg.KneeJointAngle) > angleLimit ||
                    Math.Abs(rightLeg.HipJointAngle) > angleLimit || Math.Abs(rightLeg.KneeJointAngle) > angleLimit)
                {   // Stop simulation.
                    return(new FitnessInfo(0.0, walkerIface.TorsoPosition.X));
                }

                // Test for scoring stopping conditions.
                if (walkerIface.TorsoPosition.X < -0.7 || walkerIface.TorsoPosition.X > 150f || walkerIface.TorsoPosition.Y < 0.6f)
                {   // Stop simulation.
                    break;
                }
            }

            _evalCount++;
            double fitness = Math.Max(0.0, walkerIface.TorsoPosition.X);

            return(new FitnessInfo(fitness, walkerIface.TorsoPosition.X));
        }
Beispiel #5
0
 /// <summary>
 /// Construct with the provided player interface and black box controller.
 /// </summary>
 public NeuralNetController(WalkerInterface iface, IBlackBox box, int simFrameRate)
     : base(iface)
 {
     _box          = box;
     _sineWaveIncr = (2.0 * Math.PI) / simFrameRate;
 }
Beispiel #6
0
 /// <summary>
 /// Construct with the provided player interface and black box controller.
 /// </summary>
 public NeuralNetController(WalkerInterface iface, IBlackBox box)
     : base(iface)
 {
     _box = box;
 }
 /// <summary>
 /// Construct with the provided player interface and black box controller.
 /// </summary>
 public NeuralNetController(WalkerInterface iface, IBlackBox box, int simFrameRate) 
     : base(iface) 
 {
     _box = box;
     _sineWaveIncr = (2.0 * Math.PI) / simFrameRate;
 }
 /// <summary>
 /// Construct with the provided player interface and black box controller.
 /// </summary>
 public NeuralNetController(WalkerInterface iface, IBlackBox box) 
     : base(iface) 
 {
     _box = box;
 }