예제 #1
0
    def __init__(self):
        # self.stackSize = 10
        self.scanSize = 1080
        self.action_space = spaces.Box(low=np.array([-1, -1]),
                                       high=np.array([1, 1]),
                                       dtype=np.float32)
        # stacked self.stackSize latest observations of (scan, x,y,theta,vel_x, vel_y, vel_z)*2 (we get the data from the opponent as well)
        self.observation_space = spaces.Box(low=0.0,
                                            high=1.0,
                                            shape=((self.scanSize + 6) * 2, ),
                                            dtype=np.float32)
        VecEnv.__init__(self, 2, self.observation_space, self.action_space)
        self.keys, shapes, dtypes = obs_space_info(self.observation_space)

        self.actions = None

        # simualtor params
        self.params_set = False
        self.map_inited = False
        # params list is [mu, h_cg, l_r, cs_f, cs_r, I_z, mass]
        self.params = []
        self.num_agents = 2
        self.timestep = 0.01
        self.map_path = None
        self.map_img = None
        self.ego_idx = 0
        self.timeout = 120.0
        # radius to consider done
        self.start_thresh = 0.5  # 10cm
        # env states
        # more accurate description should be ego car state
        # might not need to keep scan
        self.x = None
        self.y = None
        self.theta = None

        self.in_collision = False
        self.collision_angle = None

        # loop completion
        self.near_start = True
        self.num_toggles = 0

        # race info
        self.lap_times = [0.0, 0.0]
        self.lap_counts = [0, 0]

        self.map_height = 0.0
        self.map_width = 0.0
        self.map_resolution = 0.0
        self.free_thresh = 0.0
        self.origin = []
        self.port = 6666
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.PAIR)
        self.socket.connect('tcp://localhost:6666')
        print('Gym env - Connected env to port: ' + str(self.port))
        self.sim_p = None
        print('Gym env - env created, waiting for params...')
    def __init__(self, env_count=1, device="cpu"):
        self.gravity = 9.8
        self.masscart = 1.0
        self.masspole = 0.1
        self.total_mass = self.masspole + self.masscart
        self.length = 0.5  # actually half the pole's length
        self.polemass_length = self.masspole * self.length
        self.force_mag = 10.0
        self.tau = 0.02  # seconds between state updates
        self.kinematics_integrator = "euler"

        # Angle at which to fail the episode
        self.theta_threshold_radians = 12 * 2 * math.pi / 360
        self.x_threshold = 2.4
        self.env_count = env_count
        self.num_envs = env_count

        # Angle limit set to 2 * theta_threshold_radians so failing observation
        # is still within bounds.
        high = np.array(
            [
                self.x_threshold * 2,
                np.finfo(np.float32).max,
                self.theta_threshold_radians * 2,
                np.finfo(np.float32).max,
            ],
            dtype=np.float32,
        )

        self.action_space = spaces.Discrete(2)
        self.observation_space = spaces.Box(-high, high, dtype=np.float32)

        self.seed()
        self.viewer = None
        self.state: Optional[th.Tensor] = None
        self.done = th.full([env_count], True, dtype=th.bool, device=device)
        self.state = th.zeros([self.env_count, 4], dtype=th.float32, device=device)

        self.device = device
        VecEnv.__init__(self, env_count, self.observation_space, self.action_space)

        obs_space = self.observation_space
        self.buf_obs = torch.zeros((1,) + (self.num_envs,) + tuple(obs_space.shape)).to(
            device
        )

        self.buf_dones = torch.zeros((self.num_envs,), dtype=torch.bool).to(device)
        self.buf_rews = torch.zeros((self.num_envs,), dtype=torch.float32).to(device)
        self.buf_infos = [{} for _ in range(self.num_envs)]
        self.actions: torch.FloatTensor = None
        self.logged_error = False
        self.envs = [self]