コード例 #1
0
ファイル: CartPoleEnv.cs プロジェクト: uzbekdev1/DeepRL
        public override byte[] Render(bool toRgbArray = false)
        {
            const int SCREEN_WIDTH  = 600;
            const int SCREEN_HEIGHT = 400;

            float worldWidth = X_THRESHOLD * 2;
            float scale      = SCREEN_WIDTH / worldWidth;
            float cartY      = 100; // TOP OF CART
            float poleWidth  = 10.0f;
            float poleLen    = scale * (2 * LENGTH);
            float cartWidth  = 50.0f;
            float cartHeight = 30.0f;

            if (Viewer == null)
            {
                Viewer = new Rendering.Viewer(SCREEN_WIDTH, SCREEN_HEIGHT);
                float l          = -cartWidth / 2;
                float r          = cartWidth / 2;
                float t          = cartHeight / 2;
                float b          = -cartHeight / 2;
                float axleOffset = cartHeight / 4.0f;
                var   cart       = new Rendering.FilledPolygon(new List <float[]> {
                    new[] { l, b }, new[] { l, t }, new[] { r, t }, new[] { r, b }
                });
                CartTrans = new Rendering.Transform();
                cart.AddAttr(CartTrans);
                Viewer.AddGeom(cart);
                l    = -poleWidth / 2;
                r    = poleWidth / 2;
                t    = poleLen - poleWidth / 2;
                b    = -poleWidth / 2;
                Pole = new Rendering.FilledPolygon(new List <float[]> {
                    new[] { l, b }, new[] { l, t }, new[] { r, t }, new[] { r, b }
                });
                Pole.SetColor(.8f, .6f, .4f);
                PoleTrans = new Rendering.Transform(new[] { 0, axleOffset });
                Pole.AddAttr(PoleTrans);
                Pole.AddAttr(CartTrans);
                Viewer.AddGeom(Pole);
                Axle = Rendering.MakeCircle(poleWidth / 2);
                Axle.AddAttr(PoleTrans);
                Axle.AddAttr(CartTrans);
                Axle.SetColor(.5f, .5f, .8f);
                Viewer.AddGeom(Axle);
                Track = new Rendering.Line(new [] { 0, cartY }, new [] { SCREEN_WIDTH, cartY });
                Track.SetColor(0, 0, 0);
                Viewer.AddGeom(Track);
            }

            if (State == null)
            {
                return(null);
            }

            {
                // Edit the pole polygon vertex
                float l = -poleWidth / 2, r = poleWidth / 2, t = poleLen - poleWidth / 2, b = poleWidth / 2;
                Pole.Vertices = new List <float[]> {
                    new[] { l, b }, new[] { l, t }, new[] { r, t }, new[] { r, b }
                };
            }

            var cartX = State[0] * scale + SCREEN_WIDTH / 2.0f; // MIDDLE OF CART

            CartTrans.SetTranslation(cartX, cartY);
            PoleTrans.SetRotation(-State[2]);

            byte[] rgbArray = toRgbArray ? new byte[SCREEN_WIDTH * SCREEN_HEIGHT * 3] : null;
            Viewer.Render(rgbArray);
            return(rgbArray);
        }
コード例 #2
0
        public override byte[] Render(bool toRgbArray = false)
        {
            const int SCREEN_WIDTH  = 600;
            const int SCREEN_HEIGHT = 400;

            float     world_width = max_position - min_position;
            float     scale       = SCREEN_WIDTH / world_width;
            const int carwidth    = 40;
            const int carheight   = 20;

            if (Viewer == null)
            {
                Viewer = new Rendering.Viewer(SCREEN_WIDTH, SCREEN_HEIGHT);
                var xs  = Neuro.Tools.LinSpace(min_position, max_position, 100);
                var ys  = xs.Select(x => Height(x)).ToList();
                var xys = xs.Zip(ys, (x, y) => new[] { (x - min_position) * scale, y * scale }).ToList();

                Rendering.Polyline track = Rendering.MakePolyLine(xys) as Rendering.Polyline;
                track.SetLineWidth(4);
                Viewer.AddGeom(track);

                float clearance = 10;

                float l = -carwidth / 2, r = carwidth / 2, t = carheight, b = 0;
                var   car = new Rendering.FilledPolygon(new List <float[]> {
                    new[] { l, b }, new[] { l, t }, new[] { r, t }, new[] { r, b }
                });
                car.AddAttr(new Rendering.Transform(new[] { 0, clearance }));
                CarTrans = new Rendering.Transform();
                car.AddAttr(CarTrans);
                Viewer.AddGeom(car);
                var frontwheel = Rendering.MakeCircle(carheight / 2.5f);
                frontwheel.SetColor(.5f, .5f, .5f);
                frontwheel.AddAttr(new Rendering.Transform(new[] { carwidth / 4, clearance }));
                frontwheel.AddAttr(CarTrans);
                Viewer.AddGeom(frontwheel);
                var backwheel = Rendering.MakeCircle(carheight / 2.5f);
                backwheel.AddAttr(new Rendering.Transform(new[] { -carwidth / 4, clearance }));
                backwheel.AddAttr(CarTrans);
                backwheel.SetColor(.5f, .5f, .5f);
                Viewer.AddGeom(backwheel);
                float flagx    = (goal_position - min_position) * scale;
                float flagy1   = Height(goal_position) * scale;
                float flagy2   = flagy1 + 50;
                var   flagpole = new Rendering.Line(new[] { flagx, flagy1 }, new[] { flagx, flagy2 });
                Viewer.AddGeom(flagpole);
                var flag = new Rendering.FilledPolygon(new List <float[]>()
                {
                    new[] { flagx, flagy2 }, new[] { flagx, flagy2 - 10 }, new[] { flagx + 25, flagy2 - 5 }
                });
                flag.SetColor(.8f, .8f, 0);
                Viewer.AddGeom(flag);
            }

            float pos = State[0];

            CarTrans.SetTranslation((pos - min_position) * scale, Height(pos) * scale);
            CarTrans.SetRotation((float)Math.Cos(3 * pos));

            Viewer.Render();

            return(null);
        }