在 openAI Gym 中实现关于另一个主播的旋转

Achieve Rotation in openAI Gym about another anchor

我们开始构建基于 cartpole-v0 的环境。我们正试图实现我们的杆的类似行为,因此它不会在中心点旋转,而是在底部旋转。我们正在使用 gym.classic_control.rendering 中的 set_rotation 函数。但是没有机会设置锚点。

我们尝试在不同方向旋转之前平移 filledPolygon,但锚点仍位于中心点。

import math
import gym
from gym import spaces, logger
from gym.utils import seeding
import numpy as np
from os import path  

class THT_Env(gym.Env):
  ''' 
  I shorten the code to the render. The other parts of the code is working fine.
  '''
    def render(self, mode='human'):
        screen_width = 600
        screen_height = 600

        lead_width = 6  #Lead diameter = 0.6mm
        lead_length = 103 #Lead lenght ca. 10.3 mm
        lead_spacing = 50 #Lead spacing 5 mm

        body_height = 80 #Body height 8 mm
        body_width = 65 #Body width 6.5 mm

        pcb_thickness = 16 #1.6 mm
        #pcb_hole_diameter = 9 #0.9mm
        pcb_side = 270.5 
        pcb_middle = 41


        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)
            # Initialize Body
            fname = path.join(path.dirname(__file__), "assets/WYO_1nM.png")
            body = rendering.Image(fname,body_width, body_height)
            self.bodytrans = rendering.Transform()
            body.add_attr(self.bodytrans)
            self.viewer.add_geom(body)

            # Initialize Lead 1
            l, r, t, b = -lead_width/2, lead_width/2, lead_length/2, -lead_length/2
            lead_1 = rendering.FilledPolygon([(l,b),(l,t),(r,t),(r,b)])
            lead_1.set_color(.4, .4, .4)
            self.lead_1_trans = rendering.Transform(translation=(lead_spacing/2, (-lead_length-body_height)/2))
            lead_1.add_attr(self.lead_1_trans)
            lead_1.add_attr(self.bodytrans)
            self.viewer.add_geom(lead_1)

            lead_2 = rendering.FilledPolygon([(l,b),(l,t),(r,t),(r,b)])
            lead_2.set_color(.4, .4, .4)
            #self.lead_2_trans = rendering.Transform(translation=(-lead_spacing/2, (-lead_length-body_height)/2))
            self.lead_2_trans = rendering.Transform(translation=(0, (-lead_length-body_height)/2),rotation=np.pi/2)
            lead_2.add_attr(self.lead_2_trans)
            lead_2.add_attr(self.bodytrans)
            self.viewer.add_geom(lead_2)

            l, r, t, b = -pcb_side/2, pcb_side/2, pcb_thickness/2, -pcb_thickness/2
            pcb_1 = rendering.FilledPolygon([(l,b),(l,t),(r,t),(r,b)])
            pcb_1.set_color(.0, .42, .0)
            self.pcb_1_trans = rendering.Transform(translation=(0+pcb_side/2, 110))
            pcb_1.add_attr(self.pcb_1_trans)
            self.viewer.add_geom(pcb_1)

            pcb_2 = rendering.FilledPolygon([(l,b),(l,t),(r,t),(r,b)])
            pcb_2.set_color(.0, .42, .0)
            self.pcb_2_trans = rendering.Transform(translation=(screen_width-pcb_side/2, 110))
            pcb_2.add_attr(self.pcb_2_trans)
            self.viewer.add_geom(pcb_2)

            l, r, t, b = -pcb_middle/2, pcb_middle/2, pcb_thickness/2, -pcb_thickness/2
            self.pcb_mid = rendering.FilledPolygon([(l,b),(l,t),(r,t),(r,b)])
            self.pcb_mid.set_color(.0, .42, .0)
            self.pcb_mid_trans = rendering.Transform(translation=(screen_width/2, 110))
            self.pcb_mid.add_attr(self.pcb_mid_trans)
            self.viewer.add_geom(self.pcb_mid)

        if self.state is None: return None

        x = self.state
        body_x = x[0]+screen_width/2
        body_y = x[1]+screen_height/2+200
        self.bodytrans.set_translation(body_x, body_y)
        #self.lead_1_trans.set_translation(0,-lead_length/2)
        #self.lead_1_trans.set_rotation(np.pi/2)

        return self.viewer.render(return_rgb_array= mode == 'rgb_array')

    def close(self):
        if self.viewer:
            self.viewer.close()
            self.viewer = None

右图的旋转导致极(铅)不再连接到body的情况。我期望的是 Anchor 位于 body.

的下部

我们从一开始就想错了。在我们尝试使用 gym 之前,我们用 pyglet 构建了一些东西,并了解到 pyglet 会改变图像的锚点。在建造我们自己的健身房时,我们将我们的线索集中在一起,并认为我们也可以更换锚点。短暂的停顿后,我意识到 cartpole 的杆子以 l,r,t,b 为中心位于底部。

所以在我们的情况下,简单的解决方案是更改此行

l, r, t, b = -lead_width/2, lead_width/2, lead_length/2, -lead_length/2

进入这一行

l, r, t, b = -lead_width/2, lead_width/2, 0, -lead_length

所以结果是:

健身房多边形的锚点由顶点控制。