Esempio n. 1
0
        public override Image Render(string mode = "human")
        {
            float       b, t, r, l;
            const int   screen_width  = 600;
            const int   screen_height = 400;
            const float world_width   = x_threshold * 2;
            const float scale         = screen_width / world_width;
            const int   carty         = 300;
            const float polewidth     = 10.0f;
            const float poleheight    = scale * (2 * length);
            const float cartwidth     = 50.0f;
            const float cartheight    = 30.0f;


            if (_viewer == null)
            {
                lock (this) {
                    //to prevent double initalization.
                    if (_viewer == null)
                    {
                        if (_viewerFactory == null)
                        {
                            _viewerFactory = NullEnvViewer.Factory;
                        }
                        _viewer = _viewerFactory(screen_width, screen_height, "cartpole-v1");
                    }
                }
            }

            //pole
            l = -polewidth / 2;
            r = polewidth / 2;
            t = poleheight - polewidth / 2;
            b = -polewidth / 2;
            var pole   = new RectangularPolygon(-polewidth / 2, carty - poleheight, polewidth, poleheight);
            var circle = new EllipsePolygon(0, carty - polewidth / 2, polewidth / 2);

            //cart
            l = -cartwidth / 2;
            r = cartwidth / 2;
            t = cartheight / 2;
            b = -cartheight / 2;
            var axleoffset = cartheight / 4.0;
            var cart       = new RectangularPolygon(-cartwidth / 2, carty - cartheight / 2, cartwidth, cartheight);
            var draw       = new List <(IPath, Rgba32)>();

            if (!(state is null))
            {
                var center_x = (float)(state.GetDouble(0) * scale + screen_width / 2.0f);
                //no y cuz it doesnt change.
                var cbounds    = circle.Bounds;
                var pivotPoint = new PointF(cbounds.X + cbounds.Width / 2f, cbounds.Y + cbounds.Height / 2f);

                draw.Add((cart.Translate(center_x, 0), Rgba32.Black));
                draw.Add((pole.Transform(Matrix3x2.CreateRotation((float)state.GetDouble(2), pivotPoint)).Translate(center_x, 0), new Rgba32(204, 153, 102)));
                draw.Add((circle.Translate(center_x, 0), Rgba32.Teal));
            }
Esempio n. 2
0
        public CartPoleEnv(IEnvironmentViewerFactoryDelegate viewerFactory, NumPyRandom randomState)
        {
            _viewerFactory = viewerFactory;
            // Angle limit set to 2 * theta_threshold_radians so failing observation is still within bounds
            var high = np.array(x_threshold * 2, float.MaxValue, theta_threshold_radians * 2, float.MaxValue);

            ActionSpace      = new Discrete(2);
            ObservationSpace = new Box(-high, high, np.float32);
            random           = randomState ?? np.random.RandomState();

            Metadata = new Dict("render.modes", new[] { "human", "rgb_array" }, "video.frames_per_second", 50);
        }
Esempio n. 3
0
 public CartPoleEnv(IEnvironmentViewerFactoryDelegate viewerFactory) : this(viewerFactory, null)
 {
 }