Example #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()
Example #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))
Example #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()
Example #6
0
# 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()
plt.show()

while True:
    # 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)

    # reward
    total_reward += r

    # outputs during training
    if steps % 2 == 0 or done:
        print("\naction " + str(["{:+0.2f}".format(x) for x in a]))
        print("step {} total_reward {:+0.2f}".format(steps, total_reward))
Example #7
0
 #   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
   angle=[]
   while True:
     obs, reward, done, info = env.step(action)
     total_reward += reward
     obs=transform(obs).view(1,3,64,64)
     recon_c, mu_c, var_c = model(obs)
     mu, sigma = mu_c, var_c
     #sigma = torch.exp(sigma/2.0)
     epsilon = torch.randn_like(sigma)
     z=mu+sigma*epsilon
     z=z.cuda().view(obs.shape[0],-1).detach()
     action_p=controller(z)
     # action=action.view(-1).data.cpu().numpy().astype('float32')
     #print(action_p)
     action[0]=np.argmax(np.reshape(action_p[0].data.cpu().numpy().astype('float32'),-1))-1
     action[1]=np.argmax(np.reshape(action_p[1].data.cpu().numpy().astype('float32'),-1))
     action[2]=np.argmax(np.reshape(action_p[2].data.cpu().numpy().astype('float32'),-1))
     action=np.array(action)
Example #8
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()
Example #9
0
# 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()
plt.show()

while True:
    # perform step
    s, r, done, speed, _  = env.step(action=a)
    # lane detection
    splines = LD_module.lane_detection(s)
    
    # reward
    total_reward += r

    # outputs during training
    if steps % 2 == 0 or done:
        print("\naction " + str(["{:+0.2f}".format(x) for x in a]))
        print("step {} total_reward {:+0.2f}".format(steps, total_reward))
        LD_module.plot_state_lane(s, steps, fig)
    steps += 1
    env.render()
    
    if done or restart: break
Example #10
0
from gym.envs.box2d.car_racing import CarRacing
from tqdm import tqdm
import numpy as np

if __name__ == "__main__":
    env = CarRacing()
    env.reset()

    a = np.zeros(3)
    for _ in tqdm(range(1000000)):
        env.step(a)
#         env.render()