Ejemplo n.º 1
0
def calculate_score_for_leaderboard():
    """
    Evaluate the performance of the network. This is the function to be used for
    the final ranking on the course-wide leader-board, only with a different set
    of seeds. Better not change it.
    """
    # action variables
    a = np.array([0.0, 0.0, 0.0])

    # init environement
    env = CarRacing()
    env.render()
    env.reset()

    seeds = [
        22597174, 68545857, 75568192, 91140053, 86018367, 49636746, 66759182,
        91294619, 84274995, 31531469
    ]

    total_reward = 0

    for episode in range(10):
        env.seed(seeds[episode])
        observation = env.reset()

        # init modules of the pipeline
        LD_module = LaneDetection(gradient_threshold=25, spline_smoothness=20)
        LatC_module = LateralController(gain_constant=1.8,
                                        damping_constant=0.05)
        LongC_module = LongitudinalController(KD=0.001)

        reward_per_episode = 0
        for t in range(600):
            # perform step
            s, r, done, speed, info = env.step(a)

            # lane detection
            lane1, lane2 = LD_module.lane_detection(s)

            # waypoint and target_speed prediction
            waypoints = waypoint_prediction(lane1, lane2)
            target_speed = target_speed_prediction(waypoints,
                                                   max_speed=60,
                                                   exp_constant=6)

            # control
            a[0] = LatC_module.stanley(waypoints, speed)
            a[1], a[2] = LongC_module.control(speed, target_speed)

            # reward
            reward_per_episode += r

            env.render()

        print('episode %d \t reward %f' % (episode, reward_per_episode))
        total_reward += np.clip(reward_per_episode, 0, np.infty)

    print('---------------------------')
    print(' total score: %f' % (total_reward / 10))
    print('---------------------------')
def multiple_runs(v, on):
    env = CarRacing()
    z_set = []
    action_set = []

    for run in range(MAX_RUNS):
        zs = []
        actions = []
        state = env.reset()
        env.render() # must have!
        # done = False
        counter = 0
        for game_time in range(MAX_GAME_TIME):
            # env.render()
            action = generate_action()
            obs = state_to_1_batch_tensor(state)
            _, _, _, z = v(obs)
            z = z.detach().numpy()
            z = z.reshape(32)
            # print(z.shape)
            # if game_time == 5:
            #     plt.imshow(state)
            #     plt.show()
            #     state = _process_frame(state)
            #     plt.imshow(state)
            #     plt.show()
            zs.append(z)
            actions.append(action)
            state, r, done, _ = env.step(action)

            # print(r)
            print(
                'RUN:{},GT:{},DATA:{}'.format(
                    run, game_time, len(actions)
                )
            )
            # if counter == REST_NUM:
            #
            #     position = np.random.randint(len(env.track))
            #     env.car = Car(env.world, *env.track[position][1:4])
            #     counter = 0
            # counter += 1
        zs = np.array(zs, dtype=np.float16)
        # print(zs.shape)
        actions = np.array(actions, dtype=np.float16)
        # print(actions.shape)

        # np.save(dst + '/' + save_name, frame_and_action)

        # np.savez_compressed(dst + '/' + save_name, action=actions, z=zs)
        z_set.append(zs)
        action_set.append(actions)
    z_set = np.array(z_set)
    # print(z_set.shape)
    action_set = np.array(action_set)
    # print(action_set.shape)
    save_name = name_this + '_{}.npz'.format(on)
    np.savez_compressed(dst + '/' + save_name, action=action_set, z=z_set)
def game_runner():
    from pyglet.window import key
    a = np.array([0.0, 0.0, 0.0])

    def key_press(k, mod):
        global restart
        if k == 0xff0d: restart = True
        if k == key.LEFT: a[0] = -1.0
        if k == key.RIGHT: a[0] = +1.0
        if k == key.UP: a[1] = +1.0
        if k == key.DOWN: a[2] = +0.8

    def key_release(k, mod):
        if k == key.LEFT and a[0] == -1.0: a[0] = 0
        if k == key.RIGHT and a[0] == +1.0: a[0] = 0
        if k == key.UP: a[1] = 0
        if k == key.DOWN: a[2] = 0

    env = CarRacing()
    env.render()
    env.viewer.window.on_key_press = key_press
    env.viewer.window.on_key_release = key_release
    while True:
        env.reset()
        total_reward = 0.0
        steps = 0
        restart = False
        while True:
            s, r, done, info = env.step(a)
            total_reward += r
            if steps == 900:
                print("\n")
                print("_______________________________")
                print("\n")
                print("Human Intelligence Result:")
                print("Total Steps: {}".format(steps))
                print("Total Reward: {:.0f}".format(total_reward))
                print("\n")
                print("_______________________________")
                print("\n")
                break
            steps += 1
            env.render()
            if restart: break
    env.monitor.close()
Ejemplo n.º 4
0
def evaluate():
    """
    """

    # action variables
    a = np.array([0.0, 0.0, 0.0])

    # init environement
    env = CarRacing()
    env.render()
    env.reset()

    for episode in range(5):
        observation = env.reset()
        # init modules of the pipeline
        LD_module = LaneDetection()
        LatC_module = LateralController()
        LongC_module = LongitudinalController()
        reward_per_episode = 0
        for t in range(500):
            # perform step
            s, r, done, speed, info = env.step(a)

            # lane detection
            lane1, lane2 = LD_module.lane_detection(s)

            # waypoint and target_speed prediction
            waypoints = waypoint_prediction(lane1, lane2)
            target_speed = target_speed_prediction(waypoints,
                                                   max_speed=60,
                                                   exp_constant=4.5)

            # control
            a[0] = LatC_module.stanley(waypoints, speed)
            a[1], a[2] = LongC_module.control(speed, target_speed)

            # reward
            reward_per_episode += r
            env.render()

        print('episode %d \t reward %f' % (episode, reward_per_episode))
Ejemplo n.º 5
0
def run_caracing_by_hunman():
    a = np.array([0.0, 0.0, 0.0])

    def key_press(k, mod):
        global restart
        if k == 0xff0d: restart = True
        if k == key.LEFT: a[0] = -1.0
        if k == key.RIGHT: a[0] = +1.0
        if k == key.UP: a[1] = +1.0
        if k == key.DOWN:
            a[2] = +0.8  # set 1.0 for wheels to block to zero rotation

    def key_release(k, mod):
        if k == key.LEFT and a[0] == -1.0: a[0] = 0
        if k == key.RIGHT and a[0] == +1.0: a[0] = 0
        if k == key.UP: a[1] = 0
        if k == key.DOWN: a[2] = 0

    env = CarRacing()
    env.render()
    env.viewer.window.on_key_press = key_press
    env.viewer.window.on_key_release = key_release
    while True:
        env.reset()
        total_reward = 0.0
        steps = 0
        restart = False
        while True:
            s, r, done, info = env.step(a)
            total_reward += r
            if steps % 200 == 0 or done:
                print("\naction " + str(["{:+0.2f}".format(x) for x in a]))
                print("step {} total_reward {:+0.2f}".format(
                    steps, total_reward))
            steps += 1
            env.render()
            if done or restart: break
    env.monitor.close()
Ejemplo n.º 6
0
    global restart
    if k==0xff0d: restart = True
    if k==key.LEFT:  a[0] = -1.0
    if k==key.RIGHT: a[0] = +1.0
    if k==key.UP:    a[1] = +1.0
    if k==key.DOWN:  a[2] = +0.8   # set 1.0 for wheels to block to zero rotation

def key_release(k, mod):
    if k==key.LEFT  and a[0]==-1.0: a[0] = 0
    if k==key.RIGHT and a[0]==+1.0: a[0] = 0
    if k==key.UP:    a[1] = 0
    if k==key.DOWN:  a[2] = 0

# init environement
env = CarRacing()
env.render()
env.viewer.window.on_key_press = key_press
env.viewer.window.on_key_release = key_release
env.reset()

# define variables
total_reward = 0.0
steps = 0
restart = False

# init modules of the pipeline
LD_module = LaneDetection()

# init extra plot
fig = plt.figure()
plt.ion()