Ejemplo n.º 1
0
    def _create_vehicles(self):
        """
            Create some new random vehicles of a given type, and add them on the road.
        """
        self.vehicle = Vehicle(self.road, [0, 0], 2*np.pi*self.np_random.rand(), 0)
        self.road.vehicles.append(self.vehicle)

        lane = self.np_random.choice(self.road.network.lanes_list())
        self.goal = Obstacle(self.road, lane.position(lane.length/2, 0), heading=lane.heading)

        self.goal.COLLISIONS_ENABLED = False
        self.road.vehicles.insert(0, self.goal)

        lane_index = self.road.network.get_closest_lane_index(lane.position(lane.length/2, 0))
        side_lanes = [self.road.network.get_lane(side_lane_index) for side_lane_index in self.road.network.side_lanes(lane_index)]
        i = 1
        self.obstacles_features = []
        for side_lane in side_lanes:
            obstacle = Obstacle(self.road, side_lane.position(side_lane.length/2, 0), heading=side_lane.heading)
            obstacle.COLLISIONS_ENABLED = True
            self.road.vehicles.insert(i, obstacle)
            i += 1
            obstacle_feature = np.array([obstacle.position[0], obstacle.position[1], 0, 0, 
                math.cos(obstacle.heading), math.sin(obstacle.heading)]) / self.config['observation']['scales']
            self.obstacles_features.append(obstacle_feature)
Ejemplo n.º 2
0
    def _make_road(self):
        """
            Make a road composed of a straight highway and a merging lane.
        :return: the road
        """
        net = RoadNetwork()

        # Highway lanes
        ends = [150, 80, 80, 150]  # Before, converging, merge, after
        c, s, n = LineType.CONTINUOUS_LINE, LineType.STRIPED, LineType.NONE
        y = [0, StraightLane.DEFAULT_WIDTH]
        line_type = [[c, s], [n, c]]
        line_type_merge = [[c, s], [n, s]]
        for i in range(2):
            net.add_lane("a", "b", StraightLane([0, y[i]], [sum(ends[:2]), y[i]], line_types=line_type[i]))
            net.add_lane("b", "c", StraightLane([sum(ends[:2]), y[i]], [sum(ends[:3]), y[i]], line_types=line_type_merge[i]))
            net.add_lane("c", "d", StraightLane([sum(ends[:3]), y[i]], [sum(ends), y[i]], line_types=line_type[i]))

        # Merging lane
        amplitude = 3.25
        ljk = StraightLane([0, 6.5 + 4 + 4], [ends[0], 6.5 + 4 + 4], line_types=[c, c], forbidden=True)
        lkb = SineLane(ljk.position(ends[0], -amplitude), ljk.position(sum(ends[:2]), -amplitude),
                       amplitude, 2 * np.pi / (2*ends[1]), np.pi / 2, line_types=[c, c], forbidden=True)
        lbc = StraightLane(lkb.position(ends[1], 0), lkb.position(ends[1], 0) + [ends[2], 0],
                           line_types=[n, c], forbidden=True)
        net.add_lane("j", "k", ljk)
        net.add_lane("k", "b", lkb)
        net.add_lane("b", "c", lbc)
        road = Road(network=net, np_random=self.np_random, record_history=self.config["show_trajectories"])
        road.obstacles.append(Obstacle(road, lbc.position(ends[2], 0)))
        self.road = road
Ejemplo n.º 3
0
    def _create_vehicles(self):
        """
            Create some new random vehicles of a given type, and add them on the road.
        """
        self.vehicle = Vehicle(self.road, [0, 0],
                               2 * np.pi * self.np_random.rand(), 0)
        self.road.vehicles.append(self.vehicle)

        lane = self.np_random.choice(self.road.network.lanes_list())
        self.goal = Obstacle(self.road,
                             lane.position(lane.length / 2, 0),
                             heading=lane.heading)
        self.goal.COLLISIONS_ENABLED = False
        self.road.obstacles.append(self.goal)
Ejemplo n.º 4
0
    def _create_vehicles(self, parked_probability=0.75):
        """
            Create some new random vehicles of a given type, and add them on the road.
        """

        self.vehicle = Vehicle(self.road, self.vehicle_starting,
                               2 * np.pi * self.np_random.rand(), 0)
        self.road.vehicles.append(self.vehicle)

        goal_position = [
            self.np_random.choice([-2 * self.spots - 10, 2 * self.spots + 10]),
            0
        ]
        self.goal = Obstacle(self.road, goal_position, heading=0)
        self.goal.COLLISIONS_ENABLED = False
        self.road.vehicles.insert(0, self.goal)

        vehicles_type = utils.class_from_path(
            self.config["other_vehicles_type"])
        for i in range(self.config["vehicles_count"]):
            is_parked = self.np_random.rand() <= parked_probability
            if not is_parked:
                # Just an effort to spread the vehicles out
                idx = self.np_random.randint(0, self.num_middle_lanes)
                longitudinal = (i * 5) - (self.x_range /
                                          8) * self.np_random.randint(-1, 1)
                self.road.vehicles.append(
                    vehicles_type.make_on_lane(self.road, ("d", "e", idx),
                                               longitudinal,
                                               velocity=2))
            else:
                lane = ("a", "b",
                        i) if self.np_random.rand() >= 0.5 else ("b", "c", i)
                self.road.vehicles.append(
                    Vehicle.make_on_lane(self.road, lane, 4, velocity=0))

        for v in self.road.vehicles:  # Prevent early collisions
            if v is not self.vehicle and np.linalg.norm(
                    v.position - self.vehicle.position) < 20:
                self.road.vehicles.remove(v)