예제 #1
0
    def __init__(self):
        seed(1337)
        self.free_cam = Camera([2.0, 1.0, 2.0])
        self.free_cam.set_rotation(45.0, 100.0)

        self.init_window()
        self.init_gl()
        self.resize_gl(self.win_res)

        points = [(4.732657104, 0.0), \
                (4.811864754, 0.188118169), \
                (5.049487705, 1.841577869), \
                (5.16829918, 2.148507514), \
                (5.188101093, 3.861372951), \
                (4.782161885, 4.405925546), \
                (4.762359973, 5.049487705), \
                (4.940577186, 5.851465164), \
                (4.910874317, 6.910867486), \
                (4.841567623, 7.2475)]

        self.road = Road(points)

        for i in xrange(30):
            v1 = Vehicle(uniform(95.0, 140.0), self.road, uniform(0.1, 7.0))
            self.road.vehicles.append(v1)

        sep = 0.25
        for i in xrange(int(6.25 / sep)):
            n1 = SensorNode(sep * i + 0.25, self.road, i)
            self.road.sensors.append(n1)

        af = AccidentField(5.0, 0.25, self.road)
        self.road.accidents.append(af)

        self.clock = pygame.time.Clock()
예제 #2
0
def one_second_turn(cars, simulation):
    the_road = Road()
    simulation.car_impact(cars)
    simulation.move_rules(cars)
    simulation.move_cars_on_road(the_road, cars)
    simulation.increase_car_speed(cars)

    return the_road.track
def loop(cars, simulation):

    the_road = Road()
    simulation.driving_rules(cars)
    simulation.car_collision(cars)
    simulation.drive_cars(the_road, cars)
    simulation.accelerate_cars(cars)
    return the_road.track
예제 #4
0
파일: highway_0.py 프로젝트: maxiaoba/rlkit
    def __init__(self,
                 obs_noise=0.,
                 x_actions=[0.,0.5,3.],
                 y_actions=[0,1],
                 driver_sigma = 0.,
                 control_cost=0.01,
                 collision_cost=2.,
                 survive_reward=0.01,
                 goal_reward=2.,
                 road=Road([RoadSegment([(-100.,0.),(100.,0.),(100.,8.),(-100.,8.)])]),
                 left_bound = -30.,
                 right_bound = 30.,
                 gap_min = 8.,
                 gap_max = 12.,
                 max_veh_num = 12,
                 num_updates=1,
                 dt=0.1,
                 **kwargs):

        self.obs_noise = obs_noise
        self.x_actions = x_actions
        self.y_actions = y_actions
        # we use target value instead of target change so system is Markovian
        self.rl_actions = list(itertools.product(x_actions,y_actions))
        self.num_updates = num_updates

        self.control_cost = control_cost
        self.collision_cost = collision_cost
        self.survive_reward = survive_reward
        self.goal_reward = goal_reward

        self.left_bound = left_bound
        self.right_bound = right_bound
        self.gap_min = gap_min
        self.gap_max = gap_max
        self.max_veh_num = max_veh_num
        self.label_dim = 2
        self.label_num = self.max_veh_num

        self._collision = False
        self._goal = False
        self._intentions = []
        self._lower_lane_next_idx = 1
        self._upper_lane_next_idx = int(self.max_veh_num/2.)+1

        self.car_length = 5.0
        self.car_width = 2.0
        self.car_max_accel = 5.0
        self.car_max_speed = 5.0
        self.car_max_rotation = 0.
        self.car_expose_level = 4
        self.driver_sigma = driver_sigma
        self.min_front_x = 6.
        self.min_back_x = 6.

        super(HighWay, self).__init__(
            road=road,
            cars=[],
            drivers=[],
            dt=dt,
            **kwargs,)
예제 #5
0
    def __init__(self,
                 yld=0.5,
                 vs_actions=[0.,0.5,3.],
                 t_actions=[-1.5,0.,1.5],
                 desire_speed=3.,
                 speed_cost=0.01,
                 t_cost=0.01,
                 control_cost=0.01,
                 collision_cost=1.,
                 outroad_cost=1.,
                 goal_reward=1.,
                 road=Road([RoadSegment([(-100.,0.),(100.,0.),(100.,8.),(-100.,8.)]),
                             RoadSegment([(-2,-10.),(2,-10.),(2,0.),(-2,0.)])]),
                 n_cars=3,
                 num_updates=1,
                 dt=0.1,
                 **kwargs):

        self.yld = yld
        self.vs_actions = vs_actions
        self.t_actions = t_actions
        # we use target value instead of target change so system is Markovian
        self.rl_actions = list(itertools.product(vs_actions,t_actions))
        self.num_updates = num_updates

        self.desire_speed = desire_speed
        self.speed_cost = speed_cost
        self.t_cost = t_cost
        self.control_cost = control_cost
        self.collision_cost = collision_cost
        self.outroad_cost = outroad_cost
        self.goal_reward = goal_reward
        self._collision = False
        self._outroad = False
        self._goal = False
        self._intentions = []

        car_length=5.0
        car_width=2.0
        car_max_accel=10.0
        car_max_speed=40.0
        car_expose_level=4
        cars = [Car(idx=cid, length=car_length, width=car_width, color=random.choice(BLUE_COLORS),
                          max_accel=car_max_accel, max_speed=car_max_speed,
                          expose_level=car_expose_level) for cid in range(n_cars)
                ]

        driver_sigma = 0.0
        s_min = 2.0
        min_overlap = 1.0
        drivers = []
        drivers.append(EgoDriver(trajectory=EgoTrajectory(),idx=0,car=cars[0],dt=dt))
        for did in range(1,n_cars):
            driver = TwoTDriver(idx=did, car=cars[did], dt=dt,
                        x_driver=IDMDriver(idx=did, car=cars[did], sigma=driver_sigma, s_min=s_min, axis=0, min_overlap=min_overlap, dt=dt), 
                        y_driver=PDDriver(idx=did, car=cars[did], sigma=driver_sigma, axis=1, dt=dt)) 
            drivers.append(driver)

        super(TIntersection, self).__init__(
            road=road,
            cars=cars,
            drivers=drivers,
            dt=dt,
            **kwargs,)
예제 #6
0
    def __init__(self,
                 yld=0.5,
                 obs_noise=0.,
                 v_noise=0.,
                 vs_actions=[0., 0.5, 3.],
                 t_actions=[0.],
                 desire_speed=3.,
                 driver_sigma=0.,
                 speed_cost=0.01,
                 t_cost=0.01,
                 control_cost=0.01,
                 collision_cost=2.,
                 outroad_cost=2.,
                 survive_reward=0.01,
                 goal_reward=2.,
                 road=Road([
                     RoadSegment([(-100., 0.), (100., 0.), (100., 8.),
                                  (-100., 8.)]),
                     RoadSegment([(-2, -10.), (2, -10.), (2, 0.), (-2, 0.)])
                 ]),
                 left_bound=-20.,
                 right_bound=20.,
                 gap_min=3.,
                 gap_max=10.,
                 max_veh_num=12,
                 num_updates=1,
                 dt=0.1,
                 des_front_gap_difference=0.1,
                 des_front_gap_interval=0.3,
                 **kwargs):

        self.yld = yld
        self.obs_noise = obs_noise
        self.v_noise = v_noise
        self.vs_actions = vs_actions
        self.t_actions = t_actions
        # we use target value instead of target change so system is Markovian
        self.rl_actions = list(itertools.product(vs_actions, t_actions))
        self.num_updates = num_updates
        self.des_front_gap_difference = des_front_gap_difference
        self.des_front_gap_interval = des_front_gap_interval

        self.desire_speed = desire_speed
        self.speed_cost = speed_cost
        self.t_cost = t_cost
        self.control_cost = control_cost
        self.collision_cost = collision_cost
        self.outroad_cost = outroad_cost
        self.survive_reward = survive_reward
        self.goal_reward = goal_reward

        self.left_bound = left_bound
        self.right_bound = right_bound
        self.gap_min = gap_min
        self.gap_max = gap_max
        self.max_veh_num = max_veh_num
        self.label_dim = 2
        self.label_num = self.max_veh_num

        self._collision = False
        self._outroad = False
        self._goal = False
        self._intentions = []
        self._lower_lane_next_idx = 1
        self._upper_lane_next_idx = int(self.max_veh_num / 2.) + 1

        self.car_length = 5.0
        self.car_width = 2.0
        self.car_max_accel = 10.0
        self.car_max_speed = 40.0
        self.car_max_rotation = np.pi / 18.
        self.car_expose_level = 4
        self.driver_sigma = driver_sigma
        self.s_des = 3.0
        self.s_min = 3.0
        self.min_overlap = 1.0

        super(TIntersectionLSTM, self).__init__(
            road=road,
            cars=[],
            drivers=[],
            dt=dt,
            **kwargs,
        )
예제 #7
0
    def __init__(self,
                 obs_noise=0.,
                 x_actions=[-5, -1., 0., 1., 5.],
                 y_actions=[0, 1],
                 driver_sigma=0.,
                 x_cost=0.01,
                 y_cost=0.01,
                 control_cost=0.01,
                 collision_cost=2.,
                 survive_reward=0.01,
                 goal_reward=2.,
                 road=Road([
                     RoadSegment([(-100., 0.), (100., 0.), (100., 12.),
                                  (-100., 12.)])
                 ]),
                 num_updates=1,
                 dt=0.1,
                 **kwargs):

        self.obs_noise = obs_noise
        self.x_actions = x_actions
        self.y_actions = y_actions
        # we use target value instead of target change so system is Markovian
        self.rl_actions = list(itertools.product(x_actions, y_actions))
        self.num_updates = num_updates

        self.x_cost = x_cost
        self.y_cost = y_cost
        self.control_cost = control_cost
        self.collision_cost = collision_cost
        self.survive_reward = survive_reward
        self.goal_reward = goal_reward

        self.left_bound = -30.
        self.right_bound = 30.
        self.x_start = -20.
        self.x_goal = 20.
        self.lane_start = 0
        self.lane_goal = 1
        self.gap_min = 6.  # 8.
        self.gap_max = 10.  # 12.
        self.max_veh_num = 12  #18
        self.label_dim = 2
        self.label_num = self.max_veh_num

        self._collision = False
        self._block = False
        self._goal = False
        self._terminal = False
        self._intentions = []
        self._empty_indices = list(range(1, self.max_veh_num + 1))

        self.car_length = 5.0
        self.car_width = 2.0
        self.car_max_accel = 5.0
        self.car_max_speed = 1.0
        self.ego_max_speed = 2.0
        self.car_max_rotation = 0.
        self.car_expose_level = 4
        self.driver_sigma = driver_sigma
        self.min_x = 6.
        self.min_y = 3.
        self.ego_min_x = 6.
        self.ego_min_y = 3.

        super(HighWay, self).__init__(
            road=road,
            cars=[],
            drivers=[],
            dt=dt,
            **kwargs,
        )
예제 #8
0
        for cid, car in enumerate(self._cars):
            car.update_render(camera_center)

        self.update_extra_render(extra_input)

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

    def close(self):
        if hasattr(self, 'viewer') and self.viewer:
            self.viewer.close()
            self.viewer = None


if __name__ == '__main__':
    import time
    road = Road(
        [RoadSegment([(-20., -1.5), (100, -1.5), (100, 7.5), (-20, 7.5)])])
    dt = 0.1
    n_cars = 2
    car_class = Car
    driver_class = XYSeperateDriver
    x_driver_class = IDMDriver
    y_driver_class = PDDriver
    driver_sigma = 0.0
    car_length = 5.0
    car_width = 2.0
    car_max_accel = 10.0
    car_max_speed = 40.0
    car_expose_level = 4
    cars = [
        car_class(idx=cid,
                  length=car_length,
예제 #9
0
    def __init__(self,
                 yld=0.5,
                 observe_mode='full',
                 label_mode='full',
                 normalize_obs=False,
                 vs_actions=[0., 0.5, 3.],
                 t_actions=[0.],
                 desire_speed=3.,
                 driver_sigma=0.,
                 speed_cost=0.0,
                 t_cost=0.0,
                 control_cost=0.0,
                 collision_cost=1.,
                 outroad_cost=1.,
                 survive_reward=0.0,
                 goal_reward=1.,
                 road=Road([
                     RoadSegment([(-100., 0.), (100., 0.), (100., 8.),
                                  (-100., 8.)]),
                     RoadSegment([(-2, -10.), (2, -10.), (2, 0.), (-2, 0.)])
                 ]),
                 left_bound=-20.,
                 right_bound=20.,
                 gap_min=3.,
                 gap_max=10.,
                 max_veh_num=12,
                 num_updates=1,
                 dt=0.1,
                 **kwargs):

        self.yld = yld
        self.observe_mode = observe_mode
        self.label_mode = label_mode
        self.normalize_obs = normalize_obs
        self.vs_actions = vs_actions
        self.t_actions = t_actions
        # we use target value instead of target change so system is Markovian
        self.rl_actions = list(itertools.product(vs_actions, t_actions))
        self.num_updates = num_updates

        self.desire_speed = desire_speed
        self.speed_cost = speed_cost
        self.t_cost = t_cost
        self.control_cost = control_cost
        self.collision_cost = collision_cost
        self.outroad_cost = outroad_cost
        self.survive_reward = survive_reward
        self.goal_reward = goal_reward
        self._collision = False
        self._outroad = False
        self._goal = False
        self._intentions = []

        self.left_bound = left_bound
        self.right_bound = right_bound
        self.gap_min = gap_min
        self.gap_max = gap_max
        self.max_veh_num = max_veh_num
        self.label_dim = 2
        if self.label_mode == 'full':
            if observe_mode == 'full':
                self.label_num = self.max_veh_num
        else:
            self.label_num = int(self.max_veh_num / 2) + 1

        self.car_length = 5.0
        self.car_width = 2.0
        self.car_max_accel = 10.0
        self.car_max_speed = 40.0
        self.car_expose_level = 4
        self.driver_sigma = driver_sigma
        self.s_des = 3.0
        self.s_min = 3.0
        self.min_overlap = 1.0

        super(TIntersectionExtreme, self).__init__(
            road=road,
            cars=[],
            drivers=[],
            dt=dt,
            **kwargs,
        )
예제 #10
0
class TrafficSim(object):
    fps = 60
    win_res = (1024, 768)

    keys_held = {
        pygame.K_w: False,
        pygame.K_a: False,
        pygame.K_s: False,
        pygame.K_d: False
    }

    CAM_FREE = 1
    CAM_FPS = 2
    cam_mode = CAM_FREE

    def __init__(self):
        seed(1337)
        self.free_cam = Camera([2.0, 1.0, 2.0])
        self.free_cam.set_rotation(45.0, 100.0)

        self.init_window()
        self.init_gl()
        self.resize_gl(self.win_res)

        points = [(4.732657104, 0.0), \
                (4.811864754, 0.188118169), \
                (5.049487705, 1.841577869), \
                (5.16829918, 2.148507514), \
                (5.188101093, 3.861372951), \
                (4.782161885, 4.405925546), \
                (4.762359973, 5.049487705), \
                (4.940577186, 5.851465164), \
                (4.910874317, 6.910867486), \
                (4.841567623, 7.2475)]

        self.road = Road(points)

        for i in xrange(30):
            v1 = Vehicle(uniform(95.0, 140.0), self.road, uniform(0.1, 7.0))
            self.road.vehicles.append(v1)

        sep = 0.25
        for i in xrange(int(6.25 / sep)):
            n1 = SensorNode(sep * i + 0.25, self.road, i)
            self.road.sensors.append(n1)

        af = AccidentField(5.0, 0.25, self.road)
        self.road.accidents.append(af)

        self.clock = pygame.time.Clock()

    def init_window(self):
        glutInit()
        pygame.init()
        self.screen = pygame.display.set_mode(
            self.win_res, pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE)
        pygame.display.set_caption(
            'TrafficSim - Press Enter for mouse control of camera')

    def init_gl(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClearDepth(1.0)
        glDepthFunc(GL_LESS)
        glEnable(GL_DEPTH_TEST)

        glFrontFace(GL_CW)
        glCullFace(GL_BACK)
        #glEnable(GL_CULL_FACE)
        glShadeModel(GL_SMOOTH)

        glEnable(GL_COLOR_MATERIAL)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glEnable(GL_LIGHT0)
        glLineWidth(2.0)

        glEnable(GL_TEXTURE_2D)
        self.map_tex = self.load_image("../../img/540.PNG")[0]
        glDisable(GL_TEXTURE_2D)

    def load_image(self, image_path):
        textureSurface = pygame.image.load(image_path)

        textureData = pygame.image.tostring(textureSurface, "RGBA", 1)

        width = textureSurface.get_width()
        height = textureSurface.get_height()

        tex_id = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, tex_id)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                        GL_LINEAR_MIPMAP_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE)

        if hasGLExtension("GL_EXT_texture_filter_anisotropic"):
            max_aniso = glGetFloat(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
                            max_aniso)
        else:
            print 'Anisotropic filtering not supported.'

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
                     GL_UNSIGNED_BYTE, textureData)

        return tex_id, width, height

    def resize_gl(self, res):
        if res[1] == 0:
            res[1] = 1

        glViewport(0, 0, res[0], res[1])
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45.0, float(res[0]) / float(res[1]), 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)

    def update_cam(self):
        delta = self.clock.get_time() * 2e-3
        if self.keys_held[pygame.K_w]:
            self.free_cam.move(delta)
        if self.keys_held[pygame.K_s]:
            self.free_cam.move(-delta)
        if self.keys_held[pygame.K_d]:
            self.free_cam.strafe(-delta)
        if self.keys_held[pygame.K_a]:
            self.free_cam.strafe(delta)

        self.free_cam.update()

    def render(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        self.update_cam()

        glEnable(GL_TEXTURE_2D)
        glDisable(GL_LIGHTING)
        glColor3f(1, 1, 1)
        self.draw_textured_quad(7.2475, 7.2475, self.map_tex)
        glEnable(GL_LIGHTING)
        glDisable(GL_TEXTURE_2D)

        dt = self.clock.get_time() * 0.001
        self.road.update(dt)
        self.draw_axes()
        self.road.draw()

        pygame.display.flip()

    def draw_axes(self, size=1.0):
        glColor3f(1, 1, 0)
        glBegin(GL_LINES)
        glVertex3f(0, 0, 0)
        glVertex3f(size, 0, 0)

        glVertex3f(0, 0, 0)
        glVertex3f(0, size, 0)

        glVertex3f(0, 0, 0)
        glVertex3f(0, 0, size)
        glEnd()

    def draw_textured_quad(self, w, h, tex_id, repeat=1.0):
        glBindTexture(GL_TEXTURE_2D, tex_id)

        aspect = w / h
        glBegin(GL_TRIANGLES)
        glNormal3f(0.0, 1.0, 0.0)
        glTexCoord2f(repeat * aspect, 0.0)
        glVertex3f(0.0, 0.0, 0.0)
        glTexCoord2f(0.0, 0.0)
        glVertex3f(w, 0.0, 0.0)
        glTexCoord2f(0.0, repeat)
        glVertex3f(w, 0.0, h)

        glTexCoord2f(repeat * aspect, 0.0)
        glVertex3f(0.0, 0.0, 0.0)
        glTexCoord2f(0.0, repeat)
        glVertex3f(w, 0.0, h)
        glTexCoord2f(repeat * aspect, repeat)
        glVertex3f(0.0, 0.0, h)
        glEnd()

    def _key_down(self, key):
        self._check_keys_held(key, True)
        if (key == pygame.K_ESCAPE):
            self._running = False
        elif (key == pygame.K_RETURN):
            if self.cam_mode == self.CAM_FREE:
                self.cam_mode = self.CAM_FPS
                pygame.mouse.set_visible(False)
            else:
                self.cam_mode = self.CAM_FREE
                pygame.mouse.set_visible(True)

    def _key_up(self, key):
        self._check_keys_held(key, False)

    def _check_keys_held(self, key, key_down):
        if key_down:
            if key in self.keys_held:
                self.keys_held[key] = True
        else:  # Key Up
            if key in self.keys_held:
                self.keys_held[key] = False

    def _mouse_move(self, pos):
        if self.cam_mode == self.CAM_FPS:
            hw = self.screen.get_width() >> 1
            hh = self.screen.get_height() >> 1
            dx = 0.05 * (pos[0] - hw)
            dy = 0.05 * (pos[1] - hh)
            self.free_cam.mouse_rotate(dx, dy)
            pygame.mouse.set_pos([hw, hh])

    def doEvents(self):
        events = pygame.event.get()

        for e in events:
            if (e.type == pygame.QUIT):
                self._running = False
            elif (e.type == pygame.KEYDOWN):
                self._key_down(e.key)
            elif (e.type == pygame.KEYUP):
                self._key_up(e.key)
            elif (e.type == pygame.VIDEORESIZE):
                self.resize_gl(e.size)
            elif (e.type == pygame.MOUSEMOTION):
                self._mouse_move(e.pos)

    def run(self):
        self._running = True

        while self._running:
            self.clock.tick(self.fps)
            self.doEvents()
            self.render()
            pygame.display.set_caption(
                str(self.free_cam.pos) + ' ' + str(self.free_cam.rot))